blob: 08188f1615d9ba4fac89c8f3f77ec5b859d20f12 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050025#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050026#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027#include "ctree.h"
28#include "disk-io.h"
29#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040030#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040032#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040033#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Chris Mason56bec292009-03-13 10:10:06 -040035static int update_reserved_extents(struct btrfs_root *root,
36 u64 bytenr, u64 num, int reserve);
Josef Bacikf3465ca2008-11-12 14:19:50 -050037static int update_block_group(struct btrfs_trans_handle *trans,
38 struct btrfs_root *root,
39 u64 bytenr, u64 num_bytes, int alloc,
40 int mark_free);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040041static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root,
43 u64 bytenr, u64 num_bytes, u64 parent,
44 u64 root_objectid, u64 owner_objectid,
45 u64 owner_offset, int refs_to_drop,
46 struct btrfs_delayed_extent_op *extra_op);
47static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
48 struct extent_buffer *leaf,
49 struct btrfs_extent_item *ei);
50static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root,
52 u64 parent, u64 root_objectid,
53 u64 flags, u64 owner, u64 offset,
54 struct btrfs_key *ins, int ref_mod);
55static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 parent, u64 root_objectid,
58 u64 flags, struct btrfs_disk_key *key,
59 int level, struct btrfs_key *ins);
Yand548ee52008-01-03 13:56:30 -050060
Josef Bacik6a632092009-02-20 11:00:09 -050061static int do_chunk_alloc(struct btrfs_trans_handle *trans,
62 struct btrfs_root *extent_root, u64 alloc_bytes,
63 u64 flags, int force);
64
Josef Bacik817d52f2009-07-13 21:29:25 -040065static noinline int
66block_group_cache_done(struct btrfs_block_group_cache *cache)
67{
68 smp_mb();
69 return cache->cached == BTRFS_CACHE_FINISHED;
70}
71
Josef Bacik0f9dd462008-09-23 13:14:11 -040072static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
73{
74 return (cache->flags & bits) == bits;
75}
76
77/*
78 * this adds the block group to the fs_info rb tree for the block group
79 * cache
80 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050081static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040082 struct btrfs_block_group_cache *block_group)
83{
84 struct rb_node **p;
85 struct rb_node *parent = NULL;
86 struct btrfs_block_group_cache *cache;
87
88 spin_lock(&info->block_group_cache_lock);
89 p = &info->block_group_cache_tree.rb_node;
90
91 while (*p) {
92 parent = *p;
93 cache = rb_entry(parent, struct btrfs_block_group_cache,
94 cache_node);
95 if (block_group->key.objectid < cache->key.objectid) {
96 p = &(*p)->rb_left;
97 } else if (block_group->key.objectid > cache->key.objectid) {
98 p = &(*p)->rb_right;
99 } else {
100 spin_unlock(&info->block_group_cache_lock);
101 return -EEXIST;
102 }
103 }
104
105 rb_link_node(&block_group->cache_node, parent, p);
106 rb_insert_color(&block_group->cache_node,
107 &info->block_group_cache_tree);
108 spin_unlock(&info->block_group_cache_lock);
109
110 return 0;
111}
112
113/*
114 * This will return the block group at or after bytenr if contains is 0, else
115 * it will return the block group that contains the bytenr
116 */
117static struct btrfs_block_group_cache *
118block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
119 int contains)
120{
121 struct btrfs_block_group_cache *cache, *ret = NULL;
122 struct rb_node *n;
123 u64 end, start;
124
125 spin_lock(&info->block_group_cache_lock);
126 n = info->block_group_cache_tree.rb_node;
127
128 while (n) {
129 cache = rb_entry(n, struct btrfs_block_group_cache,
130 cache_node);
131 end = cache->key.objectid + cache->key.offset - 1;
132 start = cache->key.objectid;
133
134 if (bytenr < start) {
135 if (!contains && (!ret || start < ret->key.objectid))
136 ret = cache;
137 n = n->rb_left;
138 } else if (bytenr > start) {
139 if (contains && bytenr <= end) {
140 ret = cache;
141 break;
142 }
143 n = n->rb_right;
144 } else {
145 ret = cache;
146 break;
147 }
148 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500149 if (ret)
150 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400151 spin_unlock(&info->block_group_cache_lock);
152
153 return ret;
154}
155
Josef Bacik68b38552009-07-27 13:57:01 -0400156/*
157 * We always set EXTENT_LOCKED for the super mirror extents so we don't
158 * overwrite them, so those bits need to be unset. Also, if we are unmounting
159 * with pinned extents still sitting there because we had a block group caching,
160 * we need to clear those now, since we are done.
161 */
162void btrfs_free_pinned_extents(struct btrfs_fs_info *info)
Josef Bacik817d52f2009-07-13 21:29:25 -0400163{
164 u64 start, end, last = 0;
165 int ret;
166
167 while (1) {
168 ret = find_first_extent_bit(&info->pinned_extents, last,
Josef Bacik68b38552009-07-27 13:57:01 -0400169 &start, &end,
170 EXTENT_LOCKED|EXTENT_DIRTY);
Josef Bacik817d52f2009-07-13 21:29:25 -0400171 if (ret)
172 break;
173
Josef Bacik68b38552009-07-27 13:57:01 -0400174 clear_extent_bits(&info->pinned_extents, start, end,
175 EXTENT_LOCKED|EXTENT_DIRTY, GFP_NOFS);
Josef Bacik817d52f2009-07-13 21:29:25 -0400176 last = end+1;
177 }
178}
179
180static int remove_sb_from_cache(struct btrfs_root *root,
181 struct btrfs_block_group_cache *cache)
182{
183 struct btrfs_fs_info *fs_info = root->fs_info;
184 u64 bytenr;
185 u64 *logical;
186 int stripe_len;
187 int i, nr, ret;
188
189 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
190 bytenr = btrfs_sb_offset(i);
191 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
192 cache->key.objectid, bytenr,
193 0, &logical, &nr, &stripe_len);
194 BUG_ON(ret);
195 while (nr--) {
196 try_lock_extent(&fs_info->pinned_extents,
197 logical[nr],
198 logical[nr] + stripe_len - 1, GFP_NOFS);
199 }
200 kfree(logical);
201 }
202
203 return 0;
204}
205
Josef Bacik0f9dd462008-09-23 13:14:11 -0400206/*
207 * this is only called by cache_block_group, since we could have freed extents
208 * we need to check the pinned_extents for any extents that can't be used yet
209 * since their free space will be released as soon as the transaction commits.
210 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400211static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400212 struct btrfs_fs_info *info, u64 start, u64 end)
213{
Josef Bacik817d52f2009-07-13 21:29:25 -0400214 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400215 int ret;
216
217 while (start < end) {
218 ret = find_first_extent_bit(&info->pinned_extents, start,
219 &extent_start, &extent_end,
Josef Bacik68b38552009-07-27 13:57:01 -0400220 EXTENT_DIRTY|EXTENT_LOCKED);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400221 if (ret)
222 break;
223
224 if (extent_start == start) {
225 start = extent_end + 1;
226 } else if (extent_start > start && extent_start < end) {
227 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400228 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500229 ret = btrfs_add_free_space(block_group, start,
230 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400231 BUG_ON(ret);
232 start = extent_end + 1;
233 } else {
234 break;
235 }
236 }
237
238 if (start < end) {
239 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400240 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500241 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400242 BUG_ON(ret);
243 }
244
Josef Bacik817d52f2009-07-13 21:29:25 -0400245 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400246}
247
Josef Bacik817d52f2009-07-13 21:29:25 -0400248static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400249{
Josef Bacik817d52f2009-07-13 21:29:25 -0400250 struct btrfs_block_group_cache *block_group = data;
251 struct btrfs_fs_info *fs_info = block_group->fs_info;
252 u64 last = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400253 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400254 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400255 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400256 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400257 int slot;
Josef Bacik817d52f2009-07-13 21:29:25 -0400258 u64 total_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400259
Josef Bacik817d52f2009-07-13 21:29:25 -0400260 BUG_ON(!fs_info);
Chris Masonf510cfe2007-10-15 16:14:48 -0400261
Chris Masone37c9e62007-05-09 20:13:14 -0400262 path = btrfs_alloc_path();
263 if (!path)
264 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400265
Josef Bacik817d52f2009-07-13 21:29:25 -0400266 atomic_inc(&block_group->space_info->caching_threads);
267 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
268again:
269 /* need to make sure the commit_root doesn't disappear */
270 down_read(&fs_info->extent_root->commit_root_sem);
271
Chris Mason5cd57b22008-06-25 16:01:30 -0400272 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400273 * We don't want to deadlock with somebody trying to allocate a new
274 * extent for the extent root while also trying to search the extent
275 * root to add free space. So we skip locking and search the commit
276 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400277 */
278 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400279 path->search_commit_root = 1;
280 path->reada = 2;
281
Yan Zhenge4404d62008-12-12 10:03:26 -0500282 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400283 key.offset = 0;
284 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik817d52f2009-07-13 21:29:25 -0400285 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400286 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400287 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500288
Chris Masond3977122009-01-05 21:25:51 -0500289 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400290 smp_mb();
291 if (block_group->fs_info->closing)
292 break;
293
Chris Mason5f39d392007-10-15 16:14:19 -0400294 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400295 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400296 if (slot >= btrfs_header_nritems(leaf)) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400297 ret = btrfs_next_leaf(fs_info->extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400298 if (ret < 0)
299 goto err;
Josef Bacik817d52f2009-07-13 21:29:25 -0400300 else if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400301 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400302
303 if (need_resched()) {
304 btrfs_release_path(fs_info->extent_root, path);
305 up_read(&fs_info->extent_root->commit_root_sem);
306 cond_resched();
307 goto again;
308 }
309
310 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400311 }
Chris Mason5f39d392007-10-15 16:14:19 -0400312 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400313 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400314 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400315
Chris Masone37c9e62007-05-09 20:13:14 -0400316 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400317 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400318 break;
Yan7d7d6062007-09-14 16:15:28 -0400319
320 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400321 total_found += add_new_free_space(block_group,
322 fs_info, last,
323 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400324 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400325 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400326
327 if (total_found > (1024 * 1024 * 2)) {
328 total_found = 0;
329 wake_up(&block_group->caching_q);
330 }
Yan7d7d6062007-09-14 16:15:28 -0400331next:
Chris Masone37c9e62007-05-09 20:13:14 -0400332 path->slots[0]++;
333 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400334 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400335
336 total_found += add_new_free_space(block_group, fs_info, last,
337 block_group->key.objectid +
338 block_group->key.offset);
339
340 spin_lock(&block_group->lock);
341 block_group->cached = BTRFS_CACHE_FINISHED;
342 spin_unlock(&block_group->lock);
343
Chris Mason54aa1f42007-06-22 14:16:25 -0400344err:
Chris Masone37c9e62007-05-09 20:13:14 -0400345 btrfs_free_path(path);
Josef Bacik817d52f2009-07-13 21:29:25 -0400346 up_read(&fs_info->extent_root->commit_root_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400347 atomic_dec(&block_group->space_info->caching_threads);
348 wake_up(&block_group->caching_q);
349
Josef Bacik817d52f2009-07-13 21:29:25 -0400350 return 0;
351}
352
353static int cache_block_group(struct btrfs_block_group_cache *cache)
354{
355 struct task_struct *tsk;
356 int ret = 0;
357
358 spin_lock(&cache->lock);
359 if (cache->cached != BTRFS_CACHE_NO) {
360 spin_unlock(&cache->lock);
361 return ret;
362 }
363 cache->cached = BTRFS_CACHE_STARTED;
364 spin_unlock(&cache->lock);
365
366 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
367 cache->key.objectid);
368 if (IS_ERR(tsk)) {
369 ret = PTR_ERR(tsk);
370 printk(KERN_ERR "error running thread %d\n", ret);
371 BUG();
372 }
373
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400374 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400375}
376
Josef Bacik0f9dd462008-09-23 13:14:11 -0400377/*
378 * return the block group that starts at or after bytenr
379 */
Chris Masond3977122009-01-05 21:25:51 -0500380static struct btrfs_block_group_cache *
381btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400382{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400383 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400384
Josef Bacik0f9dd462008-09-23 13:14:11 -0400385 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400386
Josef Bacik0f9dd462008-09-23 13:14:11 -0400387 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400388}
389
Josef Bacik0f9dd462008-09-23 13:14:11 -0400390/*
Sankar P9f556842009-05-14 13:52:22 -0400391 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400392 */
Chris Masond3977122009-01-05 21:25:51 -0500393struct btrfs_block_group_cache *btrfs_lookup_block_group(
394 struct btrfs_fs_info *info,
395 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400396{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400397 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400398
Josef Bacik0f9dd462008-09-23 13:14:11 -0400399 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400400
Josef Bacik0f9dd462008-09-23 13:14:11 -0400401 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400402}
Chris Mason0b86a832008-03-24 15:01:56 -0400403
Chris Masonfa9c0d72009-04-03 09:47:43 -0400404void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
Yan Zhengd2fb3432008-12-11 16:30:39 -0500405{
406 if (atomic_dec_and_test(&cache->count))
407 kfree(cache);
408}
409
Josef Bacik0f9dd462008-09-23 13:14:11 -0400410static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
411 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400412{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400413 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400414 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400415
416 rcu_read_lock();
417 list_for_each_entry_rcu(found, head, list) {
418 if (found->flags == flags) {
419 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400420 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400421 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400422 }
Chris Mason4184ea72009-03-10 12:39:20 -0400423 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400424 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400425}
426
Chris Mason4184ea72009-03-10 12:39:20 -0400427/*
428 * after adding space to the filesystem, we need to clear the full flags
429 * on all the space infos.
430 */
431void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
432{
433 struct list_head *head = &info->space_info;
434 struct btrfs_space_info *found;
435
436 rcu_read_lock();
437 list_for_each_entry_rcu(found, head, list)
438 found->full = 0;
439 rcu_read_unlock();
440}
441
Josef Bacik80eb2342008-10-29 14:49:05 -0400442static u64 div_factor(u64 num, int factor)
443{
444 if (factor == 10)
445 return num;
446 num *= factor;
447 do_div(num, 10);
448 return num;
449}
450
Yan Zhengd2fb3432008-12-11 16:30:39 -0500451u64 btrfs_find_block_group(struct btrfs_root *root,
452 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400453{
Chris Mason96b51792007-10-15 16:15:19 -0400454 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400455 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500456 u64 last = max(search_hint, search_start);
457 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400458 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500459 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400460 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400461again:
Zheng Yane8569812008-09-26 10:05:48 -0400462 while (1) {
463 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400464 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400465 break;
Chris Mason96b51792007-10-15 16:15:19 -0400466
Chris Masonc286ac42008-07-22 23:06:41 -0400467 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400468 last = cache->key.objectid + cache->key.offset;
469 used = btrfs_block_group_used(&cache->item);
470
Yan Zhengd2fb3432008-12-11 16:30:39 -0500471 if ((full_search || !cache->ro) &&
472 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400473 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500474 div_factor(cache->key.offset, factor)) {
475 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400476 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400477 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400478 goto found;
479 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400480 }
Chris Masonc286ac42008-07-22 23:06:41 -0400481 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400482 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400483 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400484 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400485 if (!wrapped) {
486 last = search_start;
487 wrapped = 1;
488 goto again;
489 }
490 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400491 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400492 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400493 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400494 goto again;
495 }
Chris Masonbe744172007-05-06 10:15:01 -0400496found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500497 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400498}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400499
Chris Masone02119d2008-09-05 16:13:11 -0400500/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400501int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400502{
503 int ret;
504 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400505 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400506
Zheng Yan31840ae2008-09-23 13:14:14 -0400507 path = btrfs_alloc_path();
508 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400509 key.objectid = start;
510 key.offset = len;
511 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
512 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
513 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400514 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500515 return ret;
516}
517
Chris Masond8d5f3e2007-12-11 12:42:00 -0500518/*
519 * Back reference rules. Back refs have three main goals:
520 *
521 * 1) differentiate between all holders of references to an extent so that
522 * when a reference is dropped we can make sure it was a valid reference
523 * before freeing the extent.
524 *
525 * 2) Provide enough information to quickly find the holders of an extent
526 * if we notice a given block is corrupted or bad.
527 *
528 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
529 * maintenance. This is actually the same as #2, but with a slightly
530 * different use case.
531 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400532 * There are two kinds of back refs. The implicit back refs is optimized
533 * for pointers in non-shared tree blocks. For a given pointer in a block,
534 * back refs of this kind provide information about the block's owner tree
535 * and the pointer's key. These information allow us to find the block by
536 * b-tree searching. The full back refs is for pointers in tree blocks not
537 * referenced by their owner trees. The location of tree block is recorded
538 * in the back refs. Actually the full back refs is generic, and can be
539 * used in all cases the implicit back refs is used. The major shortcoming
540 * of the full back refs is its overhead. Every time a tree block gets
541 * COWed, we have to update back refs entry for all pointers in it.
542 *
543 * For a newly allocated tree block, we use implicit back refs for
544 * pointers in it. This means most tree related operations only involve
545 * implicit back refs. For a tree block created in old transaction, the
546 * only way to drop a reference to it is COW it. So we can detect the
547 * event that tree block loses its owner tree's reference and do the
548 * back refs conversion.
549 *
550 * When a tree block is COW'd through a tree, there are four cases:
551 *
552 * The reference count of the block is one and the tree is the block's
553 * owner tree. Nothing to do in this case.
554 *
555 * The reference count of the block is one and the tree is not the
556 * block's owner tree. In this case, full back refs is used for pointers
557 * in the block. Remove these full back refs, add implicit back refs for
558 * every pointers in the new block.
559 *
560 * The reference count of the block is greater than one and the tree is
561 * the block's owner tree. In this case, implicit back refs is used for
562 * pointers in the block. Add full back refs for every pointers in the
563 * block, increase lower level extents' reference counts. The original
564 * implicit back refs are entailed to the new block.
565 *
566 * The reference count of the block is greater than one and the tree is
567 * not the block's owner tree. Add implicit back refs for every pointer in
568 * the new block, increase lower level extents' reference count.
569 *
570 * Back Reference Key composing:
571 *
572 * The key objectid corresponds to the first byte in the extent,
573 * The key type is used to differentiate between types of back refs.
574 * There are different meanings of the key offset for different types
575 * of back refs.
576 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500577 * File extents can be referenced by:
578 *
579 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400580 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500581 * - different offsets inside a file (bookend extents in file.c)
582 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400583 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500584 *
585 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500586 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400587 * - original offset in the file
588 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400589 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400590 * The key offset for the implicit back refs is hash of the first
591 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500592 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400593 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500594 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400595 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500596 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400597 * The key offset for the implicit back refs is the first byte of
598 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500599 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400600 * When a file extent is allocated, The implicit back refs is used.
601 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500602 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400603 * (root_key.objectid, inode objectid, offset in file, 1)
604 *
605 * When a file extent is removed file truncation, we find the
606 * corresponding implicit back refs and check the following fields:
607 *
608 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500609 *
610 * Btree extents can be referenced by:
611 *
612 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500613 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400614 * Both the implicit back refs and the full back refs for tree blocks
615 * only consist of key. The key offset for the implicit back refs is
616 * objectid of block's owner tree. The key offset for the full back refs
617 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500618 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400619 * When implicit back refs is used, information about the lowest key and
620 * level of the tree block are required. These information are stored in
621 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500622 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400623
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400624#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
625static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
626 struct btrfs_root *root,
627 struct btrfs_path *path,
628 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500629{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400630 struct btrfs_extent_item *item;
631 struct btrfs_extent_item_v0 *ei0;
632 struct btrfs_extent_ref_v0 *ref0;
633 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400634 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400635 struct btrfs_key key;
636 struct btrfs_key found_key;
637 u32 new_size = sizeof(*item);
638 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500639 int ret;
640
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400641 leaf = path->nodes[0];
642 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500643
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400644 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
645 ei0 = btrfs_item_ptr(leaf, path->slots[0],
646 struct btrfs_extent_item_v0);
647 refs = btrfs_extent_refs_v0(leaf, ei0);
648
649 if (owner == (u64)-1) {
650 while (1) {
651 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
652 ret = btrfs_next_leaf(root, path);
653 if (ret < 0)
654 return ret;
655 BUG_ON(ret > 0);
656 leaf = path->nodes[0];
657 }
658 btrfs_item_key_to_cpu(leaf, &found_key,
659 path->slots[0]);
660 BUG_ON(key.objectid != found_key.objectid);
661 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
662 path->slots[0]++;
663 continue;
664 }
665 ref0 = btrfs_item_ptr(leaf, path->slots[0],
666 struct btrfs_extent_ref_v0);
667 owner = btrfs_ref_objectid_v0(leaf, ref0);
668 break;
669 }
670 }
671 btrfs_release_path(root, path);
672
673 if (owner < BTRFS_FIRST_FREE_OBJECTID)
674 new_size += sizeof(*bi);
675
676 new_size -= sizeof(*ei0);
677 ret = btrfs_search_slot(trans, root, &key, path,
678 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400679 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400680 return ret;
681 BUG_ON(ret);
682
683 ret = btrfs_extend_item(trans, root, path, new_size);
684 BUG_ON(ret);
685
686 leaf = path->nodes[0];
687 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
688 btrfs_set_extent_refs(leaf, item, refs);
689 /* FIXME: get real generation */
690 btrfs_set_extent_generation(leaf, item, 0);
691 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
692 btrfs_set_extent_flags(leaf, item,
693 BTRFS_EXTENT_FLAG_TREE_BLOCK |
694 BTRFS_BLOCK_FLAG_FULL_BACKREF);
695 bi = (struct btrfs_tree_block_info *)(item + 1);
696 /* FIXME: get first key of the block */
697 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
698 btrfs_set_tree_block_level(leaf, bi, (int)owner);
699 } else {
700 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
701 }
702 btrfs_mark_buffer_dirty(leaf);
703 return 0;
704}
705#endif
706
707static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
708{
709 u32 high_crc = ~(u32)0;
710 u32 low_crc = ~(u32)0;
711 __le64 lenum;
712
713 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100714 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400715 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100716 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400717 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100718 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400719
720 return ((u64)high_crc << 31) ^ (u64)low_crc;
721}
722
723static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
724 struct btrfs_extent_data_ref *ref)
725{
726 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
727 btrfs_extent_data_ref_objectid(leaf, ref),
728 btrfs_extent_data_ref_offset(leaf, ref));
729}
730
731static int match_extent_data_ref(struct extent_buffer *leaf,
732 struct btrfs_extent_data_ref *ref,
733 u64 root_objectid, u64 owner, u64 offset)
734{
735 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
736 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
737 btrfs_extent_data_ref_offset(leaf, ref) != offset)
738 return 0;
739 return 1;
740}
741
742static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
743 struct btrfs_root *root,
744 struct btrfs_path *path,
745 u64 bytenr, u64 parent,
746 u64 root_objectid,
747 u64 owner, u64 offset)
748{
749 struct btrfs_key key;
750 struct btrfs_extent_data_ref *ref;
751 struct extent_buffer *leaf;
752 u32 nritems;
753 int ret;
754 int recow;
755 int err = -ENOENT;
756
757 key.objectid = bytenr;
758 if (parent) {
759 key.type = BTRFS_SHARED_DATA_REF_KEY;
760 key.offset = parent;
761 } else {
762 key.type = BTRFS_EXTENT_DATA_REF_KEY;
763 key.offset = hash_extent_data_ref(root_objectid,
764 owner, offset);
765 }
766again:
767 recow = 0;
768 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
769 if (ret < 0) {
770 err = ret;
771 goto fail;
772 }
773
774 if (parent) {
775 if (!ret)
776 return 0;
777#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
778 key.type = BTRFS_EXTENT_REF_V0_KEY;
779 btrfs_release_path(root, path);
780 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
781 if (ret < 0) {
782 err = ret;
783 goto fail;
784 }
785 if (!ret)
786 return 0;
787#endif
788 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400789 }
790
791 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400792 nritems = btrfs_header_nritems(leaf);
793 while (1) {
794 if (path->slots[0] >= nritems) {
795 ret = btrfs_next_leaf(root, path);
796 if (ret < 0)
797 err = ret;
798 if (ret)
799 goto fail;
800
801 leaf = path->nodes[0];
802 nritems = btrfs_header_nritems(leaf);
803 recow = 1;
804 }
805
806 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
807 if (key.objectid != bytenr ||
808 key.type != BTRFS_EXTENT_DATA_REF_KEY)
809 goto fail;
810
811 ref = btrfs_item_ptr(leaf, path->slots[0],
812 struct btrfs_extent_data_ref);
813
814 if (match_extent_data_ref(leaf, ref, root_objectid,
815 owner, offset)) {
816 if (recow) {
817 btrfs_release_path(root, path);
818 goto again;
819 }
820 err = 0;
821 break;
822 }
823 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400824 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400825fail:
826 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400827}
828
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400829static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
830 struct btrfs_root *root,
831 struct btrfs_path *path,
832 u64 bytenr, u64 parent,
833 u64 root_objectid, u64 owner,
834 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400835{
836 struct btrfs_key key;
837 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400838 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400839 u32 num_refs;
840 int ret;
841
842 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400843 if (parent) {
844 key.type = BTRFS_SHARED_DATA_REF_KEY;
845 key.offset = parent;
846 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400847 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400848 key.type = BTRFS_EXTENT_DATA_REF_KEY;
849 key.offset = hash_extent_data_ref(root_objectid,
850 owner, offset);
851 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400852 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400853
854 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
855 if (ret && ret != -EEXIST)
856 goto fail;
857
858 leaf = path->nodes[0];
859 if (parent) {
860 struct btrfs_shared_data_ref *ref;
861 ref = btrfs_item_ptr(leaf, path->slots[0],
862 struct btrfs_shared_data_ref);
863 if (ret == 0) {
864 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
865 } else {
866 num_refs = btrfs_shared_data_ref_count(leaf, ref);
867 num_refs += refs_to_add;
868 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
869 }
870 } else {
871 struct btrfs_extent_data_ref *ref;
872 while (ret == -EEXIST) {
873 ref = btrfs_item_ptr(leaf, path->slots[0],
874 struct btrfs_extent_data_ref);
875 if (match_extent_data_ref(leaf, ref, root_objectid,
876 owner, offset))
877 break;
878 btrfs_release_path(root, path);
879 key.offset++;
880 ret = btrfs_insert_empty_item(trans, root, path, &key,
881 size);
882 if (ret && ret != -EEXIST)
883 goto fail;
884
885 leaf = path->nodes[0];
886 }
887 ref = btrfs_item_ptr(leaf, path->slots[0],
888 struct btrfs_extent_data_ref);
889 if (ret == 0) {
890 btrfs_set_extent_data_ref_root(leaf, ref,
891 root_objectid);
892 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
893 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
894 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
895 } else {
896 num_refs = btrfs_extent_data_ref_count(leaf, ref);
897 num_refs += refs_to_add;
898 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
899 }
900 }
901 btrfs_mark_buffer_dirty(leaf);
902 ret = 0;
903fail:
Chris Mason7bb86312007-12-11 09:25:06 -0500904 btrfs_release_path(root, path);
905 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500906}
907
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400908static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
909 struct btrfs_root *root,
910 struct btrfs_path *path,
911 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -0400912{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400913 struct btrfs_key key;
914 struct btrfs_extent_data_ref *ref1 = NULL;
915 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400916 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400917 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400918 int ret = 0;
919
920 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400921 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
922
923 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
924 ref1 = btrfs_item_ptr(leaf, path->slots[0],
925 struct btrfs_extent_data_ref);
926 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
927 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
928 ref2 = btrfs_item_ptr(leaf, path->slots[0],
929 struct btrfs_shared_data_ref);
930 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
931#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
932 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
933 struct btrfs_extent_ref_v0 *ref0;
934 ref0 = btrfs_item_ptr(leaf, path->slots[0],
935 struct btrfs_extent_ref_v0);
936 num_refs = btrfs_ref_count_v0(leaf, ref0);
937#endif
938 } else {
939 BUG();
940 }
941
Chris Mason56bec292009-03-13 10:10:06 -0400942 BUG_ON(num_refs < refs_to_drop);
943 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400944
Zheng Yan31840ae2008-09-23 13:14:14 -0400945 if (num_refs == 0) {
946 ret = btrfs_del_item(trans, root, path);
947 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400948 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
949 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
950 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
951 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
952#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
953 else {
954 struct btrfs_extent_ref_v0 *ref0;
955 ref0 = btrfs_item_ptr(leaf, path->slots[0],
956 struct btrfs_extent_ref_v0);
957 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
958 }
959#endif
Zheng Yan31840ae2008-09-23 13:14:14 -0400960 btrfs_mark_buffer_dirty(leaf);
961 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400962 return ret;
963}
964
965static noinline u32 extent_data_ref_count(struct btrfs_root *root,
966 struct btrfs_path *path,
967 struct btrfs_extent_inline_ref *iref)
968{
969 struct btrfs_key key;
970 struct extent_buffer *leaf;
971 struct btrfs_extent_data_ref *ref1;
972 struct btrfs_shared_data_ref *ref2;
973 u32 num_refs = 0;
974
975 leaf = path->nodes[0];
976 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
977 if (iref) {
978 if (btrfs_extent_inline_ref_type(leaf, iref) ==
979 BTRFS_EXTENT_DATA_REF_KEY) {
980 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
981 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
982 } else {
983 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
984 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
985 }
986 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
987 ref1 = btrfs_item_ptr(leaf, path->slots[0],
988 struct btrfs_extent_data_ref);
989 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
990 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
991 ref2 = btrfs_item_ptr(leaf, path->slots[0],
992 struct btrfs_shared_data_ref);
993 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
994#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
995 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
996 struct btrfs_extent_ref_v0 *ref0;
997 ref0 = btrfs_item_ptr(leaf, path->slots[0],
998 struct btrfs_extent_ref_v0);
999 num_refs = btrfs_ref_count_v0(leaf, ref0);
1000#endif
1001 } else {
1002 WARN_ON(1);
1003 }
1004 return num_refs;
1005}
1006
1007static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1008 struct btrfs_root *root,
1009 struct btrfs_path *path,
1010 u64 bytenr, u64 parent,
1011 u64 root_objectid)
1012{
1013 struct btrfs_key key;
1014 int ret;
1015
1016 key.objectid = bytenr;
1017 if (parent) {
1018 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1019 key.offset = parent;
1020 } else {
1021 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1022 key.offset = root_objectid;
1023 }
1024
1025 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1026 if (ret > 0)
1027 ret = -ENOENT;
1028#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1029 if (ret == -ENOENT && parent) {
1030 btrfs_release_path(root, path);
1031 key.type = BTRFS_EXTENT_REF_V0_KEY;
1032 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1033 if (ret > 0)
1034 ret = -ENOENT;
1035 }
1036#endif
1037 return ret;
1038}
1039
1040static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1041 struct btrfs_root *root,
1042 struct btrfs_path *path,
1043 u64 bytenr, u64 parent,
1044 u64 root_objectid)
1045{
1046 struct btrfs_key key;
1047 int ret;
1048
1049 key.objectid = bytenr;
1050 if (parent) {
1051 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1052 key.offset = parent;
1053 } else {
1054 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1055 key.offset = root_objectid;
1056 }
1057
1058 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001059 btrfs_release_path(root, path);
1060 return ret;
1061}
1062
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001063static inline int extent_ref_type(u64 parent, u64 owner)
1064{
1065 int type;
1066 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1067 if (parent > 0)
1068 type = BTRFS_SHARED_BLOCK_REF_KEY;
1069 else
1070 type = BTRFS_TREE_BLOCK_REF_KEY;
1071 } else {
1072 if (parent > 0)
1073 type = BTRFS_SHARED_DATA_REF_KEY;
1074 else
1075 type = BTRFS_EXTENT_DATA_REF_KEY;
1076 }
1077 return type;
1078}
1079
Yan Zheng2c47e6052009-06-27 21:07:35 -04001080static int find_next_key(struct btrfs_path *path, int level,
1081 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001082
1083{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001084 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001085 if (!path->nodes[level])
1086 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001087 if (path->slots[level] + 1 >=
1088 btrfs_header_nritems(path->nodes[level]))
1089 continue;
1090 if (level == 0)
1091 btrfs_item_key_to_cpu(path->nodes[level], key,
1092 path->slots[level] + 1);
1093 else
1094 btrfs_node_key_to_cpu(path->nodes[level], key,
1095 path->slots[level] + 1);
1096 return 0;
1097 }
1098 return 1;
1099}
1100
1101/*
1102 * look for inline back ref. if back ref is found, *ref_ret is set
1103 * to the address of inline back ref, and 0 is returned.
1104 *
1105 * if back ref isn't found, *ref_ret is set to the address where it
1106 * should be inserted, and -ENOENT is returned.
1107 *
1108 * if insert is true and there are too many inline back refs, the path
1109 * points to the extent item, and -EAGAIN is returned.
1110 *
1111 * NOTE: inline back refs are ordered in the same way that back ref
1112 * items in the tree are ordered.
1113 */
1114static noinline_for_stack
1115int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1116 struct btrfs_root *root,
1117 struct btrfs_path *path,
1118 struct btrfs_extent_inline_ref **ref_ret,
1119 u64 bytenr, u64 num_bytes,
1120 u64 parent, u64 root_objectid,
1121 u64 owner, u64 offset, int insert)
1122{
1123 struct btrfs_key key;
1124 struct extent_buffer *leaf;
1125 struct btrfs_extent_item *ei;
1126 struct btrfs_extent_inline_ref *iref;
1127 u64 flags;
1128 u64 item_size;
1129 unsigned long ptr;
1130 unsigned long end;
1131 int extra_size;
1132 int type;
1133 int want;
1134 int ret;
1135 int err = 0;
1136
1137 key.objectid = bytenr;
1138 key.type = BTRFS_EXTENT_ITEM_KEY;
1139 key.offset = num_bytes;
1140
1141 want = extent_ref_type(parent, owner);
1142 if (insert) {
1143 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001144 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001145 } else
1146 extra_size = -1;
1147 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1148 if (ret < 0) {
1149 err = ret;
1150 goto out;
1151 }
1152 BUG_ON(ret);
1153
1154 leaf = path->nodes[0];
1155 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1156#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1157 if (item_size < sizeof(*ei)) {
1158 if (!insert) {
1159 err = -ENOENT;
1160 goto out;
1161 }
1162 ret = convert_extent_item_v0(trans, root, path, owner,
1163 extra_size);
1164 if (ret < 0) {
1165 err = ret;
1166 goto out;
1167 }
1168 leaf = path->nodes[0];
1169 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1170 }
1171#endif
1172 BUG_ON(item_size < sizeof(*ei));
1173
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001174 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1175 flags = btrfs_extent_flags(leaf, ei);
1176
1177 ptr = (unsigned long)(ei + 1);
1178 end = (unsigned long)ei + item_size;
1179
1180 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1181 ptr += sizeof(struct btrfs_tree_block_info);
1182 BUG_ON(ptr > end);
1183 } else {
1184 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1185 }
1186
1187 err = -ENOENT;
1188 while (1) {
1189 if (ptr >= end) {
1190 WARN_ON(ptr > end);
1191 break;
1192 }
1193 iref = (struct btrfs_extent_inline_ref *)ptr;
1194 type = btrfs_extent_inline_ref_type(leaf, iref);
1195 if (want < type)
1196 break;
1197 if (want > type) {
1198 ptr += btrfs_extent_inline_ref_size(type);
1199 continue;
1200 }
1201
1202 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1203 struct btrfs_extent_data_ref *dref;
1204 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1205 if (match_extent_data_ref(leaf, dref, root_objectid,
1206 owner, offset)) {
1207 err = 0;
1208 break;
1209 }
1210 if (hash_extent_data_ref_item(leaf, dref) <
1211 hash_extent_data_ref(root_objectid, owner, offset))
1212 break;
1213 } else {
1214 u64 ref_offset;
1215 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1216 if (parent > 0) {
1217 if (parent == ref_offset) {
1218 err = 0;
1219 break;
1220 }
1221 if (ref_offset < parent)
1222 break;
1223 } else {
1224 if (root_objectid == ref_offset) {
1225 err = 0;
1226 break;
1227 }
1228 if (ref_offset < root_objectid)
1229 break;
1230 }
1231 }
1232 ptr += btrfs_extent_inline_ref_size(type);
1233 }
1234 if (err == -ENOENT && insert) {
1235 if (item_size + extra_size >=
1236 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1237 err = -EAGAIN;
1238 goto out;
1239 }
1240 /*
1241 * To add new inline back ref, we have to make sure
1242 * there is no corresponding back ref item.
1243 * For simplicity, we just do not add new inline back
1244 * ref if there is any kind of item for this block
1245 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001246 if (find_next_key(path, 0, &key) == 0 &&
1247 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001248 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001249 err = -EAGAIN;
1250 goto out;
1251 }
1252 }
1253 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1254out:
Yan Zheng85d41982009-06-11 08:51:10 -04001255 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001256 path->keep_locks = 0;
1257 btrfs_unlock_up_safe(path, 1);
1258 }
1259 return err;
1260}
1261
1262/*
1263 * helper to add new inline back ref
1264 */
1265static noinline_for_stack
1266int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1267 struct btrfs_root *root,
1268 struct btrfs_path *path,
1269 struct btrfs_extent_inline_ref *iref,
1270 u64 parent, u64 root_objectid,
1271 u64 owner, u64 offset, int refs_to_add,
1272 struct btrfs_delayed_extent_op *extent_op)
1273{
1274 struct extent_buffer *leaf;
1275 struct btrfs_extent_item *ei;
1276 unsigned long ptr;
1277 unsigned long end;
1278 unsigned long item_offset;
1279 u64 refs;
1280 int size;
1281 int type;
1282 int ret;
1283
1284 leaf = path->nodes[0];
1285 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1286 item_offset = (unsigned long)iref - (unsigned long)ei;
1287
1288 type = extent_ref_type(parent, owner);
1289 size = btrfs_extent_inline_ref_size(type);
1290
1291 ret = btrfs_extend_item(trans, root, path, size);
1292 BUG_ON(ret);
1293
1294 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1295 refs = btrfs_extent_refs(leaf, ei);
1296 refs += refs_to_add;
1297 btrfs_set_extent_refs(leaf, ei, refs);
1298 if (extent_op)
1299 __run_delayed_extent_op(extent_op, leaf, ei);
1300
1301 ptr = (unsigned long)ei + item_offset;
1302 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1303 if (ptr < end - size)
1304 memmove_extent_buffer(leaf, ptr + size, ptr,
1305 end - size - ptr);
1306
1307 iref = (struct btrfs_extent_inline_ref *)ptr;
1308 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1309 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1310 struct btrfs_extent_data_ref *dref;
1311 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1312 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1313 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1314 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1315 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1316 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1317 struct btrfs_shared_data_ref *sref;
1318 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1319 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1320 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1321 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1322 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1323 } else {
1324 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1325 }
1326 btrfs_mark_buffer_dirty(leaf);
1327 return 0;
1328}
1329
1330static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1331 struct btrfs_root *root,
1332 struct btrfs_path *path,
1333 struct btrfs_extent_inline_ref **ref_ret,
1334 u64 bytenr, u64 num_bytes, u64 parent,
1335 u64 root_objectid, u64 owner, u64 offset)
1336{
1337 int ret;
1338
1339 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1340 bytenr, num_bytes, parent,
1341 root_objectid, owner, offset, 0);
1342 if (ret != -ENOENT)
1343 return ret;
1344
1345 btrfs_release_path(root, path);
1346 *ref_ret = NULL;
1347
1348 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1349 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1350 root_objectid);
1351 } else {
1352 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1353 root_objectid, owner, offset);
1354 }
1355 return ret;
1356}
1357
1358/*
1359 * helper to update/remove inline back ref
1360 */
1361static noinline_for_stack
1362int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1363 struct btrfs_root *root,
1364 struct btrfs_path *path,
1365 struct btrfs_extent_inline_ref *iref,
1366 int refs_to_mod,
1367 struct btrfs_delayed_extent_op *extent_op)
1368{
1369 struct extent_buffer *leaf;
1370 struct btrfs_extent_item *ei;
1371 struct btrfs_extent_data_ref *dref = NULL;
1372 struct btrfs_shared_data_ref *sref = NULL;
1373 unsigned long ptr;
1374 unsigned long end;
1375 u32 item_size;
1376 int size;
1377 int type;
1378 int ret;
1379 u64 refs;
1380
1381 leaf = path->nodes[0];
1382 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1383 refs = btrfs_extent_refs(leaf, ei);
1384 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1385 refs += refs_to_mod;
1386 btrfs_set_extent_refs(leaf, ei, refs);
1387 if (extent_op)
1388 __run_delayed_extent_op(extent_op, leaf, ei);
1389
1390 type = btrfs_extent_inline_ref_type(leaf, iref);
1391
1392 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1393 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1394 refs = btrfs_extent_data_ref_count(leaf, dref);
1395 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1396 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1397 refs = btrfs_shared_data_ref_count(leaf, sref);
1398 } else {
1399 refs = 1;
1400 BUG_ON(refs_to_mod != -1);
1401 }
1402
1403 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1404 refs += refs_to_mod;
1405
1406 if (refs > 0) {
1407 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1408 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1409 else
1410 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1411 } else {
1412 size = btrfs_extent_inline_ref_size(type);
1413 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1414 ptr = (unsigned long)iref;
1415 end = (unsigned long)ei + item_size;
1416 if (ptr + size < end)
1417 memmove_extent_buffer(leaf, ptr, ptr + size,
1418 end - ptr - size);
1419 item_size -= size;
1420 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1421 BUG_ON(ret);
1422 }
1423 btrfs_mark_buffer_dirty(leaf);
1424 return 0;
1425}
1426
1427static noinline_for_stack
1428int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1429 struct btrfs_root *root,
1430 struct btrfs_path *path,
1431 u64 bytenr, u64 num_bytes, u64 parent,
1432 u64 root_objectid, u64 owner,
1433 u64 offset, int refs_to_add,
1434 struct btrfs_delayed_extent_op *extent_op)
1435{
1436 struct btrfs_extent_inline_ref *iref;
1437 int ret;
1438
1439 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1440 bytenr, num_bytes, parent,
1441 root_objectid, owner, offset, 1);
1442 if (ret == 0) {
1443 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1444 ret = update_inline_extent_backref(trans, root, path, iref,
1445 refs_to_add, extent_op);
1446 } else if (ret == -ENOENT) {
1447 ret = setup_inline_extent_backref(trans, root, path, iref,
1448 parent, root_objectid,
1449 owner, offset, refs_to_add,
1450 extent_op);
1451 }
1452 return ret;
1453}
1454
1455static int insert_extent_backref(struct btrfs_trans_handle *trans,
1456 struct btrfs_root *root,
1457 struct btrfs_path *path,
1458 u64 bytenr, u64 parent, u64 root_objectid,
1459 u64 owner, u64 offset, int refs_to_add)
1460{
1461 int ret;
1462 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1463 BUG_ON(refs_to_add != 1);
1464 ret = insert_tree_block_ref(trans, root, path, bytenr,
1465 parent, root_objectid);
1466 } else {
1467 ret = insert_extent_data_ref(trans, root, path, bytenr,
1468 parent, root_objectid,
1469 owner, offset, refs_to_add);
1470 }
1471 return ret;
1472}
1473
1474static int remove_extent_backref(struct btrfs_trans_handle *trans,
1475 struct btrfs_root *root,
1476 struct btrfs_path *path,
1477 struct btrfs_extent_inline_ref *iref,
1478 int refs_to_drop, int is_data)
1479{
1480 int ret;
1481
1482 BUG_ON(!is_data && refs_to_drop != 1);
1483 if (iref) {
1484 ret = update_inline_extent_backref(trans, root, path, iref,
1485 -refs_to_drop, NULL);
1486 } else if (is_data) {
1487 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1488 } else {
1489 ret = btrfs_del_item(trans, root, path);
1490 }
1491 return ret;
1492}
1493
Chris Mason4b4e25f2008-11-20 10:22:27 -05001494#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001495static void btrfs_issue_discard(struct block_device *bdev,
1496 u64 start, u64 len)
1497{
Chris Mason15916de2008-11-19 21:17:22 -05001498 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -05001499}
Chris Mason4b4e25f2008-11-20 10:22:27 -05001500#endif
Chris Mason15916de2008-11-19 21:17:22 -05001501
Liu Hui1f3c79a2009-01-05 15:57:51 -05001502static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1503 u64 num_bytes)
1504{
1505#ifdef BIO_RW_DISCARD
1506 int ret;
1507 u64 map_length = num_bytes;
1508 struct btrfs_multi_bio *multi = NULL;
1509
1510 /* Tell the block device(s) that the sectors can be discarded */
1511 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1512 bytenr, &map_length, &multi, 0);
1513 if (!ret) {
1514 struct btrfs_bio_stripe *stripe = multi->stripes;
1515 int i;
1516
1517 if (map_length > num_bytes)
1518 map_length = num_bytes;
1519
1520 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1521 btrfs_issue_discard(stripe->dev->bdev,
1522 stripe->physical,
1523 map_length);
1524 }
1525 kfree(multi);
1526 }
1527
1528 return ret;
1529#else
1530 return 0;
1531#endif
1532}
1533
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001534int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1535 struct btrfs_root *root,
1536 u64 bytenr, u64 num_bytes, u64 parent,
1537 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001538{
1539 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001540 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1541 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001542
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001543 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1544 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1545 parent, root_objectid, (int)owner,
1546 BTRFS_ADD_DELAYED_REF, NULL);
1547 } else {
1548 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1549 parent, root_objectid, owner, offset,
1550 BTRFS_ADD_DELAYED_REF, NULL);
1551 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001552 return ret;
1553}
1554
Chris Mason925baed2008-06-25 16:01:30 -04001555static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001556 struct btrfs_root *root,
1557 u64 bytenr, u64 num_bytes,
1558 u64 parent, u64 root_objectid,
1559 u64 owner, u64 offset, int refs_to_add,
1560 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001561{
Chris Mason5caf2a02007-04-02 11:20:42 -04001562 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001563 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001564 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001565 u64 refs;
1566 int ret;
1567 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001568
Chris Mason5caf2a02007-04-02 11:20:42 -04001569 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001570 if (!path)
1571 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001572
Chris Mason3c12ac72008-04-21 12:01:38 -04001573 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001574 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001575 /* this will setup the path even if it fails to insert the back ref */
1576 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1577 path, bytenr, num_bytes, parent,
1578 root_objectid, owner, offset,
1579 refs_to_add, extent_op);
1580 if (ret == 0)
1581 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001582
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001583 if (ret != -EAGAIN) {
1584 err = ret;
1585 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001586 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001587
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001588 leaf = path->nodes[0];
1589 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1590 refs = btrfs_extent_refs(leaf, item);
1591 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1592 if (extent_op)
1593 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001594
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001595 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001596 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001597
Chris Mason3c12ac72008-04-21 12:01:38 -04001598 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001599 path->leave_spinning = 1;
1600
Chris Mason56bec292009-03-13 10:10:06 -04001601 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001602 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001603 path, bytenr, parent, root_objectid,
1604 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001605 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001606out:
Chris Mason74493f72007-12-11 09:25:06 -05001607 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001608 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001609}
1610
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001611static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1612 struct btrfs_root *root,
1613 struct btrfs_delayed_ref_node *node,
1614 struct btrfs_delayed_extent_op *extent_op,
1615 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001616{
Chris Mason56bec292009-03-13 10:10:06 -04001617 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001618 struct btrfs_delayed_data_ref *ref;
1619 struct btrfs_key ins;
1620 u64 parent = 0;
1621 u64 ref_root = 0;
1622 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001623
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001624 ins.objectid = node->bytenr;
1625 ins.offset = node->num_bytes;
1626 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001627
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001628 ref = btrfs_delayed_node_to_data_ref(node);
1629 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1630 parent = ref->parent;
1631 else
1632 ref_root = ref->root;
1633
1634 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1635 if (extent_op) {
1636 BUG_ON(extent_op->update_key);
1637 flags |= extent_op->flags_to_set;
1638 }
1639 ret = alloc_reserved_file_extent(trans, root,
1640 parent, ref_root, flags,
1641 ref->objectid, ref->offset,
1642 &ins, node->ref_mod);
1643 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1644 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1645 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1646 node->num_bytes, parent,
1647 ref_root, ref->objectid,
1648 ref->offset, node->ref_mod,
1649 extent_op);
1650 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1651 ret = __btrfs_free_extent(trans, root, node->bytenr,
1652 node->num_bytes, parent,
1653 ref_root, ref->objectid,
1654 ref->offset, node->ref_mod,
1655 extent_op);
1656 } else {
1657 BUG();
1658 }
Chris Mason56bec292009-03-13 10:10:06 -04001659 return ret;
1660}
1661
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001662static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1663 struct extent_buffer *leaf,
1664 struct btrfs_extent_item *ei)
1665{
1666 u64 flags = btrfs_extent_flags(leaf, ei);
1667 if (extent_op->update_flags) {
1668 flags |= extent_op->flags_to_set;
1669 btrfs_set_extent_flags(leaf, ei, flags);
1670 }
1671
1672 if (extent_op->update_key) {
1673 struct btrfs_tree_block_info *bi;
1674 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1675 bi = (struct btrfs_tree_block_info *)(ei + 1);
1676 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1677 }
1678}
1679
1680static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1681 struct btrfs_root *root,
1682 struct btrfs_delayed_ref_node *node,
1683 struct btrfs_delayed_extent_op *extent_op)
1684{
1685 struct btrfs_key key;
1686 struct btrfs_path *path;
1687 struct btrfs_extent_item *ei;
1688 struct extent_buffer *leaf;
1689 u32 item_size;
1690 int ret;
1691 int err = 0;
1692
1693 path = btrfs_alloc_path();
1694 if (!path)
1695 return -ENOMEM;
1696
1697 key.objectid = node->bytenr;
1698 key.type = BTRFS_EXTENT_ITEM_KEY;
1699 key.offset = node->num_bytes;
1700
1701 path->reada = 1;
1702 path->leave_spinning = 1;
1703 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1704 path, 0, 1);
1705 if (ret < 0) {
1706 err = ret;
1707 goto out;
1708 }
1709 if (ret > 0) {
1710 err = -EIO;
1711 goto out;
1712 }
1713
1714 leaf = path->nodes[0];
1715 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1716#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1717 if (item_size < sizeof(*ei)) {
1718 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1719 path, (u64)-1, 0);
1720 if (ret < 0) {
1721 err = ret;
1722 goto out;
1723 }
1724 leaf = path->nodes[0];
1725 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1726 }
1727#endif
1728 BUG_ON(item_size < sizeof(*ei));
1729 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1730 __run_delayed_extent_op(extent_op, leaf, ei);
1731
1732 btrfs_mark_buffer_dirty(leaf);
1733out:
1734 btrfs_free_path(path);
1735 return err;
1736}
1737
1738static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1739 struct btrfs_root *root,
1740 struct btrfs_delayed_ref_node *node,
1741 struct btrfs_delayed_extent_op *extent_op,
1742 int insert_reserved)
1743{
1744 int ret = 0;
1745 struct btrfs_delayed_tree_ref *ref;
1746 struct btrfs_key ins;
1747 u64 parent = 0;
1748 u64 ref_root = 0;
1749
1750 ins.objectid = node->bytenr;
1751 ins.offset = node->num_bytes;
1752 ins.type = BTRFS_EXTENT_ITEM_KEY;
1753
1754 ref = btrfs_delayed_node_to_tree_ref(node);
1755 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1756 parent = ref->parent;
1757 else
1758 ref_root = ref->root;
1759
1760 BUG_ON(node->ref_mod != 1);
1761 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1762 BUG_ON(!extent_op || !extent_op->update_flags ||
1763 !extent_op->update_key);
1764 ret = alloc_reserved_tree_block(trans, root,
1765 parent, ref_root,
1766 extent_op->flags_to_set,
1767 &extent_op->key,
1768 ref->level, &ins);
1769 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1770 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1771 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1772 node->num_bytes, parent, ref_root,
1773 ref->level, 0, 1, extent_op);
1774 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1775 ret = __btrfs_free_extent(trans, root, node->bytenr,
1776 node->num_bytes, parent, ref_root,
1777 ref->level, 0, 1, extent_op);
1778 } else {
1779 BUG();
1780 }
1781 return ret;
1782}
1783
1784
Chris Mason56bec292009-03-13 10:10:06 -04001785/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001786static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1787 struct btrfs_root *root,
1788 struct btrfs_delayed_ref_node *node,
1789 struct btrfs_delayed_extent_op *extent_op,
1790 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001791{
Josef Bacikeb099672009-02-12 09:27:38 -05001792 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001793 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001794 struct btrfs_delayed_ref_head *head;
1795 /*
1796 * we've hit the end of the chain and we were supposed
1797 * to insert this extent into the tree. But, it got
1798 * deleted before we ever needed to insert it, so all
1799 * we have to do is clean up the accounting
1800 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001801 BUG_ON(extent_op);
1802 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001803 if (insert_reserved) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001804 if (head->is_data) {
1805 ret = btrfs_del_csums(trans, root,
1806 node->bytenr,
1807 node->num_bytes);
1808 BUG_ON(ret);
1809 }
1810 btrfs_update_pinned_extents(root, node->bytenr,
Josef Bacik68b38552009-07-27 13:57:01 -04001811 node->num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04001812 update_reserved_extents(root, node->bytenr,
1813 node->num_bytes, 0);
1814 }
Chris Mason56bec292009-03-13 10:10:06 -04001815 mutex_unlock(&head->mutex);
1816 return 0;
1817 }
Josef Bacikeb099672009-02-12 09:27:38 -05001818
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001819 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1820 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1821 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1822 insert_reserved);
1823 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1824 node->type == BTRFS_SHARED_DATA_REF_KEY)
1825 ret = run_delayed_data_ref(trans, root, node, extent_op,
1826 insert_reserved);
1827 else
1828 BUG();
1829 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001830}
1831
Chris Mason56bec292009-03-13 10:10:06 -04001832static noinline struct btrfs_delayed_ref_node *
1833select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001834{
Chris Mason56bec292009-03-13 10:10:06 -04001835 struct rb_node *node;
1836 struct btrfs_delayed_ref_node *ref;
1837 int action = BTRFS_ADD_DELAYED_REF;
1838again:
1839 /*
1840 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1841 * this prevents ref count from going down to zero when
1842 * there still are pending delayed ref.
1843 */
1844 node = rb_prev(&head->node.rb_node);
1845 while (1) {
1846 if (!node)
1847 break;
1848 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1849 rb_node);
1850 if (ref->bytenr != head->node.bytenr)
1851 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001852 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001853 return ref;
1854 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001855 }
Chris Mason56bec292009-03-13 10:10:06 -04001856 if (action == BTRFS_ADD_DELAYED_REF) {
1857 action = BTRFS_DROP_DELAYED_REF;
1858 goto again;
1859 }
1860 return NULL;
1861}
1862
Chris Masonc3e69d52009-03-13 10:17:05 -04001863static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1864 struct btrfs_root *root,
1865 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001866{
Chris Mason56bec292009-03-13 10:10:06 -04001867 struct btrfs_delayed_ref_root *delayed_refs;
1868 struct btrfs_delayed_ref_node *ref;
1869 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001871 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001872 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001873 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001874
1875 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001876 while (1) {
1877 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001878 /* pick a new head ref from the cluster list */
1879 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001880 break;
Chris Mason56bec292009-03-13 10:10:06 -04001881
Chris Masonc3e69d52009-03-13 10:17:05 -04001882 locked_ref = list_entry(cluster->next,
1883 struct btrfs_delayed_ref_head, cluster);
1884
1885 /* grab the lock that says we are going to process
1886 * all the refs for this head */
1887 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1888
1889 /*
1890 * we may have dropped the spin lock to get the head
1891 * mutex lock, and that might have given someone else
1892 * time to free the head. If that's true, it has been
1893 * removed from our list and we can move on.
1894 */
1895 if (ret == -EAGAIN) {
1896 locked_ref = NULL;
1897 count++;
1898 continue;
Chris Mason56bec292009-03-13 10:10:06 -04001899 }
1900 }
1901
1902 /*
1903 * record the must insert reserved flag before we
1904 * drop the spin lock.
1905 */
1906 must_insert_reserved = locked_ref->must_insert_reserved;
1907 locked_ref->must_insert_reserved = 0;
1908
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001909 extent_op = locked_ref->extent_op;
1910 locked_ref->extent_op = NULL;
1911
Chris Mason56bec292009-03-13 10:10:06 -04001912 /*
1913 * locked_ref is the head node, so we have to go one
1914 * node back for any delayed ref updates
1915 */
Chris Mason56bec292009-03-13 10:10:06 -04001916 ref = select_delayed_ref(locked_ref);
1917 if (!ref) {
1918 /* All delayed refs have been processed, Go ahead
1919 * and send the head node to run_one_delayed_ref,
1920 * so that any accounting fixes can happen
1921 */
1922 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001923
1924 if (extent_op && must_insert_reserved) {
1925 kfree(extent_op);
1926 extent_op = NULL;
1927 }
1928
1929 if (extent_op) {
1930 spin_unlock(&delayed_refs->lock);
1931
1932 ret = run_delayed_extent_op(trans, root,
1933 ref, extent_op);
1934 BUG_ON(ret);
1935 kfree(extent_op);
1936
1937 cond_resched();
1938 spin_lock(&delayed_refs->lock);
1939 continue;
1940 }
1941
Chris Masonc3e69d52009-03-13 10:17:05 -04001942 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04001943 locked_ref = NULL;
1944 }
1945
1946 ref->in_tree = 0;
1947 rb_erase(&ref->rb_node, &delayed_refs->root);
1948 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001949
Chris Mason56bec292009-03-13 10:10:06 -04001950 spin_unlock(&delayed_refs->lock);
1951
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001952 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04001953 must_insert_reserved);
1954 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04001955
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001956 btrfs_put_delayed_ref(ref);
1957 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04001958 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001959
Chris Mason1887be62009-03-13 10:11:24 -04001960 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001961 spin_lock(&delayed_refs->lock);
1962 }
Chris Masonc3e69d52009-03-13 10:17:05 -04001963 return count;
1964}
1965
1966/*
1967 * this starts processing the delayed reference count updates and
1968 * extent insertions we have queued up so far. count can be
1969 * 0, which means to process everything in the tree at the start
1970 * of the run (but not newly added entries), or it can be some target
1971 * number you'd like to process.
1972 */
1973int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1974 struct btrfs_root *root, unsigned long count)
1975{
1976 struct rb_node *node;
1977 struct btrfs_delayed_ref_root *delayed_refs;
1978 struct btrfs_delayed_ref_node *ref;
1979 struct list_head cluster;
1980 int ret;
1981 int run_all = count == (unsigned long)-1;
1982 int run_most = 0;
1983
1984 if (root == root->fs_info->extent_root)
1985 root = root->fs_info->tree_root;
1986
1987 delayed_refs = &trans->transaction->delayed_refs;
1988 INIT_LIST_HEAD(&cluster);
1989again:
1990 spin_lock(&delayed_refs->lock);
1991 if (count == 0) {
1992 count = delayed_refs->num_entries * 2;
1993 run_most = 1;
1994 }
1995 while (1) {
1996 if (!(run_all || run_most) &&
1997 delayed_refs->num_heads_ready < 64)
1998 break;
1999
2000 /*
2001 * go find something we can process in the rbtree. We start at
2002 * the beginning of the tree, and then build a cluster
2003 * of refs to process starting at the first one we are able to
2004 * lock
2005 */
2006 ret = btrfs_find_ref_cluster(trans, &cluster,
2007 delayed_refs->run_delayed_start);
2008 if (ret)
2009 break;
2010
2011 ret = run_clustered_refs(trans, root, &cluster);
2012 BUG_ON(ret < 0);
2013
2014 count -= min_t(unsigned long, ret, count);
2015
2016 if (count == 0)
2017 break;
2018 }
2019
Chris Mason56bec292009-03-13 10:10:06 -04002020 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002021 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002022 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002023 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002024 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002025
2026 while (node) {
2027 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2028 rb_node);
2029 if (btrfs_delayed_ref_is_head(ref)) {
2030 struct btrfs_delayed_ref_head *head;
2031
2032 head = btrfs_delayed_node_to_head(ref);
2033 atomic_inc(&ref->refs);
2034
2035 spin_unlock(&delayed_refs->lock);
2036 mutex_lock(&head->mutex);
2037 mutex_unlock(&head->mutex);
2038
2039 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002040 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002041 goto again;
2042 }
2043 node = rb_next(node);
2044 }
2045 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002046 schedule_timeout(1);
2047 goto again;
2048 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002049out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002050 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002051 return 0;
2052}
2053
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002054int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2055 struct btrfs_root *root,
2056 u64 bytenr, u64 num_bytes, u64 flags,
2057 int is_data)
2058{
2059 struct btrfs_delayed_extent_op *extent_op;
2060 int ret;
2061
2062 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2063 if (!extent_op)
2064 return -ENOMEM;
2065
2066 extent_op->flags_to_set = flags;
2067 extent_op->update_flags = 1;
2068 extent_op->update_key = 0;
2069 extent_op->is_data = is_data ? 1 : 0;
2070
2071 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2072 if (ret)
2073 kfree(extent_op);
2074 return ret;
2075}
2076
2077static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2078 struct btrfs_root *root,
2079 struct btrfs_path *path,
2080 u64 objectid, u64 offset, u64 bytenr)
2081{
2082 struct btrfs_delayed_ref_head *head;
2083 struct btrfs_delayed_ref_node *ref;
2084 struct btrfs_delayed_data_ref *data_ref;
2085 struct btrfs_delayed_ref_root *delayed_refs;
2086 struct rb_node *node;
2087 int ret = 0;
2088
2089 ret = -ENOENT;
2090 delayed_refs = &trans->transaction->delayed_refs;
2091 spin_lock(&delayed_refs->lock);
2092 head = btrfs_find_delayed_ref_head(trans, bytenr);
2093 if (!head)
2094 goto out;
2095
2096 if (!mutex_trylock(&head->mutex)) {
2097 atomic_inc(&head->node.refs);
2098 spin_unlock(&delayed_refs->lock);
2099
2100 btrfs_release_path(root->fs_info->extent_root, path);
2101
2102 mutex_lock(&head->mutex);
2103 mutex_unlock(&head->mutex);
2104 btrfs_put_delayed_ref(&head->node);
2105 return -EAGAIN;
2106 }
2107
2108 node = rb_prev(&head->node.rb_node);
2109 if (!node)
2110 goto out_unlock;
2111
2112 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2113
2114 if (ref->bytenr != bytenr)
2115 goto out_unlock;
2116
2117 ret = 1;
2118 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2119 goto out_unlock;
2120
2121 data_ref = btrfs_delayed_node_to_data_ref(ref);
2122
2123 node = rb_prev(node);
2124 if (node) {
2125 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2126 if (ref->bytenr == bytenr)
2127 goto out_unlock;
2128 }
2129
2130 if (data_ref->root != root->root_key.objectid ||
2131 data_ref->objectid != objectid || data_ref->offset != offset)
2132 goto out_unlock;
2133
2134 ret = 0;
2135out_unlock:
2136 mutex_unlock(&head->mutex);
2137out:
2138 spin_unlock(&delayed_refs->lock);
2139 return ret;
2140}
2141
2142static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2143 struct btrfs_root *root,
2144 struct btrfs_path *path,
2145 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002146{
2147 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002148 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002149 struct btrfs_extent_data_ref *ref;
2150 struct btrfs_extent_inline_ref *iref;
2151 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002152 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002153 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002154 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002155
Chris Masonbe20aa92007-12-17 20:14:01 -05002156 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002157 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002158 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002159
Chris Masonbe20aa92007-12-17 20:14:01 -05002160 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2161 if (ret < 0)
2162 goto out;
2163 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002164
2165 ret = -ENOENT;
2166 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002167 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002168
Zheng Yan31840ae2008-09-23 13:14:14 -04002169 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002170 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002171 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002172
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002173 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002174 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002175
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002176 ret = 1;
2177 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2178#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2179 if (item_size < sizeof(*ei)) {
2180 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2181 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002182 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002183#endif
2184 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2185
2186 if (item_size != sizeof(*ei) +
2187 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2188 goto out;
2189
2190 if (btrfs_extent_generation(leaf, ei) <=
2191 btrfs_root_last_snapshot(&root->root_item))
2192 goto out;
2193
2194 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2195 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2196 BTRFS_EXTENT_DATA_REF_KEY)
2197 goto out;
2198
2199 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2200 if (btrfs_extent_refs(leaf, ei) !=
2201 btrfs_extent_data_ref_count(leaf, ref) ||
2202 btrfs_extent_data_ref_root(leaf, ref) !=
2203 root->root_key.objectid ||
2204 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2205 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2206 goto out;
2207
Yan Zhengf321e492008-07-30 09:26:11 -04002208 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002209out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002210 return ret;
2211}
2212
2213int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2214 struct btrfs_root *root,
2215 u64 objectid, u64 offset, u64 bytenr)
2216{
2217 struct btrfs_path *path;
2218 int ret;
2219 int ret2;
2220
2221 path = btrfs_alloc_path();
2222 if (!path)
2223 return -ENOENT;
2224
2225 do {
2226 ret = check_committed_ref(trans, root, path, objectid,
2227 offset, bytenr);
2228 if (ret && ret != -ENOENT)
2229 goto out;
2230
2231 ret2 = check_delayed_ref(trans, root, path, objectid,
2232 offset, bytenr);
2233 } while (ret2 == -EAGAIN);
2234
2235 if (ret2 && ret2 != -ENOENT) {
2236 ret = ret2;
2237 goto out;
2238 }
2239
2240 if (ret != -ENOENT || ret2 != -ENOENT)
2241 ret = 0;
2242out:
Yan Zhengf321e492008-07-30 09:26:11 -04002243 btrfs_free_path(path);
2244 return ret;
2245}
2246
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002247#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002248int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2249 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002250{
Chris Mason5f39d392007-10-15 16:14:19 -04002251 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002252 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002253 u64 root_gen;
2254 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002255 int i;
Chris Masondb945352007-10-15 16:15:53 -04002256 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002257 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002258 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002259
Chris Mason3768f362007-03-13 16:47:54 -04002260 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002261 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002262
Zheng Yane4657682008-09-26 10:04:53 -04002263 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2264 shared = 0;
2265 root_gen = root->root_key.offset;
2266 } else {
2267 shared = 1;
2268 root_gen = trans->transid - 1;
2269 }
2270
Chris Masondb945352007-10-15 16:15:53 -04002271 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002272 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002273
Zheng Yan31840ae2008-09-23 13:14:14 -04002274 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002275 struct btrfs_leaf_ref *ref;
2276 struct btrfs_extent_info *info;
2277
Zheng Yan31840ae2008-09-23 13:14:14 -04002278 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002279 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002280 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002281 goto out;
2282 }
2283
Zheng Yane4657682008-09-26 10:04:53 -04002284 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002285 ref->bytenr = buf->start;
2286 ref->owner = btrfs_header_owner(buf);
2287 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002288 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002289 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002290
Zheng Yan31840ae2008-09-23 13:14:14 -04002291 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002292 u64 disk_bytenr;
2293 btrfs_item_key_to_cpu(buf, &key, i);
2294 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2295 continue;
2296 fi = btrfs_item_ptr(buf, i,
2297 struct btrfs_file_extent_item);
2298 if (btrfs_file_extent_type(buf, fi) ==
2299 BTRFS_FILE_EXTENT_INLINE)
2300 continue;
2301 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2302 if (disk_bytenr == 0)
2303 continue;
2304
2305 info->bytenr = disk_bytenr;
2306 info->num_bytes =
2307 btrfs_file_extent_disk_num_bytes(buf, fi);
2308 info->objectid = key.objectid;
2309 info->offset = key.offset;
2310 info++;
2311 }
2312
Zheng Yane4657682008-09-26 10:04:53 -04002313 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002314 if (ret == -EEXIST && shared) {
2315 struct btrfs_leaf_ref *old;
2316 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2317 BUG_ON(!old);
2318 btrfs_remove_leaf_ref(root, old);
2319 btrfs_free_leaf_ref(root, old);
2320 ret = btrfs_add_leaf_ref(root, ref, shared);
2321 }
Yan Zheng31153d82008-07-28 15:32:19 -04002322 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002323 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002324 }
2325out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002326 return ret;
2327}
2328
Chris Masonb7a9f292009-02-04 09:23:45 -05002329/* when a block goes through cow, we update the reference counts of
2330 * everything that block points to. The internal pointers of the block
2331 * can be in just about any order, and it is likely to have clusters of
2332 * things that are close together and clusters of things that are not.
2333 *
2334 * To help reduce the seeks that come with updating all of these reference
2335 * counts, sort them by byte number before actual updates are done.
2336 *
2337 * struct refsort is used to match byte number to slot in the btree block.
2338 * we sort based on the byte number and then use the slot to actually
2339 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002340 *
2341 * struct refsort is smaller than strcut btrfs_item and smaller than
2342 * struct btrfs_key_ptr. Since we're currently limited to the page size
2343 * for a btree block, there's no way for a kmalloc of refsorts for a
2344 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002345 */
2346struct refsort {
2347 u64 bytenr;
2348 u32 slot;
2349};
2350
2351/*
2352 * for passing into sort()
2353 */
2354static int refsort_cmp(const void *a_void, const void *b_void)
2355{
2356 const struct refsort *a = a_void;
2357 const struct refsort *b = b_void;
2358
2359 if (a->bytenr < b->bytenr)
2360 return -1;
2361 if (a->bytenr > b->bytenr)
2362 return 1;
2363 return 0;
2364}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002365#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002366
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002367static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002368 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002369 struct extent_buffer *buf,
2370 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002371{
2372 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002373 u64 num_bytes;
2374 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002375 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002376 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002377 struct btrfs_key key;
2378 struct btrfs_file_extent_item *fi;
2379 int i;
2380 int level;
2381 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002382 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002383 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002384
2385 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002386 nritems = btrfs_header_nritems(buf);
2387 level = btrfs_header_level(buf);
2388
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002389 if (!root->ref_cows && level == 0)
2390 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002391
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002392 if (inc)
2393 process_func = btrfs_inc_extent_ref;
2394 else
2395 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002396
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002397 if (full_backref)
2398 parent = buf->start;
2399 else
2400 parent = 0;
2401
Zheng Yan31840ae2008-09-23 13:14:14 -04002402 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002403 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002404 btrfs_item_key_to_cpu(buf, &key, i);
2405 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002406 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002407 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002408 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002409 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002410 BTRFS_FILE_EXTENT_INLINE)
2411 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002412 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2413 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002414 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002415
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002416 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2417 key.offset -= btrfs_file_extent_offset(buf, fi);
2418 ret = process_func(trans, root, bytenr, num_bytes,
2419 parent, ref_root, key.objectid,
2420 key.offset);
2421 if (ret)
2422 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002423 } else {
2424 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002425 num_bytes = btrfs_level_size(root, level - 1);
2426 ret = process_func(trans, root, bytenr, num_bytes,
2427 parent, ref_root, level - 1, 0);
2428 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002429 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002430 }
2431 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002432 return 0;
2433fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002434 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002435 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002436}
2437
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002438int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2439 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002440{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002441 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2442}
Zheng Yan31840ae2008-09-23 13:14:14 -04002443
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002444int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2445 struct extent_buffer *buf, int full_backref)
2446{
2447 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002448}
2449
Chris Mason9078a3e2007-04-26 16:46:15 -04002450static int write_one_cache_group(struct btrfs_trans_handle *trans,
2451 struct btrfs_root *root,
2452 struct btrfs_path *path,
2453 struct btrfs_block_group_cache *cache)
2454{
2455 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002456 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002457 unsigned long bi;
2458 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002459
Chris Mason9078a3e2007-04-26 16:46:15 -04002460 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002461 if (ret < 0)
2462 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002463 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002464
2465 leaf = path->nodes[0];
2466 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2467 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2468 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002469 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002470fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002471 if (ret)
2472 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002473 return 0;
2474
2475}
2476
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002477static struct btrfs_block_group_cache *
2478next_block_group(struct btrfs_root *root,
2479 struct btrfs_block_group_cache *cache)
2480{
2481 struct rb_node *node;
2482 spin_lock(&root->fs_info->block_group_cache_lock);
2483 node = rb_next(&cache->cache_node);
2484 btrfs_put_block_group(cache);
2485 if (node) {
2486 cache = rb_entry(node, struct btrfs_block_group_cache,
2487 cache_node);
2488 atomic_inc(&cache->count);
2489 } else
2490 cache = NULL;
2491 spin_unlock(&root->fs_info->block_group_cache_lock);
2492 return cache;
2493}
2494
Chris Mason96b51792007-10-15 16:15:19 -04002495int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2496 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002497{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002498 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002499 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002500 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002501 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002502
2503 path = btrfs_alloc_path();
2504 if (!path)
2505 return -ENOMEM;
2506
Chris Masond3977122009-01-05 21:25:51 -05002507 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002508 if (last == 0) {
2509 err = btrfs_run_delayed_refs(trans, root,
2510 (unsigned long)-1);
2511 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002512 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002513
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002514 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2515 while (cache) {
2516 if (cache->dirty)
2517 break;
2518 cache = next_block_group(root, cache);
2519 }
2520 if (!cache) {
2521 if (last == 0)
2522 break;
2523 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002524 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002525 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002526
2527 cache->dirty = 0;
2528 last = cache->key.objectid + cache->key.offset;
2529
2530 err = write_one_cache_group(trans, root, path, cache);
2531 BUG_ON(err);
2532 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002533 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002534
Chris Mason9078a3e2007-04-26 16:46:15 -04002535 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002536 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002537}
2538
Yan Zhengd2fb3432008-12-11 16:30:39 -05002539int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2540{
2541 struct btrfs_block_group_cache *block_group;
2542 int readonly = 0;
2543
2544 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2545 if (!block_group || block_group->ro)
2546 readonly = 1;
2547 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002548 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002549 return readonly;
2550}
2551
Chris Mason593060d2008-03-25 16:50:33 -04002552static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2553 u64 total_bytes, u64 bytes_used,
2554 struct btrfs_space_info **space_info)
2555{
2556 struct btrfs_space_info *found;
2557
2558 found = __find_space_info(info, flags);
2559 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002560 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002561 found->total_bytes += total_bytes;
2562 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002563 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002564 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002565 *space_info = found;
2566 return 0;
2567 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002568 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002569 if (!found)
2570 return -ENOMEM;
2571
Josef Bacik0f9dd462008-09-23 13:14:11 -04002572 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002573 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002574 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002575 found->flags = flags;
2576 found->total_bytes = total_bytes;
2577 found->bytes_used = bytes_used;
2578 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002579 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002580 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002581 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002582 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002583 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002584 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002585 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002586 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002587 return 0;
2588}
2589
Chris Mason8790d502008-04-03 16:29:03 -04002590static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2591{
2592 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002593 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002594 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002595 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002596 if (extra_flags) {
2597 if (flags & BTRFS_BLOCK_GROUP_DATA)
2598 fs_info->avail_data_alloc_bits |= extra_flags;
2599 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2600 fs_info->avail_metadata_alloc_bits |= extra_flags;
2601 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2602 fs_info->avail_system_alloc_bits |= extra_flags;
2603 }
2604}
Chris Mason593060d2008-03-25 16:50:33 -04002605
Yan Zhengc146afa2008-11-12 14:34:12 -05002606static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2607{
2608 spin_lock(&cache->space_info->lock);
2609 spin_lock(&cache->lock);
2610 if (!cache->ro) {
2611 cache->space_info->bytes_readonly += cache->key.offset -
2612 btrfs_block_group_used(&cache->item);
2613 cache->ro = 1;
2614 }
2615 spin_unlock(&cache->lock);
2616 spin_unlock(&cache->space_info->lock);
2617}
2618
Yan Zheng2b820322008-11-17 21:11:30 -05002619u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002620{
Yan Zheng2b820322008-11-17 21:11:30 -05002621 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002622
2623 if (num_devices == 1)
2624 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2625 if (num_devices < 4)
2626 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2627
Chris Masonec44a352008-04-28 15:29:52 -04002628 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2629 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002630 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002631 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002632 }
Chris Masonec44a352008-04-28 15:29:52 -04002633
2634 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002635 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002636 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002637 }
Chris Masonec44a352008-04-28 15:29:52 -04002638
2639 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2640 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2641 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2642 (flags & BTRFS_BLOCK_GROUP_DUP)))
2643 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2644 return flags;
2645}
2646
Josef Bacik6a632092009-02-20 11:00:09 -05002647static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2648{
2649 struct btrfs_fs_info *info = root->fs_info;
2650 u64 alloc_profile;
2651
2652 if (data) {
2653 alloc_profile = info->avail_data_alloc_bits &
2654 info->data_alloc_profile;
2655 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2656 } else if (root == root->fs_info->chunk_root) {
2657 alloc_profile = info->avail_system_alloc_bits &
2658 info->system_alloc_profile;
2659 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2660 } else {
2661 alloc_profile = info->avail_metadata_alloc_bits &
2662 info->metadata_alloc_profile;
2663 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2664 }
2665
2666 return btrfs_reduce_alloc_profile(root, data);
2667}
2668
2669void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2670{
2671 u64 alloc_target;
2672
2673 alloc_target = btrfs_get_alloc_profile(root, 1);
2674 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2675 alloc_target);
2676}
2677
2678/*
2679 * for now this just makes sure we have at least 5% of our metadata space free
2680 * for use.
2681 */
2682int btrfs_check_metadata_free_space(struct btrfs_root *root)
2683{
2684 struct btrfs_fs_info *info = root->fs_info;
2685 struct btrfs_space_info *meta_sinfo;
2686 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002687 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002688
2689 /* get the space info for where the metadata will live */
2690 alloc_target = btrfs_get_alloc_profile(root, 0);
2691 meta_sinfo = __find_space_info(info, alloc_target);
2692
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002693again:
Josef Bacik6a632092009-02-20 11:00:09 -05002694 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002695 if (!meta_sinfo->full)
2696 thresh = meta_sinfo->total_bytes * 80;
2697 else
2698 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05002699
2700 do_div(thresh, 100);
2701
2702 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2703 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002704 struct btrfs_trans_handle *trans;
2705 if (!meta_sinfo->full) {
2706 meta_sinfo->force_alloc = 1;
2707 spin_unlock(&meta_sinfo->lock);
2708
2709 trans = btrfs_start_transaction(root, 1);
2710 if (!trans)
2711 return -ENOMEM;
2712
2713 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2714 2 * 1024 * 1024, alloc_target, 0);
2715 btrfs_end_transaction(trans, root);
2716 goto again;
2717 }
Josef Bacik6a632092009-02-20 11:00:09 -05002718 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002719
2720 if (!committed) {
2721 committed = 1;
2722 trans = btrfs_join_transaction(root, 1);
2723 if (!trans)
2724 return -ENOMEM;
2725 ret = btrfs_commit_transaction(trans, root);
2726 if (ret)
2727 return ret;
2728 goto again;
2729 }
Josef Bacik6a632092009-02-20 11:00:09 -05002730 return -ENOSPC;
2731 }
2732 spin_unlock(&meta_sinfo->lock);
2733
2734 return 0;
2735}
2736
2737/*
2738 * This will check the space that the inode allocates from to make sure we have
2739 * enough space for bytes.
2740 */
2741int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
2742 u64 bytes)
2743{
2744 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002745 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002746
2747 /* make sure bytes are sectorsize aligned */
2748 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2749
2750 data_sinfo = BTRFS_I(inode)->space_info;
2751again:
2752 /* make sure we have enough space to handle the data first */
2753 spin_lock(&data_sinfo->lock);
2754 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
2755 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
2756 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
2757 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002758 struct btrfs_trans_handle *trans;
2759
Josef Bacik6a632092009-02-20 11:00:09 -05002760 /*
2761 * if we don't have enough free bytes in this space then we need
2762 * to alloc a new chunk.
2763 */
2764 if (!data_sinfo->full) {
2765 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05002766
2767 data_sinfo->force_alloc = 1;
2768 spin_unlock(&data_sinfo->lock);
2769
2770 alloc_target = btrfs_get_alloc_profile(root, 1);
2771 trans = btrfs_start_transaction(root, 1);
2772 if (!trans)
2773 return -ENOMEM;
2774
2775 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2776 bytes + 2 * 1024 * 1024,
2777 alloc_target, 0);
2778 btrfs_end_transaction(trans, root);
2779 if (ret)
2780 return ret;
2781 goto again;
2782 }
2783 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002784
2785 /* commit the current transaction and try again */
2786 if (!committed) {
2787 committed = 1;
2788 trans = btrfs_join_transaction(root, 1);
2789 if (!trans)
2790 return -ENOMEM;
2791 ret = btrfs_commit_transaction(trans, root);
2792 if (ret)
2793 return ret;
2794 goto again;
2795 }
2796
Josef Bacik6a632092009-02-20 11:00:09 -05002797 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2798 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04002799 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07002800 "%llu total\n", (unsigned long long)bytes,
2801 (unsigned long long)data_sinfo->bytes_delalloc,
2802 (unsigned long long)data_sinfo->bytes_used,
2803 (unsigned long long)data_sinfo->bytes_reserved,
2804 (unsigned long long)data_sinfo->bytes_pinned,
2805 (unsigned long long)data_sinfo->bytes_readonly,
2806 (unsigned long long)data_sinfo->bytes_may_use,
2807 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05002808 return -ENOSPC;
2809 }
2810 data_sinfo->bytes_may_use += bytes;
2811 BTRFS_I(inode)->reserved_bytes += bytes;
2812 spin_unlock(&data_sinfo->lock);
2813
2814 return btrfs_check_metadata_free_space(root);
2815}
2816
2817/*
2818 * if there was an error for whatever reason after calling
2819 * btrfs_check_data_free_space, call this so we can cleanup the counters.
2820 */
2821void btrfs_free_reserved_data_space(struct btrfs_root *root,
2822 struct inode *inode, u64 bytes)
2823{
2824 struct btrfs_space_info *data_sinfo;
2825
2826 /* make sure bytes are sectorsize aligned */
2827 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2828
2829 data_sinfo = BTRFS_I(inode)->space_info;
2830 spin_lock(&data_sinfo->lock);
2831 data_sinfo->bytes_may_use -= bytes;
2832 BTRFS_I(inode)->reserved_bytes -= bytes;
2833 spin_unlock(&data_sinfo->lock);
2834}
2835
2836/* called when we are adding a delalloc extent to the inode's io_tree */
2837void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
2838 u64 bytes)
2839{
2840 struct btrfs_space_info *data_sinfo;
2841
2842 /* get the space info for where this inode will be storing its data */
2843 data_sinfo = BTRFS_I(inode)->space_info;
2844
2845 /* make sure we have enough space to handle the data first */
2846 spin_lock(&data_sinfo->lock);
2847 data_sinfo->bytes_delalloc += bytes;
2848
2849 /*
2850 * we are adding a delalloc extent without calling
2851 * btrfs_check_data_free_space first. This happens on a weird
2852 * writepage condition, but shouldn't hurt our accounting
2853 */
2854 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
2855 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
2856 BTRFS_I(inode)->reserved_bytes = 0;
2857 } else {
2858 data_sinfo->bytes_may_use -= bytes;
2859 BTRFS_I(inode)->reserved_bytes -= bytes;
2860 }
2861
2862 spin_unlock(&data_sinfo->lock);
2863}
2864
2865/* called when we are clearing an delalloc extent from the inode's io_tree */
2866void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2867 u64 bytes)
2868{
2869 struct btrfs_space_info *info;
2870
2871 info = BTRFS_I(inode)->space_info;
2872
2873 spin_lock(&info->lock);
2874 info->bytes_delalloc -= bytes;
2875 spin_unlock(&info->lock);
2876}
2877
Josef Bacik97e728d2009-04-21 17:40:57 -04002878static void force_metadata_allocation(struct btrfs_fs_info *info)
2879{
2880 struct list_head *head = &info->space_info;
2881 struct btrfs_space_info *found;
2882
2883 rcu_read_lock();
2884 list_for_each_entry_rcu(found, head, list) {
2885 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
2886 found->force_alloc = 1;
2887 }
2888 rcu_read_unlock();
2889}
2890
Chris Mason6324fbf2008-03-24 15:01:59 -04002891static int do_chunk_alloc(struct btrfs_trans_handle *trans,
2892 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04002893 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04002894{
2895 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04002896 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04002897 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05002898 int ret = 0;
2899
Josef Bacik97e728d2009-04-21 17:40:57 -04002900 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04002901
Yan Zheng2b820322008-11-17 21:11:30 -05002902 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04002903
Chris Mason6324fbf2008-03-24 15:01:59 -04002904 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04002905 if (!space_info) {
2906 ret = update_space_info(extent_root->fs_info, flags,
2907 0, 0, &space_info);
2908 BUG_ON(ret);
2909 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002910 BUG_ON(!space_info);
2911
Josef Bacik25179202008-10-29 14:49:05 -04002912 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04002913 if (space_info->force_alloc) {
2914 force = 1;
2915 space_info->force_alloc = 0;
2916 }
Josef Bacik25179202008-10-29 14:49:05 -04002917 if (space_info->full) {
2918 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002919 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002920 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002921
Yan Zhengc146afa2008-11-12 14:34:12 -05002922 thresh = space_info->total_bytes - space_info->bytes_readonly;
2923 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04002924 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04002925 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04002926 space_info->bytes_reserved + alloc_bytes) < thresh) {
2927 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002928 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002929 }
Josef Bacik25179202008-10-29 14:49:05 -04002930 spin_unlock(&space_info->lock);
2931
Josef Bacik97e728d2009-04-21 17:40:57 -04002932 /*
2933 * if we're doing a data chunk, go ahead and make sure that
2934 * we keep a reasonable number of metadata chunks allocated in the
2935 * FS as well.
2936 */
2937 if (flags & BTRFS_BLOCK_GROUP_DATA) {
2938 fs_info->data_chunk_allocations++;
2939 if (!(fs_info->data_chunk_allocations %
2940 fs_info->metadata_ratio))
2941 force_metadata_allocation(fs_info);
2942 }
2943
Yan Zheng2b820322008-11-17 21:11:30 -05002944 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05002945 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04002946 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04002947out:
Yan Zhengc146afa2008-11-12 14:34:12 -05002948 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002949 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04002950}
2951
Chris Mason9078a3e2007-04-26 16:46:15 -04002952static int update_block_group(struct btrfs_trans_handle *trans,
2953 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04002954 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04002955 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04002956{
2957 struct btrfs_block_group_cache *cache;
2958 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04002959 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002960 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04002961 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04002962
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002963 /* block accounting for super block */
2964 spin_lock(&info->delalloc_lock);
2965 old_val = btrfs_super_bytes_used(&info->super_copy);
2966 if (alloc)
2967 old_val += num_bytes;
2968 else
2969 old_val -= num_bytes;
2970 btrfs_set_super_bytes_used(&info->super_copy, old_val);
2971
2972 /* block accounting for root item */
2973 old_val = btrfs_root_used(&root->root_item);
2974 if (alloc)
2975 old_val += num_bytes;
2976 else
2977 old_val -= num_bytes;
2978 btrfs_set_root_used(&root->root_item, old_val);
2979 spin_unlock(&info->delalloc_lock);
2980
Chris Masond3977122009-01-05 21:25:51 -05002981 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04002982 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002983 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04002984 return -1;
Chris Masondb945352007-10-15 16:15:53 -04002985 byte_in_group = bytenr - cache->key.objectid;
2986 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04002987
Josef Bacik25179202008-10-29 14:49:05 -04002988 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04002989 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002990 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04002991 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04002992 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04002993 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04002994 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002995 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05002996 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05002997 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002998 btrfs_set_block_group_used(&cache->item, old_val);
2999 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003000 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003001 } else {
Chris Masondb945352007-10-15 16:15:53 -04003002 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003003 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05003004 if (cache->ro)
3005 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003006 btrfs_set_block_group_used(&cache->item, old_val);
3007 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003008 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04003009 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003010 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003011
3012 ret = btrfs_discard_extent(root, bytenr,
3013 num_bytes);
3014 WARN_ON(ret);
3015
Josef Bacik0f9dd462008-09-23 13:14:11 -04003016 ret = btrfs_add_free_space(cache, bytenr,
3017 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003018 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04003019 }
Chris Masoncd1bc462007-04-27 10:08:34 -04003020 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003021 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003022 total -= num_bytes;
3023 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003024 }
3025 return 0;
3026}
Chris Mason6324fbf2008-03-24 15:01:59 -04003027
Chris Masona061fc82008-05-07 11:43:44 -04003028static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3029{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003030 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003031 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003032
3033 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3034 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003035 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003036
Yan Zhengd2fb3432008-12-11 16:30:39 -05003037 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003038 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003039
3040 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003041}
3042
Chris Masone02119d2008-09-05 16:13:11 -04003043int btrfs_update_pinned_extents(struct btrfs_root *root,
Josef Bacik68b38552009-07-27 13:57:01 -04003044 u64 bytenr, u64 num, int pin)
Yan324ae4d2007-11-16 14:57:08 -05003045{
3046 u64 len;
3047 struct btrfs_block_group_cache *cache;
3048 struct btrfs_fs_info *fs_info = root->fs_info;
3049
Josef Bacik68b38552009-07-27 13:57:01 -04003050 if (pin)
Yan324ae4d2007-11-16 14:57:08 -05003051 set_extent_dirty(&fs_info->pinned_extents,
3052 bytenr, bytenr + num - 1, GFP_NOFS);
Chris Masonb9473432009-03-13 11:00:37 -04003053
Yan324ae4d2007-11-16 14:57:08 -05003054 while (num > 0) {
3055 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04003056 BUG_ON(!cache);
3057 len = min(num, cache->key.offset -
3058 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05003059 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04003060 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04003061 spin_lock(&cache->lock);
3062 cache->pinned += len;
3063 cache->space_info->bytes_pinned += len;
3064 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003065 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05003066 fs_info->total_pinned += len;
3067 } else {
Josef Bacik68b38552009-07-27 13:57:01 -04003068 int unpin = 0;
3069
3070 /*
3071 * in order to not race with the block group caching, we
3072 * only want to unpin the extent if we are cached. If
3073 * we aren't cached, we want to start async caching this
3074 * block group so we can free the extent the next time
3075 * around.
3076 */
Josef Bacik25179202008-10-29 14:49:05 -04003077 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04003078 spin_lock(&cache->lock);
Josef Bacik68b38552009-07-27 13:57:01 -04003079 unpin = (cache->cached == BTRFS_CACHE_FINISHED);
3080 if (likely(unpin)) {
3081 cache->pinned -= len;
3082 cache->space_info->bytes_pinned -= len;
3083 fs_info->total_pinned -= len;
3084 }
Zheng Yane8569812008-09-26 10:05:48 -04003085 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003086 spin_unlock(&cache->space_info->lock);
Josef Bacik68b38552009-07-27 13:57:01 -04003087
3088 if (likely(unpin))
3089 clear_extent_dirty(&fs_info->pinned_extents,
3090 bytenr, bytenr + len -1,
3091 GFP_NOFS);
3092 else
3093 cache_block_group(cache);
3094
3095 if (unpin)
Josef Bacik07103a32008-11-19 15:17:55 -05003096 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05003097 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003098 btrfs_put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05003099 bytenr += len;
3100 num -= len;
3101 }
3102 return 0;
3103}
Chris Mason9078a3e2007-04-26 16:46:15 -04003104
Zheng Yane8569812008-09-26 10:05:48 -04003105static int update_reserved_extents(struct btrfs_root *root,
3106 u64 bytenr, u64 num, int reserve)
3107{
3108 u64 len;
3109 struct btrfs_block_group_cache *cache;
3110 struct btrfs_fs_info *fs_info = root->fs_info;
3111
Zheng Yane8569812008-09-26 10:05:48 -04003112 while (num > 0) {
3113 cache = btrfs_lookup_block_group(fs_info, bytenr);
3114 BUG_ON(!cache);
3115 len = min(num, cache->key.offset -
3116 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04003117
3118 spin_lock(&cache->space_info->lock);
3119 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04003120 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04003121 cache->reserved += len;
3122 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04003123 } else {
Zheng Yane8569812008-09-26 10:05:48 -04003124 cache->reserved -= len;
3125 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04003126 }
Josef Bacik25179202008-10-29 14:49:05 -04003127 spin_unlock(&cache->lock);
3128 spin_unlock(&cache->space_info->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -04003129 btrfs_put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04003130 bytenr += len;
3131 num -= len;
3132 }
3133 return 0;
3134}
3135
Chris Masond1310b22008-01-24 16:13:08 -05003136int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04003137{
Chris Masonccd467d2007-06-28 15:57:36 -04003138 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04003139 u64 start;
3140 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05003141 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04003142 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04003143
Chris Masond3977122009-01-05 21:25:51 -05003144 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003145 ret = find_first_extent_bit(pinned_extents, last,
3146 &start, &end, EXTENT_DIRTY);
3147 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04003148 break;
Josef Bacik817d52f2009-07-13 21:29:25 -04003149
Chris Mason1a5bc162007-10-15 16:15:26 -04003150 set_extent_dirty(copy, start, end, GFP_NOFS);
3151 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04003152 }
3153 return 0;
3154}
3155
3156int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
3157 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05003158 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05003159{
Chris Mason1a5bc162007-10-15 16:15:26 -04003160 u64 start;
3161 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003162 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003163
Chris Masond3977122009-01-05 21:25:51 -05003164 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003165 ret = find_first_extent_bit(unpin, 0, &start, &end,
3166 EXTENT_DIRTY);
3167 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003168 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003169
3170 ret = btrfs_discard_extent(root, start, end + 1 - start);
3171
Chris Masonb9473432009-03-13 11:00:37 -04003172 /* unlocks the pinned mutex */
Josef Bacik68b38552009-07-27 13:57:01 -04003173 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04003174 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003175
Chris Masonb9473432009-03-13 11:00:37 -04003176 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003177 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003178
Liu Hui1f3c79a2009-01-05 15:57:51 -05003179 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003180}
3181
Zheng Yan31840ae2008-09-23 13:14:14 -04003182static int pin_down_bytes(struct btrfs_trans_handle *trans,
3183 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003184 struct btrfs_path *path,
3185 u64 bytenr, u64 num_bytes, int is_data,
3186 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003187{
Chris Mason1a5bc162007-10-15 16:15:26 -04003188 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003189 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003190
Zheng Yan31840ae2008-09-23 13:14:14 -04003191 if (is_data)
3192 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003193
Zheng Yan31840ae2008-09-23 13:14:14 -04003194 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3195 if (!buf)
3196 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003197
Zheng Yan31840ae2008-09-23 13:14:14 -04003198 /* we can reuse a block if it hasn't been written
3199 * and it is from this transaction. We can't
3200 * reuse anything from the tree log root because
3201 * it has tiny sub-transactions.
3202 */
3203 if (btrfs_buffer_uptodate(buf, 0) &&
3204 btrfs_try_tree_lock(buf)) {
3205 u64 header_owner = btrfs_header_owner(buf);
3206 u64 header_transid = btrfs_header_generation(buf);
3207 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3208 header_transid == trans->transid &&
3209 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003210 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003211 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003212 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003213 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003214 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003215 free_extent_buffer(buf);
3216pinit:
Chris Masonb9473432009-03-13 11:00:37 -04003217 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003218 /* unlocks the pinned mutex */
Josef Bacik68b38552009-07-27 13:57:01 -04003219 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003220
Chris Masonbe744172007-05-06 10:15:01 -04003221 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003222 return 0;
3223}
3224
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003225
3226static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3227 struct btrfs_root *root,
3228 u64 bytenr, u64 num_bytes, u64 parent,
3229 u64 root_objectid, u64 owner_objectid,
3230 u64 owner_offset, int refs_to_drop,
3231 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003232{
Chris Masone2fa7222007-03-12 16:22:34 -04003233 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003234 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003235 struct btrfs_fs_info *info = root->fs_info;
3236 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003237 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003238 struct btrfs_extent_item *ei;
3239 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003240 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003241 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003242 int extent_slot = 0;
3243 int found_extent = 0;
3244 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003245 u32 item_size;
3246 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003247
Chris Mason5caf2a02007-04-02 11:20:42 -04003248 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003249 if (!path)
3250 return -ENOMEM;
3251
Chris Mason3c12ac72008-04-21 12:01:38 -04003252 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003253 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003254
3255 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3256 BUG_ON(!is_data && refs_to_drop != 1);
3257
3258 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3259 bytenr, num_bytes, parent,
3260 root_objectid, owner_objectid,
3261 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003262 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003263 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003264 while (extent_slot >= 0) {
3265 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003266 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003267 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003268 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003269 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3270 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003271 found_extent = 1;
3272 break;
3273 }
3274 if (path->slots[0] - extent_slot > 5)
3275 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003276 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003277 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003278#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3279 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3280 if (found_extent && item_size < sizeof(*ei))
3281 found_extent = 0;
3282#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003283 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003284 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003285 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003286 NULL, refs_to_drop,
3287 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003288 BUG_ON(ret);
3289 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003290 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003291
3292 key.objectid = bytenr;
3293 key.type = BTRFS_EXTENT_ITEM_KEY;
3294 key.offset = num_bytes;
3295
Zheng Yan31840ae2008-09-23 13:14:14 -04003296 ret = btrfs_search_slot(trans, extent_root,
3297 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003298 if (ret) {
3299 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003300 ", was looking for %llu\n", ret,
3301 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003302 btrfs_print_leaf(extent_root, path->nodes[0]);
3303 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003304 BUG_ON(ret);
3305 extent_slot = path->slots[0];
3306 }
Chris Mason7bb86312007-12-11 09:25:06 -05003307 } else {
3308 btrfs_print_leaf(extent_root, path->nodes[0]);
3309 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003310 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003311 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003312 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003313 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003314 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003315 (unsigned long long)owner_objectid,
3316 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003317 }
Chris Mason5f39d392007-10-15 16:14:19 -04003318
3319 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003320 item_size = btrfs_item_size_nr(leaf, extent_slot);
3321#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3322 if (item_size < sizeof(*ei)) {
3323 BUG_ON(found_extent || extent_slot != path->slots[0]);
3324 ret = convert_extent_item_v0(trans, extent_root, path,
3325 owner_objectid, 0);
3326 BUG_ON(ret < 0);
3327
3328 btrfs_release_path(extent_root, path);
3329 path->leave_spinning = 1;
3330
3331 key.objectid = bytenr;
3332 key.type = BTRFS_EXTENT_ITEM_KEY;
3333 key.offset = num_bytes;
3334
3335 ret = btrfs_search_slot(trans, extent_root, &key, path,
3336 -1, 1);
3337 if (ret) {
3338 printk(KERN_ERR "umm, got %d back from search"
3339 ", was looking for %llu\n", ret,
3340 (unsigned long long)bytenr);
3341 btrfs_print_leaf(extent_root, path->nodes[0]);
3342 }
3343 BUG_ON(ret);
3344 extent_slot = path->slots[0];
3345 leaf = path->nodes[0];
3346 item_size = btrfs_item_size_nr(leaf, extent_slot);
3347 }
3348#endif
3349 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003350 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003351 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003352 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3353 struct btrfs_tree_block_info *bi;
3354 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3355 bi = (struct btrfs_tree_block_info *)(ei + 1);
3356 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003357 }
3358
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003359 refs = btrfs_extent_refs(leaf, ei);
3360 BUG_ON(refs < refs_to_drop);
3361 refs -= refs_to_drop;
3362
3363 if (refs > 0) {
3364 if (extent_op)
3365 __run_delayed_extent_op(extent_op, leaf, ei);
3366 /*
3367 * In the case of inline back ref, reference count will
3368 * be updated by remove_extent_backref
3369 */
3370 if (iref) {
3371 BUG_ON(!found_extent);
3372 } else {
3373 btrfs_set_extent_refs(leaf, ei, refs);
3374 btrfs_mark_buffer_dirty(leaf);
3375 }
3376 if (found_extent) {
3377 ret = remove_extent_backref(trans, extent_root, path,
3378 iref, refs_to_drop,
3379 is_data);
3380 BUG_ON(ret);
3381 }
3382 } else {
3383 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003384 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003385
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003386 if (found_extent) {
3387 BUG_ON(is_data && refs_to_drop !=
3388 extent_data_ref_count(root, path, iref));
3389 if (iref) {
3390 BUG_ON(path->slots[0] != extent_slot);
3391 } else {
3392 BUG_ON(path->slots[0] != extent_slot + 1);
3393 path->slots[0] = extent_slot;
3394 num_to_del = 2;
3395 }
Chris Mason78fae272007-03-25 11:35:08 -04003396 }
Chris Masonb9473432009-03-13 11:00:37 -04003397
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003398 ret = pin_down_bytes(trans, root, path, bytenr,
3399 num_bytes, is_data, &must_clean);
3400 if (ret > 0)
3401 mark_free = 1;
3402 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003403 /*
3404 * it is going to be very rare for someone to be waiting
3405 * on the block we're freeing. del_items might need to
3406 * schedule, so rather than get fancy, just force it
3407 * to blocking here
3408 */
3409 if (must_clean)
3410 btrfs_set_lock_blocking(must_clean);
3411
Chris Mason952fcca2008-02-18 16:33:44 -05003412 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3413 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003414 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003415 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003416
Chris Masonb9473432009-03-13 11:00:37 -04003417 if (must_clean) {
3418 clean_tree_block(NULL, root, must_clean);
3419 btrfs_tree_unlock(must_clean);
3420 free_extent_buffer(must_clean);
3421 }
3422
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003423 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003424 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3425 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003426 } else {
3427 invalidate_mapping_pages(info->btree_inode->i_mapping,
3428 bytenr >> PAGE_CACHE_SHIFT,
3429 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003430 }
3431
Chris Masondcbdd4d2008-12-16 13:51:01 -05003432 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3433 mark_free);
3434 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003435 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003436 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003437 return ret;
3438}
3439
3440/*
Chris Mason1887be62009-03-13 10:11:24 -04003441 * when we free an extent, it is possible (and likely) that we free the last
3442 * delayed ref for that extent as well. This searches the delayed ref tree for
3443 * a given extent, and if there are no other delayed refs to be processed, it
3444 * removes it from the tree.
3445 */
3446static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3447 struct btrfs_root *root, u64 bytenr)
3448{
3449 struct btrfs_delayed_ref_head *head;
3450 struct btrfs_delayed_ref_root *delayed_refs;
3451 struct btrfs_delayed_ref_node *ref;
3452 struct rb_node *node;
3453 int ret;
3454
3455 delayed_refs = &trans->transaction->delayed_refs;
3456 spin_lock(&delayed_refs->lock);
3457 head = btrfs_find_delayed_ref_head(trans, bytenr);
3458 if (!head)
3459 goto out;
3460
3461 node = rb_prev(&head->node.rb_node);
3462 if (!node)
3463 goto out;
3464
3465 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3466
3467 /* there are still entries for this ref, we can't drop it */
3468 if (ref->bytenr == bytenr)
3469 goto out;
3470
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003471 if (head->extent_op) {
3472 if (!head->must_insert_reserved)
3473 goto out;
3474 kfree(head->extent_op);
3475 head->extent_op = NULL;
3476 }
3477
Chris Mason1887be62009-03-13 10:11:24 -04003478 /*
3479 * waiting for the lock here would deadlock. If someone else has it
3480 * locked they are already in the process of dropping it anyway
3481 */
3482 if (!mutex_trylock(&head->mutex))
3483 goto out;
3484
3485 /*
3486 * at this point we have a head with no other entries. Go
3487 * ahead and process it.
3488 */
3489 head->node.in_tree = 0;
3490 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04003491
Chris Mason1887be62009-03-13 10:11:24 -04003492 delayed_refs->num_entries--;
3493
3494 /*
3495 * we don't take a ref on the node because we're removing it from the
3496 * tree, so we just steal the ref the tree was holding.
3497 */
Chris Masonc3e69d52009-03-13 10:17:05 -04003498 delayed_refs->num_heads--;
3499 if (list_empty(&head->cluster))
3500 delayed_refs->num_heads_ready--;
3501
3502 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04003503 spin_unlock(&delayed_refs->lock);
3504
3505 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003506 &head->node, head->extent_op,
3507 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04003508 BUG_ON(ret);
3509 btrfs_put_delayed_ref(&head->node);
3510 return 0;
3511out:
3512 spin_unlock(&delayed_refs->lock);
3513 return 0;
3514}
3515
Chris Mason925baed2008-06-25 16:01:30 -04003516int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003517 struct btrfs_root *root,
3518 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003519 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04003520{
3521 int ret;
3522
Chris Mason56bec292009-03-13 10:10:06 -04003523 /*
3524 * tree log blocks never actually go into the extent allocation
3525 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003526 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003527 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
3528 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04003529 /* unlocks the pinned mutex */
Josef Bacik68b38552009-07-27 13:57:01 -04003530 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04003531 update_reserved_extents(root, bytenr, num_bytes, 0);
3532 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003533 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
3534 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
3535 parent, root_objectid, (int)owner,
3536 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04003537 BUG_ON(ret);
3538 ret = check_ref_cleanup(trans, root, bytenr);
3539 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003540 } else {
3541 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
3542 parent, root_objectid, owner,
3543 offset, BTRFS_DROP_DELAYED_REF, NULL);
3544 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04003545 }
Chris Mason925baed2008-06-25 16:01:30 -04003546 return ret;
3547}
3548
Chris Mason87ee04e2007-11-30 11:30:34 -05003549static u64 stripe_align(struct btrfs_root *root, u64 val)
3550{
3551 u64 mask = ((u64)root->stripesize - 1);
3552 u64 ret = (val + mask) & ~mask;
3553 return ret;
3554}
3555
Chris Masonfec577f2007-02-26 10:40:21 -05003556/*
Josef Bacik817d52f2009-07-13 21:29:25 -04003557 * when we wait for progress in the block group caching, its because
3558 * our allocation attempt failed at least once. So, we must sleep
3559 * and let some progress happen before we try again.
3560 *
3561 * This function will sleep at least once waiting for new free space to
3562 * show up, and then it will check the block group free space numbers
3563 * for our min num_bytes. Another option is to have it go ahead
3564 * and look in the rbtree for a free extent of a given size, but this
3565 * is a good start.
3566 */
3567static noinline int
3568wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
3569 u64 num_bytes)
3570{
3571 DEFINE_WAIT(wait);
3572
3573 prepare_to_wait(&cache->caching_q, &wait, TASK_UNINTERRUPTIBLE);
3574
3575 if (block_group_cache_done(cache)) {
3576 finish_wait(&cache->caching_q, &wait);
3577 return 0;
3578 }
3579 schedule();
3580 finish_wait(&cache->caching_q, &wait);
3581
3582 wait_event(cache->caching_q, block_group_cache_done(cache) ||
3583 (cache->free_space >= num_bytes));
3584 return 0;
3585}
3586
3587enum btrfs_loop_type {
3588 LOOP_CACHED_ONLY = 0,
3589 LOOP_CACHING_NOWAIT = 1,
3590 LOOP_CACHING_WAIT = 2,
3591 LOOP_ALLOC_CHUNK = 3,
3592 LOOP_NO_EMPTY_SIZE = 4,
3593};
3594
3595/*
Chris Masonfec577f2007-02-26 10:40:21 -05003596 * walks the btree of allocated extents and find a hole of a given size.
3597 * The key ins is changed to record the hole:
3598 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04003599 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05003600 * ins->offset == number of blocks
3601 * Any available blocks before search_start are skipped.
3602 */
Chris Masond3977122009-01-05 21:25:51 -05003603static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003604 struct btrfs_root *orig_root,
3605 u64 num_bytes, u64 empty_size,
3606 u64 search_start, u64 search_end,
3607 u64 hint_byte, struct btrfs_key *ins,
3608 u64 exclude_start, u64 exclude_nr,
3609 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05003610{
Josef Bacik80eb2342008-10-29 14:49:05 -04003611 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003612 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003613 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04003614 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04003615 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04003616 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003617 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003618 int last_ptr_loop = 0;
3619 int loop = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04003620 bool found_uncached_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05003621
Chris Masondb945352007-10-15 16:15:53 -04003622 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04003623 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04003624 ins->objectid = 0;
3625 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003626
Josef Bacik2552d172009-04-03 10:14:19 -04003627 space_info = __find_space_info(root->fs_info, data);
3628
Chris Mason0ef3e662008-05-24 14:04:53 -04003629 if (orig_root->ref_cows || empty_size)
3630 allowed_chunk_alloc = 1;
3631
Chris Mason239b14b2008-03-24 15:02:07 -04003632 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003633 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05003634 if (!btrfs_test_opt(root, SSD))
3635 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04003636 }
3637
Chris Masonfa9c0d72009-04-03 09:47:43 -04003638 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
3639 last_ptr = &root->fs_info->data_alloc_cluster;
3640 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003641
Chris Mason239b14b2008-03-24 15:02:07 -04003642 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003643 spin_lock(&last_ptr->lock);
3644 if (last_ptr->block_group)
3645 hint_byte = last_ptr->window_start;
3646 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003647 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003648
Chris Masona061fc82008-05-07 11:43:44 -04003649 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04003650 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04003651
Josef Bacik817d52f2009-07-13 21:29:25 -04003652 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003653 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003654
Josef Bacik2552d172009-04-03 10:14:19 -04003655 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04003656 block_group = btrfs_lookup_block_group(root->fs_info,
3657 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04003658 /*
3659 * we don't want to use the block group if it doesn't match our
3660 * allocation bits, or if its not cached.
3661 */
3662 if (block_group && block_group_bits(block_group, data) &&
3663 block_group_cache_done(block_group)) {
Josef Bacik2552d172009-04-03 10:14:19 -04003664 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003665 if (list_empty(&block_group->list) ||
3666 block_group->ro) {
3667 /*
3668 * someone is removing this block group,
3669 * we can't jump into the have_block_group
3670 * target because our list pointers are not
3671 * valid
3672 */
3673 btrfs_put_block_group(block_group);
3674 up_read(&space_info->groups_sem);
3675 } else
3676 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003677 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003678 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003679 }
Chris Mason42e70e72008-11-07 18:17:11 -05003680 }
Chris Mason43662112008-11-07 09:06:11 -05003681
Josef Bacik2552d172009-04-03 10:14:19 -04003682search:
Josef Bacik80eb2342008-10-29 14:49:05 -04003683 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04003684 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04003685 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04003686 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05003687
Josef Bacik2552d172009-04-03 10:14:19 -04003688 atomic_inc(&block_group->count);
3689 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05003690
Josef Bacik2552d172009-04-03 10:14:19 -04003691have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04003692 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
3693 /*
3694 * we want to start caching kthreads, but not too many
3695 * right off the bat so we don't overwhelm the system,
3696 * so only start them if there are less than 2 and we're
3697 * in the initial allocation phase.
3698 */
3699 if (loop > LOOP_CACHING_NOWAIT ||
3700 atomic_read(&space_info->caching_threads) < 2) {
3701 ret = cache_block_group(block_group);
3702 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04003703 }
Josef Bacikea6a4782008-11-20 12:16:16 -05003704 }
3705
Josef Bacik817d52f2009-07-13 21:29:25 -04003706 cached = block_group_cache_done(block_group);
3707 if (unlikely(!cached)) {
3708 found_uncached_bg = true;
3709
3710 /* if we only want cached bgs, loop */
3711 if (loop == LOOP_CACHED_ONLY)
3712 goto loop;
3713 }
3714
Josef Bacikea6a4782008-11-20 12:16:16 -05003715 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04003716 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003717
Chris Masonfa9c0d72009-04-03 09:47:43 -04003718 if (last_ptr) {
3719 /*
3720 * the refill lock keeps out other
3721 * people trying to start a new cluster
3722 */
3723 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003724 if (last_ptr->block_group &&
3725 (last_ptr->block_group->ro ||
3726 !block_group_bits(last_ptr->block_group, data))) {
3727 offset = 0;
3728 goto refill_cluster;
3729 }
3730
Chris Masonfa9c0d72009-04-03 09:47:43 -04003731 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
3732 num_bytes, search_start);
3733 if (offset) {
3734 /* we have a block, we're done */
3735 spin_unlock(&last_ptr->refill_lock);
3736 goto checks;
3737 }
3738
3739 spin_lock(&last_ptr->lock);
3740 /*
3741 * whoops, this cluster doesn't actually point to
3742 * this block group. Get a ref on the block
3743 * group is does point to and try again
3744 */
3745 if (!last_ptr_loop && last_ptr->block_group &&
3746 last_ptr->block_group != block_group) {
3747
3748 btrfs_put_block_group(block_group);
3749 block_group = last_ptr->block_group;
3750 atomic_inc(&block_group->count);
3751 spin_unlock(&last_ptr->lock);
3752 spin_unlock(&last_ptr->refill_lock);
3753
3754 last_ptr_loop = 1;
3755 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04003756 /*
3757 * we know this block group is properly
3758 * in the list because
3759 * btrfs_remove_block_group, drops the
3760 * cluster before it removes the block
3761 * group from the list
3762 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04003763 goto have_block_group;
3764 }
3765 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003766refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04003767 /*
3768 * this cluster didn't work out, free it and
3769 * start over
3770 */
3771 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3772
3773 last_ptr_loop = 0;
3774
3775 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04003776 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04003777 block_group, last_ptr,
3778 offset, num_bytes,
3779 empty_cluster + empty_size);
3780 if (ret == 0) {
3781 /*
3782 * now pull our allocation out of this
3783 * cluster
3784 */
3785 offset = btrfs_alloc_from_cluster(block_group,
3786 last_ptr, num_bytes,
3787 search_start);
3788 if (offset) {
3789 /* we found one, proceed */
3790 spin_unlock(&last_ptr->refill_lock);
3791 goto checks;
3792 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003793 } else if (!cached && loop > LOOP_CACHING_NOWAIT) {
3794 spin_unlock(&last_ptr->refill_lock);
3795
3796 wait_block_group_cache_progress(block_group,
3797 num_bytes + empty_cluster + empty_size);
3798 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003799 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003800
Chris Masonfa9c0d72009-04-03 09:47:43 -04003801 /*
3802 * at this point we either didn't find a cluster
3803 * or we weren't able to allocate a block from our
3804 * cluster. Free the cluster we've been trying
3805 * to use, and go to the next block group
3806 */
Josef Bacik817d52f2009-07-13 21:29:25 -04003807 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003808 btrfs_return_cluster_to_free_space(NULL,
3809 last_ptr);
3810 spin_unlock(&last_ptr->refill_lock);
3811 goto loop;
3812 }
3813 spin_unlock(&last_ptr->refill_lock);
3814 }
3815
Josef Bacik6226cb02009-04-03 10:14:18 -04003816 offset = btrfs_find_space_for_alloc(block_group, search_start,
3817 num_bytes, empty_size);
Josef Bacik817d52f2009-07-13 21:29:25 -04003818 if (!offset && (cached || (!cached &&
3819 loop == LOOP_CACHING_NOWAIT))) {
Josef Bacik2552d172009-04-03 10:14:19 -04003820 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04003821 } else if (!offset && (!cached &&
3822 loop > LOOP_CACHING_NOWAIT)) {
3823 wait_block_group_cache_progress(block_group,
3824 num_bytes + empty_size);
3825 goto have_block_group;
3826 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003827checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04003828 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04003829 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04003830 if (search_start + num_bytes >= search_end) {
3831 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003832 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04003833 }
Chris Masone37c9e62007-05-09 20:13:14 -04003834
Josef Bacik2552d172009-04-03 10:14:19 -04003835 /* move on to the next group */
3836 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04003837 block_group->key.objectid + block_group->key.offset) {
3838 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003839 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04003840 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003841
Josef Bacik2552d172009-04-03 10:14:19 -04003842 if (exclude_nr > 0 &&
3843 (search_start + num_bytes > exclude_start &&
3844 search_start < exclude_start + exclude_nr)) {
3845 search_start = exclude_start + exclude_nr;
3846
Josef Bacik6226cb02009-04-03 10:14:18 -04003847 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003848 /*
3849 * if search_start is still in this block group
3850 * then we just re-search this block group
3851 */
3852 if (search_start >= block_group->key.objectid &&
3853 search_start < (block_group->key.objectid +
Josef Bacik6226cb02009-04-03 10:14:18 -04003854 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04003855 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003856 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003857 }
Josef Bacik2552d172009-04-03 10:14:19 -04003858
3859 ins->objectid = search_start;
3860 ins->offset = num_bytes;
3861
Josef Bacik6226cb02009-04-03 10:14:18 -04003862 if (offset < search_start)
3863 btrfs_add_free_space(block_group, offset,
3864 search_start - offset);
3865 BUG_ON(offset > search_start);
3866
Josef Bacik2552d172009-04-03 10:14:19 -04003867 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04003868 break;
3869loop:
Chris Masonfa9c0d72009-04-03 09:47:43 -04003870 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003871 }
3872 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05003873
Josef Bacik817d52f2009-07-13 21:29:25 -04003874 /* LOOP_CACHED_ONLY, only search fully cached block groups
3875 * LOOP_CACHING_NOWAIT, search partially cached block groups, but
3876 * dont wait foR them to finish caching
3877 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3878 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3879 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3880 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04003881 */
Josef Bacik817d52f2009-07-13 21:29:25 -04003882 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
3883 (found_uncached_bg || empty_size || empty_cluster ||
3884 allowed_chunk_alloc)) {
3885 if (found_uncached_bg) {
3886 found_uncached_bg = false;
3887 if (loop < LOOP_CACHING_WAIT) {
3888 loop++;
3889 goto search;
3890 }
3891 }
3892
3893 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003894 empty_size = 0;
3895 empty_cluster = 0;
3896 }
Chris Mason42e70e72008-11-07 18:17:11 -05003897
Josef Bacik2552d172009-04-03 10:14:19 -04003898 if (allowed_chunk_alloc) {
3899 ret = do_chunk_alloc(trans, root, num_bytes +
3900 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04003901 allowed_chunk_alloc = 0;
3902 } else {
3903 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003904 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003905
Josef Bacik817d52f2009-07-13 21:29:25 -04003906 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003907 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04003908 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003909 }
Josef Bacik2552d172009-04-03 10:14:19 -04003910 ret = -ENOSPC;
3911 } else if (!ins->objectid) {
3912 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04003913 }
Chris Mason0b86a832008-03-24 15:01:56 -04003914
Josef Bacik80eb2342008-10-29 14:49:05 -04003915 /* we found what we needed */
3916 if (ins->objectid) {
3917 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05003918 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04003919
Chris Masonfa9c0d72009-04-03 09:47:43 -04003920 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003921 ret = 0;
3922 }
Chris Mason0b86a832008-03-24 15:01:56 -04003923
Chris Mason0f70abe2007-02-28 16:46:22 -05003924 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003925}
Chris Masonec44a352008-04-28 15:29:52 -04003926
Josef Bacik0f9dd462008-09-23 13:14:11 -04003927static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3928{
3929 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003930
Chris Masond3977122009-01-05 21:25:51 -05003931 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3932 (unsigned long long)(info->total_bytes - info->bytes_used -
3933 info->bytes_pinned - info->bytes_reserved),
3934 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05003935 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Joel Becker21380932009-04-21 12:38:29 -07003936 " may_use=%llu, used=%llu\n",
3937 (unsigned long long)info->total_bytes,
3938 (unsigned long long)info->bytes_pinned,
3939 (unsigned long long)info->bytes_delalloc,
3940 (unsigned long long)info->bytes_may_use,
3941 (unsigned long long)info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003942
Josef Bacik80eb2342008-10-29 14:49:05 -04003943 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003944 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003945 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05003946 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3947 "%llu pinned %llu reserved\n",
3948 (unsigned long long)cache->key.objectid,
3949 (unsigned long long)cache->key.offset,
3950 (unsigned long long)btrfs_block_group_used(&cache->item),
3951 (unsigned long long)cache->pinned,
3952 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003953 btrfs_dump_free_space(cache, bytes);
3954 spin_unlock(&cache->lock);
3955 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003956 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003957}
Zheng Yane8569812008-09-26 10:05:48 -04003958
Chris Masone6dcd2d2008-07-17 12:53:50 -04003959static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3960 struct btrfs_root *root,
3961 u64 num_bytes, u64 min_alloc_size,
3962 u64 empty_size, u64 hint_byte,
3963 u64 search_end, struct btrfs_key *ins,
3964 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003965{
3966 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003967 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04003968 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003969
Josef Bacik6a632092009-02-20 11:00:09 -05003970 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04003971again:
Chris Mason0ef3e662008-05-24 14:04:53 -04003972 /*
3973 * the only place that sets empty_size is btrfs_realloc_node, which
3974 * is not called recursively on allocations
3975 */
3976 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003977 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003978 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003979 2 * 1024 * 1024,
3980 BTRFS_BLOCK_GROUP_METADATA |
3981 (info->metadata_alloc_profile &
3982 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003983 }
3984 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003985 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003986 }
Chris Mason0b86a832008-03-24 15:01:56 -04003987
Chris Masondb945352007-10-15 16:15:53 -04003988 WARN_ON(num_bytes < root->sectorsize);
3989 ret = find_free_extent(trans, root, num_bytes, empty_size,
3990 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003991 trans->alloc_exclude_start,
3992 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003993
Chris Mason98d20f62008-04-14 09:46:10 -04003994 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3995 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003996 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003997 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003998 do_chunk_alloc(trans, root->fs_info->extent_root,
3999 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004000 goto again;
4001 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004002 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004003 struct btrfs_space_info *sinfo;
4004
4005 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004006 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4007 "wanted %llu\n", (unsigned long long)data,
4008 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004009 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04004010 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004011
4012 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004013}
4014
Chris Mason65b51a02008-08-01 15:11:20 -04004015int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4016{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004017 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004018 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004019
Josef Bacik0f9dd462008-09-23 13:14:11 -04004020 cache = btrfs_lookup_block_group(root->fs_info, start);
4021 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004022 printk(KERN_ERR "Unable to find block group for %llu\n",
4023 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004024 return -ENOSPC;
4025 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004026
4027 ret = btrfs_discard_extent(root, start, len);
4028
Josef Bacik0f9dd462008-09-23 13:14:11 -04004029 btrfs_add_free_space(cache, start, len);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004030 btrfs_put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04004031 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004032
4033 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04004034}
4035
Chris Masone6dcd2d2008-07-17 12:53:50 -04004036int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4037 struct btrfs_root *root,
4038 u64 num_bytes, u64 min_alloc_size,
4039 u64 empty_size, u64 hint_byte,
4040 u64 search_end, struct btrfs_key *ins,
4041 u64 data)
4042{
4043 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004044 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
4045 empty_size, hint_byte, search_end, ins,
4046 data);
Josef Bacik817d52f2009-07-13 21:29:25 -04004047 if (!ret)
4048 update_reserved_extents(root, ins->objectid, ins->offset, 1);
4049
Chris Masone6dcd2d2008-07-17 12:53:50 -04004050 return ret;
4051}
4052
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004053static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4054 struct btrfs_root *root,
4055 u64 parent, u64 root_objectid,
4056 u64 flags, u64 owner, u64 offset,
4057 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004058{
4059 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004060 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004061 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004062 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004063 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004064 struct extent_buffer *leaf;
4065 int type;
4066 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004067
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004068 if (parent > 0)
4069 type = BTRFS_SHARED_DATA_REF_KEY;
4070 else
4071 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004072
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004073 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004074
4075 path = btrfs_alloc_path();
4076 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004077
Chris Masonb9473432009-03-13 11:00:37 -04004078 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004079 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4080 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004081 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004082
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004083 leaf = path->nodes[0];
4084 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004085 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004086 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4087 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4088 btrfs_set_extent_flags(leaf, extent_item,
4089 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004090
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004091 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4092 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4093 if (parent > 0) {
4094 struct btrfs_shared_data_ref *ref;
4095 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4096 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4097 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4098 } else {
4099 struct btrfs_extent_data_ref *ref;
4100 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4101 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4102 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4103 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4104 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4105 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004106
4107 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004108 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004109
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004110 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4111 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05004112 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004113 printk(KERN_ERR "btrfs update block group failed for %llu "
4114 "%llu\n", (unsigned long long)ins->objectid,
4115 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004116 BUG();
4117 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004118 return ret;
4119}
4120
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004121static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4122 struct btrfs_root *root,
4123 u64 parent, u64 root_objectid,
4124 u64 flags, struct btrfs_disk_key *key,
4125 int level, struct btrfs_key *ins)
4126{
4127 int ret;
4128 struct btrfs_fs_info *fs_info = root->fs_info;
4129 struct btrfs_extent_item *extent_item;
4130 struct btrfs_tree_block_info *block_info;
4131 struct btrfs_extent_inline_ref *iref;
4132 struct btrfs_path *path;
4133 struct extent_buffer *leaf;
4134 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4135
4136 path = btrfs_alloc_path();
4137 BUG_ON(!path);
4138
4139 path->leave_spinning = 1;
4140 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4141 ins, size);
4142 BUG_ON(ret);
4143
4144 leaf = path->nodes[0];
4145 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4146 struct btrfs_extent_item);
4147 btrfs_set_extent_refs(leaf, extent_item, 1);
4148 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4149 btrfs_set_extent_flags(leaf, extent_item,
4150 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4151 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4152
4153 btrfs_set_tree_block_key(leaf, block_info, key);
4154 btrfs_set_tree_block_level(leaf, block_info, level);
4155
4156 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4157 if (parent > 0) {
4158 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4159 btrfs_set_extent_inline_ref_type(leaf, iref,
4160 BTRFS_SHARED_BLOCK_REF_KEY);
4161 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4162 } else {
4163 btrfs_set_extent_inline_ref_type(leaf, iref,
4164 BTRFS_TREE_BLOCK_REF_KEY);
4165 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4166 }
4167
4168 btrfs_mark_buffer_dirty(leaf);
4169 btrfs_free_path(path);
4170
4171 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4172 1, 0);
4173 if (ret) {
4174 printk(KERN_ERR "btrfs update block group failed for %llu "
4175 "%llu\n", (unsigned long long)ins->objectid,
4176 (unsigned long long)ins->offset);
4177 BUG();
4178 }
4179 return ret;
4180}
4181
4182int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4183 struct btrfs_root *root,
4184 u64 root_objectid, u64 owner,
4185 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004186{
4187 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04004188
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004189 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004190
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004191 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4192 0, root_objectid, owner, offset,
4193 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004194 return ret;
4195}
Chris Masone02119d2008-09-05 16:13:11 -04004196
4197/*
4198 * this is used by the tree logging recovery code. It records that
4199 * an extent has been allocated and makes sure to clear the free
4200 * space cache bits as well
4201 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004202int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4203 struct btrfs_root *root,
4204 u64 root_objectid, u64 owner, u64 offset,
4205 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004206{
4207 int ret;
4208 struct btrfs_block_group_cache *block_group;
4209
Chris Masone02119d2008-09-05 16:13:11 -04004210 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04004211 cache_block_group(block_group);
4212 wait_event(block_group->caching_q,
4213 block_group_cache_done(block_group));
Chris Masone02119d2008-09-05 16:13:11 -04004214
Josef Bacikea6a4782008-11-20 12:16:16 -05004215 ret = btrfs_remove_free_space(block_group, ins->objectid,
4216 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004217 BUG_ON(ret);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004218 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004219 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4220 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004221 return ret;
4222}
4223
Chris Masone6dcd2d2008-07-17 12:53:50 -04004224/*
4225 * finds a free extent and does all the dirty work required for allocation
4226 * returns the key for the extent through ins, and a tree buffer for
4227 * the first block of the extent through buf.
4228 *
4229 * returns 0 if everything worked, non-zero otherwise.
4230 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004231static int alloc_tree_block(struct btrfs_trans_handle *trans,
4232 struct btrfs_root *root,
4233 u64 num_bytes, u64 parent, u64 root_objectid,
4234 struct btrfs_disk_key *key, int level,
4235 u64 empty_size, u64 hint_byte, u64 search_end,
4236 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004237{
4238 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004239 u64 flags = 0;
4240
4241 ret = __btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4242 empty_size, hint_byte, search_end,
4243 ins, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004244 if (ret)
4245 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004246
4247 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4248 if (parent == 0)
4249 parent = ins->objectid;
4250 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4251 } else
4252 BUG_ON(parent > 0);
4253
4254 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04004255 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004256 struct btrfs_delayed_extent_op *extent_op;
4257 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4258 BUG_ON(!extent_op);
4259 if (key)
4260 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4261 else
4262 memset(&extent_op->key, 0, sizeof(extent_op->key));
4263 extent_op->flags_to_set = flags;
4264 extent_op->update_key = 1;
4265 extent_op->update_flags = 1;
4266 extent_op->is_data = 0;
4267
4268 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4269 ins->offset, parent, root_objectid,
4270 level, BTRFS_ADD_DELAYED_EXTENT,
4271 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004272 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004273 }
Chris Mason925baed2008-06-25 16:01:30 -04004274 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004275}
Chris Mason65b51a02008-08-01 15:11:20 -04004276
4277struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4278 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004279 u64 bytenr, u32 blocksize,
4280 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004281{
4282 struct extent_buffer *buf;
4283
4284 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4285 if (!buf)
4286 return ERR_PTR(-ENOMEM);
4287 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004288 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004289 btrfs_tree_lock(buf);
4290 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004291
4292 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004293 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004294
Chris Masond0c803c2008-09-11 16:17:57 -04004295 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4296 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04004297 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004298 } else {
4299 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4300 buf->start + buf->len - 1, GFP_NOFS);
4301 }
Chris Mason65b51a02008-08-01 15:11:20 -04004302 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004303 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004304 return buf;
4305}
4306
Chris Masonfec577f2007-02-26 10:40:21 -05004307/*
4308 * helper function to allocate a block for a given tree
4309 * returns the tree buffer or NULL.
4310 */
Chris Mason5f39d392007-10-15 16:14:19 -04004311struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004312 struct btrfs_root *root, u32 blocksize,
4313 u64 parent, u64 root_objectid,
4314 struct btrfs_disk_key *key, int level,
4315 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004316{
Chris Masone2fa7222007-03-12 16:22:34 -04004317 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004318 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004319 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004320
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004321 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4322 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004323 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004324 BUG_ON(ret > 0);
4325 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004326 }
Chris Mason55c69072008-01-09 15:55:33 -05004327
Chris Mason4008c042009-02-12 14:09:45 -05004328 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4329 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004330 return buf;
4331}
Chris Masona28ec192007-03-06 20:08:01 -05004332
Yan Zheng2c47e6052009-06-27 21:07:35 -04004333#if 0
Chris Masone02119d2008-09-05 16:13:11 -04004334int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
4335 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04004336{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004337 u64 disk_bytenr;
4338 u64 num_bytes;
Chris Mason5f39d392007-10-15 16:14:19 -04004339 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04004340 struct btrfs_file_extent_item *fi;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004341 u32 nritems;
Chris Mason6407bf62007-03-27 06:33:00 -04004342 int i;
Chris Mason6407bf62007-03-27 06:33:00 -04004343 int ret;
4344
Chris Mason5f39d392007-10-15 16:14:19 -04004345 BUG_ON(!btrfs_is_leaf(leaf));
4346 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05004347
Chris Mason6407bf62007-03-27 06:33:00 -04004348 for (i = 0; i < nritems; i++) {
Chris Masone34a5b42008-07-22 12:08:37 -04004349 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04004350 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05004351
4352 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04004353 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04004354 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004355
Chris Mason6407bf62007-03-27 06:33:00 -04004356 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05004357
4358 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04004359 if (btrfs_file_extent_type(leaf, fi) ==
4360 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04004361 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004362
Chris Masondb945352007-10-15 16:15:53 -04004363 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05004364
4365 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04004366 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04004367 continue;
Chris Mason4a096752008-07-21 10:29:44 -04004368
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004369 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4370 ret = btrfs_free_extent(trans, root, disk_bytenr, num_bytes,
4371 leaf->start, 0, key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04004372 BUG_ON(ret);
Chris Mason6407bf62007-03-27 06:33:00 -04004373 }
4374 return 0;
4375}
4376
Chris Masond3977122009-01-05 21:25:51 -05004377static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04004378 struct btrfs_root *root,
4379 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04004380{
4381 int i;
4382 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05004383 struct btrfs_extent_info *info;
4384 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04004385
Chris Masonbd56b302009-02-04 09:27:02 -05004386 if (ref->nritems == 0)
4387 return 0;
4388
4389 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04004390 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05004391 sorted[i].bytenr = ref->extents[i].bytenr;
4392 sorted[i].slot = i;
4393 }
4394 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
4395
4396 /*
4397 * the items in the ref were sorted when the ref was inserted
4398 * into the ref cache, so this is already in order
4399 */
4400 for (i = 0; i < ref->nritems; i++) {
4401 info = ref->extents + sorted[i].slot;
Chris Mason56bec292009-03-13 10:10:06 -04004402 ret = btrfs_free_extent(trans, root, info->bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004403 info->num_bytes, ref->bytenr,
4404 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004405 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04004406
4407 atomic_inc(&root->fs_info->throttle_gen);
4408 wake_up(&root->fs_info->transaction_throttle);
4409 cond_resched();
4410
Yan Zheng31153d82008-07-28 15:32:19 -04004411 BUG_ON(ret);
4412 info++;
4413 }
Yan Zheng31153d82008-07-28 15:32:19 -04004414
Chris Mason806638b2009-02-05 09:08:14 -05004415 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04004416 return 0;
4417}
4418
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004419
Chris Mason56bec292009-03-13 10:10:06 -04004420static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
4421 struct btrfs_root *root, u64 start,
Chris Masond3977122009-01-05 21:25:51 -05004422 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04004423{
Chris Mason017e5362008-07-28 15:32:51 -04004424 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04004425
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004426 ret = btrfs_lookup_extent_refs(trans, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04004427 BUG_ON(ret);
4428
Chris Masond3977122009-01-05 21:25:51 -05004429#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04004430 /* if the refs count is one, it won't get increased again. But
4431 * if the ref count is > 1, someone may be decreasing it at
4432 * the same time we are.
4433 */
4434 if (*refs != 1) {
4435 struct extent_buffer *eb = NULL;
4436 eb = btrfs_find_create_tree_block(root, start, len);
4437 if (eb)
4438 btrfs_tree_lock(eb);
4439
4440 mutex_lock(&root->fs_info->alloc_mutex);
4441 ret = lookup_extent_ref(NULL, root, start, len, refs);
4442 BUG_ON(ret);
4443 mutex_unlock(&root->fs_info->alloc_mutex);
4444
4445 if (eb) {
4446 btrfs_tree_unlock(eb);
4447 free_extent_buffer(eb);
4448 }
4449 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05004450 printk(KERN_ERR "btrfs block %llu went down to one "
4451 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04004452 }
4453
4454 }
4455#endif
4456
Chris Masone7a84562008-06-25 16:01:31 -04004457 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04004458 return ret;
Chris Mason333db942008-06-25 16:01:30 -04004459}
4460
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004461
Chris Mason333db942008-06-25 16:01:30 -04004462/*
Chris Masonbd56b302009-02-04 09:27:02 -05004463 * this is used while deleting old snapshots, and it drops the refs
4464 * on a whole subtree starting from a level 1 node.
4465 *
4466 * The idea is to sort all the leaf pointers, and then drop the
4467 * ref on all the leaves in order. Most of the time the leaves
4468 * will have ref cache entries, so no leaf IOs will be required to
4469 * find the extents they have references on.
4470 *
4471 * For each leaf, any references it has are also dropped in order
4472 *
4473 * This ends up dropping the references in something close to optimal
4474 * order for reading and modifying the extent allocation tree.
4475 */
4476static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
4477 struct btrfs_root *root,
4478 struct btrfs_path *path)
4479{
4480 u64 bytenr;
4481 u64 root_owner;
4482 u64 root_gen;
4483 struct extent_buffer *eb = path->nodes[1];
4484 struct extent_buffer *leaf;
4485 struct btrfs_leaf_ref *ref;
4486 struct refsort *sorted = NULL;
4487 int nritems = btrfs_header_nritems(eb);
4488 int ret;
4489 int i;
4490 int refi = 0;
4491 int slot = path->slots[1];
4492 u32 blocksize = btrfs_level_size(root, 0);
4493 u32 refs;
4494
4495 if (nritems == 0)
4496 goto out;
4497
4498 root_owner = btrfs_header_owner(eb);
4499 root_gen = btrfs_header_generation(eb);
4500 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
4501
4502 /*
4503 * step one, sort all the leaf pointers so we don't scribble
4504 * randomly into the extent allocation tree
4505 */
4506 for (i = slot; i < nritems; i++) {
4507 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
4508 sorted[refi].slot = i;
4509 refi++;
4510 }
4511
4512 /*
4513 * nritems won't be zero, but if we're picking up drop_snapshot
4514 * after a crash, slot might be > 0, so double check things
4515 * just in case.
4516 */
4517 if (refi == 0)
4518 goto out;
4519
4520 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
4521
4522 /*
4523 * the first loop frees everything the leaves point to
4524 */
4525 for (i = 0; i < refi; i++) {
4526 u64 ptr_gen;
4527
4528 bytenr = sorted[i].bytenr;
4529
4530 /*
4531 * check the reference count on this leaf. If it is > 1
4532 * we just decrement it below and don't update any
4533 * of the refs the leaf points to.
4534 */
Chris Mason56bec292009-03-13 10:10:06 -04004535 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4536 blocksize, &refs);
Chris Masonbd56b302009-02-04 09:27:02 -05004537 BUG_ON(ret);
4538 if (refs != 1)
4539 continue;
4540
4541 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
4542
4543 /*
4544 * the leaf only had one reference, which means the
4545 * only thing pointing to this leaf is the snapshot
4546 * we're deleting. It isn't possible for the reference
4547 * count to increase again later
4548 *
4549 * The reference cache is checked for the leaf,
4550 * and if found we'll be able to drop any refs held by
4551 * the leaf without needing to read it in.
4552 */
4553 ref = btrfs_lookup_leaf_ref(root, bytenr);
4554 if (ref && ref->generation != ptr_gen) {
4555 btrfs_free_leaf_ref(root, ref);
4556 ref = NULL;
4557 }
4558 if (ref) {
4559 ret = cache_drop_leaf_ref(trans, root, ref);
4560 BUG_ON(ret);
4561 btrfs_remove_leaf_ref(root, ref);
4562 btrfs_free_leaf_ref(root, ref);
4563 } else {
4564 /*
4565 * the leaf wasn't in the reference cache, so
4566 * we have to read it.
4567 */
4568 leaf = read_tree_block(root, bytenr, blocksize,
4569 ptr_gen);
4570 ret = btrfs_drop_leaf_ref(trans, root, leaf);
4571 BUG_ON(ret);
4572 free_extent_buffer(leaf);
4573 }
4574 atomic_inc(&root->fs_info->throttle_gen);
4575 wake_up(&root->fs_info->transaction_throttle);
4576 cond_resched();
4577 }
4578
4579 /*
4580 * run through the loop again to free the refs on the leaves.
4581 * This is faster than doing it in the loop above because
4582 * the leaves are likely to be clustered together. We end up
4583 * working in nice chunks on the extent allocation tree.
4584 */
4585 for (i = 0; i < refi; i++) {
4586 bytenr = sorted[i].bytenr;
Chris Mason56bec292009-03-13 10:10:06 -04004587 ret = btrfs_free_extent(trans, root, bytenr,
Chris Masonbd56b302009-02-04 09:27:02 -05004588 blocksize, eb->start,
4589 root_owner, root_gen, 0, 1);
4590 BUG_ON(ret);
4591
4592 atomic_inc(&root->fs_info->throttle_gen);
4593 wake_up(&root->fs_info->transaction_throttle);
4594 cond_resched();
4595 }
4596out:
4597 kfree(sorted);
4598
4599 /*
4600 * update the path to show we've processed the entire level 1
4601 * node. This will get saved into the root's drop_snapshot_progress
4602 * field so these drops are not repeated again if this transaction
4603 * commits.
4604 */
4605 path->slots[1] = nritems;
4606 return 0;
4607}
4608
4609/*
Chris Mason9aca1d52007-03-13 11:09:37 -04004610 * helper function for drop_snapshot, this walks down the tree dropping ref
4611 * counts as it goes.
4612 */
Chris Masond3977122009-01-05 21:25:51 -05004613static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004614 struct btrfs_root *root,
4615 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05004616{
Chris Mason7bb86312007-12-11 09:25:06 -05004617 u64 root_owner;
4618 u64 root_gen;
4619 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04004620 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04004621 struct extent_buffer *next;
4622 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05004623 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04004624 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05004625 int ret;
4626 u32 refs;
4627
Chris Mason5caf2a02007-04-02 11:20:42 -04004628 WARN_ON(*level < 0);
4629 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason56bec292009-03-13 10:10:06 -04004630 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04004631 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05004632 BUG_ON(ret);
4633 if (refs > 1)
4634 goto out;
Chris Masone0115992007-06-19 16:23:05 -04004635
Chris Mason9aca1d52007-03-13 11:09:37 -04004636 /*
4637 * walk down to the last node level and free all the leaves
4638 */
Chris Masond3977122009-01-05 21:25:51 -05004639 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04004640 WARN_ON(*level < 0);
4641 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05004642 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04004643
Chris Mason5f39d392007-10-15 16:14:19 -04004644 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04004645 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04004646
Chris Mason7518a232007-03-12 12:01:18 -04004647 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04004648 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05004649 break;
Chris Masonbd56b302009-02-04 09:27:02 -05004650
4651 /* the new code goes down to level 1 and does all the
4652 * leaves pointed to that node in bulk. So, this check
4653 * for level 0 will always be false.
4654 *
4655 * But, the disk format allows the drop_snapshot_progress
4656 * field in the root to leave things in a state where
4657 * a leaf will need cleaning up here. If someone crashes
4658 * with the old code and then boots with the new code,
4659 * we might find a leaf here.
4660 */
Chris Mason6407bf62007-03-27 06:33:00 -04004661 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004662 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04004663 BUG_ON(ret);
4664 break;
4665 }
Chris Masonbd56b302009-02-04 09:27:02 -05004666
4667 /*
4668 * once we get to level one, process the whole node
4669 * at once, including everything below it.
4670 */
4671 if (*level == 1) {
4672 ret = drop_level_one_refs(trans, root, path);
4673 BUG_ON(ret);
4674 break;
4675 }
4676
Chris Masondb945352007-10-15 16:15:53 -04004677 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04004678 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04004679 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04004680
Chris Mason56bec292009-03-13 10:10:06 -04004681 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4682 blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04004683 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05004684
4685 /*
4686 * if there is more than one reference, we don't need
4687 * to read that node to drop any references it has. We
4688 * just drop the ref we hold on that node and move on to the
4689 * next slot in this level.
4690 */
Chris Mason6407bf62007-03-27 06:33:00 -04004691 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05004692 parent = path->nodes[*level];
4693 root_owner = btrfs_header_owner(parent);
4694 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05004695 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04004696
Chris Mason56bec292009-03-13 10:10:06 -04004697 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004698 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004699 root_owner, root_gen,
4700 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05004701 BUG_ON(ret);
Chris Mason18e35e02008-08-01 13:11:41 -04004702
4703 atomic_inc(&root->fs_info->throttle_gen);
4704 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04004705 cond_resched();
Chris Mason18e35e02008-08-01 13:11:41 -04004706
Chris Mason20524f02007-03-10 06:35:47 -05004707 continue;
4708 }
Chris Mason333db942008-06-25 16:01:30 -04004709
Chris Masonbd56b302009-02-04 09:27:02 -05004710 /*
4711 * we need to keep freeing things in the next level down.
4712 * read the block and loop around to process it
4713 */
4714 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04004715 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04004716 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04004717 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05004718 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04004719 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05004720 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04004721 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004722 }
4723out:
Chris Mason5caf2a02007-04-02 11:20:42 -04004724 WARN_ON(*level < 0);
4725 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05004726
4727 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05004728 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04004729 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05004730 } else {
4731 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04004732 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05004733 }
4734
Yan Zheng31153d82008-07-28 15:32:19 -04004735 blocksize = btrfs_level_size(root, *level);
4736 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05004737 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04004738
Chris Masonbd56b302009-02-04 09:27:02 -05004739 /*
4740 * cleanup and free the reference on the last node
4741 * we processed
4742 */
Chris Mason56bec292009-03-13 10:10:06 -04004743 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04004744 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004745 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004746 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05004747 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05004748
Chris Mason20524f02007-03-10 06:35:47 -05004749 *level += 1;
4750 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04004751
Chris Masone7a84562008-06-25 16:01:31 -04004752 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004753 return 0;
4754}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004755#endif
Chris Mason20524f02007-03-10 06:35:47 -05004756
Yan Zheng2c47e6052009-06-27 21:07:35 -04004757struct walk_control {
4758 u64 refs[BTRFS_MAX_LEVEL];
4759 u64 flags[BTRFS_MAX_LEVEL];
4760 struct btrfs_key update_progress;
4761 int stage;
4762 int level;
4763 int shared_level;
4764 int update_ref;
4765 int keep_locks;
4766};
4767
4768#define DROP_REFERENCE 1
4769#define UPDATE_BACKREF 2
4770
Chris Mason9aca1d52007-03-13 11:09:37 -04004771/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04004772 * hepler to process tree block while walking down the tree.
4773 *
4774 * when wc->stage == DROP_REFERENCE, this function checks
4775 * reference count of the block. if the block is shared and
4776 * we need update back refs for the subtree rooted at the
4777 * block, this function changes wc->stage to UPDATE_BACKREF
4778 *
4779 * when wc->stage == UPDATE_BACKREF, this function updates
4780 * back refs for pointers in the block.
4781 *
4782 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004783 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004784static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4785 struct btrfs_root *root,
4786 struct btrfs_path *path,
4787 struct walk_control *wc)
4788{
4789 int level = wc->level;
4790 struct extent_buffer *eb = path->nodes[level];
4791 struct btrfs_key key;
4792 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4793 int ret;
4794
4795 if (wc->stage == UPDATE_BACKREF &&
4796 btrfs_header_owner(eb) != root->root_key.objectid)
4797 return 1;
4798
4799 /*
4800 * when reference count of tree block is 1, it won't increase
4801 * again. once full backref flag is set, we never clear it.
4802 */
4803 if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4804 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) {
4805 BUG_ON(!path->locks[level]);
4806 ret = btrfs_lookup_extent_info(trans, root,
4807 eb->start, eb->len,
4808 &wc->refs[level],
4809 &wc->flags[level]);
4810 BUG_ON(ret);
4811 BUG_ON(wc->refs[level] == 0);
4812 }
4813
4814 if (wc->stage == DROP_REFERENCE &&
4815 wc->update_ref && wc->refs[level] > 1) {
4816 BUG_ON(eb == root->node);
4817 BUG_ON(path->slots[level] > 0);
4818 if (level == 0)
4819 btrfs_item_key_to_cpu(eb, &key, path->slots[level]);
4820 else
4821 btrfs_node_key_to_cpu(eb, &key, path->slots[level]);
4822 if (btrfs_header_owner(eb) == root->root_key.objectid &&
4823 btrfs_comp_cpu_keys(&key, &wc->update_progress) >= 0) {
4824 wc->stage = UPDATE_BACKREF;
4825 wc->shared_level = level;
4826 }
4827 }
4828
4829 if (wc->stage == DROP_REFERENCE) {
4830 if (wc->refs[level] > 1)
4831 return 1;
4832
4833 if (path->locks[level] && !wc->keep_locks) {
4834 btrfs_tree_unlock(eb);
4835 path->locks[level] = 0;
4836 }
4837 return 0;
4838 }
4839
4840 /* wc->stage == UPDATE_BACKREF */
4841 if (!(wc->flags[level] & flag)) {
4842 BUG_ON(!path->locks[level]);
4843 ret = btrfs_inc_ref(trans, root, eb, 1);
4844 BUG_ON(ret);
4845 ret = btrfs_dec_ref(trans, root, eb, 0);
4846 BUG_ON(ret);
4847 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
4848 eb->len, flag, 0);
4849 BUG_ON(ret);
4850 wc->flags[level] |= flag;
4851 }
4852
4853 /*
4854 * the block is shared by multiple trees, so it's not good to
4855 * keep the tree lock
4856 */
4857 if (path->locks[level] && level > 0) {
4858 btrfs_tree_unlock(eb);
4859 path->locks[level] = 0;
4860 }
4861 return 0;
4862}
4863
4864/*
4865 * hepler to process tree block while walking up the tree.
4866 *
4867 * when wc->stage == DROP_REFERENCE, this function drops
4868 * reference count on the block.
4869 *
4870 * when wc->stage == UPDATE_BACKREF, this function changes
4871 * wc->stage back to DROP_REFERENCE if we changed wc->stage
4872 * to UPDATE_BACKREF previously while processing the block.
4873 *
4874 * NOTE: return value 1 means we should stop walking up.
4875 */
4876static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
4877 struct btrfs_root *root,
4878 struct btrfs_path *path,
4879 struct walk_control *wc)
4880{
4881 int ret = 0;
4882 int level = wc->level;
4883 struct extent_buffer *eb = path->nodes[level];
4884 u64 parent = 0;
4885
4886 if (wc->stage == UPDATE_BACKREF) {
4887 BUG_ON(wc->shared_level < level);
4888 if (level < wc->shared_level)
4889 goto out;
4890
4891 BUG_ON(wc->refs[level] <= 1);
4892 ret = find_next_key(path, level + 1, &wc->update_progress);
4893 if (ret > 0)
4894 wc->update_ref = 0;
4895
4896 wc->stage = DROP_REFERENCE;
4897 wc->shared_level = -1;
4898 path->slots[level] = 0;
4899
4900 /*
4901 * check reference count again if the block isn't locked.
4902 * we should start walking down the tree again if reference
4903 * count is one.
4904 */
4905 if (!path->locks[level]) {
4906 BUG_ON(level == 0);
4907 btrfs_tree_lock(eb);
4908 btrfs_set_lock_blocking(eb);
4909 path->locks[level] = 1;
4910
4911 ret = btrfs_lookup_extent_info(trans, root,
4912 eb->start, eb->len,
4913 &wc->refs[level],
4914 &wc->flags[level]);
4915 BUG_ON(ret);
4916 BUG_ON(wc->refs[level] == 0);
4917 if (wc->refs[level] == 1) {
4918 btrfs_tree_unlock(eb);
4919 path->locks[level] = 0;
4920 return 1;
4921 }
4922 } else {
4923 BUG_ON(level != 0);
4924 }
4925 }
4926
4927 /* wc->stage == DROP_REFERENCE */
4928 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
4929
4930 if (wc->refs[level] == 1) {
4931 if (level == 0) {
4932 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4933 ret = btrfs_dec_ref(trans, root, eb, 1);
4934 else
4935 ret = btrfs_dec_ref(trans, root, eb, 0);
4936 BUG_ON(ret);
4937 }
4938 /* make block locked assertion in clean_tree_block happy */
4939 if (!path->locks[level] &&
4940 btrfs_header_generation(eb) == trans->transid) {
4941 btrfs_tree_lock(eb);
4942 btrfs_set_lock_blocking(eb);
4943 path->locks[level] = 1;
4944 }
4945 clean_tree_block(trans, root, eb);
4946 }
4947
4948 if (eb == root->node) {
4949 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4950 parent = eb->start;
4951 else
4952 BUG_ON(root->root_key.objectid !=
4953 btrfs_header_owner(eb));
4954 } else {
4955 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4956 parent = path->nodes[level + 1]->start;
4957 else
4958 BUG_ON(root->root_key.objectid !=
4959 btrfs_header_owner(path->nodes[level + 1]));
4960 }
4961
4962 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
4963 root->root_key.objectid, level, 0);
4964 BUG_ON(ret);
4965out:
4966 wc->refs[level] = 0;
4967 wc->flags[level] = 0;
4968 return ret;
4969}
4970
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004971static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4972 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004973 struct btrfs_path *path,
4974 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04004975{
4976 struct extent_buffer *next;
4977 struct extent_buffer *cur;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004978 u64 bytenr;
4979 u64 ptr_gen;
4980 u32 blocksize;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004981 int level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004982 int ret;
4983
Yan Zheng2c47e6052009-06-27 21:07:35 -04004984 while (level >= 0) {
4985 cur = path->nodes[level];
4986 BUG_ON(path->slots[level] >= btrfs_header_nritems(cur));
Yan Zhengf82d02d2008-10-29 14:49:05 -04004987
Yan Zheng2c47e6052009-06-27 21:07:35 -04004988 ret = walk_down_proc(trans, root, path, wc);
4989 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04004990 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004991
Yan Zheng2c47e6052009-06-27 21:07:35 -04004992 if (level == 0)
4993 break;
4994
4995 bytenr = btrfs_node_blockptr(cur, path->slots[level]);
4996 blocksize = btrfs_level_size(root, level - 1);
4997 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[level]);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004998
4999 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
5000 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005001 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005002
Yan Zheng2c47e6052009-06-27 21:07:35 -04005003 level--;
5004 BUG_ON(level != btrfs_header_level(next));
5005 path->nodes[level] = next;
5006 path->slots[level] = 0;
5007 path->locks[level] = 1;
5008 wc->level = level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005009 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005010 return 0;
5011}
5012
Chris Masond3977122009-01-05 21:25:51 -05005013static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005014 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005015 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005016 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005017{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005018 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005019 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005020
Yan Zheng2c47e6052009-06-27 21:07:35 -04005021 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5022 while (level < max_level && path->nodes[level]) {
5023 wc->level = level;
5024 if (path->slots[level] + 1 <
5025 btrfs_header_nritems(path->nodes[level])) {
5026 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005027 return 0;
5028 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005029 ret = walk_up_proc(trans, root, path, wc);
5030 if (ret > 0)
5031 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05005032
Yan Zheng2c47e6052009-06-27 21:07:35 -04005033 if (path->locks[level]) {
5034 btrfs_tree_unlock(path->nodes[level]);
5035 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005036 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005037 free_extent_buffer(path->nodes[level]);
5038 path->nodes[level] = NULL;
5039 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005040 }
5041 }
5042 return 1;
5043}
5044
Chris Mason9aca1d52007-03-13 11:09:37 -04005045/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005046 * drop a subvolume tree.
5047 *
5048 * this function traverses the tree freeing any blocks that only
5049 * referenced by the tree.
5050 *
5051 * when a shared tree block is found. this function decreases its
5052 * reference count by one. if update_ref is true, this function
5053 * also make sure backrefs for the shared block and all lower level
5054 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04005055 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005056int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05005057{
Chris Mason5caf2a02007-04-02 11:20:42 -04005058 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005059 struct btrfs_trans_handle *trans;
5060 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005061 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005062 struct walk_control *wc;
5063 struct btrfs_key key;
5064 int err = 0;
5065 int ret;
5066 int level;
Chris Mason20524f02007-03-10 06:35:47 -05005067
Chris Mason5caf2a02007-04-02 11:20:42 -04005068 path = btrfs_alloc_path();
5069 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05005070
Yan Zheng2c47e6052009-06-27 21:07:35 -04005071 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5072 BUG_ON(!wc);
5073
5074 trans = btrfs_start_transaction(tree_root, 1);
5075
Chris Mason9f3a7422007-08-07 15:52:19 -04005076 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005077 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005078 path->nodes[level] = btrfs_lock_root_node(root);
5079 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005080 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005081 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005082 memset(&wc->update_progress, 0,
5083 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005084 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005085 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005086 memcpy(&wc->update_progress, &key,
5087 sizeof(wc->update_progress));
5088
Chris Mason6702ed42007-08-07 16:15:09 -04005089 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005090 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005091 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005092 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5093 path->lowest_level = 0;
5094 if (ret < 0) {
5095 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005096 goto out;
5097 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005098 btrfs_node_key_to_cpu(path->nodes[level], &key,
5099 path->slots[level]);
5100 WARN_ON(memcmp(&key, &wc->update_progress, sizeof(key)));
5101
Chris Mason7d9eb122008-07-08 14:19:17 -04005102 /*
5103 * unlock our path, this is safe because only this
5104 * function is allowed to delete this snapshot
5105 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005106 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005107
Yan Zheng2c47e6052009-06-27 21:07:35 -04005108 level = btrfs_header_level(root->node);
5109 while (1) {
5110 btrfs_tree_lock(path->nodes[level]);
5111 btrfs_set_lock_blocking(path->nodes[level]);
5112
5113 ret = btrfs_lookup_extent_info(trans, root,
5114 path->nodes[level]->start,
5115 path->nodes[level]->len,
5116 &wc->refs[level],
5117 &wc->flags[level]);
5118 BUG_ON(ret);
5119 BUG_ON(wc->refs[level] == 0);
5120
5121 if (level == root_item->drop_level)
5122 break;
5123
5124 btrfs_tree_unlock(path->nodes[level]);
5125 WARN_ON(wc->refs[level] != 1);
5126 level--;
5127 }
5128 }
5129
5130 wc->level = level;
5131 wc->shared_level = -1;
5132 wc->stage = DROP_REFERENCE;
5133 wc->update_ref = update_ref;
5134 wc->keep_locks = 0;
5135
5136 while (1) {
5137 ret = walk_down_tree(trans, root, path, wc);
5138 if (ret < 0) {
5139 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005140 break;
5141 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005142
5143 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5144 if (ret < 0) {
5145 err = ret;
5146 break;
5147 }
5148
5149 if (ret > 0) {
5150 BUG_ON(wc->stage != DROP_REFERENCE);
5151 break;
5152 }
5153
5154 if (wc->stage == DROP_REFERENCE) {
5155 level = wc->level;
5156 btrfs_node_key(path->nodes[level],
5157 &root_item->drop_progress,
5158 path->slots[level]);
5159 root_item->drop_level = level;
5160 }
5161
5162 BUG_ON(wc->level == 0);
5163 if (trans->transaction->in_commit ||
5164 trans->transaction->delayed_refs.flushing) {
5165 ret = btrfs_update_root(trans, tree_root,
5166 &root->root_key,
5167 root_item);
5168 BUG_ON(ret);
5169
5170 btrfs_end_transaction(trans, tree_root);
5171 trans = btrfs_start_transaction(tree_root, 1);
5172 } else {
5173 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005174 update = trans->delayed_ref_updates;
5175 trans->delayed_ref_updates = 0;
5176 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005177 btrfs_run_delayed_refs(trans, tree_root,
5178 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005179 }
Chris Mason20524f02007-03-10 06:35:47 -05005180 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005181 btrfs_release_path(root, path);
5182 BUG_ON(err);
5183
5184 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5185 BUG_ON(ret);
5186
5187 free_extent_buffer(root->node);
5188 free_extent_buffer(root->commit_root);
5189 kfree(root);
Chris Mason9f3a7422007-08-07 15:52:19 -04005190out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005191 btrfs_end_transaction(trans, tree_root);
5192 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005193 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005194 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005195}
Chris Mason9078a3e2007-04-26 16:46:15 -04005196
Yan Zheng2c47e6052009-06-27 21:07:35 -04005197/*
5198 * drop subtree rooted at tree block 'node'.
5199 *
5200 * NOTE: this function will unlock and release tree block 'node'
5201 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005202int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5203 struct btrfs_root *root,
5204 struct extent_buffer *node,
5205 struct extent_buffer *parent)
5206{
5207 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005208 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005209 int level;
5210 int parent_level;
5211 int ret = 0;
5212 int wret;
5213
Yan Zheng2c47e6052009-06-27 21:07:35 -04005214 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5215
Yan Zhengf82d02d2008-10-29 14:49:05 -04005216 path = btrfs_alloc_path();
5217 BUG_ON(!path);
5218
Yan Zheng2c47e6052009-06-27 21:07:35 -04005219 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5220 BUG_ON(!wc);
5221
Chris Masonb9447ef2009-03-09 11:45:38 -04005222 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005223 parent_level = btrfs_header_level(parent);
5224 extent_buffer_get(parent);
5225 path->nodes[parent_level] = parent;
5226 path->slots[parent_level] = btrfs_header_nritems(parent);
5227
Chris Masonb9447ef2009-03-09 11:45:38 -04005228 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005229 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005230 path->nodes[level] = node;
5231 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005232 path->locks[level] = 1;
5233
5234 wc->refs[parent_level] = 1;
5235 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5236 wc->level = level;
5237 wc->shared_level = -1;
5238 wc->stage = DROP_REFERENCE;
5239 wc->update_ref = 0;
5240 wc->keep_locks = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005241
5242 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005243 wret = walk_down_tree(trans, root, path, wc);
5244 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005245 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005246 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005247 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005248
Yan Zheng2c47e6052009-06-27 21:07:35 -04005249 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005250 if (wret < 0)
5251 ret = wret;
5252 if (wret != 0)
5253 break;
5254 }
5255
Yan Zheng2c47e6052009-06-27 21:07:35 -04005256 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005257 btrfs_free_path(path);
5258 return ret;
5259}
5260
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005261#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005262static unsigned long calc_ra(unsigned long start, unsigned long last,
5263 unsigned long nr)
5264{
5265 return min(last, start + nr - 1);
5266}
5267
Chris Masond3977122009-01-05 21:25:51 -05005268static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005269 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005270{
5271 u64 page_start;
5272 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005273 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005274 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005275 unsigned long i;
5276 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005277 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005278 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005279 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005280 unsigned int total_read = 0;
5281 unsigned int total_dirty = 0;
5282 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005283
5284 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005285
5286 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005287 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005288 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5289
Zheng Yan1a40e232008-09-26 10:09:34 -04005290 /* make sure the dirty trick played by the caller work */
5291 ret = invalidate_inode_pages2_range(inode->i_mapping,
5292 first_index, last_index);
5293 if (ret)
5294 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005295
Chris Mason4313b392008-01-03 09:08:48 -05005296 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005297
Zheng Yan1a40e232008-09-26 10:09:34 -04005298 for (i = first_index ; i <= last_index; i++) {
5299 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005300 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005301 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005302 }
5303 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005304again:
5305 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005306 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005307 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005308 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005309 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005310 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005311 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005312 if (!PageUptodate(page)) {
5313 btrfs_readpage(NULL, page);
5314 lock_page(page);
5315 if (!PageUptodate(page)) {
5316 unlock_page(page);
5317 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005318 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005319 goto out_unlock;
5320 }
5321 }
Chris Masonec44a352008-04-28 15:29:52 -04005322 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005323
Chris Masonedbd8d42007-12-21 16:27:24 -05005324 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5325 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005326 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005327
Chris Mason3eaa2882008-07-24 11:57:52 -04005328 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5329 if (ordered) {
5330 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5331 unlock_page(page);
5332 page_cache_release(page);
5333 btrfs_start_ordered_extent(inode, ordered, 1);
5334 btrfs_put_ordered_extent(ordered);
5335 goto again;
5336 }
5337 set_page_extent_mapped(page);
5338
Zheng Yan1a40e232008-09-26 10:09:34 -04005339 if (i == first_index)
5340 set_extent_bits(io_tree, page_start, page_end,
5341 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005342 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005343
Chris Masona061fc82008-05-07 11:43:44 -04005344 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005345 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005346
Chris Masond1310b22008-01-24 16:13:08 -05005347 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005348 unlock_page(page);
5349 page_cache_release(page);
5350 }
5351
5352out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005353 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005354 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005355 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005356 return ret;
5357}
5358
Chris Masond3977122009-01-05 21:25:51 -05005359static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005360 struct btrfs_key *extent_key,
5361 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005362{
Zheng Yan1a40e232008-09-26 10:09:34 -04005363 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5364 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5365 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005366 u64 start = extent_key->objectid - offset;
5367 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005368
5369 em = alloc_extent_map(GFP_NOFS);
5370 BUG_ON(!em || IS_ERR(em));
5371
Yan Zheng66435582008-10-30 14:19:50 -04005372 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005373 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005374 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005375 em->block_start = extent_key->objectid;
5376 em->bdev = root->fs_info->fs_devices->latest_bdev;
5377 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5378
5379 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005380 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005381 while (1) {
5382 int ret;
5383 spin_lock(&em_tree->lock);
5384 ret = add_extent_mapping(em_tree, em);
5385 spin_unlock(&em_tree->lock);
5386 if (ret != -EEXIST) {
5387 free_extent_map(em);
5388 break;
5389 }
Yan Zheng66435582008-10-30 14:19:50 -04005390 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005391 }
Yan Zheng66435582008-10-30 14:19:50 -04005392 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005393
Yan Zheng66435582008-10-30 14:19:50 -04005394 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005395}
5396
5397struct btrfs_ref_path {
5398 u64 extent_start;
5399 u64 nodes[BTRFS_MAX_LEVEL];
5400 u64 root_objectid;
5401 u64 root_generation;
5402 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005403 u32 num_refs;
5404 int lowest_level;
5405 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005406 int shared_level;
5407
5408 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5409 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005410};
5411
5412struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005413 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005414 u64 disk_bytenr;
5415 u64 disk_num_bytes;
5416 u64 offset;
5417 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005418 u8 compression;
5419 u8 encryption;
5420 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005421};
5422
5423static int is_cowonly_root(u64 root_objectid)
5424{
5425 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5426 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5427 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5428 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005429 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5430 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005431 return 1;
5432 return 0;
5433}
5434
Chris Masond3977122009-01-05 21:25:51 -05005435static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005436 struct btrfs_root *extent_root,
5437 struct btrfs_ref_path *ref_path,
5438 int first_time)
5439{
5440 struct extent_buffer *leaf;
5441 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005442 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005443 struct btrfs_key key;
5444 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005445 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005446 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005447 int level;
5448 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005449
Zheng Yan1a40e232008-09-26 10:09:34 -04005450 path = btrfs_alloc_path();
5451 if (!path)
5452 return -ENOMEM;
5453
Zheng Yan1a40e232008-09-26 10:09:34 -04005454 if (first_time) {
5455 ref_path->lowest_level = -1;
5456 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005457 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005458 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005459 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005460walk_down:
5461 level = ref_path->current_level - 1;
5462 while (level >= -1) {
5463 u64 parent;
5464 if (level < ref_path->lowest_level)
5465 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005466
Chris Masond3977122009-01-05 21:25:51 -05005467 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005468 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005469 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005470 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005471 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005472
Zheng Yan1a40e232008-09-26 10:09:34 -04005473 parent = ref_path->nodes[level + 1];
5474 ref_path->nodes[level + 1] = 0;
5475 ref_path->current_level = level;
5476 BUG_ON(parent == 0);
5477
5478 key.objectid = bytenr;
5479 key.offset = parent + 1;
5480 key.type = BTRFS_EXTENT_REF_KEY;
5481
5482 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005483 if (ret < 0)
5484 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005485 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005486
Chris Masonedbd8d42007-12-21 16:27:24 -05005487 leaf = path->nodes[0];
5488 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005489 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005490 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005491 if (ret < 0)
5492 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005493 if (ret > 0)
5494 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005495 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005496 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005497
5498 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005499 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005500 found_key.type == BTRFS_EXTENT_REF_KEY) {
5501 if (level < ref_path->shared_level)
5502 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005503 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005504 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005505next:
5506 level--;
5507 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005508 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005509 }
5510 /* reached lowest level */
5511 ret = 1;
5512 goto out;
5513walk_up:
5514 level = ref_path->current_level;
5515 while (level < BTRFS_MAX_LEVEL - 1) {
5516 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005517
5518 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005519 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005520 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005521 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005522
Zheng Yan1a40e232008-09-26 10:09:34 -04005523 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005524
Zheng Yan1a40e232008-09-26 10:09:34 -04005525 key.objectid = bytenr;
5526 key.offset = 0;
5527 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005528
Zheng Yan1a40e232008-09-26 10:09:34 -04005529 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5530 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005531 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005532
5533 leaf = path->nodes[0];
5534 nritems = btrfs_header_nritems(leaf);
5535 if (path->slots[0] >= nritems) {
5536 ret = btrfs_next_leaf(extent_root, path);
5537 if (ret < 0)
5538 goto out;
5539 if (ret > 0) {
5540 /* the extent was freed by someone */
5541 if (ref_path->lowest_level == level)
5542 goto out;
5543 btrfs_release_path(extent_root, path);
5544 goto walk_down;
5545 }
5546 leaf = path->nodes[0];
5547 }
5548
5549 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5550 if (found_key.objectid != bytenr ||
5551 found_key.type != BTRFS_EXTENT_REF_KEY) {
5552 /* the extent was freed by someone */
5553 if (ref_path->lowest_level == level) {
5554 ret = 1;
5555 goto out;
5556 }
5557 btrfs_release_path(extent_root, path);
5558 goto walk_down;
5559 }
5560found:
5561 ref = btrfs_item_ptr(leaf, path->slots[0],
5562 struct btrfs_extent_ref);
5563 ref_objectid = btrfs_ref_objectid(leaf, ref);
5564 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5565 if (first_time) {
5566 level = (int)ref_objectid;
5567 BUG_ON(level >= BTRFS_MAX_LEVEL);
5568 ref_path->lowest_level = level;
5569 ref_path->current_level = level;
5570 ref_path->nodes[level] = bytenr;
5571 } else {
5572 WARN_ON(ref_objectid != level);
5573 }
5574 } else {
5575 WARN_ON(level != -1);
5576 }
5577 first_time = 0;
5578
5579 if (ref_path->lowest_level == level) {
5580 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005581 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5582 }
5583
5584 /*
5585 * the block is tree root or the block isn't in reference
5586 * counted tree.
5587 */
5588 if (found_key.objectid == found_key.offset ||
5589 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5590 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5591 ref_path->root_generation =
5592 btrfs_ref_generation(leaf, ref);
5593 if (level < 0) {
5594 /* special reference from the tree log */
5595 ref_path->nodes[0] = found_key.offset;
5596 ref_path->current_level = 0;
5597 }
5598 ret = 0;
5599 goto out;
5600 }
5601
5602 level++;
5603 BUG_ON(ref_path->nodes[level] != 0);
5604 ref_path->nodes[level] = found_key.offset;
5605 ref_path->current_level = level;
5606
5607 /*
5608 * the reference was created in the running transaction,
5609 * no need to continue walking up.
5610 */
5611 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5612 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5613 ref_path->root_generation =
5614 btrfs_ref_generation(leaf, ref);
5615 ret = 0;
5616 goto out;
5617 }
5618
5619 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005620 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005621 }
5622 /* reached max tree level, but no tree root found. */
5623 BUG();
5624out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005625 btrfs_free_path(path);
5626 return ret;
5627}
5628
5629static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5630 struct btrfs_root *extent_root,
5631 struct btrfs_ref_path *ref_path,
5632 u64 extent_start)
5633{
5634 memset(ref_path, 0, sizeof(*ref_path));
5635 ref_path->extent_start = extent_start;
5636
5637 return __next_ref_path(trans, extent_root, ref_path, 1);
5638}
5639
5640static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5641 struct btrfs_root *extent_root,
5642 struct btrfs_ref_path *ref_path)
5643{
5644 return __next_ref_path(trans, extent_root, ref_path, 0);
5645}
5646
Chris Masond3977122009-01-05 21:25:51 -05005647static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005648 struct btrfs_key *extent_key,
5649 u64 offset, int no_fragment,
5650 struct disk_extent **extents,
5651 int *nr_extents)
5652{
5653 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5654 struct btrfs_path *path;
5655 struct btrfs_file_extent_item *fi;
5656 struct extent_buffer *leaf;
5657 struct disk_extent *exts = *extents;
5658 struct btrfs_key found_key;
5659 u64 cur_pos;
5660 u64 last_byte;
5661 u32 nritems;
5662 int nr = 0;
5663 int max = *nr_extents;
5664 int ret;
5665
5666 WARN_ON(!no_fragment && *extents);
5667 if (!exts) {
5668 max = 1;
5669 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
5670 if (!exts)
5671 return -ENOMEM;
5672 }
5673
5674 path = btrfs_alloc_path();
5675 BUG_ON(!path);
5676
5677 cur_pos = extent_key->objectid - offset;
5678 last_byte = extent_key->objectid + extent_key->offset;
5679 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
5680 cur_pos, 0);
5681 if (ret < 0)
5682 goto out;
5683 if (ret > 0) {
5684 ret = -ENOENT;
5685 goto out;
5686 }
5687
5688 while (1) {
5689 leaf = path->nodes[0];
5690 nritems = btrfs_header_nritems(leaf);
5691 if (path->slots[0] >= nritems) {
5692 ret = btrfs_next_leaf(root, path);
5693 if (ret < 0)
5694 goto out;
5695 if (ret > 0)
5696 break;
5697 leaf = path->nodes[0];
5698 }
5699
5700 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5701 if (found_key.offset != cur_pos ||
5702 found_key.type != BTRFS_EXTENT_DATA_KEY ||
5703 found_key.objectid != reloc_inode->i_ino)
5704 break;
5705
5706 fi = btrfs_item_ptr(leaf, path->slots[0],
5707 struct btrfs_file_extent_item);
5708 if (btrfs_file_extent_type(leaf, fi) !=
5709 BTRFS_FILE_EXTENT_REG ||
5710 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5711 break;
5712
5713 if (nr == max) {
5714 struct disk_extent *old = exts;
5715 max *= 2;
5716 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
5717 memcpy(exts, old, sizeof(*exts) * nr);
5718 if (old != *extents)
5719 kfree(old);
5720 }
5721
5722 exts[nr].disk_bytenr =
5723 btrfs_file_extent_disk_bytenr(leaf, fi);
5724 exts[nr].disk_num_bytes =
5725 btrfs_file_extent_disk_num_bytes(leaf, fi);
5726 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
5727 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04005728 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
5729 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
5730 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
5731 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
5732 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04005733 BUG_ON(exts[nr].offset > 0);
5734 BUG_ON(exts[nr].compression || exts[nr].encryption);
5735 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005736
5737 cur_pos += exts[nr].num_bytes;
5738 nr++;
5739
5740 if (cur_pos + offset >= last_byte)
5741 break;
5742
5743 if (no_fragment) {
5744 ret = 1;
5745 goto out;
5746 }
5747 path->slots[0]++;
5748 }
5749
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005750 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04005751 if (cur_pos + offset < last_byte) {
5752 ret = -ENOENT;
5753 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05005754 }
5755 ret = 0;
5756out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005757 btrfs_free_path(path);
5758 if (ret) {
5759 if (exts != *extents)
5760 kfree(exts);
5761 } else {
5762 *extents = exts;
5763 *nr_extents = nr;
5764 }
5765 return ret;
5766}
5767
Chris Masond3977122009-01-05 21:25:51 -05005768static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005769 struct btrfs_root *root,
5770 struct btrfs_path *path,
5771 struct btrfs_key *extent_key,
5772 struct btrfs_key *leaf_key,
5773 struct btrfs_ref_path *ref_path,
5774 struct disk_extent *new_extents,
5775 int nr_extents)
5776{
5777 struct extent_buffer *leaf;
5778 struct btrfs_file_extent_item *fi;
5779 struct inode *inode = NULL;
5780 struct btrfs_key key;
5781 u64 lock_start = 0;
5782 u64 lock_end = 0;
5783 u64 num_bytes;
5784 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05005785 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005786 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005787 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005788 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04005789 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04005790 int ret;
5791
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005792 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04005793 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005794 if (key.objectid < ref_path->owner_objectid ||
5795 (key.objectid == ref_path->owner_objectid &&
5796 key.type < BTRFS_EXTENT_DATA_KEY)) {
5797 key.objectid = ref_path->owner_objectid;
5798 key.type = BTRFS_EXTENT_DATA_KEY;
5799 key.offset = 0;
5800 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005801 }
5802
5803 while (1) {
5804 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5805 if (ret < 0)
5806 goto out;
5807
5808 leaf = path->nodes[0];
5809 nritems = btrfs_header_nritems(leaf);
5810next:
5811 if (extent_locked && ret > 0) {
5812 /*
5813 * the file extent item was modified by someone
5814 * before the extent got locked.
5815 */
Zheng Yan1a40e232008-09-26 10:09:34 -04005816 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5817 lock_end, GFP_NOFS);
5818 extent_locked = 0;
5819 }
5820
5821 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005822 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04005823 break;
5824
5825 BUG_ON(extent_locked);
5826 ret = btrfs_next_leaf(root, path);
5827 if (ret < 0)
5828 goto out;
5829 if (ret > 0)
5830 break;
5831 leaf = path->nodes[0];
5832 nritems = btrfs_header_nritems(leaf);
5833 }
5834
5835 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5836
5837 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
5838 if ((key.objectid > ref_path->owner_objectid) ||
5839 (key.objectid == ref_path->owner_objectid &&
5840 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05005841 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005842 break;
5843 }
5844
5845 if (inode && key.objectid != inode->i_ino) {
5846 BUG_ON(extent_locked);
5847 btrfs_release_path(root, path);
5848 mutex_unlock(&inode->i_mutex);
5849 iput(inode);
5850 inode = NULL;
5851 continue;
5852 }
5853
5854 if (key.type != BTRFS_EXTENT_DATA_KEY) {
5855 path->slots[0]++;
5856 ret = 1;
5857 goto next;
5858 }
5859 fi = btrfs_item_ptr(leaf, path->slots[0],
5860 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04005861 extent_type = btrfs_file_extent_type(leaf, fi);
5862 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
5863 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04005864 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
5865 extent_key->objectid)) {
5866 path->slots[0]++;
5867 ret = 1;
5868 goto next;
5869 }
5870
5871 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5872 ext_offset = btrfs_file_extent_offset(leaf, fi);
5873
Yan Zheng86288a12009-01-21 10:49:16 -05005874 if (search_end == (u64)-1) {
5875 search_end = key.offset - ext_offset +
5876 btrfs_file_extent_ram_bytes(leaf, fi);
5877 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005878
5879 if (!extent_locked) {
5880 lock_start = key.offset;
5881 lock_end = lock_start + num_bytes - 1;
5882 } else {
Yan Zheng66435582008-10-30 14:19:50 -04005883 if (lock_start > key.offset ||
5884 lock_end + 1 < key.offset + num_bytes) {
5885 unlock_extent(&BTRFS_I(inode)->io_tree,
5886 lock_start, lock_end, GFP_NOFS);
5887 extent_locked = 0;
5888 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005889 }
5890
5891 if (!inode) {
5892 btrfs_release_path(root, path);
5893
5894 inode = btrfs_iget_locked(root->fs_info->sb,
5895 key.objectid, root);
5896 if (inode->i_state & I_NEW) {
5897 BTRFS_I(inode)->root = root;
5898 BTRFS_I(inode)->location.objectid =
5899 key.objectid;
5900 BTRFS_I(inode)->location.type =
5901 BTRFS_INODE_ITEM_KEY;
5902 BTRFS_I(inode)->location.offset = 0;
5903 btrfs_read_locked_inode(inode);
5904 unlock_new_inode(inode);
5905 }
5906 /*
5907 * some code call btrfs_commit_transaction while
5908 * holding the i_mutex, so we can't use mutex_lock
5909 * here.
5910 */
5911 if (is_bad_inode(inode) ||
5912 !mutex_trylock(&inode->i_mutex)) {
5913 iput(inode);
5914 inode = NULL;
5915 key.offset = (u64)-1;
5916 goto skip;
5917 }
5918 }
5919
5920 if (!extent_locked) {
5921 struct btrfs_ordered_extent *ordered;
5922
5923 btrfs_release_path(root, path);
5924
5925 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5926 lock_end, GFP_NOFS);
5927 ordered = btrfs_lookup_first_ordered_extent(inode,
5928 lock_end);
5929 if (ordered &&
5930 ordered->file_offset <= lock_end &&
5931 ordered->file_offset + ordered->len > lock_start) {
5932 unlock_extent(&BTRFS_I(inode)->io_tree,
5933 lock_start, lock_end, GFP_NOFS);
5934 btrfs_start_ordered_extent(inode, ordered, 1);
5935 btrfs_put_ordered_extent(ordered);
5936 key.offset += num_bytes;
5937 goto skip;
5938 }
5939 if (ordered)
5940 btrfs_put_ordered_extent(ordered);
5941
Zheng Yan1a40e232008-09-26 10:09:34 -04005942 extent_locked = 1;
5943 continue;
5944 }
5945
5946 if (nr_extents == 1) {
5947 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04005948 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5949 new_extents[0].disk_bytenr);
5950 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5951 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005952 btrfs_mark_buffer_dirty(leaf);
5953
5954 btrfs_drop_extent_cache(inode, key.offset,
5955 key.offset + num_bytes - 1, 0);
5956
5957 ret = btrfs_inc_extent_ref(trans, root,
5958 new_extents[0].disk_bytenr,
5959 new_extents[0].disk_num_bytes,
5960 leaf->start,
5961 root->root_key.objectid,
5962 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005963 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005964 BUG_ON(ret);
5965
5966 ret = btrfs_free_extent(trans, root,
5967 extent_key->objectid,
5968 extent_key->offset,
5969 leaf->start,
5970 btrfs_header_owner(leaf),
5971 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005972 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005973 BUG_ON(ret);
5974
5975 btrfs_release_path(root, path);
5976 key.offset += num_bytes;
5977 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04005978 BUG_ON(1);
5979#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04005980 u64 alloc_hint;
5981 u64 extent_len;
5982 int i;
5983 /*
5984 * drop old extent pointer at first, then insert the
5985 * new pointers one bye one
5986 */
5987 btrfs_release_path(root, path);
5988 ret = btrfs_drop_extents(trans, root, inode, key.offset,
5989 key.offset + num_bytes,
5990 key.offset, &alloc_hint);
5991 BUG_ON(ret);
5992
5993 for (i = 0; i < nr_extents; i++) {
5994 if (ext_offset >= new_extents[i].num_bytes) {
5995 ext_offset -= new_extents[i].num_bytes;
5996 continue;
5997 }
5998 extent_len = min(new_extents[i].num_bytes -
5999 ext_offset, num_bytes);
6000
6001 ret = btrfs_insert_empty_item(trans, root,
6002 path, &key,
6003 sizeof(*fi));
6004 BUG_ON(ret);
6005
6006 leaf = path->nodes[0];
6007 fi = btrfs_item_ptr(leaf, path->slots[0],
6008 struct btrfs_file_extent_item);
6009 btrfs_set_file_extent_generation(leaf, fi,
6010 trans->transid);
6011 btrfs_set_file_extent_type(leaf, fi,
6012 BTRFS_FILE_EXTENT_REG);
6013 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6014 new_extents[i].disk_bytenr);
6015 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6016 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04006017 btrfs_set_file_extent_ram_bytes(leaf, fi,
6018 new_extents[i].ram_bytes);
6019
6020 btrfs_set_file_extent_compression(leaf, fi,
6021 new_extents[i].compression);
6022 btrfs_set_file_extent_encryption(leaf, fi,
6023 new_extents[i].encryption);
6024 btrfs_set_file_extent_other_encoding(leaf, fi,
6025 new_extents[i].other_encoding);
6026
Zheng Yan1a40e232008-09-26 10:09:34 -04006027 btrfs_set_file_extent_num_bytes(leaf, fi,
6028 extent_len);
6029 ext_offset += new_extents[i].offset;
6030 btrfs_set_file_extent_offset(leaf, fi,
6031 ext_offset);
6032 btrfs_mark_buffer_dirty(leaf);
6033
6034 btrfs_drop_extent_cache(inode, key.offset,
6035 key.offset + extent_len - 1, 0);
6036
6037 ret = btrfs_inc_extent_ref(trans, root,
6038 new_extents[i].disk_bytenr,
6039 new_extents[i].disk_num_bytes,
6040 leaf->start,
6041 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006042 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006043 BUG_ON(ret);
6044 btrfs_release_path(root, path);
6045
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006046 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04006047
6048 ext_offset = 0;
6049 num_bytes -= extent_len;
6050 key.offset += extent_len;
6051
6052 if (num_bytes == 0)
6053 break;
6054 }
6055 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006056#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04006057 }
6058
6059 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006060 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6061 lock_end, GFP_NOFS);
6062 extent_locked = 0;
6063 }
6064skip:
6065 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05006066 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006067 break;
6068
6069 cond_resched();
6070 }
6071 ret = 0;
6072out:
6073 btrfs_release_path(root, path);
6074 if (inode) {
6075 mutex_unlock(&inode->i_mutex);
6076 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006077 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6078 lock_end, GFP_NOFS);
6079 }
6080 iput(inode);
6081 }
6082 return ret;
6083}
6084
Zheng Yan1a40e232008-09-26 10:09:34 -04006085int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6086 struct btrfs_root *root,
6087 struct extent_buffer *buf, u64 orig_start)
6088{
6089 int level;
6090 int ret;
6091
6092 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6093 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6094
6095 level = btrfs_header_level(buf);
6096 if (level == 0) {
6097 struct btrfs_leaf_ref *ref;
6098 struct btrfs_leaf_ref *orig_ref;
6099
6100 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6101 if (!orig_ref)
6102 return -ENOENT;
6103
6104 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6105 if (!ref) {
6106 btrfs_free_leaf_ref(root, orig_ref);
6107 return -ENOMEM;
6108 }
6109
6110 ref->nritems = orig_ref->nritems;
6111 memcpy(ref->extents, orig_ref->extents,
6112 sizeof(ref->extents[0]) * ref->nritems);
6113
6114 btrfs_free_leaf_ref(root, orig_ref);
6115
6116 ref->root_gen = trans->transid;
6117 ref->bytenr = buf->start;
6118 ref->owner = btrfs_header_owner(buf);
6119 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006120
Zheng Yan1a40e232008-09-26 10:09:34 -04006121 ret = btrfs_add_leaf_ref(root, ref, 0);
6122 WARN_ON(ret);
6123 btrfs_free_leaf_ref(root, ref);
6124 }
6125 return 0;
6126}
6127
Chris Masond3977122009-01-05 21:25:51 -05006128static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006129 struct extent_buffer *leaf,
6130 struct btrfs_block_group_cache *group,
6131 struct btrfs_root *target_root)
6132{
6133 struct btrfs_key key;
6134 struct inode *inode = NULL;
6135 struct btrfs_file_extent_item *fi;
6136 u64 num_bytes;
6137 u64 skip_objectid = 0;
6138 u32 nritems;
6139 u32 i;
6140
6141 nritems = btrfs_header_nritems(leaf);
6142 for (i = 0; i < nritems; i++) {
6143 btrfs_item_key_to_cpu(leaf, &key, i);
6144 if (key.objectid == skip_objectid ||
6145 key.type != BTRFS_EXTENT_DATA_KEY)
6146 continue;
6147 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6148 if (btrfs_file_extent_type(leaf, fi) ==
6149 BTRFS_FILE_EXTENT_INLINE)
6150 continue;
6151 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6152 continue;
6153 if (!inode || inode->i_ino != key.objectid) {
6154 iput(inode);
6155 inode = btrfs_ilookup(target_root->fs_info->sb,
6156 key.objectid, target_root, 1);
6157 }
6158 if (!inode) {
6159 skip_objectid = key.objectid;
6160 continue;
6161 }
6162 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6163
6164 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6165 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006166 btrfs_drop_extent_cache(inode, key.offset,
6167 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006168 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6169 key.offset + num_bytes - 1, GFP_NOFS);
6170 cond_resched();
6171 }
6172 iput(inode);
6173 return 0;
6174}
6175
Chris Masond3977122009-01-05 21:25:51 -05006176static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006177 struct btrfs_root *root,
6178 struct extent_buffer *leaf,
6179 struct btrfs_block_group_cache *group,
6180 struct inode *reloc_inode)
6181{
6182 struct btrfs_key key;
6183 struct btrfs_key extent_key;
6184 struct btrfs_file_extent_item *fi;
6185 struct btrfs_leaf_ref *ref;
6186 struct disk_extent *new_extent;
6187 u64 bytenr;
6188 u64 num_bytes;
6189 u32 nritems;
6190 u32 i;
6191 int ext_index;
6192 int nr_extent;
6193 int ret;
6194
6195 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6196 BUG_ON(!new_extent);
6197
6198 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6199 BUG_ON(!ref);
6200
6201 ext_index = -1;
6202 nritems = btrfs_header_nritems(leaf);
6203 for (i = 0; i < nritems; i++) {
6204 btrfs_item_key_to_cpu(leaf, &key, i);
6205 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6206 continue;
6207 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6208 if (btrfs_file_extent_type(leaf, fi) ==
6209 BTRFS_FILE_EXTENT_INLINE)
6210 continue;
6211 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6212 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6213 if (bytenr == 0)
6214 continue;
6215
6216 ext_index++;
6217 if (bytenr >= group->key.objectid + group->key.offset ||
6218 bytenr + num_bytes <= group->key.objectid)
6219 continue;
6220
6221 extent_key.objectid = bytenr;
6222 extent_key.offset = num_bytes;
6223 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6224 nr_extent = 1;
6225 ret = get_new_locations(reloc_inode, &extent_key,
6226 group->key.objectid, 1,
6227 &new_extent, &nr_extent);
6228 if (ret > 0)
6229 continue;
6230 BUG_ON(ret < 0);
6231
6232 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6233 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6234 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6235 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6236
Zheng Yan1a40e232008-09-26 10:09:34 -04006237 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6238 new_extent->disk_bytenr);
6239 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6240 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006241 btrfs_mark_buffer_dirty(leaf);
6242
6243 ret = btrfs_inc_extent_ref(trans, root,
6244 new_extent->disk_bytenr,
6245 new_extent->disk_num_bytes,
6246 leaf->start,
6247 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006248 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006249 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006250
Zheng Yan1a40e232008-09-26 10:09:34 -04006251 ret = btrfs_free_extent(trans, root,
6252 bytenr, num_bytes, leaf->start,
6253 btrfs_header_owner(leaf),
6254 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006255 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006256 BUG_ON(ret);
6257 cond_resched();
6258 }
6259 kfree(new_extent);
6260 BUG_ON(ext_index + 1 != ref->nritems);
6261 btrfs_free_leaf_ref(root, ref);
6262 return 0;
6263}
6264
Yan Zhengf82d02d2008-10-29 14:49:05 -04006265int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6266 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006267{
6268 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006269 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006270
6271 if (root->reloc_root) {
6272 reloc_root = root->reloc_root;
6273 root->reloc_root = NULL;
6274 list_add(&reloc_root->dead_list,
6275 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006276
6277 btrfs_set_root_bytenr(&reloc_root->root_item,
6278 reloc_root->node->start);
6279 btrfs_set_root_level(&root->root_item,
6280 btrfs_header_level(reloc_root->node));
6281 memset(&reloc_root->root_item.drop_progress, 0,
6282 sizeof(struct btrfs_disk_key));
6283 reloc_root->root_item.drop_level = 0;
6284
6285 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6286 &reloc_root->root_key,
6287 &reloc_root->root_item);
6288 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006289 }
6290 return 0;
6291}
6292
6293int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6294{
6295 struct btrfs_trans_handle *trans;
6296 struct btrfs_root *reloc_root;
6297 struct btrfs_root *prev_root = NULL;
6298 struct list_head dead_roots;
6299 int ret;
6300 unsigned long nr;
6301
6302 INIT_LIST_HEAD(&dead_roots);
6303 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6304
6305 while (!list_empty(&dead_roots)) {
6306 reloc_root = list_entry(dead_roots.prev,
6307 struct btrfs_root, dead_list);
6308 list_del_init(&reloc_root->dead_list);
6309
6310 BUG_ON(reloc_root->commit_root != NULL);
6311 while (1) {
6312 trans = btrfs_join_transaction(root, 1);
6313 BUG_ON(!trans);
6314
6315 mutex_lock(&root->fs_info->drop_mutex);
6316 ret = btrfs_drop_snapshot(trans, reloc_root);
6317 if (ret != -EAGAIN)
6318 break;
6319 mutex_unlock(&root->fs_info->drop_mutex);
6320
6321 nr = trans->blocks_used;
6322 ret = btrfs_end_transaction(trans, root);
6323 BUG_ON(ret);
6324 btrfs_btree_balance_dirty(root, nr);
6325 }
6326
6327 free_extent_buffer(reloc_root->node);
6328
6329 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6330 &reloc_root->root_key);
6331 BUG_ON(ret);
6332 mutex_unlock(&root->fs_info->drop_mutex);
6333
6334 nr = trans->blocks_used;
6335 ret = btrfs_end_transaction(trans, root);
6336 BUG_ON(ret);
6337 btrfs_btree_balance_dirty(root, nr);
6338
6339 kfree(prev_root);
6340 prev_root = reloc_root;
6341 }
6342 if (prev_root) {
6343 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6344 kfree(prev_root);
6345 }
6346 return 0;
6347}
6348
6349int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6350{
6351 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6352 return 0;
6353}
6354
6355int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6356{
6357 struct btrfs_root *reloc_root;
6358 struct btrfs_trans_handle *trans;
6359 struct btrfs_key location;
6360 int found;
6361 int ret;
6362
6363 mutex_lock(&root->fs_info->tree_reloc_mutex);
6364 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6365 BUG_ON(ret);
6366 found = !list_empty(&root->fs_info->dead_reloc_roots);
6367 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6368
6369 if (found) {
6370 trans = btrfs_start_transaction(root, 1);
6371 BUG_ON(!trans);
6372 ret = btrfs_commit_transaction(trans, root);
6373 BUG_ON(ret);
6374 }
6375
6376 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6377 location.offset = (u64)-1;
6378 location.type = BTRFS_ROOT_ITEM_KEY;
6379
6380 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6381 BUG_ON(!reloc_root);
6382 btrfs_orphan_cleanup(reloc_root);
6383 return 0;
6384}
6385
Chris Masond3977122009-01-05 21:25:51 -05006386static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006387 struct btrfs_root *root)
6388{
6389 struct btrfs_root *reloc_root;
6390 struct extent_buffer *eb;
6391 struct btrfs_root_item *root_item;
6392 struct btrfs_key root_key;
6393 int ret;
6394
6395 BUG_ON(!root->ref_cows);
6396 if (root->reloc_root)
6397 return 0;
6398
6399 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6400 BUG_ON(!root_item);
6401
6402 ret = btrfs_copy_root(trans, root, root->commit_root,
6403 &eb, BTRFS_TREE_RELOC_OBJECTID);
6404 BUG_ON(ret);
6405
6406 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6407 root_key.offset = root->root_key.objectid;
6408 root_key.type = BTRFS_ROOT_ITEM_KEY;
6409
6410 memcpy(root_item, &root->root_item, sizeof(root_item));
6411 btrfs_set_root_refs(root_item, 0);
6412 btrfs_set_root_bytenr(root_item, eb->start);
6413 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006414 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006415
6416 btrfs_tree_unlock(eb);
6417 free_extent_buffer(eb);
6418
6419 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6420 &root_key, root_item);
6421 BUG_ON(ret);
6422 kfree(root_item);
6423
6424 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6425 &root_key);
6426 BUG_ON(!reloc_root);
6427 reloc_root->last_trans = trans->transid;
6428 reloc_root->commit_root = NULL;
6429 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6430
6431 root->reloc_root = reloc_root;
6432 return 0;
6433}
6434
6435/*
6436 * Core function of space balance.
6437 *
6438 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006439 * counted roots. There is one reloc tree for each subvol, and all
6440 * reloc trees share same root key objectid. Reloc trees are snapshots
6441 * of the latest committed roots of subvols (root->commit_root).
6442 *
6443 * To relocate a tree block referenced by a subvol, there are two steps.
6444 * COW the block through subvol's reloc tree, then update block pointer
6445 * in the subvol to point to the new block. Since all reloc trees share
6446 * same root key objectid, doing special handing for tree blocks owned
6447 * by them is easy. Once a tree block has been COWed in one reloc tree,
6448 * we can use the resulting new block directly when the same block is
6449 * required to COW again through other reloc trees. By this way, relocated
6450 * tree blocks are shared between reloc trees, so they are also shared
6451 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006452 */
Chris Masond3977122009-01-05 21:25:51 -05006453static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006454 struct btrfs_root *root,
6455 struct btrfs_path *path,
6456 struct btrfs_key *first_key,
6457 struct btrfs_ref_path *ref_path,
6458 struct btrfs_block_group_cache *group,
6459 struct inode *reloc_inode)
6460{
6461 struct btrfs_root *reloc_root;
6462 struct extent_buffer *eb = NULL;
6463 struct btrfs_key *keys;
6464 u64 *nodes;
6465 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006466 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006467 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006468 int ret;
6469
6470 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6471 lowest_level = ref_path->owner_objectid;
6472
Yan Zhengf82d02d2008-10-29 14:49:05 -04006473 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006474 path->lowest_level = lowest_level;
6475 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6476 BUG_ON(ret < 0);
6477 path->lowest_level = 0;
6478 btrfs_release_path(root, path);
6479 return 0;
6480 }
6481
Zheng Yan1a40e232008-09-26 10:09:34 -04006482 mutex_lock(&root->fs_info->tree_reloc_mutex);
6483 ret = init_reloc_tree(trans, root);
6484 BUG_ON(ret);
6485 reloc_root = root->reloc_root;
6486
Yan Zhengf82d02d2008-10-29 14:49:05 -04006487 shared_level = ref_path->shared_level;
6488 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006489
Yan Zhengf82d02d2008-10-29 14:49:05 -04006490 keys = ref_path->node_keys;
6491 nodes = ref_path->new_nodes;
6492 memset(&keys[shared_level + 1], 0,
6493 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6494 memset(&nodes[shared_level + 1], 0,
6495 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006496
Yan Zhengf82d02d2008-10-29 14:49:05 -04006497 if (nodes[lowest_level] == 0) {
6498 path->lowest_level = lowest_level;
6499 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6500 0, 1);
6501 BUG_ON(ret);
6502 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6503 eb = path->nodes[level];
6504 if (!eb || eb == reloc_root->node)
6505 break;
6506 nodes[level] = eb->start;
6507 if (level == 0)
6508 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6509 else
6510 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6511 }
Yan Zheng2b820322008-11-17 21:11:30 -05006512 if (nodes[0] &&
6513 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006514 eb = path->nodes[0];
6515 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6516 group, reloc_inode);
6517 BUG_ON(ret);
6518 }
6519 btrfs_release_path(reloc_root, path);
6520 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006521 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006522 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006523 BUG_ON(ret);
6524 }
6525
Zheng Yan1a40e232008-09-26 10:09:34 -04006526 /*
6527 * replace tree blocks in the fs tree with tree blocks in
6528 * the reloc tree.
6529 */
6530 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6531 BUG_ON(ret < 0);
6532
6533 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006534 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6535 0, 0);
6536 BUG_ON(ret);
6537 extent_buffer_get(path->nodes[0]);
6538 eb = path->nodes[0];
6539 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006540 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6541 BUG_ON(ret);
6542 free_extent_buffer(eb);
6543 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006544
Yan Zhengf82d02d2008-10-29 14:49:05 -04006545 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006546 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006547 return 0;
6548}
6549
Chris Masond3977122009-01-05 21:25:51 -05006550static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006551 struct btrfs_root *root,
6552 struct btrfs_path *path,
6553 struct btrfs_key *first_key,
6554 struct btrfs_ref_path *ref_path)
6555{
6556 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006557
6558 ret = relocate_one_path(trans, root, path, first_key,
6559 ref_path, NULL, NULL);
6560 BUG_ON(ret);
6561
Zheng Yan1a40e232008-09-26 10:09:34 -04006562 return 0;
6563}
6564
Chris Masond3977122009-01-05 21:25:51 -05006565static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006566 struct btrfs_root *extent_root,
6567 struct btrfs_path *path,
6568 struct btrfs_key *extent_key)
6569{
6570 int ret;
6571
Zheng Yan1a40e232008-09-26 10:09:34 -04006572 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6573 if (ret)
6574 goto out;
6575 ret = btrfs_del_item(trans, extent_root, path);
6576out:
Chris Masonedbd8d42007-12-21 16:27:24 -05006577 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006578 return ret;
6579}
6580
Chris Masond3977122009-01-05 21:25:51 -05006581static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006582 struct btrfs_ref_path *ref_path)
6583{
6584 struct btrfs_key root_key;
6585
6586 root_key.objectid = ref_path->root_objectid;
6587 root_key.type = BTRFS_ROOT_ITEM_KEY;
6588 if (is_cowonly_root(ref_path->root_objectid))
6589 root_key.offset = 0;
6590 else
6591 root_key.offset = (u64)-1;
6592
6593 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6594}
6595
Chris Masond3977122009-01-05 21:25:51 -05006596static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006597 struct btrfs_path *path,
6598 struct btrfs_key *extent_key,
6599 struct btrfs_block_group_cache *group,
6600 struct inode *reloc_inode, int pass)
6601{
6602 struct btrfs_trans_handle *trans;
6603 struct btrfs_root *found_root;
6604 struct btrfs_ref_path *ref_path = NULL;
6605 struct disk_extent *new_extents = NULL;
6606 int nr_extents = 0;
6607 int loops;
6608 int ret;
6609 int level;
6610 struct btrfs_key first_key;
6611 u64 prev_block = 0;
6612
Zheng Yan1a40e232008-09-26 10:09:34 -04006613
6614 trans = btrfs_start_transaction(extent_root, 1);
6615 BUG_ON(!trans);
6616
6617 if (extent_key->objectid == 0) {
6618 ret = del_extent_zero(trans, extent_root, path, extent_key);
6619 goto out;
6620 }
6621
6622 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6623 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05006624 ret = -ENOMEM;
6625 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006626 }
6627
6628 for (loops = 0; ; loops++) {
6629 if (loops == 0) {
6630 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6631 extent_key->objectid);
6632 } else {
6633 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6634 }
6635 if (ret < 0)
6636 goto out;
6637 if (ret > 0)
6638 break;
6639
6640 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6641 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6642 continue;
6643
6644 found_root = read_ref_root(extent_root->fs_info, ref_path);
6645 BUG_ON(!found_root);
6646 /*
6647 * for reference counted tree, only process reference paths
6648 * rooted at the latest committed root.
6649 */
6650 if (found_root->ref_cows &&
6651 ref_path->root_generation != found_root->root_key.offset)
6652 continue;
6653
6654 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6655 if (pass == 0) {
6656 /*
6657 * copy data extents to new locations
6658 */
6659 u64 group_start = group->key.objectid;
6660 ret = relocate_data_extent(reloc_inode,
6661 extent_key,
6662 group_start);
6663 if (ret < 0)
6664 goto out;
6665 break;
6666 }
6667 level = 0;
6668 } else {
6669 level = ref_path->owner_objectid;
6670 }
6671
6672 if (prev_block != ref_path->nodes[level]) {
6673 struct extent_buffer *eb;
6674 u64 block_start = ref_path->nodes[level];
6675 u64 block_size = btrfs_level_size(found_root, level);
6676
6677 eb = read_tree_block(found_root, block_start,
6678 block_size, 0);
6679 btrfs_tree_lock(eb);
6680 BUG_ON(level != btrfs_header_level(eb));
6681
6682 if (level == 0)
6683 btrfs_item_key_to_cpu(eb, &first_key, 0);
6684 else
6685 btrfs_node_key_to_cpu(eb, &first_key, 0);
6686
6687 btrfs_tree_unlock(eb);
6688 free_extent_buffer(eb);
6689 prev_block = block_start;
6690 }
6691
Yan Zheng24562422009-02-12 14:14:53 -05006692 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006693 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05006694 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006695 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6696 /*
6697 * try to update data extent references while
6698 * keeping metadata shared between snapshots.
6699 */
6700 if (pass == 1) {
6701 ret = relocate_one_path(trans, found_root,
6702 path, &first_key, ref_path,
6703 group, reloc_inode);
6704 if (ret < 0)
6705 goto out;
6706 continue;
6707 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006708 /*
6709 * use fallback method to process the remaining
6710 * references.
6711 */
6712 if (!new_extents) {
6713 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04006714 new_extents = kmalloc(sizeof(*new_extents),
6715 GFP_NOFS);
6716 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006717 ret = get_new_locations(reloc_inode,
6718 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04006719 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04006720 &new_extents,
6721 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006722 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04006723 goto out;
6724 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006725 ret = replace_one_extent(trans, found_root,
6726 path, extent_key,
6727 &first_key, ref_path,
6728 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05006729 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006730 ret = relocate_tree_block(trans, found_root, path,
6731 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006732 }
6733 if (ret < 0)
6734 goto out;
6735 }
6736 ret = 0;
6737out:
6738 btrfs_end_transaction(trans, extent_root);
6739 kfree(new_extents);
6740 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006741 return ret;
6742}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006743#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006744
Chris Masonec44a352008-04-28 15:29:52 -04006745static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6746{
6747 u64 num_devices;
6748 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6749 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6750
Yan Zheng2b820322008-11-17 21:11:30 -05006751 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04006752 if (num_devices == 1) {
6753 stripped |= BTRFS_BLOCK_GROUP_DUP;
6754 stripped = flags & ~stripped;
6755
6756 /* turn raid0 into single device chunks */
6757 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6758 return stripped;
6759
6760 /* turn mirroring into duplication */
6761 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6762 BTRFS_BLOCK_GROUP_RAID10))
6763 return stripped | BTRFS_BLOCK_GROUP_DUP;
6764 return flags;
6765 } else {
6766 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006767 if (flags & stripped)
6768 return flags;
6769
6770 stripped |= BTRFS_BLOCK_GROUP_DUP;
6771 stripped = flags & ~stripped;
6772
6773 /* switch duplicated blocks with raid1 */
6774 if (flags & BTRFS_BLOCK_GROUP_DUP)
6775 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6776
6777 /* turn single device chunks into raid0 */
6778 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6779 }
6780 return flags;
6781}
6782
Christoph Hellwigb2950862008-12-02 09:54:17 -05006783static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04006784 struct btrfs_block_group_cache *shrink_block_group,
6785 int force)
6786{
6787 struct btrfs_trans_handle *trans;
6788 u64 new_alloc_flags;
6789 u64 calc;
6790
Chris Masonc286ac42008-07-22 23:06:41 -04006791 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006792 if (btrfs_block_group_used(&shrink_block_group->item) +
6793 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04006794 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006795
Chris Mason0ef3e662008-05-24 14:04:53 -04006796 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006797 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04006798
Chris Mason0ef3e662008-05-24 14:04:53 -04006799 new_alloc_flags = update_block_group_flags(root,
6800 shrink_block_group->flags);
6801 if (new_alloc_flags != shrink_block_group->flags) {
6802 calc =
6803 btrfs_block_group_used(&shrink_block_group->item);
6804 } else {
6805 calc = shrink_block_group->key.offset;
6806 }
Chris Masonc286ac42008-07-22 23:06:41 -04006807 spin_unlock(&shrink_block_group->lock);
6808
Chris Mason0ef3e662008-05-24 14:04:53 -04006809 do_chunk_alloc(trans, root->fs_info->extent_root,
6810 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04006811
Chris Mason0ef3e662008-05-24 14:04:53 -04006812 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04006813 } else
6814 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04006815 return 0;
6816}
6817
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006818
6819int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
6820 struct btrfs_block_group_cache *group)
6821
6822{
6823 __alloc_chunk_for_shrink(root, group, 1);
6824 set_block_group_readonly(group);
6825 return 0;
6826}
6827
6828#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006829static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
6830 struct btrfs_root *root,
6831 u64 objectid, u64 size)
6832{
6833 struct btrfs_path *path;
6834 struct btrfs_inode_item *item;
6835 struct extent_buffer *leaf;
6836 int ret;
6837
6838 path = btrfs_alloc_path();
6839 if (!path)
6840 return -ENOMEM;
6841
Chris Masonb9473432009-03-13 11:00:37 -04006842 path->leave_spinning = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006843 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
6844 if (ret)
6845 goto out;
6846
6847 leaf = path->nodes[0];
6848 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
6849 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
6850 btrfs_set_inode_generation(leaf, item, 1);
6851 btrfs_set_inode_size(leaf, item, size);
6852 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05006853 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006854 btrfs_mark_buffer_dirty(leaf);
6855 btrfs_release_path(root, path);
6856out:
6857 btrfs_free_path(path);
6858 return ret;
6859}
6860
Chris Masond3977122009-01-05 21:25:51 -05006861static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006862 struct btrfs_block_group_cache *group)
6863{
6864 struct inode *inode = NULL;
6865 struct btrfs_trans_handle *trans;
6866 struct btrfs_root *root;
6867 struct btrfs_key root_key;
6868 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
6869 int err = 0;
6870
6871 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6872 root_key.type = BTRFS_ROOT_ITEM_KEY;
6873 root_key.offset = (u64)-1;
6874 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
6875 if (IS_ERR(root))
6876 return ERR_CAST(root);
6877
6878 trans = btrfs_start_transaction(root, 1);
6879 BUG_ON(!trans);
6880
6881 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
6882 if (err)
6883 goto out;
6884
6885 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
6886 BUG_ON(err);
6887
6888 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04006889 group->key.offset, 0, group->key.offset,
6890 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006891 BUG_ON(err);
6892
6893 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
6894 if (inode->i_state & I_NEW) {
6895 BTRFS_I(inode)->root = root;
6896 BTRFS_I(inode)->location.objectid = objectid;
6897 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
6898 BTRFS_I(inode)->location.offset = 0;
6899 btrfs_read_locked_inode(inode);
6900 unlock_new_inode(inode);
6901 BUG_ON(is_bad_inode(inode));
6902 } else {
6903 BUG_ON(1);
6904 }
Yan Zheng17d217f2008-12-12 10:03:38 -05006905 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006906
6907 err = btrfs_orphan_add(trans, inode);
6908out:
6909 btrfs_end_transaction(trans, root);
6910 if (err) {
6911 if (inode)
6912 iput(inode);
6913 inode = ERR_PTR(err);
6914 }
6915 return inode;
6916}
6917
Yan Zheng17d217f2008-12-12 10:03:38 -05006918int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
6919{
6920
6921 struct btrfs_ordered_sum *sums;
6922 struct btrfs_sector_sum *sector_sum;
6923 struct btrfs_ordered_extent *ordered;
6924 struct btrfs_root *root = BTRFS_I(inode)->root;
6925 struct list_head list;
6926 size_t offset;
6927 int ret;
6928 u64 disk_bytenr;
6929
6930 INIT_LIST_HEAD(&list);
6931
6932 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
6933 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
6934
6935 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05006936 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05006937 disk_bytenr + len - 1, &list);
6938
6939 while (!list_empty(&list)) {
6940 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
6941 list_del_init(&sums->list);
6942
6943 sector_sum = sums->sums;
6944 sums->bytenr = ordered->start;
6945
6946 offset = 0;
6947 while (offset < sums->len) {
6948 sector_sum->bytenr += ordered->start - disk_bytenr;
6949 sector_sum++;
6950 offset += root->sectorsize;
6951 }
6952
6953 btrfs_add_ordered_sum(inode, ordered, sums);
6954 }
6955 btrfs_put_ordered_extent(ordered);
6956 return 0;
6957}
6958
Zheng Yan1a40e232008-09-26 10:09:34 -04006959int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05006960{
6961 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05006962 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04006963 struct btrfs_fs_info *info = root->fs_info;
6964 struct extent_buffer *leaf;
6965 struct inode *reloc_inode;
6966 struct btrfs_block_group_cache *block_group;
6967 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04006968 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05006969 u64 cur_byte;
6970 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05006971 u32 nritems;
6972 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04006973 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04006974 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006975
Chris Masonedbd8d42007-12-21 16:27:24 -05006976 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04006977
6978 block_group = btrfs_lookup_block_group(info, group_start);
6979 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006980
Chris Masond3977122009-01-05 21:25:51 -05006981 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04006982 (unsigned long long)block_group->key.objectid,
6983 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04006984
Zheng Yan1a40e232008-09-26 10:09:34 -04006985 path = btrfs_alloc_path();
6986 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04006987
Zheng Yan1a40e232008-09-26 10:09:34 -04006988 reloc_inode = create_reloc_inode(info, block_group);
6989 BUG_ON(IS_ERR(reloc_inode));
6990
Zheng Yan1a40e232008-09-26 10:09:34 -04006991 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05006992 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006993
Zheng Yan1a40e232008-09-26 10:09:34 -04006994 btrfs_start_delalloc_inodes(info->tree_root);
6995 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04006996again:
Yan Zhengd899e052008-10-30 14:25:28 -04006997 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006998 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04006999 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007000 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05007001 key.offset = 0;
7002 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05007003 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05007004
Zheng Yan1a40e232008-09-26 10:09:34 -04007005 trans = btrfs_start_transaction(info->tree_root, 1);
7006 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04007007
Zheng Yan1a40e232008-09-26 10:09:34 -04007008 mutex_lock(&root->fs_info->cleaner_mutex);
7009 btrfs_clean_old_snapshots(info->tree_root);
7010 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
7011 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04007012
Chris Mason56bec292009-03-13 10:10:06 -04007013 trans = btrfs_start_transaction(info->tree_root, 1);
7014 btrfs_commit_transaction(trans, info->tree_root);
7015
Chris Masond3977122009-01-05 21:25:51 -05007016 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05007017 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7018 if (ret < 0)
7019 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04007020next:
Chris Masonedbd8d42007-12-21 16:27:24 -05007021 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05007022 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05007023 if (path->slots[0] >= nritems) {
7024 ret = btrfs_next_leaf(root, path);
7025 if (ret < 0)
7026 goto out;
7027 if (ret == 1) {
7028 ret = 0;
7029 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05007030 }
Yan73e48b22008-01-03 14:14:39 -05007031 leaf = path->nodes[0];
7032 nritems = btrfs_header_nritems(leaf);
7033 }
7034
Zheng Yan1a40e232008-09-26 10:09:34 -04007035 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05007036
Zheng Yan1a40e232008-09-26 10:09:34 -04007037 if (key.objectid >= block_group->key.objectid +
7038 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04007039 break;
7040
Chris Mason725c8462008-01-04 16:47:16 -05007041 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05007042 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007043 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05007044 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007045 continue;
Chris Mason725c8462008-01-04 16:47:16 -05007046 }
7047 progress = 1;
7048
Zheng Yan1a40e232008-09-26 10:09:34 -04007049 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
7050 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05007051 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05007052 goto next;
7053 }
Yan73e48b22008-01-03 14:14:39 -05007054
Chris Masonedbd8d42007-12-21 16:27:24 -05007055 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04007056 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05007057 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007058
7059 __alloc_chunk_for_shrink(root, block_group, 0);
7060 ret = relocate_one_extent(root, path, &key, block_group,
7061 reloc_inode, pass);
7062 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04007063 if (ret > 0)
7064 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04007065
7066 key.objectid = cur_byte;
7067 key.type = 0;
7068 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007069 }
7070
7071 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007072
7073 if (pass == 0) {
7074 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
7075 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04007076 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007077
7078 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05007079 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04007080 (unsigned long long)total_found, pass);
7081 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04007082 if (total_found == skipped && pass > 2) {
7083 iput(reloc_inode);
7084 reloc_inode = create_reloc_inode(info, block_group);
7085 pass = 0;
7086 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007087 goto again;
7088 }
7089
Zheng Yan1a40e232008-09-26 10:09:34 -04007090 /* delete reloc_inode */
7091 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04007092
Zheng Yan1a40e232008-09-26 10:09:34 -04007093 /* unpin extents in this range */
7094 trans = btrfs_start_transaction(info->tree_root, 1);
7095 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04007096
Zheng Yan1a40e232008-09-26 10:09:34 -04007097 spin_lock(&block_group->lock);
7098 WARN_ON(block_group->pinned > 0);
7099 WARN_ON(block_group->reserved > 0);
7100 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
7101 spin_unlock(&block_group->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007102 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007103 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007104out:
Zheng Yan1a40e232008-09-26 10:09:34 -04007105 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007106 return ret;
7107}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007108#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007109
Christoph Hellwigb2950862008-12-02 09:54:17 -05007110static int find_first_block_group(struct btrfs_root *root,
7111 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007112{
Chris Mason925baed2008-06-25 16:01:30 -04007113 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007114 struct btrfs_key found_key;
7115 struct extent_buffer *leaf;
7116 int slot;
7117
7118 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7119 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007120 goto out;
7121
Chris Masond3977122009-01-05 21:25:51 -05007122 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007123 slot = path->slots[0];
7124 leaf = path->nodes[0];
7125 if (slot >= btrfs_header_nritems(leaf)) {
7126 ret = btrfs_next_leaf(root, path);
7127 if (ret == 0)
7128 continue;
7129 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007130 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007131 break;
7132 }
7133 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7134
7135 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007136 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7137 ret = 0;
7138 goto out;
7139 }
Chris Mason0b86a832008-03-24 15:01:56 -04007140 path->slots[0]++;
7141 }
7142 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04007143out:
Chris Mason0b86a832008-03-24 15:01:56 -04007144 return ret;
7145}
7146
Zheng Yan1a40e232008-09-26 10:09:34 -04007147int btrfs_free_block_groups(struct btrfs_fs_info *info)
7148{
7149 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007150 struct btrfs_space_info *space_info;
Zheng Yan1a40e232008-09-26 10:09:34 -04007151 struct rb_node *n;
7152
Zheng Yan1a40e232008-09-26 10:09:34 -04007153 spin_lock(&info->block_group_cache_lock);
7154 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7155 block_group = rb_entry(n, struct btrfs_block_group_cache,
7156 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007157 rb_erase(&block_group->cache_node,
7158 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007159 spin_unlock(&info->block_group_cache_lock);
7160
Josef Bacik80eb2342008-10-29 14:49:05 -04007161 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007162 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007163 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007164
Josef Bacik817d52f2009-07-13 21:29:25 -04007165 if (block_group->cached == BTRFS_CACHE_STARTED)
7166 wait_event(block_group->caching_q,
7167 block_group_cache_done(block_group));
7168
7169 btrfs_remove_free_space_cache(block_group);
7170
Yan Zhengd2fb3432008-12-11 16:30:39 -05007171 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04007172 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007173
7174 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007175 }
7176 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007177
7178 /* now that all the block groups are freed, go through and
7179 * free all the space_info structs. This is only called during
7180 * the final stages of unmount, and so we know nobody is
7181 * using them. We call synchronize_rcu() once before we start,
7182 * just to be on the safe side.
7183 */
7184 synchronize_rcu();
7185
7186 while(!list_empty(&info->space_info)) {
7187 space_info = list_entry(info->space_info.next,
7188 struct btrfs_space_info,
7189 list);
7190
7191 list_del(&space_info->list);
7192 kfree(space_info);
7193 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007194 return 0;
7195}
7196
Chris Mason9078a3e2007-04-26 16:46:15 -04007197int btrfs_read_block_groups(struct btrfs_root *root)
7198{
7199 struct btrfs_path *path;
7200 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007201 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007202 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007203 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007204 struct btrfs_key key;
7205 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007206 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007207
Chris Masonbe744172007-05-06 10:15:01 -04007208 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007209 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007210 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007211 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007212 path = btrfs_alloc_path();
7213 if (!path)
7214 return -ENOMEM;
7215
Chris Masond3977122009-01-05 21:25:51 -05007216 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007217 ret = find_first_block_group(root, path, &key);
7218 if (ret > 0) {
7219 ret = 0;
7220 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007221 }
Chris Mason0b86a832008-03-24 15:01:56 -04007222 if (ret != 0)
7223 goto error;
7224
Chris Mason5f39d392007-10-15 16:14:19 -04007225 leaf = path->nodes[0];
7226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007227 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007228 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007229 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04007230 break;
7231 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007232
Yan Zhengd2fb3432008-12-11 16:30:39 -05007233 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007234 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007235 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007236 cache->fs_info = info;
7237 init_waitqueue_head(&cache->caching_q);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007238 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007239 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007240
7241 /*
7242 * we only want to have 32k of ram per block group for keeping
7243 * track of free space, and if we pass 1/2 of that we want to
7244 * start converting things over to using bitmaps
7245 */
7246 cache->extents_thresh = ((1024 * 32) / 2) /
7247 sizeof(struct btrfs_free_space);
7248
Chris Mason5f39d392007-10-15 16:14:19 -04007249 read_extent_buffer(leaf, &cache->item,
7250 btrfs_item_ptr_offset(leaf, path->slots[0]),
7251 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007252 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007253
Chris Mason9078a3e2007-04-26 16:46:15 -04007254 key.objectid = found_key.objectid + found_key.offset;
7255 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007256 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007257 cache->sectorsize = root->sectorsize;
7258
7259 remove_sb_from_cache(root, cache);
7260
7261 /*
7262 * check for two cases, either we are full, and therefore
7263 * don't need to bother with the caching work since we won't
7264 * find any space, or we are empty, and we can just add all
7265 * the space in and be done with it. This saves us _alot_ of
7266 * time, particularly in the full case.
7267 */
7268 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
7269 cache->cached = BTRFS_CACHE_FINISHED;
7270 } else if (btrfs_block_group_used(&cache->item) == 0) {
7271 cache->cached = BTRFS_CACHE_FINISHED;
7272 add_new_free_space(cache, root->fs_info,
7273 found_key.objectid,
7274 found_key.objectid +
7275 found_key.offset);
7276 }
Chris Mason96b51792007-10-15 16:15:19 -04007277
Chris Mason6324fbf2008-03-24 15:01:59 -04007278 ret = update_space_info(info, cache->flags, found_key.offset,
7279 btrfs_block_group_used(&cache->item),
7280 &space_info);
7281 BUG_ON(ret);
7282 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04007283 down_write(&space_info->groups_sem);
7284 list_add_tail(&cache->list, &space_info->block_groups);
7285 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007286
Josef Bacik0f9dd462008-09-23 13:14:11 -04007287 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7288 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007289
7290 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007291 if (btrfs_chunk_readonly(root, cache->key.objectid))
7292 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007293 }
Chris Mason0b86a832008-03-24 15:01:56 -04007294 ret = 0;
7295error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007296 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007297 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007298}
Chris Mason6324fbf2008-03-24 15:01:59 -04007299
7300int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7301 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007302 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007303 u64 size)
7304{
7305 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007306 struct btrfs_root *extent_root;
7307 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007308
7309 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007310
Chris Mason12fcfd22009-03-24 10:24:20 -04007311 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007312
Chris Mason8f18cf12008-04-25 16:53:30 -04007313 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007314 if (!cache)
7315 return -ENOMEM;
7316
Chris Masone17cade2008-04-15 15:41:47 -04007317 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007318 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007319 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007320 cache->sectorsize = root->sectorsize;
7321
7322 /*
7323 * we only want to have 32k of ram per block group for keeping track
7324 * of free space, and if we pass 1/2 of that we want to start
7325 * converting things over to using bitmaps
7326 */
7327 cache->extents_thresh = ((1024 * 32) / 2) /
7328 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007329 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007330 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007331 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007332 init_waitqueue_head(&cache->caching_q);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007333 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007334 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007335
Chris Mason6324fbf2008-03-24 15:01:59 -04007336 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007337 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7338 cache->flags = type;
7339 btrfs_set_block_group_flags(&cache->item, type);
7340
Josef Bacik817d52f2009-07-13 21:29:25 -04007341 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik96303082009-07-13 21:29:25 -04007342 remove_sb_from_cache(root, cache);
7343
Josef Bacik817d52f2009-07-13 21:29:25 -04007344 add_new_free_space(cache, root->fs_info, chunk_offset,
7345 chunk_offset + size);
7346
Chris Mason6324fbf2008-03-24 15:01:59 -04007347 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7348 &cache->space_info);
7349 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04007350 down_write(&cache->space_info->groups_sem);
7351 list_add_tail(&cache->list, &cache->space_info->block_groups);
7352 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007353
Josef Bacik0f9dd462008-09-23 13:14:11 -04007354 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7355 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007356
Chris Mason6324fbf2008-03-24 15:01:59 -04007357 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7358 sizeof(cache->item));
7359 BUG_ON(ret);
7360
Chris Masond18a2c42008-04-04 15:40:00 -04007361 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007362
Chris Mason6324fbf2008-03-24 15:01:59 -04007363 return 0;
7364}
Zheng Yan1a40e232008-09-26 10:09:34 -04007365
7366int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7367 struct btrfs_root *root, u64 group_start)
7368{
7369 struct btrfs_path *path;
7370 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007371 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007372 struct btrfs_key key;
7373 int ret;
7374
Zheng Yan1a40e232008-09-26 10:09:34 -04007375 root = root->fs_info->extent_root;
7376
7377 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7378 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007379 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007380
7381 memcpy(&key, &block_group->key, sizeof(key));
7382
Chris Mason44fb5512009-06-04 15:34:51 -04007383 /* make sure this block group isn't part of an allocation cluster */
7384 cluster = &root->fs_info->data_alloc_cluster;
7385 spin_lock(&cluster->refill_lock);
7386 btrfs_return_cluster_to_free_space(block_group, cluster);
7387 spin_unlock(&cluster->refill_lock);
7388
7389 /*
7390 * make sure this block group isn't part of a metadata
7391 * allocation cluster
7392 */
7393 cluster = &root->fs_info->meta_alloc_cluster;
7394 spin_lock(&cluster->refill_lock);
7395 btrfs_return_cluster_to_free_space(block_group, cluster);
7396 spin_unlock(&cluster->refill_lock);
7397
Zheng Yan1a40e232008-09-26 10:09:34 -04007398 path = btrfs_alloc_path();
7399 BUG_ON(!path);
7400
Yan Zheng3dfdb932009-01-21 10:49:16 -05007401 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007402 rb_erase(&block_group->cache_node,
7403 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007404 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007405
Josef Bacik80eb2342008-10-29 14:49:05 -04007406 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007407 /*
7408 * we must use list_del_init so people can check to see if they
7409 * are still on the list after taking the semaphore
7410 */
7411 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007412 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007413
Josef Bacik817d52f2009-07-13 21:29:25 -04007414 if (block_group->cached == BTRFS_CACHE_STARTED)
7415 wait_event(block_group->caching_q,
7416 block_group_cache_done(block_group));
7417
7418 btrfs_remove_free_space_cache(block_group);
7419
Yan Zhengc146afa2008-11-12 14:34:12 -05007420 spin_lock(&block_group->space_info->lock);
7421 block_group->space_info->total_bytes -= block_group->key.offset;
7422 block_group->space_info->bytes_readonly -= block_group->key.offset;
7423 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007424
7425 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007426
Chris Masonfa9c0d72009-04-03 09:47:43 -04007427 btrfs_put_block_group(block_group);
7428 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007429
7430 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7431 if (ret > 0)
7432 ret = -EIO;
7433 if (ret < 0)
7434 goto out;
7435
7436 ret = btrfs_del_item(trans, root, path);
7437out:
7438 btrfs_free_path(path);
7439 return ret;
7440}