blob: 0d8478700d7867bf72c4a903249fc04fd2fbb5a7 [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 Bacik1b2da372009-09-11 16:11:20 -0400273 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400274 ret = add_excluded_extent(root, logical[nr],
275 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400276 if (ret) {
277 kfree(logical);
278 return ret;
279 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400280 }
Yan Zheng11833d62009-09-11 16:11:19 -0400281
Josef Bacik817d52f2009-07-13 21:29:25 -0400282 kfree(logical);
283 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400284 return 0;
285}
286
Yan Zheng11833d62009-09-11 16:11:19 -0400287static struct btrfs_caching_control *
288get_caching_control(struct btrfs_block_group_cache *cache)
289{
290 struct btrfs_caching_control *ctl;
291
292 spin_lock(&cache->lock);
293 if (cache->cached != BTRFS_CACHE_STARTED) {
294 spin_unlock(&cache->lock);
295 return NULL;
296 }
297
Josef Bacikdde5abe2010-09-16 16:17:03 -0400298 /* We're loading it the fast way, so we don't have a caching_ctl. */
299 if (!cache->caching_ctl) {
300 spin_unlock(&cache->lock);
301 return NULL;
302 }
303
Yan Zheng11833d62009-09-11 16:11:19 -0400304 ctl = cache->caching_ctl;
305 atomic_inc(&ctl->count);
306 spin_unlock(&cache->lock);
307 return ctl;
308}
309
310static void put_caching_control(struct btrfs_caching_control *ctl)
311{
312 if (atomic_dec_and_test(&ctl->count))
313 kfree(ctl);
314}
315
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316/*
317 * this is only called by cache_block_group, since we could have freed extents
318 * we need to check the pinned_extents for any extents that can't be used yet
319 * since their free space will be released as soon as the transaction commits.
320 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400321static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400322 struct btrfs_fs_info *info, u64 start, u64 end)
323{
Josef Bacik817d52f2009-07-13 21:29:25 -0400324 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400325 int ret;
326
327 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400328 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400329 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400330 EXTENT_DIRTY | EXTENT_UPTODATE,
331 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400332 if (ret)
333 break;
334
Yan, Zheng06b23312009-11-26 09:31:11 +0000335 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400336 start = extent_end + 1;
337 } else if (extent_start > start && extent_start < end) {
338 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400339 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500340 ret = btrfs_add_free_space(block_group, start,
341 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100342 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400343 start = extent_end + 1;
344 } else {
345 break;
346 }
347 }
348
349 if (start < end) {
350 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400351 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500352 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100353 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400354 }
355
Josef Bacik817d52f2009-07-13 21:29:25 -0400356 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400357}
358
Josef Bacikbab39bf2011-06-30 14:42:28 -0400359static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400360{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400361 struct btrfs_block_group_cache *block_group;
362 struct btrfs_fs_info *fs_info;
363 struct btrfs_caching_control *caching_ctl;
364 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400365 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400366 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400367 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400368 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400369 u64 last = 0;
370 u32 nritems;
371 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400372
Josef Bacikbab39bf2011-06-30 14:42:28 -0400373 caching_ctl = container_of(work, struct btrfs_caching_control, work);
374 block_group = caching_ctl->block_group;
375 fs_info = block_group->fs_info;
376 extent_root = fs_info->extent_root;
377
Chris Masone37c9e62007-05-09 20:13:14 -0400378 path = btrfs_alloc_path();
379 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400380 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400381
Josef Bacik817d52f2009-07-13 21:29:25 -0400382 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400383
Chris Mason5cd57b22008-06-25 16:01:30 -0400384 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400385 * We don't want to deadlock with somebody trying to allocate a new
386 * extent for the extent root while also trying to search the extent
387 * root to add free space. So we skip locking and search the commit
388 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400389 */
390 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400391 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400392 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400393
Yan Zhenge4404d62008-12-12 10:03:26 -0500394 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400395 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400396 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400397again:
Yan Zheng11833d62009-09-11 16:11:19 -0400398 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400399 /* need to make sure the commit_root doesn't disappear */
400 down_read(&fs_info->extent_commit_sem);
401
Yan Zheng11833d62009-09-11 16:11:19 -0400402 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400403 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400404 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500405
Yan Zheng11833d62009-09-11 16:11:19 -0400406 leaf = path->nodes[0];
407 nritems = btrfs_header_nritems(leaf);
408
Chris Masond3977122009-01-05 21:25:51 -0500409 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200410 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400411 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400412 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400413 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400414
Yan Zheng11833d62009-09-11 16:11:19 -0400415 if (path->slots[0] < nritems) {
416 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
417 } else {
418 ret = find_next_key(path, 0, &key);
419 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400420 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400421
Josef Bacik589d8ad2011-05-11 17:30:53 -0400422 if (need_resched() ||
423 btrfs_next_leaf(extent_root, path)) {
424 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400425 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400426 up_read(&fs_info->extent_commit_sem);
427 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400428 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400429 goto again;
430 }
431 leaf = path->nodes[0];
432 nritems = btrfs_header_nritems(leaf);
433 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400434 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400435
Yan Zheng11833d62009-09-11 16:11:19 -0400436 if (key.objectid < block_group->key.objectid) {
437 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400438 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400439 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400440
Chris Masone37c9e62007-05-09 20:13:14 -0400441 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400442 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400443 break;
Yan7d7d6062007-09-14 16:15:28 -0400444
Yan Zheng11833d62009-09-11 16:11:19 -0400445 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400446 total_found += add_new_free_space(block_group,
447 fs_info, last,
448 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400449 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400450
Yan Zheng11833d62009-09-11 16:11:19 -0400451 if (total_found > (1024 * 1024 * 2)) {
452 total_found = 0;
453 wake_up(&caching_ctl->wait);
454 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400455 }
Chris Masone37c9e62007-05-09 20:13:14 -0400456 path->slots[0]++;
457 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400458 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400459
460 total_found += add_new_free_space(block_group, fs_info, last,
461 block_group->key.objectid +
462 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400463 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400464
465 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400466 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400467 block_group->cached = BTRFS_CACHE_FINISHED;
468 spin_unlock(&block_group->lock);
469
Chris Mason54aa1f42007-06-22 14:16:25 -0400470err:
Chris Masone37c9e62007-05-09 20:13:14 -0400471 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400472 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400473
Yan Zheng11833d62009-09-11 16:11:19 -0400474 free_excluded_extents(extent_root, block_group);
475
476 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400477out:
Yan Zheng11833d62009-09-11 16:11:19 -0400478 wake_up(&caching_ctl->wait);
479
480 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000481 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400482}
483
Josef Bacik9d66e232010-08-25 16:54:15 -0400484static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400485 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400486{
Josef Bacik291c7d22011-11-14 13:52:14 -0500487 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400488 struct btrfs_fs_info *fs_info = cache->fs_info;
489 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400490 int ret = 0;
491
Josef Bacik291c7d22011-11-14 13:52:14 -0500492 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100493 if (!caching_ctl)
494 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500495
496 INIT_LIST_HEAD(&caching_ctl->list);
497 mutex_init(&caching_ctl->mutex);
498 init_waitqueue_head(&caching_ctl->wait);
499 caching_ctl->block_group = cache;
500 caching_ctl->progress = cache->key.objectid;
501 atomic_set(&caching_ctl->count, 1);
502 caching_ctl->work.func = caching_thread;
503
504 spin_lock(&cache->lock);
505 /*
506 * This should be a rare occasion, but this could happen I think in the
507 * case where one thread starts to load the space cache info, and then
508 * some other thread starts a transaction commit which tries to do an
509 * allocation while the other thread is still loading the space cache
510 * info. The previous loop should have kept us from choosing this block
511 * group, but if we've moved to the state where we will wait on caching
512 * block groups we need to first check if we're doing a fast load here,
513 * so we can wait for it to finish, otherwise we could end up allocating
514 * from a block group who's cache gets evicted for one reason or
515 * another.
516 */
517 while (cache->cached == BTRFS_CACHE_FAST) {
518 struct btrfs_caching_control *ctl;
519
520 ctl = cache->caching_ctl;
521 atomic_inc(&ctl->count);
522 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
523 spin_unlock(&cache->lock);
524
525 schedule();
526
527 finish_wait(&ctl->wait, &wait);
528 put_caching_control(ctl);
529 spin_lock(&cache->lock);
530 }
531
532 if (cache->cached != BTRFS_CACHE_NO) {
533 spin_unlock(&cache->lock);
534 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400535 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500536 }
537 WARN_ON(cache->caching_ctl);
538 cache->caching_ctl = caching_ctl;
539 cache->cached = BTRFS_CACHE_FAST;
540 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400541
Josef Bacikd53ba472012-04-12 16:03:57 -0400542 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400543 ret = load_free_space_cache(fs_info, cache);
544
545 spin_lock(&cache->lock);
546 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500547 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400548 cache->cached = BTRFS_CACHE_FINISHED;
549 cache->last_byte_to_unpin = (u64)-1;
550 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500551 if (load_cache_only) {
552 cache->caching_ctl = NULL;
553 cache->cached = BTRFS_CACHE_NO;
554 } else {
555 cache->cached = BTRFS_CACHE_STARTED;
556 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400557 }
558 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500559 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000560 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500561 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000562 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400563 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000564 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500565 } else {
566 /*
567 * We are not going to do the fast caching, set cached to the
568 * appropriate value and wakeup any waiters.
569 */
570 spin_lock(&cache->lock);
571 if (load_cache_only) {
572 cache->caching_ctl = NULL;
573 cache->cached = BTRFS_CACHE_NO;
574 } else {
575 cache->cached = BTRFS_CACHE_STARTED;
576 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400577 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500578 wake_up(&caching_ctl->wait);
579 }
580
581 if (load_cache_only) {
582 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400583 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400584 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400585
Yan Zheng11833d62009-09-11 16:11:19 -0400586 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500587 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400588 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
589 up_write(&fs_info->extent_commit_sem);
590
Josef Bacik11dfe352009-11-13 20:12:59 +0000591 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400592
Josef Bacikbab39bf2011-06-30 14:42:28 -0400593 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400594
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400595 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400596}
597
Josef Bacik0f9dd462008-09-23 13:14:11 -0400598/*
599 * return the block group that starts at or after bytenr
600 */
Chris Masond3977122009-01-05 21:25:51 -0500601static struct btrfs_block_group_cache *
602btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400603{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400604 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400605
Josef Bacik0f9dd462008-09-23 13:14:11 -0400606 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400607
Josef Bacik0f9dd462008-09-23 13:14:11 -0400608 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400609}
610
Josef Bacik0f9dd462008-09-23 13:14:11 -0400611/*
Sankar P9f556842009-05-14 13:52:22 -0400612 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400613 */
Chris Masond3977122009-01-05 21:25:51 -0500614struct btrfs_block_group_cache *btrfs_lookup_block_group(
615 struct btrfs_fs_info *info,
616 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400617{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400618 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400619
Josef Bacik0f9dd462008-09-23 13:14:11 -0400620 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400621
Josef Bacik0f9dd462008-09-23 13:14:11 -0400622 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400623}
Chris Mason0b86a832008-03-24 15:01:56 -0400624
Josef Bacik0f9dd462008-09-23 13:14:11 -0400625static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
626 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400627{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400628 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400629 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400630
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200631 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400632
Chris Mason4184ea72009-03-10 12:39:20 -0400633 rcu_read_lock();
634 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400635 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400636 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400637 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400638 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400639 }
Chris Mason4184ea72009-03-10 12:39:20 -0400640 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400641 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400642}
643
Chris Mason4184ea72009-03-10 12:39:20 -0400644/*
645 * after adding space to the filesystem, we need to clear the full flags
646 * on all the space infos.
647 */
648void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
649{
650 struct list_head *head = &info->space_info;
651 struct btrfs_space_info *found;
652
653 rcu_read_lock();
654 list_for_each_entry_rcu(found, head, list)
655 found->full = 0;
656 rcu_read_unlock();
657}
658
Yan Zhengd2fb3432008-12-11 16:30:39 -0500659u64 btrfs_find_block_group(struct btrfs_root *root,
660 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400661{
Chris Mason96b51792007-10-15 16:15:19 -0400662 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400663 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500664 u64 last = max(search_hint, search_start);
665 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400666 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500667 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400668 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400669again:
Zheng Yane8569812008-09-26 10:05:48 -0400670 while (1) {
671 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400672 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400673 break;
Chris Mason96b51792007-10-15 16:15:19 -0400674
Chris Masonc286ac42008-07-22 23:06:41 -0400675 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400676 last = cache->key.objectid + cache->key.offset;
677 used = btrfs_block_group_used(&cache->item);
678
Yan Zhengd2fb3432008-12-11 16:30:39 -0500679 if ((full_search || !cache->ro) &&
680 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400681 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500682 div_factor(cache->key.offset, factor)) {
683 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400684 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400685 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400686 goto found;
687 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400688 }
Chris Masonc286ac42008-07-22 23:06:41 -0400689 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400690 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400691 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400692 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400693 if (!wrapped) {
694 last = search_start;
695 wrapped = 1;
696 goto again;
697 }
698 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400699 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400700 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400701 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400702 goto again;
703 }
Chris Masonbe744172007-05-06 10:15:01 -0400704found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500705 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400706}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400707
Chris Masone02119d2008-09-05 16:13:11 -0400708/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400709int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400710{
711 int ret;
712 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400713 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400714
Zheng Yan31840ae2008-09-23 13:14:14 -0400715 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700716 if (!path)
717 return -ENOMEM;
718
Chris Masone02119d2008-09-05 16:13:11 -0400719 key.objectid = start;
720 key.offset = len;
721 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
722 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
723 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400724 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500725 return ret;
726}
727
Chris Masond8d5f3e2007-12-11 12:42:00 -0500728/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400729 * helper function to lookup reference count and flags of extent.
730 *
731 * the head node for delayed ref is used to store the sum of all the
732 * reference count modifications queued up in the rbtree. the head
733 * node may also store the extent flags to set. This way you can check
734 * to see what the reference count and extent flags would be if all of
735 * the delayed refs are not processed.
736 */
737int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
738 struct btrfs_root *root, u64 bytenr,
739 u64 num_bytes, u64 *refs, u64 *flags)
740{
741 struct btrfs_delayed_ref_head *head;
742 struct btrfs_delayed_ref_root *delayed_refs;
743 struct btrfs_path *path;
744 struct btrfs_extent_item *ei;
745 struct extent_buffer *leaf;
746 struct btrfs_key key;
747 u32 item_size;
748 u64 num_refs;
749 u64 extent_flags;
750 int ret;
751
752 path = btrfs_alloc_path();
753 if (!path)
754 return -ENOMEM;
755
756 key.objectid = bytenr;
757 key.type = BTRFS_EXTENT_ITEM_KEY;
758 key.offset = num_bytes;
759 if (!trans) {
760 path->skip_locking = 1;
761 path->search_commit_root = 1;
762 }
763again:
764 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
765 &key, path, 0, 0);
766 if (ret < 0)
767 goto out_free;
768
769 if (ret == 0) {
770 leaf = path->nodes[0];
771 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
772 if (item_size >= sizeof(*ei)) {
773 ei = btrfs_item_ptr(leaf, path->slots[0],
774 struct btrfs_extent_item);
775 num_refs = btrfs_extent_refs(leaf, ei);
776 extent_flags = btrfs_extent_flags(leaf, ei);
777 } else {
778#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
779 struct btrfs_extent_item_v0 *ei0;
780 BUG_ON(item_size != sizeof(*ei0));
781 ei0 = btrfs_item_ptr(leaf, path->slots[0],
782 struct btrfs_extent_item_v0);
783 num_refs = btrfs_extent_refs_v0(leaf, ei0);
784 /* FIXME: this isn't correct for data */
785 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
786#else
787 BUG();
788#endif
789 }
790 BUG_ON(num_refs == 0);
791 } else {
792 num_refs = 0;
793 extent_flags = 0;
794 ret = 0;
795 }
796
797 if (!trans)
798 goto out;
799
800 delayed_refs = &trans->transaction->delayed_refs;
801 spin_lock(&delayed_refs->lock);
802 head = btrfs_find_delayed_ref_head(trans, bytenr);
803 if (head) {
804 if (!mutex_trylock(&head->mutex)) {
805 atomic_inc(&head->node.refs);
806 spin_unlock(&delayed_refs->lock);
807
David Sterbab3b4aa72011-04-21 01:20:15 +0200808 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400809
David Sterba8cc33e52011-05-02 15:29:25 +0200810 /*
811 * Mutex was contended, block until it's released and try
812 * again
813 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400814 mutex_lock(&head->mutex);
815 mutex_unlock(&head->mutex);
816 btrfs_put_delayed_ref(&head->node);
817 goto again;
818 }
819 if (head->extent_op && head->extent_op->update_flags)
820 extent_flags |= head->extent_op->flags_to_set;
821 else
822 BUG_ON(num_refs == 0);
823
824 num_refs += head->node.ref_mod;
825 mutex_unlock(&head->mutex);
826 }
827 spin_unlock(&delayed_refs->lock);
828out:
829 WARN_ON(num_refs == 0);
830 if (refs)
831 *refs = num_refs;
832 if (flags)
833 *flags = extent_flags;
834out_free:
835 btrfs_free_path(path);
836 return ret;
837}
838
839/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500840 * Back reference rules. Back refs have three main goals:
841 *
842 * 1) differentiate between all holders of references to an extent so that
843 * when a reference is dropped we can make sure it was a valid reference
844 * before freeing the extent.
845 *
846 * 2) Provide enough information to quickly find the holders of an extent
847 * if we notice a given block is corrupted or bad.
848 *
849 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
850 * maintenance. This is actually the same as #2, but with a slightly
851 * different use case.
852 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400853 * There are two kinds of back refs. The implicit back refs is optimized
854 * for pointers in non-shared tree blocks. For a given pointer in a block,
855 * back refs of this kind provide information about the block's owner tree
856 * and the pointer's key. These information allow us to find the block by
857 * b-tree searching. The full back refs is for pointers in tree blocks not
858 * referenced by their owner trees. The location of tree block is recorded
859 * in the back refs. Actually the full back refs is generic, and can be
860 * used in all cases the implicit back refs is used. The major shortcoming
861 * of the full back refs is its overhead. Every time a tree block gets
862 * COWed, we have to update back refs entry for all pointers in it.
863 *
864 * For a newly allocated tree block, we use implicit back refs for
865 * pointers in it. This means most tree related operations only involve
866 * implicit back refs. For a tree block created in old transaction, the
867 * only way to drop a reference to it is COW it. So we can detect the
868 * event that tree block loses its owner tree's reference and do the
869 * back refs conversion.
870 *
871 * When a tree block is COW'd through a tree, there are four cases:
872 *
873 * The reference count of the block is one and the tree is the block's
874 * owner tree. Nothing to do in this case.
875 *
876 * The reference count of the block is one and the tree is not the
877 * block's owner tree. In this case, full back refs is used for pointers
878 * in the block. Remove these full back refs, add implicit back refs for
879 * every pointers in the new block.
880 *
881 * The reference count of the block is greater than one and the tree is
882 * the block's owner tree. In this case, implicit back refs is used for
883 * pointers in the block. Add full back refs for every pointers in the
884 * block, increase lower level extents' reference counts. The original
885 * implicit back refs are entailed to the new block.
886 *
887 * The reference count of the block is greater than one and the tree is
888 * not the block's owner tree. Add implicit back refs for every pointer in
889 * the new block, increase lower level extents' reference count.
890 *
891 * Back Reference Key composing:
892 *
893 * The key objectid corresponds to the first byte in the extent,
894 * The key type is used to differentiate between types of back refs.
895 * There are different meanings of the key offset for different types
896 * of back refs.
897 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500898 * File extents can be referenced by:
899 *
900 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400901 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500902 * - different offsets inside a file (bookend extents in file.c)
903 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400904 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500905 *
906 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500907 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400908 * - original offset in the file
909 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400910 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911 * The key offset for the implicit back refs is hash of the first
912 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500913 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400914 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500915 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400916 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500917 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 * The key offset for the implicit back refs is the first byte of
919 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500920 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400921 * When a file extent is allocated, The implicit back refs is used.
922 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500923 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400924 * (root_key.objectid, inode objectid, offset in file, 1)
925 *
926 * When a file extent is removed file truncation, we find the
927 * corresponding implicit back refs and check the following fields:
928 *
929 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500930 *
931 * Btree extents can be referenced by:
932 *
933 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500934 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400935 * Both the implicit back refs and the full back refs for tree blocks
936 * only consist of key. The key offset for the implicit back refs is
937 * objectid of block's owner tree. The key offset for the full back refs
938 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500939 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400940 * When implicit back refs is used, information about the lowest key and
941 * level of the tree block are required. These information are stored in
942 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500943 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400944
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
946static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
947 struct btrfs_root *root,
948 struct btrfs_path *path,
949 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500950{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400951 struct btrfs_extent_item *item;
952 struct btrfs_extent_item_v0 *ei0;
953 struct btrfs_extent_ref_v0 *ref0;
954 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400955 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 struct btrfs_key key;
957 struct btrfs_key found_key;
958 u32 new_size = sizeof(*item);
959 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500960 int ret;
961
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400962 leaf = path->nodes[0];
963 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500964
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400965 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
966 ei0 = btrfs_item_ptr(leaf, path->slots[0],
967 struct btrfs_extent_item_v0);
968 refs = btrfs_extent_refs_v0(leaf, ei0);
969
970 if (owner == (u64)-1) {
971 while (1) {
972 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
973 ret = btrfs_next_leaf(root, path);
974 if (ret < 0)
975 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100976 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400977 leaf = path->nodes[0];
978 }
979 btrfs_item_key_to_cpu(leaf, &found_key,
980 path->slots[0]);
981 BUG_ON(key.objectid != found_key.objectid);
982 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
983 path->slots[0]++;
984 continue;
985 }
986 ref0 = btrfs_item_ptr(leaf, path->slots[0],
987 struct btrfs_extent_ref_v0);
988 owner = btrfs_ref_objectid_v0(leaf, ref0);
989 break;
990 }
991 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200992 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400993
994 if (owner < BTRFS_FIRST_FREE_OBJECTID)
995 new_size += sizeof(*bi);
996
997 new_size -= sizeof(*ei0);
998 ret = btrfs_search_slot(trans, root, &key, path,
999 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001000 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001001 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001002 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001003
Jeff Mahoney143bede2012-03-01 14:56:26 +01001004 btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001005
1006 leaf = path->nodes[0];
1007 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1008 btrfs_set_extent_refs(leaf, item, refs);
1009 /* FIXME: get real generation */
1010 btrfs_set_extent_generation(leaf, item, 0);
1011 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1012 btrfs_set_extent_flags(leaf, item,
1013 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1014 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1015 bi = (struct btrfs_tree_block_info *)(item + 1);
1016 /* FIXME: get first key of the block */
1017 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1018 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1019 } else {
1020 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1021 }
1022 btrfs_mark_buffer_dirty(leaf);
1023 return 0;
1024}
1025#endif
1026
1027static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1028{
1029 u32 high_crc = ~(u32)0;
1030 u32 low_crc = ~(u32)0;
1031 __le64 lenum;
1032
1033 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001034 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001035 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001036 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001038 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001039
1040 return ((u64)high_crc << 31) ^ (u64)low_crc;
1041}
1042
1043static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1044 struct btrfs_extent_data_ref *ref)
1045{
1046 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1047 btrfs_extent_data_ref_objectid(leaf, ref),
1048 btrfs_extent_data_ref_offset(leaf, ref));
1049}
1050
1051static int match_extent_data_ref(struct extent_buffer *leaf,
1052 struct btrfs_extent_data_ref *ref,
1053 u64 root_objectid, u64 owner, u64 offset)
1054{
1055 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1056 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1057 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1058 return 0;
1059 return 1;
1060}
1061
1062static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1063 struct btrfs_root *root,
1064 struct btrfs_path *path,
1065 u64 bytenr, u64 parent,
1066 u64 root_objectid,
1067 u64 owner, u64 offset)
1068{
1069 struct btrfs_key key;
1070 struct btrfs_extent_data_ref *ref;
1071 struct extent_buffer *leaf;
1072 u32 nritems;
1073 int ret;
1074 int recow;
1075 int err = -ENOENT;
1076
1077 key.objectid = bytenr;
1078 if (parent) {
1079 key.type = BTRFS_SHARED_DATA_REF_KEY;
1080 key.offset = parent;
1081 } else {
1082 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1083 key.offset = hash_extent_data_ref(root_objectid,
1084 owner, offset);
1085 }
1086again:
1087 recow = 0;
1088 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1089 if (ret < 0) {
1090 err = ret;
1091 goto fail;
1092 }
1093
1094 if (parent) {
1095 if (!ret)
1096 return 0;
1097#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1098 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001099 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001100 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1101 if (ret < 0) {
1102 err = ret;
1103 goto fail;
1104 }
1105 if (!ret)
1106 return 0;
1107#endif
1108 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001109 }
1110
1111 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001112 nritems = btrfs_header_nritems(leaf);
1113 while (1) {
1114 if (path->slots[0] >= nritems) {
1115 ret = btrfs_next_leaf(root, path);
1116 if (ret < 0)
1117 err = ret;
1118 if (ret)
1119 goto fail;
1120
1121 leaf = path->nodes[0];
1122 nritems = btrfs_header_nritems(leaf);
1123 recow = 1;
1124 }
1125
1126 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1127 if (key.objectid != bytenr ||
1128 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1129 goto fail;
1130
1131 ref = btrfs_item_ptr(leaf, path->slots[0],
1132 struct btrfs_extent_data_ref);
1133
1134 if (match_extent_data_ref(leaf, ref, root_objectid,
1135 owner, offset)) {
1136 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001137 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001138 goto again;
1139 }
1140 err = 0;
1141 break;
1142 }
1143 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001144 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001145fail:
1146 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001147}
1148
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001149static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1150 struct btrfs_root *root,
1151 struct btrfs_path *path,
1152 u64 bytenr, u64 parent,
1153 u64 root_objectid, u64 owner,
1154 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001155{
1156 struct btrfs_key key;
1157 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001158 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001159 u32 num_refs;
1160 int ret;
1161
1162 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001163 if (parent) {
1164 key.type = BTRFS_SHARED_DATA_REF_KEY;
1165 key.offset = parent;
1166 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001167 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001168 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1169 key.offset = hash_extent_data_ref(root_objectid,
1170 owner, offset);
1171 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001172 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001173
1174 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1175 if (ret && ret != -EEXIST)
1176 goto fail;
1177
1178 leaf = path->nodes[0];
1179 if (parent) {
1180 struct btrfs_shared_data_ref *ref;
1181 ref = btrfs_item_ptr(leaf, path->slots[0],
1182 struct btrfs_shared_data_ref);
1183 if (ret == 0) {
1184 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1185 } else {
1186 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1187 num_refs += refs_to_add;
1188 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1189 }
1190 } else {
1191 struct btrfs_extent_data_ref *ref;
1192 while (ret == -EEXIST) {
1193 ref = btrfs_item_ptr(leaf, path->slots[0],
1194 struct btrfs_extent_data_ref);
1195 if (match_extent_data_ref(leaf, ref, root_objectid,
1196 owner, offset))
1197 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001198 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001199 key.offset++;
1200 ret = btrfs_insert_empty_item(trans, root, path, &key,
1201 size);
1202 if (ret && ret != -EEXIST)
1203 goto fail;
1204
1205 leaf = path->nodes[0];
1206 }
1207 ref = btrfs_item_ptr(leaf, path->slots[0],
1208 struct btrfs_extent_data_ref);
1209 if (ret == 0) {
1210 btrfs_set_extent_data_ref_root(leaf, ref,
1211 root_objectid);
1212 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1213 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1214 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1215 } else {
1216 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1217 num_refs += refs_to_add;
1218 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1219 }
1220 }
1221 btrfs_mark_buffer_dirty(leaf);
1222 ret = 0;
1223fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001224 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001225 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001226}
1227
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001228static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1229 struct btrfs_root *root,
1230 struct btrfs_path *path,
1231 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001232{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001233 struct btrfs_key key;
1234 struct btrfs_extent_data_ref *ref1 = NULL;
1235 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001236 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001237 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001238 int ret = 0;
1239
1240 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001241 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1242
1243 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1244 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1245 struct btrfs_extent_data_ref);
1246 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1247 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1248 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1249 struct btrfs_shared_data_ref);
1250 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1251#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1252 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1253 struct btrfs_extent_ref_v0 *ref0;
1254 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1255 struct btrfs_extent_ref_v0);
1256 num_refs = btrfs_ref_count_v0(leaf, ref0);
1257#endif
1258 } else {
1259 BUG();
1260 }
1261
Chris Mason56bec292009-03-13 10:10:06 -04001262 BUG_ON(num_refs < refs_to_drop);
1263 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001264
Zheng Yan31840ae2008-09-23 13:14:14 -04001265 if (num_refs == 0) {
1266 ret = btrfs_del_item(trans, root, path);
1267 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1269 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1270 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1271 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1272#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1273 else {
1274 struct btrfs_extent_ref_v0 *ref0;
1275 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1276 struct btrfs_extent_ref_v0);
1277 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1278 }
1279#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001280 btrfs_mark_buffer_dirty(leaf);
1281 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001282 return ret;
1283}
1284
1285static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1286 struct btrfs_path *path,
1287 struct btrfs_extent_inline_ref *iref)
1288{
1289 struct btrfs_key key;
1290 struct extent_buffer *leaf;
1291 struct btrfs_extent_data_ref *ref1;
1292 struct btrfs_shared_data_ref *ref2;
1293 u32 num_refs = 0;
1294
1295 leaf = path->nodes[0];
1296 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1297 if (iref) {
1298 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1299 BTRFS_EXTENT_DATA_REF_KEY) {
1300 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1301 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1302 } else {
1303 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1304 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1305 }
1306 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1307 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1308 struct btrfs_extent_data_ref);
1309 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1310 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1311 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1312 struct btrfs_shared_data_ref);
1313 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1314#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1315 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1316 struct btrfs_extent_ref_v0 *ref0;
1317 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1318 struct btrfs_extent_ref_v0);
1319 num_refs = btrfs_ref_count_v0(leaf, ref0);
1320#endif
1321 } else {
1322 WARN_ON(1);
1323 }
1324 return num_refs;
1325}
1326
1327static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1328 struct btrfs_root *root,
1329 struct btrfs_path *path,
1330 u64 bytenr, u64 parent,
1331 u64 root_objectid)
1332{
1333 struct btrfs_key key;
1334 int ret;
1335
1336 key.objectid = bytenr;
1337 if (parent) {
1338 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1339 key.offset = parent;
1340 } else {
1341 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1342 key.offset = root_objectid;
1343 }
1344
1345 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1346 if (ret > 0)
1347 ret = -ENOENT;
1348#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1349 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001350 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001351 key.type = BTRFS_EXTENT_REF_V0_KEY;
1352 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1353 if (ret > 0)
1354 ret = -ENOENT;
1355 }
1356#endif
1357 return ret;
1358}
1359
1360static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1361 struct btrfs_root *root,
1362 struct btrfs_path *path,
1363 u64 bytenr, u64 parent,
1364 u64 root_objectid)
1365{
1366 struct btrfs_key key;
1367 int ret;
1368
1369 key.objectid = bytenr;
1370 if (parent) {
1371 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1372 key.offset = parent;
1373 } else {
1374 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1375 key.offset = root_objectid;
1376 }
1377
1378 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001379 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001380 return ret;
1381}
1382
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001383static inline int extent_ref_type(u64 parent, u64 owner)
1384{
1385 int type;
1386 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1387 if (parent > 0)
1388 type = BTRFS_SHARED_BLOCK_REF_KEY;
1389 else
1390 type = BTRFS_TREE_BLOCK_REF_KEY;
1391 } else {
1392 if (parent > 0)
1393 type = BTRFS_SHARED_DATA_REF_KEY;
1394 else
1395 type = BTRFS_EXTENT_DATA_REF_KEY;
1396 }
1397 return type;
1398}
1399
Yan Zheng2c47e6052009-06-27 21:07:35 -04001400static int find_next_key(struct btrfs_path *path, int level,
1401 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001402
1403{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001404 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001405 if (!path->nodes[level])
1406 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001407 if (path->slots[level] + 1 >=
1408 btrfs_header_nritems(path->nodes[level]))
1409 continue;
1410 if (level == 0)
1411 btrfs_item_key_to_cpu(path->nodes[level], key,
1412 path->slots[level] + 1);
1413 else
1414 btrfs_node_key_to_cpu(path->nodes[level], key,
1415 path->slots[level] + 1);
1416 return 0;
1417 }
1418 return 1;
1419}
1420
1421/*
1422 * look for inline back ref. if back ref is found, *ref_ret is set
1423 * to the address of inline back ref, and 0 is returned.
1424 *
1425 * if back ref isn't found, *ref_ret is set to the address where it
1426 * should be inserted, and -ENOENT is returned.
1427 *
1428 * if insert is true and there are too many inline back refs, the path
1429 * points to the extent item, and -EAGAIN is returned.
1430 *
1431 * NOTE: inline back refs are ordered in the same way that back ref
1432 * items in the tree are ordered.
1433 */
1434static noinline_for_stack
1435int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1436 struct btrfs_root *root,
1437 struct btrfs_path *path,
1438 struct btrfs_extent_inline_ref **ref_ret,
1439 u64 bytenr, u64 num_bytes,
1440 u64 parent, u64 root_objectid,
1441 u64 owner, u64 offset, int insert)
1442{
1443 struct btrfs_key key;
1444 struct extent_buffer *leaf;
1445 struct btrfs_extent_item *ei;
1446 struct btrfs_extent_inline_ref *iref;
1447 u64 flags;
1448 u64 item_size;
1449 unsigned long ptr;
1450 unsigned long end;
1451 int extra_size;
1452 int type;
1453 int want;
1454 int ret;
1455 int err = 0;
1456
1457 key.objectid = bytenr;
1458 key.type = BTRFS_EXTENT_ITEM_KEY;
1459 key.offset = num_bytes;
1460
1461 want = extent_ref_type(parent, owner);
1462 if (insert) {
1463 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001464 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001465 } else
1466 extra_size = -1;
1467 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1468 if (ret < 0) {
1469 err = ret;
1470 goto out;
1471 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001472 if (ret && !insert) {
1473 err = -ENOENT;
1474 goto out;
Josef Bacik492104c2013-03-08 15:41:02 -05001475 } else if (ret) {
1476 err = -EIO;
1477 WARN_ON(1);
1478 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001479 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001480
1481 leaf = path->nodes[0];
1482 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1483#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1484 if (item_size < sizeof(*ei)) {
1485 if (!insert) {
1486 err = -ENOENT;
1487 goto out;
1488 }
1489 ret = convert_extent_item_v0(trans, root, path, owner,
1490 extra_size);
1491 if (ret < 0) {
1492 err = ret;
1493 goto out;
1494 }
1495 leaf = path->nodes[0];
1496 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1497 }
1498#endif
1499 BUG_ON(item_size < sizeof(*ei));
1500
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001501 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1502 flags = btrfs_extent_flags(leaf, ei);
1503
1504 ptr = (unsigned long)(ei + 1);
1505 end = (unsigned long)ei + item_size;
1506
1507 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1508 ptr += sizeof(struct btrfs_tree_block_info);
1509 BUG_ON(ptr > end);
1510 } else {
1511 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1512 }
1513
1514 err = -ENOENT;
1515 while (1) {
1516 if (ptr >= end) {
1517 WARN_ON(ptr > end);
1518 break;
1519 }
1520 iref = (struct btrfs_extent_inline_ref *)ptr;
1521 type = btrfs_extent_inline_ref_type(leaf, iref);
1522 if (want < type)
1523 break;
1524 if (want > type) {
1525 ptr += btrfs_extent_inline_ref_size(type);
1526 continue;
1527 }
1528
1529 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1530 struct btrfs_extent_data_ref *dref;
1531 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1532 if (match_extent_data_ref(leaf, dref, root_objectid,
1533 owner, offset)) {
1534 err = 0;
1535 break;
1536 }
1537 if (hash_extent_data_ref_item(leaf, dref) <
1538 hash_extent_data_ref(root_objectid, owner, offset))
1539 break;
1540 } else {
1541 u64 ref_offset;
1542 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1543 if (parent > 0) {
1544 if (parent == ref_offset) {
1545 err = 0;
1546 break;
1547 }
1548 if (ref_offset < parent)
1549 break;
1550 } else {
1551 if (root_objectid == ref_offset) {
1552 err = 0;
1553 break;
1554 }
1555 if (ref_offset < root_objectid)
1556 break;
1557 }
1558 }
1559 ptr += btrfs_extent_inline_ref_size(type);
1560 }
1561 if (err == -ENOENT && insert) {
1562 if (item_size + extra_size >=
1563 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1564 err = -EAGAIN;
1565 goto out;
1566 }
1567 /*
1568 * To add new inline back ref, we have to make sure
1569 * there is no corresponding back ref item.
1570 * For simplicity, we just do not add new inline back
1571 * ref if there is any kind of item for this block
1572 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001573 if (find_next_key(path, 0, &key) == 0 &&
1574 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001575 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001576 err = -EAGAIN;
1577 goto out;
1578 }
1579 }
1580 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1581out:
Yan Zheng85d41982009-06-11 08:51:10 -04001582 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001583 path->keep_locks = 0;
1584 btrfs_unlock_up_safe(path, 1);
1585 }
1586 return err;
1587}
1588
1589/*
1590 * helper to add new inline back ref
1591 */
1592static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001593void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1594 struct btrfs_root *root,
1595 struct btrfs_path *path,
1596 struct btrfs_extent_inline_ref *iref,
1597 u64 parent, u64 root_objectid,
1598 u64 owner, u64 offset, int refs_to_add,
1599 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001600{
1601 struct extent_buffer *leaf;
1602 struct btrfs_extent_item *ei;
1603 unsigned long ptr;
1604 unsigned long end;
1605 unsigned long item_offset;
1606 u64 refs;
1607 int size;
1608 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001609
1610 leaf = path->nodes[0];
1611 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1612 item_offset = (unsigned long)iref - (unsigned long)ei;
1613
1614 type = extent_ref_type(parent, owner);
1615 size = btrfs_extent_inline_ref_size(type);
1616
Jeff Mahoney143bede2012-03-01 14:56:26 +01001617 btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001618
1619 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1620 refs = btrfs_extent_refs(leaf, ei);
1621 refs += refs_to_add;
1622 btrfs_set_extent_refs(leaf, ei, refs);
1623 if (extent_op)
1624 __run_delayed_extent_op(extent_op, leaf, ei);
1625
1626 ptr = (unsigned long)ei + item_offset;
1627 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1628 if (ptr < end - size)
1629 memmove_extent_buffer(leaf, ptr + size, ptr,
1630 end - size - ptr);
1631
1632 iref = (struct btrfs_extent_inline_ref *)ptr;
1633 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1634 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1635 struct btrfs_extent_data_ref *dref;
1636 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1637 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1638 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1639 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1640 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1641 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1642 struct btrfs_shared_data_ref *sref;
1643 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1644 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1645 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1646 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1647 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1648 } else {
1649 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1650 }
1651 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001652}
1653
1654static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1655 struct btrfs_root *root,
1656 struct btrfs_path *path,
1657 struct btrfs_extent_inline_ref **ref_ret,
1658 u64 bytenr, u64 num_bytes, u64 parent,
1659 u64 root_objectid, u64 owner, u64 offset)
1660{
1661 int ret;
1662
1663 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1664 bytenr, num_bytes, parent,
1665 root_objectid, owner, offset, 0);
1666 if (ret != -ENOENT)
1667 return ret;
1668
David Sterbab3b4aa72011-04-21 01:20:15 +02001669 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001670 *ref_ret = NULL;
1671
1672 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1673 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1674 root_objectid);
1675 } else {
1676 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1677 root_objectid, owner, offset);
1678 }
1679 return ret;
1680}
1681
1682/*
1683 * helper to update/remove inline back ref
1684 */
1685static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001686void update_inline_extent_backref(struct btrfs_trans_handle *trans,
1687 struct btrfs_root *root,
1688 struct btrfs_path *path,
1689 struct btrfs_extent_inline_ref *iref,
1690 int refs_to_mod,
1691 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001692{
1693 struct extent_buffer *leaf;
1694 struct btrfs_extent_item *ei;
1695 struct btrfs_extent_data_ref *dref = NULL;
1696 struct btrfs_shared_data_ref *sref = NULL;
1697 unsigned long ptr;
1698 unsigned long end;
1699 u32 item_size;
1700 int size;
1701 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001702 u64 refs;
1703
1704 leaf = path->nodes[0];
1705 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1706 refs = btrfs_extent_refs(leaf, ei);
1707 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1708 refs += refs_to_mod;
1709 btrfs_set_extent_refs(leaf, ei, refs);
1710 if (extent_op)
1711 __run_delayed_extent_op(extent_op, leaf, ei);
1712
1713 type = btrfs_extent_inline_ref_type(leaf, iref);
1714
1715 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1716 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1717 refs = btrfs_extent_data_ref_count(leaf, dref);
1718 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1719 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1720 refs = btrfs_shared_data_ref_count(leaf, sref);
1721 } else {
1722 refs = 1;
1723 BUG_ON(refs_to_mod != -1);
1724 }
1725
1726 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1727 refs += refs_to_mod;
1728
1729 if (refs > 0) {
1730 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1731 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1732 else
1733 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1734 } else {
1735 size = btrfs_extent_inline_ref_size(type);
1736 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1737 ptr = (unsigned long)iref;
1738 end = (unsigned long)ei + item_size;
1739 if (ptr + size < end)
1740 memmove_extent_buffer(leaf, ptr, ptr + size,
1741 end - ptr - size);
1742 item_size -= size;
Jeff Mahoney143bede2012-03-01 14:56:26 +01001743 btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001744 }
1745 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001746}
1747
1748static noinline_for_stack
1749int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1750 struct btrfs_root *root,
1751 struct btrfs_path *path,
1752 u64 bytenr, u64 num_bytes, u64 parent,
1753 u64 root_objectid, u64 owner,
1754 u64 offset, int refs_to_add,
1755 struct btrfs_delayed_extent_op *extent_op)
1756{
1757 struct btrfs_extent_inline_ref *iref;
1758 int ret;
1759
1760 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1761 bytenr, num_bytes, parent,
1762 root_objectid, owner, offset, 1);
1763 if (ret == 0) {
1764 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001765 update_inline_extent_backref(trans, root, path, iref,
1766 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001767 } else if (ret == -ENOENT) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001768 setup_inline_extent_backref(trans, root, path, iref, parent,
1769 root_objectid, owner, offset,
1770 refs_to_add, extent_op);
1771 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001772 }
1773 return ret;
1774}
1775
1776static int insert_extent_backref(struct btrfs_trans_handle *trans,
1777 struct btrfs_root *root,
1778 struct btrfs_path *path,
1779 u64 bytenr, u64 parent, u64 root_objectid,
1780 u64 owner, u64 offset, int refs_to_add)
1781{
1782 int ret;
1783 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1784 BUG_ON(refs_to_add != 1);
1785 ret = insert_tree_block_ref(trans, root, path, bytenr,
1786 parent, root_objectid);
1787 } else {
1788 ret = insert_extent_data_ref(trans, root, path, bytenr,
1789 parent, root_objectid,
1790 owner, offset, refs_to_add);
1791 }
1792 return ret;
1793}
1794
1795static int remove_extent_backref(struct btrfs_trans_handle *trans,
1796 struct btrfs_root *root,
1797 struct btrfs_path *path,
1798 struct btrfs_extent_inline_ref *iref,
1799 int refs_to_drop, int is_data)
1800{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001801 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001802
1803 BUG_ON(!is_data && refs_to_drop != 1);
1804 if (iref) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001805 update_inline_extent_backref(trans, root, path, iref,
1806 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001807 } else if (is_data) {
1808 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1809 } else {
1810 ret = btrfs_del_item(trans, root, path);
1811 }
1812 return ret;
1813}
1814
Li Dongyang5378e602011-03-24 10:24:27 +00001815static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001816 u64 start, u64 len)
1817{
Li Dongyang5378e602011-03-24 10:24:27 +00001818 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001819}
Chris Mason15916de2008-11-19 21:17:22 -05001820
Liu Hui1f3c79a2009-01-05 15:57:51 -05001821static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001822 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001823{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001824 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001825 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001826 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001827
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001828
Liu Hui1f3c79a2009-01-05 15:57:51 -05001829 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001830 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001831 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001832 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001833 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001834 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001835 int i;
1836
Liu Hui1f3c79a2009-01-05 15:57:51 -05001837
Jan Schmidta1d3c472011-08-04 17:15:33 +02001838 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001839 if (!stripe->dev->can_discard)
1840 continue;
1841
Li Dongyang5378e602011-03-24 10:24:27 +00001842 ret = btrfs_issue_discard(stripe->dev->bdev,
1843 stripe->physical,
1844 stripe->length);
1845 if (!ret)
1846 discarded_bytes += stripe->length;
1847 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001848 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001849
1850 /*
1851 * Just in case we get back EOPNOTSUPP for some reason,
1852 * just ignore the return value so we don't screw up
1853 * people calling discard_extent.
1854 */
1855 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001856 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001857 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001858 }
Li Dongyang5378e602011-03-24 10:24:27 +00001859
1860 if (actual_bytes)
1861 *actual_bytes = discarded_bytes;
1862
Liu Hui1f3c79a2009-01-05 15:57:51 -05001863
David Woodhouse53b381b2013-01-29 18:40:14 -05001864 if (ret == -EOPNOTSUPP)
1865 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001866 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001867}
1868
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001869/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1871 struct btrfs_root *root,
1872 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001873 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001874{
1875 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001876 struct btrfs_fs_info *fs_info = root->fs_info;
1877
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1879 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001880
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001881 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001882 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1883 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001884 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001885 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001886 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001887 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1888 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001889 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001890 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001891 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001892 return ret;
1893}
1894
Chris Mason925baed2008-06-25 16:01:30 -04001895static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001896 struct btrfs_root *root,
1897 u64 bytenr, u64 num_bytes,
1898 u64 parent, u64 root_objectid,
1899 u64 owner, u64 offset, int refs_to_add,
1900 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001901{
Chris Mason5caf2a02007-04-02 11:20:42 -04001902 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001903 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001904 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001905 u64 refs;
1906 int ret;
1907 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001908
Chris Mason5caf2a02007-04-02 11:20:42 -04001909 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001910 if (!path)
1911 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001912
Chris Mason3c12ac72008-04-21 12:01:38 -04001913 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001914 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001915 /* this will setup the path even if it fails to insert the back ref */
1916 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1917 path, bytenr, num_bytes, parent,
1918 root_objectid, owner, offset,
1919 refs_to_add, extent_op);
1920 if (ret == 0)
1921 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001922
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001923 if (ret != -EAGAIN) {
1924 err = ret;
1925 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001926 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001927
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001928 leaf = path->nodes[0];
1929 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1930 refs = btrfs_extent_refs(leaf, item);
1931 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1932 if (extent_op)
1933 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001934
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001935 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001936 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001937
Chris Mason3c12ac72008-04-21 12:01:38 -04001938 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001939 path->leave_spinning = 1;
1940
Chris Mason56bec292009-03-13 10:10:06 -04001941 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001942 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001943 path, bytenr, parent, root_objectid,
1944 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001945 if (ret)
1946 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001947out:
Chris Mason74493f72007-12-11 09:25:06 -05001948 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001949 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001950}
1951
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001952static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1953 struct btrfs_root *root,
1954 struct btrfs_delayed_ref_node *node,
1955 struct btrfs_delayed_extent_op *extent_op,
1956 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001957{
Chris Mason56bec292009-03-13 10:10:06 -04001958 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001959 struct btrfs_delayed_data_ref *ref;
1960 struct btrfs_key ins;
1961 u64 parent = 0;
1962 u64 ref_root = 0;
1963 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001964
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001965 ins.objectid = node->bytenr;
1966 ins.offset = node->num_bytes;
1967 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001968
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001969 ref = btrfs_delayed_node_to_data_ref(node);
1970 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1971 parent = ref->parent;
1972 else
1973 ref_root = ref->root;
1974
1975 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1976 if (extent_op) {
1977 BUG_ON(extent_op->update_key);
1978 flags |= extent_op->flags_to_set;
1979 }
1980 ret = alloc_reserved_file_extent(trans, root,
1981 parent, ref_root, flags,
1982 ref->objectid, ref->offset,
1983 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001984 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1985 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1986 node->num_bytes, parent,
1987 ref_root, ref->objectid,
1988 ref->offset, node->ref_mod,
1989 extent_op);
1990 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1991 ret = __btrfs_free_extent(trans, root, node->bytenr,
1992 node->num_bytes, parent,
1993 ref_root, ref->objectid,
1994 ref->offset, node->ref_mod,
1995 extent_op);
1996 } else {
1997 BUG();
1998 }
Chris Mason56bec292009-03-13 10:10:06 -04001999 return ret;
2000}
2001
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002002static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2003 struct extent_buffer *leaf,
2004 struct btrfs_extent_item *ei)
2005{
2006 u64 flags = btrfs_extent_flags(leaf, ei);
2007 if (extent_op->update_flags) {
2008 flags |= extent_op->flags_to_set;
2009 btrfs_set_extent_flags(leaf, ei, flags);
2010 }
2011
2012 if (extent_op->update_key) {
2013 struct btrfs_tree_block_info *bi;
2014 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2015 bi = (struct btrfs_tree_block_info *)(ei + 1);
2016 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2017 }
2018}
2019
2020static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2021 struct btrfs_root *root,
2022 struct btrfs_delayed_ref_node *node,
2023 struct btrfs_delayed_extent_op *extent_op)
2024{
2025 struct btrfs_key key;
2026 struct btrfs_path *path;
2027 struct btrfs_extent_item *ei;
2028 struct extent_buffer *leaf;
2029 u32 item_size;
2030 int ret;
2031 int err = 0;
2032
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002033 if (trans->aborted)
2034 return 0;
2035
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002036 path = btrfs_alloc_path();
2037 if (!path)
2038 return -ENOMEM;
2039
2040 key.objectid = node->bytenr;
2041 key.type = BTRFS_EXTENT_ITEM_KEY;
2042 key.offset = node->num_bytes;
2043
2044 path->reada = 1;
2045 path->leave_spinning = 1;
2046 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2047 path, 0, 1);
2048 if (ret < 0) {
2049 err = ret;
2050 goto out;
2051 }
2052 if (ret > 0) {
2053 err = -EIO;
2054 goto out;
2055 }
2056
2057 leaf = path->nodes[0];
2058 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2059#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2060 if (item_size < sizeof(*ei)) {
2061 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2062 path, (u64)-1, 0);
2063 if (ret < 0) {
2064 err = ret;
2065 goto out;
2066 }
2067 leaf = path->nodes[0];
2068 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2069 }
2070#endif
2071 BUG_ON(item_size < sizeof(*ei));
2072 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2073 __run_delayed_extent_op(extent_op, leaf, ei);
2074
2075 btrfs_mark_buffer_dirty(leaf);
2076out:
2077 btrfs_free_path(path);
2078 return err;
2079}
2080
2081static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2082 struct btrfs_root *root,
2083 struct btrfs_delayed_ref_node *node,
2084 struct btrfs_delayed_extent_op *extent_op,
2085 int insert_reserved)
2086{
2087 int ret = 0;
2088 struct btrfs_delayed_tree_ref *ref;
2089 struct btrfs_key ins;
2090 u64 parent = 0;
2091 u64 ref_root = 0;
2092
2093 ins.objectid = node->bytenr;
2094 ins.offset = node->num_bytes;
2095 ins.type = BTRFS_EXTENT_ITEM_KEY;
2096
2097 ref = btrfs_delayed_node_to_tree_ref(node);
2098 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2099 parent = ref->parent;
2100 else
2101 ref_root = ref->root;
2102
2103 BUG_ON(node->ref_mod != 1);
2104 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2105 BUG_ON(!extent_op || !extent_op->update_flags ||
2106 !extent_op->update_key);
2107 ret = alloc_reserved_tree_block(trans, root,
2108 parent, ref_root,
2109 extent_op->flags_to_set,
2110 &extent_op->key,
2111 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002112 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2113 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2114 node->num_bytes, parent, ref_root,
2115 ref->level, 0, 1, extent_op);
2116 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2117 ret = __btrfs_free_extent(trans, root, node->bytenr,
2118 node->num_bytes, parent, ref_root,
2119 ref->level, 0, 1, extent_op);
2120 } else {
2121 BUG();
2122 }
2123 return ret;
2124}
2125
Chris Mason56bec292009-03-13 10:10:06 -04002126/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002127static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2128 struct btrfs_root *root,
2129 struct btrfs_delayed_ref_node *node,
2130 struct btrfs_delayed_extent_op *extent_op,
2131 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002132{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002133 int ret = 0;
2134
2135 if (trans->aborted)
2136 return 0;
2137
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002138 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002139 struct btrfs_delayed_ref_head *head;
2140 /*
2141 * we've hit the end of the chain and we were supposed
2142 * to insert this extent into the tree. But, it got
2143 * deleted before we ever needed to insert it, so all
2144 * we have to do is clean up the accounting
2145 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002146 BUG_ON(extent_op);
2147 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002148 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002149 btrfs_pin_extent(root, node->bytenr,
2150 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002151 if (head->is_data) {
2152 ret = btrfs_del_csums(trans, root,
2153 node->bytenr,
2154 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002155 }
Chris Mason56bec292009-03-13 10:10:06 -04002156 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002157 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002158 }
Josef Bacikeb099672009-02-12 09:27:38 -05002159
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002160 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2161 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2162 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2163 insert_reserved);
2164 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2165 node->type == BTRFS_SHARED_DATA_REF_KEY)
2166 ret = run_delayed_data_ref(trans, root, node, extent_op,
2167 insert_reserved);
2168 else
2169 BUG();
2170 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002171}
2172
Chris Mason56bec292009-03-13 10:10:06 -04002173static noinline struct btrfs_delayed_ref_node *
2174select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002175{
Chris Mason56bec292009-03-13 10:10:06 -04002176 struct rb_node *node;
2177 struct btrfs_delayed_ref_node *ref;
2178 int action = BTRFS_ADD_DELAYED_REF;
2179again:
2180 /*
2181 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2182 * this prevents ref count from going down to zero when
2183 * there still are pending delayed ref.
2184 */
2185 node = rb_prev(&head->node.rb_node);
2186 while (1) {
2187 if (!node)
2188 break;
2189 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2190 rb_node);
2191 if (ref->bytenr != head->node.bytenr)
2192 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002193 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002194 return ref;
2195 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002196 }
Chris Mason56bec292009-03-13 10:10:06 -04002197 if (action == BTRFS_ADD_DELAYED_REF) {
2198 action = BTRFS_DROP_DELAYED_REF;
2199 goto again;
2200 }
2201 return NULL;
2202}
2203
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002204/*
2205 * Returns 0 on success or if called with an already aborted transaction.
2206 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2207 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002208static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2209 struct btrfs_root *root,
2210 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002211{
Chris Mason56bec292009-03-13 10:10:06 -04002212 struct btrfs_delayed_ref_root *delayed_refs;
2213 struct btrfs_delayed_ref_node *ref;
2214 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002215 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002216 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002217 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002218 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002219 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002220
2221 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002222 while (1) {
2223 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002224 /* pick a new head ref from the cluster list */
2225 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002226 break;
Chris Mason56bec292009-03-13 10:10:06 -04002227
Chris Masonc3e69d52009-03-13 10:17:05 -04002228 locked_ref = list_entry(cluster->next,
2229 struct btrfs_delayed_ref_head, cluster);
2230
2231 /* grab the lock that says we are going to process
2232 * all the refs for this head */
2233 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2234
2235 /*
2236 * we may have dropped the spin lock to get the head
2237 * mutex lock, and that might have given someone else
2238 * time to free the head. If that's true, it has been
2239 * removed from our list and we can move on.
2240 */
2241 if (ret == -EAGAIN) {
2242 locked_ref = NULL;
2243 count++;
2244 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002245 }
2246 }
2247
2248 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002249 * We need to try and merge add/drops of the same ref since we
2250 * can run into issues with relocate dropping the implicit ref
2251 * and then it being added back again before the drop can
2252 * finish. If we merged anything we need to re-loop so we can
2253 * get a good ref.
2254 */
2255 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2256 locked_ref);
2257
2258 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002259 * locked_ref is the head node, so we have to go one
2260 * node back for any delayed ref updates
2261 */
2262 ref = select_delayed_ref(locked_ref);
2263
2264 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002265 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002266 /*
2267 * there are still refs with lower seq numbers in the
2268 * process of being added. Don't run this ref yet.
2269 */
2270 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002271 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002272 locked_ref = NULL;
2273 delayed_refs->num_heads_ready++;
2274 spin_unlock(&delayed_refs->lock);
2275 cond_resched();
2276 spin_lock(&delayed_refs->lock);
2277 continue;
2278 }
2279
2280 /*
Chris Mason56bec292009-03-13 10:10:06 -04002281 * record the must insert reserved flag before we
2282 * drop the spin lock.
2283 */
2284 must_insert_reserved = locked_ref->must_insert_reserved;
2285 locked_ref->must_insert_reserved = 0;
2286
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002287 extent_op = locked_ref->extent_op;
2288 locked_ref->extent_op = NULL;
2289
Chris Mason56bec292009-03-13 10:10:06 -04002290 if (!ref) {
2291 /* All delayed refs have been processed, Go ahead
2292 * and send the head node to run_one_delayed_ref,
2293 * so that any accounting fixes can happen
2294 */
2295 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002296
2297 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002298 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299 extent_op = NULL;
2300 }
2301
2302 if (extent_op) {
2303 spin_unlock(&delayed_refs->lock);
2304
2305 ret = run_delayed_extent_op(trans, root,
2306 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002307 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002308
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002309 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002310 printk(KERN_DEBUG
2311 "btrfs: run_delayed_extent_op "
2312 "returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002313 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002314 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002315 return ret;
2316 }
2317
Chris Mason203bf282012-01-06 15:23:57 -05002318 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002319 }
Chris Mason56bec292009-03-13 10:10:06 -04002320 }
2321
2322 ref->in_tree = 0;
2323 rb_erase(&ref->rb_node, &delayed_refs->root);
2324 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002325 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002326 /*
2327 * when we play the delayed ref, also correct the
2328 * ref_mod on head
2329 */
2330 switch (ref->action) {
2331 case BTRFS_ADD_DELAYED_REF:
2332 case BTRFS_ADD_DELAYED_EXTENT:
2333 locked_ref->node.ref_mod -= ref->ref_mod;
2334 break;
2335 case BTRFS_DROP_DELAYED_REF:
2336 locked_ref->node.ref_mod += ref->ref_mod;
2337 break;
2338 default:
2339 WARN_ON(1);
2340 }
2341 }
Chris Mason56bec292009-03-13 10:10:06 -04002342 spin_unlock(&delayed_refs->lock);
2343
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002344 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002345 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002346
Miao Xie78a61842012-11-21 02:21:28 +00002347 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002348 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002349 btrfs_delayed_ref_unlock(locked_ref);
2350 btrfs_put_delayed_ref(ref);
2351 printk(KERN_DEBUG
2352 "btrfs: run_one_delayed_ref returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002353 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002354 return ret;
2355 }
2356
Miao Xie093486c2012-12-19 08:10:10 +00002357 /*
2358 * If this node is a head, that means all the refs in this head
2359 * have been dealt with, and we will pick the next head to deal
2360 * with, so we must unlock the head and drop it from the cluster
2361 * list before we release it.
2362 */
2363 if (btrfs_delayed_ref_is_head(ref)) {
2364 list_del_init(&locked_ref->cluster);
2365 btrfs_delayed_ref_unlock(locked_ref);
2366 locked_ref = NULL;
2367 }
2368 btrfs_put_delayed_ref(ref);
2369 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002370next:
Chris Mason1887be62009-03-13 10:11:24 -04002371 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002372 spin_lock(&delayed_refs->lock);
2373 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002374 return count;
2375}
2376
Arne Jansen709c0482011-09-12 12:22:57 +02002377#ifdef SCRAMBLE_DELAYED_REFS
2378/*
2379 * Normally delayed refs get processed in ascending bytenr order. This
2380 * correlates in most cases to the order added. To expose dependencies on this
2381 * order, we start to process the tree in the middle instead of the beginning
2382 */
2383static u64 find_middle(struct rb_root *root)
2384{
2385 struct rb_node *n = root->rb_node;
2386 struct btrfs_delayed_ref_node *entry;
2387 int alt = 1;
2388 u64 middle;
2389 u64 first = 0, last = 0;
2390
2391 n = rb_first(root);
2392 if (n) {
2393 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2394 first = entry->bytenr;
2395 }
2396 n = rb_last(root);
2397 if (n) {
2398 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2399 last = entry->bytenr;
2400 }
2401 n = root->rb_node;
2402
2403 while (n) {
2404 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2405 WARN_ON(!entry->in_tree);
2406
2407 middle = entry->bytenr;
2408
2409 if (alt)
2410 n = n->rb_left;
2411 else
2412 n = n->rb_right;
2413
2414 alt = 1 - alt;
2415 }
2416 return middle;
2417}
2418#endif
2419
Arne Jansenbed92ea2012-06-28 18:03:02 +02002420int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2421 struct btrfs_fs_info *fs_info)
2422{
2423 struct qgroup_update *qgroup_update;
2424 int ret = 0;
2425
2426 if (list_empty(&trans->qgroup_ref_list) !=
2427 !trans->delayed_ref_elem.seq) {
2428 /* list without seq or seq without list */
2429 printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
2430 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2431 trans->delayed_ref_elem.seq);
2432 BUG();
2433 }
2434
2435 if (!trans->delayed_ref_elem.seq)
2436 return 0;
2437
2438 while (!list_empty(&trans->qgroup_ref_list)) {
2439 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2440 struct qgroup_update, list);
2441 list_del(&qgroup_update->list);
2442 if (!ret)
2443 ret = btrfs_qgroup_account_ref(
2444 trans, fs_info, qgroup_update->node,
2445 qgroup_update->extent_op);
2446 kfree(qgroup_update);
2447 }
2448
2449 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2450
2451 return ret;
2452}
2453
Chris Masonbb721702013-01-29 18:44:12 -05002454static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
2455 int count)
2456{
2457 int val = atomic_read(&delayed_refs->ref_seq);
2458
2459 if (val < seq || val >= seq + count)
2460 return 1;
2461 return 0;
2462}
2463
Chris Masonc3e69d52009-03-13 10:17:05 -04002464/*
2465 * this starts processing the delayed reference count updates and
2466 * extent insertions we have queued up so far. count can be
2467 * 0, which means to process everything in the tree at the start
2468 * of the run (but not newly added entries), or it can be some target
2469 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002470 *
2471 * Returns 0 on success or if called with an aborted transaction
2472 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002473 */
2474int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2475 struct btrfs_root *root, unsigned long count)
2476{
2477 struct rb_node *node;
2478 struct btrfs_delayed_ref_root *delayed_refs;
2479 struct btrfs_delayed_ref_node *ref;
2480 struct list_head cluster;
2481 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002482 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002483 int run_all = count == (unsigned long)-1;
2484 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002485 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002486
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002487 /* We'll clean this up in btrfs_cleanup_transaction */
2488 if (trans->aborted)
2489 return 0;
2490
Chris Masonc3e69d52009-03-13 10:17:05 -04002491 if (root == root->fs_info->extent_root)
2492 root = root->fs_info->tree_root;
2493
Jan Schmidtedf39272012-06-28 18:04:55 +02002494 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2495
Chris Masonc3e69d52009-03-13 10:17:05 -04002496 delayed_refs = &trans->transaction->delayed_refs;
2497 INIT_LIST_HEAD(&cluster);
Chris Masonbb721702013-01-29 18:44:12 -05002498 if (count == 0) {
2499 count = delayed_refs->num_entries * 2;
2500 run_most = 1;
2501 }
2502
2503 if (!run_all && !run_most) {
2504 int old;
2505 int seq = atomic_read(&delayed_refs->ref_seq);
2506
2507progress:
2508 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2509 if (old) {
2510 DEFINE_WAIT(__wait);
2511 if (delayed_refs->num_entries < 16348)
2512 return 0;
2513
2514 prepare_to_wait(&delayed_refs->wait, &__wait,
2515 TASK_UNINTERRUPTIBLE);
2516
2517 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2518 if (old) {
2519 schedule();
2520 finish_wait(&delayed_refs->wait, &__wait);
2521
2522 if (!refs_newer(delayed_refs, seq, 256))
2523 goto progress;
2524 else
2525 return 0;
2526 } else {
2527 finish_wait(&delayed_refs->wait, &__wait);
2528 goto again;
2529 }
2530 }
2531
2532 } else {
2533 atomic_inc(&delayed_refs->procs_running_refs);
2534 }
2535
Chris Masonc3e69d52009-03-13 10:17:05 -04002536again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002537 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002538 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002539
Arne Jansen709c0482011-09-12 12:22:57 +02002540#ifdef SCRAMBLE_DELAYED_REFS
2541 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2542#endif
2543
Chris Masonc3e69d52009-03-13 10:17:05 -04002544 while (1) {
2545 if (!(run_all || run_most) &&
2546 delayed_refs->num_heads_ready < 64)
2547 break;
2548
2549 /*
2550 * go find something we can process in the rbtree. We start at
2551 * the beginning of the tree, and then build a cluster
2552 * of refs to process starting at the first one we are able to
2553 * lock
2554 */
Jan Schmidta1686502011-12-12 16:10:07 +01002555 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002556 ret = btrfs_find_ref_cluster(trans, &cluster,
2557 delayed_refs->run_delayed_start);
2558 if (ret)
2559 break;
2560
2561 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002562 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002563 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002564 spin_unlock(&delayed_refs->lock);
2565 btrfs_abort_transaction(trans, root, ret);
Chris Masonbb721702013-01-29 18:44:12 -05002566 atomic_dec(&delayed_refs->procs_running_refs);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002567 return ret;
2568 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002569
Chris Masonbb721702013-01-29 18:44:12 -05002570 atomic_add(ret, &delayed_refs->ref_seq);
2571
Chris Masonc3e69d52009-03-13 10:17:05 -04002572 count -= min_t(unsigned long, ret, count);
2573
2574 if (count == 0)
2575 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002576
Arne Jansen1fa11e22012-08-06 14:18:51 -06002577 if (delayed_start >= delayed_refs->run_delayed_start) {
2578 if (loops == 0) {
2579 /*
2580 * btrfs_find_ref_cluster looped. let's do one
2581 * more cycle. if we don't run any delayed ref
2582 * during that cycle (because we can't because
2583 * all of them are blocked), bail out.
2584 */
2585 loops = 1;
2586 } else {
2587 /*
2588 * no runnable refs left, stop trying
2589 */
2590 BUG_ON(run_all);
2591 break;
2592 }
2593 }
2594 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002595 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002596 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002597 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002598 }
2599
Chris Mason56bec292009-03-13 10:10:06 -04002600 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002601 if (!list_empty(&trans->new_bgs)) {
2602 spin_unlock(&delayed_refs->lock);
2603 btrfs_create_pending_block_groups(trans, root);
2604 spin_lock(&delayed_refs->lock);
2605 }
2606
Chris Mason56bec292009-03-13 10:10:06 -04002607 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002608 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002609 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002610 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002611
2612 while (node) {
2613 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2614 rb_node);
2615 if (btrfs_delayed_ref_is_head(ref)) {
2616 struct btrfs_delayed_ref_head *head;
2617
2618 head = btrfs_delayed_node_to_head(ref);
2619 atomic_inc(&ref->refs);
2620
2621 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002622 /*
2623 * Mutex was contended, block until it's
2624 * released and try again
2625 */
Chris Mason56bec292009-03-13 10:10:06 -04002626 mutex_lock(&head->mutex);
2627 mutex_unlock(&head->mutex);
2628
2629 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002630 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002631 goto again;
2632 }
2633 node = rb_next(node);
2634 }
2635 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002636 schedule_timeout(1);
2637 goto again;
2638 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002639out:
Chris Masonbb721702013-01-29 18:44:12 -05002640 atomic_dec(&delayed_refs->procs_running_refs);
2641 smp_mb();
2642 if (waitqueue_active(&delayed_refs->wait))
2643 wake_up(&delayed_refs->wait);
2644
Chris Masonc3e69d52009-03-13 10:17:05 -04002645 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002646 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002647 return 0;
2648}
2649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002650int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2651 struct btrfs_root *root,
2652 u64 bytenr, u64 num_bytes, u64 flags,
2653 int is_data)
2654{
2655 struct btrfs_delayed_extent_op *extent_op;
2656 int ret;
2657
Miao Xie78a61842012-11-21 02:21:28 +00002658 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002659 if (!extent_op)
2660 return -ENOMEM;
2661
2662 extent_op->flags_to_set = flags;
2663 extent_op->update_flags = 1;
2664 extent_op->update_key = 0;
2665 extent_op->is_data = is_data ? 1 : 0;
2666
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002667 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2668 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002669 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002670 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002671 return ret;
2672}
2673
2674static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2675 struct btrfs_root *root,
2676 struct btrfs_path *path,
2677 u64 objectid, u64 offset, u64 bytenr)
2678{
2679 struct btrfs_delayed_ref_head *head;
2680 struct btrfs_delayed_ref_node *ref;
2681 struct btrfs_delayed_data_ref *data_ref;
2682 struct btrfs_delayed_ref_root *delayed_refs;
2683 struct rb_node *node;
2684 int ret = 0;
2685
2686 ret = -ENOENT;
2687 delayed_refs = &trans->transaction->delayed_refs;
2688 spin_lock(&delayed_refs->lock);
2689 head = btrfs_find_delayed_ref_head(trans, bytenr);
2690 if (!head)
2691 goto out;
2692
2693 if (!mutex_trylock(&head->mutex)) {
2694 atomic_inc(&head->node.refs);
2695 spin_unlock(&delayed_refs->lock);
2696
David Sterbab3b4aa72011-04-21 01:20:15 +02002697 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002698
David Sterba8cc33e52011-05-02 15:29:25 +02002699 /*
2700 * Mutex was contended, block until it's released and let
2701 * caller try again
2702 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002703 mutex_lock(&head->mutex);
2704 mutex_unlock(&head->mutex);
2705 btrfs_put_delayed_ref(&head->node);
2706 return -EAGAIN;
2707 }
2708
2709 node = rb_prev(&head->node.rb_node);
2710 if (!node)
2711 goto out_unlock;
2712
2713 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2714
2715 if (ref->bytenr != bytenr)
2716 goto out_unlock;
2717
2718 ret = 1;
2719 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2720 goto out_unlock;
2721
2722 data_ref = btrfs_delayed_node_to_data_ref(ref);
2723
2724 node = rb_prev(node);
2725 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002726 int seq = ref->seq;
2727
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002728 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002729 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002730 goto out_unlock;
2731 }
2732
2733 if (data_ref->root != root->root_key.objectid ||
2734 data_ref->objectid != objectid || data_ref->offset != offset)
2735 goto out_unlock;
2736
2737 ret = 0;
2738out_unlock:
2739 mutex_unlock(&head->mutex);
2740out:
2741 spin_unlock(&delayed_refs->lock);
2742 return ret;
2743}
2744
2745static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2746 struct btrfs_root *root,
2747 struct btrfs_path *path,
2748 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002749{
2750 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002751 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002752 struct btrfs_extent_data_ref *ref;
2753 struct btrfs_extent_inline_ref *iref;
2754 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002755 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002756 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002757 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002758
Chris Masonbe20aa92007-12-17 20:14:01 -05002759 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002760 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002761 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002762
Chris Masonbe20aa92007-12-17 20:14:01 -05002763 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2764 if (ret < 0)
2765 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002766 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002767
2768 ret = -ENOENT;
2769 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002770 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002771
Zheng Yan31840ae2008-09-23 13:14:14 -04002772 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002773 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002774 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002775
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002776 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002777 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002778
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002779 ret = 1;
2780 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2781#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2782 if (item_size < sizeof(*ei)) {
2783 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2784 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002785 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002786#endif
2787 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2788
2789 if (item_size != sizeof(*ei) +
2790 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2791 goto out;
2792
2793 if (btrfs_extent_generation(leaf, ei) <=
2794 btrfs_root_last_snapshot(&root->root_item))
2795 goto out;
2796
2797 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2798 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2799 BTRFS_EXTENT_DATA_REF_KEY)
2800 goto out;
2801
2802 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2803 if (btrfs_extent_refs(leaf, ei) !=
2804 btrfs_extent_data_ref_count(leaf, ref) ||
2805 btrfs_extent_data_ref_root(leaf, ref) !=
2806 root->root_key.objectid ||
2807 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2808 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2809 goto out;
2810
Yan Zhengf321e492008-07-30 09:26:11 -04002811 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002812out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002813 return ret;
2814}
2815
2816int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2817 struct btrfs_root *root,
2818 u64 objectid, u64 offset, u64 bytenr)
2819{
2820 struct btrfs_path *path;
2821 int ret;
2822 int ret2;
2823
2824 path = btrfs_alloc_path();
2825 if (!path)
2826 return -ENOENT;
2827
2828 do {
2829 ret = check_committed_ref(trans, root, path, objectid,
2830 offset, bytenr);
2831 if (ret && ret != -ENOENT)
2832 goto out;
2833
2834 ret2 = check_delayed_ref(trans, root, path, objectid,
2835 offset, bytenr);
2836 } while (ret2 == -EAGAIN);
2837
2838 if (ret2 && ret2 != -ENOENT) {
2839 ret = ret2;
2840 goto out;
2841 }
2842
2843 if (ret != -ENOENT || ret2 != -ENOENT)
2844 ret = 0;
2845out:
Yan Zhengf321e492008-07-30 09:26:11 -04002846 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002847 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2848 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002849 return ret;
2850}
2851
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002852static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002853 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002854 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002855 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002856{
2857 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002858 u64 num_bytes;
2859 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002860 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002861 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002862 struct btrfs_key key;
2863 struct btrfs_file_extent_item *fi;
2864 int i;
2865 int level;
2866 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002867 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002868 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002869
2870 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002871 nritems = btrfs_header_nritems(buf);
2872 level = btrfs_header_level(buf);
2873
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002874 if (!root->ref_cows && level == 0)
2875 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002876
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002877 if (inc)
2878 process_func = btrfs_inc_extent_ref;
2879 else
2880 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002881
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002882 if (full_backref)
2883 parent = buf->start;
2884 else
2885 parent = 0;
2886
Zheng Yan31840ae2008-09-23 13:14:14 -04002887 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002888 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002889 btrfs_item_key_to_cpu(buf, &key, i);
2890 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002891 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002892 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002893 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002894 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002895 BTRFS_FILE_EXTENT_INLINE)
2896 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002897 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2898 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002899 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002900
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002901 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2902 key.offset -= btrfs_file_extent_offset(buf, fi);
2903 ret = process_func(trans, root, bytenr, num_bytes,
2904 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002905 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002906 if (ret)
2907 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002908 } else {
2909 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002910 num_bytes = btrfs_level_size(root, level - 1);
2911 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002912 parent, ref_root, level - 1, 0,
2913 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002914 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002915 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002916 }
2917 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002918 return 0;
2919fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002920 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002921}
2922
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002923int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002924 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002925{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002926 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002927}
Zheng Yan31840ae2008-09-23 13:14:14 -04002928
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002929int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002930 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002931{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002932 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04002933}
2934
Chris Mason9078a3e2007-04-26 16:46:15 -04002935static int write_one_cache_group(struct btrfs_trans_handle *trans,
2936 struct btrfs_root *root,
2937 struct btrfs_path *path,
2938 struct btrfs_block_group_cache *cache)
2939{
2940 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002941 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002942 unsigned long bi;
2943 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002944
Chris Mason9078a3e2007-04-26 16:46:15 -04002945 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002946 if (ret < 0)
2947 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002948 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04002949
2950 leaf = path->nodes[0];
2951 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2952 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2953 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002954 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002955fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002956 if (ret) {
2957 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04002958 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002959 }
Chris Mason9078a3e2007-04-26 16:46:15 -04002960 return 0;
2961
2962}
2963
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002964static struct btrfs_block_group_cache *
2965next_block_group(struct btrfs_root *root,
2966 struct btrfs_block_group_cache *cache)
2967{
2968 struct rb_node *node;
2969 spin_lock(&root->fs_info->block_group_cache_lock);
2970 node = rb_next(&cache->cache_node);
2971 btrfs_put_block_group(cache);
2972 if (node) {
2973 cache = rb_entry(node, struct btrfs_block_group_cache,
2974 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002975 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002976 } else
2977 cache = NULL;
2978 spin_unlock(&root->fs_info->block_group_cache_lock);
2979 return cache;
2980}
2981
Josef Bacik0af3d002010-06-21 14:48:16 -04002982static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2983 struct btrfs_trans_handle *trans,
2984 struct btrfs_path *path)
2985{
2986 struct btrfs_root *root = block_group->fs_info->tree_root;
2987 struct inode *inode = NULL;
2988 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002989 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002990 int num_pages = 0;
2991 int retries = 0;
2992 int ret = 0;
2993
2994 /*
2995 * If this block group is smaller than 100 megs don't bother caching the
2996 * block group.
2997 */
2998 if (block_group->key.offset < (100 * 1024 * 1024)) {
2999 spin_lock(&block_group->lock);
3000 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3001 spin_unlock(&block_group->lock);
3002 return 0;
3003 }
3004
3005again:
3006 inode = lookup_free_space_inode(root, block_group, path);
3007 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3008 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003009 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003010 goto out;
3011 }
3012
3013 if (IS_ERR(inode)) {
3014 BUG_ON(retries);
3015 retries++;
3016
3017 if (block_group->ro)
3018 goto out_free;
3019
3020 ret = create_free_space_inode(root, trans, block_group, path);
3021 if (ret)
3022 goto out_free;
3023 goto again;
3024 }
3025
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003026 /* We've already setup this transaction, go ahead and exit */
3027 if (block_group->cache_generation == trans->transid &&
3028 i_size_read(inode)) {
3029 dcs = BTRFS_DC_SETUP;
3030 goto out_put;
3031 }
3032
Josef Bacik0af3d002010-06-21 14:48:16 -04003033 /*
3034 * We want to set the generation to 0, that way if anything goes wrong
3035 * from here on out we know not to trust this cache when we load up next
3036 * time.
3037 */
3038 BTRFS_I(inode)->generation = 0;
3039 ret = btrfs_update_inode(trans, root, inode);
3040 WARN_ON(ret);
3041
3042 if (i_size_read(inode) > 0) {
3043 ret = btrfs_truncate_free_space_cache(root, trans, path,
3044 inode);
3045 if (ret)
3046 goto out_put;
3047 }
3048
3049 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003050 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3051 !btrfs_test_opt(root, SPACE_CACHE)) {
3052 /*
3053 * don't bother trying to write stuff out _if_
3054 * a) we're not cached,
3055 * b) we're with nospace_cache mount option.
3056 */
Josef Bacik2b209822010-12-03 13:17:53 -05003057 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003058 spin_unlock(&block_group->lock);
3059 goto out_put;
3060 }
3061 spin_unlock(&block_group->lock);
3062
Josef Bacik6fc823b2012-08-06 13:46:38 -06003063 /*
3064 * Try to preallocate enough space based on how big the block group is.
3065 * Keep in mind this has to include any pinned space which could end up
3066 * taking up quite a bit since it's not folded into the other space
3067 * cache.
3068 */
3069 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003070 if (!num_pages)
3071 num_pages = 1;
3072
Josef Bacik0af3d002010-06-21 14:48:16 -04003073 num_pages *= 16;
3074 num_pages *= PAGE_CACHE_SIZE;
3075
3076 ret = btrfs_check_data_free_space(inode, num_pages);
3077 if (ret)
3078 goto out_put;
3079
3080 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3081 num_pages, num_pages,
3082 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003083 if (!ret)
3084 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003085 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003086
Josef Bacik0af3d002010-06-21 14:48:16 -04003087out_put:
3088 iput(inode);
3089out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003090 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003091out:
3092 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003093 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003094 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003095 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003096 spin_unlock(&block_group->lock);
3097
3098 return ret;
3099}
3100
Chris Mason96b51792007-10-15 16:15:19 -04003101int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3102 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003103{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003104 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003105 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003106 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003107 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003108
3109 path = btrfs_alloc_path();
3110 if (!path)
3111 return -ENOMEM;
3112
Josef Bacik0af3d002010-06-21 14:48:16 -04003113again:
3114 while (1) {
3115 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3116 while (cache) {
3117 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3118 break;
3119 cache = next_block_group(root, cache);
3120 }
3121 if (!cache) {
3122 if (last == 0)
3123 break;
3124 last = 0;
3125 continue;
3126 }
3127 err = cache_save_setup(cache, trans, path);
3128 last = cache->key.objectid + cache->key.offset;
3129 btrfs_put_block_group(cache);
3130 }
3131
Chris Masond3977122009-01-05 21:25:51 -05003132 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003133 if (last == 0) {
3134 err = btrfs_run_delayed_refs(trans, root,
3135 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003136 if (err) /* File system offline */
3137 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003138 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003139
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003140 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3141 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003142 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3143 btrfs_put_block_group(cache);
3144 goto again;
3145 }
3146
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003147 if (cache->dirty)
3148 break;
3149 cache = next_block_group(root, cache);
3150 }
3151 if (!cache) {
3152 if (last == 0)
3153 break;
3154 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003155 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003156 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003157
Josef Bacik0cb59c92010-07-02 12:14:14 -04003158 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3159 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003160 cache->dirty = 0;
3161 last = cache->key.objectid + cache->key.offset;
3162
3163 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003164 if (err) /* File system offline */
3165 goto out;
3166
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003167 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003168 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003169
Josef Bacik0cb59c92010-07-02 12:14:14 -04003170 while (1) {
3171 /*
3172 * I don't think this is needed since we're just marking our
3173 * preallocated extent as written, but just in case it can't
3174 * hurt.
3175 */
3176 if (last == 0) {
3177 err = btrfs_run_delayed_refs(trans, root,
3178 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003179 if (err) /* File system offline */
3180 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003181 }
3182
3183 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3184 while (cache) {
3185 /*
3186 * Really this shouldn't happen, but it could if we
3187 * couldn't write the entire preallocated extent and
3188 * splitting the extent resulted in a new block.
3189 */
3190 if (cache->dirty) {
3191 btrfs_put_block_group(cache);
3192 goto again;
3193 }
3194 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3195 break;
3196 cache = next_block_group(root, cache);
3197 }
3198 if (!cache) {
3199 if (last == 0)
3200 break;
3201 last = 0;
3202 continue;
3203 }
3204
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003205 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003206
3207 /*
3208 * If we didn't have an error then the cache state is still
3209 * NEED_WRITE, so we can set it to WRITTEN.
3210 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003211 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003212 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3213 last = cache->key.objectid + cache->key.offset;
3214 btrfs_put_block_group(cache);
3215 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003216out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003217
Chris Mason9078a3e2007-04-26 16:46:15 -04003218 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003219 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003220}
3221
Yan Zhengd2fb3432008-12-11 16:30:39 -05003222int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3223{
3224 struct btrfs_block_group_cache *block_group;
3225 int readonly = 0;
3226
3227 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3228 if (!block_group || block_group->ro)
3229 readonly = 1;
3230 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003231 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003232 return readonly;
3233}
3234
Chris Mason593060d2008-03-25 16:50:33 -04003235static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3236 u64 total_bytes, u64 bytes_used,
3237 struct btrfs_space_info **space_info)
3238{
3239 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003240 int i;
3241 int factor;
3242
3243 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3244 BTRFS_BLOCK_GROUP_RAID10))
3245 factor = 2;
3246 else
3247 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003248
3249 found = __find_space_info(info, flags);
3250 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003251 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003252 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003253 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003254 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003255 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003256 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003257 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003258 *space_info = found;
3259 return 0;
3260 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003261 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003262 if (!found)
3263 return -ENOMEM;
3264
Yan, Zhengb742bb82010-05-16 10:46:24 -04003265 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3266 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003267 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003268 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003269 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003270 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003271 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003272 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003273 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003274 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003275 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003276 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003277 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003278 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003279 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003280 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003281 found->flush = 0;
3282 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003283 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003284 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003285 if (flags & BTRFS_BLOCK_GROUP_DATA)
3286 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003287 return 0;
3288}
3289
Chris Mason8790d502008-04-03 16:29:03 -04003290static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3291{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003292 u64 extra_flags = chunk_to_extended(flags) &
3293 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003294
Miao Xiede98ced2013-01-29 10:13:12 +00003295 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003296 if (flags & BTRFS_BLOCK_GROUP_DATA)
3297 fs_info->avail_data_alloc_bits |= extra_flags;
3298 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3299 fs_info->avail_metadata_alloc_bits |= extra_flags;
3300 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3301 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003302 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003303}
Chris Mason593060d2008-03-25 16:50:33 -04003304
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003305/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003306 * returns target flags in extended format or 0 if restripe for this
3307 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003308 *
3309 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003310 */
3311static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3312{
3313 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3314 u64 target = 0;
3315
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003316 if (!bctl)
3317 return 0;
3318
3319 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3320 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3321 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3322 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3323 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3324 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3325 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3326 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3327 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3328 }
3329
3330 return target;
3331}
3332
3333/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003334 * @flags: available profiles in extended format (see ctree.h)
3335 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003336 * Returns reduced profile in chunk format. If profile changing is in
3337 * progress (either running or paused) picks the target profile (if it's
3338 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003339 */
Yan Zheng2b820322008-11-17 21:11:30 -05003340u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003341{
Chris Masoncd02dca2010-12-13 14:56:23 -05003342 /*
3343 * we add in the count of missing devices because we want
3344 * to make sure that any RAID levels on a degraded FS
3345 * continue to be honored.
3346 */
3347 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3348 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003349 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003350 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003351
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003352 /*
3353 * see if restripe for this chunk_type is in progress, if so
3354 * try to reduce to the target profile
3355 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003356 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003357 target = get_restripe_target(root->fs_info, flags);
3358 if (target) {
3359 /* pick target profile only if it's already available */
3360 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003361 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003362 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003363 }
3364 }
3365 spin_unlock(&root->fs_info->balance_lock);
3366
David Woodhouse53b381b2013-01-29 18:40:14 -05003367 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003368 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003369 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3370 BTRFS_BLOCK_GROUP_RAID5);
3371 if (num_devices < 3)
3372 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003373 if (num_devices < 4)
3374 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3375
David Woodhouse53b381b2013-01-29 18:40:14 -05003376 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3377 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3378 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3379 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003380
David Woodhouse53b381b2013-01-29 18:40:14 -05003381 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3382 tmp = BTRFS_BLOCK_GROUP_RAID6;
3383 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3384 tmp = BTRFS_BLOCK_GROUP_RAID5;
3385 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3386 tmp = BTRFS_BLOCK_GROUP_RAID10;
3387 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3388 tmp = BTRFS_BLOCK_GROUP_RAID1;
3389 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3390 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003391
David Woodhouse53b381b2013-01-29 18:40:14 -05003392 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003393}
3394
Yan, Zhengb742bb82010-05-16 10:46:24 -04003395static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003396{
Miao Xiede98ced2013-01-29 10:13:12 +00003397 unsigned seq;
3398
3399 do {
3400 seq = read_seqbegin(&root->fs_info->profiles_lock);
3401
3402 if (flags & BTRFS_BLOCK_GROUP_DATA)
3403 flags |= root->fs_info->avail_data_alloc_bits;
3404 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3405 flags |= root->fs_info->avail_system_alloc_bits;
3406 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3407 flags |= root->fs_info->avail_metadata_alloc_bits;
3408 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003409
Yan, Zhengb742bb82010-05-16 10:46:24 -04003410 return btrfs_reduce_alloc_profile(root, flags);
3411}
Josef Bacik6a632092009-02-20 11:00:09 -05003412
Miao Xie6d07bce2011-01-05 10:07:31 +00003413u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003414{
3415 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003416 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003417
Yan, Zhengb742bb82010-05-16 10:46:24 -04003418 if (data)
3419 flags = BTRFS_BLOCK_GROUP_DATA;
3420 else if (root == root->fs_info->chunk_root)
3421 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3422 else
3423 flags = BTRFS_BLOCK_GROUP_METADATA;
3424
David Woodhouse53b381b2013-01-29 18:40:14 -05003425 ret = get_alloc_profile(root, flags);
3426 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003427}
3428
Josef Bacik6a632092009-02-20 11:00:09 -05003429/*
3430 * This will check the space that the inode allocates from to make sure we have
3431 * enough space for bytes.
3432 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003433int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003434{
3435 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003436 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003437 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003438 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003439 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003440
3441 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003442 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003443
Li Zefan82d59022011-04-20 10:33:24 +08003444 if (root == root->fs_info->tree_root ||
3445 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003446 alloc_chunk = 0;
3447 committed = 1;
3448 }
3449
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003450 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003451 if (!data_sinfo)
3452 goto alloc;
3453
Josef Bacik6a632092009-02-20 11:00:09 -05003454again:
3455 /* make sure we have enough space to handle the data first */
3456 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003457 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3458 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3459 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003460
3461 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003462 struct btrfs_trans_handle *trans;
3463
Josef Bacik6a632092009-02-20 11:00:09 -05003464 /*
3465 * if we don't have enough free bytes in this space then we need
3466 * to alloc a new chunk.
3467 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003468 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003469 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003470
Chris Mason0e4f8f82011-04-15 16:05:44 -04003471 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003472 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003473alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003474 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003475 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003476 if (IS_ERR(trans))
3477 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003478
3479 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003480 alloc_target,
3481 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003482 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003483 if (ret < 0) {
3484 if (ret != -ENOSPC)
3485 return ret;
3486 else
3487 goto commit_trans;
3488 }
Chris Mason33b4d472009-09-22 14:45:50 -04003489
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003490 if (!data_sinfo)
3491 data_sinfo = fs_info->data_sinfo;
3492
Josef Bacik6a632092009-02-20 11:00:09 -05003493 goto again;
3494 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003495
3496 /*
3497 * If we have less pinned bytes than we want to allocate then
3498 * don't bother committing the transaction, it won't help us.
3499 */
3500 if (data_sinfo->bytes_pinned < bytes)
3501 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003502 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003503
3504 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003505commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003506 if (!committed &&
3507 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003508 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003509 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003510 if (IS_ERR(trans))
3511 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003512 ret = btrfs_commit_transaction(trans, root);
3513 if (ret)
3514 return ret;
3515 goto again;
3516 }
3517
Josef Bacik6a632092009-02-20 11:00:09 -05003518 return -ENOSPC;
3519 }
3520 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003521 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003522 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003523 spin_unlock(&data_sinfo->lock);
3524
Josef Bacik9ed74f22009-09-11 16:12:44 -04003525 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003526}
3527
3528/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003529 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003530 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003531void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003532{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003533 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003534 struct btrfs_space_info *data_sinfo;
3535
3536 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003537 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003538
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003539 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003540 spin_lock(&data_sinfo->lock);
3541 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003542 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003543 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003544 spin_unlock(&data_sinfo->lock);
3545}
3546
Josef Bacik97e728d2009-04-21 17:40:57 -04003547static void force_metadata_allocation(struct btrfs_fs_info *info)
3548{
3549 struct list_head *head = &info->space_info;
3550 struct btrfs_space_info *found;
3551
3552 rcu_read_lock();
3553 list_for_each_entry_rcu(found, head, list) {
3554 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003555 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003556 }
3557 rcu_read_unlock();
3558}
3559
Chris Masone5bc2452010-10-26 13:37:56 -04003560static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003561 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003562{
Josef Bacikfb25e912011-07-26 17:00:46 -04003563 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003564 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003565 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003566 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003567
Chris Mason0e4f8f82011-04-15 16:05:44 -04003568 if (force == CHUNK_ALLOC_FORCE)
3569 return 1;
3570
3571 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003572 * We need to take into account the global rsv because for all intents
3573 * and purposes it's used space. Don't worry about locking the
3574 * global_rsv, it doesn't change except when the transaction commits.
3575 */
Josef Bacik54338b52012-08-14 16:20:52 -04003576 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3577 num_allocated += global_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003578
3579 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003580 * in limited mode, we want to have some free space up to
3581 * about 1% of the FS size.
3582 */
3583 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003584 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003585 thresh = max_t(u64, 64 * 1024 * 1024,
3586 div_factor_fine(thresh, 1));
3587
3588 if (num_bytes - num_allocated < thresh)
3589 return 1;
3590 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003591
Josef Bacik698d0082012-09-12 14:08:47 -04003592 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003593 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003594 return 1;
3595}
3596
Liu Bo15d1ff82012-03-29 09:57:44 -04003597static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3598{
3599 u64 num_dev;
3600
David Woodhouse53b381b2013-01-29 18:40:14 -05003601 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3602 BTRFS_BLOCK_GROUP_RAID0 |
3603 BTRFS_BLOCK_GROUP_RAID5 |
3604 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003605 num_dev = root->fs_info->fs_devices->rw_devices;
3606 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3607 num_dev = 2;
3608 else
3609 num_dev = 1; /* DUP or single */
3610
3611 /* metadata for updaing devices and chunk tree */
3612 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3613}
3614
3615static void check_system_chunk(struct btrfs_trans_handle *trans,
3616 struct btrfs_root *root, u64 type)
3617{
3618 struct btrfs_space_info *info;
3619 u64 left;
3620 u64 thresh;
3621
3622 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3623 spin_lock(&info->lock);
3624 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3625 info->bytes_reserved - info->bytes_readonly;
3626 spin_unlock(&info->lock);
3627
3628 thresh = get_system_chunk_thresh(root, type);
3629 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3630 printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n",
3631 left, thresh, type);
3632 dump_space_info(info, 0, 0);
3633 }
3634
3635 if (left < thresh) {
3636 u64 flags;
3637
3638 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3639 btrfs_alloc_chunk(trans, root, flags);
3640 }
3641}
3642
Chris Mason6324fbf2008-03-24 15:01:59 -04003643static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003644 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003645{
3646 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003647 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003648 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003649 int ret = 0;
3650
Josef Bacikc6b305a2012-12-18 09:16:16 -05003651 /* Don't re-enter if we're already allocating a chunk */
3652 if (trans->allocating_chunk)
3653 return -ENOSPC;
3654
Chris Mason6324fbf2008-03-24 15:01:59 -04003655 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003656 if (!space_info) {
3657 ret = update_space_info(extent_root->fs_info, flags,
3658 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003659 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003660 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003661 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003662
Josef Bacik6d741192011-04-11 20:20:11 -04003663again:
Josef Bacik25179202008-10-29 14:49:05 -04003664 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003665 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003666 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003667 if (space_info->full) {
3668 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003669 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003670 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003671
Josef Bacik698d0082012-09-12 14:08:47 -04003672 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003673 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003674 return 0;
3675 } else if (space_info->chunk_alloc) {
3676 wait_for_alloc = 1;
3677 } else {
3678 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003679 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003680
Josef Bacik25179202008-10-29 14:49:05 -04003681 spin_unlock(&space_info->lock);
3682
Josef Bacik6d741192011-04-11 20:20:11 -04003683 mutex_lock(&fs_info->chunk_mutex);
3684
3685 /*
3686 * The chunk_mutex is held throughout the entirety of a chunk
3687 * allocation, so once we've acquired the chunk_mutex we know that the
3688 * other guy is done and we need to recheck and see if we should
3689 * allocate.
3690 */
3691 if (wait_for_alloc) {
3692 mutex_unlock(&fs_info->chunk_mutex);
3693 wait_for_alloc = 0;
3694 goto again;
3695 }
3696
Josef Bacikc6b305a2012-12-18 09:16:16 -05003697 trans->allocating_chunk = true;
3698
Josef Bacik97e728d2009-04-21 17:40:57 -04003699 /*
Josef Bacik67377732010-09-16 16:19:09 -04003700 * If we have mixed data/metadata chunks we want to make sure we keep
3701 * allocating mixed chunks instead of individual chunks.
3702 */
3703 if (btrfs_mixed_space_info(space_info))
3704 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3705
3706 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003707 * if we're doing a data chunk, go ahead and make sure that
3708 * we keep a reasonable number of metadata chunks allocated in the
3709 * FS as well.
3710 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003711 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003712 fs_info->data_chunk_allocations++;
3713 if (!(fs_info->data_chunk_allocations %
3714 fs_info->metadata_ratio))
3715 force_metadata_allocation(fs_info);
3716 }
3717
Liu Bo15d1ff82012-03-29 09:57:44 -04003718 /*
3719 * Check if we have enough space in SYSTEM chunk because we may need
3720 * to update devices.
3721 */
3722 check_system_chunk(trans, extent_root, flags);
3723
Yan Zheng2b820322008-11-17 21:11:30 -05003724 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003725 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003726
Josef Bacik9ed74f22009-09-11 16:12:44 -04003727 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003728 if (ret < 0 && ret != -ENOSPC)
3729 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003730 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003731 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003732 else
3733 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003734
Chris Mason0e4f8f82011-04-15 16:05:44 -04003735 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003736out:
Josef Bacik6d741192011-04-11 20:20:11 -04003737 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003738 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003739 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003740 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003741}
3742
Josef Bacika80c8dc2012-09-06 16:59:33 -04003743static int can_overcommit(struct btrfs_root *root,
3744 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003745 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003746{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003747 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003748 u64 profile = btrfs_get_alloc_profile(root, 0);
Josef Bacik96f1bb52013-01-30 17:02:51 -05003749 u64 rsv_size = 0;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003750 u64 avail;
3751 u64 used;
Josef Bacik70afa392013-02-06 13:53:19 -05003752 u64 to_add;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003753
3754 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003755 space_info->bytes_pinned + space_info->bytes_readonly;
3756
3757 spin_lock(&global_rsv->lock);
3758 rsv_size = global_rsv->size;
3759 spin_unlock(&global_rsv->lock);
3760
3761 /*
3762 * We only want to allow over committing if we have lots of actual space
3763 * free, but if we don't have enough space to handle the global reserve
3764 * space then we could end up having a real enospc problem when trying
3765 * to allocate a chunk or some other such important allocation.
3766 */
3767 rsv_size <<= 1;
3768 if (used + rsv_size >= space_info->total_bytes)
3769 return 0;
3770
3771 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003772
3773 spin_lock(&root->fs_info->free_chunk_lock);
3774 avail = root->fs_info->free_chunk_space;
3775 spin_unlock(&root->fs_info->free_chunk_lock);
3776
3777 /*
3778 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003779 * space is actually useable. For raid56, the space info used
3780 * doesn't include the parity drive, so we don't have to
3781 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003782 */
3783 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3784 BTRFS_BLOCK_GROUP_RAID1 |
3785 BTRFS_BLOCK_GROUP_RAID10))
3786 avail >>= 1;
3787
Josef Bacik70afa392013-02-06 13:53:19 -05003788 to_add = space_info->total_bytes;
3789
Josef Bacika80c8dc2012-09-06 16:59:33 -04003790 /*
Miao Xie561c2942012-10-16 11:32:18 +00003791 * If we aren't flushing all things, let us overcommit up to
3792 * 1/2th of the space. If we can flush, don't let us overcommit
3793 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003794 */
Miao Xie08e007d2012-10-16 11:33:38 +00003795 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik70afa392013-02-06 13:53:19 -05003796 to_add >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003797 else
Josef Bacik70afa392013-02-06 13:53:19 -05003798 to_add >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003799
Josef Bacik70afa392013-02-06 13:53:19 -05003800 /*
3801 * Limit the overcommit to the amount of free space we could possibly
3802 * allocate for chunks.
3803 */
3804 to_add = min(avail, to_add);
3805
3806 if (used + bytes < space_info->total_bytes + to_add)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003807 return 1;
3808 return 0;
3809}
3810
Miao Xieda633a42012-12-20 11:19:09 +00003811static inline int writeback_inodes_sb_nr_if_idle_safe(struct super_block *sb,
3812 unsigned long nr_pages,
3813 enum wb_reason reason)
Josef Bacik1135d6d2012-12-14 13:46:43 -05003814{
Miao Xieda633a42012-12-20 11:19:09 +00003815 /* the flusher is dealing with the dirty inodes now. */
3816 if (writeback_in_progress(sb->s_bdi))
3817 return 1;
3818
3819 if (down_read_trylock(&sb->s_umount)) {
Josef Bacik1135d6d2012-12-14 13:46:43 -05003820 writeback_inodes_sb_nr(sb, nr_pages, reason);
3821 up_read(&sb->s_umount);
3822 return 1;
3823 }
3824
3825 return 0;
3826}
3827
Miao Xieda633a42012-12-20 11:19:09 +00003828void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3829 unsigned long nr_pages)
3830{
3831 struct super_block *sb = root->fs_info->sb;
3832 int started;
3833
3834 /* If we can not start writeback, just sync all the delalloc file. */
3835 started = writeback_inodes_sb_nr_if_idle_safe(sb, nr_pages,
3836 WB_REASON_FS_FREE_SPACE);
3837 if (!started) {
3838 /*
3839 * We needn't worry the filesystem going from r/w to r/o though
3840 * we don't acquire ->s_umount mutex, because the filesystem
3841 * should guarantee the delalloc inodes list be empty after
3842 * the filesystem is readonly(all dirty pages are written to
3843 * the disk).
3844 */
3845 btrfs_start_delalloc_inodes(root, 0);
3846 btrfs_wait_ordered_extents(root, 0);
3847 }
3848}
3849
Yan, Zheng5da9d012010-05-16 10:46:25 -04003850/*
3851 * shrink metadata reservation for delalloc
3852 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003853static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3854 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003855{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003856 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003857 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003858 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003859 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003860 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003861 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003862 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003863 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003864 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003865
Josef Bacik663350a2011-11-03 22:54:25 -04003866 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003867 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003868 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003869
3870 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003871 delalloc_bytes = percpu_counter_sum_positive(
3872 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003873 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003874 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003875 return;
Liu Bo6bbe3a92012-09-14 02:58:07 -06003876 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003877 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003878 }
3879
Josef Bacikf4c738c2012-07-02 17:10:51 -04003880 while (delalloc_bytes && loops < 3) {
3881 max_reclaim = min(delalloc_bytes, to_reclaim);
3882 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00003883 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04003884 /*
3885 * We need to wait for the async pages to actually start before
3886 * we do anything.
3887 */
3888 wait_event(root->fs_info->async_submit_wait,
3889 !atomic_read(&root->fs_info->async_delalloc_pages));
3890
Miao Xie08e007d2012-10-16 11:33:38 +00003891 if (!trans)
3892 flush = BTRFS_RESERVE_FLUSH_ALL;
3893 else
3894 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003895 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003896 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003897 spin_unlock(&space_info->lock);
3898 break;
3899 }
Josef Bacik0019f102010-10-15 15:18:40 -04003900 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003901
Chris Mason36e39c42011-03-12 07:08:42 -05003902 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04003903 if (wait_ordered && !trans) {
Liu Bo6bbe3a92012-09-14 02:58:07 -06003904 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04003905 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003906 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04003907 if (time_left)
3908 break;
3909 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04003910 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003911 delalloc_bytes = percpu_counter_sum_positive(
3912 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003913 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04003914}
3915
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003916/**
Josef Bacik663350a2011-11-03 22:54:25 -04003917 * maybe_commit_transaction - possibly commit the transaction if its ok to
3918 * @root - the root we're allocating for
3919 * @bytes - the number of bytes we want to reserve
3920 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003921 *
Josef Bacik663350a2011-11-03 22:54:25 -04003922 * This will check to make sure that committing the transaction will actually
3923 * get us somewhere and then commit the transaction if it does. Otherwise it
3924 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003925 */
Josef Bacik663350a2011-11-03 22:54:25 -04003926static int may_commit_transaction(struct btrfs_root *root,
3927 struct btrfs_space_info *space_info,
3928 u64 bytes, int force)
3929{
3930 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
3931 struct btrfs_trans_handle *trans;
3932
3933 trans = (struct btrfs_trans_handle *)current->journal_info;
3934 if (trans)
3935 return -EAGAIN;
3936
3937 if (force)
3938 goto commit;
3939
3940 /* See if there is enough pinned space to make this reservation */
3941 spin_lock(&space_info->lock);
3942 if (space_info->bytes_pinned >= bytes) {
3943 spin_unlock(&space_info->lock);
3944 goto commit;
3945 }
3946 spin_unlock(&space_info->lock);
3947
3948 /*
3949 * See if there is some space in the delayed insertion reservation for
3950 * this reservation.
3951 */
3952 if (space_info != delayed_rsv->space_info)
3953 return -ENOSPC;
3954
Liu Bod9b02182012-02-16 18:34:39 +08003955 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003956 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003957 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04003958 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003959 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003960 return -ENOSPC;
3961 }
3962 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003963 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003964
3965commit:
3966 trans = btrfs_join_transaction(root);
3967 if (IS_ERR(trans))
3968 return -ENOSPC;
3969
3970 return btrfs_commit_transaction(trans, root);
3971}
3972
Josef Bacik96c3f432012-06-21 14:05:49 -04003973enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04003974 FLUSH_DELAYED_ITEMS_NR = 1,
3975 FLUSH_DELAYED_ITEMS = 2,
3976 FLUSH_DELALLOC = 3,
3977 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04003978 ALLOC_CHUNK = 5,
3979 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04003980};
3981
3982static int flush_space(struct btrfs_root *root,
3983 struct btrfs_space_info *space_info, u64 num_bytes,
3984 u64 orig_bytes, int state)
3985{
3986 struct btrfs_trans_handle *trans;
3987 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003988 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04003989
3990 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04003991 case FLUSH_DELAYED_ITEMS_NR:
3992 case FLUSH_DELAYED_ITEMS:
3993 if (state == FLUSH_DELAYED_ITEMS_NR) {
3994 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
3995
3996 nr = (int)div64_u64(num_bytes, bytes);
3997 if (!nr)
3998 nr = 1;
3999 nr *= 2;
4000 } else {
4001 nr = -1;
4002 }
4003 trans = btrfs_join_transaction(root);
4004 if (IS_ERR(trans)) {
4005 ret = PTR_ERR(trans);
4006 break;
4007 }
4008 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4009 btrfs_end_transaction(trans, root);
4010 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004011 case FLUSH_DELALLOC:
4012 case FLUSH_DELALLOC_WAIT:
4013 shrink_delalloc(root, num_bytes, orig_bytes,
4014 state == FLUSH_DELALLOC_WAIT);
4015 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004016 case ALLOC_CHUNK:
4017 trans = btrfs_join_transaction(root);
4018 if (IS_ERR(trans)) {
4019 ret = PTR_ERR(trans);
4020 break;
4021 }
4022 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004023 btrfs_get_alloc_profile(root, 0),
4024 CHUNK_ALLOC_NO_FORCE);
4025 btrfs_end_transaction(trans, root);
4026 if (ret == -ENOSPC)
4027 ret = 0;
4028 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004029 case COMMIT_TRANS:
4030 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4031 break;
4032 default:
4033 ret = -ENOSPC;
4034 break;
4035 }
4036
4037 return ret;
4038}
Josef Bacik663350a2011-11-03 22:54:25 -04004039/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004040 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4041 * @root - the root we're allocating for
4042 * @block_rsv - the block_rsv we're allocating for
4043 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004044 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004045 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004046 * This will reserve orgi_bytes number of bytes from the space info associated
4047 * with the block_rsv. If there is not enough space it will make an attempt to
4048 * flush out space to make room. It will do this by flushing delalloc if
4049 * possible or committing the transaction. If flush is 0 then no attempts to
4050 * regain reservations will be made and this will fail if there is not enough
4051 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004052 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004053static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004054 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004055 u64 orig_bytes,
4056 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004057{
4058 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004059 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004060 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004061 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004062 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004063 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004064
4065again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004066 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004067 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004068 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004069 * We only want to wait if somebody other than us is flushing and we
4070 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004071 */
Miao Xie08e007d2012-10-16 11:33:38 +00004072 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4073 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004074 spin_unlock(&space_info->lock);
4075 /*
4076 * If we have a trans handle we can't wait because the flusher
4077 * may have to commit the transaction, which would mean we would
4078 * deadlock since we are waiting for the flusher to finish, but
4079 * hold the current transaction open.
4080 */
Josef Bacik663350a2011-11-03 22:54:25 -04004081 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004082 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004083 ret = wait_event_killable(space_info->wait, !space_info->flush);
4084 /* Must have been killed, return */
4085 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004086 return -EINTR;
4087
4088 spin_lock(&space_info->lock);
4089 }
4090
4091 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004092 used = space_info->bytes_used + space_info->bytes_reserved +
4093 space_info->bytes_pinned + space_info->bytes_readonly +
4094 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004095
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004096 /*
4097 * The idea here is that we've not already over-reserved the block group
4098 * then we can go ahead and save our reservation first and then start
4099 * flushing if we need to. Otherwise if we've already overcommitted
4100 * lets start flushing stuff first and then come back and try to make
4101 * our reservation.
4102 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004103 if (used <= space_info->total_bytes) {
4104 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004105 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004106 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004107 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004108 ret = 0;
4109 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004110 /*
4111 * Ok set num_bytes to orig_bytes since we aren't
4112 * overocmmitted, this way we only try and reclaim what
4113 * we need.
4114 */
4115 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004116 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004117 } else {
4118 /*
4119 * Ok we're over committed, set num_bytes to the overcommitted
4120 * amount plus the amount of bytes that we need for this
4121 * reservation.
4122 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004123 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004124 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004125 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004126
Josef Bacik44734ed2012-09-28 16:04:19 -04004127 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4128 space_info->bytes_may_use += orig_bytes;
4129 trace_btrfs_space_reservation(root->fs_info, "space_info",
4130 space_info->flags, orig_bytes,
4131 1);
4132 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004133 }
4134
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004135 /*
4136 * Couldn't make our reservation, save our place so while we're trying
4137 * to reclaim space we can actually use it instead of somebody else
4138 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004139 *
4140 * We make the other tasks wait for the flush only when we can flush
4141 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004142 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004143 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004144 flushing = true;
4145 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004146 }
4147
Yan, Zhengf0486c62010-05-16 10:46:25 -04004148 spin_unlock(&space_info->lock);
4149
Miao Xie08e007d2012-10-16 11:33:38 +00004150 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004151 goto out;
4152
Josef Bacik96c3f432012-06-21 14:05:49 -04004153 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4154 flush_state);
4155 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004156
4157 /*
4158 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4159 * would happen. So skip delalloc flush.
4160 */
4161 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4162 (flush_state == FLUSH_DELALLOC ||
4163 flush_state == FLUSH_DELALLOC_WAIT))
4164 flush_state = ALLOC_CHUNK;
4165
Josef Bacik96c3f432012-06-21 14:05:49 -04004166 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004167 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004168 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4169 flush_state < COMMIT_TRANS)
4170 goto again;
4171 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4172 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004173 goto again;
4174
4175out:
Josef Bacik5d803662013-02-07 16:06:02 -05004176 if (ret == -ENOSPC &&
4177 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4178 struct btrfs_block_rsv *global_rsv =
4179 &root->fs_info->global_block_rsv;
4180
4181 if (block_rsv != global_rsv &&
4182 !block_rsv_use_bytes(global_rsv, orig_bytes))
4183 ret = 0;
4184 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004185 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004186 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004187 space_info->flush = 0;
4188 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004189 spin_unlock(&space_info->lock);
4190 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004191 return ret;
4192}
4193
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004194static struct btrfs_block_rsv *get_block_rsv(
4195 const struct btrfs_trans_handle *trans,
4196 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004197{
Josef Bacik4c13d752011-08-30 11:31:29 -04004198 struct btrfs_block_rsv *block_rsv = NULL;
4199
Josef Bacik0e721102012-06-26 16:13:18 -04004200 if (root->ref_cows)
4201 block_rsv = trans->block_rsv;
4202
4203 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004204 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004205
4206 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004207 block_rsv = root->block_rsv;
4208
4209 if (!block_rsv)
4210 block_rsv = &root->fs_info->empty_block_rsv;
4211
4212 return block_rsv;
4213}
4214
4215static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4216 u64 num_bytes)
4217{
4218 int ret = -ENOSPC;
4219 spin_lock(&block_rsv->lock);
4220 if (block_rsv->reserved >= num_bytes) {
4221 block_rsv->reserved -= num_bytes;
4222 if (block_rsv->reserved < block_rsv->size)
4223 block_rsv->full = 0;
4224 ret = 0;
4225 }
4226 spin_unlock(&block_rsv->lock);
4227 return ret;
4228}
4229
4230static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4231 u64 num_bytes, int update_size)
4232{
4233 spin_lock(&block_rsv->lock);
4234 block_rsv->reserved += num_bytes;
4235 if (update_size)
4236 block_rsv->size += num_bytes;
4237 else if (block_rsv->reserved >= block_rsv->size)
4238 block_rsv->full = 1;
4239 spin_unlock(&block_rsv->lock);
4240}
4241
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004242static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4243 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004244 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004245{
4246 struct btrfs_space_info *space_info = block_rsv->space_info;
4247
4248 spin_lock(&block_rsv->lock);
4249 if (num_bytes == (u64)-1)
4250 num_bytes = block_rsv->size;
4251 block_rsv->size -= num_bytes;
4252 if (block_rsv->reserved >= block_rsv->size) {
4253 num_bytes = block_rsv->reserved - block_rsv->size;
4254 block_rsv->reserved = block_rsv->size;
4255 block_rsv->full = 1;
4256 } else {
4257 num_bytes = 0;
4258 }
4259 spin_unlock(&block_rsv->lock);
4260
4261 if (num_bytes > 0) {
4262 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004263 spin_lock(&dest->lock);
4264 if (!dest->full) {
4265 u64 bytes_to_add;
4266
4267 bytes_to_add = dest->size - dest->reserved;
4268 bytes_to_add = min(num_bytes, bytes_to_add);
4269 dest->reserved += bytes_to_add;
4270 if (dest->reserved >= dest->size)
4271 dest->full = 1;
4272 num_bytes -= bytes_to_add;
4273 }
4274 spin_unlock(&dest->lock);
4275 }
4276 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004277 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004278 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004279 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004280 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004281 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004282 spin_unlock(&space_info->lock);
4283 }
4284 }
4285}
4286
4287static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4288 struct btrfs_block_rsv *dst, u64 num_bytes)
4289{
4290 int ret;
4291
4292 ret = block_rsv_use_bytes(src, num_bytes);
4293 if (ret)
4294 return ret;
4295
4296 block_rsv_add_bytes(dst, num_bytes, 1);
4297 return 0;
4298}
4299
Miao Xie66d8f3d2012-09-06 04:02:28 -06004300void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004301{
4302 memset(rsv, 0, sizeof(*rsv));
4303 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004304 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004305}
4306
Miao Xie66d8f3d2012-09-06 04:02:28 -06004307struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4308 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004309{
4310 struct btrfs_block_rsv *block_rsv;
4311 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004312
4313 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4314 if (!block_rsv)
4315 return NULL;
4316
Miao Xie66d8f3d2012-09-06 04:02:28 -06004317 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004318 block_rsv->space_info = __find_space_info(fs_info,
4319 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004320 return block_rsv;
4321}
4322
4323void btrfs_free_block_rsv(struct btrfs_root *root,
4324 struct btrfs_block_rsv *rsv)
4325{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004326 if (!rsv)
4327 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004328 btrfs_block_rsv_release(root, rsv, (u64)-1);
4329 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004330}
4331
Miao Xie08e007d2012-10-16 11:33:38 +00004332int btrfs_block_rsv_add(struct btrfs_root *root,
4333 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4334 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004335{
4336 int ret;
4337
4338 if (num_bytes == 0)
4339 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004340
Miao Xie61b520a2011-11-10 20:45:05 -05004341 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004342 if (!ret) {
4343 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4344 return 0;
4345 }
4346
Yan, Zhengf0486c62010-05-16 10:46:25 -04004347 return ret;
4348}
4349
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004350int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004351 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004352{
4353 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004354 int ret = -ENOSPC;
4355
4356 if (!block_rsv)
4357 return 0;
4358
4359 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004360 num_bytes = div_factor(block_rsv->size, min_factor);
4361 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004362 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004363 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004364
Josef Bacik36ba0222011-10-18 12:15:48 -04004365 return ret;
4366}
4367
Miao Xie08e007d2012-10-16 11:33:38 +00004368int btrfs_block_rsv_refill(struct btrfs_root *root,
4369 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4370 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004371{
4372 u64 num_bytes = 0;
4373 int ret = -ENOSPC;
4374
4375 if (!block_rsv)
4376 return 0;
4377
4378 spin_lock(&block_rsv->lock);
4379 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004380 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004381 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004382 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004383 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004384 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004385
Yan, Zhengf0486c62010-05-16 10:46:25 -04004386 if (!ret)
4387 return 0;
4388
Miao Xieaa38a712011-11-18 17:43:00 +08004389 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004390 if (!ret) {
4391 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004392 return 0;
4393 }
4394
Josef Bacik13553e52011-08-08 13:33:21 -04004395 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004396}
4397
4398int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4399 struct btrfs_block_rsv *dst_rsv,
4400 u64 num_bytes)
4401{
4402 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4403}
4404
4405void btrfs_block_rsv_release(struct btrfs_root *root,
4406 struct btrfs_block_rsv *block_rsv,
4407 u64 num_bytes)
4408{
4409 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4410 if (global_rsv->full || global_rsv == block_rsv ||
4411 block_rsv->space_info != global_rsv->space_info)
4412 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004413 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4414 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004415}
4416
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004417/*
4418 * helper to calculate size of global block reservation.
4419 * the desired value is sum of space used by extent tree,
4420 * checksum tree and root tree
4421 */
4422static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4423{
4424 struct btrfs_space_info *sinfo;
4425 u64 num_bytes;
4426 u64 meta_used;
4427 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004428 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004429
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004430 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4431 spin_lock(&sinfo->lock);
4432 data_used = sinfo->bytes_used;
4433 spin_unlock(&sinfo->lock);
4434
4435 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4436 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004437 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4438 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004439 meta_used = sinfo->bytes_used;
4440 spin_unlock(&sinfo->lock);
4441
4442 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4443 csum_size * 2;
4444 num_bytes += div64_u64(data_used + meta_used, 50);
4445
4446 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004447 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004448
4449 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4450}
4451
4452static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4453{
4454 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4455 struct btrfs_space_info *sinfo = block_rsv->space_info;
4456 u64 num_bytes;
4457
4458 num_bytes = calc_global_metadata_size(fs_info);
4459
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004460 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004461 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004462
Josef Bacikfdf30d12013-03-26 15:31:45 -04004463 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004464
4465 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004466 sinfo->bytes_reserved + sinfo->bytes_readonly +
4467 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004468
4469 if (sinfo->total_bytes > num_bytes) {
4470 num_bytes = sinfo->total_bytes - num_bytes;
4471 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004472 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004473 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004474 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004475 }
4476
4477 if (block_rsv->reserved >= block_rsv->size) {
4478 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004479 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004480 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004481 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004482 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004483 block_rsv->reserved = block_rsv->size;
4484 block_rsv->full = 1;
4485 }
David Sterba182608c2011-05-05 13:13:16 +02004486
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004487 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004488 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004489}
4490
Yan, Zhengf0486c62010-05-16 10:46:25 -04004491static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4492{
4493 struct btrfs_space_info *space_info;
4494
4495 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4496 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004497
4498 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004499 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004500 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004501 fs_info->trans_block_rsv.space_info = space_info;
4502 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004503 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004504
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004505 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4506 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4507 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4508 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004509 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004510
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004511 update_global_block_rsv(fs_info);
4512}
4513
4514static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4515{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004516 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4517 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004518 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4519 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4520 WARN_ON(fs_info->trans_block_rsv.size > 0);
4521 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4522 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4523 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004524 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4525 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004526}
4527
Yan, Zhenga22285a2010-05-16 10:48:46 -04004528void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4529 struct btrfs_root *root)
4530{
Josef Bacik0e721102012-06-26 16:13:18 -04004531 if (!trans->block_rsv)
4532 return;
4533
Yan, Zhenga22285a2010-05-16 10:48:46 -04004534 if (!trans->bytes_reserved)
4535 return;
4536
Chris Masone77266e2012-02-24 10:39:05 -05004537 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004538 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004539 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004540 trans->bytes_reserved = 0;
4541}
4542
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004543/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004544int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4545 struct inode *inode)
4546{
4547 struct btrfs_root *root = BTRFS_I(inode)->root;
4548 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4549 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4550
4551 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004552 * We need to hold space in order to delete our orphan item once we've
4553 * added it, so this takes the reservation so we can release it later
4554 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004555 */
Chris Masonff5714c2011-05-28 07:00:39 -04004556 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004557 trace_btrfs_space_reservation(root->fs_info, "orphan",
4558 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004559 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4560}
4561
4562void btrfs_orphan_release_metadata(struct inode *inode)
4563{
4564 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004565 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004566 trace_btrfs_space_reservation(root->fs_info, "orphan",
4567 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004568 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4569}
4570
Miao Xied5c12072013-02-28 10:04:33 +00004571/*
4572 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4573 * root: the root of the parent directory
4574 * rsv: block reservation
4575 * items: the number of items that we need do reservation
4576 * qgroup_reserved: used to return the reserved size in qgroup
4577 *
4578 * This function is used to reserve the space for snapshot/subvolume
4579 * creation and deletion. Those operations are different with the
4580 * common file/directory operations, they change two fs/file trees
4581 * and root tree, the number of items that the qgroup reserves is
4582 * different with the free space reservation. So we can not use
4583 * the space reseravtion mechanism in start_transaction().
4584 */
4585int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4586 struct btrfs_block_rsv *rsv,
4587 int items,
4588 u64 *qgroup_reserved)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004589{
Miao Xied5c12072013-02-28 10:04:33 +00004590 u64 num_bytes;
4591 int ret;
4592
4593 if (root->fs_info->quota_enabled) {
4594 /* One for parent inode, two for dir entries */
4595 num_bytes = 3 * root->leafsize;
4596 ret = btrfs_qgroup_reserve(root, num_bytes);
4597 if (ret)
4598 return ret;
4599 } else {
4600 num_bytes = 0;
4601 }
4602
4603 *qgroup_reserved = num_bytes;
4604
4605 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4606 rsv->space_info = __find_space_info(root->fs_info,
4607 BTRFS_BLOCK_GROUP_METADATA);
4608 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4609 BTRFS_RESERVE_FLUSH_ALL);
4610 if (ret) {
4611 if (*qgroup_reserved)
4612 btrfs_qgroup_free(root, *qgroup_reserved);
4613 }
4614
4615 return ret;
4616}
4617
4618void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4619 struct btrfs_block_rsv *rsv,
4620 u64 qgroup_reserved)
4621{
4622 btrfs_block_rsv_release(root, rsv, (u64)-1);
4623 if (qgroup_reserved)
4624 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004625}
4626
Josef Bacik7709cde2011-08-04 10:25:02 -04004627/**
4628 * drop_outstanding_extent - drop an outstanding extent
4629 * @inode: the inode we're dropping the extent for
4630 *
4631 * This is called when we are freeing up an outstanding extent, either called
4632 * after an error or after an extent is written. This will return the number of
4633 * reserved extents that need to be freed. This must be called with
4634 * BTRFS_I(inode)->lock held.
4635 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004636static unsigned drop_outstanding_extent(struct inode *inode)
4637{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004638 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004639 unsigned dropped_extents = 0;
4640
Josef Bacik9e0baf62011-07-15 15:16:44 +00004641 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4642 BTRFS_I(inode)->outstanding_extents--;
4643
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004644 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004645 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4646 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004647 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004648
Josef Bacik9e0baf62011-07-15 15:16:44 +00004649 /*
4650 * If we have more or the same amount of outsanding extents than we have
4651 * reserved then we need to leave the reserved extents count alone.
4652 */
4653 if (BTRFS_I(inode)->outstanding_extents >=
4654 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004655 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004656
4657 dropped_extents = BTRFS_I(inode)->reserved_extents -
4658 BTRFS_I(inode)->outstanding_extents;
4659 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004660 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004661}
4662
Josef Bacik7709cde2011-08-04 10:25:02 -04004663/**
4664 * calc_csum_metadata_size - return the amount of metada space that must be
4665 * reserved/free'd for the given bytes.
4666 * @inode: the inode we're manipulating
4667 * @num_bytes: the number of bytes in question
4668 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4669 *
4670 * This adjusts the number of csum_bytes in the inode and then returns the
4671 * correct amount of metadata that must either be reserved or freed. We
4672 * calculate how many checksums we can fit into one leaf and then divide the
4673 * number of bytes that will need to be checksumed by this value to figure out
4674 * how many checksums will be required. If we are adding bytes then the number
4675 * may go up and we will return the number of additional bytes that must be
4676 * reserved. If it is going down we will return the number of bytes that must
4677 * be freed.
4678 *
4679 * This must be called with BTRFS_I(inode)->lock held.
4680 */
4681static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4682 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004683{
Josef Bacik7709cde2011-08-04 10:25:02 -04004684 struct btrfs_root *root = BTRFS_I(inode)->root;
4685 u64 csum_size;
4686 int num_csums_per_leaf;
4687 int num_csums;
4688 int old_csums;
4689
4690 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4691 BTRFS_I(inode)->csum_bytes == 0)
4692 return 0;
4693
4694 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4695 if (reserve)
4696 BTRFS_I(inode)->csum_bytes += num_bytes;
4697 else
4698 BTRFS_I(inode)->csum_bytes -= num_bytes;
4699 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4700 num_csums_per_leaf = (int)div64_u64(csum_size,
4701 sizeof(struct btrfs_csum_item) +
4702 sizeof(struct btrfs_disk_key));
4703 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4704 num_csums = num_csums + num_csums_per_leaf - 1;
4705 num_csums = num_csums / num_csums_per_leaf;
4706
4707 old_csums = old_csums + num_csums_per_leaf - 1;
4708 old_csums = old_csums / num_csums_per_leaf;
4709
4710 /* No change, no need to reserve more */
4711 if (old_csums == num_csums)
4712 return 0;
4713
4714 if (reserve)
4715 return btrfs_calc_trans_metadata_size(root,
4716 num_csums - old_csums);
4717
4718 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004719}
4720
4721int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4722{
4723 struct btrfs_root *root = BTRFS_I(inode)->root;
4724 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004725 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004726 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004727 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004728 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004729 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004730 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004731 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004732 u64 to_free = 0;
4733 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004734
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004735 /* If we are a free space inode we need to not flush since we will be in
4736 * the middle of a transaction commit. We also don't need the delalloc
4737 * mutex since we won't race with anybody. We need this mostly to make
4738 * lockdep shut its filthy mouth.
4739 */
4740 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004741 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004742 delalloc_lock = false;
4743 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004744
Miao Xie08e007d2012-10-16 11:33:38 +00004745 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4746 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004747 schedule_timeout(1);
4748
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004749 if (delalloc_lock)
4750 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4751
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004752 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004753
Josef Bacik9e0baf62011-07-15 15:16:44 +00004754 spin_lock(&BTRFS_I(inode)->lock);
4755 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004756
Josef Bacik9e0baf62011-07-15 15:16:44 +00004757 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004758 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004759 nr_extents = BTRFS_I(inode)->outstanding_extents -
4760 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004761
4762 /*
4763 * Add an item to reserve for updating the inode when we complete the
4764 * delalloc io.
4765 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004766 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4767 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004768 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004769 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004770 }
4771
4772 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004773 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004774 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004775 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004776
Wang Shilong88e081bf2013-03-01 11:36:01 +00004777 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02004778 ret = btrfs_qgroup_reserve(root, num_bytes +
4779 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004780 if (ret)
4781 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00004782 }
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004783
Wang Shilong88e081bf2013-03-01 11:36:01 +00004784 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
4785 if (unlikely(ret)) {
4786 if (root->fs_info->quota_enabled)
4787 btrfs_qgroup_free(root, num_bytes +
4788 nr_extents * root->leafsize);
4789 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004790 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004791
Josef Bacik660d3f62011-12-09 11:18:51 -05004792 spin_lock(&BTRFS_I(inode)->lock);
4793 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004794 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4795 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004796 nr_extents--;
4797 }
4798 BTRFS_I(inode)->reserved_extents += nr_extents;
4799 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004800
4801 if (delalloc_lock)
4802 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004803
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004804 if (to_reserve)
4805 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4806 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004807 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4808
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004809 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004810
4811out_fail:
4812 spin_lock(&BTRFS_I(inode)->lock);
4813 dropped = drop_outstanding_extent(inode);
4814 /*
4815 * If the inodes csum_bytes is the same as the original
4816 * csum_bytes then we know we haven't raced with any free()ers
4817 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00004818 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04004819 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00004820 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04004821 } else {
4822 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
4823 u64 bytes;
4824
4825 /*
4826 * This is tricky, but first we need to figure out how much we
4827 * free'd from any free-ers that occured during this
4828 * reservation, so we reset ->csum_bytes to the csum_bytes
4829 * before we dropped our lock, and then call the free for the
4830 * number of bytes that were freed while we were trying our
4831 * reservation.
4832 */
4833 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
4834 BTRFS_I(inode)->csum_bytes = csum_bytes;
4835 to_free = calc_csum_metadata_size(inode, bytes, 0);
4836
4837
4838 /*
4839 * Now we need to see how much we would have freed had we not
4840 * been making this reservation and our ->csum_bytes were not
4841 * artificially inflated.
4842 */
4843 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
4844 bytes = csum_bytes - orig_csum_bytes;
4845 bytes = calc_csum_metadata_size(inode, bytes, 0);
4846
4847 /*
4848 * Now reset ->csum_bytes to what it should be. If bytes is
4849 * more than to_free then we would have free'd more space had we
4850 * not had an artificially high ->csum_bytes, so we need to free
4851 * the remainder. If bytes is the same or less then we don't
4852 * need to do anything, the other free-ers did the correct
4853 * thing.
4854 */
4855 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
4856 if (bytes > to_free)
4857 to_free = bytes - to_free;
4858 else
4859 to_free = 0;
4860 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00004861 spin_unlock(&BTRFS_I(inode)->lock);
4862 if (dropped)
4863 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4864
4865 if (to_free) {
4866 btrfs_block_rsv_release(root, block_rsv, to_free);
4867 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4868 btrfs_ino(inode), to_free, 0);
4869 }
4870 if (delalloc_lock)
4871 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
4872 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004873}
4874
Josef Bacik7709cde2011-08-04 10:25:02 -04004875/**
4876 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4877 * @inode: the inode to release the reservation for
4878 * @num_bytes: the number of bytes we're releasing
4879 *
4880 * This will release the metadata reservation for an inode. This can be called
4881 * once we complete IO for a given set of bytes to release their metadata
4882 * reservations.
4883 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004884void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4885{
4886 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004887 u64 to_free = 0;
4888 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004889
4890 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004891 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004892 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004893
Miao Xie09348562013-02-07 10:12:07 +00004894 if (num_bytes)
4895 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04004896 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004897 if (dropped > 0)
4898 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004899
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004900 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4901 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02004902 if (root->fs_info->quota_enabled) {
4903 btrfs_qgroup_free(root, num_bytes +
4904 dropped * root->leafsize);
4905 }
4906
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004907 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4908 to_free);
4909}
4910
Josef Bacik7709cde2011-08-04 10:25:02 -04004911/**
4912 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4913 * @inode: inode we're writing to
4914 * @num_bytes: the number of bytes we want to allocate
4915 *
4916 * This will do the following things
4917 *
4918 * o reserve space in the data space info for num_bytes
4919 * o reserve space in the metadata space info based on number of outstanding
4920 * extents and how much csums will be needed
4921 * o add to the inodes ->delalloc_bytes
4922 * o add it to the fs_info's delalloc inodes list.
4923 *
4924 * This will return 0 for success and -ENOSPC if there is no space left.
4925 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004926int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4927{
4928 int ret;
4929
4930 ret = btrfs_check_data_free_space(inode, num_bytes);
4931 if (ret)
4932 return ret;
4933
4934 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4935 if (ret) {
4936 btrfs_free_reserved_data_space(inode, num_bytes);
4937 return ret;
4938 }
4939
4940 return 0;
4941}
4942
Josef Bacik7709cde2011-08-04 10:25:02 -04004943/**
4944 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4945 * @inode: inode we're releasing space for
4946 * @num_bytes: the number of bytes we want to free up
4947 *
4948 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4949 * called in the case that we don't need the metadata AND data reservations
4950 * anymore. So if there is an error or we insert an inline extent.
4951 *
4952 * This function will release the metadata space that was not used and will
4953 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4954 * list if there are no delalloc bytes left.
4955 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004956void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4957{
4958 btrfs_delalloc_release_metadata(inode, num_bytes);
4959 btrfs_free_reserved_data_space(inode, num_bytes);
4960}
4961
Liu Boc53d6132012-12-27 09:01:19 +00004962static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004963 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004964{
Josef Bacik0af3d002010-06-21 14:48:16 -04004965 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004966 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004967 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004968 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004969 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004970 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004971
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004972 /* block accounting for super block */
4973 spin_lock(&info->delalloc_lock);
David Sterba6c417612011-04-13 15:41:04 +02004974 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004975 if (alloc)
4976 old_val += num_bytes;
4977 else
4978 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02004979 btrfs_set_super_bytes_used(info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004980 spin_unlock(&info->delalloc_lock);
4981
Chris Masond3977122009-01-05 21:25:51 -05004982 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004983 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004984 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004985 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004986 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4987 BTRFS_BLOCK_GROUP_RAID1 |
4988 BTRFS_BLOCK_GROUP_RAID10))
4989 factor = 2;
4990 else
4991 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004992 /*
4993 * If this block group has free space cache written out, we
4994 * need to make sure to load it if we are removing space. This
4995 * is because we need the unpinning stage to actually add the
4996 * space back to the block group, otherwise we will leak space.
4997 */
4998 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00004999 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005000
Chris Masondb945352007-10-15 16:15:53 -04005001 byte_in_group = bytenr - cache->key.objectid;
5002 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005003
Josef Bacik25179202008-10-29 14:49:05 -04005004 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005005 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005006
Josef Bacik73bc1872011-10-03 14:07:49 -04005007 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005008 cache->disk_cache_state < BTRFS_DC_CLEAR)
5009 cache->disk_cache_state = BTRFS_DC_CLEAR;
5010
Josef Bacik0f9dd462008-09-23 13:14:11 -04005011 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005012 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005013 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005014 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005015 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005016 btrfs_set_block_group_used(&cache->item, old_val);
5017 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005018 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005019 cache->space_info->bytes_used += num_bytes;
5020 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005021 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005022 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005023 } else {
Chris Masondb945352007-10-15 16:15:53 -04005024 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005025 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005026 cache->pinned += num_bytes;
5027 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005028 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005029 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005030 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005031 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005032
Yan, Zhengf0486c62010-05-16 10:46:25 -04005033 set_extent_dirty(info->pinned_extents,
5034 bytenr, bytenr + num_bytes - 1,
5035 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005036 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005037 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005038 total -= num_bytes;
5039 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005040 }
5041 return 0;
5042}
Chris Mason6324fbf2008-03-24 15:01:59 -04005043
Chris Masona061fc82008-05-07 11:43:44 -04005044static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5045{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005046 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005047 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005048
Liu Boa1897fd2012-12-27 09:01:23 +00005049 spin_lock(&root->fs_info->block_group_cache_lock);
5050 bytenr = root->fs_info->first_logical_byte;
5051 spin_unlock(&root->fs_info->block_group_cache_lock);
5052
5053 if (bytenr < (u64)-1)
5054 return bytenr;
5055
Josef Bacik0f9dd462008-09-23 13:14:11 -04005056 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5057 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005058 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005059
Yan Zhengd2fb3432008-12-11 16:30:39 -05005060 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005061 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005062
5063 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005064}
5065
Yan, Zhengf0486c62010-05-16 10:46:25 -04005066static int pin_down_extent(struct btrfs_root *root,
5067 struct btrfs_block_group_cache *cache,
5068 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005069{
Yan Zheng11833d62009-09-11 16:11:19 -04005070 spin_lock(&cache->space_info->lock);
5071 spin_lock(&cache->lock);
5072 cache->pinned += num_bytes;
5073 cache->space_info->bytes_pinned += num_bytes;
5074 if (reserved) {
5075 cache->reserved -= num_bytes;
5076 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005077 }
Yan Zheng11833d62009-09-11 16:11:19 -04005078 spin_unlock(&cache->lock);
5079 spin_unlock(&cache->space_info->lock);
5080
Yan, Zhengf0486c62010-05-16 10:46:25 -04005081 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5082 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05005083 return 0;
5084}
Chris Mason9078a3e2007-04-26 16:46:15 -04005085
Yan, Zhengf0486c62010-05-16 10:46:25 -04005086/*
5087 * this function must be called within transaction
5088 */
5089int btrfs_pin_extent(struct btrfs_root *root,
5090 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005091{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005092 struct btrfs_block_group_cache *cache;
5093
5094 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005095 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005096
5097 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5098
5099 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005100 return 0;
5101}
Zheng Yane8569812008-09-26 10:05:48 -04005102
Yan, Zhengf0486c62010-05-16 10:46:25 -04005103/*
Chris Masone688b7252011-10-31 20:52:39 -04005104 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005105 */
Liu Bodcfac412012-12-27 09:01:20 +00005106int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04005107 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005108{
Chris Masone688b7252011-10-31 20:52:39 -04005109 struct btrfs_block_group_cache *cache;
5110
5111 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005112 BUG_ON(!cache); /* Logic error */
Chris Masone688b7252011-10-31 20:52:39 -04005113
5114 /*
5115 * pull in the free space cache (if any) so that our pin
5116 * removes the free space from the cache. We have load_only set
5117 * to one because the slow code to read in the free extents does check
5118 * the pinned extents.
5119 */
Liu Bof6373bf2012-12-27 09:01:18 +00005120 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04005121
5122 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5123
5124 /* remove us from the free space cache (if we're there at all) */
5125 btrfs_remove_free_space(cache, bytenr, num_bytes);
5126 btrfs_put_block_group(cache);
5127 return 0;
5128}
5129
Josef Bacikfb25e912011-07-26 17:00:46 -04005130/**
5131 * btrfs_update_reserved_bytes - update the block_group and space info counters
5132 * @cache: The cache we are manipulating
5133 * @num_bytes: The number of bytes in question
5134 * @reserve: One of the reservation enums
5135 *
5136 * This is called by the allocator when it reserves space, or by somebody who is
5137 * freeing space that was never actually used on disk. For example if you
5138 * reserve some space for a new leaf in transaction A and before transaction A
5139 * commits you free that leaf, you call this with reserve set to 0 in order to
5140 * clear the reservation.
5141 *
5142 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5143 * ENOSPC accounting. For data we handle the reservation through clearing the
5144 * delalloc bits in the io_tree. We have to do this since we could end up
5145 * allocating less disk space for the amount of data we have reserved in the
5146 * case of compression.
5147 *
5148 * If this is a reservation and the block group has become read only we cannot
5149 * make the reservation and return -EAGAIN, otherwise this function always
5150 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005151 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005152static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5153 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005154{
Josef Bacikfb25e912011-07-26 17:00:46 -04005155 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005156 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005157
Josef Bacikfb25e912011-07-26 17:00:46 -04005158 spin_lock(&space_info->lock);
5159 spin_lock(&cache->lock);
5160 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005161 if (cache->ro) {
5162 ret = -EAGAIN;
5163 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005164 cache->reserved += num_bytes;
5165 space_info->bytes_reserved += num_bytes;
5166 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005167 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005168 "space_info", space_info->flags,
5169 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005170 space_info->bytes_may_use -= num_bytes;
5171 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005172 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005173 } else {
5174 if (cache->ro)
5175 space_info->bytes_readonly += num_bytes;
5176 cache->reserved -= num_bytes;
5177 space_info->bytes_reserved -= num_bytes;
5178 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005179 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005180 spin_unlock(&cache->lock);
5181 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005182 return ret;
5183}
5184
Jeff Mahoney143bede2012-03-01 14:56:26 +01005185void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005186 struct btrfs_root *root)
5187{
5188 struct btrfs_fs_info *fs_info = root->fs_info;
5189 struct btrfs_caching_control *next;
5190 struct btrfs_caching_control *caching_ctl;
5191 struct btrfs_block_group_cache *cache;
5192
5193 down_write(&fs_info->extent_commit_sem);
5194
5195 list_for_each_entry_safe(caching_ctl, next,
5196 &fs_info->caching_block_groups, list) {
5197 cache = caching_ctl->block_group;
5198 if (block_group_cache_done(cache)) {
5199 cache->last_byte_to_unpin = (u64)-1;
5200 list_del_init(&caching_ctl->list);
5201 put_caching_control(caching_ctl);
5202 } else {
5203 cache->last_byte_to_unpin = caching_ctl->progress;
5204 }
5205 }
5206
5207 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5208 fs_info->pinned_extents = &fs_info->freed_extents[1];
5209 else
5210 fs_info->pinned_extents = &fs_info->freed_extents[0];
5211
5212 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005213
5214 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005215}
5216
5217static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5218{
5219 struct btrfs_fs_info *fs_info = root->fs_info;
5220 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005221 struct btrfs_space_info *space_info;
5222 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005223 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005224 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005225
5226 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005227 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005228 if (!cache ||
5229 start >= cache->key.objectid + cache->key.offset) {
5230 if (cache)
5231 btrfs_put_block_group(cache);
5232 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005233 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005234 }
5235
5236 len = cache->key.objectid + cache->key.offset - start;
5237 len = min(len, end + 1 - start);
5238
5239 if (start < cache->last_byte_to_unpin) {
5240 len = min(len, cache->last_byte_to_unpin - start);
5241 btrfs_add_free_space(cache, start, len);
5242 }
Josef Bacik25179202008-10-29 14:49:05 -04005243
Yan, Zhengf0486c62010-05-16 10:46:25 -04005244 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005245 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005246
Josef Bacik7b398f82012-10-22 15:52:28 -04005247 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005248 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005249 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005250 space_info->bytes_pinned -= len;
5251 if (cache->ro) {
5252 space_info->bytes_readonly += len;
5253 readonly = true;
5254 }
Josef Bacik25179202008-10-29 14:49:05 -04005255 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005256 if (!readonly && global_rsv->space_info == space_info) {
5257 spin_lock(&global_rsv->lock);
5258 if (!global_rsv->full) {
5259 len = min(len, global_rsv->size -
5260 global_rsv->reserved);
5261 global_rsv->reserved += len;
5262 space_info->bytes_may_use += len;
5263 if (global_rsv->reserved >= global_rsv->size)
5264 global_rsv->full = 1;
5265 }
5266 spin_unlock(&global_rsv->lock);
5267 }
5268 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005269 }
5270
5271 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005272 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005273 return 0;
5274}
5275
5276int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005277 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005278{
Yan Zheng11833d62009-09-11 16:11:19 -04005279 struct btrfs_fs_info *fs_info = root->fs_info;
5280 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005281 u64 start;
5282 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005283 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005284
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005285 if (trans->aborted)
5286 return 0;
5287
Yan Zheng11833d62009-09-11 16:11:19 -04005288 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5289 unpin = &fs_info->freed_extents[1];
5290 else
5291 unpin = &fs_info->freed_extents[0];
5292
Chris Masond3977122009-01-05 21:25:51 -05005293 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005294 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005295 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005296 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005297 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005298
Li Dongyang5378e602011-03-24 10:24:27 +00005299 if (btrfs_test_opt(root, DISCARD))
5300 ret = btrfs_discard_extent(root, start,
5301 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005302
Chris Mason1a5bc162007-10-15 16:15:26 -04005303 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005304 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005305 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005306 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005307
Chris Masone20d96d2007-03-22 12:13:20 -04005308 return 0;
5309}
5310
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005311static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5312 struct btrfs_root *root,
5313 u64 bytenr, u64 num_bytes, u64 parent,
5314 u64 root_objectid, u64 owner_objectid,
5315 u64 owner_offset, int refs_to_drop,
5316 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005317{
Chris Masone2fa7222007-03-12 16:22:34 -04005318 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005319 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005320 struct btrfs_fs_info *info = root->fs_info;
5321 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005322 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005323 struct btrfs_extent_item *ei;
5324 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005325 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005326 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005327 int extent_slot = 0;
5328 int found_extent = 0;
5329 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005330 u32 item_size;
5331 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05005332
Chris Mason5caf2a02007-04-02 11:20:42 -04005333 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005334 if (!path)
5335 return -ENOMEM;
5336
Chris Mason3c12ac72008-04-21 12:01:38 -04005337 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005338 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005339
5340 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5341 BUG_ON(!is_data && refs_to_drop != 1);
5342
5343 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5344 bytenr, num_bytes, parent,
5345 root_objectid, owner_objectid,
5346 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005347 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005348 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005349 while (extent_slot >= 0) {
5350 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005351 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005352 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005353 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005354 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5355 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005356 found_extent = 1;
5357 break;
5358 }
5359 if (path->slots[0] - extent_slot > 5)
5360 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005361 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005362 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005363#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5364 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5365 if (found_extent && item_size < sizeof(*ei))
5366 found_extent = 0;
5367#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005368 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005369 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005370 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005371 NULL, refs_to_drop,
5372 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005373 if (ret) {
5374 btrfs_abort_transaction(trans, extent_root, ret);
5375 goto out;
5376 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005377 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005378 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005379
5380 key.objectid = bytenr;
5381 key.type = BTRFS_EXTENT_ITEM_KEY;
5382 key.offset = num_bytes;
5383
Zheng Yan31840ae2008-09-23 13:14:14 -04005384 ret = btrfs_search_slot(trans, extent_root,
5385 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005386 if (ret) {
5387 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05005388 ", was looking for %llu\n", ret,
5389 (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005390 if (ret > 0)
5391 btrfs_print_leaf(extent_root,
5392 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005393 }
David Sterba005d6422012-09-18 07:52:32 -06005394 if (ret < 0) {
5395 btrfs_abort_transaction(trans, extent_root, ret);
5396 goto out;
5397 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005398 extent_slot = path->slots[0];
5399 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005400 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005401 btrfs_print_leaf(extent_root, path->nodes[0]);
5402 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05005403 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005404 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05005405 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04005406 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05005407 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005408 (unsigned long long)owner_objectid,
5409 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005410 } else {
David Sterba005d6422012-09-18 07:52:32 -06005411 btrfs_abort_transaction(trans, extent_root, ret);
5412 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005413 }
Chris Mason5f39d392007-10-15 16:14:19 -04005414
5415 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005416 item_size = btrfs_item_size_nr(leaf, extent_slot);
5417#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5418 if (item_size < sizeof(*ei)) {
5419 BUG_ON(found_extent || extent_slot != path->slots[0]);
5420 ret = convert_extent_item_v0(trans, extent_root, path,
5421 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005422 if (ret < 0) {
5423 btrfs_abort_transaction(trans, extent_root, ret);
5424 goto out;
5425 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005426
David Sterbab3b4aa72011-04-21 01:20:15 +02005427 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005428 path->leave_spinning = 1;
5429
5430 key.objectid = bytenr;
5431 key.type = BTRFS_EXTENT_ITEM_KEY;
5432 key.offset = num_bytes;
5433
5434 ret = btrfs_search_slot(trans, extent_root, &key, path,
5435 -1, 1);
5436 if (ret) {
5437 printk(KERN_ERR "umm, got %d back from search"
5438 ", was looking for %llu\n", ret,
5439 (unsigned long long)bytenr);
5440 btrfs_print_leaf(extent_root, path->nodes[0]);
5441 }
David Sterba005d6422012-09-18 07:52:32 -06005442 if (ret < 0) {
5443 btrfs_abort_transaction(trans, extent_root, ret);
5444 goto out;
5445 }
5446
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005447 extent_slot = path->slots[0];
5448 leaf = path->nodes[0];
5449 item_size = btrfs_item_size_nr(leaf, extent_slot);
5450 }
5451#endif
5452 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005453 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005454 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005455 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5456 struct btrfs_tree_block_info *bi;
5457 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5458 bi = (struct btrfs_tree_block_info *)(ei + 1);
5459 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005460 }
5461
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005462 refs = btrfs_extent_refs(leaf, ei);
5463 BUG_ON(refs < refs_to_drop);
5464 refs -= refs_to_drop;
5465
5466 if (refs > 0) {
5467 if (extent_op)
5468 __run_delayed_extent_op(extent_op, leaf, ei);
5469 /*
5470 * In the case of inline back ref, reference count will
5471 * be updated by remove_extent_backref
5472 */
5473 if (iref) {
5474 BUG_ON(!found_extent);
5475 } else {
5476 btrfs_set_extent_refs(leaf, ei, refs);
5477 btrfs_mark_buffer_dirty(leaf);
5478 }
5479 if (found_extent) {
5480 ret = remove_extent_backref(trans, extent_root, path,
5481 iref, refs_to_drop,
5482 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005483 if (ret) {
5484 btrfs_abort_transaction(trans, extent_root, ret);
5485 goto out;
5486 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005487 }
5488 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005489 if (found_extent) {
5490 BUG_ON(is_data && refs_to_drop !=
5491 extent_data_ref_count(root, path, iref));
5492 if (iref) {
5493 BUG_ON(path->slots[0] != extent_slot);
5494 } else {
5495 BUG_ON(path->slots[0] != extent_slot + 1);
5496 path->slots[0] = extent_slot;
5497 num_to_del = 2;
5498 }
Chris Mason78fae272007-03-25 11:35:08 -04005499 }
Chris Masonb9473432009-03-13 11:00:37 -04005500
Chris Mason952fcca2008-02-18 16:33:44 -05005501 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5502 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005503 if (ret) {
5504 btrfs_abort_transaction(trans, extent_root, ret);
5505 goto out;
5506 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005507 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005509 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005510 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005511 if (ret) {
5512 btrfs_abort_transaction(trans, extent_root, ret);
5513 goto out;
5514 }
Chris Mason459931e2008-12-10 09:10:46 -05005515 }
5516
Liu Boc53d6132012-12-27 09:01:19 +00005517 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005518 if (ret) {
5519 btrfs_abort_transaction(trans, extent_root, ret);
5520 goto out;
5521 }
Chris Masona28ec192007-03-06 20:08:01 -05005522 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005523out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005524 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005525 return ret;
5526}
5527
5528/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005529 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005530 * delayed ref for that extent as well. This searches the delayed ref tree for
5531 * a given extent, and if there are no other delayed refs to be processed, it
5532 * removes it from the tree.
5533 */
5534static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5535 struct btrfs_root *root, u64 bytenr)
5536{
5537 struct btrfs_delayed_ref_head *head;
5538 struct btrfs_delayed_ref_root *delayed_refs;
5539 struct btrfs_delayed_ref_node *ref;
5540 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005541 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005542
5543 delayed_refs = &trans->transaction->delayed_refs;
5544 spin_lock(&delayed_refs->lock);
5545 head = btrfs_find_delayed_ref_head(trans, bytenr);
5546 if (!head)
5547 goto out;
5548
5549 node = rb_prev(&head->node.rb_node);
5550 if (!node)
5551 goto out;
5552
5553 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5554
5555 /* there are still entries for this ref, we can't drop it */
5556 if (ref->bytenr == bytenr)
5557 goto out;
5558
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 if (head->extent_op) {
5560 if (!head->must_insert_reserved)
5561 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005562 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005563 head->extent_op = NULL;
5564 }
5565
Chris Mason1887be62009-03-13 10:11:24 -04005566 /*
5567 * waiting for the lock here would deadlock. If someone else has it
5568 * locked they are already in the process of dropping it anyway
5569 */
5570 if (!mutex_trylock(&head->mutex))
5571 goto out;
5572
5573 /*
5574 * at this point we have a head with no other entries. Go
5575 * ahead and process it.
5576 */
5577 head->node.in_tree = 0;
5578 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005579
Chris Mason1887be62009-03-13 10:11:24 -04005580 delayed_refs->num_entries--;
5581
5582 /*
5583 * we don't take a ref on the node because we're removing it from the
5584 * tree, so we just steal the ref the tree was holding.
5585 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005586 delayed_refs->num_heads--;
5587 if (list_empty(&head->cluster))
5588 delayed_refs->num_heads_ready--;
5589
5590 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005591 spin_unlock(&delayed_refs->lock);
5592
Yan, Zhengf0486c62010-05-16 10:46:25 -04005593 BUG_ON(head->extent_op);
5594 if (head->must_insert_reserved)
5595 ret = 1;
5596
5597 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005598 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005599 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005600out:
5601 spin_unlock(&delayed_refs->lock);
5602 return 0;
5603}
5604
Yan, Zhengf0486c62010-05-16 10:46:25 -04005605void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5606 struct btrfs_root *root,
5607 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005608 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005609{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005610 struct btrfs_block_group_cache *cache = NULL;
5611 int ret;
5612
5613 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005614 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5615 buf->start, buf->len,
5616 parent, root->root_key.objectid,
5617 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005618 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005619 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005620 }
5621
5622 if (!last_ref)
5623 return;
5624
Yan, Zhengf0486c62010-05-16 10:46:25 -04005625 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005626
5627 if (btrfs_header_generation(buf) == trans->transid) {
5628 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5629 ret = check_ref_cleanup(trans, root, buf->start);
5630 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005631 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005632 }
5633
5634 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5635 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005636 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005637 }
5638
5639 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5640
5641 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005642 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005643 }
5644out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005645 /*
5646 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5647 * anymore.
5648 */
5649 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005650 btrfs_put_block_group(cache);
5651}
5652
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005653/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005654int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5655 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5656 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005657{
5658 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005659 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005660
Chris Mason56bec292009-03-13 10:10:06 -04005661 /*
5662 * tree log blocks never actually go into the extent allocation
5663 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005664 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005665 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5666 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005667 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005668 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005669 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005670 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005671 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5672 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005673 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005674 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005675 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005676 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5677 num_bytes,
5678 parent, root_objectid, owner,
5679 offset, BTRFS_DROP_DELAYED_REF,
5680 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005681 }
Chris Mason925baed2008-06-25 16:01:30 -04005682 return ret;
5683}
5684
David Woodhouse53b381b2013-01-29 18:40:14 -05005685static u64 stripe_align(struct btrfs_root *root,
5686 struct btrfs_block_group_cache *cache,
5687 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05005688{
Qu Wenruofda28322013-02-26 08:10:22 +00005689 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05005690 return ret;
5691}
5692
Chris Masonfec577f2007-02-26 10:40:21 -05005693/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005694 * when we wait for progress in the block group caching, its because
5695 * our allocation attempt failed at least once. So, we must sleep
5696 * and let some progress happen before we try again.
5697 *
5698 * This function will sleep at least once waiting for new free space to
5699 * show up, and then it will check the block group free space numbers
5700 * for our min num_bytes. Another option is to have it go ahead
5701 * and look in the rbtree for a free extent of a given size, but this
5702 * is a good start.
5703 */
5704static noinline int
5705wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5706 u64 num_bytes)
5707{
Yan Zheng11833d62009-09-11 16:11:19 -04005708 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005709
Yan Zheng11833d62009-09-11 16:11:19 -04005710 caching_ctl = get_caching_control(cache);
5711 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005712 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005713
Yan Zheng11833d62009-09-11 16:11:19 -04005714 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08005715 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005716
5717 put_caching_control(caching_ctl);
5718 return 0;
5719}
5720
5721static noinline int
5722wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5723{
5724 struct btrfs_caching_control *caching_ctl;
Yan Zheng11833d62009-09-11 16:11:19 -04005725
5726 caching_ctl = get_caching_control(cache);
5727 if (!caching_ctl)
5728 return 0;
5729
5730 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5731
5732 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005733 return 0;
5734}
5735
Liu Bo31e50222012-11-21 14:18:10 +00005736int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005737{
Ilya Dryomov7738a532012-03-27 17:09:17 +03005738 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00005739 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005740 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00005741 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005742 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00005743 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005744 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00005745 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005746 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05005747 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05005748 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05005749 return BTRFS_RAID_RAID6;
5750
5751 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04005752}
5753
Ilya Dryomov7738a532012-03-27 17:09:17 +03005754static int get_block_group_index(struct btrfs_block_group_cache *cache)
5755{
Liu Bo31e50222012-11-21 14:18:10 +00005756 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03005757}
5758
Josef Bacik817d52f2009-07-13 21:29:25 -04005759enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005760 LOOP_CACHING_NOWAIT = 0,
5761 LOOP_CACHING_WAIT = 1,
5762 LOOP_ALLOC_CHUNK = 2,
5763 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005764};
5765
5766/*
Chris Masonfec577f2007-02-26 10:40:21 -05005767 * walks the btree of allocated extents and find a hole of a given size.
5768 * The key ins is changed to record the hole:
5769 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005770 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005771 * ins->offset == number of blocks
5772 * Any available blocks before search_start are skipped.
5773 */
Chris Masond3977122009-01-05 21:25:51 -05005774static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005775 struct btrfs_root *orig_root,
5776 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005777 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00005778 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005779{
Josef Bacik80eb2342008-10-29 14:49:05 -04005780 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005781 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005782 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005783 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005784 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005785 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005786 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005787 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005788 int loop = 0;
Liu Boac5c9302012-12-27 09:01:24 +00005789 int index = __get_raid_index(data);
Josef Bacikfb25e912011-07-26 17:00:46 -04005790 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5791 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005792 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005793 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005794 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005795 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08005796 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005797
Chris Masondb945352007-10-15 16:15:53 -04005798 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005799 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005800 ins->objectid = 0;
5801 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005802
Josef Bacik3f7de032011-11-10 08:29:20 -05005803 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5804
Josef Bacik2552d172009-04-03 10:14:19 -04005805 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005806 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00005807 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005808 return -ENOSPC;
5809 }
Josef Bacik2552d172009-04-03 10:14:19 -04005810
Josef Bacik67377732010-09-16 16:19:09 -04005811 /*
5812 * If the space info is for both data and metadata it means we have a
5813 * small filesystem and we can't use the clustering stuff.
5814 */
5815 if (btrfs_mixed_space_info(space_info))
5816 use_cluster = false;
5817
Josef Bacik67377732010-09-16 16:19:09 -04005818 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005819 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005820 if (!btrfs_test_opt(root, SSD))
5821 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005822 }
5823
Josef Bacik67377732010-09-16 16:19:09 -04005824 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5825 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005826 last_ptr = &root->fs_info->data_alloc_cluster;
5827 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005828
Chris Mason239b14b2008-03-24 15:02:07 -04005829 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005830 spin_lock(&last_ptr->lock);
5831 if (last_ptr->block_group)
5832 hint_byte = last_ptr->window_start;
5833 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005834 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005835
Chris Masona061fc82008-05-07 11:43:44 -04005836 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005837 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005838
Josef Bacik817d52f2009-07-13 21:29:25 -04005839 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005840 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005841
Josef Bacik2552d172009-04-03 10:14:19 -04005842 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04005843 block_group = btrfs_lookup_block_group(root->fs_info,
5844 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005845 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04005846 /*
5847 * we don't want to use the block group if it doesn't match our
5848 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005849 *
5850 * However if we are re-searching with an ideal block group
5851 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005852 */
5853 if (block_group && block_group_bits(block_group, data) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05005854 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04005855 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005856 if (list_empty(&block_group->list) ||
5857 block_group->ro) {
5858 /*
5859 * someone is removing this block group,
5860 * we can't jump into the have_block_group
5861 * target because our list pointers are not
5862 * valid
5863 */
5864 btrfs_put_block_group(block_group);
5865 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005866 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005867 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005868 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005869 }
Josef Bacik2552d172009-04-03 10:14:19 -04005870 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005871 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005872 }
Chris Mason42e70e72008-11-07 18:17:11 -05005873 }
Josef Bacik2552d172009-04-03 10:14:19 -04005874search:
Miao Xie60d2adb2011-09-09 17:34:35 +08005875 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04005876 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005877 list_for_each_entry(block_group, &space_info->block_groups[index],
5878 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005879 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005880 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005881
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005882 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005883 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005884 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005885
Chris Mason83a50de2010-12-13 15:06:46 -05005886 /*
5887 * this can happen if we end up cycling through all the
5888 * raid types, but we want to make sure we only allocate
5889 * for the proper type.
5890 */
5891 if (!block_group_bits(block_group, data)) {
5892 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5893 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05005894 BTRFS_BLOCK_GROUP_RAID5 |
5895 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05005896 BTRFS_BLOCK_GROUP_RAID10;
5897
5898 /*
5899 * if they asked for extra copies and this block group
5900 * doesn't provide them, bail. This does allow us to
5901 * fill raid0 from raid1.
5902 */
5903 if ((data & extra) && !(block_group->flags & extra))
5904 goto loop;
5905 }
5906
Josef Bacik2552d172009-04-03 10:14:19 -04005907have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05005908 cached = block_group_cache_done(block_group);
5909 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05005910 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00005911 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04005912 BUG_ON(ret < 0);
5913 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05005914 }
5915
Josef Bacikea6a4782008-11-20 12:16:16 -05005916 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005917 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005918
Josef Bacik0a243252009-09-11 16:11:20 -04005919 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005920 * Ok we want to try and use the cluster allocator, so
5921 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04005922 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005923 if (last_ptr) {
Chris Mason8de972b2013-01-04 15:39:43 -05005924 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005925 /*
5926 * the refill lock keeps out other
5927 * people trying to start a new cluster
5928 */
5929 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005930 used_block_group = last_ptr->block_group;
5931 if (used_block_group != block_group &&
5932 (!used_block_group ||
5933 used_block_group->ro ||
5934 !block_group_bits(used_block_group, data))) {
5935 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04005936 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005937 }
Chris Mason44fb5512009-06-04 15:34:51 -04005938
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005939 if (used_block_group != block_group)
5940 btrfs_get_block_group(used_block_group);
5941
5942 offset = btrfs_alloc_from_cluster(used_block_group,
5943 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005944 if (offset) {
5945 /* we have a block, we're done */
5946 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005947 trace_btrfs_reserve_extent_cluster(root,
5948 block_group, search_start, num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005949 goto checks;
5950 }
5951
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005952 WARN_ON(last_ptr->block_group != used_block_group);
5953 if (used_block_group != block_group) {
5954 btrfs_put_block_group(used_block_group);
5955 used_block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005956 }
Chris Mason44fb5512009-06-04 15:34:51 -04005957refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005958 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005959 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
5960 * set up a new clusters, so lets just skip it
5961 * and let the allocator find whatever block
5962 * it can find. If we reach this point, we
5963 * will have tried the cluster allocator
5964 * plenty of times and not have found
5965 * anything, so we are likely way too
5966 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005967 * anything.
5968 *
5969 * However, if the cluster is taken from the
5970 * current block group, release the cluster
5971 * first, so that we stand a better chance of
5972 * succeeding in the unclustered
5973 * allocation. */
5974 if (loop >= LOOP_NO_EMPTY_SIZE &&
5975 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005976 spin_unlock(&last_ptr->refill_lock);
5977 goto unclustered_alloc;
5978 }
5979
Chris Masonfa9c0d792009-04-03 09:47:43 -04005980 /*
5981 * this cluster didn't work out, free it and
5982 * start over
5983 */
5984 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5985
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005986 if (loop >= LOOP_NO_EMPTY_SIZE) {
5987 spin_unlock(&last_ptr->refill_lock);
5988 goto unclustered_alloc;
5989 }
5990
Chris Mason8de972b2013-01-04 15:39:43 -05005991 aligned_cluster = max_t(unsigned long,
5992 empty_cluster + empty_size,
5993 block_group->full_stripe_len);
5994
Chris Masonfa9c0d792009-04-03 09:47:43 -04005995 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005996 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005997 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05005998 search_start, num_bytes,
Chris Mason8de972b2013-01-04 15:39:43 -05005999 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006000 if (ret == 0) {
6001 /*
6002 * now pull our allocation out of this
6003 * cluster
6004 */
6005 offset = btrfs_alloc_from_cluster(block_group,
6006 last_ptr, num_bytes,
6007 search_start);
6008 if (offset) {
6009 /* we found one, proceed */
6010 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006011 trace_btrfs_reserve_extent_cluster(root,
6012 block_group, search_start,
6013 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006014 goto checks;
6015 }
Josef Bacik0a243252009-09-11 16:11:20 -04006016 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6017 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006018 spin_unlock(&last_ptr->refill_lock);
6019
Josef Bacik0a243252009-09-11 16:11:20 -04006020 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006021 wait_block_group_cache_progress(block_group,
6022 num_bytes + empty_cluster + empty_size);
6023 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006024 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006025
Chris Masonfa9c0d792009-04-03 09:47:43 -04006026 /*
6027 * at this point we either didn't find a cluster
6028 * or we weren't able to allocate a block from our
6029 * cluster. Free the cluster we've been trying
6030 * to use, and go to the next block group
6031 */
Josef Bacik0a243252009-09-11 16:11:20 -04006032 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006033 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006034 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006035 }
6036
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006037unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006038 spin_lock(&block_group->free_space_ctl->tree_lock);
6039 if (cached &&
6040 block_group->free_space_ctl->free_space <
6041 num_bytes + empty_cluster + empty_size) {
6042 spin_unlock(&block_group->free_space_ctl->tree_lock);
6043 goto loop;
6044 }
6045 spin_unlock(&block_group->free_space_ctl->tree_lock);
6046
Josef Bacik6226cb02009-04-03 10:14:18 -04006047 offset = btrfs_find_space_for_alloc(block_group, search_start,
6048 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006049 /*
6050 * If we didn't find a chunk, and we haven't failed on this
6051 * block group before, and this block group is in the middle of
6052 * caching and we are ok with waiting, then go ahead and wait
6053 * for progress to be made, and set failed_alloc to true.
6054 *
6055 * If failed_alloc is true then we've already waited on this
6056 * block group once and should move on to the next block group.
6057 */
6058 if (!offset && !failed_alloc && !cached &&
6059 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006060 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006061 num_bytes + empty_size);
6062 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006063 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006064 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006065 if (!cached)
6066 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006067 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006068 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006069checks:
David Woodhouse53b381b2013-01-29 18:40:14 -05006070 search_start = stripe_align(root, used_block_group,
6071 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006072
Josef Bacik2552d172009-04-03 10:14:19 -04006073 /* move on to the next group */
6074 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006075 used_block_group->key.objectid + used_block_group->key.offset) {
6076 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006077 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04006078 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006079
Josef Bacik6226cb02009-04-03 10:14:18 -04006080 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006081 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04006082 search_start - offset);
6083 BUG_ON(offset > search_start);
6084
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006085 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006086 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006087 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006088 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006089 goto loop;
6090 }
Yan Zheng11833d62009-09-11 16:11:19 -04006091
Josef Bacik2552d172009-04-03 10:14:19 -04006092 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006093 ins->objectid = search_start;
6094 ins->offset = num_bytes;
6095
Josef Bacik3f7de032011-11-10 08:29:20 -05006096 trace_btrfs_reserve_extent(orig_root, block_group,
6097 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006098 if (used_block_group != block_group)
6099 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04006100 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006101 break;
6102loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006103 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006104 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006105 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006106 if (used_block_group != block_group)
6107 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006108 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006109 }
6110 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006111
Miao Xie60d2adb2011-09-09 17:34:35 +08006112 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6113 goto search;
6114
Yan, Zhengb742bb82010-05-16 10:46:24 -04006115 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6116 goto search;
6117
Josef Bacik285ff5a2012-01-13 15:27:45 -05006118 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006119 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6120 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006121 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6122 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6123 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6124 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04006125 */
Josef Bacik723bda22011-05-27 16:11:38 -04006126 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006127 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006128 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006129 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik698d0082012-09-12 14:08:47 -04006130 ret = do_chunk_alloc(trans, root, data,
Josef Bacikea658ba2012-09-11 16:57:25 -04006131 CHUNK_ALLOC_FORCE);
6132 /*
6133 * Do not bail out on ENOSPC since we
6134 * can do more things.
6135 */
6136 if (ret < 0 && ret != -ENOSPC) {
6137 btrfs_abort_transaction(trans,
6138 root, ret);
6139 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006140 }
Josef Bacik723bda22011-05-27 16:11:38 -04006141 }
6142
6143 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006144 empty_size = 0;
6145 empty_cluster = 0;
6146 }
Chris Mason42e70e72008-11-07 18:17:11 -05006147
Josef Bacik723bda22011-05-27 16:11:38 -04006148 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006149 } else if (!ins->objectid) {
6150 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006151 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006152 ret = 0;
6153 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006154out:
Chris Mason0b86a832008-03-24 15:01:56 -04006155
Chris Mason0f70abe2007-02-28 16:46:22 -05006156 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006157}
Chris Masonec44a352008-04-28 15:29:52 -04006158
Josef Bacik9ed74f22009-09-11 16:12:44 -04006159static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6160 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006161{
6162 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006163 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006164
Josef Bacik9ed74f22009-09-11 16:12:44 -04006165 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006166 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
6167 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05006168 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04006169 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006170 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05006171 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006172 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6173 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07006174 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006175 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006176 (unsigned long long)info->bytes_pinned,
6177 (unsigned long long)info->bytes_reserved,
6178 (unsigned long long)info->bytes_may_use,
6179 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006180 spin_unlock(&info->lock);
6181
6182 if (!dump_block_groups)
6183 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006184
Josef Bacik80eb2342008-10-29 14:49:05 -04006185 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006186again:
6187 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006188 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006189 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05006190 (unsigned long long)cache->key.objectid,
6191 (unsigned long long)cache->key.offset,
6192 (unsigned long long)btrfs_block_group_used(&cache->item),
6193 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06006194 (unsigned long long)cache->reserved,
6195 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006196 btrfs_dump_free_space(cache, bytes);
6197 spin_unlock(&cache->lock);
6198 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006199 if (++index < BTRFS_NR_RAID_TYPES)
6200 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006201 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006202}
Zheng Yane8569812008-09-26 10:05:48 -04006203
Yan Zheng11833d62009-09-11 16:11:19 -04006204int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6205 struct btrfs_root *root,
6206 u64 num_bytes, u64 min_alloc_size,
6207 u64 empty_size, u64 hint_byte,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006208 struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05006209{
Miao Xie9e622d62012-01-26 15:01:12 -05006210 bool final_tried = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006211 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006212
Josef Bacik6a632092009-02-20 11:00:09 -05006213 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04006214again:
Chris Masondb945352007-10-15 16:15:53 -04006215 WARN_ON(num_bytes < root->sectorsize);
6216 ret = find_free_extent(trans, root, num_bytes, empty_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006217 hint_byte, ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04006218
Miao Xie9e622d62012-01-26 15:01:12 -05006219 if (ret == -ENOSPC) {
6220 if (!final_tried) {
6221 num_bytes = num_bytes >> 1;
Zach Brown24542bf2012-11-16 00:04:43 +00006222 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006223 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006224 if (num_bytes == min_alloc_size)
6225 final_tried = true;
6226 goto again;
6227 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6228 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006229
Miao Xie9e622d62012-01-26 15:01:12 -05006230 sinfo = __find_space_info(root->fs_info, data);
6231 printk(KERN_ERR "btrfs allocation failed flags %llu, "
6232 "wanted %llu\n", (unsigned long long)data,
6233 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006234 if (sinfo)
6235 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006236 }
Chris Mason925baed2008-06-25 16:01:30 -04006237 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006238
liubo1abe9b82011-03-24 11:18:59 +00006239 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6240
Josef Bacik0f9dd462008-09-23 13:14:11 -04006241 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006242}
6243
Chris Masone688b7252011-10-31 20:52:39 -04006244static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6245 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006246{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006247 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006248 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006249
Josef Bacik0f9dd462008-09-23 13:14:11 -04006250 cache = btrfs_lookup_block_group(root->fs_info, start);
6251 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05006252 printk(KERN_ERR "Unable to find block group for %llu\n",
6253 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006254 return -ENOSPC;
6255 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006256
Li Dongyang5378e602011-03-24 10:24:27 +00006257 if (btrfs_test_opt(root, DISCARD))
6258 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006259
Chris Masone688b7252011-10-31 20:52:39 -04006260 if (pin)
6261 pin_down_extent(root, cache, start, len, 1);
6262 else {
6263 btrfs_add_free_space(cache, start, len);
6264 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6265 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006266 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006267
liubo1abe9b82011-03-24 11:18:59 +00006268 trace_btrfs_reserved_extent_free(root, start, len);
6269
Chris Masone6dcd2d2008-07-17 12:53:50 -04006270 return ret;
6271}
6272
Chris Masone688b7252011-10-31 20:52:39 -04006273int btrfs_free_reserved_extent(struct btrfs_root *root,
6274 u64 start, u64 len)
6275{
6276 return __btrfs_free_reserved_extent(root, start, len, 0);
6277}
6278
6279int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6280 u64 start, u64 len)
6281{
6282 return __btrfs_free_reserved_extent(root, start, len, 1);
6283}
6284
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006285static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6286 struct btrfs_root *root,
6287 u64 parent, u64 root_objectid,
6288 u64 flags, u64 owner, u64 offset,
6289 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006290{
6291 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006292 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006293 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006294 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006295 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006296 struct extent_buffer *leaf;
6297 int type;
6298 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006299
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006300 if (parent > 0)
6301 type = BTRFS_SHARED_DATA_REF_KEY;
6302 else
6303 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006304
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006305 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006306
6307 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006308 if (!path)
6309 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006310
Chris Masonb9473432009-03-13 11:00:37 -04006311 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006312 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6313 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006314 if (ret) {
6315 btrfs_free_path(path);
6316 return ret;
6317 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006318
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006319 leaf = path->nodes[0];
6320 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006321 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006322 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6323 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6324 btrfs_set_extent_flags(leaf, extent_item,
6325 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006326
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006327 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6328 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6329 if (parent > 0) {
6330 struct btrfs_shared_data_ref *ref;
6331 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6332 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6333 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6334 } else {
6335 struct btrfs_extent_data_ref *ref;
6336 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6337 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6338 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6339 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6340 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6341 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006342
6343 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006344 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006345
Liu Boc53d6132012-12-27 09:01:19 +00006346 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006347 if (ret) { /* -ENOENT, logic error */
Chris Masond3977122009-01-05 21:25:51 -05006348 printk(KERN_ERR "btrfs update block group failed for %llu "
6349 "%llu\n", (unsigned long long)ins->objectid,
6350 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006351 BUG();
6352 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006353 return ret;
6354}
6355
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006356static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6357 struct btrfs_root *root,
6358 u64 parent, u64 root_objectid,
6359 u64 flags, struct btrfs_disk_key *key,
6360 int level, struct btrfs_key *ins)
6361{
6362 int ret;
6363 struct btrfs_fs_info *fs_info = root->fs_info;
6364 struct btrfs_extent_item *extent_item;
6365 struct btrfs_tree_block_info *block_info;
6366 struct btrfs_extent_inline_ref *iref;
6367 struct btrfs_path *path;
6368 struct extent_buffer *leaf;
6369 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
6370
6371 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006372 if (!path)
6373 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006374
6375 path->leave_spinning = 1;
6376 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6377 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006378 if (ret) {
6379 btrfs_free_path(path);
6380 return ret;
6381 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006382
6383 leaf = path->nodes[0];
6384 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6385 struct btrfs_extent_item);
6386 btrfs_set_extent_refs(leaf, extent_item, 1);
6387 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6388 btrfs_set_extent_flags(leaf, extent_item,
6389 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
6390 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6391
6392 btrfs_set_tree_block_key(leaf, block_info, key);
6393 btrfs_set_tree_block_level(leaf, block_info, level);
6394
6395 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6396 if (parent > 0) {
6397 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6398 btrfs_set_extent_inline_ref_type(leaf, iref,
6399 BTRFS_SHARED_BLOCK_REF_KEY);
6400 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6401 } else {
6402 btrfs_set_extent_inline_ref_type(leaf, iref,
6403 BTRFS_TREE_BLOCK_REF_KEY);
6404 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6405 }
6406
6407 btrfs_mark_buffer_dirty(leaf);
6408 btrfs_free_path(path);
6409
Liu Boc53d6132012-12-27 09:01:19 +00006410 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006411 if (ret) { /* -ENOENT, logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006412 printk(KERN_ERR "btrfs update block group failed for %llu "
6413 "%llu\n", (unsigned long long)ins->objectid,
6414 (unsigned long long)ins->offset);
6415 BUG();
6416 }
6417 return ret;
6418}
6419
6420int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6421 struct btrfs_root *root,
6422 u64 root_objectid, u64 owner,
6423 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006424{
6425 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006426
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006427 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006428
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006429 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6430 ins->offset, 0,
6431 root_objectid, owner, offset,
6432 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006433 return ret;
6434}
Chris Masone02119d2008-09-05 16:13:11 -04006435
6436/*
6437 * this is used by the tree logging recovery code. It records that
6438 * an extent has been allocated and makes sure to clear the free
6439 * space cache bits as well
6440 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006441int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6442 struct btrfs_root *root,
6443 u64 root_objectid, u64 owner, u64 offset,
6444 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006445{
6446 int ret;
6447 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04006448 struct btrfs_caching_control *caching_ctl;
6449 u64 start = ins->objectid;
6450 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04006451
Chris Masone02119d2008-09-05 16:13:11 -04006452 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Liu Bof6373bf2012-12-27 09:01:18 +00006453 cache_block_group(block_group, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04006454 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006455
Yan Zheng11833d62009-09-11 16:11:19 -04006456 if (!caching_ctl) {
6457 BUG_ON(!block_group_cache_done(block_group));
6458 ret = btrfs_remove_free_space(block_group, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006459 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006460 } else {
6461 mutex_lock(&caching_ctl->mutex);
6462
6463 if (start >= caching_ctl->progress) {
6464 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006465 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006466 } else if (start + num_bytes <= caching_ctl->progress) {
6467 ret = btrfs_remove_free_space(block_group,
6468 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006469 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006470 } else {
6471 num_bytes = caching_ctl->progress - start;
6472 ret = btrfs_remove_free_space(block_group,
6473 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006474 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006475
6476 start = caching_ctl->progress;
6477 num_bytes = ins->objectid + ins->offset -
6478 caching_ctl->progress;
6479 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006480 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006481 }
6482
6483 mutex_unlock(&caching_ctl->mutex);
6484 put_caching_control(caching_ctl);
6485 }
6486
Josef Bacikfb25e912011-07-26 17:00:46 -04006487 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6488 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006489 BUG_ON(ret); /* logic error */
Chris Masonfa9c0d792009-04-03 09:47:43 -04006490 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006491 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6492 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04006493 return ret;
6494}
6495
Chris Mason65b51a02008-08-01 15:11:20 -04006496struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6497 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05006498 u64 bytenr, u32 blocksize,
6499 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006500{
6501 struct extent_buffer *buf;
6502
6503 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6504 if (!buf)
6505 return ERR_PTR(-ENOMEM);
6506 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006507 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006508 btrfs_tree_lock(buf);
6509 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006510 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006511
6512 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006513 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006514
Chris Masond0c803c2008-09-11 16:17:57 -04006515 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006516 /*
6517 * we allow two log transactions at a time, use different
6518 * EXENT bit to differentiate dirty pages.
6519 */
6520 if (root->log_transid % 2 == 0)
6521 set_extent_dirty(&root->dirty_log_pages, buf->start,
6522 buf->start + buf->len - 1, GFP_NOFS);
6523 else
6524 set_extent_new(&root->dirty_log_pages, buf->start,
6525 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006526 } else {
6527 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6528 buf->start + buf->len - 1, GFP_NOFS);
6529 }
Chris Mason65b51a02008-08-01 15:11:20 -04006530 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006531 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006532 return buf;
6533}
6534
Yan, Zhengf0486c62010-05-16 10:46:25 -04006535static struct btrfs_block_rsv *
6536use_block_rsv(struct btrfs_trans_handle *trans,
6537 struct btrfs_root *root, u32 blocksize)
6538{
6539 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006540 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006541 int ret;
6542
6543 block_rsv = get_block_rsv(trans, root);
6544
6545 if (block_rsv->size == 0) {
Miao Xie08e007d2012-10-16 11:33:38 +00006546 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6547 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006548 /*
6549 * If we couldn't reserve metadata bytes try and use some from
6550 * the global reserve.
6551 */
6552 if (ret && block_rsv != global_rsv) {
6553 ret = block_rsv_use_bytes(global_rsv, blocksize);
6554 if (!ret)
6555 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006556 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00006557 } else if (ret) {
6558 return ERR_PTR(ret);
6559 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006560 return block_rsv;
6561 }
6562
6563 ret = block_rsv_use_bytes(block_rsv, blocksize);
6564 if (!ret)
6565 return block_rsv;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006566 if (ret && !block_rsv->failfast) {
David Sterbab069e0c2013-02-08 21:28:17 +00006567 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6568 static DEFINE_RATELIMIT_STATE(_rs,
6569 DEFAULT_RATELIMIT_INTERVAL * 10,
6570 /*DEFAULT_RATELIMIT_BURST*/ 1);
6571 if (__ratelimit(&_rs))
6572 WARN(1, KERN_DEBUG
6573 "btrfs: block rsv returned %d\n", ret);
6574 }
Miao Xie08e007d2012-10-16 11:33:38 +00006575 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6576 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006577 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00006578 return block_rsv;
6579 } else if (ret && block_rsv != global_rsv) {
6580 ret = block_rsv_use_bytes(global_rsv, blocksize);
6581 if (!ret)
6582 return global_rsv;
6583 }
6584 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006585
Yan, Zhengf0486c62010-05-16 10:46:25 -04006586 return ERR_PTR(-ENOSPC);
6587}
6588
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006589static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6590 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006591{
6592 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006593 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006594}
6595
Chris Masonfec577f2007-02-26 10:40:21 -05006596/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006597 * finds a free extent and does all the dirty work required for allocation
6598 * returns the key for the extent through ins, and a tree buffer for
6599 * the first block of the extent through buf.
6600 *
Chris Masonfec577f2007-02-26 10:40:21 -05006601 * returns the tree buffer or NULL.
6602 */
Chris Mason5f39d392007-10-15 16:14:19 -04006603struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006604 struct btrfs_root *root, u32 blocksize,
6605 u64 parent, u64 root_objectid,
6606 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006607 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006608{
Chris Masone2fa7222007-03-12 16:22:34 -04006609 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006610 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006611 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006612 u64 flags = 0;
6613 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006614
Yan, Zhengf0486c62010-05-16 10:46:25 -04006615
6616 block_rsv = use_block_rsv(trans, root, blocksize);
6617 if (IS_ERR(block_rsv))
6618 return ERR_CAST(block_rsv);
6619
6620 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006621 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006622 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006623 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006624 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006625 }
Chris Mason55c69072008-01-09 15:55:33 -05006626
Chris Mason4008c042009-02-12 14:09:45 -05006627 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6628 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006629 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006630
6631 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6632 if (parent == 0)
6633 parent = ins.objectid;
6634 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6635 } else
6636 BUG_ON(parent > 0);
6637
6638 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6639 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006640 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006641 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006642 if (key)
6643 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6644 else
6645 memset(&extent_op->key, 0, sizeof(extent_op->key));
6646 extent_op->flags_to_set = flags;
6647 extent_op->update_key = 1;
6648 extent_op->update_flags = 1;
6649 extent_op->is_data = 0;
6650
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006651 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6652 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006653 ins.offset, parent, root_objectid,
6654 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006655 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006656 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006657 }
Chris Masonfec577f2007-02-26 10:40:21 -05006658 return buf;
6659}
Chris Masona28ec192007-03-06 20:08:01 -05006660
Yan Zheng2c47e6052009-06-27 21:07:35 -04006661struct walk_control {
6662 u64 refs[BTRFS_MAX_LEVEL];
6663 u64 flags[BTRFS_MAX_LEVEL];
6664 struct btrfs_key update_progress;
6665 int stage;
6666 int level;
6667 int shared_level;
6668 int update_ref;
6669 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006670 int reada_slot;
6671 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006672 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006673};
6674
6675#define DROP_REFERENCE 1
6676#define UPDATE_BACKREF 2
6677
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006678static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6679 struct btrfs_root *root,
6680 struct walk_control *wc,
6681 struct btrfs_path *path)
6682{
6683 u64 bytenr;
6684 u64 generation;
6685 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006686 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006687 u32 nritems;
6688 u32 blocksize;
6689 struct btrfs_key key;
6690 struct extent_buffer *eb;
6691 int ret;
6692 int slot;
6693 int nread = 0;
6694
6695 if (path->slots[wc->level] < wc->reada_slot) {
6696 wc->reada_count = wc->reada_count * 2 / 3;
6697 wc->reada_count = max(wc->reada_count, 2);
6698 } else {
6699 wc->reada_count = wc->reada_count * 3 / 2;
6700 wc->reada_count = min_t(int, wc->reada_count,
6701 BTRFS_NODEPTRS_PER_BLOCK(root));
6702 }
6703
6704 eb = path->nodes[wc->level];
6705 nritems = btrfs_header_nritems(eb);
6706 blocksize = btrfs_level_size(root, wc->level - 1);
6707
6708 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6709 if (nread >= wc->reada_count)
6710 break;
6711
6712 cond_resched();
6713 bytenr = btrfs_node_blockptr(eb, slot);
6714 generation = btrfs_node_ptr_generation(eb, slot);
6715
6716 if (slot == path->slots[wc->level])
6717 goto reada;
6718
6719 if (wc->stage == UPDATE_BACKREF &&
6720 generation <= root->root_key.offset)
6721 continue;
6722
Yan, Zheng94fcca92009-10-09 09:25:16 -04006723 /* We don't lock the tree block, it's OK to be racy here */
6724 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6725 &refs, &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006726 /* We don't care about errors in readahead. */
6727 if (ret < 0)
6728 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006729 BUG_ON(refs == 0);
6730
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006731 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006732 if (refs == 1)
6733 goto reada;
6734
Yan, Zheng94fcca92009-10-09 09:25:16 -04006735 if (wc->level == 1 &&
6736 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6737 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006738 if (!wc->update_ref ||
6739 generation <= root->root_key.offset)
6740 continue;
6741 btrfs_node_key_to_cpu(eb, &key, slot);
6742 ret = btrfs_comp_cpu_keys(&key,
6743 &wc->update_progress);
6744 if (ret < 0)
6745 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006746 } else {
6747 if (wc->level == 1 &&
6748 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6749 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006750 }
6751reada:
6752 ret = readahead_tree_block(root, bytenr, blocksize,
6753 generation);
6754 if (ret)
6755 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006756 nread++;
6757 }
6758 wc->reada_slot = slot;
6759}
6760
Chris Mason9aca1d52007-03-13 11:09:37 -04006761/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006762 * hepler to process tree block while walking down the tree.
6763 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006764 * when wc->stage == UPDATE_BACKREF, this function updates
6765 * back refs for pointers in the block.
6766 *
6767 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006768 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006769static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6770 struct btrfs_root *root,
6771 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006772 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006773{
6774 int level = wc->level;
6775 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006776 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6777 int ret;
6778
6779 if (wc->stage == UPDATE_BACKREF &&
6780 btrfs_header_owner(eb) != root->root_key.objectid)
6781 return 1;
6782
6783 /*
6784 * when reference count of tree block is 1, it won't increase
6785 * again. once full backref flag is set, we never clear it.
6786 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006787 if (lookup_info &&
6788 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6789 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006790 BUG_ON(!path->locks[level]);
6791 ret = btrfs_lookup_extent_info(trans, root,
6792 eb->start, eb->len,
6793 &wc->refs[level],
6794 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006795 BUG_ON(ret == -ENOMEM);
6796 if (ret)
6797 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006798 BUG_ON(wc->refs[level] == 0);
6799 }
6800
Yan Zheng2c47e6052009-06-27 21:07:35 -04006801 if (wc->stage == DROP_REFERENCE) {
6802 if (wc->refs[level] > 1)
6803 return 1;
6804
6805 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006806 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006807 path->locks[level] = 0;
6808 }
6809 return 0;
6810 }
6811
6812 /* wc->stage == UPDATE_BACKREF */
6813 if (!(wc->flags[level] & flag)) {
6814 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006815 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006816 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006817 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006818 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006819 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6820 eb->len, flag, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006821 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006822 wc->flags[level] |= flag;
6823 }
6824
6825 /*
6826 * the block is shared by multiple trees, so it's not good to
6827 * keep the tree lock
6828 */
6829 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006830 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006831 path->locks[level] = 0;
6832 }
6833 return 0;
6834}
6835
6836/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006837 * hepler to process tree block pointer.
6838 *
6839 * when wc->stage == DROP_REFERENCE, this function checks
6840 * reference count of the block pointed to. if the block
6841 * is shared and we need update back refs for the subtree
6842 * rooted at the block, this function changes wc->stage to
6843 * UPDATE_BACKREF. if the block is shared and there is no
6844 * need to update back, this function drops the reference
6845 * to the block.
6846 *
6847 * NOTE: return value 1 means we should stop walking down.
6848 */
6849static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6850 struct btrfs_root *root,
6851 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006852 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006853{
6854 u64 bytenr;
6855 u64 generation;
6856 u64 parent;
6857 u32 blocksize;
6858 struct btrfs_key key;
6859 struct extent_buffer *next;
6860 int level = wc->level;
6861 int reada = 0;
6862 int ret = 0;
6863
6864 generation = btrfs_node_ptr_generation(path->nodes[level],
6865 path->slots[level]);
6866 /*
6867 * if the lower level block was created before the snapshot
6868 * was created, we know there is no need to update back refs
6869 * for the subtree
6870 */
6871 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006872 generation <= root->root_key.offset) {
6873 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006874 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006875 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006876
6877 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6878 blocksize = btrfs_level_size(root, level - 1);
6879
6880 next = btrfs_find_tree_block(root, bytenr, blocksize);
6881 if (!next) {
6882 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00006883 if (!next)
6884 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006885 reada = 1;
6886 }
6887 btrfs_tree_lock(next);
6888 btrfs_set_lock_blocking(next);
6889
Yan, Zheng94fcca92009-10-09 09:25:16 -04006890 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6891 &wc->refs[level - 1],
6892 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006893 if (ret < 0) {
6894 btrfs_tree_unlock(next);
6895 return ret;
6896 }
6897
Yan, Zheng94fcca92009-10-09 09:25:16 -04006898 BUG_ON(wc->refs[level - 1] == 0);
6899 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006900
Yan, Zheng94fcca92009-10-09 09:25:16 -04006901 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006902 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006903 if (level == 1 &&
6904 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6905 goto skip;
6906
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006907 if (!wc->update_ref ||
6908 generation <= root->root_key.offset)
6909 goto skip;
6910
6911 btrfs_node_key_to_cpu(path->nodes[level], &key,
6912 path->slots[level]);
6913 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6914 if (ret < 0)
6915 goto skip;
6916
6917 wc->stage = UPDATE_BACKREF;
6918 wc->shared_level = level - 1;
6919 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006920 } else {
6921 if (level == 1 &&
6922 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6923 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006924 }
6925
Chris Masonb9fab912012-05-06 07:23:47 -04006926 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006927 btrfs_tree_unlock(next);
6928 free_extent_buffer(next);
6929 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006930 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006931 }
6932
6933 if (!next) {
6934 if (reada && level == 1)
6935 reada_walk_down(trans, root, wc, path);
6936 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006937 if (!next)
6938 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006939 btrfs_tree_lock(next);
6940 btrfs_set_lock_blocking(next);
6941 }
6942
6943 level--;
6944 BUG_ON(level != btrfs_header_level(next));
6945 path->nodes[level] = next;
6946 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006947 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006948 wc->level = level;
6949 if (wc->level == 1)
6950 wc->reada_slot = 0;
6951 return 0;
6952skip:
6953 wc->refs[level - 1] = 0;
6954 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006955 if (wc->stage == DROP_REFERENCE) {
6956 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6957 parent = path->nodes[level]->start;
6958 } else {
6959 BUG_ON(root->root_key.objectid !=
6960 btrfs_header_owner(path->nodes[level]));
6961 parent = 0;
6962 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006963
Yan, Zheng94fcca92009-10-09 09:25:16 -04006964 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006965 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006966 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006967 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006968 btrfs_tree_unlock(next);
6969 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006970 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006971 return 1;
6972}
6973
6974/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006975 * hepler to process tree block while walking up the tree.
6976 *
6977 * when wc->stage == DROP_REFERENCE, this function drops
6978 * reference count on the block.
6979 *
6980 * when wc->stage == UPDATE_BACKREF, this function changes
6981 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6982 * to UPDATE_BACKREF previously while processing the block.
6983 *
6984 * NOTE: return value 1 means we should stop walking up.
6985 */
6986static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6987 struct btrfs_root *root,
6988 struct btrfs_path *path,
6989 struct walk_control *wc)
6990{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006991 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006992 int level = wc->level;
6993 struct extent_buffer *eb = path->nodes[level];
6994 u64 parent = 0;
6995
6996 if (wc->stage == UPDATE_BACKREF) {
6997 BUG_ON(wc->shared_level < level);
6998 if (level < wc->shared_level)
6999 goto out;
7000
Yan Zheng2c47e6052009-06-27 21:07:35 -04007001 ret = find_next_key(path, level + 1, &wc->update_progress);
7002 if (ret > 0)
7003 wc->update_ref = 0;
7004
7005 wc->stage = DROP_REFERENCE;
7006 wc->shared_level = -1;
7007 path->slots[level] = 0;
7008
7009 /*
7010 * check reference count again if the block isn't locked.
7011 * we should start walking down the tree again if reference
7012 * count is one.
7013 */
7014 if (!path->locks[level]) {
7015 BUG_ON(level == 0);
7016 btrfs_tree_lock(eb);
7017 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007018 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007019
7020 ret = btrfs_lookup_extent_info(trans, root,
7021 eb->start, eb->len,
7022 &wc->refs[level],
7023 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007024 if (ret < 0) {
7025 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007026 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007027 return ret;
7028 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007029 BUG_ON(wc->refs[level] == 0);
7030 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007031 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007032 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007033 return 1;
7034 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007035 }
7036 }
7037
7038 /* wc->stage == DROP_REFERENCE */
7039 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7040
7041 if (wc->refs[level] == 1) {
7042 if (level == 0) {
7043 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007044 ret = btrfs_dec_ref(trans, root, eb, 1,
7045 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007046 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007047 ret = btrfs_dec_ref(trans, root, eb, 0,
7048 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007049 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007050 }
7051 /* make block locked assertion in clean_tree_block happy */
7052 if (!path->locks[level] &&
7053 btrfs_header_generation(eb) == trans->transid) {
7054 btrfs_tree_lock(eb);
7055 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007056 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007057 }
7058 clean_tree_block(trans, root, eb);
7059 }
7060
7061 if (eb == root->node) {
7062 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7063 parent = eb->start;
7064 else
7065 BUG_ON(root->root_key.objectid !=
7066 btrfs_header_owner(eb));
7067 } else {
7068 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7069 parent = path->nodes[level + 1]->start;
7070 else
7071 BUG_ON(root->root_key.objectid !=
7072 btrfs_header_owner(path->nodes[level + 1]));
7073 }
7074
Jan Schmidt5581a512012-05-16 17:04:52 +02007075 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007076out:
7077 wc->refs[level] = 0;
7078 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007079 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007080}
7081
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007082static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7083 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007084 struct btrfs_path *path,
7085 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007086{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007087 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007088 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007089 int ret;
7090
Yan Zheng2c47e6052009-06-27 21:07:35 -04007091 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007092 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007093 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007094 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007095
Yan Zheng2c47e6052009-06-27 21:07:35 -04007096 if (level == 0)
7097 break;
7098
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007099 if (path->slots[level] >=
7100 btrfs_header_nritems(path->nodes[level]))
7101 break;
7102
Yan, Zheng94fcca92009-10-09 09:25:16 -04007103 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007104 if (ret > 0) {
7105 path->slots[level]++;
7106 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00007107 } else if (ret < 0)
7108 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007109 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007110 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007111 return 0;
7112}
7113
Chris Masond3977122009-01-05 21:25:51 -05007114static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007115 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007116 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007117 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007118{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007119 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007120 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007121
Yan Zheng2c47e6052009-06-27 21:07:35 -04007122 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7123 while (level < max_level && path->nodes[level]) {
7124 wc->level = level;
7125 if (path->slots[level] + 1 <
7126 btrfs_header_nritems(path->nodes[level])) {
7127 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007128 return 0;
7129 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007130 ret = walk_up_proc(trans, root, path, wc);
7131 if (ret > 0)
7132 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007133
Yan Zheng2c47e6052009-06-27 21:07:35 -04007134 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007135 btrfs_tree_unlock_rw(path->nodes[level],
7136 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007137 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007138 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007139 free_extent_buffer(path->nodes[level]);
7140 path->nodes[level] = NULL;
7141 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007142 }
7143 }
7144 return 1;
7145}
7146
Chris Mason9aca1d52007-03-13 11:09:37 -04007147/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007148 * drop a subvolume tree.
7149 *
7150 * this function traverses the tree freeing any blocks that only
7151 * referenced by the tree.
7152 *
7153 * when a shared tree block is found. this function decreases its
7154 * reference count by one. if update_ref is true, this function
7155 * also make sure backrefs for the shared block and all lower level
7156 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04007157 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007158int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007159 struct btrfs_block_rsv *block_rsv, int update_ref,
7160 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007161{
Chris Mason5caf2a02007-04-02 11:20:42 -04007162 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007163 struct btrfs_trans_handle *trans;
7164 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007165 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007166 struct walk_control *wc;
7167 struct btrfs_key key;
7168 int err = 0;
7169 int ret;
7170 int level;
Chris Mason20524f02007-03-10 06:35:47 -05007171
Chris Mason5caf2a02007-04-02 11:20:42 -04007172 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007173 if (!path) {
7174 err = -ENOMEM;
7175 goto out;
7176 }
Chris Mason20524f02007-03-10 06:35:47 -05007177
Yan Zheng2c47e6052009-06-27 21:07:35 -04007178 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007179 if (!wc) {
7180 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007181 err = -ENOMEM;
7182 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007183 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007184
Yan, Zhenga22285a2010-05-16 10:48:46 -04007185 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007186 if (IS_ERR(trans)) {
7187 err = PTR_ERR(trans);
7188 goto out_free;
7189 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007190
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007191 if (block_rsv)
7192 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007193
Chris Mason9f3a7422007-08-07 15:52:19 -04007194 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007195 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007196 path->nodes[level] = btrfs_lock_root_node(root);
7197 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007198 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007199 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007200 memset(&wc->update_progress, 0,
7201 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007202 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007203 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007204 memcpy(&wc->update_progress, &key,
7205 sizeof(wc->update_progress));
7206
Chris Mason6702ed42007-08-07 16:15:09 -04007207 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007208 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007209 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007210 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7211 path->lowest_level = 0;
7212 if (ret < 0) {
7213 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007214 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007215 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007216 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007217
Chris Mason7d9eb122008-07-08 14:19:17 -04007218 /*
7219 * unlock our path, this is safe because only this
7220 * function is allowed to delete this snapshot
7221 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007222 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007223
Yan Zheng2c47e6052009-06-27 21:07:35 -04007224 level = btrfs_header_level(root->node);
7225 while (1) {
7226 btrfs_tree_lock(path->nodes[level]);
7227 btrfs_set_lock_blocking(path->nodes[level]);
7228
7229 ret = btrfs_lookup_extent_info(trans, root,
7230 path->nodes[level]->start,
7231 path->nodes[level]->len,
7232 &wc->refs[level],
7233 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007234 if (ret < 0) {
7235 err = ret;
7236 goto out_end_trans;
7237 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007238 BUG_ON(wc->refs[level] == 0);
7239
7240 if (level == root_item->drop_level)
7241 break;
7242
7243 btrfs_tree_unlock(path->nodes[level]);
7244 WARN_ON(wc->refs[level] != 1);
7245 level--;
7246 }
7247 }
7248
7249 wc->level = level;
7250 wc->shared_level = -1;
7251 wc->stage = DROP_REFERENCE;
7252 wc->update_ref = update_ref;
7253 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007254 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007255 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007256
7257 while (1) {
7258 ret = walk_down_tree(trans, root, path, wc);
7259 if (ret < 0) {
7260 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007261 break;
7262 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007263
7264 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7265 if (ret < 0) {
7266 err = ret;
7267 break;
7268 }
7269
7270 if (ret > 0) {
7271 BUG_ON(wc->stage != DROP_REFERENCE);
7272 break;
7273 }
7274
7275 if (wc->stage == DROP_REFERENCE) {
7276 level = wc->level;
7277 btrfs_node_key(path->nodes[level],
7278 &root_item->drop_progress,
7279 path->slots[level]);
7280 root_item->drop_level = level;
7281 }
7282
7283 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007284 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007285 ret = btrfs_update_root(trans, tree_root,
7286 &root->root_key,
7287 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007288 if (ret) {
7289 btrfs_abort_transaction(trans, tree_root, ret);
7290 err = ret;
7291 goto out_end_trans;
7292 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007293
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007294 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007295 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007296 if (IS_ERR(trans)) {
7297 err = PTR_ERR(trans);
7298 goto out_free;
7299 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007300 if (block_rsv)
7301 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007302 }
Chris Mason20524f02007-03-10 06:35:47 -05007303 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007304 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007305 if (err)
7306 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007307
7308 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007309 if (ret) {
7310 btrfs_abort_transaction(trans, tree_root, ret);
7311 goto out_end_trans;
7312 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007313
Yan, Zheng76dda932009-09-21 16:00:26 -04007314 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7315 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7316 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007317 if (ret < 0) {
7318 btrfs_abort_transaction(trans, tree_root, ret);
7319 err = ret;
7320 goto out_end_trans;
7321 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007322 /* if we fail to delete the orphan item this time
7323 * around, it'll get picked up the next time.
7324 *
7325 * The most common failure here is just -ENOENT.
7326 */
7327 btrfs_del_orphan_item(trans, tree_root,
7328 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007329 }
7330 }
7331
7332 if (root->in_radix) {
7333 btrfs_free_fs_root(tree_root->fs_info, root);
7334 } else {
7335 free_extent_buffer(root->node);
7336 free_extent_buffer(root->commit_root);
7337 kfree(root);
7338 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007339out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007340 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007341out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007342 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007343 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007344out:
7345 if (err)
7346 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007347 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007348}
Chris Mason9078a3e2007-04-26 16:46:15 -04007349
Yan Zheng2c47e6052009-06-27 21:07:35 -04007350/*
7351 * drop subtree rooted at tree block 'node'.
7352 *
7353 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007354 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007355 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007356int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7357 struct btrfs_root *root,
7358 struct extent_buffer *node,
7359 struct extent_buffer *parent)
7360{
7361 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007362 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007363 int level;
7364 int parent_level;
7365 int ret = 0;
7366 int wret;
7367
Yan Zheng2c47e6052009-06-27 21:07:35 -04007368 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7369
Yan Zhengf82d02d2008-10-29 14:49:05 -04007370 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007371 if (!path)
7372 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007373
Yan Zheng2c47e6052009-06-27 21:07:35 -04007374 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007375 if (!wc) {
7376 btrfs_free_path(path);
7377 return -ENOMEM;
7378 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007379
Chris Masonb9447ef2009-03-09 11:45:38 -04007380 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007381 parent_level = btrfs_header_level(parent);
7382 extent_buffer_get(parent);
7383 path->nodes[parent_level] = parent;
7384 path->slots[parent_level] = btrfs_header_nritems(parent);
7385
Chris Masonb9447ef2009-03-09 11:45:38 -04007386 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007387 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007388 path->nodes[level] = node;
7389 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007390 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007391
7392 wc->refs[parent_level] = 1;
7393 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7394 wc->level = level;
7395 wc->shared_level = -1;
7396 wc->stage = DROP_REFERENCE;
7397 wc->update_ref = 0;
7398 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007399 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007400 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007401
7402 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007403 wret = walk_down_tree(trans, root, path, wc);
7404 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007405 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007406 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007407 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007408
Yan Zheng2c47e6052009-06-27 21:07:35 -04007409 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007410 if (wret < 0)
7411 ret = wret;
7412 if (wret != 0)
7413 break;
7414 }
7415
Yan Zheng2c47e6052009-06-27 21:07:35 -04007416 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007417 btrfs_free_path(path);
7418 return ret;
7419}
7420
Chris Masonec44a352008-04-28 15:29:52 -04007421static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7422{
7423 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007424 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007425
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007426 /*
7427 * if restripe for this chunk_type is on pick target profile and
7428 * return, otherwise do the usual balance
7429 */
7430 stripped = get_restripe_target(root->fs_info, flags);
7431 if (stripped)
7432 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007433
Chris Masoncd02dca2010-12-13 14:56:23 -05007434 /*
7435 * we add in the count of missing devices because we want
7436 * to make sure that any RAID levels on a degraded FS
7437 * continue to be honored.
7438 */
7439 num_devices = root->fs_info->fs_devices->rw_devices +
7440 root->fs_info->fs_devices->missing_devices;
7441
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007442 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007443 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007444 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7445
Chris Masonec44a352008-04-28 15:29:52 -04007446 if (num_devices == 1) {
7447 stripped |= BTRFS_BLOCK_GROUP_DUP;
7448 stripped = flags & ~stripped;
7449
7450 /* turn raid0 into single device chunks */
7451 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7452 return stripped;
7453
7454 /* turn mirroring into duplication */
7455 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7456 BTRFS_BLOCK_GROUP_RAID10))
7457 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007458 } else {
7459 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007460 if (flags & stripped)
7461 return flags;
7462
7463 stripped |= BTRFS_BLOCK_GROUP_DUP;
7464 stripped = flags & ~stripped;
7465
7466 /* switch duplicated blocks with raid1 */
7467 if (flags & BTRFS_BLOCK_GROUP_DUP)
7468 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7469
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007470 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007471 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007472
Chris Masonec44a352008-04-28 15:29:52 -04007473 return flags;
7474}
7475
Miao Xie199c36e2011-07-15 10:34:36 +00007476static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007477{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007478 struct btrfs_space_info *sinfo = cache->space_info;
7479 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007480 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007481 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007482
Chris Masonc286ac42008-07-22 23:06:41 -04007483
Miao Xie199c36e2011-07-15 10:34:36 +00007484 /*
7485 * We need some metadata space and system metadata space for
7486 * allocating chunks in some corner cases until we force to set
7487 * it to be readonly.
7488 */
7489 if ((sinfo->flags &
7490 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7491 !force)
7492 min_allocable_bytes = 1 * 1024 * 1024;
7493 else
7494 min_allocable_bytes = 0;
7495
Yan, Zhengf0486c62010-05-16 10:46:25 -04007496 spin_lock(&sinfo->lock);
7497 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007498
7499 if (cache->ro) {
7500 ret = 0;
7501 goto out;
7502 }
7503
Yan, Zhengf0486c62010-05-16 10:46:25 -04007504 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7505 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007506
Yan, Zhengf0486c62010-05-16 10:46:25 -04007507 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007508 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7509 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007510 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007511 cache->ro = 1;
7512 ret = 0;
7513 }
WuBo61cfea92011-07-26 03:30:11 +00007514out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007515 spin_unlock(&cache->lock);
7516 spin_unlock(&sinfo->lock);
7517 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007518}
7519
Yan, Zhengf0486c62010-05-16 10:46:25 -04007520int btrfs_set_block_group_ro(struct btrfs_root *root,
7521 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007522
7523{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007524 struct btrfs_trans_handle *trans;
7525 u64 alloc_flags;
7526 int ret;
7527
7528 BUG_ON(cache->ro);
7529
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007530 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007531 if (IS_ERR(trans))
7532 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007533
7534 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007535 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007536 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007537 CHUNK_ALLOC_FORCE);
7538 if (ret < 0)
7539 goto out;
7540 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007541
Miao Xie199c36e2011-07-15 10:34:36 +00007542 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007543 if (!ret)
7544 goto out;
7545 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007546 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007547 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007548 if (ret < 0)
7549 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007550 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007551out:
7552 btrfs_end_transaction(trans, root);
7553 return ret;
7554}
7555
Chris Masonc87f08c2011-02-16 13:57:04 -05007556int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7557 struct btrfs_root *root, u64 type)
7558{
7559 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007560 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007561 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007562}
7563
Miao Xie6d07bce2011-01-05 10:07:31 +00007564/*
7565 * helper to account the unused space of all the readonly block group in the
7566 * list. takes mirrors into account.
7567 */
7568static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7569{
7570 struct btrfs_block_group_cache *block_group;
7571 u64 free_bytes = 0;
7572 int factor;
7573
7574 list_for_each_entry(block_group, groups_list, list) {
7575 spin_lock(&block_group->lock);
7576
7577 if (!block_group->ro) {
7578 spin_unlock(&block_group->lock);
7579 continue;
7580 }
7581
7582 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7583 BTRFS_BLOCK_GROUP_RAID10 |
7584 BTRFS_BLOCK_GROUP_DUP))
7585 factor = 2;
7586 else
7587 factor = 1;
7588
7589 free_bytes += (block_group->key.offset -
7590 btrfs_block_group_used(&block_group->item)) *
7591 factor;
7592
7593 spin_unlock(&block_group->lock);
7594 }
7595
7596 return free_bytes;
7597}
7598
7599/*
7600 * helper to account the unused space of all the readonly block group in the
7601 * space_info. takes mirrors into account.
7602 */
7603u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7604{
7605 int i;
7606 u64 free_bytes = 0;
7607
7608 spin_lock(&sinfo->lock);
7609
7610 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7611 if (!list_empty(&sinfo->block_groups[i]))
7612 free_bytes += __btrfs_get_ro_block_group_free_space(
7613 &sinfo->block_groups[i]);
7614
7615 spin_unlock(&sinfo->lock);
7616
7617 return free_bytes;
7618}
7619
Jeff Mahoney143bede2012-03-01 14:56:26 +01007620void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007621 struct btrfs_block_group_cache *cache)
7622{
7623 struct btrfs_space_info *sinfo = cache->space_info;
7624 u64 num_bytes;
7625
7626 BUG_ON(!cache->ro);
7627
7628 spin_lock(&sinfo->lock);
7629 spin_lock(&cache->lock);
7630 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7631 cache->bytes_super - btrfs_block_group_used(&cache->item);
7632 sinfo->bytes_readonly -= num_bytes;
7633 cache->ro = 0;
7634 spin_unlock(&cache->lock);
7635 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007636}
7637
Josef Bacikba1bf482009-09-11 16:11:19 -04007638/*
7639 * checks to see if its even possible to relocate this block group.
7640 *
7641 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7642 * ok to go ahead and try.
7643 */
7644int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007645{
Zheng Yan1a40e232008-09-26 10:09:34 -04007646 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007647 struct btrfs_space_info *space_info;
7648 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7649 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007650 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007651 u64 dev_min = 1;
7652 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007653 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007654 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007655 int full = 0;
7656 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007657
Josef Bacikba1bf482009-09-11 16:11:19 -04007658 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007659
Josef Bacikba1bf482009-09-11 16:11:19 -04007660 /* odd, couldn't find the block group, leave it alone */
7661 if (!block_group)
7662 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007663
liubocdcb7252011-08-03 10:15:25 +00007664 min_free = btrfs_block_group_used(&block_group->item);
7665
Josef Bacikba1bf482009-09-11 16:11:19 -04007666 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007667 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007668 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007669
Josef Bacikba1bf482009-09-11 16:11:19 -04007670 space_info = block_group->space_info;
7671 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007672
Josef Bacikba1bf482009-09-11 16:11:19 -04007673 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007674
Josef Bacikba1bf482009-09-11 16:11:19 -04007675 /*
7676 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007677 * relocate it unless we're able to allocate a new chunk below.
7678 *
7679 * Otherwise, we need to make sure we have room in the space to handle
7680 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007681 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007682 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007683 (space_info->bytes_used + space_info->bytes_reserved +
7684 space_info->bytes_pinned + space_info->bytes_readonly +
7685 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007686 spin_unlock(&space_info->lock);
7687 goto out;
7688 }
7689 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007690
Josef Bacikba1bf482009-09-11 16:11:19 -04007691 /*
7692 * ok we don't have enough space, but maybe we have free space on our
7693 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007694 * alloc devices and guess if we have enough space. if this block
7695 * group is going to be restriped, run checks against the target
7696 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007697 */
7698 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007699
liubocdcb7252011-08-03 10:15:25 +00007700 /*
7701 * index:
7702 * 0: raid10
7703 * 1: raid1
7704 * 2: dup
7705 * 3: raid0
7706 * 4: single
7707 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007708 target = get_restripe_target(root->fs_info, block_group->flags);
7709 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00007710 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007711 } else {
7712 /*
7713 * this is just a balance, so if we were marked as full
7714 * we know there is no space for a new chunk
7715 */
7716 if (full)
7717 goto out;
7718
7719 index = get_block_group_index(block_group);
7720 }
7721
Miao Xiee6ec7162013-01-17 05:38:51 +00007722 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00007723 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007724 /* Divide by 2 */
7725 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007726 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00007727 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00007728 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04007729 /* Multiply by 2 */
7730 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007731 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00007732 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007733 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007734 }
7735
Josef Bacikba1bf482009-09-11 16:11:19 -04007736 mutex_lock(&root->fs_info->chunk_mutex);
7737 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007738 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007739
Josef Bacikba1bf482009-09-11 16:11:19 -04007740 /*
7741 * check to make sure we can actually find a chunk with enough
7742 * space to fit our block group in.
7743 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01007744 if (device->total_bytes > device->bytes_used + min_free &&
7745 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08007746 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007747 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007748 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007749 dev_nr++;
7750
7751 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007752 break;
liubocdcb7252011-08-03 10:15:25 +00007753
Josef Bacikba1bf482009-09-11 16:11:19 -04007754 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007755 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007756 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007757 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007758out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007759 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007760 return ret;
7761}
7762
Christoph Hellwigb2950862008-12-02 09:54:17 -05007763static int find_first_block_group(struct btrfs_root *root,
7764 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007765{
Chris Mason925baed2008-06-25 16:01:30 -04007766 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007767 struct btrfs_key found_key;
7768 struct extent_buffer *leaf;
7769 int slot;
7770
7771 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7772 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007773 goto out;
7774
Chris Masond3977122009-01-05 21:25:51 -05007775 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007776 slot = path->slots[0];
7777 leaf = path->nodes[0];
7778 if (slot >= btrfs_header_nritems(leaf)) {
7779 ret = btrfs_next_leaf(root, path);
7780 if (ret == 0)
7781 continue;
7782 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007783 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007784 break;
7785 }
7786 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7787
7788 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007789 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7790 ret = 0;
7791 goto out;
7792 }
Chris Mason0b86a832008-03-24 15:01:56 -04007793 path->slots[0]++;
7794 }
Chris Mason925baed2008-06-25 16:01:30 -04007795out:
Chris Mason0b86a832008-03-24 15:01:56 -04007796 return ret;
7797}
7798
Josef Bacik0af3d002010-06-21 14:48:16 -04007799void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
7800{
7801 struct btrfs_block_group_cache *block_group;
7802 u64 last = 0;
7803
7804 while (1) {
7805 struct inode *inode;
7806
7807 block_group = btrfs_lookup_first_block_group(info, last);
7808 while (block_group) {
7809 spin_lock(&block_group->lock);
7810 if (block_group->iref)
7811 break;
7812 spin_unlock(&block_group->lock);
7813 block_group = next_block_group(info->tree_root,
7814 block_group);
7815 }
7816 if (!block_group) {
7817 if (last == 0)
7818 break;
7819 last = 0;
7820 continue;
7821 }
7822
7823 inode = block_group->inode;
7824 block_group->iref = 0;
7825 block_group->inode = NULL;
7826 spin_unlock(&block_group->lock);
7827 iput(inode);
7828 last = block_group->key.objectid + block_group->key.offset;
7829 btrfs_put_block_group(block_group);
7830 }
7831}
7832
Zheng Yan1a40e232008-09-26 10:09:34 -04007833int btrfs_free_block_groups(struct btrfs_fs_info *info)
7834{
7835 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007836 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007837 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007838 struct rb_node *n;
7839
Yan Zheng11833d62009-09-11 16:11:19 -04007840 down_write(&info->extent_commit_sem);
7841 while (!list_empty(&info->caching_block_groups)) {
7842 caching_ctl = list_entry(info->caching_block_groups.next,
7843 struct btrfs_caching_control, list);
7844 list_del(&caching_ctl->list);
7845 put_caching_control(caching_ctl);
7846 }
7847 up_write(&info->extent_commit_sem);
7848
Zheng Yan1a40e232008-09-26 10:09:34 -04007849 spin_lock(&info->block_group_cache_lock);
7850 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7851 block_group = rb_entry(n, struct btrfs_block_group_cache,
7852 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007853 rb_erase(&block_group->cache_node,
7854 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007855 spin_unlock(&info->block_group_cache_lock);
7856
Josef Bacik80eb2342008-10-29 14:49:05 -04007857 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007858 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007859 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007860
Josef Bacik817d52f2009-07-13 21:29:25 -04007861 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007862 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007863
Josef Bacik3c148742011-02-02 15:53:47 +00007864 /*
7865 * We haven't cached this block group, which means we could
7866 * possibly have excluded extents on this block group.
7867 */
7868 if (block_group->cached == BTRFS_CACHE_NO)
7869 free_excluded_extents(info->extent_root, block_group);
7870
Josef Bacik817d52f2009-07-13 21:29:25 -04007871 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007872 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007873
7874 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007875 }
7876 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007877
7878 /* now that all the block groups are freed, go through and
7879 * free all the space_info structs. This is only called during
7880 * the final stages of unmount, and so we know nobody is
7881 * using them. We call synchronize_rcu() once before we start,
7882 * just to be on the safe side.
7883 */
7884 synchronize_rcu();
7885
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007886 release_global_block_rsv(info);
7887
Chris Mason4184ea72009-03-10 12:39:20 -04007888 while(!list_empty(&info->space_info)) {
7889 space_info = list_entry(info->space_info.next,
7890 struct btrfs_space_info,
7891 list);
David Sterbab069e0c2013-02-08 21:28:17 +00007892 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
7893 if (space_info->bytes_pinned > 0 ||
7894 space_info->bytes_reserved > 0 ||
7895 space_info->bytes_may_use > 0) {
7896 WARN_ON(1);
7897 dump_space_info(space_info, 0, 0);
7898 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007899 }
Chris Mason4184ea72009-03-10 12:39:20 -04007900 list_del(&space_info->list);
7901 kfree(space_info);
7902 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007903 return 0;
7904}
7905
Yan, Zhengb742bb82010-05-16 10:46:24 -04007906static void __link_block_group(struct btrfs_space_info *space_info,
7907 struct btrfs_block_group_cache *cache)
7908{
7909 int index = get_block_group_index(cache);
7910
7911 down_write(&space_info->groups_sem);
7912 list_add_tail(&cache->list, &space_info->block_groups[index]);
7913 up_write(&space_info->groups_sem);
7914}
7915
Chris Mason9078a3e2007-04-26 16:46:15 -04007916int btrfs_read_block_groups(struct btrfs_root *root)
7917{
7918 struct btrfs_path *path;
7919 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007920 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007921 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007922 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007923 struct btrfs_key key;
7924 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007925 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04007926 int need_clear = 0;
7927 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04007928
Chris Masonbe744172007-05-06 10:15:01 -04007929 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007930 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007931 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007932 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007933 path = btrfs_alloc_path();
7934 if (!path)
7935 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04007936 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04007937
David Sterba6c417612011-04-13 15:41:04 +02007938 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04007939 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02007940 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04007941 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04007942 if (btrfs_test_opt(root, CLEAR_CACHE))
7943 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04007944
Chris Masond3977122009-01-05 21:25:51 -05007945 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007946 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007947 if (ret > 0)
7948 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007949 if (ret != 0)
7950 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04007951 leaf = path->nodes[0];
7952 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007953 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007954 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007955 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007956 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007957 }
Li Zefan34d52cb2011-03-29 13:46:06 +08007958 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7959 GFP_NOFS);
7960 if (!cache->free_space_ctl) {
7961 kfree(cache);
7962 ret = -ENOMEM;
7963 goto error;
7964 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007965
Yan Zhengd2fb3432008-12-11 16:30:39 -05007966 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007967 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007968 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007969 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007970 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007971
Liu Bocf7c1ef2012-07-06 03:31:34 -06007972 if (need_clear) {
7973 /*
7974 * When we mount with old space cache, we need to
7975 * set BTRFS_DC_CLEAR and set dirty flag.
7976 *
7977 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
7978 * truncate the old free space cache inode and
7979 * setup a new one.
7980 * b) Setting 'dirty flag' makes sure that we flush
7981 * the new space cache info onto disk.
7982 */
Josef Bacik0af3d002010-06-21 14:48:16 -04007983 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06007984 if (btrfs_test_opt(root, SPACE_CACHE))
7985 cache->dirty = 1;
7986 }
Josef Bacik0af3d002010-06-21 14:48:16 -04007987
Chris Mason5f39d392007-10-15 16:14:19 -04007988 read_extent_buffer(leaf, &cache->item,
7989 btrfs_item_ptr_offset(leaf, path->slots[0]),
7990 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007991 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007992
Chris Mason9078a3e2007-04-26 16:46:15 -04007993 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02007994 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007995 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007996 cache->sectorsize = root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05007997 cache->full_stripe_len = btrfs_full_stripe_len(root,
7998 &root->fs_info->mapping_tree,
7999 found_key.objectid);
Li Zefan34d52cb2011-03-29 13:46:06 +08008000 btrfs_init_free_space_ctl(cache);
8001
Josef Bacik817d52f2009-07-13 21:29:25 -04008002 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008003 * We need to exclude the super stripes now so that the space
8004 * info has super bytes accounted for, otherwise we'll think
8005 * we have more space than we actually do.
8006 */
Josef Bacik835d9742013-03-19 12:13:25 -04008007 ret = exclude_super_stripes(root, cache);
8008 if (ret) {
8009 /*
8010 * We may have excluded something, so call this just in
8011 * case.
8012 */
8013 free_excluded_extents(root, cache);
8014 kfree(cache->free_space_ctl);
8015 kfree(cache);
8016 goto error;
8017 }
Josef Bacik3c148742011-02-02 15:53:47 +00008018
8019 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008020 * check for two cases, either we are full, and therefore
8021 * don't need to bother with the caching work since we won't
8022 * find any space, or we are empty, and we can just add all
8023 * the space in and be done with it. This saves us _alot_ of
8024 * time, particularly in the full case.
8025 */
8026 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008027 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008028 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008029 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008030 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008031 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008032 cache->cached = BTRFS_CACHE_FINISHED;
8033 add_new_free_space(cache, root->fs_info,
8034 found_key.objectid,
8035 found_key.objectid +
8036 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008037 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008038 }
Chris Mason96b51792007-10-15 16:15:19 -04008039
Chris Mason6324fbf2008-03-24 15:01:59 -04008040 ret = update_space_info(info, cache->flags, found_key.offset,
8041 btrfs_block_group_used(&cache->item),
8042 &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008043 BUG_ON(ret); /* -ENOMEM */
Chris Mason6324fbf2008-03-24 15:01:59 -04008044 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008045 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008046 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008047 spin_unlock(&cache->space_info->lock);
8048
Yan, Zhengb742bb82010-05-16 10:46:24 -04008049 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008050
Josef Bacik0f9dd462008-09-23 13:14:11 -04008051 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008052 BUG_ON(ret); /* Logic error */
Chris Mason75ccf472008-09-30 19:24:06 -04008053
8054 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008055 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008056 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008057 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008058
8059 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8060 if (!(get_alloc_profile(root, space_info->flags) &
8061 (BTRFS_BLOCK_GROUP_RAID10 |
8062 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008063 BTRFS_BLOCK_GROUP_RAID5 |
8064 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04008065 BTRFS_BLOCK_GROUP_DUP)))
8066 continue;
8067 /*
8068 * avoid allocating from un-mirrored block group if there are
8069 * mirrored block groups.
8070 */
8071 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008072 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008073 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008074 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008075 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008076
8077 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008078 ret = 0;
8079error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008080 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008081 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008082}
Chris Mason6324fbf2008-03-24 15:01:59 -04008083
Josef Bacikea658ba2012-09-11 16:57:25 -04008084void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8085 struct btrfs_root *root)
8086{
8087 struct btrfs_block_group_cache *block_group, *tmp;
8088 struct btrfs_root *extent_root = root->fs_info->extent_root;
8089 struct btrfs_block_group_item item;
8090 struct btrfs_key key;
8091 int ret = 0;
8092
8093 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8094 new_bg_list) {
8095 list_del_init(&block_group->new_bg_list);
8096
8097 if (ret)
8098 continue;
8099
8100 spin_lock(&block_group->lock);
8101 memcpy(&item, &block_group->item, sizeof(item));
8102 memcpy(&key, &block_group->key, sizeof(key));
8103 spin_unlock(&block_group->lock);
8104
8105 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8106 sizeof(item));
8107 if (ret)
8108 btrfs_abort_transaction(trans, extent_root, ret);
8109 }
8110}
8111
Chris Mason6324fbf2008-03-24 15:01:59 -04008112int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8113 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008114 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008115 u64 size)
8116{
8117 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008118 struct btrfs_root *extent_root;
8119 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008120
8121 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008122
Chris Mason12fcfd22009-03-24 10:24:20 -04008123 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008124
Chris Mason8f18cf12008-04-25 16:53:30 -04008125 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008126 if (!cache)
8127 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08008128 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8129 GFP_NOFS);
8130 if (!cache->free_space_ctl) {
8131 kfree(cache);
8132 return -ENOMEM;
8133 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008134
Chris Masone17cade2008-04-15 15:41:47 -04008135 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008136 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008137 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008138 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008139 cache->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05008140 cache->full_stripe_len = btrfs_full_stripe_len(root,
8141 &root->fs_info->mapping_tree,
8142 chunk_offset);
Josef Bacik96303082009-07-13 21:29:25 -04008143
Yan Zhengd2fb3432008-12-11 16:30:39 -05008144 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008145 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008146 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008147 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04008148 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008149
Li Zefan34d52cb2011-03-29 13:46:06 +08008150 btrfs_init_free_space_ctl(cache);
8151
Chris Mason6324fbf2008-03-24 15:01:59 -04008152 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008153 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8154 cache->flags = type;
8155 btrfs_set_block_group_flags(&cache->item, type);
8156
Yan Zheng11833d62009-09-11 16:11:19 -04008157 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008158 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04008159 ret = exclude_super_stripes(root, cache);
8160 if (ret) {
8161 /*
8162 * We may have excluded something, so call this just in
8163 * case.
8164 */
8165 free_excluded_extents(root, cache);
8166 kfree(cache->free_space_ctl);
8167 kfree(cache);
8168 return ret;
8169 }
Josef Bacik96303082009-07-13 21:29:25 -04008170
Josef Bacik817d52f2009-07-13 21:29:25 -04008171 add_new_free_space(cache, root->fs_info, chunk_offset,
8172 chunk_offset + size);
8173
Yan Zheng11833d62009-09-11 16:11:19 -04008174 free_excluded_extents(root, cache);
8175
Chris Mason6324fbf2008-03-24 15:01:59 -04008176 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8177 &cache->space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008178 BUG_ON(ret); /* -ENOMEM */
Li Zefanc7c144d2011-12-07 10:39:22 +08008179 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008180
8181 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008182 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008183 spin_unlock(&cache->space_info->lock);
8184
Yan, Zhengb742bb82010-05-16 10:46:24 -04008185 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008186
Josef Bacik0f9dd462008-09-23 13:14:11 -04008187 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008188 BUG_ON(ret); /* Logic error */
Chris Masonc286ac42008-07-22 23:06:41 -04008189
Josef Bacikea658ba2012-09-11 16:57:25 -04008190 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008191
Chris Masond18a2c42008-04-04 15:40:00 -04008192 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008193
Chris Mason6324fbf2008-03-24 15:01:59 -04008194 return 0;
8195}
Zheng Yan1a40e232008-09-26 10:09:34 -04008196
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008197static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8198{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008199 u64 extra_flags = chunk_to_extended(flags) &
8200 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008201
Miao Xiede98ced2013-01-29 10:13:12 +00008202 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008203 if (flags & BTRFS_BLOCK_GROUP_DATA)
8204 fs_info->avail_data_alloc_bits &= ~extra_flags;
8205 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8206 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8207 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8208 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008209 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008210}
8211
Zheng Yan1a40e232008-09-26 10:09:34 -04008212int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8213 struct btrfs_root *root, u64 group_start)
8214{
8215 struct btrfs_path *path;
8216 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008217 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008218 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008219 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008220 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008221 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008222 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008223 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008224
Zheng Yan1a40e232008-09-26 10:09:34 -04008225 root = root->fs_info->extent_root;
8226
8227 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8228 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008229 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008230
liubo9f7c43c2011-03-07 02:13:33 +00008231 /*
8232 * Free the reserved super bytes from this block group before
8233 * remove it.
8234 */
8235 free_excluded_extents(root, block_group);
8236
Zheng Yan1a40e232008-09-26 10:09:34 -04008237 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008238 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008239 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8240 BTRFS_BLOCK_GROUP_RAID1 |
8241 BTRFS_BLOCK_GROUP_RAID10))
8242 factor = 2;
8243 else
8244 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008245
Chris Mason44fb5512009-06-04 15:34:51 -04008246 /* make sure this block group isn't part of an allocation cluster */
8247 cluster = &root->fs_info->data_alloc_cluster;
8248 spin_lock(&cluster->refill_lock);
8249 btrfs_return_cluster_to_free_space(block_group, cluster);
8250 spin_unlock(&cluster->refill_lock);
8251
8252 /*
8253 * make sure this block group isn't part of a metadata
8254 * allocation cluster
8255 */
8256 cluster = &root->fs_info->meta_alloc_cluster;
8257 spin_lock(&cluster->refill_lock);
8258 btrfs_return_cluster_to_free_space(block_group, cluster);
8259 spin_unlock(&cluster->refill_lock);
8260
Zheng Yan1a40e232008-09-26 10:09:34 -04008261 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008262 if (!path) {
8263 ret = -ENOMEM;
8264 goto out;
8265 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008266
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008267 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008268 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008269 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008270 if (ret) {
8271 btrfs_add_delayed_iput(inode);
8272 goto out;
8273 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008274 clear_nlink(inode);
8275 /* One for the block groups ref */
8276 spin_lock(&block_group->lock);
8277 if (block_group->iref) {
8278 block_group->iref = 0;
8279 block_group->inode = NULL;
8280 spin_unlock(&block_group->lock);
8281 iput(inode);
8282 } else {
8283 spin_unlock(&block_group->lock);
8284 }
8285 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008286 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008287 }
8288
8289 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8290 key.offset = block_group->key.objectid;
8291 key.type = 0;
8292
8293 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8294 if (ret < 0)
8295 goto out;
8296 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008297 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008298 if (ret == 0) {
8299 ret = btrfs_del_item(trans, tree_root, path);
8300 if (ret)
8301 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008302 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008303 }
8304
Yan Zheng3dfdb932009-01-21 10:49:16 -05008305 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008306 rb_erase(&block_group->cache_node,
8307 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008308
8309 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8310 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008311 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008312
Josef Bacik80eb2342008-10-29 14:49:05 -04008313 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008314 /*
8315 * we must use list_del_init so people can check to see if they
8316 * are still on the list after taking the semaphore
8317 */
8318 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008319 if (list_empty(&block_group->space_info->block_groups[index]))
8320 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008321 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008322
Josef Bacik817d52f2009-07-13 21:29:25 -04008323 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008324 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008325
8326 btrfs_remove_free_space_cache(block_group);
8327
Yan Zhengc146afa2008-11-12 14:34:12 -05008328 spin_lock(&block_group->space_info->lock);
8329 block_group->space_info->total_bytes -= block_group->key.offset;
8330 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008331 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008332 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008333
Josef Bacik0af3d002010-06-21 14:48:16 -04008334 memcpy(&key, &block_group->key, sizeof(key));
8335
Chris Mason283bb192009-07-24 16:30:55 -04008336 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008337
Chris Masonfa9c0d792009-04-03 09:47:43 -04008338 btrfs_put_block_group(block_group);
8339 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008340
8341 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8342 if (ret > 0)
8343 ret = -EIO;
8344 if (ret < 0)
8345 goto out;
8346
8347 ret = btrfs_del_item(trans, root, path);
8348out:
8349 btrfs_free_path(path);
8350 return ret;
8351}
liuboacce9522011-01-06 19:30:25 +08008352
liuboc59021f2011-03-07 02:13:14 +00008353int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8354{
8355 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008356 struct btrfs_super_block *disk_super;
8357 u64 features;
8358 u64 flags;
8359 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008360 int ret;
8361
David Sterba6c417612011-04-13 15:41:04 +02008362 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008363 if (!btrfs_super_root(disk_super))
8364 return 1;
liuboc59021f2011-03-07 02:13:14 +00008365
liubo1aba86d2011-04-08 08:44:37 +00008366 features = btrfs_super_incompat_flags(disk_super);
8367 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8368 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008369
liubo1aba86d2011-04-08 08:44:37 +00008370 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8371 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008372 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008373 goto out;
liuboc59021f2011-03-07 02:13:14 +00008374
liubo1aba86d2011-04-08 08:44:37 +00008375 if (mixed) {
8376 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8377 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8378 } else {
8379 flags = BTRFS_BLOCK_GROUP_METADATA;
8380 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8381 if (ret)
8382 goto out;
8383
8384 flags = BTRFS_BLOCK_GROUP_DATA;
8385 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8386 }
8387out:
liuboc59021f2011-03-07 02:13:14 +00008388 return ret;
8389}
8390
liuboacce9522011-01-06 19:30:25 +08008391int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8392{
8393 return unpin_extent_range(root, start, end);
8394}
8395
8396int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008397 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008398{
Li Dongyang5378e602011-03-24 10:24:27 +00008399 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008400}
Li Dongyangf7039b12011-03-24 10:24:28 +00008401
8402int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8403{
8404 struct btrfs_fs_info *fs_info = root->fs_info;
8405 struct btrfs_block_group_cache *cache = NULL;
8406 u64 group_trimmed;
8407 u64 start;
8408 u64 end;
8409 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008410 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008411 int ret = 0;
8412
Liu Bo2cac13e2012-02-09 18:17:41 +08008413 /*
8414 * try to trim all FS space, our block group may start from non-zero.
8415 */
8416 if (range->len == total_bytes)
8417 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8418 else
8419 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008420
8421 while (cache) {
8422 if (cache->key.objectid >= (range->start + range->len)) {
8423 btrfs_put_block_group(cache);
8424 break;
8425 }
8426
8427 start = max(range->start, cache->key.objectid);
8428 end = min(range->start + range->len,
8429 cache->key.objectid + cache->key.offset);
8430
8431 if (end - start >= range->minlen) {
8432 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008433 ret = cache_block_group(cache, 0);
Li Dongyangf7039b12011-03-24 10:24:28 +00008434 if (!ret)
8435 wait_block_group_cache_done(cache);
8436 }
8437 ret = btrfs_trim_block_group(cache,
8438 &group_trimmed,
8439 start,
8440 end,
8441 range->minlen);
8442
8443 trimmed += group_trimmed;
8444 if (ret) {
8445 btrfs_put_block_group(cache);
8446 break;
8447 }
8448 }
8449
8450 cache = next_block_group(fs_info->tree_root, cache);
8451 }
8452
8453 range->len = trimmed;
8454 return ret;
8455}