blob: 71cd456fdb60360d44edd113ab7c593bdacfa658 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050027#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040031#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040033#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040034#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Chris Mason0e4f8f82011-04-15 16:05:44 -040036/* control flags for do_chunk_alloc's force field
37 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
38 * if we really need one.
39 *
40 * CHUNK_ALLOC_FORCE means it must try to allocate one
41 *
42 * CHUNK_ALLOC_LIMITED means to only try and allocate one
43 * if we have very few chunks already allocated. This is
44 * used as part of the clustering code to help make sure
45 * we have a good pool of storage to cluster in, without
46 * filling the FS with empty chunks
47 *
48 */
49enum {
50 CHUNK_ALLOC_NO_FORCE = 0,
51 CHUNK_ALLOC_FORCE = 1,
52 CHUNK_ALLOC_LIMITED = 2,
53};
54
Josef Bacikf3465ca2008-11-12 14:19:50 -050055static int update_block_group(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040057 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040058static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
59 struct btrfs_root *root,
60 u64 bytenr, u64 num_bytes, u64 parent,
61 u64 root_objectid, u64 owner_objectid,
62 u64 owner_offset, int refs_to_drop,
63 struct btrfs_delayed_extent_op *extra_op);
64static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
65 struct extent_buffer *leaf,
66 struct btrfs_extent_item *ei);
67static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
68 struct btrfs_root *root,
69 u64 parent, u64 root_objectid,
70 u64 flags, u64 owner, u64 offset,
71 struct btrfs_key *ins, int ref_mod);
72static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
73 struct btrfs_root *root,
74 u64 parent, u64 root_objectid,
75 u64 flags, struct btrfs_disk_key *key,
76 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050077static int do_chunk_alloc(struct btrfs_trans_handle *trans,
78 struct btrfs_root *extent_root, u64 alloc_bytes,
79 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040080static int find_next_key(struct btrfs_path *path, int level,
81 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040082static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
83 int dump_block_groups);
Josef Bacik6a632092009-02-20 11:00:09 -050084
Josef Bacik817d52f2009-07-13 21:29:25 -040085static noinline int
86block_group_cache_done(struct btrfs_block_group_cache *cache)
87{
88 smp_mb();
89 return cache->cached == BTRFS_CACHE_FINISHED;
90}
91
Josef Bacik0f9dd462008-09-23 13:14:11 -040092static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
93{
94 return (cache->flags & bits) == bits;
95}
96
David Sterba62a45b62011-04-20 15:52:26 +020097static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +000098{
99 atomic_inc(&cache->count);
100}
101
102void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
103{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400104 if (atomic_dec_and_test(&cache->count)) {
105 WARN_ON(cache->pinned > 0);
106 WARN_ON(cache->reserved > 0);
107 WARN_ON(cache->reserved_pinned > 0);
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800108 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000109 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400110 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000111}
112
Josef Bacik0f9dd462008-09-23 13:14:11 -0400113/*
114 * this adds the block group to the fs_info rb tree for the block group
115 * cache
116 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500117static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400118 struct btrfs_block_group_cache *block_group)
119{
120 struct rb_node **p;
121 struct rb_node *parent = NULL;
122 struct btrfs_block_group_cache *cache;
123
124 spin_lock(&info->block_group_cache_lock);
125 p = &info->block_group_cache_tree.rb_node;
126
127 while (*p) {
128 parent = *p;
129 cache = rb_entry(parent, struct btrfs_block_group_cache,
130 cache_node);
131 if (block_group->key.objectid < cache->key.objectid) {
132 p = &(*p)->rb_left;
133 } else if (block_group->key.objectid > cache->key.objectid) {
134 p = &(*p)->rb_right;
135 } else {
136 spin_unlock(&info->block_group_cache_lock);
137 return -EEXIST;
138 }
139 }
140
141 rb_link_node(&block_group->cache_node, parent, p);
142 rb_insert_color(&block_group->cache_node,
143 &info->block_group_cache_tree);
144 spin_unlock(&info->block_group_cache_lock);
145
146 return 0;
147}
148
149/*
150 * This will return the block group at or after bytenr if contains is 0, else
151 * it will return the block group that contains the bytenr
152 */
153static struct btrfs_block_group_cache *
154block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
155 int contains)
156{
157 struct btrfs_block_group_cache *cache, *ret = NULL;
158 struct rb_node *n;
159 u64 end, start;
160
161 spin_lock(&info->block_group_cache_lock);
162 n = info->block_group_cache_tree.rb_node;
163
164 while (n) {
165 cache = rb_entry(n, struct btrfs_block_group_cache,
166 cache_node);
167 end = cache->key.objectid + cache->key.offset - 1;
168 start = cache->key.objectid;
169
170 if (bytenr < start) {
171 if (!contains && (!ret || start < ret->key.objectid))
172 ret = cache;
173 n = n->rb_left;
174 } else if (bytenr > start) {
175 if (contains && bytenr <= end) {
176 ret = cache;
177 break;
178 }
179 n = n->rb_right;
180 } else {
181 ret = cache;
182 break;
183 }
184 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500185 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000186 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400187 spin_unlock(&info->block_group_cache_lock);
188
189 return ret;
190}
191
Yan Zheng11833d62009-09-11 16:11:19 -0400192static int add_excluded_extent(struct btrfs_root *root,
193 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400194{
Yan Zheng11833d62009-09-11 16:11:19 -0400195 u64 end = start + num_bytes - 1;
196 set_extent_bits(&root->fs_info->freed_extents[0],
197 start, end, EXTENT_UPTODATE, GFP_NOFS);
198 set_extent_bits(&root->fs_info->freed_extents[1],
199 start, end, EXTENT_UPTODATE, GFP_NOFS);
200 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400201}
202
Yan Zheng11833d62009-09-11 16:11:19 -0400203static void free_excluded_extents(struct btrfs_root *root,
204 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400205{
Yan Zheng11833d62009-09-11 16:11:19 -0400206 u64 start, end;
207
208 start = cache->key.objectid;
209 end = start + cache->key.offset - 1;
210
211 clear_extent_bits(&root->fs_info->freed_extents[0],
212 start, end, EXTENT_UPTODATE, GFP_NOFS);
213 clear_extent_bits(&root->fs_info->freed_extents[1],
214 start, end, EXTENT_UPTODATE, GFP_NOFS);
215}
216
217static int exclude_super_stripes(struct btrfs_root *root,
218 struct btrfs_block_group_cache *cache)
219{
Josef Bacik817d52f2009-07-13 21:29:25 -0400220 u64 bytenr;
221 u64 *logical;
222 int stripe_len;
223 int i, nr, ret;
224
Yan, Zheng06b23312009-11-26 09:31:11 +0000225 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
226 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
227 cache->bytes_super += stripe_len;
228 ret = add_excluded_extent(root, cache->key.objectid,
229 stripe_len);
230 BUG_ON(ret);
231 }
232
Josef Bacik817d52f2009-07-13 21:29:25 -0400233 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
234 bytenr = btrfs_sb_offset(i);
235 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
236 cache->key.objectid, bytenr,
237 0, &logical, &nr, &stripe_len);
238 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400239
Josef Bacik817d52f2009-07-13 21:29:25 -0400240 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400241 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400242 ret = add_excluded_extent(root, logical[nr],
243 stripe_len);
244 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400245 }
Yan Zheng11833d62009-09-11 16:11:19 -0400246
Josef Bacik817d52f2009-07-13 21:29:25 -0400247 kfree(logical);
248 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400249 return 0;
250}
251
Yan Zheng11833d62009-09-11 16:11:19 -0400252static struct btrfs_caching_control *
253get_caching_control(struct btrfs_block_group_cache *cache)
254{
255 struct btrfs_caching_control *ctl;
256
257 spin_lock(&cache->lock);
258 if (cache->cached != BTRFS_CACHE_STARTED) {
259 spin_unlock(&cache->lock);
260 return NULL;
261 }
262
Josef Bacikdde5abe2010-09-16 16:17:03 -0400263 /* We're loading it the fast way, so we don't have a caching_ctl. */
264 if (!cache->caching_ctl) {
265 spin_unlock(&cache->lock);
266 return NULL;
267 }
268
Yan Zheng11833d62009-09-11 16:11:19 -0400269 ctl = cache->caching_ctl;
270 atomic_inc(&ctl->count);
271 spin_unlock(&cache->lock);
272 return ctl;
273}
274
275static void put_caching_control(struct btrfs_caching_control *ctl)
276{
277 if (atomic_dec_and_test(&ctl->count))
278 kfree(ctl);
279}
280
Josef Bacik0f9dd462008-09-23 13:14:11 -0400281/*
282 * this is only called by cache_block_group, since we could have freed extents
283 * we need to check the pinned_extents for any extents that can't be used yet
284 * since their free space will be released as soon as the transaction commits.
285 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400286static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400287 struct btrfs_fs_info *info, u64 start, u64 end)
288{
Josef Bacik817d52f2009-07-13 21:29:25 -0400289 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400290 int ret;
291
292 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400293 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400294 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400295 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400296 if (ret)
297 break;
298
Yan, Zheng06b23312009-11-26 09:31:11 +0000299 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300 start = extent_end + 1;
301 } else if (extent_start > start && extent_start < end) {
302 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400303 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500304 ret = btrfs_add_free_space(block_group, start,
305 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400306 BUG_ON(ret);
307 start = extent_end + 1;
308 } else {
309 break;
310 }
311 }
312
313 if (start < end) {
314 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400315 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500316 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400317 BUG_ON(ret);
318 }
319
Josef Bacik817d52f2009-07-13 21:29:25 -0400320 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400321}
322
Josef Bacik817d52f2009-07-13 21:29:25 -0400323static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400324{
Josef Bacik817d52f2009-07-13 21:29:25 -0400325 struct btrfs_block_group_cache *block_group = data;
326 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400327 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
328 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400329 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400330 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400331 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400332 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400333 u64 last = 0;
334 u32 nritems;
335 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400336
Chris Masone37c9e62007-05-09 20:13:14 -0400337 path = btrfs_alloc_path();
338 if (!path)
339 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400340
Josef Bacik817d52f2009-07-13 21:29:25 -0400341 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400342
Chris Mason5cd57b22008-06-25 16:01:30 -0400343 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400344 * We don't want to deadlock with somebody trying to allocate a new
345 * extent for the extent root while also trying to search the extent
346 * root to add free space. So we skip locking and search the commit
347 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400348 */
349 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400350 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400351 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400352
Yan Zhenge4404d62008-12-12 10:03:26 -0500353 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400354 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400355 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400356again:
Yan Zheng11833d62009-09-11 16:11:19 -0400357 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400358 /* need to make sure the commit_root doesn't disappear */
359 down_read(&fs_info->extent_commit_sem);
360
Yan Zheng11833d62009-09-11 16:11:19 -0400361 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400362 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400363 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500364
Yan Zheng11833d62009-09-11 16:11:19 -0400365 leaf = path->nodes[0];
366 nritems = btrfs_header_nritems(leaf);
367
Chris Masond3977122009-01-05 21:25:51 -0500368 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200369 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400370 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400371 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400372 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400373
Yan Zheng11833d62009-09-11 16:11:19 -0400374 if (path->slots[0] < nritems) {
375 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
376 } else {
377 ret = find_next_key(path, 0, &key);
378 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400379 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400380
Josef Bacik589d8ad2011-05-11 17:30:53 -0400381 if (need_resched() ||
382 btrfs_next_leaf(extent_root, path)) {
383 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400384 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400385 up_read(&fs_info->extent_commit_sem);
386 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400387 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400388 goto again;
389 }
390 leaf = path->nodes[0];
391 nritems = btrfs_header_nritems(leaf);
392 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400393 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400394
Yan Zheng11833d62009-09-11 16:11:19 -0400395 if (key.objectid < block_group->key.objectid) {
396 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400397 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400398 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400399
Chris Masone37c9e62007-05-09 20:13:14 -0400400 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400401 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400402 break;
Yan7d7d6062007-09-14 16:15:28 -0400403
Yan Zheng11833d62009-09-11 16:11:19 -0400404 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400405 total_found += add_new_free_space(block_group,
406 fs_info, last,
407 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400408 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400409
Yan Zheng11833d62009-09-11 16:11:19 -0400410 if (total_found > (1024 * 1024 * 2)) {
411 total_found = 0;
412 wake_up(&caching_ctl->wait);
413 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400414 }
Chris Masone37c9e62007-05-09 20:13:14 -0400415 path->slots[0]++;
416 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400417 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400418
419 total_found += add_new_free_space(block_group, fs_info, last,
420 block_group->key.objectid +
421 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400422 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400423
424 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400425 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400426 block_group->cached = BTRFS_CACHE_FINISHED;
427 spin_unlock(&block_group->lock);
428
Chris Mason54aa1f42007-06-22 14:16:25 -0400429err:
Chris Masone37c9e62007-05-09 20:13:14 -0400430 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400431 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400432
Yan Zheng11833d62009-09-11 16:11:19 -0400433 free_excluded_extents(extent_root, block_group);
434
435 mutex_unlock(&caching_ctl->mutex);
436 wake_up(&caching_ctl->wait);
437
438 put_caching_control(caching_ctl);
439 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000440 btrfs_put_block_group(block_group);
441
Josef Bacik817d52f2009-07-13 21:29:25 -0400442 return 0;
443}
444
Josef Bacik9d66e232010-08-25 16:54:15 -0400445static int cache_block_group(struct btrfs_block_group_cache *cache,
446 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500447 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400448 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400449{
Yan Zheng11833d62009-09-11 16:11:19 -0400450 struct btrfs_fs_info *fs_info = cache->fs_info;
451 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400452 struct task_struct *tsk;
453 int ret = 0;
454
Yan Zheng11833d62009-09-11 16:11:19 -0400455 smp_mb();
456 if (cache->cached != BTRFS_CACHE_NO)
457 return 0;
458
Josef Bacik9d66e232010-08-25 16:54:15 -0400459 /*
460 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500461 * to have the normal tree locking. Also if we are currently trying to
462 * allocate blocks for the tree root we can't do the fast caching since
463 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400464 */
Li Dongyangf7039b12011-03-24 10:24:28 +0000465 if (trans && (!trans->transaction->in_commit) &&
Josef Bacikb8399de2010-12-08 09:15:11 -0500466 (root && root != root->fs_info->tree_root)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400467 spin_lock(&cache->lock);
468 if (cache->cached != BTRFS_CACHE_NO) {
469 spin_unlock(&cache->lock);
470 return 0;
471 }
472 cache->cached = BTRFS_CACHE_STARTED;
473 spin_unlock(&cache->lock);
474
475 ret = load_free_space_cache(fs_info, cache);
476
477 spin_lock(&cache->lock);
478 if (ret == 1) {
479 cache->cached = BTRFS_CACHE_FINISHED;
480 cache->last_byte_to_unpin = (u64)-1;
481 } else {
482 cache->cached = BTRFS_CACHE_NO;
483 }
484 spin_unlock(&cache->lock);
Josef Bacik3c148742011-02-02 15:53:47 +0000485 if (ret == 1) {
486 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400487 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000488 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400489 }
490
491 if (load_cache_only)
492 return 0;
493
Miao Xiefc0e4a32011-03-24 11:41:21 +0000494 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -0400495 BUG_ON(!caching_ctl);
496
497 INIT_LIST_HEAD(&caching_ctl->list);
498 mutex_init(&caching_ctl->mutex);
499 init_waitqueue_head(&caching_ctl->wait);
500 caching_ctl->block_group = cache;
501 caching_ctl->progress = cache->key.objectid;
502 /* one for caching kthread, one for caching block group list */
503 atomic_set(&caching_ctl->count, 2);
504
Josef Bacik817d52f2009-07-13 21:29:25 -0400505 spin_lock(&cache->lock);
506 if (cache->cached != BTRFS_CACHE_NO) {
507 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400508 kfree(caching_ctl);
509 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400510 }
Yan Zheng11833d62009-09-11 16:11:19 -0400511 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400512 cache->cached = BTRFS_CACHE_STARTED;
513 spin_unlock(&cache->lock);
514
Yan Zheng11833d62009-09-11 16:11:19 -0400515 down_write(&fs_info->extent_commit_sem);
516 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
517 up_write(&fs_info->extent_commit_sem);
518
519 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000520 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400521
Josef Bacik817d52f2009-07-13 21:29:25 -0400522 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
523 cache->key.objectid);
524 if (IS_ERR(tsk)) {
525 ret = PTR_ERR(tsk);
526 printk(KERN_ERR "error running thread %d\n", ret);
527 BUG();
528 }
529
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400530 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400531}
532
Josef Bacik0f9dd462008-09-23 13:14:11 -0400533/*
534 * return the block group that starts at or after bytenr
535 */
Chris Masond3977122009-01-05 21:25:51 -0500536static struct btrfs_block_group_cache *
537btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400538{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400539 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400540
Josef Bacik0f9dd462008-09-23 13:14:11 -0400541 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400542
Josef Bacik0f9dd462008-09-23 13:14:11 -0400543 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400544}
545
Josef Bacik0f9dd462008-09-23 13:14:11 -0400546/*
Sankar P9f556842009-05-14 13:52:22 -0400547 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400548 */
Chris Masond3977122009-01-05 21:25:51 -0500549struct btrfs_block_group_cache *btrfs_lookup_block_group(
550 struct btrfs_fs_info *info,
551 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400552{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400553 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400554
Josef Bacik0f9dd462008-09-23 13:14:11 -0400555 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400556
Josef Bacik0f9dd462008-09-23 13:14:11 -0400557 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400558}
Chris Mason0b86a832008-03-24 15:01:56 -0400559
Josef Bacik0f9dd462008-09-23 13:14:11 -0400560static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
561 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400562{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400563 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400564 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400565
Yan, Zhengb742bb82010-05-16 10:46:24 -0400566 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
567 BTRFS_BLOCK_GROUP_METADATA;
568
Chris Mason4184ea72009-03-10 12:39:20 -0400569 rcu_read_lock();
570 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400571 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400572 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400573 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400574 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400575 }
Chris Mason4184ea72009-03-10 12:39:20 -0400576 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400577 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400578}
579
Chris Mason4184ea72009-03-10 12:39:20 -0400580/*
581 * after adding space to the filesystem, we need to clear the full flags
582 * on all the space infos.
583 */
584void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
585{
586 struct list_head *head = &info->space_info;
587 struct btrfs_space_info *found;
588
589 rcu_read_lock();
590 list_for_each_entry_rcu(found, head, list)
591 found->full = 0;
592 rcu_read_unlock();
593}
594
Josef Bacik80eb2342008-10-29 14:49:05 -0400595static u64 div_factor(u64 num, int factor)
596{
597 if (factor == 10)
598 return num;
599 num *= factor;
600 do_div(num, 10);
601 return num;
602}
603
Chris Masone5bc2452010-10-26 13:37:56 -0400604static u64 div_factor_fine(u64 num, int factor)
605{
606 if (factor == 100)
607 return num;
608 num *= factor;
609 do_div(num, 100);
610 return num;
611}
612
Yan Zhengd2fb3432008-12-11 16:30:39 -0500613u64 btrfs_find_block_group(struct btrfs_root *root,
614 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400615{
Chris Mason96b51792007-10-15 16:15:19 -0400616 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400617 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500618 u64 last = max(search_hint, search_start);
619 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400620 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500621 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400622 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400623again:
Zheng Yane8569812008-09-26 10:05:48 -0400624 while (1) {
625 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400626 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400627 break;
Chris Mason96b51792007-10-15 16:15:19 -0400628
Chris Masonc286ac42008-07-22 23:06:41 -0400629 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400630 last = cache->key.objectid + cache->key.offset;
631 used = btrfs_block_group_used(&cache->item);
632
Yan Zhengd2fb3432008-12-11 16:30:39 -0500633 if ((full_search || !cache->ro) &&
634 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400635 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500636 div_factor(cache->key.offset, factor)) {
637 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400638 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400639 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400640 goto found;
641 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400642 }
Chris Masonc286ac42008-07-22 23:06:41 -0400643 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400644 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400645 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400646 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400647 if (!wrapped) {
648 last = search_start;
649 wrapped = 1;
650 goto again;
651 }
652 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400653 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400654 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400655 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400656 goto again;
657 }
Chris Masonbe744172007-05-06 10:15:01 -0400658found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500659 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400660}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400661
Chris Masone02119d2008-09-05 16:13:11 -0400662/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400663int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400664{
665 int ret;
666 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400667 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400668
Zheng Yan31840ae2008-09-23 13:14:14 -0400669 path = btrfs_alloc_path();
670 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400671 key.objectid = start;
672 key.offset = len;
673 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
674 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
675 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400676 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500677 return ret;
678}
679
Chris Masond8d5f3e2007-12-11 12:42:00 -0500680/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400681 * helper function to lookup reference count and flags of extent.
682 *
683 * the head node for delayed ref is used to store the sum of all the
684 * reference count modifications queued up in the rbtree. the head
685 * node may also store the extent flags to set. This way you can check
686 * to see what the reference count and extent flags would be if all of
687 * the delayed refs are not processed.
688 */
689int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
690 struct btrfs_root *root, u64 bytenr,
691 u64 num_bytes, u64 *refs, u64 *flags)
692{
693 struct btrfs_delayed_ref_head *head;
694 struct btrfs_delayed_ref_root *delayed_refs;
695 struct btrfs_path *path;
696 struct btrfs_extent_item *ei;
697 struct extent_buffer *leaf;
698 struct btrfs_key key;
699 u32 item_size;
700 u64 num_refs;
701 u64 extent_flags;
702 int ret;
703
704 path = btrfs_alloc_path();
705 if (!path)
706 return -ENOMEM;
707
708 key.objectid = bytenr;
709 key.type = BTRFS_EXTENT_ITEM_KEY;
710 key.offset = num_bytes;
711 if (!trans) {
712 path->skip_locking = 1;
713 path->search_commit_root = 1;
714 }
715again:
716 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
717 &key, path, 0, 0);
718 if (ret < 0)
719 goto out_free;
720
721 if (ret == 0) {
722 leaf = path->nodes[0];
723 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
724 if (item_size >= sizeof(*ei)) {
725 ei = btrfs_item_ptr(leaf, path->slots[0],
726 struct btrfs_extent_item);
727 num_refs = btrfs_extent_refs(leaf, ei);
728 extent_flags = btrfs_extent_flags(leaf, ei);
729 } else {
730#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
731 struct btrfs_extent_item_v0 *ei0;
732 BUG_ON(item_size != sizeof(*ei0));
733 ei0 = btrfs_item_ptr(leaf, path->slots[0],
734 struct btrfs_extent_item_v0);
735 num_refs = btrfs_extent_refs_v0(leaf, ei0);
736 /* FIXME: this isn't correct for data */
737 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
738#else
739 BUG();
740#endif
741 }
742 BUG_ON(num_refs == 0);
743 } else {
744 num_refs = 0;
745 extent_flags = 0;
746 ret = 0;
747 }
748
749 if (!trans)
750 goto out;
751
752 delayed_refs = &trans->transaction->delayed_refs;
753 spin_lock(&delayed_refs->lock);
754 head = btrfs_find_delayed_ref_head(trans, bytenr);
755 if (head) {
756 if (!mutex_trylock(&head->mutex)) {
757 atomic_inc(&head->node.refs);
758 spin_unlock(&delayed_refs->lock);
759
David Sterbab3b4aa72011-04-21 01:20:15 +0200760 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400761
David Sterba8cc33e52011-05-02 15:29:25 +0200762 /*
763 * Mutex was contended, block until it's released and try
764 * again
765 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400766 mutex_lock(&head->mutex);
767 mutex_unlock(&head->mutex);
768 btrfs_put_delayed_ref(&head->node);
769 goto again;
770 }
771 if (head->extent_op && head->extent_op->update_flags)
772 extent_flags |= head->extent_op->flags_to_set;
773 else
774 BUG_ON(num_refs == 0);
775
776 num_refs += head->node.ref_mod;
777 mutex_unlock(&head->mutex);
778 }
779 spin_unlock(&delayed_refs->lock);
780out:
781 WARN_ON(num_refs == 0);
782 if (refs)
783 *refs = num_refs;
784 if (flags)
785 *flags = extent_flags;
786out_free:
787 btrfs_free_path(path);
788 return ret;
789}
790
791/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500792 * Back reference rules. Back refs have three main goals:
793 *
794 * 1) differentiate between all holders of references to an extent so that
795 * when a reference is dropped we can make sure it was a valid reference
796 * before freeing the extent.
797 *
798 * 2) Provide enough information to quickly find the holders of an extent
799 * if we notice a given block is corrupted or bad.
800 *
801 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
802 * maintenance. This is actually the same as #2, but with a slightly
803 * different use case.
804 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400805 * There are two kinds of back refs. The implicit back refs is optimized
806 * for pointers in non-shared tree blocks. For a given pointer in a block,
807 * back refs of this kind provide information about the block's owner tree
808 * and the pointer's key. These information allow us to find the block by
809 * b-tree searching. The full back refs is for pointers in tree blocks not
810 * referenced by their owner trees. The location of tree block is recorded
811 * in the back refs. Actually the full back refs is generic, and can be
812 * used in all cases the implicit back refs is used. The major shortcoming
813 * of the full back refs is its overhead. Every time a tree block gets
814 * COWed, we have to update back refs entry for all pointers in it.
815 *
816 * For a newly allocated tree block, we use implicit back refs for
817 * pointers in it. This means most tree related operations only involve
818 * implicit back refs. For a tree block created in old transaction, the
819 * only way to drop a reference to it is COW it. So we can detect the
820 * event that tree block loses its owner tree's reference and do the
821 * back refs conversion.
822 *
823 * When a tree block is COW'd through a tree, there are four cases:
824 *
825 * The reference count of the block is one and the tree is the block's
826 * owner tree. Nothing to do in this case.
827 *
828 * The reference count of the block is one and the tree is not the
829 * block's owner tree. In this case, full back refs is used for pointers
830 * in the block. Remove these full back refs, add implicit back refs for
831 * every pointers in the new block.
832 *
833 * The reference count of the block is greater than one and the tree is
834 * the block's owner tree. In this case, implicit back refs is used for
835 * pointers in the block. Add full back refs for every pointers in the
836 * block, increase lower level extents' reference counts. The original
837 * implicit back refs are entailed to the new block.
838 *
839 * The reference count of the block is greater than one and the tree is
840 * not the block's owner tree. Add implicit back refs for every pointer in
841 * the new block, increase lower level extents' reference count.
842 *
843 * Back Reference Key composing:
844 *
845 * The key objectid corresponds to the first byte in the extent,
846 * The key type is used to differentiate between types of back refs.
847 * There are different meanings of the key offset for different types
848 * of back refs.
849 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500850 * File extents can be referenced by:
851 *
852 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400853 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500854 * - different offsets inside a file (bookend extents in file.c)
855 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400856 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500857 *
858 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500859 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400860 * - original offset in the file
861 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400862 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400863 * The key offset for the implicit back refs is hash of the first
864 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500865 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500867 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400868 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500869 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400870 * The key offset for the implicit back refs is the first byte of
871 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500872 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400873 * When a file extent is allocated, The implicit back refs is used.
874 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500875 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400876 * (root_key.objectid, inode objectid, offset in file, 1)
877 *
878 * When a file extent is removed file truncation, we find the
879 * corresponding implicit back refs and check the following fields:
880 *
881 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500882 *
883 * Btree extents can be referenced by:
884 *
885 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500886 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400887 * Both the implicit back refs and the full back refs for tree blocks
888 * only consist of key. The key offset for the implicit back refs is
889 * objectid of block's owner tree. The key offset for the full back refs
890 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500891 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400892 * When implicit back refs is used, information about the lowest key and
893 * level of the tree block are required. These information are stored in
894 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500895 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400896
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400897#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
898static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
899 struct btrfs_root *root,
900 struct btrfs_path *path,
901 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500902{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400903 struct btrfs_extent_item *item;
904 struct btrfs_extent_item_v0 *ei0;
905 struct btrfs_extent_ref_v0 *ref0;
906 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400907 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400908 struct btrfs_key key;
909 struct btrfs_key found_key;
910 u32 new_size = sizeof(*item);
911 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500912 int ret;
913
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400914 leaf = path->nodes[0];
915 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500916
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400917 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
918 ei0 = btrfs_item_ptr(leaf, path->slots[0],
919 struct btrfs_extent_item_v0);
920 refs = btrfs_extent_refs_v0(leaf, ei0);
921
922 if (owner == (u64)-1) {
923 while (1) {
924 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
925 ret = btrfs_next_leaf(root, path);
926 if (ret < 0)
927 return ret;
928 BUG_ON(ret > 0);
929 leaf = path->nodes[0];
930 }
931 btrfs_item_key_to_cpu(leaf, &found_key,
932 path->slots[0]);
933 BUG_ON(key.objectid != found_key.objectid);
934 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
935 path->slots[0]++;
936 continue;
937 }
938 ref0 = btrfs_item_ptr(leaf, path->slots[0],
939 struct btrfs_extent_ref_v0);
940 owner = btrfs_ref_objectid_v0(leaf, ref0);
941 break;
942 }
943 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200944 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945
946 if (owner < BTRFS_FIRST_FREE_OBJECTID)
947 new_size += sizeof(*bi);
948
949 new_size -= sizeof(*ei0);
950 ret = btrfs_search_slot(trans, root, &key, path,
951 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400952 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400953 return ret;
954 BUG_ON(ret);
955
956 ret = btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400957
958 leaf = path->nodes[0];
959 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
960 btrfs_set_extent_refs(leaf, item, refs);
961 /* FIXME: get real generation */
962 btrfs_set_extent_generation(leaf, item, 0);
963 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
964 btrfs_set_extent_flags(leaf, item,
965 BTRFS_EXTENT_FLAG_TREE_BLOCK |
966 BTRFS_BLOCK_FLAG_FULL_BACKREF);
967 bi = (struct btrfs_tree_block_info *)(item + 1);
968 /* FIXME: get first key of the block */
969 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
970 btrfs_set_tree_block_level(leaf, bi, (int)owner);
971 } else {
972 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
973 }
974 btrfs_mark_buffer_dirty(leaf);
975 return 0;
976}
977#endif
978
979static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
980{
981 u32 high_crc = ~(u32)0;
982 u32 low_crc = ~(u32)0;
983 __le64 lenum;
984
985 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100986 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400987 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100988 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400989 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100990 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400991
992 return ((u64)high_crc << 31) ^ (u64)low_crc;
993}
994
995static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
996 struct btrfs_extent_data_ref *ref)
997{
998 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
999 btrfs_extent_data_ref_objectid(leaf, ref),
1000 btrfs_extent_data_ref_offset(leaf, ref));
1001}
1002
1003static int match_extent_data_ref(struct extent_buffer *leaf,
1004 struct btrfs_extent_data_ref *ref,
1005 u64 root_objectid, u64 owner, u64 offset)
1006{
1007 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1008 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1009 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1010 return 0;
1011 return 1;
1012}
1013
1014static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1015 struct btrfs_root *root,
1016 struct btrfs_path *path,
1017 u64 bytenr, u64 parent,
1018 u64 root_objectid,
1019 u64 owner, u64 offset)
1020{
1021 struct btrfs_key key;
1022 struct btrfs_extent_data_ref *ref;
1023 struct extent_buffer *leaf;
1024 u32 nritems;
1025 int ret;
1026 int recow;
1027 int err = -ENOENT;
1028
1029 key.objectid = bytenr;
1030 if (parent) {
1031 key.type = BTRFS_SHARED_DATA_REF_KEY;
1032 key.offset = parent;
1033 } else {
1034 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1035 key.offset = hash_extent_data_ref(root_objectid,
1036 owner, offset);
1037 }
1038again:
1039 recow = 0;
1040 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1041 if (ret < 0) {
1042 err = ret;
1043 goto fail;
1044 }
1045
1046 if (parent) {
1047 if (!ret)
1048 return 0;
1049#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1050 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001051 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001052 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1053 if (ret < 0) {
1054 err = ret;
1055 goto fail;
1056 }
1057 if (!ret)
1058 return 0;
1059#endif
1060 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001061 }
1062
1063 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001064 nritems = btrfs_header_nritems(leaf);
1065 while (1) {
1066 if (path->slots[0] >= nritems) {
1067 ret = btrfs_next_leaf(root, path);
1068 if (ret < 0)
1069 err = ret;
1070 if (ret)
1071 goto fail;
1072
1073 leaf = path->nodes[0];
1074 nritems = btrfs_header_nritems(leaf);
1075 recow = 1;
1076 }
1077
1078 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1079 if (key.objectid != bytenr ||
1080 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1081 goto fail;
1082
1083 ref = btrfs_item_ptr(leaf, path->slots[0],
1084 struct btrfs_extent_data_ref);
1085
1086 if (match_extent_data_ref(leaf, ref, root_objectid,
1087 owner, offset)) {
1088 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001089 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090 goto again;
1091 }
1092 err = 0;
1093 break;
1094 }
1095 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001096 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001097fail:
1098 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001099}
1100
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001101static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1102 struct btrfs_root *root,
1103 struct btrfs_path *path,
1104 u64 bytenr, u64 parent,
1105 u64 root_objectid, u64 owner,
1106 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001107{
1108 struct btrfs_key key;
1109 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001110 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001111 u32 num_refs;
1112 int ret;
1113
1114 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001115 if (parent) {
1116 key.type = BTRFS_SHARED_DATA_REF_KEY;
1117 key.offset = parent;
1118 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001119 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001120 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1121 key.offset = hash_extent_data_ref(root_objectid,
1122 owner, offset);
1123 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001124 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001125
1126 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1127 if (ret && ret != -EEXIST)
1128 goto fail;
1129
1130 leaf = path->nodes[0];
1131 if (parent) {
1132 struct btrfs_shared_data_ref *ref;
1133 ref = btrfs_item_ptr(leaf, path->slots[0],
1134 struct btrfs_shared_data_ref);
1135 if (ret == 0) {
1136 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1137 } else {
1138 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1139 num_refs += refs_to_add;
1140 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1141 }
1142 } else {
1143 struct btrfs_extent_data_ref *ref;
1144 while (ret == -EEXIST) {
1145 ref = btrfs_item_ptr(leaf, path->slots[0],
1146 struct btrfs_extent_data_ref);
1147 if (match_extent_data_ref(leaf, ref, root_objectid,
1148 owner, offset))
1149 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001150 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001151 key.offset++;
1152 ret = btrfs_insert_empty_item(trans, root, path, &key,
1153 size);
1154 if (ret && ret != -EEXIST)
1155 goto fail;
1156
1157 leaf = path->nodes[0];
1158 }
1159 ref = btrfs_item_ptr(leaf, path->slots[0],
1160 struct btrfs_extent_data_ref);
1161 if (ret == 0) {
1162 btrfs_set_extent_data_ref_root(leaf, ref,
1163 root_objectid);
1164 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1165 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1166 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1167 } else {
1168 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1169 num_refs += refs_to_add;
1170 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1171 }
1172 }
1173 btrfs_mark_buffer_dirty(leaf);
1174 ret = 0;
1175fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001176 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001177 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001178}
1179
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001180static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1181 struct btrfs_root *root,
1182 struct btrfs_path *path,
1183 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001184{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001185 struct btrfs_key key;
1186 struct btrfs_extent_data_ref *ref1 = NULL;
1187 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001188 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001189 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001190 int ret = 0;
1191
1192 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001193 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1194
1195 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1196 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1197 struct btrfs_extent_data_ref);
1198 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1199 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1200 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1201 struct btrfs_shared_data_ref);
1202 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1203#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1204 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1205 struct btrfs_extent_ref_v0 *ref0;
1206 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1207 struct btrfs_extent_ref_v0);
1208 num_refs = btrfs_ref_count_v0(leaf, ref0);
1209#endif
1210 } else {
1211 BUG();
1212 }
1213
Chris Mason56bec292009-03-13 10:10:06 -04001214 BUG_ON(num_refs < refs_to_drop);
1215 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001216
Zheng Yan31840ae2008-09-23 13:14:14 -04001217 if (num_refs == 0) {
1218 ret = btrfs_del_item(trans, root, path);
1219 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001220 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1221 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1222 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1223 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1224#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1225 else {
1226 struct btrfs_extent_ref_v0 *ref0;
1227 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1228 struct btrfs_extent_ref_v0);
1229 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1230 }
1231#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001232 btrfs_mark_buffer_dirty(leaf);
1233 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001234 return ret;
1235}
1236
1237static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1238 struct btrfs_path *path,
1239 struct btrfs_extent_inline_ref *iref)
1240{
1241 struct btrfs_key key;
1242 struct extent_buffer *leaf;
1243 struct btrfs_extent_data_ref *ref1;
1244 struct btrfs_shared_data_ref *ref2;
1245 u32 num_refs = 0;
1246
1247 leaf = path->nodes[0];
1248 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1249 if (iref) {
1250 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1251 BTRFS_EXTENT_DATA_REF_KEY) {
1252 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1253 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1254 } else {
1255 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1256 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1257 }
1258 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1259 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1260 struct btrfs_extent_data_ref);
1261 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1262 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1263 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1264 struct btrfs_shared_data_ref);
1265 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1266#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1267 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1268 struct btrfs_extent_ref_v0 *ref0;
1269 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1270 struct btrfs_extent_ref_v0);
1271 num_refs = btrfs_ref_count_v0(leaf, ref0);
1272#endif
1273 } else {
1274 WARN_ON(1);
1275 }
1276 return num_refs;
1277}
1278
1279static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1280 struct btrfs_root *root,
1281 struct btrfs_path *path,
1282 u64 bytenr, u64 parent,
1283 u64 root_objectid)
1284{
1285 struct btrfs_key key;
1286 int ret;
1287
1288 key.objectid = bytenr;
1289 if (parent) {
1290 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1291 key.offset = parent;
1292 } else {
1293 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1294 key.offset = root_objectid;
1295 }
1296
1297 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1298 if (ret > 0)
1299 ret = -ENOENT;
1300#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1301 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001302 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001303 key.type = BTRFS_EXTENT_REF_V0_KEY;
1304 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1305 if (ret > 0)
1306 ret = -ENOENT;
1307 }
1308#endif
1309 return ret;
1310}
1311
1312static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1313 struct btrfs_root *root,
1314 struct btrfs_path *path,
1315 u64 bytenr, u64 parent,
1316 u64 root_objectid)
1317{
1318 struct btrfs_key key;
1319 int ret;
1320
1321 key.objectid = bytenr;
1322 if (parent) {
1323 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1324 key.offset = parent;
1325 } else {
1326 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1327 key.offset = root_objectid;
1328 }
1329
1330 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001331 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001332 return ret;
1333}
1334
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001335static inline int extent_ref_type(u64 parent, u64 owner)
1336{
1337 int type;
1338 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1339 if (parent > 0)
1340 type = BTRFS_SHARED_BLOCK_REF_KEY;
1341 else
1342 type = BTRFS_TREE_BLOCK_REF_KEY;
1343 } else {
1344 if (parent > 0)
1345 type = BTRFS_SHARED_DATA_REF_KEY;
1346 else
1347 type = BTRFS_EXTENT_DATA_REF_KEY;
1348 }
1349 return type;
1350}
1351
Yan Zheng2c47e6052009-06-27 21:07:35 -04001352static int find_next_key(struct btrfs_path *path, int level,
1353 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001354
1355{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001356 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001357 if (!path->nodes[level])
1358 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001359 if (path->slots[level] + 1 >=
1360 btrfs_header_nritems(path->nodes[level]))
1361 continue;
1362 if (level == 0)
1363 btrfs_item_key_to_cpu(path->nodes[level], key,
1364 path->slots[level] + 1);
1365 else
1366 btrfs_node_key_to_cpu(path->nodes[level], key,
1367 path->slots[level] + 1);
1368 return 0;
1369 }
1370 return 1;
1371}
1372
1373/*
1374 * look for inline back ref. if back ref is found, *ref_ret is set
1375 * to the address of inline back ref, and 0 is returned.
1376 *
1377 * if back ref isn't found, *ref_ret is set to the address where it
1378 * should be inserted, and -ENOENT is returned.
1379 *
1380 * if insert is true and there are too many inline back refs, the path
1381 * points to the extent item, and -EAGAIN is returned.
1382 *
1383 * NOTE: inline back refs are ordered in the same way that back ref
1384 * items in the tree are ordered.
1385 */
1386static noinline_for_stack
1387int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1388 struct btrfs_root *root,
1389 struct btrfs_path *path,
1390 struct btrfs_extent_inline_ref **ref_ret,
1391 u64 bytenr, u64 num_bytes,
1392 u64 parent, u64 root_objectid,
1393 u64 owner, u64 offset, int insert)
1394{
1395 struct btrfs_key key;
1396 struct extent_buffer *leaf;
1397 struct btrfs_extent_item *ei;
1398 struct btrfs_extent_inline_ref *iref;
1399 u64 flags;
1400 u64 item_size;
1401 unsigned long ptr;
1402 unsigned long end;
1403 int extra_size;
1404 int type;
1405 int want;
1406 int ret;
1407 int err = 0;
1408
1409 key.objectid = bytenr;
1410 key.type = BTRFS_EXTENT_ITEM_KEY;
1411 key.offset = num_bytes;
1412
1413 want = extent_ref_type(parent, owner);
1414 if (insert) {
1415 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001416 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001417 } else
1418 extra_size = -1;
1419 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1420 if (ret < 0) {
1421 err = ret;
1422 goto out;
1423 }
1424 BUG_ON(ret);
1425
1426 leaf = path->nodes[0];
1427 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1428#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1429 if (item_size < sizeof(*ei)) {
1430 if (!insert) {
1431 err = -ENOENT;
1432 goto out;
1433 }
1434 ret = convert_extent_item_v0(trans, root, path, owner,
1435 extra_size);
1436 if (ret < 0) {
1437 err = ret;
1438 goto out;
1439 }
1440 leaf = path->nodes[0];
1441 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1442 }
1443#endif
1444 BUG_ON(item_size < sizeof(*ei));
1445
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001446 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1447 flags = btrfs_extent_flags(leaf, ei);
1448
1449 ptr = (unsigned long)(ei + 1);
1450 end = (unsigned long)ei + item_size;
1451
1452 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1453 ptr += sizeof(struct btrfs_tree_block_info);
1454 BUG_ON(ptr > end);
1455 } else {
1456 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1457 }
1458
1459 err = -ENOENT;
1460 while (1) {
1461 if (ptr >= end) {
1462 WARN_ON(ptr > end);
1463 break;
1464 }
1465 iref = (struct btrfs_extent_inline_ref *)ptr;
1466 type = btrfs_extent_inline_ref_type(leaf, iref);
1467 if (want < type)
1468 break;
1469 if (want > type) {
1470 ptr += btrfs_extent_inline_ref_size(type);
1471 continue;
1472 }
1473
1474 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1475 struct btrfs_extent_data_ref *dref;
1476 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1477 if (match_extent_data_ref(leaf, dref, root_objectid,
1478 owner, offset)) {
1479 err = 0;
1480 break;
1481 }
1482 if (hash_extent_data_ref_item(leaf, dref) <
1483 hash_extent_data_ref(root_objectid, owner, offset))
1484 break;
1485 } else {
1486 u64 ref_offset;
1487 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1488 if (parent > 0) {
1489 if (parent == ref_offset) {
1490 err = 0;
1491 break;
1492 }
1493 if (ref_offset < parent)
1494 break;
1495 } else {
1496 if (root_objectid == ref_offset) {
1497 err = 0;
1498 break;
1499 }
1500 if (ref_offset < root_objectid)
1501 break;
1502 }
1503 }
1504 ptr += btrfs_extent_inline_ref_size(type);
1505 }
1506 if (err == -ENOENT && insert) {
1507 if (item_size + extra_size >=
1508 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1509 err = -EAGAIN;
1510 goto out;
1511 }
1512 /*
1513 * To add new inline back ref, we have to make sure
1514 * there is no corresponding back ref item.
1515 * For simplicity, we just do not add new inline back
1516 * ref if there is any kind of item for this block
1517 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001518 if (find_next_key(path, 0, &key) == 0 &&
1519 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001520 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001521 err = -EAGAIN;
1522 goto out;
1523 }
1524 }
1525 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1526out:
Yan Zheng85d41982009-06-11 08:51:10 -04001527 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001528 path->keep_locks = 0;
1529 btrfs_unlock_up_safe(path, 1);
1530 }
1531 return err;
1532}
1533
1534/*
1535 * helper to add new inline back ref
1536 */
1537static noinline_for_stack
1538int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1539 struct btrfs_root *root,
1540 struct btrfs_path *path,
1541 struct btrfs_extent_inline_ref *iref,
1542 u64 parent, u64 root_objectid,
1543 u64 owner, u64 offset, int refs_to_add,
1544 struct btrfs_delayed_extent_op *extent_op)
1545{
1546 struct extent_buffer *leaf;
1547 struct btrfs_extent_item *ei;
1548 unsigned long ptr;
1549 unsigned long end;
1550 unsigned long item_offset;
1551 u64 refs;
1552 int size;
1553 int type;
1554 int ret;
1555
1556 leaf = path->nodes[0];
1557 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1558 item_offset = (unsigned long)iref - (unsigned long)ei;
1559
1560 type = extent_ref_type(parent, owner);
1561 size = btrfs_extent_inline_ref_size(type);
1562
1563 ret = btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001564
1565 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1566 refs = btrfs_extent_refs(leaf, ei);
1567 refs += refs_to_add;
1568 btrfs_set_extent_refs(leaf, ei, refs);
1569 if (extent_op)
1570 __run_delayed_extent_op(extent_op, leaf, ei);
1571
1572 ptr = (unsigned long)ei + item_offset;
1573 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1574 if (ptr < end - size)
1575 memmove_extent_buffer(leaf, ptr + size, ptr,
1576 end - size - ptr);
1577
1578 iref = (struct btrfs_extent_inline_ref *)ptr;
1579 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1580 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1581 struct btrfs_extent_data_ref *dref;
1582 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1583 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1584 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1585 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1586 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1587 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1588 struct btrfs_shared_data_ref *sref;
1589 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1590 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1591 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1592 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1593 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1594 } else {
1595 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1596 }
1597 btrfs_mark_buffer_dirty(leaf);
1598 return 0;
1599}
1600
1601static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1602 struct btrfs_root *root,
1603 struct btrfs_path *path,
1604 struct btrfs_extent_inline_ref **ref_ret,
1605 u64 bytenr, u64 num_bytes, u64 parent,
1606 u64 root_objectid, u64 owner, u64 offset)
1607{
1608 int ret;
1609
1610 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1611 bytenr, num_bytes, parent,
1612 root_objectid, owner, offset, 0);
1613 if (ret != -ENOENT)
1614 return ret;
1615
David Sterbab3b4aa72011-04-21 01:20:15 +02001616 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001617 *ref_ret = NULL;
1618
1619 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1620 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1621 root_objectid);
1622 } else {
1623 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1624 root_objectid, owner, offset);
1625 }
1626 return ret;
1627}
1628
1629/*
1630 * helper to update/remove inline back ref
1631 */
1632static noinline_for_stack
1633int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1634 struct btrfs_root *root,
1635 struct btrfs_path *path,
1636 struct btrfs_extent_inline_ref *iref,
1637 int refs_to_mod,
1638 struct btrfs_delayed_extent_op *extent_op)
1639{
1640 struct extent_buffer *leaf;
1641 struct btrfs_extent_item *ei;
1642 struct btrfs_extent_data_ref *dref = NULL;
1643 struct btrfs_shared_data_ref *sref = NULL;
1644 unsigned long ptr;
1645 unsigned long end;
1646 u32 item_size;
1647 int size;
1648 int type;
1649 int ret;
1650 u64 refs;
1651
1652 leaf = path->nodes[0];
1653 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1654 refs = btrfs_extent_refs(leaf, ei);
1655 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1656 refs += refs_to_mod;
1657 btrfs_set_extent_refs(leaf, ei, refs);
1658 if (extent_op)
1659 __run_delayed_extent_op(extent_op, leaf, ei);
1660
1661 type = btrfs_extent_inline_ref_type(leaf, iref);
1662
1663 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1664 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1665 refs = btrfs_extent_data_ref_count(leaf, dref);
1666 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1667 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1668 refs = btrfs_shared_data_ref_count(leaf, sref);
1669 } else {
1670 refs = 1;
1671 BUG_ON(refs_to_mod != -1);
1672 }
1673
1674 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1675 refs += refs_to_mod;
1676
1677 if (refs > 0) {
1678 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1679 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1680 else
1681 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1682 } else {
1683 size = btrfs_extent_inline_ref_size(type);
1684 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1685 ptr = (unsigned long)iref;
1686 end = (unsigned long)ei + item_size;
1687 if (ptr + size < end)
1688 memmove_extent_buffer(leaf, ptr, ptr + size,
1689 end - ptr - size);
1690 item_size -= size;
1691 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001692 }
1693 btrfs_mark_buffer_dirty(leaf);
1694 return 0;
1695}
1696
1697static noinline_for_stack
1698int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1699 struct btrfs_root *root,
1700 struct btrfs_path *path,
1701 u64 bytenr, u64 num_bytes, u64 parent,
1702 u64 root_objectid, u64 owner,
1703 u64 offset, int refs_to_add,
1704 struct btrfs_delayed_extent_op *extent_op)
1705{
1706 struct btrfs_extent_inline_ref *iref;
1707 int ret;
1708
1709 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1710 bytenr, num_bytes, parent,
1711 root_objectid, owner, offset, 1);
1712 if (ret == 0) {
1713 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1714 ret = update_inline_extent_backref(trans, root, path, iref,
1715 refs_to_add, extent_op);
1716 } else if (ret == -ENOENT) {
1717 ret = setup_inline_extent_backref(trans, root, path, iref,
1718 parent, root_objectid,
1719 owner, offset, refs_to_add,
1720 extent_op);
1721 }
1722 return ret;
1723}
1724
1725static int insert_extent_backref(struct btrfs_trans_handle *trans,
1726 struct btrfs_root *root,
1727 struct btrfs_path *path,
1728 u64 bytenr, u64 parent, u64 root_objectid,
1729 u64 owner, u64 offset, int refs_to_add)
1730{
1731 int ret;
1732 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1733 BUG_ON(refs_to_add != 1);
1734 ret = insert_tree_block_ref(trans, root, path, bytenr,
1735 parent, root_objectid);
1736 } else {
1737 ret = insert_extent_data_ref(trans, root, path, bytenr,
1738 parent, root_objectid,
1739 owner, offset, refs_to_add);
1740 }
1741 return ret;
1742}
1743
1744static int remove_extent_backref(struct btrfs_trans_handle *trans,
1745 struct btrfs_root *root,
1746 struct btrfs_path *path,
1747 struct btrfs_extent_inline_ref *iref,
1748 int refs_to_drop, int is_data)
1749{
1750 int ret;
1751
1752 BUG_ON(!is_data && refs_to_drop != 1);
1753 if (iref) {
1754 ret = update_inline_extent_backref(trans, root, path, iref,
1755 -refs_to_drop, NULL);
1756 } else if (is_data) {
1757 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1758 } else {
1759 ret = btrfs_del_item(trans, root, path);
1760 }
1761 return ret;
1762}
1763
Li Dongyang5378e602011-03-24 10:24:27 +00001764static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001765 u64 start, u64 len)
1766{
Li Dongyang5378e602011-03-24 10:24:27 +00001767 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001768}
Chris Mason15916de2008-11-19 21:17:22 -05001769
Liu Hui1f3c79a2009-01-05 15:57:51 -05001770static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001771 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001772{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001773 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001774 u64 discarded_bytes = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001775 struct btrfs_multi_bio *multi = NULL;
1776
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001777
Liu Hui1f3c79a2009-01-05 15:57:51 -05001778 /* Tell the block device(s) that the sectors can be discarded */
Li Dongyang5378e602011-03-24 10:24:27 +00001779 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1780 bytenr, &num_bytes, &multi, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001781 if (!ret) {
1782 struct btrfs_bio_stripe *stripe = multi->stripes;
1783 int i;
1784
Liu Hui1f3c79a2009-01-05 15:57:51 -05001785
1786 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Li Dongyang5378e602011-03-24 10:24:27 +00001787 ret = btrfs_issue_discard(stripe->dev->bdev,
1788 stripe->physical,
1789 stripe->length);
1790 if (!ret)
1791 discarded_bytes += stripe->length;
1792 else if (ret != -EOPNOTSUPP)
1793 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001794 }
1795 kfree(multi);
1796 }
Li Dongyang5378e602011-03-24 10:24:27 +00001797 if (discarded_bytes && ret == -EOPNOTSUPP)
1798 ret = 0;
1799
1800 if (actual_bytes)
1801 *actual_bytes = discarded_bytes;
1802
Liu Hui1f3c79a2009-01-05 15:57:51 -05001803
1804 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001805}
1806
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001807int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1808 struct btrfs_root *root,
1809 u64 bytenr, u64 num_bytes, u64 parent,
1810 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001811{
1812 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001813 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1814 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001815
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001816 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1817 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1818 parent, root_objectid, (int)owner,
1819 BTRFS_ADD_DELAYED_REF, NULL);
1820 } else {
1821 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1822 parent, root_objectid, owner, offset,
1823 BTRFS_ADD_DELAYED_REF, NULL);
1824 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001825 return ret;
1826}
1827
Chris Mason925baed2008-06-25 16:01:30 -04001828static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001829 struct btrfs_root *root,
1830 u64 bytenr, u64 num_bytes,
1831 u64 parent, u64 root_objectid,
1832 u64 owner, u64 offset, int refs_to_add,
1833 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001834{
Chris Mason5caf2a02007-04-02 11:20:42 -04001835 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001836 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001837 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001838 u64 refs;
1839 int ret;
1840 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001841
Chris Mason5caf2a02007-04-02 11:20:42 -04001842 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001843 if (!path)
1844 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001845
Chris Mason3c12ac72008-04-21 12:01:38 -04001846 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001847 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001848 /* this will setup the path even if it fails to insert the back ref */
1849 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1850 path, bytenr, num_bytes, parent,
1851 root_objectid, owner, offset,
1852 refs_to_add, extent_op);
1853 if (ret == 0)
1854 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001855
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001856 if (ret != -EAGAIN) {
1857 err = ret;
1858 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001859 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001860
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001861 leaf = path->nodes[0];
1862 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1863 refs = btrfs_extent_refs(leaf, item);
1864 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1865 if (extent_op)
1866 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001868 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001869 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001870
Chris Mason3c12ac72008-04-21 12:01:38 -04001871 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001872 path->leave_spinning = 1;
1873
Chris Mason56bec292009-03-13 10:10:06 -04001874 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001875 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876 path, bytenr, parent, root_objectid,
1877 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001878 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001879out:
Chris Mason74493f72007-12-11 09:25:06 -05001880 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001881 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001882}
1883
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001884static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1885 struct btrfs_root *root,
1886 struct btrfs_delayed_ref_node *node,
1887 struct btrfs_delayed_extent_op *extent_op,
1888 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001889{
Chris Mason56bec292009-03-13 10:10:06 -04001890 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001891 struct btrfs_delayed_data_ref *ref;
1892 struct btrfs_key ins;
1893 u64 parent = 0;
1894 u64 ref_root = 0;
1895 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001896
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001897 ins.objectid = node->bytenr;
1898 ins.offset = node->num_bytes;
1899 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001900
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001901 ref = btrfs_delayed_node_to_data_ref(node);
1902 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1903 parent = ref->parent;
1904 else
1905 ref_root = ref->root;
1906
1907 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1908 if (extent_op) {
1909 BUG_ON(extent_op->update_key);
1910 flags |= extent_op->flags_to_set;
1911 }
1912 ret = alloc_reserved_file_extent(trans, root,
1913 parent, ref_root, flags,
1914 ref->objectid, ref->offset,
1915 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001916 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1917 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1918 node->num_bytes, parent,
1919 ref_root, ref->objectid,
1920 ref->offset, node->ref_mod,
1921 extent_op);
1922 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1923 ret = __btrfs_free_extent(trans, root, node->bytenr,
1924 node->num_bytes, parent,
1925 ref_root, ref->objectid,
1926 ref->offset, node->ref_mod,
1927 extent_op);
1928 } else {
1929 BUG();
1930 }
Chris Mason56bec292009-03-13 10:10:06 -04001931 return ret;
1932}
1933
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001934static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1935 struct extent_buffer *leaf,
1936 struct btrfs_extent_item *ei)
1937{
1938 u64 flags = btrfs_extent_flags(leaf, ei);
1939 if (extent_op->update_flags) {
1940 flags |= extent_op->flags_to_set;
1941 btrfs_set_extent_flags(leaf, ei, flags);
1942 }
1943
1944 if (extent_op->update_key) {
1945 struct btrfs_tree_block_info *bi;
1946 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1947 bi = (struct btrfs_tree_block_info *)(ei + 1);
1948 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1949 }
1950}
1951
1952static int run_delayed_extent_op(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{
1957 struct btrfs_key key;
1958 struct btrfs_path *path;
1959 struct btrfs_extent_item *ei;
1960 struct extent_buffer *leaf;
1961 u32 item_size;
1962 int ret;
1963 int err = 0;
1964
1965 path = btrfs_alloc_path();
1966 if (!path)
1967 return -ENOMEM;
1968
1969 key.objectid = node->bytenr;
1970 key.type = BTRFS_EXTENT_ITEM_KEY;
1971 key.offset = node->num_bytes;
1972
1973 path->reada = 1;
1974 path->leave_spinning = 1;
1975 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1976 path, 0, 1);
1977 if (ret < 0) {
1978 err = ret;
1979 goto out;
1980 }
1981 if (ret > 0) {
1982 err = -EIO;
1983 goto out;
1984 }
1985
1986 leaf = path->nodes[0];
1987 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1988#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1989 if (item_size < sizeof(*ei)) {
1990 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1991 path, (u64)-1, 0);
1992 if (ret < 0) {
1993 err = ret;
1994 goto out;
1995 }
1996 leaf = path->nodes[0];
1997 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1998 }
1999#endif
2000 BUG_ON(item_size < sizeof(*ei));
2001 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2002 __run_delayed_extent_op(extent_op, leaf, ei);
2003
2004 btrfs_mark_buffer_dirty(leaf);
2005out:
2006 btrfs_free_path(path);
2007 return err;
2008}
2009
2010static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2011 struct btrfs_root *root,
2012 struct btrfs_delayed_ref_node *node,
2013 struct btrfs_delayed_extent_op *extent_op,
2014 int insert_reserved)
2015{
2016 int ret = 0;
2017 struct btrfs_delayed_tree_ref *ref;
2018 struct btrfs_key ins;
2019 u64 parent = 0;
2020 u64 ref_root = 0;
2021
2022 ins.objectid = node->bytenr;
2023 ins.offset = node->num_bytes;
2024 ins.type = BTRFS_EXTENT_ITEM_KEY;
2025
2026 ref = btrfs_delayed_node_to_tree_ref(node);
2027 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2028 parent = ref->parent;
2029 else
2030 ref_root = ref->root;
2031
2032 BUG_ON(node->ref_mod != 1);
2033 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2034 BUG_ON(!extent_op || !extent_op->update_flags ||
2035 !extent_op->update_key);
2036 ret = alloc_reserved_tree_block(trans, root,
2037 parent, ref_root,
2038 extent_op->flags_to_set,
2039 &extent_op->key,
2040 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002041 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2042 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2043 node->num_bytes, parent, ref_root,
2044 ref->level, 0, 1, extent_op);
2045 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2046 ret = __btrfs_free_extent(trans, root, node->bytenr,
2047 node->num_bytes, parent, ref_root,
2048 ref->level, 0, 1, extent_op);
2049 } else {
2050 BUG();
2051 }
2052 return ret;
2053}
2054
Chris Mason56bec292009-03-13 10:10:06 -04002055/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002056static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2057 struct btrfs_root *root,
2058 struct btrfs_delayed_ref_node *node,
2059 struct btrfs_delayed_extent_op *extent_op,
2060 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002061{
Josef Bacikeb099672009-02-12 09:27:38 -05002062 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002063 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002064 struct btrfs_delayed_ref_head *head;
2065 /*
2066 * we've hit the end of the chain and we were supposed
2067 * to insert this extent into the tree. But, it got
2068 * deleted before we ever needed to insert it, so all
2069 * we have to do is clean up the accounting
2070 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002071 BUG_ON(extent_op);
2072 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002073 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002074 btrfs_pin_extent(root, node->bytenr,
2075 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002076 if (head->is_data) {
2077 ret = btrfs_del_csums(trans, root,
2078 node->bytenr,
2079 node->num_bytes);
2080 BUG_ON(ret);
2081 }
Chris Mason56bec292009-03-13 10:10:06 -04002082 }
Chris Mason56bec292009-03-13 10:10:06 -04002083 mutex_unlock(&head->mutex);
2084 return 0;
2085 }
Josef Bacikeb099672009-02-12 09:27:38 -05002086
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002087 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2088 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2089 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2090 insert_reserved);
2091 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2092 node->type == BTRFS_SHARED_DATA_REF_KEY)
2093 ret = run_delayed_data_ref(trans, root, node, extent_op,
2094 insert_reserved);
2095 else
2096 BUG();
2097 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002098}
2099
Chris Mason56bec292009-03-13 10:10:06 -04002100static noinline struct btrfs_delayed_ref_node *
2101select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002102{
Chris Mason56bec292009-03-13 10:10:06 -04002103 struct rb_node *node;
2104 struct btrfs_delayed_ref_node *ref;
2105 int action = BTRFS_ADD_DELAYED_REF;
2106again:
2107 /*
2108 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2109 * this prevents ref count from going down to zero when
2110 * there still are pending delayed ref.
2111 */
2112 node = rb_prev(&head->node.rb_node);
2113 while (1) {
2114 if (!node)
2115 break;
2116 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2117 rb_node);
2118 if (ref->bytenr != head->node.bytenr)
2119 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002120 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002121 return ref;
2122 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002123 }
Chris Mason56bec292009-03-13 10:10:06 -04002124 if (action == BTRFS_ADD_DELAYED_REF) {
2125 action = BTRFS_DROP_DELAYED_REF;
2126 goto again;
2127 }
2128 return NULL;
2129}
2130
Chris Masonc3e69d52009-03-13 10:17:05 -04002131static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2132 struct btrfs_root *root,
2133 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002134{
Chris Mason56bec292009-03-13 10:10:06 -04002135 struct btrfs_delayed_ref_root *delayed_refs;
2136 struct btrfs_delayed_ref_node *ref;
2137 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002138 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002139 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002140 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002141 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002142
2143 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002144 while (1) {
2145 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002146 /* pick a new head ref from the cluster list */
2147 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002148 break;
Chris Mason56bec292009-03-13 10:10:06 -04002149
Chris Masonc3e69d52009-03-13 10:17:05 -04002150 locked_ref = list_entry(cluster->next,
2151 struct btrfs_delayed_ref_head, cluster);
2152
2153 /* grab the lock that says we are going to process
2154 * all the refs for this head */
2155 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2156
2157 /*
2158 * we may have dropped the spin lock to get the head
2159 * mutex lock, and that might have given someone else
2160 * time to free the head. If that's true, it has been
2161 * removed from our list and we can move on.
2162 */
2163 if (ret == -EAGAIN) {
2164 locked_ref = NULL;
2165 count++;
2166 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002167 }
2168 }
2169
2170 /*
2171 * record the must insert reserved flag before we
2172 * drop the spin lock.
2173 */
2174 must_insert_reserved = locked_ref->must_insert_reserved;
2175 locked_ref->must_insert_reserved = 0;
2176
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002177 extent_op = locked_ref->extent_op;
2178 locked_ref->extent_op = NULL;
2179
Chris Mason56bec292009-03-13 10:10:06 -04002180 /*
2181 * locked_ref is the head node, so we have to go one
2182 * node back for any delayed ref updates
2183 */
Chris Mason56bec292009-03-13 10:10:06 -04002184 ref = select_delayed_ref(locked_ref);
2185 if (!ref) {
2186 /* All delayed refs have been processed, Go ahead
2187 * and send the head node to run_one_delayed_ref,
2188 * so that any accounting fixes can happen
2189 */
2190 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002191
2192 if (extent_op && must_insert_reserved) {
2193 kfree(extent_op);
2194 extent_op = NULL;
2195 }
2196
2197 if (extent_op) {
2198 spin_unlock(&delayed_refs->lock);
2199
2200 ret = run_delayed_extent_op(trans, root,
2201 ref, extent_op);
2202 BUG_ON(ret);
2203 kfree(extent_op);
2204
2205 cond_resched();
2206 spin_lock(&delayed_refs->lock);
2207 continue;
2208 }
2209
Chris Masonc3e69d52009-03-13 10:17:05 -04002210 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002211 locked_ref = NULL;
2212 }
2213
2214 ref->in_tree = 0;
2215 rb_erase(&ref->rb_node, &delayed_refs->root);
2216 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002217
Chris Mason56bec292009-03-13 10:10:06 -04002218 spin_unlock(&delayed_refs->lock);
2219
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002220 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002221 must_insert_reserved);
2222 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002223
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002224 btrfs_put_delayed_ref(ref);
2225 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002226 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002227
Chris Mason1887be62009-03-13 10:11:24 -04002228 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002229 spin_lock(&delayed_refs->lock);
2230 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002231 return count;
2232}
2233
2234/*
2235 * this starts processing the delayed reference count updates and
2236 * extent insertions we have queued up so far. count can be
2237 * 0, which means to process everything in the tree at the start
2238 * of the run (but not newly added entries), or it can be some target
2239 * number you'd like to process.
2240 */
2241int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2242 struct btrfs_root *root, unsigned long count)
2243{
2244 struct rb_node *node;
2245 struct btrfs_delayed_ref_root *delayed_refs;
2246 struct btrfs_delayed_ref_node *ref;
2247 struct list_head cluster;
2248 int ret;
2249 int run_all = count == (unsigned long)-1;
2250 int run_most = 0;
2251
2252 if (root == root->fs_info->extent_root)
2253 root = root->fs_info->tree_root;
2254
2255 delayed_refs = &trans->transaction->delayed_refs;
2256 INIT_LIST_HEAD(&cluster);
2257again:
2258 spin_lock(&delayed_refs->lock);
2259 if (count == 0) {
2260 count = delayed_refs->num_entries * 2;
2261 run_most = 1;
2262 }
2263 while (1) {
2264 if (!(run_all || run_most) &&
2265 delayed_refs->num_heads_ready < 64)
2266 break;
2267
2268 /*
2269 * go find something we can process in the rbtree. We start at
2270 * the beginning of the tree, and then build a cluster
2271 * of refs to process starting at the first one we are able to
2272 * lock
2273 */
2274 ret = btrfs_find_ref_cluster(trans, &cluster,
2275 delayed_refs->run_delayed_start);
2276 if (ret)
2277 break;
2278
2279 ret = run_clustered_refs(trans, root, &cluster);
2280 BUG_ON(ret < 0);
2281
2282 count -= min_t(unsigned long, ret, count);
2283
2284 if (count == 0)
2285 break;
2286 }
2287
Chris Mason56bec292009-03-13 10:10:06 -04002288 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002289 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002290 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002291 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002292 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002293
2294 while (node) {
2295 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2296 rb_node);
2297 if (btrfs_delayed_ref_is_head(ref)) {
2298 struct btrfs_delayed_ref_head *head;
2299
2300 head = btrfs_delayed_node_to_head(ref);
2301 atomic_inc(&ref->refs);
2302
2303 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002304 /*
2305 * Mutex was contended, block until it's
2306 * released and try again
2307 */
Chris Mason56bec292009-03-13 10:10:06 -04002308 mutex_lock(&head->mutex);
2309 mutex_unlock(&head->mutex);
2310
2311 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002312 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002313 goto again;
2314 }
2315 node = rb_next(node);
2316 }
2317 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002318 schedule_timeout(1);
2319 goto again;
2320 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002321out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002322 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002323 return 0;
2324}
2325
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002326int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2327 struct btrfs_root *root,
2328 u64 bytenr, u64 num_bytes, u64 flags,
2329 int is_data)
2330{
2331 struct btrfs_delayed_extent_op *extent_op;
2332 int ret;
2333
2334 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2335 if (!extent_op)
2336 return -ENOMEM;
2337
2338 extent_op->flags_to_set = flags;
2339 extent_op->update_flags = 1;
2340 extent_op->update_key = 0;
2341 extent_op->is_data = is_data ? 1 : 0;
2342
2343 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2344 if (ret)
2345 kfree(extent_op);
2346 return ret;
2347}
2348
2349static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2350 struct btrfs_root *root,
2351 struct btrfs_path *path,
2352 u64 objectid, u64 offset, u64 bytenr)
2353{
2354 struct btrfs_delayed_ref_head *head;
2355 struct btrfs_delayed_ref_node *ref;
2356 struct btrfs_delayed_data_ref *data_ref;
2357 struct btrfs_delayed_ref_root *delayed_refs;
2358 struct rb_node *node;
2359 int ret = 0;
2360
2361 ret = -ENOENT;
2362 delayed_refs = &trans->transaction->delayed_refs;
2363 spin_lock(&delayed_refs->lock);
2364 head = btrfs_find_delayed_ref_head(trans, bytenr);
2365 if (!head)
2366 goto out;
2367
2368 if (!mutex_trylock(&head->mutex)) {
2369 atomic_inc(&head->node.refs);
2370 spin_unlock(&delayed_refs->lock);
2371
David Sterbab3b4aa72011-04-21 01:20:15 +02002372 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002373
David Sterba8cc33e52011-05-02 15:29:25 +02002374 /*
2375 * Mutex was contended, block until it's released and let
2376 * caller try again
2377 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002378 mutex_lock(&head->mutex);
2379 mutex_unlock(&head->mutex);
2380 btrfs_put_delayed_ref(&head->node);
2381 return -EAGAIN;
2382 }
2383
2384 node = rb_prev(&head->node.rb_node);
2385 if (!node)
2386 goto out_unlock;
2387
2388 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2389
2390 if (ref->bytenr != bytenr)
2391 goto out_unlock;
2392
2393 ret = 1;
2394 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2395 goto out_unlock;
2396
2397 data_ref = btrfs_delayed_node_to_data_ref(ref);
2398
2399 node = rb_prev(node);
2400 if (node) {
2401 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2402 if (ref->bytenr == bytenr)
2403 goto out_unlock;
2404 }
2405
2406 if (data_ref->root != root->root_key.objectid ||
2407 data_ref->objectid != objectid || data_ref->offset != offset)
2408 goto out_unlock;
2409
2410 ret = 0;
2411out_unlock:
2412 mutex_unlock(&head->mutex);
2413out:
2414 spin_unlock(&delayed_refs->lock);
2415 return ret;
2416}
2417
2418static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2419 struct btrfs_root *root,
2420 struct btrfs_path *path,
2421 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002422{
2423 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002424 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002425 struct btrfs_extent_data_ref *ref;
2426 struct btrfs_extent_inline_ref *iref;
2427 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002428 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002429 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002430 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002431
Chris Masonbe20aa92007-12-17 20:14:01 -05002432 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002433 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002434 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002435
Chris Masonbe20aa92007-12-17 20:14:01 -05002436 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2437 if (ret < 0)
2438 goto out;
2439 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002440
2441 ret = -ENOENT;
2442 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002443 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002444
Zheng Yan31840ae2008-09-23 13:14:14 -04002445 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002446 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002447 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002448
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002449 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002450 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002451
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002452 ret = 1;
2453 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2454#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2455 if (item_size < sizeof(*ei)) {
2456 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2457 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002458 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002459#endif
2460 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2461
2462 if (item_size != sizeof(*ei) +
2463 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2464 goto out;
2465
2466 if (btrfs_extent_generation(leaf, ei) <=
2467 btrfs_root_last_snapshot(&root->root_item))
2468 goto out;
2469
2470 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2471 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2472 BTRFS_EXTENT_DATA_REF_KEY)
2473 goto out;
2474
2475 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2476 if (btrfs_extent_refs(leaf, ei) !=
2477 btrfs_extent_data_ref_count(leaf, ref) ||
2478 btrfs_extent_data_ref_root(leaf, ref) !=
2479 root->root_key.objectid ||
2480 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2481 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2482 goto out;
2483
Yan Zhengf321e492008-07-30 09:26:11 -04002484 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002485out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002486 return ret;
2487}
2488
2489int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2490 struct btrfs_root *root,
2491 u64 objectid, u64 offset, u64 bytenr)
2492{
2493 struct btrfs_path *path;
2494 int ret;
2495 int ret2;
2496
2497 path = btrfs_alloc_path();
2498 if (!path)
2499 return -ENOENT;
2500
2501 do {
2502 ret = check_committed_ref(trans, root, path, objectid,
2503 offset, bytenr);
2504 if (ret && ret != -ENOENT)
2505 goto out;
2506
2507 ret2 = check_delayed_ref(trans, root, path, objectid,
2508 offset, bytenr);
2509 } while (ret2 == -EAGAIN);
2510
2511 if (ret2 && ret2 != -ENOENT) {
2512 ret = ret2;
2513 goto out;
2514 }
2515
2516 if (ret != -ENOENT || ret2 != -ENOENT)
2517 ret = 0;
2518out:
Yan Zhengf321e492008-07-30 09:26:11 -04002519 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002520 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2521 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002522 return ret;
2523}
2524
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002525static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002526 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002527 struct extent_buffer *buf,
2528 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002529{
2530 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002531 u64 num_bytes;
2532 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002533 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002534 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002535 struct btrfs_key key;
2536 struct btrfs_file_extent_item *fi;
2537 int i;
2538 int level;
2539 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002540 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002541 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002542
2543 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002544 nritems = btrfs_header_nritems(buf);
2545 level = btrfs_header_level(buf);
2546
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002547 if (!root->ref_cows && level == 0)
2548 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002549
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002550 if (inc)
2551 process_func = btrfs_inc_extent_ref;
2552 else
2553 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002554
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002555 if (full_backref)
2556 parent = buf->start;
2557 else
2558 parent = 0;
2559
Zheng Yan31840ae2008-09-23 13:14:14 -04002560 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002561 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002562 btrfs_item_key_to_cpu(buf, &key, i);
2563 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002564 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002565 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002566 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002567 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002568 BTRFS_FILE_EXTENT_INLINE)
2569 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002570 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2571 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002572 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002573
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002574 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2575 key.offset -= btrfs_file_extent_offset(buf, fi);
2576 ret = process_func(trans, root, bytenr, num_bytes,
2577 parent, ref_root, key.objectid,
2578 key.offset);
2579 if (ret)
2580 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002581 } else {
2582 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002583 num_bytes = btrfs_level_size(root, level - 1);
2584 ret = process_func(trans, root, bytenr, num_bytes,
2585 parent, ref_root, level - 1, 0);
2586 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002587 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002588 }
2589 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002590 return 0;
2591fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002592 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002593 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002594}
2595
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002596int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2597 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002598{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002599 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2600}
Zheng Yan31840ae2008-09-23 13:14:14 -04002601
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002602int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2603 struct extent_buffer *buf, int full_backref)
2604{
2605 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002606}
2607
Chris Mason9078a3e2007-04-26 16:46:15 -04002608static int write_one_cache_group(struct btrfs_trans_handle *trans,
2609 struct btrfs_root *root,
2610 struct btrfs_path *path,
2611 struct btrfs_block_group_cache *cache)
2612{
2613 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002614 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002615 unsigned long bi;
2616 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002617
Chris Mason9078a3e2007-04-26 16:46:15 -04002618 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002619 if (ret < 0)
2620 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002621 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002622
2623 leaf = path->nodes[0];
2624 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2625 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2626 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002627 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002628fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002629 if (ret)
2630 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002631 return 0;
2632
2633}
2634
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002635static struct btrfs_block_group_cache *
2636next_block_group(struct btrfs_root *root,
2637 struct btrfs_block_group_cache *cache)
2638{
2639 struct rb_node *node;
2640 spin_lock(&root->fs_info->block_group_cache_lock);
2641 node = rb_next(&cache->cache_node);
2642 btrfs_put_block_group(cache);
2643 if (node) {
2644 cache = rb_entry(node, struct btrfs_block_group_cache,
2645 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002646 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002647 } else
2648 cache = NULL;
2649 spin_unlock(&root->fs_info->block_group_cache_lock);
2650 return cache;
2651}
2652
Josef Bacik0af3d002010-06-21 14:48:16 -04002653static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2654 struct btrfs_trans_handle *trans,
2655 struct btrfs_path *path)
2656{
2657 struct btrfs_root *root = block_group->fs_info->tree_root;
2658 struct inode *inode = NULL;
2659 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002660 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002661 int num_pages = 0;
2662 int retries = 0;
2663 int ret = 0;
2664
2665 /*
2666 * If this block group is smaller than 100 megs don't bother caching the
2667 * block group.
2668 */
2669 if (block_group->key.offset < (100 * 1024 * 1024)) {
2670 spin_lock(&block_group->lock);
2671 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2672 spin_unlock(&block_group->lock);
2673 return 0;
2674 }
2675
2676again:
2677 inode = lookup_free_space_inode(root, block_group, path);
2678 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2679 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002680 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002681 goto out;
2682 }
2683
2684 if (IS_ERR(inode)) {
2685 BUG_ON(retries);
2686 retries++;
2687
2688 if (block_group->ro)
2689 goto out_free;
2690
2691 ret = create_free_space_inode(root, trans, block_group, path);
2692 if (ret)
2693 goto out_free;
2694 goto again;
2695 }
2696
2697 /*
2698 * We want to set the generation to 0, that way if anything goes wrong
2699 * from here on out we know not to trust this cache when we load up next
2700 * time.
2701 */
2702 BTRFS_I(inode)->generation = 0;
2703 ret = btrfs_update_inode(trans, root, inode);
2704 WARN_ON(ret);
2705
2706 if (i_size_read(inode) > 0) {
2707 ret = btrfs_truncate_free_space_cache(root, trans, path,
2708 inode);
2709 if (ret)
2710 goto out_put;
2711 }
2712
2713 spin_lock(&block_group->lock);
2714 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002715 /* We're not cached, don't bother trying to write stuff out */
2716 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002717 spin_unlock(&block_group->lock);
2718 goto out_put;
2719 }
2720 spin_unlock(&block_group->lock);
2721
2722 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2723 if (!num_pages)
2724 num_pages = 1;
2725
2726 /*
2727 * Just to make absolutely sure we have enough space, we're going to
2728 * preallocate 12 pages worth of space for each block group. In
2729 * practice we ought to use at most 8, but we need extra space so we can
2730 * add our header and have a terminator between the extents and the
2731 * bitmaps.
2732 */
2733 num_pages *= 16;
2734 num_pages *= PAGE_CACHE_SIZE;
2735
2736 ret = btrfs_check_data_free_space(inode, num_pages);
2737 if (ret)
2738 goto out_put;
2739
2740 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2741 num_pages, num_pages,
2742 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002743 if (!ret)
2744 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002745 btrfs_free_reserved_data_space(inode, num_pages);
2746out_put:
2747 iput(inode);
2748out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02002749 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002750out:
2751 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002752 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002753 spin_unlock(&block_group->lock);
2754
2755 return ret;
2756}
2757
Chris Mason96b51792007-10-15 16:15:19 -04002758int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2759 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002760{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002761 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002762 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002763 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002764 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002765
2766 path = btrfs_alloc_path();
2767 if (!path)
2768 return -ENOMEM;
2769
Josef Bacik0af3d002010-06-21 14:48:16 -04002770again:
2771 while (1) {
2772 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2773 while (cache) {
2774 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2775 break;
2776 cache = next_block_group(root, cache);
2777 }
2778 if (!cache) {
2779 if (last == 0)
2780 break;
2781 last = 0;
2782 continue;
2783 }
2784 err = cache_save_setup(cache, trans, path);
2785 last = cache->key.objectid + cache->key.offset;
2786 btrfs_put_block_group(cache);
2787 }
2788
Chris Masond3977122009-01-05 21:25:51 -05002789 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002790 if (last == 0) {
2791 err = btrfs_run_delayed_refs(trans, root,
2792 (unsigned long)-1);
2793 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002794 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002795
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002796 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2797 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002798 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2799 btrfs_put_block_group(cache);
2800 goto again;
2801 }
2802
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002803 if (cache->dirty)
2804 break;
2805 cache = next_block_group(root, cache);
2806 }
2807 if (!cache) {
2808 if (last == 0)
2809 break;
2810 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002811 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002812 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002813
Josef Bacik0cb59c92010-07-02 12:14:14 -04002814 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2815 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002816 cache->dirty = 0;
2817 last = cache->key.objectid + cache->key.offset;
2818
2819 err = write_one_cache_group(trans, root, path, cache);
2820 BUG_ON(err);
2821 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002822 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002823
Josef Bacik0cb59c92010-07-02 12:14:14 -04002824 while (1) {
2825 /*
2826 * I don't think this is needed since we're just marking our
2827 * preallocated extent as written, but just in case it can't
2828 * hurt.
2829 */
2830 if (last == 0) {
2831 err = btrfs_run_delayed_refs(trans, root,
2832 (unsigned long)-1);
2833 BUG_ON(err);
2834 }
2835
2836 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2837 while (cache) {
2838 /*
2839 * Really this shouldn't happen, but it could if we
2840 * couldn't write the entire preallocated extent and
2841 * splitting the extent resulted in a new block.
2842 */
2843 if (cache->dirty) {
2844 btrfs_put_block_group(cache);
2845 goto again;
2846 }
2847 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2848 break;
2849 cache = next_block_group(root, cache);
2850 }
2851 if (!cache) {
2852 if (last == 0)
2853 break;
2854 last = 0;
2855 continue;
2856 }
2857
2858 btrfs_write_out_cache(root, trans, cache, path);
2859
2860 /*
2861 * If we didn't have an error then the cache state is still
2862 * NEED_WRITE, so we can set it to WRITTEN.
2863 */
2864 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2865 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2866 last = cache->key.objectid + cache->key.offset;
2867 btrfs_put_block_group(cache);
2868 }
2869
Chris Mason9078a3e2007-04-26 16:46:15 -04002870 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002871 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002872}
2873
Yan Zhengd2fb3432008-12-11 16:30:39 -05002874int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2875{
2876 struct btrfs_block_group_cache *block_group;
2877 int readonly = 0;
2878
2879 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2880 if (!block_group || block_group->ro)
2881 readonly = 1;
2882 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002883 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002884 return readonly;
2885}
2886
Chris Mason593060d2008-03-25 16:50:33 -04002887static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2888 u64 total_bytes, u64 bytes_used,
2889 struct btrfs_space_info **space_info)
2890{
2891 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002892 int i;
2893 int factor;
2894
2895 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2896 BTRFS_BLOCK_GROUP_RAID10))
2897 factor = 2;
2898 else
2899 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002900
2901 found = __find_space_info(info, flags);
2902 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002903 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002904 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002905 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002906 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002907 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002908 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002909 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002910 *space_info = found;
2911 return 0;
2912 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002913 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002914 if (!found)
2915 return -ENOMEM;
2916
Yan, Zhengb742bb82010-05-16 10:46:24 -04002917 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2918 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04002919 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002920 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04002921 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2922 BTRFS_BLOCK_GROUP_SYSTEM |
2923 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04002924 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002925 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002926 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002927 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002928 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002929 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002930 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002931 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002932 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04002933 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04002934 found->chunk_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002935 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002936 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002937 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002938 return 0;
2939}
2940
Chris Mason8790d502008-04-03 16:29:03 -04002941static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2942{
2943 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002944 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002945 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002946 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002947 if (extra_flags) {
2948 if (flags & BTRFS_BLOCK_GROUP_DATA)
2949 fs_info->avail_data_alloc_bits |= extra_flags;
2950 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2951 fs_info->avail_metadata_alloc_bits |= extra_flags;
2952 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2953 fs_info->avail_system_alloc_bits |= extra_flags;
2954 }
2955}
Chris Mason593060d2008-03-25 16:50:33 -04002956
Yan Zheng2b820322008-11-17 21:11:30 -05002957u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002958{
Chris Masoncd02dca2010-12-13 14:56:23 -05002959 /*
2960 * we add in the count of missing devices because we want
2961 * to make sure that any RAID levels on a degraded FS
2962 * continue to be honored.
2963 */
2964 u64 num_devices = root->fs_info->fs_devices->rw_devices +
2965 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002966
2967 if (num_devices == 1)
2968 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2969 if (num_devices < 4)
2970 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2971
Chris Masonec44a352008-04-28 15:29:52 -04002972 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2973 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002974 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002975 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002976 }
Chris Masonec44a352008-04-28 15:29:52 -04002977
2978 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002979 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002980 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002981 }
Chris Masonec44a352008-04-28 15:29:52 -04002982
2983 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2984 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2985 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2986 (flags & BTRFS_BLOCK_GROUP_DUP)))
2987 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2988 return flags;
2989}
2990
Yan, Zhengb742bb82010-05-16 10:46:24 -04002991static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05002992{
Yan, Zhengb742bb82010-05-16 10:46:24 -04002993 if (flags & BTRFS_BLOCK_GROUP_DATA)
2994 flags |= root->fs_info->avail_data_alloc_bits &
2995 root->fs_info->data_alloc_profile;
2996 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2997 flags |= root->fs_info->avail_system_alloc_bits &
2998 root->fs_info->system_alloc_profile;
2999 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3000 flags |= root->fs_info->avail_metadata_alloc_bits &
3001 root->fs_info->metadata_alloc_profile;
3002 return btrfs_reduce_alloc_profile(root, flags);
3003}
Josef Bacik6a632092009-02-20 11:00:09 -05003004
Miao Xie6d07bce2011-01-05 10:07:31 +00003005u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003006{
3007 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003008
Yan, Zhengb742bb82010-05-16 10:46:24 -04003009 if (data)
3010 flags = BTRFS_BLOCK_GROUP_DATA;
3011 else if (root == root->fs_info->chunk_root)
3012 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3013 else
3014 flags = BTRFS_BLOCK_GROUP_METADATA;
3015
3016 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003017}
3018
3019void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3020{
Josef Bacik6a632092009-02-20 11:00:09 -05003021 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003022 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003023}
3024
3025/*
3026 * This will check the space that the inode allocates from to make sure we have
3027 * enough space for bytes.
3028 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003029int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003030{
3031 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003032 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003033 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003034 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003035
3036 /* make sure bytes are sectorsize aligned */
3037 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3038
Li Zefan82d59022011-04-20 10:33:24 +08003039 if (root == root->fs_info->tree_root ||
3040 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003041 alloc_chunk = 0;
3042 committed = 1;
3043 }
3044
Josef Bacik6a632092009-02-20 11:00:09 -05003045 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003046 if (!data_sinfo)
3047 goto alloc;
3048
Josef Bacik6a632092009-02-20 11:00:09 -05003049again:
3050 /* make sure we have enough space to handle the data first */
3051 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003052 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3053 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3054 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003055
3056 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003057 struct btrfs_trans_handle *trans;
3058
Josef Bacik6a632092009-02-20 11:00:09 -05003059 /*
3060 * if we don't have enough free bytes in this space then we need
3061 * to alloc a new chunk.
3062 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003063 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003064 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003065
Chris Mason0e4f8f82011-04-15 16:05:44 -04003066 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003067 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003068alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003069 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003070 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003071 if (IS_ERR(trans))
3072 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003073
3074 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3075 bytes + 2 * 1024 * 1024,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003076 alloc_target,
3077 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003078 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003079 if (ret < 0) {
3080 if (ret != -ENOSPC)
3081 return ret;
3082 else
3083 goto commit_trans;
3084 }
Chris Mason33b4d472009-09-22 14:45:50 -04003085
3086 if (!data_sinfo) {
3087 btrfs_set_inode_space_info(root, inode);
3088 data_sinfo = BTRFS_I(inode)->space_info;
3089 }
Josef Bacik6a632092009-02-20 11:00:09 -05003090 goto again;
3091 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003092
3093 /*
3094 * If we have less pinned bytes than we want to allocate then
3095 * don't bother committing the transaction, it won't help us.
3096 */
3097 if (data_sinfo->bytes_pinned < bytes)
3098 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003099 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003100
3101 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003102commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003103 if (!committed &&
3104 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003105 committed = 1;
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003106 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003107 if (IS_ERR(trans))
3108 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003109 ret = btrfs_commit_transaction(trans, root);
3110 if (ret)
3111 return ret;
3112 goto again;
3113 }
3114
Josef Bacik6a632092009-02-20 11:00:09 -05003115 return -ENOSPC;
3116 }
3117 data_sinfo->bytes_may_use += bytes;
3118 BTRFS_I(inode)->reserved_bytes += bytes;
3119 spin_unlock(&data_sinfo->lock);
3120
Josef Bacik9ed74f22009-09-11 16:12:44 -04003121 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003122}
3123
3124/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003125 * called when we are clearing an delalloc extent from the
3126 * inode's io_tree or there was an error for whatever reason
3127 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003128 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003129void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003130{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003131 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003132 struct btrfs_space_info *data_sinfo;
3133
3134 /* make sure bytes are sectorsize aligned */
3135 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3136
3137 data_sinfo = BTRFS_I(inode)->space_info;
3138 spin_lock(&data_sinfo->lock);
3139 data_sinfo->bytes_may_use -= bytes;
3140 BTRFS_I(inode)->reserved_bytes -= bytes;
3141 spin_unlock(&data_sinfo->lock);
3142}
3143
Josef Bacik97e728d2009-04-21 17:40:57 -04003144static void force_metadata_allocation(struct btrfs_fs_info *info)
3145{
3146 struct list_head *head = &info->space_info;
3147 struct btrfs_space_info *found;
3148
3149 rcu_read_lock();
3150 list_for_each_entry_rcu(found, head, list) {
3151 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003152 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003153 }
3154 rcu_read_unlock();
3155}
3156
Chris Masone5bc2452010-10-26 13:37:56 -04003157static int should_alloc_chunk(struct btrfs_root *root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003158 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3159 int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003160{
3161 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003162 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003163 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003164
Chris Mason0e4f8f82011-04-15 16:05:44 -04003165 if (force == CHUNK_ALLOC_FORCE)
3166 return 1;
3167
3168 /*
3169 * in limited mode, we want to have some free space up to
3170 * about 1% of the FS size.
3171 */
3172 if (force == CHUNK_ALLOC_LIMITED) {
3173 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3174 thresh = max_t(u64, 64 * 1024 * 1024,
3175 div_factor_fine(thresh, 1));
3176
3177 if (num_bytes - num_allocated < thresh)
3178 return 1;
3179 }
3180
3181 /*
3182 * we have two similar checks here, one based on percentage
3183 * and once based on a hard number of 256MB. The idea
3184 * is that if we have a good amount of free
3185 * room, don't allocate a chunk. A good mount is
3186 * less than 80% utilized of the chunks we have allocated,
3187 * or more than 256MB free
3188 */
3189 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003190 return 0;
3191
Chris Mason0e4f8f82011-04-15 16:05:44 -04003192 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
Yan, Zheng424499d2010-05-16 10:46:25 -04003193 return 0;
3194
Chris Masone5bc2452010-10-26 13:37:56 -04003195 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003196
3197 /* 256MB or 5% of the FS */
Chris Masone5bc2452010-10-26 13:37:56 -04003198 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3199
3200 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003201 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003202 return 1;
3203}
3204
Chris Mason6324fbf2008-03-24 15:01:59 -04003205static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3206 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003207 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003208{
3209 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003210 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003211 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003212 int ret = 0;
3213
Yan Zheng2b820322008-11-17 21:11:30 -05003214 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003215
Chris Mason6324fbf2008-03-24 15:01:59 -04003216 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003217 if (!space_info) {
3218 ret = update_space_info(extent_root->fs_info, flags,
3219 0, 0, &space_info);
3220 BUG_ON(ret);
3221 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003222 BUG_ON(!space_info);
3223
Josef Bacik6d741192011-04-11 20:20:11 -04003224again:
Josef Bacik25179202008-10-29 14:49:05 -04003225 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003226 if (space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003227 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003228 if (space_info->full) {
3229 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003230 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003231 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003232
Chris Mason0e4f8f82011-04-15 16:05:44 -04003233 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003234 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003235 return 0;
3236 } else if (space_info->chunk_alloc) {
3237 wait_for_alloc = 1;
3238 } else {
3239 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003240 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003241
Josef Bacik25179202008-10-29 14:49:05 -04003242 spin_unlock(&space_info->lock);
3243
Josef Bacik6d741192011-04-11 20:20:11 -04003244 mutex_lock(&fs_info->chunk_mutex);
3245
3246 /*
3247 * The chunk_mutex is held throughout the entirety of a chunk
3248 * allocation, so once we've acquired the chunk_mutex we know that the
3249 * other guy is done and we need to recheck and see if we should
3250 * allocate.
3251 */
3252 if (wait_for_alloc) {
3253 mutex_unlock(&fs_info->chunk_mutex);
3254 wait_for_alloc = 0;
3255 goto again;
3256 }
3257
Josef Bacik97e728d2009-04-21 17:40:57 -04003258 /*
Josef Bacik67377732010-09-16 16:19:09 -04003259 * If we have mixed data/metadata chunks we want to make sure we keep
3260 * allocating mixed chunks instead of individual chunks.
3261 */
3262 if (btrfs_mixed_space_info(space_info))
3263 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3264
3265 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003266 * if we're doing a data chunk, go ahead and make sure that
3267 * we keep a reasonable number of metadata chunks allocated in the
3268 * FS as well.
3269 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003270 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003271 fs_info->data_chunk_allocations++;
3272 if (!(fs_info->data_chunk_allocations %
3273 fs_info->metadata_ratio))
3274 force_metadata_allocation(fs_info);
3275 }
3276
Yan Zheng2b820322008-11-17 21:11:30 -05003277 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003278 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003279 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003280 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003281 else
3282 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003283
Chris Mason0e4f8f82011-04-15 16:05:44 -04003284 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003285 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003286 spin_unlock(&space_info->lock);
Yan Zhengc146afa2008-11-12 14:34:12 -05003287 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003288 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003289}
3290
Yan, Zheng5da9d012010-05-16 10:46:25 -04003291/*
3292 * shrink metadata reservation for delalloc
3293 */
3294static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003295 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003296{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003297 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003298 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003299 u64 reserved;
3300 u64 max_reclaim;
3301 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003302 long time_left;
Chris Masonbf9022e2010-10-26 13:40:45 -04003303 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003304 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003305 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003306
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003307 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003308 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003309
3310 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003311 reserved = space_info->bytes_reserved;
Chris Mason36e39c42011-03-12 07:08:42 -05003312 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003313
3314 if (reserved == 0)
3315 return 0;
3316
3317 max_reclaim = min(reserved, to_reclaim);
3318
Josef Bacikb1953bc2011-01-21 21:10:01 +00003319 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003320 /* have the flusher threads jump in and do some IO */
3321 smp_mb();
3322 nr_pages = min_t(unsigned long, nr_pages,
3323 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3324 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003325
Josef Bacik0019f102010-10-15 15:18:40 -04003326 spin_lock(&space_info->lock);
Chris Mason36e39c42011-03-12 07:08:42 -05003327 if (reserved > space_info->bytes_reserved)
Josef Bacik0019f102010-10-15 15:18:40 -04003328 reclaimed += reserved - space_info->bytes_reserved;
3329 reserved = space_info->bytes_reserved;
3330 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003331
Chris Mason36e39c42011-03-12 07:08:42 -05003332 loops++;
3333
Yan, Zheng5da9d012010-05-16 10:46:25 -04003334 if (reserved == 0 || reclaimed >= max_reclaim)
3335 break;
3336
3337 if (trans && trans->transaction->blocked)
3338 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003339
Chris Mason36e39c42011-03-12 07:08:42 -05003340 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003341
3342 /* We were interrupted, exit */
3343 if (time_left)
3344 break;
3345
Chris Mason36e39c42011-03-12 07:08:42 -05003346 /* we've kicked the IO a few times, if anything has been freed,
3347 * exit. There is no sense in looping here for a long time
3348 * when we really need to commit the transaction, or there are
3349 * just too many writers without enough free space
3350 */
3351
3352 if (loops > 3) {
3353 smp_mb();
3354 if (progress != space_info->reservation_progress)
3355 break;
3356 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003357
Yan, Zheng5da9d012010-05-16 10:46:25 -04003358 }
3359 return reclaimed >= to_reclaim;
3360}
3361
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003362/*
3363 * Retries tells us how many times we've called reserve_metadata_bytes. The
3364 * idea is if this is the first call (retries == 0) then we will add to our
3365 * reserved count if we can't make the allocation in order to hold our place
3366 * while we go and try and free up space. That way for retries > 1 we don't try
3367 * and add space, we just check to see if the amount of unused space is >= the
3368 * total space, meaning that our reservation is valid.
3369 *
3370 * However if we don't intend to retry this reservation, pass -1 as retries so
3371 * that it short circuits this logic.
3372 */
3373static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3374 struct btrfs_root *root,
3375 struct btrfs_block_rsv *block_rsv,
3376 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003377{
3378 struct btrfs_space_info *space_info = block_rsv->space_info;
3379 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003380 u64 num_bytes = orig_bytes;
3381 int retries = 0;
3382 int ret = 0;
3383 bool reserved = false;
Josef Bacik38227932010-10-26 12:52:53 -04003384 bool committed = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003385
3386again:
3387 ret = -ENOSPC;
3388 if (reserved)
3389 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003390
3391 spin_lock(&space_info->lock);
3392 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003393 space_info->bytes_pinned + space_info->bytes_readonly +
3394 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003395
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003396 /*
3397 * The idea here is that we've not already over-reserved the block group
3398 * then we can go ahead and save our reservation first and then start
3399 * flushing if we need to. Otherwise if we've already overcommitted
3400 * lets start flushing stuff first and then come back and try to make
3401 * our reservation.
3402 */
3403 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003404 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003405 if (unused >= num_bytes) {
3406 if (!reserved)
3407 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003408 ret = 0;
3409 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003410 /*
3411 * Ok set num_bytes to orig_bytes since we aren't
3412 * overocmmitted, this way we only try and reclaim what
3413 * we need.
3414 */
3415 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003416 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003417 } else {
3418 /*
3419 * Ok we're over committed, set num_bytes to the overcommitted
3420 * amount plus the amount of bytes that we need for this
3421 * reservation.
3422 */
3423 num_bytes = unused - space_info->total_bytes +
3424 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003425 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003426
3427 /*
3428 * Couldn't make our reservation, save our place so while we're trying
3429 * to reclaim space we can actually use it instead of somebody else
3430 * stealing it from us.
3431 */
3432 if (ret && !reserved) {
3433 space_info->bytes_reserved += orig_bytes;
3434 reserved = true;
3435 }
3436
Yan, Zhengf0486c62010-05-16 10:46:25 -04003437 spin_unlock(&space_info->lock);
3438
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003439 if (!ret)
3440 return 0;
3441
3442 if (!flush)
3443 goto out;
3444
3445 /*
3446 * We do synchronous shrinking since we don't actually unreserve
3447 * metadata until after the IO is completed.
3448 */
3449 ret = shrink_delalloc(trans, root, num_bytes, 1);
3450 if (ret > 0)
3451 return 0;
3452 else if (ret < 0)
3453 goto out;
3454
3455 /*
3456 * So if we were overcommitted it's possible that somebody else flushed
3457 * out enough space and we simply didn't have enough space to reclaim,
3458 * so go back around and try again.
3459 */
3460 if (retries < 2) {
3461 retries++;
3462 goto again;
3463 }
3464
3465 spin_lock(&space_info->lock);
3466 /*
3467 * Not enough space to be reclaimed, don't bother committing the
3468 * transaction.
3469 */
3470 if (space_info->bytes_pinned < orig_bytes)
3471 ret = -ENOSPC;
3472 spin_unlock(&space_info->lock);
3473 if (ret)
3474 goto out;
3475
3476 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003477 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003478 goto out;
3479
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003480 ret = -ENOSPC;
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003481 trans = btrfs_join_transaction(root);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003482 if (IS_ERR(trans))
3483 goto out;
3484 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003485 if (!ret) {
3486 trans = NULL;
3487 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003488 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003489 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003490
3491out:
3492 if (reserved) {
3493 spin_lock(&space_info->lock);
3494 space_info->bytes_reserved -= orig_bytes;
3495 spin_unlock(&space_info->lock);
3496 }
3497
Yan, Zhengf0486c62010-05-16 10:46:25 -04003498 return ret;
3499}
3500
3501static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3502 struct btrfs_root *root)
3503{
3504 struct btrfs_block_rsv *block_rsv;
3505 if (root->ref_cows)
3506 block_rsv = trans->block_rsv;
3507 else
3508 block_rsv = root->block_rsv;
3509
3510 if (!block_rsv)
3511 block_rsv = &root->fs_info->empty_block_rsv;
3512
3513 return block_rsv;
3514}
3515
3516static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3517 u64 num_bytes)
3518{
3519 int ret = -ENOSPC;
3520 spin_lock(&block_rsv->lock);
3521 if (block_rsv->reserved >= num_bytes) {
3522 block_rsv->reserved -= num_bytes;
3523 if (block_rsv->reserved < block_rsv->size)
3524 block_rsv->full = 0;
3525 ret = 0;
3526 }
3527 spin_unlock(&block_rsv->lock);
3528 return ret;
3529}
3530
3531static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3532 u64 num_bytes, int update_size)
3533{
3534 spin_lock(&block_rsv->lock);
3535 block_rsv->reserved += num_bytes;
3536 if (update_size)
3537 block_rsv->size += num_bytes;
3538 else if (block_rsv->reserved >= block_rsv->size)
3539 block_rsv->full = 1;
3540 spin_unlock(&block_rsv->lock);
3541}
3542
David Sterba62a45b62011-04-20 15:52:26 +02003543static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3544 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003545{
3546 struct btrfs_space_info *space_info = block_rsv->space_info;
3547
3548 spin_lock(&block_rsv->lock);
3549 if (num_bytes == (u64)-1)
3550 num_bytes = block_rsv->size;
3551 block_rsv->size -= num_bytes;
3552 if (block_rsv->reserved >= block_rsv->size) {
3553 num_bytes = block_rsv->reserved - block_rsv->size;
3554 block_rsv->reserved = block_rsv->size;
3555 block_rsv->full = 1;
3556 } else {
3557 num_bytes = 0;
3558 }
3559 spin_unlock(&block_rsv->lock);
3560
3561 if (num_bytes > 0) {
3562 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003563 spin_lock(&dest->lock);
3564 if (!dest->full) {
3565 u64 bytes_to_add;
3566
3567 bytes_to_add = dest->size - dest->reserved;
3568 bytes_to_add = min(num_bytes, bytes_to_add);
3569 dest->reserved += bytes_to_add;
3570 if (dest->reserved >= dest->size)
3571 dest->full = 1;
3572 num_bytes -= bytes_to_add;
3573 }
3574 spin_unlock(&dest->lock);
3575 }
3576 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003577 spin_lock(&space_info->lock);
3578 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003579 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003580 spin_unlock(&space_info->lock);
3581 }
3582 }
3583}
3584
3585static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3586 struct btrfs_block_rsv *dst, u64 num_bytes)
3587{
3588 int ret;
3589
3590 ret = block_rsv_use_bytes(src, num_bytes);
3591 if (ret)
3592 return ret;
3593
3594 block_rsv_add_bytes(dst, num_bytes, 1);
3595 return 0;
3596}
3597
3598void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3599{
3600 memset(rsv, 0, sizeof(*rsv));
3601 spin_lock_init(&rsv->lock);
3602 atomic_set(&rsv->usage, 1);
3603 rsv->priority = 6;
3604 INIT_LIST_HEAD(&rsv->list);
3605}
3606
3607struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3608{
3609 struct btrfs_block_rsv *block_rsv;
3610 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003611
3612 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3613 if (!block_rsv)
3614 return NULL;
3615
3616 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003617 block_rsv->space_info = __find_space_info(fs_info,
3618 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003619 return block_rsv;
3620}
3621
3622void btrfs_free_block_rsv(struct btrfs_root *root,
3623 struct btrfs_block_rsv *rsv)
3624{
3625 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3626 btrfs_block_rsv_release(root, rsv, (u64)-1);
3627 if (!rsv->durable)
3628 kfree(rsv);
3629 }
3630}
3631
3632/*
3633 * make the block_rsv struct be able to capture freed space.
3634 * the captured space will re-add to the the block_rsv struct
3635 * after transaction commit
3636 */
3637void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3638 struct btrfs_block_rsv *block_rsv)
3639{
3640 block_rsv->durable = 1;
3641 mutex_lock(&fs_info->durable_block_rsv_mutex);
3642 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3643 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3644}
3645
3646int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3647 struct btrfs_root *root,
3648 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003649 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003650{
3651 int ret;
3652
3653 if (num_bytes == 0)
3654 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003655
3656 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003657 if (!ret) {
3658 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3659 return 0;
3660 }
3661
Yan, Zhengf0486c62010-05-16 10:46:25 -04003662 return ret;
3663}
3664
3665int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3666 struct btrfs_root *root,
3667 struct btrfs_block_rsv *block_rsv,
3668 u64 min_reserved, int min_factor)
3669{
3670 u64 num_bytes = 0;
3671 int commit_trans = 0;
3672 int ret = -ENOSPC;
3673
3674 if (!block_rsv)
3675 return 0;
3676
3677 spin_lock(&block_rsv->lock);
3678 if (min_factor > 0)
3679 num_bytes = div_factor(block_rsv->size, min_factor);
3680 if (min_reserved > num_bytes)
3681 num_bytes = min_reserved;
3682
3683 if (block_rsv->reserved >= num_bytes) {
3684 ret = 0;
3685 } else {
3686 num_bytes -= block_rsv->reserved;
3687 if (block_rsv->durable &&
3688 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3689 commit_trans = 1;
3690 }
3691 spin_unlock(&block_rsv->lock);
3692 if (!ret)
3693 return 0;
3694
3695 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003696 ret = reserve_metadata_bytes(trans, root, block_rsv,
3697 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003698 if (!ret) {
3699 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3700 return 0;
3701 }
3702 }
3703
3704 if (commit_trans) {
3705 if (trans)
3706 return -EAGAIN;
3707
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003708 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003709 BUG_ON(IS_ERR(trans));
3710 ret = btrfs_commit_transaction(trans, root);
3711 return 0;
3712 }
3713
Yan, Zhengf0486c62010-05-16 10:46:25 -04003714 return -ENOSPC;
3715}
3716
3717int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3718 struct btrfs_block_rsv *dst_rsv,
3719 u64 num_bytes)
3720{
3721 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3722}
3723
3724void btrfs_block_rsv_release(struct btrfs_root *root,
3725 struct btrfs_block_rsv *block_rsv,
3726 u64 num_bytes)
3727{
3728 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3729 if (global_rsv->full || global_rsv == block_rsv ||
3730 block_rsv->space_info != global_rsv->space_info)
3731 global_rsv = NULL;
3732 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3733}
3734
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003735/*
3736 * helper to calculate size of global block reservation.
3737 * the desired value is sum of space used by extent tree,
3738 * checksum tree and root tree
3739 */
3740static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3741{
3742 struct btrfs_space_info *sinfo;
3743 u64 num_bytes;
3744 u64 meta_used;
3745 u64 data_used;
3746 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003747
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003748 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3749 spin_lock(&sinfo->lock);
3750 data_used = sinfo->bytes_used;
3751 spin_unlock(&sinfo->lock);
3752
3753 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3754 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003755 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3756 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003757 meta_used = sinfo->bytes_used;
3758 spin_unlock(&sinfo->lock);
3759
3760 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3761 csum_size * 2;
3762 num_bytes += div64_u64(data_used + meta_used, 50);
3763
3764 if (num_bytes * 3 > meta_used)
3765 num_bytes = div64_u64(meta_used, 3);
3766
3767 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3768}
3769
3770static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3771{
3772 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3773 struct btrfs_space_info *sinfo = block_rsv->space_info;
3774 u64 num_bytes;
3775
3776 num_bytes = calc_global_metadata_size(fs_info);
3777
3778 spin_lock(&block_rsv->lock);
3779 spin_lock(&sinfo->lock);
3780
3781 block_rsv->size = num_bytes;
3782
3783 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003784 sinfo->bytes_reserved + sinfo->bytes_readonly +
3785 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003786
3787 if (sinfo->total_bytes > num_bytes) {
3788 num_bytes = sinfo->total_bytes - num_bytes;
3789 block_rsv->reserved += num_bytes;
3790 sinfo->bytes_reserved += num_bytes;
3791 }
3792
3793 if (block_rsv->reserved >= block_rsv->size) {
3794 num_bytes = block_rsv->reserved - block_rsv->size;
3795 sinfo->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003796 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003797 block_rsv->reserved = block_rsv->size;
3798 block_rsv->full = 1;
3799 }
David Sterba182608c2011-05-05 13:13:16 +02003800
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003801 spin_unlock(&sinfo->lock);
3802 spin_unlock(&block_rsv->lock);
3803}
3804
Yan, Zhengf0486c62010-05-16 10:46:25 -04003805static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3806{
3807 struct btrfs_space_info *space_info;
3808
3809 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3810 fs_info->chunk_block_rsv.space_info = space_info;
3811 fs_info->chunk_block_rsv.priority = 10;
3812
3813 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003814 fs_info->global_block_rsv.space_info = space_info;
3815 fs_info->global_block_rsv.priority = 10;
3816 fs_info->global_block_rsv.refill_used = 1;
3817 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003818 fs_info->trans_block_rsv.space_info = space_info;
3819 fs_info->empty_block_rsv.space_info = space_info;
3820 fs_info->empty_block_rsv.priority = 10;
3821
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003822 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3823 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3824 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3825 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003826 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003827
3828 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3829
3830 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3831
3832 update_global_block_rsv(fs_info);
3833}
3834
3835static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3836{
3837 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3838 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3839 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3840 WARN_ON(fs_info->trans_block_rsv.size > 0);
3841 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3842 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3843 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003844}
3845
Josef Bacikfcb80c22011-05-03 10:40:22 -04003846int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans,
3847 struct btrfs_root *root,
3848 struct btrfs_block_rsv *rsv)
3849{
3850 struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv;
3851 u64 num_bytes;
3852 int ret;
3853
3854 /*
3855 * Truncate should be freeing data, but give us 2 items just in case it
3856 * needs to use some space. We may want to be smarter about this in the
3857 * future.
3858 */
Chris Masonff5714c2011-05-28 07:00:39 -04003859 num_bytes = btrfs_calc_trans_metadata_size(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04003860
3861 /* We already have enough bytes, just return */
3862 if (rsv->reserved >= num_bytes)
3863 return 0;
3864
3865 num_bytes -= rsv->reserved;
3866
3867 /*
3868 * You should have reserved enough space before hand to do this, so this
3869 * should not fail.
3870 */
3871 ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes);
3872 BUG_ON(ret);
3873
3874 return 0;
3875}
3876
Yan, Zhenga22285a2010-05-16 10:48:46 -04003877int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3878 struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003879 int num_items)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003880{
3881 u64 num_bytes;
3882 int ret;
3883
3884 if (num_items == 0 || root->fs_info->chunk_root == root)
3885 return 0;
3886
Miao Xie16cdcec2011-04-22 18:12:22 +08003887 num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003888 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003889 num_bytes);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003890 if (!ret) {
3891 trans->bytes_reserved += num_bytes;
3892 trans->block_rsv = &root->fs_info->trans_block_rsv;
3893 }
3894 return ret;
3895}
3896
3897void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3898 struct btrfs_root *root)
3899{
3900 if (!trans->bytes_reserved)
3901 return;
3902
3903 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3904 btrfs_block_rsv_release(root, trans->block_rsv,
3905 trans->bytes_reserved);
3906 trans->bytes_reserved = 0;
3907}
3908
Yan, Zhengd68fc572010-05-16 10:49:58 -04003909int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3910 struct inode *inode)
3911{
3912 struct btrfs_root *root = BTRFS_I(inode)->root;
3913 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3914 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3915
3916 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04003917 * We need to hold space in order to delete our orphan item once we've
3918 * added it, so this takes the reservation so we can release it later
3919 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003920 */
Chris Masonff5714c2011-05-28 07:00:39 -04003921 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003922 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3923}
3924
3925void btrfs_orphan_release_metadata(struct inode *inode)
3926{
3927 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04003928 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003929 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3930}
3931
Yan, Zhenga22285a2010-05-16 10:48:46 -04003932int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3933 struct btrfs_pending_snapshot *pending)
3934{
3935 struct btrfs_root *root = pending->root;
3936 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3937 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3938 /*
3939 * two for root back/forward refs, two for directory entries
3940 * and one for root of the snapshot.
3941 */
Miao Xie16cdcec2011-04-22 18:12:22 +08003942 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003943 dst_rsv->space_info = src_rsv->space_info;
3944 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3945}
3946
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003947static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3948{
3949 return num_bytes >>= 3;
3950}
3951
3952int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3953{
3954 struct btrfs_root *root = BTRFS_I(inode)->root;
3955 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3956 u64 to_reserve;
3957 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05003958 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003959 int ret;
3960
3961 if (btrfs_transaction_in_commit(root->fs_info))
3962 schedule_timeout(1);
3963
3964 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003965
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003966 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
Josef Bacik57a45ced2011-01-25 16:30:38 -05003967 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
3968
3969 if (nr_extents > reserved_extents) {
3970 nr_extents -= reserved_extents;
Miao Xie16cdcec2011-04-22 18:12:22 +08003971 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003972 } else {
3973 nr_extents = 0;
3974 to_reserve = 0;
3975 }
Josef Bacik57a45ced2011-01-25 16:30:38 -05003976
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003977 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003978 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
3979 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003980 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003981
Josef Bacik57a45ced2011-01-25 16:30:38 -05003982 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003983 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003984
3985 block_rsv_add_bytes(block_rsv, to_reserve, 1);
3986
3987 if (block_rsv->size > 512 * 1024 * 1024)
Josef Bacik0019f102010-10-15 15:18:40 -04003988 shrink_delalloc(NULL, root, to_reserve, 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003989
3990 return 0;
3991}
3992
3993void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
3994{
3995 struct btrfs_root *root = BTRFS_I(inode)->root;
3996 u64 to_free;
3997 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05003998 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003999
4000 num_bytes = ALIGN(num_bytes, root->sectorsize);
4001 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik3c148742011-02-02 15:53:47 +00004002 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004003
Josef Bacik57a45ced2011-01-25 16:30:38 -05004004 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4005 do {
4006 int old, new;
4007
4008 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4009 if (nr_extents >= reserved_extents) {
4010 nr_extents = 0;
4011 break;
4012 }
4013 old = reserved_extents;
4014 nr_extents = reserved_extents - nr_extents;
4015 new = reserved_extents - nr_extents;
4016 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
4017 reserved_extents, new);
4018 if (likely(old == reserved_extents))
4019 break;
4020 reserved_extents = old;
4021 } while (1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004022
4023 to_free = calc_csum_metadata_size(inode, num_bytes);
4024 if (nr_extents > 0)
Miao Xie16cdcec2011-04-22 18:12:22 +08004025 to_free += btrfs_calc_trans_metadata_size(root, nr_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004026
4027 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4028 to_free);
4029}
4030
4031int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4032{
4033 int ret;
4034
4035 ret = btrfs_check_data_free_space(inode, num_bytes);
4036 if (ret)
4037 return ret;
4038
4039 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4040 if (ret) {
4041 btrfs_free_reserved_data_space(inode, num_bytes);
4042 return ret;
4043 }
4044
4045 return 0;
4046}
4047
4048void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4049{
4050 btrfs_delalloc_release_metadata(inode, num_bytes);
4051 btrfs_free_reserved_data_space(inode, num_bytes);
4052}
4053
Chris Mason9078a3e2007-04-26 16:46:15 -04004054static int update_block_group(struct btrfs_trans_handle *trans,
4055 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004056 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004057{
Josef Bacik0af3d002010-06-21 14:48:16 -04004058 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004059 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004060 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004061 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004062 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004063 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004064
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004065 /* block accounting for super block */
4066 spin_lock(&info->delalloc_lock);
4067 old_val = btrfs_super_bytes_used(&info->super_copy);
4068 if (alloc)
4069 old_val += num_bytes;
4070 else
4071 old_val -= num_bytes;
4072 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004073 spin_unlock(&info->delalloc_lock);
4074
Chris Masond3977122009-01-05 21:25:51 -05004075 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004076 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004077 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004078 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004079 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4080 BTRFS_BLOCK_GROUP_RAID1 |
4081 BTRFS_BLOCK_GROUP_RAID10))
4082 factor = 2;
4083 else
4084 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004085 /*
4086 * If this block group has free space cache written out, we
4087 * need to make sure to load it if we are removing space. This
4088 * is because we need the unpinning stage to actually add the
4089 * space back to the block group, otherwise we will leak space.
4090 */
4091 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004092 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004093
Chris Masondb945352007-10-15 16:15:53 -04004094 byte_in_group = bytenr - cache->key.objectid;
4095 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004096
Josef Bacik25179202008-10-29 14:49:05 -04004097 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004098 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004099
4100 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4101 cache->disk_cache_state < BTRFS_DC_CLEAR)
4102 cache->disk_cache_state = BTRFS_DC_CLEAR;
4103
Josef Bacik0f9dd462008-09-23 13:14:11 -04004104 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004105 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004106 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004107 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004108 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004109 btrfs_set_block_group_used(&cache->item, old_val);
4110 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004111 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004112 cache->space_info->reservation_progress++;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004113 cache->space_info->bytes_used += num_bytes;
4114 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004115 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004116 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004117 } else {
Chris Masondb945352007-10-15 16:15:53 -04004118 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004119 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004120 cache->pinned += num_bytes;
4121 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004122 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004123 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004124 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004125 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004126
Yan, Zhengf0486c62010-05-16 10:46:25 -04004127 set_extent_dirty(info->pinned_extents,
4128 bytenr, bytenr + num_bytes - 1,
4129 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004130 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004131 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004132 total -= num_bytes;
4133 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004134 }
4135 return 0;
4136}
Chris Mason6324fbf2008-03-24 15:01:59 -04004137
Chris Masona061fc82008-05-07 11:43:44 -04004138static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4139{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004140 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004141 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004142
4143 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4144 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004145 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004146
Yan Zhengd2fb3432008-12-11 16:30:39 -05004147 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004148 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004149
4150 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004151}
4152
Yan, Zhengf0486c62010-05-16 10:46:25 -04004153static int pin_down_extent(struct btrfs_root *root,
4154 struct btrfs_block_group_cache *cache,
4155 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004156{
Yan Zheng11833d62009-09-11 16:11:19 -04004157 spin_lock(&cache->space_info->lock);
4158 spin_lock(&cache->lock);
4159 cache->pinned += num_bytes;
4160 cache->space_info->bytes_pinned += num_bytes;
4161 if (reserved) {
4162 cache->reserved -= num_bytes;
4163 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004164 cache->space_info->reservation_progress++;
Yan324ae4d2007-11-16 14:57:08 -05004165 }
Yan Zheng11833d62009-09-11 16:11:19 -04004166 spin_unlock(&cache->lock);
4167 spin_unlock(&cache->space_info->lock);
4168
Yan, Zhengf0486c62010-05-16 10:46:25 -04004169 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4170 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004171 return 0;
4172}
Chris Mason9078a3e2007-04-26 16:46:15 -04004173
Yan, Zhengf0486c62010-05-16 10:46:25 -04004174/*
4175 * this function must be called within transaction
4176 */
4177int btrfs_pin_extent(struct btrfs_root *root,
4178 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004179{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004180 struct btrfs_block_group_cache *cache;
4181
4182 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4183 BUG_ON(!cache);
4184
4185 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4186
4187 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004188 return 0;
4189}
Zheng Yane8569812008-09-26 10:05:48 -04004190
Yan, Zhengf0486c62010-05-16 10:46:25 -04004191/*
4192 * update size of reserved extents. this function may return -EAGAIN
4193 * if 'reserve' is true or 'sinfo' is false.
4194 */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004195int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4196 u64 num_bytes, int reserve, int sinfo)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004197{
4198 int ret = 0;
4199 if (sinfo) {
4200 struct btrfs_space_info *space_info = cache->space_info;
4201 spin_lock(&space_info->lock);
4202 spin_lock(&cache->lock);
4203 if (reserve) {
4204 if (cache->ro) {
4205 ret = -EAGAIN;
4206 } else {
4207 cache->reserved += num_bytes;
4208 space_info->bytes_reserved += num_bytes;
4209 }
4210 } else {
4211 if (cache->ro)
4212 space_info->bytes_readonly += num_bytes;
4213 cache->reserved -= num_bytes;
4214 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004215 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004216 }
4217 spin_unlock(&cache->lock);
4218 spin_unlock(&space_info->lock);
4219 } else {
4220 spin_lock(&cache->lock);
4221 if (cache->ro) {
4222 ret = -EAGAIN;
4223 } else {
4224 if (reserve)
4225 cache->reserved += num_bytes;
4226 else
4227 cache->reserved -= num_bytes;
4228 }
4229 spin_unlock(&cache->lock);
4230 }
4231 return ret;
4232}
4233
Yan Zheng11833d62009-09-11 16:11:19 -04004234int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4235 struct btrfs_root *root)
4236{
4237 struct btrfs_fs_info *fs_info = root->fs_info;
4238 struct btrfs_caching_control *next;
4239 struct btrfs_caching_control *caching_ctl;
4240 struct btrfs_block_group_cache *cache;
4241
4242 down_write(&fs_info->extent_commit_sem);
4243
4244 list_for_each_entry_safe(caching_ctl, next,
4245 &fs_info->caching_block_groups, list) {
4246 cache = caching_ctl->block_group;
4247 if (block_group_cache_done(cache)) {
4248 cache->last_byte_to_unpin = (u64)-1;
4249 list_del_init(&caching_ctl->list);
4250 put_caching_control(caching_ctl);
4251 } else {
4252 cache->last_byte_to_unpin = caching_ctl->progress;
4253 }
4254 }
4255
4256 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4257 fs_info->pinned_extents = &fs_info->freed_extents[1];
4258 else
4259 fs_info->pinned_extents = &fs_info->freed_extents[0];
4260
4261 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004262
4263 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004264 return 0;
4265}
4266
4267static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4268{
4269 struct btrfs_fs_info *fs_info = root->fs_info;
4270 struct btrfs_block_group_cache *cache = NULL;
4271 u64 len;
4272
4273 while (start <= end) {
4274 if (!cache ||
4275 start >= cache->key.objectid + cache->key.offset) {
4276 if (cache)
4277 btrfs_put_block_group(cache);
4278 cache = btrfs_lookup_block_group(fs_info, start);
4279 BUG_ON(!cache);
4280 }
4281
4282 len = cache->key.objectid + cache->key.offset - start;
4283 len = min(len, end + 1 - start);
4284
4285 if (start < cache->last_byte_to_unpin) {
4286 len = min(len, cache->last_byte_to_unpin - start);
4287 btrfs_add_free_space(cache, start, len);
4288 }
Josef Bacik25179202008-10-29 14:49:05 -04004289
Yan, Zhengf0486c62010-05-16 10:46:25 -04004290 start += len;
4291
Josef Bacik25179202008-10-29 14:49:05 -04004292 spin_lock(&cache->space_info->lock);
4293 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004294 cache->pinned -= len;
4295 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004296 if (cache->ro) {
4297 cache->space_info->bytes_readonly += len;
4298 } else if (cache->reserved_pinned > 0) {
4299 len = min(len, cache->reserved_pinned);
4300 cache->reserved_pinned -= len;
4301 cache->space_info->bytes_reserved += len;
4302 }
Josef Bacik25179202008-10-29 14:49:05 -04004303 spin_unlock(&cache->lock);
4304 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004305 }
4306
4307 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004308 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004309 return 0;
4310}
4311
4312int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004313 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004314{
Yan Zheng11833d62009-09-11 16:11:19 -04004315 struct btrfs_fs_info *fs_info = root->fs_info;
4316 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004317 struct btrfs_block_rsv *block_rsv;
4318 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004319 u64 start;
4320 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004321 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004322 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004323
Yan Zheng11833d62009-09-11 16:11:19 -04004324 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4325 unpin = &fs_info->freed_extents[1];
4326 else
4327 unpin = &fs_info->freed_extents[0];
4328
Chris Masond3977122009-01-05 21:25:51 -05004329 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004330 ret = find_first_extent_bit(unpin, 0, &start, &end,
4331 EXTENT_DIRTY);
4332 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004333 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004334
Li Dongyang5378e602011-03-24 10:24:27 +00004335 if (btrfs_test_opt(root, DISCARD))
4336 ret = btrfs_discard_extent(root, start,
4337 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004338
Chris Mason1a5bc162007-10-15 16:15:26 -04004339 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004340 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004341 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004342 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004343
Yan, Zhengf0486c62010-05-16 10:46:25 -04004344 mutex_lock(&fs_info->durable_block_rsv_mutex);
4345 list_for_each_entry_safe(block_rsv, next_rsv,
4346 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004347
Yan, Zhengf0486c62010-05-16 10:46:25 -04004348 idx = trans->transid & 0x1;
4349 if (block_rsv->freed[idx] > 0) {
4350 block_rsv_add_bytes(block_rsv,
4351 block_rsv->freed[idx], 0);
4352 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004353 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004354 if (atomic_read(&block_rsv->usage) == 0) {
4355 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004356
Yan, Zhengf0486c62010-05-16 10:46:25 -04004357 if (block_rsv->freed[0] == 0 &&
4358 block_rsv->freed[1] == 0) {
4359 list_del_init(&block_rsv->list);
4360 kfree(block_rsv);
4361 }
4362 } else {
4363 btrfs_block_rsv_release(root, block_rsv, 0);
4364 }
4365 }
4366 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4367
Chris Masone20d96d2007-03-22 12:13:20 -04004368 return 0;
4369}
4370
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004371static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4372 struct btrfs_root *root,
4373 u64 bytenr, u64 num_bytes, u64 parent,
4374 u64 root_objectid, u64 owner_objectid,
4375 u64 owner_offset, int refs_to_drop,
4376 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004377{
Chris Masone2fa7222007-03-12 16:22:34 -04004378 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004379 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004380 struct btrfs_fs_info *info = root->fs_info;
4381 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004382 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004383 struct btrfs_extent_item *ei;
4384 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004385 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004386 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004387 int extent_slot = 0;
4388 int found_extent = 0;
4389 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004390 u32 item_size;
4391 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004392
Chris Mason5caf2a02007-04-02 11:20:42 -04004393 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004394 if (!path)
4395 return -ENOMEM;
4396
Chris Mason3c12ac72008-04-21 12:01:38 -04004397 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004398 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004399
4400 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4401 BUG_ON(!is_data && refs_to_drop != 1);
4402
4403 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4404 bytenr, num_bytes, parent,
4405 root_objectid, owner_objectid,
4406 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004407 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004408 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004409 while (extent_slot >= 0) {
4410 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004411 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004412 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004413 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004414 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4415 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004416 found_extent = 1;
4417 break;
4418 }
4419 if (path->slots[0] - extent_slot > 5)
4420 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004421 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004422 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004423#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4424 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4425 if (found_extent && item_size < sizeof(*ei))
4426 found_extent = 0;
4427#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004428 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004429 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004430 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004431 NULL, refs_to_drop,
4432 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004433 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004434 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04004435 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004436
4437 key.objectid = bytenr;
4438 key.type = BTRFS_EXTENT_ITEM_KEY;
4439 key.offset = num_bytes;
4440
Zheng Yan31840ae2008-09-23 13:14:14 -04004441 ret = btrfs_search_slot(trans, extent_root,
4442 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004443 if (ret) {
4444 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004445 ", was looking for %llu\n", ret,
4446 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004447 btrfs_print_leaf(extent_root, path->nodes[0]);
4448 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004449 BUG_ON(ret);
4450 extent_slot = path->slots[0];
4451 }
Chris Mason7bb86312007-12-11 09:25:06 -05004452 } else {
4453 btrfs_print_leaf(extent_root, path->nodes[0]);
4454 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004455 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004456 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004457 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004458 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004459 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004460 (unsigned long long)owner_objectid,
4461 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004462 }
Chris Mason5f39d392007-10-15 16:14:19 -04004463
4464 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004465 item_size = btrfs_item_size_nr(leaf, extent_slot);
4466#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4467 if (item_size < sizeof(*ei)) {
4468 BUG_ON(found_extent || extent_slot != path->slots[0]);
4469 ret = convert_extent_item_v0(trans, extent_root, path,
4470 owner_objectid, 0);
4471 BUG_ON(ret < 0);
4472
David Sterbab3b4aa72011-04-21 01:20:15 +02004473 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004474 path->leave_spinning = 1;
4475
4476 key.objectid = bytenr;
4477 key.type = BTRFS_EXTENT_ITEM_KEY;
4478 key.offset = num_bytes;
4479
4480 ret = btrfs_search_slot(trans, extent_root, &key, path,
4481 -1, 1);
4482 if (ret) {
4483 printk(KERN_ERR "umm, got %d back from search"
4484 ", was looking for %llu\n", ret,
4485 (unsigned long long)bytenr);
4486 btrfs_print_leaf(extent_root, path->nodes[0]);
4487 }
4488 BUG_ON(ret);
4489 extent_slot = path->slots[0];
4490 leaf = path->nodes[0];
4491 item_size = btrfs_item_size_nr(leaf, extent_slot);
4492 }
4493#endif
4494 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004495 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004496 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004497 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4498 struct btrfs_tree_block_info *bi;
4499 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4500 bi = (struct btrfs_tree_block_info *)(ei + 1);
4501 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004502 }
4503
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004504 refs = btrfs_extent_refs(leaf, ei);
4505 BUG_ON(refs < refs_to_drop);
4506 refs -= refs_to_drop;
4507
4508 if (refs > 0) {
4509 if (extent_op)
4510 __run_delayed_extent_op(extent_op, leaf, ei);
4511 /*
4512 * In the case of inline back ref, reference count will
4513 * be updated by remove_extent_backref
4514 */
4515 if (iref) {
4516 BUG_ON(!found_extent);
4517 } else {
4518 btrfs_set_extent_refs(leaf, ei, refs);
4519 btrfs_mark_buffer_dirty(leaf);
4520 }
4521 if (found_extent) {
4522 ret = remove_extent_backref(trans, extent_root, path,
4523 iref, refs_to_drop,
4524 is_data);
4525 BUG_ON(ret);
4526 }
4527 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004528 if (found_extent) {
4529 BUG_ON(is_data && refs_to_drop !=
4530 extent_data_ref_count(root, path, iref));
4531 if (iref) {
4532 BUG_ON(path->slots[0] != extent_slot);
4533 } else {
4534 BUG_ON(path->slots[0] != extent_slot + 1);
4535 path->slots[0] = extent_slot;
4536 num_to_del = 2;
4537 }
Chris Mason78fae272007-03-25 11:35:08 -04004538 }
Chris Masonb9473432009-03-13 11:00:37 -04004539
Chris Mason952fcca2008-02-18 16:33:44 -05004540 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4541 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004542 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004543 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01004544
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004545 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004546 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4547 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004548 } else {
4549 invalidate_mapping_pages(info->btree_inode->i_mapping,
4550 bytenr >> PAGE_CACHE_SHIFT,
4551 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004552 }
4553
Yan, Zhengf0486c62010-05-16 10:46:25 -04004554 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004555 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004556 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004557 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004558 return ret;
4559}
4560
4561/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004562 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004563 * delayed ref for that extent as well. This searches the delayed ref tree for
4564 * a given extent, and if there are no other delayed refs to be processed, it
4565 * removes it from the tree.
4566 */
4567static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4568 struct btrfs_root *root, u64 bytenr)
4569{
4570 struct btrfs_delayed_ref_head *head;
4571 struct btrfs_delayed_ref_root *delayed_refs;
4572 struct btrfs_delayed_ref_node *ref;
4573 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004574 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004575
4576 delayed_refs = &trans->transaction->delayed_refs;
4577 spin_lock(&delayed_refs->lock);
4578 head = btrfs_find_delayed_ref_head(trans, bytenr);
4579 if (!head)
4580 goto out;
4581
4582 node = rb_prev(&head->node.rb_node);
4583 if (!node)
4584 goto out;
4585
4586 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4587
4588 /* there are still entries for this ref, we can't drop it */
4589 if (ref->bytenr == bytenr)
4590 goto out;
4591
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004592 if (head->extent_op) {
4593 if (!head->must_insert_reserved)
4594 goto out;
4595 kfree(head->extent_op);
4596 head->extent_op = NULL;
4597 }
4598
Chris Mason1887be62009-03-13 10:11:24 -04004599 /*
4600 * waiting for the lock here would deadlock. If someone else has it
4601 * locked they are already in the process of dropping it anyway
4602 */
4603 if (!mutex_trylock(&head->mutex))
4604 goto out;
4605
4606 /*
4607 * at this point we have a head with no other entries. Go
4608 * ahead and process it.
4609 */
4610 head->node.in_tree = 0;
4611 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004612
Chris Mason1887be62009-03-13 10:11:24 -04004613 delayed_refs->num_entries--;
4614
4615 /*
4616 * we don't take a ref on the node because we're removing it from the
4617 * tree, so we just steal the ref the tree was holding.
4618 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004619 delayed_refs->num_heads--;
4620 if (list_empty(&head->cluster))
4621 delayed_refs->num_heads_ready--;
4622
4623 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004624 spin_unlock(&delayed_refs->lock);
4625
Yan, Zhengf0486c62010-05-16 10:46:25 -04004626 BUG_ON(head->extent_op);
4627 if (head->must_insert_reserved)
4628 ret = 1;
4629
4630 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004631 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004632 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004633out:
4634 spin_unlock(&delayed_refs->lock);
4635 return 0;
4636}
4637
Yan, Zhengf0486c62010-05-16 10:46:25 -04004638void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4639 struct btrfs_root *root,
4640 struct extent_buffer *buf,
4641 u64 parent, int last_ref)
4642{
4643 struct btrfs_block_rsv *block_rsv;
4644 struct btrfs_block_group_cache *cache = NULL;
4645 int ret;
4646
4647 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4648 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4649 parent, root->root_key.objectid,
4650 btrfs_header_level(buf),
4651 BTRFS_DROP_DELAYED_REF, NULL);
4652 BUG_ON(ret);
4653 }
4654
4655 if (!last_ref)
4656 return;
4657
4658 block_rsv = get_block_rsv(trans, root);
4659 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004660 if (block_rsv->space_info != cache->space_info)
4661 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004662
4663 if (btrfs_header_generation(buf) == trans->transid) {
4664 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4665 ret = check_ref_cleanup(trans, root, buf->start);
4666 if (!ret)
4667 goto pin;
4668 }
4669
4670 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4671 pin_down_extent(root, cache, buf->start, buf->len, 1);
4672 goto pin;
4673 }
4674
4675 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4676
4677 btrfs_add_free_space(cache, buf->start, buf->len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00004678 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004679 if (ret == -EAGAIN) {
4680 /* block group became read-only */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004681 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004682 goto out;
4683 }
4684
4685 ret = 1;
4686 spin_lock(&block_rsv->lock);
4687 if (block_rsv->reserved < block_rsv->size) {
4688 block_rsv->reserved += buf->len;
4689 ret = 0;
4690 }
4691 spin_unlock(&block_rsv->lock);
4692
4693 if (ret) {
4694 spin_lock(&cache->space_info->lock);
4695 cache->space_info->bytes_reserved -= buf->len;
Chris Mason36e39c42011-03-12 07:08:42 -05004696 cache->space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004697 spin_unlock(&cache->space_info->lock);
4698 }
4699 goto out;
4700 }
4701pin:
4702 if (block_rsv->durable && !cache->ro) {
4703 ret = 0;
4704 spin_lock(&cache->lock);
4705 if (!cache->ro) {
4706 cache->reserved_pinned += buf->len;
4707 ret = 1;
4708 }
4709 spin_unlock(&cache->lock);
4710
4711 if (ret) {
4712 spin_lock(&block_rsv->lock);
4713 block_rsv->freed[trans->transid & 0x1] += buf->len;
4714 spin_unlock(&block_rsv->lock);
4715 }
4716 }
4717out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004718 /*
4719 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4720 * anymore.
4721 */
4722 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004723 btrfs_put_block_group(cache);
4724}
4725
Chris Mason925baed2008-06-25 16:01:30 -04004726int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004727 struct btrfs_root *root,
4728 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004729 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004730{
4731 int ret;
4732
Chris Mason56bec292009-03-13 10:10:06 -04004733 /*
4734 * tree log blocks never actually go into the extent allocation
4735 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004736 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004737 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4738 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004739 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004740 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004741 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004742 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4743 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4744 parent, root_objectid, (int)owner,
4745 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004746 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004747 } else {
4748 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4749 parent, root_objectid, owner,
4750 offset, BTRFS_DROP_DELAYED_REF, NULL);
4751 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004752 }
Chris Mason925baed2008-06-25 16:01:30 -04004753 return ret;
4754}
4755
Chris Mason87ee04e2007-11-30 11:30:34 -05004756static u64 stripe_align(struct btrfs_root *root, u64 val)
4757{
4758 u64 mask = ((u64)root->stripesize - 1);
4759 u64 ret = (val + mask) & ~mask;
4760 return ret;
4761}
4762
Chris Masonfec577f2007-02-26 10:40:21 -05004763/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004764 * when we wait for progress in the block group caching, its because
4765 * our allocation attempt failed at least once. So, we must sleep
4766 * and let some progress happen before we try again.
4767 *
4768 * This function will sleep at least once waiting for new free space to
4769 * show up, and then it will check the block group free space numbers
4770 * for our min num_bytes. Another option is to have it go ahead
4771 * and look in the rbtree for a free extent of a given size, but this
4772 * is a good start.
4773 */
4774static noinline int
4775wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4776 u64 num_bytes)
4777{
Yan Zheng11833d62009-09-11 16:11:19 -04004778 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004779 DEFINE_WAIT(wait);
4780
Yan Zheng11833d62009-09-11 16:11:19 -04004781 caching_ctl = get_caching_control(cache);
4782 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004783 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004784
Yan Zheng11833d62009-09-11 16:11:19 -04004785 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb6c2011-03-29 13:46:06 +08004786 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004787
4788 put_caching_control(caching_ctl);
4789 return 0;
4790}
4791
4792static noinline int
4793wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4794{
4795 struct btrfs_caching_control *caching_ctl;
4796 DEFINE_WAIT(wait);
4797
4798 caching_ctl = get_caching_control(cache);
4799 if (!caching_ctl)
4800 return 0;
4801
4802 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4803
4804 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004805 return 0;
4806}
4807
Yan, Zhengb742bb82010-05-16 10:46:24 -04004808static int get_block_group_index(struct btrfs_block_group_cache *cache)
4809{
4810 int index;
4811 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4812 index = 0;
4813 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4814 index = 1;
4815 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4816 index = 2;
4817 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4818 index = 3;
4819 else
4820 index = 4;
4821 return index;
4822}
4823
Josef Bacik817d52f2009-07-13 21:29:25 -04004824enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004825 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004826 LOOP_CACHING_NOWAIT = 1,
4827 LOOP_CACHING_WAIT = 2,
4828 LOOP_ALLOC_CHUNK = 3,
4829 LOOP_NO_EMPTY_SIZE = 4,
4830};
4831
4832/*
Chris Masonfec577f2007-02-26 10:40:21 -05004833 * walks the btree of allocated extents and find a hole of a given size.
4834 * The key ins is changed to record the hole:
4835 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004836 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004837 * ins->offset == number of blocks
4838 * Any available blocks before search_start are skipped.
4839 */
Chris Masond3977122009-01-05 21:25:51 -05004840static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004841 struct btrfs_root *orig_root,
4842 u64 num_bytes, u64 empty_size,
4843 u64 search_start, u64 search_end,
4844 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00004845 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004846{
Josef Bacik80eb2342008-10-29 14:49:05 -04004847 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004848 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004849 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004850 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004851 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004852 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004853 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004854 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004855 int last_ptr_loop = 0;
4856 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004857 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004858 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004859 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004860 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004861 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004862 u64 ideal_cache_percent = 0;
4863 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004864
Chris Masondb945352007-10-15 16:15:53 -04004865 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004866 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004867 ins->objectid = 0;
4868 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004869
Josef Bacik2552d172009-04-03 10:14:19 -04004870 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004871 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00004872 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004873 return -ENOSPC;
4874 }
Josef Bacik2552d172009-04-03 10:14:19 -04004875
Josef Bacik67377732010-09-16 16:19:09 -04004876 /*
4877 * If the space info is for both data and metadata it means we have a
4878 * small filesystem and we can't use the clustering stuff.
4879 */
4880 if (btrfs_mixed_space_info(space_info))
4881 use_cluster = false;
4882
Chris Mason0ef3e662008-05-24 14:04:53 -04004883 if (orig_root->ref_cows || empty_size)
4884 allowed_chunk_alloc = 1;
4885
Josef Bacik67377732010-09-16 16:19:09 -04004886 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004887 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004888 if (!btrfs_test_opt(root, SSD))
4889 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004890 }
4891
Josef Bacik67377732010-09-16 16:19:09 -04004892 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4893 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004894 last_ptr = &root->fs_info->data_alloc_cluster;
4895 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004896
Chris Mason239b14b2008-03-24 15:02:07 -04004897 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004898 spin_lock(&last_ptr->lock);
4899 if (last_ptr->block_group)
4900 hint_byte = last_ptr->window_start;
4901 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004902 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004903
Chris Masona061fc82008-05-07 11:43:44 -04004904 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004905 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004906
Josef Bacik817d52f2009-07-13 21:29:25 -04004907 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004908 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004909
Josef Bacik2552d172009-04-03 10:14:19 -04004910 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004911ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004912 block_group = btrfs_lookup_block_group(root->fs_info,
4913 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004914 /*
4915 * we don't want to use the block group if it doesn't match our
4916 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004917 *
4918 * However if we are re-searching with an ideal block group
4919 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004920 */
4921 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004922 (block_group->cached != BTRFS_CACHE_NO ||
4923 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004924 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004925 if (list_empty(&block_group->list) ||
4926 block_group->ro) {
4927 /*
4928 * someone is removing this block group,
4929 * we can't jump into the have_block_group
4930 * target because our list pointers are not
4931 * valid
4932 */
4933 btrfs_put_block_group(block_group);
4934 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004935 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004936 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004937 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004938 }
Josef Bacik2552d172009-04-03 10:14:19 -04004939 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004940 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004941 }
Chris Mason42e70e72008-11-07 18:17:11 -05004942 }
Josef Bacik2552d172009-04-03 10:14:19 -04004943search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004944 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004945 list_for_each_entry(block_group, &space_info->block_groups[index],
4946 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004947 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004948 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004949
Josef Bacik11dfe352009-11-13 20:12:59 +00004950 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004951 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004952
Chris Mason83a50de2010-12-13 15:06:46 -05004953 /*
4954 * this can happen if we end up cycling through all the
4955 * raid types, but we want to make sure we only allocate
4956 * for the proper type.
4957 */
4958 if (!block_group_bits(block_group, data)) {
4959 u64 extra = BTRFS_BLOCK_GROUP_DUP |
4960 BTRFS_BLOCK_GROUP_RAID1 |
4961 BTRFS_BLOCK_GROUP_RAID10;
4962
4963 /*
4964 * if they asked for extra copies and this block group
4965 * doesn't provide them, bail. This does allow us to
4966 * fill raid0 from raid1.
4967 */
4968 if ((data & extra) && !(block_group->flags & extra))
4969 goto loop;
4970 }
4971
Josef Bacik2552d172009-04-03 10:14:19 -04004972have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004973 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004974 u64 free_percent;
4975
Josef Bacikb8399de2010-12-08 09:15:11 -05004976 ret = cache_block_group(block_group, trans,
4977 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04004978 if (block_group->cached == BTRFS_CACHE_FINISHED)
4979 goto have_block_group;
4980
Josef Bacikccf0e722009-11-10 21:23:48 -05004981 free_percent = btrfs_block_group_used(&block_group->item);
4982 free_percent *= 100;
4983 free_percent = div64_u64(free_percent,
4984 block_group->key.offset);
4985 free_percent = 100 - free_percent;
4986 if (free_percent > ideal_cache_percent &&
4987 likely(!block_group->ro)) {
4988 ideal_cache_offset = block_group->key.objectid;
4989 ideal_cache_percent = free_percent;
4990 }
4991
Josef Bacik817d52f2009-07-13 21:29:25 -04004992 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05004993 * We only want to start kthread caching if we are at
4994 * the point where we will wait for caching to make
4995 * progress, or if our ideal search is over and we've
4996 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04004997 */
4998 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05004999 (loop > LOOP_FIND_IDEAL &&
5000 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacikb8399de2010-12-08 09:15:11 -05005001 ret = cache_block_group(block_group, trans,
5002 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005003 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005004 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005005 found_uncached_bg = true;
5006
5007 /*
5008 * If loop is set for cached only, try the next block
5009 * group.
5010 */
5011 if (loop == LOOP_FIND_IDEAL)
5012 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005013 }
5014
Josef Bacik817d52f2009-07-13 21:29:25 -04005015 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005016 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005017 found_uncached_bg = true;
5018
Josef Bacikea6a4782008-11-20 12:16:16 -05005019 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005020 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005021
Chris Masonff5714c2011-05-28 07:00:39 -04005022 spin_lock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005023 if (cached &&
Chris Masonff5714c2011-05-28 07:00:39 -04005024 block_group->free_space_ctl->free_space <
5025 num_bytes + empty_size) {
5026 spin_unlock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005027 goto loop;
5028 }
Chris Masonff5714c2011-05-28 07:00:39 -04005029 spin_unlock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005030
Josef Bacik0a243252009-09-11 16:11:20 -04005031 /*
5032 * Ok we want to try and use the cluster allocator, so lets look
5033 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5034 * have tried the cluster allocator plenty of times at this
5035 * point and not have found anything, so we are likely way too
5036 * fragmented for the clustering stuff to find anything, so lets
5037 * just skip it and let the allocator find whatever block it can
5038 * find
5039 */
5040 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005041 /*
5042 * the refill lock keeps out other
5043 * people trying to start a new cluster
5044 */
5045 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005046 if (last_ptr->block_group &&
5047 (last_ptr->block_group->ro ||
5048 !block_group_bits(last_ptr->block_group, data))) {
5049 offset = 0;
5050 goto refill_cluster;
5051 }
5052
Chris Masonfa9c0d72009-04-03 09:47:43 -04005053 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5054 num_bytes, search_start);
5055 if (offset) {
5056 /* we have a block, we're done */
5057 spin_unlock(&last_ptr->refill_lock);
5058 goto checks;
5059 }
5060
5061 spin_lock(&last_ptr->lock);
5062 /*
5063 * whoops, this cluster doesn't actually point to
5064 * this block group. Get a ref on the block
5065 * group is does point to and try again
5066 */
5067 if (!last_ptr_loop && last_ptr->block_group &&
5068 last_ptr->block_group != block_group) {
5069
5070 btrfs_put_block_group(block_group);
5071 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005072 btrfs_get_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005073 spin_unlock(&last_ptr->lock);
5074 spin_unlock(&last_ptr->refill_lock);
5075
5076 last_ptr_loop = 1;
5077 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005078 /*
5079 * we know this block group is properly
5080 * in the list because
5081 * btrfs_remove_block_group, drops the
5082 * cluster before it removes the block
5083 * group from the list
5084 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04005085 goto have_block_group;
5086 }
5087 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005088refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04005089 /*
5090 * this cluster didn't work out, free it and
5091 * start over
5092 */
5093 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5094
5095 last_ptr_loop = 0;
5096
5097 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005098 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005099 block_group, last_ptr,
5100 offset, num_bytes,
5101 empty_cluster + empty_size);
5102 if (ret == 0) {
5103 /*
5104 * now pull our allocation out of this
5105 * cluster
5106 */
5107 offset = btrfs_alloc_from_cluster(block_group,
5108 last_ptr, num_bytes,
5109 search_start);
5110 if (offset) {
5111 /* we found one, proceed */
5112 spin_unlock(&last_ptr->refill_lock);
5113 goto checks;
5114 }
Josef Bacik0a243252009-09-11 16:11:20 -04005115 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5116 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005117 spin_unlock(&last_ptr->refill_lock);
5118
Josef Bacik0a243252009-09-11 16:11:20 -04005119 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005120 wait_block_group_cache_progress(block_group,
5121 num_bytes + empty_cluster + empty_size);
5122 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005123 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005124
Chris Masonfa9c0d72009-04-03 09:47:43 -04005125 /*
5126 * at this point we either didn't find a cluster
5127 * or we weren't able to allocate a block from our
5128 * cluster. Free the cluster we've been trying
5129 * to use, and go to the next block group
5130 */
Josef Bacik0a243252009-09-11 16:11:20 -04005131 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005132 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005133 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005134 }
5135
Josef Bacik6226cb02009-04-03 10:14:18 -04005136 offset = btrfs_find_space_for_alloc(block_group, search_start,
5137 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005138 /*
5139 * If we didn't find a chunk, and we haven't failed on this
5140 * block group before, and this block group is in the middle of
5141 * caching and we are ok with waiting, then go ahead and wait
5142 * for progress to be made, and set failed_alloc to true.
5143 *
5144 * If failed_alloc is true then we've already waited on this
5145 * block group once and should move on to the next block group.
5146 */
5147 if (!offset && !failed_alloc && !cached &&
5148 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005149 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005150 num_bytes + empty_size);
5151 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005152 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005153 } else if (!offset) {
5154 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005155 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005156checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005157 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005158 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005159 if (search_start + num_bytes >= search_end) {
5160 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005161 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005162 }
Chris Masone37c9e62007-05-09 20:13:14 -04005163
Josef Bacik2552d172009-04-03 10:14:19 -04005164 /* move on to the next group */
5165 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005166 block_group->key.objectid + block_group->key.offset) {
5167 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005168 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005169 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005170
Josef Bacik2552d172009-04-03 10:14:19 -04005171 ins->objectid = search_start;
5172 ins->offset = num_bytes;
5173
Josef Bacik6226cb02009-04-03 10:14:18 -04005174 if (offset < search_start)
5175 btrfs_add_free_space(block_group, offset,
5176 search_start - offset);
5177 BUG_ON(offset > search_start);
5178
Li Dongyangb4d00d52011-03-24 10:24:25 +00005179 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005180 (data & BTRFS_BLOCK_GROUP_DATA));
5181 if (ret == -EAGAIN) {
5182 btrfs_add_free_space(block_group, offset, num_bytes);
5183 goto loop;
5184 }
Yan Zheng11833d62009-09-11 16:11:19 -04005185
Josef Bacik2552d172009-04-03 10:14:19 -04005186 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005187 ins->objectid = search_start;
5188 ins->offset = num_bytes;
5189
5190 if (offset < search_start)
5191 btrfs_add_free_space(block_group, offset,
5192 search_start - offset);
5193 BUG_ON(offset > search_start);
Josef Bacikd82a6f12011-05-11 15:26:06 -04005194 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005195 break;
5196loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005197 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005198 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005199 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d72009-04-03 09:47:43 -04005200 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005201 }
5202 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005203
Yan, Zhengb742bb82010-05-16 10:46:24 -04005204 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5205 goto search;
5206
Josef Bacikccf0e722009-11-10 21:23:48 -05005207 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5208 * for them to make caching progress. Also
5209 * determine the best possible bg to cache
5210 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5211 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005212 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5213 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5214 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5215 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04005216 */
Josef Bacik723bda22011-05-27 16:11:38 -04005217 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005218 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005219 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005220 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005221 loop++;
5222 if (!ideal_cache_percent &&
5223 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005224 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005225
5226 /*
5227 * 1 of the following 2 things have happened so far
5228 *
5229 * 1) We found an ideal block group for caching that
5230 * is mostly full and will cache quickly, so we might
5231 * as well wait for it.
5232 *
5233 * 2) We searched for cached only and we didn't find
5234 * anything, and we didn't start any caching kthreads
5235 * either, so chances are we will loop through and
5236 * start a couple caching kthreads, and then come back
5237 * around and just wait for them. This will be slower
5238 * because we will have 2 caching kthreads reading at
5239 * the same time when we could have just started one
5240 * and waited for it to get far enough to give us an
5241 * allocation, so go ahead and go to the wait caching
5242 * loop.
5243 */
5244 loop = LOOP_CACHING_WAIT;
5245 search_start = ideal_cache_offset;
5246 ideal_cache_percent = 0;
5247 goto ideal_cache;
5248 } else if (loop == LOOP_FIND_IDEAL) {
5249 /*
5250 * Didn't find a uncached bg, wait on anything we find
5251 * next.
5252 */
5253 loop = LOOP_CACHING_WAIT;
5254 goto search;
5255 }
5256
Josef Bacik723bda22011-05-27 16:11:38 -04005257 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04005258
5259 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik723bda22011-05-27 16:11:38 -04005260 if (allowed_chunk_alloc) {
5261 ret = do_chunk_alloc(trans, root, num_bytes +
5262 2 * 1024 * 1024, data,
5263 CHUNK_ALLOC_LIMITED);
5264 allowed_chunk_alloc = 0;
5265 if (ret == 1)
5266 done_chunk_alloc = 1;
5267 } else if (!done_chunk_alloc &&
5268 space_info->force_alloc ==
5269 CHUNK_ALLOC_NO_FORCE) {
5270 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
5271 }
5272
5273 /*
5274 * We didn't allocate a chunk, go ahead and drop the
5275 * empty size and loop again.
5276 */
5277 if (!done_chunk_alloc)
5278 loop = LOOP_NO_EMPTY_SIZE;
5279 }
5280
5281 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005282 empty_size = 0;
5283 empty_cluster = 0;
5284 }
Chris Mason42e70e72008-11-07 18:17:11 -05005285
Josef Bacik723bda22011-05-27 16:11:38 -04005286 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04005287 } else if (!ins->objectid) {
5288 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04005289 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005290 ret = 0;
5291 }
Chris Mason0b86a832008-03-24 15:01:56 -04005292
Chris Mason0f70abe2007-02-28 16:46:22 -05005293 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005294}
Chris Masonec44a352008-04-28 15:29:52 -04005295
Josef Bacik9ed74f22009-09-11 16:12:44 -04005296static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5297 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005298{
5299 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005300 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005301
Josef Bacik9ed74f22009-09-11 16:12:44 -04005302 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005303 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5304 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005305 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005306 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005307 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005308 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5309 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005310 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005311 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005312 (unsigned long long)info->bytes_pinned,
5313 (unsigned long long)info->bytes_reserved,
5314 (unsigned long long)info->bytes_may_use,
5315 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005316 spin_unlock(&info->lock);
5317
5318 if (!dump_block_groups)
5319 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005320
Josef Bacik80eb2342008-10-29 14:49:05 -04005321 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005322again:
5323 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005324 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005325 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5326 "%llu pinned %llu reserved\n",
5327 (unsigned long long)cache->key.objectid,
5328 (unsigned long long)cache->key.offset,
5329 (unsigned long long)btrfs_block_group_used(&cache->item),
5330 (unsigned long long)cache->pinned,
5331 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005332 btrfs_dump_free_space(cache, bytes);
5333 spin_unlock(&cache->lock);
5334 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005335 if (++index < BTRFS_NR_RAID_TYPES)
5336 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005337 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005338}
Zheng Yane8569812008-09-26 10:05:48 -04005339
Yan Zheng11833d62009-09-11 16:11:19 -04005340int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5341 struct btrfs_root *root,
5342 u64 num_bytes, u64 min_alloc_size,
5343 u64 empty_size, u64 hint_byte,
5344 u64 search_end, struct btrfs_key *ins,
5345 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005346{
5347 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005348 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005349
Josef Bacik6a632092009-02-20 11:00:09 -05005350 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005351again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005352 /*
5353 * the only place that sets empty_size is btrfs_realloc_node, which
5354 * is not called recursively on allocations
5355 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005356 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005357 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005358 num_bytes + 2 * 1024 * 1024, data,
5359 CHUNK_ALLOC_NO_FORCE);
Chris Mason0b86a832008-03-24 15:01:56 -04005360
Chris Masondb945352007-10-15 16:15:53 -04005361 WARN_ON(num_bytes < root->sectorsize);
5362 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005363 search_start, search_end, hint_byte,
5364 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005365
Chris Mason98d20f62008-04-14 09:46:10 -04005366 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5367 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005368 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005369 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005370 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005371 num_bytes, data, CHUNK_ALLOC_FORCE);
Chris Mason98d20f62008-04-14 09:46:10 -04005372 goto again;
5373 }
Chris Mason91435652011-02-16 13:10:41 -05005374 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005375 struct btrfs_space_info *sinfo;
5376
5377 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005378 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5379 "wanted %llu\n", (unsigned long long)data,
5380 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005381 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005382 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005383
liubo1abe9b82011-03-24 11:18:59 +00005384 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5385
Josef Bacik0f9dd462008-09-23 13:14:11 -04005386 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005387}
5388
Chris Mason65b51a02008-08-01 15:11:20 -04005389int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5390{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005391 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005392 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005393
Josef Bacik0f9dd462008-09-23 13:14:11 -04005394 cache = btrfs_lookup_block_group(root->fs_info, start);
5395 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005396 printk(KERN_ERR "Unable to find block group for %llu\n",
5397 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005398 return -ENOSPC;
5399 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005400
Li Dongyang5378e602011-03-24 10:24:27 +00005401 if (btrfs_test_opt(root, DISCARD))
5402 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005403
Josef Bacik0f9dd462008-09-23 13:14:11 -04005404 btrfs_add_free_space(cache, start, len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00005405 btrfs_update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005406 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005407
liubo1abe9b82011-03-24 11:18:59 +00005408 trace_btrfs_reserved_extent_free(root, start, len);
5409
Chris Masone6dcd2d2008-07-17 12:53:50 -04005410 return ret;
5411}
5412
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005413static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5414 struct btrfs_root *root,
5415 u64 parent, u64 root_objectid,
5416 u64 flags, u64 owner, u64 offset,
5417 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005418{
5419 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005420 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005421 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005422 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005423 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005424 struct extent_buffer *leaf;
5425 int type;
5426 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005427
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005428 if (parent > 0)
5429 type = BTRFS_SHARED_DATA_REF_KEY;
5430 else
5431 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005432
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005433 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005434
5435 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005436 if (!path)
5437 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005438
Chris Masonb9473432009-03-13 11:00:37 -04005439 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005440 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5441 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005442 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005443
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005444 leaf = path->nodes[0];
5445 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005446 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005447 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5448 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5449 btrfs_set_extent_flags(leaf, extent_item,
5450 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005451
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005452 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5453 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5454 if (parent > 0) {
5455 struct btrfs_shared_data_ref *ref;
5456 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5457 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5458 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5459 } else {
5460 struct btrfs_extent_data_ref *ref;
5461 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5462 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5463 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5464 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5465 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5466 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005467
5468 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005469 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005470
Yan, Zhengf0486c62010-05-16 10:46:25 -04005471 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005472 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005473 printk(KERN_ERR "btrfs update block group failed for %llu "
5474 "%llu\n", (unsigned long long)ins->objectid,
5475 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005476 BUG();
5477 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005478 return ret;
5479}
5480
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005481static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5482 struct btrfs_root *root,
5483 u64 parent, u64 root_objectid,
5484 u64 flags, struct btrfs_disk_key *key,
5485 int level, struct btrfs_key *ins)
5486{
5487 int ret;
5488 struct btrfs_fs_info *fs_info = root->fs_info;
5489 struct btrfs_extent_item *extent_item;
5490 struct btrfs_tree_block_info *block_info;
5491 struct btrfs_extent_inline_ref *iref;
5492 struct btrfs_path *path;
5493 struct extent_buffer *leaf;
5494 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5495
5496 path = btrfs_alloc_path();
5497 BUG_ON(!path);
5498
5499 path->leave_spinning = 1;
5500 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5501 ins, size);
5502 BUG_ON(ret);
5503
5504 leaf = path->nodes[0];
5505 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5506 struct btrfs_extent_item);
5507 btrfs_set_extent_refs(leaf, extent_item, 1);
5508 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5509 btrfs_set_extent_flags(leaf, extent_item,
5510 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5511 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5512
5513 btrfs_set_tree_block_key(leaf, block_info, key);
5514 btrfs_set_tree_block_level(leaf, block_info, level);
5515
5516 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5517 if (parent > 0) {
5518 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5519 btrfs_set_extent_inline_ref_type(leaf, iref,
5520 BTRFS_SHARED_BLOCK_REF_KEY);
5521 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5522 } else {
5523 btrfs_set_extent_inline_ref_type(leaf, iref,
5524 BTRFS_TREE_BLOCK_REF_KEY);
5525 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5526 }
5527
5528 btrfs_mark_buffer_dirty(leaf);
5529 btrfs_free_path(path);
5530
Yan, Zhengf0486c62010-05-16 10:46:25 -04005531 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005532 if (ret) {
5533 printk(KERN_ERR "btrfs update block group failed for %llu "
5534 "%llu\n", (unsigned long long)ins->objectid,
5535 (unsigned long long)ins->offset);
5536 BUG();
5537 }
5538 return ret;
5539}
5540
5541int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5542 struct btrfs_root *root,
5543 u64 root_objectid, u64 owner,
5544 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005545{
5546 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005547
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005548 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005549
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005550 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5551 0, root_objectid, owner, offset,
5552 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005553 return ret;
5554}
Chris Masone02119d2008-09-05 16:13:11 -04005555
5556/*
5557 * this is used by the tree logging recovery code. It records that
5558 * an extent has been allocated and makes sure to clear the free
5559 * space cache bits as well
5560 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5562 struct btrfs_root *root,
5563 u64 root_objectid, u64 owner, u64 offset,
5564 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005565{
5566 int ret;
5567 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005568 struct btrfs_caching_control *caching_ctl;
5569 u64 start = ins->objectid;
5570 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005571
Chris Masone02119d2008-09-05 16:13:11 -04005572 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005573 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005574 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005575
Yan Zheng11833d62009-09-11 16:11:19 -04005576 if (!caching_ctl) {
5577 BUG_ON(!block_group_cache_done(block_group));
5578 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5579 BUG_ON(ret);
5580 } else {
5581 mutex_lock(&caching_ctl->mutex);
5582
5583 if (start >= caching_ctl->progress) {
5584 ret = add_excluded_extent(root, start, num_bytes);
5585 BUG_ON(ret);
5586 } else if (start + num_bytes <= caching_ctl->progress) {
5587 ret = btrfs_remove_free_space(block_group,
5588 start, num_bytes);
5589 BUG_ON(ret);
5590 } else {
5591 num_bytes = caching_ctl->progress - start;
5592 ret = btrfs_remove_free_space(block_group,
5593 start, num_bytes);
5594 BUG_ON(ret);
5595
5596 start = caching_ctl->progress;
5597 num_bytes = ins->objectid + ins->offset -
5598 caching_ctl->progress;
5599 ret = add_excluded_extent(root, start, num_bytes);
5600 BUG_ON(ret);
5601 }
5602
5603 mutex_unlock(&caching_ctl->mutex);
5604 put_caching_control(caching_ctl);
5605 }
5606
Li Dongyangb4d00d52011-03-24 10:24:25 +00005607 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005608 BUG_ON(ret);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005609 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5611 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005612 return ret;
5613}
5614
Chris Mason65b51a02008-08-01 15:11:20 -04005615struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5616 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005617 u64 bytenr, u32 blocksize,
5618 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005619{
5620 struct extent_buffer *buf;
5621
5622 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5623 if (!buf)
5624 return ERR_PTR(-ENOMEM);
5625 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005626 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005627 btrfs_tree_lock(buf);
5628 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005629
5630 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005631 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005632
Chris Masond0c803c2008-09-11 16:17:57 -04005633 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005634 /*
5635 * we allow two log transactions at a time, use different
5636 * EXENT bit to differentiate dirty pages.
5637 */
5638 if (root->log_transid % 2 == 0)
5639 set_extent_dirty(&root->dirty_log_pages, buf->start,
5640 buf->start + buf->len - 1, GFP_NOFS);
5641 else
5642 set_extent_new(&root->dirty_log_pages, buf->start,
5643 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005644 } else {
5645 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5646 buf->start + buf->len - 1, GFP_NOFS);
5647 }
Chris Mason65b51a02008-08-01 15:11:20 -04005648 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005649 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005650 return buf;
5651}
5652
Yan, Zhengf0486c62010-05-16 10:46:25 -04005653static struct btrfs_block_rsv *
5654use_block_rsv(struct btrfs_trans_handle *trans,
5655 struct btrfs_root *root, u32 blocksize)
5656{
5657 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005658 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005659 int ret;
5660
5661 block_rsv = get_block_rsv(trans, root);
5662
5663 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005664 ret = reserve_metadata_bytes(trans, root, block_rsv,
5665 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005666 /*
5667 * If we couldn't reserve metadata bytes try and use some from
5668 * the global reserve.
5669 */
5670 if (ret && block_rsv != global_rsv) {
5671 ret = block_rsv_use_bytes(global_rsv, blocksize);
5672 if (!ret)
5673 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005674 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005675 } else if (ret) {
5676 return ERR_PTR(ret);
5677 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005678 return block_rsv;
5679 }
5680
5681 ret = block_rsv_use_bytes(block_rsv, blocksize);
5682 if (!ret)
5683 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005684 if (ret) {
5685 WARN_ON(1);
5686 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5687 0);
5688 if (!ret) {
5689 spin_lock(&block_rsv->lock);
5690 block_rsv->size += blocksize;
5691 spin_unlock(&block_rsv->lock);
5692 return block_rsv;
5693 } else if (ret && block_rsv != global_rsv) {
5694 ret = block_rsv_use_bytes(global_rsv, blocksize);
5695 if (!ret)
5696 return global_rsv;
5697 }
5698 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005699
Yan, Zhengf0486c62010-05-16 10:46:25 -04005700 return ERR_PTR(-ENOSPC);
5701}
5702
5703static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5704{
5705 block_rsv_add_bytes(block_rsv, blocksize, 0);
5706 block_rsv_release_bytes(block_rsv, NULL, 0);
5707}
5708
Chris Masonfec577f2007-02-26 10:40:21 -05005709/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005710 * finds a free extent and does all the dirty work required for allocation
5711 * returns the key for the extent through ins, and a tree buffer for
5712 * the first block of the extent through buf.
5713 *
Chris Masonfec577f2007-02-26 10:40:21 -05005714 * returns the tree buffer or NULL.
5715 */
Chris Mason5f39d392007-10-15 16:14:19 -04005716struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005717 struct btrfs_root *root, u32 blocksize,
5718 u64 parent, u64 root_objectid,
5719 struct btrfs_disk_key *key, int level,
5720 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005721{
Chris Masone2fa7222007-03-12 16:22:34 -04005722 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005723 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005724 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005725 u64 flags = 0;
5726 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005727
Yan, Zhengf0486c62010-05-16 10:46:25 -04005728
5729 block_rsv = use_block_rsv(trans, root, blocksize);
5730 if (IS_ERR(block_rsv))
5731 return ERR_CAST(block_rsv);
5732
5733 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5734 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005735 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005736 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005737 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005738 }
Chris Mason55c69072008-01-09 15:55:33 -05005739
Chris Mason4008c042009-02-12 14:09:45 -05005740 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5741 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005742 BUG_ON(IS_ERR(buf));
5743
5744 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5745 if (parent == 0)
5746 parent = ins.objectid;
5747 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5748 } else
5749 BUG_ON(parent > 0);
5750
5751 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5752 struct btrfs_delayed_extent_op *extent_op;
5753 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5754 BUG_ON(!extent_op);
5755 if (key)
5756 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5757 else
5758 memset(&extent_op->key, 0, sizeof(extent_op->key));
5759 extent_op->flags_to_set = flags;
5760 extent_op->update_key = 1;
5761 extent_op->update_flags = 1;
5762 extent_op->is_data = 0;
5763
5764 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5765 ins.offset, parent, root_objectid,
5766 level, BTRFS_ADD_DELAYED_EXTENT,
5767 extent_op);
5768 BUG_ON(ret);
5769 }
Chris Masonfec577f2007-02-26 10:40:21 -05005770 return buf;
5771}
Chris Masona28ec192007-03-06 20:08:01 -05005772
Yan Zheng2c47e6052009-06-27 21:07:35 -04005773struct walk_control {
5774 u64 refs[BTRFS_MAX_LEVEL];
5775 u64 flags[BTRFS_MAX_LEVEL];
5776 struct btrfs_key update_progress;
5777 int stage;
5778 int level;
5779 int shared_level;
5780 int update_ref;
5781 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005782 int reada_slot;
5783 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005784};
5785
5786#define DROP_REFERENCE 1
5787#define UPDATE_BACKREF 2
5788
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005789static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5790 struct btrfs_root *root,
5791 struct walk_control *wc,
5792 struct btrfs_path *path)
5793{
5794 u64 bytenr;
5795 u64 generation;
5796 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005797 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005798 u32 nritems;
5799 u32 blocksize;
5800 struct btrfs_key key;
5801 struct extent_buffer *eb;
5802 int ret;
5803 int slot;
5804 int nread = 0;
5805
5806 if (path->slots[wc->level] < wc->reada_slot) {
5807 wc->reada_count = wc->reada_count * 2 / 3;
5808 wc->reada_count = max(wc->reada_count, 2);
5809 } else {
5810 wc->reada_count = wc->reada_count * 3 / 2;
5811 wc->reada_count = min_t(int, wc->reada_count,
5812 BTRFS_NODEPTRS_PER_BLOCK(root));
5813 }
5814
5815 eb = path->nodes[wc->level];
5816 nritems = btrfs_header_nritems(eb);
5817 blocksize = btrfs_level_size(root, wc->level - 1);
5818
5819 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5820 if (nread >= wc->reada_count)
5821 break;
5822
5823 cond_resched();
5824 bytenr = btrfs_node_blockptr(eb, slot);
5825 generation = btrfs_node_ptr_generation(eb, slot);
5826
5827 if (slot == path->slots[wc->level])
5828 goto reada;
5829
5830 if (wc->stage == UPDATE_BACKREF &&
5831 generation <= root->root_key.offset)
5832 continue;
5833
Yan, Zheng94fcca92009-10-09 09:25:16 -04005834 /* We don't lock the tree block, it's OK to be racy here */
5835 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5836 &refs, &flags);
5837 BUG_ON(ret);
5838 BUG_ON(refs == 0);
5839
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005840 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005841 if (refs == 1)
5842 goto reada;
5843
Yan, Zheng94fcca92009-10-09 09:25:16 -04005844 if (wc->level == 1 &&
5845 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5846 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005847 if (!wc->update_ref ||
5848 generation <= root->root_key.offset)
5849 continue;
5850 btrfs_node_key_to_cpu(eb, &key, slot);
5851 ret = btrfs_comp_cpu_keys(&key,
5852 &wc->update_progress);
5853 if (ret < 0)
5854 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005855 } else {
5856 if (wc->level == 1 &&
5857 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5858 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005859 }
5860reada:
5861 ret = readahead_tree_block(root, bytenr, blocksize,
5862 generation);
5863 if (ret)
5864 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005865 nread++;
5866 }
5867 wc->reada_slot = slot;
5868}
5869
Chris Mason9aca1d52007-03-13 11:09:37 -04005870/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005871 * hepler to process tree block while walking down the tree.
5872 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005873 * when wc->stage == UPDATE_BACKREF, this function updates
5874 * back refs for pointers in the block.
5875 *
5876 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005877 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005878static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5879 struct btrfs_root *root,
5880 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005881 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005882{
5883 int level = wc->level;
5884 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005885 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5886 int ret;
5887
5888 if (wc->stage == UPDATE_BACKREF &&
5889 btrfs_header_owner(eb) != root->root_key.objectid)
5890 return 1;
5891
5892 /*
5893 * when reference count of tree block is 1, it won't increase
5894 * again. once full backref flag is set, we never clear it.
5895 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005896 if (lookup_info &&
5897 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5898 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005899 BUG_ON(!path->locks[level]);
5900 ret = btrfs_lookup_extent_info(trans, root,
5901 eb->start, eb->len,
5902 &wc->refs[level],
5903 &wc->flags[level]);
5904 BUG_ON(ret);
5905 BUG_ON(wc->refs[level] == 0);
5906 }
5907
Yan Zheng2c47e6052009-06-27 21:07:35 -04005908 if (wc->stage == DROP_REFERENCE) {
5909 if (wc->refs[level] > 1)
5910 return 1;
5911
5912 if (path->locks[level] && !wc->keep_locks) {
5913 btrfs_tree_unlock(eb);
5914 path->locks[level] = 0;
5915 }
5916 return 0;
5917 }
5918
5919 /* wc->stage == UPDATE_BACKREF */
5920 if (!(wc->flags[level] & flag)) {
5921 BUG_ON(!path->locks[level]);
5922 ret = btrfs_inc_ref(trans, root, eb, 1);
5923 BUG_ON(ret);
5924 ret = btrfs_dec_ref(trans, root, eb, 0);
5925 BUG_ON(ret);
5926 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5927 eb->len, flag, 0);
5928 BUG_ON(ret);
5929 wc->flags[level] |= flag;
5930 }
5931
5932 /*
5933 * the block is shared by multiple trees, so it's not good to
5934 * keep the tree lock
5935 */
5936 if (path->locks[level] && level > 0) {
5937 btrfs_tree_unlock(eb);
5938 path->locks[level] = 0;
5939 }
5940 return 0;
5941}
5942
5943/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005944 * hepler to process tree block pointer.
5945 *
5946 * when wc->stage == DROP_REFERENCE, this function checks
5947 * reference count of the block pointed to. if the block
5948 * is shared and we need update back refs for the subtree
5949 * rooted at the block, this function changes wc->stage to
5950 * UPDATE_BACKREF. if the block is shared and there is no
5951 * need to update back, this function drops the reference
5952 * to the block.
5953 *
5954 * NOTE: return value 1 means we should stop walking down.
5955 */
5956static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5957 struct btrfs_root *root,
5958 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005959 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005960{
5961 u64 bytenr;
5962 u64 generation;
5963 u64 parent;
5964 u32 blocksize;
5965 struct btrfs_key key;
5966 struct extent_buffer *next;
5967 int level = wc->level;
5968 int reada = 0;
5969 int ret = 0;
5970
5971 generation = btrfs_node_ptr_generation(path->nodes[level],
5972 path->slots[level]);
5973 /*
5974 * if the lower level block was created before the snapshot
5975 * was created, we know there is no need to update back refs
5976 * for the subtree
5977 */
5978 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005979 generation <= root->root_key.offset) {
5980 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005981 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005982 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005983
5984 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5985 blocksize = btrfs_level_size(root, level - 1);
5986
5987 next = btrfs_find_tree_block(root, bytenr, blocksize);
5988 if (!next) {
5989 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00005990 if (!next)
5991 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005992 reada = 1;
5993 }
5994 btrfs_tree_lock(next);
5995 btrfs_set_lock_blocking(next);
5996
Yan, Zheng94fcca92009-10-09 09:25:16 -04005997 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5998 &wc->refs[level - 1],
5999 &wc->flags[level - 1]);
6000 BUG_ON(ret);
6001 BUG_ON(wc->refs[level - 1] == 0);
6002 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006003
Yan, Zheng94fcca92009-10-09 09:25:16 -04006004 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006005 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006006 if (level == 1 &&
6007 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6008 goto skip;
6009
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006010 if (!wc->update_ref ||
6011 generation <= root->root_key.offset)
6012 goto skip;
6013
6014 btrfs_node_key_to_cpu(path->nodes[level], &key,
6015 path->slots[level]);
6016 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6017 if (ret < 0)
6018 goto skip;
6019
6020 wc->stage = UPDATE_BACKREF;
6021 wc->shared_level = level - 1;
6022 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006023 } else {
6024 if (level == 1 &&
6025 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6026 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006027 }
6028
6029 if (!btrfs_buffer_uptodate(next, generation)) {
6030 btrfs_tree_unlock(next);
6031 free_extent_buffer(next);
6032 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006033 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006034 }
6035
6036 if (!next) {
6037 if (reada && level == 1)
6038 reada_walk_down(trans, root, wc, path);
6039 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006040 if (!next)
6041 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006042 btrfs_tree_lock(next);
6043 btrfs_set_lock_blocking(next);
6044 }
6045
6046 level--;
6047 BUG_ON(level != btrfs_header_level(next));
6048 path->nodes[level] = next;
6049 path->slots[level] = 0;
6050 path->locks[level] = 1;
6051 wc->level = level;
6052 if (wc->level == 1)
6053 wc->reada_slot = 0;
6054 return 0;
6055skip:
6056 wc->refs[level - 1] = 0;
6057 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006058 if (wc->stage == DROP_REFERENCE) {
6059 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6060 parent = path->nodes[level]->start;
6061 } else {
6062 BUG_ON(root->root_key.objectid !=
6063 btrfs_header_owner(path->nodes[level]));
6064 parent = 0;
6065 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006066
Yan, Zheng94fcca92009-10-09 09:25:16 -04006067 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6068 root->root_key.objectid, level - 1, 0);
6069 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006070 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006071 btrfs_tree_unlock(next);
6072 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006073 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006074 return 1;
6075}
6076
6077/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006078 * hepler to process tree block while walking up the tree.
6079 *
6080 * when wc->stage == DROP_REFERENCE, this function drops
6081 * reference count on the block.
6082 *
6083 * when wc->stage == UPDATE_BACKREF, this function changes
6084 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6085 * to UPDATE_BACKREF previously while processing the block.
6086 *
6087 * NOTE: return value 1 means we should stop walking up.
6088 */
6089static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6090 struct btrfs_root *root,
6091 struct btrfs_path *path,
6092 struct walk_control *wc)
6093{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006094 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006095 int level = wc->level;
6096 struct extent_buffer *eb = path->nodes[level];
6097 u64 parent = 0;
6098
6099 if (wc->stage == UPDATE_BACKREF) {
6100 BUG_ON(wc->shared_level < level);
6101 if (level < wc->shared_level)
6102 goto out;
6103
Yan Zheng2c47e6052009-06-27 21:07:35 -04006104 ret = find_next_key(path, level + 1, &wc->update_progress);
6105 if (ret > 0)
6106 wc->update_ref = 0;
6107
6108 wc->stage = DROP_REFERENCE;
6109 wc->shared_level = -1;
6110 path->slots[level] = 0;
6111
6112 /*
6113 * check reference count again if the block isn't locked.
6114 * we should start walking down the tree again if reference
6115 * count is one.
6116 */
6117 if (!path->locks[level]) {
6118 BUG_ON(level == 0);
6119 btrfs_tree_lock(eb);
6120 btrfs_set_lock_blocking(eb);
6121 path->locks[level] = 1;
6122
6123 ret = btrfs_lookup_extent_info(trans, root,
6124 eb->start, eb->len,
6125 &wc->refs[level],
6126 &wc->flags[level]);
6127 BUG_ON(ret);
6128 BUG_ON(wc->refs[level] == 0);
6129 if (wc->refs[level] == 1) {
6130 btrfs_tree_unlock(eb);
6131 path->locks[level] = 0;
6132 return 1;
6133 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006134 }
6135 }
6136
6137 /* wc->stage == DROP_REFERENCE */
6138 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6139
6140 if (wc->refs[level] == 1) {
6141 if (level == 0) {
6142 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6143 ret = btrfs_dec_ref(trans, root, eb, 1);
6144 else
6145 ret = btrfs_dec_ref(trans, root, eb, 0);
6146 BUG_ON(ret);
6147 }
6148 /* make block locked assertion in clean_tree_block happy */
6149 if (!path->locks[level] &&
6150 btrfs_header_generation(eb) == trans->transid) {
6151 btrfs_tree_lock(eb);
6152 btrfs_set_lock_blocking(eb);
6153 path->locks[level] = 1;
6154 }
6155 clean_tree_block(trans, root, eb);
6156 }
6157
6158 if (eb == root->node) {
6159 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6160 parent = eb->start;
6161 else
6162 BUG_ON(root->root_key.objectid !=
6163 btrfs_header_owner(eb));
6164 } else {
6165 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6166 parent = path->nodes[level + 1]->start;
6167 else
6168 BUG_ON(root->root_key.objectid !=
6169 btrfs_header_owner(path->nodes[level + 1]));
6170 }
6171
Yan, Zhengf0486c62010-05-16 10:46:25 -04006172 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006173out:
6174 wc->refs[level] = 0;
6175 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006176 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006177}
6178
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006179static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6180 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006181 struct btrfs_path *path,
6182 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006183{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006184 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006185 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006186 int ret;
6187
Yan Zheng2c47e6052009-06-27 21:07:35 -04006188 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006189 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006190 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006191 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006192
Yan Zheng2c47e6052009-06-27 21:07:35 -04006193 if (level == 0)
6194 break;
6195
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006196 if (path->slots[level] >=
6197 btrfs_header_nritems(path->nodes[level]))
6198 break;
6199
Yan, Zheng94fcca92009-10-09 09:25:16 -04006200 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006201 if (ret > 0) {
6202 path->slots[level]++;
6203 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00006204 } else if (ret < 0)
6205 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006206 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006207 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006208 return 0;
6209}
6210
Chris Masond3977122009-01-05 21:25:51 -05006211static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006212 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006213 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006214 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006215{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006216 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006217 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006218
Yan Zheng2c47e6052009-06-27 21:07:35 -04006219 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6220 while (level < max_level && path->nodes[level]) {
6221 wc->level = level;
6222 if (path->slots[level] + 1 <
6223 btrfs_header_nritems(path->nodes[level])) {
6224 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006225 return 0;
6226 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006227 ret = walk_up_proc(trans, root, path, wc);
6228 if (ret > 0)
6229 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006230
Yan Zheng2c47e6052009-06-27 21:07:35 -04006231 if (path->locks[level]) {
6232 btrfs_tree_unlock(path->nodes[level]);
6233 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006234 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006235 free_extent_buffer(path->nodes[level]);
6236 path->nodes[level] = NULL;
6237 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006238 }
6239 }
6240 return 1;
6241}
6242
Chris Mason9aca1d52007-03-13 11:09:37 -04006243/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006244 * drop a subvolume tree.
6245 *
6246 * this function traverses the tree freeing any blocks that only
6247 * referenced by the tree.
6248 *
6249 * when a shared tree block is found. this function decreases its
6250 * reference count by one. if update_ref is true, this function
6251 * also make sure backrefs for the shared block and all lower level
6252 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006253 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006254int btrfs_drop_snapshot(struct btrfs_root *root,
6255 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006256{
Chris Mason5caf2a02007-04-02 11:20:42 -04006257 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006258 struct btrfs_trans_handle *trans;
6259 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006260 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006261 struct walk_control *wc;
6262 struct btrfs_key key;
6263 int err = 0;
6264 int ret;
6265 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006266
Chris Mason5caf2a02007-04-02 11:20:42 -04006267 path = btrfs_alloc_path();
6268 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006269
Yan Zheng2c47e6052009-06-27 21:07:35 -04006270 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6271 BUG_ON(!wc);
6272
Yan, Zhenga22285a2010-05-16 10:48:46 -04006273 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006274 BUG_ON(IS_ERR(trans));
6275
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006276 if (block_rsv)
6277 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006278
Chris Mason9f3a7422007-08-07 15:52:19 -04006279 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006280 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006281 path->nodes[level] = btrfs_lock_root_node(root);
6282 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006283 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006284 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006285 memset(&wc->update_progress, 0,
6286 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006287 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006288 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006289 memcpy(&wc->update_progress, &key,
6290 sizeof(wc->update_progress));
6291
Chris Mason6702ed42007-08-07 16:15:09 -04006292 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006293 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006294 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006295 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6296 path->lowest_level = 0;
6297 if (ret < 0) {
6298 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006299 goto out;
6300 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006301 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006302
Chris Mason7d9eb122008-07-08 14:19:17 -04006303 /*
6304 * unlock our path, this is safe because only this
6305 * function is allowed to delete this snapshot
6306 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006307 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006308
Yan Zheng2c47e6052009-06-27 21:07:35 -04006309 level = btrfs_header_level(root->node);
6310 while (1) {
6311 btrfs_tree_lock(path->nodes[level]);
6312 btrfs_set_lock_blocking(path->nodes[level]);
6313
6314 ret = btrfs_lookup_extent_info(trans, root,
6315 path->nodes[level]->start,
6316 path->nodes[level]->len,
6317 &wc->refs[level],
6318 &wc->flags[level]);
6319 BUG_ON(ret);
6320 BUG_ON(wc->refs[level] == 0);
6321
6322 if (level == root_item->drop_level)
6323 break;
6324
6325 btrfs_tree_unlock(path->nodes[level]);
6326 WARN_ON(wc->refs[level] != 1);
6327 level--;
6328 }
6329 }
6330
6331 wc->level = level;
6332 wc->shared_level = -1;
6333 wc->stage = DROP_REFERENCE;
6334 wc->update_ref = update_ref;
6335 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006336 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006337
6338 while (1) {
6339 ret = walk_down_tree(trans, root, path, wc);
6340 if (ret < 0) {
6341 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006342 break;
6343 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006344
6345 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6346 if (ret < 0) {
6347 err = ret;
6348 break;
6349 }
6350
6351 if (ret > 0) {
6352 BUG_ON(wc->stage != DROP_REFERENCE);
6353 break;
6354 }
6355
6356 if (wc->stage == DROP_REFERENCE) {
6357 level = wc->level;
6358 btrfs_node_key(path->nodes[level],
6359 &root_item->drop_progress,
6360 path->slots[level]);
6361 root_item->drop_level = level;
6362 }
6363
6364 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006365 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006366 ret = btrfs_update_root(trans, tree_root,
6367 &root->root_key,
6368 root_item);
6369 BUG_ON(ret);
6370
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006371 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006372 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006373 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006374 if (block_rsv)
6375 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006376 }
Chris Mason20524f02007-03-10 06:35:47 -05006377 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006378 btrfs_release_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006379 BUG_ON(err);
6380
6381 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6382 BUG_ON(ret);
6383
Yan, Zheng76dda932009-09-21 16:00:26 -04006384 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6385 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6386 NULL, NULL);
6387 BUG_ON(ret < 0);
6388 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006389 /* if we fail to delete the orphan item this time
6390 * around, it'll get picked up the next time.
6391 *
6392 * The most common failure here is just -ENOENT.
6393 */
6394 btrfs_del_orphan_item(trans, tree_root,
6395 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006396 }
6397 }
6398
6399 if (root->in_radix) {
6400 btrfs_free_fs_root(tree_root->fs_info, root);
6401 } else {
6402 free_extent_buffer(root->node);
6403 free_extent_buffer(root->commit_root);
6404 kfree(root);
6405 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006406out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006407 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006408 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006409 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006410 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006411}
Chris Mason9078a3e2007-04-26 16:46:15 -04006412
Yan Zheng2c47e6052009-06-27 21:07:35 -04006413/*
6414 * drop subtree rooted at tree block 'node'.
6415 *
6416 * NOTE: this function will unlock and release tree block 'node'
6417 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006418int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6419 struct btrfs_root *root,
6420 struct extent_buffer *node,
6421 struct extent_buffer *parent)
6422{
6423 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006424 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006425 int level;
6426 int parent_level;
6427 int ret = 0;
6428 int wret;
6429
Yan Zheng2c47e6052009-06-27 21:07:35 -04006430 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6431
Yan Zhengf82d02d2008-10-29 14:49:05 -04006432 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006433 if (!path)
6434 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006435
Yan Zheng2c47e6052009-06-27 21:07:35 -04006436 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006437 if (!wc) {
6438 btrfs_free_path(path);
6439 return -ENOMEM;
6440 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006441
Chris Masonb9447ef2009-03-09 11:45:38 -04006442 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006443 parent_level = btrfs_header_level(parent);
6444 extent_buffer_get(parent);
6445 path->nodes[parent_level] = parent;
6446 path->slots[parent_level] = btrfs_header_nritems(parent);
6447
Chris Masonb9447ef2009-03-09 11:45:38 -04006448 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006449 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006450 path->nodes[level] = node;
6451 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006452 path->locks[level] = 1;
6453
6454 wc->refs[parent_level] = 1;
6455 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6456 wc->level = level;
6457 wc->shared_level = -1;
6458 wc->stage = DROP_REFERENCE;
6459 wc->update_ref = 0;
6460 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006461 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006462
6463 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006464 wret = walk_down_tree(trans, root, path, wc);
6465 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006466 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006467 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006468 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006469
Yan Zheng2c47e6052009-06-27 21:07:35 -04006470 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006471 if (wret < 0)
6472 ret = wret;
6473 if (wret != 0)
6474 break;
6475 }
6476
Yan Zheng2c47e6052009-06-27 21:07:35 -04006477 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006478 btrfs_free_path(path);
6479 return ret;
6480}
6481
Chris Masonec44a352008-04-28 15:29:52 -04006482static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6483{
6484 u64 num_devices;
6485 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6486 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6487
Chris Masoncd02dca2010-12-13 14:56:23 -05006488 /*
6489 * we add in the count of missing devices because we want
6490 * to make sure that any RAID levels on a degraded FS
6491 * continue to be honored.
6492 */
6493 num_devices = root->fs_info->fs_devices->rw_devices +
6494 root->fs_info->fs_devices->missing_devices;
6495
Chris Masonec44a352008-04-28 15:29:52 -04006496 if (num_devices == 1) {
6497 stripped |= BTRFS_BLOCK_GROUP_DUP;
6498 stripped = flags & ~stripped;
6499
6500 /* turn raid0 into single device chunks */
6501 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6502 return stripped;
6503
6504 /* turn mirroring into duplication */
6505 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6506 BTRFS_BLOCK_GROUP_RAID10))
6507 return stripped | BTRFS_BLOCK_GROUP_DUP;
6508 return flags;
6509 } else {
6510 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006511 if (flags & stripped)
6512 return flags;
6513
6514 stripped |= BTRFS_BLOCK_GROUP_DUP;
6515 stripped = flags & ~stripped;
6516
6517 /* switch duplicated blocks with raid1 */
6518 if (flags & BTRFS_BLOCK_GROUP_DUP)
6519 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6520
6521 /* turn single device chunks into raid0 */
6522 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6523 }
6524 return flags;
6525}
6526
Yan, Zhengf0486c62010-05-16 10:46:25 -04006527static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04006528{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006529 struct btrfs_space_info *sinfo = cache->space_info;
6530 u64 num_bytes;
6531 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04006532
Yan, Zhengf0486c62010-05-16 10:46:25 -04006533 if (cache->ro)
6534 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04006535
Yan, Zhengf0486c62010-05-16 10:46:25 -04006536 spin_lock(&sinfo->lock);
6537 spin_lock(&cache->lock);
6538 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6539 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04006540
Yan, Zhengf0486c62010-05-16 10:46:25 -04006541 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
6542 sinfo->bytes_may_use + sinfo->bytes_readonly +
Chris Mason65e53412010-12-24 06:41:52 -05006543 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04006544 sinfo->bytes_readonly += num_bytes;
6545 sinfo->bytes_reserved += cache->reserved_pinned;
6546 cache->reserved_pinned = 0;
6547 cache->ro = 1;
6548 ret = 0;
6549 }
Chris Mason65e53412010-12-24 06:41:52 -05006550
Yan, Zhengf0486c62010-05-16 10:46:25 -04006551 spin_unlock(&cache->lock);
6552 spin_unlock(&sinfo->lock);
6553 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04006554}
6555
Yan, Zhengf0486c62010-05-16 10:46:25 -04006556int btrfs_set_block_group_ro(struct btrfs_root *root,
6557 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006558
6559{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006560 struct btrfs_trans_handle *trans;
6561 u64 alloc_flags;
6562 int ret;
6563
6564 BUG_ON(cache->ro);
6565
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006566 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006567 BUG_ON(IS_ERR(trans));
6568
6569 alloc_flags = update_block_group_flags(root, cache->flags);
6570 if (alloc_flags != cache->flags)
Chris Mason0e4f8f82011-04-15 16:05:44 -04006571 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6572 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006573
6574 ret = set_block_group_ro(cache);
6575 if (!ret)
6576 goto out;
6577 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006578 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6579 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006580 if (ret < 0)
6581 goto out;
6582 ret = set_block_group_ro(cache);
6583out:
6584 btrfs_end_transaction(trans, root);
6585 return ret;
6586}
6587
Chris Masonc87f08c2011-02-16 13:57:04 -05006588int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
6589 struct btrfs_root *root, u64 type)
6590{
6591 u64 alloc_flags = get_alloc_profile(root, type);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006592 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6593 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05006594}
6595
Miao Xie6d07bce2011-01-05 10:07:31 +00006596/*
6597 * helper to account the unused space of all the readonly block group in the
6598 * list. takes mirrors into account.
6599 */
6600static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
6601{
6602 struct btrfs_block_group_cache *block_group;
6603 u64 free_bytes = 0;
6604 int factor;
6605
6606 list_for_each_entry(block_group, groups_list, list) {
6607 spin_lock(&block_group->lock);
6608
6609 if (!block_group->ro) {
6610 spin_unlock(&block_group->lock);
6611 continue;
6612 }
6613
6614 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
6615 BTRFS_BLOCK_GROUP_RAID10 |
6616 BTRFS_BLOCK_GROUP_DUP))
6617 factor = 2;
6618 else
6619 factor = 1;
6620
6621 free_bytes += (block_group->key.offset -
6622 btrfs_block_group_used(&block_group->item)) *
6623 factor;
6624
6625 spin_unlock(&block_group->lock);
6626 }
6627
6628 return free_bytes;
6629}
6630
6631/*
6632 * helper to account the unused space of all the readonly block group in the
6633 * space_info. takes mirrors into account.
6634 */
6635u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
6636{
6637 int i;
6638 u64 free_bytes = 0;
6639
6640 spin_lock(&sinfo->lock);
6641
6642 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
6643 if (!list_empty(&sinfo->block_groups[i]))
6644 free_bytes += __btrfs_get_ro_block_group_free_space(
6645 &sinfo->block_groups[i]);
6646
6647 spin_unlock(&sinfo->lock);
6648
6649 return free_bytes;
6650}
6651
Yan, Zhengf0486c62010-05-16 10:46:25 -04006652int btrfs_set_block_group_rw(struct btrfs_root *root,
6653 struct btrfs_block_group_cache *cache)
6654{
6655 struct btrfs_space_info *sinfo = cache->space_info;
6656 u64 num_bytes;
6657
6658 BUG_ON(!cache->ro);
6659
6660 spin_lock(&sinfo->lock);
6661 spin_lock(&cache->lock);
6662 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6663 cache->bytes_super - btrfs_block_group_used(&cache->item);
6664 sinfo->bytes_readonly -= num_bytes;
6665 cache->ro = 0;
6666 spin_unlock(&cache->lock);
6667 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006668 return 0;
6669}
6670
Josef Bacikba1bf482009-09-11 16:11:19 -04006671/*
6672 * checks to see if its even possible to relocate this block group.
6673 *
6674 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6675 * ok to go ahead and try.
6676 */
6677int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04006678{
Zheng Yan1a40e232008-09-26 10:09:34 -04006679 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04006680 struct btrfs_space_info *space_info;
6681 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6682 struct btrfs_device *device;
6683 int full = 0;
6684 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006685
Josef Bacikba1bf482009-09-11 16:11:19 -04006686 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04006687
Josef Bacikba1bf482009-09-11 16:11:19 -04006688 /* odd, couldn't find the block group, leave it alone */
6689 if (!block_group)
6690 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006691
Josef Bacikba1bf482009-09-11 16:11:19 -04006692 /* no bytes used, we're good */
6693 if (!btrfs_block_group_used(&block_group->item))
6694 goto out;
Chris Mason323da792008-05-09 11:46:48 -04006695
Josef Bacikba1bf482009-09-11 16:11:19 -04006696 space_info = block_group->space_info;
6697 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04006698
Josef Bacikba1bf482009-09-11 16:11:19 -04006699 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04006700
Josef Bacikba1bf482009-09-11 16:11:19 -04006701 /*
6702 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04006703 * relocate it unless we're able to allocate a new chunk below.
6704 *
6705 * Otherwise, we need to make sure we have room in the space to handle
6706 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04006707 */
Chris Mason7ce618d2009-09-22 14:48:44 -04006708 if ((space_info->total_bytes != block_group->key.offset) &&
6709 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04006710 space_info->bytes_pinned + space_info->bytes_readonly +
6711 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04006712 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04006713 spin_unlock(&space_info->lock);
6714 goto out;
6715 }
6716 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006717
Josef Bacikba1bf482009-09-11 16:11:19 -04006718 /*
6719 * ok we don't have enough space, but maybe we have free space on our
6720 * devices to allocate new chunks for relocation, so loop through our
6721 * alloc devices and guess if we have enough space. However, if we
6722 * were marked as full, then we know there aren't enough chunks, and we
6723 * can just return.
6724 */
6725 ret = -1;
6726 if (full)
6727 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05006728
Josef Bacikba1bf482009-09-11 16:11:19 -04006729 mutex_lock(&root->fs_info->chunk_mutex);
6730 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
6731 u64 min_free = btrfs_block_group_used(&block_group->item);
Miao Xie7bfc8372011-01-05 10:07:26 +00006732 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04006733
Josef Bacikba1bf482009-09-11 16:11:19 -04006734 /*
6735 * check to make sure we can actually find a chunk with enough
6736 * space to fit our block group in.
6737 */
6738 if (device->total_bytes > device->bytes_used + min_free) {
6739 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00006740 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04006741 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05006742 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04006743 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05006744 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006745 }
Josef Bacikba1bf482009-09-11 16:11:19 -04006746 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05006747out:
Josef Bacikba1bf482009-09-11 16:11:19 -04006748 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006749 return ret;
6750}
6751
Christoph Hellwigb2950862008-12-02 09:54:17 -05006752static int find_first_block_group(struct btrfs_root *root,
6753 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04006754{
Chris Mason925baed2008-06-25 16:01:30 -04006755 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006756 struct btrfs_key found_key;
6757 struct extent_buffer *leaf;
6758 int slot;
6759
6760 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6761 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006762 goto out;
6763
Chris Masond3977122009-01-05 21:25:51 -05006764 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006765 slot = path->slots[0];
6766 leaf = path->nodes[0];
6767 if (slot >= btrfs_header_nritems(leaf)) {
6768 ret = btrfs_next_leaf(root, path);
6769 if (ret == 0)
6770 continue;
6771 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006772 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04006773 break;
6774 }
6775 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6776
6777 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04006778 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6779 ret = 0;
6780 goto out;
6781 }
Chris Mason0b86a832008-03-24 15:01:56 -04006782 path->slots[0]++;
6783 }
Chris Mason925baed2008-06-25 16:01:30 -04006784out:
Chris Mason0b86a832008-03-24 15:01:56 -04006785 return ret;
6786}
6787
Josef Bacik0af3d002010-06-21 14:48:16 -04006788void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
6789{
6790 struct btrfs_block_group_cache *block_group;
6791 u64 last = 0;
6792
6793 while (1) {
6794 struct inode *inode;
6795
6796 block_group = btrfs_lookup_first_block_group(info, last);
6797 while (block_group) {
6798 spin_lock(&block_group->lock);
6799 if (block_group->iref)
6800 break;
6801 spin_unlock(&block_group->lock);
6802 block_group = next_block_group(info->tree_root,
6803 block_group);
6804 }
6805 if (!block_group) {
6806 if (last == 0)
6807 break;
6808 last = 0;
6809 continue;
6810 }
6811
6812 inode = block_group->inode;
6813 block_group->iref = 0;
6814 block_group->inode = NULL;
6815 spin_unlock(&block_group->lock);
6816 iput(inode);
6817 last = block_group->key.objectid + block_group->key.offset;
6818 btrfs_put_block_group(block_group);
6819 }
6820}
6821
Zheng Yan1a40e232008-09-26 10:09:34 -04006822int btrfs_free_block_groups(struct btrfs_fs_info *info)
6823{
6824 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04006825 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04006826 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04006827 struct rb_node *n;
6828
Yan Zheng11833d62009-09-11 16:11:19 -04006829 down_write(&info->extent_commit_sem);
6830 while (!list_empty(&info->caching_block_groups)) {
6831 caching_ctl = list_entry(info->caching_block_groups.next,
6832 struct btrfs_caching_control, list);
6833 list_del(&caching_ctl->list);
6834 put_caching_control(caching_ctl);
6835 }
6836 up_write(&info->extent_commit_sem);
6837
Zheng Yan1a40e232008-09-26 10:09:34 -04006838 spin_lock(&info->block_group_cache_lock);
6839 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6840 block_group = rb_entry(n, struct btrfs_block_group_cache,
6841 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04006842 rb_erase(&block_group->cache_node,
6843 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04006844 spin_unlock(&info->block_group_cache_lock);
6845
Josef Bacik80eb2342008-10-29 14:49:05 -04006846 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006847 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006848 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006849
Josef Bacik817d52f2009-07-13 21:29:25 -04006850 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04006851 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04006852
Josef Bacik3c148742011-02-02 15:53:47 +00006853 /*
6854 * We haven't cached this block group, which means we could
6855 * possibly have excluded extents on this block group.
6856 */
6857 if (block_group->cached == BTRFS_CACHE_NO)
6858 free_excluded_extents(info->extent_root, block_group);
6859
Josef Bacik817d52f2009-07-13 21:29:25 -04006860 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00006861 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04006862
6863 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006864 }
6865 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04006866
6867 /* now that all the block groups are freed, go through and
6868 * free all the space_info structs. This is only called during
6869 * the final stages of unmount, and so we know nobody is
6870 * using them. We call synchronize_rcu() once before we start,
6871 * just to be on the safe side.
6872 */
6873 synchronize_rcu();
6874
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006875 release_global_block_rsv(info);
6876
Chris Mason4184ea72009-03-10 12:39:20 -04006877 while(!list_empty(&info->space_info)) {
6878 space_info = list_entry(info->space_info.next,
6879 struct btrfs_space_info,
6880 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006881 if (space_info->bytes_pinned > 0 ||
6882 space_info->bytes_reserved > 0) {
6883 WARN_ON(1);
6884 dump_space_info(space_info, 0, 0);
6885 }
Chris Mason4184ea72009-03-10 12:39:20 -04006886 list_del(&space_info->list);
6887 kfree(space_info);
6888 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006889 return 0;
6890}
6891
Yan, Zhengb742bb82010-05-16 10:46:24 -04006892static void __link_block_group(struct btrfs_space_info *space_info,
6893 struct btrfs_block_group_cache *cache)
6894{
6895 int index = get_block_group_index(cache);
6896
6897 down_write(&space_info->groups_sem);
6898 list_add_tail(&cache->list, &space_info->block_groups[index]);
6899 up_write(&space_info->groups_sem);
6900}
6901
Chris Mason9078a3e2007-04-26 16:46:15 -04006902int btrfs_read_block_groups(struct btrfs_root *root)
6903{
6904 struct btrfs_path *path;
6905 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04006906 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04006907 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04006908 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04006909 struct btrfs_key key;
6910 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04006911 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04006912 int need_clear = 0;
6913 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04006914
Chris Masonbe744172007-05-06 10:15:01 -04006915 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04006916 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006917 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04006918 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04006919 path = btrfs_alloc_path();
6920 if (!path)
6921 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04006922 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04006923
Josef Bacik0af3d002010-06-21 14:48:16 -04006924 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
6925 if (cache_gen != 0 &&
6926 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
6927 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04006928 if (btrfs_test_opt(root, CLEAR_CACHE))
6929 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04006930 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
6931 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04006932
Chris Masond3977122009-01-05 21:25:51 -05006933 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006934 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006935 if (ret > 0)
6936 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006937 if (ret != 0)
6938 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04006939 leaf = path->nodes[0];
6940 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04006941 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04006942 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04006943 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006944 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04006945 }
Li Zefan34d52cb6c2011-03-29 13:46:06 +08006946 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
6947 GFP_NOFS);
6948 if (!cache->free_space_ctl) {
6949 kfree(cache);
6950 ret = -ENOMEM;
6951 goto error;
6952 }
Chris Mason3e1ad542007-05-07 20:03:49 -04006953
Yan Zhengd2fb3432008-12-11 16:30:39 -05006954 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006955 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04006956 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006957 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006958 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04006959
Josef Bacik0af3d002010-06-21 14:48:16 -04006960 if (need_clear)
6961 cache->disk_cache_state = BTRFS_DC_CLEAR;
6962
Chris Mason5f39d392007-10-15 16:14:19 -04006963 read_extent_buffer(leaf, &cache->item,
6964 btrfs_item_ptr_offset(leaf, path->slots[0]),
6965 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04006966 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04006967
Chris Mason9078a3e2007-04-26 16:46:15 -04006968 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02006969 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006970 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04006971 cache->sectorsize = root->sectorsize;
6972
Li Zefan34d52cb6c2011-03-29 13:46:06 +08006973 btrfs_init_free_space_ctl(cache);
6974
Josef Bacik817d52f2009-07-13 21:29:25 -04006975 /*
Josef Bacik3c148742011-02-02 15:53:47 +00006976 * We need to exclude the super stripes now so that the space
6977 * info has super bytes accounted for, otherwise we'll think
6978 * we have more space than we actually do.
6979 */
6980 exclude_super_stripes(root, cache);
6981
6982 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04006983 * check for two cases, either we are full, and therefore
6984 * don't need to bother with the caching work since we won't
6985 * find any space, or we are empty, and we can just add all
6986 * the space in and be done with it. This saves us _alot_ of
6987 * time, particularly in the full case.
6988 */
6989 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04006990 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04006991 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04006992 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006993 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04006994 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04006995 cache->cached = BTRFS_CACHE_FINISHED;
6996 add_new_free_space(cache, root->fs_info,
6997 found_key.objectid,
6998 found_key.objectid +
6999 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007000 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007001 }
Chris Mason96b51792007-10-15 16:15:19 -04007002
Chris Mason6324fbf2008-03-24 15:01:59 -04007003 ret = update_space_info(info, cache->flags, found_key.offset,
7004 btrfs_block_group_used(&cache->item),
7005 &space_info);
7006 BUG_ON(ret);
7007 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007008 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007009 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007010 spin_unlock(&cache->space_info->lock);
7011
Yan, Zhengb742bb82010-05-16 10:46:24 -04007012 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007013
Josef Bacik0f9dd462008-09-23 13:14:11 -04007014 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7015 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007016
7017 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007018 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04007019 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007020 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007021
7022 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7023 if (!(get_alloc_profile(root, space_info->flags) &
7024 (BTRFS_BLOCK_GROUP_RAID10 |
7025 BTRFS_BLOCK_GROUP_RAID1 |
7026 BTRFS_BLOCK_GROUP_DUP)))
7027 continue;
7028 /*
7029 * avoid allocating from un-mirrored block group if there are
7030 * mirrored block groups.
7031 */
7032 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007033 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007034 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007035 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007036 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007037
7038 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007039 ret = 0;
7040error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007041 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007042 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007043}
Chris Mason6324fbf2008-03-24 15:01:59 -04007044
7045int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7046 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007047 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007048 u64 size)
7049{
7050 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007051 struct btrfs_root *extent_root;
7052 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007053
7054 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007055
Chris Mason12fcfd22009-03-24 10:24:20 -04007056 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007057
Chris Mason8f18cf12008-04-25 16:53:30 -04007058 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007059 if (!cache)
7060 return -ENOMEM;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007061 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7062 GFP_NOFS);
7063 if (!cache->free_space_ctl) {
7064 kfree(cache);
7065 return -ENOMEM;
7066 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007067
Chris Masone17cade2008-04-15 15:41:47 -04007068 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007069 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007070 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007071 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007072 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04007073
Yan Zhengd2fb3432008-12-11 16:30:39 -05007074 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007075 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007076 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007077 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007078
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007079 btrfs_init_free_space_ctl(cache);
7080
Chris Mason6324fbf2008-03-24 15:01:59 -04007081 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007082 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7083 cache->flags = type;
7084 btrfs_set_block_group_flags(&cache->item, type);
7085
Yan Zheng11833d62009-09-11 16:11:19 -04007086 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007087 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007088 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007089
Josef Bacik817d52f2009-07-13 21:29:25 -04007090 add_new_free_space(cache, root->fs_info, chunk_offset,
7091 chunk_offset + size);
7092
Yan Zheng11833d62009-09-11 16:11:19 -04007093 free_excluded_extents(root, cache);
7094
Chris Mason6324fbf2008-03-24 15:01:59 -04007095 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7096 &cache->space_info);
7097 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007098
7099 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007100 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007101 spin_unlock(&cache->space_info->lock);
7102
Yan, Zhengb742bb82010-05-16 10:46:24 -04007103 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007104
Josef Bacik0f9dd462008-09-23 13:14:11 -04007105 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7106 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007107
Chris Mason6324fbf2008-03-24 15:01:59 -04007108 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7109 sizeof(cache->item));
7110 BUG_ON(ret);
7111
Chris Masond18a2c42008-04-04 15:40:00 -04007112 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007113
Chris Mason6324fbf2008-03-24 15:01:59 -04007114 return 0;
7115}
Zheng Yan1a40e232008-09-26 10:09:34 -04007116
7117int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7118 struct btrfs_root *root, u64 group_start)
7119{
7120 struct btrfs_path *path;
7121 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007122 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04007123 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04007124 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04007125 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04007126 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04007127 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04007128
Zheng Yan1a40e232008-09-26 10:09:34 -04007129 root = root->fs_info->extent_root;
7130
7131 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7132 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007133 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007134
liubo9f7c43c2011-03-07 02:13:33 +00007135 /*
7136 * Free the reserved super bytes from this block group before
7137 * remove it.
7138 */
7139 free_excluded_extents(root, block_group);
7140
Zheng Yan1a40e232008-09-26 10:09:34 -04007141 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04007142 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7143 BTRFS_BLOCK_GROUP_RAID1 |
7144 BTRFS_BLOCK_GROUP_RAID10))
7145 factor = 2;
7146 else
7147 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007148
Chris Mason44fb5512009-06-04 15:34:51 -04007149 /* make sure this block group isn't part of an allocation cluster */
7150 cluster = &root->fs_info->data_alloc_cluster;
7151 spin_lock(&cluster->refill_lock);
7152 btrfs_return_cluster_to_free_space(block_group, cluster);
7153 spin_unlock(&cluster->refill_lock);
7154
7155 /*
7156 * make sure this block group isn't part of a metadata
7157 * allocation cluster
7158 */
7159 cluster = &root->fs_info->meta_alloc_cluster;
7160 spin_lock(&cluster->refill_lock);
7161 btrfs_return_cluster_to_free_space(block_group, cluster);
7162 spin_unlock(&cluster->refill_lock);
7163
Zheng Yan1a40e232008-09-26 10:09:34 -04007164 path = btrfs_alloc_path();
7165 BUG_ON(!path);
7166
Josef Bacik0af3d002010-06-21 14:48:16 -04007167 inode = lookup_free_space_inode(root, block_group, path);
7168 if (!IS_ERR(inode)) {
7169 btrfs_orphan_add(trans, inode);
7170 clear_nlink(inode);
7171 /* One for the block groups ref */
7172 spin_lock(&block_group->lock);
7173 if (block_group->iref) {
7174 block_group->iref = 0;
7175 block_group->inode = NULL;
7176 spin_unlock(&block_group->lock);
7177 iput(inode);
7178 } else {
7179 spin_unlock(&block_group->lock);
7180 }
7181 /* One for our lookup ref */
7182 iput(inode);
7183 }
7184
7185 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
7186 key.offset = block_group->key.objectid;
7187 key.type = 0;
7188
7189 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
7190 if (ret < 0)
7191 goto out;
7192 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02007193 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007194 if (ret == 0) {
7195 ret = btrfs_del_item(trans, tree_root, path);
7196 if (ret)
7197 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02007198 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007199 }
7200
Yan Zheng3dfdb932009-01-21 10:49:16 -05007201 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007202 rb_erase(&block_group->cache_node,
7203 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007204 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007205
Josef Bacik80eb2342008-10-29 14:49:05 -04007206 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007207 /*
7208 * we must use list_del_init so people can check to see if they
7209 * are still on the list after taking the semaphore
7210 */
7211 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007212 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007213
Josef Bacik817d52f2009-07-13 21:29:25 -04007214 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007215 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007216
7217 btrfs_remove_free_space_cache(block_group);
7218
Yan Zhengc146afa2008-11-12 14:34:12 -05007219 spin_lock(&block_group->space_info->lock);
7220 block_group->space_info->total_bytes -= block_group->key.offset;
7221 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04007222 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05007223 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007224
Josef Bacik0af3d002010-06-21 14:48:16 -04007225 memcpy(&key, &block_group->key, sizeof(key));
7226
Chris Mason283bb192009-07-24 16:30:55 -04007227 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007228
Chris Masonfa9c0d72009-04-03 09:47:43 -04007229 btrfs_put_block_group(block_group);
7230 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007231
7232 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7233 if (ret > 0)
7234 ret = -EIO;
7235 if (ret < 0)
7236 goto out;
7237
7238 ret = btrfs_del_item(trans, root, path);
7239out:
7240 btrfs_free_path(path);
7241 return ret;
7242}
liuboacce9522011-01-06 19:30:25 +08007243
liuboc59021f2011-03-07 02:13:14 +00007244int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
7245{
7246 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00007247 struct btrfs_super_block *disk_super;
7248 u64 features;
7249 u64 flags;
7250 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00007251 int ret;
7252
liubo1aba86d2011-04-08 08:44:37 +00007253 disk_super = &fs_info->super_copy;
7254 if (!btrfs_super_root(disk_super))
7255 return 1;
liuboc59021f2011-03-07 02:13:14 +00007256
liubo1aba86d2011-04-08 08:44:37 +00007257 features = btrfs_super_incompat_flags(disk_super);
7258 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
7259 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00007260
liubo1aba86d2011-04-08 08:44:37 +00007261 flags = BTRFS_BLOCK_GROUP_SYSTEM;
7262 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00007263 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00007264 goto out;
liuboc59021f2011-03-07 02:13:14 +00007265
liubo1aba86d2011-04-08 08:44:37 +00007266 if (mixed) {
7267 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
7268 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7269 } else {
7270 flags = BTRFS_BLOCK_GROUP_METADATA;
7271 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7272 if (ret)
7273 goto out;
7274
7275 flags = BTRFS_BLOCK_GROUP_DATA;
7276 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7277 }
7278out:
liuboc59021f2011-03-07 02:13:14 +00007279 return ret;
7280}
7281
liuboacce9522011-01-06 19:30:25 +08007282int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
7283{
7284 return unpin_extent_range(root, start, end);
7285}
7286
7287int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00007288 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08007289{
Li Dongyang5378e602011-03-24 10:24:27 +00007290 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08007291}
Li Dongyangf7039b12011-03-24 10:24:28 +00007292
7293int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
7294{
7295 struct btrfs_fs_info *fs_info = root->fs_info;
7296 struct btrfs_block_group_cache *cache = NULL;
7297 u64 group_trimmed;
7298 u64 start;
7299 u64 end;
7300 u64 trimmed = 0;
7301 int ret = 0;
7302
7303 cache = btrfs_lookup_block_group(fs_info, range->start);
7304
7305 while (cache) {
7306 if (cache->key.objectid >= (range->start + range->len)) {
7307 btrfs_put_block_group(cache);
7308 break;
7309 }
7310
7311 start = max(range->start, cache->key.objectid);
7312 end = min(range->start + range->len,
7313 cache->key.objectid + cache->key.offset);
7314
7315 if (end - start >= range->minlen) {
7316 if (!block_group_cache_done(cache)) {
7317 ret = cache_block_group(cache, NULL, root, 0);
7318 if (!ret)
7319 wait_block_group_cache_done(cache);
7320 }
7321 ret = btrfs_trim_block_group(cache,
7322 &group_trimmed,
7323 start,
7324 end,
7325 range->minlen);
7326
7327 trimmed += group_trimmed;
7328 if (ret) {
7329 btrfs_put_block_group(cache);
7330 break;
7331 }
7332 }
7333
7334 cache = next_block_group(fs_info->tree_root, cache);
7335 }
7336
7337 range->len = trimmed;
7338 return ret;
7339}