blob: 95ce8da63b280d57e337f2115801df763497155a [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 Masonfa9c0d792009-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
Josef Bacik11dfe352009-11-13 20:12:59 +000097void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
98{
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;
351 path->reada = 2;
352
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
Yan Zheng11833d62009-09-11 16:11:19 -0400382 caching_ctl->progress = last;
383 btrfs_release_path(extent_root, path);
384 up_read(&fs_info->extent_commit_sem);
385 mutex_unlock(&caching_ctl->mutex);
386 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400387 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400388 else
389 cond_resched();
390 goto again;
391 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400392
Yan Zheng11833d62009-09-11 16:11:19 -0400393 if (key.objectid < block_group->key.objectid) {
394 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400395 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400396 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400397
Chris Masone37c9e62007-05-09 20:13:14 -0400398 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400399 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400400 break;
Yan7d7d6062007-09-14 16:15:28 -0400401
Yan Zheng11833d62009-09-11 16:11:19 -0400402 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400403 total_found += add_new_free_space(block_group,
404 fs_info, last,
405 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400406 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400407
Yan Zheng11833d62009-09-11 16:11:19 -0400408 if (total_found > (1024 * 1024 * 2)) {
409 total_found = 0;
410 wake_up(&caching_ctl->wait);
411 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400412 }
Chris Masone37c9e62007-05-09 20:13:14 -0400413 path->slots[0]++;
414 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400415 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400416
417 total_found += add_new_free_space(block_group, fs_info, last,
418 block_group->key.objectid +
419 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400420 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400421
422 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400423 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400424 block_group->cached = BTRFS_CACHE_FINISHED;
425 spin_unlock(&block_group->lock);
426
Chris Mason54aa1f42007-06-22 14:16:25 -0400427err:
Chris Masone37c9e62007-05-09 20:13:14 -0400428 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400429 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400430
Yan Zheng11833d62009-09-11 16:11:19 -0400431 free_excluded_extents(extent_root, block_group);
432
433 mutex_unlock(&caching_ctl->mutex);
434 wake_up(&caching_ctl->wait);
435
436 put_caching_control(caching_ctl);
437 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000438 btrfs_put_block_group(block_group);
439
Josef Bacik817d52f2009-07-13 21:29:25 -0400440 return 0;
441}
442
Josef Bacik9d66e232010-08-25 16:54:15 -0400443static int cache_block_group(struct btrfs_block_group_cache *cache,
444 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500445 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400446 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400447{
Yan Zheng11833d62009-09-11 16:11:19 -0400448 struct btrfs_fs_info *fs_info = cache->fs_info;
449 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400450 struct task_struct *tsk;
451 int ret = 0;
452
Yan Zheng11833d62009-09-11 16:11:19 -0400453 smp_mb();
454 if (cache->cached != BTRFS_CACHE_NO)
455 return 0;
456
Josef Bacik9d66e232010-08-25 16:54:15 -0400457 /*
458 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500459 * to have the normal tree locking. Also if we are currently trying to
460 * allocate blocks for the tree root we can't do the fast caching since
461 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400462 */
Li Dongyangf7039b12011-03-24 10:24:28 +0000463 if (trans && (!trans->transaction->in_commit) &&
Josef Bacikb8399de2010-12-08 09:15:11 -0500464 (root && root != root->fs_info->tree_root)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400465 spin_lock(&cache->lock);
466 if (cache->cached != BTRFS_CACHE_NO) {
467 spin_unlock(&cache->lock);
468 return 0;
469 }
470 cache->cached = BTRFS_CACHE_STARTED;
471 spin_unlock(&cache->lock);
472
473 ret = load_free_space_cache(fs_info, cache);
474
475 spin_lock(&cache->lock);
476 if (ret == 1) {
477 cache->cached = BTRFS_CACHE_FINISHED;
478 cache->last_byte_to_unpin = (u64)-1;
479 } else {
480 cache->cached = BTRFS_CACHE_NO;
481 }
482 spin_unlock(&cache->lock);
Josef Bacik3c148742011-02-02 15:53:47 +0000483 if (ret == 1) {
484 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400485 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000486 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400487 }
488
489 if (load_cache_only)
490 return 0;
491
Miao Xiefc0e4a32011-03-24 11:41:21 +0000492 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -0400493 BUG_ON(!caching_ctl);
494
495 INIT_LIST_HEAD(&caching_ctl->list);
496 mutex_init(&caching_ctl->mutex);
497 init_waitqueue_head(&caching_ctl->wait);
498 caching_ctl->block_group = cache;
499 caching_ctl->progress = cache->key.objectid;
500 /* one for caching kthread, one for caching block group list */
501 atomic_set(&caching_ctl->count, 2);
502
Josef Bacik817d52f2009-07-13 21:29:25 -0400503 spin_lock(&cache->lock);
504 if (cache->cached != BTRFS_CACHE_NO) {
505 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400506 kfree(caching_ctl);
507 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400508 }
Yan Zheng11833d62009-09-11 16:11:19 -0400509 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400510 cache->cached = BTRFS_CACHE_STARTED;
511 spin_unlock(&cache->lock);
512
Yan Zheng11833d62009-09-11 16:11:19 -0400513 down_write(&fs_info->extent_commit_sem);
514 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
515 up_write(&fs_info->extent_commit_sem);
516
517 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000518 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400519
Josef Bacik817d52f2009-07-13 21:29:25 -0400520 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
521 cache->key.objectid);
522 if (IS_ERR(tsk)) {
523 ret = PTR_ERR(tsk);
524 printk(KERN_ERR "error running thread %d\n", ret);
525 BUG();
526 }
527
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400528 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400529}
530
Josef Bacik0f9dd462008-09-23 13:14:11 -0400531/*
532 * return the block group that starts at or after bytenr
533 */
Chris Masond3977122009-01-05 21:25:51 -0500534static struct btrfs_block_group_cache *
535btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400536{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400537 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400538
Josef Bacik0f9dd462008-09-23 13:14:11 -0400539 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400540
Josef Bacik0f9dd462008-09-23 13:14:11 -0400541 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400542}
543
Josef Bacik0f9dd462008-09-23 13:14:11 -0400544/*
Sankar P9f556842009-05-14 13:52:22 -0400545 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400546 */
Chris Masond3977122009-01-05 21:25:51 -0500547struct btrfs_block_group_cache *btrfs_lookup_block_group(
548 struct btrfs_fs_info *info,
549 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400550{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400551 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400552
Josef Bacik0f9dd462008-09-23 13:14:11 -0400553 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400554
Josef Bacik0f9dd462008-09-23 13:14:11 -0400555 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400556}
Chris Mason0b86a832008-03-24 15:01:56 -0400557
Josef Bacik0f9dd462008-09-23 13:14:11 -0400558static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
559 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400560{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400561 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400562 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400563
Yan, Zhengb742bb82010-05-16 10:46:24 -0400564 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
565 BTRFS_BLOCK_GROUP_METADATA;
566
Chris Mason4184ea72009-03-10 12:39:20 -0400567 rcu_read_lock();
568 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400569 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400570 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400571 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400572 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400573 }
Chris Mason4184ea72009-03-10 12:39:20 -0400574 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400575 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400576}
577
Chris Mason4184ea72009-03-10 12:39:20 -0400578/*
579 * after adding space to the filesystem, we need to clear the full flags
580 * on all the space infos.
581 */
582void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
583{
584 struct list_head *head = &info->space_info;
585 struct btrfs_space_info *found;
586
587 rcu_read_lock();
588 list_for_each_entry_rcu(found, head, list)
589 found->full = 0;
590 rcu_read_unlock();
591}
592
Josef Bacik80eb2342008-10-29 14:49:05 -0400593static u64 div_factor(u64 num, int factor)
594{
595 if (factor == 10)
596 return num;
597 num *= factor;
598 do_div(num, 10);
599 return num;
600}
601
Chris Masone5bc2452010-10-26 13:37:56 -0400602static u64 div_factor_fine(u64 num, int factor)
603{
604 if (factor == 100)
605 return num;
606 num *= factor;
607 do_div(num, 100);
608 return num;
609}
610
Yan Zhengd2fb3432008-12-11 16:30:39 -0500611u64 btrfs_find_block_group(struct btrfs_root *root,
612 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400613{
Chris Mason96b51792007-10-15 16:15:19 -0400614 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400615 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500616 u64 last = max(search_hint, search_start);
617 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400618 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500619 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400620 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400621again:
Zheng Yane8569812008-09-26 10:05:48 -0400622 while (1) {
623 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400624 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400625 break;
Chris Mason96b51792007-10-15 16:15:19 -0400626
Chris Masonc286ac42008-07-22 23:06:41 -0400627 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400628 last = cache->key.objectid + cache->key.offset;
629 used = btrfs_block_group_used(&cache->item);
630
Yan Zhengd2fb3432008-12-11 16:30:39 -0500631 if ((full_search || !cache->ro) &&
632 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400633 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500634 div_factor(cache->key.offset, factor)) {
635 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400636 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400637 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400638 goto found;
639 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400640 }
Chris Masonc286ac42008-07-22 23:06:41 -0400641 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400642 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400643 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400644 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400645 if (!wrapped) {
646 last = search_start;
647 wrapped = 1;
648 goto again;
649 }
650 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400651 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400652 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400653 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400654 goto again;
655 }
Chris Masonbe744172007-05-06 10:15:01 -0400656found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500657 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400658}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400659
Chris Masone02119d2008-09-05 16:13:11 -0400660/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400661int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400662{
663 int ret;
664 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400665 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400666
Zheng Yan31840ae2008-09-23 13:14:14 -0400667 path = btrfs_alloc_path();
668 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400669 key.objectid = start;
670 key.offset = len;
671 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
672 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
673 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400674 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500675 return ret;
676}
677
Chris Masond8d5f3e2007-12-11 12:42:00 -0500678/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400679 * helper function to lookup reference count and flags of extent.
680 *
681 * the head node for delayed ref is used to store the sum of all the
682 * reference count modifications queued up in the rbtree. the head
683 * node may also store the extent flags to set. This way you can check
684 * to see what the reference count and extent flags would be if all of
685 * the delayed refs are not processed.
686 */
687int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
688 struct btrfs_root *root, u64 bytenr,
689 u64 num_bytes, u64 *refs, u64 *flags)
690{
691 struct btrfs_delayed_ref_head *head;
692 struct btrfs_delayed_ref_root *delayed_refs;
693 struct btrfs_path *path;
694 struct btrfs_extent_item *ei;
695 struct extent_buffer *leaf;
696 struct btrfs_key key;
697 u32 item_size;
698 u64 num_refs;
699 u64 extent_flags;
700 int ret;
701
702 path = btrfs_alloc_path();
703 if (!path)
704 return -ENOMEM;
705
706 key.objectid = bytenr;
707 key.type = BTRFS_EXTENT_ITEM_KEY;
708 key.offset = num_bytes;
709 if (!trans) {
710 path->skip_locking = 1;
711 path->search_commit_root = 1;
712 }
713again:
714 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
715 &key, path, 0, 0);
716 if (ret < 0)
717 goto out_free;
718
719 if (ret == 0) {
720 leaf = path->nodes[0];
721 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
722 if (item_size >= sizeof(*ei)) {
723 ei = btrfs_item_ptr(leaf, path->slots[0],
724 struct btrfs_extent_item);
725 num_refs = btrfs_extent_refs(leaf, ei);
726 extent_flags = btrfs_extent_flags(leaf, ei);
727 } else {
728#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
729 struct btrfs_extent_item_v0 *ei0;
730 BUG_ON(item_size != sizeof(*ei0));
731 ei0 = btrfs_item_ptr(leaf, path->slots[0],
732 struct btrfs_extent_item_v0);
733 num_refs = btrfs_extent_refs_v0(leaf, ei0);
734 /* FIXME: this isn't correct for data */
735 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
736#else
737 BUG();
738#endif
739 }
740 BUG_ON(num_refs == 0);
741 } else {
742 num_refs = 0;
743 extent_flags = 0;
744 ret = 0;
745 }
746
747 if (!trans)
748 goto out;
749
750 delayed_refs = &trans->transaction->delayed_refs;
751 spin_lock(&delayed_refs->lock);
752 head = btrfs_find_delayed_ref_head(trans, bytenr);
753 if (head) {
754 if (!mutex_trylock(&head->mutex)) {
755 atomic_inc(&head->node.refs);
756 spin_unlock(&delayed_refs->lock);
757
758 btrfs_release_path(root->fs_info->extent_root, path);
759
760 mutex_lock(&head->mutex);
761 mutex_unlock(&head->mutex);
762 btrfs_put_delayed_ref(&head->node);
763 goto again;
764 }
765 if (head->extent_op && head->extent_op->update_flags)
766 extent_flags |= head->extent_op->flags_to_set;
767 else
768 BUG_ON(num_refs == 0);
769
770 num_refs += head->node.ref_mod;
771 mutex_unlock(&head->mutex);
772 }
773 spin_unlock(&delayed_refs->lock);
774out:
775 WARN_ON(num_refs == 0);
776 if (refs)
777 *refs = num_refs;
778 if (flags)
779 *flags = extent_flags;
780out_free:
781 btrfs_free_path(path);
782 return ret;
783}
784
785/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500786 * Back reference rules. Back refs have three main goals:
787 *
788 * 1) differentiate between all holders of references to an extent so that
789 * when a reference is dropped we can make sure it was a valid reference
790 * before freeing the extent.
791 *
792 * 2) Provide enough information to quickly find the holders of an extent
793 * if we notice a given block is corrupted or bad.
794 *
795 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
796 * maintenance. This is actually the same as #2, but with a slightly
797 * different use case.
798 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400799 * There are two kinds of back refs. The implicit back refs is optimized
800 * for pointers in non-shared tree blocks. For a given pointer in a block,
801 * back refs of this kind provide information about the block's owner tree
802 * and the pointer's key. These information allow us to find the block by
803 * b-tree searching. The full back refs is for pointers in tree blocks not
804 * referenced by their owner trees. The location of tree block is recorded
805 * in the back refs. Actually the full back refs is generic, and can be
806 * used in all cases the implicit back refs is used. The major shortcoming
807 * of the full back refs is its overhead. Every time a tree block gets
808 * COWed, we have to update back refs entry for all pointers in it.
809 *
810 * For a newly allocated tree block, we use implicit back refs for
811 * pointers in it. This means most tree related operations only involve
812 * implicit back refs. For a tree block created in old transaction, the
813 * only way to drop a reference to it is COW it. So we can detect the
814 * event that tree block loses its owner tree's reference and do the
815 * back refs conversion.
816 *
817 * When a tree block is COW'd through a tree, there are four cases:
818 *
819 * The reference count of the block is one and the tree is the block's
820 * owner tree. Nothing to do in this case.
821 *
822 * The reference count of the block is one and the tree is not the
823 * block's owner tree. In this case, full back refs is used for pointers
824 * in the block. Remove these full back refs, add implicit back refs for
825 * every pointers in the new block.
826 *
827 * The reference count of the block is greater than one and the tree is
828 * the block's owner tree. In this case, implicit back refs is used for
829 * pointers in the block. Add full back refs for every pointers in the
830 * block, increase lower level extents' reference counts. The original
831 * implicit back refs are entailed to the new block.
832 *
833 * The reference count of the block is greater than one and the tree is
834 * not the block's owner tree. Add implicit back refs for every pointer in
835 * the new block, increase lower level extents' reference count.
836 *
837 * Back Reference Key composing:
838 *
839 * The key objectid corresponds to the first byte in the extent,
840 * The key type is used to differentiate between types of back refs.
841 * There are different meanings of the key offset for different types
842 * of back refs.
843 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500844 * File extents can be referenced by:
845 *
846 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400847 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500848 * - different offsets inside a file (bookend extents in file.c)
849 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400850 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500851 *
852 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500853 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400854 * - original offset in the file
855 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400856 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857 * The key offset for the implicit back refs is hash of the first
858 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500859 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400860 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500861 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400862 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500863 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400864 * The key offset for the implicit back refs is the first byte of
865 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500866 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400867 * When a file extent is allocated, The implicit back refs is used.
868 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500869 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400870 * (root_key.objectid, inode objectid, offset in file, 1)
871 *
872 * When a file extent is removed file truncation, we find the
873 * corresponding implicit back refs and check the following fields:
874 *
875 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500876 *
877 * Btree extents can be referenced by:
878 *
879 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500880 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881 * Both the implicit back refs and the full back refs for tree blocks
882 * only consist of key. The key offset for the implicit back refs is
883 * objectid of block's owner tree. The key offset for the full back refs
884 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500885 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886 * When implicit back refs is used, information about the lowest key and
887 * level of the tree block are required. These information are stored in
888 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500889 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400890
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400891#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
892static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
893 struct btrfs_root *root,
894 struct btrfs_path *path,
895 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500896{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400897 struct btrfs_extent_item *item;
898 struct btrfs_extent_item_v0 *ei0;
899 struct btrfs_extent_ref_v0 *ref0;
900 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400901 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902 struct btrfs_key key;
903 struct btrfs_key found_key;
904 u32 new_size = sizeof(*item);
905 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500906 int ret;
907
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400908 leaf = path->nodes[0];
909 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500910
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
912 ei0 = btrfs_item_ptr(leaf, path->slots[0],
913 struct btrfs_extent_item_v0);
914 refs = btrfs_extent_refs_v0(leaf, ei0);
915
916 if (owner == (u64)-1) {
917 while (1) {
918 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
919 ret = btrfs_next_leaf(root, path);
920 if (ret < 0)
921 return ret;
922 BUG_ON(ret > 0);
923 leaf = path->nodes[0];
924 }
925 btrfs_item_key_to_cpu(leaf, &found_key,
926 path->slots[0]);
927 BUG_ON(key.objectid != found_key.objectid);
928 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
929 path->slots[0]++;
930 continue;
931 }
932 ref0 = btrfs_item_ptr(leaf, path->slots[0],
933 struct btrfs_extent_ref_v0);
934 owner = btrfs_ref_objectid_v0(leaf, ref0);
935 break;
936 }
937 }
938 btrfs_release_path(root, path);
939
940 if (owner < BTRFS_FIRST_FREE_OBJECTID)
941 new_size += sizeof(*bi);
942
943 new_size -= sizeof(*ei0);
944 ret = btrfs_search_slot(trans, root, &key, path,
945 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400946 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400947 return ret;
948 BUG_ON(ret);
949
950 ret = btrfs_extend_item(trans, root, path, new_size);
951 BUG_ON(ret);
952
953 leaf = path->nodes[0];
954 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
955 btrfs_set_extent_refs(leaf, item, refs);
956 /* FIXME: get real generation */
957 btrfs_set_extent_generation(leaf, item, 0);
958 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
959 btrfs_set_extent_flags(leaf, item,
960 BTRFS_EXTENT_FLAG_TREE_BLOCK |
961 BTRFS_BLOCK_FLAG_FULL_BACKREF);
962 bi = (struct btrfs_tree_block_info *)(item + 1);
963 /* FIXME: get first key of the block */
964 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
965 btrfs_set_tree_block_level(leaf, bi, (int)owner);
966 } else {
967 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
968 }
969 btrfs_mark_buffer_dirty(leaf);
970 return 0;
971}
972#endif
973
974static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
975{
976 u32 high_crc = ~(u32)0;
977 u32 low_crc = ~(u32)0;
978 __le64 lenum;
979
980 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100981 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400982 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100983 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400984 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100985 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986
987 return ((u64)high_crc << 31) ^ (u64)low_crc;
988}
989
990static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
991 struct btrfs_extent_data_ref *ref)
992{
993 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
994 btrfs_extent_data_ref_objectid(leaf, ref),
995 btrfs_extent_data_ref_offset(leaf, ref));
996}
997
998static int match_extent_data_ref(struct extent_buffer *leaf,
999 struct btrfs_extent_data_ref *ref,
1000 u64 root_objectid, u64 owner, u64 offset)
1001{
1002 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1003 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1004 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1005 return 0;
1006 return 1;
1007}
1008
1009static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1010 struct btrfs_root *root,
1011 struct btrfs_path *path,
1012 u64 bytenr, u64 parent,
1013 u64 root_objectid,
1014 u64 owner, u64 offset)
1015{
1016 struct btrfs_key key;
1017 struct btrfs_extent_data_ref *ref;
1018 struct extent_buffer *leaf;
1019 u32 nritems;
1020 int ret;
1021 int recow;
1022 int err = -ENOENT;
1023
1024 key.objectid = bytenr;
1025 if (parent) {
1026 key.type = BTRFS_SHARED_DATA_REF_KEY;
1027 key.offset = parent;
1028 } else {
1029 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1030 key.offset = hash_extent_data_ref(root_objectid,
1031 owner, offset);
1032 }
1033again:
1034 recow = 0;
1035 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1036 if (ret < 0) {
1037 err = ret;
1038 goto fail;
1039 }
1040
1041 if (parent) {
1042 if (!ret)
1043 return 0;
1044#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1045 key.type = BTRFS_EXTENT_REF_V0_KEY;
1046 btrfs_release_path(root, path);
1047 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1048 if (ret < 0) {
1049 err = ret;
1050 goto fail;
1051 }
1052 if (!ret)
1053 return 0;
1054#endif
1055 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001056 }
1057
1058 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001059 nritems = btrfs_header_nritems(leaf);
1060 while (1) {
1061 if (path->slots[0] >= nritems) {
1062 ret = btrfs_next_leaf(root, path);
1063 if (ret < 0)
1064 err = ret;
1065 if (ret)
1066 goto fail;
1067
1068 leaf = path->nodes[0];
1069 nritems = btrfs_header_nritems(leaf);
1070 recow = 1;
1071 }
1072
1073 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1074 if (key.objectid != bytenr ||
1075 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1076 goto fail;
1077
1078 ref = btrfs_item_ptr(leaf, path->slots[0],
1079 struct btrfs_extent_data_ref);
1080
1081 if (match_extent_data_ref(leaf, ref, root_objectid,
1082 owner, offset)) {
1083 if (recow) {
1084 btrfs_release_path(root, path);
1085 goto again;
1086 }
1087 err = 0;
1088 break;
1089 }
1090 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001091 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001092fail:
1093 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001094}
1095
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001096static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1097 struct btrfs_root *root,
1098 struct btrfs_path *path,
1099 u64 bytenr, u64 parent,
1100 u64 root_objectid, u64 owner,
1101 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001102{
1103 struct btrfs_key key;
1104 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001105 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001106 u32 num_refs;
1107 int ret;
1108
1109 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001110 if (parent) {
1111 key.type = BTRFS_SHARED_DATA_REF_KEY;
1112 key.offset = parent;
1113 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001114 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001115 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1116 key.offset = hash_extent_data_ref(root_objectid,
1117 owner, offset);
1118 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001119 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001120
1121 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1122 if (ret && ret != -EEXIST)
1123 goto fail;
1124
1125 leaf = path->nodes[0];
1126 if (parent) {
1127 struct btrfs_shared_data_ref *ref;
1128 ref = btrfs_item_ptr(leaf, path->slots[0],
1129 struct btrfs_shared_data_ref);
1130 if (ret == 0) {
1131 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1132 } else {
1133 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1134 num_refs += refs_to_add;
1135 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1136 }
1137 } else {
1138 struct btrfs_extent_data_ref *ref;
1139 while (ret == -EEXIST) {
1140 ref = btrfs_item_ptr(leaf, path->slots[0],
1141 struct btrfs_extent_data_ref);
1142 if (match_extent_data_ref(leaf, ref, root_objectid,
1143 owner, offset))
1144 break;
1145 btrfs_release_path(root, path);
1146 key.offset++;
1147 ret = btrfs_insert_empty_item(trans, root, path, &key,
1148 size);
1149 if (ret && ret != -EEXIST)
1150 goto fail;
1151
1152 leaf = path->nodes[0];
1153 }
1154 ref = btrfs_item_ptr(leaf, path->slots[0],
1155 struct btrfs_extent_data_ref);
1156 if (ret == 0) {
1157 btrfs_set_extent_data_ref_root(leaf, ref,
1158 root_objectid);
1159 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1160 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1161 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1162 } else {
1163 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1164 num_refs += refs_to_add;
1165 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1166 }
1167 }
1168 btrfs_mark_buffer_dirty(leaf);
1169 ret = 0;
1170fail:
Chris Mason7bb86312007-12-11 09:25:06 -05001171 btrfs_release_path(root, path);
1172 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001173}
1174
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001175static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1176 struct btrfs_root *root,
1177 struct btrfs_path *path,
1178 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001179{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001180 struct btrfs_key key;
1181 struct btrfs_extent_data_ref *ref1 = NULL;
1182 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001183 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001184 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001185 int ret = 0;
1186
1187 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001188 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1189
1190 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1191 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1192 struct btrfs_extent_data_ref);
1193 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1194 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1195 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1196 struct btrfs_shared_data_ref);
1197 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1198#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1199 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1200 struct btrfs_extent_ref_v0 *ref0;
1201 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1202 struct btrfs_extent_ref_v0);
1203 num_refs = btrfs_ref_count_v0(leaf, ref0);
1204#endif
1205 } else {
1206 BUG();
1207 }
1208
Chris Mason56bec292009-03-13 10:10:06 -04001209 BUG_ON(num_refs < refs_to_drop);
1210 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001211
Zheng Yan31840ae2008-09-23 13:14:14 -04001212 if (num_refs == 0) {
1213 ret = btrfs_del_item(trans, root, path);
1214 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001215 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1216 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1217 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1218 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1219#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1220 else {
1221 struct btrfs_extent_ref_v0 *ref0;
1222 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1223 struct btrfs_extent_ref_v0);
1224 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1225 }
1226#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001227 btrfs_mark_buffer_dirty(leaf);
1228 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001229 return ret;
1230}
1231
1232static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1233 struct btrfs_path *path,
1234 struct btrfs_extent_inline_ref *iref)
1235{
1236 struct btrfs_key key;
1237 struct extent_buffer *leaf;
1238 struct btrfs_extent_data_ref *ref1;
1239 struct btrfs_shared_data_ref *ref2;
1240 u32 num_refs = 0;
1241
1242 leaf = path->nodes[0];
1243 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1244 if (iref) {
1245 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1246 BTRFS_EXTENT_DATA_REF_KEY) {
1247 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1248 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1249 } else {
1250 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1251 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1252 }
1253 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1254 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1255 struct btrfs_extent_data_ref);
1256 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1257 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1258 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1259 struct btrfs_shared_data_ref);
1260 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1261#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1262 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1263 struct btrfs_extent_ref_v0 *ref0;
1264 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1265 struct btrfs_extent_ref_v0);
1266 num_refs = btrfs_ref_count_v0(leaf, ref0);
1267#endif
1268 } else {
1269 WARN_ON(1);
1270 }
1271 return num_refs;
1272}
1273
1274static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1275 struct btrfs_root *root,
1276 struct btrfs_path *path,
1277 u64 bytenr, u64 parent,
1278 u64 root_objectid)
1279{
1280 struct btrfs_key key;
1281 int ret;
1282
1283 key.objectid = bytenr;
1284 if (parent) {
1285 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1286 key.offset = parent;
1287 } else {
1288 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1289 key.offset = root_objectid;
1290 }
1291
1292 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1293 if (ret > 0)
1294 ret = -ENOENT;
1295#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1296 if (ret == -ENOENT && parent) {
1297 btrfs_release_path(root, path);
1298 key.type = BTRFS_EXTENT_REF_V0_KEY;
1299 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1300 if (ret > 0)
1301 ret = -ENOENT;
1302 }
1303#endif
1304 return ret;
1305}
1306
1307static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1308 struct btrfs_root *root,
1309 struct btrfs_path *path,
1310 u64 bytenr, u64 parent,
1311 u64 root_objectid)
1312{
1313 struct btrfs_key key;
1314 int ret;
1315
1316 key.objectid = bytenr;
1317 if (parent) {
1318 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1319 key.offset = parent;
1320 } else {
1321 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1322 key.offset = root_objectid;
1323 }
1324
1325 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001326 btrfs_release_path(root, path);
1327 return ret;
1328}
1329
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001330static inline int extent_ref_type(u64 parent, u64 owner)
1331{
1332 int type;
1333 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1334 if (parent > 0)
1335 type = BTRFS_SHARED_BLOCK_REF_KEY;
1336 else
1337 type = BTRFS_TREE_BLOCK_REF_KEY;
1338 } else {
1339 if (parent > 0)
1340 type = BTRFS_SHARED_DATA_REF_KEY;
1341 else
1342 type = BTRFS_EXTENT_DATA_REF_KEY;
1343 }
1344 return type;
1345}
1346
Yan Zheng2c47e6052009-06-27 21:07:35 -04001347static int find_next_key(struct btrfs_path *path, int level,
1348 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001349
1350{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001351 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001352 if (!path->nodes[level])
1353 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001354 if (path->slots[level] + 1 >=
1355 btrfs_header_nritems(path->nodes[level]))
1356 continue;
1357 if (level == 0)
1358 btrfs_item_key_to_cpu(path->nodes[level], key,
1359 path->slots[level] + 1);
1360 else
1361 btrfs_node_key_to_cpu(path->nodes[level], key,
1362 path->slots[level] + 1);
1363 return 0;
1364 }
1365 return 1;
1366}
1367
1368/*
1369 * look for inline back ref. if back ref is found, *ref_ret is set
1370 * to the address of inline back ref, and 0 is returned.
1371 *
1372 * if back ref isn't found, *ref_ret is set to the address where it
1373 * should be inserted, and -ENOENT is returned.
1374 *
1375 * if insert is true and there are too many inline back refs, the path
1376 * points to the extent item, and -EAGAIN is returned.
1377 *
1378 * NOTE: inline back refs are ordered in the same way that back ref
1379 * items in the tree are ordered.
1380 */
1381static noinline_for_stack
1382int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1383 struct btrfs_root *root,
1384 struct btrfs_path *path,
1385 struct btrfs_extent_inline_ref **ref_ret,
1386 u64 bytenr, u64 num_bytes,
1387 u64 parent, u64 root_objectid,
1388 u64 owner, u64 offset, int insert)
1389{
1390 struct btrfs_key key;
1391 struct extent_buffer *leaf;
1392 struct btrfs_extent_item *ei;
1393 struct btrfs_extent_inline_ref *iref;
1394 u64 flags;
1395 u64 item_size;
1396 unsigned long ptr;
1397 unsigned long end;
1398 int extra_size;
1399 int type;
1400 int want;
1401 int ret;
1402 int err = 0;
1403
1404 key.objectid = bytenr;
1405 key.type = BTRFS_EXTENT_ITEM_KEY;
1406 key.offset = num_bytes;
1407
1408 want = extent_ref_type(parent, owner);
1409 if (insert) {
1410 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001411 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001412 } else
1413 extra_size = -1;
1414 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1415 if (ret < 0) {
1416 err = ret;
1417 goto out;
1418 }
1419 BUG_ON(ret);
1420
1421 leaf = path->nodes[0];
1422 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1423#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1424 if (item_size < sizeof(*ei)) {
1425 if (!insert) {
1426 err = -ENOENT;
1427 goto out;
1428 }
1429 ret = convert_extent_item_v0(trans, root, path, owner,
1430 extra_size);
1431 if (ret < 0) {
1432 err = ret;
1433 goto out;
1434 }
1435 leaf = path->nodes[0];
1436 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1437 }
1438#endif
1439 BUG_ON(item_size < sizeof(*ei));
1440
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001441 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1442 flags = btrfs_extent_flags(leaf, ei);
1443
1444 ptr = (unsigned long)(ei + 1);
1445 end = (unsigned long)ei + item_size;
1446
1447 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1448 ptr += sizeof(struct btrfs_tree_block_info);
1449 BUG_ON(ptr > end);
1450 } else {
1451 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1452 }
1453
1454 err = -ENOENT;
1455 while (1) {
1456 if (ptr >= end) {
1457 WARN_ON(ptr > end);
1458 break;
1459 }
1460 iref = (struct btrfs_extent_inline_ref *)ptr;
1461 type = btrfs_extent_inline_ref_type(leaf, iref);
1462 if (want < type)
1463 break;
1464 if (want > type) {
1465 ptr += btrfs_extent_inline_ref_size(type);
1466 continue;
1467 }
1468
1469 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1470 struct btrfs_extent_data_ref *dref;
1471 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1472 if (match_extent_data_ref(leaf, dref, root_objectid,
1473 owner, offset)) {
1474 err = 0;
1475 break;
1476 }
1477 if (hash_extent_data_ref_item(leaf, dref) <
1478 hash_extent_data_ref(root_objectid, owner, offset))
1479 break;
1480 } else {
1481 u64 ref_offset;
1482 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1483 if (parent > 0) {
1484 if (parent == ref_offset) {
1485 err = 0;
1486 break;
1487 }
1488 if (ref_offset < parent)
1489 break;
1490 } else {
1491 if (root_objectid == ref_offset) {
1492 err = 0;
1493 break;
1494 }
1495 if (ref_offset < root_objectid)
1496 break;
1497 }
1498 }
1499 ptr += btrfs_extent_inline_ref_size(type);
1500 }
1501 if (err == -ENOENT && insert) {
1502 if (item_size + extra_size >=
1503 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1504 err = -EAGAIN;
1505 goto out;
1506 }
1507 /*
1508 * To add new inline back ref, we have to make sure
1509 * there is no corresponding back ref item.
1510 * For simplicity, we just do not add new inline back
1511 * ref if there is any kind of item for this block
1512 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001513 if (find_next_key(path, 0, &key) == 0 &&
1514 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001515 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001516 err = -EAGAIN;
1517 goto out;
1518 }
1519 }
1520 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1521out:
Yan Zheng85d41982009-06-11 08:51:10 -04001522 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001523 path->keep_locks = 0;
1524 btrfs_unlock_up_safe(path, 1);
1525 }
1526 return err;
1527}
1528
1529/*
1530 * helper to add new inline back ref
1531 */
1532static noinline_for_stack
1533int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1534 struct btrfs_root *root,
1535 struct btrfs_path *path,
1536 struct btrfs_extent_inline_ref *iref,
1537 u64 parent, u64 root_objectid,
1538 u64 owner, u64 offset, int refs_to_add,
1539 struct btrfs_delayed_extent_op *extent_op)
1540{
1541 struct extent_buffer *leaf;
1542 struct btrfs_extent_item *ei;
1543 unsigned long ptr;
1544 unsigned long end;
1545 unsigned long item_offset;
1546 u64 refs;
1547 int size;
1548 int type;
1549 int ret;
1550
1551 leaf = path->nodes[0];
1552 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1553 item_offset = (unsigned long)iref - (unsigned long)ei;
1554
1555 type = extent_ref_type(parent, owner);
1556 size = btrfs_extent_inline_ref_size(type);
1557
1558 ret = btrfs_extend_item(trans, root, path, size);
1559 BUG_ON(ret);
1560
1561 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1562 refs = btrfs_extent_refs(leaf, ei);
1563 refs += refs_to_add;
1564 btrfs_set_extent_refs(leaf, ei, refs);
1565 if (extent_op)
1566 __run_delayed_extent_op(extent_op, leaf, ei);
1567
1568 ptr = (unsigned long)ei + item_offset;
1569 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1570 if (ptr < end - size)
1571 memmove_extent_buffer(leaf, ptr + size, ptr,
1572 end - size - ptr);
1573
1574 iref = (struct btrfs_extent_inline_ref *)ptr;
1575 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1576 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1577 struct btrfs_extent_data_ref *dref;
1578 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1579 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1580 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1581 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1582 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1583 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1584 struct btrfs_shared_data_ref *sref;
1585 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1586 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1587 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1588 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1589 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1590 } else {
1591 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1592 }
1593 btrfs_mark_buffer_dirty(leaf);
1594 return 0;
1595}
1596
1597static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1598 struct btrfs_root *root,
1599 struct btrfs_path *path,
1600 struct btrfs_extent_inline_ref **ref_ret,
1601 u64 bytenr, u64 num_bytes, u64 parent,
1602 u64 root_objectid, u64 owner, u64 offset)
1603{
1604 int ret;
1605
1606 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1607 bytenr, num_bytes, parent,
1608 root_objectid, owner, offset, 0);
1609 if (ret != -ENOENT)
1610 return ret;
1611
1612 btrfs_release_path(root, path);
1613 *ref_ret = NULL;
1614
1615 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1616 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1617 root_objectid);
1618 } else {
1619 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1620 root_objectid, owner, offset);
1621 }
1622 return ret;
1623}
1624
1625/*
1626 * helper to update/remove inline back ref
1627 */
1628static noinline_for_stack
1629int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1630 struct btrfs_root *root,
1631 struct btrfs_path *path,
1632 struct btrfs_extent_inline_ref *iref,
1633 int refs_to_mod,
1634 struct btrfs_delayed_extent_op *extent_op)
1635{
1636 struct extent_buffer *leaf;
1637 struct btrfs_extent_item *ei;
1638 struct btrfs_extent_data_ref *dref = NULL;
1639 struct btrfs_shared_data_ref *sref = NULL;
1640 unsigned long ptr;
1641 unsigned long end;
1642 u32 item_size;
1643 int size;
1644 int type;
1645 int ret;
1646 u64 refs;
1647
1648 leaf = path->nodes[0];
1649 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1650 refs = btrfs_extent_refs(leaf, ei);
1651 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1652 refs += refs_to_mod;
1653 btrfs_set_extent_refs(leaf, ei, refs);
1654 if (extent_op)
1655 __run_delayed_extent_op(extent_op, leaf, ei);
1656
1657 type = btrfs_extent_inline_ref_type(leaf, iref);
1658
1659 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1660 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1661 refs = btrfs_extent_data_ref_count(leaf, dref);
1662 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1663 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1664 refs = btrfs_shared_data_ref_count(leaf, sref);
1665 } else {
1666 refs = 1;
1667 BUG_ON(refs_to_mod != -1);
1668 }
1669
1670 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1671 refs += refs_to_mod;
1672
1673 if (refs > 0) {
1674 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1675 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1676 else
1677 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1678 } else {
1679 size = btrfs_extent_inline_ref_size(type);
1680 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1681 ptr = (unsigned long)iref;
1682 end = (unsigned long)ei + item_size;
1683 if (ptr + size < end)
1684 memmove_extent_buffer(leaf, ptr, ptr + size,
1685 end - ptr - size);
1686 item_size -= size;
1687 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1688 BUG_ON(ret);
1689 }
1690 btrfs_mark_buffer_dirty(leaf);
1691 return 0;
1692}
1693
1694static noinline_for_stack
1695int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1696 struct btrfs_root *root,
1697 struct btrfs_path *path,
1698 u64 bytenr, u64 num_bytes, u64 parent,
1699 u64 root_objectid, u64 owner,
1700 u64 offset, int refs_to_add,
1701 struct btrfs_delayed_extent_op *extent_op)
1702{
1703 struct btrfs_extent_inline_ref *iref;
1704 int ret;
1705
1706 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1707 bytenr, num_bytes, parent,
1708 root_objectid, owner, offset, 1);
1709 if (ret == 0) {
1710 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1711 ret = update_inline_extent_backref(trans, root, path, iref,
1712 refs_to_add, extent_op);
1713 } else if (ret == -ENOENT) {
1714 ret = setup_inline_extent_backref(trans, root, path, iref,
1715 parent, root_objectid,
1716 owner, offset, refs_to_add,
1717 extent_op);
1718 }
1719 return ret;
1720}
1721
1722static int insert_extent_backref(struct btrfs_trans_handle *trans,
1723 struct btrfs_root *root,
1724 struct btrfs_path *path,
1725 u64 bytenr, u64 parent, u64 root_objectid,
1726 u64 owner, u64 offset, int refs_to_add)
1727{
1728 int ret;
1729 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1730 BUG_ON(refs_to_add != 1);
1731 ret = insert_tree_block_ref(trans, root, path, bytenr,
1732 parent, root_objectid);
1733 } else {
1734 ret = insert_extent_data_ref(trans, root, path, bytenr,
1735 parent, root_objectid,
1736 owner, offset, refs_to_add);
1737 }
1738 return ret;
1739}
1740
1741static int remove_extent_backref(struct btrfs_trans_handle *trans,
1742 struct btrfs_root *root,
1743 struct btrfs_path *path,
1744 struct btrfs_extent_inline_ref *iref,
1745 int refs_to_drop, int is_data)
1746{
1747 int ret;
1748
1749 BUG_ON(!is_data && refs_to_drop != 1);
1750 if (iref) {
1751 ret = update_inline_extent_backref(trans, root, path, iref,
1752 -refs_to_drop, NULL);
1753 } else if (is_data) {
1754 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1755 } else {
1756 ret = btrfs_del_item(trans, root, path);
1757 }
1758 return ret;
1759}
1760
Li Dongyang5378e602011-03-24 10:24:27 +00001761static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001762 u64 start, u64 len)
1763{
Li Dongyang5378e602011-03-24 10:24:27 +00001764 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001765}
Chris Mason15916de2008-11-19 21:17:22 -05001766
Liu Hui1f3c79a2009-01-05 15:57:51 -05001767static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001768 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001769{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001770 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001771 u64 discarded_bytes = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001772 struct btrfs_multi_bio *multi = NULL;
1773
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001774
Liu Hui1f3c79a2009-01-05 15:57:51 -05001775 /* Tell the block device(s) that the sectors can be discarded */
Li Dongyang5378e602011-03-24 10:24:27 +00001776 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1777 bytenr, &num_bytes, &multi, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001778 if (!ret) {
1779 struct btrfs_bio_stripe *stripe = multi->stripes;
1780 int i;
1781
Liu Hui1f3c79a2009-01-05 15:57:51 -05001782
1783 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Li Dongyang5378e602011-03-24 10:24:27 +00001784 ret = btrfs_issue_discard(stripe->dev->bdev,
1785 stripe->physical,
1786 stripe->length);
1787 if (!ret)
1788 discarded_bytes += stripe->length;
1789 else if (ret != -EOPNOTSUPP)
1790 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001791 }
1792 kfree(multi);
1793 }
Li Dongyang5378e602011-03-24 10:24:27 +00001794 if (discarded_bytes && ret == -EOPNOTSUPP)
1795 ret = 0;
1796
1797 if (actual_bytes)
1798 *actual_bytes = discarded_bytes;
1799
Liu Hui1f3c79a2009-01-05 15:57:51 -05001800
1801 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001802}
1803
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001804int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1805 struct btrfs_root *root,
1806 u64 bytenr, u64 num_bytes, u64 parent,
1807 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001808{
1809 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001810 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1811 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001812
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001813 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1814 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1815 parent, root_objectid, (int)owner,
1816 BTRFS_ADD_DELAYED_REF, NULL);
1817 } else {
1818 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1819 parent, root_objectid, owner, offset,
1820 BTRFS_ADD_DELAYED_REF, NULL);
1821 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001822 return ret;
1823}
1824
Chris Mason925baed2008-06-25 16:01:30 -04001825static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001826 struct btrfs_root *root,
1827 u64 bytenr, u64 num_bytes,
1828 u64 parent, u64 root_objectid,
1829 u64 owner, u64 offset, int refs_to_add,
1830 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001831{
Chris Mason5caf2a02007-04-02 11:20:42 -04001832 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001833 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001834 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001835 u64 refs;
1836 int ret;
1837 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001838
Chris Mason5caf2a02007-04-02 11:20:42 -04001839 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001840 if (!path)
1841 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001842
Chris Mason3c12ac72008-04-21 12:01:38 -04001843 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001844 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001845 /* this will setup the path even if it fails to insert the back ref */
1846 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1847 path, bytenr, num_bytes, parent,
1848 root_objectid, owner, offset,
1849 refs_to_add, extent_op);
1850 if (ret == 0)
1851 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001852
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001853 if (ret != -EAGAIN) {
1854 err = ret;
1855 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001856 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001857
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001858 leaf = path->nodes[0];
1859 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1860 refs = btrfs_extent_refs(leaf, item);
1861 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1862 if (extent_op)
1863 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001864
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001865 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001866 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001867
Chris Mason3c12ac72008-04-21 12:01:38 -04001868 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001869 path->leave_spinning = 1;
1870
Chris Mason56bec292009-03-13 10:10:06 -04001871 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001872 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001873 path, bytenr, parent, root_objectid,
1874 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001875 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876out:
Chris Mason74493f72007-12-11 09:25:06 -05001877 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001879}
1880
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001881static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1882 struct btrfs_root *root,
1883 struct btrfs_delayed_ref_node *node,
1884 struct btrfs_delayed_extent_op *extent_op,
1885 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001886{
Chris Mason56bec292009-03-13 10:10:06 -04001887 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001888 struct btrfs_delayed_data_ref *ref;
1889 struct btrfs_key ins;
1890 u64 parent = 0;
1891 u64 ref_root = 0;
1892 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001893
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001894 ins.objectid = node->bytenr;
1895 ins.offset = node->num_bytes;
1896 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001897
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001898 ref = btrfs_delayed_node_to_data_ref(node);
1899 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1900 parent = ref->parent;
1901 else
1902 ref_root = ref->root;
1903
1904 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1905 if (extent_op) {
1906 BUG_ON(extent_op->update_key);
1907 flags |= extent_op->flags_to_set;
1908 }
1909 ret = alloc_reserved_file_extent(trans, root,
1910 parent, ref_root, flags,
1911 ref->objectid, ref->offset,
1912 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001913 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1914 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1915 node->num_bytes, parent,
1916 ref_root, ref->objectid,
1917 ref->offset, node->ref_mod,
1918 extent_op);
1919 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1920 ret = __btrfs_free_extent(trans, root, node->bytenr,
1921 node->num_bytes, parent,
1922 ref_root, ref->objectid,
1923 ref->offset, node->ref_mod,
1924 extent_op);
1925 } else {
1926 BUG();
1927 }
Chris Mason56bec292009-03-13 10:10:06 -04001928 return ret;
1929}
1930
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001931static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1932 struct extent_buffer *leaf,
1933 struct btrfs_extent_item *ei)
1934{
1935 u64 flags = btrfs_extent_flags(leaf, ei);
1936 if (extent_op->update_flags) {
1937 flags |= extent_op->flags_to_set;
1938 btrfs_set_extent_flags(leaf, ei, flags);
1939 }
1940
1941 if (extent_op->update_key) {
1942 struct btrfs_tree_block_info *bi;
1943 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1944 bi = (struct btrfs_tree_block_info *)(ei + 1);
1945 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1946 }
1947}
1948
1949static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1950 struct btrfs_root *root,
1951 struct btrfs_delayed_ref_node *node,
1952 struct btrfs_delayed_extent_op *extent_op)
1953{
1954 struct btrfs_key key;
1955 struct btrfs_path *path;
1956 struct btrfs_extent_item *ei;
1957 struct extent_buffer *leaf;
1958 u32 item_size;
1959 int ret;
1960 int err = 0;
1961
1962 path = btrfs_alloc_path();
1963 if (!path)
1964 return -ENOMEM;
1965
1966 key.objectid = node->bytenr;
1967 key.type = BTRFS_EXTENT_ITEM_KEY;
1968 key.offset = node->num_bytes;
1969
1970 path->reada = 1;
1971 path->leave_spinning = 1;
1972 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1973 path, 0, 1);
1974 if (ret < 0) {
1975 err = ret;
1976 goto out;
1977 }
1978 if (ret > 0) {
1979 err = -EIO;
1980 goto out;
1981 }
1982
1983 leaf = path->nodes[0];
1984 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1985#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1986 if (item_size < sizeof(*ei)) {
1987 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1988 path, (u64)-1, 0);
1989 if (ret < 0) {
1990 err = ret;
1991 goto out;
1992 }
1993 leaf = path->nodes[0];
1994 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1995 }
1996#endif
1997 BUG_ON(item_size < sizeof(*ei));
1998 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1999 __run_delayed_extent_op(extent_op, leaf, ei);
2000
2001 btrfs_mark_buffer_dirty(leaf);
2002out:
2003 btrfs_free_path(path);
2004 return err;
2005}
2006
2007static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2008 struct btrfs_root *root,
2009 struct btrfs_delayed_ref_node *node,
2010 struct btrfs_delayed_extent_op *extent_op,
2011 int insert_reserved)
2012{
2013 int ret = 0;
2014 struct btrfs_delayed_tree_ref *ref;
2015 struct btrfs_key ins;
2016 u64 parent = 0;
2017 u64 ref_root = 0;
2018
2019 ins.objectid = node->bytenr;
2020 ins.offset = node->num_bytes;
2021 ins.type = BTRFS_EXTENT_ITEM_KEY;
2022
2023 ref = btrfs_delayed_node_to_tree_ref(node);
2024 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2025 parent = ref->parent;
2026 else
2027 ref_root = ref->root;
2028
2029 BUG_ON(node->ref_mod != 1);
2030 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2031 BUG_ON(!extent_op || !extent_op->update_flags ||
2032 !extent_op->update_key);
2033 ret = alloc_reserved_tree_block(trans, root,
2034 parent, ref_root,
2035 extent_op->flags_to_set,
2036 &extent_op->key,
2037 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002038 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2039 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2040 node->num_bytes, parent, ref_root,
2041 ref->level, 0, 1, extent_op);
2042 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2043 ret = __btrfs_free_extent(trans, root, node->bytenr,
2044 node->num_bytes, parent, ref_root,
2045 ref->level, 0, 1, extent_op);
2046 } else {
2047 BUG();
2048 }
2049 return ret;
2050}
2051
Chris Mason56bec292009-03-13 10:10:06 -04002052/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002053static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2054 struct btrfs_root *root,
2055 struct btrfs_delayed_ref_node *node,
2056 struct btrfs_delayed_extent_op *extent_op,
2057 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002058{
Josef Bacikeb099672009-02-12 09:27:38 -05002059 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002060 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002061 struct btrfs_delayed_ref_head *head;
2062 /*
2063 * we've hit the end of the chain and we were supposed
2064 * to insert this extent into the tree. But, it got
2065 * deleted before we ever needed to insert it, so all
2066 * we have to do is clean up the accounting
2067 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002068 BUG_ON(extent_op);
2069 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002070 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002071 btrfs_pin_extent(root, node->bytenr,
2072 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002073 if (head->is_data) {
2074 ret = btrfs_del_csums(trans, root,
2075 node->bytenr,
2076 node->num_bytes);
2077 BUG_ON(ret);
2078 }
Chris Mason56bec292009-03-13 10:10:06 -04002079 }
Chris Mason56bec292009-03-13 10:10:06 -04002080 mutex_unlock(&head->mutex);
2081 return 0;
2082 }
Josef Bacikeb099672009-02-12 09:27:38 -05002083
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002084 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2085 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2086 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2087 insert_reserved);
2088 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2089 node->type == BTRFS_SHARED_DATA_REF_KEY)
2090 ret = run_delayed_data_ref(trans, root, node, extent_op,
2091 insert_reserved);
2092 else
2093 BUG();
2094 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002095}
2096
Chris Mason56bec292009-03-13 10:10:06 -04002097static noinline struct btrfs_delayed_ref_node *
2098select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002099{
Chris Mason56bec292009-03-13 10:10:06 -04002100 struct rb_node *node;
2101 struct btrfs_delayed_ref_node *ref;
2102 int action = BTRFS_ADD_DELAYED_REF;
2103again:
2104 /*
2105 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2106 * this prevents ref count from going down to zero when
2107 * there still are pending delayed ref.
2108 */
2109 node = rb_prev(&head->node.rb_node);
2110 while (1) {
2111 if (!node)
2112 break;
2113 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2114 rb_node);
2115 if (ref->bytenr != head->node.bytenr)
2116 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002117 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002118 return ref;
2119 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002120 }
Chris Mason56bec292009-03-13 10:10:06 -04002121 if (action == BTRFS_ADD_DELAYED_REF) {
2122 action = BTRFS_DROP_DELAYED_REF;
2123 goto again;
2124 }
2125 return NULL;
2126}
2127
Chris Masonc3e69d52009-03-13 10:17:05 -04002128static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2129 struct btrfs_root *root,
2130 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002131{
Chris Mason56bec292009-03-13 10:10:06 -04002132 struct btrfs_delayed_ref_root *delayed_refs;
2133 struct btrfs_delayed_ref_node *ref;
2134 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002135 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002136 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002137 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002138 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002139
2140 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002141 while (1) {
2142 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002143 /* pick a new head ref from the cluster list */
2144 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002145 break;
Chris Mason56bec292009-03-13 10:10:06 -04002146
Chris Masonc3e69d52009-03-13 10:17:05 -04002147 locked_ref = list_entry(cluster->next,
2148 struct btrfs_delayed_ref_head, cluster);
2149
2150 /* grab the lock that says we are going to process
2151 * all the refs for this head */
2152 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2153
2154 /*
2155 * we may have dropped the spin lock to get the head
2156 * mutex lock, and that might have given someone else
2157 * time to free the head. If that's true, it has been
2158 * removed from our list and we can move on.
2159 */
2160 if (ret == -EAGAIN) {
2161 locked_ref = NULL;
2162 count++;
2163 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002164 }
2165 }
2166
2167 /*
2168 * record the must insert reserved flag before we
2169 * drop the spin lock.
2170 */
2171 must_insert_reserved = locked_ref->must_insert_reserved;
2172 locked_ref->must_insert_reserved = 0;
2173
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002174 extent_op = locked_ref->extent_op;
2175 locked_ref->extent_op = NULL;
2176
Chris Mason56bec292009-03-13 10:10:06 -04002177 /*
2178 * locked_ref is the head node, so we have to go one
2179 * node back for any delayed ref updates
2180 */
Chris Mason56bec292009-03-13 10:10:06 -04002181 ref = select_delayed_ref(locked_ref);
2182 if (!ref) {
2183 /* All delayed refs have been processed, Go ahead
2184 * and send the head node to run_one_delayed_ref,
2185 * so that any accounting fixes can happen
2186 */
2187 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002188
2189 if (extent_op && must_insert_reserved) {
2190 kfree(extent_op);
2191 extent_op = NULL;
2192 }
2193
2194 if (extent_op) {
2195 spin_unlock(&delayed_refs->lock);
2196
2197 ret = run_delayed_extent_op(trans, root,
2198 ref, extent_op);
2199 BUG_ON(ret);
2200 kfree(extent_op);
2201
2202 cond_resched();
2203 spin_lock(&delayed_refs->lock);
2204 continue;
2205 }
2206
Chris Masonc3e69d52009-03-13 10:17:05 -04002207 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002208 locked_ref = NULL;
2209 }
2210
2211 ref->in_tree = 0;
2212 rb_erase(&ref->rb_node, &delayed_refs->root);
2213 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002214
Chris Mason56bec292009-03-13 10:10:06 -04002215 spin_unlock(&delayed_refs->lock);
2216
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002217 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002218 must_insert_reserved);
2219 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002220
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002221 btrfs_put_delayed_ref(ref);
2222 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002223 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002224
Chris Mason1887be62009-03-13 10:11:24 -04002225 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002226 spin_lock(&delayed_refs->lock);
2227 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002228 return count;
2229}
2230
2231/*
2232 * this starts processing the delayed reference count updates and
2233 * extent insertions we have queued up so far. count can be
2234 * 0, which means to process everything in the tree at the start
2235 * of the run (but not newly added entries), or it can be some target
2236 * number you'd like to process.
2237 */
2238int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2239 struct btrfs_root *root, unsigned long count)
2240{
2241 struct rb_node *node;
2242 struct btrfs_delayed_ref_root *delayed_refs;
2243 struct btrfs_delayed_ref_node *ref;
2244 struct list_head cluster;
2245 int ret;
2246 int run_all = count == (unsigned long)-1;
2247 int run_most = 0;
2248
2249 if (root == root->fs_info->extent_root)
2250 root = root->fs_info->tree_root;
2251
2252 delayed_refs = &trans->transaction->delayed_refs;
2253 INIT_LIST_HEAD(&cluster);
2254again:
2255 spin_lock(&delayed_refs->lock);
2256 if (count == 0) {
2257 count = delayed_refs->num_entries * 2;
2258 run_most = 1;
2259 }
2260 while (1) {
2261 if (!(run_all || run_most) &&
2262 delayed_refs->num_heads_ready < 64)
2263 break;
2264
2265 /*
2266 * go find something we can process in the rbtree. We start at
2267 * the beginning of the tree, and then build a cluster
2268 * of refs to process starting at the first one we are able to
2269 * lock
2270 */
2271 ret = btrfs_find_ref_cluster(trans, &cluster,
2272 delayed_refs->run_delayed_start);
2273 if (ret)
2274 break;
2275
2276 ret = run_clustered_refs(trans, root, &cluster);
2277 BUG_ON(ret < 0);
2278
2279 count -= min_t(unsigned long, ret, count);
2280
2281 if (count == 0)
2282 break;
2283 }
2284
Chris Mason56bec292009-03-13 10:10:06 -04002285 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002286 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002287 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002288 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002289 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002290
2291 while (node) {
2292 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2293 rb_node);
2294 if (btrfs_delayed_ref_is_head(ref)) {
2295 struct btrfs_delayed_ref_head *head;
2296
2297 head = btrfs_delayed_node_to_head(ref);
2298 atomic_inc(&ref->refs);
2299
2300 spin_unlock(&delayed_refs->lock);
2301 mutex_lock(&head->mutex);
2302 mutex_unlock(&head->mutex);
2303
2304 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002305 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002306 goto again;
2307 }
2308 node = rb_next(node);
2309 }
2310 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002311 schedule_timeout(1);
2312 goto again;
2313 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002314out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002315 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002316 return 0;
2317}
2318
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002319int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2320 struct btrfs_root *root,
2321 u64 bytenr, u64 num_bytes, u64 flags,
2322 int is_data)
2323{
2324 struct btrfs_delayed_extent_op *extent_op;
2325 int ret;
2326
2327 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2328 if (!extent_op)
2329 return -ENOMEM;
2330
2331 extent_op->flags_to_set = flags;
2332 extent_op->update_flags = 1;
2333 extent_op->update_key = 0;
2334 extent_op->is_data = is_data ? 1 : 0;
2335
2336 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2337 if (ret)
2338 kfree(extent_op);
2339 return ret;
2340}
2341
2342static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2343 struct btrfs_root *root,
2344 struct btrfs_path *path,
2345 u64 objectid, u64 offset, u64 bytenr)
2346{
2347 struct btrfs_delayed_ref_head *head;
2348 struct btrfs_delayed_ref_node *ref;
2349 struct btrfs_delayed_data_ref *data_ref;
2350 struct btrfs_delayed_ref_root *delayed_refs;
2351 struct rb_node *node;
2352 int ret = 0;
2353
2354 ret = -ENOENT;
2355 delayed_refs = &trans->transaction->delayed_refs;
2356 spin_lock(&delayed_refs->lock);
2357 head = btrfs_find_delayed_ref_head(trans, bytenr);
2358 if (!head)
2359 goto out;
2360
2361 if (!mutex_trylock(&head->mutex)) {
2362 atomic_inc(&head->node.refs);
2363 spin_unlock(&delayed_refs->lock);
2364
2365 btrfs_release_path(root->fs_info->extent_root, path);
2366
2367 mutex_lock(&head->mutex);
2368 mutex_unlock(&head->mutex);
2369 btrfs_put_delayed_ref(&head->node);
2370 return -EAGAIN;
2371 }
2372
2373 node = rb_prev(&head->node.rb_node);
2374 if (!node)
2375 goto out_unlock;
2376
2377 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2378
2379 if (ref->bytenr != bytenr)
2380 goto out_unlock;
2381
2382 ret = 1;
2383 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2384 goto out_unlock;
2385
2386 data_ref = btrfs_delayed_node_to_data_ref(ref);
2387
2388 node = rb_prev(node);
2389 if (node) {
2390 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2391 if (ref->bytenr == bytenr)
2392 goto out_unlock;
2393 }
2394
2395 if (data_ref->root != root->root_key.objectid ||
2396 data_ref->objectid != objectid || data_ref->offset != offset)
2397 goto out_unlock;
2398
2399 ret = 0;
2400out_unlock:
2401 mutex_unlock(&head->mutex);
2402out:
2403 spin_unlock(&delayed_refs->lock);
2404 return ret;
2405}
2406
2407static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2408 struct btrfs_root *root,
2409 struct btrfs_path *path,
2410 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002411{
2412 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002413 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002414 struct btrfs_extent_data_ref *ref;
2415 struct btrfs_extent_inline_ref *iref;
2416 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002417 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002418 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002419 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002420
Chris Masonbe20aa92007-12-17 20:14:01 -05002421 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002422 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002423 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002424
Chris Masonbe20aa92007-12-17 20:14:01 -05002425 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2426 if (ret < 0)
2427 goto out;
2428 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002429
2430 ret = -ENOENT;
2431 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002432 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002433
Zheng Yan31840ae2008-09-23 13:14:14 -04002434 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002435 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002436 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002437
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002438 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002439 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002440
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002441 ret = 1;
2442 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2443#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2444 if (item_size < sizeof(*ei)) {
2445 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2446 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002447 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002448#endif
2449 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2450
2451 if (item_size != sizeof(*ei) +
2452 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2453 goto out;
2454
2455 if (btrfs_extent_generation(leaf, ei) <=
2456 btrfs_root_last_snapshot(&root->root_item))
2457 goto out;
2458
2459 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2460 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2461 BTRFS_EXTENT_DATA_REF_KEY)
2462 goto out;
2463
2464 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2465 if (btrfs_extent_refs(leaf, ei) !=
2466 btrfs_extent_data_ref_count(leaf, ref) ||
2467 btrfs_extent_data_ref_root(leaf, ref) !=
2468 root->root_key.objectid ||
2469 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2470 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2471 goto out;
2472
Yan Zhengf321e492008-07-30 09:26:11 -04002473 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002474out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002475 return ret;
2476}
2477
2478int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2479 struct btrfs_root *root,
2480 u64 objectid, u64 offset, u64 bytenr)
2481{
2482 struct btrfs_path *path;
2483 int ret;
2484 int ret2;
2485
2486 path = btrfs_alloc_path();
2487 if (!path)
2488 return -ENOENT;
2489
2490 do {
2491 ret = check_committed_ref(trans, root, path, objectid,
2492 offset, bytenr);
2493 if (ret && ret != -ENOENT)
2494 goto out;
2495
2496 ret2 = check_delayed_ref(trans, root, path, objectid,
2497 offset, bytenr);
2498 } while (ret2 == -EAGAIN);
2499
2500 if (ret2 && ret2 != -ENOENT) {
2501 ret = ret2;
2502 goto out;
2503 }
2504
2505 if (ret != -ENOENT || ret2 != -ENOENT)
2506 ret = 0;
2507out:
Yan Zhengf321e492008-07-30 09:26:11 -04002508 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002509 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2510 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002511 return ret;
2512}
2513
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002514#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002515int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2516 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002517{
Chris Mason5f39d392007-10-15 16:14:19 -04002518 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002519 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002520 u64 root_gen;
2521 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002522 int i;
Chris Masondb945352007-10-15 16:15:53 -04002523 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002524 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002525 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002526
Chris Mason3768f362007-03-13 16:47:54 -04002527 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002528 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002529
Zheng Yane4657682008-09-26 10:04:53 -04002530 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2531 shared = 0;
2532 root_gen = root->root_key.offset;
2533 } else {
2534 shared = 1;
2535 root_gen = trans->transid - 1;
2536 }
2537
Chris Masondb945352007-10-15 16:15:53 -04002538 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002539 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002540
Zheng Yan31840ae2008-09-23 13:14:14 -04002541 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002542 struct btrfs_leaf_ref *ref;
2543 struct btrfs_extent_info *info;
2544
Zheng Yan31840ae2008-09-23 13:14:14 -04002545 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002546 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002547 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002548 goto out;
2549 }
2550
Zheng Yane4657682008-09-26 10:04:53 -04002551 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002552 ref->bytenr = buf->start;
2553 ref->owner = btrfs_header_owner(buf);
2554 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002555 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002556 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002557
Zheng Yan31840ae2008-09-23 13:14:14 -04002558 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002559 u64 disk_bytenr;
2560 btrfs_item_key_to_cpu(buf, &key, i);
2561 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2562 continue;
2563 fi = btrfs_item_ptr(buf, i,
2564 struct btrfs_file_extent_item);
2565 if (btrfs_file_extent_type(buf, fi) ==
2566 BTRFS_FILE_EXTENT_INLINE)
2567 continue;
2568 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2569 if (disk_bytenr == 0)
2570 continue;
2571
2572 info->bytenr = disk_bytenr;
2573 info->num_bytes =
2574 btrfs_file_extent_disk_num_bytes(buf, fi);
2575 info->objectid = key.objectid;
2576 info->offset = key.offset;
2577 info++;
2578 }
2579
Zheng Yane4657682008-09-26 10:04:53 -04002580 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002581 if (ret == -EEXIST && shared) {
2582 struct btrfs_leaf_ref *old;
2583 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2584 BUG_ON(!old);
2585 btrfs_remove_leaf_ref(root, old);
2586 btrfs_free_leaf_ref(root, old);
2587 ret = btrfs_add_leaf_ref(root, ref, shared);
2588 }
Yan Zheng31153d82008-07-28 15:32:19 -04002589 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002590 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002591 }
2592out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002593 return ret;
2594}
2595
Chris Masonb7a9f292009-02-04 09:23:45 -05002596/* when a block goes through cow, we update the reference counts of
2597 * everything that block points to. The internal pointers of the block
2598 * can be in just about any order, and it is likely to have clusters of
2599 * things that are close together and clusters of things that are not.
2600 *
2601 * To help reduce the seeks that come with updating all of these reference
2602 * counts, sort them by byte number before actual updates are done.
2603 *
2604 * struct refsort is used to match byte number to slot in the btree block.
2605 * we sort based on the byte number and then use the slot to actually
2606 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002607 *
2608 * struct refsort is smaller than strcut btrfs_item and smaller than
2609 * struct btrfs_key_ptr. Since we're currently limited to the page size
2610 * for a btree block, there's no way for a kmalloc of refsorts for a
2611 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002612 */
2613struct refsort {
2614 u64 bytenr;
2615 u32 slot;
2616};
2617
2618/*
2619 * for passing into sort()
2620 */
2621static int refsort_cmp(const void *a_void, const void *b_void)
2622{
2623 const struct refsort *a = a_void;
2624 const struct refsort *b = b_void;
2625
2626 if (a->bytenr < b->bytenr)
2627 return -1;
2628 if (a->bytenr > b->bytenr)
2629 return 1;
2630 return 0;
2631}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002632#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002633
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002634static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002635 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002636 struct extent_buffer *buf,
2637 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002638{
2639 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002640 u64 num_bytes;
2641 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002642 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002643 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002644 struct btrfs_key key;
2645 struct btrfs_file_extent_item *fi;
2646 int i;
2647 int level;
2648 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002649 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002650 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002651
2652 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002653 nritems = btrfs_header_nritems(buf);
2654 level = btrfs_header_level(buf);
2655
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002656 if (!root->ref_cows && level == 0)
2657 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002658
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002659 if (inc)
2660 process_func = btrfs_inc_extent_ref;
2661 else
2662 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002663
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002664 if (full_backref)
2665 parent = buf->start;
2666 else
2667 parent = 0;
2668
Zheng Yan31840ae2008-09-23 13:14:14 -04002669 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002670 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002671 btrfs_item_key_to_cpu(buf, &key, i);
2672 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002673 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002674 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002675 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002676 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002677 BTRFS_FILE_EXTENT_INLINE)
2678 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002679 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2680 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002681 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002682
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002683 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2684 key.offset -= btrfs_file_extent_offset(buf, fi);
2685 ret = process_func(trans, root, bytenr, num_bytes,
2686 parent, ref_root, key.objectid,
2687 key.offset);
2688 if (ret)
2689 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002690 } else {
2691 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002692 num_bytes = btrfs_level_size(root, level - 1);
2693 ret = process_func(trans, root, bytenr, num_bytes,
2694 parent, ref_root, level - 1, 0);
2695 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002696 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002697 }
2698 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002699 return 0;
2700fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002701 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002702 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002703}
2704
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002705int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2706 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002707{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002708 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2709}
Zheng Yan31840ae2008-09-23 13:14:14 -04002710
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002711int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2712 struct extent_buffer *buf, int full_backref)
2713{
2714 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002715}
2716
Chris Mason9078a3e2007-04-26 16:46:15 -04002717static int write_one_cache_group(struct btrfs_trans_handle *trans,
2718 struct btrfs_root *root,
2719 struct btrfs_path *path,
2720 struct btrfs_block_group_cache *cache)
2721{
2722 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002723 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002724 unsigned long bi;
2725 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002726
Chris Mason9078a3e2007-04-26 16:46:15 -04002727 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002728 if (ret < 0)
2729 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002730 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002731
2732 leaf = path->nodes[0];
2733 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2734 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2735 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002736 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002737fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002738 if (ret)
2739 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002740 return 0;
2741
2742}
2743
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002744static struct btrfs_block_group_cache *
2745next_block_group(struct btrfs_root *root,
2746 struct btrfs_block_group_cache *cache)
2747{
2748 struct rb_node *node;
2749 spin_lock(&root->fs_info->block_group_cache_lock);
2750 node = rb_next(&cache->cache_node);
2751 btrfs_put_block_group(cache);
2752 if (node) {
2753 cache = rb_entry(node, struct btrfs_block_group_cache,
2754 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002755 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002756 } else
2757 cache = NULL;
2758 spin_unlock(&root->fs_info->block_group_cache_lock);
2759 return cache;
2760}
2761
Josef Bacik0af3d002010-06-21 14:48:16 -04002762static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2763 struct btrfs_trans_handle *trans,
2764 struct btrfs_path *path)
2765{
2766 struct btrfs_root *root = block_group->fs_info->tree_root;
2767 struct inode *inode = NULL;
2768 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002769 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002770 int num_pages = 0;
2771 int retries = 0;
2772 int ret = 0;
2773
2774 /*
2775 * If this block group is smaller than 100 megs don't bother caching the
2776 * block group.
2777 */
2778 if (block_group->key.offset < (100 * 1024 * 1024)) {
2779 spin_lock(&block_group->lock);
2780 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2781 spin_unlock(&block_group->lock);
2782 return 0;
2783 }
2784
2785again:
2786 inode = lookup_free_space_inode(root, block_group, path);
2787 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2788 ret = PTR_ERR(inode);
2789 btrfs_release_path(root, path);
2790 goto out;
2791 }
2792
2793 if (IS_ERR(inode)) {
2794 BUG_ON(retries);
2795 retries++;
2796
2797 if (block_group->ro)
2798 goto out_free;
2799
2800 ret = create_free_space_inode(root, trans, block_group, path);
2801 if (ret)
2802 goto out_free;
2803 goto again;
2804 }
2805
2806 /*
2807 * We want to set the generation to 0, that way if anything goes wrong
2808 * from here on out we know not to trust this cache when we load up next
2809 * time.
2810 */
2811 BTRFS_I(inode)->generation = 0;
2812 ret = btrfs_update_inode(trans, root, inode);
2813 WARN_ON(ret);
2814
2815 if (i_size_read(inode) > 0) {
2816 ret = btrfs_truncate_free_space_cache(root, trans, path,
2817 inode);
2818 if (ret)
2819 goto out_put;
2820 }
2821
2822 spin_lock(&block_group->lock);
2823 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002824 /* We're not cached, don't bother trying to write stuff out */
2825 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002826 spin_unlock(&block_group->lock);
2827 goto out_put;
2828 }
2829 spin_unlock(&block_group->lock);
2830
2831 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2832 if (!num_pages)
2833 num_pages = 1;
2834
2835 /*
2836 * Just to make absolutely sure we have enough space, we're going to
2837 * preallocate 12 pages worth of space for each block group. In
2838 * practice we ought to use at most 8, but we need extra space so we can
2839 * add our header and have a terminator between the extents and the
2840 * bitmaps.
2841 */
2842 num_pages *= 16;
2843 num_pages *= PAGE_CACHE_SIZE;
2844
2845 ret = btrfs_check_data_free_space(inode, num_pages);
2846 if (ret)
2847 goto out_put;
2848
2849 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2850 num_pages, num_pages,
2851 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002852 if (!ret)
2853 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002854 btrfs_free_reserved_data_space(inode, num_pages);
2855out_put:
2856 iput(inode);
2857out_free:
2858 btrfs_release_path(root, path);
2859out:
2860 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002861 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002862 spin_unlock(&block_group->lock);
2863
2864 return ret;
2865}
2866
Chris Mason96b51792007-10-15 16:15:19 -04002867int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2868 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002869{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002870 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002871 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002872 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002873 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002874
2875 path = btrfs_alloc_path();
2876 if (!path)
2877 return -ENOMEM;
2878
Josef Bacik0af3d002010-06-21 14:48:16 -04002879again:
2880 while (1) {
2881 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2882 while (cache) {
2883 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2884 break;
2885 cache = next_block_group(root, cache);
2886 }
2887 if (!cache) {
2888 if (last == 0)
2889 break;
2890 last = 0;
2891 continue;
2892 }
2893 err = cache_save_setup(cache, trans, path);
2894 last = cache->key.objectid + cache->key.offset;
2895 btrfs_put_block_group(cache);
2896 }
2897
Chris Masond3977122009-01-05 21:25:51 -05002898 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002899 if (last == 0) {
2900 err = btrfs_run_delayed_refs(trans, root,
2901 (unsigned long)-1);
2902 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002903 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002904
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002905 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2906 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002907 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2908 btrfs_put_block_group(cache);
2909 goto again;
2910 }
2911
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002912 if (cache->dirty)
2913 break;
2914 cache = next_block_group(root, cache);
2915 }
2916 if (!cache) {
2917 if (last == 0)
2918 break;
2919 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002920 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002921 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002922
Josef Bacik0cb59c92010-07-02 12:14:14 -04002923 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2924 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002925 cache->dirty = 0;
2926 last = cache->key.objectid + cache->key.offset;
2927
2928 err = write_one_cache_group(trans, root, path, cache);
2929 BUG_ON(err);
2930 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002931 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002932
Josef Bacik0cb59c92010-07-02 12:14:14 -04002933 while (1) {
2934 /*
2935 * I don't think this is needed since we're just marking our
2936 * preallocated extent as written, but just in case it can't
2937 * hurt.
2938 */
2939 if (last == 0) {
2940 err = btrfs_run_delayed_refs(trans, root,
2941 (unsigned long)-1);
2942 BUG_ON(err);
2943 }
2944
2945 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2946 while (cache) {
2947 /*
2948 * Really this shouldn't happen, but it could if we
2949 * couldn't write the entire preallocated extent and
2950 * splitting the extent resulted in a new block.
2951 */
2952 if (cache->dirty) {
2953 btrfs_put_block_group(cache);
2954 goto again;
2955 }
2956 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2957 break;
2958 cache = next_block_group(root, cache);
2959 }
2960 if (!cache) {
2961 if (last == 0)
2962 break;
2963 last = 0;
2964 continue;
2965 }
2966
2967 btrfs_write_out_cache(root, trans, cache, path);
2968
2969 /*
2970 * If we didn't have an error then the cache state is still
2971 * NEED_WRITE, so we can set it to WRITTEN.
2972 */
2973 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2974 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2975 last = cache->key.objectid + cache->key.offset;
2976 btrfs_put_block_group(cache);
2977 }
2978
Chris Mason9078a3e2007-04-26 16:46:15 -04002979 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002980 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002981}
2982
Yan Zhengd2fb3432008-12-11 16:30:39 -05002983int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2984{
2985 struct btrfs_block_group_cache *block_group;
2986 int readonly = 0;
2987
2988 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2989 if (!block_group || block_group->ro)
2990 readonly = 1;
2991 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002992 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002993 return readonly;
2994}
2995
Chris Mason593060d2008-03-25 16:50:33 -04002996static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2997 u64 total_bytes, u64 bytes_used,
2998 struct btrfs_space_info **space_info)
2999{
3000 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003001 int i;
3002 int factor;
3003
3004 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3005 BTRFS_BLOCK_GROUP_RAID10))
3006 factor = 2;
3007 else
3008 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003009
3010 found = __find_space_info(info, flags);
3011 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003012 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003013 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003014 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003015 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003016 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003017 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003018 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003019 *space_info = found;
3020 return 0;
3021 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003022 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003023 if (!found)
3024 return -ENOMEM;
3025
Yan, Zhengb742bb82010-05-16 10:46:24 -04003026 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3027 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003028 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003029 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04003030 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
3031 BTRFS_BLOCK_GROUP_SYSTEM |
3032 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04003033 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003034 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003035 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003036 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003037 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003038 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003039 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003040 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003041 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003042 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003043 found->chunk_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003044 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003045 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04003046 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04003047 return 0;
3048}
3049
Chris Mason8790d502008-04-03 16:29:03 -04003050static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3051{
3052 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04003053 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04003054 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04003055 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04003056 if (extra_flags) {
3057 if (flags & BTRFS_BLOCK_GROUP_DATA)
3058 fs_info->avail_data_alloc_bits |= extra_flags;
3059 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3060 fs_info->avail_metadata_alloc_bits |= extra_flags;
3061 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3062 fs_info->avail_system_alloc_bits |= extra_flags;
3063 }
3064}
Chris Mason593060d2008-03-25 16:50:33 -04003065
Yan Zheng2b820322008-11-17 21:11:30 -05003066u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003067{
Chris Masoncd02dca2010-12-13 14:56:23 -05003068 /*
3069 * we add in the count of missing devices because we want
3070 * to make sure that any RAID levels on a degraded FS
3071 * continue to be honored.
3072 */
3073 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3074 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04003075
3076 if (num_devices == 1)
3077 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3078 if (num_devices < 4)
3079 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3080
Chris Masonec44a352008-04-28 15:29:52 -04003081 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3082 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003083 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003084 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003085 }
Chris Masonec44a352008-04-28 15:29:52 -04003086
3087 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003088 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003089 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003090 }
Chris Masonec44a352008-04-28 15:29:52 -04003091
3092 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3093 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3094 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3095 (flags & BTRFS_BLOCK_GROUP_DUP)))
3096 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3097 return flags;
3098}
3099
Yan, Zhengb742bb82010-05-16 10:46:24 -04003100static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003101{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003102 if (flags & BTRFS_BLOCK_GROUP_DATA)
3103 flags |= root->fs_info->avail_data_alloc_bits &
3104 root->fs_info->data_alloc_profile;
3105 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3106 flags |= root->fs_info->avail_system_alloc_bits &
3107 root->fs_info->system_alloc_profile;
3108 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3109 flags |= root->fs_info->avail_metadata_alloc_bits &
3110 root->fs_info->metadata_alloc_profile;
3111 return btrfs_reduce_alloc_profile(root, flags);
3112}
Josef Bacik6a632092009-02-20 11:00:09 -05003113
Miao Xie6d07bce2011-01-05 10:07:31 +00003114u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003115{
3116 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003117
Yan, Zhengb742bb82010-05-16 10:46:24 -04003118 if (data)
3119 flags = BTRFS_BLOCK_GROUP_DATA;
3120 else if (root == root->fs_info->chunk_root)
3121 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3122 else
3123 flags = BTRFS_BLOCK_GROUP_METADATA;
3124
3125 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003126}
3127
3128void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3129{
Josef Bacik6a632092009-02-20 11:00:09 -05003130 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003131 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003132}
3133
3134/*
3135 * This will check the space that the inode allocates from to make sure we have
3136 * enough space for bytes.
3137 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003138int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003139{
3140 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003141 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003142 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003143 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003144
3145 /* make sure bytes are sectorsize aligned */
3146 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3147
Li Zefan82d59022011-04-20 10:33:24 +08003148 if (root == root->fs_info->tree_root ||
3149 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003150 alloc_chunk = 0;
3151 committed = 1;
3152 }
3153
Josef Bacik6a632092009-02-20 11:00:09 -05003154 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003155 if (!data_sinfo)
3156 goto alloc;
3157
Josef Bacik6a632092009-02-20 11:00:09 -05003158again:
3159 /* make sure we have enough space to handle the data first */
3160 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003161 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3162 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3163 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003164
3165 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003166 struct btrfs_trans_handle *trans;
3167
Josef Bacik6a632092009-02-20 11:00:09 -05003168 /*
3169 * if we don't have enough free bytes in this space then we need
3170 * to alloc a new chunk.
3171 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003172 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003173 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003174
Chris Mason0e4f8f82011-04-15 16:05:44 -04003175 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003176 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003177alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003178 alloc_target = btrfs_get_alloc_profile(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003179 trans = btrfs_join_transaction(root, 1);
3180 if (IS_ERR(trans))
3181 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003182
3183 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3184 bytes + 2 * 1024 * 1024,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003185 alloc_target,
3186 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003187 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003188 if (ret < 0) {
3189 if (ret != -ENOSPC)
3190 return ret;
3191 else
3192 goto commit_trans;
3193 }
Chris Mason33b4d472009-09-22 14:45:50 -04003194
3195 if (!data_sinfo) {
3196 btrfs_set_inode_space_info(root, inode);
3197 data_sinfo = BTRFS_I(inode)->space_info;
3198 }
Josef Bacik6a632092009-02-20 11:00:09 -05003199 goto again;
3200 }
3201 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003202
3203 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003204commit_trans:
Sage Weildd7e0b72009-09-29 18:38:44 -04003205 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003206 committed = 1;
3207 trans = btrfs_join_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003208 if (IS_ERR(trans))
3209 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003210 ret = btrfs_commit_transaction(trans, root);
3211 if (ret)
3212 return ret;
3213 goto again;
3214 }
3215
Chris Mason933b5852010-05-26 11:31:00 -04003216#if 0 /* I hope we never need this code again, just in case */
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003217 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3218 "%llu bytes_reserved, " "%llu bytes_pinned, "
3219 "%llu bytes_readonly, %llu may use %llu total\n",
3220 (unsigned long long)bytes,
Joel Becker21380932009-04-21 12:38:29 -07003221 (unsigned long long)data_sinfo->bytes_used,
3222 (unsigned long long)data_sinfo->bytes_reserved,
3223 (unsigned long long)data_sinfo->bytes_pinned,
3224 (unsigned long long)data_sinfo->bytes_readonly,
3225 (unsigned long long)data_sinfo->bytes_may_use,
3226 (unsigned long long)data_sinfo->total_bytes);
Chris Mason933b5852010-05-26 11:31:00 -04003227#endif
Josef Bacik6a632092009-02-20 11:00:09 -05003228 return -ENOSPC;
3229 }
3230 data_sinfo->bytes_may_use += bytes;
3231 BTRFS_I(inode)->reserved_bytes += bytes;
3232 spin_unlock(&data_sinfo->lock);
3233
Josef Bacik9ed74f22009-09-11 16:12:44 -04003234 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003235}
3236
3237/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003238 * called when we are clearing an delalloc extent from the
3239 * inode's io_tree or there was an error for whatever reason
3240 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003241 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003242void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003243{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003244 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003245 struct btrfs_space_info *data_sinfo;
3246
3247 /* make sure bytes are sectorsize aligned */
3248 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3249
3250 data_sinfo = BTRFS_I(inode)->space_info;
3251 spin_lock(&data_sinfo->lock);
3252 data_sinfo->bytes_may_use -= bytes;
3253 BTRFS_I(inode)->reserved_bytes -= bytes;
3254 spin_unlock(&data_sinfo->lock);
3255}
3256
Josef Bacik97e728d2009-04-21 17:40:57 -04003257static void force_metadata_allocation(struct btrfs_fs_info *info)
3258{
3259 struct list_head *head = &info->space_info;
3260 struct btrfs_space_info *found;
3261
3262 rcu_read_lock();
3263 list_for_each_entry_rcu(found, head, list) {
3264 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003265 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003266 }
3267 rcu_read_unlock();
3268}
3269
Chris Masone5bc2452010-10-26 13:37:56 -04003270static int should_alloc_chunk(struct btrfs_root *root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003271 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3272 int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003273{
3274 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003275 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003276 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003277
Chris Mason0e4f8f82011-04-15 16:05:44 -04003278 if (force == CHUNK_ALLOC_FORCE)
3279 return 1;
3280
3281 /*
3282 * in limited mode, we want to have some free space up to
3283 * about 1% of the FS size.
3284 */
3285 if (force == CHUNK_ALLOC_LIMITED) {
3286 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3287 thresh = max_t(u64, 64 * 1024 * 1024,
3288 div_factor_fine(thresh, 1));
3289
3290 if (num_bytes - num_allocated < thresh)
3291 return 1;
3292 }
3293
3294 /*
3295 * we have two similar checks here, one based on percentage
3296 * and once based on a hard number of 256MB. The idea
3297 * is that if we have a good amount of free
3298 * room, don't allocate a chunk. A good mount is
3299 * less than 80% utilized of the chunks we have allocated,
3300 * or more than 256MB free
3301 */
3302 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003303 return 0;
3304
Chris Mason0e4f8f82011-04-15 16:05:44 -04003305 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
Yan, Zheng424499d2010-05-16 10:46:25 -04003306 return 0;
3307
Chris Masone5bc2452010-10-26 13:37:56 -04003308 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003309
3310 /* 256MB or 5% of the FS */
Chris Masone5bc2452010-10-26 13:37:56 -04003311 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3312
3313 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003314 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003315 return 1;
3316}
3317
Chris Mason6324fbf2008-03-24 15:01:59 -04003318static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3319 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003320 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003321{
3322 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003323 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003324 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003325 int ret = 0;
3326
Yan Zheng2b820322008-11-17 21:11:30 -05003327 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003328
Chris Mason6324fbf2008-03-24 15:01:59 -04003329 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003330 if (!space_info) {
3331 ret = update_space_info(extent_root->fs_info, flags,
3332 0, 0, &space_info);
3333 BUG_ON(ret);
3334 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003335 BUG_ON(!space_info);
3336
Josef Bacik6d741192011-04-11 20:20:11 -04003337again:
Josef Bacik25179202008-10-29 14:49:05 -04003338 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003339 if (space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003340 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003341 if (space_info->full) {
3342 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003343 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003344 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003345
Chris Mason0e4f8f82011-04-15 16:05:44 -04003346 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003347 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003348 return 0;
3349 } else if (space_info->chunk_alloc) {
3350 wait_for_alloc = 1;
3351 } else {
3352 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003353 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003354
Josef Bacik25179202008-10-29 14:49:05 -04003355 spin_unlock(&space_info->lock);
3356
Josef Bacik6d741192011-04-11 20:20:11 -04003357 mutex_lock(&fs_info->chunk_mutex);
3358
3359 /*
3360 * The chunk_mutex is held throughout the entirety of a chunk
3361 * allocation, so once we've acquired the chunk_mutex we know that the
3362 * other guy is done and we need to recheck and see if we should
3363 * allocate.
3364 */
3365 if (wait_for_alloc) {
3366 mutex_unlock(&fs_info->chunk_mutex);
3367 wait_for_alloc = 0;
3368 goto again;
3369 }
3370
Josef Bacik97e728d2009-04-21 17:40:57 -04003371 /*
Josef Bacik67377732010-09-16 16:19:09 -04003372 * If we have mixed data/metadata chunks we want to make sure we keep
3373 * allocating mixed chunks instead of individual chunks.
3374 */
3375 if (btrfs_mixed_space_info(space_info))
3376 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3377
3378 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003379 * if we're doing a data chunk, go ahead and make sure that
3380 * we keep a reasonable number of metadata chunks allocated in the
3381 * FS as well.
3382 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003383 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003384 fs_info->data_chunk_allocations++;
3385 if (!(fs_info->data_chunk_allocations %
3386 fs_info->metadata_ratio))
3387 force_metadata_allocation(fs_info);
3388 }
3389
Yan Zheng2b820322008-11-17 21:11:30 -05003390 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003391 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003392 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003393 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003394 else
3395 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003396
Chris Mason0e4f8f82011-04-15 16:05:44 -04003397 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003398 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003399 spin_unlock(&space_info->lock);
Yan Zhengc146afa2008-11-12 14:34:12 -05003400 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003401 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003402}
3403
Yan, Zheng5da9d012010-05-16 10:46:25 -04003404/*
3405 * shrink metadata reservation for delalloc
3406 */
3407static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003408 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003409{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003410 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003411 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003412 u64 reserved;
3413 u64 max_reclaim;
3414 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003415 long time_left;
Chris Masonbf9022e2010-10-26 13:40:45 -04003416 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003417 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003418 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003419
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003420 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003421 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003422
3423 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003424 reserved = space_info->bytes_reserved;
Chris Mason36e39c42011-03-12 07:08:42 -05003425 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003426
3427 if (reserved == 0)
3428 return 0;
3429
3430 max_reclaim = min(reserved, to_reclaim);
3431
Josef Bacikb1953bc2011-01-21 21:10:01 +00003432 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003433 /* have the flusher threads jump in and do some IO */
3434 smp_mb();
3435 nr_pages = min_t(unsigned long, nr_pages,
3436 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3437 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003438
Josef Bacik0019f102010-10-15 15:18:40 -04003439 spin_lock(&space_info->lock);
Chris Mason36e39c42011-03-12 07:08:42 -05003440 if (reserved > space_info->bytes_reserved)
Josef Bacik0019f102010-10-15 15:18:40 -04003441 reclaimed += reserved - space_info->bytes_reserved;
3442 reserved = space_info->bytes_reserved;
3443 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003444
Chris Mason36e39c42011-03-12 07:08:42 -05003445 loops++;
3446
Yan, Zheng5da9d012010-05-16 10:46:25 -04003447 if (reserved == 0 || reclaimed >= max_reclaim)
3448 break;
3449
3450 if (trans && trans->transaction->blocked)
3451 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003452
Chris Mason36e39c42011-03-12 07:08:42 -05003453 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003454
3455 /* We were interrupted, exit */
3456 if (time_left)
3457 break;
3458
Chris Mason36e39c42011-03-12 07:08:42 -05003459 /* we've kicked the IO a few times, if anything has been freed,
3460 * exit. There is no sense in looping here for a long time
3461 * when we really need to commit the transaction, or there are
3462 * just too many writers without enough free space
3463 */
3464
3465 if (loops > 3) {
3466 smp_mb();
3467 if (progress != space_info->reservation_progress)
3468 break;
3469 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003470
Yan, Zheng5da9d012010-05-16 10:46:25 -04003471 }
3472 return reclaimed >= to_reclaim;
3473}
3474
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003475/*
3476 * Retries tells us how many times we've called reserve_metadata_bytes. The
3477 * idea is if this is the first call (retries == 0) then we will add to our
3478 * reserved count if we can't make the allocation in order to hold our place
3479 * while we go and try and free up space. That way for retries > 1 we don't try
3480 * and add space, we just check to see if the amount of unused space is >= the
3481 * total space, meaning that our reservation is valid.
3482 *
3483 * However if we don't intend to retry this reservation, pass -1 as retries so
3484 * that it short circuits this logic.
3485 */
3486static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3487 struct btrfs_root *root,
3488 struct btrfs_block_rsv *block_rsv,
3489 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003490{
3491 struct btrfs_space_info *space_info = block_rsv->space_info;
3492 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003493 u64 num_bytes = orig_bytes;
3494 int retries = 0;
3495 int ret = 0;
3496 bool reserved = false;
Josef Bacik38227932010-10-26 12:52:53 -04003497 bool committed = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003498
3499again:
3500 ret = -ENOSPC;
3501 if (reserved)
3502 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003503
3504 spin_lock(&space_info->lock);
3505 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003506 space_info->bytes_pinned + space_info->bytes_readonly +
3507 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003508
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003509 /*
3510 * The idea here is that we've not already over-reserved the block group
3511 * then we can go ahead and save our reservation first and then start
3512 * flushing if we need to. Otherwise if we've already overcommitted
3513 * lets start flushing stuff first and then come back and try to make
3514 * our reservation.
3515 */
3516 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003517 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003518 if (unused >= num_bytes) {
3519 if (!reserved)
3520 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003521 ret = 0;
3522 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003523 /*
3524 * Ok set num_bytes to orig_bytes since we aren't
3525 * overocmmitted, this way we only try and reclaim what
3526 * we need.
3527 */
3528 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003529 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003530 } else {
3531 /*
3532 * Ok we're over committed, set num_bytes to the overcommitted
3533 * amount plus the amount of bytes that we need for this
3534 * reservation.
3535 */
3536 num_bytes = unused - space_info->total_bytes +
3537 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003538 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003539
3540 /*
3541 * Couldn't make our reservation, save our place so while we're trying
3542 * to reclaim space we can actually use it instead of somebody else
3543 * stealing it from us.
3544 */
3545 if (ret && !reserved) {
3546 space_info->bytes_reserved += orig_bytes;
3547 reserved = true;
3548 }
3549
Yan, Zhengf0486c62010-05-16 10:46:25 -04003550 spin_unlock(&space_info->lock);
3551
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003552 if (!ret)
3553 return 0;
3554
3555 if (!flush)
3556 goto out;
3557
3558 /*
3559 * We do synchronous shrinking since we don't actually unreserve
3560 * metadata until after the IO is completed.
3561 */
3562 ret = shrink_delalloc(trans, root, num_bytes, 1);
3563 if (ret > 0)
3564 return 0;
3565 else if (ret < 0)
3566 goto out;
3567
3568 /*
3569 * So if we were overcommitted it's possible that somebody else flushed
3570 * out enough space and we simply didn't have enough space to reclaim,
3571 * so go back around and try again.
3572 */
3573 if (retries < 2) {
3574 retries++;
3575 goto again;
3576 }
3577
3578 spin_lock(&space_info->lock);
3579 /*
3580 * Not enough space to be reclaimed, don't bother committing the
3581 * transaction.
3582 */
3583 if (space_info->bytes_pinned < orig_bytes)
3584 ret = -ENOSPC;
3585 spin_unlock(&space_info->lock);
3586 if (ret)
3587 goto out;
3588
3589 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003590 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003591 goto out;
3592
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003593 ret = -ENOSPC;
3594 trans = btrfs_join_transaction(root, 1);
3595 if (IS_ERR(trans))
3596 goto out;
3597 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003598 if (!ret) {
3599 trans = NULL;
3600 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003601 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003602 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003603
3604out:
3605 if (reserved) {
3606 spin_lock(&space_info->lock);
3607 space_info->bytes_reserved -= orig_bytes;
3608 spin_unlock(&space_info->lock);
3609 }
3610
Yan, Zhengf0486c62010-05-16 10:46:25 -04003611 return ret;
3612}
3613
3614static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3615 struct btrfs_root *root)
3616{
3617 struct btrfs_block_rsv *block_rsv;
3618 if (root->ref_cows)
3619 block_rsv = trans->block_rsv;
3620 else
3621 block_rsv = root->block_rsv;
3622
3623 if (!block_rsv)
3624 block_rsv = &root->fs_info->empty_block_rsv;
3625
3626 return block_rsv;
3627}
3628
3629static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3630 u64 num_bytes)
3631{
3632 int ret = -ENOSPC;
3633 spin_lock(&block_rsv->lock);
3634 if (block_rsv->reserved >= num_bytes) {
3635 block_rsv->reserved -= num_bytes;
3636 if (block_rsv->reserved < block_rsv->size)
3637 block_rsv->full = 0;
3638 ret = 0;
3639 }
3640 spin_unlock(&block_rsv->lock);
3641 return ret;
3642}
3643
3644static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3645 u64 num_bytes, int update_size)
3646{
3647 spin_lock(&block_rsv->lock);
3648 block_rsv->reserved += num_bytes;
3649 if (update_size)
3650 block_rsv->size += num_bytes;
3651 else if (block_rsv->reserved >= block_rsv->size)
3652 block_rsv->full = 1;
3653 spin_unlock(&block_rsv->lock);
3654}
3655
3656void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3657 struct btrfs_block_rsv *dest, u64 num_bytes)
3658{
3659 struct btrfs_space_info *space_info = block_rsv->space_info;
3660
3661 spin_lock(&block_rsv->lock);
3662 if (num_bytes == (u64)-1)
3663 num_bytes = block_rsv->size;
3664 block_rsv->size -= num_bytes;
3665 if (block_rsv->reserved >= block_rsv->size) {
3666 num_bytes = block_rsv->reserved - block_rsv->size;
3667 block_rsv->reserved = block_rsv->size;
3668 block_rsv->full = 1;
3669 } else {
3670 num_bytes = 0;
3671 }
3672 spin_unlock(&block_rsv->lock);
3673
3674 if (num_bytes > 0) {
3675 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003676 spin_lock(&dest->lock);
3677 if (!dest->full) {
3678 u64 bytes_to_add;
3679
3680 bytes_to_add = dest->size - dest->reserved;
3681 bytes_to_add = min(num_bytes, bytes_to_add);
3682 dest->reserved += bytes_to_add;
3683 if (dest->reserved >= dest->size)
3684 dest->full = 1;
3685 num_bytes -= bytes_to_add;
3686 }
3687 spin_unlock(&dest->lock);
3688 }
3689 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003690 spin_lock(&space_info->lock);
3691 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003692 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003693 spin_unlock(&space_info->lock);
3694 }
3695 }
3696}
3697
3698static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3699 struct btrfs_block_rsv *dst, u64 num_bytes)
3700{
3701 int ret;
3702
3703 ret = block_rsv_use_bytes(src, num_bytes);
3704 if (ret)
3705 return ret;
3706
3707 block_rsv_add_bytes(dst, num_bytes, 1);
3708 return 0;
3709}
3710
3711void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3712{
3713 memset(rsv, 0, sizeof(*rsv));
3714 spin_lock_init(&rsv->lock);
3715 atomic_set(&rsv->usage, 1);
3716 rsv->priority = 6;
3717 INIT_LIST_HEAD(&rsv->list);
3718}
3719
3720struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3721{
3722 struct btrfs_block_rsv *block_rsv;
3723 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003724
3725 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3726 if (!block_rsv)
3727 return NULL;
3728
3729 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003730 block_rsv->space_info = __find_space_info(fs_info,
3731 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003732 return block_rsv;
3733}
3734
3735void btrfs_free_block_rsv(struct btrfs_root *root,
3736 struct btrfs_block_rsv *rsv)
3737{
3738 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3739 btrfs_block_rsv_release(root, rsv, (u64)-1);
3740 if (!rsv->durable)
3741 kfree(rsv);
3742 }
3743}
3744
3745/*
3746 * make the block_rsv struct be able to capture freed space.
3747 * the captured space will re-add to the the block_rsv struct
3748 * after transaction commit
3749 */
3750void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3751 struct btrfs_block_rsv *block_rsv)
3752{
3753 block_rsv->durable = 1;
3754 mutex_lock(&fs_info->durable_block_rsv_mutex);
3755 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3756 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3757}
3758
3759int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3760 struct btrfs_root *root,
3761 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003762 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003763{
3764 int ret;
3765
3766 if (num_bytes == 0)
3767 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003768
3769 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003770 if (!ret) {
3771 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3772 return 0;
3773 }
3774
Yan, Zhengf0486c62010-05-16 10:46:25 -04003775 return ret;
3776}
3777
3778int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3779 struct btrfs_root *root,
3780 struct btrfs_block_rsv *block_rsv,
3781 u64 min_reserved, int min_factor)
3782{
3783 u64 num_bytes = 0;
3784 int commit_trans = 0;
3785 int ret = -ENOSPC;
3786
3787 if (!block_rsv)
3788 return 0;
3789
3790 spin_lock(&block_rsv->lock);
3791 if (min_factor > 0)
3792 num_bytes = div_factor(block_rsv->size, min_factor);
3793 if (min_reserved > num_bytes)
3794 num_bytes = min_reserved;
3795
3796 if (block_rsv->reserved >= num_bytes) {
3797 ret = 0;
3798 } else {
3799 num_bytes -= block_rsv->reserved;
3800 if (block_rsv->durable &&
3801 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3802 commit_trans = 1;
3803 }
3804 spin_unlock(&block_rsv->lock);
3805 if (!ret)
3806 return 0;
3807
3808 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003809 ret = reserve_metadata_bytes(trans, root, block_rsv,
3810 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003811 if (!ret) {
3812 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3813 return 0;
3814 }
3815 }
3816
3817 if (commit_trans) {
3818 if (trans)
3819 return -EAGAIN;
3820
3821 trans = btrfs_join_transaction(root, 1);
3822 BUG_ON(IS_ERR(trans));
3823 ret = btrfs_commit_transaction(trans, root);
3824 return 0;
3825 }
3826
Yan, Zhengf0486c62010-05-16 10:46:25 -04003827 return -ENOSPC;
3828}
3829
3830int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3831 struct btrfs_block_rsv *dst_rsv,
3832 u64 num_bytes)
3833{
3834 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3835}
3836
3837void btrfs_block_rsv_release(struct btrfs_root *root,
3838 struct btrfs_block_rsv *block_rsv,
3839 u64 num_bytes)
3840{
3841 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3842 if (global_rsv->full || global_rsv == block_rsv ||
3843 block_rsv->space_info != global_rsv->space_info)
3844 global_rsv = NULL;
3845 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3846}
3847
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003848/*
3849 * helper to calculate size of global block reservation.
3850 * the desired value is sum of space used by extent tree,
3851 * checksum tree and root tree
3852 */
3853static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3854{
3855 struct btrfs_space_info *sinfo;
3856 u64 num_bytes;
3857 u64 meta_used;
3858 u64 data_used;
3859 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3860#if 0
3861 /*
3862 * per tree used space accounting can be inaccuracy, so we
3863 * can't rely on it.
3864 */
3865 spin_lock(&fs_info->extent_root->accounting_lock);
3866 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3867 spin_unlock(&fs_info->extent_root->accounting_lock);
3868
3869 spin_lock(&fs_info->csum_root->accounting_lock);
3870 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3871 spin_unlock(&fs_info->csum_root->accounting_lock);
3872
3873 spin_lock(&fs_info->tree_root->accounting_lock);
3874 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3875 spin_unlock(&fs_info->tree_root->accounting_lock);
3876#endif
3877 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3878 spin_lock(&sinfo->lock);
3879 data_used = sinfo->bytes_used;
3880 spin_unlock(&sinfo->lock);
3881
3882 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3883 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003884 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3885 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003886 meta_used = sinfo->bytes_used;
3887 spin_unlock(&sinfo->lock);
3888
3889 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3890 csum_size * 2;
3891 num_bytes += div64_u64(data_used + meta_used, 50);
3892
3893 if (num_bytes * 3 > meta_used)
3894 num_bytes = div64_u64(meta_used, 3);
3895
3896 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3897}
3898
3899static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3900{
3901 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3902 struct btrfs_space_info *sinfo = block_rsv->space_info;
3903 u64 num_bytes;
3904
3905 num_bytes = calc_global_metadata_size(fs_info);
3906
3907 spin_lock(&block_rsv->lock);
3908 spin_lock(&sinfo->lock);
3909
3910 block_rsv->size = num_bytes;
3911
3912 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003913 sinfo->bytes_reserved + sinfo->bytes_readonly +
3914 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003915
3916 if (sinfo->total_bytes > num_bytes) {
3917 num_bytes = sinfo->total_bytes - num_bytes;
3918 block_rsv->reserved += num_bytes;
3919 sinfo->bytes_reserved += num_bytes;
3920 }
3921
3922 if (block_rsv->reserved >= block_rsv->size) {
3923 num_bytes = block_rsv->reserved - block_rsv->size;
3924 sinfo->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003925 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003926 block_rsv->reserved = block_rsv->size;
3927 block_rsv->full = 1;
3928 }
3929#if 0
3930 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3931 block_rsv->size, block_rsv->reserved);
3932#endif
3933 spin_unlock(&sinfo->lock);
3934 spin_unlock(&block_rsv->lock);
3935}
3936
Yan, Zhengf0486c62010-05-16 10:46:25 -04003937static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3938{
3939 struct btrfs_space_info *space_info;
3940
3941 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3942 fs_info->chunk_block_rsv.space_info = space_info;
3943 fs_info->chunk_block_rsv.priority = 10;
3944
3945 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003946 fs_info->global_block_rsv.space_info = space_info;
3947 fs_info->global_block_rsv.priority = 10;
3948 fs_info->global_block_rsv.refill_used = 1;
3949 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003950 fs_info->trans_block_rsv.space_info = space_info;
3951 fs_info->empty_block_rsv.space_info = space_info;
3952 fs_info->empty_block_rsv.priority = 10;
3953
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003954 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3955 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3956 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3957 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003958 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003959
3960 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3961
3962 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3963
3964 update_global_block_rsv(fs_info);
3965}
3966
3967static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3968{
3969 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3970 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3971 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3972 WARN_ON(fs_info->trans_block_rsv.size > 0);
3973 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3974 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3975 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003976}
3977
Yan, Zhenga22285a2010-05-16 10:48:46 -04003978static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3979{
3980 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3981 3 * num_items;
3982}
3983
3984int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3985 struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003986 int num_items)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003987{
3988 u64 num_bytes;
3989 int ret;
3990
3991 if (num_items == 0 || root->fs_info->chunk_root == root)
3992 return 0;
3993
3994 num_bytes = calc_trans_metadata_size(root, num_items);
3995 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003996 num_bytes);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003997 if (!ret) {
3998 trans->bytes_reserved += num_bytes;
3999 trans->block_rsv = &root->fs_info->trans_block_rsv;
4000 }
4001 return ret;
4002}
4003
4004void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4005 struct btrfs_root *root)
4006{
4007 if (!trans->bytes_reserved)
4008 return;
4009
4010 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
4011 btrfs_block_rsv_release(root, trans->block_rsv,
4012 trans->bytes_reserved);
4013 trans->bytes_reserved = 0;
4014}
4015
Yan, Zhengd68fc572010-05-16 10:49:58 -04004016int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4017 struct inode *inode)
4018{
4019 struct btrfs_root *root = BTRFS_I(inode)->root;
4020 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4021 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4022
4023 /*
4024 * one for deleting orphan item, one for updating inode and
4025 * two for calling btrfs_truncate_inode_items.
4026 *
4027 * btrfs_truncate_inode_items is a delete operation, it frees
4028 * more space than it uses in most cases. So two units of
4029 * metadata space should be enough for calling it many times.
4030 * If all of the metadata space is used, we can commit
4031 * transaction and use space it freed.
4032 */
4033 u64 num_bytes = calc_trans_metadata_size(root, 4);
4034 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4035}
4036
4037void btrfs_orphan_release_metadata(struct inode *inode)
4038{
4039 struct btrfs_root *root = BTRFS_I(inode)->root;
4040 u64 num_bytes = calc_trans_metadata_size(root, 4);
4041 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4042}
4043
Yan, Zhenga22285a2010-05-16 10:48:46 -04004044int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4045 struct btrfs_pending_snapshot *pending)
4046{
4047 struct btrfs_root *root = pending->root;
4048 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4049 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4050 /*
4051 * two for root back/forward refs, two for directory entries
4052 * and one for root of the snapshot.
4053 */
4054 u64 num_bytes = calc_trans_metadata_size(root, 5);
4055 dst_rsv->space_info = src_rsv->space_info;
4056 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4057}
4058
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004059static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
4060{
4061 return num_bytes >>= 3;
4062}
4063
4064int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4065{
4066 struct btrfs_root *root = BTRFS_I(inode)->root;
4067 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
4068 u64 to_reserve;
4069 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004070 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004071 int ret;
4072
4073 if (btrfs_transaction_in_commit(root->fs_info))
4074 schedule_timeout(1);
4075
4076 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004077
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004078 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004079 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4080
4081 if (nr_extents > reserved_extents) {
4082 nr_extents -= reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004083 to_reserve = calc_trans_metadata_size(root, nr_extents);
4084 } else {
4085 nr_extents = 0;
4086 to_reserve = 0;
4087 }
Josef Bacik57a45ced2011-01-25 16:30:38 -05004088
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004089 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004090 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
4091 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004092 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004093
Josef Bacik57a45ced2011-01-25 16:30:38 -05004094 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004095 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004096
4097 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4098
4099 if (block_rsv->size > 512 * 1024 * 1024)
Josef Bacik0019f102010-10-15 15:18:40 -04004100 shrink_delalloc(NULL, root, to_reserve, 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004101
4102 return 0;
4103}
4104
4105void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4106{
4107 struct btrfs_root *root = BTRFS_I(inode)->root;
4108 u64 to_free;
4109 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004110 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004111
4112 num_bytes = ALIGN(num_bytes, root->sectorsize);
4113 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik3c148742011-02-02 15:53:47 +00004114 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004115
Josef Bacik57a45ced2011-01-25 16:30:38 -05004116 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4117 do {
4118 int old, new;
4119
4120 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4121 if (nr_extents >= reserved_extents) {
4122 nr_extents = 0;
4123 break;
4124 }
4125 old = reserved_extents;
4126 nr_extents = reserved_extents - nr_extents;
4127 new = reserved_extents - nr_extents;
4128 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
4129 reserved_extents, new);
4130 if (likely(old == reserved_extents))
4131 break;
4132 reserved_extents = old;
4133 } while (1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004134
4135 to_free = calc_csum_metadata_size(inode, num_bytes);
4136 if (nr_extents > 0)
4137 to_free += calc_trans_metadata_size(root, nr_extents);
4138
4139 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4140 to_free);
4141}
4142
4143int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4144{
4145 int ret;
4146
4147 ret = btrfs_check_data_free_space(inode, num_bytes);
4148 if (ret)
4149 return ret;
4150
4151 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4152 if (ret) {
4153 btrfs_free_reserved_data_space(inode, num_bytes);
4154 return ret;
4155 }
4156
4157 return 0;
4158}
4159
4160void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4161{
4162 btrfs_delalloc_release_metadata(inode, num_bytes);
4163 btrfs_free_reserved_data_space(inode, num_bytes);
4164}
4165
Chris Mason9078a3e2007-04-26 16:46:15 -04004166static int update_block_group(struct btrfs_trans_handle *trans,
4167 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004168 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004169{
Josef Bacik0af3d002010-06-21 14:48:16 -04004170 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004171 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004172 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004173 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004174 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004175 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004176
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004177 /* block accounting for super block */
4178 spin_lock(&info->delalloc_lock);
4179 old_val = btrfs_super_bytes_used(&info->super_copy);
4180 if (alloc)
4181 old_val += num_bytes;
4182 else
4183 old_val -= num_bytes;
4184 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004185 spin_unlock(&info->delalloc_lock);
4186
Chris Masond3977122009-01-05 21:25:51 -05004187 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004188 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004189 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004190 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004191 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4192 BTRFS_BLOCK_GROUP_RAID1 |
4193 BTRFS_BLOCK_GROUP_RAID10))
4194 factor = 2;
4195 else
4196 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004197 /*
4198 * If this block group has free space cache written out, we
4199 * need to make sure to load it if we are removing space. This
4200 * is because we need the unpinning stage to actually add the
4201 * space back to the block group, otherwise we will leak space.
4202 */
4203 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004204 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004205
Chris Masondb945352007-10-15 16:15:53 -04004206 byte_in_group = bytenr - cache->key.objectid;
4207 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004208
Josef Bacik25179202008-10-29 14:49:05 -04004209 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004210 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004211
4212 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4213 cache->disk_cache_state < BTRFS_DC_CLEAR)
4214 cache->disk_cache_state = BTRFS_DC_CLEAR;
4215
Josef Bacik0f9dd462008-09-23 13:14:11 -04004216 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004217 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004218 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004219 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004220 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004221 btrfs_set_block_group_used(&cache->item, old_val);
4222 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004223 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004224 cache->space_info->reservation_progress++;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004225 cache->space_info->bytes_used += num_bytes;
4226 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004227 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004228 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004229 } else {
Chris Masondb945352007-10-15 16:15:53 -04004230 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004231 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004232 cache->pinned += num_bytes;
4233 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004234 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004235 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004236 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004237 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004238
Yan, Zhengf0486c62010-05-16 10:46:25 -04004239 set_extent_dirty(info->pinned_extents,
4240 bytenr, bytenr + num_bytes - 1,
4241 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004242 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004243 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004244 total -= num_bytes;
4245 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004246 }
4247 return 0;
4248}
Chris Mason6324fbf2008-03-24 15:01:59 -04004249
Chris Masona061fc82008-05-07 11:43:44 -04004250static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4251{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004252 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004253 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004254
4255 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4256 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004257 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004258
Yan Zhengd2fb3432008-12-11 16:30:39 -05004259 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004260 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004261
4262 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004263}
4264
Yan, Zhengf0486c62010-05-16 10:46:25 -04004265static int pin_down_extent(struct btrfs_root *root,
4266 struct btrfs_block_group_cache *cache,
4267 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004268{
Yan Zheng11833d62009-09-11 16:11:19 -04004269 spin_lock(&cache->space_info->lock);
4270 spin_lock(&cache->lock);
4271 cache->pinned += num_bytes;
4272 cache->space_info->bytes_pinned += num_bytes;
4273 if (reserved) {
4274 cache->reserved -= num_bytes;
4275 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004276 cache->space_info->reservation_progress++;
Yan324ae4d2007-11-16 14:57:08 -05004277 }
Yan Zheng11833d62009-09-11 16:11:19 -04004278 spin_unlock(&cache->lock);
4279 spin_unlock(&cache->space_info->lock);
4280
Yan, Zhengf0486c62010-05-16 10:46:25 -04004281 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4282 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004283 return 0;
4284}
Chris Mason9078a3e2007-04-26 16:46:15 -04004285
Yan, Zhengf0486c62010-05-16 10:46:25 -04004286/*
4287 * this function must be called within transaction
4288 */
4289int btrfs_pin_extent(struct btrfs_root *root,
4290 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004291{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004292 struct btrfs_block_group_cache *cache;
4293
4294 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4295 BUG_ON(!cache);
4296
4297 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4298
4299 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004300 return 0;
4301}
Zheng Yane8569812008-09-26 10:05:48 -04004302
Yan, Zhengf0486c62010-05-16 10:46:25 -04004303/*
4304 * update size of reserved extents. this function may return -EAGAIN
4305 * if 'reserve' is true or 'sinfo' is false.
4306 */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004307int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4308 u64 num_bytes, int reserve, int sinfo)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004309{
4310 int ret = 0;
4311 if (sinfo) {
4312 struct btrfs_space_info *space_info = cache->space_info;
4313 spin_lock(&space_info->lock);
4314 spin_lock(&cache->lock);
4315 if (reserve) {
4316 if (cache->ro) {
4317 ret = -EAGAIN;
4318 } else {
4319 cache->reserved += num_bytes;
4320 space_info->bytes_reserved += num_bytes;
4321 }
4322 } else {
4323 if (cache->ro)
4324 space_info->bytes_readonly += num_bytes;
4325 cache->reserved -= num_bytes;
4326 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004327 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004328 }
4329 spin_unlock(&cache->lock);
4330 spin_unlock(&space_info->lock);
4331 } else {
4332 spin_lock(&cache->lock);
4333 if (cache->ro) {
4334 ret = -EAGAIN;
4335 } else {
4336 if (reserve)
4337 cache->reserved += num_bytes;
4338 else
4339 cache->reserved -= num_bytes;
4340 }
4341 spin_unlock(&cache->lock);
4342 }
4343 return ret;
4344}
4345
Yan Zheng11833d62009-09-11 16:11:19 -04004346int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4347 struct btrfs_root *root)
4348{
4349 struct btrfs_fs_info *fs_info = root->fs_info;
4350 struct btrfs_caching_control *next;
4351 struct btrfs_caching_control *caching_ctl;
4352 struct btrfs_block_group_cache *cache;
4353
4354 down_write(&fs_info->extent_commit_sem);
4355
4356 list_for_each_entry_safe(caching_ctl, next,
4357 &fs_info->caching_block_groups, list) {
4358 cache = caching_ctl->block_group;
4359 if (block_group_cache_done(cache)) {
4360 cache->last_byte_to_unpin = (u64)-1;
4361 list_del_init(&caching_ctl->list);
4362 put_caching_control(caching_ctl);
4363 } else {
4364 cache->last_byte_to_unpin = caching_ctl->progress;
4365 }
4366 }
4367
4368 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4369 fs_info->pinned_extents = &fs_info->freed_extents[1];
4370 else
4371 fs_info->pinned_extents = &fs_info->freed_extents[0];
4372
4373 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004374
4375 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004376 return 0;
4377}
4378
4379static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4380{
4381 struct btrfs_fs_info *fs_info = root->fs_info;
4382 struct btrfs_block_group_cache *cache = NULL;
4383 u64 len;
4384
4385 while (start <= end) {
4386 if (!cache ||
4387 start >= cache->key.objectid + cache->key.offset) {
4388 if (cache)
4389 btrfs_put_block_group(cache);
4390 cache = btrfs_lookup_block_group(fs_info, start);
4391 BUG_ON(!cache);
4392 }
4393
4394 len = cache->key.objectid + cache->key.offset - start;
4395 len = min(len, end + 1 - start);
4396
4397 if (start < cache->last_byte_to_unpin) {
4398 len = min(len, cache->last_byte_to_unpin - start);
4399 btrfs_add_free_space(cache, start, len);
4400 }
Josef Bacik25179202008-10-29 14:49:05 -04004401
Yan, Zhengf0486c62010-05-16 10:46:25 -04004402 start += len;
4403
Josef Bacik25179202008-10-29 14:49:05 -04004404 spin_lock(&cache->space_info->lock);
4405 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004406 cache->pinned -= len;
4407 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004408 if (cache->ro) {
4409 cache->space_info->bytes_readonly += len;
4410 } else if (cache->reserved_pinned > 0) {
4411 len = min(len, cache->reserved_pinned);
4412 cache->reserved_pinned -= len;
4413 cache->space_info->bytes_reserved += len;
4414 }
Josef Bacik25179202008-10-29 14:49:05 -04004415 spin_unlock(&cache->lock);
4416 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004417 }
4418
4419 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004420 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004421 return 0;
4422}
4423
4424int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004425 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004426{
Yan Zheng11833d62009-09-11 16:11:19 -04004427 struct btrfs_fs_info *fs_info = root->fs_info;
4428 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004429 struct btrfs_block_rsv *block_rsv;
4430 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004431 u64 start;
4432 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004433 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004434 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004435
Yan Zheng11833d62009-09-11 16:11:19 -04004436 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4437 unpin = &fs_info->freed_extents[1];
4438 else
4439 unpin = &fs_info->freed_extents[0];
4440
Chris Masond3977122009-01-05 21:25:51 -05004441 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004442 ret = find_first_extent_bit(unpin, 0, &start, &end,
4443 EXTENT_DIRTY);
4444 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004445 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004446
Li Dongyang5378e602011-03-24 10:24:27 +00004447 if (btrfs_test_opt(root, DISCARD))
4448 ret = btrfs_discard_extent(root, start,
4449 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004450
Chris Mason1a5bc162007-10-15 16:15:26 -04004451 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004452 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004453 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004454 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004455
Yan, Zhengf0486c62010-05-16 10:46:25 -04004456 mutex_lock(&fs_info->durable_block_rsv_mutex);
4457 list_for_each_entry_safe(block_rsv, next_rsv,
4458 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004459
Yan, Zhengf0486c62010-05-16 10:46:25 -04004460 idx = trans->transid & 0x1;
4461 if (block_rsv->freed[idx] > 0) {
4462 block_rsv_add_bytes(block_rsv,
4463 block_rsv->freed[idx], 0);
4464 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004465 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004466 if (atomic_read(&block_rsv->usage) == 0) {
4467 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004468
Yan, Zhengf0486c62010-05-16 10:46:25 -04004469 if (block_rsv->freed[0] == 0 &&
4470 block_rsv->freed[1] == 0) {
4471 list_del_init(&block_rsv->list);
4472 kfree(block_rsv);
4473 }
4474 } else {
4475 btrfs_block_rsv_release(root, block_rsv, 0);
4476 }
4477 }
4478 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4479
Chris Masone20d96d2007-03-22 12:13:20 -04004480 return 0;
4481}
4482
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004483static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4484 struct btrfs_root *root,
4485 u64 bytenr, u64 num_bytes, u64 parent,
4486 u64 root_objectid, u64 owner_objectid,
4487 u64 owner_offset, int refs_to_drop,
4488 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004489{
Chris Masone2fa7222007-03-12 16:22:34 -04004490 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004491 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004492 struct btrfs_fs_info *info = root->fs_info;
4493 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004494 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004495 struct btrfs_extent_item *ei;
4496 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004497 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004498 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004499 int extent_slot = 0;
4500 int found_extent = 0;
4501 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004502 u32 item_size;
4503 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004504
Chris Mason5caf2a02007-04-02 11:20:42 -04004505 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004506 if (!path)
4507 return -ENOMEM;
4508
Chris Mason3c12ac72008-04-21 12:01:38 -04004509 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004510 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004511
4512 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4513 BUG_ON(!is_data && refs_to_drop != 1);
4514
4515 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4516 bytenr, num_bytes, parent,
4517 root_objectid, owner_objectid,
4518 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004519 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004520 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004521 while (extent_slot >= 0) {
4522 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004523 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004524 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004525 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004526 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4527 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004528 found_extent = 1;
4529 break;
4530 }
4531 if (path->slots[0] - extent_slot > 5)
4532 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004533 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004534 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004535#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4536 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4537 if (found_extent && item_size < sizeof(*ei))
4538 found_extent = 0;
4539#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004540 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004541 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004542 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004543 NULL, refs_to_drop,
4544 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004545 BUG_ON(ret);
4546 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04004547 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004548
4549 key.objectid = bytenr;
4550 key.type = BTRFS_EXTENT_ITEM_KEY;
4551 key.offset = num_bytes;
4552
Zheng Yan31840ae2008-09-23 13:14:14 -04004553 ret = btrfs_search_slot(trans, extent_root,
4554 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004555 if (ret) {
4556 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004557 ", was looking for %llu\n", ret,
4558 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004559 btrfs_print_leaf(extent_root, path->nodes[0]);
4560 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004561 BUG_ON(ret);
4562 extent_slot = path->slots[0];
4563 }
Chris Mason7bb86312007-12-11 09:25:06 -05004564 } else {
4565 btrfs_print_leaf(extent_root, path->nodes[0]);
4566 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004567 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004568 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004569 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004570 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004571 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004572 (unsigned long long)owner_objectid,
4573 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004574 }
Chris Mason5f39d392007-10-15 16:14:19 -04004575
4576 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004577 item_size = btrfs_item_size_nr(leaf, extent_slot);
4578#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4579 if (item_size < sizeof(*ei)) {
4580 BUG_ON(found_extent || extent_slot != path->slots[0]);
4581 ret = convert_extent_item_v0(trans, extent_root, path,
4582 owner_objectid, 0);
4583 BUG_ON(ret < 0);
4584
4585 btrfs_release_path(extent_root, path);
4586 path->leave_spinning = 1;
4587
4588 key.objectid = bytenr;
4589 key.type = BTRFS_EXTENT_ITEM_KEY;
4590 key.offset = num_bytes;
4591
4592 ret = btrfs_search_slot(trans, extent_root, &key, path,
4593 -1, 1);
4594 if (ret) {
4595 printk(KERN_ERR "umm, got %d back from search"
4596 ", was looking for %llu\n", ret,
4597 (unsigned long long)bytenr);
4598 btrfs_print_leaf(extent_root, path->nodes[0]);
4599 }
4600 BUG_ON(ret);
4601 extent_slot = path->slots[0];
4602 leaf = path->nodes[0];
4603 item_size = btrfs_item_size_nr(leaf, extent_slot);
4604 }
4605#endif
4606 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004607 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004608 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004609 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4610 struct btrfs_tree_block_info *bi;
4611 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4612 bi = (struct btrfs_tree_block_info *)(ei + 1);
4613 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004614 }
4615
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004616 refs = btrfs_extent_refs(leaf, ei);
4617 BUG_ON(refs < refs_to_drop);
4618 refs -= refs_to_drop;
4619
4620 if (refs > 0) {
4621 if (extent_op)
4622 __run_delayed_extent_op(extent_op, leaf, ei);
4623 /*
4624 * In the case of inline back ref, reference count will
4625 * be updated by remove_extent_backref
4626 */
4627 if (iref) {
4628 BUG_ON(!found_extent);
4629 } else {
4630 btrfs_set_extent_refs(leaf, ei, refs);
4631 btrfs_mark_buffer_dirty(leaf);
4632 }
4633 if (found_extent) {
4634 ret = remove_extent_backref(trans, extent_root, path,
4635 iref, refs_to_drop,
4636 is_data);
4637 BUG_ON(ret);
4638 }
4639 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004640 if (found_extent) {
4641 BUG_ON(is_data && refs_to_drop !=
4642 extent_data_ref_count(root, path, iref));
4643 if (iref) {
4644 BUG_ON(path->slots[0] != extent_slot);
4645 } else {
4646 BUG_ON(path->slots[0] != extent_slot + 1);
4647 path->slots[0] = extent_slot;
4648 num_to_del = 2;
4649 }
Chris Mason78fae272007-03-25 11:35:08 -04004650 }
Chris Masonb9473432009-03-13 11:00:37 -04004651
Chris Mason952fcca2008-02-18 16:33:44 -05004652 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4653 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004654 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04004655 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01004656
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004657 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004658 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4659 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004660 } else {
4661 invalidate_mapping_pages(info->btree_inode->i_mapping,
4662 bytenr >> PAGE_CACHE_SHIFT,
4663 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004664 }
4665
Yan, Zhengf0486c62010-05-16 10:46:25 -04004666 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004667 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004668 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004669 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004670 return ret;
4671}
4672
4673/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004674 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004675 * delayed ref for that extent as well. This searches the delayed ref tree for
4676 * a given extent, and if there are no other delayed refs to be processed, it
4677 * removes it from the tree.
4678 */
4679static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4680 struct btrfs_root *root, u64 bytenr)
4681{
4682 struct btrfs_delayed_ref_head *head;
4683 struct btrfs_delayed_ref_root *delayed_refs;
4684 struct btrfs_delayed_ref_node *ref;
4685 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004686 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004687
4688 delayed_refs = &trans->transaction->delayed_refs;
4689 spin_lock(&delayed_refs->lock);
4690 head = btrfs_find_delayed_ref_head(trans, bytenr);
4691 if (!head)
4692 goto out;
4693
4694 node = rb_prev(&head->node.rb_node);
4695 if (!node)
4696 goto out;
4697
4698 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4699
4700 /* there are still entries for this ref, we can't drop it */
4701 if (ref->bytenr == bytenr)
4702 goto out;
4703
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004704 if (head->extent_op) {
4705 if (!head->must_insert_reserved)
4706 goto out;
4707 kfree(head->extent_op);
4708 head->extent_op = NULL;
4709 }
4710
Chris Mason1887be62009-03-13 10:11:24 -04004711 /*
4712 * waiting for the lock here would deadlock. If someone else has it
4713 * locked they are already in the process of dropping it anyway
4714 */
4715 if (!mutex_trylock(&head->mutex))
4716 goto out;
4717
4718 /*
4719 * at this point we have a head with no other entries. Go
4720 * ahead and process it.
4721 */
4722 head->node.in_tree = 0;
4723 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004724
Chris Mason1887be62009-03-13 10:11:24 -04004725 delayed_refs->num_entries--;
4726
4727 /*
4728 * we don't take a ref on the node because we're removing it from the
4729 * tree, so we just steal the ref the tree was holding.
4730 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004731 delayed_refs->num_heads--;
4732 if (list_empty(&head->cluster))
4733 delayed_refs->num_heads_ready--;
4734
4735 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004736 spin_unlock(&delayed_refs->lock);
4737
Yan, Zhengf0486c62010-05-16 10:46:25 -04004738 BUG_ON(head->extent_op);
4739 if (head->must_insert_reserved)
4740 ret = 1;
4741
4742 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004743 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004744 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004745out:
4746 spin_unlock(&delayed_refs->lock);
4747 return 0;
4748}
4749
Yan, Zhengf0486c62010-05-16 10:46:25 -04004750void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4751 struct btrfs_root *root,
4752 struct extent_buffer *buf,
4753 u64 parent, int last_ref)
4754{
4755 struct btrfs_block_rsv *block_rsv;
4756 struct btrfs_block_group_cache *cache = NULL;
4757 int ret;
4758
4759 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4760 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4761 parent, root->root_key.objectid,
4762 btrfs_header_level(buf),
4763 BTRFS_DROP_DELAYED_REF, NULL);
4764 BUG_ON(ret);
4765 }
4766
4767 if (!last_ref)
4768 return;
4769
4770 block_rsv = get_block_rsv(trans, root);
4771 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004772 if (block_rsv->space_info != cache->space_info)
4773 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004774
4775 if (btrfs_header_generation(buf) == trans->transid) {
4776 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4777 ret = check_ref_cleanup(trans, root, buf->start);
4778 if (!ret)
4779 goto pin;
4780 }
4781
4782 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4783 pin_down_extent(root, cache, buf->start, buf->len, 1);
4784 goto pin;
4785 }
4786
4787 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4788
4789 btrfs_add_free_space(cache, buf->start, buf->len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00004790 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004791 if (ret == -EAGAIN) {
4792 /* block group became read-only */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004793 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004794 goto out;
4795 }
4796
4797 ret = 1;
4798 spin_lock(&block_rsv->lock);
4799 if (block_rsv->reserved < block_rsv->size) {
4800 block_rsv->reserved += buf->len;
4801 ret = 0;
4802 }
4803 spin_unlock(&block_rsv->lock);
4804
4805 if (ret) {
4806 spin_lock(&cache->space_info->lock);
4807 cache->space_info->bytes_reserved -= buf->len;
Chris Mason36e39c42011-03-12 07:08:42 -05004808 cache->space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004809 spin_unlock(&cache->space_info->lock);
4810 }
4811 goto out;
4812 }
4813pin:
4814 if (block_rsv->durable && !cache->ro) {
4815 ret = 0;
4816 spin_lock(&cache->lock);
4817 if (!cache->ro) {
4818 cache->reserved_pinned += buf->len;
4819 ret = 1;
4820 }
4821 spin_unlock(&cache->lock);
4822
4823 if (ret) {
4824 spin_lock(&block_rsv->lock);
4825 block_rsv->freed[trans->transid & 0x1] += buf->len;
4826 spin_unlock(&block_rsv->lock);
4827 }
4828 }
4829out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004830 /*
4831 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4832 * anymore.
4833 */
4834 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004835 btrfs_put_block_group(cache);
4836}
4837
Chris Mason925baed2008-06-25 16:01:30 -04004838int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004839 struct btrfs_root *root,
4840 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004841 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004842{
4843 int ret;
4844
Chris Mason56bec292009-03-13 10:10:06 -04004845 /*
4846 * tree log blocks never actually go into the extent allocation
4847 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004848 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004849 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4850 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004851 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004852 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004853 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004854 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4855 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4856 parent, root_objectid, (int)owner,
4857 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004858 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004859 } else {
4860 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4861 parent, root_objectid, owner,
4862 offset, BTRFS_DROP_DELAYED_REF, NULL);
4863 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004864 }
Chris Mason925baed2008-06-25 16:01:30 -04004865 return ret;
4866}
4867
Chris Mason87ee04e2007-11-30 11:30:34 -05004868static u64 stripe_align(struct btrfs_root *root, u64 val)
4869{
4870 u64 mask = ((u64)root->stripesize - 1);
4871 u64 ret = (val + mask) & ~mask;
4872 return ret;
4873}
4874
Chris Masonfec577f2007-02-26 10:40:21 -05004875/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004876 * when we wait for progress in the block group caching, its because
4877 * our allocation attempt failed at least once. So, we must sleep
4878 * and let some progress happen before we try again.
4879 *
4880 * This function will sleep at least once waiting for new free space to
4881 * show up, and then it will check the block group free space numbers
4882 * for our min num_bytes. Another option is to have it go ahead
4883 * and look in the rbtree for a free extent of a given size, but this
4884 * is a good start.
4885 */
4886static noinline int
4887wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4888 u64 num_bytes)
4889{
Yan Zheng11833d62009-09-11 16:11:19 -04004890 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004891 DEFINE_WAIT(wait);
4892
Yan Zheng11833d62009-09-11 16:11:19 -04004893 caching_ctl = get_caching_control(cache);
4894 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004895 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004896
Yan Zheng11833d62009-09-11 16:11:19 -04004897 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08004898 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004899
4900 put_caching_control(caching_ctl);
4901 return 0;
4902}
4903
4904static noinline int
4905wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4906{
4907 struct btrfs_caching_control *caching_ctl;
4908 DEFINE_WAIT(wait);
4909
4910 caching_ctl = get_caching_control(cache);
4911 if (!caching_ctl)
4912 return 0;
4913
4914 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4915
4916 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004917 return 0;
4918}
4919
Yan, Zhengb742bb82010-05-16 10:46:24 -04004920static int get_block_group_index(struct btrfs_block_group_cache *cache)
4921{
4922 int index;
4923 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4924 index = 0;
4925 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4926 index = 1;
4927 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4928 index = 2;
4929 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4930 index = 3;
4931 else
4932 index = 4;
4933 return index;
4934}
4935
Josef Bacik817d52f2009-07-13 21:29:25 -04004936enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004937 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004938 LOOP_CACHING_NOWAIT = 1,
4939 LOOP_CACHING_WAIT = 2,
4940 LOOP_ALLOC_CHUNK = 3,
4941 LOOP_NO_EMPTY_SIZE = 4,
4942};
4943
4944/*
Chris Masonfec577f2007-02-26 10:40:21 -05004945 * walks the btree of allocated extents and find a hole of a given size.
4946 * The key ins is changed to record the hole:
4947 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004948 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004949 * ins->offset == number of blocks
4950 * Any available blocks before search_start are skipped.
4951 */
Chris Masond3977122009-01-05 21:25:51 -05004952static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004953 struct btrfs_root *orig_root,
4954 u64 num_bytes, u64 empty_size,
4955 u64 search_start, u64 search_end,
4956 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004957 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004958{
Josef Bacik80eb2342008-10-29 14:49:05 -04004959 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004960 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004961 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004962 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004963 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004964 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004965 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004966 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004967 int last_ptr_loop = 0;
4968 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004969 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004970 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004971 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004972 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004973 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004974 u64 ideal_cache_percent = 0;
4975 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004976
Chris Masondb945352007-10-15 16:15:53 -04004977 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004978 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004979 ins->objectid = 0;
4980 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004981
Josef Bacik2552d172009-04-03 10:14:19 -04004982 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004983 if (!space_info) {
4984 printk(KERN_ERR "No space info for %d\n", data);
4985 return -ENOSPC;
4986 }
Josef Bacik2552d172009-04-03 10:14:19 -04004987
Josef Bacik67377732010-09-16 16:19:09 -04004988 /*
4989 * If the space info is for both data and metadata it means we have a
4990 * small filesystem and we can't use the clustering stuff.
4991 */
4992 if (btrfs_mixed_space_info(space_info))
4993 use_cluster = false;
4994
Chris Mason0ef3e662008-05-24 14:04:53 -04004995 if (orig_root->ref_cows || empty_size)
4996 allowed_chunk_alloc = 1;
4997
Josef Bacik67377732010-09-16 16:19:09 -04004998 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004999 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005000 if (!btrfs_test_opt(root, SSD))
5001 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005002 }
5003
Josef Bacik67377732010-09-16 16:19:09 -04005004 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5005 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005006 last_ptr = &root->fs_info->data_alloc_cluster;
5007 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005008
Chris Mason239b14b2008-03-24 15:02:07 -04005009 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005010 spin_lock(&last_ptr->lock);
5011 if (last_ptr->block_group)
5012 hint_byte = last_ptr->window_start;
5013 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005014 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005015
Chris Masona061fc82008-05-07 11:43:44 -04005016 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005017 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005018
Josef Bacik817d52f2009-07-13 21:29:25 -04005019 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005020 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005021
Josef Bacik2552d172009-04-03 10:14:19 -04005022 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005023ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04005024 block_group = btrfs_lookup_block_group(root->fs_info,
5025 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04005026 /*
5027 * we don't want to use the block group if it doesn't match our
5028 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005029 *
5030 * However if we are re-searching with an ideal block group
5031 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005032 */
5033 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05005034 (block_group->cached != BTRFS_CACHE_NO ||
5035 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04005036 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005037 if (list_empty(&block_group->list) ||
5038 block_group->ro) {
5039 /*
5040 * someone is removing this block group,
5041 * we can't jump into the have_block_group
5042 * target because our list pointers are not
5043 * valid
5044 */
5045 btrfs_put_block_group(block_group);
5046 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005047 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005048 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005049 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005050 }
Josef Bacik2552d172009-04-03 10:14:19 -04005051 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005052 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005053 }
Chris Mason42e70e72008-11-07 18:17:11 -05005054 }
Josef Bacik2552d172009-04-03 10:14:19 -04005055search:
Josef Bacik80eb2342008-10-29 14:49:05 -04005056 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005057 list_for_each_entry(block_group, &space_info->block_groups[index],
5058 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005059 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005060 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005061
Josef Bacik11dfe352009-11-13 20:12:59 +00005062 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005063 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005064
Chris Mason83a50de2010-12-13 15:06:46 -05005065 /*
5066 * this can happen if we end up cycling through all the
5067 * raid types, but we want to make sure we only allocate
5068 * for the proper type.
5069 */
5070 if (!block_group_bits(block_group, data)) {
5071 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5072 BTRFS_BLOCK_GROUP_RAID1 |
5073 BTRFS_BLOCK_GROUP_RAID10;
5074
5075 /*
5076 * if they asked for extra copies and this block group
5077 * doesn't provide them, bail. This does allow us to
5078 * fill raid0 from raid1.
5079 */
5080 if ((data & extra) && !(block_group->flags & extra))
5081 goto loop;
5082 }
5083
Josef Bacik2552d172009-04-03 10:14:19 -04005084have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04005085 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005086 u64 free_percent;
5087
Josef Bacikb8399de2010-12-08 09:15:11 -05005088 ret = cache_block_group(block_group, trans,
5089 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04005090 if (block_group->cached == BTRFS_CACHE_FINISHED)
5091 goto have_block_group;
5092
Josef Bacikccf0e722009-11-10 21:23:48 -05005093 free_percent = btrfs_block_group_used(&block_group->item);
5094 free_percent *= 100;
5095 free_percent = div64_u64(free_percent,
5096 block_group->key.offset);
5097 free_percent = 100 - free_percent;
5098 if (free_percent > ideal_cache_percent &&
5099 likely(!block_group->ro)) {
5100 ideal_cache_offset = block_group->key.objectid;
5101 ideal_cache_percent = free_percent;
5102 }
5103
Josef Bacik817d52f2009-07-13 21:29:25 -04005104 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005105 * We only want to start kthread caching if we are at
5106 * the point where we will wait for caching to make
5107 * progress, or if our ideal search is over and we've
5108 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04005109 */
5110 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05005111 (loop > LOOP_FIND_IDEAL &&
5112 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacikb8399de2010-12-08 09:15:11 -05005113 ret = cache_block_group(block_group, trans,
5114 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005115 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005116 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005117 found_uncached_bg = true;
5118
5119 /*
5120 * If loop is set for cached only, try the next block
5121 * group.
5122 */
5123 if (loop == LOOP_FIND_IDEAL)
5124 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005125 }
5126
Josef Bacik817d52f2009-07-13 21:29:25 -04005127 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005128 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005129 found_uncached_bg = true;
5130
Josef Bacikea6a4782008-11-20 12:16:16 -05005131 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005132 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005133
Josef Bacik0a243252009-09-11 16:11:20 -04005134 /*
5135 * Ok we want to try and use the cluster allocator, so lets look
5136 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5137 * have tried the cluster allocator plenty of times at this
5138 * point and not have found anything, so we are likely way too
5139 * fragmented for the clustering stuff to find anything, so lets
5140 * just skip it and let the allocator find whatever block it can
5141 * find
5142 */
5143 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005144 /*
5145 * the refill lock keeps out other
5146 * people trying to start a new cluster
5147 */
5148 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005149 if (last_ptr->block_group &&
5150 (last_ptr->block_group->ro ||
5151 !block_group_bits(last_ptr->block_group, data))) {
5152 offset = 0;
5153 goto refill_cluster;
5154 }
5155
Chris Masonfa9c0d792009-04-03 09:47:43 -04005156 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5157 num_bytes, search_start);
5158 if (offset) {
5159 /* we have a block, we're done */
5160 spin_unlock(&last_ptr->refill_lock);
5161 goto checks;
5162 }
5163
5164 spin_lock(&last_ptr->lock);
5165 /*
5166 * whoops, this cluster doesn't actually point to
5167 * this block group. Get a ref on the block
5168 * group is does point to and try again
5169 */
5170 if (!last_ptr_loop && last_ptr->block_group &&
5171 last_ptr->block_group != block_group) {
5172
5173 btrfs_put_block_group(block_group);
5174 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005175 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005176 spin_unlock(&last_ptr->lock);
5177 spin_unlock(&last_ptr->refill_lock);
5178
5179 last_ptr_loop = 1;
5180 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005181 /*
5182 * we know this block group is properly
5183 * in the list because
5184 * btrfs_remove_block_group, drops the
5185 * cluster before it removes the block
5186 * group from the list
5187 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04005188 goto have_block_group;
5189 }
5190 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005191refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04005192 /*
5193 * this cluster didn't work out, free it and
5194 * start over
5195 */
5196 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5197
5198 last_ptr_loop = 0;
5199
5200 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005201 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005202 block_group, last_ptr,
5203 offset, num_bytes,
5204 empty_cluster + empty_size);
5205 if (ret == 0) {
5206 /*
5207 * now pull our allocation out of this
5208 * cluster
5209 */
5210 offset = btrfs_alloc_from_cluster(block_group,
5211 last_ptr, num_bytes,
5212 search_start);
5213 if (offset) {
5214 /* we found one, proceed */
5215 spin_unlock(&last_ptr->refill_lock);
5216 goto checks;
5217 }
Josef Bacik0a243252009-09-11 16:11:20 -04005218 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5219 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005220 spin_unlock(&last_ptr->refill_lock);
5221
Josef Bacik0a243252009-09-11 16:11:20 -04005222 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005223 wait_block_group_cache_progress(block_group,
5224 num_bytes + empty_cluster + empty_size);
5225 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005226 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005227
Chris Masonfa9c0d792009-04-03 09:47:43 -04005228 /*
5229 * at this point we either didn't find a cluster
5230 * or we weren't able to allocate a block from our
5231 * cluster. Free the cluster we've been trying
5232 * to use, and go to the next block group
5233 */
Josef Bacik0a243252009-09-11 16:11:20 -04005234 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005235 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005236 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005237 }
5238
Josef Bacik6226cb02009-04-03 10:14:18 -04005239 offset = btrfs_find_space_for_alloc(block_group, search_start,
5240 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005241 /*
5242 * If we didn't find a chunk, and we haven't failed on this
5243 * block group before, and this block group is in the middle of
5244 * caching and we are ok with waiting, then go ahead and wait
5245 * for progress to be made, and set failed_alloc to true.
5246 *
5247 * If failed_alloc is true then we've already waited on this
5248 * block group once and should move on to the next block group.
5249 */
5250 if (!offset && !failed_alloc && !cached &&
5251 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005252 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005253 num_bytes + empty_size);
5254 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005255 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005256 } else if (!offset) {
5257 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005258 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005259checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005260 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005261 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005262 if (search_start + num_bytes >= search_end) {
5263 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005264 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005265 }
Chris Masone37c9e62007-05-09 20:13:14 -04005266
Josef Bacik2552d172009-04-03 10:14:19 -04005267 /* move on to the next group */
5268 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005269 block_group->key.objectid + block_group->key.offset) {
5270 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005271 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005272 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005273
Josef Bacik2552d172009-04-03 10:14:19 -04005274 ins->objectid = search_start;
5275 ins->offset = num_bytes;
5276
Josef Bacik6226cb02009-04-03 10:14:18 -04005277 if (offset < search_start)
5278 btrfs_add_free_space(block_group, offset,
5279 search_start - offset);
5280 BUG_ON(offset > search_start);
5281
Li Dongyangb4d00d52011-03-24 10:24:25 +00005282 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005283 (data & BTRFS_BLOCK_GROUP_DATA));
5284 if (ret == -EAGAIN) {
5285 btrfs_add_free_space(block_group, offset, num_bytes);
5286 goto loop;
5287 }
Yan Zheng11833d62009-09-11 16:11:19 -04005288
Josef Bacik2552d172009-04-03 10:14:19 -04005289 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005290 ins->objectid = search_start;
5291 ins->offset = num_bytes;
5292
5293 if (offset < search_start)
5294 btrfs_add_free_space(block_group, offset,
5295 search_start - offset);
5296 BUG_ON(offset > search_start);
Josef Bacik2552d172009-04-03 10:14:19 -04005297 break;
5298loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005299 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005300 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005301 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d792009-04-03 09:47:43 -04005302 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005303 }
5304 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005305
Yan, Zhengb742bb82010-05-16 10:46:24 -04005306 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5307 goto search;
5308
Josef Bacikccf0e722009-11-10 21:23:48 -05005309 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5310 * for them to make caching progress. Also
5311 * determine the best possible bg to cache
5312 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5313 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005314 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5315 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5316 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5317 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04005318 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005319 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5320 (found_uncached_bg || empty_size || empty_cluster ||
5321 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005322 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005323 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005324 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005325 loop++;
5326 if (!ideal_cache_percent &&
5327 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005328 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005329
5330 /*
5331 * 1 of the following 2 things have happened so far
5332 *
5333 * 1) We found an ideal block group for caching that
5334 * is mostly full and will cache quickly, so we might
5335 * as well wait for it.
5336 *
5337 * 2) We searched for cached only and we didn't find
5338 * anything, and we didn't start any caching kthreads
5339 * either, so chances are we will loop through and
5340 * start a couple caching kthreads, and then come back
5341 * around and just wait for them. This will be slower
5342 * because we will have 2 caching kthreads reading at
5343 * the same time when we could have just started one
5344 * and waited for it to get far enough to give us an
5345 * allocation, so go ahead and go to the wait caching
5346 * loop.
5347 */
5348 loop = LOOP_CACHING_WAIT;
5349 search_start = ideal_cache_offset;
5350 ideal_cache_percent = 0;
5351 goto ideal_cache;
5352 } else if (loop == LOOP_FIND_IDEAL) {
5353 /*
5354 * Didn't find a uncached bg, wait on anything we find
5355 * next.
5356 */
5357 loop = LOOP_CACHING_WAIT;
5358 goto search;
5359 }
5360
5361 if (loop < LOOP_CACHING_WAIT) {
5362 loop++;
5363 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005364 }
5365
5366 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005367 empty_size = 0;
5368 empty_cluster = 0;
5369 }
Chris Mason42e70e72008-11-07 18:17:11 -05005370
Josef Bacik2552d172009-04-03 10:14:19 -04005371 if (allowed_chunk_alloc) {
5372 ret = do_chunk_alloc(trans, root, num_bytes +
Chris Mason0e4f8f82011-04-15 16:05:44 -04005373 2 * 1024 * 1024, data,
5374 CHUNK_ALLOC_LIMITED);
Josef Bacik2552d172009-04-03 10:14:19 -04005375 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005376 done_chunk_alloc = 1;
Chris Mason0e4f8f82011-04-15 16:05:44 -04005377 } else if (!done_chunk_alloc &&
5378 space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) {
5379 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005380 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005381
Josef Bacik817d52f2009-07-13 21:29:25 -04005382 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005383 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005384 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005385 }
Josef Bacik2552d172009-04-03 10:14:19 -04005386 ret = -ENOSPC;
5387 } else if (!ins->objectid) {
5388 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04005389 }
Chris Mason0b86a832008-03-24 15:01:56 -04005390
Josef Bacik80eb2342008-10-29 14:49:05 -04005391 /* we found what we needed */
5392 if (ins->objectid) {
5393 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05005394 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04005395
Chris Masonfa9c0d792009-04-03 09:47:43 -04005396 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005397 ret = 0;
5398 }
Chris Mason0b86a832008-03-24 15:01:56 -04005399
Chris Mason0f70abe2007-02-28 16:46:22 -05005400 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005401}
Chris Masonec44a352008-04-28 15:29:52 -04005402
Josef Bacik9ed74f22009-09-11 16:12:44 -04005403static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5404 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005405{
5406 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005407 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005408
Josef Bacik9ed74f22009-09-11 16:12:44 -04005409 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005410 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5411 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005412 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005413 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005414 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005415 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5416 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005417 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005418 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005419 (unsigned long long)info->bytes_pinned,
5420 (unsigned long long)info->bytes_reserved,
5421 (unsigned long long)info->bytes_may_use,
5422 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005423 spin_unlock(&info->lock);
5424
5425 if (!dump_block_groups)
5426 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005427
Josef Bacik80eb2342008-10-29 14:49:05 -04005428 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005429again:
5430 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005431 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005432 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5433 "%llu pinned %llu reserved\n",
5434 (unsigned long long)cache->key.objectid,
5435 (unsigned long long)cache->key.offset,
5436 (unsigned long long)btrfs_block_group_used(&cache->item),
5437 (unsigned long long)cache->pinned,
5438 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005439 btrfs_dump_free_space(cache, bytes);
5440 spin_unlock(&cache->lock);
5441 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005442 if (++index < BTRFS_NR_RAID_TYPES)
5443 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005444 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005445}
Zheng Yane8569812008-09-26 10:05:48 -04005446
Yan Zheng11833d62009-09-11 16:11:19 -04005447int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5448 struct btrfs_root *root,
5449 u64 num_bytes, u64 min_alloc_size,
5450 u64 empty_size, u64 hint_byte,
5451 u64 search_end, struct btrfs_key *ins,
5452 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005453{
5454 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005455 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005456
Josef Bacik6a632092009-02-20 11:00:09 -05005457 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005458again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005459 /*
5460 * the only place that sets empty_size is btrfs_realloc_node, which
5461 * is not called recursively on allocations
5462 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005463 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005464 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005465 num_bytes + 2 * 1024 * 1024, data,
5466 CHUNK_ALLOC_NO_FORCE);
Chris Mason0b86a832008-03-24 15:01:56 -04005467
Chris Masondb945352007-10-15 16:15:53 -04005468 WARN_ON(num_bytes < root->sectorsize);
5469 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005470 search_start, search_end, hint_byte,
5471 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005472
Chris Mason98d20f62008-04-14 09:46:10 -04005473 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5474 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005475 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005476 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005477 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005478 num_bytes, data, CHUNK_ALLOC_FORCE);
Chris Mason98d20f62008-04-14 09:46:10 -04005479 goto again;
5480 }
Chris Mason91435652011-02-16 13:10:41 -05005481 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005482 struct btrfs_space_info *sinfo;
5483
5484 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005485 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5486 "wanted %llu\n", (unsigned long long)data,
5487 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005488 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005489 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005490
liubo1abe9b82011-03-24 11:18:59 +00005491 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5492
Josef Bacik0f9dd462008-09-23 13:14:11 -04005493 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005494}
5495
Chris Mason65b51a02008-08-01 15:11:20 -04005496int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5497{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005498 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005499 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005500
Josef Bacik0f9dd462008-09-23 13:14:11 -04005501 cache = btrfs_lookup_block_group(root->fs_info, start);
5502 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005503 printk(KERN_ERR "Unable to find block group for %llu\n",
5504 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005505 return -ENOSPC;
5506 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005507
Li Dongyang5378e602011-03-24 10:24:27 +00005508 if (btrfs_test_opt(root, DISCARD))
5509 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005510
Josef Bacik0f9dd462008-09-23 13:14:11 -04005511 btrfs_add_free_space(cache, start, len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00005512 btrfs_update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005513 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005514
liubo1abe9b82011-03-24 11:18:59 +00005515 trace_btrfs_reserved_extent_free(root, start, len);
5516
Chris Masone6dcd2d2008-07-17 12:53:50 -04005517 return ret;
5518}
5519
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005520static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5521 struct btrfs_root *root,
5522 u64 parent, u64 root_objectid,
5523 u64 flags, u64 owner, u64 offset,
5524 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005525{
5526 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005527 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005528 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005529 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005530 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005531 struct extent_buffer *leaf;
5532 int type;
5533 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005534
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005535 if (parent > 0)
5536 type = BTRFS_SHARED_DATA_REF_KEY;
5537 else
5538 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005539
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005540 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005541
5542 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005543 if (!path)
5544 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005545
Chris Masonb9473432009-03-13 11:00:37 -04005546 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005547 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5548 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005549 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005550
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005551 leaf = path->nodes[0];
5552 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005553 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005554 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5555 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5556 btrfs_set_extent_flags(leaf, extent_item,
5557 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005558
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5560 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5561 if (parent > 0) {
5562 struct btrfs_shared_data_ref *ref;
5563 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5564 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5565 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5566 } else {
5567 struct btrfs_extent_data_ref *ref;
5568 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5569 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5570 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5571 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5572 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5573 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005574
5575 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005576 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005577
Yan, Zhengf0486c62010-05-16 10:46:25 -04005578 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005579 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005580 printk(KERN_ERR "btrfs update block group failed for %llu "
5581 "%llu\n", (unsigned long long)ins->objectid,
5582 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005583 BUG();
5584 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005585 return ret;
5586}
5587
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005588static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5589 struct btrfs_root *root,
5590 u64 parent, u64 root_objectid,
5591 u64 flags, struct btrfs_disk_key *key,
5592 int level, struct btrfs_key *ins)
5593{
5594 int ret;
5595 struct btrfs_fs_info *fs_info = root->fs_info;
5596 struct btrfs_extent_item *extent_item;
5597 struct btrfs_tree_block_info *block_info;
5598 struct btrfs_extent_inline_ref *iref;
5599 struct btrfs_path *path;
5600 struct extent_buffer *leaf;
5601 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5602
5603 path = btrfs_alloc_path();
5604 BUG_ON(!path);
5605
5606 path->leave_spinning = 1;
5607 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5608 ins, size);
5609 BUG_ON(ret);
5610
5611 leaf = path->nodes[0];
5612 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5613 struct btrfs_extent_item);
5614 btrfs_set_extent_refs(leaf, extent_item, 1);
5615 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5616 btrfs_set_extent_flags(leaf, extent_item,
5617 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5618 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5619
5620 btrfs_set_tree_block_key(leaf, block_info, key);
5621 btrfs_set_tree_block_level(leaf, block_info, level);
5622
5623 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5624 if (parent > 0) {
5625 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5626 btrfs_set_extent_inline_ref_type(leaf, iref,
5627 BTRFS_SHARED_BLOCK_REF_KEY);
5628 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5629 } else {
5630 btrfs_set_extent_inline_ref_type(leaf, iref,
5631 BTRFS_TREE_BLOCK_REF_KEY);
5632 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5633 }
5634
5635 btrfs_mark_buffer_dirty(leaf);
5636 btrfs_free_path(path);
5637
Yan, Zhengf0486c62010-05-16 10:46:25 -04005638 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005639 if (ret) {
5640 printk(KERN_ERR "btrfs update block group failed for %llu "
5641 "%llu\n", (unsigned long long)ins->objectid,
5642 (unsigned long long)ins->offset);
5643 BUG();
5644 }
5645 return ret;
5646}
5647
5648int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5649 struct btrfs_root *root,
5650 u64 root_objectid, u64 owner,
5651 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005652{
5653 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005654
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005655 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005656
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005657 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5658 0, root_objectid, owner, offset,
5659 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005660 return ret;
5661}
Chris Masone02119d2008-09-05 16:13:11 -04005662
5663/*
5664 * this is used by the tree logging recovery code. It records that
5665 * an extent has been allocated and makes sure to clear the free
5666 * space cache bits as well
5667 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005668int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5669 struct btrfs_root *root,
5670 u64 root_objectid, u64 owner, u64 offset,
5671 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005672{
5673 int ret;
5674 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005675 struct btrfs_caching_control *caching_ctl;
5676 u64 start = ins->objectid;
5677 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005678
Chris Masone02119d2008-09-05 16:13:11 -04005679 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005680 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005681 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005682
Yan Zheng11833d62009-09-11 16:11:19 -04005683 if (!caching_ctl) {
5684 BUG_ON(!block_group_cache_done(block_group));
5685 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5686 BUG_ON(ret);
5687 } else {
5688 mutex_lock(&caching_ctl->mutex);
5689
5690 if (start >= caching_ctl->progress) {
5691 ret = add_excluded_extent(root, start, num_bytes);
5692 BUG_ON(ret);
5693 } else if (start + num_bytes <= caching_ctl->progress) {
5694 ret = btrfs_remove_free_space(block_group,
5695 start, num_bytes);
5696 BUG_ON(ret);
5697 } else {
5698 num_bytes = caching_ctl->progress - start;
5699 ret = btrfs_remove_free_space(block_group,
5700 start, num_bytes);
5701 BUG_ON(ret);
5702
5703 start = caching_ctl->progress;
5704 num_bytes = ins->objectid + ins->offset -
5705 caching_ctl->progress;
5706 ret = add_excluded_extent(root, start, num_bytes);
5707 BUG_ON(ret);
5708 }
5709
5710 mutex_unlock(&caching_ctl->mutex);
5711 put_caching_control(caching_ctl);
5712 }
5713
Li Dongyangb4d00d52011-03-24 10:24:25 +00005714 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005715 BUG_ON(ret);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005716 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005717 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5718 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005719 return ret;
5720}
5721
Chris Mason65b51a02008-08-01 15:11:20 -04005722struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5723 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005724 u64 bytenr, u32 blocksize,
5725 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005726{
5727 struct extent_buffer *buf;
5728
5729 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5730 if (!buf)
5731 return ERR_PTR(-ENOMEM);
5732 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005733 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005734 btrfs_tree_lock(buf);
5735 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005736
5737 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005738 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005739
Chris Masond0c803c2008-09-11 16:17:57 -04005740 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005741 /*
5742 * we allow two log transactions at a time, use different
5743 * EXENT bit to differentiate dirty pages.
5744 */
5745 if (root->log_transid % 2 == 0)
5746 set_extent_dirty(&root->dirty_log_pages, buf->start,
5747 buf->start + buf->len - 1, GFP_NOFS);
5748 else
5749 set_extent_new(&root->dirty_log_pages, buf->start,
5750 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005751 } else {
5752 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5753 buf->start + buf->len - 1, GFP_NOFS);
5754 }
Chris Mason65b51a02008-08-01 15:11:20 -04005755 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005756 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005757 return buf;
5758}
5759
Yan, Zhengf0486c62010-05-16 10:46:25 -04005760static struct btrfs_block_rsv *
5761use_block_rsv(struct btrfs_trans_handle *trans,
5762 struct btrfs_root *root, u32 blocksize)
5763{
5764 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005765 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005766 int ret;
5767
5768 block_rsv = get_block_rsv(trans, root);
5769
5770 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005771 ret = reserve_metadata_bytes(trans, root, block_rsv,
5772 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005773 /*
5774 * If we couldn't reserve metadata bytes try and use some from
5775 * the global reserve.
5776 */
5777 if (ret && block_rsv != global_rsv) {
5778 ret = block_rsv_use_bytes(global_rsv, blocksize);
5779 if (!ret)
5780 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005781 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005782 } else if (ret) {
5783 return ERR_PTR(ret);
5784 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005785 return block_rsv;
5786 }
5787
5788 ret = block_rsv_use_bytes(block_rsv, blocksize);
5789 if (!ret)
5790 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005791 if (ret) {
5792 WARN_ON(1);
5793 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5794 0);
5795 if (!ret) {
5796 spin_lock(&block_rsv->lock);
5797 block_rsv->size += blocksize;
5798 spin_unlock(&block_rsv->lock);
5799 return block_rsv;
5800 } else if (ret && block_rsv != global_rsv) {
5801 ret = block_rsv_use_bytes(global_rsv, blocksize);
5802 if (!ret)
5803 return global_rsv;
5804 }
5805 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005806
Yan, Zhengf0486c62010-05-16 10:46:25 -04005807 return ERR_PTR(-ENOSPC);
5808}
5809
5810static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5811{
5812 block_rsv_add_bytes(block_rsv, blocksize, 0);
5813 block_rsv_release_bytes(block_rsv, NULL, 0);
5814}
5815
Chris Masonfec577f2007-02-26 10:40:21 -05005816/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005817 * finds a free extent and does all the dirty work required for allocation
5818 * returns the key for the extent through ins, and a tree buffer for
5819 * the first block of the extent through buf.
5820 *
Chris Masonfec577f2007-02-26 10:40:21 -05005821 * returns the tree buffer or NULL.
5822 */
Chris Mason5f39d392007-10-15 16:14:19 -04005823struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005824 struct btrfs_root *root, u32 blocksize,
5825 u64 parent, u64 root_objectid,
5826 struct btrfs_disk_key *key, int level,
5827 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005828{
Chris Masone2fa7222007-03-12 16:22:34 -04005829 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005830 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005831 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005832 u64 flags = 0;
5833 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005834
Yan, Zhengf0486c62010-05-16 10:46:25 -04005835
5836 block_rsv = use_block_rsv(trans, root, blocksize);
5837 if (IS_ERR(block_rsv))
5838 return ERR_CAST(block_rsv);
5839
5840 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5841 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005842 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005843 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005844 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005845 }
Chris Mason55c69072008-01-09 15:55:33 -05005846
Chris Mason4008c042009-02-12 14:09:45 -05005847 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5848 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005849 BUG_ON(IS_ERR(buf));
5850
5851 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5852 if (parent == 0)
5853 parent = ins.objectid;
5854 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5855 } else
5856 BUG_ON(parent > 0);
5857
5858 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5859 struct btrfs_delayed_extent_op *extent_op;
5860 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5861 BUG_ON(!extent_op);
5862 if (key)
5863 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5864 else
5865 memset(&extent_op->key, 0, sizeof(extent_op->key));
5866 extent_op->flags_to_set = flags;
5867 extent_op->update_key = 1;
5868 extent_op->update_flags = 1;
5869 extent_op->is_data = 0;
5870
5871 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5872 ins.offset, parent, root_objectid,
5873 level, BTRFS_ADD_DELAYED_EXTENT,
5874 extent_op);
5875 BUG_ON(ret);
5876 }
Chris Masonfec577f2007-02-26 10:40:21 -05005877 return buf;
5878}
Chris Masona28ec192007-03-06 20:08:01 -05005879
Yan Zheng2c47e6052009-06-27 21:07:35 -04005880struct walk_control {
5881 u64 refs[BTRFS_MAX_LEVEL];
5882 u64 flags[BTRFS_MAX_LEVEL];
5883 struct btrfs_key update_progress;
5884 int stage;
5885 int level;
5886 int shared_level;
5887 int update_ref;
5888 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005889 int reada_slot;
5890 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005891};
5892
5893#define DROP_REFERENCE 1
5894#define UPDATE_BACKREF 2
5895
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005896static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5897 struct btrfs_root *root,
5898 struct walk_control *wc,
5899 struct btrfs_path *path)
5900{
5901 u64 bytenr;
5902 u64 generation;
5903 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005904 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005905 u32 nritems;
5906 u32 blocksize;
5907 struct btrfs_key key;
5908 struct extent_buffer *eb;
5909 int ret;
5910 int slot;
5911 int nread = 0;
5912
5913 if (path->slots[wc->level] < wc->reada_slot) {
5914 wc->reada_count = wc->reada_count * 2 / 3;
5915 wc->reada_count = max(wc->reada_count, 2);
5916 } else {
5917 wc->reada_count = wc->reada_count * 3 / 2;
5918 wc->reada_count = min_t(int, wc->reada_count,
5919 BTRFS_NODEPTRS_PER_BLOCK(root));
5920 }
5921
5922 eb = path->nodes[wc->level];
5923 nritems = btrfs_header_nritems(eb);
5924 blocksize = btrfs_level_size(root, wc->level - 1);
5925
5926 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5927 if (nread >= wc->reada_count)
5928 break;
5929
5930 cond_resched();
5931 bytenr = btrfs_node_blockptr(eb, slot);
5932 generation = btrfs_node_ptr_generation(eb, slot);
5933
5934 if (slot == path->slots[wc->level])
5935 goto reada;
5936
5937 if (wc->stage == UPDATE_BACKREF &&
5938 generation <= root->root_key.offset)
5939 continue;
5940
Yan, Zheng94fcca92009-10-09 09:25:16 -04005941 /* We don't lock the tree block, it's OK to be racy here */
5942 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5943 &refs, &flags);
5944 BUG_ON(ret);
5945 BUG_ON(refs == 0);
5946
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005947 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005948 if (refs == 1)
5949 goto reada;
5950
Yan, Zheng94fcca92009-10-09 09:25:16 -04005951 if (wc->level == 1 &&
5952 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5953 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005954 if (!wc->update_ref ||
5955 generation <= root->root_key.offset)
5956 continue;
5957 btrfs_node_key_to_cpu(eb, &key, slot);
5958 ret = btrfs_comp_cpu_keys(&key,
5959 &wc->update_progress);
5960 if (ret < 0)
5961 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005962 } else {
5963 if (wc->level == 1 &&
5964 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5965 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005966 }
5967reada:
5968 ret = readahead_tree_block(root, bytenr, blocksize,
5969 generation);
5970 if (ret)
5971 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005972 nread++;
5973 }
5974 wc->reada_slot = slot;
5975}
5976
Chris Mason9aca1d52007-03-13 11:09:37 -04005977/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005978 * hepler to process tree block while walking down the tree.
5979 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005980 * when wc->stage == UPDATE_BACKREF, this function updates
5981 * back refs for pointers in the block.
5982 *
5983 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005984 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005985static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5986 struct btrfs_root *root,
5987 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005988 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005989{
5990 int level = wc->level;
5991 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005992 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5993 int ret;
5994
5995 if (wc->stage == UPDATE_BACKREF &&
5996 btrfs_header_owner(eb) != root->root_key.objectid)
5997 return 1;
5998
5999 /*
6000 * when reference count of tree block is 1, it won't increase
6001 * again. once full backref flag is set, we never clear it.
6002 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006003 if (lookup_info &&
6004 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6005 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006006 BUG_ON(!path->locks[level]);
6007 ret = btrfs_lookup_extent_info(trans, root,
6008 eb->start, eb->len,
6009 &wc->refs[level],
6010 &wc->flags[level]);
6011 BUG_ON(ret);
6012 BUG_ON(wc->refs[level] == 0);
6013 }
6014
Yan Zheng2c47e6052009-06-27 21:07:35 -04006015 if (wc->stage == DROP_REFERENCE) {
6016 if (wc->refs[level] > 1)
6017 return 1;
6018
6019 if (path->locks[level] && !wc->keep_locks) {
6020 btrfs_tree_unlock(eb);
6021 path->locks[level] = 0;
6022 }
6023 return 0;
6024 }
6025
6026 /* wc->stage == UPDATE_BACKREF */
6027 if (!(wc->flags[level] & flag)) {
6028 BUG_ON(!path->locks[level]);
6029 ret = btrfs_inc_ref(trans, root, eb, 1);
6030 BUG_ON(ret);
6031 ret = btrfs_dec_ref(trans, root, eb, 0);
6032 BUG_ON(ret);
6033 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6034 eb->len, flag, 0);
6035 BUG_ON(ret);
6036 wc->flags[level] |= flag;
6037 }
6038
6039 /*
6040 * the block is shared by multiple trees, so it's not good to
6041 * keep the tree lock
6042 */
6043 if (path->locks[level] && level > 0) {
6044 btrfs_tree_unlock(eb);
6045 path->locks[level] = 0;
6046 }
6047 return 0;
6048}
6049
6050/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006051 * hepler to process tree block pointer.
6052 *
6053 * when wc->stage == DROP_REFERENCE, this function checks
6054 * reference count of the block pointed to. if the block
6055 * is shared and we need update back refs for the subtree
6056 * rooted at the block, this function changes wc->stage to
6057 * UPDATE_BACKREF. if the block is shared and there is no
6058 * need to update back, this function drops the reference
6059 * to the block.
6060 *
6061 * NOTE: return value 1 means we should stop walking down.
6062 */
6063static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6064 struct btrfs_root *root,
6065 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006066 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006067{
6068 u64 bytenr;
6069 u64 generation;
6070 u64 parent;
6071 u32 blocksize;
6072 struct btrfs_key key;
6073 struct extent_buffer *next;
6074 int level = wc->level;
6075 int reada = 0;
6076 int ret = 0;
6077
6078 generation = btrfs_node_ptr_generation(path->nodes[level],
6079 path->slots[level]);
6080 /*
6081 * if the lower level block was created before the snapshot
6082 * was created, we know there is no need to update back refs
6083 * for the subtree
6084 */
6085 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006086 generation <= root->root_key.offset) {
6087 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006088 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006089 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006090
6091 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6092 blocksize = btrfs_level_size(root, level - 1);
6093
6094 next = btrfs_find_tree_block(root, bytenr, blocksize);
6095 if (!next) {
6096 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00006097 if (!next)
6098 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006099 reada = 1;
6100 }
6101 btrfs_tree_lock(next);
6102 btrfs_set_lock_blocking(next);
6103
Yan, Zheng94fcca92009-10-09 09:25:16 -04006104 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6105 &wc->refs[level - 1],
6106 &wc->flags[level - 1]);
6107 BUG_ON(ret);
6108 BUG_ON(wc->refs[level - 1] == 0);
6109 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006110
Yan, Zheng94fcca92009-10-09 09:25:16 -04006111 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006112 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006113 if (level == 1 &&
6114 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6115 goto skip;
6116
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006117 if (!wc->update_ref ||
6118 generation <= root->root_key.offset)
6119 goto skip;
6120
6121 btrfs_node_key_to_cpu(path->nodes[level], &key,
6122 path->slots[level]);
6123 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6124 if (ret < 0)
6125 goto skip;
6126
6127 wc->stage = UPDATE_BACKREF;
6128 wc->shared_level = level - 1;
6129 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006130 } else {
6131 if (level == 1 &&
6132 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6133 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006134 }
6135
6136 if (!btrfs_buffer_uptodate(next, generation)) {
6137 btrfs_tree_unlock(next);
6138 free_extent_buffer(next);
6139 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006140 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006141 }
6142
6143 if (!next) {
6144 if (reada && level == 1)
6145 reada_walk_down(trans, root, wc, path);
6146 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006147 if (!next)
6148 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006149 btrfs_tree_lock(next);
6150 btrfs_set_lock_blocking(next);
6151 }
6152
6153 level--;
6154 BUG_ON(level != btrfs_header_level(next));
6155 path->nodes[level] = next;
6156 path->slots[level] = 0;
6157 path->locks[level] = 1;
6158 wc->level = level;
6159 if (wc->level == 1)
6160 wc->reada_slot = 0;
6161 return 0;
6162skip:
6163 wc->refs[level - 1] = 0;
6164 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006165 if (wc->stage == DROP_REFERENCE) {
6166 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6167 parent = path->nodes[level]->start;
6168 } else {
6169 BUG_ON(root->root_key.objectid !=
6170 btrfs_header_owner(path->nodes[level]));
6171 parent = 0;
6172 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006173
Yan, Zheng94fcca92009-10-09 09:25:16 -04006174 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6175 root->root_key.objectid, level - 1, 0);
6176 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006177 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006178 btrfs_tree_unlock(next);
6179 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006180 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006181 return 1;
6182}
6183
6184/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006185 * hepler to process tree block while walking up the tree.
6186 *
6187 * when wc->stage == DROP_REFERENCE, this function drops
6188 * reference count on the block.
6189 *
6190 * when wc->stage == UPDATE_BACKREF, this function changes
6191 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6192 * to UPDATE_BACKREF previously while processing the block.
6193 *
6194 * NOTE: return value 1 means we should stop walking up.
6195 */
6196static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6197 struct btrfs_root *root,
6198 struct btrfs_path *path,
6199 struct walk_control *wc)
6200{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006201 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006202 int level = wc->level;
6203 struct extent_buffer *eb = path->nodes[level];
6204 u64 parent = 0;
6205
6206 if (wc->stage == UPDATE_BACKREF) {
6207 BUG_ON(wc->shared_level < level);
6208 if (level < wc->shared_level)
6209 goto out;
6210
Yan Zheng2c47e6052009-06-27 21:07:35 -04006211 ret = find_next_key(path, level + 1, &wc->update_progress);
6212 if (ret > 0)
6213 wc->update_ref = 0;
6214
6215 wc->stage = DROP_REFERENCE;
6216 wc->shared_level = -1;
6217 path->slots[level] = 0;
6218
6219 /*
6220 * check reference count again if the block isn't locked.
6221 * we should start walking down the tree again if reference
6222 * count is one.
6223 */
6224 if (!path->locks[level]) {
6225 BUG_ON(level == 0);
6226 btrfs_tree_lock(eb);
6227 btrfs_set_lock_blocking(eb);
6228 path->locks[level] = 1;
6229
6230 ret = btrfs_lookup_extent_info(trans, root,
6231 eb->start, eb->len,
6232 &wc->refs[level],
6233 &wc->flags[level]);
6234 BUG_ON(ret);
6235 BUG_ON(wc->refs[level] == 0);
6236 if (wc->refs[level] == 1) {
6237 btrfs_tree_unlock(eb);
6238 path->locks[level] = 0;
6239 return 1;
6240 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006241 }
6242 }
6243
6244 /* wc->stage == DROP_REFERENCE */
6245 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6246
6247 if (wc->refs[level] == 1) {
6248 if (level == 0) {
6249 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6250 ret = btrfs_dec_ref(trans, root, eb, 1);
6251 else
6252 ret = btrfs_dec_ref(trans, root, eb, 0);
6253 BUG_ON(ret);
6254 }
6255 /* make block locked assertion in clean_tree_block happy */
6256 if (!path->locks[level] &&
6257 btrfs_header_generation(eb) == trans->transid) {
6258 btrfs_tree_lock(eb);
6259 btrfs_set_lock_blocking(eb);
6260 path->locks[level] = 1;
6261 }
6262 clean_tree_block(trans, root, eb);
6263 }
6264
6265 if (eb == root->node) {
6266 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6267 parent = eb->start;
6268 else
6269 BUG_ON(root->root_key.objectid !=
6270 btrfs_header_owner(eb));
6271 } else {
6272 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6273 parent = path->nodes[level + 1]->start;
6274 else
6275 BUG_ON(root->root_key.objectid !=
6276 btrfs_header_owner(path->nodes[level + 1]));
6277 }
6278
Yan, Zhengf0486c62010-05-16 10:46:25 -04006279 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006280out:
6281 wc->refs[level] = 0;
6282 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006283 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006284}
6285
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006286static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6287 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006288 struct btrfs_path *path,
6289 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006290{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006291 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006292 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006293 int ret;
6294
Yan Zheng2c47e6052009-06-27 21:07:35 -04006295 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006296 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006297 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006298 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006299
Yan Zheng2c47e6052009-06-27 21:07:35 -04006300 if (level == 0)
6301 break;
6302
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006303 if (path->slots[level] >=
6304 btrfs_header_nritems(path->nodes[level]))
6305 break;
6306
Yan, Zheng94fcca92009-10-09 09:25:16 -04006307 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006308 if (ret > 0) {
6309 path->slots[level]++;
6310 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006311 } else if (ret < 0)
6312 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006313 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006314 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006315 return 0;
6316}
6317
Chris Masond3977122009-01-05 21:25:51 -05006318static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006319 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006320 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006321 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006322{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006323 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006324 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006325
Yan Zheng2c47e6052009-06-27 21:07:35 -04006326 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6327 while (level < max_level && path->nodes[level]) {
6328 wc->level = level;
6329 if (path->slots[level] + 1 <
6330 btrfs_header_nritems(path->nodes[level])) {
6331 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006332 return 0;
6333 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006334 ret = walk_up_proc(trans, root, path, wc);
6335 if (ret > 0)
6336 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006337
Yan Zheng2c47e6052009-06-27 21:07:35 -04006338 if (path->locks[level]) {
6339 btrfs_tree_unlock(path->nodes[level]);
6340 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006341 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006342 free_extent_buffer(path->nodes[level]);
6343 path->nodes[level] = NULL;
6344 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006345 }
6346 }
6347 return 1;
6348}
6349
Chris Mason9aca1d52007-03-13 11:09:37 -04006350/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006351 * drop a subvolume tree.
6352 *
6353 * this function traverses the tree freeing any blocks that only
6354 * referenced by the tree.
6355 *
6356 * when a shared tree block is found. this function decreases its
6357 * reference count by one. if update_ref is true, this function
6358 * also make sure backrefs for the shared block and all lower level
6359 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006360 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006361int btrfs_drop_snapshot(struct btrfs_root *root,
6362 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006363{
Chris Mason5caf2a02007-04-02 11:20:42 -04006364 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006365 struct btrfs_trans_handle *trans;
6366 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006367 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006368 struct walk_control *wc;
6369 struct btrfs_key key;
6370 int err = 0;
6371 int ret;
6372 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006373
Chris Mason5caf2a02007-04-02 11:20:42 -04006374 path = btrfs_alloc_path();
6375 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006376
Yan Zheng2c47e6052009-06-27 21:07:35 -04006377 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6378 BUG_ON(!wc);
6379
Yan, Zhenga22285a2010-05-16 10:48:46 -04006380 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006381 BUG_ON(IS_ERR(trans));
6382
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006383 if (block_rsv)
6384 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006385
Chris Mason9f3a7422007-08-07 15:52:19 -04006386 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006387 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006388 path->nodes[level] = btrfs_lock_root_node(root);
6389 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006390 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006391 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006392 memset(&wc->update_progress, 0,
6393 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006394 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006395 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006396 memcpy(&wc->update_progress, &key,
6397 sizeof(wc->update_progress));
6398
Chris Mason6702ed42007-08-07 16:15:09 -04006399 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006400 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006401 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006402 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6403 path->lowest_level = 0;
6404 if (ret < 0) {
6405 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006406 goto out;
6407 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006408 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006409
Chris Mason7d9eb122008-07-08 14:19:17 -04006410 /*
6411 * unlock our path, this is safe because only this
6412 * function is allowed to delete this snapshot
6413 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006414 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006415
Yan Zheng2c47e6052009-06-27 21:07:35 -04006416 level = btrfs_header_level(root->node);
6417 while (1) {
6418 btrfs_tree_lock(path->nodes[level]);
6419 btrfs_set_lock_blocking(path->nodes[level]);
6420
6421 ret = btrfs_lookup_extent_info(trans, root,
6422 path->nodes[level]->start,
6423 path->nodes[level]->len,
6424 &wc->refs[level],
6425 &wc->flags[level]);
6426 BUG_ON(ret);
6427 BUG_ON(wc->refs[level] == 0);
6428
6429 if (level == root_item->drop_level)
6430 break;
6431
6432 btrfs_tree_unlock(path->nodes[level]);
6433 WARN_ON(wc->refs[level] != 1);
6434 level--;
6435 }
6436 }
6437
6438 wc->level = level;
6439 wc->shared_level = -1;
6440 wc->stage = DROP_REFERENCE;
6441 wc->update_ref = update_ref;
6442 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006443 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006444
6445 while (1) {
6446 ret = walk_down_tree(trans, root, path, wc);
6447 if (ret < 0) {
6448 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006449 break;
6450 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006451
6452 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6453 if (ret < 0) {
6454 err = ret;
6455 break;
6456 }
6457
6458 if (ret > 0) {
6459 BUG_ON(wc->stage != DROP_REFERENCE);
6460 break;
6461 }
6462
6463 if (wc->stage == DROP_REFERENCE) {
6464 level = wc->level;
6465 btrfs_node_key(path->nodes[level],
6466 &root_item->drop_progress,
6467 path->slots[level]);
6468 root_item->drop_level = level;
6469 }
6470
6471 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006472 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006473 ret = btrfs_update_root(trans, tree_root,
6474 &root->root_key,
6475 root_item);
6476 BUG_ON(ret);
6477
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006478 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006479 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006480 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006481 if (block_rsv)
6482 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006483 }
Chris Mason20524f02007-03-10 06:35:47 -05006484 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006485 btrfs_release_path(root, path);
6486 BUG_ON(err);
6487
6488 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6489 BUG_ON(ret);
6490
Yan, Zheng76dda932009-09-21 16:00:26 -04006491 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6492 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6493 NULL, NULL);
6494 BUG_ON(ret < 0);
6495 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006496 /* if we fail to delete the orphan item this time
6497 * around, it'll get picked up the next time.
6498 *
6499 * The most common failure here is just -ENOENT.
6500 */
6501 btrfs_del_orphan_item(trans, tree_root,
6502 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006503 }
6504 }
6505
6506 if (root->in_radix) {
6507 btrfs_free_fs_root(tree_root->fs_info, root);
6508 } else {
6509 free_extent_buffer(root->node);
6510 free_extent_buffer(root->commit_root);
6511 kfree(root);
6512 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006513out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006514 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006515 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006516 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006517 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006518}
Chris Mason9078a3e2007-04-26 16:46:15 -04006519
Yan Zheng2c47e6052009-06-27 21:07:35 -04006520/*
6521 * drop subtree rooted at tree block 'node'.
6522 *
6523 * NOTE: this function will unlock and release tree block 'node'
6524 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006525int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6526 struct btrfs_root *root,
6527 struct extent_buffer *node,
6528 struct extent_buffer *parent)
6529{
6530 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006531 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006532 int level;
6533 int parent_level;
6534 int ret = 0;
6535 int wret;
6536
Yan Zheng2c47e6052009-06-27 21:07:35 -04006537 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6538
Yan Zhengf82d02d2008-10-29 14:49:05 -04006539 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006540 if (!path)
6541 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006542
Yan Zheng2c47e6052009-06-27 21:07:35 -04006543 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006544 if (!wc) {
6545 btrfs_free_path(path);
6546 return -ENOMEM;
6547 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006548
Chris Masonb9447ef2009-03-09 11:45:38 -04006549 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006550 parent_level = btrfs_header_level(parent);
6551 extent_buffer_get(parent);
6552 path->nodes[parent_level] = parent;
6553 path->slots[parent_level] = btrfs_header_nritems(parent);
6554
Chris Masonb9447ef2009-03-09 11:45:38 -04006555 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006556 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006557 path->nodes[level] = node;
6558 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006559 path->locks[level] = 1;
6560
6561 wc->refs[parent_level] = 1;
6562 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6563 wc->level = level;
6564 wc->shared_level = -1;
6565 wc->stage = DROP_REFERENCE;
6566 wc->update_ref = 0;
6567 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006568 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006569
6570 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006571 wret = walk_down_tree(trans, root, path, wc);
6572 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006573 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006574 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006575 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006576
Yan Zheng2c47e6052009-06-27 21:07:35 -04006577 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006578 if (wret < 0)
6579 ret = wret;
6580 if (wret != 0)
6581 break;
6582 }
6583
Yan Zheng2c47e6052009-06-27 21:07:35 -04006584 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006585 btrfs_free_path(path);
6586 return ret;
6587}
6588
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006589#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04006590static unsigned long calc_ra(unsigned long start, unsigned long last,
6591 unsigned long nr)
6592{
6593 return min(last, start + nr - 1);
6594}
6595
Chris Masond3977122009-01-05 21:25:51 -05006596static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05006597 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05006598{
6599 u64 page_start;
6600 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04006601 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006602 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006603 unsigned long i;
6604 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05006605 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05006606 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04006607 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04006608 unsigned int total_read = 0;
6609 unsigned int total_dirty = 0;
6610 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05006611
6612 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00006613 if (!ra)
6614 return -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006615
6616 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006617 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05006618 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6619
Zheng Yan1a40e232008-09-26 10:09:34 -04006620 /* make sure the dirty trick played by the caller work */
6621 ret = invalidate_inode_pages2_range(inode->i_mapping,
6622 first_index, last_index);
6623 if (ret)
6624 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04006625
Chris Mason4313b392008-01-03 09:08:48 -05006626 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05006627
Zheng Yan1a40e232008-09-26 10:09:34 -04006628 for (i = first_index ; i <= last_index; i++) {
6629 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04006630 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04006631 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04006632 }
6633 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04006634again:
6635 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04006636 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05006637 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04006638 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006639 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006640 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04006641 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006642 if (!PageUptodate(page)) {
6643 btrfs_readpage(NULL, page);
6644 lock_page(page);
6645 if (!PageUptodate(page)) {
6646 unlock_page(page);
6647 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006648 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05006649 goto out_unlock;
6650 }
6651 }
Chris Masonec44a352008-04-28 15:29:52 -04006652 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04006653
Chris Masonedbd8d42007-12-21 16:27:24 -05006654 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6655 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006656 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006657
Chris Mason3eaa2882008-07-24 11:57:52 -04006658 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6659 if (ordered) {
6660 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6661 unlock_page(page);
6662 page_cache_release(page);
6663 btrfs_start_ordered_extent(inode, ordered, 1);
6664 btrfs_put_ordered_extent(ordered);
6665 goto again;
6666 }
6667 set_page_extent_mapped(page);
6668
Zheng Yan1a40e232008-09-26 10:09:34 -04006669 if (i == first_index)
6670 set_extent_bits(io_tree, page_start, page_end,
6671 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006672 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04006673
Chris Masona061fc82008-05-07 11:43:44 -04006674 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006675 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006676
Chris Masond1310b22008-01-24 16:13:08 -05006677 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006678 unlock_page(page);
6679 page_cache_release(page);
6680 }
6681
6682out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04006683 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05006684 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006685 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04006686 return ret;
6687}
6688
Chris Masond3977122009-01-05 21:25:51 -05006689static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006690 struct btrfs_key *extent_key,
6691 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05006692{
Zheng Yan1a40e232008-09-26 10:09:34 -04006693 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6694 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6695 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04006696 u64 start = extent_key->objectid - offset;
6697 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006698
6699 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +00006700 BUG_ON(!em);
Zheng Yan1a40e232008-09-26 10:09:34 -04006701
Yan Zheng66435582008-10-30 14:19:50 -04006702 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006703 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04006704 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04006705 em->block_start = extent_key->objectid;
6706 em->bdev = root->fs_info->fs_devices->latest_bdev;
6707 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6708
6709 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04006710 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006711 while (1) {
6712 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04006713 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006714 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04006715 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006716 if (ret != -EEXIST) {
6717 free_extent_map(em);
6718 break;
6719 }
Yan Zheng66435582008-10-30 14:19:50 -04006720 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006721 }
Yan Zheng66435582008-10-30 14:19:50 -04006722 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006723
Yan Zheng66435582008-10-30 14:19:50 -04006724 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04006725}
6726
6727struct btrfs_ref_path {
6728 u64 extent_start;
6729 u64 nodes[BTRFS_MAX_LEVEL];
6730 u64 root_objectid;
6731 u64 root_generation;
6732 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006733 u32 num_refs;
6734 int lowest_level;
6735 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006736 int shared_level;
6737
6738 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6739 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04006740};
6741
6742struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04006743 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04006744 u64 disk_bytenr;
6745 u64 disk_num_bytes;
6746 u64 offset;
6747 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04006748 u8 compression;
6749 u8 encryption;
6750 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04006751};
6752
6753static int is_cowonly_root(u64 root_objectid)
6754{
6755 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6756 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6757 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6758 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05006759 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6760 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04006761 return 1;
6762 return 0;
6763}
6764
Chris Masond3977122009-01-05 21:25:51 -05006765static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006766 struct btrfs_root *extent_root,
6767 struct btrfs_ref_path *ref_path,
6768 int first_time)
6769{
6770 struct extent_buffer *leaf;
6771 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05006772 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05006773 struct btrfs_key key;
6774 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04006775 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05006776 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04006777 int level;
6778 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006779
Zheng Yan1a40e232008-09-26 10:09:34 -04006780 path = btrfs_alloc_path();
6781 if (!path)
6782 return -ENOMEM;
6783
Zheng Yan1a40e232008-09-26 10:09:34 -04006784 if (first_time) {
6785 ref_path->lowest_level = -1;
6786 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006787 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006788 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04006789 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006790walk_down:
6791 level = ref_path->current_level - 1;
6792 while (level >= -1) {
6793 u64 parent;
6794 if (level < ref_path->lowest_level)
6795 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006796
Chris Masond3977122009-01-05 21:25:51 -05006797 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006798 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006799 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006800 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006801 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006802
Zheng Yan1a40e232008-09-26 10:09:34 -04006803 parent = ref_path->nodes[level + 1];
6804 ref_path->nodes[level + 1] = 0;
6805 ref_path->current_level = level;
6806 BUG_ON(parent == 0);
6807
6808 key.objectid = bytenr;
6809 key.offset = parent + 1;
6810 key.type = BTRFS_EXTENT_REF_KEY;
6811
6812 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006813 if (ret < 0)
6814 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006815 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006816
Chris Masonedbd8d42007-12-21 16:27:24 -05006817 leaf = path->nodes[0];
6818 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04006819 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04006820 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04006821 if (ret < 0)
6822 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006823 if (ret > 0)
6824 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04006825 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04006826 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006827
6828 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04006829 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04006830 found_key.type == BTRFS_EXTENT_REF_KEY) {
6831 if (level < ref_path->shared_level)
6832 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006833 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006834 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006835next:
6836 level--;
6837 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006838 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006839 }
6840 /* reached lowest level */
6841 ret = 1;
6842 goto out;
6843walk_up:
6844 level = ref_path->current_level;
6845 while (level < BTRFS_MAX_LEVEL - 1) {
6846 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006847
6848 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006849 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006850 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006851 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006852
Zheng Yan1a40e232008-09-26 10:09:34 -04006853 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006854
Zheng Yan1a40e232008-09-26 10:09:34 -04006855 key.objectid = bytenr;
6856 key.offset = 0;
6857 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006858
Zheng Yan1a40e232008-09-26 10:09:34 -04006859 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6860 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006861 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006862
6863 leaf = path->nodes[0];
6864 nritems = btrfs_header_nritems(leaf);
6865 if (path->slots[0] >= nritems) {
6866 ret = btrfs_next_leaf(extent_root, path);
6867 if (ret < 0)
6868 goto out;
6869 if (ret > 0) {
6870 /* the extent was freed by someone */
6871 if (ref_path->lowest_level == level)
6872 goto out;
6873 btrfs_release_path(extent_root, path);
6874 goto walk_down;
6875 }
6876 leaf = path->nodes[0];
6877 }
6878
6879 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6880 if (found_key.objectid != bytenr ||
6881 found_key.type != BTRFS_EXTENT_REF_KEY) {
6882 /* the extent was freed by someone */
6883 if (ref_path->lowest_level == level) {
6884 ret = 1;
6885 goto out;
6886 }
6887 btrfs_release_path(extent_root, path);
6888 goto walk_down;
6889 }
6890found:
6891 ref = btrfs_item_ptr(leaf, path->slots[0],
6892 struct btrfs_extent_ref);
6893 ref_objectid = btrfs_ref_objectid(leaf, ref);
6894 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6895 if (first_time) {
6896 level = (int)ref_objectid;
6897 BUG_ON(level >= BTRFS_MAX_LEVEL);
6898 ref_path->lowest_level = level;
6899 ref_path->current_level = level;
6900 ref_path->nodes[level] = bytenr;
6901 } else {
6902 WARN_ON(ref_objectid != level);
6903 }
6904 } else {
6905 WARN_ON(level != -1);
6906 }
6907 first_time = 0;
6908
6909 if (ref_path->lowest_level == level) {
6910 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006911 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6912 }
6913
6914 /*
6915 * the block is tree root or the block isn't in reference
6916 * counted tree.
6917 */
6918 if (found_key.objectid == found_key.offset ||
6919 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6920 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6921 ref_path->root_generation =
6922 btrfs_ref_generation(leaf, ref);
6923 if (level < 0) {
6924 /* special reference from the tree log */
6925 ref_path->nodes[0] = found_key.offset;
6926 ref_path->current_level = 0;
6927 }
6928 ret = 0;
6929 goto out;
6930 }
6931
6932 level++;
6933 BUG_ON(ref_path->nodes[level] != 0);
6934 ref_path->nodes[level] = found_key.offset;
6935 ref_path->current_level = level;
6936
6937 /*
6938 * the reference was created in the running transaction,
6939 * no need to continue walking up.
6940 */
6941 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6942 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6943 ref_path->root_generation =
6944 btrfs_ref_generation(leaf, ref);
6945 ret = 0;
6946 goto out;
6947 }
6948
6949 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006950 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006951 }
6952 /* reached max tree level, but no tree root found. */
6953 BUG();
6954out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006955 btrfs_free_path(path);
6956 return ret;
6957}
6958
6959static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6960 struct btrfs_root *extent_root,
6961 struct btrfs_ref_path *ref_path,
6962 u64 extent_start)
6963{
6964 memset(ref_path, 0, sizeof(*ref_path));
6965 ref_path->extent_start = extent_start;
6966
6967 return __next_ref_path(trans, extent_root, ref_path, 1);
6968}
6969
6970static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6971 struct btrfs_root *extent_root,
6972 struct btrfs_ref_path *ref_path)
6973{
6974 return __next_ref_path(trans, extent_root, ref_path, 0);
6975}
6976
Chris Masond3977122009-01-05 21:25:51 -05006977static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006978 struct btrfs_key *extent_key,
6979 u64 offset, int no_fragment,
6980 struct disk_extent **extents,
6981 int *nr_extents)
6982{
6983 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6984 struct btrfs_path *path;
6985 struct btrfs_file_extent_item *fi;
6986 struct extent_buffer *leaf;
6987 struct disk_extent *exts = *extents;
6988 struct btrfs_key found_key;
6989 u64 cur_pos;
6990 u64 last_byte;
6991 u32 nritems;
6992 int nr = 0;
6993 int max = *nr_extents;
6994 int ret;
6995
6996 WARN_ON(!no_fragment && *extents);
6997 if (!exts) {
6998 max = 1;
6999 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
7000 if (!exts)
7001 return -ENOMEM;
7002 }
7003
7004 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007005 if (!path) {
7006 if (exts != *extents)
7007 kfree(exts);
7008 return -ENOMEM;
7009 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007010
7011 cur_pos = extent_key->objectid - offset;
7012 last_byte = extent_key->objectid + extent_key->offset;
Li Zefan33345d012011-04-20 10:31:50 +08007013 ret = btrfs_lookup_file_extent(NULL, root, path,
7014 btrfs_ino(reloc_inode), cur_pos, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007015 if (ret < 0)
7016 goto out;
7017 if (ret > 0) {
7018 ret = -ENOENT;
7019 goto out;
7020 }
7021
7022 while (1) {
7023 leaf = path->nodes[0];
7024 nritems = btrfs_header_nritems(leaf);
7025 if (path->slots[0] >= nritems) {
7026 ret = btrfs_next_leaf(root, path);
7027 if (ret < 0)
7028 goto out;
7029 if (ret > 0)
7030 break;
7031 leaf = path->nodes[0];
7032 }
7033
7034 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7035 if (found_key.offset != cur_pos ||
7036 found_key.type != BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08007037 found_key.objectid != btrfs_ino(reloc_inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04007038 break;
7039
7040 fi = btrfs_item_ptr(leaf, path->slots[0],
7041 struct btrfs_file_extent_item);
7042 if (btrfs_file_extent_type(leaf, fi) !=
7043 BTRFS_FILE_EXTENT_REG ||
7044 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7045 break;
7046
7047 if (nr == max) {
7048 struct disk_extent *old = exts;
7049 max *= 2;
7050 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
Yoshinori Sanodac97e52011-02-15 12:01:42 +00007051 if (!exts) {
7052 ret = -ENOMEM;
7053 goto out;
7054 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007055 memcpy(exts, old, sizeof(*exts) * nr);
7056 if (old != *extents)
7057 kfree(old);
7058 }
7059
7060 exts[nr].disk_bytenr =
7061 btrfs_file_extent_disk_bytenr(leaf, fi);
7062 exts[nr].disk_num_bytes =
7063 btrfs_file_extent_disk_num_bytes(leaf, fi);
7064 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
7065 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04007066 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7067 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
7068 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
7069 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
7070 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04007071 BUG_ON(exts[nr].offset > 0);
7072 BUG_ON(exts[nr].compression || exts[nr].encryption);
7073 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007074
7075 cur_pos += exts[nr].num_bytes;
7076 nr++;
7077
7078 if (cur_pos + offset >= last_byte)
7079 break;
7080
7081 if (no_fragment) {
7082 ret = 1;
7083 goto out;
7084 }
7085 path->slots[0]++;
7086 }
7087
Yan Zheng1f80e4d2008-12-19 10:59:04 -05007088 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04007089 if (cur_pos + offset < last_byte) {
7090 ret = -ENOENT;
7091 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05007092 }
7093 ret = 0;
7094out:
Zheng Yan1a40e232008-09-26 10:09:34 -04007095 btrfs_free_path(path);
7096 if (ret) {
7097 if (exts != *extents)
7098 kfree(exts);
7099 } else {
7100 *extents = exts;
7101 *nr_extents = nr;
7102 }
7103 return ret;
7104}
7105
Chris Masond3977122009-01-05 21:25:51 -05007106static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007107 struct btrfs_root *root,
7108 struct btrfs_path *path,
7109 struct btrfs_key *extent_key,
7110 struct btrfs_key *leaf_key,
7111 struct btrfs_ref_path *ref_path,
7112 struct disk_extent *new_extents,
7113 int nr_extents)
7114{
7115 struct extent_buffer *leaf;
7116 struct btrfs_file_extent_item *fi;
7117 struct inode *inode = NULL;
7118 struct btrfs_key key;
7119 u64 lock_start = 0;
7120 u64 lock_end = 0;
7121 u64 num_bytes;
7122 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05007123 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007124 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007125 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007126 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04007127 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04007128 int ret;
7129
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007130 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04007131 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007132 if (key.objectid < ref_path->owner_objectid ||
7133 (key.objectid == ref_path->owner_objectid &&
7134 key.type < BTRFS_EXTENT_DATA_KEY)) {
7135 key.objectid = ref_path->owner_objectid;
7136 key.type = BTRFS_EXTENT_DATA_KEY;
7137 key.offset = 0;
7138 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007139 }
7140
7141 while (1) {
7142 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7143 if (ret < 0)
7144 goto out;
7145
7146 leaf = path->nodes[0];
7147 nritems = btrfs_header_nritems(leaf);
7148next:
7149 if (extent_locked && ret > 0) {
7150 /*
7151 * the file extent item was modified by someone
7152 * before the extent got locked.
7153 */
Zheng Yan1a40e232008-09-26 10:09:34 -04007154 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7155 lock_end, GFP_NOFS);
7156 extent_locked = 0;
7157 }
7158
7159 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007160 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04007161 break;
7162
7163 BUG_ON(extent_locked);
7164 ret = btrfs_next_leaf(root, path);
7165 if (ret < 0)
7166 goto out;
7167 if (ret > 0)
7168 break;
7169 leaf = path->nodes[0];
7170 nritems = btrfs_header_nritems(leaf);
7171 }
7172
7173 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7174
7175 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7176 if ((key.objectid > ref_path->owner_objectid) ||
7177 (key.objectid == ref_path->owner_objectid &&
7178 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05007179 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007180 break;
7181 }
7182
Li Zefan33345d012011-04-20 10:31:50 +08007183 if (inode && key.objectid != btrfs_ino(inode)) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007184 BUG_ON(extent_locked);
7185 btrfs_release_path(root, path);
7186 mutex_unlock(&inode->i_mutex);
7187 iput(inode);
7188 inode = NULL;
7189 continue;
7190 }
7191
7192 if (key.type != BTRFS_EXTENT_DATA_KEY) {
7193 path->slots[0]++;
7194 ret = 1;
7195 goto next;
7196 }
7197 fi = btrfs_item_ptr(leaf, path->slots[0],
7198 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04007199 extent_type = btrfs_file_extent_type(leaf, fi);
7200 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
7201 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04007202 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
7203 extent_key->objectid)) {
7204 path->slots[0]++;
7205 ret = 1;
7206 goto next;
7207 }
7208
7209 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7210 ext_offset = btrfs_file_extent_offset(leaf, fi);
7211
Yan Zheng86288a12009-01-21 10:49:16 -05007212 if (search_end == (u64)-1) {
7213 search_end = key.offset - ext_offset +
7214 btrfs_file_extent_ram_bytes(leaf, fi);
7215 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007216
7217 if (!extent_locked) {
7218 lock_start = key.offset;
7219 lock_end = lock_start + num_bytes - 1;
7220 } else {
Yan Zheng66435582008-10-30 14:19:50 -04007221 if (lock_start > key.offset ||
7222 lock_end + 1 < key.offset + num_bytes) {
7223 unlock_extent(&BTRFS_I(inode)->io_tree,
7224 lock_start, lock_end, GFP_NOFS);
7225 extent_locked = 0;
7226 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007227 }
7228
7229 if (!inode) {
7230 btrfs_release_path(root, path);
7231
7232 inode = btrfs_iget_locked(root->fs_info->sb,
7233 key.objectid, root);
7234 if (inode->i_state & I_NEW) {
7235 BTRFS_I(inode)->root = root;
7236 BTRFS_I(inode)->location.objectid =
7237 key.objectid;
7238 BTRFS_I(inode)->location.type =
7239 BTRFS_INODE_ITEM_KEY;
7240 BTRFS_I(inode)->location.offset = 0;
7241 btrfs_read_locked_inode(inode);
7242 unlock_new_inode(inode);
7243 }
7244 /*
7245 * some code call btrfs_commit_transaction while
7246 * holding the i_mutex, so we can't use mutex_lock
7247 * here.
7248 */
7249 if (is_bad_inode(inode) ||
7250 !mutex_trylock(&inode->i_mutex)) {
7251 iput(inode);
7252 inode = NULL;
7253 key.offset = (u64)-1;
7254 goto skip;
7255 }
7256 }
7257
7258 if (!extent_locked) {
7259 struct btrfs_ordered_extent *ordered;
7260
7261 btrfs_release_path(root, path);
7262
7263 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7264 lock_end, GFP_NOFS);
7265 ordered = btrfs_lookup_first_ordered_extent(inode,
7266 lock_end);
7267 if (ordered &&
7268 ordered->file_offset <= lock_end &&
7269 ordered->file_offset + ordered->len > lock_start) {
7270 unlock_extent(&BTRFS_I(inode)->io_tree,
7271 lock_start, lock_end, GFP_NOFS);
7272 btrfs_start_ordered_extent(inode, ordered, 1);
7273 btrfs_put_ordered_extent(ordered);
7274 key.offset += num_bytes;
7275 goto skip;
7276 }
7277 if (ordered)
7278 btrfs_put_ordered_extent(ordered);
7279
Zheng Yan1a40e232008-09-26 10:09:34 -04007280 extent_locked = 1;
7281 continue;
7282 }
7283
7284 if (nr_extents == 1) {
7285 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04007286 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7287 new_extents[0].disk_bytenr);
7288 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7289 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007290 btrfs_mark_buffer_dirty(leaf);
7291
7292 btrfs_drop_extent_cache(inode, key.offset,
7293 key.offset + num_bytes - 1, 0);
7294
7295 ret = btrfs_inc_extent_ref(trans, root,
7296 new_extents[0].disk_bytenr,
7297 new_extents[0].disk_num_bytes,
7298 leaf->start,
7299 root->root_key.objectid,
7300 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007301 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007302 BUG_ON(ret);
7303
7304 ret = btrfs_free_extent(trans, root,
7305 extent_key->objectid,
7306 extent_key->offset,
7307 leaf->start,
7308 btrfs_header_owner(leaf),
7309 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007310 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007311 BUG_ON(ret);
7312
7313 btrfs_release_path(root, path);
7314 key.offset += num_bytes;
7315 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04007316 BUG_ON(1);
7317#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04007318 u64 alloc_hint;
7319 u64 extent_len;
7320 int i;
7321 /*
7322 * drop old extent pointer at first, then insert the
7323 * new pointers one bye one
7324 */
7325 btrfs_release_path(root, path);
7326 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7327 key.offset + num_bytes,
7328 key.offset, &alloc_hint);
7329 BUG_ON(ret);
7330
7331 for (i = 0; i < nr_extents; i++) {
7332 if (ext_offset >= new_extents[i].num_bytes) {
7333 ext_offset -= new_extents[i].num_bytes;
7334 continue;
7335 }
7336 extent_len = min(new_extents[i].num_bytes -
7337 ext_offset, num_bytes);
7338
7339 ret = btrfs_insert_empty_item(trans, root,
7340 path, &key,
7341 sizeof(*fi));
7342 BUG_ON(ret);
7343
7344 leaf = path->nodes[0];
7345 fi = btrfs_item_ptr(leaf, path->slots[0],
7346 struct btrfs_file_extent_item);
7347 btrfs_set_file_extent_generation(leaf, fi,
7348 trans->transid);
7349 btrfs_set_file_extent_type(leaf, fi,
7350 BTRFS_FILE_EXTENT_REG);
7351 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7352 new_extents[i].disk_bytenr);
7353 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7354 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04007355 btrfs_set_file_extent_ram_bytes(leaf, fi,
7356 new_extents[i].ram_bytes);
7357
7358 btrfs_set_file_extent_compression(leaf, fi,
7359 new_extents[i].compression);
7360 btrfs_set_file_extent_encryption(leaf, fi,
7361 new_extents[i].encryption);
7362 btrfs_set_file_extent_other_encoding(leaf, fi,
7363 new_extents[i].other_encoding);
7364
Zheng Yan1a40e232008-09-26 10:09:34 -04007365 btrfs_set_file_extent_num_bytes(leaf, fi,
7366 extent_len);
7367 ext_offset += new_extents[i].offset;
7368 btrfs_set_file_extent_offset(leaf, fi,
7369 ext_offset);
7370 btrfs_mark_buffer_dirty(leaf);
7371
7372 btrfs_drop_extent_cache(inode, key.offset,
7373 key.offset + extent_len - 1, 0);
7374
7375 ret = btrfs_inc_extent_ref(trans, root,
7376 new_extents[i].disk_bytenr,
7377 new_extents[i].disk_num_bytes,
7378 leaf->start,
7379 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007380 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007381 BUG_ON(ret);
7382 btrfs_release_path(root, path);
7383
Yan Zhenga76a3cd2008-10-09 11:46:29 -04007384 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04007385
7386 ext_offset = 0;
7387 num_bytes -= extent_len;
7388 key.offset += extent_len;
7389
7390 if (num_bytes == 0)
7391 break;
7392 }
7393 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007394#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04007395 }
7396
7397 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007398 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7399 lock_end, GFP_NOFS);
7400 extent_locked = 0;
7401 }
7402skip:
7403 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05007404 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007405 break;
7406
7407 cond_resched();
7408 }
7409 ret = 0;
7410out:
7411 btrfs_release_path(root, path);
7412 if (inode) {
7413 mutex_unlock(&inode->i_mutex);
7414 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007415 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7416 lock_end, GFP_NOFS);
7417 }
7418 iput(inode);
7419 }
7420 return ret;
7421}
7422
Zheng Yan1a40e232008-09-26 10:09:34 -04007423int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7424 struct btrfs_root *root,
7425 struct extent_buffer *buf, u64 orig_start)
7426{
7427 int level;
7428 int ret;
7429
7430 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7431 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7432
7433 level = btrfs_header_level(buf);
7434 if (level == 0) {
7435 struct btrfs_leaf_ref *ref;
7436 struct btrfs_leaf_ref *orig_ref;
7437
7438 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7439 if (!orig_ref)
7440 return -ENOENT;
7441
7442 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7443 if (!ref) {
7444 btrfs_free_leaf_ref(root, orig_ref);
7445 return -ENOMEM;
7446 }
7447
7448 ref->nritems = orig_ref->nritems;
7449 memcpy(ref->extents, orig_ref->extents,
7450 sizeof(ref->extents[0]) * ref->nritems);
7451
7452 btrfs_free_leaf_ref(root, orig_ref);
7453
7454 ref->root_gen = trans->transid;
7455 ref->bytenr = buf->start;
7456 ref->owner = btrfs_header_owner(buf);
7457 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05007458
Zheng Yan1a40e232008-09-26 10:09:34 -04007459 ret = btrfs_add_leaf_ref(root, ref, 0);
7460 WARN_ON(ret);
7461 btrfs_free_leaf_ref(root, ref);
7462 }
7463 return 0;
7464}
7465
Chris Masond3977122009-01-05 21:25:51 -05007466static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007467 struct extent_buffer *leaf,
7468 struct btrfs_block_group_cache *group,
7469 struct btrfs_root *target_root)
7470{
7471 struct btrfs_key key;
7472 struct inode *inode = NULL;
7473 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007474 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04007475 u64 num_bytes;
7476 u64 skip_objectid = 0;
7477 u32 nritems;
7478 u32 i;
7479
7480 nritems = btrfs_header_nritems(leaf);
7481 for (i = 0; i < nritems; i++) {
7482 btrfs_item_key_to_cpu(leaf, &key, i);
7483 if (key.objectid == skip_objectid ||
7484 key.type != BTRFS_EXTENT_DATA_KEY)
7485 continue;
7486 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7487 if (btrfs_file_extent_type(leaf, fi) ==
7488 BTRFS_FILE_EXTENT_INLINE)
7489 continue;
7490 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7491 continue;
Li Zefan33345d012011-04-20 10:31:50 +08007492 if (!inode || btrfs_ino(inode) != key.objectid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007493 iput(inode);
7494 inode = btrfs_ilookup(target_root->fs_info->sb,
7495 key.objectid, target_root, 1);
7496 }
7497 if (!inode) {
7498 skip_objectid = key.objectid;
7499 continue;
7500 }
7501 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7502
Josef Bacik2ac55d42010-02-03 19:33:23 +00007503 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7504 key.offset + num_bytes - 1, 0, &cached_state,
7505 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007506 btrfs_drop_extent_cache(inode, key.offset,
7507 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007508 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7509 key.offset + num_bytes - 1, &cached_state,
7510 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007511 cond_resched();
7512 }
7513 iput(inode);
7514 return 0;
7515}
7516
Chris Masond3977122009-01-05 21:25:51 -05007517static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007518 struct btrfs_root *root,
7519 struct extent_buffer *leaf,
7520 struct btrfs_block_group_cache *group,
7521 struct inode *reloc_inode)
7522{
7523 struct btrfs_key key;
7524 struct btrfs_key extent_key;
7525 struct btrfs_file_extent_item *fi;
7526 struct btrfs_leaf_ref *ref;
7527 struct disk_extent *new_extent;
7528 u64 bytenr;
7529 u64 num_bytes;
7530 u32 nritems;
7531 u32 i;
7532 int ext_index;
7533 int nr_extent;
7534 int ret;
7535
7536 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007537 if (!new_extent)
7538 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007539
7540 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7541 BUG_ON(!ref);
7542
7543 ext_index = -1;
7544 nritems = btrfs_header_nritems(leaf);
7545 for (i = 0; i < nritems; i++) {
7546 btrfs_item_key_to_cpu(leaf, &key, i);
7547 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7548 continue;
7549 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7550 if (btrfs_file_extent_type(leaf, fi) ==
7551 BTRFS_FILE_EXTENT_INLINE)
7552 continue;
7553 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7554 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7555 if (bytenr == 0)
7556 continue;
7557
7558 ext_index++;
7559 if (bytenr >= group->key.objectid + group->key.offset ||
7560 bytenr + num_bytes <= group->key.objectid)
7561 continue;
7562
7563 extent_key.objectid = bytenr;
7564 extent_key.offset = num_bytes;
7565 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7566 nr_extent = 1;
7567 ret = get_new_locations(reloc_inode, &extent_key,
7568 group->key.objectid, 1,
7569 &new_extent, &nr_extent);
7570 if (ret > 0)
7571 continue;
7572 BUG_ON(ret < 0);
7573
7574 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7575 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7576 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7577 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7578
Zheng Yan1a40e232008-09-26 10:09:34 -04007579 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7580 new_extent->disk_bytenr);
7581 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7582 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007583 btrfs_mark_buffer_dirty(leaf);
7584
7585 ret = btrfs_inc_extent_ref(trans, root,
7586 new_extent->disk_bytenr,
7587 new_extent->disk_num_bytes,
7588 leaf->start,
7589 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007590 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007591 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04007592
Zheng Yan1a40e232008-09-26 10:09:34 -04007593 ret = btrfs_free_extent(trans, root,
7594 bytenr, num_bytes, leaf->start,
7595 btrfs_header_owner(leaf),
7596 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007597 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007598 BUG_ON(ret);
7599 cond_resched();
7600 }
7601 kfree(new_extent);
7602 BUG_ON(ext_index + 1 != ref->nritems);
7603 btrfs_free_leaf_ref(root, ref);
7604 return 0;
7605}
7606
Yan Zhengf82d02d2008-10-29 14:49:05 -04007607int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7608 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04007609{
7610 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007611 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007612
7613 if (root->reloc_root) {
7614 reloc_root = root->reloc_root;
7615 root->reloc_root = NULL;
7616 list_add(&reloc_root->dead_list,
7617 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007618
7619 btrfs_set_root_bytenr(&reloc_root->root_item,
7620 reloc_root->node->start);
7621 btrfs_set_root_level(&root->root_item,
7622 btrfs_header_level(reloc_root->node));
7623 memset(&reloc_root->root_item.drop_progress, 0,
7624 sizeof(struct btrfs_disk_key));
7625 reloc_root->root_item.drop_level = 0;
7626
7627 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7628 &reloc_root->root_key,
7629 &reloc_root->root_item);
7630 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007631 }
7632 return 0;
7633}
7634
7635int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7636{
7637 struct btrfs_trans_handle *trans;
7638 struct btrfs_root *reloc_root;
7639 struct btrfs_root *prev_root = NULL;
7640 struct list_head dead_roots;
7641 int ret;
7642 unsigned long nr;
7643
7644 INIT_LIST_HEAD(&dead_roots);
7645 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7646
7647 while (!list_empty(&dead_roots)) {
7648 reloc_root = list_entry(dead_roots.prev,
7649 struct btrfs_root, dead_list);
7650 list_del_init(&reloc_root->dead_list);
7651
7652 BUG_ON(reloc_root->commit_root != NULL);
7653 while (1) {
7654 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007655 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007656
7657 mutex_lock(&root->fs_info->drop_mutex);
7658 ret = btrfs_drop_snapshot(trans, reloc_root);
7659 if (ret != -EAGAIN)
7660 break;
7661 mutex_unlock(&root->fs_info->drop_mutex);
7662
7663 nr = trans->blocks_used;
7664 ret = btrfs_end_transaction(trans, root);
7665 BUG_ON(ret);
7666 btrfs_btree_balance_dirty(root, nr);
7667 }
7668
7669 free_extent_buffer(reloc_root->node);
7670
7671 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7672 &reloc_root->root_key);
7673 BUG_ON(ret);
7674 mutex_unlock(&root->fs_info->drop_mutex);
7675
7676 nr = trans->blocks_used;
7677 ret = btrfs_end_transaction(trans, root);
7678 BUG_ON(ret);
7679 btrfs_btree_balance_dirty(root, nr);
7680
7681 kfree(prev_root);
7682 prev_root = reloc_root;
7683 }
7684 if (prev_root) {
7685 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7686 kfree(prev_root);
7687 }
7688 return 0;
7689}
7690
7691int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7692{
7693 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7694 return 0;
7695}
7696
7697int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7698{
7699 struct btrfs_root *reloc_root;
7700 struct btrfs_trans_handle *trans;
7701 struct btrfs_key location;
7702 int found;
7703 int ret;
7704
7705 mutex_lock(&root->fs_info->tree_reloc_mutex);
7706 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7707 BUG_ON(ret);
7708 found = !list_empty(&root->fs_info->dead_reloc_roots);
7709 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7710
7711 if (found) {
7712 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007713 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007714 ret = btrfs_commit_transaction(trans, root);
7715 BUG_ON(ret);
7716 }
7717
7718 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7719 location.offset = (u64)-1;
7720 location.type = BTRFS_ROOT_ITEM_KEY;
7721
7722 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7723 BUG_ON(!reloc_root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05007724 ret = btrfs_orphan_cleanup(reloc_root);
7725 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007726 return 0;
7727}
7728
Chris Masond3977122009-01-05 21:25:51 -05007729static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007730 struct btrfs_root *root)
7731{
7732 struct btrfs_root *reloc_root;
7733 struct extent_buffer *eb;
7734 struct btrfs_root_item *root_item;
7735 struct btrfs_key root_key;
7736 int ret;
7737
7738 BUG_ON(!root->ref_cows);
7739 if (root->reloc_root)
7740 return 0;
7741
7742 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007743 if (!root_item)
7744 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007745
7746 ret = btrfs_copy_root(trans, root, root->commit_root,
7747 &eb, BTRFS_TREE_RELOC_OBJECTID);
7748 BUG_ON(ret);
7749
7750 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7751 root_key.offset = root->root_key.objectid;
7752 root_key.type = BTRFS_ROOT_ITEM_KEY;
7753
7754 memcpy(root_item, &root->root_item, sizeof(root_item));
7755 btrfs_set_root_refs(root_item, 0);
7756 btrfs_set_root_bytenr(root_item, eb->start);
7757 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04007758 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007759
7760 btrfs_tree_unlock(eb);
7761 free_extent_buffer(eb);
7762
7763 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7764 &root_key, root_item);
7765 BUG_ON(ret);
7766 kfree(root_item);
7767
7768 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7769 &root_key);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007770 BUG_ON(IS_ERR(reloc_root));
Zheng Yan1a40e232008-09-26 10:09:34 -04007771 reloc_root->last_trans = trans->transid;
7772 reloc_root->commit_root = NULL;
7773 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7774
7775 root->reloc_root = reloc_root;
7776 return 0;
7777}
7778
7779/*
7780 * Core function of space balance.
7781 *
7782 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04007783 * counted roots. There is one reloc tree for each subvol, and all
7784 * reloc trees share same root key objectid. Reloc trees are snapshots
7785 * of the latest committed roots of subvols (root->commit_root).
7786 *
7787 * To relocate a tree block referenced by a subvol, there are two steps.
7788 * COW the block through subvol's reloc tree, then update block pointer
7789 * in the subvol to point to the new block. Since all reloc trees share
7790 * same root key objectid, doing special handing for tree blocks owned
7791 * by them is easy. Once a tree block has been COWed in one reloc tree,
7792 * we can use the resulting new block directly when the same block is
7793 * required to COW again through other reloc trees. By this way, relocated
7794 * tree blocks are shared between reloc trees, so they are also shared
7795 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04007796 */
Chris Masond3977122009-01-05 21:25:51 -05007797static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007798 struct btrfs_root *root,
7799 struct btrfs_path *path,
7800 struct btrfs_key *first_key,
7801 struct btrfs_ref_path *ref_path,
7802 struct btrfs_block_group_cache *group,
7803 struct inode *reloc_inode)
7804{
7805 struct btrfs_root *reloc_root;
7806 struct extent_buffer *eb = NULL;
7807 struct btrfs_key *keys;
7808 u64 *nodes;
7809 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007810 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04007811 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007812 int ret;
7813
7814 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7815 lowest_level = ref_path->owner_objectid;
7816
Yan Zhengf82d02d2008-10-29 14:49:05 -04007817 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007818 path->lowest_level = lowest_level;
7819 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7820 BUG_ON(ret < 0);
7821 path->lowest_level = 0;
7822 btrfs_release_path(root, path);
7823 return 0;
7824 }
7825
Zheng Yan1a40e232008-09-26 10:09:34 -04007826 mutex_lock(&root->fs_info->tree_reloc_mutex);
7827 ret = init_reloc_tree(trans, root);
7828 BUG_ON(ret);
7829 reloc_root = root->reloc_root;
7830
Yan Zhengf82d02d2008-10-29 14:49:05 -04007831 shared_level = ref_path->shared_level;
7832 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007833
Yan Zhengf82d02d2008-10-29 14:49:05 -04007834 keys = ref_path->node_keys;
7835 nodes = ref_path->new_nodes;
7836 memset(&keys[shared_level + 1], 0,
7837 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7838 memset(&nodes[shared_level + 1], 0,
7839 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04007840
Yan Zhengf82d02d2008-10-29 14:49:05 -04007841 if (nodes[lowest_level] == 0) {
7842 path->lowest_level = lowest_level;
7843 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7844 0, 1);
7845 BUG_ON(ret);
7846 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7847 eb = path->nodes[level];
7848 if (!eb || eb == reloc_root->node)
7849 break;
7850 nodes[level] = eb->start;
7851 if (level == 0)
7852 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7853 else
7854 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7855 }
Yan Zheng2b820322008-11-17 21:11:30 -05007856 if (nodes[0] &&
7857 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007858 eb = path->nodes[0];
7859 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7860 group, reloc_inode);
7861 BUG_ON(ret);
7862 }
7863 btrfs_release_path(reloc_root, path);
7864 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007865 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007866 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007867 BUG_ON(ret);
7868 }
7869
Zheng Yan1a40e232008-09-26 10:09:34 -04007870 /*
7871 * replace tree blocks in the fs tree with tree blocks in
7872 * the reloc tree.
7873 */
7874 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7875 BUG_ON(ret < 0);
7876
7877 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007878 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7879 0, 0);
7880 BUG_ON(ret);
7881 extent_buffer_get(path->nodes[0]);
7882 eb = path->nodes[0];
7883 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007884 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7885 BUG_ON(ret);
7886 free_extent_buffer(eb);
7887 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007888
Yan Zhengf82d02d2008-10-29 14:49:05 -04007889 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007890 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007891 return 0;
7892}
7893
Chris Masond3977122009-01-05 21:25:51 -05007894static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007895 struct btrfs_root *root,
7896 struct btrfs_path *path,
7897 struct btrfs_key *first_key,
7898 struct btrfs_ref_path *ref_path)
7899{
7900 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007901
7902 ret = relocate_one_path(trans, root, path, first_key,
7903 ref_path, NULL, NULL);
7904 BUG_ON(ret);
7905
Zheng Yan1a40e232008-09-26 10:09:34 -04007906 return 0;
7907}
7908
Chris Masond3977122009-01-05 21:25:51 -05007909static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007910 struct btrfs_root *extent_root,
7911 struct btrfs_path *path,
7912 struct btrfs_key *extent_key)
7913{
7914 int ret;
7915
Zheng Yan1a40e232008-09-26 10:09:34 -04007916 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7917 if (ret)
7918 goto out;
7919 ret = btrfs_del_item(trans, extent_root, path);
7920out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007921 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007922 return ret;
7923}
7924
Chris Masond3977122009-01-05 21:25:51 -05007925static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007926 struct btrfs_ref_path *ref_path)
7927{
7928 struct btrfs_key root_key;
7929
7930 root_key.objectid = ref_path->root_objectid;
7931 root_key.type = BTRFS_ROOT_ITEM_KEY;
7932 if (is_cowonly_root(ref_path->root_objectid))
7933 root_key.offset = 0;
7934 else
7935 root_key.offset = (u64)-1;
7936
7937 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7938}
7939
Chris Masond3977122009-01-05 21:25:51 -05007940static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007941 struct btrfs_path *path,
7942 struct btrfs_key *extent_key,
7943 struct btrfs_block_group_cache *group,
7944 struct inode *reloc_inode, int pass)
7945{
7946 struct btrfs_trans_handle *trans;
7947 struct btrfs_root *found_root;
7948 struct btrfs_ref_path *ref_path = NULL;
7949 struct disk_extent *new_extents = NULL;
7950 int nr_extents = 0;
7951 int loops;
7952 int ret;
7953 int level;
7954 struct btrfs_key first_key;
7955 u64 prev_block = 0;
7956
Zheng Yan1a40e232008-09-26 10:09:34 -04007957
7958 trans = btrfs_start_transaction(extent_root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007959 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007960
7961 if (extent_key->objectid == 0) {
7962 ret = del_extent_zero(trans, extent_root, path, extent_key);
7963 goto out;
7964 }
7965
7966 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7967 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007968 ret = -ENOMEM;
7969 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007970 }
7971
7972 for (loops = 0; ; loops++) {
7973 if (loops == 0) {
7974 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7975 extent_key->objectid);
7976 } else {
7977 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7978 }
7979 if (ret < 0)
7980 goto out;
7981 if (ret > 0)
7982 break;
7983
7984 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7985 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7986 continue;
7987
7988 found_root = read_ref_root(extent_root->fs_info, ref_path);
7989 BUG_ON(!found_root);
7990 /*
7991 * for reference counted tree, only process reference paths
7992 * rooted at the latest committed root.
7993 */
7994 if (found_root->ref_cows &&
7995 ref_path->root_generation != found_root->root_key.offset)
7996 continue;
7997
7998 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7999 if (pass == 0) {
8000 /*
8001 * copy data extents to new locations
8002 */
8003 u64 group_start = group->key.objectid;
8004 ret = relocate_data_extent(reloc_inode,
8005 extent_key,
8006 group_start);
8007 if (ret < 0)
8008 goto out;
8009 break;
8010 }
8011 level = 0;
8012 } else {
8013 level = ref_path->owner_objectid;
8014 }
8015
8016 if (prev_block != ref_path->nodes[level]) {
8017 struct extent_buffer *eb;
8018 u64 block_start = ref_path->nodes[level];
8019 u64 block_size = btrfs_level_size(found_root, level);
8020
8021 eb = read_tree_block(found_root, block_start,
8022 block_size, 0);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008023 if (!eb) {
8024 ret = -EIO;
8025 goto out;
8026 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008027 btrfs_tree_lock(eb);
8028 BUG_ON(level != btrfs_header_level(eb));
8029
8030 if (level == 0)
8031 btrfs_item_key_to_cpu(eb, &first_key, 0);
8032 else
8033 btrfs_node_key_to_cpu(eb, &first_key, 0);
8034
8035 btrfs_tree_unlock(eb);
8036 free_extent_buffer(eb);
8037 prev_block = block_start;
8038 }
8039
Yan Zheng24562422009-02-12 14:14:53 -05008040 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05008041 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05008042 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05008043 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8044 /*
8045 * try to update data extent references while
8046 * keeping metadata shared between snapshots.
8047 */
8048 if (pass == 1) {
8049 ret = relocate_one_path(trans, found_root,
8050 path, &first_key, ref_path,
8051 group, reloc_inode);
8052 if (ret < 0)
8053 goto out;
8054 continue;
8055 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008056 /*
8057 * use fallback method to process the remaining
8058 * references.
8059 */
8060 if (!new_extents) {
8061 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04008062 new_extents = kmalloc(sizeof(*new_extents),
8063 GFP_NOFS);
8064 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008065 ret = get_new_locations(reloc_inode,
8066 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04008067 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04008068 &new_extents,
8069 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04008070 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04008071 goto out;
8072 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008073 ret = replace_one_extent(trans, found_root,
8074 path, extent_key,
8075 &first_key, ref_path,
8076 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05008077 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04008078 ret = relocate_tree_block(trans, found_root, path,
8079 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04008080 }
8081 if (ret < 0)
8082 goto out;
8083 }
8084 ret = 0;
8085out:
8086 btrfs_end_transaction(trans, extent_root);
8087 kfree(new_extents);
8088 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05008089 return ret;
8090}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008091#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05008092
Chris Masonec44a352008-04-28 15:29:52 -04008093static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8094{
8095 u64 num_devices;
8096 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
8097 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8098
Chris Masoncd02dca2010-12-13 14:56:23 -05008099 /*
8100 * we add in the count of missing devices because we want
8101 * to make sure that any RAID levels on a degraded FS
8102 * continue to be honored.
8103 */
8104 num_devices = root->fs_info->fs_devices->rw_devices +
8105 root->fs_info->fs_devices->missing_devices;
8106
Chris Masonec44a352008-04-28 15:29:52 -04008107 if (num_devices == 1) {
8108 stripped |= BTRFS_BLOCK_GROUP_DUP;
8109 stripped = flags & ~stripped;
8110
8111 /* turn raid0 into single device chunks */
8112 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8113 return stripped;
8114
8115 /* turn mirroring into duplication */
8116 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8117 BTRFS_BLOCK_GROUP_RAID10))
8118 return stripped | BTRFS_BLOCK_GROUP_DUP;
8119 return flags;
8120 } else {
8121 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04008122 if (flags & stripped)
8123 return flags;
8124
8125 stripped |= BTRFS_BLOCK_GROUP_DUP;
8126 stripped = flags & ~stripped;
8127
8128 /* switch duplicated blocks with raid1 */
8129 if (flags & BTRFS_BLOCK_GROUP_DUP)
8130 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8131
8132 /* turn single device chunks into raid0 */
8133 return stripped | BTRFS_BLOCK_GROUP_RAID0;
8134 }
8135 return flags;
8136}
8137
Yan, Zhengf0486c62010-05-16 10:46:25 -04008138static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04008139{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008140 struct btrfs_space_info *sinfo = cache->space_info;
8141 u64 num_bytes;
8142 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04008143
Yan, Zhengf0486c62010-05-16 10:46:25 -04008144 if (cache->ro)
8145 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04008146
Yan, Zhengf0486c62010-05-16 10:46:25 -04008147 spin_lock(&sinfo->lock);
8148 spin_lock(&cache->lock);
8149 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8150 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04008151
Yan, Zhengf0486c62010-05-16 10:46:25 -04008152 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
8153 sinfo->bytes_may_use + sinfo->bytes_readonly +
Chris Mason65e53412010-12-24 06:41:52 -05008154 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04008155 sinfo->bytes_readonly += num_bytes;
8156 sinfo->bytes_reserved += cache->reserved_pinned;
8157 cache->reserved_pinned = 0;
8158 cache->ro = 1;
8159 ret = 0;
8160 }
Chris Mason65e53412010-12-24 06:41:52 -05008161
Yan, Zhengf0486c62010-05-16 10:46:25 -04008162 spin_unlock(&cache->lock);
8163 spin_unlock(&sinfo->lock);
8164 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04008165}
8166
Yan, Zhengf0486c62010-05-16 10:46:25 -04008167int btrfs_set_block_group_ro(struct btrfs_root *root,
8168 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008169
8170{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008171 struct btrfs_trans_handle *trans;
8172 u64 alloc_flags;
8173 int ret;
8174
8175 BUG_ON(cache->ro);
8176
8177 trans = btrfs_join_transaction(root, 1);
8178 BUG_ON(IS_ERR(trans));
8179
8180 alloc_flags = update_block_group_flags(root, cache->flags);
8181 if (alloc_flags != cache->flags)
Chris Mason0e4f8f82011-04-15 16:05:44 -04008182 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8183 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008184
8185 ret = set_block_group_ro(cache);
8186 if (!ret)
8187 goto out;
8188 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Chris Mason0e4f8f82011-04-15 16:05:44 -04008189 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8190 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008191 if (ret < 0)
8192 goto out;
8193 ret = set_block_group_ro(cache);
8194out:
8195 btrfs_end_transaction(trans, root);
8196 return ret;
8197}
8198
Chris Masonc87f08c2011-02-16 13:57:04 -05008199int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8200 struct btrfs_root *root, u64 type)
8201{
8202 u64 alloc_flags = get_alloc_profile(root, type);
Chris Mason0e4f8f82011-04-15 16:05:44 -04008203 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8204 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05008205}
8206
Miao Xie6d07bce2011-01-05 10:07:31 +00008207/*
8208 * helper to account the unused space of all the readonly block group in the
8209 * list. takes mirrors into account.
8210 */
8211static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8212{
8213 struct btrfs_block_group_cache *block_group;
8214 u64 free_bytes = 0;
8215 int factor;
8216
8217 list_for_each_entry(block_group, groups_list, list) {
8218 spin_lock(&block_group->lock);
8219
8220 if (!block_group->ro) {
8221 spin_unlock(&block_group->lock);
8222 continue;
8223 }
8224
8225 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8226 BTRFS_BLOCK_GROUP_RAID10 |
8227 BTRFS_BLOCK_GROUP_DUP))
8228 factor = 2;
8229 else
8230 factor = 1;
8231
8232 free_bytes += (block_group->key.offset -
8233 btrfs_block_group_used(&block_group->item)) *
8234 factor;
8235
8236 spin_unlock(&block_group->lock);
8237 }
8238
8239 return free_bytes;
8240}
8241
8242/*
8243 * helper to account the unused space of all the readonly block group in the
8244 * space_info. takes mirrors into account.
8245 */
8246u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8247{
8248 int i;
8249 u64 free_bytes = 0;
8250
8251 spin_lock(&sinfo->lock);
8252
8253 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
8254 if (!list_empty(&sinfo->block_groups[i]))
8255 free_bytes += __btrfs_get_ro_block_group_free_space(
8256 &sinfo->block_groups[i]);
8257
8258 spin_unlock(&sinfo->lock);
8259
8260 return free_bytes;
8261}
8262
Yan, Zhengf0486c62010-05-16 10:46:25 -04008263int btrfs_set_block_group_rw(struct btrfs_root *root,
8264 struct btrfs_block_group_cache *cache)
8265{
8266 struct btrfs_space_info *sinfo = cache->space_info;
8267 u64 num_bytes;
8268
8269 BUG_ON(!cache->ro);
8270
8271 spin_lock(&sinfo->lock);
8272 spin_lock(&cache->lock);
8273 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8274 cache->bytes_super - btrfs_block_group_used(&cache->item);
8275 sinfo->bytes_readonly -= num_bytes;
8276 cache->ro = 0;
8277 spin_unlock(&cache->lock);
8278 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008279 return 0;
8280}
8281
Josef Bacikba1bf482009-09-11 16:11:19 -04008282/*
8283 * checks to see if its even possible to relocate this block group.
8284 *
8285 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8286 * ok to go ahead and try.
8287 */
8288int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008289{
Zheng Yan1a40e232008-09-26 10:09:34 -04008290 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008291 struct btrfs_space_info *space_info;
8292 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8293 struct btrfs_device *device;
8294 int full = 0;
8295 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008296
Josef Bacikba1bf482009-09-11 16:11:19 -04008297 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008298
Josef Bacikba1bf482009-09-11 16:11:19 -04008299 /* odd, couldn't find the block group, leave it alone */
8300 if (!block_group)
8301 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008302
Josef Bacikba1bf482009-09-11 16:11:19 -04008303 /* no bytes used, we're good */
8304 if (!btrfs_block_group_used(&block_group->item))
8305 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008306
Josef Bacikba1bf482009-09-11 16:11:19 -04008307 space_info = block_group->space_info;
8308 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008309
Josef Bacikba1bf482009-09-11 16:11:19 -04008310 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008311
Josef Bacikba1bf482009-09-11 16:11:19 -04008312 /*
8313 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008314 * relocate it unless we're able to allocate a new chunk below.
8315 *
8316 * Otherwise, we need to make sure we have room in the space to handle
8317 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008318 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008319 if ((space_info->total_bytes != block_group->key.offset) &&
8320 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04008321 space_info->bytes_pinned + space_info->bytes_readonly +
8322 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04008323 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008324 spin_unlock(&space_info->lock);
8325 goto out;
8326 }
8327 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008328
Josef Bacikba1bf482009-09-11 16:11:19 -04008329 /*
8330 * ok we don't have enough space, but maybe we have free space on our
8331 * devices to allocate new chunks for relocation, so loop through our
8332 * alloc devices and guess if we have enough space. However, if we
8333 * were marked as full, then we know there aren't enough chunks, and we
8334 * can just return.
8335 */
8336 ret = -1;
8337 if (full)
8338 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05008339
Josef Bacikba1bf482009-09-11 16:11:19 -04008340 mutex_lock(&root->fs_info->chunk_mutex);
8341 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8342 u64 min_free = btrfs_block_group_used(&block_group->item);
Miao Xie7bfc8372011-01-05 10:07:26 +00008343 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008344
Josef Bacikba1bf482009-09-11 16:11:19 -04008345 /*
8346 * check to make sure we can actually find a chunk with enough
8347 * space to fit our block group in.
8348 */
8349 if (device->total_bytes > device->bytes_used + min_free) {
8350 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008351 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008352 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05008353 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04008354 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008355 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008356 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008357 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05008358out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008359 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008360 return ret;
8361}
8362
Christoph Hellwigb2950862008-12-02 09:54:17 -05008363static int find_first_block_group(struct btrfs_root *root,
8364 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008365{
Chris Mason925baed2008-06-25 16:01:30 -04008366 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008367 struct btrfs_key found_key;
8368 struct extent_buffer *leaf;
8369 int slot;
8370
8371 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8372 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008373 goto out;
8374
Chris Masond3977122009-01-05 21:25:51 -05008375 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008376 slot = path->slots[0];
8377 leaf = path->nodes[0];
8378 if (slot >= btrfs_header_nritems(leaf)) {
8379 ret = btrfs_next_leaf(root, path);
8380 if (ret == 0)
8381 continue;
8382 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008383 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008384 break;
8385 }
8386 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8387
8388 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008389 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8390 ret = 0;
8391 goto out;
8392 }
Chris Mason0b86a832008-03-24 15:01:56 -04008393 path->slots[0]++;
8394 }
Chris Mason925baed2008-06-25 16:01:30 -04008395out:
Chris Mason0b86a832008-03-24 15:01:56 -04008396 return ret;
8397}
8398
Josef Bacik0af3d002010-06-21 14:48:16 -04008399void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8400{
8401 struct btrfs_block_group_cache *block_group;
8402 u64 last = 0;
8403
8404 while (1) {
8405 struct inode *inode;
8406
8407 block_group = btrfs_lookup_first_block_group(info, last);
8408 while (block_group) {
8409 spin_lock(&block_group->lock);
8410 if (block_group->iref)
8411 break;
8412 spin_unlock(&block_group->lock);
8413 block_group = next_block_group(info->tree_root,
8414 block_group);
8415 }
8416 if (!block_group) {
8417 if (last == 0)
8418 break;
8419 last = 0;
8420 continue;
8421 }
8422
8423 inode = block_group->inode;
8424 block_group->iref = 0;
8425 block_group->inode = NULL;
8426 spin_unlock(&block_group->lock);
8427 iput(inode);
8428 last = block_group->key.objectid + block_group->key.offset;
8429 btrfs_put_block_group(block_group);
8430 }
8431}
8432
Zheng Yan1a40e232008-09-26 10:09:34 -04008433int btrfs_free_block_groups(struct btrfs_fs_info *info)
8434{
8435 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008436 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008437 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008438 struct rb_node *n;
8439
Yan Zheng11833d62009-09-11 16:11:19 -04008440 down_write(&info->extent_commit_sem);
8441 while (!list_empty(&info->caching_block_groups)) {
8442 caching_ctl = list_entry(info->caching_block_groups.next,
8443 struct btrfs_caching_control, list);
8444 list_del(&caching_ctl->list);
8445 put_caching_control(caching_ctl);
8446 }
8447 up_write(&info->extent_commit_sem);
8448
Zheng Yan1a40e232008-09-26 10:09:34 -04008449 spin_lock(&info->block_group_cache_lock);
8450 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8451 block_group = rb_entry(n, struct btrfs_block_group_cache,
8452 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008453 rb_erase(&block_group->cache_node,
8454 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008455 spin_unlock(&info->block_group_cache_lock);
8456
Josef Bacik80eb2342008-10-29 14:49:05 -04008457 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008458 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008459 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008460
Josef Bacik817d52f2009-07-13 21:29:25 -04008461 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008462 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008463
Josef Bacik3c148742011-02-02 15:53:47 +00008464 /*
8465 * We haven't cached this block group, which means we could
8466 * possibly have excluded extents on this block group.
8467 */
8468 if (block_group->cached == BTRFS_CACHE_NO)
8469 free_excluded_extents(info->extent_root, block_group);
8470
Josef Bacik817d52f2009-07-13 21:29:25 -04008471 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008472 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008473
8474 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008475 }
8476 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008477
8478 /* now that all the block groups are freed, go through and
8479 * free all the space_info structs. This is only called during
8480 * the final stages of unmount, and so we know nobody is
8481 * using them. We call synchronize_rcu() once before we start,
8482 * just to be on the safe side.
8483 */
8484 synchronize_rcu();
8485
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008486 release_global_block_rsv(info);
8487
Chris Mason4184ea72009-03-10 12:39:20 -04008488 while(!list_empty(&info->space_info)) {
8489 space_info = list_entry(info->space_info.next,
8490 struct btrfs_space_info,
8491 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008492 if (space_info->bytes_pinned > 0 ||
8493 space_info->bytes_reserved > 0) {
8494 WARN_ON(1);
8495 dump_space_info(space_info, 0, 0);
8496 }
Chris Mason4184ea72009-03-10 12:39:20 -04008497 list_del(&space_info->list);
8498 kfree(space_info);
8499 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008500 return 0;
8501}
8502
Yan, Zhengb742bb82010-05-16 10:46:24 -04008503static void __link_block_group(struct btrfs_space_info *space_info,
8504 struct btrfs_block_group_cache *cache)
8505{
8506 int index = get_block_group_index(cache);
8507
8508 down_write(&space_info->groups_sem);
8509 list_add_tail(&cache->list, &space_info->block_groups[index]);
8510 up_write(&space_info->groups_sem);
8511}
8512
Chris Mason9078a3e2007-04-26 16:46:15 -04008513int btrfs_read_block_groups(struct btrfs_root *root)
8514{
8515 struct btrfs_path *path;
8516 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008517 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008518 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008519 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008520 struct btrfs_key key;
8521 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008522 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008523 int need_clear = 0;
8524 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008525
Chris Masonbe744172007-05-06 10:15:01 -04008526 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008527 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008528 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008529 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008530 path = btrfs_alloc_path();
8531 if (!path)
8532 return -ENOMEM;
8533
Josef Bacik0af3d002010-06-21 14:48:16 -04008534 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8535 if (cache_gen != 0 &&
8536 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8537 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008538 if (btrfs_test_opt(root, CLEAR_CACHE))
8539 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04008540 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
8541 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04008542
Chris Masond3977122009-01-05 21:25:51 -05008543 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008544 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008545 if (ret > 0)
8546 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008547 if (ret != 0)
8548 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008549 leaf = path->nodes[0];
8550 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008551 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008552 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008553 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008554 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008555 }
Li Zefan34d52cb2011-03-29 13:46:06 +08008556 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8557 GFP_NOFS);
8558 if (!cache->free_space_ctl) {
8559 kfree(cache);
8560 ret = -ENOMEM;
8561 goto error;
8562 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008563
Yan Zhengd2fb3432008-12-11 16:30:39 -05008564 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008565 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008566 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008567 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008568 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008569
Josef Bacik0af3d002010-06-21 14:48:16 -04008570 if (need_clear)
8571 cache->disk_cache_state = BTRFS_DC_CLEAR;
8572
Chris Mason5f39d392007-10-15 16:14:19 -04008573 read_extent_buffer(leaf, &cache->item,
8574 btrfs_item_ptr_offset(leaf, path->slots[0]),
8575 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008576 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008577
Chris Mason9078a3e2007-04-26 16:46:15 -04008578 key.objectid = found_key.objectid + found_key.offset;
8579 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04008580 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008581 cache->sectorsize = root->sectorsize;
8582
Li Zefan34d52cb2011-03-29 13:46:06 +08008583 btrfs_init_free_space_ctl(cache);
8584
Josef Bacik817d52f2009-07-13 21:29:25 -04008585 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008586 * We need to exclude the super stripes now so that the space
8587 * info has super bytes accounted for, otherwise we'll think
8588 * we have more space than we actually do.
8589 */
8590 exclude_super_stripes(root, cache);
8591
8592 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008593 * check for two cases, either we are full, and therefore
8594 * don't need to bother with the caching work since we won't
8595 * find any space, or we are empty, and we can just add all
8596 * the space in and be done with it. This saves us _alot_ of
8597 * time, particularly in the full case.
8598 */
8599 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008600 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008601 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008602 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008603 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008604 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008605 cache->cached = BTRFS_CACHE_FINISHED;
8606 add_new_free_space(cache, root->fs_info,
8607 found_key.objectid,
8608 found_key.objectid +
8609 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008610 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008611 }
Chris Mason96b51792007-10-15 16:15:19 -04008612
Chris Mason6324fbf2008-03-24 15:01:59 -04008613 ret = update_space_info(info, cache->flags, found_key.offset,
8614 btrfs_block_group_used(&cache->item),
8615 &space_info);
8616 BUG_ON(ret);
8617 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008618 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008619 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008620 spin_unlock(&cache->space_info->lock);
8621
Yan, Zhengb742bb82010-05-16 10:46:24 -04008622 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008623
Josef Bacik0f9dd462008-09-23 13:14:11 -04008624 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8625 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04008626
8627 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008628 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04008629 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04008630 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008631
8632 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8633 if (!(get_alloc_profile(root, space_info->flags) &
8634 (BTRFS_BLOCK_GROUP_RAID10 |
8635 BTRFS_BLOCK_GROUP_RAID1 |
8636 BTRFS_BLOCK_GROUP_DUP)))
8637 continue;
8638 /*
8639 * avoid allocating from un-mirrored block group if there are
8640 * mirrored block groups.
8641 */
8642 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008643 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008644 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008645 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008646 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008647
8648 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008649 ret = 0;
8650error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008651 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008652 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008653}
Chris Mason6324fbf2008-03-24 15:01:59 -04008654
8655int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8656 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008657 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008658 u64 size)
8659{
8660 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008661 struct btrfs_root *extent_root;
8662 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008663
8664 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008665
Chris Mason12fcfd22009-03-24 10:24:20 -04008666 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008667
Chris Mason8f18cf12008-04-25 16:53:30 -04008668 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008669 if (!cache)
8670 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08008671 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8672 GFP_NOFS);
8673 if (!cache->free_space_ctl) {
8674 kfree(cache);
8675 return -ENOMEM;
8676 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008677
Chris Masone17cade2008-04-15 15:41:47 -04008678 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008679 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008680 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008681 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008682 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04008683
Yan Zhengd2fb3432008-12-11 16:30:39 -05008684 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008685 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008686 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008687 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008688
Li Zefan34d52cb2011-03-29 13:46:06 +08008689 btrfs_init_free_space_ctl(cache);
8690
Chris Mason6324fbf2008-03-24 15:01:59 -04008691 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008692 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8693 cache->flags = type;
8694 btrfs_set_block_group_flags(&cache->item, type);
8695
Yan Zheng11833d62009-09-11 16:11:19 -04008696 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008697 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04008698 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04008699
Josef Bacik817d52f2009-07-13 21:29:25 -04008700 add_new_free_space(cache, root->fs_info, chunk_offset,
8701 chunk_offset + size);
8702
Yan Zheng11833d62009-09-11 16:11:19 -04008703 free_excluded_extents(root, cache);
8704
Chris Mason6324fbf2008-03-24 15:01:59 -04008705 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8706 &cache->space_info);
8707 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04008708
8709 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008710 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008711 spin_unlock(&cache->space_info->lock);
8712
Yan, Zhengb742bb82010-05-16 10:46:24 -04008713 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008714
Josef Bacik0f9dd462008-09-23 13:14:11 -04008715 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8716 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04008717
Chris Mason6324fbf2008-03-24 15:01:59 -04008718 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8719 sizeof(cache->item));
8720 BUG_ON(ret);
8721
Chris Masond18a2c42008-04-04 15:40:00 -04008722 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008723
Chris Mason6324fbf2008-03-24 15:01:59 -04008724 return 0;
8725}
Zheng Yan1a40e232008-09-26 10:09:34 -04008726
8727int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8728 struct btrfs_root *root, u64 group_start)
8729{
8730 struct btrfs_path *path;
8731 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008732 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008733 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008734 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008735 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008736 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04008737 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008738
Zheng Yan1a40e232008-09-26 10:09:34 -04008739 root = root->fs_info->extent_root;
8740
8741 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8742 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008743 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008744
liubo9f7c43c2011-03-07 02:13:33 +00008745 /*
8746 * Free the reserved super bytes from this block group before
8747 * remove it.
8748 */
8749 free_excluded_extents(root, block_group);
8750
Zheng Yan1a40e232008-09-26 10:09:34 -04008751 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04008752 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8753 BTRFS_BLOCK_GROUP_RAID1 |
8754 BTRFS_BLOCK_GROUP_RAID10))
8755 factor = 2;
8756 else
8757 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008758
Chris Mason44fb5512009-06-04 15:34:51 -04008759 /* make sure this block group isn't part of an allocation cluster */
8760 cluster = &root->fs_info->data_alloc_cluster;
8761 spin_lock(&cluster->refill_lock);
8762 btrfs_return_cluster_to_free_space(block_group, cluster);
8763 spin_unlock(&cluster->refill_lock);
8764
8765 /*
8766 * make sure this block group isn't part of a metadata
8767 * allocation cluster
8768 */
8769 cluster = &root->fs_info->meta_alloc_cluster;
8770 spin_lock(&cluster->refill_lock);
8771 btrfs_return_cluster_to_free_space(block_group, cluster);
8772 spin_unlock(&cluster->refill_lock);
8773
Zheng Yan1a40e232008-09-26 10:09:34 -04008774 path = btrfs_alloc_path();
8775 BUG_ON(!path);
8776
Josef Bacik0af3d002010-06-21 14:48:16 -04008777 inode = lookup_free_space_inode(root, block_group, path);
8778 if (!IS_ERR(inode)) {
8779 btrfs_orphan_add(trans, inode);
8780 clear_nlink(inode);
8781 /* One for the block groups ref */
8782 spin_lock(&block_group->lock);
8783 if (block_group->iref) {
8784 block_group->iref = 0;
8785 block_group->inode = NULL;
8786 spin_unlock(&block_group->lock);
8787 iput(inode);
8788 } else {
8789 spin_unlock(&block_group->lock);
8790 }
8791 /* One for our lookup ref */
8792 iput(inode);
8793 }
8794
8795 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8796 key.offset = block_group->key.objectid;
8797 key.type = 0;
8798
8799 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8800 if (ret < 0)
8801 goto out;
8802 if (ret > 0)
8803 btrfs_release_path(tree_root, path);
8804 if (ret == 0) {
8805 ret = btrfs_del_item(trans, tree_root, path);
8806 if (ret)
8807 goto out;
8808 btrfs_release_path(tree_root, path);
8809 }
8810
Yan Zheng3dfdb932009-01-21 10:49:16 -05008811 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008812 rb_erase(&block_group->cache_node,
8813 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008814 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008815
Josef Bacik80eb2342008-10-29 14:49:05 -04008816 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008817 /*
8818 * we must use list_del_init so people can check to see if they
8819 * are still on the list after taking the semaphore
8820 */
8821 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008822 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008823
Josef Bacik817d52f2009-07-13 21:29:25 -04008824 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008825 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008826
8827 btrfs_remove_free_space_cache(block_group);
8828
Yan Zhengc146afa2008-11-12 14:34:12 -05008829 spin_lock(&block_group->space_info->lock);
8830 block_group->space_info->total_bytes -= block_group->key.offset;
8831 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008832 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008833 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008834
Josef Bacik0af3d002010-06-21 14:48:16 -04008835 memcpy(&key, &block_group->key, sizeof(key));
8836
Chris Mason283bb192009-07-24 16:30:55 -04008837 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008838
Chris Masonfa9c0d792009-04-03 09:47:43 -04008839 btrfs_put_block_group(block_group);
8840 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008841
8842 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8843 if (ret > 0)
8844 ret = -EIO;
8845 if (ret < 0)
8846 goto out;
8847
8848 ret = btrfs_del_item(trans, root, path);
8849out:
8850 btrfs_free_path(path);
8851 return ret;
8852}
liuboacce9522011-01-06 19:30:25 +08008853
liuboc59021f2011-03-07 02:13:14 +00008854int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8855{
8856 struct btrfs_space_info *space_info;
8857 int ret;
8858
8859 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0,
8860 &space_info);
8861 if (ret)
8862 return ret;
8863
8864 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0,
8865 &space_info);
8866 if (ret)
8867 return ret;
8868
8869 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0,
8870 &space_info);
8871 if (ret)
8872 return ret;
8873
8874 return ret;
8875}
8876
liuboacce9522011-01-06 19:30:25 +08008877int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8878{
8879 return unpin_extent_range(root, start, end);
8880}
8881
8882int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008883 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008884{
Li Dongyang5378e602011-03-24 10:24:27 +00008885 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008886}
Li Dongyangf7039b12011-03-24 10:24:28 +00008887
8888int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8889{
8890 struct btrfs_fs_info *fs_info = root->fs_info;
8891 struct btrfs_block_group_cache *cache = NULL;
8892 u64 group_trimmed;
8893 u64 start;
8894 u64 end;
8895 u64 trimmed = 0;
8896 int ret = 0;
8897
8898 cache = btrfs_lookup_block_group(fs_info, range->start);
8899
8900 while (cache) {
8901 if (cache->key.objectid >= (range->start + range->len)) {
8902 btrfs_put_block_group(cache);
8903 break;
8904 }
8905
8906 start = max(range->start, cache->key.objectid);
8907 end = min(range->start + range->len,
8908 cache->key.objectid + cache->key.offset);
8909
8910 if (end - start >= range->minlen) {
8911 if (!block_group_cache_done(cache)) {
8912 ret = cache_block_group(cache, NULL, root, 0);
8913 if (!ret)
8914 wait_block_group_cache_done(cache);
8915 }
8916 ret = btrfs_trim_block_group(cache,
8917 &group_trimmed,
8918 start,
8919 end,
8920 range->minlen);
8921
8922 trimmed += group_trimmed;
8923 if (ret) {
8924 btrfs_put_block_group(cache);
8925 break;
8926 }
8927 }
8928
8929 cache = next_block_group(fs_info->tree_root, cache);
8930 }
8931
8932 range->len = trimmed;
8933 return ret;
8934}