blob: b0a3fab98713c41f635af998f4c402fef85d5e8c [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050027#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050028#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050029#include "ctree.h"
30#include "disk-io.h"
31#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040032#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040033#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050034#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040035#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040036#include "free-space-cache.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060037#include "math.h"
Chris Masonfec577f2007-02-26 10:40:21 -050038
Arne Jansen709c0482011-09-12 12:22:57 +020039#undef SCRAMBLE_DELAYED_REFS
40
Miao Xie9e622d62012-01-26 15:01:12 -050041/*
42 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040043 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
44 * if we really need one.
45 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040046 * CHUNK_ALLOC_LIMITED means to only try and allocate one
47 * if we have very few chunks already allocated. This is
48 * used as part of the clustering code to help make sure
49 * we have a good pool of storage to cluster in, without
50 * filling the FS with empty chunks
51 *
Miao Xie9e622d62012-01-26 15:01:12 -050052 * CHUNK_ALLOC_FORCE means it must try to allocate one
53 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040054 */
55enum {
56 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050057 CHUNK_ALLOC_LIMITED = 1,
58 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040059};
60
Josef Bacikfb25e912011-07-26 17:00:46 -040061/*
62 * Control how reservations are dealt with.
63 *
64 * RESERVE_FREE - freeing a reservation.
65 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
66 * ENOSPC accounting
67 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
68 * bytes_may_use as the ENOSPC accounting is done elsewhere
69 */
70enum {
71 RESERVE_FREE = 0,
72 RESERVE_ALLOC = 1,
73 RESERVE_ALLOC_NO_ACCOUNT = 2,
74};
75
Liu Boc53d6132012-12-27 09:01:19 +000076static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040077 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040078static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
79 struct btrfs_root *root,
80 u64 bytenr, u64 num_bytes, u64 parent,
81 u64 root_objectid, u64 owner_objectid,
82 u64 owner_offset, int refs_to_drop,
83 struct btrfs_delayed_extent_op *extra_op);
84static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
85 struct extent_buffer *leaf,
86 struct btrfs_extent_item *ei);
87static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
88 struct btrfs_root *root,
89 u64 parent, u64 root_objectid,
90 u64 flags, u64 owner, u64 offset,
91 struct btrfs_key *ins, int ref_mod);
92static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
93 struct btrfs_root *root,
94 u64 parent, u64 root_objectid,
95 u64 flags, struct btrfs_disk_key *key,
96 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050097static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -040098 struct btrfs_root *extent_root, u64 flags,
99 int force);
Yan Zheng11833d62009-09-11 16:11:19 -0400100static int find_next_key(struct btrfs_path *path, int level,
101 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400102static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
103 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -0400104static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
105 u64 num_bytes, int reserve);
Josef Bacik5d803662013-02-07 16:06:02 -0500106static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
107 u64 num_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500108
Josef Bacik817d52f2009-07-13 21:29:25 -0400109static noinline int
110block_group_cache_done(struct btrfs_block_group_cache *cache)
111{
112 smp_mb();
113 return cache->cached == BTRFS_CACHE_FINISHED;
114}
115
Josef Bacik0f9dd462008-09-23 13:14:11 -0400116static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
117{
118 return (cache->flags & bits) == bits;
119}
120
David Sterba62a45b62011-04-20 15:52:26 +0200121static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000122{
123 atomic_inc(&cache->count);
124}
125
126void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
127{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400128 if (atomic_dec_and_test(&cache->count)) {
129 WARN_ON(cache->pinned > 0);
130 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800131 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000132 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400133 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000134}
135
Josef Bacik0f9dd462008-09-23 13:14:11 -0400136/*
137 * this adds the block group to the fs_info rb tree for the block group
138 * cache
139 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500140static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400141 struct btrfs_block_group_cache *block_group)
142{
143 struct rb_node **p;
144 struct rb_node *parent = NULL;
145 struct btrfs_block_group_cache *cache;
146
147 spin_lock(&info->block_group_cache_lock);
148 p = &info->block_group_cache_tree.rb_node;
149
150 while (*p) {
151 parent = *p;
152 cache = rb_entry(parent, struct btrfs_block_group_cache,
153 cache_node);
154 if (block_group->key.objectid < cache->key.objectid) {
155 p = &(*p)->rb_left;
156 } else if (block_group->key.objectid > cache->key.objectid) {
157 p = &(*p)->rb_right;
158 } else {
159 spin_unlock(&info->block_group_cache_lock);
160 return -EEXIST;
161 }
162 }
163
164 rb_link_node(&block_group->cache_node, parent, p);
165 rb_insert_color(&block_group->cache_node,
166 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000167
168 if (info->first_logical_byte > block_group->key.objectid)
169 info->first_logical_byte = block_group->key.objectid;
170
Josef Bacik0f9dd462008-09-23 13:14:11 -0400171 spin_unlock(&info->block_group_cache_lock);
172
173 return 0;
174}
175
176/*
177 * This will return the block group at or after bytenr if contains is 0, else
178 * it will return the block group that contains the bytenr
179 */
180static struct btrfs_block_group_cache *
181block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
182 int contains)
183{
184 struct btrfs_block_group_cache *cache, *ret = NULL;
185 struct rb_node *n;
186 u64 end, start;
187
188 spin_lock(&info->block_group_cache_lock);
189 n = info->block_group_cache_tree.rb_node;
190
191 while (n) {
192 cache = rb_entry(n, struct btrfs_block_group_cache,
193 cache_node);
194 end = cache->key.objectid + cache->key.offset - 1;
195 start = cache->key.objectid;
196
197 if (bytenr < start) {
198 if (!contains && (!ret || start < ret->key.objectid))
199 ret = cache;
200 n = n->rb_left;
201 } else if (bytenr > start) {
202 if (contains && bytenr <= end) {
203 ret = cache;
204 break;
205 }
206 n = n->rb_right;
207 } else {
208 ret = cache;
209 break;
210 }
211 }
Liu Boa1897fd2012-12-27 09:01:23 +0000212 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000213 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000214 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
215 info->first_logical_byte = ret->key.objectid;
216 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400217 spin_unlock(&info->block_group_cache_lock);
218
219 return ret;
220}
221
Yan Zheng11833d62009-09-11 16:11:19 -0400222static int add_excluded_extent(struct btrfs_root *root,
223 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400224{
Yan Zheng11833d62009-09-11 16:11:19 -0400225 u64 end = start + num_bytes - 1;
226 set_extent_bits(&root->fs_info->freed_extents[0],
227 start, end, EXTENT_UPTODATE, GFP_NOFS);
228 set_extent_bits(&root->fs_info->freed_extents[1],
229 start, end, EXTENT_UPTODATE, GFP_NOFS);
230 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400231}
232
Yan Zheng11833d62009-09-11 16:11:19 -0400233static void free_excluded_extents(struct btrfs_root *root,
234 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400235{
Yan Zheng11833d62009-09-11 16:11:19 -0400236 u64 start, end;
237
238 start = cache->key.objectid;
239 end = start + cache->key.offset - 1;
240
241 clear_extent_bits(&root->fs_info->freed_extents[0],
242 start, end, EXTENT_UPTODATE, GFP_NOFS);
243 clear_extent_bits(&root->fs_info->freed_extents[1],
244 start, end, EXTENT_UPTODATE, GFP_NOFS);
245}
246
247static int exclude_super_stripes(struct btrfs_root *root,
248 struct btrfs_block_group_cache *cache)
249{
Josef Bacik817d52f2009-07-13 21:29:25 -0400250 u64 bytenr;
251 u64 *logical;
252 int stripe_len;
253 int i, nr, ret;
254
Yan, Zheng06b23312009-11-26 09:31:11 +0000255 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
256 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
257 cache->bytes_super += stripe_len;
258 ret = add_excluded_extent(root, cache->key.objectid,
259 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400260 if (ret)
261 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000262 }
263
Josef Bacik817d52f2009-07-13 21:29:25 -0400264 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
265 bytenr = btrfs_sb_offset(i);
266 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
267 cache->key.objectid, bytenr,
268 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400269 if (ret)
270 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400271
Josef Bacik817d52f2009-07-13 21:29:25 -0400272 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400273 u64 start, len;
274
275 if (logical[nr] > cache->key.objectid +
276 cache->key.offset)
277 continue;
278
279 if (logical[nr] + stripe_len <= cache->key.objectid)
280 continue;
281
282 start = logical[nr];
283 if (start < cache->key.objectid) {
284 start = cache->key.objectid;
285 len = (logical[nr] + stripe_len) - start;
286 } else {
287 len = min_t(u64, stripe_len,
288 cache->key.objectid +
289 cache->key.offset - start);
290 }
291
292 cache->bytes_super += len;
293 ret = add_excluded_extent(root, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400294 if (ret) {
295 kfree(logical);
296 return ret;
297 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400298 }
Yan Zheng11833d62009-09-11 16:11:19 -0400299
Josef Bacik817d52f2009-07-13 21:29:25 -0400300 kfree(logical);
301 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400302 return 0;
303}
304
Yan Zheng11833d62009-09-11 16:11:19 -0400305static struct btrfs_caching_control *
306get_caching_control(struct btrfs_block_group_cache *cache)
307{
308 struct btrfs_caching_control *ctl;
309
310 spin_lock(&cache->lock);
311 if (cache->cached != BTRFS_CACHE_STARTED) {
312 spin_unlock(&cache->lock);
313 return NULL;
314 }
315
Josef Bacikdde5abe2010-09-16 16:17:03 -0400316 /* We're loading it the fast way, so we don't have a caching_ctl. */
317 if (!cache->caching_ctl) {
318 spin_unlock(&cache->lock);
319 return NULL;
320 }
321
Yan Zheng11833d62009-09-11 16:11:19 -0400322 ctl = cache->caching_ctl;
323 atomic_inc(&ctl->count);
324 spin_unlock(&cache->lock);
325 return ctl;
326}
327
328static void put_caching_control(struct btrfs_caching_control *ctl)
329{
330 if (atomic_dec_and_test(&ctl->count))
331 kfree(ctl);
332}
333
Josef Bacik0f9dd462008-09-23 13:14:11 -0400334/*
335 * this is only called by cache_block_group, since we could have freed extents
336 * we need to check the pinned_extents for any extents that can't be used yet
337 * since their free space will be released as soon as the transaction commits.
338 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400339static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400340 struct btrfs_fs_info *info, u64 start, u64 end)
341{
Josef Bacik817d52f2009-07-13 21:29:25 -0400342 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400343 int ret;
344
345 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400346 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400347 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400348 EXTENT_DIRTY | EXTENT_UPTODATE,
349 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400350 if (ret)
351 break;
352
Yan, Zheng06b23312009-11-26 09:31:11 +0000353 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400354 start = extent_end + 1;
355 } else if (extent_start > start && extent_start < end) {
356 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400357 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500358 ret = btrfs_add_free_space(block_group, start,
359 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100360 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400361 start = extent_end + 1;
362 } else {
363 break;
364 }
365 }
366
367 if (start < end) {
368 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400369 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500370 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100371 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400372 }
373
Josef Bacik817d52f2009-07-13 21:29:25 -0400374 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400375}
376
Josef Bacikbab39bf2011-06-30 14:42:28 -0400377static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400378{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400379 struct btrfs_block_group_cache *block_group;
380 struct btrfs_fs_info *fs_info;
381 struct btrfs_caching_control *caching_ctl;
382 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400383 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400384 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400385 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400386 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400387 u64 last = 0;
388 u32 nritems;
389 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400390
Josef Bacikbab39bf2011-06-30 14:42:28 -0400391 caching_ctl = container_of(work, struct btrfs_caching_control, work);
392 block_group = caching_ctl->block_group;
393 fs_info = block_group->fs_info;
394 extent_root = fs_info->extent_root;
395
Chris Masone37c9e62007-05-09 20:13:14 -0400396 path = btrfs_alloc_path();
397 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400398 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400399
Josef Bacik817d52f2009-07-13 21:29:25 -0400400 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400401
Chris Mason5cd57b22008-06-25 16:01:30 -0400402 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400403 * We don't want to deadlock with somebody trying to allocate a new
404 * extent for the extent root while also trying to search the extent
405 * root to add free space. So we skip locking and search the commit
406 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400407 */
408 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400409 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400410 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400411
Yan Zhenge4404d62008-12-12 10:03:26 -0500412 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400413 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400414 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400415again:
Yan Zheng11833d62009-09-11 16:11:19 -0400416 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400417 /* need to make sure the commit_root doesn't disappear */
418 down_read(&fs_info->extent_commit_sem);
419
Yan Zheng11833d62009-09-11 16:11:19 -0400420 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400421 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400422 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500423
Yan Zheng11833d62009-09-11 16:11:19 -0400424 leaf = path->nodes[0];
425 nritems = btrfs_header_nritems(leaf);
426
Chris Masond3977122009-01-05 21:25:51 -0500427 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200428 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400429 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400430 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400431 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400432
Yan Zheng11833d62009-09-11 16:11:19 -0400433 if (path->slots[0] < nritems) {
434 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
435 } else {
436 ret = find_next_key(path, 0, &key);
437 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400438 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400439
Josef Bacik0a3896d2013-04-19 14:37:26 -0400440 if (need_resched()) {
Josef Bacik589d8ad2011-05-11 17:30:53 -0400441 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400442 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400443 up_read(&fs_info->extent_commit_sem);
444 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400445 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400446 goto again;
447 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400448
449 ret = btrfs_next_leaf(extent_root, path);
450 if (ret < 0)
451 goto err;
452 if (ret)
453 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400454 leaf = path->nodes[0];
455 nritems = btrfs_header_nritems(leaf);
456 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400457 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400458
Yan Zheng11833d62009-09-11 16:11:19 -0400459 if (key.objectid < block_group->key.objectid) {
460 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400461 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400462 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400463
Chris Masone37c9e62007-05-09 20:13:14 -0400464 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400465 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400466 break;
Yan7d7d6062007-09-14 16:15:28 -0400467
Josef Bacik3173a182013-03-07 14:22:04 -0500468 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
469 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400470 total_found += add_new_free_space(block_group,
471 fs_info, last,
472 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500473 if (key.type == BTRFS_METADATA_ITEM_KEY)
474 last = key.objectid +
475 fs_info->tree_root->leafsize;
476 else
477 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400478
Yan Zheng11833d62009-09-11 16:11:19 -0400479 if (total_found > (1024 * 1024 * 2)) {
480 total_found = 0;
481 wake_up(&caching_ctl->wait);
482 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400483 }
Chris Masone37c9e62007-05-09 20:13:14 -0400484 path->slots[0]++;
485 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400486 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400487
488 total_found += add_new_free_space(block_group, fs_info, last,
489 block_group->key.objectid +
490 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400491 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400492
493 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400494 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400495 block_group->cached = BTRFS_CACHE_FINISHED;
496 spin_unlock(&block_group->lock);
497
Chris Mason54aa1f42007-06-22 14:16:25 -0400498err:
Chris Masone37c9e62007-05-09 20:13:14 -0400499 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400500 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400501
Yan Zheng11833d62009-09-11 16:11:19 -0400502 free_excluded_extents(extent_root, block_group);
503
504 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400505out:
Yan Zheng11833d62009-09-11 16:11:19 -0400506 wake_up(&caching_ctl->wait);
507
508 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000509 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400510}
511
Josef Bacik9d66e232010-08-25 16:54:15 -0400512static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400513 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400514{
Josef Bacik291c7d22011-11-14 13:52:14 -0500515 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400516 struct btrfs_fs_info *fs_info = cache->fs_info;
517 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400518 int ret = 0;
519
Josef Bacik291c7d22011-11-14 13:52:14 -0500520 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100521 if (!caching_ctl)
522 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500523
524 INIT_LIST_HEAD(&caching_ctl->list);
525 mutex_init(&caching_ctl->mutex);
526 init_waitqueue_head(&caching_ctl->wait);
527 caching_ctl->block_group = cache;
528 caching_ctl->progress = cache->key.objectid;
529 atomic_set(&caching_ctl->count, 1);
530 caching_ctl->work.func = caching_thread;
531
532 spin_lock(&cache->lock);
533 /*
534 * This should be a rare occasion, but this could happen I think in the
535 * case where one thread starts to load the space cache info, and then
536 * some other thread starts a transaction commit which tries to do an
537 * allocation while the other thread is still loading the space cache
538 * info. The previous loop should have kept us from choosing this block
539 * group, but if we've moved to the state where we will wait on caching
540 * block groups we need to first check if we're doing a fast load here,
541 * so we can wait for it to finish, otherwise we could end up allocating
542 * from a block group who's cache gets evicted for one reason or
543 * another.
544 */
545 while (cache->cached == BTRFS_CACHE_FAST) {
546 struct btrfs_caching_control *ctl;
547
548 ctl = cache->caching_ctl;
549 atomic_inc(&ctl->count);
550 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
551 spin_unlock(&cache->lock);
552
553 schedule();
554
555 finish_wait(&ctl->wait, &wait);
556 put_caching_control(ctl);
557 spin_lock(&cache->lock);
558 }
559
560 if (cache->cached != BTRFS_CACHE_NO) {
561 spin_unlock(&cache->lock);
562 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400563 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500564 }
565 WARN_ON(cache->caching_ctl);
566 cache->caching_ctl = caching_ctl;
567 cache->cached = BTRFS_CACHE_FAST;
568 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400569
Josef Bacikd53ba472012-04-12 16:03:57 -0400570 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400571 ret = load_free_space_cache(fs_info, cache);
572
573 spin_lock(&cache->lock);
574 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500575 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400576 cache->cached = BTRFS_CACHE_FINISHED;
577 cache->last_byte_to_unpin = (u64)-1;
578 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500579 if (load_cache_only) {
580 cache->caching_ctl = NULL;
581 cache->cached = BTRFS_CACHE_NO;
582 } else {
583 cache->cached = BTRFS_CACHE_STARTED;
584 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400585 }
586 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500587 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000588 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500589 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000590 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400591 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000592 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500593 } else {
594 /*
595 * We are not going to do the fast caching, set cached to the
596 * appropriate value and wakeup any waiters.
597 */
598 spin_lock(&cache->lock);
599 if (load_cache_only) {
600 cache->caching_ctl = NULL;
601 cache->cached = BTRFS_CACHE_NO;
602 } else {
603 cache->cached = BTRFS_CACHE_STARTED;
604 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400605 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500606 wake_up(&caching_ctl->wait);
607 }
608
609 if (load_cache_only) {
610 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400611 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400612 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400613
Yan Zheng11833d62009-09-11 16:11:19 -0400614 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500615 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400616 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
617 up_write(&fs_info->extent_commit_sem);
618
Josef Bacik11dfe352009-11-13 20:12:59 +0000619 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400620
Josef Bacikbab39bf2011-06-30 14:42:28 -0400621 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400622
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400623 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400624}
625
Josef Bacik0f9dd462008-09-23 13:14:11 -0400626/*
627 * return the block group that starts at or after bytenr
628 */
Chris Masond3977122009-01-05 21:25:51 -0500629static struct btrfs_block_group_cache *
630btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400631{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400632 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400633
Josef Bacik0f9dd462008-09-23 13:14:11 -0400634 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400635
Josef Bacik0f9dd462008-09-23 13:14:11 -0400636 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400637}
638
Josef Bacik0f9dd462008-09-23 13:14:11 -0400639/*
Sankar P9f556842009-05-14 13:52:22 -0400640 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400641 */
Chris Masond3977122009-01-05 21:25:51 -0500642struct btrfs_block_group_cache *btrfs_lookup_block_group(
643 struct btrfs_fs_info *info,
644 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400645{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400646 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400647
Josef Bacik0f9dd462008-09-23 13:14:11 -0400648 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400649
Josef Bacik0f9dd462008-09-23 13:14:11 -0400650 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400651}
Chris Mason0b86a832008-03-24 15:01:56 -0400652
Josef Bacik0f9dd462008-09-23 13:14:11 -0400653static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
654 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400655{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400656 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400657 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400658
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200659 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400660
Chris Mason4184ea72009-03-10 12:39:20 -0400661 rcu_read_lock();
662 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400663 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400664 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400665 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400666 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400667 }
Chris Mason4184ea72009-03-10 12:39:20 -0400668 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400669 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400670}
671
Chris Mason4184ea72009-03-10 12:39:20 -0400672/*
673 * after adding space to the filesystem, we need to clear the full flags
674 * on all the space infos.
675 */
676void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
677{
678 struct list_head *head = &info->space_info;
679 struct btrfs_space_info *found;
680
681 rcu_read_lock();
682 list_for_each_entry_rcu(found, head, list)
683 found->full = 0;
684 rcu_read_unlock();
685}
686
Yan Zhengd2fb3432008-12-11 16:30:39 -0500687u64 btrfs_find_block_group(struct btrfs_root *root,
688 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400689{
Chris Mason96b51792007-10-15 16:15:19 -0400690 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400691 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500692 u64 last = max(search_hint, search_start);
693 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400694 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500695 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400696 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400697again:
Zheng Yane8569812008-09-26 10:05:48 -0400698 while (1) {
699 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400700 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400701 break;
Chris Mason96b51792007-10-15 16:15:19 -0400702
Chris Masonc286ac42008-07-22 23:06:41 -0400703 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400704 last = cache->key.objectid + cache->key.offset;
705 used = btrfs_block_group_used(&cache->item);
706
Yan Zhengd2fb3432008-12-11 16:30:39 -0500707 if ((full_search || !cache->ro) &&
708 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400709 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500710 div_factor(cache->key.offset, factor)) {
711 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400712 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400713 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400714 goto found;
715 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400716 }
Chris Masonc286ac42008-07-22 23:06:41 -0400717 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400718 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400719 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400720 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400721 if (!wrapped) {
722 last = search_start;
723 wrapped = 1;
724 goto again;
725 }
726 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400727 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400728 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400729 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400730 goto again;
731 }
Chris Masonbe744172007-05-06 10:15:01 -0400732found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500733 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400734}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400735
Chris Masone02119d2008-09-05 16:13:11 -0400736/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400737int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400738{
739 int ret;
740 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400741 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400742
Zheng Yan31840ae2008-09-23 13:14:14 -0400743 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700744 if (!path)
745 return -ENOMEM;
746
Chris Masone02119d2008-09-05 16:13:11 -0400747 key.objectid = start;
748 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500749 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400750 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
751 0, 0);
Josef Bacik3173a182013-03-07 14:22:04 -0500752 if (ret > 0) {
753 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
754 if (key.objectid == start &&
755 key.type == BTRFS_METADATA_ITEM_KEY)
756 ret = 0;
757 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400758 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500759 return ret;
760}
761
Chris Masond8d5f3e2007-12-11 12:42:00 -0500762/*
Josef Bacik3173a182013-03-07 14:22:04 -0500763 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400764 *
765 * the head node for delayed ref is used to store the sum of all the
766 * reference count modifications queued up in the rbtree. the head
767 * node may also store the extent flags to set. This way you can check
768 * to see what the reference count and extent flags would be if all of
769 * the delayed refs are not processed.
770 */
771int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
772 struct btrfs_root *root, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500773 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400774{
775 struct btrfs_delayed_ref_head *head;
776 struct btrfs_delayed_ref_root *delayed_refs;
777 struct btrfs_path *path;
778 struct btrfs_extent_item *ei;
779 struct extent_buffer *leaf;
780 struct btrfs_key key;
781 u32 item_size;
782 u64 num_refs;
783 u64 extent_flags;
784 int ret;
785
Josef Bacik3173a182013-03-07 14:22:04 -0500786 /*
787 * If we don't have skinny metadata, don't bother doing anything
788 * different
789 */
790 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
791 offset = root->leafsize;
792 metadata = 0;
793 }
794
Yan, Zhenga22285a2010-05-16 10:48:46 -0400795 path = btrfs_alloc_path();
796 if (!path)
797 return -ENOMEM;
798
Josef Bacik3173a182013-03-07 14:22:04 -0500799 if (metadata) {
800 key.objectid = bytenr;
801 key.type = BTRFS_METADATA_ITEM_KEY;
802 key.offset = offset;
803 } else {
804 key.objectid = bytenr;
805 key.type = BTRFS_EXTENT_ITEM_KEY;
806 key.offset = offset;
807 }
808
Yan, Zhenga22285a2010-05-16 10:48:46 -0400809 if (!trans) {
810 path->skip_locking = 1;
811 path->search_commit_root = 1;
812 }
813again:
814 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
815 &key, path, 0, 0);
816 if (ret < 0)
817 goto out_free;
818
Josef Bacik3173a182013-03-07 14:22:04 -0500819 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
820 key.type = BTRFS_EXTENT_ITEM_KEY;
821 key.offset = root->leafsize;
822 btrfs_release_path(path);
823 goto again;
824 }
825
Yan, Zhenga22285a2010-05-16 10:48:46 -0400826 if (ret == 0) {
827 leaf = path->nodes[0];
828 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
829 if (item_size >= sizeof(*ei)) {
830 ei = btrfs_item_ptr(leaf, path->slots[0],
831 struct btrfs_extent_item);
832 num_refs = btrfs_extent_refs(leaf, ei);
833 extent_flags = btrfs_extent_flags(leaf, ei);
834 } else {
835#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
836 struct btrfs_extent_item_v0 *ei0;
837 BUG_ON(item_size != sizeof(*ei0));
838 ei0 = btrfs_item_ptr(leaf, path->slots[0],
839 struct btrfs_extent_item_v0);
840 num_refs = btrfs_extent_refs_v0(leaf, ei0);
841 /* FIXME: this isn't correct for data */
842 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
843#else
844 BUG();
845#endif
846 }
847 BUG_ON(num_refs == 0);
848 } else {
849 num_refs = 0;
850 extent_flags = 0;
851 ret = 0;
852 }
853
854 if (!trans)
855 goto out;
856
857 delayed_refs = &trans->transaction->delayed_refs;
858 spin_lock(&delayed_refs->lock);
859 head = btrfs_find_delayed_ref_head(trans, bytenr);
860 if (head) {
861 if (!mutex_trylock(&head->mutex)) {
862 atomic_inc(&head->node.refs);
863 spin_unlock(&delayed_refs->lock);
864
David Sterbab3b4aa72011-04-21 01:20:15 +0200865 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400866
David Sterba8cc33e52011-05-02 15:29:25 +0200867 /*
868 * Mutex was contended, block until it's released and try
869 * again
870 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400871 mutex_lock(&head->mutex);
872 mutex_unlock(&head->mutex);
873 btrfs_put_delayed_ref(&head->node);
874 goto again;
875 }
876 if (head->extent_op && head->extent_op->update_flags)
877 extent_flags |= head->extent_op->flags_to_set;
878 else
879 BUG_ON(num_refs == 0);
880
881 num_refs += head->node.ref_mod;
882 mutex_unlock(&head->mutex);
883 }
884 spin_unlock(&delayed_refs->lock);
885out:
886 WARN_ON(num_refs == 0);
887 if (refs)
888 *refs = num_refs;
889 if (flags)
890 *flags = extent_flags;
891out_free:
892 btrfs_free_path(path);
893 return ret;
894}
895
896/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500897 * Back reference rules. Back refs have three main goals:
898 *
899 * 1) differentiate between all holders of references to an extent so that
900 * when a reference is dropped we can make sure it was a valid reference
901 * before freeing the extent.
902 *
903 * 2) Provide enough information to quickly find the holders of an extent
904 * if we notice a given block is corrupted or bad.
905 *
906 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
907 * maintenance. This is actually the same as #2, but with a slightly
908 * different use case.
909 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400910 * There are two kinds of back refs. The implicit back refs is optimized
911 * for pointers in non-shared tree blocks. For a given pointer in a block,
912 * back refs of this kind provide information about the block's owner tree
913 * and the pointer's key. These information allow us to find the block by
914 * b-tree searching. The full back refs is for pointers in tree blocks not
915 * referenced by their owner trees. The location of tree block is recorded
916 * in the back refs. Actually the full back refs is generic, and can be
917 * used in all cases the implicit back refs is used. The major shortcoming
918 * of the full back refs is its overhead. Every time a tree block gets
919 * COWed, we have to update back refs entry for all pointers in it.
920 *
921 * For a newly allocated tree block, we use implicit back refs for
922 * pointers in it. This means most tree related operations only involve
923 * implicit back refs. For a tree block created in old transaction, the
924 * only way to drop a reference to it is COW it. So we can detect the
925 * event that tree block loses its owner tree's reference and do the
926 * back refs conversion.
927 *
928 * When a tree block is COW'd through a tree, there are four cases:
929 *
930 * The reference count of the block is one and the tree is the block's
931 * owner tree. Nothing to do in this case.
932 *
933 * The reference count of the block is one and the tree is not the
934 * block's owner tree. In this case, full back refs is used for pointers
935 * in the block. Remove these full back refs, add implicit back refs for
936 * every pointers in the new block.
937 *
938 * The reference count of the block is greater than one and the tree is
939 * the block's owner tree. In this case, implicit back refs is used for
940 * pointers in the block. Add full back refs for every pointers in the
941 * block, increase lower level extents' reference counts. The original
942 * implicit back refs are entailed to the new block.
943 *
944 * The reference count of the block is greater than one and the tree is
945 * not the block's owner tree. Add implicit back refs for every pointer in
946 * the new block, increase lower level extents' reference count.
947 *
948 * Back Reference Key composing:
949 *
950 * The key objectid corresponds to the first byte in the extent,
951 * The key type is used to differentiate between types of back refs.
952 * There are different meanings of the key offset for different types
953 * of back refs.
954 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500955 * File extents can be referenced by:
956 *
957 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400958 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500959 * - different offsets inside a file (bookend extents in file.c)
960 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400961 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500962 *
963 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500964 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400965 * - original offset in the file
966 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400967 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 * The key offset for the implicit back refs is hash of the first
969 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500970 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400971 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500972 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400973 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500974 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400975 * The key offset for the implicit back refs is the first byte of
976 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500977 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400978 * When a file extent is allocated, The implicit back refs is used.
979 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500980 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400981 * (root_key.objectid, inode objectid, offset in file, 1)
982 *
983 * When a file extent is removed file truncation, we find the
984 * corresponding implicit back refs and check the following fields:
985 *
986 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500987 *
988 * Btree extents can be referenced by:
989 *
990 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500991 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400992 * Both the implicit back refs and the full back refs for tree blocks
993 * only consist of key. The key offset for the implicit back refs is
994 * objectid of block's owner tree. The key offset for the full back refs
995 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500996 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400997 * When implicit back refs is used, information about the lowest key and
998 * level of the tree block are required. These information are stored in
999 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001000 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001001
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1003static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
1004 struct btrfs_root *root,
1005 struct btrfs_path *path,
1006 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -05001007{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001008 struct btrfs_extent_item *item;
1009 struct btrfs_extent_item_v0 *ei0;
1010 struct btrfs_extent_ref_v0 *ref0;
1011 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001012 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013 struct btrfs_key key;
1014 struct btrfs_key found_key;
1015 u32 new_size = sizeof(*item);
1016 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001017 int ret;
1018
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001019 leaf = path->nodes[0];
1020 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001021
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001022 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1023 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1024 struct btrfs_extent_item_v0);
1025 refs = btrfs_extent_refs_v0(leaf, ei0);
1026
1027 if (owner == (u64)-1) {
1028 while (1) {
1029 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1030 ret = btrfs_next_leaf(root, path);
1031 if (ret < 0)
1032 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001033 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001034 leaf = path->nodes[0];
1035 }
1036 btrfs_item_key_to_cpu(leaf, &found_key,
1037 path->slots[0]);
1038 BUG_ON(key.objectid != found_key.objectid);
1039 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1040 path->slots[0]++;
1041 continue;
1042 }
1043 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1044 struct btrfs_extent_ref_v0);
1045 owner = btrfs_ref_objectid_v0(leaf, ref0);
1046 break;
1047 }
1048 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001049 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001050
1051 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1052 new_size += sizeof(*bi);
1053
1054 new_size -= sizeof(*ei0);
1055 ret = btrfs_search_slot(trans, root, &key, path,
1056 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001057 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001058 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001059 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001060
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001061 btrfs_extend_item(root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001062
1063 leaf = path->nodes[0];
1064 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1065 btrfs_set_extent_refs(leaf, item, refs);
1066 /* FIXME: get real generation */
1067 btrfs_set_extent_generation(leaf, item, 0);
1068 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1069 btrfs_set_extent_flags(leaf, item,
1070 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1071 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1072 bi = (struct btrfs_tree_block_info *)(item + 1);
1073 /* FIXME: get first key of the block */
1074 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1075 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1076 } else {
1077 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1078 }
1079 btrfs_mark_buffer_dirty(leaf);
1080 return 0;
1081}
1082#endif
1083
1084static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1085{
1086 u32 high_crc = ~(u32)0;
1087 u32 low_crc = ~(u32)0;
1088 __le64 lenum;
1089
1090 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001091 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001092 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001093 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001094 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001095 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001096
1097 return ((u64)high_crc << 31) ^ (u64)low_crc;
1098}
1099
1100static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1101 struct btrfs_extent_data_ref *ref)
1102{
1103 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1104 btrfs_extent_data_ref_objectid(leaf, ref),
1105 btrfs_extent_data_ref_offset(leaf, ref));
1106}
1107
1108static int match_extent_data_ref(struct extent_buffer *leaf,
1109 struct btrfs_extent_data_ref *ref,
1110 u64 root_objectid, u64 owner, u64 offset)
1111{
1112 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1113 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1114 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1115 return 0;
1116 return 1;
1117}
1118
1119static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1120 struct btrfs_root *root,
1121 struct btrfs_path *path,
1122 u64 bytenr, u64 parent,
1123 u64 root_objectid,
1124 u64 owner, u64 offset)
1125{
1126 struct btrfs_key key;
1127 struct btrfs_extent_data_ref *ref;
1128 struct extent_buffer *leaf;
1129 u32 nritems;
1130 int ret;
1131 int recow;
1132 int err = -ENOENT;
1133
1134 key.objectid = bytenr;
1135 if (parent) {
1136 key.type = BTRFS_SHARED_DATA_REF_KEY;
1137 key.offset = parent;
1138 } else {
1139 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1140 key.offset = hash_extent_data_ref(root_objectid,
1141 owner, offset);
1142 }
1143again:
1144 recow = 0;
1145 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1146 if (ret < 0) {
1147 err = ret;
1148 goto fail;
1149 }
1150
1151 if (parent) {
1152 if (!ret)
1153 return 0;
1154#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1155 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001156 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001157 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1158 if (ret < 0) {
1159 err = ret;
1160 goto fail;
1161 }
1162 if (!ret)
1163 return 0;
1164#endif
1165 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001166 }
1167
1168 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001169 nritems = btrfs_header_nritems(leaf);
1170 while (1) {
1171 if (path->slots[0] >= nritems) {
1172 ret = btrfs_next_leaf(root, path);
1173 if (ret < 0)
1174 err = ret;
1175 if (ret)
1176 goto fail;
1177
1178 leaf = path->nodes[0];
1179 nritems = btrfs_header_nritems(leaf);
1180 recow = 1;
1181 }
1182
1183 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1184 if (key.objectid != bytenr ||
1185 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1186 goto fail;
1187
1188 ref = btrfs_item_ptr(leaf, path->slots[0],
1189 struct btrfs_extent_data_ref);
1190
1191 if (match_extent_data_ref(leaf, ref, root_objectid,
1192 owner, offset)) {
1193 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001194 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001195 goto again;
1196 }
1197 err = 0;
1198 break;
1199 }
1200 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001201 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001202fail:
1203 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001204}
1205
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001206static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1207 struct btrfs_root *root,
1208 struct btrfs_path *path,
1209 u64 bytenr, u64 parent,
1210 u64 root_objectid, u64 owner,
1211 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001212{
1213 struct btrfs_key key;
1214 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001215 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001216 u32 num_refs;
1217 int ret;
1218
1219 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001220 if (parent) {
1221 key.type = BTRFS_SHARED_DATA_REF_KEY;
1222 key.offset = parent;
1223 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001224 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001225 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1226 key.offset = hash_extent_data_ref(root_objectid,
1227 owner, offset);
1228 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001229 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001230
1231 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1232 if (ret && ret != -EEXIST)
1233 goto fail;
1234
1235 leaf = path->nodes[0];
1236 if (parent) {
1237 struct btrfs_shared_data_ref *ref;
1238 ref = btrfs_item_ptr(leaf, path->slots[0],
1239 struct btrfs_shared_data_ref);
1240 if (ret == 0) {
1241 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1242 } else {
1243 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1244 num_refs += refs_to_add;
1245 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1246 }
1247 } else {
1248 struct btrfs_extent_data_ref *ref;
1249 while (ret == -EEXIST) {
1250 ref = btrfs_item_ptr(leaf, path->slots[0],
1251 struct btrfs_extent_data_ref);
1252 if (match_extent_data_ref(leaf, ref, root_objectid,
1253 owner, offset))
1254 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001255 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001256 key.offset++;
1257 ret = btrfs_insert_empty_item(trans, root, path, &key,
1258 size);
1259 if (ret && ret != -EEXIST)
1260 goto fail;
1261
1262 leaf = path->nodes[0];
1263 }
1264 ref = btrfs_item_ptr(leaf, path->slots[0],
1265 struct btrfs_extent_data_ref);
1266 if (ret == 0) {
1267 btrfs_set_extent_data_ref_root(leaf, ref,
1268 root_objectid);
1269 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1270 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1271 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1272 } else {
1273 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1274 num_refs += refs_to_add;
1275 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1276 }
1277 }
1278 btrfs_mark_buffer_dirty(leaf);
1279 ret = 0;
1280fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001281 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001282 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001283}
1284
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001285static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1286 struct btrfs_root *root,
1287 struct btrfs_path *path,
1288 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001289{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001290 struct btrfs_key key;
1291 struct btrfs_extent_data_ref *ref1 = NULL;
1292 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001293 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001294 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001295 int ret = 0;
1296
1297 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001298 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1299
1300 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1301 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1302 struct btrfs_extent_data_ref);
1303 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1304 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1305 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1306 struct btrfs_shared_data_ref);
1307 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1308#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1309 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1310 struct btrfs_extent_ref_v0 *ref0;
1311 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1312 struct btrfs_extent_ref_v0);
1313 num_refs = btrfs_ref_count_v0(leaf, ref0);
1314#endif
1315 } else {
1316 BUG();
1317 }
1318
Chris Mason56bec292009-03-13 10:10:06 -04001319 BUG_ON(num_refs < refs_to_drop);
1320 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001321
Zheng Yan31840ae2008-09-23 13:14:14 -04001322 if (num_refs == 0) {
1323 ret = btrfs_del_item(trans, root, path);
1324 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001325 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1326 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1327 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1328 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1329#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1330 else {
1331 struct btrfs_extent_ref_v0 *ref0;
1332 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1333 struct btrfs_extent_ref_v0);
1334 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1335 }
1336#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001337 btrfs_mark_buffer_dirty(leaf);
1338 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001339 return ret;
1340}
1341
1342static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1343 struct btrfs_path *path,
1344 struct btrfs_extent_inline_ref *iref)
1345{
1346 struct btrfs_key key;
1347 struct extent_buffer *leaf;
1348 struct btrfs_extent_data_ref *ref1;
1349 struct btrfs_shared_data_ref *ref2;
1350 u32 num_refs = 0;
1351
1352 leaf = path->nodes[0];
1353 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1354 if (iref) {
1355 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1356 BTRFS_EXTENT_DATA_REF_KEY) {
1357 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1358 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1359 } else {
1360 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1361 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1362 }
1363 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1364 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1365 struct btrfs_extent_data_ref);
1366 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1367 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1368 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1369 struct btrfs_shared_data_ref);
1370 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1371#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1372 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1373 struct btrfs_extent_ref_v0 *ref0;
1374 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1375 struct btrfs_extent_ref_v0);
1376 num_refs = btrfs_ref_count_v0(leaf, ref0);
1377#endif
1378 } else {
1379 WARN_ON(1);
1380 }
1381 return num_refs;
1382}
1383
1384static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1385 struct btrfs_root *root,
1386 struct btrfs_path *path,
1387 u64 bytenr, u64 parent,
1388 u64 root_objectid)
1389{
1390 struct btrfs_key key;
1391 int ret;
1392
1393 key.objectid = bytenr;
1394 if (parent) {
1395 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1396 key.offset = parent;
1397 } else {
1398 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1399 key.offset = root_objectid;
1400 }
1401
1402 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1403 if (ret > 0)
1404 ret = -ENOENT;
1405#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1406 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001407 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001408 key.type = BTRFS_EXTENT_REF_V0_KEY;
1409 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1410 if (ret > 0)
1411 ret = -ENOENT;
1412 }
1413#endif
1414 return ret;
1415}
1416
1417static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1418 struct btrfs_root *root,
1419 struct btrfs_path *path,
1420 u64 bytenr, u64 parent,
1421 u64 root_objectid)
1422{
1423 struct btrfs_key key;
1424 int ret;
1425
1426 key.objectid = bytenr;
1427 if (parent) {
1428 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1429 key.offset = parent;
1430 } else {
1431 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1432 key.offset = root_objectid;
1433 }
1434
1435 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001436 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001437 return ret;
1438}
1439
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001440static inline int extent_ref_type(u64 parent, u64 owner)
1441{
1442 int type;
1443 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1444 if (parent > 0)
1445 type = BTRFS_SHARED_BLOCK_REF_KEY;
1446 else
1447 type = BTRFS_TREE_BLOCK_REF_KEY;
1448 } else {
1449 if (parent > 0)
1450 type = BTRFS_SHARED_DATA_REF_KEY;
1451 else
1452 type = BTRFS_EXTENT_DATA_REF_KEY;
1453 }
1454 return type;
1455}
1456
Yan Zheng2c47e6052009-06-27 21:07:35 -04001457static int find_next_key(struct btrfs_path *path, int level,
1458 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001459
1460{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001461 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001462 if (!path->nodes[level])
1463 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001464 if (path->slots[level] + 1 >=
1465 btrfs_header_nritems(path->nodes[level]))
1466 continue;
1467 if (level == 0)
1468 btrfs_item_key_to_cpu(path->nodes[level], key,
1469 path->slots[level] + 1);
1470 else
1471 btrfs_node_key_to_cpu(path->nodes[level], key,
1472 path->slots[level] + 1);
1473 return 0;
1474 }
1475 return 1;
1476}
1477
1478/*
1479 * look for inline back ref. if back ref is found, *ref_ret is set
1480 * to the address of inline back ref, and 0 is returned.
1481 *
1482 * if back ref isn't found, *ref_ret is set to the address where it
1483 * should be inserted, and -ENOENT is returned.
1484 *
1485 * if insert is true and there are too many inline back refs, the path
1486 * points to the extent item, and -EAGAIN is returned.
1487 *
1488 * NOTE: inline back refs are ordered in the same way that back ref
1489 * items in the tree are ordered.
1490 */
1491static noinline_for_stack
1492int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1493 struct btrfs_root *root,
1494 struct btrfs_path *path,
1495 struct btrfs_extent_inline_ref **ref_ret,
1496 u64 bytenr, u64 num_bytes,
1497 u64 parent, u64 root_objectid,
1498 u64 owner, u64 offset, int insert)
1499{
1500 struct btrfs_key key;
1501 struct extent_buffer *leaf;
1502 struct btrfs_extent_item *ei;
1503 struct btrfs_extent_inline_ref *iref;
1504 u64 flags;
1505 u64 item_size;
1506 unsigned long ptr;
1507 unsigned long end;
1508 int extra_size;
1509 int type;
1510 int want;
1511 int ret;
1512 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001513 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1514 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001515
1516 key.objectid = bytenr;
1517 key.type = BTRFS_EXTENT_ITEM_KEY;
1518 key.offset = num_bytes;
1519
1520 want = extent_ref_type(parent, owner);
1521 if (insert) {
1522 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001523 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001524 } else
1525 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001526
1527 /*
1528 * Owner is our parent level, so we can just add one to get the level
1529 * for the block we are interested in.
1530 */
1531 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1532 key.type = BTRFS_METADATA_ITEM_KEY;
1533 key.offset = owner;
1534 }
1535
1536again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001537 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1538 if (ret < 0) {
1539 err = ret;
1540 goto out;
1541 }
Josef Bacik3173a182013-03-07 14:22:04 -05001542
1543 /*
1544 * We may be a newly converted file system which still has the old fat
1545 * extent entries for metadata, so try and see if we have one of those.
1546 */
1547 if (ret > 0 && skinny_metadata) {
1548 skinny_metadata = false;
1549 if (path->slots[0]) {
1550 path->slots[0]--;
1551 btrfs_item_key_to_cpu(path->nodes[0], &key,
1552 path->slots[0]);
1553 if (key.objectid == bytenr &&
1554 key.type == BTRFS_EXTENT_ITEM_KEY &&
1555 key.offset == num_bytes)
1556 ret = 0;
1557 }
1558 if (ret) {
1559 key.type = BTRFS_EXTENT_ITEM_KEY;
1560 key.offset = num_bytes;
1561 btrfs_release_path(path);
1562 goto again;
1563 }
1564 }
1565
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001566 if (ret && !insert) {
1567 err = -ENOENT;
1568 goto out;
Josef Bacik492104c2013-03-08 15:41:02 -05001569 } else if (ret) {
1570 err = -EIO;
1571 WARN_ON(1);
1572 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001573 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001574
1575 leaf = path->nodes[0];
1576 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1577#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1578 if (item_size < sizeof(*ei)) {
1579 if (!insert) {
1580 err = -ENOENT;
1581 goto out;
1582 }
1583 ret = convert_extent_item_v0(trans, root, path, owner,
1584 extra_size);
1585 if (ret < 0) {
1586 err = ret;
1587 goto out;
1588 }
1589 leaf = path->nodes[0];
1590 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1591 }
1592#endif
1593 BUG_ON(item_size < sizeof(*ei));
1594
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001595 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1596 flags = btrfs_extent_flags(leaf, ei);
1597
1598 ptr = (unsigned long)(ei + 1);
1599 end = (unsigned long)ei + item_size;
1600
Josef Bacik3173a182013-03-07 14:22:04 -05001601 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001602 ptr += sizeof(struct btrfs_tree_block_info);
1603 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001604 }
1605
1606 err = -ENOENT;
1607 while (1) {
1608 if (ptr >= end) {
1609 WARN_ON(ptr > end);
1610 break;
1611 }
1612 iref = (struct btrfs_extent_inline_ref *)ptr;
1613 type = btrfs_extent_inline_ref_type(leaf, iref);
1614 if (want < type)
1615 break;
1616 if (want > type) {
1617 ptr += btrfs_extent_inline_ref_size(type);
1618 continue;
1619 }
1620
1621 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1622 struct btrfs_extent_data_ref *dref;
1623 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1624 if (match_extent_data_ref(leaf, dref, root_objectid,
1625 owner, offset)) {
1626 err = 0;
1627 break;
1628 }
1629 if (hash_extent_data_ref_item(leaf, dref) <
1630 hash_extent_data_ref(root_objectid, owner, offset))
1631 break;
1632 } else {
1633 u64 ref_offset;
1634 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1635 if (parent > 0) {
1636 if (parent == ref_offset) {
1637 err = 0;
1638 break;
1639 }
1640 if (ref_offset < parent)
1641 break;
1642 } else {
1643 if (root_objectid == ref_offset) {
1644 err = 0;
1645 break;
1646 }
1647 if (ref_offset < root_objectid)
1648 break;
1649 }
1650 }
1651 ptr += btrfs_extent_inline_ref_size(type);
1652 }
1653 if (err == -ENOENT && insert) {
1654 if (item_size + extra_size >=
1655 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1656 err = -EAGAIN;
1657 goto out;
1658 }
1659 /*
1660 * To add new inline back ref, we have to make sure
1661 * there is no corresponding back ref item.
1662 * For simplicity, we just do not add new inline back
1663 * ref if there is any kind of item for this block
1664 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001665 if (find_next_key(path, 0, &key) == 0 &&
1666 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001667 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668 err = -EAGAIN;
1669 goto out;
1670 }
1671 }
1672 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1673out:
Yan Zheng85d41982009-06-11 08:51:10 -04001674 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001675 path->keep_locks = 0;
1676 btrfs_unlock_up_safe(path, 1);
1677 }
1678 return err;
1679}
1680
1681/*
1682 * helper to add new inline back ref
1683 */
1684static noinline_for_stack
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001685void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001686 struct btrfs_path *path,
1687 struct btrfs_extent_inline_ref *iref,
1688 u64 parent, u64 root_objectid,
1689 u64 owner, u64 offset, int refs_to_add,
1690 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001691{
1692 struct extent_buffer *leaf;
1693 struct btrfs_extent_item *ei;
1694 unsigned long ptr;
1695 unsigned long end;
1696 unsigned long item_offset;
1697 u64 refs;
1698 int size;
1699 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001700
1701 leaf = path->nodes[0];
1702 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1703 item_offset = (unsigned long)iref - (unsigned long)ei;
1704
1705 type = extent_ref_type(parent, owner);
1706 size = btrfs_extent_inline_ref_size(type);
1707
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001708 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001709
1710 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1711 refs = btrfs_extent_refs(leaf, ei);
1712 refs += refs_to_add;
1713 btrfs_set_extent_refs(leaf, ei, refs);
1714 if (extent_op)
1715 __run_delayed_extent_op(extent_op, leaf, ei);
1716
1717 ptr = (unsigned long)ei + item_offset;
1718 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1719 if (ptr < end - size)
1720 memmove_extent_buffer(leaf, ptr + size, ptr,
1721 end - size - ptr);
1722
1723 iref = (struct btrfs_extent_inline_ref *)ptr;
1724 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1725 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1726 struct btrfs_extent_data_ref *dref;
1727 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1728 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1729 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1730 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1731 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1732 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1733 struct btrfs_shared_data_ref *sref;
1734 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1735 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1736 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1737 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1738 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1739 } else {
1740 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1741 }
1742 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001743}
1744
1745static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1746 struct btrfs_root *root,
1747 struct btrfs_path *path,
1748 struct btrfs_extent_inline_ref **ref_ret,
1749 u64 bytenr, u64 num_bytes, u64 parent,
1750 u64 root_objectid, u64 owner, u64 offset)
1751{
1752 int ret;
1753
1754 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1755 bytenr, num_bytes, parent,
1756 root_objectid, owner, offset, 0);
1757 if (ret != -ENOENT)
1758 return ret;
1759
David Sterbab3b4aa72011-04-21 01:20:15 +02001760 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001761 *ref_ret = NULL;
1762
1763 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1764 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1765 root_objectid);
1766 } else {
1767 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1768 root_objectid, owner, offset);
1769 }
1770 return ret;
1771}
1772
1773/*
1774 * helper to update/remove inline back ref
1775 */
1776static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001777void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001778 struct btrfs_path *path,
1779 struct btrfs_extent_inline_ref *iref,
1780 int refs_to_mod,
1781 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001782{
1783 struct extent_buffer *leaf;
1784 struct btrfs_extent_item *ei;
1785 struct btrfs_extent_data_ref *dref = NULL;
1786 struct btrfs_shared_data_ref *sref = NULL;
1787 unsigned long ptr;
1788 unsigned long end;
1789 u32 item_size;
1790 int size;
1791 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001792 u64 refs;
1793
1794 leaf = path->nodes[0];
1795 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1796 refs = btrfs_extent_refs(leaf, ei);
1797 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1798 refs += refs_to_mod;
1799 btrfs_set_extent_refs(leaf, ei, refs);
1800 if (extent_op)
1801 __run_delayed_extent_op(extent_op, leaf, ei);
1802
1803 type = btrfs_extent_inline_ref_type(leaf, iref);
1804
1805 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1806 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1807 refs = btrfs_extent_data_ref_count(leaf, dref);
1808 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1809 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1810 refs = btrfs_shared_data_ref_count(leaf, sref);
1811 } else {
1812 refs = 1;
1813 BUG_ON(refs_to_mod != -1);
1814 }
1815
1816 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1817 refs += refs_to_mod;
1818
1819 if (refs > 0) {
1820 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1821 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1822 else
1823 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1824 } else {
1825 size = btrfs_extent_inline_ref_size(type);
1826 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1827 ptr = (unsigned long)iref;
1828 end = (unsigned long)ei + item_size;
1829 if (ptr + size < end)
1830 memmove_extent_buffer(leaf, ptr, ptr + size,
1831 end - ptr - size);
1832 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001833 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001834 }
1835 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001836}
1837
1838static noinline_for_stack
1839int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1840 struct btrfs_root *root,
1841 struct btrfs_path *path,
1842 u64 bytenr, u64 num_bytes, u64 parent,
1843 u64 root_objectid, u64 owner,
1844 u64 offset, int refs_to_add,
1845 struct btrfs_delayed_extent_op *extent_op)
1846{
1847 struct btrfs_extent_inline_ref *iref;
1848 int ret;
1849
1850 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1851 bytenr, num_bytes, parent,
1852 root_objectid, owner, offset, 1);
1853 if (ret == 0) {
1854 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001855 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001856 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001857 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001858 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001859 root_objectid, owner, offset,
1860 refs_to_add, extent_op);
1861 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001862 }
1863 return ret;
1864}
1865
1866static int insert_extent_backref(struct btrfs_trans_handle *trans,
1867 struct btrfs_root *root,
1868 struct btrfs_path *path,
1869 u64 bytenr, u64 parent, u64 root_objectid,
1870 u64 owner, u64 offset, int refs_to_add)
1871{
1872 int ret;
1873 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1874 BUG_ON(refs_to_add != 1);
1875 ret = insert_tree_block_ref(trans, root, path, bytenr,
1876 parent, root_objectid);
1877 } else {
1878 ret = insert_extent_data_ref(trans, root, path, bytenr,
1879 parent, root_objectid,
1880 owner, offset, refs_to_add);
1881 }
1882 return ret;
1883}
1884
1885static int remove_extent_backref(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *root,
1887 struct btrfs_path *path,
1888 struct btrfs_extent_inline_ref *iref,
1889 int refs_to_drop, int is_data)
1890{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001891 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001892
1893 BUG_ON(!is_data && refs_to_drop != 1);
1894 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001895 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001896 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001897 } else if (is_data) {
1898 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1899 } else {
1900 ret = btrfs_del_item(trans, root, path);
1901 }
1902 return ret;
1903}
1904
Li Dongyang5378e602011-03-24 10:24:27 +00001905static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001906 u64 start, u64 len)
1907{
Li Dongyang5378e602011-03-24 10:24:27 +00001908 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001909}
Chris Mason15916de2008-11-19 21:17:22 -05001910
Liu Hui1f3c79a2009-01-05 15:57:51 -05001911static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001912 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001913{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001914 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001915 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001916 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001917
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001918
Liu Hui1f3c79a2009-01-05 15:57:51 -05001919 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001920 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001921 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001922 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001923 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001924 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001925 int i;
1926
Liu Hui1f3c79a2009-01-05 15:57:51 -05001927
Jan Schmidta1d3c472011-08-04 17:15:33 +02001928 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001929 if (!stripe->dev->can_discard)
1930 continue;
1931
Li Dongyang5378e602011-03-24 10:24:27 +00001932 ret = btrfs_issue_discard(stripe->dev->bdev,
1933 stripe->physical,
1934 stripe->length);
1935 if (!ret)
1936 discarded_bytes += stripe->length;
1937 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001938 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001939
1940 /*
1941 * Just in case we get back EOPNOTSUPP for some reason,
1942 * just ignore the return value so we don't screw up
1943 * people calling discard_extent.
1944 */
1945 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001946 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001947 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001948 }
Li Dongyang5378e602011-03-24 10:24:27 +00001949
1950 if (actual_bytes)
1951 *actual_bytes = discarded_bytes;
1952
Liu Hui1f3c79a2009-01-05 15:57:51 -05001953
David Woodhouse53b381b2013-01-29 18:40:14 -05001954 if (ret == -EOPNOTSUPP)
1955 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001956 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001957}
1958
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001959/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001960int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1961 struct btrfs_root *root,
1962 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001963 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001964{
1965 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001966 struct btrfs_fs_info *fs_info = root->fs_info;
1967
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001968 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1969 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001970
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001971 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001972 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1973 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001974 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001975 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001976 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001977 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1978 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001979 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001980 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001981 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001982 return ret;
1983}
1984
Chris Mason925baed2008-06-25 16:01:30 -04001985static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001986 struct btrfs_root *root,
1987 u64 bytenr, u64 num_bytes,
1988 u64 parent, u64 root_objectid,
1989 u64 owner, u64 offset, int refs_to_add,
1990 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001991{
Chris Mason5caf2a02007-04-02 11:20:42 -04001992 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001993 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001994 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001995 u64 refs;
1996 int ret;
1997 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001998
Chris Mason5caf2a02007-04-02 11:20:42 -04001999 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002000 if (!path)
2001 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04002002
Chris Mason3c12ac72008-04-21 12:01:38 -04002003 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002004 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002005 /* this will setup the path even if it fails to insert the back ref */
2006 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
2007 path, bytenr, num_bytes, parent,
2008 root_objectid, owner, offset,
2009 refs_to_add, extent_op);
2010 if (ret == 0)
2011 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04002012
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002013 if (ret != -EAGAIN) {
2014 err = ret;
2015 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04002016 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002017
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002018 leaf = path->nodes[0];
2019 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2020 refs = btrfs_extent_refs(leaf, item);
2021 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2022 if (extent_op)
2023 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002024
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002025 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002026 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002027
Chris Mason3c12ac72008-04-21 12:01:38 -04002028 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002029 path->leave_spinning = 1;
2030
Chris Mason56bec292009-03-13 10:10:06 -04002031 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002032 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002033 path, bytenr, parent, root_objectid,
2034 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002035 if (ret)
2036 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002037out:
Chris Mason74493f72007-12-11 09:25:06 -05002038 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002039 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05002040}
2041
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002042static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2043 struct btrfs_root *root,
2044 struct btrfs_delayed_ref_node *node,
2045 struct btrfs_delayed_extent_op *extent_op,
2046 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002047{
Chris Mason56bec292009-03-13 10:10:06 -04002048 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002049 struct btrfs_delayed_data_ref *ref;
2050 struct btrfs_key ins;
2051 u64 parent = 0;
2052 u64 ref_root = 0;
2053 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002054
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002055 ins.objectid = node->bytenr;
2056 ins.offset = node->num_bytes;
2057 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002058
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002059 ref = btrfs_delayed_node_to_data_ref(node);
2060 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2061 parent = ref->parent;
2062 else
2063 ref_root = ref->root;
2064
2065 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002066 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002067 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002068 ret = alloc_reserved_file_extent(trans, root,
2069 parent, ref_root, flags,
2070 ref->objectid, ref->offset,
2071 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002072 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2073 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2074 node->num_bytes, parent,
2075 ref_root, ref->objectid,
2076 ref->offset, node->ref_mod,
2077 extent_op);
2078 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2079 ret = __btrfs_free_extent(trans, root, node->bytenr,
2080 node->num_bytes, parent,
2081 ref_root, ref->objectid,
2082 ref->offset, node->ref_mod,
2083 extent_op);
2084 } else {
2085 BUG();
2086 }
Chris Mason56bec292009-03-13 10:10:06 -04002087 return ret;
2088}
2089
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002090static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2091 struct extent_buffer *leaf,
2092 struct btrfs_extent_item *ei)
2093{
2094 u64 flags = btrfs_extent_flags(leaf, ei);
2095 if (extent_op->update_flags) {
2096 flags |= extent_op->flags_to_set;
2097 btrfs_set_extent_flags(leaf, ei, flags);
2098 }
2099
2100 if (extent_op->update_key) {
2101 struct btrfs_tree_block_info *bi;
2102 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2103 bi = (struct btrfs_tree_block_info *)(ei + 1);
2104 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2105 }
2106}
2107
2108static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2109 struct btrfs_root *root,
2110 struct btrfs_delayed_ref_node *node,
2111 struct btrfs_delayed_extent_op *extent_op)
2112{
2113 struct btrfs_key key;
2114 struct btrfs_path *path;
2115 struct btrfs_extent_item *ei;
2116 struct extent_buffer *leaf;
2117 u32 item_size;
2118 int ret;
2119 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002120 int metadata = (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2121 node->type == BTRFS_SHARED_BLOCK_REF_KEY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002122
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002123 if (trans->aborted)
2124 return 0;
2125
Josef Bacik3173a182013-03-07 14:22:04 -05002126 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2127 metadata = 0;
2128
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002129 path = btrfs_alloc_path();
2130 if (!path)
2131 return -ENOMEM;
2132
2133 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002134
Josef Bacik3173a182013-03-07 14:22:04 -05002135 if (metadata) {
2136 struct btrfs_delayed_tree_ref *tree_ref;
2137
2138 tree_ref = btrfs_delayed_node_to_tree_ref(node);
2139 key.type = BTRFS_METADATA_ITEM_KEY;
2140 key.offset = tree_ref->level;
2141 } else {
2142 key.type = BTRFS_EXTENT_ITEM_KEY;
2143 key.offset = node->num_bytes;
2144 }
2145
2146again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002147 path->reada = 1;
2148 path->leave_spinning = 1;
2149 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2150 path, 0, 1);
2151 if (ret < 0) {
2152 err = ret;
2153 goto out;
2154 }
2155 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002156 if (metadata) {
2157 btrfs_release_path(path);
2158 metadata = 0;
2159
2160 key.offset = node->num_bytes;
2161 key.type = BTRFS_EXTENT_ITEM_KEY;
2162 goto again;
2163 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002164 err = -EIO;
2165 goto out;
2166 }
2167
2168 leaf = path->nodes[0];
2169 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2170#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2171 if (item_size < sizeof(*ei)) {
2172 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2173 path, (u64)-1, 0);
2174 if (ret < 0) {
2175 err = ret;
2176 goto out;
2177 }
2178 leaf = path->nodes[0];
2179 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2180 }
2181#endif
2182 BUG_ON(item_size < sizeof(*ei));
2183 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2184 __run_delayed_extent_op(extent_op, leaf, ei);
2185
2186 btrfs_mark_buffer_dirty(leaf);
2187out:
2188 btrfs_free_path(path);
2189 return err;
2190}
2191
2192static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2193 struct btrfs_root *root,
2194 struct btrfs_delayed_ref_node *node,
2195 struct btrfs_delayed_extent_op *extent_op,
2196 int insert_reserved)
2197{
2198 int ret = 0;
2199 struct btrfs_delayed_tree_ref *ref;
2200 struct btrfs_key ins;
2201 u64 parent = 0;
2202 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002203 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2204 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002205
2206 ref = btrfs_delayed_node_to_tree_ref(node);
2207 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2208 parent = ref->parent;
2209 else
2210 ref_root = ref->root;
2211
Josef Bacik3173a182013-03-07 14:22:04 -05002212 ins.objectid = node->bytenr;
2213 if (skinny_metadata) {
2214 ins.offset = ref->level;
2215 ins.type = BTRFS_METADATA_ITEM_KEY;
2216 } else {
2217 ins.offset = node->num_bytes;
2218 ins.type = BTRFS_EXTENT_ITEM_KEY;
2219 }
2220
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002221 BUG_ON(node->ref_mod != 1);
2222 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002223 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002224 ret = alloc_reserved_tree_block(trans, root,
2225 parent, ref_root,
2226 extent_op->flags_to_set,
2227 &extent_op->key,
2228 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002229 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2230 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2231 node->num_bytes, parent, ref_root,
2232 ref->level, 0, 1, extent_op);
2233 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2234 ret = __btrfs_free_extent(trans, root, node->bytenr,
2235 node->num_bytes, parent, ref_root,
2236 ref->level, 0, 1, extent_op);
2237 } else {
2238 BUG();
2239 }
2240 return ret;
2241}
2242
Chris Mason56bec292009-03-13 10:10:06 -04002243/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002244static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2245 struct btrfs_root *root,
2246 struct btrfs_delayed_ref_node *node,
2247 struct btrfs_delayed_extent_op *extent_op,
2248 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002249{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002250 int ret = 0;
2251
2252 if (trans->aborted)
2253 return 0;
2254
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002255 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002256 struct btrfs_delayed_ref_head *head;
2257 /*
2258 * we've hit the end of the chain and we were supposed
2259 * to insert this extent into the tree. But, it got
2260 * deleted before we ever needed to insert it, so all
2261 * we have to do is clean up the accounting
2262 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002263 BUG_ON(extent_op);
2264 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002265 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002266 btrfs_pin_extent(root, node->bytenr,
2267 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002268 if (head->is_data) {
2269 ret = btrfs_del_csums(trans, root,
2270 node->bytenr,
2271 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002272 }
Chris Mason56bec292009-03-13 10:10:06 -04002273 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002274 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002275 }
Josef Bacikeb099672009-02-12 09:27:38 -05002276
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002277 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2278 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2279 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2280 insert_reserved);
2281 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2282 node->type == BTRFS_SHARED_DATA_REF_KEY)
2283 ret = run_delayed_data_ref(trans, root, node, extent_op,
2284 insert_reserved);
2285 else
2286 BUG();
2287 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002288}
2289
Chris Mason56bec292009-03-13 10:10:06 -04002290static noinline struct btrfs_delayed_ref_node *
2291select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002292{
Chris Mason56bec292009-03-13 10:10:06 -04002293 struct rb_node *node;
2294 struct btrfs_delayed_ref_node *ref;
2295 int action = BTRFS_ADD_DELAYED_REF;
2296again:
2297 /*
2298 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2299 * this prevents ref count from going down to zero when
2300 * there still are pending delayed ref.
2301 */
2302 node = rb_prev(&head->node.rb_node);
2303 while (1) {
2304 if (!node)
2305 break;
2306 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2307 rb_node);
2308 if (ref->bytenr != head->node.bytenr)
2309 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002310 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002311 return ref;
2312 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002313 }
Chris Mason56bec292009-03-13 10:10:06 -04002314 if (action == BTRFS_ADD_DELAYED_REF) {
2315 action = BTRFS_DROP_DELAYED_REF;
2316 goto again;
2317 }
2318 return NULL;
2319}
2320
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002321/*
2322 * Returns 0 on success or if called with an already aborted transaction.
2323 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2324 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002325static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2326 struct btrfs_root *root,
2327 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002328{
Chris Mason56bec292009-03-13 10:10:06 -04002329 struct btrfs_delayed_ref_root *delayed_refs;
2330 struct btrfs_delayed_ref_node *ref;
2331 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002332 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002333 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002334 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002335 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002336 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002337
2338 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002339 while (1) {
2340 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002341 /* pick a new head ref from the cluster list */
2342 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002343 break;
Chris Mason56bec292009-03-13 10:10:06 -04002344
Chris Masonc3e69d52009-03-13 10:17:05 -04002345 locked_ref = list_entry(cluster->next,
2346 struct btrfs_delayed_ref_head, cluster);
2347
2348 /* grab the lock that says we are going to process
2349 * all the refs for this head */
2350 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2351
2352 /*
2353 * we may have dropped the spin lock to get the head
2354 * mutex lock, and that might have given someone else
2355 * time to free the head. If that's true, it has been
2356 * removed from our list and we can move on.
2357 */
2358 if (ret == -EAGAIN) {
2359 locked_ref = NULL;
2360 count++;
2361 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002362 }
2363 }
2364
2365 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002366 * We need to try and merge add/drops of the same ref since we
2367 * can run into issues with relocate dropping the implicit ref
2368 * and then it being added back again before the drop can
2369 * finish. If we merged anything we need to re-loop so we can
2370 * get a good ref.
2371 */
2372 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2373 locked_ref);
2374
2375 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002376 * locked_ref is the head node, so we have to go one
2377 * node back for any delayed ref updates
2378 */
2379 ref = select_delayed_ref(locked_ref);
2380
2381 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002382 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002383 /*
2384 * there are still refs with lower seq numbers in the
2385 * process of being added. Don't run this ref yet.
2386 */
2387 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002388 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002389 locked_ref = NULL;
2390 delayed_refs->num_heads_ready++;
2391 spin_unlock(&delayed_refs->lock);
2392 cond_resched();
2393 spin_lock(&delayed_refs->lock);
2394 continue;
2395 }
2396
2397 /*
Chris Mason56bec292009-03-13 10:10:06 -04002398 * record the must insert reserved flag before we
2399 * drop the spin lock.
2400 */
2401 must_insert_reserved = locked_ref->must_insert_reserved;
2402 locked_ref->must_insert_reserved = 0;
2403
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002404 extent_op = locked_ref->extent_op;
2405 locked_ref->extent_op = NULL;
2406
Chris Mason56bec292009-03-13 10:10:06 -04002407 if (!ref) {
2408 /* All delayed refs have been processed, Go ahead
2409 * and send the head node to run_one_delayed_ref,
2410 * so that any accounting fixes can happen
2411 */
2412 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002413
2414 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002415 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002416 extent_op = NULL;
2417 }
2418
2419 if (extent_op) {
2420 spin_unlock(&delayed_refs->lock);
2421
2422 ret = run_delayed_extent_op(trans, root,
2423 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002424 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002425
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002426 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002427 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002428 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002429 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002430 return ret;
2431 }
2432
Chris Mason203bf282012-01-06 15:23:57 -05002433 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002434 }
Chris Mason56bec292009-03-13 10:10:06 -04002435 }
2436
2437 ref->in_tree = 0;
2438 rb_erase(&ref->rb_node, &delayed_refs->root);
2439 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002440 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002441 /*
2442 * when we play the delayed ref, also correct the
2443 * ref_mod on head
2444 */
2445 switch (ref->action) {
2446 case BTRFS_ADD_DELAYED_REF:
2447 case BTRFS_ADD_DELAYED_EXTENT:
2448 locked_ref->node.ref_mod -= ref->ref_mod;
2449 break;
2450 case BTRFS_DROP_DELAYED_REF:
2451 locked_ref->node.ref_mod += ref->ref_mod;
2452 break;
2453 default:
2454 WARN_ON(1);
2455 }
2456 }
Chris Mason56bec292009-03-13 10:10:06 -04002457 spin_unlock(&delayed_refs->lock);
2458
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002459 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002460 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002461
Miao Xie78a61842012-11-21 02:21:28 +00002462 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002463 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002464 btrfs_delayed_ref_unlock(locked_ref);
2465 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002466 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002467 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002468 return ret;
2469 }
2470
Miao Xie093486c2012-12-19 08:10:10 +00002471 /*
2472 * If this node is a head, that means all the refs in this head
2473 * have been dealt with, and we will pick the next head to deal
2474 * with, so we must unlock the head and drop it from the cluster
2475 * list before we release it.
2476 */
2477 if (btrfs_delayed_ref_is_head(ref)) {
2478 list_del_init(&locked_ref->cluster);
2479 btrfs_delayed_ref_unlock(locked_ref);
2480 locked_ref = NULL;
2481 }
2482 btrfs_put_delayed_ref(ref);
2483 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002484next:
Chris Mason1887be62009-03-13 10:11:24 -04002485 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002486 spin_lock(&delayed_refs->lock);
2487 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002488 return count;
2489}
2490
Arne Jansen709c0482011-09-12 12:22:57 +02002491#ifdef SCRAMBLE_DELAYED_REFS
2492/*
2493 * Normally delayed refs get processed in ascending bytenr order. This
2494 * correlates in most cases to the order added. To expose dependencies on this
2495 * order, we start to process the tree in the middle instead of the beginning
2496 */
2497static u64 find_middle(struct rb_root *root)
2498{
2499 struct rb_node *n = root->rb_node;
2500 struct btrfs_delayed_ref_node *entry;
2501 int alt = 1;
2502 u64 middle;
2503 u64 first = 0, last = 0;
2504
2505 n = rb_first(root);
2506 if (n) {
2507 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2508 first = entry->bytenr;
2509 }
2510 n = rb_last(root);
2511 if (n) {
2512 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2513 last = entry->bytenr;
2514 }
2515 n = root->rb_node;
2516
2517 while (n) {
2518 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2519 WARN_ON(!entry->in_tree);
2520
2521 middle = entry->bytenr;
2522
2523 if (alt)
2524 n = n->rb_left;
2525 else
2526 n = n->rb_right;
2527
2528 alt = 1 - alt;
2529 }
2530 return middle;
2531}
2532#endif
2533
Arne Jansenbed92ea2012-06-28 18:03:02 +02002534int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2535 struct btrfs_fs_info *fs_info)
2536{
2537 struct qgroup_update *qgroup_update;
2538 int ret = 0;
2539
2540 if (list_empty(&trans->qgroup_ref_list) !=
2541 !trans->delayed_ref_elem.seq) {
2542 /* list without seq or seq without list */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002543 btrfs_err(fs_info,
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +00002544 "qgroup accounting update error, list is%s empty, seq is %#x.%x",
Arne Jansenbed92ea2012-06-28 18:03:02 +02002545 list_empty(&trans->qgroup_ref_list) ? "" : " not",
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +00002546 (u32)(trans->delayed_ref_elem.seq >> 32),
2547 (u32)trans->delayed_ref_elem.seq);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002548 BUG();
2549 }
2550
2551 if (!trans->delayed_ref_elem.seq)
2552 return 0;
2553
2554 while (!list_empty(&trans->qgroup_ref_list)) {
2555 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2556 struct qgroup_update, list);
2557 list_del(&qgroup_update->list);
2558 if (!ret)
2559 ret = btrfs_qgroup_account_ref(
2560 trans, fs_info, qgroup_update->node,
2561 qgroup_update->extent_op);
2562 kfree(qgroup_update);
2563 }
2564
2565 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2566
2567 return ret;
2568}
2569
Chris Masonbb721702013-01-29 18:44:12 -05002570static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
2571 int count)
2572{
2573 int val = atomic_read(&delayed_refs->ref_seq);
2574
2575 if (val < seq || val >= seq + count)
2576 return 1;
2577 return 0;
2578}
2579
Chris Masonc3e69d52009-03-13 10:17:05 -04002580/*
2581 * this starts processing the delayed reference count updates and
2582 * extent insertions we have queued up so far. count can be
2583 * 0, which means to process everything in the tree at the start
2584 * of the run (but not newly added entries), or it can be some target
2585 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002586 *
2587 * Returns 0 on success or if called with an aborted transaction
2588 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002589 */
2590int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2591 struct btrfs_root *root, unsigned long count)
2592{
2593 struct rb_node *node;
2594 struct btrfs_delayed_ref_root *delayed_refs;
2595 struct btrfs_delayed_ref_node *ref;
2596 struct list_head cluster;
2597 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002598 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002599 int run_all = count == (unsigned long)-1;
2600 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002601 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002602
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002603 /* We'll clean this up in btrfs_cleanup_transaction */
2604 if (trans->aborted)
2605 return 0;
2606
Chris Masonc3e69d52009-03-13 10:17:05 -04002607 if (root == root->fs_info->extent_root)
2608 root = root->fs_info->tree_root;
2609
Jan Schmidtedf39272012-06-28 18:04:55 +02002610 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2611
Chris Masonc3e69d52009-03-13 10:17:05 -04002612 delayed_refs = &trans->transaction->delayed_refs;
2613 INIT_LIST_HEAD(&cluster);
Chris Masonbb721702013-01-29 18:44:12 -05002614 if (count == 0) {
2615 count = delayed_refs->num_entries * 2;
2616 run_most = 1;
2617 }
2618
2619 if (!run_all && !run_most) {
2620 int old;
2621 int seq = atomic_read(&delayed_refs->ref_seq);
2622
2623progress:
2624 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2625 if (old) {
2626 DEFINE_WAIT(__wait);
2627 if (delayed_refs->num_entries < 16348)
2628 return 0;
2629
2630 prepare_to_wait(&delayed_refs->wait, &__wait,
2631 TASK_UNINTERRUPTIBLE);
2632
2633 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2634 if (old) {
2635 schedule();
2636 finish_wait(&delayed_refs->wait, &__wait);
2637
2638 if (!refs_newer(delayed_refs, seq, 256))
2639 goto progress;
2640 else
2641 return 0;
2642 } else {
2643 finish_wait(&delayed_refs->wait, &__wait);
2644 goto again;
2645 }
2646 }
2647
2648 } else {
2649 atomic_inc(&delayed_refs->procs_running_refs);
2650 }
2651
Chris Masonc3e69d52009-03-13 10:17:05 -04002652again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002653 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002654 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002655
Arne Jansen709c0482011-09-12 12:22:57 +02002656#ifdef SCRAMBLE_DELAYED_REFS
2657 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2658#endif
2659
Chris Masonc3e69d52009-03-13 10:17:05 -04002660 while (1) {
2661 if (!(run_all || run_most) &&
2662 delayed_refs->num_heads_ready < 64)
2663 break;
2664
2665 /*
2666 * go find something we can process in the rbtree. We start at
2667 * the beginning of the tree, and then build a cluster
2668 * of refs to process starting at the first one we are able to
2669 * lock
2670 */
Jan Schmidta1686502011-12-12 16:10:07 +01002671 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002672 ret = btrfs_find_ref_cluster(trans, &cluster,
2673 delayed_refs->run_delayed_start);
2674 if (ret)
2675 break;
2676
2677 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002678 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002679 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002680 spin_unlock(&delayed_refs->lock);
2681 btrfs_abort_transaction(trans, root, ret);
Chris Masonbb721702013-01-29 18:44:12 -05002682 atomic_dec(&delayed_refs->procs_running_refs);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002683 return ret;
2684 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002685
Chris Masonbb721702013-01-29 18:44:12 -05002686 atomic_add(ret, &delayed_refs->ref_seq);
2687
Chris Masonc3e69d52009-03-13 10:17:05 -04002688 count -= min_t(unsigned long, ret, count);
2689
2690 if (count == 0)
2691 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002692
Arne Jansen1fa11e22012-08-06 14:18:51 -06002693 if (delayed_start >= delayed_refs->run_delayed_start) {
2694 if (loops == 0) {
2695 /*
2696 * btrfs_find_ref_cluster looped. let's do one
2697 * more cycle. if we don't run any delayed ref
2698 * during that cycle (because we can't because
2699 * all of them are blocked), bail out.
2700 */
2701 loops = 1;
2702 } else {
2703 /*
2704 * no runnable refs left, stop trying
2705 */
2706 BUG_ON(run_all);
2707 break;
2708 }
2709 }
2710 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002711 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002712 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002713 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002714 }
2715
Chris Mason56bec292009-03-13 10:10:06 -04002716 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002717 if (!list_empty(&trans->new_bgs)) {
2718 spin_unlock(&delayed_refs->lock);
2719 btrfs_create_pending_block_groups(trans, root);
2720 spin_lock(&delayed_refs->lock);
2721 }
2722
Chris Mason56bec292009-03-13 10:10:06 -04002723 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002724 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002725 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002726 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002727
2728 while (node) {
2729 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2730 rb_node);
2731 if (btrfs_delayed_ref_is_head(ref)) {
2732 struct btrfs_delayed_ref_head *head;
2733
2734 head = btrfs_delayed_node_to_head(ref);
2735 atomic_inc(&ref->refs);
2736
2737 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002738 /*
2739 * Mutex was contended, block until it's
2740 * released and try again
2741 */
Chris Mason56bec292009-03-13 10:10:06 -04002742 mutex_lock(&head->mutex);
2743 mutex_unlock(&head->mutex);
2744
2745 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002746 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002747 goto again;
2748 }
2749 node = rb_next(node);
2750 }
2751 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002752 schedule_timeout(1);
2753 goto again;
2754 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002755out:
Chris Masonbb721702013-01-29 18:44:12 -05002756 atomic_dec(&delayed_refs->procs_running_refs);
2757 smp_mb();
2758 if (waitqueue_active(&delayed_refs->wait))
2759 wake_up(&delayed_refs->wait);
2760
Chris Masonc3e69d52009-03-13 10:17:05 -04002761 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002762 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002763 return 0;
2764}
2765
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002766int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2767 struct btrfs_root *root,
2768 u64 bytenr, u64 num_bytes, u64 flags,
2769 int is_data)
2770{
2771 struct btrfs_delayed_extent_op *extent_op;
2772 int ret;
2773
Miao Xie78a61842012-11-21 02:21:28 +00002774 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002775 if (!extent_op)
2776 return -ENOMEM;
2777
2778 extent_op->flags_to_set = flags;
2779 extent_op->update_flags = 1;
2780 extent_op->update_key = 0;
2781 extent_op->is_data = is_data ? 1 : 0;
2782
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002783 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2784 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002785 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002786 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002787 return ret;
2788}
2789
2790static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2791 struct btrfs_root *root,
2792 struct btrfs_path *path,
2793 u64 objectid, u64 offset, u64 bytenr)
2794{
2795 struct btrfs_delayed_ref_head *head;
2796 struct btrfs_delayed_ref_node *ref;
2797 struct btrfs_delayed_data_ref *data_ref;
2798 struct btrfs_delayed_ref_root *delayed_refs;
2799 struct rb_node *node;
2800 int ret = 0;
2801
2802 ret = -ENOENT;
2803 delayed_refs = &trans->transaction->delayed_refs;
2804 spin_lock(&delayed_refs->lock);
2805 head = btrfs_find_delayed_ref_head(trans, bytenr);
2806 if (!head)
2807 goto out;
2808
2809 if (!mutex_trylock(&head->mutex)) {
2810 atomic_inc(&head->node.refs);
2811 spin_unlock(&delayed_refs->lock);
2812
David Sterbab3b4aa72011-04-21 01:20:15 +02002813 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002814
David Sterba8cc33e52011-05-02 15:29:25 +02002815 /*
2816 * Mutex was contended, block until it's released and let
2817 * caller try again
2818 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002819 mutex_lock(&head->mutex);
2820 mutex_unlock(&head->mutex);
2821 btrfs_put_delayed_ref(&head->node);
2822 return -EAGAIN;
2823 }
2824
2825 node = rb_prev(&head->node.rb_node);
2826 if (!node)
2827 goto out_unlock;
2828
2829 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2830
2831 if (ref->bytenr != bytenr)
2832 goto out_unlock;
2833
2834 ret = 1;
2835 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2836 goto out_unlock;
2837
2838 data_ref = btrfs_delayed_node_to_data_ref(ref);
2839
2840 node = rb_prev(node);
2841 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002842 int seq = ref->seq;
2843
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002844 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002845 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002846 goto out_unlock;
2847 }
2848
2849 if (data_ref->root != root->root_key.objectid ||
2850 data_ref->objectid != objectid || data_ref->offset != offset)
2851 goto out_unlock;
2852
2853 ret = 0;
2854out_unlock:
2855 mutex_unlock(&head->mutex);
2856out:
2857 spin_unlock(&delayed_refs->lock);
2858 return ret;
2859}
2860
2861static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2862 struct btrfs_root *root,
2863 struct btrfs_path *path,
2864 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002865{
2866 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002867 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002868 struct btrfs_extent_data_ref *ref;
2869 struct btrfs_extent_inline_ref *iref;
2870 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002871 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002872 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002873 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002874
Chris Masonbe20aa92007-12-17 20:14:01 -05002875 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002876 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002877 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002878
Chris Masonbe20aa92007-12-17 20:14:01 -05002879 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2880 if (ret < 0)
2881 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002882 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002883
2884 ret = -ENOENT;
2885 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002886 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002887
Zheng Yan31840ae2008-09-23 13:14:14 -04002888 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002889 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002890 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002891
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002892 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002893 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002894
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002895 ret = 1;
2896 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2897#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2898 if (item_size < sizeof(*ei)) {
2899 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2900 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002901 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002902#endif
2903 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2904
2905 if (item_size != sizeof(*ei) +
2906 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2907 goto out;
2908
2909 if (btrfs_extent_generation(leaf, ei) <=
2910 btrfs_root_last_snapshot(&root->root_item))
2911 goto out;
2912
2913 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2914 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2915 BTRFS_EXTENT_DATA_REF_KEY)
2916 goto out;
2917
2918 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2919 if (btrfs_extent_refs(leaf, ei) !=
2920 btrfs_extent_data_ref_count(leaf, ref) ||
2921 btrfs_extent_data_ref_root(leaf, ref) !=
2922 root->root_key.objectid ||
2923 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2924 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2925 goto out;
2926
Yan Zhengf321e492008-07-30 09:26:11 -04002927 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002928out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002929 return ret;
2930}
2931
2932int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2933 struct btrfs_root *root,
2934 u64 objectid, u64 offset, u64 bytenr)
2935{
2936 struct btrfs_path *path;
2937 int ret;
2938 int ret2;
2939
2940 path = btrfs_alloc_path();
2941 if (!path)
2942 return -ENOENT;
2943
2944 do {
2945 ret = check_committed_ref(trans, root, path, objectid,
2946 offset, bytenr);
2947 if (ret && ret != -ENOENT)
2948 goto out;
2949
2950 ret2 = check_delayed_ref(trans, root, path, objectid,
2951 offset, bytenr);
2952 } while (ret2 == -EAGAIN);
2953
2954 if (ret2 && ret2 != -ENOENT) {
2955 ret = ret2;
2956 goto out;
2957 }
2958
2959 if (ret != -ENOENT || ret2 != -ENOENT)
2960 ret = 0;
2961out:
Yan Zhengf321e492008-07-30 09:26:11 -04002962 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002963 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2964 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002965 return ret;
2966}
2967
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002968static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002969 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002970 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002971 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002972{
2973 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002974 u64 num_bytes;
2975 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002976 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002977 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002978 struct btrfs_key key;
2979 struct btrfs_file_extent_item *fi;
2980 int i;
2981 int level;
2982 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002983 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002984 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002985
2986 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002987 nritems = btrfs_header_nritems(buf);
2988 level = btrfs_header_level(buf);
2989
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002990 if (!root->ref_cows && level == 0)
2991 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002992
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002993 if (inc)
2994 process_func = btrfs_inc_extent_ref;
2995 else
2996 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002997
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002998 if (full_backref)
2999 parent = buf->start;
3000 else
3001 parent = 0;
3002
Zheng Yan31840ae2008-09-23 13:14:14 -04003003 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003004 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003005 btrfs_item_key_to_cpu(buf, &key, i);
3006 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003007 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003008 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003009 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003010 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003011 BTRFS_FILE_EXTENT_INLINE)
3012 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003013 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3014 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003015 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003016
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003017 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3018 key.offset -= btrfs_file_extent_offset(buf, fi);
3019 ret = process_func(trans, root, bytenr, num_bytes,
3020 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003021 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003022 if (ret)
3023 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003024 } else {
3025 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003026 num_bytes = btrfs_level_size(root, level - 1);
3027 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003028 parent, ref_root, level - 1, 0,
3029 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003030 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003031 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003032 }
3033 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003034 return 0;
3035fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003036 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003037}
3038
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003039int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003040 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04003041{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003042 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003043}
Zheng Yan31840ae2008-09-23 13:14:14 -04003044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003045int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003046 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003047{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003048 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04003049}
3050
Chris Mason9078a3e2007-04-26 16:46:15 -04003051static int write_one_cache_group(struct btrfs_trans_handle *trans,
3052 struct btrfs_root *root,
3053 struct btrfs_path *path,
3054 struct btrfs_block_group_cache *cache)
3055{
3056 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003057 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003058 unsigned long bi;
3059 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003060
Chris Mason9078a3e2007-04-26 16:46:15 -04003061 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003062 if (ret < 0)
3063 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003064 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04003065
3066 leaf = path->nodes[0];
3067 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3068 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3069 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003070 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04003071fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003072 if (ret) {
3073 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003074 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003075 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003076 return 0;
3077
3078}
3079
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003080static struct btrfs_block_group_cache *
3081next_block_group(struct btrfs_root *root,
3082 struct btrfs_block_group_cache *cache)
3083{
3084 struct rb_node *node;
3085 spin_lock(&root->fs_info->block_group_cache_lock);
3086 node = rb_next(&cache->cache_node);
3087 btrfs_put_block_group(cache);
3088 if (node) {
3089 cache = rb_entry(node, struct btrfs_block_group_cache,
3090 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003091 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003092 } else
3093 cache = NULL;
3094 spin_unlock(&root->fs_info->block_group_cache_lock);
3095 return cache;
3096}
3097
Josef Bacik0af3d002010-06-21 14:48:16 -04003098static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3099 struct btrfs_trans_handle *trans,
3100 struct btrfs_path *path)
3101{
3102 struct btrfs_root *root = block_group->fs_info->tree_root;
3103 struct inode *inode = NULL;
3104 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003105 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04003106 int num_pages = 0;
3107 int retries = 0;
3108 int ret = 0;
3109
3110 /*
3111 * If this block group is smaller than 100 megs don't bother caching the
3112 * block group.
3113 */
3114 if (block_group->key.offset < (100 * 1024 * 1024)) {
3115 spin_lock(&block_group->lock);
3116 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3117 spin_unlock(&block_group->lock);
3118 return 0;
3119 }
3120
3121again:
3122 inode = lookup_free_space_inode(root, block_group, path);
3123 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3124 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003125 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003126 goto out;
3127 }
3128
3129 if (IS_ERR(inode)) {
3130 BUG_ON(retries);
3131 retries++;
3132
3133 if (block_group->ro)
3134 goto out_free;
3135
3136 ret = create_free_space_inode(root, trans, block_group, path);
3137 if (ret)
3138 goto out_free;
3139 goto again;
3140 }
3141
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003142 /* We've already setup this transaction, go ahead and exit */
3143 if (block_group->cache_generation == trans->transid &&
3144 i_size_read(inode)) {
3145 dcs = BTRFS_DC_SETUP;
3146 goto out_put;
3147 }
3148
Josef Bacik0af3d002010-06-21 14:48:16 -04003149 /*
3150 * We want to set the generation to 0, that way if anything goes wrong
3151 * from here on out we know not to trust this cache when we load up next
3152 * time.
3153 */
3154 BTRFS_I(inode)->generation = 0;
3155 ret = btrfs_update_inode(trans, root, inode);
3156 WARN_ON(ret);
3157
3158 if (i_size_read(inode) > 0) {
3159 ret = btrfs_truncate_free_space_cache(root, trans, path,
3160 inode);
3161 if (ret)
3162 goto out_put;
3163 }
3164
3165 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003166 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3167 !btrfs_test_opt(root, SPACE_CACHE)) {
3168 /*
3169 * don't bother trying to write stuff out _if_
3170 * a) we're not cached,
3171 * b) we're with nospace_cache mount option.
3172 */
Josef Bacik2b209822010-12-03 13:17:53 -05003173 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003174 spin_unlock(&block_group->lock);
3175 goto out_put;
3176 }
3177 spin_unlock(&block_group->lock);
3178
Josef Bacik6fc823b2012-08-06 13:46:38 -06003179 /*
3180 * Try to preallocate enough space based on how big the block group is.
3181 * Keep in mind this has to include any pinned space which could end up
3182 * taking up quite a bit since it's not folded into the other space
3183 * cache.
3184 */
3185 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003186 if (!num_pages)
3187 num_pages = 1;
3188
Josef Bacik0af3d002010-06-21 14:48:16 -04003189 num_pages *= 16;
3190 num_pages *= PAGE_CACHE_SIZE;
3191
3192 ret = btrfs_check_data_free_space(inode, num_pages);
3193 if (ret)
3194 goto out_put;
3195
3196 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3197 num_pages, num_pages,
3198 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003199 if (!ret)
3200 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003201 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003202
Josef Bacik0af3d002010-06-21 14:48:16 -04003203out_put:
3204 iput(inode);
3205out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003206 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003207out:
3208 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003209 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003210 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003211 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003212 spin_unlock(&block_group->lock);
3213
3214 return ret;
3215}
3216
Chris Mason96b51792007-10-15 16:15:19 -04003217int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3218 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003219{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003220 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003221 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003222 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003223 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003224
3225 path = btrfs_alloc_path();
3226 if (!path)
3227 return -ENOMEM;
3228
Josef Bacik0af3d002010-06-21 14:48:16 -04003229again:
3230 while (1) {
3231 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3232 while (cache) {
3233 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3234 break;
3235 cache = next_block_group(root, cache);
3236 }
3237 if (!cache) {
3238 if (last == 0)
3239 break;
3240 last = 0;
3241 continue;
3242 }
3243 err = cache_save_setup(cache, trans, path);
3244 last = cache->key.objectid + cache->key.offset;
3245 btrfs_put_block_group(cache);
3246 }
3247
Chris Masond3977122009-01-05 21:25:51 -05003248 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003249 if (last == 0) {
3250 err = btrfs_run_delayed_refs(trans, root,
3251 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003252 if (err) /* File system offline */
3253 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003254 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003255
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003256 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3257 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003258 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3259 btrfs_put_block_group(cache);
3260 goto again;
3261 }
3262
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003263 if (cache->dirty)
3264 break;
3265 cache = next_block_group(root, cache);
3266 }
3267 if (!cache) {
3268 if (last == 0)
3269 break;
3270 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003271 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003272 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003273
Josef Bacik0cb59c92010-07-02 12:14:14 -04003274 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3275 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003276 cache->dirty = 0;
3277 last = cache->key.objectid + cache->key.offset;
3278
3279 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003280 if (err) /* File system offline */
3281 goto out;
3282
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003283 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003284 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003285
Josef Bacik0cb59c92010-07-02 12:14:14 -04003286 while (1) {
3287 /*
3288 * I don't think this is needed since we're just marking our
3289 * preallocated extent as written, but just in case it can't
3290 * hurt.
3291 */
3292 if (last == 0) {
3293 err = btrfs_run_delayed_refs(trans, root,
3294 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003295 if (err) /* File system offline */
3296 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003297 }
3298
3299 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3300 while (cache) {
3301 /*
3302 * Really this shouldn't happen, but it could if we
3303 * couldn't write the entire preallocated extent and
3304 * splitting the extent resulted in a new block.
3305 */
3306 if (cache->dirty) {
3307 btrfs_put_block_group(cache);
3308 goto again;
3309 }
3310 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3311 break;
3312 cache = next_block_group(root, cache);
3313 }
3314 if (!cache) {
3315 if (last == 0)
3316 break;
3317 last = 0;
3318 continue;
3319 }
3320
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003321 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003322
3323 /*
3324 * If we didn't have an error then the cache state is still
3325 * NEED_WRITE, so we can set it to WRITTEN.
3326 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003327 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003328 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3329 last = cache->key.objectid + cache->key.offset;
3330 btrfs_put_block_group(cache);
3331 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003332out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003333
Chris Mason9078a3e2007-04-26 16:46:15 -04003334 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003335 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003336}
3337
Yan Zhengd2fb3432008-12-11 16:30:39 -05003338int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3339{
3340 struct btrfs_block_group_cache *block_group;
3341 int readonly = 0;
3342
3343 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3344 if (!block_group || block_group->ro)
3345 readonly = 1;
3346 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003347 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003348 return readonly;
3349}
3350
Chris Mason593060d2008-03-25 16:50:33 -04003351static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3352 u64 total_bytes, u64 bytes_used,
3353 struct btrfs_space_info **space_info)
3354{
3355 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003356 int i;
3357 int factor;
3358
3359 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3360 BTRFS_BLOCK_GROUP_RAID10))
3361 factor = 2;
3362 else
3363 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003364
3365 found = __find_space_info(info, flags);
3366 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003367 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003368 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003369 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003370 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003371 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003372 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003373 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003374 *space_info = found;
3375 return 0;
3376 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003377 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003378 if (!found)
3379 return -ENOMEM;
3380
Yan, Zhengb742bb82010-05-16 10:46:24 -04003381 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3382 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003383 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003384 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003385 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003386 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003387 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003388 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003389 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003390 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003391 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003392 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003393 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003394 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003395 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003396 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003397 found->flush = 0;
3398 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003399 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003400 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003401 if (flags & BTRFS_BLOCK_GROUP_DATA)
3402 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003403 return 0;
3404}
3405
Chris Mason8790d502008-04-03 16:29:03 -04003406static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3407{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003408 u64 extra_flags = chunk_to_extended(flags) &
3409 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003410
Miao Xiede98ced2013-01-29 10:13:12 +00003411 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003412 if (flags & BTRFS_BLOCK_GROUP_DATA)
3413 fs_info->avail_data_alloc_bits |= extra_flags;
3414 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3415 fs_info->avail_metadata_alloc_bits |= extra_flags;
3416 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3417 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003418 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003419}
Chris Mason593060d2008-03-25 16:50:33 -04003420
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003421/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003422 * returns target flags in extended format or 0 if restripe for this
3423 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003424 *
3425 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003426 */
3427static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3428{
3429 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3430 u64 target = 0;
3431
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003432 if (!bctl)
3433 return 0;
3434
3435 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3436 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3437 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3438 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3439 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3440 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3441 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3442 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3443 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3444 }
3445
3446 return target;
3447}
3448
3449/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003450 * @flags: available profiles in extended format (see ctree.h)
3451 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003452 * Returns reduced profile in chunk format. If profile changing is in
3453 * progress (either running or paused) picks the target profile (if it's
3454 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003455 */
Yan Zheng2b820322008-11-17 21:11:30 -05003456u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003457{
Chris Masoncd02dca2010-12-13 14:56:23 -05003458 /*
3459 * we add in the count of missing devices because we want
3460 * to make sure that any RAID levels on a degraded FS
3461 * continue to be honored.
3462 */
3463 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3464 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003465 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003466 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003467
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003468 /*
3469 * see if restripe for this chunk_type is in progress, if so
3470 * try to reduce to the target profile
3471 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003472 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003473 target = get_restripe_target(root->fs_info, flags);
3474 if (target) {
3475 /* pick target profile only if it's already available */
3476 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003477 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003478 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003479 }
3480 }
3481 spin_unlock(&root->fs_info->balance_lock);
3482
David Woodhouse53b381b2013-01-29 18:40:14 -05003483 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003484 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003485 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3486 BTRFS_BLOCK_GROUP_RAID5);
3487 if (num_devices < 3)
3488 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003489 if (num_devices < 4)
3490 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3491
David Woodhouse53b381b2013-01-29 18:40:14 -05003492 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3493 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3494 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3495 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003496
David Woodhouse53b381b2013-01-29 18:40:14 -05003497 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3498 tmp = BTRFS_BLOCK_GROUP_RAID6;
3499 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3500 tmp = BTRFS_BLOCK_GROUP_RAID5;
3501 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3502 tmp = BTRFS_BLOCK_GROUP_RAID10;
3503 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3504 tmp = BTRFS_BLOCK_GROUP_RAID1;
3505 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3506 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003507
David Woodhouse53b381b2013-01-29 18:40:14 -05003508 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003509}
3510
Yan, Zhengb742bb82010-05-16 10:46:24 -04003511static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003512{
Miao Xiede98ced2013-01-29 10:13:12 +00003513 unsigned seq;
3514
3515 do {
3516 seq = read_seqbegin(&root->fs_info->profiles_lock);
3517
3518 if (flags & BTRFS_BLOCK_GROUP_DATA)
3519 flags |= root->fs_info->avail_data_alloc_bits;
3520 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3521 flags |= root->fs_info->avail_system_alloc_bits;
3522 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3523 flags |= root->fs_info->avail_metadata_alloc_bits;
3524 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003525
Yan, Zhengb742bb82010-05-16 10:46:24 -04003526 return btrfs_reduce_alloc_profile(root, flags);
3527}
Josef Bacik6a632092009-02-20 11:00:09 -05003528
Miao Xie6d07bce2011-01-05 10:07:31 +00003529u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003530{
3531 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003532 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003533
Yan, Zhengb742bb82010-05-16 10:46:24 -04003534 if (data)
3535 flags = BTRFS_BLOCK_GROUP_DATA;
3536 else if (root == root->fs_info->chunk_root)
3537 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3538 else
3539 flags = BTRFS_BLOCK_GROUP_METADATA;
3540
David Woodhouse53b381b2013-01-29 18:40:14 -05003541 ret = get_alloc_profile(root, flags);
3542 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003543}
3544
Josef Bacik6a632092009-02-20 11:00:09 -05003545/*
3546 * This will check the space that the inode allocates from to make sure we have
3547 * enough space for bytes.
3548 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003549int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003550{
3551 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003552 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003553 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003554 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003555 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003556
3557 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003558 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003559
Li Zefan82d59022011-04-20 10:33:24 +08003560 if (root == root->fs_info->tree_root ||
3561 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003562 alloc_chunk = 0;
3563 committed = 1;
3564 }
3565
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003566 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003567 if (!data_sinfo)
3568 goto alloc;
3569
Josef Bacik6a632092009-02-20 11:00:09 -05003570again:
3571 /* make sure we have enough space to handle the data first */
3572 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003573 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3574 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3575 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003576
3577 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003578 struct btrfs_trans_handle *trans;
3579
Josef Bacik6a632092009-02-20 11:00:09 -05003580 /*
3581 * if we don't have enough free bytes in this space then we need
3582 * to alloc a new chunk.
3583 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003584 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003585 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003586
Chris Mason0e4f8f82011-04-15 16:05:44 -04003587 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003588 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003589alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003590 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003591 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003592 if (IS_ERR(trans))
3593 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003594
3595 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003596 alloc_target,
3597 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003598 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003599 if (ret < 0) {
3600 if (ret != -ENOSPC)
3601 return ret;
3602 else
3603 goto commit_trans;
3604 }
Chris Mason33b4d472009-09-22 14:45:50 -04003605
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003606 if (!data_sinfo)
3607 data_sinfo = fs_info->data_sinfo;
3608
Josef Bacik6a632092009-02-20 11:00:09 -05003609 goto again;
3610 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003611
3612 /*
3613 * If we have less pinned bytes than we want to allocate then
3614 * don't bother committing the transaction, it won't help us.
3615 */
3616 if (data_sinfo->bytes_pinned < bytes)
3617 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003618 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003619
3620 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003621commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003622 if (!committed &&
3623 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003624 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003625 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003626 if (IS_ERR(trans))
3627 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003628 ret = btrfs_commit_transaction(trans, root);
3629 if (ret)
3630 return ret;
3631 goto again;
3632 }
3633
Josef Bacik6a632092009-02-20 11:00:09 -05003634 return -ENOSPC;
3635 }
3636 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003637 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003638 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003639 spin_unlock(&data_sinfo->lock);
3640
Josef Bacik9ed74f22009-09-11 16:12:44 -04003641 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003642}
3643
3644/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003645 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003646 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003647void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003648{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003649 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003650 struct btrfs_space_info *data_sinfo;
3651
3652 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003653 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003654
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003655 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003656 spin_lock(&data_sinfo->lock);
3657 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003658 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003659 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003660 spin_unlock(&data_sinfo->lock);
3661}
3662
Josef Bacik97e728d2009-04-21 17:40:57 -04003663static void force_metadata_allocation(struct btrfs_fs_info *info)
3664{
3665 struct list_head *head = &info->space_info;
3666 struct btrfs_space_info *found;
3667
3668 rcu_read_lock();
3669 list_for_each_entry_rcu(found, head, list) {
3670 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003671 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003672 }
3673 rcu_read_unlock();
3674}
3675
Miao Xie3c76cd82013-04-25 10:12:38 +00003676static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3677{
3678 return (global->size << 1);
3679}
3680
Chris Masone5bc2452010-10-26 13:37:56 -04003681static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003682 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003683{
Josef Bacikfb25e912011-07-26 17:00:46 -04003684 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003685 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003686 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003687 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003688
Chris Mason0e4f8f82011-04-15 16:05:44 -04003689 if (force == CHUNK_ALLOC_FORCE)
3690 return 1;
3691
3692 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003693 * We need to take into account the global rsv because for all intents
3694 * and purposes it's used space. Don't worry about locking the
3695 * global_rsv, it doesn't change except when the transaction commits.
3696 */
Josef Bacik54338b52012-08-14 16:20:52 -04003697 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00003698 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04003699
3700 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003701 * in limited mode, we want to have some free space up to
3702 * about 1% of the FS size.
3703 */
3704 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003705 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003706 thresh = max_t(u64, 64 * 1024 * 1024,
3707 div_factor_fine(thresh, 1));
3708
3709 if (num_bytes - num_allocated < thresh)
3710 return 1;
3711 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003712
Josef Bacik698d0082012-09-12 14:08:47 -04003713 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003714 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003715 return 1;
3716}
3717
Liu Bo15d1ff82012-03-29 09:57:44 -04003718static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3719{
3720 u64 num_dev;
3721
David Woodhouse53b381b2013-01-29 18:40:14 -05003722 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3723 BTRFS_BLOCK_GROUP_RAID0 |
3724 BTRFS_BLOCK_GROUP_RAID5 |
3725 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003726 num_dev = root->fs_info->fs_devices->rw_devices;
3727 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3728 num_dev = 2;
3729 else
3730 num_dev = 1; /* DUP or single */
3731
3732 /* metadata for updaing devices and chunk tree */
3733 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3734}
3735
3736static void check_system_chunk(struct btrfs_trans_handle *trans,
3737 struct btrfs_root *root, u64 type)
3738{
3739 struct btrfs_space_info *info;
3740 u64 left;
3741 u64 thresh;
3742
3743 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3744 spin_lock(&info->lock);
3745 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3746 info->bytes_reserved - info->bytes_readonly;
3747 spin_unlock(&info->lock);
3748
3749 thresh = get_system_chunk_thresh(root, type);
3750 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003751 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3752 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04003753 dump_space_info(info, 0, 0);
3754 }
3755
3756 if (left < thresh) {
3757 u64 flags;
3758
3759 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3760 btrfs_alloc_chunk(trans, root, flags);
3761 }
3762}
3763
Chris Mason6324fbf2008-03-24 15:01:59 -04003764static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003765 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003766{
3767 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003768 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003769 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003770 int ret = 0;
3771
Josef Bacikc6b305a2012-12-18 09:16:16 -05003772 /* Don't re-enter if we're already allocating a chunk */
3773 if (trans->allocating_chunk)
3774 return -ENOSPC;
3775
Chris Mason6324fbf2008-03-24 15:01:59 -04003776 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003777 if (!space_info) {
3778 ret = update_space_info(extent_root->fs_info, flags,
3779 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003780 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003781 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003782 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003783
Josef Bacik6d741192011-04-11 20:20:11 -04003784again:
Josef Bacik25179202008-10-29 14:49:05 -04003785 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003786 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003787 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003788 if (space_info->full) {
3789 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003790 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003791 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003792
Josef Bacik698d0082012-09-12 14:08:47 -04003793 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003794 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003795 return 0;
3796 } else if (space_info->chunk_alloc) {
3797 wait_for_alloc = 1;
3798 } else {
3799 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003800 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003801
Josef Bacik25179202008-10-29 14:49:05 -04003802 spin_unlock(&space_info->lock);
3803
Josef Bacik6d741192011-04-11 20:20:11 -04003804 mutex_lock(&fs_info->chunk_mutex);
3805
3806 /*
3807 * The chunk_mutex is held throughout the entirety of a chunk
3808 * allocation, so once we've acquired the chunk_mutex we know that the
3809 * other guy is done and we need to recheck and see if we should
3810 * allocate.
3811 */
3812 if (wait_for_alloc) {
3813 mutex_unlock(&fs_info->chunk_mutex);
3814 wait_for_alloc = 0;
3815 goto again;
3816 }
3817
Josef Bacikc6b305a2012-12-18 09:16:16 -05003818 trans->allocating_chunk = true;
3819
Josef Bacik97e728d2009-04-21 17:40:57 -04003820 /*
Josef Bacik67377732010-09-16 16:19:09 -04003821 * If we have mixed data/metadata chunks we want to make sure we keep
3822 * allocating mixed chunks instead of individual chunks.
3823 */
3824 if (btrfs_mixed_space_info(space_info))
3825 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3826
3827 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003828 * if we're doing a data chunk, go ahead and make sure that
3829 * we keep a reasonable number of metadata chunks allocated in the
3830 * FS as well.
3831 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003832 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003833 fs_info->data_chunk_allocations++;
3834 if (!(fs_info->data_chunk_allocations %
3835 fs_info->metadata_ratio))
3836 force_metadata_allocation(fs_info);
3837 }
3838
Liu Bo15d1ff82012-03-29 09:57:44 -04003839 /*
3840 * Check if we have enough space in SYSTEM chunk because we may need
3841 * to update devices.
3842 */
3843 check_system_chunk(trans, extent_root, flags);
3844
Yan Zheng2b820322008-11-17 21:11:30 -05003845 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003846 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003847
Josef Bacik9ed74f22009-09-11 16:12:44 -04003848 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003849 if (ret < 0 && ret != -ENOSPC)
3850 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003851 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003852 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003853 else
3854 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003855
Chris Mason0e4f8f82011-04-15 16:05:44 -04003856 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003857out:
Josef Bacik6d741192011-04-11 20:20:11 -04003858 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003859 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003860 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003861 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003862}
3863
Josef Bacika80c8dc2012-09-06 16:59:33 -04003864static int can_overcommit(struct btrfs_root *root,
3865 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003866 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003867{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003868 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003869 u64 profile = btrfs_get_alloc_profile(root, 0);
Miao Xie3c76cd82013-04-25 10:12:38 +00003870 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003871 u64 avail;
3872 u64 used;
Josef Bacik70afa392013-02-06 13:53:19 -05003873 u64 to_add;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003874
3875 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003876 space_info->bytes_pinned + space_info->bytes_readonly;
3877
Josef Bacik96f1bb52013-01-30 17:02:51 -05003878 /*
3879 * We only want to allow over committing if we have lots of actual space
3880 * free, but if we don't have enough space to handle the global reserve
3881 * space then we could end up having a real enospc problem when trying
3882 * to allocate a chunk or some other such important allocation.
3883 */
Miao Xie3c76cd82013-04-25 10:12:38 +00003884 spin_lock(&global_rsv->lock);
3885 space_size = calc_global_rsv_need_space(global_rsv);
3886 spin_unlock(&global_rsv->lock);
3887 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05003888 return 0;
3889
3890 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003891
3892 spin_lock(&root->fs_info->free_chunk_lock);
3893 avail = root->fs_info->free_chunk_space;
3894 spin_unlock(&root->fs_info->free_chunk_lock);
3895
3896 /*
3897 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003898 * space is actually useable. For raid56, the space info used
3899 * doesn't include the parity drive, so we don't have to
3900 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003901 */
3902 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3903 BTRFS_BLOCK_GROUP_RAID1 |
3904 BTRFS_BLOCK_GROUP_RAID10))
3905 avail >>= 1;
3906
Josef Bacik70afa392013-02-06 13:53:19 -05003907 to_add = space_info->total_bytes;
3908
Josef Bacika80c8dc2012-09-06 16:59:33 -04003909 /*
Miao Xie561c2942012-10-16 11:32:18 +00003910 * If we aren't flushing all things, let us overcommit up to
3911 * 1/2th of the space. If we can flush, don't let us overcommit
3912 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003913 */
Miao Xie08e007d2012-10-16 11:33:38 +00003914 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik70afa392013-02-06 13:53:19 -05003915 to_add >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003916 else
Josef Bacik70afa392013-02-06 13:53:19 -05003917 to_add >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003918
Josef Bacik70afa392013-02-06 13:53:19 -05003919 /*
3920 * Limit the overcommit to the amount of free space we could possibly
3921 * allocate for chunks.
3922 */
3923 to_add = min(avail, to_add);
3924
3925 if (used + bytes < space_info->total_bytes + to_add)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003926 return 1;
3927 return 0;
3928}
3929
Miao Xieda633a42012-12-20 11:19:09 +00003930void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3931 unsigned long nr_pages)
3932{
3933 struct super_block *sb = root->fs_info->sb;
3934 int started;
3935
3936 /* If we can not start writeback, just sync all the delalloc file. */
Linus Torvaldsb6951882013-03-02 16:41:54 -08003937 started = try_to_writeback_inodes_sb_nr(sb, nr_pages,
Miao Xieda633a42012-12-20 11:19:09 +00003938 WB_REASON_FS_FREE_SPACE);
3939 if (!started) {
3940 /*
3941 * We needn't worry the filesystem going from r/w to r/o though
3942 * we don't acquire ->s_umount mutex, because the filesystem
3943 * should guarantee the delalloc inodes list be empty after
3944 * the filesystem is readonly(all dirty pages are written to
3945 * the disk).
3946 */
3947 btrfs_start_delalloc_inodes(root, 0);
Josef Bacik98ad69c2013-04-04 11:55:49 -04003948 if (!current->journal_info)
3949 btrfs_wait_ordered_extents(root, 0);
Miao Xieda633a42012-12-20 11:19:09 +00003950 }
3951}
3952
Yan, Zheng5da9d012010-05-16 10:46:25 -04003953/*
3954 * shrink metadata reservation for delalloc
3955 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003956static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3957 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003958{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003959 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003960 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003961 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003962 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003963 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003964 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003965 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003966 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003967 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003968
Josef Bacik663350a2011-11-03 22:54:25 -04003969 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003970 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003971 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003972
3973 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003974 delalloc_bytes = percpu_counter_sum_positive(
3975 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003976 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003977 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003978 return;
Liu Bo6bbe3a92012-09-14 02:58:07 -06003979 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003980 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003981 }
3982
Josef Bacikf4c738c2012-07-02 17:10:51 -04003983 while (delalloc_bytes && loops < 3) {
3984 max_reclaim = min(delalloc_bytes, to_reclaim);
3985 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00003986 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04003987 /*
3988 * We need to wait for the async pages to actually start before
3989 * we do anything.
3990 */
3991 wait_event(root->fs_info->async_submit_wait,
3992 !atomic_read(&root->fs_info->async_delalloc_pages));
3993
Miao Xie08e007d2012-10-16 11:33:38 +00003994 if (!trans)
3995 flush = BTRFS_RESERVE_FLUSH_ALL;
3996 else
3997 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003998 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003999 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004000 spin_unlock(&space_info->lock);
4001 break;
4002 }
Josef Bacik0019f102010-10-15 15:18:40 -04004003 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004004
Chris Mason36e39c42011-03-12 07:08:42 -05004005 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004006 if (wait_ordered && !trans) {
Liu Bo6bbe3a92012-09-14 02:58:07 -06004007 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04004008 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004009 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004010 if (time_left)
4011 break;
4012 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04004013 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00004014 delalloc_bytes = percpu_counter_sum_positive(
4015 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004016 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004017}
4018
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004019/**
Josef Bacik663350a2011-11-03 22:54:25 -04004020 * maybe_commit_transaction - possibly commit the transaction if its ok to
4021 * @root - the root we're allocating for
4022 * @bytes - the number of bytes we want to reserve
4023 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004024 *
Josef Bacik663350a2011-11-03 22:54:25 -04004025 * This will check to make sure that committing the transaction will actually
4026 * get us somewhere and then commit the transaction if it does. Otherwise it
4027 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004028 */
Josef Bacik663350a2011-11-03 22:54:25 -04004029static int may_commit_transaction(struct btrfs_root *root,
4030 struct btrfs_space_info *space_info,
4031 u64 bytes, int force)
4032{
4033 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4034 struct btrfs_trans_handle *trans;
4035
4036 trans = (struct btrfs_trans_handle *)current->journal_info;
4037 if (trans)
4038 return -EAGAIN;
4039
4040 if (force)
4041 goto commit;
4042
4043 /* See if there is enough pinned space to make this reservation */
4044 spin_lock(&space_info->lock);
4045 if (space_info->bytes_pinned >= bytes) {
4046 spin_unlock(&space_info->lock);
4047 goto commit;
4048 }
4049 spin_unlock(&space_info->lock);
4050
4051 /*
4052 * See if there is some space in the delayed insertion reservation for
4053 * this reservation.
4054 */
4055 if (space_info != delayed_rsv->space_info)
4056 return -ENOSPC;
4057
Liu Bod9b02182012-02-16 18:34:39 +08004058 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004059 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004060 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04004061 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004062 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004063 return -ENOSPC;
4064 }
4065 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004066 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004067
4068commit:
4069 trans = btrfs_join_transaction(root);
4070 if (IS_ERR(trans))
4071 return -ENOSPC;
4072
4073 return btrfs_commit_transaction(trans, root);
4074}
4075
Josef Bacik96c3f432012-06-21 14:05:49 -04004076enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004077 FLUSH_DELAYED_ITEMS_NR = 1,
4078 FLUSH_DELAYED_ITEMS = 2,
4079 FLUSH_DELALLOC = 3,
4080 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004081 ALLOC_CHUNK = 5,
4082 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004083};
4084
4085static int flush_space(struct btrfs_root *root,
4086 struct btrfs_space_info *space_info, u64 num_bytes,
4087 u64 orig_bytes, int state)
4088{
4089 struct btrfs_trans_handle *trans;
4090 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004091 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004092
4093 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004094 case FLUSH_DELAYED_ITEMS_NR:
4095 case FLUSH_DELAYED_ITEMS:
4096 if (state == FLUSH_DELAYED_ITEMS_NR) {
4097 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
4098
4099 nr = (int)div64_u64(num_bytes, bytes);
4100 if (!nr)
4101 nr = 1;
4102 nr *= 2;
4103 } else {
4104 nr = -1;
4105 }
4106 trans = btrfs_join_transaction(root);
4107 if (IS_ERR(trans)) {
4108 ret = PTR_ERR(trans);
4109 break;
4110 }
4111 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4112 btrfs_end_transaction(trans, root);
4113 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004114 case FLUSH_DELALLOC:
4115 case FLUSH_DELALLOC_WAIT:
4116 shrink_delalloc(root, num_bytes, orig_bytes,
4117 state == FLUSH_DELALLOC_WAIT);
4118 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004119 case ALLOC_CHUNK:
4120 trans = btrfs_join_transaction(root);
4121 if (IS_ERR(trans)) {
4122 ret = PTR_ERR(trans);
4123 break;
4124 }
4125 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004126 btrfs_get_alloc_profile(root, 0),
4127 CHUNK_ALLOC_NO_FORCE);
4128 btrfs_end_transaction(trans, root);
4129 if (ret == -ENOSPC)
4130 ret = 0;
4131 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004132 case COMMIT_TRANS:
4133 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4134 break;
4135 default:
4136 ret = -ENOSPC;
4137 break;
4138 }
4139
4140 return ret;
4141}
Josef Bacik663350a2011-11-03 22:54:25 -04004142/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004143 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4144 * @root - the root we're allocating for
4145 * @block_rsv - the block_rsv we're allocating for
4146 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004147 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004148 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004149 * This will reserve orgi_bytes number of bytes from the space info associated
4150 * with the block_rsv. If there is not enough space it will make an attempt to
4151 * flush out space to make room. It will do this by flushing delalloc if
4152 * possible or committing the transaction. If flush is 0 then no attempts to
4153 * regain reservations will be made and this will fail if there is not enough
4154 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004155 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004156static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004157 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004158 u64 orig_bytes,
4159 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004160{
4161 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004162 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004163 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004164 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004165 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004166 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004167
4168again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004169 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004170 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004171 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004172 * We only want to wait if somebody other than us is flushing and we
4173 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004174 */
Miao Xie08e007d2012-10-16 11:33:38 +00004175 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4176 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004177 spin_unlock(&space_info->lock);
4178 /*
4179 * If we have a trans handle we can't wait because the flusher
4180 * may have to commit the transaction, which would mean we would
4181 * deadlock since we are waiting for the flusher to finish, but
4182 * hold the current transaction open.
4183 */
Josef Bacik663350a2011-11-03 22:54:25 -04004184 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004185 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004186 ret = wait_event_killable(space_info->wait, !space_info->flush);
4187 /* Must have been killed, return */
4188 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004189 return -EINTR;
4190
4191 spin_lock(&space_info->lock);
4192 }
4193
4194 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004195 used = space_info->bytes_used + space_info->bytes_reserved +
4196 space_info->bytes_pinned + space_info->bytes_readonly +
4197 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004198
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004199 /*
4200 * The idea here is that we've not already over-reserved the block group
4201 * then we can go ahead and save our reservation first and then start
4202 * flushing if we need to. Otherwise if we've already overcommitted
4203 * lets start flushing stuff first and then come back and try to make
4204 * our reservation.
4205 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004206 if (used <= space_info->total_bytes) {
4207 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004208 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004209 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004210 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004211 ret = 0;
4212 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004213 /*
4214 * Ok set num_bytes to orig_bytes since we aren't
4215 * overocmmitted, this way we only try and reclaim what
4216 * we need.
4217 */
4218 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004219 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004220 } else {
4221 /*
4222 * Ok we're over committed, set num_bytes to the overcommitted
4223 * amount plus the amount of bytes that we need for this
4224 * reservation.
4225 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004226 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004227 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004228 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004229
Josef Bacik44734ed2012-09-28 16:04:19 -04004230 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4231 space_info->bytes_may_use += orig_bytes;
4232 trace_btrfs_space_reservation(root->fs_info, "space_info",
4233 space_info->flags, orig_bytes,
4234 1);
4235 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004236 }
4237
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004238 /*
4239 * Couldn't make our reservation, save our place so while we're trying
4240 * to reclaim space we can actually use it instead of somebody else
4241 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004242 *
4243 * We make the other tasks wait for the flush only when we can flush
4244 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004245 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004246 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004247 flushing = true;
4248 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004249 }
4250
Yan, Zhengf0486c62010-05-16 10:46:25 -04004251 spin_unlock(&space_info->lock);
4252
Miao Xie08e007d2012-10-16 11:33:38 +00004253 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004254 goto out;
4255
Josef Bacik96c3f432012-06-21 14:05:49 -04004256 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4257 flush_state);
4258 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004259
4260 /*
4261 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4262 * would happen. So skip delalloc flush.
4263 */
4264 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4265 (flush_state == FLUSH_DELALLOC ||
4266 flush_state == FLUSH_DELALLOC_WAIT))
4267 flush_state = ALLOC_CHUNK;
4268
Josef Bacik96c3f432012-06-21 14:05:49 -04004269 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004270 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004271 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4272 flush_state < COMMIT_TRANS)
4273 goto again;
4274 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4275 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004276 goto again;
4277
4278out:
Josef Bacik5d803662013-02-07 16:06:02 -05004279 if (ret == -ENOSPC &&
4280 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4281 struct btrfs_block_rsv *global_rsv =
4282 &root->fs_info->global_block_rsv;
4283
4284 if (block_rsv != global_rsv &&
4285 !block_rsv_use_bytes(global_rsv, orig_bytes))
4286 ret = 0;
4287 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004288 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004289 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004290 space_info->flush = 0;
4291 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004292 spin_unlock(&space_info->lock);
4293 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004294 return ret;
4295}
4296
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004297static struct btrfs_block_rsv *get_block_rsv(
4298 const struct btrfs_trans_handle *trans,
4299 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004300{
Josef Bacik4c13d752011-08-30 11:31:29 -04004301 struct btrfs_block_rsv *block_rsv = NULL;
4302
Josef Bacik0e721102012-06-26 16:13:18 -04004303 if (root->ref_cows)
4304 block_rsv = trans->block_rsv;
4305
4306 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004307 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004308
4309 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004310 block_rsv = root->block_rsv;
4311
4312 if (!block_rsv)
4313 block_rsv = &root->fs_info->empty_block_rsv;
4314
4315 return block_rsv;
4316}
4317
4318static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4319 u64 num_bytes)
4320{
4321 int ret = -ENOSPC;
4322 spin_lock(&block_rsv->lock);
4323 if (block_rsv->reserved >= num_bytes) {
4324 block_rsv->reserved -= num_bytes;
4325 if (block_rsv->reserved < block_rsv->size)
4326 block_rsv->full = 0;
4327 ret = 0;
4328 }
4329 spin_unlock(&block_rsv->lock);
4330 return ret;
4331}
4332
4333static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4334 u64 num_bytes, int update_size)
4335{
4336 spin_lock(&block_rsv->lock);
4337 block_rsv->reserved += num_bytes;
4338 if (update_size)
4339 block_rsv->size += num_bytes;
4340 else if (block_rsv->reserved >= block_rsv->size)
4341 block_rsv->full = 1;
4342 spin_unlock(&block_rsv->lock);
4343}
4344
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004345static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4346 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004347 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004348{
4349 struct btrfs_space_info *space_info = block_rsv->space_info;
4350
4351 spin_lock(&block_rsv->lock);
4352 if (num_bytes == (u64)-1)
4353 num_bytes = block_rsv->size;
4354 block_rsv->size -= num_bytes;
4355 if (block_rsv->reserved >= block_rsv->size) {
4356 num_bytes = block_rsv->reserved - block_rsv->size;
4357 block_rsv->reserved = block_rsv->size;
4358 block_rsv->full = 1;
4359 } else {
4360 num_bytes = 0;
4361 }
4362 spin_unlock(&block_rsv->lock);
4363
4364 if (num_bytes > 0) {
4365 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004366 spin_lock(&dest->lock);
4367 if (!dest->full) {
4368 u64 bytes_to_add;
4369
4370 bytes_to_add = dest->size - dest->reserved;
4371 bytes_to_add = min(num_bytes, bytes_to_add);
4372 dest->reserved += bytes_to_add;
4373 if (dest->reserved >= dest->size)
4374 dest->full = 1;
4375 num_bytes -= bytes_to_add;
4376 }
4377 spin_unlock(&dest->lock);
4378 }
4379 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004380 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004381 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004382 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004383 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004384 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004385 spin_unlock(&space_info->lock);
4386 }
4387 }
4388}
4389
4390static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4391 struct btrfs_block_rsv *dst, u64 num_bytes)
4392{
4393 int ret;
4394
4395 ret = block_rsv_use_bytes(src, num_bytes);
4396 if (ret)
4397 return ret;
4398
4399 block_rsv_add_bytes(dst, num_bytes, 1);
4400 return 0;
4401}
4402
Miao Xie66d8f3d2012-09-06 04:02:28 -06004403void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004404{
4405 memset(rsv, 0, sizeof(*rsv));
4406 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004407 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004408}
4409
Miao Xie66d8f3d2012-09-06 04:02:28 -06004410struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4411 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004412{
4413 struct btrfs_block_rsv *block_rsv;
4414 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004415
4416 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4417 if (!block_rsv)
4418 return NULL;
4419
Miao Xie66d8f3d2012-09-06 04:02:28 -06004420 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004421 block_rsv->space_info = __find_space_info(fs_info,
4422 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004423 return block_rsv;
4424}
4425
4426void btrfs_free_block_rsv(struct btrfs_root *root,
4427 struct btrfs_block_rsv *rsv)
4428{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004429 if (!rsv)
4430 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004431 btrfs_block_rsv_release(root, rsv, (u64)-1);
4432 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004433}
4434
Miao Xie08e007d2012-10-16 11:33:38 +00004435int btrfs_block_rsv_add(struct btrfs_root *root,
4436 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4437 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004438{
4439 int ret;
4440
4441 if (num_bytes == 0)
4442 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004443
Miao Xie61b520a2011-11-10 20:45:05 -05004444 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004445 if (!ret) {
4446 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4447 return 0;
4448 }
4449
Yan, Zhengf0486c62010-05-16 10:46:25 -04004450 return ret;
4451}
4452
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004453int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004454 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004455{
4456 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004457 int ret = -ENOSPC;
4458
4459 if (!block_rsv)
4460 return 0;
4461
4462 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004463 num_bytes = div_factor(block_rsv->size, min_factor);
4464 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004465 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004466 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004467
Josef Bacik36ba0222011-10-18 12:15:48 -04004468 return ret;
4469}
4470
Miao Xie08e007d2012-10-16 11:33:38 +00004471int btrfs_block_rsv_refill(struct btrfs_root *root,
4472 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4473 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004474{
4475 u64 num_bytes = 0;
4476 int ret = -ENOSPC;
4477
4478 if (!block_rsv)
4479 return 0;
4480
4481 spin_lock(&block_rsv->lock);
4482 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004483 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004484 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004485 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004486 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004487 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004488
Yan, Zhengf0486c62010-05-16 10:46:25 -04004489 if (!ret)
4490 return 0;
4491
Miao Xieaa38a712011-11-18 17:43:00 +08004492 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004493 if (!ret) {
4494 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004495 return 0;
4496 }
4497
Josef Bacik13553e52011-08-08 13:33:21 -04004498 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004499}
4500
4501int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4502 struct btrfs_block_rsv *dst_rsv,
4503 u64 num_bytes)
4504{
4505 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4506}
4507
4508void btrfs_block_rsv_release(struct btrfs_root *root,
4509 struct btrfs_block_rsv *block_rsv,
4510 u64 num_bytes)
4511{
4512 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4513 if (global_rsv->full || global_rsv == block_rsv ||
4514 block_rsv->space_info != global_rsv->space_info)
4515 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004516 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4517 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004518}
4519
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004520/*
4521 * helper to calculate size of global block reservation.
4522 * the desired value is sum of space used by extent tree,
4523 * checksum tree and root tree
4524 */
4525static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4526{
4527 struct btrfs_space_info *sinfo;
4528 u64 num_bytes;
4529 u64 meta_used;
4530 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004531 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004532
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004533 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4534 spin_lock(&sinfo->lock);
4535 data_used = sinfo->bytes_used;
4536 spin_unlock(&sinfo->lock);
4537
4538 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4539 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004540 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4541 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004542 meta_used = sinfo->bytes_used;
4543 spin_unlock(&sinfo->lock);
4544
4545 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4546 csum_size * 2;
4547 num_bytes += div64_u64(data_used + meta_used, 50);
4548
4549 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004550 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004551
4552 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4553}
4554
4555static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4556{
4557 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4558 struct btrfs_space_info *sinfo = block_rsv->space_info;
4559 u64 num_bytes;
4560
4561 num_bytes = calc_global_metadata_size(fs_info);
4562
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004563 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004564 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004565
Josef Bacikfdf30d12013-03-26 15:31:45 -04004566 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004567
4568 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004569 sinfo->bytes_reserved + sinfo->bytes_readonly +
4570 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004571
4572 if (sinfo->total_bytes > num_bytes) {
4573 num_bytes = sinfo->total_bytes - num_bytes;
4574 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004575 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004576 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004577 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004578 }
4579
4580 if (block_rsv->reserved >= block_rsv->size) {
4581 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004582 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004583 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004584 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004585 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004586 block_rsv->reserved = block_rsv->size;
4587 block_rsv->full = 1;
4588 }
David Sterba182608c2011-05-05 13:13:16 +02004589
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004590 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004591 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004592}
4593
Yan, Zhengf0486c62010-05-16 10:46:25 -04004594static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4595{
4596 struct btrfs_space_info *space_info;
4597
4598 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4599 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004600
4601 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004602 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004603 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004604 fs_info->trans_block_rsv.space_info = space_info;
4605 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004606 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004607
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004608 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4609 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4610 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4611 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004612 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004613
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004614 update_global_block_rsv(fs_info);
4615}
4616
4617static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4618{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004619 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4620 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004621 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4622 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4623 WARN_ON(fs_info->trans_block_rsv.size > 0);
4624 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4625 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4626 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004627 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4628 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004629}
4630
Yan, Zhenga22285a2010-05-16 10:48:46 -04004631void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4632 struct btrfs_root *root)
4633{
Josef Bacik0e721102012-06-26 16:13:18 -04004634 if (!trans->block_rsv)
4635 return;
4636
Yan, Zhenga22285a2010-05-16 10:48:46 -04004637 if (!trans->bytes_reserved)
4638 return;
4639
Chris Masone77266e2012-02-24 10:39:05 -05004640 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004641 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004642 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004643 trans->bytes_reserved = 0;
4644}
4645
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004646/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004647int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4648 struct inode *inode)
4649{
4650 struct btrfs_root *root = BTRFS_I(inode)->root;
4651 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4652 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4653
4654 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004655 * We need to hold space in order to delete our orphan item once we've
4656 * added it, so this takes the reservation so we can release it later
4657 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004658 */
Chris Masonff5714c2011-05-28 07:00:39 -04004659 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004660 trace_btrfs_space_reservation(root->fs_info, "orphan",
4661 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004662 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4663}
4664
4665void btrfs_orphan_release_metadata(struct inode *inode)
4666{
4667 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004668 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004669 trace_btrfs_space_reservation(root->fs_info, "orphan",
4670 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004671 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4672}
4673
Miao Xied5c12072013-02-28 10:04:33 +00004674/*
4675 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4676 * root: the root of the parent directory
4677 * rsv: block reservation
4678 * items: the number of items that we need do reservation
4679 * qgroup_reserved: used to return the reserved size in qgroup
4680 *
4681 * This function is used to reserve the space for snapshot/subvolume
4682 * creation and deletion. Those operations are different with the
4683 * common file/directory operations, they change two fs/file trees
4684 * and root tree, the number of items that the qgroup reserves is
4685 * different with the free space reservation. So we can not use
4686 * the space reseravtion mechanism in start_transaction().
4687 */
4688int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4689 struct btrfs_block_rsv *rsv,
4690 int items,
4691 u64 *qgroup_reserved)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004692{
Miao Xied5c12072013-02-28 10:04:33 +00004693 u64 num_bytes;
4694 int ret;
4695
4696 if (root->fs_info->quota_enabled) {
4697 /* One for parent inode, two for dir entries */
4698 num_bytes = 3 * root->leafsize;
4699 ret = btrfs_qgroup_reserve(root, num_bytes);
4700 if (ret)
4701 return ret;
4702 } else {
4703 num_bytes = 0;
4704 }
4705
4706 *qgroup_reserved = num_bytes;
4707
4708 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4709 rsv->space_info = __find_space_info(root->fs_info,
4710 BTRFS_BLOCK_GROUP_METADATA);
4711 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4712 BTRFS_RESERVE_FLUSH_ALL);
4713 if (ret) {
4714 if (*qgroup_reserved)
4715 btrfs_qgroup_free(root, *qgroup_reserved);
4716 }
4717
4718 return ret;
4719}
4720
4721void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4722 struct btrfs_block_rsv *rsv,
4723 u64 qgroup_reserved)
4724{
4725 btrfs_block_rsv_release(root, rsv, (u64)-1);
4726 if (qgroup_reserved)
4727 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004728}
4729
Josef Bacik7709cde2011-08-04 10:25:02 -04004730/**
4731 * drop_outstanding_extent - drop an outstanding extent
4732 * @inode: the inode we're dropping the extent for
4733 *
4734 * This is called when we are freeing up an outstanding extent, either called
4735 * after an error or after an extent is written. This will return the number of
4736 * reserved extents that need to be freed. This must be called with
4737 * BTRFS_I(inode)->lock held.
4738 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004739static unsigned drop_outstanding_extent(struct inode *inode)
4740{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004741 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004742 unsigned dropped_extents = 0;
4743
Josef Bacik9e0baf62011-07-15 15:16:44 +00004744 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4745 BTRFS_I(inode)->outstanding_extents--;
4746
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004747 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004748 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4749 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004750 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004751
Josef Bacik9e0baf62011-07-15 15:16:44 +00004752 /*
4753 * If we have more or the same amount of outsanding extents than we have
4754 * reserved then we need to leave the reserved extents count alone.
4755 */
4756 if (BTRFS_I(inode)->outstanding_extents >=
4757 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004758 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004759
4760 dropped_extents = BTRFS_I(inode)->reserved_extents -
4761 BTRFS_I(inode)->outstanding_extents;
4762 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004763 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004764}
4765
Josef Bacik7709cde2011-08-04 10:25:02 -04004766/**
4767 * calc_csum_metadata_size - return the amount of metada space that must be
4768 * reserved/free'd for the given bytes.
4769 * @inode: the inode we're manipulating
4770 * @num_bytes: the number of bytes in question
4771 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4772 *
4773 * This adjusts the number of csum_bytes in the inode and then returns the
4774 * correct amount of metadata that must either be reserved or freed. We
4775 * calculate how many checksums we can fit into one leaf and then divide the
4776 * number of bytes that will need to be checksumed by this value to figure out
4777 * how many checksums will be required. If we are adding bytes then the number
4778 * may go up and we will return the number of additional bytes that must be
4779 * reserved. If it is going down we will return the number of bytes that must
4780 * be freed.
4781 *
4782 * This must be called with BTRFS_I(inode)->lock held.
4783 */
4784static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4785 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004786{
Josef Bacik7709cde2011-08-04 10:25:02 -04004787 struct btrfs_root *root = BTRFS_I(inode)->root;
4788 u64 csum_size;
4789 int num_csums_per_leaf;
4790 int num_csums;
4791 int old_csums;
4792
4793 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4794 BTRFS_I(inode)->csum_bytes == 0)
4795 return 0;
4796
4797 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4798 if (reserve)
4799 BTRFS_I(inode)->csum_bytes += num_bytes;
4800 else
4801 BTRFS_I(inode)->csum_bytes -= num_bytes;
4802 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4803 num_csums_per_leaf = (int)div64_u64(csum_size,
4804 sizeof(struct btrfs_csum_item) +
4805 sizeof(struct btrfs_disk_key));
4806 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4807 num_csums = num_csums + num_csums_per_leaf - 1;
4808 num_csums = num_csums / num_csums_per_leaf;
4809
4810 old_csums = old_csums + num_csums_per_leaf - 1;
4811 old_csums = old_csums / num_csums_per_leaf;
4812
4813 /* No change, no need to reserve more */
4814 if (old_csums == num_csums)
4815 return 0;
4816
4817 if (reserve)
4818 return btrfs_calc_trans_metadata_size(root,
4819 num_csums - old_csums);
4820
4821 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004822}
4823
4824int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4825{
4826 struct btrfs_root *root = BTRFS_I(inode)->root;
4827 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004828 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004829 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004830 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004831 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004832 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004833 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004834 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004835 u64 to_free = 0;
4836 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004837
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004838 /* If we are a free space inode we need to not flush since we will be in
4839 * the middle of a transaction commit. We also don't need the delalloc
4840 * mutex since we won't race with anybody. We need this mostly to make
4841 * lockdep shut its filthy mouth.
4842 */
4843 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004844 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004845 delalloc_lock = false;
4846 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004847
Miao Xie08e007d2012-10-16 11:33:38 +00004848 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4849 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004850 schedule_timeout(1);
4851
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004852 if (delalloc_lock)
4853 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4854
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004855 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004856
Josef Bacik9e0baf62011-07-15 15:16:44 +00004857 spin_lock(&BTRFS_I(inode)->lock);
4858 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004859
Josef Bacik9e0baf62011-07-15 15:16:44 +00004860 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004861 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004862 nr_extents = BTRFS_I(inode)->outstanding_extents -
4863 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004864
4865 /*
4866 * Add an item to reserve for updating the inode when we complete the
4867 * delalloc io.
4868 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004869 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4870 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004871 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004872 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004873 }
4874
4875 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004876 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004877 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004878 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004879
Wang Shilong88e081bf2013-03-01 11:36:01 +00004880 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02004881 ret = btrfs_qgroup_reserve(root, num_bytes +
4882 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004883 if (ret)
4884 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00004885 }
Arne Jansenc5567232011-09-14 15:44:05 +02004886
Wang Shilong88e081bf2013-03-01 11:36:01 +00004887 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
4888 if (unlikely(ret)) {
4889 if (root->fs_info->quota_enabled)
Miao Xie4b5829a2012-12-05 10:53:25 +00004890 btrfs_qgroup_free(root, num_bytes +
4891 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004892 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004893 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004894
Josef Bacik660d3f62011-12-09 11:18:51 -05004895 spin_lock(&BTRFS_I(inode)->lock);
4896 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004897 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4898 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004899 nr_extents--;
4900 }
4901 BTRFS_I(inode)->reserved_extents += nr_extents;
4902 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004903
4904 if (delalloc_lock)
4905 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004906
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004907 if (to_reserve)
4908 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4909 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004910 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4911
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004912 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004913
4914out_fail:
4915 spin_lock(&BTRFS_I(inode)->lock);
4916 dropped = drop_outstanding_extent(inode);
4917 /*
4918 * If the inodes csum_bytes is the same as the original
4919 * csum_bytes then we know we haven't raced with any free()ers
4920 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00004921 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04004922 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00004923 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04004924 } else {
4925 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
4926 u64 bytes;
4927
4928 /*
4929 * This is tricky, but first we need to figure out how much we
4930 * free'd from any free-ers that occured during this
4931 * reservation, so we reset ->csum_bytes to the csum_bytes
4932 * before we dropped our lock, and then call the free for the
4933 * number of bytes that were freed while we were trying our
4934 * reservation.
4935 */
4936 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
4937 BTRFS_I(inode)->csum_bytes = csum_bytes;
4938 to_free = calc_csum_metadata_size(inode, bytes, 0);
4939
4940
4941 /*
4942 * Now we need to see how much we would have freed had we not
4943 * been making this reservation and our ->csum_bytes were not
4944 * artificially inflated.
4945 */
4946 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
4947 bytes = csum_bytes - orig_csum_bytes;
4948 bytes = calc_csum_metadata_size(inode, bytes, 0);
4949
4950 /*
4951 * Now reset ->csum_bytes to what it should be. If bytes is
4952 * more than to_free then we would have free'd more space had we
4953 * not had an artificially high ->csum_bytes, so we need to free
4954 * the remainder. If bytes is the same or less then we don't
4955 * need to do anything, the other free-ers did the correct
4956 * thing.
4957 */
4958 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
4959 if (bytes > to_free)
4960 to_free = bytes - to_free;
4961 else
4962 to_free = 0;
4963 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00004964 spin_unlock(&BTRFS_I(inode)->lock);
4965 if (dropped)
4966 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4967
4968 if (to_free) {
4969 btrfs_block_rsv_release(root, block_rsv, to_free);
4970 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4971 btrfs_ino(inode), to_free, 0);
4972 }
4973 if (delalloc_lock)
4974 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
4975 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004976}
4977
Josef Bacik7709cde2011-08-04 10:25:02 -04004978/**
4979 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4980 * @inode: the inode to release the reservation for
4981 * @num_bytes: the number of bytes we're releasing
4982 *
4983 * This will release the metadata reservation for an inode. This can be called
4984 * once we complete IO for a given set of bytes to release their metadata
4985 * reservations.
4986 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004987void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4988{
4989 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004990 u64 to_free = 0;
4991 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004992
4993 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004994 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004995 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004996
Miao Xie09348562013-02-07 10:12:07 +00004997 if (num_bytes)
4998 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04004999 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005000 if (dropped > 0)
5001 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005002
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005003 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5004 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02005005 if (root->fs_info->quota_enabled) {
5006 btrfs_qgroup_free(root, num_bytes +
5007 dropped * root->leafsize);
5008 }
5009
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005010 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5011 to_free);
5012}
5013
Josef Bacik7709cde2011-08-04 10:25:02 -04005014/**
5015 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5016 * @inode: inode we're writing to
5017 * @num_bytes: the number of bytes we want to allocate
5018 *
5019 * This will do the following things
5020 *
5021 * o reserve space in the data space info for num_bytes
5022 * o reserve space in the metadata space info based on number of outstanding
5023 * extents and how much csums will be needed
5024 * o add to the inodes ->delalloc_bytes
5025 * o add it to the fs_info's delalloc inodes list.
5026 *
5027 * This will return 0 for success and -ENOSPC if there is no space left.
5028 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005029int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5030{
5031 int ret;
5032
5033 ret = btrfs_check_data_free_space(inode, num_bytes);
5034 if (ret)
5035 return ret;
5036
5037 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5038 if (ret) {
5039 btrfs_free_reserved_data_space(inode, num_bytes);
5040 return ret;
5041 }
5042
5043 return 0;
5044}
5045
Josef Bacik7709cde2011-08-04 10:25:02 -04005046/**
5047 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5048 * @inode: inode we're releasing space for
5049 * @num_bytes: the number of bytes we want to free up
5050 *
5051 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5052 * called in the case that we don't need the metadata AND data reservations
5053 * anymore. So if there is an error or we insert an inline extent.
5054 *
5055 * This function will release the metadata space that was not used and will
5056 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5057 * list if there are no delalloc bytes left.
5058 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005059void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5060{
5061 btrfs_delalloc_release_metadata(inode, num_bytes);
5062 btrfs_free_reserved_data_space(inode, num_bytes);
5063}
5064
Liu Boc53d6132012-12-27 09:01:19 +00005065static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005066 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005067{
Josef Bacik0af3d002010-06-21 14:48:16 -04005068 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005069 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005070 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005071 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005072 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005073 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005074
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005075 /* block accounting for super block */
5076 spin_lock(&info->delalloc_lock);
David Sterba6c417612011-04-13 15:41:04 +02005077 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005078 if (alloc)
5079 old_val += num_bytes;
5080 else
5081 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005082 btrfs_set_super_bytes_used(info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005083 spin_unlock(&info->delalloc_lock);
5084
Chris Masond3977122009-01-05 21:25:51 -05005085 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005086 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005087 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005088 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005089 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5090 BTRFS_BLOCK_GROUP_RAID1 |
5091 BTRFS_BLOCK_GROUP_RAID10))
5092 factor = 2;
5093 else
5094 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005095 /*
5096 * If this block group has free space cache written out, we
5097 * need to make sure to load it if we are removing space. This
5098 * is because we need the unpinning stage to actually add the
5099 * space back to the block group, otherwise we will leak space.
5100 */
5101 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005102 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005103
Chris Masondb945352007-10-15 16:15:53 -04005104 byte_in_group = bytenr - cache->key.objectid;
5105 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005106
Josef Bacik25179202008-10-29 14:49:05 -04005107 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005108 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005109
Josef Bacik73bc1872011-10-03 14:07:49 -04005110 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005111 cache->disk_cache_state < BTRFS_DC_CLEAR)
5112 cache->disk_cache_state = BTRFS_DC_CLEAR;
5113
Josef Bacik0f9dd462008-09-23 13:14:11 -04005114 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005115 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005116 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005117 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005118 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005119 btrfs_set_block_group_used(&cache->item, old_val);
5120 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005121 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005122 cache->space_info->bytes_used += num_bytes;
5123 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005124 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005125 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005126 } else {
Chris Masondb945352007-10-15 16:15:53 -04005127 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005128 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005129 cache->pinned += num_bytes;
5130 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005131 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005132 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005133 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005134 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005135
Yan, Zhengf0486c62010-05-16 10:46:25 -04005136 set_extent_dirty(info->pinned_extents,
5137 bytenr, bytenr + num_bytes - 1,
5138 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005139 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005140 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005141 total -= num_bytes;
5142 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005143 }
5144 return 0;
5145}
Chris Mason6324fbf2008-03-24 15:01:59 -04005146
Chris Masona061fc82008-05-07 11:43:44 -04005147static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5148{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005149 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005150 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005151
Liu Boa1897fd2012-12-27 09:01:23 +00005152 spin_lock(&root->fs_info->block_group_cache_lock);
5153 bytenr = root->fs_info->first_logical_byte;
5154 spin_unlock(&root->fs_info->block_group_cache_lock);
5155
5156 if (bytenr < (u64)-1)
5157 return bytenr;
5158
Josef Bacik0f9dd462008-09-23 13:14:11 -04005159 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5160 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005161 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005162
Yan Zhengd2fb3432008-12-11 16:30:39 -05005163 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005164 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005165
5166 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005167}
5168
Yan, Zhengf0486c62010-05-16 10:46:25 -04005169static int pin_down_extent(struct btrfs_root *root,
5170 struct btrfs_block_group_cache *cache,
5171 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005172{
Yan Zheng11833d62009-09-11 16:11:19 -04005173 spin_lock(&cache->space_info->lock);
5174 spin_lock(&cache->lock);
5175 cache->pinned += num_bytes;
5176 cache->space_info->bytes_pinned += num_bytes;
5177 if (reserved) {
5178 cache->reserved -= num_bytes;
5179 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005180 }
Yan Zheng11833d62009-09-11 16:11:19 -04005181 spin_unlock(&cache->lock);
5182 spin_unlock(&cache->space_info->lock);
5183
Yan, Zhengf0486c62010-05-16 10:46:25 -04005184 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5185 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05005186 return 0;
5187}
Chris Mason9078a3e2007-04-26 16:46:15 -04005188
Yan, Zhengf0486c62010-05-16 10:46:25 -04005189/*
5190 * this function must be called within transaction
5191 */
5192int btrfs_pin_extent(struct btrfs_root *root,
5193 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005194{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005195 struct btrfs_block_group_cache *cache;
5196
5197 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005198 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005199
5200 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5201
5202 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005203 return 0;
5204}
Zheng Yane8569812008-09-26 10:05:48 -04005205
Yan, Zhengf0486c62010-05-16 10:46:25 -04005206/*
Chris Masone688b7252011-10-31 20:52:39 -04005207 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005208 */
Liu Bodcfac412012-12-27 09:01:20 +00005209int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04005210 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005211{
Chris Masone688b7252011-10-31 20:52:39 -04005212 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04005213 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04005214
5215 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005216 if (!cache)
5217 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04005218
5219 /*
5220 * pull in the free space cache (if any) so that our pin
5221 * removes the free space from the cache. We have load_only set
5222 * to one because the slow code to read in the free extents does check
5223 * the pinned extents.
5224 */
Liu Bof6373bf2012-12-27 09:01:18 +00005225 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04005226
5227 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5228
5229 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04005230 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04005231 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005232 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04005233}
5234
Josef Bacikfb25e912011-07-26 17:00:46 -04005235/**
5236 * btrfs_update_reserved_bytes - update the block_group and space info counters
5237 * @cache: The cache we are manipulating
5238 * @num_bytes: The number of bytes in question
5239 * @reserve: One of the reservation enums
5240 *
5241 * This is called by the allocator when it reserves space, or by somebody who is
5242 * freeing space that was never actually used on disk. For example if you
5243 * reserve some space for a new leaf in transaction A and before transaction A
5244 * commits you free that leaf, you call this with reserve set to 0 in order to
5245 * clear the reservation.
5246 *
5247 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5248 * ENOSPC accounting. For data we handle the reservation through clearing the
5249 * delalloc bits in the io_tree. We have to do this since we could end up
5250 * allocating less disk space for the amount of data we have reserved in the
5251 * case of compression.
5252 *
5253 * If this is a reservation and the block group has become read only we cannot
5254 * make the reservation and return -EAGAIN, otherwise this function always
5255 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005256 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005257static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5258 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005259{
Josef Bacikfb25e912011-07-26 17:00:46 -04005260 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005261 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005262
Josef Bacikfb25e912011-07-26 17:00:46 -04005263 spin_lock(&space_info->lock);
5264 spin_lock(&cache->lock);
5265 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005266 if (cache->ro) {
5267 ret = -EAGAIN;
5268 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005269 cache->reserved += num_bytes;
5270 space_info->bytes_reserved += num_bytes;
5271 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005272 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005273 "space_info", space_info->flags,
5274 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005275 space_info->bytes_may_use -= num_bytes;
5276 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005277 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005278 } else {
5279 if (cache->ro)
5280 space_info->bytes_readonly += num_bytes;
5281 cache->reserved -= num_bytes;
5282 space_info->bytes_reserved -= num_bytes;
5283 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005284 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005285 spin_unlock(&cache->lock);
5286 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005287 return ret;
5288}
5289
Jeff Mahoney143bede2012-03-01 14:56:26 +01005290void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005291 struct btrfs_root *root)
5292{
5293 struct btrfs_fs_info *fs_info = root->fs_info;
5294 struct btrfs_caching_control *next;
5295 struct btrfs_caching_control *caching_ctl;
5296 struct btrfs_block_group_cache *cache;
5297
5298 down_write(&fs_info->extent_commit_sem);
5299
5300 list_for_each_entry_safe(caching_ctl, next,
5301 &fs_info->caching_block_groups, list) {
5302 cache = caching_ctl->block_group;
5303 if (block_group_cache_done(cache)) {
5304 cache->last_byte_to_unpin = (u64)-1;
5305 list_del_init(&caching_ctl->list);
5306 put_caching_control(caching_ctl);
5307 } else {
5308 cache->last_byte_to_unpin = caching_ctl->progress;
5309 }
5310 }
5311
5312 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5313 fs_info->pinned_extents = &fs_info->freed_extents[1];
5314 else
5315 fs_info->pinned_extents = &fs_info->freed_extents[0];
5316
5317 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005318
5319 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005320}
5321
5322static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5323{
5324 struct btrfs_fs_info *fs_info = root->fs_info;
5325 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005326 struct btrfs_space_info *space_info;
5327 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005328 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005329 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005330
5331 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005332 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005333 if (!cache ||
5334 start >= cache->key.objectid + cache->key.offset) {
5335 if (cache)
5336 btrfs_put_block_group(cache);
5337 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005338 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005339 }
5340
5341 len = cache->key.objectid + cache->key.offset - start;
5342 len = min(len, end + 1 - start);
5343
5344 if (start < cache->last_byte_to_unpin) {
5345 len = min(len, cache->last_byte_to_unpin - start);
5346 btrfs_add_free_space(cache, start, len);
5347 }
Josef Bacik25179202008-10-29 14:49:05 -04005348
Yan, Zhengf0486c62010-05-16 10:46:25 -04005349 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005350 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005351
Josef Bacik7b398f82012-10-22 15:52:28 -04005352 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005353 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005354 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005355 space_info->bytes_pinned -= len;
5356 if (cache->ro) {
5357 space_info->bytes_readonly += len;
5358 readonly = true;
5359 }
Josef Bacik25179202008-10-29 14:49:05 -04005360 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005361 if (!readonly && global_rsv->space_info == space_info) {
5362 spin_lock(&global_rsv->lock);
5363 if (!global_rsv->full) {
5364 len = min(len, global_rsv->size -
5365 global_rsv->reserved);
5366 global_rsv->reserved += len;
5367 space_info->bytes_may_use += len;
5368 if (global_rsv->reserved >= global_rsv->size)
5369 global_rsv->full = 1;
5370 }
5371 spin_unlock(&global_rsv->lock);
5372 }
5373 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005374 }
5375
5376 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005377 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005378 return 0;
5379}
5380
5381int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005382 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005383{
Yan Zheng11833d62009-09-11 16:11:19 -04005384 struct btrfs_fs_info *fs_info = root->fs_info;
5385 struct extent_io_tree *unpin;
Chris Mason1a5bc1672007-10-15 16:15:26 -04005386 u64 start;
5387 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005388 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005389
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005390 if (trans->aborted)
5391 return 0;
5392
Yan Zheng11833d62009-09-11 16:11:19 -04005393 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5394 unpin = &fs_info->freed_extents[1];
5395 else
5396 unpin = &fs_info->freed_extents[0];
5397
Chris Masond3977122009-01-05 21:25:51 -05005398 while (1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04005399 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005400 EXTENT_DIRTY, NULL);
Chris Mason1a5bc1672007-10-15 16:15:26 -04005401 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005402 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005403
Li Dongyang5378e602011-03-24 10:24:27 +00005404 if (btrfs_test_opt(root, DISCARD))
5405 ret = btrfs_discard_extent(root, start,
5406 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005407
Chris Mason1a5bc1672007-10-15 16:15:26 -04005408 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005409 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005410 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005411 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005412
Chris Masone20d96d2007-03-22 12:13:20 -04005413 return 0;
5414}
5415
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005416static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5417 struct btrfs_root *root,
5418 u64 bytenr, u64 num_bytes, u64 parent,
5419 u64 root_objectid, u64 owner_objectid,
5420 u64 owner_offset, int refs_to_drop,
5421 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005422{
Chris Masone2fa7222007-03-12 16:22:34 -04005423 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005424 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005425 struct btrfs_fs_info *info = root->fs_info;
5426 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005427 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005428 struct btrfs_extent_item *ei;
5429 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005430 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005431 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005432 int extent_slot = 0;
5433 int found_extent = 0;
5434 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005435 u32 item_size;
5436 u64 refs;
Josef Bacik3173a182013-03-07 14:22:04 -05005437 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5438 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05005439
Chris Mason5caf2a02007-04-02 11:20:42 -04005440 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005441 if (!path)
5442 return -ENOMEM;
5443
Chris Mason3c12ac72008-04-21 12:01:38 -04005444 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005445 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005446
5447 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5448 BUG_ON(!is_data && refs_to_drop != 1);
5449
Josef Bacik3173a182013-03-07 14:22:04 -05005450 if (is_data)
5451 skinny_metadata = 0;
5452
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005453 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5454 bytenr, num_bytes, parent,
5455 root_objectid, owner_objectid,
5456 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005457 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005458 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005459 while (extent_slot >= 0) {
5460 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005461 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005462 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005463 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005464 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5465 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005466 found_extent = 1;
5467 break;
5468 }
Josef Bacik3173a182013-03-07 14:22:04 -05005469 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5470 key.offset == owner_objectid) {
5471 found_extent = 1;
5472 break;
5473 }
Chris Mason952fcca2008-02-18 16:33:44 -05005474 if (path->slots[0] - extent_slot > 5)
5475 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005476 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005477 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005478#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5479 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5480 if (found_extent && item_size < sizeof(*ei))
5481 found_extent = 0;
5482#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005483 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005484 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005485 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005486 NULL, refs_to_drop,
5487 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005488 if (ret) {
5489 btrfs_abort_transaction(trans, extent_root, ret);
5490 goto out;
5491 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005492 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005493 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005494
5495 key.objectid = bytenr;
5496 key.type = BTRFS_EXTENT_ITEM_KEY;
5497 key.offset = num_bytes;
5498
Josef Bacik3173a182013-03-07 14:22:04 -05005499 if (!is_data && skinny_metadata) {
5500 key.type = BTRFS_METADATA_ITEM_KEY;
5501 key.offset = owner_objectid;
5502 }
5503
Zheng Yan31840ae2008-09-23 13:14:14 -04005504 ret = btrfs_search_slot(trans, extent_root,
5505 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05005506 if (ret > 0 && skinny_metadata && path->slots[0]) {
5507 /*
5508 * Couldn't find our skinny metadata item,
5509 * see if we have ye olde extent item.
5510 */
5511 path->slots[0]--;
5512 btrfs_item_key_to_cpu(path->nodes[0], &key,
5513 path->slots[0]);
5514 if (key.objectid == bytenr &&
5515 key.type == BTRFS_EXTENT_ITEM_KEY &&
5516 key.offset == num_bytes)
5517 ret = 0;
5518 }
5519
5520 if (ret > 0 && skinny_metadata) {
5521 skinny_metadata = false;
5522 key.type = BTRFS_EXTENT_ITEM_KEY;
5523 key.offset = num_bytes;
5524 btrfs_release_path(path);
5525 ret = btrfs_search_slot(trans, extent_root,
5526 &key, path, -1, 1);
5527 }
5528
Josef Bacikf3465ca2008-11-12 14:19:50 -05005529 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005530 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5531 ret, (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005532 if (ret > 0)
5533 btrfs_print_leaf(extent_root,
5534 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005535 }
David Sterba005d6422012-09-18 07:52:32 -06005536 if (ret < 0) {
5537 btrfs_abort_transaction(trans, extent_root, ret);
5538 goto out;
5539 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005540 extent_slot = path->slots[0];
5541 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005542 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005543 btrfs_print_leaf(extent_root, path->nodes[0]);
5544 WARN_ON(1);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005545 btrfs_err(info,
5546 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5547 (unsigned long long)bytenr,
5548 (unsigned long long)parent,
5549 (unsigned long long)root_objectid,
5550 (unsigned long long)owner_objectid,
5551 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005552 } else {
David Sterba005d6422012-09-18 07:52:32 -06005553 btrfs_abort_transaction(trans, extent_root, ret);
5554 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005555 }
Chris Mason5f39d392007-10-15 16:14:19 -04005556
5557 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005558 item_size = btrfs_item_size_nr(leaf, extent_slot);
5559#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5560 if (item_size < sizeof(*ei)) {
5561 BUG_ON(found_extent || extent_slot != path->slots[0]);
5562 ret = convert_extent_item_v0(trans, extent_root, path,
5563 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005564 if (ret < 0) {
5565 btrfs_abort_transaction(trans, extent_root, ret);
5566 goto out;
5567 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005568
David Sterbab3b4aa72011-04-21 01:20:15 +02005569 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 path->leave_spinning = 1;
5571
5572 key.objectid = bytenr;
5573 key.type = BTRFS_EXTENT_ITEM_KEY;
5574 key.offset = num_bytes;
5575
5576 ret = btrfs_search_slot(trans, extent_root, &key, path,
5577 -1, 1);
5578 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005579 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5580 ret, (unsigned long long)bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005581 btrfs_print_leaf(extent_root, path->nodes[0]);
5582 }
David Sterba005d6422012-09-18 07:52:32 -06005583 if (ret < 0) {
5584 btrfs_abort_transaction(trans, extent_root, ret);
5585 goto out;
5586 }
5587
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005588 extent_slot = path->slots[0];
5589 leaf = path->nodes[0];
5590 item_size = btrfs_item_size_nr(leaf, extent_slot);
5591 }
5592#endif
5593 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005594 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005595 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05005596 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
5597 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005598 struct btrfs_tree_block_info *bi;
5599 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5600 bi = (struct btrfs_tree_block_info *)(ei + 1);
5601 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005602 }
5603
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005604 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04005605 if (refs < refs_to_drop) {
5606 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
5607 "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
5608 ret = -EINVAL;
5609 btrfs_abort_transaction(trans, extent_root, ret);
5610 goto out;
5611 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005612 refs -= refs_to_drop;
5613
5614 if (refs > 0) {
5615 if (extent_op)
5616 __run_delayed_extent_op(extent_op, leaf, ei);
5617 /*
5618 * In the case of inline back ref, reference count will
5619 * be updated by remove_extent_backref
5620 */
5621 if (iref) {
5622 BUG_ON(!found_extent);
5623 } else {
5624 btrfs_set_extent_refs(leaf, ei, refs);
5625 btrfs_mark_buffer_dirty(leaf);
5626 }
5627 if (found_extent) {
5628 ret = remove_extent_backref(trans, extent_root, path,
5629 iref, refs_to_drop,
5630 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005631 if (ret) {
5632 btrfs_abort_transaction(trans, extent_root, ret);
5633 goto out;
5634 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005635 }
5636 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005637 if (found_extent) {
5638 BUG_ON(is_data && refs_to_drop !=
5639 extent_data_ref_count(root, path, iref));
5640 if (iref) {
5641 BUG_ON(path->slots[0] != extent_slot);
5642 } else {
5643 BUG_ON(path->slots[0] != extent_slot + 1);
5644 path->slots[0] = extent_slot;
5645 num_to_del = 2;
5646 }
Chris Mason78fae272007-03-25 11:35:08 -04005647 }
Chris Masonb9473432009-03-13 11:00:37 -04005648
Chris Mason952fcca2008-02-18 16:33:44 -05005649 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5650 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005651 if (ret) {
5652 btrfs_abort_transaction(trans, extent_root, ret);
5653 goto out;
5654 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005655 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005656
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005657 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005658 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005659 if (ret) {
5660 btrfs_abort_transaction(trans, extent_root, ret);
5661 goto out;
5662 }
Chris Mason459931e2008-12-10 09:10:46 -05005663 }
5664
Liu Boc53d6132012-12-27 09:01:19 +00005665 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005666 if (ret) {
5667 btrfs_abort_transaction(trans, extent_root, ret);
5668 goto out;
5669 }
Chris Masona28ec192007-03-06 20:08:01 -05005670 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005671out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005672 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005673 return ret;
5674}
5675
5676/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005677 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005678 * delayed ref for that extent as well. This searches the delayed ref tree for
5679 * a given extent, and if there are no other delayed refs to be processed, it
5680 * removes it from the tree.
5681 */
5682static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5683 struct btrfs_root *root, u64 bytenr)
5684{
5685 struct btrfs_delayed_ref_head *head;
5686 struct btrfs_delayed_ref_root *delayed_refs;
5687 struct btrfs_delayed_ref_node *ref;
5688 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005689 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005690
5691 delayed_refs = &trans->transaction->delayed_refs;
5692 spin_lock(&delayed_refs->lock);
5693 head = btrfs_find_delayed_ref_head(trans, bytenr);
5694 if (!head)
5695 goto out;
5696
5697 node = rb_prev(&head->node.rb_node);
5698 if (!node)
5699 goto out;
5700
5701 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5702
5703 /* there are still entries for this ref, we can't drop it */
5704 if (ref->bytenr == bytenr)
5705 goto out;
5706
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005707 if (head->extent_op) {
5708 if (!head->must_insert_reserved)
5709 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005710 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005711 head->extent_op = NULL;
5712 }
5713
Chris Mason1887be62009-03-13 10:11:24 -04005714 /*
5715 * waiting for the lock here would deadlock. If someone else has it
5716 * locked they are already in the process of dropping it anyway
5717 */
5718 if (!mutex_trylock(&head->mutex))
5719 goto out;
5720
5721 /*
5722 * at this point we have a head with no other entries. Go
5723 * ahead and process it.
5724 */
5725 head->node.in_tree = 0;
5726 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005727
Chris Mason1887be62009-03-13 10:11:24 -04005728 delayed_refs->num_entries--;
5729
5730 /*
5731 * we don't take a ref on the node because we're removing it from the
5732 * tree, so we just steal the ref the tree was holding.
5733 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005734 delayed_refs->num_heads--;
5735 if (list_empty(&head->cluster))
5736 delayed_refs->num_heads_ready--;
5737
5738 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005739 spin_unlock(&delayed_refs->lock);
5740
Yan, Zhengf0486c62010-05-16 10:46:25 -04005741 BUG_ON(head->extent_op);
5742 if (head->must_insert_reserved)
5743 ret = 1;
5744
5745 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005746 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005747 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005748out:
5749 spin_unlock(&delayed_refs->lock);
5750 return 0;
5751}
5752
Yan, Zhengf0486c62010-05-16 10:46:25 -04005753void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5754 struct btrfs_root *root,
5755 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005756 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005757{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005758 struct btrfs_block_group_cache *cache = NULL;
5759 int ret;
5760
5761 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005762 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5763 buf->start, buf->len,
5764 parent, root->root_key.objectid,
5765 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005766 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005767 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005768 }
5769
5770 if (!last_ref)
5771 return;
5772
Yan, Zhengf0486c62010-05-16 10:46:25 -04005773 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005774
5775 if (btrfs_header_generation(buf) == trans->transid) {
5776 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5777 ret = check_ref_cleanup(trans, root, buf->start);
5778 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005779 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005780 }
5781
5782 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5783 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005784 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005785 }
5786
5787 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5788
5789 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005790 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005791 }
5792out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005793 /*
5794 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5795 * anymore.
5796 */
5797 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005798 btrfs_put_block_group(cache);
5799}
5800
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005801/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005802int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5803 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5804 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005805{
5806 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005807 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005808
Chris Mason56bec292009-03-13 10:10:06 -04005809 /*
5810 * tree log blocks never actually go into the extent allocation
5811 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005812 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005813 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5814 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005815 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005816 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005817 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005818 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005819 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5820 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005821 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005822 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005823 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005824 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5825 num_bytes,
5826 parent, root_objectid, owner,
5827 offset, BTRFS_DROP_DELAYED_REF,
5828 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005829 }
Chris Mason925baed2008-06-25 16:01:30 -04005830 return ret;
5831}
5832
David Woodhouse53b381b2013-01-29 18:40:14 -05005833static u64 stripe_align(struct btrfs_root *root,
5834 struct btrfs_block_group_cache *cache,
5835 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05005836{
Qu Wenruofda28322013-02-26 08:10:22 +00005837 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05005838 return ret;
5839}
5840
Chris Masonfec577f2007-02-26 10:40:21 -05005841/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005842 * when we wait for progress in the block group caching, its because
5843 * our allocation attempt failed at least once. So, we must sleep
5844 * and let some progress happen before we try again.
5845 *
5846 * This function will sleep at least once waiting for new free space to
5847 * show up, and then it will check the block group free space numbers
5848 * for our min num_bytes. Another option is to have it go ahead
5849 * and look in the rbtree for a free extent of a given size, but this
5850 * is a good start.
5851 */
5852static noinline int
5853wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5854 u64 num_bytes)
5855{
Yan Zheng11833d62009-09-11 16:11:19 -04005856 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005857
Yan Zheng11833d62009-09-11 16:11:19 -04005858 caching_ctl = get_caching_control(cache);
5859 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005860 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005861
Yan Zheng11833d62009-09-11 16:11:19 -04005862 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08005863 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005864
5865 put_caching_control(caching_ctl);
5866 return 0;
5867}
5868
5869static noinline int
5870wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5871{
5872 struct btrfs_caching_control *caching_ctl;
Yan Zheng11833d62009-09-11 16:11:19 -04005873
5874 caching_ctl = get_caching_control(cache);
5875 if (!caching_ctl)
5876 return 0;
5877
5878 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5879
5880 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005881 return 0;
5882}
5883
Liu Bo31e50222012-11-21 14:18:10 +00005884int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005885{
Ilya Dryomov7738a532012-03-27 17:09:17 +03005886 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00005887 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005888 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00005889 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005890 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00005891 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005892 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00005893 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005894 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05005895 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05005896 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05005897 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005898
Chris Masone942f882013-02-20 14:06:05 -05005899 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04005900}
5901
Ilya Dryomov7738a532012-03-27 17:09:17 +03005902static int get_block_group_index(struct btrfs_block_group_cache *cache)
5903{
Liu Bo31e50222012-11-21 14:18:10 +00005904 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03005905}
5906
Josef Bacik817d52f2009-07-13 21:29:25 -04005907enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005908 LOOP_CACHING_NOWAIT = 0,
5909 LOOP_CACHING_WAIT = 1,
5910 LOOP_ALLOC_CHUNK = 2,
5911 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005912};
5913
5914/*
Chris Masonfec577f2007-02-26 10:40:21 -05005915 * walks the btree of allocated extents and find a hole of a given size.
5916 * The key ins is changed to record the hole:
5917 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005918 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005919 * ins->offset == number of blocks
5920 * Any available blocks before search_start are skipped.
5921 */
Chris Masond3977122009-01-05 21:25:51 -05005922static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005923 struct btrfs_root *orig_root,
5924 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005925 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00005926 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005927{
Josef Bacik80eb2342008-10-29 14:49:05 -04005928 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005929 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005930 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005931 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005932 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005933 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005934 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005935 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005936 int loop = 0;
Liu Boac5c9302012-12-27 09:01:24 +00005937 int index = __get_raid_index(data);
Josef Bacikfb25e912011-07-26 17:00:46 -04005938 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5939 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005940 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005941 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005942 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005943 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08005944 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005945
Chris Masondb945352007-10-15 16:15:53 -04005946 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005947 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005948 ins->objectid = 0;
5949 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005950
Josef Bacik3f7de032011-11-10 08:29:20 -05005951 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5952
Josef Bacik2552d172009-04-03 10:14:19 -04005953 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005954 if (!space_info) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005955 btrfs_err(root->fs_info, "No space info for %llu", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005956 return -ENOSPC;
5957 }
Josef Bacik2552d172009-04-03 10:14:19 -04005958
Josef Bacik67377732010-09-16 16:19:09 -04005959 /*
5960 * If the space info is for both data and metadata it means we have a
5961 * small filesystem and we can't use the clustering stuff.
5962 */
5963 if (btrfs_mixed_space_info(space_info))
5964 use_cluster = false;
5965
Josef Bacik67377732010-09-16 16:19:09 -04005966 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005967 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005968 if (!btrfs_test_opt(root, SSD))
5969 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005970 }
5971
Josef Bacik67377732010-09-16 16:19:09 -04005972 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5973 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005974 last_ptr = &root->fs_info->data_alloc_cluster;
5975 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005976
Chris Mason239b14b2008-03-24 15:02:07 -04005977 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005978 spin_lock(&last_ptr->lock);
5979 if (last_ptr->block_group)
5980 hint_byte = last_ptr->window_start;
5981 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005982 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005983
Chris Masona061fc82008-05-07 11:43:44 -04005984 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005985 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005986
Josef Bacik817d52f2009-07-13 21:29:25 -04005987 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005988 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005989
Josef Bacik2552d172009-04-03 10:14:19 -04005990 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04005991 block_group = btrfs_lookup_block_group(root->fs_info,
5992 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005993 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04005994 /*
5995 * we don't want to use the block group if it doesn't match our
5996 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005997 *
5998 * However if we are re-searching with an ideal block group
5999 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04006000 */
6001 if (block_group && block_group_bits(block_group, data) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05006002 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04006003 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04006004 if (list_empty(&block_group->list) ||
6005 block_group->ro) {
6006 /*
6007 * someone is removing this block group,
6008 * we can't jump into the have_block_group
6009 * target because our list pointers are not
6010 * valid
6011 */
6012 btrfs_put_block_group(block_group);
6013 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05006014 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006015 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04006016 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05006017 }
Josef Bacik2552d172009-04-03 10:14:19 -04006018 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006019 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006020 }
Chris Mason42e70e72008-11-07 18:17:11 -05006021 }
Josef Bacik2552d172009-04-03 10:14:19 -04006022search:
Miao Xie60d2adb2011-09-09 17:34:35 +08006023 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04006024 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006025 list_for_each_entry(block_group, &space_info->block_groups[index],
6026 list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006027 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04006028 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05006029
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006030 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00006031 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006032 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05006033
Chris Mason83a50de2010-12-13 15:06:46 -05006034 /*
6035 * this can happen if we end up cycling through all the
6036 * raid types, but we want to make sure we only allocate
6037 * for the proper type.
6038 */
6039 if (!block_group_bits(block_group, data)) {
6040 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6041 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05006042 BTRFS_BLOCK_GROUP_RAID5 |
6043 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05006044 BTRFS_BLOCK_GROUP_RAID10;
6045
6046 /*
6047 * if they asked for extra copies and this block group
6048 * doesn't provide them, bail. This does allow us to
6049 * fill raid0 from raid1.
6050 */
6051 if ((data & extra) && !(block_group->flags & extra))
6052 goto loop;
6053 }
6054
Josef Bacik2552d172009-04-03 10:14:19 -04006055have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05006056 cached = block_group_cache_done(block_group);
6057 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05006058 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00006059 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04006060 BUG_ON(ret < 0);
6061 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05006062 }
6063
Josef Bacikea6a4782008-11-20 12:16:16 -05006064 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04006065 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006066
Josef Bacik0a243252009-09-11 16:11:20 -04006067 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006068 * Ok we want to try and use the cluster allocator, so
6069 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04006070 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006071 if (last_ptr) {
Chris Mason8de972b2013-01-04 15:39:43 -05006072 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006073 /*
6074 * the refill lock keeps out other
6075 * people trying to start a new cluster
6076 */
6077 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006078 used_block_group = last_ptr->block_group;
6079 if (used_block_group != block_group &&
6080 (!used_block_group ||
6081 used_block_group->ro ||
6082 !block_group_bits(used_block_group, data))) {
6083 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04006084 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006085 }
Chris Mason44fb5512009-06-04 15:34:51 -04006086
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006087 if (used_block_group != block_group)
6088 btrfs_get_block_group(used_block_group);
6089
6090 offset = btrfs_alloc_from_cluster(used_block_group,
6091 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006092 if (offset) {
6093 /* we have a block, we're done */
6094 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006095 trace_btrfs_reserve_extent_cluster(root,
6096 block_group, search_start, num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006097 goto checks;
6098 }
6099
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006100 WARN_ON(last_ptr->block_group != used_block_group);
6101 if (used_block_group != block_group) {
6102 btrfs_put_block_group(used_block_group);
6103 used_block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006104 }
Chris Mason44fb5512009-06-04 15:34:51 -04006105refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006106 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006107 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6108 * set up a new clusters, so lets just skip it
6109 * and let the allocator find whatever block
6110 * it can find. If we reach this point, we
6111 * will have tried the cluster allocator
6112 * plenty of times and not have found
6113 * anything, so we are likely way too
6114 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006115 * anything.
6116 *
6117 * However, if the cluster is taken from the
6118 * current block group, release the cluster
6119 * first, so that we stand a better chance of
6120 * succeeding in the unclustered
6121 * allocation. */
6122 if (loop >= LOOP_NO_EMPTY_SIZE &&
6123 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006124 spin_unlock(&last_ptr->refill_lock);
6125 goto unclustered_alloc;
6126 }
6127
Chris Masonfa9c0d792009-04-03 09:47:43 -04006128 /*
6129 * this cluster didn't work out, free it and
6130 * start over
6131 */
6132 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6133
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006134 if (loop >= LOOP_NO_EMPTY_SIZE) {
6135 spin_unlock(&last_ptr->refill_lock);
6136 goto unclustered_alloc;
6137 }
6138
Chris Mason8de972b2013-01-04 15:39:43 -05006139 aligned_cluster = max_t(unsigned long,
6140 empty_cluster + empty_size,
6141 block_group->full_stripe_len);
6142
Chris Masonfa9c0d792009-04-03 09:47:43 -04006143 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04006144 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04006145 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05006146 search_start, num_bytes,
Chris Mason8de972b2013-01-04 15:39:43 -05006147 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006148 if (ret == 0) {
6149 /*
6150 * now pull our allocation out of this
6151 * cluster
6152 */
6153 offset = btrfs_alloc_from_cluster(block_group,
6154 last_ptr, num_bytes,
6155 search_start);
6156 if (offset) {
6157 /* we found one, proceed */
6158 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006159 trace_btrfs_reserve_extent_cluster(root,
6160 block_group, search_start,
6161 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006162 goto checks;
6163 }
Josef Bacik0a243252009-09-11 16:11:20 -04006164 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6165 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006166 spin_unlock(&last_ptr->refill_lock);
6167
Josef Bacik0a243252009-09-11 16:11:20 -04006168 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006169 wait_block_group_cache_progress(block_group,
6170 num_bytes + empty_cluster + empty_size);
6171 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006172 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006173
Chris Masonfa9c0d792009-04-03 09:47:43 -04006174 /*
6175 * at this point we either didn't find a cluster
6176 * or we weren't able to allocate a block from our
6177 * cluster. Free the cluster we've been trying
6178 * to use, and go to the next block group
6179 */
Josef Bacik0a243252009-09-11 16:11:20 -04006180 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006181 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006182 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006183 }
6184
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006185unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006186 spin_lock(&block_group->free_space_ctl->tree_lock);
6187 if (cached &&
6188 block_group->free_space_ctl->free_space <
6189 num_bytes + empty_cluster + empty_size) {
6190 spin_unlock(&block_group->free_space_ctl->tree_lock);
6191 goto loop;
6192 }
6193 spin_unlock(&block_group->free_space_ctl->tree_lock);
6194
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006195 offset = btrfs_find_space_for_alloc(block_group, search_start,
6196 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006197 /*
6198 * If we didn't find a chunk, and we haven't failed on this
6199 * block group before, and this block group is in the middle of
6200 * caching and we are ok with waiting, then go ahead and wait
6201 * for progress to be made, and set failed_alloc to true.
6202 *
6203 * If failed_alloc is true then we've already waited on this
6204 * block group once and should move on to the next block group.
6205 */
6206 if (!offset && !failed_alloc && !cached &&
6207 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006208 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006209 num_bytes + empty_size);
6210 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006211 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006212 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006213 if (!cached)
6214 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006215 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006216 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006217checks:
David Woodhouse53b381b2013-01-29 18:40:14 -05006218 search_start = stripe_align(root, used_block_group,
6219 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006220
Josef Bacik2552d172009-04-03 10:14:19 -04006221 /* move on to the next group */
6222 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006223 used_block_group->key.objectid + used_block_group->key.offset) {
6224 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006225 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006226 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006227
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006228 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006229 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006230 search_start - offset);
6231 BUG_ON(offset > search_start);
6232
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006233 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006234 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006235 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006236 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006237 goto loop;
6238 }
Yan Zheng11833d62009-09-11 16:11:19 -04006239
Josef Bacik2552d172009-04-03 10:14:19 -04006240 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006241 ins->objectid = search_start;
6242 ins->offset = num_bytes;
6243
Josef Bacik3f7de032011-11-10 08:29:20 -05006244 trace_btrfs_reserve_extent(orig_root, block_group,
6245 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006246 if (used_block_group != block_group)
6247 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04006248 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006249 break;
6250loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006251 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006252 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006253 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006254 if (used_block_group != block_group)
6255 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006256 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006257 }
6258 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006259
Miao Xie60d2adb2011-09-09 17:34:35 +08006260 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6261 goto search;
6262
Yan, Zhengb742bb82010-05-16 10:46:24 -04006263 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6264 goto search;
6265
Josef Bacik285ff5a2012-01-13 15:27:45 -05006266 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006267 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6268 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006269 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6270 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6271 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6272 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04006273 */
Josef Bacik723bda22011-05-27 16:11:38 -04006274 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006275 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006276 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006277 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik698d0082012-09-12 14:08:47 -04006278 ret = do_chunk_alloc(trans, root, data,
Josef Bacikea658ba2012-09-11 16:57:25 -04006279 CHUNK_ALLOC_FORCE);
6280 /*
6281 * Do not bail out on ENOSPC since we
6282 * can do more things.
6283 */
6284 if (ret < 0 && ret != -ENOSPC) {
6285 btrfs_abort_transaction(trans,
6286 root, ret);
6287 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006288 }
Josef Bacik723bda22011-05-27 16:11:38 -04006289 }
6290
6291 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006292 empty_size = 0;
6293 empty_cluster = 0;
6294 }
Chris Mason42e70e72008-11-07 18:17:11 -05006295
Josef Bacik723bda22011-05-27 16:11:38 -04006296 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006297 } else if (!ins->objectid) {
6298 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006299 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006300 ret = 0;
6301 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006302out:
Chris Mason0b86a832008-03-24 15:01:56 -04006303
Chris Mason0f70abe2007-02-28 16:46:22 -05006304 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006305}
Chris Masonec44a352008-04-28 15:29:52 -04006306
Josef Bacik9ed74f22009-09-11 16:12:44 -04006307static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6308 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006309{
6310 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006311 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006312
Josef Bacik9ed74f22009-09-11 16:12:44 -04006313 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006314 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
6315 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05006316 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04006317 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006318 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05006319 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006320 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6321 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07006322 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006323 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006324 (unsigned long long)info->bytes_pinned,
6325 (unsigned long long)info->bytes_reserved,
6326 (unsigned long long)info->bytes_may_use,
6327 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006328 spin_unlock(&info->lock);
6329
6330 if (!dump_block_groups)
6331 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006332
Josef Bacik80eb2342008-10-29 14:49:05 -04006333 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006334again:
6335 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006336 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006337 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05006338 (unsigned long long)cache->key.objectid,
6339 (unsigned long long)cache->key.offset,
6340 (unsigned long long)btrfs_block_group_used(&cache->item),
6341 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06006342 (unsigned long long)cache->reserved,
6343 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006344 btrfs_dump_free_space(cache, bytes);
6345 spin_unlock(&cache->lock);
6346 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006347 if (++index < BTRFS_NR_RAID_TYPES)
6348 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006349 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006350}
Zheng Yane8569812008-09-26 10:05:48 -04006351
Yan Zheng11833d62009-09-11 16:11:19 -04006352int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6353 struct btrfs_root *root,
6354 u64 num_bytes, u64 min_alloc_size,
6355 u64 empty_size, u64 hint_byte,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006356 struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05006357{
Miao Xie9e622d62012-01-26 15:01:12 -05006358 bool final_tried = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006359 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006360
Josef Bacik6a632092009-02-20 11:00:09 -05006361 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04006362again:
Chris Masondb945352007-10-15 16:15:53 -04006363 WARN_ON(num_bytes < root->sectorsize);
6364 ret = find_free_extent(trans, root, num_bytes, empty_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006365 hint_byte, ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04006366
Miao Xie9e622d62012-01-26 15:01:12 -05006367 if (ret == -ENOSPC) {
6368 if (!final_tried) {
6369 num_bytes = num_bytes >> 1;
Zach Brown24542bf2012-11-16 00:04:43 +00006370 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006371 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006372 if (num_bytes == min_alloc_size)
6373 final_tried = true;
6374 goto again;
6375 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6376 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006377
Miao Xie9e622d62012-01-26 15:01:12 -05006378 sinfo = __find_space_info(root->fs_info, data);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006379 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
6380 (unsigned long long)data,
6381 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006382 if (sinfo)
6383 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006384 }
Chris Mason925baed2008-06-25 16:01:30 -04006385 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006386
liubo1abe9b82011-03-24 11:18:59 +00006387 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6388
Josef Bacik0f9dd462008-09-23 13:14:11 -04006389 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006390}
6391
Chris Masone688b7252011-10-31 20:52:39 -04006392static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6393 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006394{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006395 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006396 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006397
Josef Bacik0f9dd462008-09-23 13:14:11 -04006398 cache = btrfs_lookup_block_group(root->fs_info, start);
6399 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006400 btrfs_err(root->fs_info, "Unable to find block group for %llu",
6401 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006402 return -ENOSPC;
6403 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006404
Li Dongyang5378e602011-03-24 10:24:27 +00006405 if (btrfs_test_opt(root, DISCARD))
6406 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006407
Chris Masone688b7252011-10-31 20:52:39 -04006408 if (pin)
6409 pin_down_extent(root, cache, start, len, 1);
6410 else {
6411 btrfs_add_free_space(cache, start, len);
6412 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6413 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006414 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006415
liubo1abe9b82011-03-24 11:18:59 +00006416 trace_btrfs_reserved_extent_free(root, start, len);
6417
Chris Masone6dcd2d2008-07-17 12:53:50 -04006418 return ret;
6419}
6420
Chris Masone688b7252011-10-31 20:52:39 -04006421int btrfs_free_reserved_extent(struct btrfs_root *root,
6422 u64 start, u64 len)
6423{
6424 return __btrfs_free_reserved_extent(root, start, len, 0);
6425}
6426
6427int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6428 u64 start, u64 len)
6429{
6430 return __btrfs_free_reserved_extent(root, start, len, 1);
6431}
6432
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006433static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6434 struct btrfs_root *root,
6435 u64 parent, u64 root_objectid,
6436 u64 flags, u64 owner, u64 offset,
6437 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006438{
6439 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006440 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006441 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006442 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006443 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006444 struct extent_buffer *leaf;
6445 int type;
6446 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006447
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006448 if (parent > 0)
6449 type = BTRFS_SHARED_DATA_REF_KEY;
6450 else
6451 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006452
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006453 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006454
6455 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006456 if (!path)
6457 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006458
Chris Masonb9473432009-03-13 11:00:37 -04006459 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006460 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6461 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006462 if (ret) {
6463 btrfs_free_path(path);
6464 return ret;
6465 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006466
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006467 leaf = path->nodes[0];
6468 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006469 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006470 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6471 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6472 btrfs_set_extent_flags(leaf, extent_item,
6473 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006474
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006475 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6476 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6477 if (parent > 0) {
6478 struct btrfs_shared_data_ref *ref;
6479 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6480 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6481 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6482 } else {
6483 struct btrfs_extent_data_ref *ref;
6484 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6485 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6486 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6487 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6488 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6489 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006490
6491 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006492 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006493
Liu Boc53d6132012-12-27 09:01:19 +00006494 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006495 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006496 btrfs_err(fs_info, "update block group failed for %llu %llu",
6497 (unsigned long long)ins->objectid,
6498 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006499 BUG();
6500 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006501 return ret;
6502}
6503
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006504static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6505 struct btrfs_root *root,
6506 u64 parent, u64 root_objectid,
6507 u64 flags, struct btrfs_disk_key *key,
6508 int level, struct btrfs_key *ins)
6509{
6510 int ret;
6511 struct btrfs_fs_info *fs_info = root->fs_info;
6512 struct btrfs_extent_item *extent_item;
6513 struct btrfs_tree_block_info *block_info;
6514 struct btrfs_extent_inline_ref *iref;
6515 struct btrfs_path *path;
6516 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05006517 u32 size = sizeof(*extent_item) + sizeof(*iref);
6518 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6519 SKINNY_METADATA);
6520
6521 if (!skinny_metadata)
6522 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006523
6524 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006525 if (!path)
6526 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006527
6528 path->leave_spinning = 1;
6529 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6530 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006531 if (ret) {
6532 btrfs_free_path(path);
6533 return ret;
6534 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006535
6536 leaf = path->nodes[0];
6537 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6538 struct btrfs_extent_item);
6539 btrfs_set_extent_refs(leaf, extent_item, 1);
6540 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6541 btrfs_set_extent_flags(leaf, extent_item,
6542 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006543
Josef Bacik3173a182013-03-07 14:22:04 -05006544 if (skinny_metadata) {
6545 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6546 } else {
6547 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6548 btrfs_set_tree_block_key(leaf, block_info, key);
6549 btrfs_set_tree_block_level(leaf, block_info, level);
6550 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6551 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006552
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006553 if (parent > 0) {
6554 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6555 btrfs_set_extent_inline_ref_type(leaf, iref,
6556 BTRFS_SHARED_BLOCK_REF_KEY);
6557 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6558 } else {
6559 btrfs_set_extent_inline_ref_type(leaf, iref,
6560 BTRFS_TREE_BLOCK_REF_KEY);
6561 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6562 }
6563
6564 btrfs_mark_buffer_dirty(leaf);
6565 btrfs_free_path(path);
6566
Josef Bacik3173a182013-03-07 14:22:04 -05006567 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006568 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006569 btrfs_err(fs_info, "update block group failed for %llu %llu",
6570 (unsigned long long)ins->objectid,
6571 (unsigned long long)ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006572 BUG();
6573 }
6574 return ret;
6575}
6576
6577int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6578 struct btrfs_root *root,
6579 u64 root_objectid, u64 owner,
6580 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006581{
6582 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006583
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006584 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006585
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006586 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6587 ins->offset, 0,
6588 root_objectid, owner, offset,
6589 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006590 return ret;
6591}
Chris Masone02119d2008-09-05 16:13:11 -04006592
6593/*
6594 * this is used by the tree logging recovery code. It records that
6595 * an extent has been allocated and makes sure to clear the free
6596 * space cache bits as well
6597 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006598int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6599 struct btrfs_root *root,
6600 u64 root_objectid, u64 owner, u64 offset,
6601 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006602{
6603 int ret;
6604 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04006605 struct btrfs_caching_control *caching_ctl;
6606 u64 start = ins->objectid;
6607 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04006608
Chris Masone02119d2008-09-05 16:13:11 -04006609 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Liu Bof6373bf2012-12-27 09:01:18 +00006610 cache_block_group(block_group, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04006611 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006612
Yan Zheng11833d62009-09-11 16:11:19 -04006613 if (!caching_ctl) {
6614 BUG_ON(!block_group_cache_done(block_group));
6615 ret = btrfs_remove_free_space(block_group, start, num_bytes);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006616 if (ret)
6617 goto out;
Yan Zheng11833d62009-09-11 16:11:19 -04006618 } else {
6619 mutex_lock(&caching_ctl->mutex);
6620
6621 if (start >= caching_ctl->progress) {
6622 ret = add_excluded_extent(root, start, num_bytes);
Yan Zheng11833d62009-09-11 16:11:19 -04006623 } else if (start + num_bytes <= caching_ctl->progress) {
6624 ret = btrfs_remove_free_space(block_group,
6625 start, num_bytes);
Yan Zheng11833d62009-09-11 16:11:19 -04006626 } else {
6627 num_bytes = caching_ctl->progress - start;
6628 ret = btrfs_remove_free_space(block_group,
6629 start, num_bytes);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006630 if (ret)
6631 goto out_lock;
Yan Zheng11833d62009-09-11 16:11:19 -04006632
6633 start = caching_ctl->progress;
6634 num_bytes = ins->objectid + ins->offset -
6635 caching_ctl->progress;
6636 ret = add_excluded_extent(root, start, num_bytes);
Yan Zheng11833d62009-09-11 16:11:19 -04006637 }
Josef Bacikb50c6e22013-04-25 15:55:30 -04006638out_lock:
Yan Zheng11833d62009-09-11 16:11:19 -04006639 mutex_unlock(&caching_ctl->mutex);
6640 put_caching_control(caching_ctl);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006641 if (ret)
6642 goto out;
Yan Zheng11833d62009-09-11 16:11:19 -04006643 }
6644
Josef Bacikfb25e912011-07-26 17:00:46 -04006645 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6646 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006647 BUG_ON(ret); /* logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006648 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6649 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006650out:
6651 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006652 return ret;
6653}
6654
Chris Mason65b51a02008-08-01 15:11:20 -04006655struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6656 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05006657 u64 bytenr, u32 blocksize,
6658 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006659{
6660 struct extent_buffer *buf;
6661
6662 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6663 if (!buf)
6664 return ERR_PTR(-ENOMEM);
6665 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006666 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006667 btrfs_tree_lock(buf);
6668 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006669 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006670
6671 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006672 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006673
Chris Masond0c803c2008-09-11 16:17:57 -04006674 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006675 /*
6676 * we allow two log transactions at a time, use different
6677 * EXENT bit to differentiate dirty pages.
6678 */
6679 if (root->log_transid % 2 == 0)
6680 set_extent_dirty(&root->dirty_log_pages, buf->start,
6681 buf->start + buf->len - 1, GFP_NOFS);
6682 else
6683 set_extent_new(&root->dirty_log_pages, buf->start,
6684 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006685 } else {
6686 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6687 buf->start + buf->len - 1, GFP_NOFS);
6688 }
Chris Mason65b51a02008-08-01 15:11:20 -04006689 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006690 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006691 return buf;
6692}
6693
Yan, Zhengf0486c62010-05-16 10:46:25 -04006694static struct btrfs_block_rsv *
6695use_block_rsv(struct btrfs_trans_handle *trans,
6696 struct btrfs_root *root, u32 blocksize)
6697{
6698 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006699 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006700 int ret;
6701
6702 block_rsv = get_block_rsv(trans, root);
6703
6704 if (block_rsv->size == 0) {
Miao Xie08e007d2012-10-16 11:33:38 +00006705 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6706 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006707 /*
6708 * If we couldn't reserve metadata bytes try and use some from
6709 * the global reserve.
6710 */
6711 if (ret && block_rsv != global_rsv) {
6712 ret = block_rsv_use_bytes(global_rsv, blocksize);
6713 if (!ret)
6714 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006715 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00006716 } else if (ret) {
6717 return ERR_PTR(ret);
6718 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006719 return block_rsv;
6720 }
6721
6722 ret = block_rsv_use_bytes(block_rsv, blocksize);
6723 if (!ret)
6724 return block_rsv;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006725 if (ret && !block_rsv->failfast) {
David Sterbab069e0c2013-02-08 21:28:17 +00006726 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6727 static DEFINE_RATELIMIT_STATE(_rs,
6728 DEFAULT_RATELIMIT_INTERVAL * 10,
6729 /*DEFAULT_RATELIMIT_BURST*/ 1);
6730 if (__ratelimit(&_rs))
6731 WARN(1, KERN_DEBUG
6732 "btrfs: block rsv returned %d\n", ret);
6733 }
Miao Xie08e007d2012-10-16 11:33:38 +00006734 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6735 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006736 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00006737 return block_rsv;
6738 } else if (ret && block_rsv != global_rsv) {
6739 ret = block_rsv_use_bytes(global_rsv, blocksize);
6740 if (!ret)
6741 return global_rsv;
6742 }
6743 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006744
Yan, Zhengf0486c62010-05-16 10:46:25 -04006745 return ERR_PTR(-ENOSPC);
6746}
6747
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006748static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6749 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006750{
6751 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006752 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006753}
6754
Chris Masonfec577f2007-02-26 10:40:21 -05006755/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006756 * finds a free extent and does all the dirty work required for allocation
6757 * returns the key for the extent through ins, and a tree buffer for
6758 * the first block of the extent through buf.
6759 *
Chris Masonfec577f2007-02-26 10:40:21 -05006760 * returns the tree buffer or NULL.
6761 */
Chris Mason5f39d392007-10-15 16:14:19 -04006762struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006763 struct btrfs_root *root, u32 blocksize,
6764 u64 parent, u64 root_objectid,
6765 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006766 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006767{
Chris Masone2fa7222007-03-12 16:22:34 -04006768 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006769 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006770 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006771 u64 flags = 0;
6772 int ret;
Josef Bacik3173a182013-03-07 14:22:04 -05006773 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6774 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006775
6776 block_rsv = use_block_rsv(trans, root, blocksize);
6777 if (IS_ERR(block_rsv))
6778 return ERR_CAST(block_rsv);
6779
6780 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006781 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006782 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006783 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006784 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006785 }
Chris Mason55c69072008-01-09 15:55:33 -05006786
Chris Mason4008c042009-02-12 14:09:45 -05006787 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6788 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006789 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006790
6791 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6792 if (parent == 0)
6793 parent = ins.objectid;
6794 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6795 } else
6796 BUG_ON(parent > 0);
6797
6798 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6799 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006800 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006801 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006802 if (key)
6803 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6804 else
6805 memset(&extent_op->key, 0, sizeof(extent_op->key));
6806 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05006807 if (skinny_metadata)
6808 extent_op->update_key = 0;
6809 else
6810 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006811 extent_op->update_flags = 1;
6812 extent_op->is_data = 0;
6813
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006814 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6815 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006816 ins.offset, parent, root_objectid,
6817 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006818 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006819 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006820 }
Chris Masonfec577f2007-02-26 10:40:21 -05006821 return buf;
6822}
Chris Masona28ec192007-03-06 20:08:01 -05006823
Yan Zheng2c47e6052009-06-27 21:07:35 -04006824struct walk_control {
6825 u64 refs[BTRFS_MAX_LEVEL];
6826 u64 flags[BTRFS_MAX_LEVEL];
6827 struct btrfs_key update_progress;
6828 int stage;
6829 int level;
6830 int shared_level;
6831 int update_ref;
6832 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006833 int reada_slot;
6834 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006835 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006836};
6837
6838#define DROP_REFERENCE 1
6839#define UPDATE_BACKREF 2
6840
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006841static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6842 struct btrfs_root *root,
6843 struct walk_control *wc,
6844 struct btrfs_path *path)
6845{
6846 u64 bytenr;
6847 u64 generation;
6848 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006849 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006850 u32 nritems;
6851 u32 blocksize;
6852 struct btrfs_key key;
6853 struct extent_buffer *eb;
6854 int ret;
6855 int slot;
6856 int nread = 0;
6857
6858 if (path->slots[wc->level] < wc->reada_slot) {
6859 wc->reada_count = wc->reada_count * 2 / 3;
6860 wc->reada_count = max(wc->reada_count, 2);
6861 } else {
6862 wc->reada_count = wc->reada_count * 3 / 2;
6863 wc->reada_count = min_t(int, wc->reada_count,
6864 BTRFS_NODEPTRS_PER_BLOCK(root));
6865 }
6866
6867 eb = path->nodes[wc->level];
6868 nritems = btrfs_header_nritems(eb);
6869 blocksize = btrfs_level_size(root, wc->level - 1);
6870
6871 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6872 if (nread >= wc->reada_count)
6873 break;
6874
6875 cond_resched();
6876 bytenr = btrfs_node_blockptr(eb, slot);
6877 generation = btrfs_node_ptr_generation(eb, slot);
6878
6879 if (slot == path->slots[wc->level])
6880 goto reada;
6881
6882 if (wc->stage == UPDATE_BACKREF &&
6883 generation <= root->root_key.offset)
6884 continue;
6885
Yan, Zheng94fcca92009-10-09 09:25:16 -04006886 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05006887 ret = btrfs_lookup_extent_info(trans, root, bytenr,
6888 wc->level - 1, 1, &refs,
6889 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006890 /* We don't care about errors in readahead. */
6891 if (ret < 0)
6892 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006893 BUG_ON(refs == 0);
6894
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006895 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006896 if (refs == 1)
6897 goto reada;
6898
Yan, Zheng94fcca92009-10-09 09:25:16 -04006899 if (wc->level == 1 &&
6900 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6901 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006902 if (!wc->update_ref ||
6903 generation <= root->root_key.offset)
6904 continue;
6905 btrfs_node_key_to_cpu(eb, &key, slot);
6906 ret = btrfs_comp_cpu_keys(&key,
6907 &wc->update_progress);
6908 if (ret < 0)
6909 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006910 } else {
6911 if (wc->level == 1 &&
6912 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6913 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006914 }
6915reada:
6916 ret = readahead_tree_block(root, bytenr, blocksize,
6917 generation);
6918 if (ret)
6919 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006920 nread++;
6921 }
6922 wc->reada_slot = slot;
6923}
6924
Chris Mason9aca1d52007-03-13 11:09:37 -04006925/*
Liu Bo2c016dc2012-12-26 15:32:17 +08006926 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04006927 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006928 * when wc->stage == UPDATE_BACKREF, this function updates
6929 * back refs for pointers in the block.
6930 *
6931 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006932 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006933static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6934 struct btrfs_root *root,
6935 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006936 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006937{
6938 int level = wc->level;
6939 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006940 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6941 int ret;
6942
6943 if (wc->stage == UPDATE_BACKREF &&
6944 btrfs_header_owner(eb) != root->root_key.objectid)
6945 return 1;
6946
6947 /*
6948 * when reference count of tree block is 1, it won't increase
6949 * again. once full backref flag is set, we never clear it.
6950 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006951 if (lookup_info &&
6952 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6953 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006954 BUG_ON(!path->locks[level]);
6955 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05006956 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006957 &wc->refs[level],
6958 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006959 BUG_ON(ret == -ENOMEM);
6960 if (ret)
6961 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006962 BUG_ON(wc->refs[level] == 0);
6963 }
6964
Yan Zheng2c47e6052009-06-27 21:07:35 -04006965 if (wc->stage == DROP_REFERENCE) {
6966 if (wc->refs[level] > 1)
6967 return 1;
6968
6969 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006970 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006971 path->locks[level] = 0;
6972 }
6973 return 0;
6974 }
6975
6976 /* wc->stage == UPDATE_BACKREF */
6977 if (!(wc->flags[level] & flag)) {
6978 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006979 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006980 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006981 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006982 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006983 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6984 eb->len, flag, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006985 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006986 wc->flags[level] |= flag;
6987 }
6988
6989 /*
6990 * the block is shared by multiple trees, so it's not good to
6991 * keep the tree lock
6992 */
6993 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006994 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006995 path->locks[level] = 0;
6996 }
6997 return 0;
6998}
6999
7000/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007001 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007002 *
7003 * when wc->stage == DROP_REFERENCE, this function checks
7004 * reference count of the block pointed to. if the block
7005 * is shared and we need update back refs for the subtree
7006 * rooted at the block, this function changes wc->stage to
7007 * UPDATE_BACKREF. if the block is shared and there is no
7008 * need to update back, this function drops the reference
7009 * to the block.
7010 *
7011 * NOTE: return value 1 means we should stop walking down.
7012 */
7013static noinline int do_walk_down(struct btrfs_trans_handle *trans,
7014 struct btrfs_root *root,
7015 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007016 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007017{
7018 u64 bytenr;
7019 u64 generation;
7020 u64 parent;
7021 u32 blocksize;
7022 struct btrfs_key key;
7023 struct extent_buffer *next;
7024 int level = wc->level;
7025 int reada = 0;
7026 int ret = 0;
7027
7028 generation = btrfs_node_ptr_generation(path->nodes[level],
7029 path->slots[level]);
7030 /*
7031 * if the lower level block was created before the snapshot
7032 * was created, we know there is no need to update back refs
7033 * for the subtree
7034 */
7035 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04007036 generation <= root->root_key.offset) {
7037 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007038 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007039 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007040
7041 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7042 blocksize = btrfs_level_size(root, level - 1);
7043
7044 next = btrfs_find_tree_block(root, bytenr, blocksize);
7045 if (!next) {
7046 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00007047 if (!next)
7048 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007049 reada = 1;
7050 }
7051 btrfs_tree_lock(next);
7052 btrfs_set_lock_blocking(next);
7053
Josef Bacik3173a182013-03-07 14:22:04 -05007054 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007055 &wc->refs[level - 1],
7056 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007057 if (ret < 0) {
7058 btrfs_tree_unlock(next);
7059 return ret;
7060 }
7061
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007062 if (unlikely(wc->refs[level - 1] == 0)) {
7063 btrfs_err(root->fs_info, "Missing references.");
7064 BUG();
7065 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007066 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007067
Yan, Zheng94fcca92009-10-09 09:25:16 -04007068 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007069 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007070 if (level == 1 &&
7071 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7072 goto skip;
7073
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007074 if (!wc->update_ref ||
7075 generation <= root->root_key.offset)
7076 goto skip;
7077
7078 btrfs_node_key_to_cpu(path->nodes[level], &key,
7079 path->slots[level]);
7080 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7081 if (ret < 0)
7082 goto skip;
7083
7084 wc->stage = UPDATE_BACKREF;
7085 wc->shared_level = level - 1;
7086 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007087 } else {
7088 if (level == 1 &&
7089 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7090 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007091 }
7092
Chris Masonb9fab912012-05-06 07:23:47 -04007093 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007094 btrfs_tree_unlock(next);
7095 free_extent_buffer(next);
7096 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007097 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007098 }
7099
7100 if (!next) {
7101 if (reada && level == 1)
7102 reada_walk_down(trans, root, wc, path);
7103 next = read_tree_block(root, bytenr, blocksize, generation);
Josef Bacik416bc652013-04-23 14:17:42 -04007104 if (!next || !extent_buffer_uptodate(next)) {
7105 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007106 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04007107 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007108 btrfs_tree_lock(next);
7109 btrfs_set_lock_blocking(next);
7110 }
7111
7112 level--;
7113 BUG_ON(level != btrfs_header_level(next));
7114 path->nodes[level] = next;
7115 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007116 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007117 wc->level = level;
7118 if (wc->level == 1)
7119 wc->reada_slot = 0;
7120 return 0;
7121skip:
7122 wc->refs[level - 1] = 0;
7123 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007124 if (wc->stage == DROP_REFERENCE) {
7125 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7126 parent = path->nodes[level]->start;
7127 } else {
7128 BUG_ON(root->root_key.objectid !=
7129 btrfs_header_owner(path->nodes[level]));
7130 parent = 0;
7131 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007132
Yan, Zheng94fcca92009-10-09 09:25:16 -04007133 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007134 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007135 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007136 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007137 btrfs_tree_unlock(next);
7138 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04007139 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007140 return 1;
7141}
7142
7143/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007144 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007145 *
7146 * when wc->stage == DROP_REFERENCE, this function drops
7147 * reference count on the block.
7148 *
7149 * when wc->stage == UPDATE_BACKREF, this function changes
7150 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7151 * to UPDATE_BACKREF previously while processing the block.
7152 *
7153 * NOTE: return value 1 means we should stop walking up.
7154 */
7155static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7156 struct btrfs_root *root,
7157 struct btrfs_path *path,
7158 struct walk_control *wc)
7159{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007160 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007161 int level = wc->level;
7162 struct extent_buffer *eb = path->nodes[level];
7163 u64 parent = 0;
7164
7165 if (wc->stage == UPDATE_BACKREF) {
7166 BUG_ON(wc->shared_level < level);
7167 if (level < wc->shared_level)
7168 goto out;
7169
Yan Zheng2c47e6052009-06-27 21:07:35 -04007170 ret = find_next_key(path, level + 1, &wc->update_progress);
7171 if (ret > 0)
7172 wc->update_ref = 0;
7173
7174 wc->stage = DROP_REFERENCE;
7175 wc->shared_level = -1;
7176 path->slots[level] = 0;
7177
7178 /*
7179 * check reference count again if the block isn't locked.
7180 * we should start walking down the tree again if reference
7181 * count is one.
7182 */
7183 if (!path->locks[level]) {
7184 BUG_ON(level == 0);
7185 btrfs_tree_lock(eb);
7186 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007187 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007188
7189 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007190 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007191 &wc->refs[level],
7192 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007193 if (ret < 0) {
7194 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007195 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007196 return ret;
7197 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007198 BUG_ON(wc->refs[level] == 0);
7199 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007200 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007201 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007202 return 1;
7203 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007204 }
7205 }
7206
7207 /* wc->stage == DROP_REFERENCE */
7208 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7209
7210 if (wc->refs[level] == 1) {
7211 if (level == 0) {
7212 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007213 ret = btrfs_dec_ref(trans, root, eb, 1,
7214 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007215 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007216 ret = btrfs_dec_ref(trans, root, eb, 0,
7217 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007218 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007219 }
7220 /* make block locked assertion in clean_tree_block happy */
7221 if (!path->locks[level] &&
7222 btrfs_header_generation(eb) == trans->transid) {
7223 btrfs_tree_lock(eb);
7224 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007225 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007226 }
7227 clean_tree_block(trans, root, eb);
7228 }
7229
7230 if (eb == root->node) {
7231 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7232 parent = eb->start;
7233 else
7234 BUG_ON(root->root_key.objectid !=
7235 btrfs_header_owner(eb));
7236 } else {
7237 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7238 parent = path->nodes[level + 1]->start;
7239 else
7240 BUG_ON(root->root_key.objectid !=
7241 btrfs_header_owner(path->nodes[level + 1]));
7242 }
7243
Jan Schmidt5581a512012-05-16 17:04:52 +02007244 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007245out:
7246 wc->refs[level] = 0;
7247 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007248 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007249}
7250
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007251static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7252 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007253 struct btrfs_path *path,
7254 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007255{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007256 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007257 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007258 int ret;
7259
Yan Zheng2c47e6052009-06-27 21:07:35 -04007260 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007261 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007262 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007263 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007264
Yan Zheng2c47e6052009-06-27 21:07:35 -04007265 if (level == 0)
7266 break;
7267
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007268 if (path->slots[level] >=
7269 btrfs_header_nritems(path->nodes[level]))
7270 break;
7271
Yan, Zheng94fcca92009-10-09 09:25:16 -04007272 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007273 if (ret > 0) {
7274 path->slots[level]++;
7275 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00007276 } else if (ret < 0)
7277 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007278 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007279 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007280 return 0;
7281}
7282
Chris Masond3977122009-01-05 21:25:51 -05007283static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007284 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007285 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007286 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007287{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007288 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007289 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007290
Yan Zheng2c47e6052009-06-27 21:07:35 -04007291 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7292 while (level < max_level && path->nodes[level]) {
7293 wc->level = level;
7294 if (path->slots[level] + 1 <
7295 btrfs_header_nritems(path->nodes[level])) {
7296 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007297 return 0;
7298 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007299 ret = walk_up_proc(trans, root, path, wc);
7300 if (ret > 0)
7301 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007302
Yan Zheng2c47e6052009-06-27 21:07:35 -04007303 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007304 btrfs_tree_unlock_rw(path->nodes[level],
7305 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007306 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007307 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007308 free_extent_buffer(path->nodes[level]);
7309 path->nodes[level] = NULL;
7310 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007311 }
7312 }
7313 return 1;
7314}
7315
Chris Mason9aca1d52007-03-13 11:09:37 -04007316/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007317 * drop a subvolume tree.
7318 *
7319 * this function traverses the tree freeing any blocks that only
7320 * referenced by the tree.
7321 *
7322 * when a shared tree block is found. this function decreases its
7323 * reference count by one. if update_ref is true, this function
7324 * also make sure backrefs for the shared block and all lower level
7325 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00007326 *
7327 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04007328 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007329int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007330 struct btrfs_block_rsv *block_rsv, int update_ref,
7331 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007332{
Chris Mason5caf2a02007-04-02 11:20:42 -04007333 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007334 struct btrfs_trans_handle *trans;
7335 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007336 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007337 struct walk_control *wc;
7338 struct btrfs_key key;
7339 int err = 0;
7340 int ret;
7341 int level;
Chris Mason20524f02007-03-10 06:35:47 -05007342
Chris Mason5caf2a02007-04-02 11:20:42 -04007343 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007344 if (!path) {
7345 err = -ENOMEM;
7346 goto out;
7347 }
Chris Mason20524f02007-03-10 06:35:47 -05007348
Yan Zheng2c47e6052009-06-27 21:07:35 -04007349 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007350 if (!wc) {
7351 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007352 err = -ENOMEM;
7353 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007354 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007355
Yan, Zhenga22285a2010-05-16 10:48:46 -04007356 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007357 if (IS_ERR(trans)) {
7358 err = PTR_ERR(trans);
7359 goto out_free;
7360 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007361
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007362 if (block_rsv)
7363 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007364
Chris Mason9f3a7422007-08-07 15:52:19 -04007365 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007366 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007367 path->nodes[level] = btrfs_lock_root_node(root);
7368 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007369 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007370 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007371 memset(&wc->update_progress, 0,
7372 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007373 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007374 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007375 memcpy(&wc->update_progress, &key,
7376 sizeof(wc->update_progress));
7377
Chris Mason6702ed42007-08-07 16:15:09 -04007378 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007379 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007380 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007381 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7382 path->lowest_level = 0;
7383 if (ret < 0) {
7384 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007385 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007386 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007387 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007388
Chris Mason7d9eb122008-07-08 14:19:17 -04007389 /*
7390 * unlock our path, this is safe because only this
7391 * function is allowed to delete this snapshot
7392 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007393 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007394
Yan Zheng2c47e6052009-06-27 21:07:35 -04007395 level = btrfs_header_level(root->node);
7396 while (1) {
7397 btrfs_tree_lock(path->nodes[level]);
7398 btrfs_set_lock_blocking(path->nodes[level]);
7399
7400 ret = btrfs_lookup_extent_info(trans, root,
7401 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05007402 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04007403 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007404 if (ret < 0) {
7405 err = ret;
7406 goto out_end_trans;
7407 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007408 BUG_ON(wc->refs[level] == 0);
7409
7410 if (level == root_item->drop_level)
7411 break;
7412
7413 btrfs_tree_unlock(path->nodes[level]);
7414 WARN_ON(wc->refs[level] != 1);
7415 level--;
7416 }
7417 }
7418
7419 wc->level = level;
7420 wc->shared_level = -1;
7421 wc->stage = DROP_REFERENCE;
7422 wc->update_ref = update_ref;
7423 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007424 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007425 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007426
7427 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00007428 if (!for_reloc && btrfs_fs_closing(root->fs_info)) {
7429 pr_debug("btrfs: drop snapshot early exit\n");
7430 err = -EAGAIN;
7431 goto out_end_trans;
7432 }
7433
Yan Zheng2c47e6052009-06-27 21:07:35 -04007434 ret = walk_down_tree(trans, root, path, wc);
7435 if (ret < 0) {
7436 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007437 break;
7438 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007439
7440 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7441 if (ret < 0) {
7442 err = ret;
7443 break;
7444 }
7445
7446 if (ret > 0) {
7447 BUG_ON(wc->stage != DROP_REFERENCE);
7448 break;
7449 }
7450
7451 if (wc->stage == DROP_REFERENCE) {
7452 level = wc->level;
7453 btrfs_node_key(path->nodes[level],
7454 &root_item->drop_progress,
7455 path->slots[level]);
7456 root_item->drop_level = level;
7457 }
7458
7459 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007460 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007461 ret = btrfs_update_root(trans, tree_root,
7462 &root->root_key,
7463 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007464 if (ret) {
7465 btrfs_abort_transaction(trans, tree_root, ret);
7466 err = ret;
7467 goto out_end_trans;
7468 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007469
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007470 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007471 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007472 if (IS_ERR(trans)) {
7473 err = PTR_ERR(trans);
7474 goto out_free;
7475 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007476 if (block_rsv)
7477 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007478 }
Chris Mason20524f02007-03-10 06:35:47 -05007479 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007480 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007481 if (err)
7482 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007483
7484 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007485 if (ret) {
7486 btrfs_abort_transaction(trans, tree_root, ret);
7487 goto out_end_trans;
7488 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007489
Yan, Zheng76dda932009-09-21 16:00:26 -04007490 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7491 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7492 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007493 if (ret < 0) {
7494 btrfs_abort_transaction(trans, tree_root, ret);
7495 err = ret;
7496 goto out_end_trans;
7497 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007498 /* if we fail to delete the orphan item this time
7499 * around, it'll get picked up the next time.
7500 *
7501 * The most common failure here is just -ENOENT.
7502 */
7503 btrfs_del_orphan_item(trans, tree_root,
7504 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007505 }
7506 }
7507
7508 if (root->in_radix) {
7509 btrfs_free_fs_root(tree_root->fs_info, root);
7510 } else {
7511 free_extent_buffer(root->node);
7512 free_extent_buffer(root->commit_root);
7513 kfree(root);
7514 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007515out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007516 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007517out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007518 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007519 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007520out:
7521 if (err)
7522 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007523 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007524}
Chris Mason9078a3e2007-04-26 16:46:15 -04007525
Yan Zheng2c47e6052009-06-27 21:07:35 -04007526/*
7527 * drop subtree rooted at tree block 'node'.
7528 *
7529 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007530 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007531 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007532int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7533 struct btrfs_root *root,
7534 struct extent_buffer *node,
7535 struct extent_buffer *parent)
7536{
7537 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007538 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007539 int level;
7540 int parent_level;
7541 int ret = 0;
7542 int wret;
7543
Yan Zheng2c47e6052009-06-27 21:07:35 -04007544 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7545
Yan Zhengf82d02d2008-10-29 14:49:05 -04007546 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007547 if (!path)
7548 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007549
Yan Zheng2c47e6052009-06-27 21:07:35 -04007550 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007551 if (!wc) {
7552 btrfs_free_path(path);
7553 return -ENOMEM;
7554 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007555
Chris Masonb9447ef2009-03-09 11:45:38 -04007556 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007557 parent_level = btrfs_header_level(parent);
7558 extent_buffer_get(parent);
7559 path->nodes[parent_level] = parent;
7560 path->slots[parent_level] = btrfs_header_nritems(parent);
7561
Chris Masonb9447ef2009-03-09 11:45:38 -04007562 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007563 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007564 path->nodes[level] = node;
7565 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007566 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007567
7568 wc->refs[parent_level] = 1;
7569 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7570 wc->level = level;
7571 wc->shared_level = -1;
7572 wc->stage = DROP_REFERENCE;
7573 wc->update_ref = 0;
7574 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007575 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007576 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007577
7578 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007579 wret = walk_down_tree(trans, root, path, wc);
7580 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007581 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007582 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007583 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007584
Yan Zheng2c47e6052009-06-27 21:07:35 -04007585 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007586 if (wret < 0)
7587 ret = wret;
7588 if (wret != 0)
7589 break;
7590 }
7591
Yan Zheng2c47e6052009-06-27 21:07:35 -04007592 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007593 btrfs_free_path(path);
7594 return ret;
7595}
7596
Chris Masonec44a352008-04-28 15:29:52 -04007597static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7598{
7599 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007600 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007601
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007602 /*
7603 * if restripe for this chunk_type is on pick target profile and
7604 * return, otherwise do the usual balance
7605 */
7606 stripped = get_restripe_target(root->fs_info, flags);
7607 if (stripped)
7608 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007609
Chris Masoncd02dca2010-12-13 14:56:23 -05007610 /*
7611 * we add in the count of missing devices because we want
7612 * to make sure that any RAID levels on a degraded FS
7613 * continue to be honored.
7614 */
7615 num_devices = root->fs_info->fs_devices->rw_devices +
7616 root->fs_info->fs_devices->missing_devices;
7617
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007618 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007619 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007620 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7621
Chris Masonec44a352008-04-28 15:29:52 -04007622 if (num_devices == 1) {
7623 stripped |= BTRFS_BLOCK_GROUP_DUP;
7624 stripped = flags & ~stripped;
7625
7626 /* turn raid0 into single device chunks */
7627 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7628 return stripped;
7629
7630 /* turn mirroring into duplication */
7631 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7632 BTRFS_BLOCK_GROUP_RAID10))
7633 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007634 } else {
7635 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007636 if (flags & stripped)
7637 return flags;
7638
7639 stripped |= BTRFS_BLOCK_GROUP_DUP;
7640 stripped = flags & ~stripped;
7641
7642 /* switch duplicated blocks with raid1 */
7643 if (flags & BTRFS_BLOCK_GROUP_DUP)
7644 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7645
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007646 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007647 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007648
Chris Masonec44a352008-04-28 15:29:52 -04007649 return flags;
7650}
7651
Miao Xie199c36e2011-07-15 10:34:36 +00007652static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007653{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007654 struct btrfs_space_info *sinfo = cache->space_info;
7655 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007656 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007657 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007658
Chris Masonc286ac42008-07-22 23:06:41 -04007659
Miao Xie199c36e2011-07-15 10:34:36 +00007660 /*
7661 * We need some metadata space and system metadata space for
7662 * allocating chunks in some corner cases until we force to set
7663 * it to be readonly.
7664 */
7665 if ((sinfo->flags &
7666 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7667 !force)
7668 min_allocable_bytes = 1 * 1024 * 1024;
7669 else
7670 min_allocable_bytes = 0;
7671
Yan, Zhengf0486c62010-05-16 10:46:25 -04007672 spin_lock(&sinfo->lock);
7673 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007674
7675 if (cache->ro) {
7676 ret = 0;
7677 goto out;
7678 }
7679
Yan, Zhengf0486c62010-05-16 10:46:25 -04007680 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7681 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007682
Yan, Zhengf0486c62010-05-16 10:46:25 -04007683 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007684 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7685 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007686 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007687 cache->ro = 1;
7688 ret = 0;
7689 }
WuBo61cfea92011-07-26 03:30:11 +00007690out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007691 spin_unlock(&cache->lock);
7692 spin_unlock(&sinfo->lock);
7693 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007694}
7695
Yan, Zhengf0486c62010-05-16 10:46:25 -04007696int btrfs_set_block_group_ro(struct btrfs_root *root,
7697 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007698
7699{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007700 struct btrfs_trans_handle *trans;
7701 u64 alloc_flags;
7702 int ret;
7703
7704 BUG_ON(cache->ro);
7705
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007706 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007707 if (IS_ERR(trans))
7708 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007709
7710 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007711 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007712 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007713 CHUNK_ALLOC_FORCE);
7714 if (ret < 0)
7715 goto out;
7716 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007717
Miao Xie199c36e2011-07-15 10:34:36 +00007718 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007719 if (!ret)
7720 goto out;
7721 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007722 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007723 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007724 if (ret < 0)
7725 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007726 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007727out:
7728 btrfs_end_transaction(trans, root);
7729 return ret;
7730}
7731
Chris Masonc87f08c2011-02-16 13:57:04 -05007732int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7733 struct btrfs_root *root, u64 type)
7734{
7735 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007736 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007737 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007738}
7739
Miao Xie6d07bce2011-01-05 10:07:31 +00007740/*
7741 * helper to account the unused space of all the readonly block group in the
7742 * list. takes mirrors into account.
7743 */
7744static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7745{
7746 struct btrfs_block_group_cache *block_group;
7747 u64 free_bytes = 0;
7748 int factor;
7749
7750 list_for_each_entry(block_group, groups_list, list) {
7751 spin_lock(&block_group->lock);
7752
7753 if (!block_group->ro) {
7754 spin_unlock(&block_group->lock);
7755 continue;
7756 }
7757
7758 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7759 BTRFS_BLOCK_GROUP_RAID10 |
7760 BTRFS_BLOCK_GROUP_DUP))
7761 factor = 2;
7762 else
7763 factor = 1;
7764
7765 free_bytes += (block_group->key.offset -
7766 btrfs_block_group_used(&block_group->item)) *
7767 factor;
7768
7769 spin_unlock(&block_group->lock);
7770 }
7771
7772 return free_bytes;
7773}
7774
7775/*
7776 * helper to account the unused space of all the readonly block group in the
7777 * space_info. takes mirrors into account.
7778 */
7779u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7780{
7781 int i;
7782 u64 free_bytes = 0;
7783
7784 spin_lock(&sinfo->lock);
7785
7786 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7787 if (!list_empty(&sinfo->block_groups[i]))
7788 free_bytes += __btrfs_get_ro_block_group_free_space(
7789 &sinfo->block_groups[i]);
7790
7791 spin_unlock(&sinfo->lock);
7792
7793 return free_bytes;
7794}
7795
Jeff Mahoney143bede2012-03-01 14:56:26 +01007796void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007797 struct btrfs_block_group_cache *cache)
7798{
7799 struct btrfs_space_info *sinfo = cache->space_info;
7800 u64 num_bytes;
7801
7802 BUG_ON(!cache->ro);
7803
7804 spin_lock(&sinfo->lock);
7805 spin_lock(&cache->lock);
7806 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7807 cache->bytes_super - btrfs_block_group_used(&cache->item);
7808 sinfo->bytes_readonly -= num_bytes;
7809 cache->ro = 0;
7810 spin_unlock(&cache->lock);
7811 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007812}
7813
Josef Bacikba1bf482009-09-11 16:11:19 -04007814/*
7815 * checks to see if its even possible to relocate this block group.
7816 *
7817 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7818 * ok to go ahead and try.
7819 */
7820int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007821{
Zheng Yan1a40e232008-09-26 10:09:34 -04007822 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007823 struct btrfs_space_info *space_info;
7824 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7825 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007826 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007827 u64 dev_min = 1;
7828 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007829 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007830 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007831 int full = 0;
7832 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007833
Josef Bacikba1bf482009-09-11 16:11:19 -04007834 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007835
Josef Bacikba1bf482009-09-11 16:11:19 -04007836 /* odd, couldn't find the block group, leave it alone */
7837 if (!block_group)
7838 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007839
liubocdcb7252011-08-03 10:15:25 +00007840 min_free = btrfs_block_group_used(&block_group->item);
7841
Josef Bacikba1bf482009-09-11 16:11:19 -04007842 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007843 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007844 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007845
Josef Bacikba1bf482009-09-11 16:11:19 -04007846 space_info = block_group->space_info;
7847 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007848
Josef Bacikba1bf482009-09-11 16:11:19 -04007849 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007850
Josef Bacikba1bf482009-09-11 16:11:19 -04007851 /*
7852 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007853 * relocate it unless we're able to allocate a new chunk below.
7854 *
7855 * Otherwise, we need to make sure we have room in the space to handle
7856 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007857 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007858 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007859 (space_info->bytes_used + space_info->bytes_reserved +
7860 space_info->bytes_pinned + space_info->bytes_readonly +
7861 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007862 spin_unlock(&space_info->lock);
7863 goto out;
7864 }
7865 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007866
Josef Bacikba1bf482009-09-11 16:11:19 -04007867 /*
7868 * ok we don't have enough space, but maybe we have free space on our
7869 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007870 * alloc devices and guess if we have enough space. if this block
7871 * group is going to be restriped, run checks against the target
7872 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007873 */
7874 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007875
liubocdcb7252011-08-03 10:15:25 +00007876 /*
7877 * index:
7878 * 0: raid10
7879 * 1: raid1
7880 * 2: dup
7881 * 3: raid0
7882 * 4: single
7883 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007884 target = get_restripe_target(root->fs_info, block_group->flags);
7885 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00007886 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007887 } else {
7888 /*
7889 * this is just a balance, so if we were marked as full
7890 * we know there is no space for a new chunk
7891 */
7892 if (full)
7893 goto out;
7894
7895 index = get_block_group_index(block_group);
7896 }
7897
Miao Xiee6ec7162013-01-17 05:38:51 +00007898 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00007899 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007900 /* Divide by 2 */
7901 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007902 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00007903 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00007904 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04007905 /* Multiply by 2 */
7906 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007907 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00007908 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007909 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007910 }
7911
Josef Bacikba1bf482009-09-11 16:11:19 -04007912 mutex_lock(&root->fs_info->chunk_mutex);
7913 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007914 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007915
Josef Bacikba1bf482009-09-11 16:11:19 -04007916 /*
7917 * check to make sure we can actually find a chunk with enough
7918 * space to fit our block group in.
7919 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01007920 if (device->total_bytes > device->bytes_used + min_free &&
7921 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08007922 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007923 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007924 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007925 dev_nr++;
7926
7927 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007928 break;
liubocdcb7252011-08-03 10:15:25 +00007929
Josef Bacikba1bf482009-09-11 16:11:19 -04007930 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007931 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007932 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007933 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007934out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007935 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007936 return ret;
7937}
7938
Christoph Hellwigb2950862008-12-02 09:54:17 -05007939static int find_first_block_group(struct btrfs_root *root,
7940 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007941{
Chris Mason925baed2008-06-25 16:01:30 -04007942 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007943 struct btrfs_key found_key;
7944 struct extent_buffer *leaf;
7945 int slot;
7946
7947 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7948 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007949 goto out;
7950
Chris Masond3977122009-01-05 21:25:51 -05007951 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007952 slot = path->slots[0];
7953 leaf = path->nodes[0];
7954 if (slot >= btrfs_header_nritems(leaf)) {
7955 ret = btrfs_next_leaf(root, path);
7956 if (ret == 0)
7957 continue;
7958 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007959 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007960 break;
7961 }
7962 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7963
7964 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007965 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7966 ret = 0;
7967 goto out;
7968 }
Chris Mason0b86a832008-03-24 15:01:56 -04007969 path->slots[0]++;
7970 }
Chris Mason925baed2008-06-25 16:01:30 -04007971out:
Chris Mason0b86a832008-03-24 15:01:56 -04007972 return ret;
7973}
7974
Josef Bacik0af3d002010-06-21 14:48:16 -04007975void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
7976{
7977 struct btrfs_block_group_cache *block_group;
7978 u64 last = 0;
7979
7980 while (1) {
7981 struct inode *inode;
7982
7983 block_group = btrfs_lookup_first_block_group(info, last);
7984 while (block_group) {
7985 spin_lock(&block_group->lock);
7986 if (block_group->iref)
7987 break;
7988 spin_unlock(&block_group->lock);
7989 block_group = next_block_group(info->tree_root,
7990 block_group);
7991 }
7992 if (!block_group) {
7993 if (last == 0)
7994 break;
7995 last = 0;
7996 continue;
7997 }
7998
7999 inode = block_group->inode;
8000 block_group->iref = 0;
8001 block_group->inode = NULL;
8002 spin_unlock(&block_group->lock);
8003 iput(inode);
8004 last = block_group->key.objectid + block_group->key.offset;
8005 btrfs_put_block_group(block_group);
8006 }
8007}
8008
Zheng Yan1a40e232008-09-26 10:09:34 -04008009int btrfs_free_block_groups(struct btrfs_fs_info *info)
8010{
8011 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008012 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008013 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008014 struct rb_node *n;
8015
Yan Zheng11833d62009-09-11 16:11:19 -04008016 down_write(&info->extent_commit_sem);
8017 while (!list_empty(&info->caching_block_groups)) {
8018 caching_ctl = list_entry(info->caching_block_groups.next,
8019 struct btrfs_caching_control, list);
8020 list_del(&caching_ctl->list);
8021 put_caching_control(caching_ctl);
8022 }
8023 up_write(&info->extent_commit_sem);
8024
Zheng Yan1a40e232008-09-26 10:09:34 -04008025 spin_lock(&info->block_group_cache_lock);
8026 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8027 block_group = rb_entry(n, struct btrfs_block_group_cache,
8028 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008029 rb_erase(&block_group->cache_node,
8030 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008031 spin_unlock(&info->block_group_cache_lock);
8032
Josef Bacik80eb2342008-10-29 14:49:05 -04008033 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008034 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008035 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008036
Josef Bacik817d52f2009-07-13 21:29:25 -04008037 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008038 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008039
Josef Bacik3c148742011-02-02 15:53:47 +00008040 /*
8041 * We haven't cached this block group, which means we could
8042 * possibly have excluded extents on this block group.
8043 */
8044 if (block_group->cached == BTRFS_CACHE_NO)
8045 free_excluded_extents(info->extent_root, block_group);
8046
Josef Bacik817d52f2009-07-13 21:29:25 -04008047 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008048 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008049
8050 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008051 }
8052 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008053
8054 /* now that all the block groups are freed, go through and
8055 * free all the space_info structs. This is only called during
8056 * the final stages of unmount, and so we know nobody is
8057 * using them. We call synchronize_rcu() once before we start,
8058 * just to be on the safe side.
8059 */
8060 synchronize_rcu();
8061
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008062 release_global_block_rsv(info);
8063
Chris Mason4184ea72009-03-10 12:39:20 -04008064 while(!list_empty(&info->space_info)) {
8065 space_info = list_entry(info->space_info.next,
8066 struct btrfs_space_info,
8067 list);
David Sterbab069e0c2013-02-08 21:28:17 +00008068 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
8069 if (space_info->bytes_pinned > 0 ||
8070 space_info->bytes_reserved > 0 ||
8071 space_info->bytes_may_use > 0) {
8072 WARN_ON(1);
8073 dump_space_info(space_info, 0, 0);
8074 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008075 }
Chris Mason4184ea72009-03-10 12:39:20 -04008076 list_del(&space_info->list);
8077 kfree(space_info);
8078 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008079 return 0;
8080}
8081
Yan, Zhengb742bb82010-05-16 10:46:24 -04008082static void __link_block_group(struct btrfs_space_info *space_info,
8083 struct btrfs_block_group_cache *cache)
8084{
8085 int index = get_block_group_index(cache);
8086
8087 down_write(&space_info->groups_sem);
8088 list_add_tail(&cache->list, &space_info->block_groups[index]);
8089 up_write(&space_info->groups_sem);
8090}
8091
Chris Mason9078a3e2007-04-26 16:46:15 -04008092int btrfs_read_block_groups(struct btrfs_root *root)
8093{
8094 struct btrfs_path *path;
8095 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008096 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008097 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008098 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008099 struct btrfs_key key;
8100 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008101 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008102 int need_clear = 0;
8103 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008104
Chris Masonbe744172007-05-06 10:15:01 -04008105 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008106 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008107 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008108 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008109 path = btrfs_alloc_path();
8110 if (!path)
8111 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04008112 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04008113
David Sterba6c417612011-04-13 15:41:04 +02008114 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04008115 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02008116 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04008117 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008118 if (btrfs_test_opt(root, CLEAR_CACHE))
8119 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04008120
Chris Masond3977122009-01-05 21:25:51 -05008121 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008122 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008123 if (ret > 0)
8124 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008125 if (ret != 0)
8126 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008127 leaf = path->nodes[0];
8128 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008129 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008130 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008131 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008132 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008133 }
Li Zefan34d52cb2011-03-29 13:46:06 +08008134 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8135 GFP_NOFS);
8136 if (!cache->free_space_ctl) {
8137 kfree(cache);
8138 ret = -ENOMEM;
8139 goto error;
8140 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008141
Yan Zhengd2fb3432008-12-11 16:30:39 -05008142 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008143 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008144 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008145 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008146 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008147
Liu Bocf7c1ef2012-07-06 03:31:34 -06008148 if (need_clear) {
8149 /*
8150 * When we mount with old space cache, we need to
8151 * set BTRFS_DC_CLEAR and set dirty flag.
8152 *
8153 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8154 * truncate the old free space cache inode and
8155 * setup a new one.
8156 * b) Setting 'dirty flag' makes sure that we flush
8157 * the new space cache info onto disk.
8158 */
Josef Bacik0af3d002010-06-21 14:48:16 -04008159 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06008160 if (btrfs_test_opt(root, SPACE_CACHE))
8161 cache->dirty = 1;
8162 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008163
Chris Mason5f39d392007-10-15 16:14:19 -04008164 read_extent_buffer(leaf, &cache->item,
8165 btrfs_item_ptr_offset(leaf, path->slots[0]),
8166 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008167 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008168
Chris Mason9078a3e2007-04-26 16:46:15 -04008169 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02008170 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008171 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008172 cache->sectorsize = root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05008173 cache->full_stripe_len = btrfs_full_stripe_len(root,
8174 &root->fs_info->mapping_tree,
8175 found_key.objectid);
Li Zefan34d52cb2011-03-29 13:46:06 +08008176 btrfs_init_free_space_ctl(cache);
8177
Josef Bacik817d52f2009-07-13 21:29:25 -04008178 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008179 * We need to exclude the super stripes now so that the space
8180 * info has super bytes accounted for, otherwise we'll think
8181 * we have more space than we actually do.
8182 */
Josef Bacik835d9742013-03-19 12:13:25 -04008183 ret = exclude_super_stripes(root, cache);
8184 if (ret) {
8185 /*
8186 * We may have excluded something, so call this just in
8187 * case.
8188 */
8189 free_excluded_extents(root, cache);
8190 kfree(cache->free_space_ctl);
8191 kfree(cache);
8192 goto error;
8193 }
Josef Bacik3c148742011-02-02 15:53:47 +00008194
8195 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008196 * check for two cases, either we are full, and therefore
8197 * don't need to bother with the caching work since we won't
8198 * find any space, or we are empty, and we can just add all
8199 * the space in and be done with it. This saves us _alot_ of
8200 * time, particularly in the full case.
8201 */
8202 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008203 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008204 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008205 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008206 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008207 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008208 cache->cached = BTRFS_CACHE_FINISHED;
8209 add_new_free_space(cache, root->fs_info,
8210 found_key.objectid,
8211 found_key.objectid +
8212 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008213 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008214 }
Chris Mason96b51792007-10-15 16:15:19 -04008215
Josef Bacik8c579fe2013-04-02 12:40:42 -04008216 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8217 if (ret) {
8218 btrfs_remove_free_space_cache(cache);
8219 btrfs_put_block_group(cache);
8220 goto error;
8221 }
8222
Chris Mason6324fbf2008-03-24 15:01:59 -04008223 ret = update_space_info(info, cache->flags, found_key.offset,
8224 btrfs_block_group_used(&cache->item),
8225 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008226 if (ret) {
8227 btrfs_remove_free_space_cache(cache);
8228 spin_lock(&info->block_group_cache_lock);
8229 rb_erase(&cache->cache_node,
8230 &info->block_group_cache_tree);
8231 spin_unlock(&info->block_group_cache_lock);
8232 btrfs_put_block_group(cache);
8233 goto error;
8234 }
8235
Chris Mason6324fbf2008-03-24 15:01:59 -04008236 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008237 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008238 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008239 spin_unlock(&cache->space_info->lock);
8240
Yan, Zhengb742bb82010-05-16 10:46:24 -04008241 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008242
Chris Mason75ccf472008-09-30 19:24:06 -04008243 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008244 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008245 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008246 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008247
8248 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8249 if (!(get_alloc_profile(root, space_info->flags) &
8250 (BTRFS_BLOCK_GROUP_RAID10 |
8251 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008252 BTRFS_BLOCK_GROUP_RAID5 |
8253 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04008254 BTRFS_BLOCK_GROUP_DUP)))
8255 continue;
8256 /*
8257 * avoid allocating from un-mirrored block group if there are
8258 * mirrored block groups.
8259 */
8260 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008261 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008262 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008263 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008264 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008265
8266 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008267 ret = 0;
8268error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008269 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008270 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008271}
Chris Mason6324fbf2008-03-24 15:01:59 -04008272
Josef Bacikea658ba2012-09-11 16:57:25 -04008273void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8274 struct btrfs_root *root)
8275{
8276 struct btrfs_block_group_cache *block_group, *tmp;
8277 struct btrfs_root *extent_root = root->fs_info->extent_root;
8278 struct btrfs_block_group_item item;
8279 struct btrfs_key key;
8280 int ret = 0;
8281
8282 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8283 new_bg_list) {
8284 list_del_init(&block_group->new_bg_list);
8285
8286 if (ret)
8287 continue;
8288
8289 spin_lock(&block_group->lock);
8290 memcpy(&item, &block_group->item, sizeof(item));
8291 memcpy(&key, &block_group->key, sizeof(key));
8292 spin_unlock(&block_group->lock);
8293
8294 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8295 sizeof(item));
8296 if (ret)
8297 btrfs_abort_transaction(trans, extent_root, ret);
8298 }
8299}
8300
Chris Mason6324fbf2008-03-24 15:01:59 -04008301int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8302 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008303 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008304 u64 size)
8305{
8306 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008307 struct btrfs_root *extent_root;
8308 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008309
8310 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008311
Chris Mason12fcfd22009-03-24 10:24:20 -04008312 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008313
Chris Mason8f18cf12008-04-25 16:53:30 -04008314 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008315 if (!cache)
8316 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08008317 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8318 GFP_NOFS);
8319 if (!cache->free_space_ctl) {
8320 kfree(cache);
8321 return -ENOMEM;
8322 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008323
Chris Masone17cade2008-04-15 15:41:47 -04008324 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008325 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008326 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008327 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008328 cache->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05008329 cache->full_stripe_len = btrfs_full_stripe_len(root,
8330 &root->fs_info->mapping_tree,
8331 chunk_offset);
Josef Bacik96303082009-07-13 21:29:25 -04008332
Yan Zhengd2fb3432008-12-11 16:30:39 -05008333 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008334 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008335 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008336 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04008337 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008338
Li Zefan34d52cb2011-03-29 13:46:06 +08008339 btrfs_init_free_space_ctl(cache);
8340
Chris Mason6324fbf2008-03-24 15:01:59 -04008341 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008342 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8343 cache->flags = type;
8344 btrfs_set_block_group_flags(&cache->item, type);
8345
Yan Zheng11833d62009-09-11 16:11:19 -04008346 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008347 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04008348 ret = exclude_super_stripes(root, cache);
8349 if (ret) {
8350 /*
8351 * We may have excluded something, so call this just in
8352 * case.
8353 */
8354 free_excluded_extents(root, cache);
8355 kfree(cache->free_space_ctl);
8356 kfree(cache);
8357 return ret;
8358 }
Josef Bacik96303082009-07-13 21:29:25 -04008359
Josef Bacik817d52f2009-07-13 21:29:25 -04008360 add_new_free_space(cache, root->fs_info, chunk_offset,
8361 chunk_offset + size);
8362
Yan Zheng11833d62009-09-11 16:11:19 -04008363 free_excluded_extents(root, cache);
8364
Josef Bacik8c579fe2013-04-02 12:40:42 -04008365 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8366 if (ret) {
8367 btrfs_remove_free_space_cache(cache);
8368 btrfs_put_block_group(cache);
8369 return ret;
8370 }
8371
Chris Mason6324fbf2008-03-24 15:01:59 -04008372 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8373 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008374 if (ret) {
8375 btrfs_remove_free_space_cache(cache);
8376 spin_lock(&root->fs_info->block_group_cache_lock);
8377 rb_erase(&cache->cache_node,
8378 &root->fs_info->block_group_cache_tree);
8379 spin_unlock(&root->fs_info->block_group_cache_lock);
8380 btrfs_put_block_group(cache);
8381 return ret;
8382 }
Li Zefanc7c144d2011-12-07 10:39:22 +08008383 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008384
8385 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008386 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008387 spin_unlock(&cache->space_info->lock);
8388
Yan, Zhengb742bb82010-05-16 10:46:24 -04008389 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008390
Josef Bacikea658ba2012-09-11 16:57:25 -04008391 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008392
Chris Masond18a2c42008-04-04 15:40:00 -04008393 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008394
Chris Mason6324fbf2008-03-24 15:01:59 -04008395 return 0;
8396}
Zheng Yan1a40e232008-09-26 10:09:34 -04008397
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008398static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8399{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008400 u64 extra_flags = chunk_to_extended(flags) &
8401 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008402
Miao Xiede98ced2013-01-29 10:13:12 +00008403 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008404 if (flags & BTRFS_BLOCK_GROUP_DATA)
8405 fs_info->avail_data_alloc_bits &= ~extra_flags;
8406 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8407 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8408 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8409 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008410 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008411}
8412
Zheng Yan1a40e232008-09-26 10:09:34 -04008413int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8414 struct btrfs_root *root, u64 group_start)
8415{
8416 struct btrfs_path *path;
8417 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008418 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008419 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008420 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008421 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008422 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008423 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008424 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008425
Zheng Yan1a40e232008-09-26 10:09:34 -04008426 root = root->fs_info->extent_root;
8427
8428 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8429 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008430 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008431
liubo9f7c43c2011-03-07 02:13:33 +00008432 /*
8433 * Free the reserved super bytes from this block group before
8434 * remove it.
8435 */
8436 free_excluded_extents(root, block_group);
8437
Zheng Yan1a40e232008-09-26 10:09:34 -04008438 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008439 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008440 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8441 BTRFS_BLOCK_GROUP_RAID1 |
8442 BTRFS_BLOCK_GROUP_RAID10))
8443 factor = 2;
8444 else
8445 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008446
Chris Mason44fb5512009-06-04 15:34:51 -04008447 /* make sure this block group isn't part of an allocation cluster */
8448 cluster = &root->fs_info->data_alloc_cluster;
8449 spin_lock(&cluster->refill_lock);
8450 btrfs_return_cluster_to_free_space(block_group, cluster);
8451 spin_unlock(&cluster->refill_lock);
8452
8453 /*
8454 * make sure this block group isn't part of a metadata
8455 * allocation cluster
8456 */
8457 cluster = &root->fs_info->meta_alloc_cluster;
8458 spin_lock(&cluster->refill_lock);
8459 btrfs_return_cluster_to_free_space(block_group, cluster);
8460 spin_unlock(&cluster->refill_lock);
8461
Zheng Yan1a40e232008-09-26 10:09:34 -04008462 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008463 if (!path) {
8464 ret = -ENOMEM;
8465 goto out;
8466 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008467
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008468 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008469 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008470 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008471 if (ret) {
8472 btrfs_add_delayed_iput(inode);
8473 goto out;
8474 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008475 clear_nlink(inode);
8476 /* One for the block groups ref */
8477 spin_lock(&block_group->lock);
8478 if (block_group->iref) {
8479 block_group->iref = 0;
8480 block_group->inode = NULL;
8481 spin_unlock(&block_group->lock);
8482 iput(inode);
8483 } else {
8484 spin_unlock(&block_group->lock);
8485 }
8486 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008487 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008488 }
8489
8490 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8491 key.offset = block_group->key.objectid;
8492 key.type = 0;
8493
8494 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8495 if (ret < 0)
8496 goto out;
8497 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008498 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008499 if (ret == 0) {
8500 ret = btrfs_del_item(trans, tree_root, path);
8501 if (ret)
8502 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008503 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008504 }
8505
Yan Zheng3dfdb932009-01-21 10:49:16 -05008506 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008507 rb_erase(&block_group->cache_node,
8508 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008509
8510 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8511 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008512 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008513
Josef Bacik80eb2342008-10-29 14:49:05 -04008514 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008515 /*
8516 * we must use list_del_init so people can check to see if they
8517 * are still on the list after taking the semaphore
8518 */
8519 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008520 if (list_empty(&block_group->space_info->block_groups[index]))
8521 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008522 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008523
Josef Bacik817d52f2009-07-13 21:29:25 -04008524 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008525 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008526
8527 btrfs_remove_free_space_cache(block_group);
8528
Yan Zhengc146afa2008-11-12 14:34:12 -05008529 spin_lock(&block_group->space_info->lock);
8530 block_group->space_info->total_bytes -= block_group->key.offset;
8531 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008532 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008533 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008534
Josef Bacik0af3d002010-06-21 14:48:16 -04008535 memcpy(&key, &block_group->key, sizeof(key));
8536
Chris Mason283bb192009-07-24 16:30:55 -04008537 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008538
Chris Masonfa9c0d792009-04-03 09:47:43 -04008539 btrfs_put_block_group(block_group);
8540 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008541
8542 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8543 if (ret > 0)
8544 ret = -EIO;
8545 if (ret < 0)
8546 goto out;
8547
8548 ret = btrfs_del_item(trans, root, path);
8549out:
8550 btrfs_free_path(path);
8551 return ret;
8552}
liuboacce9522011-01-06 19:30:25 +08008553
liuboc59021f2011-03-07 02:13:14 +00008554int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8555{
8556 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008557 struct btrfs_super_block *disk_super;
8558 u64 features;
8559 u64 flags;
8560 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008561 int ret;
8562
David Sterba6c417612011-04-13 15:41:04 +02008563 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008564 if (!btrfs_super_root(disk_super))
8565 return 1;
liuboc59021f2011-03-07 02:13:14 +00008566
liubo1aba86d2011-04-08 08:44:37 +00008567 features = btrfs_super_incompat_flags(disk_super);
8568 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8569 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008570
liubo1aba86d2011-04-08 08:44:37 +00008571 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8572 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008573 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008574 goto out;
liuboc59021f2011-03-07 02:13:14 +00008575
liubo1aba86d2011-04-08 08:44:37 +00008576 if (mixed) {
8577 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8578 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8579 } else {
8580 flags = BTRFS_BLOCK_GROUP_METADATA;
8581 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8582 if (ret)
8583 goto out;
8584
8585 flags = BTRFS_BLOCK_GROUP_DATA;
8586 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8587 }
8588out:
liuboc59021f2011-03-07 02:13:14 +00008589 return ret;
8590}
8591
liuboacce9522011-01-06 19:30:25 +08008592int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8593{
8594 return unpin_extent_range(root, start, end);
8595}
8596
8597int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008598 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008599{
Li Dongyang5378e602011-03-24 10:24:27 +00008600 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008601}
Li Dongyangf7039b12011-03-24 10:24:28 +00008602
8603int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8604{
8605 struct btrfs_fs_info *fs_info = root->fs_info;
8606 struct btrfs_block_group_cache *cache = NULL;
8607 u64 group_trimmed;
8608 u64 start;
8609 u64 end;
8610 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008611 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008612 int ret = 0;
8613
Liu Bo2cac13e2012-02-09 18:17:41 +08008614 /*
8615 * try to trim all FS space, our block group may start from non-zero.
8616 */
8617 if (range->len == total_bytes)
8618 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8619 else
8620 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008621
8622 while (cache) {
8623 if (cache->key.objectid >= (range->start + range->len)) {
8624 btrfs_put_block_group(cache);
8625 break;
8626 }
8627
8628 start = max(range->start, cache->key.objectid);
8629 end = min(range->start + range->len,
8630 cache->key.objectid + cache->key.offset);
8631
8632 if (end - start >= range->minlen) {
8633 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008634 ret = cache_block_group(cache, 0);
Li Dongyangf7039b12011-03-24 10:24:28 +00008635 if (!ret)
8636 wait_block_group_cache_done(cache);
8637 }
8638 ret = btrfs_trim_block_group(cache,
8639 &group_trimmed,
8640 start,
8641 end,
8642 range->minlen);
8643
8644 trimmed += group_trimmed;
8645 if (ret) {
8646 btrfs_put_block_group(cache);
8647 break;
8648 }
8649 }
8650
8651 cache = next_block_group(fs_info->tree_root, cache);
8652 }
8653
8654 range->len = trimmed;
8655 return ret;
8656}