blob: c9173a7827b0596cbe7e5ef7f6e2a4bb94ddc33b [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 Zefan34d52cb2011-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) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400369 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400370 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400371 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400372 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400373 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400374
Yan Zheng11833d62009-09-11 16:11:19 -0400375 if (path->slots[0] < nritems) {
376 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
377 } else {
378 ret = find_next_key(path, 0, &key);
379 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400380 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400381
Josef Bacik589d8ad2011-05-11 17:30:53 -0400382 if (need_resched() ||
383 btrfs_next_leaf(extent_root, path)) {
384 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400385 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400386 up_read(&fs_info->extent_commit_sem);
387 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400388 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400389 goto again;
390 }
391 leaf = path->nodes[0];
392 nritems = btrfs_header_nritems(leaf);
393 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400394 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400395
Yan Zheng11833d62009-09-11 16:11:19 -0400396 if (key.objectid < block_group->key.objectid) {
397 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400398 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400399 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400400
Chris Masone37c9e62007-05-09 20:13:14 -0400401 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400402 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400403 break;
Yan7d7d6062007-09-14 16:15:28 -0400404
Yan Zheng11833d62009-09-11 16:11:19 -0400405 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400406 total_found += add_new_free_space(block_group,
407 fs_info, last,
408 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400409 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400410
Yan Zheng11833d62009-09-11 16:11:19 -0400411 if (total_found > (1024 * 1024 * 2)) {
412 total_found = 0;
413 wake_up(&caching_ctl->wait);
414 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400415 }
Chris Masone37c9e62007-05-09 20:13:14 -0400416 path->slots[0]++;
417 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400418 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400419
420 total_found += add_new_free_space(block_group, fs_info, last,
421 block_group->key.objectid +
422 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400423 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400424
425 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400426 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400427 block_group->cached = BTRFS_CACHE_FINISHED;
428 spin_unlock(&block_group->lock);
429
Chris Mason54aa1f42007-06-22 14:16:25 -0400430err:
Chris Masone37c9e62007-05-09 20:13:14 -0400431 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400432 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400433
Yan Zheng11833d62009-09-11 16:11:19 -0400434 free_excluded_extents(extent_root, block_group);
435
436 mutex_unlock(&caching_ctl->mutex);
437 wake_up(&caching_ctl->wait);
438
439 put_caching_control(caching_ctl);
440 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000441 btrfs_put_block_group(block_group);
442
Josef Bacik817d52f2009-07-13 21:29:25 -0400443 return 0;
444}
445
Josef Bacik9d66e232010-08-25 16:54:15 -0400446static int cache_block_group(struct btrfs_block_group_cache *cache,
447 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500448 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400449 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400450{
Yan Zheng11833d62009-09-11 16:11:19 -0400451 struct btrfs_fs_info *fs_info = cache->fs_info;
452 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400453 struct task_struct *tsk;
454 int ret = 0;
455
Yan Zheng11833d62009-09-11 16:11:19 -0400456 smp_mb();
457 if (cache->cached != BTRFS_CACHE_NO)
458 return 0;
459
Josef Bacik9d66e232010-08-25 16:54:15 -0400460 /*
461 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500462 * to have the normal tree locking. Also if we are currently trying to
463 * allocate blocks for the tree root we can't do the fast caching since
464 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400465 */
Li Dongyangf7039b12011-03-24 10:24:28 +0000466 if (trans && (!trans->transaction->in_commit) &&
Josef Bacikb8399de2010-12-08 09:15:11 -0500467 (root && root != root->fs_info->tree_root)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400468 spin_lock(&cache->lock);
469 if (cache->cached != BTRFS_CACHE_NO) {
470 spin_unlock(&cache->lock);
471 return 0;
472 }
473 cache->cached = BTRFS_CACHE_STARTED;
474 spin_unlock(&cache->lock);
475
476 ret = load_free_space_cache(fs_info, cache);
477
478 spin_lock(&cache->lock);
479 if (ret == 1) {
480 cache->cached = BTRFS_CACHE_FINISHED;
481 cache->last_byte_to_unpin = (u64)-1;
482 } else {
483 cache->cached = BTRFS_CACHE_NO;
484 }
485 spin_unlock(&cache->lock);
Josef Bacik3c148742011-02-02 15:53:47 +0000486 if (ret == 1) {
487 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400488 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000489 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400490 }
491
492 if (load_cache_only)
493 return 0;
494
Miao Xiefc0e4a32011-03-24 11:41:21 +0000495 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -0400496 BUG_ON(!caching_ctl);
497
498 INIT_LIST_HEAD(&caching_ctl->list);
499 mutex_init(&caching_ctl->mutex);
500 init_waitqueue_head(&caching_ctl->wait);
501 caching_ctl->block_group = cache;
502 caching_ctl->progress = cache->key.objectid;
503 /* one for caching kthread, one for caching block group list */
504 atomic_set(&caching_ctl->count, 2);
505
Josef Bacik817d52f2009-07-13 21:29:25 -0400506 spin_lock(&cache->lock);
507 if (cache->cached != BTRFS_CACHE_NO) {
508 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400509 kfree(caching_ctl);
510 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400511 }
Yan Zheng11833d62009-09-11 16:11:19 -0400512 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400513 cache->cached = BTRFS_CACHE_STARTED;
514 spin_unlock(&cache->lock);
515
Yan Zheng11833d62009-09-11 16:11:19 -0400516 down_write(&fs_info->extent_commit_sem);
517 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
518 up_write(&fs_info->extent_commit_sem);
519
520 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000521 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400522
Josef Bacik817d52f2009-07-13 21:29:25 -0400523 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
524 cache->key.objectid);
525 if (IS_ERR(tsk)) {
526 ret = PTR_ERR(tsk);
527 printk(KERN_ERR "error running thread %d\n", ret);
528 BUG();
529 }
530
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400531 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400532}
533
Josef Bacik0f9dd462008-09-23 13:14:11 -0400534/*
535 * return the block group that starts at or after bytenr
536 */
Chris Masond3977122009-01-05 21:25:51 -0500537static struct btrfs_block_group_cache *
538btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400539{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400540 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400541
Josef Bacik0f9dd462008-09-23 13:14:11 -0400542 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400543
Josef Bacik0f9dd462008-09-23 13:14:11 -0400544 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400545}
546
Josef Bacik0f9dd462008-09-23 13:14:11 -0400547/*
Sankar P9f556842009-05-14 13:52:22 -0400548 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400549 */
Chris Masond3977122009-01-05 21:25:51 -0500550struct btrfs_block_group_cache *btrfs_lookup_block_group(
551 struct btrfs_fs_info *info,
552 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400553{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400554 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400555
Josef Bacik0f9dd462008-09-23 13:14:11 -0400556 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400557
Josef Bacik0f9dd462008-09-23 13:14:11 -0400558 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400559}
Chris Mason0b86a832008-03-24 15:01:56 -0400560
Josef Bacik0f9dd462008-09-23 13:14:11 -0400561static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
562 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400563{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400564 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400565 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400566
Yan, Zhengb742bb82010-05-16 10:46:24 -0400567 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
568 BTRFS_BLOCK_GROUP_METADATA;
569
Chris Mason4184ea72009-03-10 12:39:20 -0400570 rcu_read_lock();
571 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400572 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400573 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400574 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400575 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400576 }
Chris Mason4184ea72009-03-10 12:39:20 -0400577 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400578 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400579}
580
Chris Mason4184ea72009-03-10 12:39:20 -0400581/*
582 * after adding space to the filesystem, we need to clear the full flags
583 * on all the space infos.
584 */
585void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
586{
587 struct list_head *head = &info->space_info;
588 struct btrfs_space_info *found;
589
590 rcu_read_lock();
591 list_for_each_entry_rcu(found, head, list)
592 found->full = 0;
593 rcu_read_unlock();
594}
595
Josef Bacik80eb2342008-10-29 14:49:05 -0400596static u64 div_factor(u64 num, int factor)
597{
598 if (factor == 10)
599 return num;
600 num *= factor;
601 do_div(num, 10);
602 return num;
603}
604
Chris Masone5bc2452010-10-26 13:37:56 -0400605static u64 div_factor_fine(u64 num, int factor)
606{
607 if (factor == 100)
608 return num;
609 num *= factor;
610 do_div(num, 100);
611 return num;
612}
613
Yan Zhengd2fb3432008-12-11 16:30:39 -0500614u64 btrfs_find_block_group(struct btrfs_root *root,
615 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400616{
Chris Mason96b51792007-10-15 16:15:19 -0400617 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400618 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500619 u64 last = max(search_hint, search_start);
620 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400621 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500622 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400623 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400624again:
Zheng Yane8569812008-09-26 10:05:48 -0400625 while (1) {
626 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400627 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400628 break;
Chris Mason96b51792007-10-15 16:15:19 -0400629
Chris Masonc286ac42008-07-22 23:06:41 -0400630 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400631 last = cache->key.objectid + cache->key.offset;
632 used = btrfs_block_group_used(&cache->item);
633
Yan Zhengd2fb3432008-12-11 16:30:39 -0500634 if ((full_search || !cache->ro) &&
635 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400636 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500637 div_factor(cache->key.offset, factor)) {
638 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400639 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400640 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400641 goto found;
642 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400643 }
Chris Masonc286ac42008-07-22 23:06:41 -0400644 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400645 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400646 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400647 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400648 if (!wrapped) {
649 last = search_start;
650 wrapped = 1;
651 goto again;
652 }
653 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400654 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400655 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400656 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400657 goto again;
658 }
Chris Masonbe744172007-05-06 10:15:01 -0400659found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500660 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400661}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400662
Chris Masone02119d2008-09-05 16:13:11 -0400663/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400664int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400665{
666 int ret;
667 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400668 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400669
Zheng Yan31840ae2008-09-23 13:14:14 -0400670 path = btrfs_alloc_path();
671 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400672 key.objectid = start;
673 key.offset = len;
674 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
675 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
676 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400677 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500678 return ret;
679}
680
Chris Masond8d5f3e2007-12-11 12:42:00 -0500681/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400682 * helper function to lookup reference count and flags of extent.
683 *
684 * the head node for delayed ref is used to store the sum of all the
685 * reference count modifications queued up in the rbtree. the head
686 * node may also store the extent flags to set. This way you can check
687 * to see what the reference count and extent flags would be if all of
688 * the delayed refs are not processed.
689 */
690int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
691 struct btrfs_root *root, u64 bytenr,
692 u64 num_bytes, u64 *refs, u64 *flags)
693{
694 struct btrfs_delayed_ref_head *head;
695 struct btrfs_delayed_ref_root *delayed_refs;
696 struct btrfs_path *path;
697 struct btrfs_extent_item *ei;
698 struct extent_buffer *leaf;
699 struct btrfs_key key;
700 u32 item_size;
701 u64 num_refs;
702 u64 extent_flags;
703 int ret;
704
705 path = btrfs_alloc_path();
706 if (!path)
707 return -ENOMEM;
708
709 key.objectid = bytenr;
710 key.type = BTRFS_EXTENT_ITEM_KEY;
711 key.offset = num_bytes;
712 if (!trans) {
713 path->skip_locking = 1;
714 path->search_commit_root = 1;
715 }
716again:
717 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
718 &key, path, 0, 0);
719 if (ret < 0)
720 goto out_free;
721
722 if (ret == 0) {
723 leaf = path->nodes[0];
724 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
725 if (item_size >= sizeof(*ei)) {
726 ei = btrfs_item_ptr(leaf, path->slots[0],
727 struct btrfs_extent_item);
728 num_refs = btrfs_extent_refs(leaf, ei);
729 extent_flags = btrfs_extent_flags(leaf, ei);
730 } else {
731#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
732 struct btrfs_extent_item_v0 *ei0;
733 BUG_ON(item_size != sizeof(*ei0));
734 ei0 = btrfs_item_ptr(leaf, path->slots[0],
735 struct btrfs_extent_item_v0);
736 num_refs = btrfs_extent_refs_v0(leaf, ei0);
737 /* FIXME: this isn't correct for data */
738 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
739#else
740 BUG();
741#endif
742 }
743 BUG_ON(num_refs == 0);
744 } else {
745 num_refs = 0;
746 extent_flags = 0;
747 ret = 0;
748 }
749
750 if (!trans)
751 goto out;
752
753 delayed_refs = &trans->transaction->delayed_refs;
754 spin_lock(&delayed_refs->lock);
755 head = btrfs_find_delayed_ref_head(trans, bytenr);
756 if (head) {
757 if (!mutex_trylock(&head->mutex)) {
758 atomic_inc(&head->node.refs);
759 spin_unlock(&delayed_refs->lock);
760
David Sterbab3b4aa72011-04-21 01:20:15 +0200761 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400762
David Sterba8cc33e52011-05-02 15:29:25 +0200763 /*
764 * Mutex was contended, block until it's released and try
765 * again
766 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400767 mutex_lock(&head->mutex);
768 mutex_unlock(&head->mutex);
769 btrfs_put_delayed_ref(&head->node);
770 goto again;
771 }
772 if (head->extent_op && head->extent_op->update_flags)
773 extent_flags |= head->extent_op->flags_to_set;
774 else
775 BUG_ON(num_refs == 0);
776
777 num_refs += head->node.ref_mod;
778 mutex_unlock(&head->mutex);
779 }
780 spin_unlock(&delayed_refs->lock);
781out:
782 WARN_ON(num_refs == 0);
783 if (refs)
784 *refs = num_refs;
785 if (flags)
786 *flags = extent_flags;
787out_free:
788 btrfs_free_path(path);
789 return ret;
790}
791
792/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500793 * Back reference rules. Back refs have three main goals:
794 *
795 * 1) differentiate between all holders of references to an extent so that
796 * when a reference is dropped we can make sure it was a valid reference
797 * before freeing the extent.
798 *
799 * 2) Provide enough information to quickly find the holders of an extent
800 * if we notice a given block is corrupted or bad.
801 *
802 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
803 * maintenance. This is actually the same as #2, but with a slightly
804 * different use case.
805 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400806 * There are two kinds of back refs. The implicit back refs is optimized
807 * for pointers in non-shared tree blocks. For a given pointer in a block,
808 * back refs of this kind provide information about the block's owner tree
809 * and the pointer's key. These information allow us to find the block by
810 * b-tree searching. The full back refs is for pointers in tree blocks not
811 * referenced by their owner trees. The location of tree block is recorded
812 * in the back refs. Actually the full back refs is generic, and can be
813 * used in all cases the implicit back refs is used. The major shortcoming
814 * of the full back refs is its overhead. Every time a tree block gets
815 * COWed, we have to update back refs entry for all pointers in it.
816 *
817 * For a newly allocated tree block, we use implicit back refs for
818 * pointers in it. This means most tree related operations only involve
819 * implicit back refs. For a tree block created in old transaction, the
820 * only way to drop a reference to it is COW it. So we can detect the
821 * event that tree block loses its owner tree's reference and do the
822 * back refs conversion.
823 *
824 * When a tree block is COW'd through a tree, there are four cases:
825 *
826 * The reference count of the block is one and the tree is the block's
827 * owner tree. Nothing to do in this case.
828 *
829 * The reference count of the block is one and the tree is not the
830 * block's owner tree. In this case, full back refs is used for pointers
831 * in the block. Remove these full back refs, add implicit back refs for
832 * every pointers in the new block.
833 *
834 * The reference count of the block is greater than one and the tree is
835 * the block's owner tree. In this case, implicit back refs is used for
836 * pointers in the block. Add full back refs for every pointers in the
837 * block, increase lower level extents' reference counts. The original
838 * implicit back refs are entailed to the new block.
839 *
840 * The reference count of the block is greater than one and the tree is
841 * not the block's owner tree. Add implicit back refs for every pointer in
842 * the new block, increase lower level extents' reference count.
843 *
844 * Back Reference Key composing:
845 *
846 * The key objectid corresponds to the first byte in the extent,
847 * The key type is used to differentiate between types of back refs.
848 * There are different meanings of the key offset for different types
849 * of back refs.
850 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500851 * File extents can be referenced by:
852 *
853 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400854 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500855 * - different offsets inside a file (bookend extents in file.c)
856 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500858 *
859 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500860 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861 * - original offset in the file
862 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400863 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400864 * The key offset for the implicit back refs is hash of the first
865 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500866 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400867 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500868 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400869 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500870 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871 * The key offset for the implicit back refs is the first byte of
872 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500873 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400874 * When a file extent is allocated, The implicit back refs is used.
875 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500876 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400877 * (root_key.objectid, inode objectid, offset in file, 1)
878 *
879 * When a file extent is removed file truncation, we find the
880 * corresponding implicit back refs and check the following fields:
881 *
882 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500883 *
884 * Btree extents can be referenced by:
885 *
886 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500887 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400888 * Both the implicit back refs and the full back refs for tree blocks
889 * only consist of key. The key offset for the implicit back refs is
890 * objectid of block's owner tree. The key offset for the full back refs
891 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500892 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400893 * When implicit back refs is used, information about the lowest key and
894 * level of the tree block are required. These information are stored in
895 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500896 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400897
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400898#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
899static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
900 struct btrfs_root *root,
901 struct btrfs_path *path,
902 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500903{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400904 struct btrfs_extent_item *item;
905 struct btrfs_extent_item_v0 *ei0;
906 struct btrfs_extent_ref_v0 *ref0;
907 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400908 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400909 struct btrfs_key key;
910 struct btrfs_key found_key;
911 u32 new_size = sizeof(*item);
912 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500913 int ret;
914
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400915 leaf = path->nodes[0];
916 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500917
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
919 ei0 = btrfs_item_ptr(leaf, path->slots[0],
920 struct btrfs_extent_item_v0);
921 refs = btrfs_extent_refs_v0(leaf, ei0);
922
923 if (owner == (u64)-1) {
924 while (1) {
925 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
926 ret = btrfs_next_leaf(root, path);
927 if (ret < 0)
928 return ret;
929 BUG_ON(ret > 0);
930 leaf = path->nodes[0];
931 }
932 btrfs_item_key_to_cpu(leaf, &found_key,
933 path->slots[0]);
934 BUG_ON(key.objectid != found_key.objectid);
935 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
936 path->slots[0]++;
937 continue;
938 }
939 ref0 = btrfs_item_ptr(leaf, path->slots[0],
940 struct btrfs_extent_ref_v0);
941 owner = btrfs_ref_objectid_v0(leaf, ref0);
942 break;
943 }
944 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200945 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946
947 if (owner < BTRFS_FIRST_FREE_OBJECTID)
948 new_size += sizeof(*bi);
949
950 new_size -= sizeof(*ei0);
951 ret = btrfs_search_slot(trans, root, &key, path,
952 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400953 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400954 return ret;
955 BUG_ON(ret);
956
957 ret = btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400958
959 leaf = path->nodes[0];
960 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
961 btrfs_set_extent_refs(leaf, item, refs);
962 /* FIXME: get real generation */
963 btrfs_set_extent_generation(leaf, item, 0);
964 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
965 btrfs_set_extent_flags(leaf, item,
966 BTRFS_EXTENT_FLAG_TREE_BLOCK |
967 BTRFS_BLOCK_FLAG_FULL_BACKREF);
968 bi = (struct btrfs_tree_block_info *)(item + 1);
969 /* FIXME: get first key of the block */
970 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
971 btrfs_set_tree_block_level(leaf, bi, (int)owner);
972 } else {
973 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
974 }
975 btrfs_mark_buffer_dirty(leaf);
976 return 0;
977}
978#endif
979
980static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
981{
982 u32 high_crc = ~(u32)0;
983 u32 low_crc = ~(u32)0;
984 __le64 lenum;
985
986 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100987 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100989 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400990 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100991 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400992
993 return ((u64)high_crc << 31) ^ (u64)low_crc;
994}
995
996static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
997 struct btrfs_extent_data_ref *ref)
998{
999 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1000 btrfs_extent_data_ref_objectid(leaf, ref),
1001 btrfs_extent_data_ref_offset(leaf, ref));
1002}
1003
1004static int match_extent_data_ref(struct extent_buffer *leaf,
1005 struct btrfs_extent_data_ref *ref,
1006 u64 root_objectid, u64 owner, u64 offset)
1007{
1008 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1009 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1010 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1011 return 0;
1012 return 1;
1013}
1014
1015static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1016 struct btrfs_root *root,
1017 struct btrfs_path *path,
1018 u64 bytenr, u64 parent,
1019 u64 root_objectid,
1020 u64 owner, u64 offset)
1021{
1022 struct btrfs_key key;
1023 struct btrfs_extent_data_ref *ref;
1024 struct extent_buffer *leaf;
1025 u32 nritems;
1026 int ret;
1027 int recow;
1028 int err = -ENOENT;
1029
1030 key.objectid = bytenr;
1031 if (parent) {
1032 key.type = BTRFS_SHARED_DATA_REF_KEY;
1033 key.offset = parent;
1034 } else {
1035 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1036 key.offset = hash_extent_data_ref(root_objectid,
1037 owner, offset);
1038 }
1039again:
1040 recow = 0;
1041 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1042 if (ret < 0) {
1043 err = ret;
1044 goto fail;
1045 }
1046
1047 if (parent) {
1048 if (!ret)
1049 return 0;
1050#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1051 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001052 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001053 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1054 if (ret < 0) {
1055 err = ret;
1056 goto fail;
1057 }
1058 if (!ret)
1059 return 0;
1060#endif
1061 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001062 }
1063
1064 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001065 nritems = btrfs_header_nritems(leaf);
1066 while (1) {
1067 if (path->slots[0] >= nritems) {
1068 ret = btrfs_next_leaf(root, path);
1069 if (ret < 0)
1070 err = ret;
1071 if (ret)
1072 goto fail;
1073
1074 leaf = path->nodes[0];
1075 nritems = btrfs_header_nritems(leaf);
1076 recow = 1;
1077 }
1078
1079 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1080 if (key.objectid != bytenr ||
1081 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1082 goto fail;
1083
1084 ref = btrfs_item_ptr(leaf, path->slots[0],
1085 struct btrfs_extent_data_ref);
1086
1087 if (match_extent_data_ref(leaf, ref, root_objectid,
1088 owner, offset)) {
1089 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001090 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001091 goto again;
1092 }
1093 err = 0;
1094 break;
1095 }
1096 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001097 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001098fail:
1099 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001100}
1101
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001102static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1103 struct btrfs_root *root,
1104 struct btrfs_path *path,
1105 u64 bytenr, u64 parent,
1106 u64 root_objectid, u64 owner,
1107 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001108{
1109 struct btrfs_key key;
1110 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001111 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001112 u32 num_refs;
1113 int ret;
1114
1115 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001116 if (parent) {
1117 key.type = BTRFS_SHARED_DATA_REF_KEY;
1118 key.offset = parent;
1119 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001120 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001121 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1122 key.offset = hash_extent_data_ref(root_objectid,
1123 owner, offset);
1124 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001125 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001126
1127 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1128 if (ret && ret != -EEXIST)
1129 goto fail;
1130
1131 leaf = path->nodes[0];
1132 if (parent) {
1133 struct btrfs_shared_data_ref *ref;
1134 ref = btrfs_item_ptr(leaf, path->slots[0],
1135 struct btrfs_shared_data_ref);
1136 if (ret == 0) {
1137 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1138 } else {
1139 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1140 num_refs += refs_to_add;
1141 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1142 }
1143 } else {
1144 struct btrfs_extent_data_ref *ref;
1145 while (ret == -EEXIST) {
1146 ref = btrfs_item_ptr(leaf, path->slots[0],
1147 struct btrfs_extent_data_ref);
1148 if (match_extent_data_ref(leaf, ref, root_objectid,
1149 owner, offset))
1150 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001151 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001152 key.offset++;
1153 ret = btrfs_insert_empty_item(trans, root, path, &key,
1154 size);
1155 if (ret && ret != -EEXIST)
1156 goto fail;
1157
1158 leaf = path->nodes[0];
1159 }
1160 ref = btrfs_item_ptr(leaf, path->slots[0],
1161 struct btrfs_extent_data_ref);
1162 if (ret == 0) {
1163 btrfs_set_extent_data_ref_root(leaf, ref,
1164 root_objectid);
1165 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1166 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1167 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1168 } else {
1169 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1170 num_refs += refs_to_add;
1171 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1172 }
1173 }
1174 btrfs_mark_buffer_dirty(leaf);
1175 ret = 0;
1176fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001177 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001178 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001179}
1180
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001181static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1182 struct btrfs_root *root,
1183 struct btrfs_path *path,
1184 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001185{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001186 struct btrfs_key key;
1187 struct btrfs_extent_data_ref *ref1 = NULL;
1188 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001189 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001190 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001191 int ret = 0;
1192
1193 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001194 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1195
1196 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1197 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1198 struct btrfs_extent_data_ref);
1199 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1200 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1201 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1202 struct btrfs_shared_data_ref);
1203 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1204#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1205 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1206 struct btrfs_extent_ref_v0 *ref0;
1207 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1208 struct btrfs_extent_ref_v0);
1209 num_refs = btrfs_ref_count_v0(leaf, ref0);
1210#endif
1211 } else {
1212 BUG();
1213 }
1214
Chris Mason56bec292009-03-13 10:10:06 -04001215 BUG_ON(num_refs < refs_to_drop);
1216 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001217
Zheng Yan31840ae2008-09-23 13:14:14 -04001218 if (num_refs == 0) {
1219 ret = btrfs_del_item(trans, root, path);
1220 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001221 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1222 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1223 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1224 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1225#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1226 else {
1227 struct btrfs_extent_ref_v0 *ref0;
1228 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1229 struct btrfs_extent_ref_v0);
1230 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1231 }
1232#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001233 btrfs_mark_buffer_dirty(leaf);
1234 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001235 return ret;
1236}
1237
1238static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1239 struct btrfs_path *path,
1240 struct btrfs_extent_inline_ref *iref)
1241{
1242 struct btrfs_key key;
1243 struct extent_buffer *leaf;
1244 struct btrfs_extent_data_ref *ref1;
1245 struct btrfs_shared_data_ref *ref2;
1246 u32 num_refs = 0;
1247
1248 leaf = path->nodes[0];
1249 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1250 if (iref) {
1251 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1252 BTRFS_EXTENT_DATA_REF_KEY) {
1253 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1254 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1255 } else {
1256 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1257 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1258 }
1259 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1260 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1261 struct btrfs_extent_data_ref);
1262 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1263 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1264 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1265 struct btrfs_shared_data_ref);
1266 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1267#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1268 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1269 struct btrfs_extent_ref_v0 *ref0;
1270 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1271 struct btrfs_extent_ref_v0);
1272 num_refs = btrfs_ref_count_v0(leaf, ref0);
1273#endif
1274 } else {
1275 WARN_ON(1);
1276 }
1277 return num_refs;
1278}
1279
1280static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1281 struct btrfs_root *root,
1282 struct btrfs_path *path,
1283 u64 bytenr, u64 parent,
1284 u64 root_objectid)
1285{
1286 struct btrfs_key key;
1287 int ret;
1288
1289 key.objectid = bytenr;
1290 if (parent) {
1291 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1292 key.offset = parent;
1293 } else {
1294 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1295 key.offset = root_objectid;
1296 }
1297
1298 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1299 if (ret > 0)
1300 ret = -ENOENT;
1301#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1302 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001303 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001304 key.type = BTRFS_EXTENT_REF_V0_KEY;
1305 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1306 if (ret > 0)
1307 ret = -ENOENT;
1308 }
1309#endif
1310 return ret;
1311}
1312
1313static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1314 struct btrfs_root *root,
1315 struct btrfs_path *path,
1316 u64 bytenr, u64 parent,
1317 u64 root_objectid)
1318{
1319 struct btrfs_key key;
1320 int ret;
1321
1322 key.objectid = bytenr;
1323 if (parent) {
1324 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1325 key.offset = parent;
1326 } else {
1327 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1328 key.offset = root_objectid;
1329 }
1330
1331 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001332 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001333 return ret;
1334}
1335
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001336static inline int extent_ref_type(u64 parent, u64 owner)
1337{
1338 int type;
1339 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1340 if (parent > 0)
1341 type = BTRFS_SHARED_BLOCK_REF_KEY;
1342 else
1343 type = BTRFS_TREE_BLOCK_REF_KEY;
1344 } else {
1345 if (parent > 0)
1346 type = BTRFS_SHARED_DATA_REF_KEY;
1347 else
1348 type = BTRFS_EXTENT_DATA_REF_KEY;
1349 }
1350 return type;
1351}
1352
Yan Zheng2c47e6052009-06-27 21:07:35 -04001353static int find_next_key(struct btrfs_path *path, int level,
1354 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001355
1356{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001357 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001358 if (!path->nodes[level])
1359 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001360 if (path->slots[level] + 1 >=
1361 btrfs_header_nritems(path->nodes[level]))
1362 continue;
1363 if (level == 0)
1364 btrfs_item_key_to_cpu(path->nodes[level], key,
1365 path->slots[level] + 1);
1366 else
1367 btrfs_node_key_to_cpu(path->nodes[level], key,
1368 path->slots[level] + 1);
1369 return 0;
1370 }
1371 return 1;
1372}
1373
1374/*
1375 * look for inline back ref. if back ref is found, *ref_ret is set
1376 * to the address of inline back ref, and 0 is returned.
1377 *
1378 * if back ref isn't found, *ref_ret is set to the address where it
1379 * should be inserted, and -ENOENT is returned.
1380 *
1381 * if insert is true and there are too many inline back refs, the path
1382 * points to the extent item, and -EAGAIN is returned.
1383 *
1384 * NOTE: inline back refs are ordered in the same way that back ref
1385 * items in the tree are ordered.
1386 */
1387static noinline_for_stack
1388int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1389 struct btrfs_root *root,
1390 struct btrfs_path *path,
1391 struct btrfs_extent_inline_ref **ref_ret,
1392 u64 bytenr, u64 num_bytes,
1393 u64 parent, u64 root_objectid,
1394 u64 owner, u64 offset, int insert)
1395{
1396 struct btrfs_key key;
1397 struct extent_buffer *leaf;
1398 struct btrfs_extent_item *ei;
1399 struct btrfs_extent_inline_ref *iref;
1400 u64 flags;
1401 u64 item_size;
1402 unsigned long ptr;
1403 unsigned long end;
1404 int extra_size;
1405 int type;
1406 int want;
1407 int ret;
1408 int err = 0;
1409
1410 key.objectid = bytenr;
1411 key.type = BTRFS_EXTENT_ITEM_KEY;
1412 key.offset = num_bytes;
1413
1414 want = extent_ref_type(parent, owner);
1415 if (insert) {
1416 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001417 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001418 } else
1419 extra_size = -1;
1420 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1421 if (ret < 0) {
1422 err = ret;
1423 goto out;
1424 }
1425 BUG_ON(ret);
1426
1427 leaf = path->nodes[0];
1428 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1429#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1430 if (item_size < sizeof(*ei)) {
1431 if (!insert) {
1432 err = -ENOENT;
1433 goto out;
1434 }
1435 ret = convert_extent_item_v0(trans, root, path, owner,
1436 extra_size);
1437 if (ret < 0) {
1438 err = ret;
1439 goto out;
1440 }
1441 leaf = path->nodes[0];
1442 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1443 }
1444#endif
1445 BUG_ON(item_size < sizeof(*ei));
1446
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001447 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1448 flags = btrfs_extent_flags(leaf, ei);
1449
1450 ptr = (unsigned long)(ei + 1);
1451 end = (unsigned long)ei + item_size;
1452
1453 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1454 ptr += sizeof(struct btrfs_tree_block_info);
1455 BUG_ON(ptr > end);
1456 } else {
1457 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1458 }
1459
1460 err = -ENOENT;
1461 while (1) {
1462 if (ptr >= end) {
1463 WARN_ON(ptr > end);
1464 break;
1465 }
1466 iref = (struct btrfs_extent_inline_ref *)ptr;
1467 type = btrfs_extent_inline_ref_type(leaf, iref);
1468 if (want < type)
1469 break;
1470 if (want > type) {
1471 ptr += btrfs_extent_inline_ref_size(type);
1472 continue;
1473 }
1474
1475 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1476 struct btrfs_extent_data_ref *dref;
1477 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1478 if (match_extent_data_ref(leaf, dref, root_objectid,
1479 owner, offset)) {
1480 err = 0;
1481 break;
1482 }
1483 if (hash_extent_data_ref_item(leaf, dref) <
1484 hash_extent_data_ref(root_objectid, owner, offset))
1485 break;
1486 } else {
1487 u64 ref_offset;
1488 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1489 if (parent > 0) {
1490 if (parent == ref_offset) {
1491 err = 0;
1492 break;
1493 }
1494 if (ref_offset < parent)
1495 break;
1496 } else {
1497 if (root_objectid == ref_offset) {
1498 err = 0;
1499 break;
1500 }
1501 if (ref_offset < root_objectid)
1502 break;
1503 }
1504 }
1505 ptr += btrfs_extent_inline_ref_size(type);
1506 }
1507 if (err == -ENOENT && insert) {
1508 if (item_size + extra_size >=
1509 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1510 err = -EAGAIN;
1511 goto out;
1512 }
1513 /*
1514 * To add new inline back ref, we have to make sure
1515 * there is no corresponding back ref item.
1516 * For simplicity, we just do not add new inline back
1517 * ref if there is any kind of item for this block
1518 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001519 if (find_next_key(path, 0, &key) == 0 &&
1520 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001521 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001522 err = -EAGAIN;
1523 goto out;
1524 }
1525 }
1526 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1527out:
Yan Zheng85d41982009-06-11 08:51:10 -04001528 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001529 path->keep_locks = 0;
1530 btrfs_unlock_up_safe(path, 1);
1531 }
1532 return err;
1533}
1534
1535/*
1536 * helper to add new inline back ref
1537 */
1538static noinline_for_stack
1539int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1540 struct btrfs_root *root,
1541 struct btrfs_path *path,
1542 struct btrfs_extent_inline_ref *iref,
1543 u64 parent, u64 root_objectid,
1544 u64 owner, u64 offset, int refs_to_add,
1545 struct btrfs_delayed_extent_op *extent_op)
1546{
1547 struct extent_buffer *leaf;
1548 struct btrfs_extent_item *ei;
1549 unsigned long ptr;
1550 unsigned long end;
1551 unsigned long item_offset;
1552 u64 refs;
1553 int size;
1554 int type;
1555 int ret;
1556
1557 leaf = path->nodes[0];
1558 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1559 item_offset = (unsigned long)iref - (unsigned long)ei;
1560
1561 type = extent_ref_type(parent, owner);
1562 size = btrfs_extent_inline_ref_size(type);
1563
1564 ret = btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001565
1566 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1567 refs = btrfs_extent_refs(leaf, ei);
1568 refs += refs_to_add;
1569 btrfs_set_extent_refs(leaf, ei, refs);
1570 if (extent_op)
1571 __run_delayed_extent_op(extent_op, leaf, ei);
1572
1573 ptr = (unsigned long)ei + item_offset;
1574 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1575 if (ptr < end - size)
1576 memmove_extent_buffer(leaf, ptr + size, ptr,
1577 end - size - ptr);
1578
1579 iref = (struct btrfs_extent_inline_ref *)ptr;
1580 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1581 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1582 struct btrfs_extent_data_ref *dref;
1583 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1584 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1585 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1586 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1587 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1588 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1589 struct btrfs_shared_data_ref *sref;
1590 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1591 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1592 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1593 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1594 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1595 } else {
1596 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1597 }
1598 btrfs_mark_buffer_dirty(leaf);
1599 return 0;
1600}
1601
1602static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1603 struct btrfs_root *root,
1604 struct btrfs_path *path,
1605 struct btrfs_extent_inline_ref **ref_ret,
1606 u64 bytenr, u64 num_bytes, u64 parent,
1607 u64 root_objectid, u64 owner, u64 offset)
1608{
1609 int ret;
1610
1611 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1612 bytenr, num_bytes, parent,
1613 root_objectid, owner, offset, 0);
1614 if (ret != -ENOENT)
1615 return ret;
1616
David Sterbab3b4aa72011-04-21 01:20:15 +02001617 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001618 *ref_ret = NULL;
1619
1620 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1621 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1622 root_objectid);
1623 } else {
1624 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1625 root_objectid, owner, offset);
1626 }
1627 return ret;
1628}
1629
1630/*
1631 * helper to update/remove inline back ref
1632 */
1633static noinline_for_stack
1634int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1635 struct btrfs_root *root,
1636 struct btrfs_path *path,
1637 struct btrfs_extent_inline_ref *iref,
1638 int refs_to_mod,
1639 struct btrfs_delayed_extent_op *extent_op)
1640{
1641 struct extent_buffer *leaf;
1642 struct btrfs_extent_item *ei;
1643 struct btrfs_extent_data_ref *dref = NULL;
1644 struct btrfs_shared_data_ref *sref = NULL;
1645 unsigned long ptr;
1646 unsigned long end;
1647 u32 item_size;
1648 int size;
1649 int type;
1650 int ret;
1651 u64 refs;
1652
1653 leaf = path->nodes[0];
1654 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1655 refs = btrfs_extent_refs(leaf, ei);
1656 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1657 refs += refs_to_mod;
1658 btrfs_set_extent_refs(leaf, ei, refs);
1659 if (extent_op)
1660 __run_delayed_extent_op(extent_op, leaf, ei);
1661
1662 type = btrfs_extent_inline_ref_type(leaf, iref);
1663
1664 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1665 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1666 refs = btrfs_extent_data_ref_count(leaf, dref);
1667 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1668 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1669 refs = btrfs_shared_data_ref_count(leaf, sref);
1670 } else {
1671 refs = 1;
1672 BUG_ON(refs_to_mod != -1);
1673 }
1674
1675 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1676 refs += refs_to_mod;
1677
1678 if (refs > 0) {
1679 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1680 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1681 else
1682 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1683 } else {
1684 size = btrfs_extent_inline_ref_size(type);
1685 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1686 ptr = (unsigned long)iref;
1687 end = (unsigned long)ei + item_size;
1688 if (ptr + size < end)
1689 memmove_extent_buffer(leaf, ptr, ptr + size,
1690 end - ptr - size);
1691 item_size -= size;
1692 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001693 }
1694 btrfs_mark_buffer_dirty(leaf);
1695 return 0;
1696}
1697
1698static noinline_for_stack
1699int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
1701 struct btrfs_path *path,
1702 u64 bytenr, u64 num_bytes, u64 parent,
1703 u64 root_objectid, u64 owner,
1704 u64 offset, int refs_to_add,
1705 struct btrfs_delayed_extent_op *extent_op)
1706{
1707 struct btrfs_extent_inline_ref *iref;
1708 int ret;
1709
1710 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1711 bytenr, num_bytes, parent,
1712 root_objectid, owner, offset, 1);
1713 if (ret == 0) {
1714 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1715 ret = update_inline_extent_backref(trans, root, path, iref,
1716 refs_to_add, extent_op);
1717 } else if (ret == -ENOENT) {
1718 ret = setup_inline_extent_backref(trans, root, path, iref,
1719 parent, root_objectid,
1720 owner, offset, refs_to_add,
1721 extent_op);
1722 }
1723 return ret;
1724}
1725
1726static int insert_extent_backref(struct btrfs_trans_handle *trans,
1727 struct btrfs_root *root,
1728 struct btrfs_path *path,
1729 u64 bytenr, u64 parent, u64 root_objectid,
1730 u64 owner, u64 offset, int refs_to_add)
1731{
1732 int ret;
1733 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1734 BUG_ON(refs_to_add != 1);
1735 ret = insert_tree_block_ref(trans, root, path, bytenr,
1736 parent, root_objectid);
1737 } else {
1738 ret = insert_extent_data_ref(trans, root, path, bytenr,
1739 parent, root_objectid,
1740 owner, offset, refs_to_add);
1741 }
1742 return ret;
1743}
1744
1745static int remove_extent_backref(struct btrfs_trans_handle *trans,
1746 struct btrfs_root *root,
1747 struct btrfs_path *path,
1748 struct btrfs_extent_inline_ref *iref,
1749 int refs_to_drop, int is_data)
1750{
1751 int ret;
1752
1753 BUG_ON(!is_data && refs_to_drop != 1);
1754 if (iref) {
1755 ret = update_inline_extent_backref(trans, root, path, iref,
1756 -refs_to_drop, NULL);
1757 } else if (is_data) {
1758 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1759 } else {
1760 ret = btrfs_del_item(trans, root, path);
1761 }
1762 return ret;
1763}
1764
Li Dongyang5378e602011-03-24 10:24:27 +00001765static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001766 u64 start, u64 len)
1767{
Li Dongyang5378e602011-03-24 10:24:27 +00001768 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001769}
Chris Mason15916de2008-11-19 21:17:22 -05001770
Liu Hui1f3c79a2009-01-05 15:57:51 -05001771static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001772 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001773{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001774 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001775 u64 discarded_bytes = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001776 struct btrfs_multi_bio *multi = NULL;
1777
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001778
Liu Hui1f3c79a2009-01-05 15:57:51 -05001779 /* Tell the block device(s) that the sectors can be discarded */
Li Dongyang5378e602011-03-24 10:24:27 +00001780 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1781 bytenr, &num_bytes, &multi, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001782 if (!ret) {
1783 struct btrfs_bio_stripe *stripe = multi->stripes;
1784 int i;
1785
Liu Hui1f3c79a2009-01-05 15:57:51 -05001786
1787 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Li Dongyang5378e602011-03-24 10:24:27 +00001788 ret = btrfs_issue_discard(stripe->dev->bdev,
1789 stripe->physical,
1790 stripe->length);
1791 if (!ret)
1792 discarded_bytes += stripe->length;
1793 else if (ret != -EOPNOTSUPP)
1794 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001795 }
1796 kfree(multi);
1797 }
Li Dongyang5378e602011-03-24 10:24:27 +00001798 if (discarded_bytes && ret == -EOPNOTSUPP)
1799 ret = 0;
1800
1801 if (actual_bytes)
1802 *actual_bytes = discarded_bytes;
1803
Liu Hui1f3c79a2009-01-05 15:57:51 -05001804
1805 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001806}
1807
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001808int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1809 struct btrfs_root *root,
1810 u64 bytenr, u64 num_bytes, u64 parent,
1811 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001812{
1813 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001814 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1815 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001816
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001817 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1818 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1819 parent, root_objectid, (int)owner,
1820 BTRFS_ADD_DELAYED_REF, NULL);
1821 } else {
1822 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1823 parent, root_objectid, owner, offset,
1824 BTRFS_ADD_DELAYED_REF, NULL);
1825 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001826 return ret;
1827}
1828
Chris Mason925baed2008-06-25 16:01:30 -04001829static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001830 struct btrfs_root *root,
1831 u64 bytenr, u64 num_bytes,
1832 u64 parent, u64 root_objectid,
1833 u64 owner, u64 offset, int refs_to_add,
1834 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001835{
Chris Mason5caf2a02007-04-02 11:20:42 -04001836 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001837 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001838 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001839 u64 refs;
1840 int ret;
1841 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001842
Chris Mason5caf2a02007-04-02 11:20:42 -04001843 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001844 if (!path)
1845 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001846
Chris Mason3c12ac72008-04-21 12:01:38 -04001847 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001848 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001849 /* this will setup the path even if it fails to insert the back ref */
1850 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1851 path, bytenr, num_bytes, parent,
1852 root_objectid, owner, offset,
1853 refs_to_add, extent_op);
1854 if (ret == 0)
1855 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001856
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001857 if (ret != -EAGAIN) {
1858 err = ret;
1859 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001860 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001861
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001862 leaf = path->nodes[0];
1863 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1864 refs = btrfs_extent_refs(leaf, item);
1865 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1866 if (extent_op)
1867 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001868
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001869 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001870 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001871
Chris Mason3c12ac72008-04-21 12:01:38 -04001872 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001873 path->leave_spinning = 1;
1874
Chris Mason56bec292009-03-13 10:10:06 -04001875 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001876 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001877 path, bytenr, parent, root_objectid,
1878 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001879 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001880out:
Chris Mason74493f72007-12-11 09:25:06 -05001881 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001882 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001883}
1884
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001885static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *root,
1887 struct btrfs_delayed_ref_node *node,
1888 struct btrfs_delayed_extent_op *extent_op,
1889 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001890{
Chris Mason56bec292009-03-13 10:10:06 -04001891 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001892 struct btrfs_delayed_data_ref *ref;
1893 struct btrfs_key ins;
1894 u64 parent = 0;
1895 u64 ref_root = 0;
1896 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001897
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001898 ins.objectid = node->bytenr;
1899 ins.offset = node->num_bytes;
1900 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001901
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001902 ref = btrfs_delayed_node_to_data_ref(node);
1903 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1904 parent = ref->parent;
1905 else
1906 ref_root = ref->root;
1907
1908 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1909 if (extent_op) {
1910 BUG_ON(extent_op->update_key);
1911 flags |= extent_op->flags_to_set;
1912 }
1913 ret = alloc_reserved_file_extent(trans, root,
1914 parent, ref_root, flags,
1915 ref->objectid, ref->offset,
1916 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001917 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1918 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1919 node->num_bytes, parent,
1920 ref_root, ref->objectid,
1921 ref->offset, node->ref_mod,
1922 extent_op);
1923 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1924 ret = __btrfs_free_extent(trans, root, node->bytenr,
1925 node->num_bytes, parent,
1926 ref_root, ref->objectid,
1927 ref->offset, node->ref_mod,
1928 extent_op);
1929 } else {
1930 BUG();
1931 }
Chris Mason56bec292009-03-13 10:10:06 -04001932 return ret;
1933}
1934
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001935static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1936 struct extent_buffer *leaf,
1937 struct btrfs_extent_item *ei)
1938{
1939 u64 flags = btrfs_extent_flags(leaf, ei);
1940 if (extent_op->update_flags) {
1941 flags |= extent_op->flags_to_set;
1942 btrfs_set_extent_flags(leaf, ei, flags);
1943 }
1944
1945 if (extent_op->update_key) {
1946 struct btrfs_tree_block_info *bi;
1947 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1948 bi = (struct btrfs_tree_block_info *)(ei + 1);
1949 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1950 }
1951}
1952
1953static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1954 struct btrfs_root *root,
1955 struct btrfs_delayed_ref_node *node,
1956 struct btrfs_delayed_extent_op *extent_op)
1957{
1958 struct btrfs_key key;
1959 struct btrfs_path *path;
1960 struct btrfs_extent_item *ei;
1961 struct extent_buffer *leaf;
1962 u32 item_size;
1963 int ret;
1964 int err = 0;
1965
1966 path = btrfs_alloc_path();
1967 if (!path)
1968 return -ENOMEM;
1969
1970 key.objectid = node->bytenr;
1971 key.type = BTRFS_EXTENT_ITEM_KEY;
1972 key.offset = node->num_bytes;
1973
1974 path->reada = 1;
1975 path->leave_spinning = 1;
1976 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1977 path, 0, 1);
1978 if (ret < 0) {
1979 err = ret;
1980 goto out;
1981 }
1982 if (ret > 0) {
1983 err = -EIO;
1984 goto out;
1985 }
1986
1987 leaf = path->nodes[0];
1988 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1989#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1990 if (item_size < sizeof(*ei)) {
1991 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1992 path, (u64)-1, 0);
1993 if (ret < 0) {
1994 err = ret;
1995 goto out;
1996 }
1997 leaf = path->nodes[0];
1998 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1999 }
2000#endif
2001 BUG_ON(item_size < sizeof(*ei));
2002 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2003 __run_delayed_extent_op(extent_op, leaf, ei);
2004
2005 btrfs_mark_buffer_dirty(leaf);
2006out:
2007 btrfs_free_path(path);
2008 return err;
2009}
2010
2011static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2012 struct btrfs_root *root,
2013 struct btrfs_delayed_ref_node *node,
2014 struct btrfs_delayed_extent_op *extent_op,
2015 int insert_reserved)
2016{
2017 int ret = 0;
2018 struct btrfs_delayed_tree_ref *ref;
2019 struct btrfs_key ins;
2020 u64 parent = 0;
2021 u64 ref_root = 0;
2022
2023 ins.objectid = node->bytenr;
2024 ins.offset = node->num_bytes;
2025 ins.type = BTRFS_EXTENT_ITEM_KEY;
2026
2027 ref = btrfs_delayed_node_to_tree_ref(node);
2028 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2029 parent = ref->parent;
2030 else
2031 ref_root = ref->root;
2032
2033 BUG_ON(node->ref_mod != 1);
2034 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2035 BUG_ON(!extent_op || !extent_op->update_flags ||
2036 !extent_op->update_key);
2037 ret = alloc_reserved_tree_block(trans, root,
2038 parent, ref_root,
2039 extent_op->flags_to_set,
2040 &extent_op->key,
2041 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002042 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2043 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2044 node->num_bytes, parent, ref_root,
2045 ref->level, 0, 1, extent_op);
2046 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2047 ret = __btrfs_free_extent(trans, root, node->bytenr,
2048 node->num_bytes, parent, ref_root,
2049 ref->level, 0, 1, extent_op);
2050 } else {
2051 BUG();
2052 }
2053 return ret;
2054}
2055
Chris Mason56bec292009-03-13 10:10:06 -04002056/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002057static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2058 struct btrfs_root *root,
2059 struct btrfs_delayed_ref_node *node,
2060 struct btrfs_delayed_extent_op *extent_op,
2061 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002062{
Josef Bacikeb099672009-02-12 09:27:38 -05002063 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002065 struct btrfs_delayed_ref_head *head;
2066 /*
2067 * we've hit the end of the chain and we were supposed
2068 * to insert this extent into the tree. But, it got
2069 * deleted before we ever needed to insert it, so all
2070 * we have to do is clean up the accounting
2071 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002072 BUG_ON(extent_op);
2073 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002074 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002075 btrfs_pin_extent(root, node->bytenr,
2076 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002077 if (head->is_data) {
2078 ret = btrfs_del_csums(trans, root,
2079 node->bytenr,
2080 node->num_bytes);
2081 BUG_ON(ret);
2082 }
Chris Mason56bec292009-03-13 10:10:06 -04002083 }
Chris Mason56bec292009-03-13 10:10:06 -04002084 mutex_unlock(&head->mutex);
2085 return 0;
2086 }
Josef Bacikeb099672009-02-12 09:27:38 -05002087
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002088 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2089 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2090 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2091 insert_reserved);
2092 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2093 node->type == BTRFS_SHARED_DATA_REF_KEY)
2094 ret = run_delayed_data_ref(trans, root, node, extent_op,
2095 insert_reserved);
2096 else
2097 BUG();
2098 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002099}
2100
Chris Mason56bec292009-03-13 10:10:06 -04002101static noinline struct btrfs_delayed_ref_node *
2102select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002103{
Chris Mason56bec292009-03-13 10:10:06 -04002104 struct rb_node *node;
2105 struct btrfs_delayed_ref_node *ref;
2106 int action = BTRFS_ADD_DELAYED_REF;
2107again:
2108 /*
2109 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2110 * this prevents ref count from going down to zero when
2111 * there still are pending delayed ref.
2112 */
2113 node = rb_prev(&head->node.rb_node);
2114 while (1) {
2115 if (!node)
2116 break;
2117 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2118 rb_node);
2119 if (ref->bytenr != head->node.bytenr)
2120 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002121 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002122 return ref;
2123 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002124 }
Chris Mason56bec292009-03-13 10:10:06 -04002125 if (action == BTRFS_ADD_DELAYED_REF) {
2126 action = BTRFS_DROP_DELAYED_REF;
2127 goto again;
2128 }
2129 return NULL;
2130}
2131
Chris Masonc3e69d52009-03-13 10:17:05 -04002132static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2133 struct btrfs_root *root,
2134 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002135{
Chris Mason56bec292009-03-13 10:10:06 -04002136 struct btrfs_delayed_ref_root *delayed_refs;
2137 struct btrfs_delayed_ref_node *ref;
2138 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002139 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002140 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002141 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002142 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002143
2144 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002145 while (1) {
2146 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002147 /* pick a new head ref from the cluster list */
2148 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002149 break;
Chris Mason56bec292009-03-13 10:10:06 -04002150
Chris Masonc3e69d52009-03-13 10:17:05 -04002151 locked_ref = list_entry(cluster->next,
2152 struct btrfs_delayed_ref_head, cluster);
2153
2154 /* grab the lock that says we are going to process
2155 * all the refs for this head */
2156 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2157
2158 /*
2159 * we may have dropped the spin lock to get the head
2160 * mutex lock, and that might have given someone else
2161 * time to free the head. If that's true, it has been
2162 * removed from our list and we can move on.
2163 */
2164 if (ret == -EAGAIN) {
2165 locked_ref = NULL;
2166 count++;
2167 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002168 }
2169 }
2170
2171 /*
2172 * record the must insert reserved flag before we
2173 * drop the spin lock.
2174 */
2175 must_insert_reserved = locked_ref->must_insert_reserved;
2176 locked_ref->must_insert_reserved = 0;
2177
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002178 extent_op = locked_ref->extent_op;
2179 locked_ref->extent_op = NULL;
2180
Chris Mason56bec292009-03-13 10:10:06 -04002181 /*
2182 * locked_ref is the head node, so we have to go one
2183 * node back for any delayed ref updates
2184 */
Chris Mason56bec292009-03-13 10:10:06 -04002185 ref = select_delayed_ref(locked_ref);
2186 if (!ref) {
2187 /* All delayed refs have been processed, Go ahead
2188 * and send the head node to run_one_delayed_ref,
2189 * so that any accounting fixes can happen
2190 */
2191 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002192
2193 if (extent_op && must_insert_reserved) {
2194 kfree(extent_op);
2195 extent_op = NULL;
2196 }
2197
2198 if (extent_op) {
2199 spin_unlock(&delayed_refs->lock);
2200
2201 ret = run_delayed_extent_op(trans, root,
2202 ref, extent_op);
2203 BUG_ON(ret);
2204 kfree(extent_op);
2205
2206 cond_resched();
2207 spin_lock(&delayed_refs->lock);
2208 continue;
2209 }
2210
Chris Masonc3e69d52009-03-13 10:17:05 -04002211 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002212 locked_ref = NULL;
2213 }
2214
2215 ref->in_tree = 0;
2216 rb_erase(&ref->rb_node, &delayed_refs->root);
2217 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002218
Chris Mason56bec292009-03-13 10:10:06 -04002219 spin_unlock(&delayed_refs->lock);
2220
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002221 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002222 must_insert_reserved);
2223 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002224
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002225 btrfs_put_delayed_ref(ref);
2226 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002227 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002228
Chris Mason1887be62009-03-13 10:11:24 -04002229 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002230 spin_lock(&delayed_refs->lock);
2231 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002232 return count;
2233}
2234
2235/*
2236 * this starts processing the delayed reference count updates and
2237 * extent insertions we have queued up so far. count can be
2238 * 0, which means to process everything in the tree at the start
2239 * of the run (but not newly added entries), or it can be some target
2240 * number you'd like to process.
2241 */
2242int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2243 struct btrfs_root *root, unsigned long count)
2244{
2245 struct rb_node *node;
2246 struct btrfs_delayed_ref_root *delayed_refs;
2247 struct btrfs_delayed_ref_node *ref;
2248 struct list_head cluster;
2249 int ret;
2250 int run_all = count == (unsigned long)-1;
2251 int run_most = 0;
2252
2253 if (root == root->fs_info->extent_root)
2254 root = root->fs_info->tree_root;
2255
2256 delayed_refs = &trans->transaction->delayed_refs;
2257 INIT_LIST_HEAD(&cluster);
2258again:
2259 spin_lock(&delayed_refs->lock);
2260 if (count == 0) {
2261 count = delayed_refs->num_entries * 2;
2262 run_most = 1;
2263 }
2264 while (1) {
2265 if (!(run_all || run_most) &&
2266 delayed_refs->num_heads_ready < 64)
2267 break;
2268
2269 /*
2270 * go find something we can process in the rbtree. We start at
2271 * the beginning of the tree, and then build a cluster
2272 * of refs to process starting at the first one we are able to
2273 * lock
2274 */
2275 ret = btrfs_find_ref_cluster(trans, &cluster,
2276 delayed_refs->run_delayed_start);
2277 if (ret)
2278 break;
2279
2280 ret = run_clustered_refs(trans, root, &cluster);
2281 BUG_ON(ret < 0);
2282
2283 count -= min_t(unsigned long, ret, count);
2284
2285 if (count == 0)
2286 break;
2287 }
2288
Chris Mason56bec292009-03-13 10:10:06 -04002289 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002290 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002291 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002292 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002293 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002294
2295 while (node) {
2296 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2297 rb_node);
2298 if (btrfs_delayed_ref_is_head(ref)) {
2299 struct btrfs_delayed_ref_head *head;
2300
2301 head = btrfs_delayed_node_to_head(ref);
2302 atomic_inc(&ref->refs);
2303
2304 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002305 /*
2306 * Mutex was contended, block until it's
2307 * released and try again
2308 */
Chris Mason56bec292009-03-13 10:10:06 -04002309 mutex_lock(&head->mutex);
2310 mutex_unlock(&head->mutex);
2311
2312 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002313 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002314 goto again;
2315 }
2316 node = rb_next(node);
2317 }
2318 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002319 schedule_timeout(1);
2320 goto again;
2321 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002322out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002323 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002324 return 0;
2325}
2326
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002327int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2328 struct btrfs_root *root,
2329 u64 bytenr, u64 num_bytes, u64 flags,
2330 int is_data)
2331{
2332 struct btrfs_delayed_extent_op *extent_op;
2333 int ret;
2334
2335 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2336 if (!extent_op)
2337 return -ENOMEM;
2338
2339 extent_op->flags_to_set = flags;
2340 extent_op->update_flags = 1;
2341 extent_op->update_key = 0;
2342 extent_op->is_data = is_data ? 1 : 0;
2343
2344 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2345 if (ret)
2346 kfree(extent_op);
2347 return ret;
2348}
2349
2350static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2351 struct btrfs_root *root,
2352 struct btrfs_path *path,
2353 u64 objectid, u64 offset, u64 bytenr)
2354{
2355 struct btrfs_delayed_ref_head *head;
2356 struct btrfs_delayed_ref_node *ref;
2357 struct btrfs_delayed_data_ref *data_ref;
2358 struct btrfs_delayed_ref_root *delayed_refs;
2359 struct rb_node *node;
2360 int ret = 0;
2361
2362 ret = -ENOENT;
2363 delayed_refs = &trans->transaction->delayed_refs;
2364 spin_lock(&delayed_refs->lock);
2365 head = btrfs_find_delayed_ref_head(trans, bytenr);
2366 if (!head)
2367 goto out;
2368
2369 if (!mutex_trylock(&head->mutex)) {
2370 atomic_inc(&head->node.refs);
2371 spin_unlock(&delayed_refs->lock);
2372
David Sterbab3b4aa72011-04-21 01:20:15 +02002373 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002374
David Sterba8cc33e52011-05-02 15:29:25 +02002375 /*
2376 * Mutex was contended, block until it's released and let
2377 * caller try again
2378 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002379 mutex_lock(&head->mutex);
2380 mutex_unlock(&head->mutex);
2381 btrfs_put_delayed_ref(&head->node);
2382 return -EAGAIN;
2383 }
2384
2385 node = rb_prev(&head->node.rb_node);
2386 if (!node)
2387 goto out_unlock;
2388
2389 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2390
2391 if (ref->bytenr != bytenr)
2392 goto out_unlock;
2393
2394 ret = 1;
2395 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2396 goto out_unlock;
2397
2398 data_ref = btrfs_delayed_node_to_data_ref(ref);
2399
2400 node = rb_prev(node);
2401 if (node) {
2402 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2403 if (ref->bytenr == bytenr)
2404 goto out_unlock;
2405 }
2406
2407 if (data_ref->root != root->root_key.objectid ||
2408 data_ref->objectid != objectid || data_ref->offset != offset)
2409 goto out_unlock;
2410
2411 ret = 0;
2412out_unlock:
2413 mutex_unlock(&head->mutex);
2414out:
2415 spin_unlock(&delayed_refs->lock);
2416 return ret;
2417}
2418
2419static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2420 struct btrfs_root *root,
2421 struct btrfs_path *path,
2422 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002423{
2424 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002425 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002426 struct btrfs_extent_data_ref *ref;
2427 struct btrfs_extent_inline_ref *iref;
2428 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002429 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002430 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002431 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002432
Chris Masonbe20aa92007-12-17 20:14:01 -05002433 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002434 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002435 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002436
Chris Masonbe20aa92007-12-17 20:14:01 -05002437 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2438 if (ret < 0)
2439 goto out;
2440 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002441
2442 ret = -ENOENT;
2443 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002444 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002445
Zheng Yan31840ae2008-09-23 13:14:14 -04002446 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002447 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002448 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002449
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002450 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002451 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002452
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002453 ret = 1;
2454 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2455#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2456 if (item_size < sizeof(*ei)) {
2457 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2458 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002459 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002460#endif
2461 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2462
2463 if (item_size != sizeof(*ei) +
2464 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2465 goto out;
2466
2467 if (btrfs_extent_generation(leaf, ei) <=
2468 btrfs_root_last_snapshot(&root->root_item))
2469 goto out;
2470
2471 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2472 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2473 BTRFS_EXTENT_DATA_REF_KEY)
2474 goto out;
2475
2476 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2477 if (btrfs_extent_refs(leaf, ei) !=
2478 btrfs_extent_data_ref_count(leaf, ref) ||
2479 btrfs_extent_data_ref_root(leaf, ref) !=
2480 root->root_key.objectid ||
2481 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2482 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2483 goto out;
2484
Yan Zhengf321e492008-07-30 09:26:11 -04002485 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002486out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002487 return ret;
2488}
2489
2490int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2491 struct btrfs_root *root,
2492 u64 objectid, u64 offset, u64 bytenr)
2493{
2494 struct btrfs_path *path;
2495 int ret;
2496 int ret2;
2497
2498 path = btrfs_alloc_path();
2499 if (!path)
2500 return -ENOENT;
2501
2502 do {
2503 ret = check_committed_ref(trans, root, path, objectid,
2504 offset, bytenr);
2505 if (ret && ret != -ENOENT)
2506 goto out;
2507
2508 ret2 = check_delayed_ref(trans, root, path, objectid,
2509 offset, bytenr);
2510 } while (ret2 == -EAGAIN);
2511
2512 if (ret2 && ret2 != -ENOENT) {
2513 ret = ret2;
2514 goto out;
2515 }
2516
2517 if (ret != -ENOENT || ret2 != -ENOENT)
2518 ret = 0;
2519out:
Yan Zhengf321e492008-07-30 09:26:11 -04002520 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002521 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2522 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002523 return ret;
2524}
2525
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002526static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002527 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002528 struct extent_buffer *buf,
2529 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002530{
2531 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002532 u64 num_bytes;
2533 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002534 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002535 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002536 struct btrfs_key key;
2537 struct btrfs_file_extent_item *fi;
2538 int i;
2539 int level;
2540 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002541 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002542 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002543
2544 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002545 nritems = btrfs_header_nritems(buf);
2546 level = btrfs_header_level(buf);
2547
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002548 if (!root->ref_cows && level == 0)
2549 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002550
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002551 if (inc)
2552 process_func = btrfs_inc_extent_ref;
2553 else
2554 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002555
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002556 if (full_backref)
2557 parent = buf->start;
2558 else
2559 parent = 0;
2560
Zheng Yan31840ae2008-09-23 13:14:14 -04002561 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002562 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002563 btrfs_item_key_to_cpu(buf, &key, i);
2564 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002565 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002566 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002567 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002568 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002569 BTRFS_FILE_EXTENT_INLINE)
2570 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002571 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2572 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002573 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002574
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002575 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2576 key.offset -= btrfs_file_extent_offset(buf, fi);
2577 ret = process_func(trans, root, bytenr, num_bytes,
2578 parent, ref_root, key.objectid,
2579 key.offset);
2580 if (ret)
2581 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002582 } else {
2583 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002584 num_bytes = btrfs_level_size(root, level - 1);
2585 ret = process_func(trans, root, bytenr, num_bytes,
2586 parent, ref_root, level - 1, 0);
2587 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002588 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002589 }
2590 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002591 return 0;
2592fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002593 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002594 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002595}
2596
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002597int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2598 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002599{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002600 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2601}
Zheng Yan31840ae2008-09-23 13:14:14 -04002602
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002603int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2604 struct extent_buffer *buf, int full_backref)
2605{
2606 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002607}
2608
Chris Mason9078a3e2007-04-26 16:46:15 -04002609static int write_one_cache_group(struct btrfs_trans_handle *trans,
2610 struct btrfs_root *root,
2611 struct btrfs_path *path,
2612 struct btrfs_block_group_cache *cache)
2613{
2614 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002615 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002616 unsigned long bi;
2617 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002618
Chris Mason9078a3e2007-04-26 16:46:15 -04002619 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002620 if (ret < 0)
2621 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002622 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002623
2624 leaf = path->nodes[0];
2625 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2626 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2627 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002628 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002629fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002630 if (ret)
2631 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002632 return 0;
2633
2634}
2635
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002636static struct btrfs_block_group_cache *
2637next_block_group(struct btrfs_root *root,
2638 struct btrfs_block_group_cache *cache)
2639{
2640 struct rb_node *node;
2641 spin_lock(&root->fs_info->block_group_cache_lock);
2642 node = rb_next(&cache->cache_node);
2643 btrfs_put_block_group(cache);
2644 if (node) {
2645 cache = rb_entry(node, struct btrfs_block_group_cache,
2646 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002647 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002648 } else
2649 cache = NULL;
2650 spin_unlock(&root->fs_info->block_group_cache_lock);
2651 return cache;
2652}
2653
Josef Bacik0af3d002010-06-21 14:48:16 -04002654static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2655 struct btrfs_trans_handle *trans,
2656 struct btrfs_path *path)
2657{
2658 struct btrfs_root *root = block_group->fs_info->tree_root;
2659 struct inode *inode = NULL;
2660 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002661 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002662 int num_pages = 0;
2663 int retries = 0;
2664 int ret = 0;
2665
2666 /*
2667 * If this block group is smaller than 100 megs don't bother caching the
2668 * block group.
2669 */
2670 if (block_group->key.offset < (100 * 1024 * 1024)) {
2671 spin_lock(&block_group->lock);
2672 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2673 spin_unlock(&block_group->lock);
2674 return 0;
2675 }
2676
2677again:
2678 inode = lookup_free_space_inode(root, block_group, path);
2679 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2680 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002681 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002682 goto out;
2683 }
2684
2685 if (IS_ERR(inode)) {
2686 BUG_ON(retries);
2687 retries++;
2688
2689 if (block_group->ro)
2690 goto out_free;
2691
2692 ret = create_free_space_inode(root, trans, block_group, path);
2693 if (ret)
2694 goto out_free;
2695 goto again;
2696 }
2697
2698 /*
2699 * We want to set the generation to 0, that way if anything goes wrong
2700 * from here on out we know not to trust this cache when we load up next
2701 * time.
2702 */
2703 BTRFS_I(inode)->generation = 0;
2704 ret = btrfs_update_inode(trans, root, inode);
2705 WARN_ON(ret);
2706
2707 if (i_size_read(inode) > 0) {
2708 ret = btrfs_truncate_free_space_cache(root, trans, path,
2709 inode);
2710 if (ret)
2711 goto out_put;
2712 }
2713
2714 spin_lock(&block_group->lock);
2715 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002716 /* We're not cached, don't bother trying to write stuff out */
2717 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002718 spin_unlock(&block_group->lock);
2719 goto out_put;
2720 }
2721 spin_unlock(&block_group->lock);
2722
2723 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2724 if (!num_pages)
2725 num_pages = 1;
2726
2727 /*
2728 * Just to make absolutely sure we have enough space, we're going to
2729 * preallocate 12 pages worth of space for each block group. In
2730 * practice we ought to use at most 8, but we need extra space so we can
2731 * add our header and have a terminator between the extents and the
2732 * bitmaps.
2733 */
2734 num_pages *= 16;
2735 num_pages *= PAGE_CACHE_SIZE;
2736
2737 ret = btrfs_check_data_free_space(inode, num_pages);
2738 if (ret)
2739 goto out_put;
2740
2741 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2742 num_pages, num_pages,
2743 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002744 if (!ret)
2745 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002746 btrfs_free_reserved_data_space(inode, num_pages);
2747out_put:
2748 iput(inode);
2749out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02002750 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002751out:
2752 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002753 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002754 spin_unlock(&block_group->lock);
2755
2756 return ret;
2757}
2758
Chris Mason96b51792007-10-15 16:15:19 -04002759int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2760 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002761{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002762 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002763 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002764 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002765 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002766
2767 path = btrfs_alloc_path();
2768 if (!path)
2769 return -ENOMEM;
2770
Josef Bacik0af3d002010-06-21 14:48:16 -04002771again:
2772 while (1) {
2773 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2774 while (cache) {
2775 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2776 break;
2777 cache = next_block_group(root, cache);
2778 }
2779 if (!cache) {
2780 if (last == 0)
2781 break;
2782 last = 0;
2783 continue;
2784 }
2785 err = cache_save_setup(cache, trans, path);
2786 last = cache->key.objectid + cache->key.offset;
2787 btrfs_put_block_group(cache);
2788 }
2789
Chris Masond3977122009-01-05 21:25:51 -05002790 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002791 if (last == 0) {
2792 err = btrfs_run_delayed_refs(trans, root,
2793 (unsigned long)-1);
2794 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002795 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002796
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002797 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2798 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002799 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2800 btrfs_put_block_group(cache);
2801 goto again;
2802 }
2803
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002804 if (cache->dirty)
2805 break;
2806 cache = next_block_group(root, cache);
2807 }
2808 if (!cache) {
2809 if (last == 0)
2810 break;
2811 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002812 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002813 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002814
Josef Bacik0cb59c92010-07-02 12:14:14 -04002815 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2816 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002817 cache->dirty = 0;
2818 last = cache->key.objectid + cache->key.offset;
2819
2820 err = write_one_cache_group(trans, root, path, cache);
2821 BUG_ON(err);
2822 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002823 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002824
Josef Bacik0cb59c92010-07-02 12:14:14 -04002825 while (1) {
2826 /*
2827 * I don't think this is needed since we're just marking our
2828 * preallocated extent as written, but just in case it can't
2829 * hurt.
2830 */
2831 if (last == 0) {
2832 err = btrfs_run_delayed_refs(trans, root,
2833 (unsigned long)-1);
2834 BUG_ON(err);
2835 }
2836
2837 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2838 while (cache) {
2839 /*
2840 * Really this shouldn't happen, but it could if we
2841 * couldn't write the entire preallocated extent and
2842 * splitting the extent resulted in a new block.
2843 */
2844 if (cache->dirty) {
2845 btrfs_put_block_group(cache);
2846 goto again;
2847 }
2848 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2849 break;
2850 cache = next_block_group(root, cache);
2851 }
2852 if (!cache) {
2853 if (last == 0)
2854 break;
2855 last = 0;
2856 continue;
2857 }
2858
2859 btrfs_write_out_cache(root, trans, cache, path);
2860
2861 /*
2862 * If we didn't have an error then the cache state is still
2863 * NEED_WRITE, so we can set it to WRITTEN.
2864 */
2865 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2866 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2867 last = cache->key.objectid + cache->key.offset;
2868 btrfs_put_block_group(cache);
2869 }
2870
Chris Mason9078a3e2007-04-26 16:46:15 -04002871 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002872 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002873}
2874
Yan Zhengd2fb3432008-12-11 16:30:39 -05002875int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2876{
2877 struct btrfs_block_group_cache *block_group;
2878 int readonly = 0;
2879
2880 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2881 if (!block_group || block_group->ro)
2882 readonly = 1;
2883 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002884 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002885 return readonly;
2886}
2887
Chris Mason593060d2008-03-25 16:50:33 -04002888static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2889 u64 total_bytes, u64 bytes_used,
2890 struct btrfs_space_info **space_info)
2891{
2892 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002893 int i;
2894 int factor;
2895
2896 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2897 BTRFS_BLOCK_GROUP_RAID10))
2898 factor = 2;
2899 else
2900 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002901
2902 found = __find_space_info(info, flags);
2903 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002904 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002905 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002906 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002907 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002908 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002909 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002910 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002911 *space_info = found;
2912 return 0;
2913 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002914 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002915 if (!found)
2916 return -ENOMEM;
2917
Yan, Zhengb742bb82010-05-16 10:46:24 -04002918 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2919 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04002920 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002921 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04002922 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2923 BTRFS_BLOCK_GROUP_SYSTEM |
2924 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04002925 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002926 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002927 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002928 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002929 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002930 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002931 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002932 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002933 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04002934 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04002935 found->chunk_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002936 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002937 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002938 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002939 return 0;
2940}
2941
Chris Mason8790d502008-04-03 16:29:03 -04002942static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2943{
2944 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002945 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002946 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002947 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002948 if (extra_flags) {
2949 if (flags & BTRFS_BLOCK_GROUP_DATA)
2950 fs_info->avail_data_alloc_bits |= extra_flags;
2951 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2952 fs_info->avail_metadata_alloc_bits |= extra_flags;
2953 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2954 fs_info->avail_system_alloc_bits |= extra_flags;
2955 }
2956}
Chris Mason593060d2008-03-25 16:50:33 -04002957
Yan Zheng2b820322008-11-17 21:11:30 -05002958u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002959{
Chris Masoncd02dca2010-12-13 14:56:23 -05002960 /*
2961 * we add in the count of missing devices because we want
2962 * to make sure that any RAID levels on a degraded FS
2963 * continue to be honored.
2964 */
2965 u64 num_devices = root->fs_info->fs_devices->rw_devices +
2966 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002967
2968 if (num_devices == 1)
2969 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2970 if (num_devices < 4)
2971 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2972
Chris Masonec44a352008-04-28 15:29:52 -04002973 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2974 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002975 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002976 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002977 }
Chris Masonec44a352008-04-28 15:29:52 -04002978
2979 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002980 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002981 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002982 }
Chris Masonec44a352008-04-28 15:29:52 -04002983
2984 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2985 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2986 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2987 (flags & BTRFS_BLOCK_GROUP_DUP)))
2988 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2989 return flags;
2990}
2991
Yan, Zhengb742bb82010-05-16 10:46:24 -04002992static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05002993{
Yan, Zhengb742bb82010-05-16 10:46:24 -04002994 if (flags & BTRFS_BLOCK_GROUP_DATA)
2995 flags |= root->fs_info->avail_data_alloc_bits &
2996 root->fs_info->data_alloc_profile;
2997 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2998 flags |= root->fs_info->avail_system_alloc_bits &
2999 root->fs_info->system_alloc_profile;
3000 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3001 flags |= root->fs_info->avail_metadata_alloc_bits &
3002 root->fs_info->metadata_alloc_profile;
3003 return btrfs_reduce_alloc_profile(root, flags);
3004}
Josef Bacik6a632092009-02-20 11:00:09 -05003005
Miao Xie6d07bce2011-01-05 10:07:31 +00003006u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003007{
3008 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003009
Yan, Zhengb742bb82010-05-16 10:46:24 -04003010 if (data)
3011 flags = BTRFS_BLOCK_GROUP_DATA;
3012 else if (root == root->fs_info->chunk_root)
3013 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3014 else
3015 flags = BTRFS_BLOCK_GROUP_METADATA;
3016
3017 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003018}
3019
3020void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3021{
Josef Bacik6a632092009-02-20 11:00:09 -05003022 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003023 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003024}
3025
3026/*
3027 * This will check the space that the inode allocates from to make sure we have
3028 * enough space for bytes.
3029 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003030int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003031{
3032 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003033 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e2412010-03-19 14:38:13 +00003034 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003035 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003036
3037 /* make sure bytes are sectorsize aligned */
3038 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3039
Li Zefan82d59022011-04-20 10:33:24 +08003040 if (root == root->fs_info->tree_root ||
3041 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003042 alloc_chunk = 0;
3043 committed = 1;
3044 }
3045
Josef Bacik6a632092009-02-20 11:00:09 -05003046 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003047 if (!data_sinfo)
3048 goto alloc;
3049
Josef Bacik6a632092009-02-20 11:00:09 -05003050again:
3051 /* make sure we have enough space to handle the data first */
3052 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003053 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3054 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3055 data_sinfo->bytes_may_use;
Josef Bacikab6e2412010-03-19 14:38:13 +00003056
3057 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003058 struct btrfs_trans_handle *trans;
3059
Josef Bacik6a632092009-02-20 11:00:09 -05003060 /*
3061 * if we don't have enough free bytes in this space then we need
3062 * to alloc a new chunk.
3063 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003064 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003065 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003066
Chris Mason0e4f8f82011-04-15 16:05:44 -04003067 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003068 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003069alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003070 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003071 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003072 if (IS_ERR(trans))
3073 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003074
3075 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3076 bytes + 2 * 1024 * 1024,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003077 alloc_target,
3078 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003079 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003080 if (ret < 0) {
3081 if (ret != -ENOSPC)
3082 return ret;
3083 else
3084 goto commit_trans;
3085 }
Chris Mason33b4d472009-09-22 14:45:50 -04003086
3087 if (!data_sinfo) {
3088 btrfs_set_inode_space_info(root, inode);
3089 data_sinfo = BTRFS_I(inode)->space_info;
3090 }
Josef Bacik6a632092009-02-20 11:00:09 -05003091 goto again;
3092 }
3093 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003094
3095 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003096commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003097 if (!committed &&
3098 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003099 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003100 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003101 if (IS_ERR(trans))
3102 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003103 ret = btrfs_commit_transaction(trans, root);
3104 if (ret)
3105 return ret;
3106 goto again;
3107 }
3108
Josef Bacik6a632092009-02-20 11:00:09 -05003109 return -ENOSPC;
3110 }
3111 data_sinfo->bytes_may_use += bytes;
3112 BTRFS_I(inode)->reserved_bytes += bytes;
3113 spin_unlock(&data_sinfo->lock);
3114
Josef Bacik9ed74f22009-09-11 16:12:44 -04003115 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003116}
3117
3118/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003119 * called when we are clearing an delalloc extent from the
3120 * inode's io_tree or there was an error for whatever reason
3121 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003122 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003123void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003124{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003125 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003126 struct btrfs_space_info *data_sinfo;
3127
3128 /* make sure bytes are sectorsize aligned */
3129 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3130
3131 data_sinfo = BTRFS_I(inode)->space_info;
3132 spin_lock(&data_sinfo->lock);
3133 data_sinfo->bytes_may_use -= bytes;
3134 BTRFS_I(inode)->reserved_bytes -= bytes;
3135 spin_unlock(&data_sinfo->lock);
3136}
3137
Josef Bacik97e728d2009-04-21 17:40:57 -04003138static void force_metadata_allocation(struct btrfs_fs_info *info)
3139{
3140 struct list_head *head = &info->space_info;
3141 struct btrfs_space_info *found;
3142
3143 rcu_read_lock();
3144 list_for_each_entry_rcu(found, head, list) {
3145 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003146 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003147 }
3148 rcu_read_unlock();
3149}
3150
Chris Masone5bc2452010-10-26 13:37:56 -04003151static int should_alloc_chunk(struct btrfs_root *root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003152 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3153 int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003154{
3155 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003156 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003157 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003158
Chris Mason0e4f8f82011-04-15 16:05:44 -04003159 if (force == CHUNK_ALLOC_FORCE)
3160 return 1;
3161
3162 /*
3163 * in limited mode, we want to have some free space up to
3164 * about 1% of the FS size.
3165 */
3166 if (force == CHUNK_ALLOC_LIMITED) {
3167 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3168 thresh = max_t(u64, 64 * 1024 * 1024,
3169 div_factor_fine(thresh, 1));
3170
3171 if (num_bytes - num_allocated < thresh)
3172 return 1;
3173 }
3174
3175 /*
3176 * we have two similar checks here, one based on percentage
3177 * and once based on a hard number of 256MB. The idea
3178 * is that if we have a good amount of free
3179 * room, don't allocate a chunk. A good mount is
3180 * less than 80% utilized of the chunks we have allocated,
3181 * or more than 256MB free
3182 */
3183 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003184 return 0;
3185
Chris Mason0e4f8f82011-04-15 16:05:44 -04003186 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
Yan, Zheng424499d2010-05-16 10:46:25 -04003187 return 0;
3188
Chris Masone5bc2452010-10-26 13:37:56 -04003189 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003190
3191 /* 256MB or 5% of the FS */
Chris Masone5bc2452010-10-26 13:37:56 -04003192 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3193
3194 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003195 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003196 return 1;
3197}
3198
Chris Mason6324fbf2008-03-24 15:01:59 -04003199static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3200 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003201 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003202{
3203 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003204 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003205 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003206 int ret = 0;
3207
Yan Zheng2b820322008-11-17 21:11:30 -05003208 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003209
Chris Mason6324fbf2008-03-24 15:01:59 -04003210 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003211 if (!space_info) {
3212 ret = update_space_info(extent_root->fs_info, flags,
3213 0, 0, &space_info);
3214 BUG_ON(ret);
3215 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003216 BUG_ON(!space_info);
3217
Josef Bacik6d741192011-04-11 20:20:11 -04003218again:
Josef Bacik25179202008-10-29 14:49:05 -04003219 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003220 if (space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003221 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003222 if (space_info->full) {
3223 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003224 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003225 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003226
Chris Mason0e4f8f82011-04-15 16:05:44 -04003227 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003228 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003229 return 0;
3230 } else if (space_info->chunk_alloc) {
3231 wait_for_alloc = 1;
3232 } else {
3233 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003234 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003235
Josef Bacik25179202008-10-29 14:49:05 -04003236 spin_unlock(&space_info->lock);
3237
Josef Bacik6d741192011-04-11 20:20:11 -04003238 mutex_lock(&fs_info->chunk_mutex);
3239
3240 /*
3241 * The chunk_mutex is held throughout the entirety of a chunk
3242 * allocation, so once we've acquired the chunk_mutex we know that the
3243 * other guy is done and we need to recheck and see if we should
3244 * allocate.
3245 */
3246 if (wait_for_alloc) {
3247 mutex_unlock(&fs_info->chunk_mutex);
3248 wait_for_alloc = 0;
3249 goto again;
3250 }
3251
Josef Bacik97e728d2009-04-21 17:40:57 -04003252 /*
Josef Bacik67377732010-09-16 16:19:09 -04003253 * If we have mixed data/metadata chunks we want to make sure we keep
3254 * allocating mixed chunks instead of individual chunks.
3255 */
3256 if (btrfs_mixed_space_info(space_info))
3257 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3258
3259 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003260 * if we're doing a data chunk, go ahead and make sure that
3261 * we keep a reasonable number of metadata chunks allocated in the
3262 * FS as well.
3263 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003264 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003265 fs_info->data_chunk_allocations++;
3266 if (!(fs_info->data_chunk_allocations %
3267 fs_info->metadata_ratio))
3268 force_metadata_allocation(fs_info);
3269 }
3270
Yan Zheng2b820322008-11-17 21:11:30 -05003271 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003272 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003273 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003274 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003275 else
3276 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003277
Chris Mason0e4f8f82011-04-15 16:05:44 -04003278 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003279 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003280 spin_unlock(&space_info->lock);
Yan Zhengc146afa2008-11-12 14:34:12 -05003281 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003282 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003283}
3284
Yan, Zheng5da9d012010-05-16 10:46:25 -04003285/*
3286 * shrink metadata reservation for delalloc
3287 */
3288static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003289 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003290{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003291 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003292 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003293 u64 reserved;
3294 u64 max_reclaim;
3295 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003296 long time_left;
Chris Masonbf9022e2010-10-26 13:40:45 -04003297 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003298 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003299 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003300
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003301 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003302 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003303
3304 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003305 reserved = space_info->bytes_reserved;
Chris Mason36e39c42011-03-12 07:08:42 -05003306 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003307
3308 if (reserved == 0)
3309 return 0;
3310
Sergei Trofimovichc4f675c2011-05-20 20:20:30 +00003311 /* nothing to shrink - nothing to reclaim */
3312 if (root->fs_info->delalloc_bytes == 0)
3313 return 0;
3314
Yan, Zheng5da9d012010-05-16 10:46:25 -04003315 max_reclaim = min(reserved, to_reclaim);
3316
Josef Bacikb1953bc2011-01-21 21:10:01 +00003317 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003318 /* have the flusher threads jump in and do some IO */
3319 smp_mb();
3320 nr_pages = min_t(unsigned long, nr_pages,
3321 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3322 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003323
Josef Bacik0019f102010-10-15 15:18:40 -04003324 spin_lock(&space_info->lock);
Chris Mason36e39c42011-03-12 07:08:42 -05003325 if (reserved > space_info->bytes_reserved)
Josef Bacik0019f102010-10-15 15:18:40 -04003326 reclaimed += reserved - space_info->bytes_reserved;
3327 reserved = space_info->bytes_reserved;
3328 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003329
Chris Mason36e39c42011-03-12 07:08:42 -05003330 loops++;
3331
Yan, Zheng5da9d012010-05-16 10:46:25 -04003332 if (reserved == 0 || reclaimed >= max_reclaim)
3333 break;
3334
3335 if (trans && trans->transaction->blocked)
3336 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003337
Chris Mason36e39c42011-03-12 07:08:42 -05003338 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003339
3340 /* We were interrupted, exit */
3341 if (time_left)
3342 break;
3343
Chris Mason36e39c42011-03-12 07:08:42 -05003344 /* we've kicked the IO a few times, if anything has been freed,
3345 * exit. There is no sense in looping here for a long time
3346 * when we really need to commit the transaction, or there are
3347 * just too many writers without enough free space
3348 */
3349
3350 if (loops > 3) {
3351 smp_mb();
3352 if (progress != space_info->reservation_progress)
3353 break;
3354 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003355
Yan, Zheng5da9d012010-05-16 10:46:25 -04003356 }
3357 return reclaimed >= to_reclaim;
3358}
3359
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003360/*
3361 * Retries tells us how many times we've called reserve_metadata_bytes. The
3362 * idea is if this is the first call (retries == 0) then we will add to our
3363 * reserved count if we can't make the allocation in order to hold our place
3364 * while we go and try and free up space. That way for retries > 1 we don't try
3365 * and add space, we just check to see if the amount of unused space is >= the
3366 * total space, meaning that our reservation is valid.
3367 *
3368 * However if we don't intend to retry this reservation, pass -1 as retries so
3369 * that it short circuits this logic.
3370 */
3371static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3372 struct btrfs_root *root,
3373 struct btrfs_block_rsv *block_rsv,
3374 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003375{
3376 struct btrfs_space_info *space_info = block_rsv->space_info;
3377 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003378 u64 num_bytes = orig_bytes;
3379 int retries = 0;
3380 int ret = 0;
3381 bool reserved = false;
Josef Bacik38227932010-10-26 12:52:53 -04003382 bool committed = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003383
3384again:
3385 ret = -ENOSPC;
3386 if (reserved)
3387 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003388
3389 spin_lock(&space_info->lock);
3390 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003391 space_info->bytes_pinned + space_info->bytes_readonly +
3392 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003393
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003394 /*
3395 * The idea here is that we've not already over-reserved the block group
3396 * then we can go ahead and save our reservation first and then start
3397 * flushing if we need to. Otherwise if we've already overcommitted
3398 * lets start flushing stuff first and then come back and try to make
3399 * our reservation.
3400 */
3401 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003402 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003403 if (unused >= num_bytes) {
3404 if (!reserved)
3405 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003406 ret = 0;
3407 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003408 /*
3409 * Ok set num_bytes to orig_bytes since we aren't
3410 * overocmmitted, this way we only try and reclaim what
3411 * we need.
3412 */
3413 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003414 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003415 } else {
3416 /*
3417 * Ok we're over committed, set num_bytes to the overcommitted
3418 * amount plus the amount of bytes that we need for this
3419 * reservation.
3420 */
3421 num_bytes = unused - space_info->total_bytes +
3422 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003423 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003424
3425 /*
3426 * Couldn't make our reservation, save our place so while we're trying
3427 * to reclaim space we can actually use it instead of somebody else
3428 * stealing it from us.
3429 */
3430 if (ret && !reserved) {
3431 space_info->bytes_reserved += orig_bytes;
3432 reserved = true;
3433 }
3434
Yan, Zhengf0486c62010-05-16 10:46:25 -04003435 spin_unlock(&space_info->lock);
3436
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003437 if (!ret)
3438 return 0;
3439
3440 if (!flush)
3441 goto out;
3442
3443 /*
3444 * We do synchronous shrinking since we don't actually unreserve
3445 * metadata until after the IO is completed.
3446 */
3447 ret = shrink_delalloc(trans, root, num_bytes, 1);
3448 if (ret > 0)
3449 return 0;
3450 else if (ret < 0)
3451 goto out;
3452
3453 /*
3454 * So if we were overcommitted it's possible that somebody else flushed
3455 * out enough space and we simply didn't have enough space to reclaim,
3456 * so go back around and try again.
3457 */
3458 if (retries < 2) {
3459 retries++;
3460 goto again;
3461 }
3462
3463 spin_lock(&space_info->lock);
3464 /*
3465 * Not enough space to be reclaimed, don't bother committing the
3466 * transaction.
3467 */
3468 if (space_info->bytes_pinned < orig_bytes)
3469 ret = -ENOSPC;
3470 spin_unlock(&space_info->lock);
3471 if (ret)
3472 goto out;
3473
3474 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003475 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003476 goto out;
3477
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003478 ret = -ENOSPC;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003479 trans = btrfs_join_transaction(root);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003480 if (IS_ERR(trans))
3481 goto out;
3482 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003483 if (!ret) {
3484 trans = NULL;
3485 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003486 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003487 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003488
3489out:
3490 if (reserved) {
3491 spin_lock(&space_info->lock);
3492 space_info->bytes_reserved -= orig_bytes;
3493 spin_unlock(&space_info->lock);
3494 }
3495
Yan, Zhengf0486c62010-05-16 10:46:25 -04003496 return ret;
3497}
3498
3499static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3500 struct btrfs_root *root)
3501{
3502 struct btrfs_block_rsv *block_rsv;
3503 if (root->ref_cows)
3504 block_rsv = trans->block_rsv;
3505 else
3506 block_rsv = root->block_rsv;
3507
3508 if (!block_rsv)
3509 block_rsv = &root->fs_info->empty_block_rsv;
3510
3511 return block_rsv;
3512}
3513
3514static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3515 u64 num_bytes)
3516{
3517 int ret = -ENOSPC;
3518 spin_lock(&block_rsv->lock);
3519 if (block_rsv->reserved >= num_bytes) {
3520 block_rsv->reserved -= num_bytes;
3521 if (block_rsv->reserved < block_rsv->size)
3522 block_rsv->full = 0;
3523 ret = 0;
3524 }
3525 spin_unlock(&block_rsv->lock);
3526 return ret;
3527}
3528
3529static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3530 u64 num_bytes, int update_size)
3531{
3532 spin_lock(&block_rsv->lock);
3533 block_rsv->reserved += num_bytes;
3534 if (update_size)
3535 block_rsv->size += num_bytes;
3536 else if (block_rsv->reserved >= block_rsv->size)
3537 block_rsv->full = 1;
3538 spin_unlock(&block_rsv->lock);
3539}
3540
David Sterba62a45b62011-04-20 15:52:26 +02003541static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3542 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003543{
3544 struct btrfs_space_info *space_info = block_rsv->space_info;
3545
3546 spin_lock(&block_rsv->lock);
3547 if (num_bytes == (u64)-1)
3548 num_bytes = block_rsv->size;
3549 block_rsv->size -= num_bytes;
3550 if (block_rsv->reserved >= block_rsv->size) {
3551 num_bytes = block_rsv->reserved - block_rsv->size;
3552 block_rsv->reserved = block_rsv->size;
3553 block_rsv->full = 1;
3554 } else {
3555 num_bytes = 0;
3556 }
3557 spin_unlock(&block_rsv->lock);
3558
3559 if (num_bytes > 0) {
3560 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003561 spin_lock(&dest->lock);
3562 if (!dest->full) {
3563 u64 bytes_to_add;
3564
3565 bytes_to_add = dest->size - dest->reserved;
3566 bytes_to_add = min(num_bytes, bytes_to_add);
3567 dest->reserved += bytes_to_add;
3568 if (dest->reserved >= dest->size)
3569 dest->full = 1;
3570 num_bytes -= bytes_to_add;
3571 }
3572 spin_unlock(&dest->lock);
3573 }
3574 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003575 spin_lock(&space_info->lock);
3576 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003577 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003578 spin_unlock(&space_info->lock);
3579 }
3580 }
3581}
3582
3583static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3584 struct btrfs_block_rsv *dst, u64 num_bytes)
3585{
3586 int ret;
3587
3588 ret = block_rsv_use_bytes(src, num_bytes);
3589 if (ret)
3590 return ret;
3591
3592 block_rsv_add_bytes(dst, num_bytes, 1);
3593 return 0;
3594}
3595
3596void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3597{
3598 memset(rsv, 0, sizeof(*rsv));
3599 spin_lock_init(&rsv->lock);
3600 atomic_set(&rsv->usage, 1);
3601 rsv->priority = 6;
3602 INIT_LIST_HEAD(&rsv->list);
3603}
3604
3605struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3606{
3607 struct btrfs_block_rsv *block_rsv;
3608 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003609
3610 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3611 if (!block_rsv)
3612 return NULL;
3613
3614 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003615 block_rsv->space_info = __find_space_info(fs_info,
3616 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003617 return block_rsv;
3618}
3619
3620void btrfs_free_block_rsv(struct btrfs_root *root,
3621 struct btrfs_block_rsv *rsv)
3622{
3623 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3624 btrfs_block_rsv_release(root, rsv, (u64)-1);
3625 if (!rsv->durable)
3626 kfree(rsv);
3627 }
3628}
3629
3630/*
3631 * make the block_rsv struct be able to capture freed space.
3632 * the captured space will re-add to the the block_rsv struct
3633 * after transaction commit
3634 */
3635void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3636 struct btrfs_block_rsv *block_rsv)
3637{
3638 block_rsv->durable = 1;
3639 mutex_lock(&fs_info->durable_block_rsv_mutex);
3640 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3641 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3642}
3643
3644int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3645 struct btrfs_root *root,
3646 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003647 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003648{
3649 int ret;
3650
3651 if (num_bytes == 0)
3652 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003653
3654 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003655 if (!ret) {
3656 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3657 return 0;
3658 }
3659
Yan, Zhengf0486c62010-05-16 10:46:25 -04003660 return ret;
3661}
3662
3663int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3664 struct btrfs_root *root,
3665 struct btrfs_block_rsv *block_rsv,
3666 u64 min_reserved, int min_factor)
3667{
3668 u64 num_bytes = 0;
3669 int commit_trans = 0;
3670 int ret = -ENOSPC;
3671
3672 if (!block_rsv)
3673 return 0;
3674
3675 spin_lock(&block_rsv->lock);
3676 if (min_factor > 0)
3677 num_bytes = div_factor(block_rsv->size, min_factor);
3678 if (min_reserved > num_bytes)
3679 num_bytes = min_reserved;
3680
3681 if (block_rsv->reserved >= num_bytes) {
3682 ret = 0;
3683 } else {
3684 num_bytes -= block_rsv->reserved;
3685 if (block_rsv->durable &&
3686 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3687 commit_trans = 1;
3688 }
3689 spin_unlock(&block_rsv->lock);
3690 if (!ret)
3691 return 0;
3692
3693 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003694 ret = reserve_metadata_bytes(trans, root, block_rsv,
3695 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003696 if (!ret) {
3697 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3698 return 0;
3699 }
3700 }
3701
3702 if (commit_trans) {
3703 if (trans)
3704 return -EAGAIN;
3705
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003706 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003707 BUG_ON(IS_ERR(trans));
3708 ret = btrfs_commit_transaction(trans, root);
3709 return 0;
3710 }
3711
Yan, Zhengf0486c62010-05-16 10:46:25 -04003712 return -ENOSPC;
3713}
3714
3715int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3716 struct btrfs_block_rsv *dst_rsv,
3717 u64 num_bytes)
3718{
3719 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3720}
3721
3722void btrfs_block_rsv_release(struct btrfs_root *root,
3723 struct btrfs_block_rsv *block_rsv,
3724 u64 num_bytes)
3725{
3726 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3727 if (global_rsv->full || global_rsv == block_rsv ||
3728 block_rsv->space_info != global_rsv->space_info)
3729 global_rsv = NULL;
3730 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3731}
3732
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003733/*
3734 * helper to calculate size of global block reservation.
3735 * the desired value is sum of space used by extent tree,
3736 * checksum tree and root tree
3737 */
3738static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3739{
3740 struct btrfs_space_info *sinfo;
3741 u64 num_bytes;
3742 u64 meta_used;
3743 u64 data_used;
3744 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003745
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003746 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3747 spin_lock(&sinfo->lock);
3748 data_used = sinfo->bytes_used;
3749 spin_unlock(&sinfo->lock);
3750
3751 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3752 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003753 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3754 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003755 meta_used = sinfo->bytes_used;
3756 spin_unlock(&sinfo->lock);
3757
3758 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3759 csum_size * 2;
3760 num_bytes += div64_u64(data_used + meta_used, 50);
3761
3762 if (num_bytes * 3 > meta_used)
3763 num_bytes = div64_u64(meta_used, 3);
3764
3765 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3766}
3767
3768static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3769{
3770 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3771 struct btrfs_space_info *sinfo = block_rsv->space_info;
3772 u64 num_bytes;
3773
3774 num_bytes = calc_global_metadata_size(fs_info);
3775
3776 spin_lock(&block_rsv->lock);
3777 spin_lock(&sinfo->lock);
3778
3779 block_rsv->size = num_bytes;
3780
3781 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003782 sinfo->bytes_reserved + sinfo->bytes_readonly +
3783 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003784
3785 if (sinfo->total_bytes > num_bytes) {
3786 num_bytes = sinfo->total_bytes - num_bytes;
3787 block_rsv->reserved += num_bytes;
3788 sinfo->bytes_reserved += num_bytes;
3789 }
3790
3791 if (block_rsv->reserved >= block_rsv->size) {
3792 num_bytes = block_rsv->reserved - block_rsv->size;
3793 sinfo->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003794 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003795 block_rsv->reserved = block_rsv->size;
3796 block_rsv->full = 1;
3797 }
David Sterba182608c2011-05-05 13:13:16 +02003798
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003799 spin_unlock(&sinfo->lock);
3800 spin_unlock(&block_rsv->lock);
3801}
3802
Yan, Zhengf0486c62010-05-16 10:46:25 -04003803static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3804{
3805 struct btrfs_space_info *space_info;
3806
3807 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3808 fs_info->chunk_block_rsv.space_info = space_info;
3809 fs_info->chunk_block_rsv.priority = 10;
3810
3811 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003812 fs_info->global_block_rsv.space_info = space_info;
3813 fs_info->global_block_rsv.priority = 10;
3814 fs_info->global_block_rsv.refill_used = 1;
3815 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003816 fs_info->trans_block_rsv.space_info = space_info;
3817 fs_info->empty_block_rsv.space_info = space_info;
3818 fs_info->empty_block_rsv.priority = 10;
3819
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003820 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3821 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3822 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3823 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003824 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003825
3826 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3827
3828 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3829
3830 update_global_block_rsv(fs_info);
3831}
3832
3833static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3834{
3835 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3836 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3837 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3838 WARN_ON(fs_info->trans_block_rsv.size > 0);
3839 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3840 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3841 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003842}
3843
Josef Bacikfcb80c22011-05-03 10:40:22 -04003844int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans,
3845 struct btrfs_root *root,
3846 struct btrfs_block_rsv *rsv)
3847{
3848 struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv;
3849 u64 num_bytes;
3850 int ret;
3851
3852 /*
3853 * Truncate should be freeing data, but give us 2 items just in case it
3854 * needs to use some space. We may want to be smarter about this in the
3855 * future.
3856 */
Chris Masonff5714c2011-05-28 07:00:39 -04003857 num_bytes = btrfs_calc_trans_metadata_size(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04003858
3859 /* We already have enough bytes, just return */
3860 if (rsv->reserved >= num_bytes)
3861 return 0;
3862
3863 num_bytes -= rsv->reserved;
3864
3865 /*
3866 * You should have reserved enough space before hand to do this, so this
3867 * should not fail.
3868 */
3869 ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes);
3870 BUG_ON(ret);
3871
3872 return 0;
3873}
3874
Yan, Zhenga22285a2010-05-16 10:48:46 -04003875int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3876 struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003877 int num_items)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003878{
3879 u64 num_bytes;
3880 int ret;
3881
3882 if (num_items == 0 || root->fs_info->chunk_root == root)
3883 return 0;
3884
Miao Xie16cdcec2011-04-22 18:12:22 +08003885 num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003886 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003887 num_bytes);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003888 if (!ret) {
3889 trans->bytes_reserved += num_bytes;
3890 trans->block_rsv = &root->fs_info->trans_block_rsv;
3891 }
3892 return ret;
3893}
3894
3895void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3896 struct btrfs_root *root)
3897{
3898 if (!trans->bytes_reserved)
3899 return;
3900
3901 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3902 btrfs_block_rsv_release(root, trans->block_rsv,
3903 trans->bytes_reserved);
3904 trans->bytes_reserved = 0;
3905}
3906
Yan, Zhengd68fc572010-05-16 10:49:58 -04003907int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3908 struct inode *inode)
3909{
3910 struct btrfs_root *root = BTRFS_I(inode)->root;
3911 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3912 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3913
3914 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04003915 * We need to hold space in order to delete our orphan item once we've
3916 * added it, so this takes the reservation so we can release it later
3917 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003918 */
Chris Masonff5714c2011-05-28 07:00:39 -04003919 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003920 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3921}
3922
3923void btrfs_orphan_release_metadata(struct inode *inode)
3924{
3925 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04003926 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003927 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3928}
3929
Yan, Zhenga22285a2010-05-16 10:48:46 -04003930int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3931 struct btrfs_pending_snapshot *pending)
3932{
3933 struct btrfs_root *root = pending->root;
3934 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3935 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3936 /*
3937 * two for root back/forward refs, two for directory entries
3938 * and one for root of the snapshot.
3939 */
Miao Xie16cdcec2011-04-22 18:12:22 +08003940 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003941 dst_rsv->space_info = src_rsv->space_info;
3942 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3943}
3944
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003945static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3946{
3947 return num_bytes >>= 3;
3948}
3949
3950int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3951{
3952 struct btrfs_root *root = BTRFS_I(inode)->root;
3953 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3954 u64 to_reserve;
3955 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05003956 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003957 int ret;
3958
3959 if (btrfs_transaction_in_commit(root->fs_info))
3960 schedule_timeout(1);
3961
3962 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003963
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003964 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
Josef Bacik57a45ced2011-01-25 16:30:38 -05003965 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
3966
3967 if (nr_extents > reserved_extents) {
3968 nr_extents -= reserved_extents;
Miao Xie16cdcec2011-04-22 18:12:22 +08003969 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003970 } else {
3971 nr_extents = 0;
3972 to_reserve = 0;
3973 }
Josef Bacik57a45ced2011-01-25 16:30:38 -05003974
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003975 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003976 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
3977 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003978 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003979
Josef Bacik57a45ced2011-01-25 16:30:38 -05003980 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003981 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003982
3983 block_rsv_add_bytes(block_rsv, to_reserve, 1);
3984
3985 if (block_rsv->size > 512 * 1024 * 1024)
Josef Bacik0019f102010-10-15 15:18:40 -04003986 shrink_delalloc(NULL, root, to_reserve, 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003987
3988 return 0;
3989}
3990
3991void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
3992{
3993 struct btrfs_root *root = BTRFS_I(inode)->root;
3994 u64 to_free;
3995 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05003996 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003997
3998 num_bytes = ALIGN(num_bytes, root->sectorsize);
3999 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik3c148742011-02-02 15:53:47 +00004000 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004001
Josef Bacik57a45ced2011-01-25 16:30:38 -05004002 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4003 do {
4004 int old, new;
4005
4006 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4007 if (nr_extents >= reserved_extents) {
4008 nr_extents = 0;
4009 break;
4010 }
4011 old = reserved_extents;
4012 nr_extents = reserved_extents - nr_extents;
4013 new = reserved_extents - nr_extents;
4014 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
4015 reserved_extents, new);
4016 if (likely(old == reserved_extents))
4017 break;
4018 reserved_extents = old;
4019 } while (1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004020
4021 to_free = calc_csum_metadata_size(inode, num_bytes);
4022 if (nr_extents > 0)
Miao Xie16cdcec2011-04-22 18:12:22 +08004023 to_free += btrfs_calc_trans_metadata_size(root, nr_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004024
4025 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4026 to_free);
4027}
4028
4029int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4030{
4031 int ret;
4032
4033 ret = btrfs_check_data_free_space(inode, num_bytes);
4034 if (ret)
4035 return ret;
4036
4037 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4038 if (ret) {
4039 btrfs_free_reserved_data_space(inode, num_bytes);
4040 return ret;
4041 }
4042
4043 return 0;
4044}
4045
4046void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4047{
4048 btrfs_delalloc_release_metadata(inode, num_bytes);
4049 btrfs_free_reserved_data_space(inode, num_bytes);
4050}
4051
Chris Mason9078a3e2007-04-26 16:46:15 -04004052static int update_block_group(struct btrfs_trans_handle *trans,
4053 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004054 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004055{
Josef Bacik0af3d002010-06-21 14:48:16 -04004056 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004057 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004058 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004059 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004060 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004061 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004062
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004063 /* block accounting for super block */
4064 spin_lock(&info->delalloc_lock);
4065 old_val = btrfs_super_bytes_used(&info->super_copy);
4066 if (alloc)
4067 old_val += num_bytes;
4068 else
4069 old_val -= num_bytes;
4070 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004071 spin_unlock(&info->delalloc_lock);
4072
Chris Masond3977122009-01-05 21:25:51 -05004073 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004074 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004075 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004076 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004077 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4078 BTRFS_BLOCK_GROUP_RAID1 |
4079 BTRFS_BLOCK_GROUP_RAID10))
4080 factor = 2;
4081 else
4082 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004083 /*
4084 * If this block group has free space cache written out, we
4085 * need to make sure to load it if we are removing space. This
4086 * is because we need the unpinning stage to actually add the
4087 * space back to the block group, otherwise we will leak space.
4088 */
4089 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004090 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004091
Chris Masondb945352007-10-15 16:15:53 -04004092 byte_in_group = bytenr - cache->key.objectid;
4093 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004094
Josef Bacik25179202008-10-29 14:49:05 -04004095 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004096 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004097
4098 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4099 cache->disk_cache_state < BTRFS_DC_CLEAR)
4100 cache->disk_cache_state = BTRFS_DC_CLEAR;
4101
Josef Bacik0f9dd462008-09-23 13:14:11 -04004102 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004103 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004104 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004105 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004106 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004107 btrfs_set_block_group_used(&cache->item, old_val);
4108 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004109 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004110 cache->space_info->reservation_progress++;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004111 cache->space_info->bytes_used += num_bytes;
4112 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004113 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004114 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004115 } else {
Chris Masondb945352007-10-15 16:15:53 -04004116 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004117 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004118 cache->pinned += num_bytes;
4119 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004120 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004121 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004122 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004123 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004124
Yan, Zhengf0486c62010-05-16 10:46:25 -04004125 set_extent_dirty(info->pinned_extents,
4126 bytenr, bytenr + num_bytes - 1,
4127 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004128 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004129 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004130 total -= num_bytes;
4131 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004132 }
4133 return 0;
4134}
Chris Mason6324fbf2008-03-24 15:01:59 -04004135
Chris Masona061fc82008-05-07 11:43:44 -04004136static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4137{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004138 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004139 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004140
4141 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4142 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004143 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004144
Yan Zhengd2fb3432008-12-11 16:30:39 -05004145 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004146 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004147
4148 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004149}
4150
Yan, Zhengf0486c62010-05-16 10:46:25 -04004151static int pin_down_extent(struct btrfs_root *root,
4152 struct btrfs_block_group_cache *cache,
4153 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004154{
Yan Zheng11833d62009-09-11 16:11:19 -04004155 spin_lock(&cache->space_info->lock);
4156 spin_lock(&cache->lock);
4157 cache->pinned += num_bytes;
4158 cache->space_info->bytes_pinned += num_bytes;
4159 if (reserved) {
4160 cache->reserved -= num_bytes;
4161 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004162 cache->space_info->reservation_progress++;
Yan324ae4d2007-11-16 14:57:08 -05004163 }
Yan Zheng11833d62009-09-11 16:11:19 -04004164 spin_unlock(&cache->lock);
4165 spin_unlock(&cache->space_info->lock);
4166
Yan, Zhengf0486c62010-05-16 10:46:25 -04004167 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4168 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004169 return 0;
4170}
Chris Mason9078a3e2007-04-26 16:46:15 -04004171
Yan, Zhengf0486c62010-05-16 10:46:25 -04004172/*
4173 * this function must be called within transaction
4174 */
4175int btrfs_pin_extent(struct btrfs_root *root,
4176 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004177{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004178 struct btrfs_block_group_cache *cache;
4179
4180 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4181 BUG_ON(!cache);
4182
4183 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4184
4185 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004186 return 0;
4187}
Zheng Yane8569812008-09-26 10:05:48 -04004188
Yan, Zhengf0486c62010-05-16 10:46:25 -04004189/*
4190 * update size of reserved extents. this function may return -EAGAIN
4191 * if 'reserve' is true or 'sinfo' is false.
4192 */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004193int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4194 u64 num_bytes, int reserve, int sinfo)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004195{
4196 int ret = 0;
4197 if (sinfo) {
4198 struct btrfs_space_info *space_info = cache->space_info;
4199 spin_lock(&space_info->lock);
4200 spin_lock(&cache->lock);
4201 if (reserve) {
4202 if (cache->ro) {
4203 ret = -EAGAIN;
4204 } else {
4205 cache->reserved += num_bytes;
4206 space_info->bytes_reserved += num_bytes;
4207 }
4208 } else {
4209 if (cache->ro)
4210 space_info->bytes_readonly += num_bytes;
4211 cache->reserved -= num_bytes;
4212 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004213 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004214 }
4215 spin_unlock(&cache->lock);
4216 spin_unlock(&space_info->lock);
4217 } else {
4218 spin_lock(&cache->lock);
4219 if (cache->ro) {
4220 ret = -EAGAIN;
4221 } else {
4222 if (reserve)
4223 cache->reserved += num_bytes;
4224 else
4225 cache->reserved -= num_bytes;
4226 }
4227 spin_unlock(&cache->lock);
4228 }
4229 return ret;
4230}
4231
Yan Zheng11833d62009-09-11 16:11:19 -04004232int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4233 struct btrfs_root *root)
4234{
4235 struct btrfs_fs_info *fs_info = root->fs_info;
4236 struct btrfs_caching_control *next;
4237 struct btrfs_caching_control *caching_ctl;
4238 struct btrfs_block_group_cache *cache;
4239
4240 down_write(&fs_info->extent_commit_sem);
4241
4242 list_for_each_entry_safe(caching_ctl, next,
4243 &fs_info->caching_block_groups, list) {
4244 cache = caching_ctl->block_group;
4245 if (block_group_cache_done(cache)) {
4246 cache->last_byte_to_unpin = (u64)-1;
4247 list_del_init(&caching_ctl->list);
4248 put_caching_control(caching_ctl);
4249 } else {
4250 cache->last_byte_to_unpin = caching_ctl->progress;
4251 }
4252 }
4253
4254 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4255 fs_info->pinned_extents = &fs_info->freed_extents[1];
4256 else
4257 fs_info->pinned_extents = &fs_info->freed_extents[0];
4258
4259 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004260
4261 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004262 return 0;
4263}
4264
4265static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4266{
4267 struct btrfs_fs_info *fs_info = root->fs_info;
4268 struct btrfs_block_group_cache *cache = NULL;
4269 u64 len;
4270
4271 while (start <= end) {
4272 if (!cache ||
4273 start >= cache->key.objectid + cache->key.offset) {
4274 if (cache)
4275 btrfs_put_block_group(cache);
4276 cache = btrfs_lookup_block_group(fs_info, start);
4277 BUG_ON(!cache);
4278 }
4279
4280 len = cache->key.objectid + cache->key.offset - start;
4281 len = min(len, end + 1 - start);
4282
4283 if (start < cache->last_byte_to_unpin) {
4284 len = min(len, cache->last_byte_to_unpin - start);
4285 btrfs_add_free_space(cache, start, len);
4286 }
Josef Bacik25179202008-10-29 14:49:05 -04004287
Yan, Zhengf0486c62010-05-16 10:46:25 -04004288 start += len;
4289
Josef Bacik25179202008-10-29 14:49:05 -04004290 spin_lock(&cache->space_info->lock);
4291 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004292 cache->pinned -= len;
4293 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004294 if (cache->ro) {
4295 cache->space_info->bytes_readonly += len;
4296 } else if (cache->reserved_pinned > 0) {
4297 len = min(len, cache->reserved_pinned);
4298 cache->reserved_pinned -= len;
4299 cache->space_info->bytes_reserved += len;
4300 }
Josef Bacik25179202008-10-29 14:49:05 -04004301 spin_unlock(&cache->lock);
4302 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004303 }
4304
4305 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004306 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004307 return 0;
4308}
4309
4310int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004311 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004312{
Yan Zheng11833d62009-09-11 16:11:19 -04004313 struct btrfs_fs_info *fs_info = root->fs_info;
4314 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004315 struct btrfs_block_rsv *block_rsv;
4316 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004317 u64 start;
4318 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004319 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004320 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004321
Yan Zheng11833d62009-09-11 16:11:19 -04004322 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4323 unpin = &fs_info->freed_extents[1];
4324 else
4325 unpin = &fs_info->freed_extents[0];
4326
Chris Masond3977122009-01-05 21:25:51 -05004327 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004328 ret = find_first_extent_bit(unpin, 0, &start, &end,
4329 EXTENT_DIRTY);
4330 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004331 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004332
Li Dongyang5378e602011-03-24 10:24:27 +00004333 if (btrfs_test_opt(root, DISCARD))
4334 ret = btrfs_discard_extent(root, start,
4335 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004336
Chris Mason1a5bc162007-10-15 16:15:26 -04004337 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004338 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004339 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004340 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004341
Yan, Zhengf0486c62010-05-16 10:46:25 -04004342 mutex_lock(&fs_info->durable_block_rsv_mutex);
4343 list_for_each_entry_safe(block_rsv, next_rsv,
4344 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004345
Yan, Zhengf0486c62010-05-16 10:46:25 -04004346 idx = trans->transid & 0x1;
4347 if (block_rsv->freed[idx] > 0) {
4348 block_rsv_add_bytes(block_rsv,
4349 block_rsv->freed[idx], 0);
4350 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004351 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004352 if (atomic_read(&block_rsv->usage) == 0) {
4353 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004354
Yan, Zhengf0486c62010-05-16 10:46:25 -04004355 if (block_rsv->freed[0] == 0 &&
4356 block_rsv->freed[1] == 0) {
4357 list_del_init(&block_rsv->list);
4358 kfree(block_rsv);
4359 }
4360 } else {
4361 btrfs_block_rsv_release(root, block_rsv, 0);
4362 }
4363 }
4364 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4365
Chris Masone20d96d2007-03-22 12:13:20 -04004366 return 0;
4367}
4368
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004369static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4370 struct btrfs_root *root,
4371 u64 bytenr, u64 num_bytes, u64 parent,
4372 u64 root_objectid, u64 owner_objectid,
4373 u64 owner_offset, int refs_to_drop,
4374 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004375{
Chris Masone2fa7222007-03-12 16:22:34 -04004376 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004377 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004378 struct btrfs_fs_info *info = root->fs_info;
4379 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004380 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004381 struct btrfs_extent_item *ei;
4382 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004383 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004384 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004385 int extent_slot = 0;
4386 int found_extent = 0;
4387 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004388 u32 item_size;
4389 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004390
Chris Mason5caf2a02007-04-02 11:20:42 -04004391 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004392 if (!path)
4393 return -ENOMEM;
4394
Chris Mason3c12ac72008-04-21 12:01:38 -04004395 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004396 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004397
4398 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4399 BUG_ON(!is_data && refs_to_drop != 1);
4400
4401 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4402 bytenr, num_bytes, parent,
4403 root_objectid, owner_objectid,
4404 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004405 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004406 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004407 while (extent_slot >= 0) {
4408 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004409 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004410 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004411 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004412 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4413 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004414 found_extent = 1;
4415 break;
4416 }
4417 if (path->slots[0] - extent_slot > 5)
4418 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004419 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004420 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004421#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4422 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4423 if (found_extent && item_size < sizeof(*ei))
4424 found_extent = 0;
4425#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004426 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004427 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004428 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004429 NULL, refs_to_drop,
4430 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004431 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004432 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04004433 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004434
4435 key.objectid = bytenr;
4436 key.type = BTRFS_EXTENT_ITEM_KEY;
4437 key.offset = num_bytes;
4438
Zheng Yan31840ae2008-09-23 13:14:14 -04004439 ret = btrfs_search_slot(trans, extent_root,
4440 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004441 if (ret) {
4442 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004443 ", was looking for %llu\n", ret,
4444 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004445 btrfs_print_leaf(extent_root, path->nodes[0]);
4446 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004447 BUG_ON(ret);
4448 extent_slot = path->slots[0];
4449 }
Chris Mason7bb86312007-12-11 09:25:06 -05004450 } else {
4451 btrfs_print_leaf(extent_root, path->nodes[0]);
4452 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004453 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004454 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004455 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004456 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004457 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004458 (unsigned long long)owner_objectid,
4459 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004460 }
Chris Mason5f39d392007-10-15 16:14:19 -04004461
4462 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004463 item_size = btrfs_item_size_nr(leaf, extent_slot);
4464#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4465 if (item_size < sizeof(*ei)) {
4466 BUG_ON(found_extent || extent_slot != path->slots[0]);
4467 ret = convert_extent_item_v0(trans, extent_root, path,
4468 owner_objectid, 0);
4469 BUG_ON(ret < 0);
4470
David Sterbab3b4aa72011-04-21 01:20:15 +02004471 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004472 path->leave_spinning = 1;
4473
4474 key.objectid = bytenr;
4475 key.type = BTRFS_EXTENT_ITEM_KEY;
4476 key.offset = num_bytes;
4477
4478 ret = btrfs_search_slot(trans, extent_root, &key, path,
4479 -1, 1);
4480 if (ret) {
4481 printk(KERN_ERR "umm, got %d back from search"
4482 ", was looking for %llu\n", ret,
4483 (unsigned long long)bytenr);
4484 btrfs_print_leaf(extent_root, path->nodes[0]);
4485 }
4486 BUG_ON(ret);
4487 extent_slot = path->slots[0];
4488 leaf = path->nodes[0];
4489 item_size = btrfs_item_size_nr(leaf, extent_slot);
4490 }
4491#endif
4492 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004493 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004494 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004495 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4496 struct btrfs_tree_block_info *bi;
4497 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4498 bi = (struct btrfs_tree_block_info *)(ei + 1);
4499 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004500 }
4501
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004502 refs = btrfs_extent_refs(leaf, ei);
4503 BUG_ON(refs < refs_to_drop);
4504 refs -= refs_to_drop;
4505
4506 if (refs > 0) {
4507 if (extent_op)
4508 __run_delayed_extent_op(extent_op, leaf, ei);
4509 /*
4510 * In the case of inline back ref, reference count will
4511 * be updated by remove_extent_backref
4512 */
4513 if (iref) {
4514 BUG_ON(!found_extent);
4515 } else {
4516 btrfs_set_extent_refs(leaf, ei, refs);
4517 btrfs_mark_buffer_dirty(leaf);
4518 }
4519 if (found_extent) {
4520 ret = remove_extent_backref(trans, extent_root, path,
4521 iref, refs_to_drop,
4522 is_data);
4523 BUG_ON(ret);
4524 }
4525 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004526 if (found_extent) {
4527 BUG_ON(is_data && refs_to_drop !=
4528 extent_data_ref_count(root, path, iref));
4529 if (iref) {
4530 BUG_ON(path->slots[0] != extent_slot);
4531 } else {
4532 BUG_ON(path->slots[0] != extent_slot + 1);
4533 path->slots[0] = extent_slot;
4534 num_to_del = 2;
4535 }
Chris Mason78fae272007-03-25 11:35:08 -04004536 }
Chris Masonb9473432009-03-13 11:00:37 -04004537
Chris Mason952fcca2008-02-18 16:33:44 -05004538 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4539 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004540 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004541 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01004542
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004543 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004544 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4545 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004546 } else {
4547 invalidate_mapping_pages(info->btree_inode->i_mapping,
4548 bytenr >> PAGE_CACHE_SHIFT,
4549 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004550 }
4551
Yan, Zhengf0486c62010-05-16 10:46:25 -04004552 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004553 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004554 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004555 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004556 return ret;
4557}
4558
4559/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004560 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004561 * delayed ref for that extent as well. This searches the delayed ref tree for
4562 * a given extent, and if there are no other delayed refs to be processed, it
4563 * removes it from the tree.
4564 */
4565static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4566 struct btrfs_root *root, u64 bytenr)
4567{
4568 struct btrfs_delayed_ref_head *head;
4569 struct btrfs_delayed_ref_root *delayed_refs;
4570 struct btrfs_delayed_ref_node *ref;
4571 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004572 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004573
4574 delayed_refs = &trans->transaction->delayed_refs;
4575 spin_lock(&delayed_refs->lock);
4576 head = btrfs_find_delayed_ref_head(trans, bytenr);
4577 if (!head)
4578 goto out;
4579
4580 node = rb_prev(&head->node.rb_node);
4581 if (!node)
4582 goto out;
4583
4584 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4585
4586 /* there are still entries for this ref, we can't drop it */
4587 if (ref->bytenr == bytenr)
4588 goto out;
4589
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004590 if (head->extent_op) {
4591 if (!head->must_insert_reserved)
4592 goto out;
4593 kfree(head->extent_op);
4594 head->extent_op = NULL;
4595 }
4596
Chris Mason1887be62009-03-13 10:11:24 -04004597 /*
4598 * waiting for the lock here would deadlock. If someone else has it
4599 * locked they are already in the process of dropping it anyway
4600 */
4601 if (!mutex_trylock(&head->mutex))
4602 goto out;
4603
4604 /*
4605 * at this point we have a head with no other entries. Go
4606 * ahead and process it.
4607 */
4608 head->node.in_tree = 0;
4609 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004610
Chris Mason1887be62009-03-13 10:11:24 -04004611 delayed_refs->num_entries--;
4612
4613 /*
4614 * we don't take a ref on the node because we're removing it from the
4615 * tree, so we just steal the ref the tree was holding.
4616 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004617 delayed_refs->num_heads--;
4618 if (list_empty(&head->cluster))
4619 delayed_refs->num_heads_ready--;
4620
4621 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004622 spin_unlock(&delayed_refs->lock);
4623
Yan, Zhengf0486c62010-05-16 10:46:25 -04004624 BUG_ON(head->extent_op);
4625 if (head->must_insert_reserved)
4626 ret = 1;
4627
4628 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004629 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004630 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004631out:
4632 spin_unlock(&delayed_refs->lock);
4633 return 0;
4634}
4635
Yan, Zhengf0486c62010-05-16 10:46:25 -04004636void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4637 struct btrfs_root *root,
4638 struct extent_buffer *buf,
4639 u64 parent, int last_ref)
4640{
4641 struct btrfs_block_rsv *block_rsv;
4642 struct btrfs_block_group_cache *cache = NULL;
4643 int ret;
4644
4645 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4646 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4647 parent, root->root_key.objectid,
4648 btrfs_header_level(buf),
4649 BTRFS_DROP_DELAYED_REF, NULL);
4650 BUG_ON(ret);
4651 }
4652
4653 if (!last_ref)
4654 return;
4655
4656 block_rsv = get_block_rsv(trans, root);
4657 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004658 if (block_rsv->space_info != cache->space_info)
4659 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004660
4661 if (btrfs_header_generation(buf) == trans->transid) {
4662 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4663 ret = check_ref_cleanup(trans, root, buf->start);
4664 if (!ret)
4665 goto pin;
4666 }
4667
4668 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4669 pin_down_extent(root, cache, buf->start, buf->len, 1);
4670 goto pin;
4671 }
4672
4673 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4674
4675 btrfs_add_free_space(cache, buf->start, buf->len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00004676 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004677 if (ret == -EAGAIN) {
4678 /* block group became read-only */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004679 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004680 goto out;
4681 }
4682
4683 ret = 1;
4684 spin_lock(&block_rsv->lock);
4685 if (block_rsv->reserved < block_rsv->size) {
4686 block_rsv->reserved += buf->len;
4687 ret = 0;
4688 }
4689 spin_unlock(&block_rsv->lock);
4690
4691 if (ret) {
4692 spin_lock(&cache->space_info->lock);
4693 cache->space_info->bytes_reserved -= buf->len;
Chris Mason36e39c42011-03-12 07:08:42 -05004694 cache->space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004695 spin_unlock(&cache->space_info->lock);
4696 }
4697 goto out;
4698 }
4699pin:
4700 if (block_rsv->durable && !cache->ro) {
4701 ret = 0;
4702 spin_lock(&cache->lock);
4703 if (!cache->ro) {
4704 cache->reserved_pinned += buf->len;
4705 ret = 1;
4706 }
4707 spin_unlock(&cache->lock);
4708
4709 if (ret) {
4710 spin_lock(&block_rsv->lock);
4711 block_rsv->freed[trans->transid & 0x1] += buf->len;
4712 spin_unlock(&block_rsv->lock);
4713 }
4714 }
4715out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004716 /*
4717 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4718 * anymore.
4719 */
4720 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004721 btrfs_put_block_group(cache);
4722}
4723
Chris Mason925baed2008-06-25 16:01:30 -04004724int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004725 struct btrfs_root *root,
4726 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004727 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004728{
4729 int ret;
4730
Chris Mason56bec292009-03-13 10:10:06 -04004731 /*
4732 * tree log blocks never actually go into the extent allocation
4733 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004734 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004735 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4736 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004737 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004738 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004739 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004740 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4741 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4742 parent, root_objectid, (int)owner,
4743 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004744 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004745 } else {
4746 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4747 parent, root_objectid, owner,
4748 offset, BTRFS_DROP_DELAYED_REF, NULL);
4749 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004750 }
Chris Mason925baed2008-06-25 16:01:30 -04004751 return ret;
4752}
4753
Chris Mason87ee04e2007-11-30 11:30:34 -05004754static u64 stripe_align(struct btrfs_root *root, u64 val)
4755{
4756 u64 mask = ((u64)root->stripesize - 1);
4757 u64 ret = (val + mask) & ~mask;
4758 return ret;
4759}
4760
Chris Masonfec577f2007-02-26 10:40:21 -05004761/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004762 * when we wait for progress in the block group caching, its because
4763 * our allocation attempt failed at least once. So, we must sleep
4764 * and let some progress happen before we try again.
4765 *
4766 * This function will sleep at least once waiting for new free space to
4767 * show up, and then it will check the block group free space numbers
4768 * for our min num_bytes. Another option is to have it go ahead
4769 * and look in the rbtree for a free extent of a given size, but this
4770 * is a good start.
4771 */
4772static noinline int
4773wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4774 u64 num_bytes)
4775{
Yan Zheng11833d62009-09-11 16:11:19 -04004776 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004777 DEFINE_WAIT(wait);
4778
Yan Zheng11833d62009-09-11 16:11:19 -04004779 caching_ctl = get_caching_control(cache);
4780 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004781 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004782
Yan Zheng11833d62009-09-11 16:11:19 -04004783 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08004784 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004785
4786 put_caching_control(caching_ctl);
4787 return 0;
4788}
4789
4790static noinline int
4791wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4792{
4793 struct btrfs_caching_control *caching_ctl;
4794 DEFINE_WAIT(wait);
4795
4796 caching_ctl = get_caching_control(cache);
4797 if (!caching_ctl)
4798 return 0;
4799
4800 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4801
4802 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004803 return 0;
4804}
4805
Yan, Zhengb742bb82010-05-16 10:46:24 -04004806static int get_block_group_index(struct btrfs_block_group_cache *cache)
4807{
4808 int index;
4809 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4810 index = 0;
4811 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4812 index = 1;
4813 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4814 index = 2;
4815 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4816 index = 3;
4817 else
4818 index = 4;
4819 return index;
4820}
4821
Josef Bacik817d52f2009-07-13 21:29:25 -04004822enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004823 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004824 LOOP_CACHING_NOWAIT = 1,
4825 LOOP_CACHING_WAIT = 2,
4826 LOOP_ALLOC_CHUNK = 3,
4827 LOOP_NO_EMPTY_SIZE = 4,
4828};
4829
4830/*
Chris Masonfec577f2007-02-26 10:40:21 -05004831 * walks the btree of allocated extents and find a hole of a given size.
4832 * The key ins is changed to record the hole:
4833 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004834 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004835 * ins->offset == number of blocks
4836 * Any available blocks before search_start are skipped.
4837 */
Chris Masond3977122009-01-05 21:25:51 -05004838static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004839 struct btrfs_root *orig_root,
4840 u64 num_bytes, u64 empty_size,
4841 u64 search_start, u64 search_end,
4842 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004843 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004844{
Josef Bacik80eb2342008-10-29 14:49:05 -04004845 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004846 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004847 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004848 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004849 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004850 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004851 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004852 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004853 int last_ptr_loop = 0;
4854 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004855 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004856 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004857 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004858 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004859 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004860 u64 ideal_cache_percent = 0;
4861 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004862
Chris Masondb945352007-10-15 16:15:53 -04004863 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004864 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004865 ins->objectid = 0;
4866 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004867
Josef Bacik2552d172009-04-03 10:14:19 -04004868 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004869 if (!space_info) {
4870 printk(KERN_ERR "No space info for %d\n", data);
4871 return -ENOSPC;
4872 }
Josef Bacik2552d172009-04-03 10:14:19 -04004873
Josef Bacik67377732010-09-16 16:19:09 -04004874 /*
4875 * If the space info is for both data and metadata it means we have a
4876 * small filesystem and we can't use the clustering stuff.
4877 */
4878 if (btrfs_mixed_space_info(space_info))
4879 use_cluster = false;
4880
Chris Mason0ef3e662008-05-24 14:04:53 -04004881 if (orig_root->ref_cows || empty_size)
4882 allowed_chunk_alloc = 1;
4883
Josef Bacik67377732010-09-16 16:19:09 -04004884 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004885 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004886 if (!btrfs_test_opt(root, SSD))
4887 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004888 }
4889
Josef Bacik67377732010-09-16 16:19:09 -04004890 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4891 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004892 last_ptr = &root->fs_info->data_alloc_cluster;
4893 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004894
Chris Mason239b14b2008-03-24 15:02:07 -04004895 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004896 spin_lock(&last_ptr->lock);
4897 if (last_ptr->block_group)
4898 hint_byte = last_ptr->window_start;
4899 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004900 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004901
Chris Masona061fc82008-05-07 11:43:44 -04004902 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004903 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004904
Josef Bacik817d52f2009-07-13 21:29:25 -04004905 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004906 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004907
Josef Bacik2552d172009-04-03 10:14:19 -04004908 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004909ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004910 block_group = btrfs_lookup_block_group(root->fs_info,
4911 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004912 /*
4913 * we don't want to use the block group if it doesn't match our
4914 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004915 *
4916 * However if we are re-searching with an ideal block group
4917 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004918 */
4919 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004920 (block_group->cached != BTRFS_CACHE_NO ||
4921 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004922 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004923 if (list_empty(&block_group->list) ||
4924 block_group->ro) {
4925 /*
4926 * someone is removing this block group,
4927 * we can't jump into the have_block_group
4928 * target because our list pointers are not
4929 * valid
4930 */
4931 btrfs_put_block_group(block_group);
4932 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004933 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004934 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004935 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004936 }
Josef Bacik2552d172009-04-03 10:14:19 -04004937 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004938 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004939 }
Chris Mason42e70e72008-11-07 18:17:11 -05004940 }
Josef Bacik2552d172009-04-03 10:14:19 -04004941search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004942 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004943 list_for_each_entry(block_group, &space_info->block_groups[index],
4944 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004945 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004946 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004947
Josef Bacik11dfe352009-11-13 20:12:59 +00004948 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004949 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004950
Chris Mason83a50de2010-12-13 15:06:46 -05004951 /*
4952 * this can happen if we end up cycling through all the
4953 * raid types, but we want to make sure we only allocate
4954 * for the proper type.
4955 */
4956 if (!block_group_bits(block_group, data)) {
4957 u64 extra = BTRFS_BLOCK_GROUP_DUP |
4958 BTRFS_BLOCK_GROUP_RAID1 |
4959 BTRFS_BLOCK_GROUP_RAID10;
4960
4961 /*
4962 * if they asked for extra copies and this block group
4963 * doesn't provide them, bail. This does allow us to
4964 * fill raid0 from raid1.
4965 */
4966 if ((data & extra) && !(block_group->flags & extra))
4967 goto loop;
4968 }
4969
Josef Bacik2552d172009-04-03 10:14:19 -04004970have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004971 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004972 u64 free_percent;
4973
Josef Bacikb8399de2010-12-08 09:15:11 -05004974 ret = cache_block_group(block_group, trans,
4975 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04004976 if (block_group->cached == BTRFS_CACHE_FINISHED)
4977 goto have_block_group;
4978
Josef Bacikccf0e722009-11-10 21:23:48 -05004979 free_percent = btrfs_block_group_used(&block_group->item);
4980 free_percent *= 100;
4981 free_percent = div64_u64(free_percent,
4982 block_group->key.offset);
4983 free_percent = 100 - free_percent;
4984 if (free_percent > ideal_cache_percent &&
4985 likely(!block_group->ro)) {
4986 ideal_cache_offset = block_group->key.objectid;
4987 ideal_cache_percent = free_percent;
4988 }
4989
Josef Bacik817d52f2009-07-13 21:29:25 -04004990 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05004991 * We only want to start kthread caching if we are at
4992 * the point where we will wait for caching to make
4993 * progress, or if our ideal search is over and we've
4994 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04004995 */
4996 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05004997 (loop > LOOP_FIND_IDEAL &&
4998 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacikb8399de2010-12-08 09:15:11 -05004999 ret = cache_block_group(block_group, trans,
5000 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005001 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005002 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005003 found_uncached_bg = true;
5004
5005 /*
5006 * If loop is set for cached only, try the next block
5007 * group.
5008 */
5009 if (loop == LOOP_FIND_IDEAL)
5010 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005011 }
5012
Josef Bacik817d52f2009-07-13 21:29:25 -04005013 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005014 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005015 found_uncached_bg = true;
5016
Josef Bacikea6a4782008-11-20 12:16:16 -05005017 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005018 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005019
Chris Masonff5714c2011-05-28 07:00:39 -04005020 spin_lock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005021 if (cached &&
Chris Masonff5714c2011-05-28 07:00:39 -04005022 block_group->free_space_ctl->free_space <
5023 num_bytes + empty_size) {
5024 spin_unlock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005025 goto loop;
5026 }
Chris Masonff5714c2011-05-28 07:00:39 -04005027 spin_unlock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005028
Josef Bacik0a243252009-09-11 16:11:20 -04005029 /*
5030 * Ok we want to try and use the cluster allocator, so lets look
5031 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5032 * have tried the cluster allocator plenty of times at this
5033 * point and not have found anything, so we are likely way too
5034 * fragmented for the clustering stuff to find anything, so lets
5035 * just skip it and let the allocator find whatever block it can
5036 * find
5037 */
5038 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005039 /*
5040 * the refill lock keeps out other
5041 * people trying to start a new cluster
5042 */
5043 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005044 if (last_ptr->block_group &&
5045 (last_ptr->block_group->ro ||
5046 !block_group_bits(last_ptr->block_group, data))) {
5047 offset = 0;
5048 goto refill_cluster;
5049 }
5050
Chris Masonfa9c0d72009-04-03 09:47:43 -04005051 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5052 num_bytes, search_start);
5053 if (offset) {
5054 /* we have a block, we're done */
5055 spin_unlock(&last_ptr->refill_lock);
5056 goto checks;
5057 }
5058
5059 spin_lock(&last_ptr->lock);
5060 /*
5061 * whoops, this cluster doesn't actually point to
5062 * this block group. Get a ref on the block
5063 * group is does point to and try again
5064 */
5065 if (!last_ptr_loop && last_ptr->block_group &&
5066 last_ptr->block_group != block_group) {
5067
5068 btrfs_put_block_group(block_group);
5069 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005070 btrfs_get_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005071 spin_unlock(&last_ptr->lock);
5072 spin_unlock(&last_ptr->refill_lock);
5073
5074 last_ptr_loop = 1;
5075 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005076 /*
5077 * we know this block group is properly
5078 * in the list because
5079 * btrfs_remove_block_group, drops the
5080 * cluster before it removes the block
5081 * group from the list
5082 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04005083 goto have_block_group;
5084 }
5085 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005086refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04005087 /*
5088 * this cluster didn't work out, free it and
5089 * start over
5090 */
5091 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5092
5093 last_ptr_loop = 0;
5094
5095 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005096 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005097 block_group, last_ptr,
5098 offset, num_bytes,
5099 empty_cluster + empty_size);
5100 if (ret == 0) {
5101 /*
5102 * now pull our allocation out of this
5103 * cluster
5104 */
5105 offset = btrfs_alloc_from_cluster(block_group,
5106 last_ptr, num_bytes,
5107 search_start);
5108 if (offset) {
5109 /* we found one, proceed */
5110 spin_unlock(&last_ptr->refill_lock);
5111 goto checks;
5112 }
Josef Bacik0a243252009-09-11 16:11:20 -04005113 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5114 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005115 spin_unlock(&last_ptr->refill_lock);
5116
Josef Bacik0a243252009-09-11 16:11:20 -04005117 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005118 wait_block_group_cache_progress(block_group,
5119 num_bytes + empty_cluster + empty_size);
5120 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005121 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005122
Chris Masonfa9c0d72009-04-03 09:47:43 -04005123 /*
5124 * at this point we either didn't find a cluster
5125 * or we weren't able to allocate a block from our
5126 * cluster. Free the cluster we've been trying
5127 * to use, and go to the next block group
5128 */
Josef Bacik0a243252009-09-11 16:11:20 -04005129 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005130 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005131 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005132 }
5133
Josef Bacik6226cb02009-04-03 10:14:18 -04005134 offset = btrfs_find_space_for_alloc(block_group, search_start,
5135 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005136 /*
5137 * If we didn't find a chunk, and we haven't failed on this
5138 * block group before, and this block group is in the middle of
5139 * caching and we are ok with waiting, then go ahead and wait
5140 * for progress to be made, and set failed_alloc to true.
5141 *
5142 * If failed_alloc is true then we've already waited on this
5143 * block group once and should move on to the next block group.
5144 */
5145 if (!offset && !failed_alloc && !cached &&
5146 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005147 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005148 num_bytes + empty_size);
5149 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005150 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005151 } else if (!offset) {
5152 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005153 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005154checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005155 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005156 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005157 if (search_start + num_bytes >= search_end) {
5158 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005159 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005160 }
Chris Masone37c9e62007-05-09 20:13:14 -04005161
Josef Bacik2552d172009-04-03 10:14:19 -04005162 /* move on to the next group */
5163 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005164 block_group->key.objectid + block_group->key.offset) {
5165 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005166 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005167 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005168
Josef Bacik2552d172009-04-03 10:14:19 -04005169 ins->objectid = search_start;
5170 ins->offset = num_bytes;
5171
Josef Bacik6226cb02009-04-03 10:14:18 -04005172 if (offset < search_start)
5173 btrfs_add_free_space(block_group, offset,
5174 search_start - offset);
5175 BUG_ON(offset > search_start);
5176
Li Dongyangb4d00d52011-03-24 10:24:25 +00005177 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005178 (data & BTRFS_BLOCK_GROUP_DATA));
5179 if (ret == -EAGAIN) {
5180 btrfs_add_free_space(block_group, offset, num_bytes);
5181 goto loop;
5182 }
Yan Zheng11833d62009-09-11 16:11:19 -04005183
Josef Bacik2552d172009-04-03 10:14:19 -04005184 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005185 ins->objectid = search_start;
5186 ins->offset = num_bytes;
5187
5188 if (offset < search_start)
5189 btrfs_add_free_space(block_group, offset,
5190 search_start - offset);
5191 BUG_ON(offset > search_start);
Josef Bacikd82a6f12011-05-11 15:26:06 -04005192 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005193 break;
5194loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005195 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005196 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005197 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d72009-04-03 09:47:43 -04005198 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005199 }
5200 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005201
Yan, Zhengb742bb82010-05-16 10:46:24 -04005202 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5203 goto search;
5204
Josef Bacikccf0e722009-11-10 21:23:48 -05005205 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5206 * for them to make caching progress. Also
5207 * determine the best possible bg to cache
5208 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5209 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005210 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5211 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5212 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5213 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04005214 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005215 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5216 (found_uncached_bg || empty_size || empty_cluster ||
5217 allowed_chunk_alloc)) {
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
5257 if (loop < LOOP_CACHING_WAIT) {
5258 loop++;
5259 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005260 }
5261
5262 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005263 empty_size = 0;
5264 empty_cluster = 0;
5265 }
Chris Mason42e70e72008-11-07 18:17:11 -05005266
Josef Bacik2552d172009-04-03 10:14:19 -04005267 if (allowed_chunk_alloc) {
5268 ret = do_chunk_alloc(trans, root, num_bytes +
Chris Mason0e4f8f82011-04-15 16:05:44 -04005269 2 * 1024 * 1024, data,
5270 CHUNK_ALLOC_LIMITED);
Josef Bacik2552d172009-04-03 10:14:19 -04005271 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005272 done_chunk_alloc = 1;
Chris Mason0e4f8f82011-04-15 16:05:44 -04005273 } else if (!done_chunk_alloc &&
5274 space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) {
5275 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005276 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005277
Josef Bacik817d52f2009-07-13 21:29:25 -04005278 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005279 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005280 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005281 }
Josef Bacik2552d172009-04-03 10:14:19 -04005282 ret = -ENOSPC;
5283 } else if (!ins->objectid) {
5284 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04005285 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005286 ret = 0;
5287 }
Chris Mason0b86a832008-03-24 15:01:56 -04005288
Chris Mason0f70abe2007-02-28 16:46:22 -05005289 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005290}
Chris Masonec44a352008-04-28 15:29:52 -04005291
Josef Bacik9ed74f22009-09-11 16:12:44 -04005292static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5293 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005294{
5295 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005296 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005297
Josef Bacik9ed74f22009-09-11 16:12:44 -04005298 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005299 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5300 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005301 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005302 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005303 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005304 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5305 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005306 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005307 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005308 (unsigned long long)info->bytes_pinned,
5309 (unsigned long long)info->bytes_reserved,
5310 (unsigned long long)info->bytes_may_use,
5311 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005312 spin_unlock(&info->lock);
5313
5314 if (!dump_block_groups)
5315 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005316
Josef Bacik80eb2342008-10-29 14:49:05 -04005317 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005318again:
5319 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005320 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005321 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5322 "%llu pinned %llu reserved\n",
5323 (unsigned long long)cache->key.objectid,
5324 (unsigned long long)cache->key.offset,
5325 (unsigned long long)btrfs_block_group_used(&cache->item),
5326 (unsigned long long)cache->pinned,
5327 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005328 btrfs_dump_free_space(cache, bytes);
5329 spin_unlock(&cache->lock);
5330 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005331 if (++index < BTRFS_NR_RAID_TYPES)
5332 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005333 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005334}
Zheng Yane8569812008-09-26 10:05:48 -04005335
Yan Zheng11833d62009-09-11 16:11:19 -04005336int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5337 struct btrfs_root *root,
5338 u64 num_bytes, u64 min_alloc_size,
5339 u64 empty_size, u64 hint_byte,
5340 u64 search_end, struct btrfs_key *ins,
5341 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005342{
5343 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005344 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005345
Josef Bacik6a632092009-02-20 11:00:09 -05005346 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005347again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005348 /*
5349 * the only place that sets empty_size is btrfs_realloc_node, which
5350 * is not called recursively on allocations
5351 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005352 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005353 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005354 num_bytes + 2 * 1024 * 1024, data,
5355 CHUNK_ALLOC_NO_FORCE);
Chris Mason0b86a832008-03-24 15:01:56 -04005356
Chris Masondb945352007-10-15 16:15:53 -04005357 WARN_ON(num_bytes < root->sectorsize);
5358 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005359 search_start, search_end, hint_byte,
5360 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005361
Chris Mason98d20f62008-04-14 09:46:10 -04005362 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5363 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005364 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005365 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005366 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005367 num_bytes, data, CHUNK_ALLOC_FORCE);
Chris Mason98d20f62008-04-14 09:46:10 -04005368 goto again;
5369 }
Chris Mason91435652011-02-16 13:10:41 -05005370 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005371 struct btrfs_space_info *sinfo;
5372
5373 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005374 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5375 "wanted %llu\n", (unsigned long long)data,
5376 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005377 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005378 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005379
liubo1abe9b82011-03-24 11:18:59 +00005380 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5381
Josef Bacik0f9dd462008-09-23 13:14:11 -04005382 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005383}
5384
Chris Mason65b51a02008-08-01 15:11:20 -04005385int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5386{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005387 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005388 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005389
Josef Bacik0f9dd462008-09-23 13:14:11 -04005390 cache = btrfs_lookup_block_group(root->fs_info, start);
5391 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005392 printk(KERN_ERR "Unable to find block group for %llu\n",
5393 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005394 return -ENOSPC;
5395 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005396
Li Dongyang5378e602011-03-24 10:24:27 +00005397 if (btrfs_test_opt(root, DISCARD))
5398 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005399
Josef Bacik0f9dd462008-09-23 13:14:11 -04005400 btrfs_add_free_space(cache, start, len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00005401 btrfs_update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005402 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005403
liubo1abe9b82011-03-24 11:18:59 +00005404 trace_btrfs_reserved_extent_free(root, start, len);
5405
Chris Masone6dcd2d2008-07-17 12:53:50 -04005406 return ret;
5407}
5408
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005409static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5410 struct btrfs_root *root,
5411 u64 parent, u64 root_objectid,
5412 u64 flags, u64 owner, u64 offset,
5413 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005414{
5415 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005416 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005417 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005418 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005419 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005420 struct extent_buffer *leaf;
5421 int type;
5422 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005423
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005424 if (parent > 0)
5425 type = BTRFS_SHARED_DATA_REF_KEY;
5426 else
5427 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005429 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005430
5431 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005432 if (!path)
5433 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005434
Chris Masonb9473432009-03-13 11:00:37 -04005435 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005436 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5437 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005438 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005439
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005440 leaf = path->nodes[0];
5441 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005442 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005443 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5444 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5445 btrfs_set_extent_flags(leaf, extent_item,
5446 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005447
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005448 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5449 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5450 if (parent > 0) {
5451 struct btrfs_shared_data_ref *ref;
5452 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5453 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5454 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5455 } else {
5456 struct btrfs_extent_data_ref *ref;
5457 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5458 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5459 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5460 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5461 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5462 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005463
5464 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005465 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005466
Yan, Zhengf0486c62010-05-16 10:46:25 -04005467 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005468 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005469 printk(KERN_ERR "btrfs update block group failed for %llu "
5470 "%llu\n", (unsigned long long)ins->objectid,
5471 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005472 BUG();
5473 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005474 return ret;
5475}
5476
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005477static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5478 struct btrfs_root *root,
5479 u64 parent, u64 root_objectid,
5480 u64 flags, struct btrfs_disk_key *key,
5481 int level, struct btrfs_key *ins)
5482{
5483 int ret;
5484 struct btrfs_fs_info *fs_info = root->fs_info;
5485 struct btrfs_extent_item *extent_item;
5486 struct btrfs_tree_block_info *block_info;
5487 struct btrfs_extent_inline_ref *iref;
5488 struct btrfs_path *path;
5489 struct extent_buffer *leaf;
5490 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5491
5492 path = btrfs_alloc_path();
5493 BUG_ON(!path);
5494
5495 path->leave_spinning = 1;
5496 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5497 ins, size);
5498 BUG_ON(ret);
5499
5500 leaf = path->nodes[0];
5501 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5502 struct btrfs_extent_item);
5503 btrfs_set_extent_refs(leaf, extent_item, 1);
5504 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5505 btrfs_set_extent_flags(leaf, extent_item,
5506 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5507 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5508
5509 btrfs_set_tree_block_key(leaf, block_info, key);
5510 btrfs_set_tree_block_level(leaf, block_info, level);
5511
5512 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5513 if (parent > 0) {
5514 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5515 btrfs_set_extent_inline_ref_type(leaf, iref,
5516 BTRFS_SHARED_BLOCK_REF_KEY);
5517 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5518 } else {
5519 btrfs_set_extent_inline_ref_type(leaf, iref,
5520 BTRFS_TREE_BLOCK_REF_KEY);
5521 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5522 }
5523
5524 btrfs_mark_buffer_dirty(leaf);
5525 btrfs_free_path(path);
5526
Yan, Zhengf0486c62010-05-16 10:46:25 -04005527 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005528 if (ret) {
5529 printk(KERN_ERR "btrfs update block group failed for %llu "
5530 "%llu\n", (unsigned long long)ins->objectid,
5531 (unsigned long long)ins->offset);
5532 BUG();
5533 }
5534 return ret;
5535}
5536
5537int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5538 struct btrfs_root *root,
5539 u64 root_objectid, u64 owner,
5540 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005541{
5542 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005543
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005544 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005545
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005546 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5547 0, root_objectid, owner, offset,
5548 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005549 return ret;
5550}
Chris Masone02119d2008-09-05 16:13:11 -04005551
5552/*
5553 * this is used by the tree logging recovery code. It records that
5554 * an extent has been allocated and makes sure to clear the free
5555 * space cache bits as well
5556 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005557int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5558 struct btrfs_root *root,
5559 u64 root_objectid, u64 owner, u64 offset,
5560 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005561{
5562 int ret;
5563 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005564 struct btrfs_caching_control *caching_ctl;
5565 u64 start = ins->objectid;
5566 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005567
Chris Masone02119d2008-09-05 16:13:11 -04005568 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005569 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005570 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005571
Yan Zheng11833d62009-09-11 16:11:19 -04005572 if (!caching_ctl) {
5573 BUG_ON(!block_group_cache_done(block_group));
5574 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5575 BUG_ON(ret);
5576 } else {
5577 mutex_lock(&caching_ctl->mutex);
5578
5579 if (start >= caching_ctl->progress) {
5580 ret = add_excluded_extent(root, start, num_bytes);
5581 BUG_ON(ret);
5582 } else if (start + num_bytes <= caching_ctl->progress) {
5583 ret = btrfs_remove_free_space(block_group,
5584 start, num_bytes);
5585 BUG_ON(ret);
5586 } else {
5587 num_bytes = caching_ctl->progress - start;
5588 ret = btrfs_remove_free_space(block_group,
5589 start, num_bytes);
5590 BUG_ON(ret);
5591
5592 start = caching_ctl->progress;
5593 num_bytes = ins->objectid + ins->offset -
5594 caching_ctl->progress;
5595 ret = add_excluded_extent(root, start, num_bytes);
5596 BUG_ON(ret);
5597 }
5598
5599 mutex_unlock(&caching_ctl->mutex);
5600 put_caching_control(caching_ctl);
5601 }
5602
Li Dongyangb4d00d52011-03-24 10:24:25 +00005603 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005604 BUG_ON(ret);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005605 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005606 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5607 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005608 return ret;
5609}
5610
Chris Mason65b51a02008-08-01 15:11:20 -04005611struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5612 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005613 u64 bytenr, u32 blocksize,
5614 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005615{
5616 struct extent_buffer *buf;
5617
5618 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5619 if (!buf)
5620 return ERR_PTR(-ENOMEM);
5621 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005622 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005623 btrfs_tree_lock(buf);
5624 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005625
5626 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005627 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005628
Chris Masond0c803c2008-09-11 16:17:57 -04005629 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005630 /*
5631 * we allow two log transactions at a time, use different
5632 * EXENT bit to differentiate dirty pages.
5633 */
5634 if (root->log_transid % 2 == 0)
5635 set_extent_dirty(&root->dirty_log_pages, buf->start,
5636 buf->start + buf->len - 1, GFP_NOFS);
5637 else
5638 set_extent_new(&root->dirty_log_pages, buf->start,
5639 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005640 } else {
5641 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5642 buf->start + buf->len - 1, GFP_NOFS);
5643 }
Chris Mason65b51a02008-08-01 15:11:20 -04005644 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005645 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005646 return buf;
5647}
5648
Yan, Zhengf0486c62010-05-16 10:46:25 -04005649static struct btrfs_block_rsv *
5650use_block_rsv(struct btrfs_trans_handle *trans,
5651 struct btrfs_root *root, u32 blocksize)
5652{
5653 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005654 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005655 int ret;
5656
5657 block_rsv = get_block_rsv(trans, root);
5658
5659 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005660 ret = reserve_metadata_bytes(trans, root, block_rsv,
5661 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005662 /*
5663 * If we couldn't reserve metadata bytes try and use some from
5664 * the global reserve.
5665 */
5666 if (ret && block_rsv != global_rsv) {
5667 ret = block_rsv_use_bytes(global_rsv, blocksize);
5668 if (!ret)
5669 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005670 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005671 } else if (ret) {
5672 return ERR_PTR(ret);
5673 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005674 return block_rsv;
5675 }
5676
5677 ret = block_rsv_use_bytes(block_rsv, blocksize);
5678 if (!ret)
5679 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005680 if (ret) {
5681 WARN_ON(1);
5682 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5683 0);
5684 if (!ret) {
5685 spin_lock(&block_rsv->lock);
5686 block_rsv->size += blocksize;
5687 spin_unlock(&block_rsv->lock);
5688 return block_rsv;
5689 } else if (ret && block_rsv != global_rsv) {
5690 ret = block_rsv_use_bytes(global_rsv, blocksize);
5691 if (!ret)
5692 return global_rsv;
5693 }
5694 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005695
Yan, Zhengf0486c62010-05-16 10:46:25 -04005696 return ERR_PTR(-ENOSPC);
5697}
5698
5699static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5700{
5701 block_rsv_add_bytes(block_rsv, blocksize, 0);
5702 block_rsv_release_bytes(block_rsv, NULL, 0);
5703}
5704
Chris Masonfec577f2007-02-26 10:40:21 -05005705/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005706 * finds a free extent and does all the dirty work required for allocation
5707 * returns the key for the extent through ins, and a tree buffer for
5708 * the first block of the extent through buf.
5709 *
Chris Masonfec577f2007-02-26 10:40:21 -05005710 * returns the tree buffer or NULL.
5711 */
Chris Mason5f39d392007-10-15 16:14:19 -04005712struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005713 struct btrfs_root *root, u32 blocksize,
5714 u64 parent, u64 root_objectid,
5715 struct btrfs_disk_key *key, int level,
5716 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005717{
Chris Masone2fa7222007-03-12 16:22:34 -04005718 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005719 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005720 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005721 u64 flags = 0;
5722 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005723
Yan, Zhengf0486c62010-05-16 10:46:25 -04005724
5725 block_rsv = use_block_rsv(trans, root, blocksize);
5726 if (IS_ERR(block_rsv))
5727 return ERR_CAST(block_rsv);
5728
5729 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5730 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005731 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005732 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005733 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005734 }
Chris Mason55c69072008-01-09 15:55:33 -05005735
Chris Mason4008c042009-02-12 14:09:45 -05005736 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5737 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005738 BUG_ON(IS_ERR(buf));
5739
5740 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5741 if (parent == 0)
5742 parent = ins.objectid;
5743 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5744 } else
5745 BUG_ON(parent > 0);
5746
5747 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5748 struct btrfs_delayed_extent_op *extent_op;
5749 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5750 BUG_ON(!extent_op);
5751 if (key)
5752 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5753 else
5754 memset(&extent_op->key, 0, sizeof(extent_op->key));
5755 extent_op->flags_to_set = flags;
5756 extent_op->update_key = 1;
5757 extent_op->update_flags = 1;
5758 extent_op->is_data = 0;
5759
5760 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5761 ins.offset, parent, root_objectid,
5762 level, BTRFS_ADD_DELAYED_EXTENT,
5763 extent_op);
5764 BUG_ON(ret);
5765 }
Chris Masonfec577f2007-02-26 10:40:21 -05005766 return buf;
5767}
Chris Masona28ec192007-03-06 20:08:01 -05005768
Yan Zheng2c47e6052009-06-27 21:07:35 -04005769struct walk_control {
5770 u64 refs[BTRFS_MAX_LEVEL];
5771 u64 flags[BTRFS_MAX_LEVEL];
5772 struct btrfs_key update_progress;
5773 int stage;
5774 int level;
5775 int shared_level;
5776 int update_ref;
5777 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005778 int reada_slot;
5779 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005780};
5781
5782#define DROP_REFERENCE 1
5783#define UPDATE_BACKREF 2
5784
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005785static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5786 struct btrfs_root *root,
5787 struct walk_control *wc,
5788 struct btrfs_path *path)
5789{
5790 u64 bytenr;
5791 u64 generation;
5792 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005793 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005794 u32 nritems;
5795 u32 blocksize;
5796 struct btrfs_key key;
5797 struct extent_buffer *eb;
5798 int ret;
5799 int slot;
5800 int nread = 0;
5801
5802 if (path->slots[wc->level] < wc->reada_slot) {
5803 wc->reada_count = wc->reada_count * 2 / 3;
5804 wc->reada_count = max(wc->reada_count, 2);
5805 } else {
5806 wc->reada_count = wc->reada_count * 3 / 2;
5807 wc->reada_count = min_t(int, wc->reada_count,
5808 BTRFS_NODEPTRS_PER_BLOCK(root));
5809 }
5810
5811 eb = path->nodes[wc->level];
5812 nritems = btrfs_header_nritems(eb);
5813 blocksize = btrfs_level_size(root, wc->level - 1);
5814
5815 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5816 if (nread >= wc->reada_count)
5817 break;
5818
5819 cond_resched();
5820 bytenr = btrfs_node_blockptr(eb, slot);
5821 generation = btrfs_node_ptr_generation(eb, slot);
5822
5823 if (slot == path->slots[wc->level])
5824 goto reada;
5825
5826 if (wc->stage == UPDATE_BACKREF &&
5827 generation <= root->root_key.offset)
5828 continue;
5829
Yan, Zheng94fcca92009-10-09 09:25:16 -04005830 /* We don't lock the tree block, it's OK to be racy here */
5831 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5832 &refs, &flags);
5833 BUG_ON(ret);
5834 BUG_ON(refs == 0);
5835
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005836 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005837 if (refs == 1)
5838 goto reada;
5839
Yan, Zheng94fcca92009-10-09 09:25:16 -04005840 if (wc->level == 1 &&
5841 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5842 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005843 if (!wc->update_ref ||
5844 generation <= root->root_key.offset)
5845 continue;
5846 btrfs_node_key_to_cpu(eb, &key, slot);
5847 ret = btrfs_comp_cpu_keys(&key,
5848 &wc->update_progress);
5849 if (ret < 0)
5850 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005851 } else {
5852 if (wc->level == 1 &&
5853 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5854 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005855 }
5856reada:
5857 ret = readahead_tree_block(root, bytenr, blocksize,
5858 generation);
5859 if (ret)
5860 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005861 nread++;
5862 }
5863 wc->reada_slot = slot;
5864}
5865
Chris Mason9aca1d52007-03-13 11:09:37 -04005866/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005867 * hepler to process tree block while walking down the tree.
5868 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005869 * when wc->stage == UPDATE_BACKREF, this function updates
5870 * back refs for pointers in the block.
5871 *
5872 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005873 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005874static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5875 struct btrfs_root *root,
5876 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005877 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005878{
5879 int level = wc->level;
5880 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005881 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5882 int ret;
5883
5884 if (wc->stage == UPDATE_BACKREF &&
5885 btrfs_header_owner(eb) != root->root_key.objectid)
5886 return 1;
5887
5888 /*
5889 * when reference count of tree block is 1, it won't increase
5890 * again. once full backref flag is set, we never clear it.
5891 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005892 if (lookup_info &&
5893 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5894 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005895 BUG_ON(!path->locks[level]);
5896 ret = btrfs_lookup_extent_info(trans, root,
5897 eb->start, eb->len,
5898 &wc->refs[level],
5899 &wc->flags[level]);
5900 BUG_ON(ret);
5901 BUG_ON(wc->refs[level] == 0);
5902 }
5903
Yan Zheng2c47e6052009-06-27 21:07:35 -04005904 if (wc->stage == DROP_REFERENCE) {
5905 if (wc->refs[level] > 1)
5906 return 1;
5907
5908 if (path->locks[level] && !wc->keep_locks) {
5909 btrfs_tree_unlock(eb);
5910 path->locks[level] = 0;
5911 }
5912 return 0;
5913 }
5914
5915 /* wc->stage == UPDATE_BACKREF */
5916 if (!(wc->flags[level] & flag)) {
5917 BUG_ON(!path->locks[level]);
5918 ret = btrfs_inc_ref(trans, root, eb, 1);
5919 BUG_ON(ret);
5920 ret = btrfs_dec_ref(trans, root, eb, 0);
5921 BUG_ON(ret);
5922 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5923 eb->len, flag, 0);
5924 BUG_ON(ret);
5925 wc->flags[level] |= flag;
5926 }
5927
5928 /*
5929 * the block is shared by multiple trees, so it's not good to
5930 * keep the tree lock
5931 */
5932 if (path->locks[level] && level > 0) {
5933 btrfs_tree_unlock(eb);
5934 path->locks[level] = 0;
5935 }
5936 return 0;
5937}
5938
5939/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005940 * hepler to process tree block pointer.
5941 *
5942 * when wc->stage == DROP_REFERENCE, this function checks
5943 * reference count of the block pointed to. if the block
5944 * is shared and we need update back refs for the subtree
5945 * rooted at the block, this function changes wc->stage to
5946 * UPDATE_BACKREF. if the block is shared and there is no
5947 * need to update back, this function drops the reference
5948 * to the block.
5949 *
5950 * NOTE: return value 1 means we should stop walking down.
5951 */
5952static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5953 struct btrfs_root *root,
5954 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005955 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005956{
5957 u64 bytenr;
5958 u64 generation;
5959 u64 parent;
5960 u32 blocksize;
5961 struct btrfs_key key;
5962 struct extent_buffer *next;
5963 int level = wc->level;
5964 int reada = 0;
5965 int ret = 0;
5966
5967 generation = btrfs_node_ptr_generation(path->nodes[level],
5968 path->slots[level]);
5969 /*
5970 * if the lower level block was created before the snapshot
5971 * was created, we know there is no need to update back refs
5972 * for the subtree
5973 */
5974 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005975 generation <= root->root_key.offset) {
5976 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005977 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005978 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005979
5980 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5981 blocksize = btrfs_level_size(root, level - 1);
5982
5983 next = btrfs_find_tree_block(root, bytenr, blocksize);
5984 if (!next) {
5985 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00005986 if (!next)
5987 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005988 reada = 1;
5989 }
5990 btrfs_tree_lock(next);
5991 btrfs_set_lock_blocking(next);
5992
Yan, Zheng94fcca92009-10-09 09:25:16 -04005993 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5994 &wc->refs[level - 1],
5995 &wc->flags[level - 1]);
5996 BUG_ON(ret);
5997 BUG_ON(wc->refs[level - 1] == 0);
5998 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005999
Yan, Zheng94fcca92009-10-09 09:25:16 -04006000 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006001 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006002 if (level == 1 &&
6003 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6004 goto skip;
6005
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006006 if (!wc->update_ref ||
6007 generation <= root->root_key.offset)
6008 goto skip;
6009
6010 btrfs_node_key_to_cpu(path->nodes[level], &key,
6011 path->slots[level]);
6012 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6013 if (ret < 0)
6014 goto skip;
6015
6016 wc->stage = UPDATE_BACKREF;
6017 wc->shared_level = level - 1;
6018 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006019 } else {
6020 if (level == 1 &&
6021 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6022 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006023 }
6024
6025 if (!btrfs_buffer_uptodate(next, generation)) {
6026 btrfs_tree_unlock(next);
6027 free_extent_buffer(next);
6028 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006029 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006030 }
6031
6032 if (!next) {
6033 if (reada && level == 1)
6034 reada_walk_down(trans, root, wc, path);
6035 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006036 if (!next)
6037 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006038 btrfs_tree_lock(next);
6039 btrfs_set_lock_blocking(next);
6040 }
6041
6042 level--;
6043 BUG_ON(level != btrfs_header_level(next));
6044 path->nodes[level] = next;
6045 path->slots[level] = 0;
6046 path->locks[level] = 1;
6047 wc->level = level;
6048 if (wc->level == 1)
6049 wc->reada_slot = 0;
6050 return 0;
6051skip:
6052 wc->refs[level - 1] = 0;
6053 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006054 if (wc->stage == DROP_REFERENCE) {
6055 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6056 parent = path->nodes[level]->start;
6057 } else {
6058 BUG_ON(root->root_key.objectid !=
6059 btrfs_header_owner(path->nodes[level]));
6060 parent = 0;
6061 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006062
Yan, Zheng94fcca92009-10-09 09:25:16 -04006063 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6064 root->root_key.objectid, level - 1, 0);
6065 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006066 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006067 btrfs_tree_unlock(next);
6068 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006069 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006070 return 1;
6071}
6072
6073/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006074 * hepler to process tree block while walking up the tree.
6075 *
6076 * when wc->stage == DROP_REFERENCE, this function drops
6077 * reference count on the block.
6078 *
6079 * when wc->stage == UPDATE_BACKREF, this function changes
6080 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6081 * to UPDATE_BACKREF previously while processing the block.
6082 *
6083 * NOTE: return value 1 means we should stop walking up.
6084 */
6085static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6086 struct btrfs_root *root,
6087 struct btrfs_path *path,
6088 struct walk_control *wc)
6089{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006090 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006091 int level = wc->level;
6092 struct extent_buffer *eb = path->nodes[level];
6093 u64 parent = 0;
6094
6095 if (wc->stage == UPDATE_BACKREF) {
6096 BUG_ON(wc->shared_level < level);
6097 if (level < wc->shared_level)
6098 goto out;
6099
Yan Zheng2c47e6052009-06-27 21:07:35 -04006100 ret = find_next_key(path, level + 1, &wc->update_progress);
6101 if (ret > 0)
6102 wc->update_ref = 0;
6103
6104 wc->stage = DROP_REFERENCE;
6105 wc->shared_level = -1;
6106 path->slots[level] = 0;
6107
6108 /*
6109 * check reference count again if the block isn't locked.
6110 * we should start walking down the tree again if reference
6111 * count is one.
6112 */
6113 if (!path->locks[level]) {
6114 BUG_ON(level == 0);
6115 btrfs_tree_lock(eb);
6116 btrfs_set_lock_blocking(eb);
6117 path->locks[level] = 1;
6118
6119 ret = btrfs_lookup_extent_info(trans, root,
6120 eb->start, eb->len,
6121 &wc->refs[level],
6122 &wc->flags[level]);
6123 BUG_ON(ret);
6124 BUG_ON(wc->refs[level] == 0);
6125 if (wc->refs[level] == 1) {
6126 btrfs_tree_unlock(eb);
6127 path->locks[level] = 0;
6128 return 1;
6129 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006130 }
6131 }
6132
6133 /* wc->stage == DROP_REFERENCE */
6134 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6135
6136 if (wc->refs[level] == 1) {
6137 if (level == 0) {
6138 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6139 ret = btrfs_dec_ref(trans, root, eb, 1);
6140 else
6141 ret = btrfs_dec_ref(trans, root, eb, 0);
6142 BUG_ON(ret);
6143 }
6144 /* make block locked assertion in clean_tree_block happy */
6145 if (!path->locks[level] &&
6146 btrfs_header_generation(eb) == trans->transid) {
6147 btrfs_tree_lock(eb);
6148 btrfs_set_lock_blocking(eb);
6149 path->locks[level] = 1;
6150 }
6151 clean_tree_block(trans, root, eb);
6152 }
6153
6154 if (eb == root->node) {
6155 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6156 parent = eb->start;
6157 else
6158 BUG_ON(root->root_key.objectid !=
6159 btrfs_header_owner(eb));
6160 } else {
6161 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6162 parent = path->nodes[level + 1]->start;
6163 else
6164 BUG_ON(root->root_key.objectid !=
6165 btrfs_header_owner(path->nodes[level + 1]));
6166 }
6167
Yan, Zhengf0486c62010-05-16 10:46:25 -04006168 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006169out:
6170 wc->refs[level] = 0;
6171 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006172 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006173}
6174
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006175static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6176 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006177 struct btrfs_path *path,
6178 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006179{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006180 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006181 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006182 int ret;
6183
Yan Zheng2c47e6052009-06-27 21:07:35 -04006184 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006185 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006186 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006187 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006188
Yan Zheng2c47e6052009-06-27 21:07:35 -04006189 if (level == 0)
6190 break;
6191
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006192 if (path->slots[level] >=
6193 btrfs_header_nritems(path->nodes[level]))
6194 break;
6195
Yan, Zheng94fcca92009-10-09 09:25:16 -04006196 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006197 if (ret > 0) {
6198 path->slots[level]++;
6199 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00006200 } else if (ret < 0)
6201 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006202 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006203 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006204 return 0;
6205}
6206
Chris Masond3977122009-01-05 21:25:51 -05006207static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006208 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006209 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006210 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006211{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006212 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006213 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006214
Yan Zheng2c47e6052009-06-27 21:07:35 -04006215 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6216 while (level < max_level && path->nodes[level]) {
6217 wc->level = level;
6218 if (path->slots[level] + 1 <
6219 btrfs_header_nritems(path->nodes[level])) {
6220 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006221 return 0;
6222 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006223 ret = walk_up_proc(trans, root, path, wc);
6224 if (ret > 0)
6225 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006226
Yan Zheng2c47e6052009-06-27 21:07:35 -04006227 if (path->locks[level]) {
6228 btrfs_tree_unlock(path->nodes[level]);
6229 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006230 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006231 free_extent_buffer(path->nodes[level]);
6232 path->nodes[level] = NULL;
6233 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006234 }
6235 }
6236 return 1;
6237}
6238
Chris Mason9aca1d52007-03-13 11:09:37 -04006239/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006240 * drop a subvolume tree.
6241 *
6242 * this function traverses the tree freeing any blocks that only
6243 * referenced by the tree.
6244 *
6245 * when a shared tree block is found. this function decreases its
6246 * reference count by one. if update_ref is true, this function
6247 * also make sure backrefs for the shared block and all lower level
6248 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006249 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006250int btrfs_drop_snapshot(struct btrfs_root *root,
6251 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006252{
Chris Mason5caf2a02007-04-02 11:20:42 -04006253 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006254 struct btrfs_trans_handle *trans;
6255 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006256 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006257 struct walk_control *wc;
6258 struct btrfs_key key;
6259 int err = 0;
6260 int ret;
6261 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006262
Chris Mason5caf2a02007-04-02 11:20:42 -04006263 path = btrfs_alloc_path();
6264 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006265
Yan Zheng2c47e6052009-06-27 21:07:35 -04006266 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6267 BUG_ON(!wc);
6268
Yan, Zhenga22285a2010-05-16 10:48:46 -04006269 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006270 BUG_ON(IS_ERR(trans));
6271
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006272 if (block_rsv)
6273 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006274
Chris Mason9f3a7422007-08-07 15:52:19 -04006275 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006276 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006277 path->nodes[level] = btrfs_lock_root_node(root);
6278 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006279 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006280 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006281 memset(&wc->update_progress, 0,
6282 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006283 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006284 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006285 memcpy(&wc->update_progress, &key,
6286 sizeof(wc->update_progress));
6287
Chris Mason6702ed42007-08-07 16:15:09 -04006288 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006289 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006290 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006291 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6292 path->lowest_level = 0;
6293 if (ret < 0) {
6294 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006295 goto out;
6296 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006297 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006298
Chris Mason7d9eb122008-07-08 14:19:17 -04006299 /*
6300 * unlock our path, this is safe because only this
6301 * function is allowed to delete this snapshot
6302 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006303 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006304
Yan Zheng2c47e6052009-06-27 21:07:35 -04006305 level = btrfs_header_level(root->node);
6306 while (1) {
6307 btrfs_tree_lock(path->nodes[level]);
6308 btrfs_set_lock_blocking(path->nodes[level]);
6309
6310 ret = btrfs_lookup_extent_info(trans, root,
6311 path->nodes[level]->start,
6312 path->nodes[level]->len,
6313 &wc->refs[level],
6314 &wc->flags[level]);
6315 BUG_ON(ret);
6316 BUG_ON(wc->refs[level] == 0);
6317
6318 if (level == root_item->drop_level)
6319 break;
6320
6321 btrfs_tree_unlock(path->nodes[level]);
6322 WARN_ON(wc->refs[level] != 1);
6323 level--;
6324 }
6325 }
6326
6327 wc->level = level;
6328 wc->shared_level = -1;
6329 wc->stage = DROP_REFERENCE;
6330 wc->update_ref = update_ref;
6331 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006332 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006333
6334 while (1) {
6335 ret = walk_down_tree(trans, root, path, wc);
6336 if (ret < 0) {
6337 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006338 break;
6339 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006340
6341 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6342 if (ret < 0) {
6343 err = ret;
6344 break;
6345 }
6346
6347 if (ret > 0) {
6348 BUG_ON(wc->stage != DROP_REFERENCE);
6349 break;
6350 }
6351
6352 if (wc->stage == DROP_REFERENCE) {
6353 level = wc->level;
6354 btrfs_node_key(path->nodes[level],
6355 &root_item->drop_progress,
6356 path->slots[level]);
6357 root_item->drop_level = level;
6358 }
6359
6360 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006361 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006362 ret = btrfs_update_root(trans, tree_root,
6363 &root->root_key,
6364 root_item);
6365 BUG_ON(ret);
6366
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006367 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006368 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006369 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006370 if (block_rsv)
6371 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006372 }
Chris Mason20524f02007-03-10 06:35:47 -05006373 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006374 btrfs_release_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006375 BUG_ON(err);
6376
6377 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6378 BUG_ON(ret);
6379
Yan, Zheng76dda932009-09-21 16:00:26 -04006380 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6381 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6382 NULL, NULL);
6383 BUG_ON(ret < 0);
6384 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006385 /* if we fail to delete the orphan item this time
6386 * around, it'll get picked up the next time.
6387 *
6388 * The most common failure here is just -ENOENT.
6389 */
6390 btrfs_del_orphan_item(trans, tree_root,
6391 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006392 }
6393 }
6394
6395 if (root->in_radix) {
6396 btrfs_free_fs_root(tree_root->fs_info, root);
6397 } else {
6398 free_extent_buffer(root->node);
6399 free_extent_buffer(root->commit_root);
6400 kfree(root);
6401 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006402out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006403 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006404 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006405 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006406 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006407}
Chris Mason9078a3e2007-04-26 16:46:15 -04006408
Yan Zheng2c47e6052009-06-27 21:07:35 -04006409/*
6410 * drop subtree rooted at tree block 'node'.
6411 *
6412 * NOTE: this function will unlock and release tree block 'node'
6413 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006414int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6415 struct btrfs_root *root,
6416 struct extent_buffer *node,
6417 struct extent_buffer *parent)
6418{
6419 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006420 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006421 int level;
6422 int parent_level;
6423 int ret = 0;
6424 int wret;
6425
Yan Zheng2c47e6052009-06-27 21:07:35 -04006426 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6427
Yan Zhengf82d02d2008-10-29 14:49:05 -04006428 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006429 if (!path)
6430 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006431
Yan Zheng2c47e6052009-06-27 21:07:35 -04006432 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006433 if (!wc) {
6434 btrfs_free_path(path);
6435 return -ENOMEM;
6436 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006437
Chris Masonb9447ef2009-03-09 11:45:38 -04006438 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006439 parent_level = btrfs_header_level(parent);
6440 extent_buffer_get(parent);
6441 path->nodes[parent_level] = parent;
6442 path->slots[parent_level] = btrfs_header_nritems(parent);
6443
Chris Masonb9447ef2009-03-09 11:45:38 -04006444 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006445 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006446 path->nodes[level] = node;
6447 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006448 path->locks[level] = 1;
6449
6450 wc->refs[parent_level] = 1;
6451 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6452 wc->level = level;
6453 wc->shared_level = -1;
6454 wc->stage = DROP_REFERENCE;
6455 wc->update_ref = 0;
6456 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006457 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006458
6459 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006460 wret = walk_down_tree(trans, root, path, wc);
6461 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006462 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006463 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006464 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006465
Yan Zheng2c47e6052009-06-27 21:07:35 -04006466 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006467 if (wret < 0)
6468 ret = wret;
6469 if (wret != 0)
6470 break;
6471 }
6472
Yan Zheng2c47e6052009-06-27 21:07:35 -04006473 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006474 btrfs_free_path(path);
6475 return ret;
6476}
6477
Chris Masonec44a352008-04-28 15:29:52 -04006478static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6479{
6480 u64 num_devices;
6481 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6482 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6483
Chris Masoncd02dca2010-12-13 14:56:23 -05006484 /*
6485 * we add in the count of missing devices because we want
6486 * to make sure that any RAID levels on a degraded FS
6487 * continue to be honored.
6488 */
6489 num_devices = root->fs_info->fs_devices->rw_devices +
6490 root->fs_info->fs_devices->missing_devices;
6491
Chris Masonec44a352008-04-28 15:29:52 -04006492 if (num_devices == 1) {
6493 stripped |= BTRFS_BLOCK_GROUP_DUP;
6494 stripped = flags & ~stripped;
6495
6496 /* turn raid0 into single device chunks */
6497 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6498 return stripped;
6499
6500 /* turn mirroring into duplication */
6501 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6502 BTRFS_BLOCK_GROUP_RAID10))
6503 return stripped | BTRFS_BLOCK_GROUP_DUP;
6504 return flags;
6505 } else {
6506 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006507 if (flags & stripped)
6508 return flags;
6509
6510 stripped |= BTRFS_BLOCK_GROUP_DUP;
6511 stripped = flags & ~stripped;
6512
6513 /* switch duplicated blocks with raid1 */
6514 if (flags & BTRFS_BLOCK_GROUP_DUP)
6515 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6516
6517 /* turn single device chunks into raid0 */
6518 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6519 }
6520 return flags;
6521}
6522
Yan, Zhengf0486c62010-05-16 10:46:25 -04006523static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04006524{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006525 struct btrfs_space_info *sinfo = cache->space_info;
6526 u64 num_bytes;
6527 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04006528
Yan, Zhengf0486c62010-05-16 10:46:25 -04006529 if (cache->ro)
6530 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04006531
Yan, Zhengf0486c62010-05-16 10:46:25 -04006532 spin_lock(&sinfo->lock);
6533 spin_lock(&cache->lock);
6534 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6535 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04006536
Yan, Zhengf0486c62010-05-16 10:46:25 -04006537 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
6538 sinfo->bytes_may_use + sinfo->bytes_readonly +
Chris Mason65e53412010-12-24 06:41:52 -05006539 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04006540 sinfo->bytes_readonly += num_bytes;
6541 sinfo->bytes_reserved += cache->reserved_pinned;
6542 cache->reserved_pinned = 0;
6543 cache->ro = 1;
6544 ret = 0;
6545 }
Chris Mason65e53412010-12-24 06:41:52 -05006546
Yan, Zhengf0486c62010-05-16 10:46:25 -04006547 spin_unlock(&cache->lock);
6548 spin_unlock(&sinfo->lock);
6549 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04006550}
6551
Yan, Zhengf0486c62010-05-16 10:46:25 -04006552int btrfs_set_block_group_ro(struct btrfs_root *root,
6553 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006554
6555{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006556 struct btrfs_trans_handle *trans;
6557 u64 alloc_flags;
6558 int ret;
6559
6560 BUG_ON(cache->ro);
6561
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006562 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006563 BUG_ON(IS_ERR(trans));
6564
6565 alloc_flags = update_block_group_flags(root, cache->flags);
6566 if (alloc_flags != cache->flags)
Chris Mason0e4f8f82011-04-15 16:05:44 -04006567 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6568 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006569
6570 ret = set_block_group_ro(cache);
6571 if (!ret)
6572 goto out;
6573 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006574 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6575 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006576 if (ret < 0)
6577 goto out;
6578 ret = set_block_group_ro(cache);
6579out:
6580 btrfs_end_transaction(trans, root);
6581 return ret;
6582}
6583
Chris Masonc87f08c2011-02-16 13:57:04 -05006584int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
6585 struct btrfs_root *root, u64 type)
6586{
6587 u64 alloc_flags = get_alloc_profile(root, type);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006588 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6589 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05006590}
6591
Miao Xie6d07bce2011-01-05 10:07:31 +00006592/*
6593 * helper to account the unused space of all the readonly block group in the
6594 * list. takes mirrors into account.
6595 */
6596static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
6597{
6598 struct btrfs_block_group_cache *block_group;
6599 u64 free_bytes = 0;
6600 int factor;
6601
6602 list_for_each_entry(block_group, groups_list, list) {
6603 spin_lock(&block_group->lock);
6604
6605 if (!block_group->ro) {
6606 spin_unlock(&block_group->lock);
6607 continue;
6608 }
6609
6610 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
6611 BTRFS_BLOCK_GROUP_RAID10 |
6612 BTRFS_BLOCK_GROUP_DUP))
6613 factor = 2;
6614 else
6615 factor = 1;
6616
6617 free_bytes += (block_group->key.offset -
6618 btrfs_block_group_used(&block_group->item)) *
6619 factor;
6620
6621 spin_unlock(&block_group->lock);
6622 }
6623
6624 return free_bytes;
6625}
6626
6627/*
6628 * helper to account the unused space of all the readonly block group in the
6629 * space_info. takes mirrors into account.
6630 */
6631u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
6632{
6633 int i;
6634 u64 free_bytes = 0;
6635
6636 spin_lock(&sinfo->lock);
6637
6638 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
6639 if (!list_empty(&sinfo->block_groups[i]))
6640 free_bytes += __btrfs_get_ro_block_group_free_space(
6641 &sinfo->block_groups[i]);
6642
6643 spin_unlock(&sinfo->lock);
6644
6645 return free_bytes;
6646}
6647
Yan, Zhengf0486c62010-05-16 10:46:25 -04006648int btrfs_set_block_group_rw(struct btrfs_root *root,
6649 struct btrfs_block_group_cache *cache)
6650{
6651 struct btrfs_space_info *sinfo = cache->space_info;
6652 u64 num_bytes;
6653
6654 BUG_ON(!cache->ro);
6655
6656 spin_lock(&sinfo->lock);
6657 spin_lock(&cache->lock);
6658 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6659 cache->bytes_super - btrfs_block_group_used(&cache->item);
6660 sinfo->bytes_readonly -= num_bytes;
6661 cache->ro = 0;
6662 spin_unlock(&cache->lock);
6663 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006664 return 0;
6665}
6666
Josef Bacikba1bf482009-09-11 16:11:19 -04006667/*
6668 * checks to see if its even possible to relocate this block group.
6669 *
6670 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6671 * ok to go ahead and try.
6672 */
6673int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04006674{
Zheng Yan1a40e232008-09-26 10:09:34 -04006675 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04006676 struct btrfs_space_info *space_info;
6677 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6678 struct btrfs_device *device;
6679 int full = 0;
6680 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006681
Josef Bacikba1bf482009-09-11 16:11:19 -04006682 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04006683
Josef Bacikba1bf482009-09-11 16:11:19 -04006684 /* odd, couldn't find the block group, leave it alone */
6685 if (!block_group)
6686 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006687
Josef Bacikba1bf482009-09-11 16:11:19 -04006688 /* no bytes used, we're good */
6689 if (!btrfs_block_group_used(&block_group->item))
6690 goto out;
Chris Mason323da792008-05-09 11:46:48 -04006691
Josef Bacikba1bf482009-09-11 16:11:19 -04006692 space_info = block_group->space_info;
6693 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04006694
Josef Bacikba1bf482009-09-11 16:11:19 -04006695 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04006696
Josef Bacikba1bf482009-09-11 16:11:19 -04006697 /*
6698 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04006699 * relocate it unless we're able to allocate a new chunk below.
6700 *
6701 * Otherwise, we need to make sure we have room in the space to handle
6702 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04006703 */
Chris Mason7ce618d2009-09-22 14:48:44 -04006704 if ((space_info->total_bytes != block_group->key.offset) &&
6705 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04006706 space_info->bytes_pinned + space_info->bytes_readonly +
6707 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04006708 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04006709 spin_unlock(&space_info->lock);
6710 goto out;
6711 }
6712 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006713
Josef Bacikba1bf482009-09-11 16:11:19 -04006714 /*
6715 * ok we don't have enough space, but maybe we have free space on our
6716 * devices to allocate new chunks for relocation, so loop through our
6717 * alloc devices and guess if we have enough space. However, if we
6718 * were marked as full, then we know there aren't enough chunks, and we
6719 * can just return.
6720 */
6721 ret = -1;
6722 if (full)
6723 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05006724
Josef Bacikba1bf482009-09-11 16:11:19 -04006725 mutex_lock(&root->fs_info->chunk_mutex);
6726 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
6727 u64 min_free = btrfs_block_group_used(&block_group->item);
Miao Xie7bfc8372011-01-05 10:07:26 +00006728 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04006729
Josef Bacikba1bf482009-09-11 16:11:19 -04006730 /*
6731 * check to make sure we can actually find a chunk with enough
6732 * space to fit our block group in.
6733 */
6734 if (device->total_bytes > device->bytes_used + min_free) {
6735 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00006736 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04006737 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05006738 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04006739 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05006740 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006741 }
Josef Bacikba1bf482009-09-11 16:11:19 -04006742 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05006743out:
Josef Bacikba1bf482009-09-11 16:11:19 -04006744 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006745 return ret;
6746}
6747
Christoph Hellwigb2950862008-12-02 09:54:17 -05006748static int find_first_block_group(struct btrfs_root *root,
6749 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04006750{
Chris Mason925baed2008-06-25 16:01:30 -04006751 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006752 struct btrfs_key found_key;
6753 struct extent_buffer *leaf;
6754 int slot;
6755
6756 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6757 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006758 goto out;
6759
Chris Masond3977122009-01-05 21:25:51 -05006760 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006761 slot = path->slots[0];
6762 leaf = path->nodes[0];
6763 if (slot >= btrfs_header_nritems(leaf)) {
6764 ret = btrfs_next_leaf(root, path);
6765 if (ret == 0)
6766 continue;
6767 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006768 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04006769 break;
6770 }
6771 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6772
6773 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04006774 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6775 ret = 0;
6776 goto out;
6777 }
Chris Mason0b86a832008-03-24 15:01:56 -04006778 path->slots[0]++;
6779 }
Chris Mason925baed2008-06-25 16:01:30 -04006780out:
Chris Mason0b86a832008-03-24 15:01:56 -04006781 return ret;
6782}
6783
Josef Bacik0af3d002010-06-21 14:48:16 -04006784void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
6785{
6786 struct btrfs_block_group_cache *block_group;
6787 u64 last = 0;
6788
6789 while (1) {
6790 struct inode *inode;
6791
6792 block_group = btrfs_lookup_first_block_group(info, last);
6793 while (block_group) {
6794 spin_lock(&block_group->lock);
6795 if (block_group->iref)
6796 break;
6797 spin_unlock(&block_group->lock);
6798 block_group = next_block_group(info->tree_root,
6799 block_group);
6800 }
6801 if (!block_group) {
6802 if (last == 0)
6803 break;
6804 last = 0;
6805 continue;
6806 }
6807
6808 inode = block_group->inode;
6809 block_group->iref = 0;
6810 block_group->inode = NULL;
6811 spin_unlock(&block_group->lock);
6812 iput(inode);
6813 last = block_group->key.objectid + block_group->key.offset;
6814 btrfs_put_block_group(block_group);
6815 }
6816}
6817
Zheng Yan1a40e232008-09-26 10:09:34 -04006818int btrfs_free_block_groups(struct btrfs_fs_info *info)
6819{
6820 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04006821 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04006822 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04006823 struct rb_node *n;
6824
Yan Zheng11833d62009-09-11 16:11:19 -04006825 down_write(&info->extent_commit_sem);
6826 while (!list_empty(&info->caching_block_groups)) {
6827 caching_ctl = list_entry(info->caching_block_groups.next,
6828 struct btrfs_caching_control, list);
6829 list_del(&caching_ctl->list);
6830 put_caching_control(caching_ctl);
6831 }
6832 up_write(&info->extent_commit_sem);
6833
Zheng Yan1a40e232008-09-26 10:09:34 -04006834 spin_lock(&info->block_group_cache_lock);
6835 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6836 block_group = rb_entry(n, struct btrfs_block_group_cache,
6837 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04006838 rb_erase(&block_group->cache_node,
6839 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04006840 spin_unlock(&info->block_group_cache_lock);
6841
Josef Bacik80eb2342008-10-29 14:49:05 -04006842 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006843 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006844 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006845
Josef Bacik817d52f2009-07-13 21:29:25 -04006846 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04006847 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04006848
Josef Bacik3c148742011-02-02 15:53:47 +00006849 /*
6850 * We haven't cached this block group, which means we could
6851 * possibly have excluded extents on this block group.
6852 */
6853 if (block_group->cached == BTRFS_CACHE_NO)
6854 free_excluded_extents(info->extent_root, block_group);
6855
Josef Bacik817d52f2009-07-13 21:29:25 -04006856 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00006857 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04006858
6859 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006860 }
6861 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04006862
6863 /* now that all the block groups are freed, go through and
6864 * free all the space_info structs. This is only called during
6865 * the final stages of unmount, and so we know nobody is
6866 * using them. We call synchronize_rcu() once before we start,
6867 * just to be on the safe side.
6868 */
6869 synchronize_rcu();
6870
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006871 release_global_block_rsv(info);
6872
Chris Mason4184ea72009-03-10 12:39:20 -04006873 while(!list_empty(&info->space_info)) {
6874 space_info = list_entry(info->space_info.next,
6875 struct btrfs_space_info,
6876 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006877 if (space_info->bytes_pinned > 0 ||
6878 space_info->bytes_reserved > 0) {
6879 WARN_ON(1);
6880 dump_space_info(space_info, 0, 0);
6881 }
Chris Mason4184ea72009-03-10 12:39:20 -04006882 list_del(&space_info->list);
6883 kfree(space_info);
6884 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006885 return 0;
6886}
6887
Yan, Zhengb742bb82010-05-16 10:46:24 -04006888static void __link_block_group(struct btrfs_space_info *space_info,
6889 struct btrfs_block_group_cache *cache)
6890{
6891 int index = get_block_group_index(cache);
6892
6893 down_write(&space_info->groups_sem);
6894 list_add_tail(&cache->list, &space_info->block_groups[index]);
6895 up_write(&space_info->groups_sem);
6896}
6897
Chris Mason9078a3e2007-04-26 16:46:15 -04006898int btrfs_read_block_groups(struct btrfs_root *root)
6899{
6900 struct btrfs_path *path;
6901 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04006902 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04006903 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04006904 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04006905 struct btrfs_key key;
6906 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04006907 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04006908 int need_clear = 0;
6909 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04006910
Chris Masonbe744172007-05-06 10:15:01 -04006911 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04006912 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006913 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04006914 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04006915 path = btrfs_alloc_path();
6916 if (!path)
6917 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04006918 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04006919
Josef Bacik0af3d002010-06-21 14:48:16 -04006920 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
6921 if (cache_gen != 0 &&
6922 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
6923 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04006924 if (btrfs_test_opt(root, CLEAR_CACHE))
6925 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04006926 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
6927 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04006928
Chris Masond3977122009-01-05 21:25:51 -05006929 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006930 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006931 if (ret > 0)
6932 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006933 if (ret != 0)
6934 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04006935 leaf = path->nodes[0];
6936 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04006937 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04006938 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04006939 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006940 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04006941 }
Li Zefan34d52cb2011-03-29 13:46:06 +08006942 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
6943 GFP_NOFS);
6944 if (!cache->free_space_ctl) {
6945 kfree(cache);
6946 ret = -ENOMEM;
6947 goto error;
6948 }
Chris Mason3e1ad542007-05-07 20:03:49 -04006949
Yan Zhengd2fb3432008-12-11 16:30:39 -05006950 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006951 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04006952 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006953 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006954 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04006955
Josef Bacik0af3d002010-06-21 14:48:16 -04006956 if (need_clear)
6957 cache->disk_cache_state = BTRFS_DC_CLEAR;
6958
Chris Mason5f39d392007-10-15 16:14:19 -04006959 read_extent_buffer(leaf, &cache->item,
6960 btrfs_item_ptr_offset(leaf, path->slots[0]),
6961 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04006962 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04006963
Chris Mason9078a3e2007-04-26 16:46:15 -04006964 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02006965 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006966 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04006967 cache->sectorsize = root->sectorsize;
6968
Li Zefan34d52cb2011-03-29 13:46:06 +08006969 btrfs_init_free_space_ctl(cache);
6970
Josef Bacik817d52f2009-07-13 21:29:25 -04006971 /*
Josef Bacik3c148742011-02-02 15:53:47 +00006972 * We need to exclude the super stripes now so that the space
6973 * info has super bytes accounted for, otherwise we'll think
6974 * we have more space than we actually do.
6975 */
6976 exclude_super_stripes(root, cache);
6977
6978 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04006979 * check for two cases, either we are full, and therefore
6980 * don't need to bother with the caching work since we won't
6981 * find any space, or we are empty, and we can just add all
6982 * the space in and be done with it. This saves us _alot_ of
6983 * time, particularly in the full case.
6984 */
6985 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04006986 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04006987 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04006988 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006989 } else if (btrfs_block_group_used(&cache->item) == 0) {
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;
6992 add_new_free_space(cache, root->fs_info,
6993 found_key.objectid,
6994 found_key.objectid +
6995 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04006996 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006997 }
Chris Mason96b51792007-10-15 16:15:19 -04006998
Chris Mason6324fbf2008-03-24 15:01:59 -04006999 ret = update_space_info(info, cache->flags, found_key.offset,
7000 btrfs_block_group_used(&cache->item),
7001 &space_info);
7002 BUG_ON(ret);
7003 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007004 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007005 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007006 spin_unlock(&cache->space_info->lock);
7007
Yan, Zhengb742bb82010-05-16 10:46:24 -04007008 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007009
Josef Bacik0f9dd462008-09-23 13:14:11 -04007010 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7011 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007012
7013 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007014 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04007015 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007016 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007017
7018 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7019 if (!(get_alloc_profile(root, space_info->flags) &
7020 (BTRFS_BLOCK_GROUP_RAID10 |
7021 BTRFS_BLOCK_GROUP_RAID1 |
7022 BTRFS_BLOCK_GROUP_DUP)))
7023 continue;
7024 /*
7025 * avoid allocating from un-mirrored block group if there are
7026 * mirrored block groups.
7027 */
7028 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007029 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007030 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007031 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007032 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007033
7034 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007035 ret = 0;
7036error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007037 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007038 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007039}
Chris Mason6324fbf2008-03-24 15:01:59 -04007040
7041int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7042 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007043 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007044 u64 size)
7045{
7046 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007047 struct btrfs_root *extent_root;
7048 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007049
7050 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007051
Chris Mason12fcfd22009-03-24 10:24:20 -04007052 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007053
Chris Mason8f18cf12008-04-25 16:53:30 -04007054 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007055 if (!cache)
7056 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08007057 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7058 GFP_NOFS);
7059 if (!cache->free_space_ctl) {
7060 kfree(cache);
7061 return -ENOMEM;
7062 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007063
Chris Masone17cade2008-04-15 15:41:47 -04007064 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007065 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007066 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007067 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007068 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04007069
Yan Zhengd2fb3432008-12-11 16:30:39 -05007070 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007071 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007072 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007073 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007074
Li Zefan34d52cb2011-03-29 13:46:06 +08007075 btrfs_init_free_space_ctl(cache);
7076
Chris Mason6324fbf2008-03-24 15:01:59 -04007077 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007078 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7079 cache->flags = type;
7080 btrfs_set_block_group_flags(&cache->item, type);
7081
Yan Zheng11833d62009-09-11 16:11:19 -04007082 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007083 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007084 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007085
Josef Bacik817d52f2009-07-13 21:29:25 -04007086 add_new_free_space(cache, root->fs_info, chunk_offset,
7087 chunk_offset + size);
7088
Yan Zheng11833d62009-09-11 16:11:19 -04007089 free_excluded_extents(root, cache);
7090
Chris Mason6324fbf2008-03-24 15:01:59 -04007091 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7092 &cache->space_info);
7093 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007094
7095 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007096 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007097 spin_unlock(&cache->space_info->lock);
7098
Yan, Zhengb742bb82010-05-16 10:46:24 -04007099 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007100
Josef Bacik0f9dd462008-09-23 13:14:11 -04007101 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7102 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007103
Chris Mason6324fbf2008-03-24 15:01:59 -04007104 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7105 sizeof(cache->item));
7106 BUG_ON(ret);
7107
Chris Masond18a2c42008-04-04 15:40:00 -04007108 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007109
Chris Mason6324fbf2008-03-24 15:01:59 -04007110 return 0;
7111}
Zheng Yan1a40e232008-09-26 10:09:34 -04007112
7113int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7114 struct btrfs_root *root, u64 group_start)
7115{
7116 struct btrfs_path *path;
7117 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007118 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04007119 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04007120 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04007121 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04007122 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04007123 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04007124
Zheng Yan1a40e232008-09-26 10:09:34 -04007125 root = root->fs_info->extent_root;
7126
7127 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7128 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007129 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007130
liubo9f7c43c2011-03-07 02:13:33 +00007131 /*
7132 * Free the reserved super bytes from this block group before
7133 * remove it.
7134 */
7135 free_excluded_extents(root, block_group);
7136
Zheng Yan1a40e232008-09-26 10:09:34 -04007137 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04007138 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7139 BTRFS_BLOCK_GROUP_RAID1 |
7140 BTRFS_BLOCK_GROUP_RAID10))
7141 factor = 2;
7142 else
7143 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007144
Chris Mason44fb5512009-06-04 15:34:51 -04007145 /* make sure this block group isn't part of an allocation cluster */
7146 cluster = &root->fs_info->data_alloc_cluster;
7147 spin_lock(&cluster->refill_lock);
7148 btrfs_return_cluster_to_free_space(block_group, cluster);
7149 spin_unlock(&cluster->refill_lock);
7150
7151 /*
7152 * make sure this block group isn't part of a metadata
7153 * allocation cluster
7154 */
7155 cluster = &root->fs_info->meta_alloc_cluster;
7156 spin_lock(&cluster->refill_lock);
7157 btrfs_return_cluster_to_free_space(block_group, cluster);
7158 spin_unlock(&cluster->refill_lock);
7159
Zheng Yan1a40e232008-09-26 10:09:34 -04007160 path = btrfs_alloc_path();
7161 BUG_ON(!path);
7162
Josef Bacik0af3d002010-06-21 14:48:16 -04007163 inode = lookup_free_space_inode(root, block_group, path);
7164 if (!IS_ERR(inode)) {
7165 btrfs_orphan_add(trans, inode);
7166 clear_nlink(inode);
7167 /* One for the block groups ref */
7168 spin_lock(&block_group->lock);
7169 if (block_group->iref) {
7170 block_group->iref = 0;
7171 block_group->inode = NULL;
7172 spin_unlock(&block_group->lock);
7173 iput(inode);
7174 } else {
7175 spin_unlock(&block_group->lock);
7176 }
7177 /* One for our lookup ref */
7178 iput(inode);
7179 }
7180
7181 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
7182 key.offset = block_group->key.objectid;
7183 key.type = 0;
7184
7185 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
7186 if (ret < 0)
7187 goto out;
7188 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02007189 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007190 if (ret == 0) {
7191 ret = btrfs_del_item(trans, tree_root, path);
7192 if (ret)
7193 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02007194 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007195 }
7196
Yan Zheng3dfdb932009-01-21 10:49:16 -05007197 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007198 rb_erase(&block_group->cache_node,
7199 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007200 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007201
Josef Bacik80eb2342008-10-29 14:49:05 -04007202 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007203 /*
7204 * we must use list_del_init so people can check to see if they
7205 * are still on the list after taking the semaphore
7206 */
7207 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007208 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007209
Josef Bacik817d52f2009-07-13 21:29:25 -04007210 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007211 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007212
7213 btrfs_remove_free_space_cache(block_group);
7214
Yan Zhengc146afa2008-11-12 14:34:12 -05007215 spin_lock(&block_group->space_info->lock);
7216 block_group->space_info->total_bytes -= block_group->key.offset;
7217 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04007218 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05007219 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007220
Josef Bacik0af3d002010-06-21 14:48:16 -04007221 memcpy(&key, &block_group->key, sizeof(key));
7222
Chris Mason283bb192009-07-24 16:30:55 -04007223 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007224
Chris Masonfa9c0d72009-04-03 09:47:43 -04007225 btrfs_put_block_group(block_group);
7226 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007227
7228 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7229 if (ret > 0)
7230 ret = -EIO;
7231 if (ret < 0)
7232 goto out;
7233
7234 ret = btrfs_del_item(trans, root, path);
7235out:
7236 btrfs_free_path(path);
7237 return ret;
7238}
liuboacce9522011-01-06 19:30:25 +08007239
liuboc59021f2011-03-07 02:13:14 +00007240int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
7241{
7242 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00007243 struct btrfs_super_block *disk_super;
7244 u64 features;
7245 u64 flags;
7246 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00007247 int ret;
7248
liubo1aba86d2011-04-08 08:44:37 +00007249 disk_super = &fs_info->super_copy;
7250 if (!btrfs_super_root(disk_super))
7251 return 1;
liuboc59021f2011-03-07 02:13:14 +00007252
liubo1aba86d2011-04-08 08:44:37 +00007253 features = btrfs_super_incompat_flags(disk_super);
7254 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
7255 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00007256
liubo1aba86d2011-04-08 08:44:37 +00007257 flags = BTRFS_BLOCK_GROUP_SYSTEM;
7258 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00007259 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00007260 goto out;
liuboc59021f2011-03-07 02:13:14 +00007261
liubo1aba86d2011-04-08 08:44:37 +00007262 if (mixed) {
7263 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
7264 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7265 } else {
7266 flags = BTRFS_BLOCK_GROUP_METADATA;
7267 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7268 if (ret)
7269 goto out;
7270
7271 flags = BTRFS_BLOCK_GROUP_DATA;
7272 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7273 }
7274out:
liuboc59021f2011-03-07 02:13:14 +00007275 return ret;
7276}
7277
liuboacce9522011-01-06 19:30:25 +08007278int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
7279{
7280 return unpin_extent_range(root, start, end);
7281}
7282
7283int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00007284 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08007285{
Li Dongyang5378e602011-03-24 10:24:27 +00007286 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08007287}
Li Dongyangf7039b12011-03-24 10:24:28 +00007288
7289int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
7290{
7291 struct btrfs_fs_info *fs_info = root->fs_info;
7292 struct btrfs_block_group_cache *cache = NULL;
7293 u64 group_trimmed;
7294 u64 start;
7295 u64 end;
7296 u64 trimmed = 0;
7297 int ret = 0;
7298
7299 cache = btrfs_lookup_block_group(fs_info, range->start);
7300
7301 while (cache) {
7302 if (cache->key.objectid >= (range->start + range->len)) {
7303 btrfs_put_block_group(cache);
7304 break;
7305 }
7306
7307 start = max(range->start, cache->key.objectid);
7308 end = min(range->start + range->len,
7309 cache->key.objectid + cache->key.offset);
7310
7311 if (end - start >= range->minlen) {
7312 if (!block_group_cache_done(cache)) {
7313 ret = cache_block_group(cache, NULL, root, 0);
7314 if (!ret)
7315 wait_block_group_cache_done(cache);
7316 }
7317 ret = btrfs_trim_block_group(cache,
7318 &group_trimmed,
7319 start,
7320 end,
7321 range->minlen);
7322
7323 trimmed += group_trimmed;
7324 if (ret) {
7325 btrfs_put_block_group(cache);
7326 break;
7327 }
7328 }
7329
7330 cache = next_block_group(fs_info->tree_root, cache);
7331 }
7332
7333 range->len = trimmed;
7334 return ret;
7335}