blob: 1b9b87870f5195bfd838c1e7fe081219c7bcdd0e [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 Masonfa9c0d792009-04-03 09:47:43 -040033#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Josef Bacikf3465ca2008-11-12 14:19:50 -050035static int update_block_group(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 u64 bytenr, u64 num_bytes, int alloc,
38 int mark_free);
Yan Zheng11833d62009-09-11 16:11:19 -040039static int update_reserved_extents(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve);
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);
Josef Bacik6a632092009-02-20 11:00:09 -050060static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61 struct btrfs_root *extent_root, u64 alloc_bytes,
62 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040063static int pin_down_bytes(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root,
65 struct btrfs_path *path,
66 u64 bytenr, u64 num_bytes,
67 int is_data, int reserved,
68 struct extent_buffer **must_clean);
69static int find_next_key(struct btrfs_path *path, int level,
70 struct btrfs_key *key);
Josef Bacik6a632092009-02-20 11:00:09 -050071
Josef Bacik817d52f2009-07-13 21:29:25 -040072static noinline int
73block_group_cache_done(struct btrfs_block_group_cache *cache)
74{
75 smp_mb();
76 return cache->cached == BTRFS_CACHE_FINISHED;
77}
78
Josef Bacik0f9dd462008-09-23 13:14:11 -040079static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
80{
81 return (cache->flags & bits) == bits;
82}
83
84/*
85 * this adds the block group to the fs_info rb tree for the block group
86 * cache
87 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050088static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040089 struct btrfs_block_group_cache *block_group)
90{
91 struct rb_node **p;
92 struct rb_node *parent = NULL;
93 struct btrfs_block_group_cache *cache;
94
95 spin_lock(&info->block_group_cache_lock);
96 p = &info->block_group_cache_tree.rb_node;
97
98 while (*p) {
99 parent = *p;
100 cache = rb_entry(parent, struct btrfs_block_group_cache,
101 cache_node);
102 if (block_group->key.objectid < cache->key.objectid) {
103 p = &(*p)->rb_left;
104 } else if (block_group->key.objectid > cache->key.objectid) {
105 p = &(*p)->rb_right;
106 } else {
107 spin_unlock(&info->block_group_cache_lock);
108 return -EEXIST;
109 }
110 }
111
112 rb_link_node(&block_group->cache_node, parent, p);
113 rb_insert_color(&block_group->cache_node,
114 &info->block_group_cache_tree);
115 spin_unlock(&info->block_group_cache_lock);
116
117 return 0;
118}
119
120/*
121 * This will return the block group at or after bytenr if contains is 0, else
122 * it will return the block group that contains the bytenr
123 */
124static struct btrfs_block_group_cache *
125block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
126 int contains)
127{
128 struct btrfs_block_group_cache *cache, *ret = NULL;
129 struct rb_node *n;
130 u64 end, start;
131
132 spin_lock(&info->block_group_cache_lock);
133 n = info->block_group_cache_tree.rb_node;
134
135 while (n) {
136 cache = rb_entry(n, struct btrfs_block_group_cache,
137 cache_node);
138 end = cache->key.objectid + cache->key.offset - 1;
139 start = cache->key.objectid;
140
141 if (bytenr < start) {
142 if (!contains && (!ret || start < ret->key.objectid))
143 ret = cache;
144 n = n->rb_left;
145 } else if (bytenr > start) {
146 if (contains && bytenr <= end) {
147 ret = cache;
148 break;
149 }
150 n = n->rb_right;
151 } else {
152 ret = cache;
153 break;
154 }
155 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500156 if (ret)
157 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400158 spin_unlock(&info->block_group_cache_lock);
159
160 return ret;
161}
162
Yan Zheng11833d62009-09-11 16:11:19 -0400163static int add_excluded_extent(struct btrfs_root *root,
164 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400165{
Yan Zheng11833d62009-09-11 16:11:19 -0400166 u64 end = start + num_bytes - 1;
167 set_extent_bits(&root->fs_info->freed_extents[0],
168 start, end, EXTENT_UPTODATE, GFP_NOFS);
169 set_extent_bits(&root->fs_info->freed_extents[1],
170 start, end, EXTENT_UPTODATE, GFP_NOFS);
171 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400172}
173
Yan Zheng11833d62009-09-11 16:11:19 -0400174static void free_excluded_extents(struct btrfs_root *root,
175 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400176{
Yan Zheng11833d62009-09-11 16:11:19 -0400177 u64 start, end;
178
179 start = cache->key.objectid;
180 end = start + cache->key.offset - 1;
181
182 clear_extent_bits(&root->fs_info->freed_extents[0],
183 start, end, EXTENT_UPTODATE, GFP_NOFS);
184 clear_extent_bits(&root->fs_info->freed_extents[1],
185 start, end, EXTENT_UPTODATE, GFP_NOFS);
186}
187
188static int exclude_super_stripes(struct btrfs_root *root,
189 struct btrfs_block_group_cache *cache)
190{
Josef Bacik817d52f2009-07-13 21:29:25 -0400191 u64 bytenr;
192 u64 *logical;
193 int stripe_len;
194 int i, nr, ret;
195
196 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
197 bytenr = btrfs_sb_offset(i);
198 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
199 cache->key.objectid, bytenr,
200 0, &logical, &nr, &stripe_len);
201 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400202
Josef Bacik817d52f2009-07-13 21:29:25 -0400203 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400204 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400205 ret = add_excluded_extent(root, logical[nr],
206 stripe_len);
207 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400208 }
Yan Zheng11833d62009-09-11 16:11:19 -0400209
Josef Bacik817d52f2009-07-13 21:29:25 -0400210 kfree(logical);
211 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400212 return 0;
213}
214
Yan Zheng11833d62009-09-11 16:11:19 -0400215static struct btrfs_caching_control *
216get_caching_control(struct btrfs_block_group_cache *cache)
217{
218 struct btrfs_caching_control *ctl;
219
220 spin_lock(&cache->lock);
221 if (cache->cached != BTRFS_CACHE_STARTED) {
222 spin_unlock(&cache->lock);
223 return NULL;
224 }
225
226 ctl = cache->caching_ctl;
227 atomic_inc(&ctl->count);
228 spin_unlock(&cache->lock);
229 return ctl;
230}
231
232static void put_caching_control(struct btrfs_caching_control *ctl)
233{
234 if (atomic_dec_and_test(&ctl->count))
235 kfree(ctl);
236}
237
Josef Bacik0f9dd462008-09-23 13:14:11 -0400238/*
239 * this is only called by cache_block_group, since we could have freed extents
240 * we need to check the pinned_extents for any extents that can't be used yet
241 * since their free space will be released as soon as the transaction commits.
242 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400243static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400244 struct btrfs_fs_info *info, u64 start, u64 end)
245{
Josef Bacik817d52f2009-07-13 21:29:25 -0400246 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400247 int ret;
248
249 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400250 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400251 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400252 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400253 if (ret)
254 break;
255
256 if (extent_start == start) {
257 start = extent_end + 1;
258 } else if (extent_start > start && extent_start < end) {
259 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400260 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500261 ret = btrfs_add_free_space(block_group, start,
262 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263 BUG_ON(ret);
264 start = extent_end + 1;
265 } else {
266 break;
267 }
268 }
269
270 if (start < end) {
271 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400272 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500273 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400274 BUG_ON(ret);
275 }
276
Josef Bacik817d52f2009-07-13 21:29:25 -0400277 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400278}
279
Josef Bacik817d52f2009-07-13 21:29:25 -0400280static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400281{
Josef Bacik817d52f2009-07-13 21:29:25 -0400282 struct btrfs_block_group_cache *block_group = data;
283 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400284 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
285 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400286 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400287 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400288 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400289 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400290 u64 last = 0;
291 u32 nritems;
292 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400293
Chris Masone37c9e62007-05-09 20:13:14 -0400294 path = btrfs_alloc_path();
295 if (!path)
296 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400297
Yan Zheng11833d62009-09-11 16:11:19 -0400298 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400299 spin_lock(&block_group->space_info->lock);
300 block_group->space_info->bytes_super += block_group->bytes_super;
301 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400302
Josef Bacik817d52f2009-07-13 21:29:25 -0400303 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400304
Chris Mason5cd57b22008-06-25 16:01:30 -0400305 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400306 * We don't want to deadlock with somebody trying to allocate a new
307 * extent for the extent root while also trying to search the extent
308 * root to add free space. So we skip locking and search the commit
309 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400310 */
311 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400312 path->search_commit_root = 1;
313 path->reada = 2;
314
Yan Zhenge4404d62008-12-12 10:03:26 -0500315 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400316 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400317 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400318again:
Yan Zheng11833d62009-09-11 16:11:19 -0400319 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400320 /* need to make sure the commit_root doesn't disappear */
321 down_read(&fs_info->extent_commit_sem);
322
Yan Zheng11833d62009-09-11 16:11:19 -0400323 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400324 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400325 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500326
Yan Zheng11833d62009-09-11 16:11:19 -0400327 leaf = path->nodes[0];
328 nritems = btrfs_header_nritems(leaf);
329
Chris Masond3977122009-01-05 21:25:51 -0500330 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400332 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400333 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400334 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400335 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400336
Yan Zheng11833d62009-09-11 16:11:19 -0400337 if (path->slots[0] < nritems) {
338 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
339 } else {
340 ret = find_next_key(path, 0, &key);
341 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400342 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400343
Yan Zheng11833d62009-09-11 16:11:19 -0400344 caching_ctl->progress = last;
345 btrfs_release_path(extent_root, path);
346 up_read(&fs_info->extent_commit_sem);
347 mutex_unlock(&caching_ctl->mutex);
348 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400349 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400350 else
351 cond_resched();
352 goto again;
353 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400354
Yan Zheng11833d62009-09-11 16:11:19 -0400355 if (key.objectid < block_group->key.objectid) {
356 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400357 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400358 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400359
Chris Masone37c9e62007-05-09 20:13:14 -0400360 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400361 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400362 break;
Yan7d7d6062007-09-14 16:15:28 -0400363
Yan Zheng11833d62009-09-11 16:11:19 -0400364 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400365 total_found += add_new_free_space(block_group,
366 fs_info, last,
367 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400368 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400369
Yan Zheng11833d62009-09-11 16:11:19 -0400370 if (total_found > (1024 * 1024 * 2)) {
371 total_found = 0;
372 wake_up(&caching_ctl->wait);
373 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400374 }
Chris Masone37c9e62007-05-09 20:13:14 -0400375 path->slots[0]++;
376 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400377 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400378
379 total_found += add_new_free_space(block_group, fs_info, last,
380 block_group->key.objectid +
381 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400382 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400383
384 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400385 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400386 block_group->cached = BTRFS_CACHE_FINISHED;
387 spin_unlock(&block_group->lock);
388
Chris Mason54aa1f42007-06-22 14:16:25 -0400389err:
Chris Masone37c9e62007-05-09 20:13:14 -0400390 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400391 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400392
Yan Zheng11833d62009-09-11 16:11:19 -0400393 free_excluded_extents(extent_root, block_group);
394
395 mutex_unlock(&caching_ctl->mutex);
396 wake_up(&caching_ctl->wait);
397
398 put_caching_control(caching_ctl);
399 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik817d52f2009-07-13 21:29:25 -0400400 return 0;
401}
402
403static int cache_block_group(struct btrfs_block_group_cache *cache)
404{
Yan Zheng11833d62009-09-11 16:11:19 -0400405 struct btrfs_fs_info *fs_info = cache->fs_info;
406 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400407 struct task_struct *tsk;
408 int ret = 0;
409
Yan Zheng11833d62009-09-11 16:11:19 -0400410 smp_mb();
411 if (cache->cached != BTRFS_CACHE_NO)
412 return 0;
413
414 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
415 BUG_ON(!caching_ctl);
416
417 INIT_LIST_HEAD(&caching_ctl->list);
418 mutex_init(&caching_ctl->mutex);
419 init_waitqueue_head(&caching_ctl->wait);
420 caching_ctl->block_group = cache;
421 caching_ctl->progress = cache->key.objectid;
422 /* one for caching kthread, one for caching block group list */
423 atomic_set(&caching_ctl->count, 2);
424
Josef Bacik817d52f2009-07-13 21:29:25 -0400425 spin_lock(&cache->lock);
426 if (cache->cached != BTRFS_CACHE_NO) {
427 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400428 kfree(caching_ctl);
429 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400430 }
Yan Zheng11833d62009-09-11 16:11:19 -0400431 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400432 cache->cached = BTRFS_CACHE_STARTED;
433 spin_unlock(&cache->lock);
434
Yan Zheng11833d62009-09-11 16:11:19 -0400435 down_write(&fs_info->extent_commit_sem);
436 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
437 up_write(&fs_info->extent_commit_sem);
438
439 atomic_inc(&cache->space_info->caching_threads);
440
Josef Bacik817d52f2009-07-13 21:29:25 -0400441 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
442 cache->key.objectid);
443 if (IS_ERR(tsk)) {
444 ret = PTR_ERR(tsk);
445 printk(KERN_ERR "error running thread %d\n", ret);
446 BUG();
447 }
448
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400449 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400450}
451
Josef Bacik0f9dd462008-09-23 13:14:11 -0400452/*
453 * return the block group that starts at or after bytenr
454 */
Chris Masond3977122009-01-05 21:25:51 -0500455static struct btrfs_block_group_cache *
456btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400457{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400458 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400459
Josef Bacik0f9dd462008-09-23 13:14:11 -0400460 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400461
Josef Bacik0f9dd462008-09-23 13:14:11 -0400462 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400463}
464
Josef Bacik0f9dd462008-09-23 13:14:11 -0400465/*
Sankar P9f556842009-05-14 13:52:22 -0400466 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400467 */
Chris Masond3977122009-01-05 21:25:51 -0500468struct btrfs_block_group_cache *btrfs_lookup_block_group(
469 struct btrfs_fs_info *info,
470 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400471{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400472 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400473
Josef Bacik0f9dd462008-09-23 13:14:11 -0400474 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400475
Josef Bacik0f9dd462008-09-23 13:14:11 -0400476 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400477}
Chris Mason0b86a832008-03-24 15:01:56 -0400478
Chris Masonfa9c0d792009-04-03 09:47:43 -0400479void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
Yan Zhengd2fb3432008-12-11 16:30:39 -0500480{
481 if (atomic_dec_and_test(&cache->count))
482 kfree(cache);
483}
484
Josef Bacik0f9dd462008-09-23 13:14:11 -0400485static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
486 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400487{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400488 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400489 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400490
491 rcu_read_lock();
492 list_for_each_entry_rcu(found, head, list) {
493 if (found->flags == flags) {
494 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400495 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400496 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400497 }
Chris Mason4184ea72009-03-10 12:39:20 -0400498 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400499 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400500}
501
Chris Mason4184ea72009-03-10 12:39:20 -0400502/*
503 * after adding space to the filesystem, we need to clear the full flags
504 * on all the space infos.
505 */
506void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
507{
508 struct list_head *head = &info->space_info;
509 struct btrfs_space_info *found;
510
511 rcu_read_lock();
512 list_for_each_entry_rcu(found, head, list)
513 found->full = 0;
514 rcu_read_unlock();
515}
516
Josef Bacik80eb2342008-10-29 14:49:05 -0400517static u64 div_factor(u64 num, int factor)
518{
519 if (factor == 10)
520 return num;
521 num *= factor;
522 do_div(num, 10);
523 return num;
524}
525
Yan Zhengd2fb3432008-12-11 16:30:39 -0500526u64 btrfs_find_block_group(struct btrfs_root *root,
527 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400528{
Chris Mason96b51792007-10-15 16:15:19 -0400529 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400530 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500531 u64 last = max(search_hint, search_start);
532 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400533 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500534 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400535 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400536again:
Zheng Yane8569812008-09-26 10:05:48 -0400537 while (1) {
538 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400539 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400540 break;
Chris Mason96b51792007-10-15 16:15:19 -0400541
Chris Masonc286ac42008-07-22 23:06:41 -0400542 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400543 last = cache->key.objectid + cache->key.offset;
544 used = btrfs_block_group_used(&cache->item);
545
Yan Zhengd2fb3432008-12-11 16:30:39 -0500546 if ((full_search || !cache->ro) &&
547 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400548 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500549 div_factor(cache->key.offset, factor)) {
550 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400551 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400552 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400553 goto found;
554 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400555 }
Chris Masonc286ac42008-07-22 23:06:41 -0400556 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400557 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400558 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400559 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400560 if (!wrapped) {
561 last = search_start;
562 wrapped = 1;
563 goto again;
564 }
565 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400566 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400567 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400568 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400569 goto again;
570 }
Chris Masonbe744172007-05-06 10:15:01 -0400571found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500572 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400573}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400574
Chris Masone02119d2008-09-05 16:13:11 -0400575/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400576int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400577{
578 int ret;
579 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400580 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400581
Zheng Yan31840ae2008-09-23 13:14:14 -0400582 path = btrfs_alloc_path();
583 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400584 key.objectid = start;
585 key.offset = len;
586 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
587 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
588 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400589 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500590 return ret;
591}
592
Chris Masond8d5f3e2007-12-11 12:42:00 -0500593/*
594 * Back reference rules. Back refs have three main goals:
595 *
596 * 1) differentiate between all holders of references to an extent so that
597 * when a reference is dropped we can make sure it was a valid reference
598 * before freeing the extent.
599 *
600 * 2) Provide enough information to quickly find the holders of an extent
601 * if we notice a given block is corrupted or bad.
602 *
603 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
604 * maintenance. This is actually the same as #2, but with a slightly
605 * different use case.
606 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400607 * There are two kinds of back refs. The implicit back refs is optimized
608 * for pointers in non-shared tree blocks. For a given pointer in a block,
609 * back refs of this kind provide information about the block's owner tree
610 * and the pointer's key. These information allow us to find the block by
611 * b-tree searching. The full back refs is for pointers in tree blocks not
612 * referenced by their owner trees. The location of tree block is recorded
613 * in the back refs. Actually the full back refs is generic, and can be
614 * used in all cases the implicit back refs is used. The major shortcoming
615 * of the full back refs is its overhead. Every time a tree block gets
616 * COWed, we have to update back refs entry for all pointers in it.
617 *
618 * For a newly allocated tree block, we use implicit back refs for
619 * pointers in it. This means most tree related operations only involve
620 * implicit back refs. For a tree block created in old transaction, the
621 * only way to drop a reference to it is COW it. So we can detect the
622 * event that tree block loses its owner tree's reference and do the
623 * back refs conversion.
624 *
625 * When a tree block is COW'd through a tree, there are four cases:
626 *
627 * The reference count of the block is one and the tree is the block's
628 * owner tree. Nothing to do in this case.
629 *
630 * The reference count of the block is one and the tree is not the
631 * block's owner tree. In this case, full back refs is used for pointers
632 * in the block. Remove these full back refs, add implicit back refs for
633 * every pointers in the new block.
634 *
635 * The reference count of the block is greater than one and the tree is
636 * the block's owner tree. In this case, implicit back refs is used for
637 * pointers in the block. Add full back refs for every pointers in the
638 * block, increase lower level extents' reference counts. The original
639 * implicit back refs are entailed to the new block.
640 *
641 * The reference count of the block is greater than one and the tree is
642 * not the block's owner tree. Add implicit back refs for every pointer in
643 * the new block, increase lower level extents' reference count.
644 *
645 * Back Reference Key composing:
646 *
647 * The key objectid corresponds to the first byte in the extent,
648 * The key type is used to differentiate between types of back refs.
649 * There are different meanings of the key offset for different types
650 * of back refs.
651 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500652 * File extents can be referenced by:
653 *
654 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400655 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500656 * - different offsets inside a file (bookend extents in file.c)
657 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400658 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500659 *
660 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500661 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400662 * - original offset in the file
663 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400664 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400665 * The key offset for the implicit back refs is hash of the first
666 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500667 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400668 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500669 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400670 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500671 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400672 * The key offset for the implicit back refs is the first byte of
673 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500674 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400675 * When a file extent is allocated, The implicit back refs is used.
676 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500677 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400678 * (root_key.objectid, inode objectid, offset in file, 1)
679 *
680 * When a file extent is removed file truncation, we find the
681 * corresponding implicit back refs and check the following fields:
682 *
683 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500684 *
685 * Btree extents can be referenced by:
686 *
687 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500688 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400689 * Both the implicit back refs and the full back refs for tree blocks
690 * only consist of key. The key offset for the implicit back refs is
691 * objectid of block's owner tree. The key offset for the full back refs
692 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500693 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400694 * When implicit back refs is used, information about the lowest key and
695 * level of the tree block are required. These information are stored in
696 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500697 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400698
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400699#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
700static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
701 struct btrfs_root *root,
702 struct btrfs_path *path,
703 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500704{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400705 struct btrfs_extent_item *item;
706 struct btrfs_extent_item_v0 *ei0;
707 struct btrfs_extent_ref_v0 *ref0;
708 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400709 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400710 struct btrfs_key key;
711 struct btrfs_key found_key;
712 u32 new_size = sizeof(*item);
713 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500714 int ret;
715
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400716 leaf = path->nodes[0];
717 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500718
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400719 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
720 ei0 = btrfs_item_ptr(leaf, path->slots[0],
721 struct btrfs_extent_item_v0);
722 refs = btrfs_extent_refs_v0(leaf, ei0);
723
724 if (owner == (u64)-1) {
725 while (1) {
726 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
727 ret = btrfs_next_leaf(root, path);
728 if (ret < 0)
729 return ret;
730 BUG_ON(ret > 0);
731 leaf = path->nodes[0];
732 }
733 btrfs_item_key_to_cpu(leaf, &found_key,
734 path->slots[0]);
735 BUG_ON(key.objectid != found_key.objectid);
736 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
737 path->slots[0]++;
738 continue;
739 }
740 ref0 = btrfs_item_ptr(leaf, path->slots[0],
741 struct btrfs_extent_ref_v0);
742 owner = btrfs_ref_objectid_v0(leaf, ref0);
743 break;
744 }
745 }
746 btrfs_release_path(root, path);
747
748 if (owner < BTRFS_FIRST_FREE_OBJECTID)
749 new_size += sizeof(*bi);
750
751 new_size -= sizeof(*ei0);
752 ret = btrfs_search_slot(trans, root, &key, path,
753 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400754 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400755 return ret;
756 BUG_ON(ret);
757
758 ret = btrfs_extend_item(trans, root, path, new_size);
759 BUG_ON(ret);
760
761 leaf = path->nodes[0];
762 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
763 btrfs_set_extent_refs(leaf, item, refs);
764 /* FIXME: get real generation */
765 btrfs_set_extent_generation(leaf, item, 0);
766 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
767 btrfs_set_extent_flags(leaf, item,
768 BTRFS_EXTENT_FLAG_TREE_BLOCK |
769 BTRFS_BLOCK_FLAG_FULL_BACKREF);
770 bi = (struct btrfs_tree_block_info *)(item + 1);
771 /* FIXME: get first key of the block */
772 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
773 btrfs_set_tree_block_level(leaf, bi, (int)owner);
774 } else {
775 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
776 }
777 btrfs_mark_buffer_dirty(leaf);
778 return 0;
779}
780#endif
781
782static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
783{
784 u32 high_crc = ~(u32)0;
785 u32 low_crc = ~(u32)0;
786 __le64 lenum;
787
788 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100789 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400790 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100791 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400792 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100793 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400794
795 return ((u64)high_crc << 31) ^ (u64)low_crc;
796}
797
798static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
799 struct btrfs_extent_data_ref *ref)
800{
801 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
802 btrfs_extent_data_ref_objectid(leaf, ref),
803 btrfs_extent_data_ref_offset(leaf, ref));
804}
805
806static int match_extent_data_ref(struct extent_buffer *leaf,
807 struct btrfs_extent_data_ref *ref,
808 u64 root_objectid, u64 owner, u64 offset)
809{
810 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
811 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
812 btrfs_extent_data_ref_offset(leaf, ref) != offset)
813 return 0;
814 return 1;
815}
816
817static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
818 struct btrfs_root *root,
819 struct btrfs_path *path,
820 u64 bytenr, u64 parent,
821 u64 root_objectid,
822 u64 owner, u64 offset)
823{
824 struct btrfs_key key;
825 struct btrfs_extent_data_ref *ref;
826 struct extent_buffer *leaf;
827 u32 nritems;
828 int ret;
829 int recow;
830 int err = -ENOENT;
831
832 key.objectid = bytenr;
833 if (parent) {
834 key.type = BTRFS_SHARED_DATA_REF_KEY;
835 key.offset = parent;
836 } else {
837 key.type = BTRFS_EXTENT_DATA_REF_KEY;
838 key.offset = hash_extent_data_ref(root_objectid,
839 owner, offset);
840 }
841again:
842 recow = 0;
843 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
844 if (ret < 0) {
845 err = ret;
846 goto fail;
847 }
848
849 if (parent) {
850 if (!ret)
851 return 0;
852#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
853 key.type = BTRFS_EXTENT_REF_V0_KEY;
854 btrfs_release_path(root, path);
855 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
856 if (ret < 0) {
857 err = ret;
858 goto fail;
859 }
860 if (!ret)
861 return 0;
862#endif
863 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400864 }
865
866 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400867 nritems = btrfs_header_nritems(leaf);
868 while (1) {
869 if (path->slots[0] >= nritems) {
870 ret = btrfs_next_leaf(root, path);
871 if (ret < 0)
872 err = ret;
873 if (ret)
874 goto fail;
875
876 leaf = path->nodes[0];
877 nritems = btrfs_header_nritems(leaf);
878 recow = 1;
879 }
880
881 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
882 if (key.objectid != bytenr ||
883 key.type != BTRFS_EXTENT_DATA_REF_KEY)
884 goto fail;
885
886 ref = btrfs_item_ptr(leaf, path->slots[0],
887 struct btrfs_extent_data_ref);
888
889 if (match_extent_data_ref(leaf, ref, root_objectid,
890 owner, offset)) {
891 if (recow) {
892 btrfs_release_path(root, path);
893 goto again;
894 }
895 err = 0;
896 break;
897 }
898 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400899 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400900fail:
901 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400902}
903
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400904static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
905 struct btrfs_root *root,
906 struct btrfs_path *path,
907 u64 bytenr, u64 parent,
908 u64 root_objectid, u64 owner,
909 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400910{
911 struct btrfs_key key;
912 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400913 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400914 u32 num_refs;
915 int ret;
916
917 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 if (parent) {
919 key.type = BTRFS_SHARED_DATA_REF_KEY;
920 key.offset = parent;
921 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400922 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400923 key.type = BTRFS_EXTENT_DATA_REF_KEY;
924 key.offset = hash_extent_data_ref(root_objectid,
925 owner, offset);
926 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400927 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400928
929 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
930 if (ret && ret != -EEXIST)
931 goto fail;
932
933 leaf = path->nodes[0];
934 if (parent) {
935 struct btrfs_shared_data_ref *ref;
936 ref = btrfs_item_ptr(leaf, path->slots[0],
937 struct btrfs_shared_data_ref);
938 if (ret == 0) {
939 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
940 } else {
941 num_refs = btrfs_shared_data_ref_count(leaf, ref);
942 num_refs += refs_to_add;
943 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
944 }
945 } else {
946 struct btrfs_extent_data_ref *ref;
947 while (ret == -EEXIST) {
948 ref = btrfs_item_ptr(leaf, path->slots[0],
949 struct btrfs_extent_data_ref);
950 if (match_extent_data_ref(leaf, ref, root_objectid,
951 owner, offset))
952 break;
953 btrfs_release_path(root, path);
954 key.offset++;
955 ret = btrfs_insert_empty_item(trans, root, path, &key,
956 size);
957 if (ret && ret != -EEXIST)
958 goto fail;
959
960 leaf = path->nodes[0];
961 }
962 ref = btrfs_item_ptr(leaf, path->slots[0],
963 struct btrfs_extent_data_ref);
964 if (ret == 0) {
965 btrfs_set_extent_data_ref_root(leaf, ref,
966 root_objectid);
967 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
968 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
969 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
970 } else {
971 num_refs = btrfs_extent_data_ref_count(leaf, ref);
972 num_refs += refs_to_add;
973 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
974 }
975 }
976 btrfs_mark_buffer_dirty(leaf);
977 ret = 0;
978fail:
Chris Mason7bb86312007-12-11 09:25:06 -0500979 btrfs_release_path(root, path);
980 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500981}
982
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400983static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
984 struct btrfs_root *root,
985 struct btrfs_path *path,
986 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -0400987{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988 struct btrfs_key key;
989 struct btrfs_extent_data_ref *ref1 = NULL;
990 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400991 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400992 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400993 int ret = 0;
994
995 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400996 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
997
998 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
999 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1000 struct btrfs_extent_data_ref);
1001 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1002 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1003 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1004 struct btrfs_shared_data_ref);
1005 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1006#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1007 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1008 struct btrfs_extent_ref_v0 *ref0;
1009 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1010 struct btrfs_extent_ref_v0);
1011 num_refs = btrfs_ref_count_v0(leaf, ref0);
1012#endif
1013 } else {
1014 BUG();
1015 }
1016
Chris Mason56bec292009-03-13 10:10:06 -04001017 BUG_ON(num_refs < refs_to_drop);
1018 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001019
Zheng Yan31840ae2008-09-23 13:14:14 -04001020 if (num_refs == 0) {
1021 ret = btrfs_del_item(trans, root, path);
1022 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001023 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1024 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1025 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1026 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1027#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1028 else {
1029 struct btrfs_extent_ref_v0 *ref0;
1030 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1031 struct btrfs_extent_ref_v0);
1032 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1033 }
1034#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001035 btrfs_mark_buffer_dirty(leaf);
1036 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037 return ret;
1038}
1039
1040static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1041 struct btrfs_path *path,
1042 struct btrfs_extent_inline_ref *iref)
1043{
1044 struct btrfs_key key;
1045 struct extent_buffer *leaf;
1046 struct btrfs_extent_data_ref *ref1;
1047 struct btrfs_shared_data_ref *ref2;
1048 u32 num_refs = 0;
1049
1050 leaf = path->nodes[0];
1051 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1052 if (iref) {
1053 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1054 BTRFS_EXTENT_DATA_REF_KEY) {
1055 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1056 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1057 } else {
1058 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1059 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1060 }
1061 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1062 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1063 struct btrfs_extent_data_ref);
1064 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1065 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1066 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1067 struct btrfs_shared_data_ref);
1068 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1069#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1070 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1071 struct btrfs_extent_ref_v0 *ref0;
1072 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1073 struct btrfs_extent_ref_v0);
1074 num_refs = btrfs_ref_count_v0(leaf, ref0);
1075#endif
1076 } else {
1077 WARN_ON(1);
1078 }
1079 return num_refs;
1080}
1081
1082static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1083 struct btrfs_root *root,
1084 struct btrfs_path *path,
1085 u64 bytenr, u64 parent,
1086 u64 root_objectid)
1087{
1088 struct btrfs_key key;
1089 int ret;
1090
1091 key.objectid = bytenr;
1092 if (parent) {
1093 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1094 key.offset = parent;
1095 } else {
1096 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1097 key.offset = root_objectid;
1098 }
1099
1100 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1101 if (ret > 0)
1102 ret = -ENOENT;
1103#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1104 if (ret == -ENOENT && parent) {
1105 btrfs_release_path(root, path);
1106 key.type = BTRFS_EXTENT_REF_V0_KEY;
1107 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1108 if (ret > 0)
1109 ret = -ENOENT;
1110 }
1111#endif
1112 return ret;
1113}
1114
1115static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1116 struct btrfs_root *root,
1117 struct btrfs_path *path,
1118 u64 bytenr, u64 parent,
1119 u64 root_objectid)
1120{
1121 struct btrfs_key key;
1122 int ret;
1123
1124 key.objectid = bytenr;
1125 if (parent) {
1126 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1127 key.offset = parent;
1128 } else {
1129 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1130 key.offset = root_objectid;
1131 }
1132
1133 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001134 btrfs_release_path(root, path);
1135 return ret;
1136}
1137
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001138static inline int extent_ref_type(u64 parent, u64 owner)
1139{
1140 int type;
1141 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1142 if (parent > 0)
1143 type = BTRFS_SHARED_BLOCK_REF_KEY;
1144 else
1145 type = BTRFS_TREE_BLOCK_REF_KEY;
1146 } else {
1147 if (parent > 0)
1148 type = BTRFS_SHARED_DATA_REF_KEY;
1149 else
1150 type = BTRFS_EXTENT_DATA_REF_KEY;
1151 }
1152 return type;
1153}
1154
Yan Zheng2c47e6052009-06-27 21:07:35 -04001155static int find_next_key(struct btrfs_path *path, int level,
1156 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001157
1158{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001159 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001160 if (!path->nodes[level])
1161 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001162 if (path->slots[level] + 1 >=
1163 btrfs_header_nritems(path->nodes[level]))
1164 continue;
1165 if (level == 0)
1166 btrfs_item_key_to_cpu(path->nodes[level], key,
1167 path->slots[level] + 1);
1168 else
1169 btrfs_node_key_to_cpu(path->nodes[level], key,
1170 path->slots[level] + 1);
1171 return 0;
1172 }
1173 return 1;
1174}
1175
1176/*
1177 * look for inline back ref. if back ref is found, *ref_ret is set
1178 * to the address of inline back ref, and 0 is returned.
1179 *
1180 * if back ref isn't found, *ref_ret is set to the address where it
1181 * should be inserted, and -ENOENT is returned.
1182 *
1183 * if insert is true and there are too many inline back refs, the path
1184 * points to the extent item, and -EAGAIN is returned.
1185 *
1186 * NOTE: inline back refs are ordered in the same way that back ref
1187 * items in the tree are ordered.
1188 */
1189static noinline_for_stack
1190int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1191 struct btrfs_root *root,
1192 struct btrfs_path *path,
1193 struct btrfs_extent_inline_ref **ref_ret,
1194 u64 bytenr, u64 num_bytes,
1195 u64 parent, u64 root_objectid,
1196 u64 owner, u64 offset, int insert)
1197{
1198 struct btrfs_key key;
1199 struct extent_buffer *leaf;
1200 struct btrfs_extent_item *ei;
1201 struct btrfs_extent_inline_ref *iref;
1202 u64 flags;
1203 u64 item_size;
1204 unsigned long ptr;
1205 unsigned long end;
1206 int extra_size;
1207 int type;
1208 int want;
1209 int ret;
1210 int err = 0;
1211
1212 key.objectid = bytenr;
1213 key.type = BTRFS_EXTENT_ITEM_KEY;
1214 key.offset = num_bytes;
1215
1216 want = extent_ref_type(parent, owner);
1217 if (insert) {
1218 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001219 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001220 } else
1221 extra_size = -1;
1222 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1223 if (ret < 0) {
1224 err = ret;
1225 goto out;
1226 }
1227 BUG_ON(ret);
1228
1229 leaf = path->nodes[0];
1230 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1231#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1232 if (item_size < sizeof(*ei)) {
1233 if (!insert) {
1234 err = -ENOENT;
1235 goto out;
1236 }
1237 ret = convert_extent_item_v0(trans, root, path, owner,
1238 extra_size);
1239 if (ret < 0) {
1240 err = ret;
1241 goto out;
1242 }
1243 leaf = path->nodes[0];
1244 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1245 }
1246#endif
1247 BUG_ON(item_size < sizeof(*ei));
1248
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001249 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1250 flags = btrfs_extent_flags(leaf, ei);
1251
1252 ptr = (unsigned long)(ei + 1);
1253 end = (unsigned long)ei + item_size;
1254
1255 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1256 ptr += sizeof(struct btrfs_tree_block_info);
1257 BUG_ON(ptr > end);
1258 } else {
1259 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1260 }
1261
1262 err = -ENOENT;
1263 while (1) {
1264 if (ptr >= end) {
1265 WARN_ON(ptr > end);
1266 break;
1267 }
1268 iref = (struct btrfs_extent_inline_ref *)ptr;
1269 type = btrfs_extent_inline_ref_type(leaf, iref);
1270 if (want < type)
1271 break;
1272 if (want > type) {
1273 ptr += btrfs_extent_inline_ref_size(type);
1274 continue;
1275 }
1276
1277 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1278 struct btrfs_extent_data_ref *dref;
1279 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1280 if (match_extent_data_ref(leaf, dref, root_objectid,
1281 owner, offset)) {
1282 err = 0;
1283 break;
1284 }
1285 if (hash_extent_data_ref_item(leaf, dref) <
1286 hash_extent_data_ref(root_objectid, owner, offset))
1287 break;
1288 } else {
1289 u64 ref_offset;
1290 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1291 if (parent > 0) {
1292 if (parent == ref_offset) {
1293 err = 0;
1294 break;
1295 }
1296 if (ref_offset < parent)
1297 break;
1298 } else {
1299 if (root_objectid == ref_offset) {
1300 err = 0;
1301 break;
1302 }
1303 if (ref_offset < root_objectid)
1304 break;
1305 }
1306 }
1307 ptr += btrfs_extent_inline_ref_size(type);
1308 }
1309 if (err == -ENOENT && insert) {
1310 if (item_size + extra_size >=
1311 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1312 err = -EAGAIN;
1313 goto out;
1314 }
1315 /*
1316 * To add new inline back ref, we have to make sure
1317 * there is no corresponding back ref item.
1318 * For simplicity, we just do not add new inline back
1319 * ref if there is any kind of item for this block
1320 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001321 if (find_next_key(path, 0, &key) == 0 &&
1322 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001323 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001324 err = -EAGAIN;
1325 goto out;
1326 }
1327 }
1328 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1329out:
Yan Zheng85d41982009-06-11 08:51:10 -04001330 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331 path->keep_locks = 0;
1332 btrfs_unlock_up_safe(path, 1);
1333 }
1334 return err;
1335}
1336
1337/*
1338 * helper to add new inline back ref
1339 */
1340static noinline_for_stack
1341int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1342 struct btrfs_root *root,
1343 struct btrfs_path *path,
1344 struct btrfs_extent_inline_ref *iref,
1345 u64 parent, u64 root_objectid,
1346 u64 owner, u64 offset, int refs_to_add,
1347 struct btrfs_delayed_extent_op *extent_op)
1348{
1349 struct extent_buffer *leaf;
1350 struct btrfs_extent_item *ei;
1351 unsigned long ptr;
1352 unsigned long end;
1353 unsigned long item_offset;
1354 u64 refs;
1355 int size;
1356 int type;
1357 int ret;
1358
1359 leaf = path->nodes[0];
1360 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1361 item_offset = (unsigned long)iref - (unsigned long)ei;
1362
1363 type = extent_ref_type(parent, owner);
1364 size = btrfs_extent_inline_ref_size(type);
1365
1366 ret = btrfs_extend_item(trans, root, path, size);
1367 BUG_ON(ret);
1368
1369 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1370 refs = btrfs_extent_refs(leaf, ei);
1371 refs += refs_to_add;
1372 btrfs_set_extent_refs(leaf, ei, refs);
1373 if (extent_op)
1374 __run_delayed_extent_op(extent_op, leaf, ei);
1375
1376 ptr = (unsigned long)ei + item_offset;
1377 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1378 if (ptr < end - size)
1379 memmove_extent_buffer(leaf, ptr + size, ptr,
1380 end - size - ptr);
1381
1382 iref = (struct btrfs_extent_inline_ref *)ptr;
1383 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1384 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1385 struct btrfs_extent_data_ref *dref;
1386 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1387 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1388 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1389 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1390 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1391 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1392 struct btrfs_shared_data_ref *sref;
1393 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1394 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1395 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1396 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1397 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1398 } else {
1399 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1400 }
1401 btrfs_mark_buffer_dirty(leaf);
1402 return 0;
1403}
1404
1405static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1406 struct btrfs_root *root,
1407 struct btrfs_path *path,
1408 struct btrfs_extent_inline_ref **ref_ret,
1409 u64 bytenr, u64 num_bytes, u64 parent,
1410 u64 root_objectid, u64 owner, u64 offset)
1411{
1412 int ret;
1413
1414 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1415 bytenr, num_bytes, parent,
1416 root_objectid, owner, offset, 0);
1417 if (ret != -ENOENT)
1418 return ret;
1419
1420 btrfs_release_path(root, path);
1421 *ref_ret = NULL;
1422
1423 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1424 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1425 root_objectid);
1426 } else {
1427 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1428 root_objectid, owner, offset);
1429 }
1430 return ret;
1431}
1432
1433/*
1434 * helper to update/remove inline back ref
1435 */
1436static noinline_for_stack
1437int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1438 struct btrfs_root *root,
1439 struct btrfs_path *path,
1440 struct btrfs_extent_inline_ref *iref,
1441 int refs_to_mod,
1442 struct btrfs_delayed_extent_op *extent_op)
1443{
1444 struct extent_buffer *leaf;
1445 struct btrfs_extent_item *ei;
1446 struct btrfs_extent_data_ref *dref = NULL;
1447 struct btrfs_shared_data_ref *sref = NULL;
1448 unsigned long ptr;
1449 unsigned long end;
1450 u32 item_size;
1451 int size;
1452 int type;
1453 int ret;
1454 u64 refs;
1455
1456 leaf = path->nodes[0];
1457 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1458 refs = btrfs_extent_refs(leaf, ei);
1459 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1460 refs += refs_to_mod;
1461 btrfs_set_extent_refs(leaf, ei, refs);
1462 if (extent_op)
1463 __run_delayed_extent_op(extent_op, leaf, ei);
1464
1465 type = btrfs_extent_inline_ref_type(leaf, iref);
1466
1467 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1468 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1469 refs = btrfs_extent_data_ref_count(leaf, dref);
1470 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1471 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1472 refs = btrfs_shared_data_ref_count(leaf, sref);
1473 } else {
1474 refs = 1;
1475 BUG_ON(refs_to_mod != -1);
1476 }
1477
1478 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1479 refs += refs_to_mod;
1480
1481 if (refs > 0) {
1482 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1483 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1484 else
1485 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1486 } else {
1487 size = btrfs_extent_inline_ref_size(type);
1488 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1489 ptr = (unsigned long)iref;
1490 end = (unsigned long)ei + item_size;
1491 if (ptr + size < end)
1492 memmove_extent_buffer(leaf, ptr, ptr + size,
1493 end - ptr - size);
1494 item_size -= size;
1495 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1496 BUG_ON(ret);
1497 }
1498 btrfs_mark_buffer_dirty(leaf);
1499 return 0;
1500}
1501
1502static noinline_for_stack
1503int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1504 struct btrfs_root *root,
1505 struct btrfs_path *path,
1506 u64 bytenr, u64 num_bytes, u64 parent,
1507 u64 root_objectid, u64 owner,
1508 u64 offset, int refs_to_add,
1509 struct btrfs_delayed_extent_op *extent_op)
1510{
1511 struct btrfs_extent_inline_ref *iref;
1512 int ret;
1513
1514 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1515 bytenr, num_bytes, parent,
1516 root_objectid, owner, offset, 1);
1517 if (ret == 0) {
1518 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1519 ret = update_inline_extent_backref(trans, root, path, iref,
1520 refs_to_add, extent_op);
1521 } else if (ret == -ENOENT) {
1522 ret = setup_inline_extent_backref(trans, root, path, iref,
1523 parent, root_objectid,
1524 owner, offset, refs_to_add,
1525 extent_op);
1526 }
1527 return ret;
1528}
1529
1530static int insert_extent_backref(struct btrfs_trans_handle *trans,
1531 struct btrfs_root *root,
1532 struct btrfs_path *path,
1533 u64 bytenr, u64 parent, u64 root_objectid,
1534 u64 owner, u64 offset, int refs_to_add)
1535{
1536 int ret;
1537 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1538 BUG_ON(refs_to_add != 1);
1539 ret = insert_tree_block_ref(trans, root, path, bytenr,
1540 parent, root_objectid);
1541 } else {
1542 ret = insert_extent_data_ref(trans, root, path, bytenr,
1543 parent, root_objectid,
1544 owner, offset, refs_to_add);
1545 }
1546 return ret;
1547}
1548
1549static int remove_extent_backref(struct btrfs_trans_handle *trans,
1550 struct btrfs_root *root,
1551 struct btrfs_path *path,
1552 struct btrfs_extent_inline_ref *iref,
1553 int refs_to_drop, int is_data)
1554{
1555 int ret;
1556
1557 BUG_ON(!is_data && refs_to_drop != 1);
1558 if (iref) {
1559 ret = update_inline_extent_backref(trans, root, path, iref,
1560 -refs_to_drop, NULL);
1561 } else if (is_data) {
1562 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1563 } else {
1564 ret = btrfs_del_item(trans, root, path);
1565 }
1566 return ret;
1567}
1568
Chris Mason4b4e25f2008-11-20 10:22:27 -05001569#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001570static void btrfs_issue_discard(struct block_device *bdev,
1571 u64 start, u64 len)
1572{
Chris Mason15916de2008-11-19 21:17:22 -05001573 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -05001574}
Chris Mason4b4e25f2008-11-20 10:22:27 -05001575#endif
Chris Mason15916de2008-11-19 21:17:22 -05001576
Liu Hui1f3c79a2009-01-05 15:57:51 -05001577static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1578 u64 num_bytes)
1579{
1580#ifdef BIO_RW_DISCARD
1581 int ret;
1582 u64 map_length = num_bytes;
1583 struct btrfs_multi_bio *multi = NULL;
1584
1585 /* Tell the block device(s) that the sectors can be discarded */
1586 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1587 bytenr, &map_length, &multi, 0);
1588 if (!ret) {
1589 struct btrfs_bio_stripe *stripe = multi->stripes;
1590 int i;
1591
1592 if (map_length > num_bytes)
1593 map_length = num_bytes;
1594
1595 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1596 btrfs_issue_discard(stripe->dev->bdev,
1597 stripe->physical,
1598 map_length);
1599 }
1600 kfree(multi);
1601 }
1602
1603 return ret;
1604#else
1605 return 0;
1606#endif
1607}
1608
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001609int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1610 struct btrfs_root *root,
1611 u64 bytenr, u64 num_bytes, u64 parent,
1612 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001613{
1614 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001615 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1616 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001617
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001618 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1619 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1620 parent, root_objectid, (int)owner,
1621 BTRFS_ADD_DELAYED_REF, NULL);
1622 } else {
1623 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1624 parent, root_objectid, owner, offset,
1625 BTRFS_ADD_DELAYED_REF, NULL);
1626 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001627 return ret;
1628}
1629
Chris Mason925baed2008-06-25 16:01:30 -04001630static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001631 struct btrfs_root *root,
1632 u64 bytenr, u64 num_bytes,
1633 u64 parent, u64 root_objectid,
1634 u64 owner, u64 offset, int refs_to_add,
1635 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001636{
Chris Mason5caf2a02007-04-02 11:20:42 -04001637 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001638 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001639 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001640 u64 refs;
1641 int ret;
1642 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001643
Chris Mason5caf2a02007-04-02 11:20:42 -04001644 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001645 if (!path)
1646 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001647
Chris Mason3c12ac72008-04-21 12:01:38 -04001648 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001649 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001650 /* this will setup the path even if it fails to insert the back ref */
1651 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1652 path, bytenr, num_bytes, parent,
1653 root_objectid, owner, offset,
1654 refs_to_add, extent_op);
1655 if (ret == 0)
1656 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001657
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001658 if (ret != -EAGAIN) {
1659 err = ret;
1660 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001661 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001662
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001663 leaf = path->nodes[0];
1664 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1665 refs = btrfs_extent_refs(leaf, item);
1666 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1667 if (extent_op)
1668 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001669
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001670 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001671 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001672
Chris Mason3c12ac72008-04-21 12:01:38 -04001673 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001674 path->leave_spinning = 1;
1675
Chris Mason56bec292009-03-13 10:10:06 -04001676 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001677 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001678 path, bytenr, parent, root_objectid,
1679 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001680 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001681out:
Chris Mason74493f72007-12-11 09:25:06 -05001682 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001683 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001684}
1685
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1687 struct btrfs_root *root,
1688 struct btrfs_delayed_ref_node *node,
1689 struct btrfs_delayed_extent_op *extent_op,
1690 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001691{
Chris Mason56bec292009-03-13 10:10:06 -04001692 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001693 struct btrfs_delayed_data_ref *ref;
1694 struct btrfs_key ins;
1695 u64 parent = 0;
1696 u64 ref_root = 0;
1697 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001698
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001699 ins.objectid = node->bytenr;
1700 ins.offset = node->num_bytes;
1701 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001702
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001703 ref = btrfs_delayed_node_to_data_ref(node);
1704 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1705 parent = ref->parent;
1706 else
1707 ref_root = ref->root;
1708
1709 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1710 if (extent_op) {
1711 BUG_ON(extent_op->update_key);
1712 flags |= extent_op->flags_to_set;
1713 }
1714 ret = alloc_reserved_file_extent(trans, root,
1715 parent, ref_root, flags,
1716 ref->objectid, ref->offset,
1717 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001718 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1719 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1720 node->num_bytes, parent,
1721 ref_root, ref->objectid,
1722 ref->offset, node->ref_mod,
1723 extent_op);
1724 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1725 ret = __btrfs_free_extent(trans, root, node->bytenr,
1726 node->num_bytes, parent,
1727 ref_root, ref->objectid,
1728 ref->offset, node->ref_mod,
1729 extent_op);
1730 } else {
1731 BUG();
1732 }
Chris Mason56bec292009-03-13 10:10:06 -04001733 return ret;
1734}
1735
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001736static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1737 struct extent_buffer *leaf,
1738 struct btrfs_extent_item *ei)
1739{
1740 u64 flags = btrfs_extent_flags(leaf, ei);
1741 if (extent_op->update_flags) {
1742 flags |= extent_op->flags_to_set;
1743 btrfs_set_extent_flags(leaf, ei, flags);
1744 }
1745
1746 if (extent_op->update_key) {
1747 struct btrfs_tree_block_info *bi;
1748 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1749 bi = (struct btrfs_tree_block_info *)(ei + 1);
1750 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1751 }
1752}
1753
1754static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1755 struct btrfs_root *root,
1756 struct btrfs_delayed_ref_node *node,
1757 struct btrfs_delayed_extent_op *extent_op)
1758{
1759 struct btrfs_key key;
1760 struct btrfs_path *path;
1761 struct btrfs_extent_item *ei;
1762 struct extent_buffer *leaf;
1763 u32 item_size;
1764 int ret;
1765 int err = 0;
1766
1767 path = btrfs_alloc_path();
1768 if (!path)
1769 return -ENOMEM;
1770
1771 key.objectid = node->bytenr;
1772 key.type = BTRFS_EXTENT_ITEM_KEY;
1773 key.offset = node->num_bytes;
1774
1775 path->reada = 1;
1776 path->leave_spinning = 1;
1777 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1778 path, 0, 1);
1779 if (ret < 0) {
1780 err = ret;
1781 goto out;
1782 }
1783 if (ret > 0) {
1784 err = -EIO;
1785 goto out;
1786 }
1787
1788 leaf = path->nodes[0];
1789 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1790#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1791 if (item_size < sizeof(*ei)) {
1792 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1793 path, (u64)-1, 0);
1794 if (ret < 0) {
1795 err = ret;
1796 goto out;
1797 }
1798 leaf = path->nodes[0];
1799 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1800 }
1801#endif
1802 BUG_ON(item_size < sizeof(*ei));
1803 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1804 __run_delayed_extent_op(extent_op, leaf, ei);
1805
1806 btrfs_mark_buffer_dirty(leaf);
1807out:
1808 btrfs_free_path(path);
1809 return err;
1810}
1811
1812static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1813 struct btrfs_root *root,
1814 struct btrfs_delayed_ref_node *node,
1815 struct btrfs_delayed_extent_op *extent_op,
1816 int insert_reserved)
1817{
1818 int ret = 0;
1819 struct btrfs_delayed_tree_ref *ref;
1820 struct btrfs_key ins;
1821 u64 parent = 0;
1822 u64 ref_root = 0;
1823
1824 ins.objectid = node->bytenr;
1825 ins.offset = node->num_bytes;
1826 ins.type = BTRFS_EXTENT_ITEM_KEY;
1827
1828 ref = btrfs_delayed_node_to_tree_ref(node);
1829 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1830 parent = ref->parent;
1831 else
1832 ref_root = ref->root;
1833
1834 BUG_ON(node->ref_mod != 1);
1835 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1836 BUG_ON(!extent_op || !extent_op->update_flags ||
1837 !extent_op->update_key);
1838 ret = alloc_reserved_tree_block(trans, root,
1839 parent, ref_root,
1840 extent_op->flags_to_set,
1841 &extent_op->key,
1842 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001843 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1844 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1845 node->num_bytes, parent, ref_root,
1846 ref->level, 0, 1, extent_op);
1847 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1848 ret = __btrfs_free_extent(trans, root, node->bytenr,
1849 node->num_bytes, parent, ref_root,
1850 ref->level, 0, 1, extent_op);
1851 } else {
1852 BUG();
1853 }
1854 return ret;
1855}
1856
1857
Chris Mason56bec292009-03-13 10:10:06 -04001858/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001859static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1860 struct btrfs_root *root,
1861 struct btrfs_delayed_ref_node *node,
1862 struct btrfs_delayed_extent_op *extent_op,
1863 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001864{
Josef Bacikeb099672009-02-12 09:27:38 -05001865 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001866 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001867 struct btrfs_delayed_ref_head *head;
1868 /*
1869 * we've hit the end of the chain and we were supposed
1870 * to insert this extent into the tree. But, it got
1871 * deleted before we ever needed to insert it, so all
1872 * we have to do is clean up the accounting
1873 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001874 BUG_ON(extent_op);
1875 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001876 if (insert_reserved) {
Yan Zheng11833d62009-09-11 16:11:19 -04001877 int mark_free = 0;
1878 struct extent_buffer *must_clean = NULL;
1879
1880 ret = pin_down_bytes(trans, root, NULL,
1881 node->bytenr, node->num_bytes,
1882 head->is_data, 1, &must_clean);
1883 if (ret > 0)
1884 mark_free = 1;
1885
1886 if (must_clean) {
1887 clean_tree_block(NULL, root, must_clean);
1888 btrfs_tree_unlock(must_clean);
1889 free_extent_buffer(must_clean);
1890 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001891 if (head->is_data) {
1892 ret = btrfs_del_csums(trans, root,
1893 node->bytenr,
1894 node->num_bytes);
1895 BUG_ON(ret);
1896 }
Yan Zheng11833d62009-09-11 16:11:19 -04001897 if (mark_free) {
1898 ret = btrfs_free_reserved_extent(root,
1899 node->bytenr,
1900 node->num_bytes);
1901 BUG_ON(ret);
1902 }
Chris Mason56bec292009-03-13 10:10:06 -04001903 }
Chris Mason56bec292009-03-13 10:10:06 -04001904 mutex_unlock(&head->mutex);
1905 return 0;
1906 }
Josef Bacikeb099672009-02-12 09:27:38 -05001907
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001908 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1909 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1910 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1911 insert_reserved);
1912 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1913 node->type == BTRFS_SHARED_DATA_REF_KEY)
1914 ret = run_delayed_data_ref(trans, root, node, extent_op,
1915 insert_reserved);
1916 else
1917 BUG();
1918 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001919}
1920
Chris Mason56bec292009-03-13 10:10:06 -04001921static noinline struct btrfs_delayed_ref_node *
1922select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001923{
Chris Mason56bec292009-03-13 10:10:06 -04001924 struct rb_node *node;
1925 struct btrfs_delayed_ref_node *ref;
1926 int action = BTRFS_ADD_DELAYED_REF;
1927again:
1928 /*
1929 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1930 * this prevents ref count from going down to zero when
1931 * there still are pending delayed ref.
1932 */
1933 node = rb_prev(&head->node.rb_node);
1934 while (1) {
1935 if (!node)
1936 break;
1937 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1938 rb_node);
1939 if (ref->bytenr != head->node.bytenr)
1940 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001941 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001942 return ref;
1943 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001944 }
Chris Mason56bec292009-03-13 10:10:06 -04001945 if (action == BTRFS_ADD_DELAYED_REF) {
1946 action = BTRFS_DROP_DELAYED_REF;
1947 goto again;
1948 }
1949 return NULL;
1950}
1951
Chris Masonc3e69d52009-03-13 10:17:05 -04001952static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1953 struct btrfs_root *root,
1954 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001955{
Chris Mason56bec292009-03-13 10:10:06 -04001956 struct btrfs_delayed_ref_root *delayed_refs;
1957 struct btrfs_delayed_ref_node *ref;
1958 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001959 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001960 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001961 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001962 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001963
1964 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001965 while (1) {
1966 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001967 /* pick a new head ref from the cluster list */
1968 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001969 break;
Chris Mason56bec292009-03-13 10:10:06 -04001970
Chris Masonc3e69d52009-03-13 10:17:05 -04001971 locked_ref = list_entry(cluster->next,
1972 struct btrfs_delayed_ref_head, cluster);
1973
1974 /* grab the lock that says we are going to process
1975 * all the refs for this head */
1976 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1977
1978 /*
1979 * we may have dropped the spin lock to get the head
1980 * mutex lock, and that might have given someone else
1981 * time to free the head. If that's true, it has been
1982 * removed from our list and we can move on.
1983 */
1984 if (ret == -EAGAIN) {
1985 locked_ref = NULL;
1986 count++;
1987 continue;
Chris Mason56bec292009-03-13 10:10:06 -04001988 }
1989 }
1990
1991 /*
1992 * record the must insert reserved flag before we
1993 * drop the spin lock.
1994 */
1995 must_insert_reserved = locked_ref->must_insert_reserved;
1996 locked_ref->must_insert_reserved = 0;
1997
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001998 extent_op = locked_ref->extent_op;
1999 locked_ref->extent_op = NULL;
2000
Chris Mason56bec292009-03-13 10:10:06 -04002001 /*
2002 * locked_ref is the head node, so we have to go one
2003 * node back for any delayed ref updates
2004 */
Chris Mason56bec292009-03-13 10:10:06 -04002005 ref = select_delayed_ref(locked_ref);
2006 if (!ref) {
2007 /* All delayed refs have been processed, Go ahead
2008 * and send the head node to run_one_delayed_ref,
2009 * so that any accounting fixes can happen
2010 */
2011 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002012
2013 if (extent_op && must_insert_reserved) {
2014 kfree(extent_op);
2015 extent_op = NULL;
2016 }
2017
2018 if (extent_op) {
2019 spin_unlock(&delayed_refs->lock);
2020
2021 ret = run_delayed_extent_op(trans, root,
2022 ref, extent_op);
2023 BUG_ON(ret);
2024 kfree(extent_op);
2025
2026 cond_resched();
2027 spin_lock(&delayed_refs->lock);
2028 continue;
2029 }
2030
Chris Masonc3e69d52009-03-13 10:17:05 -04002031 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002032 locked_ref = NULL;
2033 }
2034
2035 ref->in_tree = 0;
2036 rb_erase(&ref->rb_node, &delayed_refs->root);
2037 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002038
Chris Mason56bec292009-03-13 10:10:06 -04002039 spin_unlock(&delayed_refs->lock);
2040
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002041 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002042 must_insert_reserved);
2043 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002045 btrfs_put_delayed_ref(ref);
2046 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002047 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002048
Chris Mason1887be62009-03-13 10:11:24 -04002049 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002050 spin_lock(&delayed_refs->lock);
2051 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002052 return count;
2053}
2054
2055/*
2056 * this starts processing the delayed reference count updates and
2057 * extent insertions we have queued up so far. count can be
2058 * 0, which means to process everything in the tree at the start
2059 * of the run (but not newly added entries), or it can be some target
2060 * number you'd like to process.
2061 */
2062int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2063 struct btrfs_root *root, unsigned long count)
2064{
2065 struct rb_node *node;
2066 struct btrfs_delayed_ref_root *delayed_refs;
2067 struct btrfs_delayed_ref_node *ref;
2068 struct list_head cluster;
2069 int ret;
2070 int run_all = count == (unsigned long)-1;
2071 int run_most = 0;
2072
2073 if (root == root->fs_info->extent_root)
2074 root = root->fs_info->tree_root;
2075
2076 delayed_refs = &trans->transaction->delayed_refs;
2077 INIT_LIST_HEAD(&cluster);
2078again:
2079 spin_lock(&delayed_refs->lock);
2080 if (count == 0) {
2081 count = delayed_refs->num_entries * 2;
2082 run_most = 1;
2083 }
2084 while (1) {
2085 if (!(run_all || run_most) &&
2086 delayed_refs->num_heads_ready < 64)
2087 break;
2088
2089 /*
2090 * go find something we can process in the rbtree. We start at
2091 * the beginning of the tree, and then build a cluster
2092 * of refs to process starting at the first one we are able to
2093 * lock
2094 */
2095 ret = btrfs_find_ref_cluster(trans, &cluster,
2096 delayed_refs->run_delayed_start);
2097 if (ret)
2098 break;
2099
2100 ret = run_clustered_refs(trans, root, &cluster);
2101 BUG_ON(ret < 0);
2102
2103 count -= min_t(unsigned long, ret, count);
2104
2105 if (count == 0)
2106 break;
2107 }
2108
Chris Mason56bec292009-03-13 10:10:06 -04002109 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002110 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002111 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002112 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002113 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002114
2115 while (node) {
2116 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2117 rb_node);
2118 if (btrfs_delayed_ref_is_head(ref)) {
2119 struct btrfs_delayed_ref_head *head;
2120
2121 head = btrfs_delayed_node_to_head(ref);
2122 atomic_inc(&ref->refs);
2123
2124 spin_unlock(&delayed_refs->lock);
2125 mutex_lock(&head->mutex);
2126 mutex_unlock(&head->mutex);
2127
2128 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002129 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002130 goto again;
2131 }
2132 node = rb_next(node);
2133 }
2134 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002135 schedule_timeout(1);
2136 goto again;
2137 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002138out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002139 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002140 return 0;
2141}
2142
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002143int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2144 struct btrfs_root *root,
2145 u64 bytenr, u64 num_bytes, u64 flags,
2146 int is_data)
2147{
2148 struct btrfs_delayed_extent_op *extent_op;
2149 int ret;
2150
2151 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2152 if (!extent_op)
2153 return -ENOMEM;
2154
2155 extent_op->flags_to_set = flags;
2156 extent_op->update_flags = 1;
2157 extent_op->update_key = 0;
2158 extent_op->is_data = is_data ? 1 : 0;
2159
2160 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2161 if (ret)
2162 kfree(extent_op);
2163 return ret;
2164}
2165
2166static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2167 struct btrfs_root *root,
2168 struct btrfs_path *path,
2169 u64 objectid, u64 offset, u64 bytenr)
2170{
2171 struct btrfs_delayed_ref_head *head;
2172 struct btrfs_delayed_ref_node *ref;
2173 struct btrfs_delayed_data_ref *data_ref;
2174 struct btrfs_delayed_ref_root *delayed_refs;
2175 struct rb_node *node;
2176 int ret = 0;
2177
2178 ret = -ENOENT;
2179 delayed_refs = &trans->transaction->delayed_refs;
2180 spin_lock(&delayed_refs->lock);
2181 head = btrfs_find_delayed_ref_head(trans, bytenr);
2182 if (!head)
2183 goto out;
2184
2185 if (!mutex_trylock(&head->mutex)) {
2186 atomic_inc(&head->node.refs);
2187 spin_unlock(&delayed_refs->lock);
2188
2189 btrfs_release_path(root->fs_info->extent_root, path);
2190
2191 mutex_lock(&head->mutex);
2192 mutex_unlock(&head->mutex);
2193 btrfs_put_delayed_ref(&head->node);
2194 return -EAGAIN;
2195 }
2196
2197 node = rb_prev(&head->node.rb_node);
2198 if (!node)
2199 goto out_unlock;
2200
2201 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2202
2203 if (ref->bytenr != bytenr)
2204 goto out_unlock;
2205
2206 ret = 1;
2207 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2208 goto out_unlock;
2209
2210 data_ref = btrfs_delayed_node_to_data_ref(ref);
2211
2212 node = rb_prev(node);
2213 if (node) {
2214 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2215 if (ref->bytenr == bytenr)
2216 goto out_unlock;
2217 }
2218
2219 if (data_ref->root != root->root_key.objectid ||
2220 data_ref->objectid != objectid || data_ref->offset != offset)
2221 goto out_unlock;
2222
2223 ret = 0;
2224out_unlock:
2225 mutex_unlock(&head->mutex);
2226out:
2227 spin_unlock(&delayed_refs->lock);
2228 return ret;
2229}
2230
2231static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2232 struct btrfs_root *root,
2233 struct btrfs_path *path,
2234 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002235{
2236 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002237 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002238 struct btrfs_extent_data_ref *ref;
2239 struct btrfs_extent_inline_ref *iref;
2240 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002241 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002243 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002244
Chris Masonbe20aa92007-12-17 20:14:01 -05002245 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002246 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002247 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002248
Chris Masonbe20aa92007-12-17 20:14:01 -05002249 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2250 if (ret < 0)
2251 goto out;
2252 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002253
2254 ret = -ENOENT;
2255 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002256 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002257
Zheng Yan31840ae2008-09-23 13:14:14 -04002258 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002259 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002260 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002261
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002262 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002263 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002264
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002265 ret = 1;
2266 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2267#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2268 if (item_size < sizeof(*ei)) {
2269 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2270 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002271 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002272#endif
2273 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2274
2275 if (item_size != sizeof(*ei) +
2276 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2277 goto out;
2278
2279 if (btrfs_extent_generation(leaf, ei) <=
2280 btrfs_root_last_snapshot(&root->root_item))
2281 goto out;
2282
2283 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2284 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2285 BTRFS_EXTENT_DATA_REF_KEY)
2286 goto out;
2287
2288 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2289 if (btrfs_extent_refs(leaf, ei) !=
2290 btrfs_extent_data_ref_count(leaf, ref) ||
2291 btrfs_extent_data_ref_root(leaf, ref) !=
2292 root->root_key.objectid ||
2293 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2294 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2295 goto out;
2296
Yan Zhengf321e492008-07-30 09:26:11 -04002297 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002298out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299 return ret;
2300}
2301
2302int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2303 struct btrfs_root *root,
2304 u64 objectid, u64 offset, u64 bytenr)
2305{
2306 struct btrfs_path *path;
2307 int ret;
2308 int ret2;
2309
2310 path = btrfs_alloc_path();
2311 if (!path)
2312 return -ENOENT;
2313
2314 do {
2315 ret = check_committed_ref(trans, root, path, objectid,
2316 offset, bytenr);
2317 if (ret && ret != -ENOENT)
2318 goto out;
2319
2320 ret2 = check_delayed_ref(trans, root, path, objectid,
2321 offset, bytenr);
2322 } while (ret2 == -EAGAIN);
2323
2324 if (ret2 && ret2 != -ENOENT) {
2325 ret = ret2;
2326 goto out;
2327 }
2328
2329 if (ret != -ENOENT || ret2 != -ENOENT)
2330 ret = 0;
2331out:
Yan Zhengf321e492008-07-30 09:26:11 -04002332 btrfs_free_path(path);
2333 return ret;
2334}
2335
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002336#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002337int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2338 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002339{
Chris Mason5f39d392007-10-15 16:14:19 -04002340 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002341 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002342 u64 root_gen;
2343 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002344 int i;
Chris Masondb945352007-10-15 16:15:53 -04002345 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002346 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002347 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002348
Chris Mason3768f362007-03-13 16:47:54 -04002349 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002350 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002351
Zheng Yane4657682008-09-26 10:04:53 -04002352 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2353 shared = 0;
2354 root_gen = root->root_key.offset;
2355 } else {
2356 shared = 1;
2357 root_gen = trans->transid - 1;
2358 }
2359
Chris Masondb945352007-10-15 16:15:53 -04002360 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002361 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002362
Zheng Yan31840ae2008-09-23 13:14:14 -04002363 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002364 struct btrfs_leaf_ref *ref;
2365 struct btrfs_extent_info *info;
2366
Zheng Yan31840ae2008-09-23 13:14:14 -04002367 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002368 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002369 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002370 goto out;
2371 }
2372
Zheng Yane4657682008-09-26 10:04:53 -04002373 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002374 ref->bytenr = buf->start;
2375 ref->owner = btrfs_header_owner(buf);
2376 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002377 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002378 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002379
Zheng Yan31840ae2008-09-23 13:14:14 -04002380 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002381 u64 disk_bytenr;
2382 btrfs_item_key_to_cpu(buf, &key, i);
2383 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2384 continue;
2385 fi = btrfs_item_ptr(buf, i,
2386 struct btrfs_file_extent_item);
2387 if (btrfs_file_extent_type(buf, fi) ==
2388 BTRFS_FILE_EXTENT_INLINE)
2389 continue;
2390 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2391 if (disk_bytenr == 0)
2392 continue;
2393
2394 info->bytenr = disk_bytenr;
2395 info->num_bytes =
2396 btrfs_file_extent_disk_num_bytes(buf, fi);
2397 info->objectid = key.objectid;
2398 info->offset = key.offset;
2399 info++;
2400 }
2401
Zheng Yane4657682008-09-26 10:04:53 -04002402 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002403 if (ret == -EEXIST && shared) {
2404 struct btrfs_leaf_ref *old;
2405 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2406 BUG_ON(!old);
2407 btrfs_remove_leaf_ref(root, old);
2408 btrfs_free_leaf_ref(root, old);
2409 ret = btrfs_add_leaf_ref(root, ref, shared);
2410 }
Yan Zheng31153d82008-07-28 15:32:19 -04002411 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002412 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002413 }
2414out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002415 return ret;
2416}
2417
Chris Masonb7a9f292009-02-04 09:23:45 -05002418/* when a block goes through cow, we update the reference counts of
2419 * everything that block points to. The internal pointers of the block
2420 * can be in just about any order, and it is likely to have clusters of
2421 * things that are close together and clusters of things that are not.
2422 *
2423 * To help reduce the seeks that come with updating all of these reference
2424 * counts, sort them by byte number before actual updates are done.
2425 *
2426 * struct refsort is used to match byte number to slot in the btree block.
2427 * we sort based on the byte number and then use the slot to actually
2428 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002429 *
2430 * struct refsort is smaller than strcut btrfs_item and smaller than
2431 * struct btrfs_key_ptr. Since we're currently limited to the page size
2432 * for a btree block, there's no way for a kmalloc of refsorts for a
2433 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002434 */
2435struct refsort {
2436 u64 bytenr;
2437 u32 slot;
2438};
2439
2440/*
2441 * for passing into sort()
2442 */
2443static int refsort_cmp(const void *a_void, const void *b_void)
2444{
2445 const struct refsort *a = a_void;
2446 const struct refsort *b = b_void;
2447
2448 if (a->bytenr < b->bytenr)
2449 return -1;
2450 if (a->bytenr > b->bytenr)
2451 return 1;
2452 return 0;
2453}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002454#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002455
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002456static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002457 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002458 struct extent_buffer *buf,
2459 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002460{
2461 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002462 u64 num_bytes;
2463 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002464 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002465 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002466 struct btrfs_key key;
2467 struct btrfs_file_extent_item *fi;
2468 int i;
2469 int level;
2470 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002471 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002472 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002473
2474 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002475 nritems = btrfs_header_nritems(buf);
2476 level = btrfs_header_level(buf);
2477
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002478 if (!root->ref_cows && level == 0)
2479 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002480
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002481 if (inc)
2482 process_func = btrfs_inc_extent_ref;
2483 else
2484 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002485
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002486 if (full_backref)
2487 parent = buf->start;
2488 else
2489 parent = 0;
2490
Zheng Yan31840ae2008-09-23 13:14:14 -04002491 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002492 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002493 btrfs_item_key_to_cpu(buf, &key, i);
2494 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002495 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002496 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002497 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002498 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002499 BTRFS_FILE_EXTENT_INLINE)
2500 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002501 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2502 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002503 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002504
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002505 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2506 key.offset -= btrfs_file_extent_offset(buf, fi);
2507 ret = process_func(trans, root, bytenr, num_bytes,
2508 parent, ref_root, key.objectid,
2509 key.offset);
2510 if (ret)
2511 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002512 } else {
2513 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002514 num_bytes = btrfs_level_size(root, level - 1);
2515 ret = process_func(trans, root, bytenr, num_bytes,
2516 parent, ref_root, level - 1, 0);
2517 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002518 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002519 }
2520 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002521 return 0;
2522fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002523 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002524 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002525}
2526
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002527int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2528 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002529{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002530 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2531}
Zheng Yan31840ae2008-09-23 13:14:14 -04002532
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002533int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2534 struct extent_buffer *buf, int full_backref)
2535{
2536 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002537}
2538
Chris Mason9078a3e2007-04-26 16:46:15 -04002539static int write_one_cache_group(struct btrfs_trans_handle *trans,
2540 struct btrfs_root *root,
2541 struct btrfs_path *path,
2542 struct btrfs_block_group_cache *cache)
2543{
2544 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002545 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002546 unsigned long bi;
2547 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002548
Chris Mason9078a3e2007-04-26 16:46:15 -04002549 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002550 if (ret < 0)
2551 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002552 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002553
2554 leaf = path->nodes[0];
2555 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2556 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2557 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002558 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002559fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002560 if (ret)
2561 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002562 return 0;
2563
2564}
2565
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002566static struct btrfs_block_group_cache *
2567next_block_group(struct btrfs_root *root,
2568 struct btrfs_block_group_cache *cache)
2569{
2570 struct rb_node *node;
2571 spin_lock(&root->fs_info->block_group_cache_lock);
2572 node = rb_next(&cache->cache_node);
2573 btrfs_put_block_group(cache);
2574 if (node) {
2575 cache = rb_entry(node, struct btrfs_block_group_cache,
2576 cache_node);
2577 atomic_inc(&cache->count);
2578 } else
2579 cache = NULL;
2580 spin_unlock(&root->fs_info->block_group_cache_lock);
2581 return cache;
2582}
2583
Chris Mason96b51792007-10-15 16:15:19 -04002584int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2585 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002586{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002587 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002588 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002589 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002590 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002591
2592 path = btrfs_alloc_path();
2593 if (!path)
2594 return -ENOMEM;
2595
Chris Masond3977122009-01-05 21:25:51 -05002596 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002597 if (last == 0) {
2598 err = btrfs_run_delayed_refs(trans, root,
2599 (unsigned long)-1);
2600 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002601 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002602
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002603 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2604 while (cache) {
2605 if (cache->dirty)
2606 break;
2607 cache = next_block_group(root, cache);
2608 }
2609 if (!cache) {
2610 if (last == 0)
2611 break;
2612 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002613 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002614 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002615
2616 cache->dirty = 0;
2617 last = cache->key.objectid + cache->key.offset;
2618
2619 err = write_one_cache_group(trans, root, path, cache);
2620 BUG_ON(err);
2621 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002622 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002623
Chris Mason9078a3e2007-04-26 16:46:15 -04002624 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002625 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002626}
2627
Yan Zhengd2fb3432008-12-11 16:30:39 -05002628int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2629{
2630 struct btrfs_block_group_cache *block_group;
2631 int readonly = 0;
2632
2633 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2634 if (!block_group || block_group->ro)
2635 readonly = 1;
2636 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002637 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002638 return readonly;
2639}
2640
Chris Mason593060d2008-03-25 16:50:33 -04002641static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2642 u64 total_bytes, u64 bytes_used,
2643 struct btrfs_space_info **space_info)
2644{
2645 struct btrfs_space_info *found;
2646
2647 found = __find_space_info(info, flags);
2648 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002649 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002650 found->total_bytes += total_bytes;
2651 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002652 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002653 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002654 *space_info = found;
2655 return 0;
2656 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002657 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002658 if (!found)
2659 return -ENOMEM;
2660
Josef Bacik0f9dd462008-09-23 13:14:11 -04002661 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002662 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002663 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002664 found->flags = flags;
2665 found->total_bytes = total_bytes;
2666 found->bytes_used = bytes_used;
2667 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002668 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002669 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002670 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002671 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002672 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002673 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002674 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002675 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002676 return 0;
2677}
2678
Chris Mason8790d502008-04-03 16:29:03 -04002679static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2680{
2681 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002682 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002683 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002684 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002685 if (extra_flags) {
2686 if (flags & BTRFS_BLOCK_GROUP_DATA)
2687 fs_info->avail_data_alloc_bits |= extra_flags;
2688 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2689 fs_info->avail_metadata_alloc_bits |= extra_flags;
2690 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2691 fs_info->avail_system_alloc_bits |= extra_flags;
2692 }
2693}
Chris Mason593060d2008-03-25 16:50:33 -04002694
Yan Zhengc146afa2008-11-12 14:34:12 -05002695static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2696{
2697 spin_lock(&cache->space_info->lock);
2698 spin_lock(&cache->lock);
2699 if (!cache->ro) {
2700 cache->space_info->bytes_readonly += cache->key.offset -
2701 btrfs_block_group_used(&cache->item);
2702 cache->ro = 1;
2703 }
2704 spin_unlock(&cache->lock);
2705 spin_unlock(&cache->space_info->lock);
2706}
2707
Yan Zheng2b820322008-11-17 21:11:30 -05002708u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002709{
Yan Zheng2b820322008-11-17 21:11:30 -05002710 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002711
2712 if (num_devices == 1)
2713 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2714 if (num_devices < 4)
2715 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2716
Chris Masonec44a352008-04-28 15:29:52 -04002717 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2718 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002719 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002720 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002721 }
Chris Masonec44a352008-04-28 15:29:52 -04002722
2723 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002724 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002725 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002726 }
Chris Masonec44a352008-04-28 15:29:52 -04002727
2728 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2729 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2730 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2731 (flags & BTRFS_BLOCK_GROUP_DUP)))
2732 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2733 return flags;
2734}
2735
Josef Bacik6a632092009-02-20 11:00:09 -05002736static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2737{
2738 struct btrfs_fs_info *info = root->fs_info;
2739 u64 alloc_profile;
2740
2741 if (data) {
2742 alloc_profile = info->avail_data_alloc_bits &
2743 info->data_alloc_profile;
2744 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2745 } else if (root == root->fs_info->chunk_root) {
2746 alloc_profile = info->avail_system_alloc_bits &
2747 info->system_alloc_profile;
2748 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2749 } else {
2750 alloc_profile = info->avail_metadata_alloc_bits &
2751 info->metadata_alloc_profile;
2752 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2753 }
2754
2755 return btrfs_reduce_alloc_profile(root, data);
2756}
2757
2758void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2759{
2760 u64 alloc_target;
2761
2762 alloc_target = btrfs_get_alloc_profile(root, 1);
2763 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2764 alloc_target);
2765}
2766
2767/*
2768 * for now this just makes sure we have at least 5% of our metadata space free
2769 * for use.
2770 */
2771int btrfs_check_metadata_free_space(struct btrfs_root *root)
2772{
2773 struct btrfs_fs_info *info = root->fs_info;
2774 struct btrfs_space_info *meta_sinfo;
2775 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002776 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002777
2778 /* get the space info for where the metadata will live */
2779 alloc_target = btrfs_get_alloc_profile(root, 0);
2780 meta_sinfo = __find_space_info(info, alloc_target);
Chris Mason33b4d472009-09-22 14:45:50 -04002781 if (!meta_sinfo)
2782 goto alloc;
Josef Bacik6a632092009-02-20 11:00:09 -05002783
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002784again:
Josef Bacik6a632092009-02-20 11:00:09 -05002785 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002786 if (!meta_sinfo->full)
2787 thresh = meta_sinfo->total_bytes * 80;
2788 else
2789 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05002790
2791 do_div(thresh, 100);
2792
2793 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
Josef Bacik1b2da372009-09-11 16:11:20 -04002794 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2795 meta_sinfo->bytes_super > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002796 struct btrfs_trans_handle *trans;
2797 if (!meta_sinfo->full) {
2798 meta_sinfo->force_alloc = 1;
2799 spin_unlock(&meta_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04002800alloc:
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002801 trans = btrfs_start_transaction(root, 1);
2802 if (!trans)
2803 return -ENOMEM;
2804
2805 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2806 2 * 1024 * 1024, alloc_target, 0);
2807 btrfs_end_transaction(trans, root);
Chris Mason33b4d472009-09-22 14:45:50 -04002808 if (!meta_sinfo) {
2809 meta_sinfo = __find_space_info(info,
2810 alloc_target);
2811 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002812 goto again;
2813 }
Josef Bacik6a632092009-02-20 11:00:09 -05002814 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002815
2816 if (!committed) {
2817 committed = 1;
2818 trans = btrfs_join_transaction(root, 1);
2819 if (!trans)
2820 return -ENOMEM;
2821 ret = btrfs_commit_transaction(trans, root);
2822 if (ret)
2823 return ret;
2824 goto again;
2825 }
Josef Bacik6a632092009-02-20 11:00:09 -05002826 return -ENOSPC;
2827 }
2828 spin_unlock(&meta_sinfo->lock);
2829
2830 return 0;
2831}
2832
2833/*
2834 * This will check the space that the inode allocates from to make sure we have
2835 * enough space for bytes.
2836 */
2837int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
2838 u64 bytes)
2839{
2840 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002841 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002842
2843 /* make sure bytes are sectorsize aligned */
2844 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2845
2846 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04002847 if (!data_sinfo)
2848 goto alloc;
2849
Josef Bacik6a632092009-02-20 11:00:09 -05002850again:
2851 /* make sure we have enough space to handle the data first */
2852 spin_lock(&data_sinfo->lock);
2853 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
2854 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
2855 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
Josef Bacik1b2da372009-09-11 16:11:20 -04002856 data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002857 struct btrfs_trans_handle *trans;
2858
Josef Bacik6a632092009-02-20 11:00:09 -05002859 /*
2860 * if we don't have enough free bytes in this space then we need
2861 * to alloc a new chunk.
2862 */
2863 if (!data_sinfo->full) {
2864 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05002865
2866 data_sinfo->force_alloc = 1;
2867 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04002868alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05002869 alloc_target = btrfs_get_alloc_profile(root, 1);
2870 trans = btrfs_start_transaction(root, 1);
2871 if (!trans)
2872 return -ENOMEM;
2873
2874 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2875 bytes + 2 * 1024 * 1024,
2876 alloc_target, 0);
2877 btrfs_end_transaction(trans, root);
2878 if (ret)
2879 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04002880
2881 if (!data_sinfo) {
2882 btrfs_set_inode_space_info(root, inode);
2883 data_sinfo = BTRFS_I(inode)->space_info;
2884 }
Josef Bacik6a632092009-02-20 11:00:09 -05002885 goto again;
2886 }
2887 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002888
2889 /* commit the current transaction and try again */
2890 if (!committed) {
2891 committed = 1;
2892 trans = btrfs_join_transaction(root, 1);
2893 if (!trans)
2894 return -ENOMEM;
2895 ret = btrfs_commit_transaction(trans, root);
2896 if (ret)
2897 return ret;
2898 goto again;
2899 }
2900
Josef Bacik6a632092009-02-20 11:00:09 -05002901 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2902 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04002903 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07002904 "%llu total\n", (unsigned long long)bytes,
2905 (unsigned long long)data_sinfo->bytes_delalloc,
2906 (unsigned long long)data_sinfo->bytes_used,
2907 (unsigned long long)data_sinfo->bytes_reserved,
2908 (unsigned long long)data_sinfo->bytes_pinned,
2909 (unsigned long long)data_sinfo->bytes_readonly,
2910 (unsigned long long)data_sinfo->bytes_may_use,
2911 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05002912 return -ENOSPC;
2913 }
2914 data_sinfo->bytes_may_use += bytes;
2915 BTRFS_I(inode)->reserved_bytes += bytes;
2916 spin_unlock(&data_sinfo->lock);
2917
2918 return btrfs_check_metadata_free_space(root);
2919}
2920
2921/*
2922 * if there was an error for whatever reason after calling
2923 * btrfs_check_data_free_space, call this so we can cleanup the counters.
2924 */
2925void btrfs_free_reserved_data_space(struct btrfs_root *root,
2926 struct inode *inode, u64 bytes)
2927{
2928 struct btrfs_space_info *data_sinfo;
2929
2930 /* make sure bytes are sectorsize aligned */
2931 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2932
2933 data_sinfo = BTRFS_I(inode)->space_info;
2934 spin_lock(&data_sinfo->lock);
2935 data_sinfo->bytes_may_use -= bytes;
2936 BTRFS_I(inode)->reserved_bytes -= bytes;
2937 spin_unlock(&data_sinfo->lock);
2938}
2939
2940/* called when we are adding a delalloc extent to the inode's io_tree */
2941void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
2942 u64 bytes)
2943{
2944 struct btrfs_space_info *data_sinfo;
2945
2946 /* get the space info for where this inode will be storing its data */
2947 data_sinfo = BTRFS_I(inode)->space_info;
2948
2949 /* make sure we have enough space to handle the data first */
2950 spin_lock(&data_sinfo->lock);
2951 data_sinfo->bytes_delalloc += bytes;
2952
2953 /*
2954 * we are adding a delalloc extent without calling
2955 * btrfs_check_data_free_space first. This happens on a weird
2956 * writepage condition, but shouldn't hurt our accounting
2957 */
2958 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
2959 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
2960 BTRFS_I(inode)->reserved_bytes = 0;
2961 } else {
2962 data_sinfo->bytes_may_use -= bytes;
2963 BTRFS_I(inode)->reserved_bytes -= bytes;
2964 }
2965
2966 spin_unlock(&data_sinfo->lock);
2967}
2968
2969/* called when we are clearing an delalloc extent from the inode's io_tree */
2970void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2971 u64 bytes)
2972{
2973 struct btrfs_space_info *info;
2974
2975 info = BTRFS_I(inode)->space_info;
2976
2977 spin_lock(&info->lock);
2978 info->bytes_delalloc -= bytes;
2979 spin_unlock(&info->lock);
2980}
2981
Josef Bacik97e728d2009-04-21 17:40:57 -04002982static void force_metadata_allocation(struct btrfs_fs_info *info)
2983{
2984 struct list_head *head = &info->space_info;
2985 struct btrfs_space_info *found;
2986
2987 rcu_read_lock();
2988 list_for_each_entry_rcu(found, head, list) {
2989 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
2990 found->force_alloc = 1;
2991 }
2992 rcu_read_unlock();
2993}
2994
Chris Mason6324fbf2008-03-24 15:01:59 -04002995static int do_chunk_alloc(struct btrfs_trans_handle *trans,
2996 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04002997 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04002998{
2999 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003000 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003001 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05003002 int ret = 0;
3003
Josef Bacik97e728d2009-04-21 17:40:57 -04003004 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003005
Yan Zheng2b820322008-11-17 21:11:30 -05003006 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003007
Chris Mason6324fbf2008-03-24 15:01:59 -04003008 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003009 if (!space_info) {
3010 ret = update_space_info(extent_root->fs_info, flags,
3011 0, 0, &space_info);
3012 BUG_ON(ret);
3013 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003014 BUG_ON(!space_info);
3015
Josef Bacik25179202008-10-29 14:49:05 -04003016 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003017 if (space_info->force_alloc) {
3018 force = 1;
3019 space_info->force_alloc = 0;
3020 }
Josef Bacik25179202008-10-29 14:49:05 -04003021 if (space_info->full) {
3022 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003023 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003024 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003025
Yan Zhengc146afa2008-11-12 14:34:12 -05003026 thresh = space_info->total_bytes - space_info->bytes_readonly;
3027 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04003028 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04003029 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04003030 space_info->bytes_reserved + alloc_bytes) < thresh) {
3031 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003032 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003033 }
Josef Bacik25179202008-10-29 14:49:05 -04003034 spin_unlock(&space_info->lock);
3035
Josef Bacik97e728d2009-04-21 17:40:57 -04003036 /*
3037 * if we're doing a data chunk, go ahead and make sure that
3038 * we keep a reasonable number of metadata chunks allocated in the
3039 * FS as well.
3040 */
3041 if (flags & BTRFS_BLOCK_GROUP_DATA) {
3042 fs_info->data_chunk_allocations++;
3043 if (!(fs_info->data_chunk_allocations %
3044 fs_info->metadata_ratio))
3045 force_metadata_allocation(fs_info);
3046 }
3047
Yan Zheng2b820322008-11-17 21:11:30 -05003048 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05003049 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003050 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04003051out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003052 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003053 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003054}
3055
Chris Mason9078a3e2007-04-26 16:46:15 -04003056static int update_block_group(struct btrfs_trans_handle *trans,
3057 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04003058 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04003059 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04003060{
3061 struct btrfs_block_group_cache *cache;
3062 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04003063 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003064 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003065 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04003066
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003067 /* block accounting for super block */
3068 spin_lock(&info->delalloc_lock);
3069 old_val = btrfs_super_bytes_used(&info->super_copy);
3070 if (alloc)
3071 old_val += num_bytes;
3072 else
3073 old_val -= num_bytes;
3074 btrfs_set_super_bytes_used(&info->super_copy, old_val);
3075
3076 /* block accounting for root item */
3077 old_val = btrfs_root_used(&root->root_item);
3078 if (alloc)
3079 old_val += num_bytes;
3080 else
3081 old_val -= num_bytes;
3082 btrfs_set_root_used(&root->root_item, old_val);
3083 spin_unlock(&info->delalloc_lock);
3084
Chris Masond3977122009-01-05 21:25:51 -05003085 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003086 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003087 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04003088 return -1;
Chris Masondb945352007-10-15 16:15:53 -04003089 byte_in_group = bytenr - cache->key.objectid;
3090 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003091
Josef Bacik25179202008-10-29 14:49:05 -04003092 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003093 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003094 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04003095 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04003096 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04003097 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04003098 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003099 btrfs_set_block_group_used(&cache->item, old_val);
3100 cache->reserved -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003101 cache->space_info->bytes_used += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003102 cache->space_info->bytes_reserved -= num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003103 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05003104 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003105 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003106 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003107 } else {
Chris Masondb945352007-10-15 16:15:53 -04003108 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003109 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05003110 if (cache->ro)
3111 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003112 btrfs_set_block_group_used(&cache->item, old_val);
3113 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003114 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04003115 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003116 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003117
3118 ret = btrfs_discard_extent(root, bytenr,
3119 num_bytes);
3120 WARN_ON(ret);
3121
Josef Bacik0f9dd462008-09-23 13:14:11 -04003122 ret = btrfs_add_free_space(cache, bytenr,
3123 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003124 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04003125 }
Chris Masoncd1bc462007-04-27 10:08:34 -04003126 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003127 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003128 total -= num_bytes;
3129 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003130 }
3131 return 0;
3132}
Chris Mason6324fbf2008-03-24 15:01:59 -04003133
Chris Masona061fc82008-05-07 11:43:44 -04003134static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3135{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003136 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003137 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003138
3139 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3140 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003141 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003142
Yan Zhengd2fb3432008-12-11 16:30:39 -05003143 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003144 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003145
3146 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003147}
3148
Yan Zheng11833d62009-09-11 16:11:19 -04003149/*
3150 * this function must be called within transaction
3151 */
3152int btrfs_pin_extent(struct btrfs_root *root,
3153 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05003154{
Yan324ae4d2007-11-16 14:57:08 -05003155 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -04003156 struct btrfs_block_group_cache *cache;
Yan324ae4d2007-11-16 14:57:08 -05003157
Yan Zheng11833d62009-09-11 16:11:19 -04003158 cache = btrfs_lookup_block_group(fs_info, bytenr);
3159 BUG_ON(!cache);
Chris Masonb9473432009-03-13 11:00:37 -04003160
Yan Zheng11833d62009-09-11 16:11:19 -04003161 spin_lock(&cache->space_info->lock);
3162 spin_lock(&cache->lock);
3163 cache->pinned += num_bytes;
3164 cache->space_info->bytes_pinned += num_bytes;
3165 if (reserved) {
3166 cache->reserved -= num_bytes;
3167 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05003168 }
Yan Zheng11833d62009-09-11 16:11:19 -04003169 spin_unlock(&cache->lock);
3170 spin_unlock(&cache->space_info->lock);
3171
3172 btrfs_put_block_group(cache);
3173
3174 set_extent_dirty(fs_info->pinned_extents,
3175 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Yan324ae4d2007-11-16 14:57:08 -05003176 return 0;
3177}
Chris Mason9078a3e2007-04-26 16:46:15 -04003178
Yan Zheng11833d62009-09-11 16:11:19 -04003179static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3180 u64 num_bytes, int reserve)
Zheng Yane8569812008-09-26 10:05:48 -04003181{
Yan Zheng11833d62009-09-11 16:11:19 -04003182 spin_lock(&cache->space_info->lock);
3183 spin_lock(&cache->lock);
3184 if (reserve) {
3185 cache->reserved += num_bytes;
3186 cache->space_info->bytes_reserved += num_bytes;
3187 } else {
3188 cache->reserved -= num_bytes;
3189 cache->space_info->bytes_reserved -= num_bytes;
3190 }
3191 spin_unlock(&cache->lock);
3192 spin_unlock(&cache->space_info->lock);
3193 return 0;
3194}
Zheng Yane8569812008-09-26 10:05:48 -04003195
Yan Zheng11833d62009-09-11 16:11:19 -04003196int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3197 struct btrfs_root *root)
3198{
3199 struct btrfs_fs_info *fs_info = root->fs_info;
3200 struct btrfs_caching_control *next;
3201 struct btrfs_caching_control *caching_ctl;
3202 struct btrfs_block_group_cache *cache;
3203
3204 down_write(&fs_info->extent_commit_sem);
3205
3206 list_for_each_entry_safe(caching_ctl, next,
3207 &fs_info->caching_block_groups, list) {
3208 cache = caching_ctl->block_group;
3209 if (block_group_cache_done(cache)) {
3210 cache->last_byte_to_unpin = (u64)-1;
3211 list_del_init(&caching_ctl->list);
3212 put_caching_control(caching_ctl);
3213 } else {
3214 cache->last_byte_to_unpin = caching_ctl->progress;
3215 }
3216 }
3217
3218 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3219 fs_info->pinned_extents = &fs_info->freed_extents[1];
3220 else
3221 fs_info->pinned_extents = &fs_info->freed_extents[0];
3222
3223 up_write(&fs_info->extent_commit_sem);
3224 return 0;
3225}
3226
3227static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3228{
3229 struct btrfs_fs_info *fs_info = root->fs_info;
3230 struct btrfs_block_group_cache *cache = NULL;
3231 u64 len;
3232
3233 while (start <= end) {
3234 if (!cache ||
3235 start >= cache->key.objectid + cache->key.offset) {
3236 if (cache)
3237 btrfs_put_block_group(cache);
3238 cache = btrfs_lookup_block_group(fs_info, start);
3239 BUG_ON(!cache);
3240 }
3241
3242 len = cache->key.objectid + cache->key.offset - start;
3243 len = min(len, end + 1 - start);
3244
3245 if (start < cache->last_byte_to_unpin) {
3246 len = min(len, cache->last_byte_to_unpin - start);
3247 btrfs_add_free_space(cache, start, len);
3248 }
Josef Bacik25179202008-10-29 14:49:05 -04003249
3250 spin_lock(&cache->space_info->lock);
3251 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003252 cache->pinned -= len;
3253 cache->space_info->bytes_pinned -= len;
Josef Bacik25179202008-10-29 14:49:05 -04003254 spin_unlock(&cache->lock);
3255 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003256
3257 start += len;
3258 }
3259
3260 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003261 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04003262 return 0;
3263}
3264
3265int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04003266 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05003267{
Yan Zheng11833d62009-09-11 16:11:19 -04003268 struct btrfs_fs_info *fs_info = root->fs_info;
3269 struct extent_io_tree *unpin;
Chris Mason1a5bc1672007-10-15 16:15:26 -04003270 u64 start;
3271 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003272 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003273
Yan Zheng11833d62009-09-11 16:11:19 -04003274 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3275 unpin = &fs_info->freed_extents[1];
3276 else
3277 unpin = &fs_info->freed_extents[0];
3278
Chris Masond3977122009-01-05 21:25:51 -05003279 while (1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04003280 ret = find_first_extent_bit(unpin, 0, &start, &end,
3281 EXTENT_DIRTY);
3282 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003283 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003284
3285 ret = btrfs_discard_extent(root, start, end + 1 - start);
3286
Chris Mason1a5bc1672007-10-15 16:15:26 -04003287 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04003288 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04003289 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003290 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003291
Liu Hui1f3c79a2009-01-05 15:57:51 -05003292 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003293}
3294
Zheng Yan31840ae2008-09-23 13:14:14 -04003295static int pin_down_bytes(struct btrfs_trans_handle *trans,
3296 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003297 struct btrfs_path *path,
Yan Zheng11833d62009-09-11 16:11:19 -04003298 u64 bytenr, u64 num_bytes,
3299 int is_data, int reserved,
Chris Masonb9473432009-03-13 11:00:37 -04003300 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003301{
Chris Mason1a5bc1672007-10-15 16:15:26 -04003302 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003303 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003304
Zheng Yan31840ae2008-09-23 13:14:14 -04003305 if (is_data)
3306 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003307
Zheng Yan31840ae2008-09-23 13:14:14 -04003308 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3309 if (!buf)
3310 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003311
Zheng Yan31840ae2008-09-23 13:14:14 -04003312 /* we can reuse a block if it hasn't been written
3313 * and it is from this transaction. We can't
3314 * reuse anything from the tree log root because
3315 * it has tiny sub-transactions.
3316 */
3317 if (btrfs_buffer_uptodate(buf, 0) &&
3318 btrfs_try_tree_lock(buf)) {
3319 u64 header_owner = btrfs_header_owner(buf);
3320 u64 header_transid = btrfs_header_generation(buf);
3321 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3322 header_transid == trans->transid &&
3323 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003324 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003325 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003326 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003327 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003328 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003329 free_extent_buffer(buf);
3330pinit:
Yan Zheng11833d62009-09-11 16:11:19 -04003331 if (path)
3332 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003333 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003334 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
Zheng Yan31840ae2008-09-23 13:14:14 -04003335
Chris Masonbe744172007-05-06 10:15:01 -04003336 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003337 return 0;
3338}
3339
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003340static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3341 struct btrfs_root *root,
3342 u64 bytenr, u64 num_bytes, u64 parent,
3343 u64 root_objectid, u64 owner_objectid,
3344 u64 owner_offset, int refs_to_drop,
3345 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003346{
Chris Masone2fa7222007-03-12 16:22:34 -04003347 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003348 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003349 struct btrfs_fs_info *info = root->fs_info;
3350 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003351 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003352 struct btrfs_extent_item *ei;
3353 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003354 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003355 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003356 int extent_slot = 0;
3357 int found_extent = 0;
3358 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003359 u32 item_size;
3360 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003361
Chris Mason5caf2a02007-04-02 11:20:42 -04003362 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003363 if (!path)
3364 return -ENOMEM;
3365
Chris Mason3c12ac72008-04-21 12:01:38 -04003366 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003367 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003368
3369 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3370 BUG_ON(!is_data && refs_to_drop != 1);
3371
3372 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3373 bytenr, num_bytes, parent,
3374 root_objectid, owner_objectid,
3375 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003376 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003377 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003378 while (extent_slot >= 0) {
3379 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003380 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003381 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003382 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003383 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3384 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003385 found_extent = 1;
3386 break;
3387 }
3388 if (path->slots[0] - extent_slot > 5)
3389 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003390 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003391 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003392#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3393 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3394 if (found_extent && item_size < sizeof(*ei))
3395 found_extent = 0;
3396#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003397 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003398 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003399 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003400 NULL, refs_to_drop,
3401 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003402 BUG_ON(ret);
3403 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003404 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003405
3406 key.objectid = bytenr;
3407 key.type = BTRFS_EXTENT_ITEM_KEY;
3408 key.offset = num_bytes;
3409
Zheng Yan31840ae2008-09-23 13:14:14 -04003410 ret = btrfs_search_slot(trans, extent_root,
3411 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003412 if (ret) {
3413 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003414 ", was looking for %llu\n", ret,
3415 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003416 btrfs_print_leaf(extent_root, path->nodes[0]);
3417 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003418 BUG_ON(ret);
3419 extent_slot = path->slots[0];
3420 }
Chris Mason7bb86312007-12-11 09:25:06 -05003421 } else {
3422 btrfs_print_leaf(extent_root, path->nodes[0]);
3423 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003424 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003425 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003426 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003427 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003428 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003429 (unsigned long long)owner_objectid,
3430 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003431 }
Chris Mason5f39d392007-10-15 16:14:19 -04003432
3433 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003434 item_size = btrfs_item_size_nr(leaf, extent_slot);
3435#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3436 if (item_size < sizeof(*ei)) {
3437 BUG_ON(found_extent || extent_slot != path->slots[0]);
3438 ret = convert_extent_item_v0(trans, extent_root, path,
3439 owner_objectid, 0);
3440 BUG_ON(ret < 0);
3441
3442 btrfs_release_path(extent_root, path);
3443 path->leave_spinning = 1;
3444
3445 key.objectid = bytenr;
3446 key.type = BTRFS_EXTENT_ITEM_KEY;
3447 key.offset = num_bytes;
3448
3449 ret = btrfs_search_slot(trans, extent_root, &key, path,
3450 -1, 1);
3451 if (ret) {
3452 printk(KERN_ERR "umm, got %d back from search"
3453 ", was looking for %llu\n", ret,
3454 (unsigned long long)bytenr);
3455 btrfs_print_leaf(extent_root, path->nodes[0]);
3456 }
3457 BUG_ON(ret);
3458 extent_slot = path->slots[0];
3459 leaf = path->nodes[0];
3460 item_size = btrfs_item_size_nr(leaf, extent_slot);
3461 }
3462#endif
3463 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003464 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003465 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003466 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3467 struct btrfs_tree_block_info *bi;
3468 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3469 bi = (struct btrfs_tree_block_info *)(ei + 1);
3470 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003471 }
3472
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003473 refs = btrfs_extent_refs(leaf, ei);
3474 BUG_ON(refs < refs_to_drop);
3475 refs -= refs_to_drop;
3476
3477 if (refs > 0) {
3478 if (extent_op)
3479 __run_delayed_extent_op(extent_op, leaf, ei);
3480 /*
3481 * In the case of inline back ref, reference count will
3482 * be updated by remove_extent_backref
3483 */
3484 if (iref) {
3485 BUG_ON(!found_extent);
3486 } else {
3487 btrfs_set_extent_refs(leaf, ei, refs);
3488 btrfs_mark_buffer_dirty(leaf);
3489 }
3490 if (found_extent) {
3491 ret = remove_extent_backref(trans, extent_root, path,
3492 iref, refs_to_drop,
3493 is_data);
3494 BUG_ON(ret);
3495 }
3496 } else {
3497 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003498 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003499
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003500 if (found_extent) {
3501 BUG_ON(is_data && refs_to_drop !=
3502 extent_data_ref_count(root, path, iref));
3503 if (iref) {
3504 BUG_ON(path->slots[0] != extent_slot);
3505 } else {
3506 BUG_ON(path->slots[0] != extent_slot + 1);
3507 path->slots[0] = extent_slot;
3508 num_to_del = 2;
3509 }
Chris Mason78fae272007-03-25 11:35:08 -04003510 }
Chris Masonb9473432009-03-13 11:00:37 -04003511
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003512 ret = pin_down_bytes(trans, root, path, bytenr,
Yan Zheng11833d62009-09-11 16:11:19 -04003513 num_bytes, is_data, 0, &must_clean);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003514 if (ret > 0)
3515 mark_free = 1;
3516 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003517 /*
3518 * it is going to be very rare for someone to be waiting
3519 * on the block we're freeing. del_items might need to
3520 * schedule, so rather than get fancy, just force it
3521 * to blocking here
3522 */
3523 if (must_clean)
3524 btrfs_set_lock_blocking(must_clean);
3525
Chris Mason952fcca2008-02-18 16:33:44 -05003526 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3527 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003528 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003529 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003530
Chris Masonb9473432009-03-13 11:00:37 -04003531 if (must_clean) {
3532 clean_tree_block(NULL, root, must_clean);
3533 btrfs_tree_unlock(must_clean);
3534 free_extent_buffer(must_clean);
3535 }
3536
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003537 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003538 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3539 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003540 } else {
3541 invalidate_mapping_pages(info->btree_inode->i_mapping,
3542 bytenr >> PAGE_CACHE_SHIFT,
3543 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003544 }
3545
Chris Masondcbdd4d2008-12-16 13:51:01 -05003546 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3547 mark_free);
3548 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003549 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003550 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003551 return ret;
3552}
3553
3554/*
Chris Mason1887be62009-03-13 10:11:24 -04003555 * when we free an extent, it is possible (and likely) that we free the last
3556 * delayed ref for that extent as well. This searches the delayed ref tree for
3557 * a given extent, and if there are no other delayed refs to be processed, it
3558 * removes it from the tree.
3559 */
3560static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3561 struct btrfs_root *root, u64 bytenr)
3562{
3563 struct btrfs_delayed_ref_head *head;
3564 struct btrfs_delayed_ref_root *delayed_refs;
3565 struct btrfs_delayed_ref_node *ref;
3566 struct rb_node *node;
3567 int ret;
3568
3569 delayed_refs = &trans->transaction->delayed_refs;
3570 spin_lock(&delayed_refs->lock);
3571 head = btrfs_find_delayed_ref_head(trans, bytenr);
3572 if (!head)
3573 goto out;
3574
3575 node = rb_prev(&head->node.rb_node);
3576 if (!node)
3577 goto out;
3578
3579 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3580
3581 /* there are still entries for this ref, we can't drop it */
3582 if (ref->bytenr == bytenr)
3583 goto out;
3584
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003585 if (head->extent_op) {
3586 if (!head->must_insert_reserved)
3587 goto out;
3588 kfree(head->extent_op);
3589 head->extent_op = NULL;
3590 }
3591
Chris Mason1887be62009-03-13 10:11:24 -04003592 /*
3593 * waiting for the lock here would deadlock. If someone else has it
3594 * locked they are already in the process of dropping it anyway
3595 */
3596 if (!mutex_trylock(&head->mutex))
3597 goto out;
3598
3599 /*
3600 * at this point we have a head with no other entries. Go
3601 * ahead and process it.
3602 */
3603 head->node.in_tree = 0;
3604 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04003605
Chris Mason1887be62009-03-13 10:11:24 -04003606 delayed_refs->num_entries--;
3607
3608 /*
3609 * we don't take a ref on the node because we're removing it from the
3610 * tree, so we just steal the ref the tree was holding.
3611 */
Chris Masonc3e69d52009-03-13 10:17:05 -04003612 delayed_refs->num_heads--;
3613 if (list_empty(&head->cluster))
3614 delayed_refs->num_heads_ready--;
3615
3616 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04003617 spin_unlock(&delayed_refs->lock);
3618
3619 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003620 &head->node, head->extent_op,
3621 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04003622 BUG_ON(ret);
3623 btrfs_put_delayed_ref(&head->node);
3624 return 0;
3625out:
3626 spin_unlock(&delayed_refs->lock);
3627 return 0;
3628}
3629
Chris Mason925baed2008-06-25 16:01:30 -04003630int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003631 struct btrfs_root *root,
3632 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003633 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04003634{
3635 int ret;
3636
Chris Mason56bec292009-03-13 10:10:06 -04003637 /*
3638 * tree log blocks never actually go into the extent allocation
3639 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003640 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003641 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
3642 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04003643 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003644 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04003645 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003646 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
3647 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
3648 parent, root_objectid, (int)owner,
3649 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04003650 BUG_ON(ret);
3651 ret = check_ref_cleanup(trans, root, bytenr);
3652 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003653 } else {
3654 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
3655 parent, root_objectid, owner,
3656 offset, BTRFS_DROP_DELAYED_REF, NULL);
3657 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04003658 }
Chris Mason925baed2008-06-25 16:01:30 -04003659 return ret;
3660}
3661
Chris Mason87ee04e2007-11-30 11:30:34 -05003662static u64 stripe_align(struct btrfs_root *root, u64 val)
3663{
3664 u64 mask = ((u64)root->stripesize - 1);
3665 u64 ret = (val + mask) & ~mask;
3666 return ret;
3667}
3668
Chris Masonfec577f2007-02-26 10:40:21 -05003669/*
Josef Bacik817d52f2009-07-13 21:29:25 -04003670 * when we wait for progress in the block group caching, its because
3671 * our allocation attempt failed at least once. So, we must sleep
3672 * and let some progress happen before we try again.
3673 *
3674 * This function will sleep at least once waiting for new free space to
3675 * show up, and then it will check the block group free space numbers
3676 * for our min num_bytes. Another option is to have it go ahead
3677 * and look in the rbtree for a free extent of a given size, but this
3678 * is a good start.
3679 */
3680static noinline int
3681wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
3682 u64 num_bytes)
3683{
Yan Zheng11833d62009-09-11 16:11:19 -04003684 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04003685 DEFINE_WAIT(wait);
3686
Yan Zheng11833d62009-09-11 16:11:19 -04003687 caching_ctl = get_caching_control(cache);
3688 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04003689 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04003690
Yan Zheng11833d62009-09-11 16:11:19 -04003691 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04003692 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04003693
3694 put_caching_control(caching_ctl);
3695 return 0;
3696}
3697
3698static noinline int
3699wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
3700{
3701 struct btrfs_caching_control *caching_ctl;
3702 DEFINE_WAIT(wait);
3703
3704 caching_ctl = get_caching_control(cache);
3705 if (!caching_ctl)
3706 return 0;
3707
3708 wait_event(caching_ctl->wait, block_group_cache_done(cache));
3709
3710 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04003711 return 0;
3712}
3713
3714enum btrfs_loop_type {
3715 LOOP_CACHED_ONLY = 0,
3716 LOOP_CACHING_NOWAIT = 1,
3717 LOOP_CACHING_WAIT = 2,
3718 LOOP_ALLOC_CHUNK = 3,
3719 LOOP_NO_EMPTY_SIZE = 4,
3720};
3721
3722/*
Chris Masonfec577f2007-02-26 10:40:21 -05003723 * walks the btree of allocated extents and find a hole of a given size.
3724 * The key ins is changed to record the hole:
3725 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04003726 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05003727 * ins->offset == number of blocks
3728 * Any available blocks before search_start are skipped.
3729 */
Chris Masond3977122009-01-05 21:25:51 -05003730static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003731 struct btrfs_root *orig_root,
3732 u64 num_bytes, u64 empty_size,
3733 u64 search_start, u64 search_end,
3734 u64 hint_byte, struct btrfs_key *ins,
3735 u64 exclude_start, u64 exclude_nr,
3736 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05003737{
Josef Bacik80eb2342008-10-29 14:49:05 -04003738 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003739 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003740 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04003741 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04003742 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04003743 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003744 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003745 int last_ptr_loop = 0;
3746 int loop = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04003747 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04003748 bool failed_cluster_refill = false;
Chris Masonfec577f2007-02-26 10:40:21 -05003749
Chris Masondb945352007-10-15 16:15:53 -04003750 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04003751 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04003752 ins->objectid = 0;
3753 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003754
Josef Bacik2552d172009-04-03 10:14:19 -04003755 space_info = __find_space_info(root->fs_info, data);
3756
Chris Mason0ef3e662008-05-24 14:04:53 -04003757 if (orig_root->ref_cows || empty_size)
3758 allowed_chunk_alloc = 1;
3759
Chris Mason239b14b2008-03-24 15:02:07 -04003760 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003761 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05003762 if (!btrfs_test_opt(root, SSD))
3763 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04003764 }
3765
Chris Masonfa9c0d792009-04-03 09:47:43 -04003766 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
3767 last_ptr = &root->fs_info->data_alloc_cluster;
3768 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003769
Chris Mason239b14b2008-03-24 15:02:07 -04003770 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003771 spin_lock(&last_ptr->lock);
3772 if (last_ptr->block_group)
3773 hint_byte = last_ptr->window_start;
3774 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003775 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003776
Chris Masona061fc82008-05-07 11:43:44 -04003777 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04003778 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04003779
Josef Bacik817d52f2009-07-13 21:29:25 -04003780 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003781 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003782
Josef Bacik2552d172009-04-03 10:14:19 -04003783 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04003784 block_group = btrfs_lookup_block_group(root->fs_info,
3785 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04003786 /*
3787 * we don't want to use the block group if it doesn't match our
3788 * allocation bits, or if its not cached.
3789 */
3790 if (block_group && block_group_bits(block_group, data) &&
3791 block_group_cache_done(block_group)) {
Josef Bacik2552d172009-04-03 10:14:19 -04003792 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003793 if (list_empty(&block_group->list) ||
3794 block_group->ro) {
3795 /*
3796 * someone is removing this block group,
3797 * we can't jump into the have_block_group
3798 * target because our list pointers are not
3799 * valid
3800 */
3801 btrfs_put_block_group(block_group);
3802 up_read(&space_info->groups_sem);
3803 } else
3804 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003805 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003806 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003807 }
Chris Mason42e70e72008-11-07 18:17:11 -05003808 }
Chris Mason43662112008-11-07 09:06:11 -05003809
Josef Bacik2552d172009-04-03 10:14:19 -04003810search:
Josef Bacik80eb2342008-10-29 14:49:05 -04003811 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04003812 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04003813 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04003814 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05003815
Josef Bacik2552d172009-04-03 10:14:19 -04003816 atomic_inc(&block_group->count);
3817 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05003818
Josef Bacik2552d172009-04-03 10:14:19 -04003819have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04003820 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
3821 /*
3822 * we want to start caching kthreads, but not too many
3823 * right off the bat so we don't overwhelm the system,
3824 * so only start them if there are less than 2 and we're
3825 * in the initial allocation phase.
3826 */
3827 if (loop > LOOP_CACHING_NOWAIT ||
3828 atomic_read(&space_info->caching_threads) < 2) {
3829 ret = cache_block_group(block_group);
3830 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04003831 }
Josef Bacikea6a4782008-11-20 12:16:16 -05003832 }
3833
Josef Bacik817d52f2009-07-13 21:29:25 -04003834 cached = block_group_cache_done(block_group);
3835 if (unlikely(!cached)) {
3836 found_uncached_bg = true;
3837
3838 /* if we only want cached bgs, loop */
3839 if (loop == LOOP_CACHED_ONLY)
3840 goto loop;
3841 }
3842
Josef Bacikea6a4782008-11-20 12:16:16 -05003843 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04003844 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003845
Josef Bacik0a243252009-09-11 16:11:20 -04003846 /*
3847 * Ok we want to try and use the cluster allocator, so lets look
3848 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
3849 * have tried the cluster allocator plenty of times at this
3850 * point and not have found anything, so we are likely way too
3851 * fragmented for the clustering stuff to find anything, so lets
3852 * just skip it and let the allocator find whatever block it can
3853 * find
3854 */
3855 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003856 /*
3857 * the refill lock keeps out other
3858 * people trying to start a new cluster
3859 */
3860 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003861 if (last_ptr->block_group &&
3862 (last_ptr->block_group->ro ||
3863 !block_group_bits(last_ptr->block_group, data))) {
3864 offset = 0;
3865 goto refill_cluster;
3866 }
3867
Chris Masonfa9c0d792009-04-03 09:47:43 -04003868 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
3869 num_bytes, search_start);
3870 if (offset) {
3871 /* we have a block, we're done */
3872 spin_unlock(&last_ptr->refill_lock);
3873 goto checks;
3874 }
3875
3876 spin_lock(&last_ptr->lock);
3877 /*
3878 * whoops, this cluster doesn't actually point to
3879 * this block group. Get a ref on the block
3880 * group is does point to and try again
3881 */
3882 if (!last_ptr_loop && last_ptr->block_group &&
3883 last_ptr->block_group != block_group) {
3884
3885 btrfs_put_block_group(block_group);
3886 block_group = last_ptr->block_group;
3887 atomic_inc(&block_group->count);
3888 spin_unlock(&last_ptr->lock);
3889 spin_unlock(&last_ptr->refill_lock);
3890
3891 last_ptr_loop = 1;
3892 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04003893 /*
3894 * we know this block group is properly
3895 * in the list because
3896 * btrfs_remove_block_group, drops the
3897 * cluster before it removes the block
3898 * group from the list
3899 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04003900 goto have_block_group;
3901 }
3902 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003903refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04003904 /*
3905 * this cluster didn't work out, free it and
3906 * start over
3907 */
3908 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3909
3910 last_ptr_loop = 0;
3911
3912 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04003913 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003914 block_group, last_ptr,
3915 offset, num_bytes,
3916 empty_cluster + empty_size);
3917 if (ret == 0) {
3918 /*
3919 * now pull our allocation out of this
3920 * cluster
3921 */
3922 offset = btrfs_alloc_from_cluster(block_group,
3923 last_ptr, num_bytes,
3924 search_start);
3925 if (offset) {
3926 /* we found one, proceed */
3927 spin_unlock(&last_ptr->refill_lock);
3928 goto checks;
3929 }
Josef Bacik0a243252009-09-11 16:11:20 -04003930 } else if (!cached && loop > LOOP_CACHING_NOWAIT
3931 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04003932 spin_unlock(&last_ptr->refill_lock);
3933
Josef Bacik0a243252009-09-11 16:11:20 -04003934 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04003935 wait_block_group_cache_progress(block_group,
3936 num_bytes + empty_cluster + empty_size);
3937 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003938 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003939
Chris Masonfa9c0d792009-04-03 09:47:43 -04003940 /*
3941 * at this point we either didn't find a cluster
3942 * or we weren't able to allocate a block from our
3943 * cluster. Free the cluster we've been trying
3944 * to use, and go to the next block group
3945 */
Josef Bacik0a243252009-09-11 16:11:20 -04003946 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003947 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04003948 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003949 }
3950
Josef Bacik6226cb02009-04-03 10:14:18 -04003951 offset = btrfs_find_space_for_alloc(block_group, search_start,
3952 num_bytes, empty_size);
Josef Bacik817d52f2009-07-13 21:29:25 -04003953 if (!offset && (cached || (!cached &&
3954 loop == LOOP_CACHING_NOWAIT))) {
Josef Bacik2552d172009-04-03 10:14:19 -04003955 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04003956 } else if (!offset && (!cached &&
3957 loop > LOOP_CACHING_NOWAIT)) {
3958 wait_block_group_cache_progress(block_group,
3959 num_bytes + empty_size);
3960 goto have_block_group;
3961 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003962checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04003963 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04003964 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04003965 if (search_start + num_bytes >= search_end) {
3966 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003967 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04003968 }
Chris Masone37c9e62007-05-09 20:13:14 -04003969
Josef Bacik2552d172009-04-03 10:14:19 -04003970 /* move on to the next group */
3971 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04003972 block_group->key.objectid + block_group->key.offset) {
3973 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003974 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04003975 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003976
Josef Bacik2552d172009-04-03 10:14:19 -04003977 if (exclude_nr > 0 &&
3978 (search_start + num_bytes > exclude_start &&
3979 search_start < exclude_start + exclude_nr)) {
3980 search_start = exclude_start + exclude_nr;
3981
Josef Bacik6226cb02009-04-03 10:14:18 -04003982 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003983 /*
3984 * if search_start is still in this block group
3985 * then we just re-search this block group
3986 */
3987 if (search_start >= block_group->key.objectid &&
3988 search_start < (block_group->key.objectid +
Josef Bacik6226cb02009-04-03 10:14:18 -04003989 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04003990 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003991 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003992 }
Josef Bacik2552d172009-04-03 10:14:19 -04003993
3994 ins->objectid = search_start;
3995 ins->offset = num_bytes;
3996
Josef Bacik6226cb02009-04-03 10:14:18 -04003997 if (offset < search_start)
3998 btrfs_add_free_space(block_group, offset,
3999 search_start - offset);
4000 BUG_ON(offset > search_start);
4001
Yan Zheng11833d62009-09-11 16:11:19 -04004002 update_reserved_extents(block_group, num_bytes, 1);
4003
Josef Bacik2552d172009-04-03 10:14:19 -04004004 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04004005 break;
4006loop:
Josef Bacik0a243252009-09-11 16:11:20 -04004007 failed_cluster_refill = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004008 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004009 }
4010 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004011
Josef Bacik817d52f2009-07-13 21:29:25 -04004012 /* LOOP_CACHED_ONLY, only search fully cached block groups
4013 * LOOP_CACHING_NOWAIT, search partially cached block groups, but
4014 * dont wait foR them to finish caching
4015 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4016 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4017 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4018 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04004019 */
Josef Bacik817d52f2009-07-13 21:29:25 -04004020 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4021 (found_uncached_bg || empty_size || empty_cluster ||
4022 allowed_chunk_alloc)) {
4023 if (found_uncached_bg) {
4024 found_uncached_bg = false;
4025 if (loop < LOOP_CACHING_WAIT) {
4026 loop++;
4027 goto search;
4028 }
4029 }
4030
4031 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004032 empty_size = 0;
4033 empty_cluster = 0;
4034 }
Chris Mason42e70e72008-11-07 18:17:11 -05004035
Josef Bacik2552d172009-04-03 10:14:19 -04004036 if (allowed_chunk_alloc) {
4037 ret = do_chunk_alloc(trans, root, num_bytes +
4038 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04004039 allowed_chunk_alloc = 0;
4040 } else {
4041 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004042 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004043
Josef Bacik817d52f2009-07-13 21:29:25 -04004044 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004045 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04004046 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004047 }
Josef Bacik2552d172009-04-03 10:14:19 -04004048 ret = -ENOSPC;
4049 } else if (!ins->objectid) {
4050 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04004051 }
Chris Mason0b86a832008-03-24 15:01:56 -04004052
Josef Bacik80eb2342008-10-29 14:49:05 -04004053 /* we found what we needed */
4054 if (ins->objectid) {
4055 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05004056 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04004057
Chris Masonfa9c0d792009-04-03 09:47:43 -04004058 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004059 ret = 0;
4060 }
Chris Mason0b86a832008-03-24 15:01:56 -04004061
Chris Mason0f70abe2007-02-28 16:46:22 -05004062 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004063}
Chris Masonec44a352008-04-28 15:29:52 -04004064
Josef Bacik0f9dd462008-09-23 13:14:11 -04004065static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
4066{
4067 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004068
Chris Masond3977122009-01-05 21:25:51 -05004069 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4070 (unsigned long long)(info->total_bytes - info->bytes_used -
4071 info->bytes_pinned - info->bytes_reserved),
4072 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05004073 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Joel Becker21380932009-04-21 12:38:29 -07004074 " may_use=%llu, used=%llu\n",
4075 (unsigned long long)info->total_bytes,
4076 (unsigned long long)info->bytes_pinned,
4077 (unsigned long long)info->bytes_delalloc,
4078 (unsigned long long)info->bytes_may_use,
4079 (unsigned long long)info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004080
Josef Bacik80eb2342008-10-29 14:49:05 -04004081 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05004082 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004083 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05004084 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4085 "%llu pinned %llu reserved\n",
4086 (unsigned long long)cache->key.objectid,
4087 (unsigned long long)cache->key.offset,
4088 (unsigned long long)btrfs_block_group_used(&cache->item),
4089 (unsigned long long)cache->pinned,
4090 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004091 btrfs_dump_free_space(cache, bytes);
4092 spin_unlock(&cache->lock);
4093 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004094 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004095}
Zheng Yane8569812008-09-26 10:05:48 -04004096
Yan Zheng11833d62009-09-11 16:11:19 -04004097int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4098 struct btrfs_root *root,
4099 u64 num_bytes, u64 min_alloc_size,
4100 u64 empty_size, u64 hint_byte,
4101 u64 search_end, struct btrfs_key *ins,
4102 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004103{
4104 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04004105 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04004106 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04004107
Josef Bacik6a632092009-02-20 11:00:09 -05004108 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04004109again:
Chris Mason0ef3e662008-05-24 14:04:53 -04004110 /*
4111 * the only place that sets empty_size is btrfs_realloc_node, which
4112 * is not called recursively on allocations
4113 */
4114 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04004115 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04004116 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004117 2 * 1024 * 1024,
4118 BTRFS_BLOCK_GROUP_METADATA |
4119 (info->metadata_alloc_profile &
4120 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04004121 }
4122 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004123 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04004124 }
Chris Mason0b86a832008-03-24 15:01:56 -04004125
Chris Masondb945352007-10-15 16:15:53 -04004126 WARN_ON(num_bytes < root->sectorsize);
4127 ret = find_free_extent(trans, root, num_bytes, empty_size,
4128 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04004129 trans->alloc_exclude_start,
4130 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04004131
Chris Mason98d20f62008-04-14 09:46:10 -04004132 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4133 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004134 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004135 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04004136 do_chunk_alloc(trans, root->fs_info->extent_root,
4137 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004138 goto again;
4139 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004140 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004141 struct btrfs_space_info *sinfo;
4142
4143 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004144 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4145 "wanted %llu\n", (unsigned long long)data,
4146 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004147 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04004148 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004149
4150 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004151}
4152
Chris Mason65b51a02008-08-01 15:11:20 -04004153int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4154{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004155 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004156 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004157
Josef Bacik0f9dd462008-09-23 13:14:11 -04004158 cache = btrfs_lookup_block_group(root->fs_info, start);
4159 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004160 printk(KERN_ERR "Unable to find block group for %llu\n",
4161 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004162 return -ENOSPC;
4163 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004164
4165 ret = btrfs_discard_extent(root, start, len);
4166
Josef Bacik0f9dd462008-09-23 13:14:11 -04004167 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04004168 update_reserved_extents(cache, len, 0);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004169 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04004170
Chris Masone6dcd2d2008-07-17 12:53:50 -04004171 return ret;
4172}
4173
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004174static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4175 struct btrfs_root *root,
4176 u64 parent, u64 root_objectid,
4177 u64 flags, u64 owner, u64 offset,
4178 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004179{
4180 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004181 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004182 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004183 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004184 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004185 struct extent_buffer *leaf;
4186 int type;
4187 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004188
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004189 if (parent > 0)
4190 type = BTRFS_SHARED_DATA_REF_KEY;
4191 else
4192 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004193
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004194 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004195
4196 path = btrfs_alloc_path();
4197 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004198
Chris Masonb9473432009-03-13 11:00:37 -04004199 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004200 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4201 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004202 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004203
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004204 leaf = path->nodes[0];
4205 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004206 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004207 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4208 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4209 btrfs_set_extent_flags(leaf, extent_item,
4210 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004211
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004212 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4213 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4214 if (parent > 0) {
4215 struct btrfs_shared_data_ref *ref;
4216 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4217 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4218 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4219 } else {
4220 struct btrfs_extent_data_ref *ref;
4221 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4222 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4223 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4224 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4225 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4226 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004227
4228 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004229 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004230
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004231 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4232 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05004233 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004234 printk(KERN_ERR "btrfs update block group failed for %llu "
4235 "%llu\n", (unsigned long long)ins->objectid,
4236 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004237 BUG();
4238 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004239 return ret;
4240}
4241
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004242static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4243 struct btrfs_root *root,
4244 u64 parent, u64 root_objectid,
4245 u64 flags, struct btrfs_disk_key *key,
4246 int level, struct btrfs_key *ins)
4247{
4248 int ret;
4249 struct btrfs_fs_info *fs_info = root->fs_info;
4250 struct btrfs_extent_item *extent_item;
4251 struct btrfs_tree_block_info *block_info;
4252 struct btrfs_extent_inline_ref *iref;
4253 struct btrfs_path *path;
4254 struct extent_buffer *leaf;
4255 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4256
4257 path = btrfs_alloc_path();
4258 BUG_ON(!path);
4259
4260 path->leave_spinning = 1;
4261 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4262 ins, size);
4263 BUG_ON(ret);
4264
4265 leaf = path->nodes[0];
4266 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4267 struct btrfs_extent_item);
4268 btrfs_set_extent_refs(leaf, extent_item, 1);
4269 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4270 btrfs_set_extent_flags(leaf, extent_item,
4271 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4272 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4273
4274 btrfs_set_tree_block_key(leaf, block_info, key);
4275 btrfs_set_tree_block_level(leaf, block_info, level);
4276
4277 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4278 if (parent > 0) {
4279 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4280 btrfs_set_extent_inline_ref_type(leaf, iref,
4281 BTRFS_SHARED_BLOCK_REF_KEY);
4282 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4283 } else {
4284 btrfs_set_extent_inline_ref_type(leaf, iref,
4285 BTRFS_TREE_BLOCK_REF_KEY);
4286 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4287 }
4288
4289 btrfs_mark_buffer_dirty(leaf);
4290 btrfs_free_path(path);
4291
4292 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4293 1, 0);
4294 if (ret) {
4295 printk(KERN_ERR "btrfs update block group failed for %llu "
4296 "%llu\n", (unsigned long long)ins->objectid,
4297 (unsigned long long)ins->offset);
4298 BUG();
4299 }
4300 return ret;
4301}
4302
4303int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4304 struct btrfs_root *root,
4305 u64 root_objectid, u64 owner,
4306 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004307{
4308 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04004309
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004310 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004311
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004312 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4313 0, root_objectid, owner, offset,
4314 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004315 return ret;
4316}
Chris Masone02119d2008-09-05 16:13:11 -04004317
4318/*
4319 * this is used by the tree logging recovery code. It records that
4320 * an extent has been allocated and makes sure to clear the free
4321 * space cache bits as well
4322 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004323int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4324 struct btrfs_root *root,
4325 u64 root_objectid, u64 owner, u64 offset,
4326 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004327{
4328 int ret;
4329 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04004330 struct btrfs_caching_control *caching_ctl;
4331 u64 start = ins->objectid;
4332 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04004333
Chris Masone02119d2008-09-05 16:13:11 -04004334 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04004335 cache_block_group(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004336 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004337
Yan Zheng11833d62009-09-11 16:11:19 -04004338 if (!caching_ctl) {
4339 BUG_ON(!block_group_cache_done(block_group));
4340 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4341 BUG_ON(ret);
4342 } else {
4343 mutex_lock(&caching_ctl->mutex);
4344
4345 if (start >= caching_ctl->progress) {
4346 ret = add_excluded_extent(root, start, num_bytes);
4347 BUG_ON(ret);
4348 } else if (start + num_bytes <= caching_ctl->progress) {
4349 ret = btrfs_remove_free_space(block_group,
4350 start, num_bytes);
4351 BUG_ON(ret);
4352 } else {
4353 num_bytes = caching_ctl->progress - start;
4354 ret = btrfs_remove_free_space(block_group,
4355 start, num_bytes);
4356 BUG_ON(ret);
4357
4358 start = caching_ctl->progress;
4359 num_bytes = ins->objectid + ins->offset -
4360 caching_ctl->progress;
4361 ret = add_excluded_extent(root, start, num_bytes);
4362 BUG_ON(ret);
4363 }
4364
4365 mutex_unlock(&caching_ctl->mutex);
4366 put_caching_control(caching_ctl);
4367 }
4368
4369 update_reserved_extents(block_group, ins->offset, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004370 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004371 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4372 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004373 return ret;
4374}
4375
Chris Masone6dcd2d2008-07-17 12:53:50 -04004376/*
4377 * finds a free extent and does all the dirty work required for allocation
4378 * returns the key for the extent through ins, and a tree buffer for
4379 * the first block of the extent through buf.
4380 *
4381 * returns 0 if everything worked, non-zero otherwise.
4382 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004383static int alloc_tree_block(struct btrfs_trans_handle *trans,
4384 struct btrfs_root *root,
4385 u64 num_bytes, u64 parent, u64 root_objectid,
4386 struct btrfs_disk_key *key, int level,
4387 u64 empty_size, u64 hint_byte, u64 search_end,
4388 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004389{
4390 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004391 u64 flags = 0;
4392
Yan Zheng11833d62009-09-11 16:11:19 -04004393 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4394 empty_size, hint_byte, search_end,
4395 ins, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004396 if (ret)
4397 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004398
4399 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4400 if (parent == 0)
4401 parent = ins->objectid;
4402 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4403 } else
4404 BUG_ON(parent > 0);
4405
Chris Masond00aff02008-09-11 15:54:42 -04004406 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004407 struct btrfs_delayed_extent_op *extent_op;
4408 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4409 BUG_ON(!extent_op);
4410 if (key)
4411 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4412 else
4413 memset(&extent_op->key, 0, sizeof(extent_op->key));
4414 extent_op->flags_to_set = flags;
4415 extent_op->update_key = 1;
4416 extent_op->update_flags = 1;
4417 extent_op->is_data = 0;
4418
4419 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4420 ins->offset, parent, root_objectid,
4421 level, BTRFS_ADD_DELAYED_EXTENT,
4422 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004423 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004424 }
Chris Mason925baed2008-06-25 16:01:30 -04004425 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004426}
Chris Mason65b51a02008-08-01 15:11:20 -04004427
4428struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4429 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004430 u64 bytenr, u32 blocksize,
4431 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004432{
4433 struct extent_buffer *buf;
4434
4435 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4436 if (!buf)
4437 return ERR_PTR(-ENOMEM);
4438 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004439 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004440 btrfs_tree_lock(buf);
4441 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004442
4443 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004444 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004445
Chris Masond0c803c2008-09-11 16:17:57 -04004446 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4447 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04004448 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004449 } else {
4450 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4451 buf->start + buf->len - 1, GFP_NOFS);
4452 }
Chris Mason65b51a02008-08-01 15:11:20 -04004453 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004454 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004455 return buf;
4456}
4457
Chris Masonfec577f2007-02-26 10:40:21 -05004458/*
4459 * helper function to allocate a block for a given tree
4460 * returns the tree buffer or NULL.
4461 */
Chris Mason5f39d392007-10-15 16:14:19 -04004462struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004463 struct btrfs_root *root, u32 blocksize,
4464 u64 parent, u64 root_objectid,
4465 struct btrfs_disk_key *key, int level,
4466 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004467{
Chris Masone2fa7222007-03-12 16:22:34 -04004468 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004469 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004470 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004471
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004472 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4473 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004474 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004475 BUG_ON(ret > 0);
4476 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004477 }
Chris Mason55c69072008-01-09 15:55:33 -05004478
Chris Mason4008c042009-02-12 14:09:45 -05004479 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4480 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004481 return buf;
4482}
Chris Masona28ec192007-03-06 20:08:01 -05004483
Yan Zheng2c47e6052009-06-27 21:07:35 -04004484struct walk_control {
4485 u64 refs[BTRFS_MAX_LEVEL];
4486 u64 flags[BTRFS_MAX_LEVEL];
4487 struct btrfs_key update_progress;
4488 int stage;
4489 int level;
4490 int shared_level;
4491 int update_ref;
4492 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004493 int reada_slot;
4494 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004495};
4496
4497#define DROP_REFERENCE 1
4498#define UPDATE_BACKREF 2
4499
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004500static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4501 struct btrfs_root *root,
4502 struct walk_control *wc,
4503 struct btrfs_path *path)
4504{
4505 u64 bytenr;
4506 u64 generation;
4507 u64 refs;
4508 u64 last = 0;
4509 u32 nritems;
4510 u32 blocksize;
4511 struct btrfs_key key;
4512 struct extent_buffer *eb;
4513 int ret;
4514 int slot;
4515 int nread = 0;
4516
4517 if (path->slots[wc->level] < wc->reada_slot) {
4518 wc->reada_count = wc->reada_count * 2 / 3;
4519 wc->reada_count = max(wc->reada_count, 2);
4520 } else {
4521 wc->reada_count = wc->reada_count * 3 / 2;
4522 wc->reada_count = min_t(int, wc->reada_count,
4523 BTRFS_NODEPTRS_PER_BLOCK(root));
4524 }
4525
4526 eb = path->nodes[wc->level];
4527 nritems = btrfs_header_nritems(eb);
4528 blocksize = btrfs_level_size(root, wc->level - 1);
4529
4530 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4531 if (nread >= wc->reada_count)
4532 break;
4533
4534 cond_resched();
4535 bytenr = btrfs_node_blockptr(eb, slot);
4536 generation = btrfs_node_ptr_generation(eb, slot);
4537
4538 if (slot == path->slots[wc->level])
4539 goto reada;
4540
4541 if (wc->stage == UPDATE_BACKREF &&
4542 generation <= root->root_key.offset)
4543 continue;
4544
4545 if (wc->stage == DROP_REFERENCE) {
4546 ret = btrfs_lookup_extent_info(trans, root,
4547 bytenr, blocksize,
4548 &refs, NULL);
4549 BUG_ON(ret);
4550 BUG_ON(refs == 0);
4551 if (refs == 1)
4552 goto reada;
4553
4554 if (!wc->update_ref ||
4555 generation <= root->root_key.offset)
4556 continue;
4557 btrfs_node_key_to_cpu(eb, &key, slot);
4558 ret = btrfs_comp_cpu_keys(&key,
4559 &wc->update_progress);
4560 if (ret < 0)
4561 continue;
4562 }
4563reada:
4564 ret = readahead_tree_block(root, bytenr, blocksize,
4565 generation);
4566 if (ret)
4567 break;
4568 last = bytenr + blocksize;
4569 nread++;
4570 }
4571 wc->reada_slot = slot;
4572}
4573
Chris Mason9aca1d52007-03-13 11:09:37 -04004574/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04004575 * hepler to process tree block while walking down the tree.
4576 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04004577 * when wc->stage == UPDATE_BACKREF, this function updates
4578 * back refs for pointers in the block.
4579 *
4580 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004581 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004582static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4583 struct btrfs_root *root,
4584 struct btrfs_path *path,
4585 struct walk_control *wc)
4586{
4587 int level = wc->level;
4588 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04004589 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4590 int ret;
4591
4592 if (wc->stage == UPDATE_BACKREF &&
4593 btrfs_header_owner(eb) != root->root_key.objectid)
4594 return 1;
4595
4596 /*
4597 * when reference count of tree block is 1, it won't increase
4598 * again. once full backref flag is set, we never clear it.
4599 */
4600 if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4601 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) {
4602 BUG_ON(!path->locks[level]);
4603 ret = btrfs_lookup_extent_info(trans, root,
4604 eb->start, eb->len,
4605 &wc->refs[level],
4606 &wc->flags[level]);
4607 BUG_ON(ret);
4608 BUG_ON(wc->refs[level] == 0);
4609 }
4610
Yan Zheng2c47e6052009-06-27 21:07:35 -04004611 if (wc->stage == DROP_REFERENCE) {
4612 if (wc->refs[level] > 1)
4613 return 1;
4614
4615 if (path->locks[level] && !wc->keep_locks) {
4616 btrfs_tree_unlock(eb);
4617 path->locks[level] = 0;
4618 }
4619 return 0;
4620 }
4621
4622 /* wc->stage == UPDATE_BACKREF */
4623 if (!(wc->flags[level] & flag)) {
4624 BUG_ON(!path->locks[level]);
4625 ret = btrfs_inc_ref(trans, root, eb, 1);
4626 BUG_ON(ret);
4627 ret = btrfs_dec_ref(trans, root, eb, 0);
4628 BUG_ON(ret);
4629 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
4630 eb->len, flag, 0);
4631 BUG_ON(ret);
4632 wc->flags[level] |= flag;
4633 }
4634
4635 /*
4636 * the block is shared by multiple trees, so it's not good to
4637 * keep the tree lock
4638 */
4639 if (path->locks[level] && level > 0) {
4640 btrfs_tree_unlock(eb);
4641 path->locks[level] = 0;
4642 }
4643 return 0;
4644}
4645
4646/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004647 * hepler to process tree block pointer.
4648 *
4649 * when wc->stage == DROP_REFERENCE, this function checks
4650 * reference count of the block pointed to. if the block
4651 * is shared and we need update back refs for the subtree
4652 * rooted at the block, this function changes wc->stage to
4653 * UPDATE_BACKREF. if the block is shared and there is no
4654 * need to update back, this function drops the reference
4655 * to the block.
4656 *
4657 * NOTE: return value 1 means we should stop walking down.
4658 */
4659static noinline int do_walk_down(struct btrfs_trans_handle *trans,
4660 struct btrfs_root *root,
4661 struct btrfs_path *path,
4662 struct walk_control *wc)
4663{
4664 u64 bytenr;
4665 u64 generation;
4666 u64 parent;
4667 u32 blocksize;
4668 struct btrfs_key key;
4669 struct extent_buffer *next;
4670 int level = wc->level;
4671 int reada = 0;
4672 int ret = 0;
4673
4674 generation = btrfs_node_ptr_generation(path->nodes[level],
4675 path->slots[level]);
4676 /*
4677 * if the lower level block was created before the snapshot
4678 * was created, we know there is no need to update back refs
4679 * for the subtree
4680 */
4681 if (wc->stage == UPDATE_BACKREF &&
4682 generation <= root->root_key.offset)
4683 return 1;
4684
4685 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
4686 blocksize = btrfs_level_size(root, level - 1);
4687
4688 next = btrfs_find_tree_block(root, bytenr, blocksize);
4689 if (!next) {
4690 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
4691 reada = 1;
4692 }
4693 btrfs_tree_lock(next);
4694 btrfs_set_lock_blocking(next);
4695
4696 if (wc->stage == DROP_REFERENCE) {
4697 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
4698 &wc->refs[level - 1],
4699 &wc->flags[level - 1]);
4700 BUG_ON(ret);
4701 BUG_ON(wc->refs[level - 1] == 0);
4702
4703 if (wc->refs[level - 1] > 1) {
4704 if (!wc->update_ref ||
4705 generation <= root->root_key.offset)
4706 goto skip;
4707
4708 btrfs_node_key_to_cpu(path->nodes[level], &key,
4709 path->slots[level]);
4710 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
4711 if (ret < 0)
4712 goto skip;
4713
4714 wc->stage = UPDATE_BACKREF;
4715 wc->shared_level = level - 1;
4716 }
4717 }
4718
4719 if (!btrfs_buffer_uptodate(next, generation)) {
4720 btrfs_tree_unlock(next);
4721 free_extent_buffer(next);
4722 next = NULL;
4723 }
4724
4725 if (!next) {
4726 if (reada && level == 1)
4727 reada_walk_down(trans, root, wc, path);
4728 next = read_tree_block(root, bytenr, blocksize, generation);
4729 btrfs_tree_lock(next);
4730 btrfs_set_lock_blocking(next);
4731 }
4732
4733 level--;
4734 BUG_ON(level != btrfs_header_level(next));
4735 path->nodes[level] = next;
4736 path->slots[level] = 0;
4737 path->locks[level] = 1;
4738 wc->level = level;
4739 if (wc->level == 1)
4740 wc->reada_slot = 0;
4741 return 0;
4742skip:
4743 wc->refs[level - 1] = 0;
4744 wc->flags[level - 1] = 0;
4745
4746 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4747 parent = path->nodes[level]->start;
4748 } else {
4749 BUG_ON(root->root_key.objectid !=
4750 btrfs_header_owner(path->nodes[level]));
4751 parent = 0;
4752 }
4753
4754 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
4755 root->root_key.objectid, level - 1, 0);
4756 BUG_ON(ret);
4757
4758 btrfs_tree_unlock(next);
4759 free_extent_buffer(next);
4760 return 1;
4761}
4762
4763/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04004764 * hepler to process tree block while walking up the tree.
4765 *
4766 * when wc->stage == DROP_REFERENCE, this function drops
4767 * reference count on the block.
4768 *
4769 * when wc->stage == UPDATE_BACKREF, this function changes
4770 * wc->stage back to DROP_REFERENCE if we changed wc->stage
4771 * to UPDATE_BACKREF previously while processing the block.
4772 *
4773 * NOTE: return value 1 means we should stop walking up.
4774 */
4775static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
4776 struct btrfs_root *root,
4777 struct btrfs_path *path,
4778 struct walk_control *wc)
4779{
4780 int ret = 0;
4781 int level = wc->level;
4782 struct extent_buffer *eb = path->nodes[level];
4783 u64 parent = 0;
4784
4785 if (wc->stage == UPDATE_BACKREF) {
4786 BUG_ON(wc->shared_level < level);
4787 if (level < wc->shared_level)
4788 goto out;
4789
Yan Zheng2c47e6052009-06-27 21:07:35 -04004790 ret = find_next_key(path, level + 1, &wc->update_progress);
4791 if (ret > 0)
4792 wc->update_ref = 0;
4793
4794 wc->stage = DROP_REFERENCE;
4795 wc->shared_level = -1;
4796 path->slots[level] = 0;
4797
4798 /*
4799 * check reference count again if the block isn't locked.
4800 * we should start walking down the tree again if reference
4801 * count is one.
4802 */
4803 if (!path->locks[level]) {
4804 BUG_ON(level == 0);
4805 btrfs_tree_lock(eb);
4806 btrfs_set_lock_blocking(eb);
4807 path->locks[level] = 1;
4808
4809 ret = btrfs_lookup_extent_info(trans, root,
4810 eb->start, eb->len,
4811 &wc->refs[level],
4812 &wc->flags[level]);
4813 BUG_ON(ret);
4814 BUG_ON(wc->refs[level] == 0);
4815 if (wc->refs[level] == 1) {
4816 btrfs_tree_unlock(eb);
4817 path->locks[level] = 0;
4818 return 1;
4819 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04004820 }
4821 }
4822
4823 /* wc->stage == DROP_REFERENCE */
4824 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
4825
4826 if (wc->refs[level] == 1) {
4827 if (level == 0) {
4828 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4829 ret = btrfs_dec_ref(trans, root, eb, 1);
4830 else
4831 ret = btrfs_dec_ref(trans, root, eb, 0);
4832 BUG_ON(ret);
4833 }
4834 /* make block locked assertion in clean_tree_block happy */
4835 if (!path->locks[level] &&
4836 btrfs_header_generation(eb) == trans->transid) {
4837 btrfs_tree_lock(eb);
4838 btrfs_set_lock_blocking(eb);
4839 path->locks[level] = 1;
4840 }
4841 clean_tree_block(trans, root, eb);
4842 }
4843
4844 if (eb == root->node) {
4845 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4846 parent = eb->start;
4847 else
4848 BUG_ON(root->root_key.objectid !=
4849 btrfs_header_owner(eb));
4850 } else {
4851 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4852 parent = path->nodes[level + 1]->start;
4853 else
4854 BUG_ON(root->root_key.objectid !=
4855 btrfs_header_owner(path->nodes[level + 1]));
4856 }
4857
4858 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
4859 root->root_key.objectid, level, 0);
4860 BUG_ON(ret);
4861out:
4862 wc->refs[level] = 0;
4863 wc->flags[level] = 0;
4864 return ret;
4865}
4866
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004867static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4868 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004869 struct btrfs_path *path,
4870 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04004871{
Yan Zheng2c47e6052009-06-27 21:07:35 -04004872 int level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004873 int ret;
4874
Yan Zheng2c47e6052009-06-27 21:07:35 -04004875 while (level >= 0) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004876 if (path->slots[level] >=
4877 btrfs_header_nritems(path->nodes[level]))
4878 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004879
Yan Zheng2c47e6052009-06-27 21:07:35 -04004880 ret = walk_down_proc(trans, root, path, wc);
4881 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04004882 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004883
Yan Zheng2c47e6052009-06-27 21:07:35 -04004884 if (level == 0)
4885 break;
4886
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004887 ret = do_walk_down(trans, root, path, wc);
4888 if (ret > 0) {
4889 path->slots[level]++;
4890 continue;
4891 }
4892 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004893 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04004894 return 0;
4895}
4896
Chris Masond3977122009-01-05 21:25:51 -05004897static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004898 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04004899 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004900 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05004901{
Yan Zheng2c47e6052009-06-27 21:07:35 -04004902 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05004903 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04004904
Yan Zheng2c47e6052009-06-27 21:07:35 -04004905 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
4906 while (level < max_level && path->nodes[level]) {
4907 wc->level = level;
4908 if (path->slots[level] + 1 <
4909 btrfs_header_nritems(path->nodes[level])) {
4910 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05004911 return 0;
4912 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04004913 ret = walk_up_proc(trans, root, path, wc);
4914 if (ret > 0)
4915 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05004916
Yan Zheng2c47e6052009-06-27 21:07:35 -04004917 if (path->locks[level]) {
4918 btrfs_tree_unlock(path->nodes[level]);
4919 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004920 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04004921 free_extent_buffer(path->nodes[level]);
4922 path->nodes[level] = NULL;
4923 level++;
Chris Mason20524f02007-03-10 06:35:47 -05004924 }
4925 }
4926 return 1;
4927}
4928
Chris Mason9aca1d52007-03-13 11:09:37 -04004929/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04004930 * drop a subvolume tree.
4931 *
4932 * this function traverses the tree freeing any blocks that only
4933 * referenced by the tree.
4934 *
4935 * when a shared tree block is found. this function decreases its
4936 * reference count by one. if update_ref is true, this function
4937 * also make sure backrefs for the shared block and all lower level
4938 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04004939 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004940int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05004941{
Chris Mason5caf2a02007-04-02 11:20:42 -04004942 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004943 struct btrfs_trans_handle *trans;
4944 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04004945 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004946 struct walk_control *wc;
4947 struct btrfs_key key;
4948 int err = 0;
4949 int ret;
4950 int level;
Chris Mason20524f02007-03-10 06:35:47 -05004951
Chris Mason5caf2a02007-04-02 11:20:42 -04004952 path = btrfs_alloc_path();
4953 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05004954
Yan Zheng2c47e6052009-06-27 21:07:35 -04004955 wc = kzalloc(sizeof(*wc), GFP_NOFS);
4956 BUG_ON(!wc);
4957
4958 trans = btrfs_start_transaction(tree_root, 1);
4959
Chris Mason9f3a7422007-08-07 15:52:19 -04004960 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04004961 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004962 path->nodes[level] = btrfs_lock_root_node(root);
4963 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04004964 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004965 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004966 memset(&wc->update_progress, 0,
4967 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04004968 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04004969 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004970 memcpy(&wc->update_progress, &key,
4971 sizeof(wc->update_progress));
4972
Chris Mason6702ed42007-08-07 16:15:09 -04004973 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004974 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04004975 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004976 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4977 path->lowest_level = 0;
4978 if (ret < 0) {
4979 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04004980 goto out;
4981 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004982 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004983
Chris Mason7d9eb122008-07-08 14:19:17 -04004984 /*
4985 * unlock our path, this is safe because only this
4986 * function is allowed to delete this snapshot
4987 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004988 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04004989
Yan Zheng2c47e6052009-06-27 21:07:35 -04004990 level = btrfs_header_level(root->node);
4991 while (1) {
4992 btrfs_tree_lock(path->nodes[level]);
4993 btrfs_set_lock_blocking(path->nodes[level]);
4994
4995 ret = btrfs_lookup_extent_info(trans, root,
4996 path->nodes[level]->start,
4997 path->nodes[level]->len,
4998 &wc->refs[level],
4999 &wc->flags[level]);
5000 BUG_ON(ret);
5001 BUG_ON(wc->refs[level] == 0);
5002
5003 if (level == root_item->drop_level)
5004 break;
5005
5006 btrfs_tree_unlock(path->nodes[level]);
5007 WARN_ON(wc->refs[level] != 1);
5008 level--;
5009 }
5010 }
5011
5012 wc->level = level;
5013 wc->shared_level = -1;
5014 wc->stage = DROP_REFERENCE;
5015 wc->update_ref = update_ref;
5016 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005017 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005018
5019 while (1) {
5020 ret = walk_down_tree(trans, root, path, wc);
5021 if (ret < 0) {
5022 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005023 break;
5024 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005025
5026 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5027 if (ret < 0) {
5028 err = ret;
5029 break;
5030 }
5031
5032 if (ret > 0) {
5033 BUG_ON(wc->stage != DROP_REFERENCE);
5034 break;
5035 }
5036
5037 if (wc->stage == DROP_REFERENCE) {
5038 level = wc->level;
5039 btrfs_node_key(path->nodes[level],
5040 &root_item->drop_progress,
5041 path->slots[level]);
5042 root_item->drop_level = level;
5043 }
5044
5045 BUG_ON(wc->level == 0);
5046 if (trans->transaction->in_commit ||
5047 trans->transaction->delayed_refs.flushing) {
5048 ret = btrfs_update_root(trans, tree_root,
5049 &root->root_key,
5050 root_item);
5051 BUG_ON(ret);
5052
5053 btrfs_end_transaction(trans, tree_root);
5054 trans = btrfs_start_transaction(tree_root, 1);
5055 } else {
5056 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005057 update = trans->delayed_ref_updates;
5058 trans->delayed_ref_updates = 0;
5059 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005060 btrfs_run_delayed_refs(trans, tree_root,
5061 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005062 }
Chris Mason20524f02007-03-10 06:35:47 -05005063 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005064 btrfs_release_path(root, path);
5065 BUG_ON(err);
5066
5067 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5068 BUG_ON(ret);
5069
Yan, Zheng76dda932009-09-21 16:00:26 -04005070 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5071 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5072 NULL, NULL);
5073 BUG_ON(ret < 0);
5074 if (ret > 0) {
5075 ret = btrfs_del_orphan_item(trans, tree_root,
5076 root->root_key.objectid);
5077 BUG_ON(ret);
5078 }
5079 }
5080
5081 if (root->in_radix) {
5082 btrfs_free_fs_root(tree_root->fs_info, root);
5083 } else {
5084 free_extent_buffer(root->node);
5085 free_extent_buffer(root->commit_root);
5086 kfree(root);
5087 }
Chris Mason9f3a7422007-08-07 15:52:19 -04005088out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005089 btrfs_end_transaction(trans, tree_root);
5090 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005091 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005092 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005093}
Chris Mason9078a3e2007-04-26 16:46:15 -04005094
Yan Zheng2c47e6052009-06-27 21:07:35 -04005095/*
5096 * drop subtree rooted at tree block 'node'.
5097 *
5098 * NOTE: this function will unlock and release tree block 'node'
5099 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005100int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5101 struct btrfs_root *root,
5102 struct extent_buffer *node,
5103 struct extent_buffer *parent)
5104{
5105 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005106 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005107 int level;
5108 int parent_level;
5109 int ret = 0;
5110 int wret;
5111
Yan Zheng2c47e6052009-06-27 21:07:35 -04005112 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5113
Yan Zhengf82d02d2008-10-29 14:49:05 -04005114 path = btrfs_alloc_path();
5115 BUG_ON(!path);
5116
Yan Zheng2c47e6052009-06-27 21:07:35 -04005117 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5118 BUG_ON(!wc);
5119
Chris Masonb9447ef2009-03-09 11:45:38 -04005120 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005121 parent_level = btrfs_header_level(parent);
5122 extent_buffer_get(parent);
5123 path->nodes[parent_level] = parent;
5124 path->slots[parent_level] = btrfs_header_nritems(parent);
5125
Chris Masonb9447ef2009-03-09 11:45:38 -04005126 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005127 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005128 path->nodes[level] = node;
5129 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005130 path->locks[level] = 1;
5131
5132 wc->refs[parent_level] = 1;
5133 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5134 wc->level = level;
5135 wc->shared_level = -1;
5136 wc->stage = DROP_REFERENCE;
5137 wc->update_ref = 0;
5138 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005139 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005140
5141 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005142 wret = walk_down_tree(trans, root, path, wc);
5143 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005144 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005145 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005146 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005147
Yan Zheng2c47e6052009-06-27 21:07:35 -04005148 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005149 if (wret < 0)
5150 ret = wret;
5151 if (wret != 0)
5152 break;
5153 }
5154
Yan Zheng2c47e6052009-06-27 21:07:35 -04005155 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005156 btrfs_free_path(path);
5157 return ret;
5158}
5159
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005160#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005161static unsigned long calc_ra(unsigned long start, unsigned long last,
5162 unsigned long nr)
5163{
5164 return min(last, start + nr - 1);
5165}
5166
Chris Masond3977122009-01-05 21:25:51 -05005167static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005168 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005169{
5170 u64 page_start;
5171 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005172 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005173 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005174 unsigned long i;
5175 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005176 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005177 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005178 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005179 unsigned int total_read = 0;
5180 unsigned int total_dirty = 0;
5181 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005182
5183 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005184
5185 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005186 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005187 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5188
Zheng Yan1a40e232008-09-26 10:09:34 -04005189 /* make sure the dirty trick played by the caller work */
5190 ret = invalidate_inode_pages2_range(inode->i_mapping,
5191 first_index, last_index);
5192 if (ret)
5193 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005194
Chris Mason4313b392008-01-03 09:08:48 -05005195 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005196
Zheng Yan1a40e232008-09-26 10:09:34 -04005197 for (i = first_index ; i <= last_index; i++) {
5198 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005199 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005200 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005201 }
5202 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005203again:
5204 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005205 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005206 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005207 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005208 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005209 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005210 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005211 if (!PageUptodate(page)) {
5212 btrfs_readpage(NULL, page);
5213 lock_page(page);
5214 if (!PageUptodate(page)) {
5215 unlock_page(page);
5216 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005217 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005218 goto out_unlock;
5219 }
5220 }
Chris Masonec44a352008-04-28 15:29:52 -04005221 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005222
Chris Masonedbd8d42007-12-21 16:27:24 -05005223 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5224 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005225 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005226
Chris Mason3eaa2882008-07-24 11:57:52 -04005227 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5228 if (ordered) {
5229 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5230 unlock_page(page);
5231 page_cache_release(page);
5232 btrfs_start_ordered_extent(inode, ordered, 1);
5233 btrfs_put_ordered_extent(ordered);
5234 goto again;
5235 }
5236 set_page_extent_mapped(page);
5237
Zheng Yan1a40e232008-09-26 10:09:34 -04005238 if (i == first_index)
5239 set_extent_bits(io_tree, page_start, page_end,
5240 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005241 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005242
Chris Masona061fc82008-05-07 11:43:44 -04005243 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005244 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005245
Chris Masond1310b22008-01-24 16:13:08 -05005246 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005247 unlock_page(page);
5248 page_cache_release(page);
5249 }
5250
5251out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005252 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005253 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005254 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005255 return ret;
5256}
5257
Chris Masond3977122009-01-05 21:25:51 -05005258static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005259 struct btrfs_key *extent_key,
5260 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005261{
Zheng Yan1a40e232008-09-26 10:09:34 -04005262 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5263 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5264 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005265 u64 start = extent_key->objectid - offset;
5266 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005267
5268 em = alloc_extent_map(GFP_NOFS);
5269 BUG_ON(!em || IS_ERR(em));
5270
Yan Zheng66435582008-10-30 14:19:50 -04005271 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005272 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005273 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005274 em->block_start = extent_key->objectid;
5275 em->bdev = root->fs_info->fs_devices->latest_bdev;
5276 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5277
5278 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005279 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005280 while (1) {
5281 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04005282 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005283 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005284 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005285 if (ret != -EEXIST) {
5286 free_extent_map(em);
5287 break;
5288 }
Yan Zheng66435582008-10-30 14:19:50 -04005289 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005290 }
Yan Zheng66435582008-10-30 14:19:50 -04005291 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005292
Yan Zheng66435582008-10-30 14:19:50 -04005293 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005294}
5295
5296struct btrfs_ref_path {
5297 u64 extent_start;
5298 u64 nodes[BTRFS_MAX_LEVEL];
5299 u64 root_objectid;
5300 u64 root_generation;
5301 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005302 u32 num_refs;
5303 int lowest_level;
5304 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005305 int shared_level;
5306
5307 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5308 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005309};
5310
5311struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005312 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005313 u64 disk_bytenr;
5314 u64 disk_num_bytes;
5315 u64 offset;
5316 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005317 u8 compression;
5318 u8 encryption;
5319 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005320};
5321
5322static int is_cowonly_root(u64 root_objectid)
5323{
5324 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5325 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5326 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5327 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005328 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5329 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005330 return 1;
5331 return 0;
5332}
5333
Chris Masond3977122009-01-05 21:25:51 -05005334static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005335 struct btrfs_root *extent_root,
5336 struct btrfs_ref_path *ref_path,
5337 int first_time)
5338{
5339 struct extent_buffer *leaf;
5340 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005341 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005342 struct btrfs_key key;
5343 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005344 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005345 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005346 int level;
5347 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005348
Zheng Yan1a40e232008-09-26 10:09:34 -04005349 path = btrfs_alloc_path();
5350 if (!path)
5351 return -ENOMEM;
5352
Zheng Yan1a40e232008-09-26 10:09:34 -04005353 if (first_time) {
5354 ref_path->lowest_level = -1;
5355 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005356 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005357 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005358 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005359walk_down:
5360 level = ref_path->current_level - 1;
5361 while (level >= -1) {
5362 u64 parent;
5363 if (level < ref_path->lowest_level)
5364 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005365
Chris Masond3977122009-01-05 21:25:51 -05005366 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005367 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005368 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005369 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005370 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005371
Zheng Yan1a40e232008-09-26 10:09:34 -04005372 parent = ref_path->nodes[level + 1];
5373 ref_path->nodes[level + 1] = 0;
5374 ref_path->current_level = level;
5375 BUG_ON(parent == 0);
5376
5377 key.objectid = bytenr;
5378 key.offset = parent + 1;
5379 key.type = BTRFS_EXTENT_REF_KEY;
5380
5381 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005382 if (ret < 0)
5383 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005384 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005385
Chris Masonedbd8d42007-12-21 16:27:24 -05005386 leaf = path->nodes[0];
5387 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005388 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005389 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005390 if (ret < 0)
5391 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005392 if (ret > 0)
5393 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005394 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005395 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005396
5397 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005398 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005399 found_key.type == BTRFS_EXTENT_REF_KEY) {
5400 if (level < ref_path->shared_level)
5401 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005402 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005403 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005404next:
5405 level--;
5406 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005407 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005408 }
5409 /* reached lowest level */
5410 ret = 1;
5411 goto out;
5412walk_up:
5413 level = ref_path->current_level;
5414 while (level < BTRFS_MAX_LEVEL - 1) {
5415 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005416
5417 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005418 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005419 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005420 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005421
Zheng Yan1a40e232008-09-26 10:09:34 -04005422 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005423
Zheng Yan1a40e232008-09-26 10:09:34 -04005424 key.objectid = bytenr;
5425 key.offset = 0;
5426 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005427
Zheng Yan1a40e232008-09-26 10:09:34 -04005428 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5429 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005430 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005431
5432 leaf = path->nodes[0];
5433 nritems = btrfs_header_nritems(leaf);
5434 if (path->slots[0] >= nritems) {
5435 ret = btrfs_next_leaf(extent_root, path);
5436 if (ret < 0)
5437 goto out;
5438 if (ret > 0) {
5439 /* the extent was freed by someone */
5440 if (ref_path->lowest_level == level)
5441 goto out;
5442 btrfs_release_path(extent_root, path);
5443 goto walk_down;
5444 }
5445 leaf = path->nodes[0];
5446 }
5447
5448 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5449 if (found_key.objectid != bytenr ||
5450 found_key.type != BTRFS_EXTENT_REF_KEY) {
5451 /* the extent was freed by someone */
5452 if (ref_path->lowest_level == level) {
5453 ret = 1;
5454 goto out;
5455 }
5456 btrfs_release_path(extent_root, path);
5457 goto walk_down;
5458 }
5459found:
5460 ref = btrfs_item_ptr(leaf, path->slots[0],
5461 struct btrfs_extent_ref);
5462 ref_objectid = btrfs_ref_objectid(leaf, ref);
5463 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5464 if (first_time) {
5465 level = (int)ref_objectid;
5466 BUG_ON(level >= BTRFS_MAX_LEVEL);
5467 ref_path->lowest_level = level;
5468 ref_path->current_level = level;
5469 ref_path->nodes[level] = bytenr;
5470 } else {
5471 WARN_ON(ref_objectid != level);
5472 }
5473 } else {
5474 WARN_ON(level != -1);
5475 }
5476 first_time = 0;
5477
5478 if (ref_path->lowest_level == level) {
5479 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005480 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5481 }
5482
5483 /*
5484 * the block is tree root or the block isn't in reference
5485 * counted tree.
5486 */
5487 if (found_key.objectid == found_key.offset ||
5488 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5489 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5490 ref_path->root_generation =
5491 btrfs_ref_generation(leaf, ref);
5492 if (level < 0) {
5493 /* special reference from the tree log */
5494 ref_path->nodes[0] = found_key.offset;
5495 ref_path->current_level = 0;
5496 }
5497 ret = 0;
5498 goto out;
5499 }
5500
5501 level++;
5502 BUG_ON(ref_path->nodes[level] != 0);
5503 ref_path->nodes[level] = found_key.offset;
5504 ref_path->current_level = level;
5505
5506 /*
5507 * the reference was created in the running transaction,
5508 * no need to continue walking up.
5509 */
5510 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5511 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5512 ref_path->root_generation =
5513 btrfs_ref_generation(leaf, ref);
5514 ret = 0;
5515 goto out;
5516 }
5517
5518 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005519 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005520 }
5521 /* reached max tree level, but no tree root found. */
5522 BUG();
5523out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005524 btrfs_free_path(path);
5525 return ret;
5526}
5527
5528static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5529 struct btrfs_root *extent_root,
5530 struct btrfs_ref_path *ref_path,
5531 u64 extent_start)
5532{
5533 memset(ref_path, 0, sizeof(*ref_path));
5534 ref_path->extent_start = extent_start;
5535
5536 return __next_ref_path(trans, extent_root, ref_path, 1);
5537}
5538
5539static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5540 struct btrfs_root *extent_root,
5541 struct btrfs_ref_path *ref_path)
5542{
5543 return __next_ref_path(trans, extent_root, ref_path, 0);
5544}
5545
Chris Masond3977122009-01-05 21:25:51 -05005546static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005547 struct btrfs_key *extent_key,
5548 u64 offset, int no_fragment,
5549 struct disk_extent **extents,
5550 int *nr_extents)
5551{
5552 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5553 struct btrfs_path *path;
5554 struct btrfs_file_extent_item *fi;
5555 struct extent_buffer *leaf;
5556 struct disk_extent *exts = *extents;
5557 struct btrfs_key found_key;
5558 u64 cur_pos;
5559 u64 last_byte;
5560 u32 nritems;
5561 int nr = 0;
5562 int max = *nr_extents;
5563 int ret;
5564
5565 WARN_ON(!no_fragment && *extents);
5566 if (!exts) {
5567 max = 1;
5568 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
5569 if (!exts)
5570 return -ENOMEM;
5571 }
5572
5573 path = btrfs_alloc_path();
5574 BUG_ON(!path);
5575
5576 cur_pos = extent_key->objectid - offset;
5577 last_byte = extent_key->objectid + extent_key->offset;
5578 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
5579 cur_pos, 0);
5580 if (ret < 0)
5581 goto out;
5582 if (ret > 0) {
5583 ret = -ENOENT;
5584 goto out;
5585 }
5586
5587 while (1) {
5588 leaf = path->nodes[0];
5589 nritems = btrfs_header_nritems(leaf);
5590 if (path->slots[0] >= nritems) {
5591 ret = btrfs_next_leaf(root, path);
5592 if (ret < 0)
5593 goto out;
5594 if (ret > 0)
5595 break;
5596 leaf = path->nodes[0];
5597 }
5598
5599 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5600 if (found_key.offset != cur_pos ||
5601 found_key.type != BTRFS_EXTENT_DATA_KEY ||
5602 found_key.objectid != reloc_inode->i_ino)
5603 break;
5604
5605 fi = btrfs_item_ptr(leaf, path->slots[0],
5606 struct btrfs_file_extent_item);
5607 if (btrfs_file_extent_type(leaf, fi) !=
5608 BTRFS_FILE_EXTENT_REG ||
5609 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5610 break;
5611
5612 if (nr == max) {
5613 struct disk_extent *old = exts;
5614 max *= 2;
5615 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
5616 memcpy(exts, old, sizeof(*exts) * nr);
5617 if (old != *extents)
5618 kfree(old);
5619 }
5620
5621 exts[nr].disk_bytenr =
5622 btrfs_file_extent_disk_bytenr(leaf, fi);
5623 exts[nr].disk_num_bytes =
5624 btrfs_file_extent_disk_num_bytes(leaf, fi);
5625 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
5626 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04005627 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
5628 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
5629 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
5630 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
5631 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04005632 BUG_ON(exts[nr].offset > 0);
5633 BUG_ON(exts[nr].compression || exts[nr].encryption);
5634 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005635
5636 cur_pos += exts[nr].num_bytes;
5637 nr++;
5638
5639 if (cur_pos + offset >= last_byte)
5640 break;
5641
5642 if (no_fragment) {
5643 ret = 1;
5644 goto out;
5645 }
5646 path->slots[0]++;
5647 }
5648
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005649 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04005650 if (cur_pos + offset < last_byte) {
5651 ret = -ENOENT;
5652 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05005653 }
5654 ret = 0;
5655out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005656 btrfs_free_path(path);
5657 if (ret) {
5658 if (exts != *extents)
5659 kfree(exts);
5660 } else {
5661 *extents = exts;
5662 *nr_extents = nr;
5663 }
5664 return ret;
5665}
5666
Chris Masond3977122009-01-05 21:25:51 -05005667static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005668 struct btrfs_root *root,
5669 struct btrfs_path *path,
5670 struct btrfs_key *extent_key,
5671 struct btrfs_key *leaf_key,
5672 struct btrfs_ref_path *ref_path,
5673 struct disk_extent *new_extents,
5674 int nr_extents)
5675{
5676 struct extent_buffer *leaf;
5677 struct btrfs_file_extent_item *fi;
5678 struct inode *inode = NULL;
5679 struct btrfs_key key;
5680 u64 lock_start = 0;
5681 u64 lock_end = 0;
5682 u64 num_bytes;
5683 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05005684 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005685 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005686 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005687 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04005688 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04005689 int ret;
5690
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005691 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04005692 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005693 if (key.objectid < ref_path->owner_objectid ||
5694 (key.objectid == ref_path->owner_objectid &&
5695 key.type < BTRFS_EXTENT_DATA_KEY)) {
5696 key.objectid = ref_path->owner_objectid;
5697 key.type = BTRFS_EXTENT_DATA_KEY;
5698 key.offset = 0;
5699 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005700 }
5701
5702 while (1) {
5703 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5704 if (ret < 0)
5705 goto out;
5706
5707 leaf = path->nodes[0];
5708 nritems = btrfs_header_nritems(leaf);
5709next:
5710 if (extent_locked && ret > 0) {
5711 /*
5712 * the file extent item was modified by someone
5713 * before the extent got locked.
5714 */
Zheng Yan1a40e232008-09-26 10:09:34 -04005715 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5716 lock_end, GFP_NOFS);
5717 extent_locked = 0;
5718 }
5719
5720 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005721 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04005722 break;
5723
5724 BUG_ON(extent_locked);
5725 ret = btrfs_next_leaf(root, path);
5726 if (ret < 0)
5727 goto out;
5728 if (ret > 0)
5729 break;
5730 leaf = path->nodes[0];
5731 nritems = btrfs_header_nritems(leaf);
5732 }
5733
5734 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5735
5736 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
5737 if ((key.objectid > ref_path->owner_objectid) ||
5738 (key.objectid == ref_path->owner_objectid &&
5739 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05005740 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005741 break;
5742 }
5743
5744 if (inode && key.objectid != inode->i_ino) {
5745 BUG_ON(extent_locked);
5746 btrfs_release_path(root, path);
5747 mutex_unlock(&inode->i_mutex);
5748 iput(inode);
5749 inode = NULL;
5750 continue;
5751 }
5752
5753 if (key.type != BTRFS_EXTENT_DATA_KEY) {
5754 path->slots[0]++;
5755 ret = 1;
5756 goto next;
5757 }
5758 fi = btrfs_item_ptr(leaf, path->slots[0],
5759 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04005760 extent_type = btrfs_file_extent_type(leaf, fi);
5761 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
5762 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04005763 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
5764 extent_key->objectid)) {
5765 path->slots[0]++;
5766 ret = 1;
5767 goto next;
5768 }
5769
5770 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5771 ext_offset = btrfs_file_extent_offset(leaf, fi);
5772
Yan Zheng86288a12009-01-21 10:49:16 -05005773 if (search_end == (u64)-1) {
5774 search_end = key.offset - ext_offset +
5775 btrfs_file_extent_ram_bytes(leaf, fi);
5776 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005777
5778 if (!extent_locked) {
5779 lock_start = key.offset;
5780 lock_end = lock_start + num_bytes - 1;
5781 } else {
Yan Zheng66435582008-10-30 14:19:50 -04005782 if (lock_start > key.offset ||
5783 lock_end + 1 < key.offset + num_bytes) {
5784 unlock_extent(&BTRFS_I(inode)->io_tree,
5785 lock_start, lock_end, GFP_NOFS);
5786 extent_locked = 0;
5787 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005788 }
5789
5790 if (!inode) {
5791 btrfs_release_path(root, path);
5792
5793 inode = btrfs_iget_locked(root->fs_info->sb,
5794 key.objectid, root);
5795 if (inode->i_state & I_NEW) {
5796 BTRFS_I(inode)->root = root;
5797 BTRFS_I(inode)->location.objectid =
5798 key.objectid;
5799 BTRFS_I(inode)->location.type =
5800 BTRFS_INODE_ITEM_KEY;
5801 BTRFS_I(inode)->location.offset = 0;
5802 btrfs_read_locked_inode(inode);
5803 unlock_new_inode(inode);
5804 }
5805 /*
5806 * some code call btrfs_commit_transaction while
5807 * holding the i_mutex, so we can't use mutex_lock
5808 * here.
5809 */
5810 if (is_bad_inode(inode) ||
5811 !mutex_trylock(&inode->i_mutex)) {
5812 iput(inode);
5813 inode = NULL;
5814 key.offset = (u64)-1;
5815 goto skip;
5816 }
5817 }
5818
5819 if (!extent_locked) {
5820 struct btrfs_ordered_extent *ordered;
5821
5822 btrfs_release_path(root, path);
5823
5824 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5825 lock_end, GFP_NOFS);
5826 ordered = btrfs_lookup_first_ordered_extent(inode,
5827 lock_end);
5828 if (ordered &&
5829 ordered->file_offset <= lock_end &&
5830 ordered->file_offset + ordered->len > lock_start) {
5831 unlock_extent(&BTRFS_I(inode)->io_tree,
5832 lock_start, lock_end, GFP_NOFS);
5833 btrfs_start_ordered_extent(inode, ordered, 1);
5834 btrfs_put_ordered_extent(ordered);
5835 key.offset += num_bytes;
5836 goto skip;
5837 }
5838 if (ordered)
5839 btrfs_put_ordered_extent(ordered);
5840
Zheng Yan1a40e232008-09-26 10:09:34 -04005841 extent_locked = 1;
5842 continue;
5843 }
5844
5845 if (nr_extents == 1) {
5846 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04005847 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5848 new_extents[0].disk_bytenr);
5849 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5850 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005851 btrfs_mark_buffer_dirty(leaf);
5852
5853 btrfs_drop_extent_cache(inode, key.offset,
5854 key.offset + num_bytes - 1, 0);
5855
5856 ret = btrfs_inc_extent_ref(trans, root,
5857 new_extents[0].disk_bytenr,
5858 new_extents[0].disk_num_bytes,
5859 leaf->start,
5860 root->root_key.objectid,
5861 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005862 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005863 BUG_ON(ret);
5864
5865 ret = btrfs_free_extent(trans, root,
5866 extent_key->objectid,
5867 extent_key->offset,
5868 leaf->start,
5869 btrfs_header_owner(leaf),
5870 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005871 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005872 BUG_ON(ret);
5873
5874 btrfs_release_path(root, path);
5875 key.offset += num_bytes;
5876 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04005877 BUG_ON(1);
5878#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04005879 u64 alloc_hint;
5880 u64 extent_len;
5881 int i;
5882 /*
5883 * drop old extent pointer at first, then insert the
5884 * new pointers one bye one
5885 */
5886 btrfs_release_path(root, path);
5887 ret = btrfs_drop_extents(trans, root, inode, key.offset,
5888 key.offset + num_bytes,
5889 key.offset, &alloc_hint);
5890 BUG_ON(ret);
5891
5892 for (i = 0; i < nr_extents; i++) {
5893 if (ext_offset >= new_extents[i].num_bytes) {
5894 ext_offset -= new_extents[i].num_bytes;
5895 continue;
5896 }
5897 extent_len = min(new_extents[i].num_bytes -
5898 ext_offset, num_bytes);
5899
5900 ret = btrfs_insert_empty_item(trans, root,
5901 path, &key,
5902 sizeof(*fi));
5903 BUG_ON(ret);
5904
5905 leaf = path->nodes[0];
5906 fi = btrfs_item_ptr(leaf, path->slots[0],
5907 struct btrfs_file_extent_item);
5908 btrfs_set_file_extent_generation(leaf, fi,
5909 trans->transid);
5910 btrfs_set_file_extent_type(leaf, fi,
5911 BTRFS_FILE_EXTENT_REG);
5912 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5913 new_extents[i].disk_bytenr);
5914 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5915 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04005916 btrfs_set_file_extent_ram_bytes(leaf, fi,
5917 new_extents[i].ram_bytes);
5918
5919 btrfs_set_file_extent_compression(leaf, fi,
5920 new_extents[i].compression);
5921 btrfs_set_file_extent_encryption(leaf, fi,
5922 new_extents[i].encryption);
5923 btrfs_set_file_extent_other_encoding(leaf, fi,
5924 new_extents[i].other_encoding);
5925
Zheng Yan1a40e232008-09-26 10:09:34 -04005926 btrfs_set_file_extent_num_bytes(leaf, fi,
5927 extent_len);
5928 ext_offset += new_extents[i].offset;
5929 btrfs_set_file_extent_offset(leaf, fi,
5930 ext_offset);
5931 btrfs_mark_buffer_dirty(leaf);
5932
5933 btrfs_drop_extent_cache(inode, key.offset,
5934 key.offset + extent_len - 1, 0);
5935
5936 ret = btrfs_inc_extent_ref(trans, root,
5937 new_extents[i].disk_bytenr,
5938 new_extents[i].disk_num_bytes,
5939 leaf->start,
5940 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005941 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005942 BUG_ON(ret);
5943 btrfs_release_path(root, path);
5944
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005945 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04005946
5947 ext_offset = 0;
5948 num_bytes -= extent_len;
5949 key.offset += extent_len;
5950
5951 if (num_bytes == 0)
5952 break;
5953 }
5954 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005955#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04005956 }
5957
5958 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005959 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5960 lock_end, GFP_NOFS);
5961 extent_locked = 0;
5962 }
5963skip:
5964 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05005965 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005966 break;
5967
5968 cond_resched();
5969 }
5970 ret = 0;
5971out:
5972 btrfs_release_path(root, path);
5973 if (inode) {
5974 mutex_unlock(&inode->i_mutex);
5975 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005976 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5977 lock_end, GFP_NOFS);
5978 }
5979 iput(inode);
5980 }
5981 return ret;
5982}
5983
Zheng Yan1a40e232008-09-26 10:09:34 -04005984int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
5985 struct btrfs_root *root,
5986 struct extent_buffer *buf, u64 orig_start)
5987{
5988 int level;
5989 int ret;
5990
5991 BUG_ON(btrfs_header_generation(buf) != trans->transid);
5992 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5993
5994 level = btrfs_header_level(buf);
5995 if (level == 0) {
5996 struct btrfs_leaf_ref *ref;
5997 struct btrfs_leaf_ref *orig_ref;
5998
5999 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6000 if (!orig_ref)
6001 return -ENOENT;
6002
6003 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6004 if (!ref) {
6005 btrfs_free_leaf_ref(root, orig_ref);
6006 return -ENOMEM;
6007 }
6008
6009 ref->nritems = orig_ref->nritems;
6010 memcpy(ref->extents, orig_ref->extents,
6011 sizeof(ref->extents[0]) * ref->nritems);
6012
6013 btrfs_free_leaf_ref(root, orig_ref);
6014
6015 ref->root_gen = trans->transid;
6016 ref->bytenr = buf->start;
6017 ref->owner = btrfs_header_owner(buf);
6018 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006019
Zheng Yan1a40e232008-09-26 10:09:34 -04006020 ret = btrfs_add_leaf_ref(root, ref, 0);
6021 WARN_ON(ret);
6022 btrfs_free_leaf_ref(root, ref);
6023 }
6024 return 0;
6025}
6026
Chris Masond3977122009-01-05 21:25:51 -05006027static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006028 struct extent_buffer *leaf,
6029 struct btrfs_block_group_cache *group,
6030 struct btrfs_root *target_root)
6031{
6032 struct btrfs_key key;
6033 struct inode *inode = NULL;
6034 struct btrfs_file_extent_item *fi;
6035 u64 num_bytes;
6036 u64 skip_objectid = 0;
6037 u32 nritems;
6038 u32 i;
6039
6040 nritems = btrfs_header_nritems(leaf);
6041 for (i = 0; i < nritems; i++) {
6042 btrfs_item_key_to_cpu(leaf, &key, i);
6043 if (key.objectid == skip_objectid ||
6044 key.type != BTRFS_EXTENT_DATA_KEY)
6045 continue;
6046 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6047 if (btrfs_file_extent_type(leaf, fi) ==
6048 BTRFS_FILE_EXTENT_INLINE)
6049 continue;
6050 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6051 continue;
6052 if (!inode || inode->i_ino != key.objectid) {
6053 iput(inode);
6054 inode = btrfs_ilookup(target_root->fs_info->sb,
6055 key.objectid, target_root, 1);
6056 }
6057 if (!inode) {
6058 skip_objectid = key.objectid;
6059 continue;
6060 }
6061 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6062
6063 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6064 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006065 btrfs_drop_extent_cache(inode, key.offset,
6066 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006067 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6068 key.offset + num_bytes - 1, GFP_NOFS);
6069 cond_resched();
6070 }
6071 iput(inode);
6072 return 0;
6073}
6074
Chris Masond3977122009-01-05 21:25:51 -05006075static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006076 struct btrfs_root *root,
6077 struct extent_buffer *leaf,
6078 struct btrfs_block_group_cache *group,
6079 struct inode *reloc_inode)
6080{
6081 struct btrfs_key key;
6082 struct btrfs_key extent_key;
6083 struct btrfs_file_extent_item *fi;
6084 struct btrfs_leaf_ref *ref;
6085 struct disk_extent *new_extent;
6086 u64 bytenr;
6087 u64 num_bytes;
6088 u32 nritems;
6089 u32 i;
6090 int ext_index;
6091 int nr_extent;
6092 int ret;
6093
6094 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6095 BUG_ON(!new_extent);
6096
6097 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6098 BUG_ON(!ref);
6099
6100 ext_index = -1;
6101 nritems = btrfs_header_nritems(leaf);
6102 for (i = 0; i < nritems; i++) {
6103 btrfs_item_key_to_cpu(leaf, &key, i);
6104 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6105 continue;
6106 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6107 if (btrfs_file_extent_type(leaf, fi) ==
6108 BTRFS_FILE_EXTENT_INLINE)
6109 continue;
6110 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6111 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6112 if (bytenr == 0)
6113 continue;
6114
6115 ext_index++;
6116 if (bytenr >= group->key.objectid + group->key.offset ||
6117 bytenr + num_bytes <= group->key.objectid)
6118 continue;
6119
6120 extent_key.objectid = bytenr;
6121 extent_key.offset = num_bytes;
6122 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6123 nr_extent = 1;
6124 ret = get_new_locations(reloc_inode, &extent_key,
6125 group->key.objectid, 1,
6126 &new_extent, &nr_extent);
6127 if (ret > 0)
6128 continue;
6129 BUG_ON(ret < 0);
6130
6131 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6132 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6133 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6134 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6135
Zheng Yan1a40e232008-09-26 10:09:34 -04006136 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6137 new_extent->disk_bytenr);
6138 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6139 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006140 btrfs_mark_buffer_dirty(leaf);
6141
6142 ret = btrfs_inc_extent_ref(trans, root,
6143 new_extent->disk_bytenr,
6144 new_extent->disk_num_bytes,
6145 leaf->start,
6146 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006147 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006148 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006149
Zheng Yan1a40e232008-09-26 10:09:34 -04006150 ret = btrfs_free_extent(trans, root,
6151 bytenr, num_bytes, leaf->start,
6152 btrfs_header_owner(leaf),
6153 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006154 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006155 BUG_ON(ret);
6156 cond_resched();
6157 }
6158 kfree(new_extent);
6159 BUG_ON(ext_index + 1 != ref->nritems);
6160 btrfs_free_leaf_ref(root, ref);
6161 return 0;
6162}
6163
Yan Zhengf82d02d2008-10-29 14:49:05 -04006164int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6165 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006166{
6167 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006168 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006169
6170 if (root->reloc_root) {
6171 reloc_root = root->reloc_root;
6172 root->reloc_root = NULL;
6173 list_add(&reloc_root->dead_list,
6174 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006175
6176 btrfs_set_root_bytenr(&reloc_root->root_item,
6177 reloc_root->node->start);
6178 btrfs_set_root_level(&root->root_item,
6179 btrfs_header_level(reloc_root->node));
6180 memset(&reloc_root->root_item.drop_progress, 0,
6181 sizeof(struct btrfs_disk_key));
6182 reloc_root->root_item.drop_level = 0;
6183
6184 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6185 &reloc_root->root_key,
6186 &reloc_root->root_item);
6187 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006188 }
6189 return 0;
6190}
6191
6192int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6193{
6194 struct btrfs_trans_handle *trans;
6195 struct btrfs_root *reloc_root;
6196 struct btrfs_root *prev_root = NULL;
6197 struct list_head dead_roots;
6198 int ret;
6199 unsigned long nr;
6200
6201 INIT_LIST_HEAD(&dead_roots);
6202 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6203
6204 while (!list_empty(&dead_roots)) {
6205 reloc_root = list_entry(dead_roots.prev,
6206 struct btrfs_root, dead_list);
6207 list_del_init(&reloc_root->dead_list);
6208
6209 BUG_ON(reloc_root->commit_root != NULL);
6210 while (1) {
6211 trans = btrfs_join_transaction(root, 1);
6212 BUG_ON(!trans);
6213
6214 mutex_lock(&root->fs_info->drop_mutex);
6215 ret = btrfs_drop_snapshot(trans, reloc_root);
6216 if (ret != -EAGAIN)
6217 break;
6218 mutex_unlock(&root->fs_info->drop_mutex);
6219
6220 nr = trans->blocks_used;
6221 ret = btrfs_end_transaction(trans, root);
6222 BUG_ON(ret);
6223 btrfs_btree_balance_dirty(root, nr);
6224 }
6225
6226 free_extent_buffer(reloc_root->node);
6227
6228 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6229 &reloc_root->root_key);
6230 BUG_ON(ret);
6231 mutex_unlock(&root->fs_info->drop_mutex);
6232
6233 nr = trans->blocks_used;
6234 ret = btrfs_end_transaction(trans, root);
6235 BUG_ON(ret);
6236 btrfs_btree_balance_dirty(root, nr);
6237
6238 kfree(prev_root);
6239 prev_root = reloc_root;
6240 }
6241 if (prev_root) {
6242 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6243 kfree(prev_root);
6244 }
6245 return 0;
6246}
6247
6248int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6249{
6250 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6251 return 0;
6252}
6253
6254int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6255{
6256 struct btrfs_root *reloc_root;
6257 struct btrfs_trans_handle *trans;
6258 struct btrfs_key location;
6259 int found;
6260 int ret;
6261
6262 mutex_lock(&root->fs_info->tree_reloc_mutex);
6263 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6264 BUG_ON(ret);
6265 found = !list_empty(&root->fs_info->dead_reloc_roots);
6266 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6267
6268 if (found) {
6269 trans = btrfs_start_transaction(root, 1);
6270 BUG_ON(!trans);
6271 ret = btrfs_commit_transaction(trans, root);
6272 BUG_ON(ret);
6273 }
6274
6275 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6276 location.offset = (u64)-1;
6277 location.type = BTRFS_ROOT_ITEM_KEY;
6278
6279 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6280 BUG_ON(!reloc_root);
6281 btrfs_orphan_cleanup(reloc_root);
6282 return 0;
6283}
6284
Chris Masond3977122009-01-05 21:25:51 -05006285static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006286 struct btrfs_root *root)
6287{
6288 struct btrfs_root *reloc_root;
6289 struct extent_buffer *eb;
6290 struct btrfs_root_item *root_item;
6291 struct btrfs_key root_key;
6292 int ret;
6293
6294 BUG_ON(!root->ref_cows);
6295 if (root->reloc_root)
6296 return 0;
6297
6298 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6299 BUG_ON(!root_item);
6300
6301 ret = btrfs_copy_root(trans, root, root->commit_root,
6302 &eb, BTRFS_TREE_RELOC_OBJECTID);
6303 BUG_ON(ret);
6304
6305 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6306 root_key.offset = root->root_key.objectid;
6307 root_key.type = BTRFS_ROOT_ITEM_KEY;
6308
6309 memcpy(root_item, &root->root_item, sizeof(root_item));
6310 btrfs_set_root_refs(root_item, 0);
6311 btrfs_set_root_bytenr(root_item, eb->start);
6312 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006313 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006314
6315 btrfs_tree_unlock(eb);
6316 free_extent_buffer(eb);
6317
6318 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6319 &root_key, root_item);
6320 BUG_ON(ret);
6321 kfree(root_item);
6322
6323 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6324 &root_key);
6325 BUG_ON(!reloc_root);
6326 reloc_root->last_trans = trans->transid;
6327 reloc_root->commit_root = NULL;
6328 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6329
6330 root->reloc_root = reloc_root;
6331 return 0;
6332}
6333
6334/*
6335 * Core function of space balance.
6336 *
6337 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006338 * counted roots. There is one reloc tree for each subvol, and all
6339 * reloc trees share same root key objectid. Reloc trees are snapshots
6340 * of the latest committed roots of subvols (root->commit_root).
6341 *
6342 * To relocate a tree block referenced by a subvol, there are two steps.
6343 * COW the block through subvol's reloc tree, then update block pointer
6344 * in the subvol to point to the new block. Since all reloc trees share
6345 * same root key objectid, doing special handing for tree blocks owned
6346 * by them is easy. Once a tree block has been COWed in one reloc tree,
6347 * we can use the resulting new block directly when the same block is
6348 * required to COW again through other reloc trees. By this way, relocated
6349 * tree blocks are shared between reloc trees, so they are also shared
6350 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006351 */
Chris Masond3977122009-01-05 21:25:51 -05006352static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006353 struct btrfs_root *root,
6354 struct btrfs_path *path,
6355 struct btrfs_key *first_key,
6356 struct btrfs_ref_path *ref_path,
6357 struct btrfs_block_group_cache *group,
6358 struct inode *reloc_inode)
6359{
6360 struct btrfs_root *reloc_root;
6361 struct extent_buffer *eb = NULL;
6362 struct btrfs_key *keys;
6363 u64 *nodes;
6364 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006365 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006366 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006367 int ret;
6368
6369 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6370 lowest_level = ref_path->owner_objectid;
6371
Yan Zhengf82d02d2008-10-29 14:49:05 -04006372 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006373 path->lowest_level = lowest_level;
6374 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6375 BUG_ON(ret < 0);
6376 path->lowest_level = 0;
6377 btrfs_release_path(root, path);
6378 return 0;
6379 }
6380
Zheng Yan1a40e232008-09-26 10:09:34 -04006381 mutex_lock(&root->fs_info->tree_reloc_mutex);
6382 ret = init_reloc_tree(trans, root);
6383 BUG_ON(ret);
6384 reloc_root = root->reloc_root;
6385
Yan Zhengf82d02d2008-10-29 14:49:05 -04006386 shared_level = ref_path->shared_level;
6387 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006388
Yan Zhengf82d02d2008-10-29 14:49:05 -04006389 keys = ref_path->node_keys;
6390 nodes = ref_path->new_nodes;
6391 memset(&keys[shared_level + 1], 0,
6392 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6393 memset(&nodes[shared_level + 1], 0,
6394 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006395
Yan Zhengf82d02d2008-10-29 14:49:05 -04006396 if (nodes[lowest_level] == 0) {
6397 path->lowest_level = lowest_level;
6398 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6399 0, 1);
6400 BUG_ON(ret);
6401 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6402 eb = path->nodes[level];
6403 if (!eb || eb == reloc_root->node)
6404 break;
6405 nodes[level] = eb->start;
6406 if (level == 0)
6407 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6408 else
6409 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6410 }
Yan Zheng2b820322008-11-17 21:11:30 -05006411 if (nodes[0] &&
6412 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006413 eb = path->nodes[0];
6414 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6415 group, reloc_inode);
6416 BUG_ON(ret);
6417 }
6418 btrfs_release_path(reloc_root, path);
6419 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006420 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006421 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006422 BUG_ON(ret);
6423 }
6424
Zheng Yan1a40e232008-09-26 10:09:34 -04006425 /*
6426 * replace tree blocks in the fs tree with tree blocks in
6427 * the reloc tree.
6428 */
6429 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6430 BUG_ON(ret < 0);
6431
6432 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006433 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6434 0, 0);
6435 BUG_ON(ret);
6436 extent_buffer_get(path->nodes[0]);
6437 eb = path->nodes[0];
6438 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006439 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6440 BUG_ON(ret);
6441 free_extent_buffer(eb);
6442 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006443
Yan Zhengf82d02d2008-10-29 14:49:05 -04006444 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006445 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006446 return 0;
6447}
6448
Chris Masond3977122009-01-05 21:25:51 -05006449static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006450 struct btrfs_root *root,
6451 struct btrfs_path *path,
6452 struct btrfs_key *first_key,
6453 struct btrfs_ref_path *ref_path)
6454{
6455 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006456
6457 ret = relocate_one_path(trans, root, path, first_key,
6458 ref_path, NULL, NULL);
6459 BUG_ON(ret);
6460
Zheng Yan1a40e232008-09-26 10:09:34 -04006461 return 0;
6462}
6463
Chris Masond3977122009-01-05 21:25:51 -05006464static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006465 struct btrfs_root *extent_root,
6466 struct btrfs_path *path,
6467 struct btrfs_key *extent_key)
6468{
6469 int ret;
6470
Zheng Yan1a40e232008-09-26 10:09:34 -04006471 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6472 if (ret)
6473 goto out;
6474 ret = btrfs_del_item(trans, extent_root, path);
6475out:
Chris Masonedbd8d42007-12-21 16:27:24 -05006476 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006477 return ret;
6478}
6479
Chris Masond3977122009-01-05 21:25:51 -05006480static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006481 struct btrfs_ref_path *ref_path)
6482{
6483 struct btrfs_key root_key;
6484
6485 root_key.objectid = ref_path->root_objectid;
6486 root_key.type = BTRFS_ROOT_ITEM_KEY;
6487 if (is_cowonly_root(ref_path->root_objectid))
6488 root_key.offset = 0;
6489 else
6490 root_key.offset = (u64)-1;
6491
6492 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6493}
6494
Chris Masond3977122009-01-05 21:25:51 -05006495static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006496 struct btrfs_path *path,
6497 struct btrfs_key *extent_key,
6498 struct btrfs_block_group_cache *group,
6499 struct inode *reloc_inode, int pass)
6500{
6501 struct btrfs_trans_handle *trans;
6502 struct btrfs_root *found_root;
6503 struct btrfs_ref_path *ref_path = NULL;
6504 struct disk_extent *new_extents = NULL;
6505 int nr_extents = 0;
6506 int loops;
6507 int ret;
6508 int level;
6509 struct btrfs_key first_key;
6510 u64 prev_block = 0;
6511
Zheng Yan1a40e232008-09-26 10:09:34 -04006512
6513 trans = btrfs_start_transaction(extent_root, 1);
6514 BUG_ON(!trans);
6515
6516 if (extent_key->objectid == 0) {
6517 ret = del_extent_zero(trans, extent_root, path, extent_key);
6518 goto out;
6519 }
6520
6521 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6522 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05006523 ret = -ENOMEM;
6524 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006525 }
6526
6527 for (loops = 0; ; loops++) {
6528 if (loops == 0) {
6529 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6530 extent_key->objectid);
6531 } else {
6532 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6533 }
6534 if (ret < 0)
6535 goto out;
6536 if (ret > 0)
6537 break;
6538
6539 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6540 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6541 continue;
6542
6543 found_root = read_ref_root(extent_root->fs_info, ref_path);
6544 BUG_ON(!found_root);
6545 /*
6546 * for reference counted tree, only process reference paths
6547 * rooted at the latest committed root.
6548 */
6549 if (found_root->ref_cows &&
6550 ref_path->root_generation != found_root->root_key.offset)
6551 continue;
6552
6553 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6554 if (pass == 0) {
6555 /*
6556 * copy data extents to new locations
6557 */
6558 u64 group_start = group->key.objectid;
6559 ret = relocate_data_extent(reloc_inode,
6560 extent_key,
6561 group_start);
6562 if (ret < 0)
6563 goto out;
6564 break;
6565 }
6566 level = 0;
6567 } else {
6568 level = ref_path->owner_objectid;
6569 }
6570
6571 if (prev_block != ref_path->nodes[level]) {
6572 struct extent_buffer *eb;
6573 u64 block_start = ref_path->nodes[level];
6574 u64 block_size = btrfs_level_size(found_root, level);
6575
6576 eb = read_tree_block(found_root, block_start,
6577 block_size, 0);
6578 btrfs_tree_lock(eb);
6579 BUG_ON(level != btrfs_header_level(eb));
6580
6581 if (level == 0)
6582 btrfs_item_key_to_cpu(eb, &first_key, 0);
6583 else
6584 btrfs_node_key_to_cpu(eb, &first_key, 0);
6585
6586 btrfs_tree_unlock(eb);
6587 free_extent_buffer(eb);
6588 prev_block = block_start;
6589 }
6590
Yan Zheng24562422009-02-12 14:14:53 -05006591 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006592 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05006593 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006594 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6595 /*
6596 * try to update data extent references while
6597 * keeping metadata shared between snapshots.
6598 */
6599 if (pass == 1) {
6600 ret = relocate_one_path(trans, found_root,
6601 path, &first_key, ref_path,
6602 group, reloc_inode);
6603 if (ret < 0)
6604 goto out;
6605 continue;
6606 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006607 /*
6608 * use fallback method to process the remaining
6609 * references.
6610 */
6611 if (!new_extents) {
6612 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04006613 new_extents = kmalloc(sizeof(*new_extents),
6614 GFP_NOFS);
6615 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006616 ret = get_new_locations(reloc_inode,
6617 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04006618 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04006619 &new_extents,
6620 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006621 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04006622 goto out;
6623 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006624 ret = replace_one_extent(trans, found_root,
6625 path, extent_key,
6626 &first_key, ref_path,
6627 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05006628 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006629 ret = relocate_tree_block(trans, found_root, path,
6630 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006631 }
6632 if (ret < 0)
6633 goto out;
6634 }
6635 ret = 0;
6636out:
6637 btrfs_end_transaction(trans, extent_root);
6638 kfree(new_extents);
6639 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006640 return ret;
6641}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006642#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006643
Chris Masonec44a352008-04-28 15:29:52 -04006644static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6645{
6646 u64 num_devices;
6647 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6648 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6649
Yan Zheng2b820322008-11-17 21:11:30 -05006650 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04006651 if (num_devices == 1) {
6652 stripped |= BTRFS_BLOCK_GROUP_DUP;
6653 stripped = flags & ~stripped;
6654
6655 /* turn raid0 into single device chunks */
6656 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6657 return stripped;
6658
6659 /* turn mirroring into duplication */
6660 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6661 BTRFS_BLOCK_GROUP_RAID10))
6662 return stripped | BTRFS_BLOCK_GROUP_DUP;
6663 return flags;
6664 } else {
6665 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006666 if (flags & stripped)
6667 return flags;
6668
6669 stripped |= BTRFS_BLOCK_GROUP_DUP;
6670 stripped = flags & ~stripped;
6671
6672 /* switch duplicated blocks with raid1 */
6673 if (flags & BTRFS_BLOCK_GROUP_DUP)
6674 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6675
6676 /* turn single device chunks into raid0 */
6677 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6678 }
6679 return flags;
6680}
6681
Christoph Hellwigb2950862008-12-02 09:54:17 -05006682static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04006683 struct btrfs_block_group_cache *shrink_block_group,
6684 int force)
6685{
6686 struct btrfs_trans_handle *trans;
6687 u64 new_alloc_flags;
6688 u64 calc;
6689
Chris Masonc286ac42008-07-22 23:06:41 -04006690 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006691 if (btrfs_block_group_used(&shrink_block_group->item) +
6692 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04006693 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006694
Chris Mason0ef3e662008-05-24 14:04:53 -04006695 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006696 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04006697
Chris Mason0ef3e662008-05-24 14:04:53 -04006698 new_alloc_flags = update_block_group_flags(root,
6699 shrink_block_group->flags);
6700 if (new_alloc_flags != shrink_block_group->flags) {
6701 calc =
6702 btrfs_block_group_used(&shrink_block_group->item);
6703 } else {
6704 calc = shrink_block_group->key.offset;
6705 }
Chris Masonc286ac42008-07-22 23:06:41 -04006706 spin_unlock(&shrink_block_group->lock);
6707
Chris Mason0ef3e662008-05-24 14:04:53 -04006708 do_chunk_alloc(trans, root->fs_info->extent_root,
6709 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04006710
Chris Mason0ef3e662008-05-24 14:04:53 -04006711 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04006712 } else
6713 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04006714 return 0;
6715}
6716
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006717
6718int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
6719 struct btrfs_block_group_cache *group)
6720
6721{
6722 __alloc_chunk_for_shrink(root, group, 1);
6723 set_block_group_readonly(group);
6724 return 0;
6725}
6726
Josef Bacikba1bf482009-09-11 16:11:19 -04006727/*
6728 * checks to see if its even possible to relocate this block group.
6729 *
6730 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6731 * ok to go ahead and try.
6732 */
6733int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
6734{
6735 struct btrfs_block_group_cache *block_group;
6736 struct btrfs_space_info *space_info;
6737 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6738 struct btrfs_device *device;
6739 int full = 0;
6740 int ret = 0;
6741
6742 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
6743
6744 /* odd, couldn't find the block group, leave it alone */
6745 if (!block_group)
6746 return -1;
6747
6748 /* no bytes used, we're good */
6749 if (!btrfs_block_group_used(&block_group->item))
6750 goto out;
6751
6752 space_info = block_group->space_info;
6753 spin_lock(&space_info->lock);
6754
6755 full = space_info->full;
6756
6757 /*
6758 * if this is the last block group we have in this space, we can't
6759 * relocate it.
6760 */
6761 if (space_info->total_bytes == block_group->key.offset) {
6762 ret = -1;
6763 spin_unlock(&space_info->lock);
6764 goto out;
6765 }
6766
6767 /*
6768 * need to make sure we have room in the space to handle all of the
6769 * extents from this block group. If we can, we're good
6770 */
6771 if (space_info->bytes_used + space_info->bytes_reserved +
6772 space_info->bytes_pinned + space_info->bytes_readonly +
6773 btrfs_block_group_used(&block_group->item) <
6774 space_info->total_bytes) {
6775 spin_unlock(&space_info->lock);
6776 goto out;
6777 }
6778 spin_unlock(&space_info->lock);
6779
6780 /*
6781 * ok we don't have enough space, but maybe we have free space on our
6782 * devices to allocate new chunks for relocation, so loop through our
6783 * alloc devices and guess if we have enough space. However, if we
6784 * were marked as full, then we know there aren't enough chunks, and we
6785 * can just return.
6786 */
6787 ret = -1;
6788 if (full)
6789 goto out;
6790
6791 mutex_lock(&root->fs_info->chunk_mutex);
6792 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
6793 u64 min_free = btrfs_block_group_used(&block_group->item);
6794 u64 dev_offset, max_avail;
6795
6796 /*
6797 * check to make sure we can actually find a chunk with enough
6798 * space to fit our block group in.
6799 */
6800 if (device->total_bytes > device->bytes_used + min_free) {
6801 ret = find_free_dev_extent(NULL, device, min_free,
6802 &dev_offset, &max_avail);
6803 if (!ret)
6804 break;
6805 ret = -1;
6806 }
6807 }
6808 mutex_unlock(&root->fs_info->chunk_mutex);
6809out:
6810 btrfs_put_block_group(block_group);
6811 return ret;
6812}
6813
Christoph Hellwigb2950862008-12-02 09:54:17 -05006814static int find_first_block_group(struct btrfs_root *root,
6815 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04006816{
Chris Mason925baed2008-06-25 16:01:30 -04006817 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006818 struct btrfs_key found_key;
6819 struct extent_buffer *leaf;
6820 int slot;
6821
6822 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6823 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006824 goto out;
6825
Chris Masond3977122009-01-05 21:25:51 -05006826 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006827 slot = path->slots[0];
6828 leaf = path->nodes[0];
6829 if (slot >= btrfs_header_nritems(leaf)) {
6830 ret = btrfs_next_leaf(root, path);
6831 if (ret == 0)
6832 continue;
6833 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006834 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04006835 break;
6836 }
6837 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6838
6839 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04006840 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6841 ret = 0;
6842 goto out;
6843 }
Chris Mason0b86a832008-03-24 15:01:56 -04006844 path->slots[0]++;
6845 }
6846 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04006847out:
Chris Mason0b86a832008-03-24 15:01:56 -04006848 return ret;
6849}
6850
Zheng Yan1a40e232008-09-26 10:09:34 -04006851int btrfs_free_block_groups(struct btrfs_fs_info *info)
6852{
6853 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04006854 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04006855 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04006856 struct rb_node *n;
6857
Yan Zheng11833d62009-09-11 16:11:19 -04006858 down_write(&info->extent_commit_sem);
6859 while (!list_empty(&info->caching_block_groups)) {
6860 caching_ctl = list_entry(info->caching_block_groups.next,
6861 struct btrfs_caching_control, list);
6862 list_del(&caching_ctl->list);
6863 put_caching_control(caching_ctl);
6864 }
6865 up_write(&info->extent_commit_sem);
6866
Zheng Yan1a40e232008-09-26 10:09:34 -04006867 spin_lock(&info->block_group_cache_lock);
6868 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6869 block_group = rb_entry(n, struct btrfs_block_group_cache,
6870 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04006871 rb_erase(&block_group->cache_node,
6872 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04006873 spin_unlock(&info->block_group_cache_lock);
6874
Josef Bacik80eb2342008-10-29 14:49:05 -04006875 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006876 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006877 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006878
Josef Bacik817d52f2009-07-13 21:29:25 -04006879 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04006880 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04006881
6882 btrfs_remove_free_space_cache(block_group);
6883
Yan Zhengd2fb3432008-12-11 16:30:39 -05006884 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006885 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04006886
6887 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006888 }
6889 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04006890
6891 /* now that all the block groups are freed, go through and
6892 * free all the space_info structs. This is only called during
6893 * the final stages of unmount, and so we know nobody is
6894 * using them. We call synchronize_rcu() once before we start,
6895 * just to be on the safe side.
6896 */
6897 synchronize_rcu();
6898
6899 while(!list_empty(&info->space_info)) {
6900 space_info = list_entry(info->space_info.next,
6901 struct btrfs_space_info,
6902 list);
6903
6904 list_del(&space_info->list);
6905 kfree(space_info);
6906 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006907 return 0;
6908}
6909
Chris Mason9078a3e2007-04-26 16:46:15 -04006910int btrfs_read_block_groups(struct btrfs_root *root)
6911{
6912 struct btrfs_path *path;
6913 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04006914 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04006915 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04006916 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04006917 struct btrfs_key key;
6918 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04006919 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04006920
Chris Masonbe744172007-05-06 10:15:01 -04006921 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04006922 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006923 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04006924 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04006925 path = btrfs_alloc_path();
6926 if (!path)
6927 return -ENOMEM;
6928
Chris Masond3977122009-01-05 21:25:51 -05006929 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006930 ret = find_first_block_group(root, path, &key);
6931 if (ret > 0) {
6932 ret = 0;
6933 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04006934 }
Chris Mason0b86a832008-03-24 15:01:56 -04006935 if (ret != 0)
6936 goto error;
6937
Chris Mason5f39d392007-10-15 16:14:19 -04006938 leaf = path->nodes[0];
6939 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04006940 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04006941 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04006942 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04006943 break;
6944 }
Chris Mason3e1ad542007-05-07 20:03:49 -04006945
Yan Zhengd2fb3432008-12-11 16:30:39 -05006946 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006947 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04006948 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04006949 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006950 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006951 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04006952
6953 /*
6954 * we only want to have 32k of ram per block group for keeping
6955 * track of free space, and if we pass 1/2 of that we want to
6956 * start converting things over to using bitmaps
6957 */
6958 cache->extents_thresh = ((1024 * 32) / 2) /
6959 sizeof(struct btrfs_free_space);
6960
Chris Mason5f39d392007-10-15 16:14:19 -04006961 read_extent_buffer(leaf, &cache->item,
6962 btrfs_item_ptr_offset(leaf, path->slots[0]),
6963 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04006964 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04006965
Chris Mason9078a3e2007-04-26 16:46:15 -04006966 key.objectid = found_key.objectid + found_key.offset;
6967 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04006968 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04006969 cache->sectorsize = root->sectorsize;
6970
Josef Bacik817d52f2009-07-13 21:29:25 -04006971 /*
6972 * check for two cases, either we are full, and therefore
6973 * don't need to bother with the caching work since we won't
6974 * find any space, or we are empty, and we can just add all
6975 * the space in and be done with it. This saves us _alot_ of
6976 * time, particularly in the full case.
6977 */
6978 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04006979 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04006980 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04006981 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04006982 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006983 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04006984 exclude_super_stripes(root, cache);
6985 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04006986 cache->cached = BTRFS_CACHE_FINISHED;
6987 add_new_free_space(cache, root->fs_info,
6988 found_key.objectid,
6989 found_key.objectid +
6990 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04006991 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006992 }
Chris Mason96b51792007-10-15 16:15:19 -04006993
Chris Mason6324fbf2008-03-24 15:01:59 -04006994 ret = update_space_info(info, cache->flags, found_key.offset,
6995 btrfs_block_group_used(&cache->item),
6996 &space_info);
6997 BUG_ON(ret);
6998 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04006999 spin_lock(&cache->space_info->lock);
7000 cache->space_info->bytes_super += cache->bytes_super;
7001 spin_unlock(&cache->space_info->lock);
7002
Josef Bacik80eb2342008-10-29 14:49:05 -04007003 down_write(&space_info->groups_sem);
7004 list_add_tail(&cache->list, &space_info->block_groups);
7005 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007006
Josef Bacik0f9dd462008-09-23 13:14:11 -04007007 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7008 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007009
7010 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007011 if (btrfs_chunk_readonly(root, cache->key.objectid))
7012 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007013 }
Chris Mason0b86a832008-03-24 15:01:56 -04007014 ret = 0;
7015error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007016 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007017 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007018}
Chris Mason6324fbf2008-03-24 15:01:59 -04007019
7020int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7021 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007022 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007023 u64 size)
7024{
7025 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007026 struct btrfs_root *extent_root;
7027 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007028
7029 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007030
Chris Mason12fcfd22009-03-24 10:24:20 -04007031 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007032
Chris Mason8f18cf12008-04-25 16:53:30 -04007033 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007034 if (!cache)
7035 return -ENOMEM;
7036
Chris Masone17cade2008-04-15 15:41:47 -04007037 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007038 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007039 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007040 cache->sectorsize = root->sectorsize;
7041
7042 /*
7043 * we only want to have 32k of ram per block group for keeping track
7044 * of free space, and if we pass 1/2 of that we want to start
7045 * converting things over to using bitmaps
7046 */
7047 cache->extents_thresh = ((1024 * 32) / 2) /
7048 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007049 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007050 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007051 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007052 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007053 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007054
Chris Mason6324fbf2008-03-24 15:01:59 -04007055 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007056 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7057 cache->flags = type;
7058 btrfs_set_block_group_flags(&cache->item, type);
7059
Yan Zheng11833d62009-09-11 16:11:19 -04007060 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007061 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007062 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007063
Josef Bacik817d52f2009-07-13 21:29:25 -04007064 add_new_free_space(cache, root->fs_info, chunk_offset,
7065 chunk_offset + size);
7066
Yan Zheng11833d62009-09-11 16:11:19 -04007067 free_excluded_extents(root, cache);
7068
Chris Mason6324fbf2008-03-24 15:01:59 -04007069 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7070 &cache->space_info);
7071 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007072
7073 spin_lock(&cache->space_info->lock);
7074 cache->space_info->bytes_super += cache->bytes_super;
7075 spin_unlock(&cache->space_info->lock);
7076
Josef Bacik80eb2342008-10-29 14:49:05 -04007077 down_write(&cache->space_info->groups_sem);
7078 list_add_tail(&cache->list, &cache->space_info->block_groups);
7079 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007080
Josef Bacik0f9dd462008-09-23 13:14:11 -04007081 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7082 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007083
Chris Mason6324fbf2008-03-24 15:01:59 -04007084 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7085 sizeof(cache->item));
7086 BUG_ON(ret);
7087
Chris Masond18a2c42008-04-04 15:40:00 -04007088 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007089
Chris Mason6324fbf2008-03-24 15:01:59 -04007090 return 0;
7091}
Zheng Yan1a40e232008-09-26 10:09:34 -04007092
7093int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7094 struct btrfs_root *root, u64 group_start)
7095{
7096 struct btrfs_path *path;
7097 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007098 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007099 struct btrfs_key key;
7100 int ret;
7101
Zheng Yan1a40e232008-09-26 10:09:34 -04007102 root = root->fs_info->extent_root;
7103
7104 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7105 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007106 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007107
7108 memcpy(&key, &block_group->key, sizeof(key));
7109
Chris Mason44fb5512009-06-04 15:34:51 -04007110 /* make sure this block group isn't part of an allocation cluster */
7111 cluster = &root->fs_info->data_alloc_cluster;
7112 spin_lock(&cluster->refill_lock);
7113 btrfs_return_cluster_to_free_space(block_group, cluster);
7114 spin_unlock(&cluster->refill_lock);
7115
7116 /*
7117 * make sure this block group isn't part of a metadata
7118 * allocation cluster
7119 */
7120 cluster = &root->fs_info->meta_alloc_cluster;
7121 spin_lock(&cluster->refill_lock);
7122 btrfs_return_cluster_to_free_space(block_group, cluster);
7123 spin_unlock(&cluster->refill_lock);
7124
Zheng Yan1a40e232008-09-26 10:09:34 -04007125 path = btrfs_alloc_path();
7126 BUG_ON(!path);
7127
Yan Zheng3dfdb932009-01-21 10:49:16 -05007128 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007129 rb_erase(&block_group->cache_node,
7130 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007131 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007132
Josef Bacik80eb2342008-10-29 14:49:05 -04007133 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007134 /*
7135 * we must use list_del_init so people can check to see if they
7136 * are still on the list after taking the semaphore
7137 */
7138 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007139 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007140
Josef Bacik817d52f2009-07-13 21:29:25 -04007141 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007142 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007143
7144 btrfs_remove_free_space_cache(block_group);
7145
Yan Zhengc146afa2008-11-12 14:34:12 -05007146 spin_lock(&block_group->space_info->lock);
7147 block_group->space_info->total_bytes -= block_group->key.offset;
7148 block_group->space_info->bytes_readonly -= block_group->key.offset;
7149 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007150
7151 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007152
Chris Masonfa9c0d792009-04-03 09:47:43 -04007153 btrfs_put_block_group(block_group);
7154 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007155
7156 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7157 if (ret > 0)
7158 ret = -EIO;
7159 if (ret < 0)
7160 goto out;
7161
7162 ret = btrfs_del_item(trans, root, path);
7163out:
7164 btrfs_free_path(path);
7165 return ret;
7166}