blob: 9bcb9c09c3b81271b87bf3c043e61df2ff6a8cb4 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050025#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050026#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027#include "ctree.h"
28#include "disk-io.h"
29#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040030#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040032#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040033#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
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--) {
Yan Zheng11833d62009-09-11 16:11:19 -0400204 ret = add_excluded_extent(root, logical[nr],
205 stripe_len);
206 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400207 }
Yan Zheng11833d62009-09-11 16:11:19 -0400208
Josef Bacik817d52f2009-07-13 21:29:25 -0400209 kfree(logical);
210 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400211 return 0;
212}
213
Yan Zheng11833d62009-09-11 16:11:19 -0400214static struct btrfs_caching_control *
215get_caching_control(struct btrfs_block_group_cache *cache)
216{
217 struct btrfs_caching_control *ctl;
218
219 spin_lock(&cache->lock);
220 if (cache->cached != BTRFS_CACHE_STARTED) {
221 spin_unlock(&cache->lock);
222 return NULL;
223 }
224
225 ctl = cache->caching_ctl;
226 atomic_inc(&ctl->count);
227 spin_unlock(&cache->lock);
228 return ctl;
229}
230
231static void put_caching_control(struct btrfs_caching_control *ctl)
232{
233 if (atomic_dec_and_test(&ctl->count))
234 kfree(ctl);
235}
236
Josef Bacik0f9dd462008-09-23 13:14:11 -0400237/*
238 * this is only called by cache_block_group, since we could have freed extents
239 * we need to check the pinned_extents for any extents that can't be used yet
240 * since their free space will be released as soon as the transaction commits.
241 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400242static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400243 struct btrfs_fs_info *info, u64 start, u64 end)
244{
Josef Bacik817d52f2009-07-13 21:29:25 -0400245 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400246 int ret;
247
248 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400249 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400250 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400251 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400252 if (ret)
253 break;
254
255 if (extent_start == start) {
256 start = extent_end + 1;
257 } else if (extent_start > start && extent_start < end) {
258 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400259 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500260 ret = btrfs_add_free_space(block_group, start,
261 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400262 BUG_ON(ret);
263 start = extent_end + 1;
264 } else {
265 break;
266 }
267 }
268
269 if (start < end) {
270 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400271 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500272 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400273 BUG_ON(ret);
274 }
275
Josef Bacik817d52f2009-07-13 21:29:25 -0400276 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400277}
278
Josef Bacik817d52f2009-07-13 21:29:25 -0400279static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400280{
Josef Bacik817d52f2009-07-13 21:29:25 -0400281 struct btrfs_block_group_cache *block_group = data;
282 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400283 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
284 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400285 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400286 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400287 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400288 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400289 u64 last = 0;
290 u32 nritems;
291 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400292
Chris Masone37c9e62007-05-09 20:13:14 -0400293 path = btrfs_alloc_path();
294 if (!path)
295 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400296
Yan Zheng11833d62009-09-11 16:11:19 -0400297 exclude_super_stripes(extent_root, block_group);
298
Josef Bacik817d52f2009-07-13 21:29:25 -0400299 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400300
Chris Mason5cd57b22008-06-25 16:01:30 -0400301 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400302 * We don't want to deadlock with somebody trying to allocate a new
303 * extent for the extent root while also trying to search the extent
304 * root to add free space. So we skip locking and search the commit
305 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400306 */
307 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400308 path->search_commit_root = 1;
309 path->reada = 2;
310
Yan Zhenge4404d62008-12-12 10:03:26 -0500311 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400312 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400313 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400314again:
Yan Zheng11833d62009-09-11 16:11:19 -0400315 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400316 /* need to make sure the commit_root doesn't disappear */
317 down_read(&fs_info->extent_commit_sem);
318
Yan Zheng11833d62009-09-11 16:11:19 -0400319 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400320 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400321 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500322
Yan Zheng11833d62009-09-11 16:11:19 -0400323 leaf = path->nodes[0];
324 nritems = btrfs_header_nritems(leaf);
325
Chris Masond3977122009-01-05 21:25:51 -0500326 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400327 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400328 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400329 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400330 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400331 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400332
Yan Zheng11833d62009-09-11 16:11:19 -0400333 if (path->slots[0] < nritems) {
334 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
335 } else {
336 ret = find_next_key(path, 0, &key);
337 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400338 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400339
Yan Zheng11833d62009-09-11 16:11:19 -0400340 caching_ctl->progress = last;
341 btrfs_release_path(extent_root, path);
342 up_read(&fs_info->extent_commit_sem);
343 mutex_unlock(&caching_ctl->mutex);
344 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400345 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400346 else
347 cond_resched();
348 goto again;
349 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400350
Yan Zheng11833d62009-09-11 16:11:19 -0400351 if (key.objectid < block_group->key.objectid) {
352 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400353 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400354 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400355
Chris Masone37c9e62007-05-09 20:13:14 -0400356 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400357 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400358 break;
Yan7d7d6062007-09-14 16:15:28 -0400359
Yan Zheng11833d62009-09-11 16:11:19 -0400360 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400361 total_found += add_new_free_space(block_group,
362 fs_info, last,
363 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400364 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400365
Yan Zheng11833d62009-09-11 16:11:19 -0400366 if (total_found > (1024 * 1024 * 2)) {
367 total_found = 0;
368 wake_up(&caching_ctl->wait);
369 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400370 }
Chris Masone37c9e62007-05-09 20:13:14 -0400371 path->slots[0]++;
372 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400373 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400374
375 total_found += add_new_free_space(block_group, fs_info, last,
376 block_group->key.objectid +
377 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400378 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400379
380 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400381 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400382 block_group->cached = BTRFS_CACHE_FINISHED;
383 spin_unlock(&block_group->lock);
384
Chris Mason54aa1f42007-06-22 14:16:25 -0400385err:
Chris Masone37c9e62007-05-09 20:13:14 -0400386 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400387 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400388
Yan Zheng11833d62009-09-11 16:11:19 -0400389 free_excluded_extents(extent_root, block_group);
390
391 mutex_unlock(&caching_ctl->mutex);
392 wake_up(&caching_ctl->wait);
393
394 put_caching_control(caching_ctl);
395 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik817d52f2009-07-13 21:29:25 -0400396 return 0;
397}
398
399static int cache_block_group(struct btrfs_block_group_cache *cache)
400{
Yan Zheng11833d62009-09-11 16:11:19 -0400401 struct btrfs_fs_info *fs_info = cache->fs_info;
402 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400403 struct task_struct *tsk;
404 int ret = 0;
405
Yan Zheng11833d62009-09-11 16:11:19 -0400406 smp_mb();
407 if (cache->cached != BTRFS_CACHE_NO)
408 return 0;
409
410 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
411 BUG_ON(!caching_ctl);
412
413 INIT_LIST_HEAD(&caching_ctl->list);
414 mutex_init(&caching_ctl->mutex);
415 init_waitqueue_head(&caching_ctl->wait);
416 caching_ctl->block_group = cache;
417 caching_ctl->progress = cache->key.objectid;
418 /* one for caching kthread, one for caching block group list */
419 atomic_set(&caching_ctl->count, 2);
420
Josef Bacik817d52f2009-07-13 21:29:25 -0400421 spin_lock(&cache->lock);
422 if (cache->cached != BTRFS_CACHE_NO) {
423 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400424 kfree(caching_ctl);
425 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400426 }
Yan Zheng11833d62009-09-11 16:11:19 -0400427 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400428 cache->cached = BTRFS_CACHE_STARTED;
429 spin_unlock(&cache->lock);
430
Yan Zheng11833d62009-09-11 16:11:19 -0400431 down_write(&fs_info->extent_commit_sem);
432 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
433 up_write(&fs_info->extent_commit_sem);
434
435 atomic_inc(&cache->space_info->caching_threads);
436
Josef Bacik817d52f2009-07-13 21:29:25 -0400437 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
438 cache->key.objectid);
439 if (IS_ERR(tsk)) {
440 ret = PTR_ERR(tsk);
441 printk(KERN_ERR "error running thread %d\n", ret);
442 BUG();
443 }
444
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400445 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400446}
447
Josef Bacik0f9dd462008-09-23 13:14:11 -0400448/*
449 * return the block group that starts at or after bytenr
450 */
Chris Masond3977122009-01-05 21:25:51 -0500451static struct btrfs_block_group_cache *
452btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400453{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400454 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400455
Josef Bacik0f9dd462008-09-23 13:14:11 -0400456 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400457
Josef Bacik0f9dd462008-09-23 13:14:11 -0400458 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400459}
460
Josef Bacik0f9dd462008-09-23 13:14:11 -0400461/*
Sankar P9f556842009-05-14 13:52:22 -0400462 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400463 */
Chris Masond3977122009-01-05 21:25:51 -0500464struct btrfs_block_group_cache *btrfs_lookup_block_group(
465 struct btrfs_fs_info *info,
466 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400467{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400468 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400469
Josef Bacik0f9dd462008-09-23 13:14:11 -0400470 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400471
Josef Bacik0f9dd462008-09-23 13:14:11 -0400472 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400473}
Chris Mason0b86a832008-03-24 15:01:56 -0400474
Chris Masonfa9c0d72009-04-03 09:47:43 -0400475void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
Yan Zhengd2fb3432008-12-11 16:30:39 -0500476{
477 if (atomic_dec_and_test(&cache->count))
478 kfree(cache);
479}
480
Josef Bacik0f9dd462008-09-23 13:14:11 -0400481static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
482 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400483{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400484 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400485 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400486
487 rcu_read_lock();
488 list_for_each_entry_rcu(found, head, list) {
489 if (found->flags == flags) {
490 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400491 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400492 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400493 }
Chris Mason4184ea72009-03-10 12:39:20 -0400494 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400495 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400496}
497
Chris Mason4184ea72009-03-10 12:39:20 -0400498/*
499 * after adding space to the filesystem, we need to clear the full flags
500 * on all the space infos.
501 */
502void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
503{
504 struct list_head *head = &info->space_info;
505 struct btrfs_space_info *found;
506
507 rcu_read_lock();
508 list_for_each_entry_rcu(found, head, list)
509 found->full = 0;
510 rcu_read_unlock();
511}
512
Josef Bacik80eb2342008-10-29 14:49:05 -0400513static u64 div_factor(u64 num, int factor)
514{
515 if (factor == 10)
516 return num;
517 num *= factor;
518 do_div(num, 10);
519 return num;
520}
521
Yan Zhengd2fb3432008-12-11 16:30:39 -0500522u64 btrfs_find_block_group(struct btrfs_root *root,
523 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400524{
Chris Mason96b51792007-10-15 16:15:19 -0400525 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400526 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500527 u64 last = max(search_hint, search_start);
528 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400529 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500530 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400531 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400532again:
Zheng Yane8569812008-09-26 10:05:48 -0400533 while (1) {
534 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400535 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400536 break;
Chris Mason96b51792007-10-15 16:15:19 -0400537
Chris Masonc286ac42008-07-22 23:06:41 -0400538 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400539 last = cache->key.objectid + cache->key.offset;
540 used = btrfs_block_group_used(&cache->item);
541
Yan Zhengd2fb3432008-12-11 16:30:39 -0500542 if ((full_search || !cache->ro) &&
543 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400544 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500545 div_factor(cache->key.offset, factor)) {
546 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400547 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400548 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400549 goto found;
550 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400551 }
Chris Masonc286ac42008-07-22 23:06:41 -0400552 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400553 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400554 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400555 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400556 if (!wrapped) {
557 last = search_start;
558 wrapped = 1;
559 goto again;
560 }
561 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400562 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400563 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400564 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400565 goto again;
566 }
Chris Masonbe744172007-05-06 10:15:01 -0400567found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500568 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400569}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400570
Chris Masone02119d2008-09-05 16:13:11 -0400571/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400572int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400573{
574 int ret;
575 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400576 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400577
Zheng Yan31840ae2008-09-23 13:14:14 -0400578 path = btrfs_alloc_path();
579 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400580 key.objectid = start;
581 key.offset = len;
582 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
583 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
584 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400585 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500586 return ret;
587}
588
Chris Masond8d5f3e2007-12-11 12:42:00 -0500589/*
590 * Back reference rules. Back refs have three main goals:
591 *
592 * 1) differentiate between all holders of references to an extent so that
593 * when a reference is dropped we can make sure it was a valid reference
594 * before freeing the extent.
595 *
596 * 2) Provide enough information to quickly find the holders of an extent
597 * if we notice a given block is corrupted or bad.
598 *
599 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
600 * maintenance. This is actually the same as #2, but with a slightly
601 * different use case.
602 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400603 * There are two kinds of back refs. The implicit back refs is optimized
604 * for pointers in non-shared tree blocks. For a given pointer in a block,
605 * back refs of this kind provide information about the block's owner tree
606 * and the pointer's key. These information allow us to find the block by
607 * b-tree searching. The full back refs is for pointers in tree blocks not
608 * referenced by their owner trees. The location of tree block is recorded
609 * in the back refs. Actually the full back refs is generic, and can be
610 * used in all cases the implicit back refs is used. The major shortcoming
611 * of the full back refs is its overhead. Every time a tree block gets
612 * COWed, we have to update back refs entry for all pointers in it.
613 *
614 * For a newly allocated tree block, we use implicit back refs for
615 * pointers in it. This means most tree related operations only involve
616 * implicit back refs. For a tree block created in old transaction, the
617 * only way to drop a reference to it is COW it. So we can detect the
618 * event that tree block loses its owner tree's reference and do the
619 * back refs conversion.
620 *
621 * When a tree block is COW'd through a tree, there are four cases:
622 *
623 * The reference count of the block is one and the tree is the block's
624 * owner tree. Nothing to do in this case.
625 *
626 * The reference count of the block is one and the tree is not the
627 * block's owner tree. In this case, full back refs is used for pointers
628 * in the block. Remove these full back refs, add implicit back refs for
629 * every pointers in the new block.
630 *
631 * The reference count of the block is greater than one and the tree is
632 * the block's owner tree. In this case, implicit back refs is used for
633 * pointers in the block. Add full back refs for every pointers in the
634 * block, increase lower level extents' reference counts. The original
635 * implicit back refs are entailed to the new block.
636 *
637 * The reference count of the block is greater than one and the tree is
638 * not the block's owner tree. Add implicit back refs for every pointer in
639 * the new block, increase lower level extents' reference count.
640 *
641 * Back Reference Key composing:
642 *
643 * The key objectid corresponds to the first byte in the extent,
644 * The key type is used to differentiate between types of back refs.
645 * There are different meanings of the key offset for different types
646 * of back refs.
647 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500648 * File extents can be referenced by:
649 *
650 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400651 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500652 * - different offsets inside a file (bookend extents in file.c)
653 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400654 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500655 *
656 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500657 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400658 * - original offset in the file
659 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400660 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400661 * The key offset for the implicit back refs is hash of the first
662 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500663 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400664 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500665 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400666 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500667 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400668 * The key offset for the implicit back refs is the first byte of
669 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500670 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400671 * When a file extent is allocated, The implicit back refs is used.
672 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500673 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400674 * (root_key.objectid, inode objectid, offset in file, 1)
675 *
676 * When a file extent is removed file truncation, we find the
677 * corresponding implicit back refs and check the following fields:
678 *
679 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500680 *
681 * Btree extents can be referenced by:
682 *
683 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500684 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400685 * Both the implicit back refs and the full back refs for tree blocks
686 * only consist of key. The key offset for the implicit back refs is
687 * objectid of block's owner tree. The key offset for the full back refs
688 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500689 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400690 * When implicit back refs is used, information about the lowest key and
691 * level of the tree block are required. These information are stored in
692 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500693 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400694
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400695#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
696static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
697 struct btrfs_root *root,
698 struct btrfs_path *path,
699 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500700{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400701 struct btrfs_extent_item *item;
702 struct btrfs_extent_item_v0 *ei0;
703 struct btrfs_extent_ref_v0 *ref0;
704 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400705 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400706 struct btrfs_key key;
707 struct btrfs_key found_key;
708 u32 new_size = sizeof(*item);
709 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500710 int ret;
711
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400712 leaf = path->nodes[0];
713 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500714
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400715 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
716 ei0 = btrfs_item_ptr(leaf, path->slots[0],
717 struct btrfs_extent_item_v0);
718 refs = btrfs_extent_refs_v0(leaf, ei0);
719
720 if (owner == (u64)-1) {
721 while (1) {
722 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
723 ret = btrfs_next_leaf(root, path);
724 if (ret < 0)
725 return ret;
726 BUG_ON(ret > 0);
727 leaf = path->nodes[0];
728 }
729 btrfs_item_key_to_cpu(leaf, &found_key,
730 path->slots[0]);
731 BUG_ON(key.objectid != found_key.objectid);
732 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
733 path->slots[0]++;
734 continue;
735 }
736 ref0 = btrfs_item_ptr(leaf, path->slots[0],
737 struct btrfs_extent_ref_v0);
738 owner = btrfs_ref_objectid_v0(leaf, ref0);
739 break;
740 }
741 }
742 btrfs_release_path(root, path);
743
744 if (owner < BTRFS_FIRST_FREE_OBJECTID)
745 new_size += sizeof(*bi);
746
747 new_size -= sizeof(*ei0);
748 ret = btrfs_search_slot(trans, root, &key, path,
749 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400750 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400751 return ret;
752 BUG_ON(ret);
753
754 ret = btrfs_extend_item(trans, root, path, new_size);
755 BUG_ON(ret);
756
757 leaf = path->nodes[0];
758 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
759 btrfs_set_extent_refs(leaf, item, refs);
760 /* FIXME: get real generation */
761 btrfs_set_extent_generation(leaf, item, 0);
762 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
763 btrfs_set_extent_flags(leaf, item,
764 BTRFS_EXTENT_FLAG_TREE_BLOCK |
765 BTRFS_BLOCK_FLAG_FULL_BACKREF);
766 bi = (struct btrfs_tree_block_info *)(item + 1);
767 /* FIXME: get first key of the block */
768 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
769 btrfs_set_tree_block_level(leaf, bi, (int)owner);
770 } else {
771 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
772 }
773 btrfs_mark_buffer_dirty(leaf);
774 return 0;
775}
776#endif
777
778static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
779{
780 u32 high_crc = ~(u32)0;
781 u32 low_crc = ~(u32)0;
782 __le64 lenum;
783
784 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100785 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400786 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100787 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400788 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100789 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400790
791 return ((u64)high_crc << 31) ^ (u64)low_crc;
792}
793
794static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
795 struct btrfs_extent_data_ref *ref)
796{
797 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
798 btrfs_extent_data_ref_objectid(leaf, ref),
799 btrfs_extent_data_ref_offset(leaf, ref));
800}
801
802static int match_extent_data_ref(struct extent_buffer *leaf,
803 struct btrfs_extent_data_ref *ref,
804 u64 root_objectid, u64 owner, u64 offset)
805{
806 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
807 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
808 btrfs_extent_data_ref_offset(leaf, ref) != offset)
809 return 0;
810 return 1;
811}
812
813static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
814 struct btrfs_root *root,
815 struct btrfs_path *path,
816 u64 bytenr, u64 parent,
817 u64 root_objectid,
818 u64 owner, u64 offset)
819{
820 struct btrfs_key key;
821 struct btrfs_extent_data_ref *ref;
822 struct extent_buffer *leaf;
823 u32 nritems;
824 int ret;
825 int recow;
826 int err = -ENOENT;
827
828 key.objectid = bytenr;
829 if (parent) {
830 key.type = BTRFS_SHARED_DATA_REF_KEY;
831 key.offset = parent;
832 } else {
833 key.type = BTRFS_EXTENT_DATA_REF_KEY;
834 key.offset = hash_extent_data_ref(root_objectid,
835 owner, offset);
836 }
837again:
838 recow = 0;
839 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
840 if (ret < 0) {
841 err = ret;
842 goto fail;
843 }
844
845 if (parent) {
846 if (!ret)
847 return 0;
848#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
849 key.type = BTRFS_EXTENT_REF_V0_KEY;
850 btrfs_release_path(root, path);
851 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
852 if (ret < 0) {
853 err = ret;
854 goto fail;
855 }
856 if (!ret)
857 return 0;
858#endif
859 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400860 }
861
862 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400863 nritems = btrfs_header_nritems(leaf);
864 while (1) {
865 if (path->slots[0] >= nritems) {
866 ret = btrfs_next_leaf(root, path);
867 if (ret < 0)
868 err = ret;
869 if (ret)
870 goto fail;
871
872 leaf = path->nodes[0];
873 nritems = btrfs_header_nritems(leaf);
874 recow = 1;
875 }
876
877 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
878 if (key.objectid != bytenr ||
879 key.type != BTRFS_EXTENT_DATA_REF_KEY)
880 goto fail;
881
882 ref = btrfs_item_ptr(leaf, path->slots[0],
883 struct btrfs_extent_data_ref);
884
885 if (match_extent_data_ref(leaf, ref, root_objectid,
886 owner, offset)) {
887 if (recow) {
888 btrfs_release_path(root, path);
889 goto again;
890 }
891 err = 0;
892 break;
893 }
894 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400895 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400896fail:
897 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400898}
899
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400900static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
901 struct btrfs_root *root,
902 struct btrfs_path *path,
903 u64 bytenr, u64 parent,
904 u64 root_objectid, u64 owner,
905 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400906{
907 struct btrfs_key key;
908 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400909 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400910 u32 num_refs;
911 int ret;
912
913 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400914 if (parent) {
915 key.type = BTRFS_SHARED_DATA_REF_KEY;
916 key.offset = parent;
917 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400918 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400919 key.type = BTRFS_EXTENT_DATA_REF_KEY;
920 key.offset = hash_extent_data_ref(root_objectid,
921 owner, offset);
922 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400923 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400924
925 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
926 if (ret && ret != -EEXIST)
927 goto fail;
928
929 leaf = path->nodes[0];
930 if (parent) {
931 struct btrfs_shared_data_ref *ref;
932 ref = btrfs_item_ptr(leaf, path->slots[0],
933 struct btrfs_shared_data_ref);
934 if (ret == 0) {
935 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
936 } else {
937 num_refs = btrfs_shared_data_ref_count(leaf, ref);
938 num_refs += refs_to_add;
939 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
940 }
941 } else {
942 struct btrfs_extent_data_ref *ref;
943 while (ret == -EEXIST) {
944 ref = btrfs_item_ptr(leaf, path->slots[0],
945 struct btrfs_extent_data_ref);
946 if (match_extent_data_ref(leaf, ref, root_objectid,
947 owner, offset))
948 break;
949 btrfs_release_path(root, path);
950 key.offset++;
951 ret = btrfs_insert_empty_item(trans, root, path, &key,
952 size);
953 if (ret && ret != -EEXIST)
954 goto fail;
955
956 leaf = path->nodes[0];
957 }
958 ref = btrfs_item_ptr(leaf, path->slots[0],
959 struct btrfs_extent_data_ref);
960 if (ret == 0) {
961 btrfs_set_extent_data_ref_root(leaf, ref,
962 root_objectid);
963 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
964 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
965 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
966 } else {
967 num_refs = btrfs_extent_data_ref_count(leaf, ref);
968 num_refs += refs_to_add;
969 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
970 }
971 }
972 btrfs_mark_buffer_dirty(leaf);
973 ret = 0;
974fail:
Chris Mason7bb86312007-12-11 09:25:06 -0500975 btrfs_release_path(root, path);
976 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500977}
978
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400979static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
980 struct btrfs_root *root,
981 struct btrfs_path *path,
982 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -0400983{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400984 struct btrfs_key key;
985 struct btrfs_extent_data_ref *ref1 = NULL;
986 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400987 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400989 int ret = 0;
990
991 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400992 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
993
994 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
995 ref1 = btrfs_item_ptr(leaf, path->slots[0],
996 struct btrfs_extent_data_ref);
997 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
998 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
999 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1000 struct btrfs_shared_data_ref);
1001 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1002#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1003 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1004 struct btrfs_extent_ref_v0 *ref0;
1005 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_extent_ref_v0);
1007 num_refs = btrfs_ref_count_v0(leaf, ref0);
1008#endif
1009 } else {
1010 BUG();
1011 }
1012
Chris Mason56bec292009-03-13 10:10:06 -04001013 BUG_ON(num_refs < refs_to_drop);
1014 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015
Zheng Yan31840ae2008-09-23 13:14:14 -04001016 if (num_refs == 0) {
1017 ret = btrfs_del_item(trans, root, path);
1018 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001019 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1020 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1021 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1022 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1023#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1024 else {
1025 struct btrfs_extent_ref_v0 *ref0;
1026 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1027 struct btrfs_extent_ref_v0);
1028 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1029 }
1030#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001031 btrfs_mark_buffer_dirty(leaf);
1032 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001033 return ret;
1034}
1035
1036static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1037 struct btrfs_path *path,
1038 struct btrfs_extent_inline_ref *iref)
1039{
1040 struct btrfs_key key;
1041 struct extent_buffer *leaf;
1042 struct btrfs_extent_data_ref *ref1;
1043 struct btrfs_shared_data_ref *ref2;
1044 u32 num_refs = 0;
1045
1046 leaf = path->nodes[0];
1047 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1048 if (iref) {
1049 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1050 BTRFS_EXTENT_DATA_REF_KEY) {
1051 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1052 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1053 } else {
1054 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1055 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1056 }
1057 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1058 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1059 struct btrfs_extent_data_ref);
1060 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1061 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1062 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1063 struct btrfs_shared_data_ref);
1064 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1065#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1066 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1067 struct btrfs_extent_ref_v0 *ref0;
1068 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1069 struct btrfs_extent_ref_v0);
1070 num_refs = btrfs_ref_count_v0(leaf, ref0);
1071#endif
1072 } else {
1073 WARN_ON(1);
1074 }
1075 return num_refs;
1076}
1077
1078static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1079 struct btrfs_root *root,
1080 struct btrfs_path *path,
1081 u64 bytenr, u64 parent,
1082 u64 root_objectid)
1083{
1084 struct btrfs_key key;
1085 int ret;
1086
1087 key.objectid = bytenr;
1088 if (parent) {
1089 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1090 key.offset = parent;
1091 } else {
1092 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1093 key.offset = root_objectid;
1094 }
1095
1096 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1097 if (ret > 0)
1098 ret = -ENOENT;
1099#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1100 if (ret == -ENOENT && parent) {
1101 btrfs_release_path(root, path);
1102 key.type = BTRFS_EXTENT_REF_V0_KEY;
1103 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1104 if (ret > 0)
1105 ret = -ENOENT;
1106 }
1107#endif
1108 return ret;
1109}
1110
1111static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1112 struct btrfs_root *root,
1113 struct btrfs_path *path,
1114 u64 bytenr, u64 parent,
1115 u64 root_objectid)
1116{
1117 struct btrfs_key key;
1118 int ret;
1119
1120 key.objectid = bytenr;
1121 if (parent) {
1122 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1123 key.offset = parent;
1124 } else {
1125 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1126 key.offset = root_objectid;
1127 }
1128
1129 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001130 btrfs_release_path(root, path);
1131 return ret;
1132}
1133
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001134static inline int extent_ref_type(u64 parent, u64 owner)
1135{
1136 int type;
1137 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1138 if (parent > 0)
1139 type = BTRFS_SHARED_BLOCK_REF_KEY;
1140 else
1141 type = BTRFS_TREE_BLOCK_REF_KEY;
1142 } else {
1143 if (parent > 0)
1144 type = BTRFS_SHARED_DATA_REF_KEY;
1145 else
1146 type = BTRFS_EXTENT_DATA_REF_KEY;
1147 }
1148 return type;
1149}
1150
Yan Zheng2c47e6052009-06-27 21:07:35 -04001151static int find_next_key(struct btrfs_path *path, int level,
1152 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001153
1154{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001155 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001156 if (!path->nodes[level])
1157 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001158 if (path->slots[level] + 1 >=
1159 btrfs_header_nritems(path->nodes[level]))
1160 continue;
1161 if (level == 0)
1162 btrfs_item_key_to_cpu(path->nodes[level], key,
1163 path->slots[level] + 1);
1164 else
1165 btrfs_node_key_to_cpu(path->nodes[level], key,
1166 path->slots[level] + 1);
1167 return 0;
1168 }
1169 return 1;
1170}
1171
1172/*
1173 * look for inline back ref. if back ref is found, *ref_ret is set
1174 * to the address of inline back ref, and 0 is returned.
1175 *
1176 * if back ref isn't found, *ref_ret is set to the address where it
1177 * should be inserted, and -ENOENT is returned.
1178 *
1179 * if insert is true and there are too many inline back refs, the path
1180 * points to the extent item, and -EAGAIN is returned.
1181 *
1182 * NOTE: inline back refs are ordered in the same way that back ref
1183 * items in the tree are ordered.
1184 */
1185static noinline_for_stack
1186int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1187 struct btrfs_root *root,
1188 struct btrfs_path *path,
1189 struct btrfs_extent_inline_ref **ref_ret,
1190 u64 bytenr, u64 num_bytes,
1191 u64 parent, u64 root_objectid,
1192 u64 owner, u64 offset, int insert)
1193{
1194 struct btrfs_key key;
1195 struct extent_buffer *leaf;
1196 struct btrfs_extent_item *ei;
1197 struct btrfs_extent_inline_ref *iref;
1198 u64 flags;
1199 u64 item_size;
1200 unsigned long ptr;
1201 unsigned long end;
1202 int extra_size;
1203 int type;
1204 int want;
1205 int ret;
1206 int err = 0;
1207
1208 key.objectid = bytenr;
1209 key.type = BTRFS_EXTENT_ITEM_KEY;
1210 key.offset = num_bytes;
1211
1212 want = extent_ref_type(parent, owner);
1213 if (insert) {
1214 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001215 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001216 } else
1217 extra_size = -1;
1218 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1219 if (ret < 0) {
1220 err = ret;
1221 goto out;
1222 }
1223 BUG_ON(ret);
1224
1225 leaf = path->nodes[0];
1226 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1227#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1228 if (item_size < sizeof(*ei)) {
1229 if (!insert) {
1230 err = -ENOENT;
1231 goto out;
1232 }
1233 ret = convert_extent_item_v0(trans, root, path, owner,
1234 extra_size);
1235 if (ret < 0) {
1236 err = ret;
1237 goto out;
1238 }
1239 leaf = path->nodes[0];
1240 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1241 }
1242#endif
1243 BUG_ON(item_size < sizeof(*ei));
1244
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001245 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1246 flags = btrfs_extent_flags(leaf, ei);
1247
1248 ptr = (unsigned long)(ei + 1);
1249 end = (unsigned long)ei + item_size;
1250
1251 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1252 ptr += sizeof(struct btrfs_tree_block_info);
1253 BUG_ON(ptr > end);
1254 } else {
1255 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1256 }
1257
1258 err = -ENOENT;
1259 while (1) {
1260 if (ptr >= end) {
1261 WARN_ON(ptr > end);
1262 break;
1263 }
1264 iref = (struct btrfs_extent_inline_ref *)ptr;
1265 type = btrfs_extent_inline_ref_type(leaf, iref);
1266 if (want < type)
1267 break;
1268 if (want > type) {
1269 ptr += btrfs_extent_inline_ref_size(type);
1270 continue;
1271 }
1272
1273 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1274 struct btrfs_extent_data_ref *dref;
1275 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1276 if (match_extent_data_ref(leaf, dref, root_objectid,
1277 owner, offset)) {
1278 err = 0;
1279 break;
1280 }
1281 if (hash_extent_data_ref_item(leaf, dref) <
1282 hash_extent_data_ref(root_objectid, owner, offset))
1283 break;
1284 } else {
1285 u64 ref_offset;
1286 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1287 if (parent > 0) {
1288 if (parent == ref_offset) {
1289 err = 0;
1290 break;
1291 }
1292 if (ref_offset < parent)
1293 break;
1294 } else {
1295 if (root_objectid == ref_offset) {
1296 err = 0;
1297 break;
1298 }
1299 if (ref_offset < root_objectid)
1300 break;
1301 }
1302 }
1303 ptr += btrfs_extent_inline_ref_size(type);
1304 }
1305 if (err == -ENOENT && insert) {
1306 if (item_size + extra_size >=
1307 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1308 err = -EAGAIN;
1309 goto out;
1310 }
1311 /*
1312 * To add new inline back ref, we have to make sure
1313 * there is no corresponding back ref item.
1314 * For simplicity, we just do not add new inline back
1315 * ref if there is any kind of item for this block
1316 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001317 if (find_next_key(path, 0, &key) == 0 &&
1318 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001319 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001320 err = -EAGAIN;
1321 goto out;
1322 }
1323 }
1324 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1325out:
Yan Zheng85d41982009-06-11 08:51:10 -04001326 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001327 path->keep_locks = 0;
1328 btrfs_unlock_up_safe(path, 1);
1329 }
1330 return err;
1331}
1332
1333/*
1334 * helper to add new inline back ref
1335 */
1336static noinline_for_stack
1337int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1338 struct btrfs_root *root,
1339 struct btrfs_path *path,
1340 struct btrfs_extent_inline_ref *iref,
1341 u64 parent, u64 root_objectid,
1342 u64 owner, u64 offset, int refs_to_add,
1343 struct btrfs_delayed_extent_op *extent_op)
1344{
1345 struct extent_buffer *leaf;
1346 struct btrfs_extent_item *ei;
1347 unsigned long ptr;
1348 unsigned long end;
1349 unsigned long item_offset;
1350 u64 refs;
1351 int size;
1352 int type;
1353 int ret;
1354
1355 leaf = path->nodes[0];
1356 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1357 item_offset = (unsigned long)iref - (unsigned long)ei;
1358
1359 type = extent_ref_type(parent, owner);
1360 size = btrfs_extent_inline_ref_size(type);
1361
1362 ret = btrfs_extend_item(trans, root, path, size);
1363 BUG_ON(ret);
1364
1365 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1366 refs = btrfs_extent_refs(leaf, ei);
1367 refs += refs_to_add;
1368 btrfs_set_extent_refs(leaf, ei, refs);
1369 if (extent_op)
1370 __run_delayed_extent_op(extent_op, leaf, ei);
1371
1372 ptr = (unsigned long)ei + item_offset;
1373 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1374 if (ptr < end - size)
1375 memmove_extent_buffer(leaf, ptr + size, ptr,
1376 end - size - ptr);
1377
1378 iref = (struct btrfs_extent_inline_ref *)ptr;
1379 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1380 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1381 struct btrfs_extent_data_ref *dref;
1382 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1383 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1384 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1385 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1386 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1387 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1388 struct btrfs_shared_data_ref *sref;
1389 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1390 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1391 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1392 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1393 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1394 } else {
1395 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1396 }
1397 btrfs_mark_buffer_dirty(leaf);
1398 return 0;
1399}
1400
1401static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1402 struct btrfs_root *root,
1403 struct btrfs_path *path,
1404 struct btrfs_extent_inline_ref **ref_ret,
1405 u64 bytenr, u64 num_bytes, u64 parent,
1406 u64 root_objectid, u64 owner, u64 offset)
1407{
1408 int ret;
1409
1410 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1411 bytenr, num_bytes, parent,
1412 root_objectid, owner, offset, 0);
1413 if (ret != -ENOENT)
1414 return ret;
1415
1416 btrfs_release_path(root, path);
1417 *ref_ret = NULL;
1418
1419 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1420 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1421 root_objectid);
1422 } else {
1423 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1424 root_objectid, owner, offset);
1425 }
1426 return ret;
1427}
1428
1429/*
1430 * helper to update/remove inline back ref
1431 */
1432static noinline_for_stack
1433int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1434 struct btrfs_root *root,
1435 struct btrfs_path *path,
1436 struct btrfs_extent_inline_ref *iref,
1437 int refs_to_mod,
1438 struct btrfs_delayed_extent_op *extent_op)
1439{
1440 struct extent_buffer *leaf;
1441 struct btrfs_extent_item *ei;
1442 struct btrfs_extent_data_ref *dref = NULL;
1443 struct btrfs_shared_data_ref *sref = NULL;
1444 unsigned long ptr;
1445 unsigned long end;
1446 u32 item_size;
1447 int size;
1448 int type;
1449 int ret;
1450 u64 refs;
1451
1452 leaf = path->nodes[0];
1453 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1454 refs = btrfs_extent_refs(leaf, ei);
1455 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1456 refs += refs_to_mod;
1457 btrfs_set_extent_refs(leaf, ei, refs);
1458 if (extent_op)
1459 __run_delayed_extent_op(extent_op, leaf, ei);
1460
1461 type = btrfs_extent_inline_ref_type(leaf, iref);
1462
1463 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1464 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1465 refs = btrfs_extent_data_ref_count(leaf, dref);
1466 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1467 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1468 refs = btrfs_shared_data_ref_count(leaf, sref);
1469 } else {
1470 refs = 1;
1471 BUG_ON(refs_to_mod != -1);
1472 }
1473
1474 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1475 refs += refs_to_mod;
1476
1477 if (refs > 0) {
1478 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1479 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1480 else
1481 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1482 } else {
1483 size = btrfs_extent_inline_ref_size(type);
1484 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1485 ptr = (unsigned long)iref;
1486 end = (unsigned long)ei + item_size;
1487 if (ptr + size < end)
1488 memmove_extent_buffer(leaf, ptr, ptr + size,
1489 end - ptr - size);
1490 item_size -= size;
1491 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1492 BUG_ON(ret);
1493 }
1494 btrfs_mark_buffer_dirty(leaf);
1495 return 0;
1496}
1497
1498static noinline_for_stack
1499int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1500 struct btrfs_root *root,
1501 struct btrfs_path *path,
1502 u64 bytenr, u64 num_bytes, u64 parent,
1503 u64 root_objectid, u64 owner,
1504 u64 offset, int refs_to_add,
1505 struct btrfs_delayed_extent_op *extent_op)
1506{
1507 struct btrfs_extent_inline_ref *iref;
1508 int ret;
1509
1510 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1511 bytenr, num_bytes, parent,
1512 root_objectid, owner, offset, 1);
1513 if (ret == 0) {
1514 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1515 ret = update_inline_extent_backref(trans, root, path, iref,
1516 refs_to_add, extent_op);
1517 } else if (ret == -ENOENT) {
1518 ret = setup_inline_extent_backref(trans, root, path, iref,
1519 parent, root_objectid,
1520 owner, offset, refs_to_add,
1521 extent_op);
1522 }
1523 return ret;
1524}
1525
1526static int insert_extent_backref(struct btrfs_trans_handle *trans,
1527 struct btrfs_root *root,
1528 struct btrfs_path *path,
1529 u64 bytenr, u64 parent, u64 root_objectid,
1530 u64 owner, u64 offset, int refs_to_add)
1531{
1532 int ret;
1533 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1534 BUG_ON(refs_to_add != 1);
1535 ret = insert_tree_block_ref(trans, root, path, bytenr,
1536 parent, root_objectid);
1537 } else {
1538 ret = insert_extent_data_ref(trans, root, path, bytenr,
1539 parent, root_objectid,
1540 owner, offset, refs_to_add);
1541 }
1542 return ret;
1543}
1544
1545static int remove_extent_backref(struct btrfs_trans_handle *trans,
1546 struct btrfs_root *root,
1547 struct btrfs_path *path,
1548 struct btrfs_extent_inline_ref *iref,
1549 int refs_to_drop, int is_data)
1550{
1551 int ret;
1552
1553 BUG_ON(!is_data && refs_to_drop != 1);
1554 if (iref) {
1555 ret = update_inline_extent_backref(trans, root, path, iref,
1556 -refs_to_drop, NULL);
1557 } else if (is_data) {
1558 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1559 } else {
1560 ret = btrfs_del_item(trans, root, path);
1561 }
1562 return ret;
1563}
1564
Chris Mason4b4e25f2008-11-20 10:22:27 -05001565#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001566static void btrfs_issue_discard(struct block_device *bdev,
1567 u64 start, u64 len)
1568{
Chris Mason15916de2008-11-19 21:17:22 -05001569 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -05001570}
Chris Mason4b4e25f2008-11-20 10:22:27 -05001571#endif
Chris Mason15916de2008-11-19 21:17:22 -05001572
Liu Hui1f3c79a2009-01-05 15:57:51 -05001573static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1574 u64 num_bytes)
1575{
1576#ifdef BIO_RW_DISCARD
1577 int ret;
1578 u64 map_length = num_bytes;
1579 struct btrfs_multi_bio *multi = NULL;
1580
1581 /* Tell the block device(s) that the sectors can be discarded */
1582 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1583 bytenr, &map_length, &multi, 0);
1584 if (!ret) {
1585 struct btrfs_bio_stripe *stripe = multi->stripes;
1586 int i;
1587
1588 if (map_length > num_bytes)
1589 map_length = num_bytes;
1590
1591 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1592 btrfs_issue_discard(stripe->dev->bdev,
1593 stripe->physical,
1594 map_length);
1595 }
1596 kfree(multi);
1597 }
1598
1599 return ret;
1600#else
1601 return 0;
1602#endif
1603}
1604
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001605int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1606 struct btrfs_root *root,
1607 u64 bytenr, u64 num_bytes, u64 parent,
1608 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001609{
1610 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001611 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1612 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001613
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001614 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1615 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1616 parent, root_objectid, (int)owner,
1617 BTRFS_ADD_DELAYED_REF, NULL);
1618 } else {
1619 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1620 parent, root_objectid, owner, offset,
1621 BTRFS_ADD_DELAYED_REF, NULL);
1622 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001623 return ret;
1624}
1625
Chris Mason925baed2008-06-25 16:01:30 -04001626static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001627 struct btrfs_root *root,
1628 u64 bytenr, u64 num_bytes,
1629 u64 parent, u64 root_objectid,
1630 u64 owner, u64 offset, int refs_to_add,
1631 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001632{
Chris Mason5caf2a02007-04-02 11:20:42 -04001633 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001634 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001635 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001636 u64 refs;
1637 int ret;
1638 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001639
Chris Mason5caf2a02007-04-02 11:20:42 -04001640 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001641 if (!path)
1642 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001643
Chris Mason3c12ac72008-04-21 12:01:38 -04001644 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001645 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001646 /* this will setup the path even if it fails to insert the back ref */
1647 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1648 path, bytenr, num_bytes, parent,
1649 root_objectid, owner, offset,
1650 refs_to_add, extent_op);
1651 if (ret == 0)
1652 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001653
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001654 if (ret != -EAGAIN) {
1655 err = ret;
1656 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001657 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001658
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001659 leaf = path->nodes[0];
1660 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1661 refs = btrfs_extent_refs(leaf, item);
1662 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1663 if (extent_op)
1664 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001665
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001666 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001667 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001668
Chris Mason3c12ac72008-04-21 12:01:38 -04001669 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001670 path->leave_spinning = 1;
1671
Chris Mason56bec292009-03-13 10:10:06 -04001672 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001673 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001674 path, bytenr, parent, root_objectid,
1675 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001676 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001677out:
Chris Mason74493f72007-12-11 09:25:06 -05001678 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001679 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001680}
1681
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001682static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1683 struct btrfs_root *root,
1684 struct btrfs_delayed_ref_node *node,
1685 struct btrfs_delayed_extent_op *extent_op,
1686 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001687{
Chris Mason56bec292009-03-13 10:10:06 -04001688 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001689 struct btrfs_delayed_data_ref *ref;
1690 struct btrfs_key ins;
1691 u64 parent = 0;
1692 u64 ref_root = 0;
1693 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001694
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001695 ins.objectid = node->bytenr;
1696 ins.offset = node->num_bytes;
1697 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001698
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001699 ref = btrfs_delayed_node_to_data_ref(node);
1700 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1701 parent = ref->parent;
1702 else
1703 ref_root = ref->root;
1704
1705 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1706 if (extent_op) {
1707 BUG_ON(extent_op->update_key);
1708 flags |= extent_op->flags_to_set;
1709 }
1710 ret = alloc_reserved_file_extent(trans, root,
1711 parent, ref_root, flags,
1712 ref->objectid, ref->offset,
1713 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001714 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1715 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1716 node->num_bytes, parent,
1717 ref_root, ref->objectid,
1718 ref->offset, node->ref_mod,
1719 extent_op);
1720 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1721 ret = __btrfs_free_extent(trans, root, node->bytenr,
1722 node->num_bytes, parent,
1723 ref_root, ref->objectid,
1724 ref->offset, node->ref_mod,
1725 extent_op);
1726 } else {
1727 BUG();
1728 }
Chris Mason56bec292009-03-13 10:10:06 -04001729 return ret;
1730}
1731
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001732static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1733 struct extent_buffer *leaf,
1734 struct btrfs_extent_item *ei)
1735{
1736 u64 flags = btrfs_extent_flags(leaf, ei);
1737 if (extent_op->update_flags) {
1738 flags |= extent_op->flags_to_set;
1739 btrfs_set_extent_flags(leaf, ei, flags);
1740 }
1741
1742 if (extent_op->update_key) {
1743 struct btrfs_tree_block_info *bi;
1744 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1745 bi = (struct btrfs_tree_block_info *)(ei + 1);
1746 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1747 }
1748}
1749
1750static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1751 struct btrfs_root *root,
1752 struct btrfs_delayed_ref_node *node,
1753 struct btrfs_delayed_extent_op *extent_op)
1754{
1755 struct btrfs_key key;
1756 struct btrfs_path *path;
1757 struct btrfs_extent_item *ei;
1758 struct extent_buffer *leaf;
1759 u32 item_size;
1760 int ret;
1761 int err = 0;
1762
1763 path = btrfs_alloc_path();
1764 if (!path)
1765 return -ENOMEM;
1766
1767 key.objectid = node->bytenr;
1768 key.type = BTRFS_EXTENT_ITEM_KEY;
1769 key.offset = node->num_bytes;
1770
1771 path->reada = 1;
1772 path->leave_spinning = 1;
1773 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1774 path, 0, 1);
1775 if (ret < 0) {
1776 err = ret;
1777 goto out;
1778 }
1779 if (ret > 0) {
1780 err = -EIO;
1781 goto out;
1782 }
1783
1784 leaf = path->nodes[0];
1785 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1786#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1787 if (item_size < sizeof(*ei)) {
1788 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1789 path, (u64)-1, 0);
1790 if (ret < 0) {
1791 err = ret;
1792 goto out;
1793 }
1794 leaf = path->nodes[0];
1795 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1796 }
1797#endif
1798 BUG_ON(item_size < sizeof(*ei));
1799 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1800 __run_delayed_extent_op(extent_op, leaf, ei);
1801
1802 btrfs_mark_buffer_dirty(leaf);
1803out:
1804 btrfs_free_path(path);
1805 return err;
1806}
1807
1808static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1809 struct btrfs_root *root,
1810 struct btrfs_delayed_ref_node *node,
1811 struct btrfs_delayed_extent_op *extent_op,
1812 int insert_reserved)
1813{
1814 int ret = 0;
1815 struct btrfs_delayed_tree_ref *ref;
1816 struct btrfs_key ins;
1817 u64 parent = 0;
1818 u64 ref_root = 0;
1819
1820 ins.objectid = node->bytenr;
1821 ins.offset = node->num_bytes;
1822 ins.type = BTRFS_EXTENT_ITEM_KEY;
1823
1824 ref = btrfs_delayed_node_to_tree_ref(node);
1825 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1826 parent = ref->parent;
1827 else
1828 ref_root = ref->root;
1829
1830 BUG_ON(node->ref_mod != 1);
1831 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1832 BUG_ON(!extent_op || !extent_op->update_flags ||
1833 !extent_op->update_key);
1834 ret = alloc_reserved_tree_block(trans, root,
1835 parent, ref_root,
1836 extent_op->flags_to_set,
1837 &extent_op->key,
1838 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001839 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1840 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1841 node->num_bytes, parent, ref_root,
1842 ref->level, 0, 1, extent_op);
1843 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1844 ret = __btrfs_free_extent(trans, root, node->bytenr,
1845 node->num_bytes, parent, ref_root,
1846 ref->level, 0, 1, extent_op);
1847 } else {
1848 BUG();
1849 }
1850 return ret;
1851}
1852
1853
Chris Mason56bec292009-03-13 10:10:06 -04001854/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001855static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1856 struct btrfs_root *root,
1857 struct btrfs_delayed_ref_node *node,
1858 struct btrfs_delayed_extent_op *extent_op,
1859 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001860{
Josef Bacikeb099672009-02-12 09:27:38 -05001861 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001862 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001863 struct btrfs_delayed_ref_head *head;
1864 /*
1865 * we've hit the end of the chain and we were supposed
1866 * to insert this extent into the tree. But, it got
1867 * deleted before we ever needed to insert it, so all
1868 * we have to do is clean up the accounting
1869 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 BUG_ON(extent_op);
1871 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001872 if (insert_reserved) {
Yan Zheng11833d62009-09-11 16:11:19 -04001873 int mark_free = 0;
1874 struct extent_buffer *must_clean = NULL;
1875
1876 ret = pin_down_bytes(trans, root, NULL,
1877 node->bytenr, node->num_bytes,
1878 head->is_data, 1, &must_clean);
1879 if (ret > 0)
1880 mark_free = 1;
1881
1882 if (must_clean) {
1883 clean_tree_block(NULL, root, must_clean);
1884 btrfs_tree_unlock(must_clean);
1885 free_extent_buffer(must_clean);
1886 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001887 if (head->is_data) {
1888 ret = btrfs_del_csums(trans, root,
1889 node->bytenr,
1890 node->num_bytes);
1891 BUG_ON(ret);
1892 }
Yan Zheng11833d62009-09-11 16:11:19 -04001893 if (mark_free) {
1894 ret = btrfs_free_reserved_extent(root,
1895 node->bytenr,
1896 node->num_bytes);
1897 BUG_ON(ret);
1898 }
Chris Mason56bec292009-03-13 10:10:06 -04001899 }
Chris Mason56bec292009-03-13 10:10:06 -04001900 mutex_unlock(&head->mutex);
1901 return 0;
1902 }
Josef Bacikeb099672009-02-12 09:27:38 -05001903
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001904 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1905 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1906 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1907 insert_reserved);
1908 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1909 node->type == BTRFS_SHARED_DATA_REF_KEY)
1910 ret = run_delayed_data_ref(trans, root, node, extent_op,
1911 insert_reserved);
1912 else
1913 BUG();
1914 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001915}
1916
Chris Mason56bec292009-03-13 10:10:06 -04001917static noinline struct btrfs_delayed_ref_node *
1918select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001919{
Chris Mason56bec292009-03-13 10:10:06 -04001920 struct rb_node *node;
1921 struct btrfs_delayed_ref_node *ref;
1922 int action = BTRFS_ADD_DELAYED_REF;
1923again:
1924 /*
1925 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1926 * this prevents ref count from going down to zero when
1927 * there still are pending delayed ref.
1928 */
1929 node = rb_prev(&head->node.rb_node);
1930 while (1) {
1931 if (!node)
1932 break;
1933 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1934 rb_node);
1935 if (ref->bytenr != head->node.bytenr)
1936 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001937 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001938 return ref;
1939 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001940 }
Chris Mason56bec292009-03-13 10:10:06 -04001941 if (action == BTRFS_ADD_DELAYED_REF) {
1942 action = BTRFS_DROP_DELAYED_REF;
1943 goto again;
1944 }
1945 return NULL;
1946}
1947
Chris Masonc3e69d52009-03-13 10:17:05 -04001948static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1949 struct btrfs_root *root,
1950 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001951{
Chris Mason56bec292009-03-13 10:10:06 -04001952 struct btrfs_delayed_ref_root *delayed_refs;
1953 struct btrfs_delayed_ref_node *ref;
1954 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001955 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001956 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001957 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001958 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001959
1960 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001961 while (1) {
1962 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001963 /* pick a new head ref from the cluster list */
1964 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001965 break;
Chris Mason56bec292009-03-13 10:10:06 -04001966
Chris Masonc3e69d52009-03-13 10:17:05 -04001967 locked_ref = list_entry(cluster->next,
1968 struct btrfs_delayed_ref_head, cluster);
1969
1970 /* grab the lock that says we are going to process
1971 * all the refs for this head */
1972 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1973
1974 /*
1975 * we may have dropped the spin lock to get the head
1976 * mutex lock, and that might have given someone else
1977 * time to free the head. If that's true, it has been
1978 * removed from our list and we can move on.
1979 */
1980 if (ret == -EAGAIN) {
1981 locked_ref = NULL;
1982 count++;
1983 continue;
Chris Mason56bec292009-03-13 10:10:06 -04001984 }
1985 }
1986
1987 /*
1988 * record the must insert reserved flag before we
1989 * drop the spin lock.
1990 */
1991 must_insert_reserved = locked_ref->must_insert_reserved;
1992 locked_ref->must_insert_reserved = 0;
1993
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001994 extent_op = locked_ref->extent_op;
1995 locked_ref->extent_op = NULL;
1996
Chris Mason56bec292009-03-13 10:10:06 -04001997 /*
1998 * locked_ref is the head node, so we have to go one
1999 * node back for any delayed ref updates
2000 */
Chris Mason56bec292009-03-13 10:10:06 -04002001 ref = select_delayed_ref(locked_ref);
2002 if (!ref) {
2003 /* All delayed refs have been processed, Go ahead
2004 * and send the head node to run_one_delayed_ref,
2005 * so that any accounting fixes can happen
2006 */
2007 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002008
2009 if (extent_op && must_insert_reserved) {
2010 kfree(extent_op);
2011 extent_op = NULL;
2012 }
2013
2014 if (extent_op) {
2015 spin_unlock(&delayed_refs->lock);
2016
2017 ret = run_delayed_extent_op(trans, root,
2018 ref, extent_op);
2019 BUG_ON(ret);
2020 kfree(extent_op);
2021
2022 cond_resched();
2023 spin_lock(&delayed_refs->lock);
2024 continue;
2025 }
2026
Chris Masonc3e69d52009-03-13 10:17:05 -04002027 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002028 locked_ref = NULL;
2029 }
2030
2031 ref->in_tree = 0;
2032 rb_erase(&ref->rb_node, &delayed_refs->root);
2033 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002034
Chris Mason56bec292009-03-13 10:10:06 -04002035 spin_unlock(&delayed_refs->lock);
2036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002037 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002038 must_insert_reserved);
2039 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002040
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002041 btrfs_put_delayed_ref(ref);
2042 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002043 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002044
Chris Mason1887be62009-03-13 10:11:24 -04002045 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002046 spin_lock(&delayed_refs->lock);
2047 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002048 return count;
2049}
2050
2051/*
2052 * this starts processing the delayed reference count updates and
2053 * extent insertions we have queued up so far. count can be
2054 * 0, which means to process everything in the tree at the start
2055 * of the run (but not newly added entries), or it can be some target
2056 * number you'd like to process.
2057 */
2058int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2059 struct btrfs_root *root, unsigned long count)
2060{
2061 struct rb_node *node;
2062 struct btrfs_delayed_ref_root *delayed_refs;
2063 struct btrfs_delayed_ref_node *ref;
2064 struct list_head cluster;
2065 int ret;
2066 int run_all = count == (unsigned long)-1;
2067 int run_most = 0;
2068
2069 if (root == root->fs_info->extent_root)
2070 root = root->fs_info->tree_root;
2071
2072 delayed_refs = &trans->transaction->delayed_refs;
2073 INIT_LIST_HEAD(&cluster);
2074again:
2075 spin_lock(&delayed_refs->lock);
2076 if (count == 0) {
2077 count = delayed_refs->num_entries * 2;
2078 run_most = 1;
2079 }
2080 while (1) {
2081 if (!(run_all || run_most) &&
2082 delayed_refs->num_heads_ready < 64)
2083 break;
2084
2085 /*
2086 * go find something we can process in the rbtree. We start at
2087 * the beginning of the tree, and then build a cluster
2088 * of refs to process starting at the first one we are able to
2089 * lock
2090 */
2091 ret = btrfs_find_ref_cluster(trans, &cluster,
2092 delayed_refs->run_delayed_start);
2093 if (ret)
2094 break;
2095
2096 ret = run_clustered_refs(trans, root, &cluster);
2097 BUG_ON(ret < 0);
2098
2099 count -= min_t(unsigned long, ret, count);
2100
2101 if (count == 0)
2102 break;
2103 }
2104
Chris Mason56bec292009-03-13 10:10:06 -04002105 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002106 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002107 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002108 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002109 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002110
2111 while (node) {
2112 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2113 rb_node);
2114 if (btrfs_delayed_ref_is_head(ref)) {
2115 struct btrfs_delayed_ref_head *head;
2116
2117 head = btrfs_delayed_node_to_head(ref);
2118 atomic_inc(&ref->refs);
2119
2120 spin_unlock(&delayed_refs->lock);
2121 mutex_lock(&head->mutex);
2122 mutex_unlock(&head->mutex);
2123
2124 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002125 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002126 goto again;
2127 }
2128 node = rb_next(node);
2129 }
2130 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002131 schedule_timeout(1);
2132 goto again;
2133 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002134out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002135 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002136 return 0;
2137}
2138
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002139int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2140 struct btrfs_root *root,
2141 u64 bytenr, u64 num_bytes, u64 flags,
2142 int is_data)
2143{
2144 struct btrfs_delayed_extent_op *extent_op;
2145 int ret;
2146
2147 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2148 if (!extent_op)
2149 return -ENOMEM;
2150
2151 extent_op->flags_to_set = flags;
2152 extent_op->update_flags = 1;
2153 extent_op->update_key = 0;
2154 extent_op->is_data = is_data ? 1 : 0;
2155
2156 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2157 if (ret)
2158 kfree(extent_op);
2159 return ret;
2160}
2161
2162static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2163 struct btrfs_root *root,
2164 struct btrfs_path *path,
2165 u64 objectid, u64 offset, u64 bytenr)
2166{
2167 struct btrfs_delayed_ref_head *head;
2168 struct btrfs_delayed_ref_node *ref;
2169 struct btrfs_delayed_data_ref *data_ref;
2170 struct btrfs_delayed_ref_root *delayed_refs;
2171 struct rb_node *node;
2172 int ret = 0;
2173
2174 ret = -ENOENT;
2175 delayed_refs = &trans->transaction->delayed_refs;
2176 spin_lock(&delayed_refs->lock);
2177 head = btrfs_find_delayed_ref_head(trans, bytenr);
2178 if (!head)
2179 goto out;
2180
2181 if (!mutex_trylock(&head->mutex)) {
2182 atomic_inc(&head->node.refs);
2183 spin_unlock(&delayed_refs->lock);
2184
2185 btrfs_release_path(root->fs_info->extent_root, path);
2186
2187 mutex_lock(&head->mutex);
2188 mutex_unlock(&head->mutex);
2189 btrfs_put_delayed_ref(&head->node);
2190 return -EAGAIN;
2191 }
2192
2193 node = rb_prev(&head->node.rb_node);
2194 if (!node)
2195 goto out_unlock;
2196
2197 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2198
2199 if (ref->bytenr != bytenr)
2200 goto out_unlock;
2201
2202 ret = 1;
2203 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2204 goto out_unlock;
2205
2206 data_ref = btrfs_delayed_node_to_data_ref(ref);
2207
2208 node = rb_prev(node);
2209 if (node) {
2210 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2211 if (ref->bytenr == bytenr)
2212 goto out_unlock;
2213 }
2214
2215 if (data_ref->root != root->root_key.objectid ||
2216 data_ref->objectid != objectid || data_ref->offset != offset)
2217 goto out_unlock;
2218
2219 ret = 0;
2220out_unlock:
2221 mutex_unlock(&head->mutex);
2222out:
2223 spin_unlock(&delayed_refs->lock);
2224 return ret;
2225}
2226
2227static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2228 struct btrfs_root *root,
2229 struct btrfs_path *path,
2230 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002231{
2232 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002233 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002234 struct btrfs_extent_data_ref *ref;
2235 struct btrfs_extent_inline_ref *iref;
2236 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002237 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002238 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002239 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002240
Chris Masonbe20aa92007-12-17 20:14:01 -05002241 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002242 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002243 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002244
Chris Masonbe20aa92007-12-17 20:14:01 -05002245 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2246 if (ret < 0)
2247 goto out;
2248 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002249
2250 ret = -ENOENT;
2251 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002252 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002253
Zheng Yan31840ae2008-09-23 13:14:14 -04002254 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002255 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002256 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002257
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002258 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002259 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002260
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002261 ret = 1;
2262 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2263#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2264 if (item_size < sizeof(*ei)) {
2265 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2266 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002267 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002268#endif
2269 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2270
2271 if (item_size != sizeof(*ei) +
2272 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2273 goto out;
2274
2275 if (btrfs_extent_generation(leaf, ei) <=
2276 btrfs_root_last_snapshot(&root->root_item))
2277 goto out;
2278
2279 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2280 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2281 BTRFS_EXTENT_DATA_REF_KEY)
2282 goto out;
2283
2284 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2285 if (btrfs_extent_refs(leaf, ei) !=
2286 btrfs_extent_data_ref_count(leaf, ref) ||
2287 btrfs_extent_data_ref_root(leaf, ref) !=
2288 root->root_key.objectid ||
2289 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2290 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2291 goto out;
2292
Yan Zhengf321e492008-07-30 09:26:11 -04002293 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002294out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002295 return ret;
2296}
2297
2298int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2299 struct btrfs_root *root,
2300 u64 objectid, u64 offset, u64 bytenr)
2301{
2302 struct btrfs_path *path;
2303 int ret;
2304 int ret2;
2305
2306 path = btrfs_alloc_path();
2307 if (!path)
2308 return -ENOENT;
2309
2310 do {
2311 ret = check_committed_ref(trans, root, path, objectid,
2312 offset, bytenr);
2313 if (ret && ret != -ENOENT)
2314 goto out;
2315
2316 ret2 = check_delayed_ref(trans, root, path, objectid,
2317 offset, bytenr);
2318 } while (ret2 == -EAGAIN);
2319
2320 if (ret2 && ret2 != -ENOENT) {
2321 ret = ret2;
2322 goto out;
2323 }
2324
2325 if (ret != -ENOENT || ret2 != -ENOENT)
2326 ret = 0;
2327out:
Yan Zhengf321e492008-07-30 09:26:11 -04002328 btrfs_free_path(path);
2329 return ret;
2330}
2331
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002332#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002333int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2334 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002335{
Chris Mason5f39d392007-10-15 16:14:19 -04002336 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002337 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002338 u64 root_gen;
2339 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002340 int i;
Chris Masondb945352007-10-15 16:15:53 -04002341 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002342 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002343 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002344
Chris Mason3768f362007-03-13 16:47:54 -04002345 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002346 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002347
Zheng Yane4657682008-09-26 10:04:53 -04002348 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2349 shared = 0;
2350 root_gen = root->root_key.offset;
2351 } else {
2352 shared = 1;
2353 root_gen = trans->transid - 1;
2354 }
2355
Chris Masondb945352007-10-15 16:15:53 -04002356 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002357 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002358
Zheng Yan31840ae2008-09-23 13:14:14 -04002359 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002360 struct btrfs_leaf_ref *ref;
2361 struct btrfs_extent_info *info;
2362
Zheng Yan31840ae2008-09-23 13:14:14 -04002363 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002364 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002365 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002366 goto out;
2367 }
2368
Zheng Yane4657682008-09-26 10:04:53 -04002369 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002370 ref->bytenr = buf->start;
2371 ref->owner = btrfs_header_owner(buf);
2372 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002373 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002374 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002375
Zheng Yan31840ae2008-09-23 13:14:14 -04002376 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002377 u64 disk_bytenr;
2378 btrfs_item_key_to_cpu(buf, &key, i);
2379 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2380 continue;
2381 fi = btrfs_item_ptr(buf, i,
2382 struct btrfs_file_extent_item);
2383 if (btrfs_file_extent_type(buf, fi) ==
2384 BTRFS_FILE_EXTENT_INLINE)
2385 continue;
2386 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2387 if (disk_bytenr == 0)
2388 continue;
2389
2390 info->bytenr = disk_bytenr;
2391 info->num_bytes =
2392 btrfs_file_extent_disk_num_bytes(buf, fi);
2393 info->objectid = key.objectid;
2394 info->offset = key.offset;
2395 info++;
2396 }
2397
Zheng Yane4657682008-09-26 10:04:53 -04002398 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002399 if (ret == -EEXIST && shared) {
2400 struct btrfs_leaf_ref *old;
2401 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2402 BUG_ON(!old);
2403 btrfs_remove_leaf_ref(root, old);
2404 btrfs_free_leaf_ref(root, old);
2405 ret = btrfs_add_leaf_ref(root, ref, shared);
2406 }
Yan Zheng31153d82008-07-28 15:32:19 -04002407 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002408 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002409 }
2410out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002411 return ret;
2412}
2413
Chris Masonb7a9f292009-02-04 09:23:45 -05002414/* when a block goes through cow, we update the reference counts of
2415 * everything that block points to. The internal pointers of the block
2416 * can be in just about any order, and it is likely to have clusters of
2417 * things that are close together and clusters of things that are not.
2418 *
2419 * To help reduce the seeks that come with updating all of these reference
2420 * counts, sort them by byte number before actual updates are done.
2421 *
2422 * struct refsort is used to match byte number to slot in the btree block.
2423 * we sort based on the byte number and then use the slot to actually
2424 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002425 *
2426 * struct refsort is smaller than strcut btrfs_item and smaller than
2427 * struct btrfs_key_ptr. Since we're currently limited to the page size
2428 * for a btree block, there's no way for a kmalloc of refsorts for a
2429 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002430 */
2431struct refsort {
2432 u64 bytenr;
2433 u32 slot;
2434};
2435
2436/*
2437 * for passing into sort()
2438 */
2439static int refsort_cmp(const void *a_void, const void *b_void)
2440{
2441 const struct refsort *a = a_void;
2442 const struct refsort *b = b_void;
2443
2444 if (a->bytenr < b->bytenr)
2445 return -1;
2446 if (a->bytenr > b->bytenr)
2447 return 1;
2448 return 0;
2449}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002450#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002451
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002452static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002453 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002454 struct extent_buffer *buf,
2455 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002456{
2457 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002458 u64 num_bytes;
2459 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002460 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002461 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002462 struct btrfs_key key;
2463 struct btrfs_file_extent_item *fi;
2464 int i;
2465 int level;
2466 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002467 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002468 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002469
2470 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002471 nritems = btrfs_header_nritems(buf);
2472 level = btrfs_header_level(buf);
2473
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002474 if (!root->ref_cows && level == 0)
2475 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002476
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002477 if (inc)
2478 process_func = btrfs_inc_extent_ref;
2479 else
2480 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002481
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002482 if (full_backref)
2483 parent = buf->start;
2484 else
2485 parent = 0;
2486
Zheng Yan31840ae2008-09-23 13:14:14 -04002487 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002488 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002489 btrfs_item_key_to_cpu(buf, &key, i);
2490 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002491 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002492 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002493 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002494 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002495 BTRFS_FILE_EXTENT_INLINE)
2496 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002497 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2498 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002499 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002500
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002501 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2502 key.offset -= btrfs_file_extent_offset(buf, fi);
2503 ret = process_func(trans, root, bytenr, num_bytes,
2504 parent, ref_root, key.objectid,
2505 key.offset);
2506 if (ret)
2507 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002508 } else {
2509 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002510 num_bytes = btrfs_level_size(root, level - 1);
2511 ret = process_func(trans, root, bytenr, num_bytes,
2512 parent, ref_root, level - 1, 0);
2513 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002514 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002515 }
2516 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002517 return 0;
2518fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002519 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002520 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002521}
2522
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002523int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2524 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002525{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002526 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2527}
Zheng Yan31840ae2008-09-23 13:14:14 -04002528
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002529int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2530 struct extent_buffer *buf, int full_backref)
2531{
2532 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002533}
2534
Chris Mason9078a3e2007-04-26 16:46:15 -04002535static int write_one_cache_group(struct btrfs_trans_handle *trans,
2536 struct btrfs_root *root,
2537 struct btrfs_path *path,
2538 struct btrfs_block_group_cache *cache)
2539{
2540 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002541 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002542 unsigned long bi;
2543 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002544
Chris Mason9078a3e2007-04-26 16:46:15 -04002545 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002546 if (ret < 0)
2547 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002548 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002549
2550 leaf = path->nodes[0];
2551 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2552 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2553 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002554 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002555fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002556 if (ret)
2557 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002558 return 0;
2559
2560}
2561
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002562static struct btrfs_block_group_cache *
2563next_block_group(struct btrfs_root *root,
2564 struct btrfs_block_group_cache *cache)
2565{
2566 struct rb_node *node;
2567 spin_lock(&root->fs_info->block_group_cache_lock);
2568 node = rb_next(&cache->cache_node);
2569 btrfs_put_block_group(cache);
2570 if (node) {
2571 cache = rb_entry(node, struct btrfs_block_group_cache,
2572 cache_node);
2573 atomic_inc(&cache->count);
2574 } else
2575 cache = NULL;
2576 spin_unlock(&root->fs_info->block_group_cache_lock);
2577 return cache;
2578}
2579
Chris Mason96b51792007-10-15 16:15:19 -04002580int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2581 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002582{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002583 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002584 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002585 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002586 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002587
2588 path = btrfs_alloc_path();
2589 if (!path)
2590 return -ENOMEM;
2591
Chris Masond3977122009-01-05 21:25:51 -05002592 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002593 if (last == 0) {
2594 err = btrfs_run_delayed_refs(trans, root,
2595 (unsigned long)-1);
2596 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002597 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002598
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002599 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2600 while (cache) {
2601 if (cache->dirty)
2602 break;
2603 cache = next_block_group(root, cache);
2604 }
2605 if (!cache) {
2606 if (last == 0)
2607 break;
2608 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002609 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002610 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002611
2612 cache->dirty = 0;
2613 last = cache->key.objectid + cache->key.offset;
2614
2615 err = write_one_cache_group(trans, root, path, cache);
2616 BUG_ON(err);
2617 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002618 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002619
Chris Mason9078a3e2007-04-26 16:46:15 -04002620 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002621 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002622}
2623
Yan Zhengd2fb3432008-12-11 16:30:39 -05002624int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2625{
2626 struct btrfs_block_group_cache *block_group;
2627 int readonly = 0;
2628
2629 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2630 if (!block_group || block_group->ro)
2631 readonly = 1;
2632 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002633 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002634 return readonly;
2635}
2636
Chris Mason593060d2008-03-25 16:50:33 -04002637static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2638 u64 total_bytes, u64 bytes_used,
2639 struct btrfs_space_info **space_info)
2640{
2641 struct btrfs_space_info *found;
2642
2643 found = __find_space_info(info, flags);
2644 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002645 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002646 found->total_bytes += total_bytes;
2647 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002648 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002649 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002650 *space_info = found;
2651 return 0;
2652 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002653 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002654 if (!found)
2655 return -ENOMEM;
2656
Josef Bacik0f9dd462008-09-23 13:14:11 -04002657 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002658 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002659 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002660 found->flags = flags;
2661 found->total_bytes = total_bytes;
2662 found->bytes_used = bytes_used;
2663 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002664 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002665 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002666 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002667 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002668 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002669 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002670 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002671 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002672 return 0;
2673}
2674
Chris Mason8790d502008-04-03 16:29:03 -04002675static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2676{
2677 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002678 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002679 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002680 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002681 if (extra_flags) {
2682 if (flags & BTRFS_BLOCK_GROUP_DATA)
2683 fs_info->avail_data_alloc_bits |= extra_flags;
2684 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2685 fs_info->avail_metadata_alloc_bits |= extra_flags;
2686 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2687 fs_info->avail_system_alloc_bits |= extra_flags;
2688 }
2689}
Chris Mason593060d2008-03-25 16:50:33 -04002690
Yan Zhengc146afa2008-11-12 14:34:12 -05002691static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2692{
2693 spin_lock(&cache->space_info->lock);
2694 spin_lock(&cache->lock);
2695 if (!cache->ro) {
2696 cache->space_info->bytes_readonly += cache->key.offset -
2697 btrfs_block_group_used(&cache->item);
2698 cache->ro = 1;
2699 }
2700 spin_unlock(&cache->lock);
2701 spin_unlock(&cache->space_info->lock);
2702}
2703
Yan Zheng2b820322008-11-17 21:11:30 -05002704u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002705{
Yan Zheng2b820322008-11-17 21:11:30 -05002706 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002707
2708 if (num_devices == 1)
2709 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2710 if (num_devices < 4)
2711 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2712
Chris Masonec44a352008-04-28 15:29:52 -04002713 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2714 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002715 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002716 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002717 }
Chris Masonec44a352008-04-28 15:29:52 -04002718
2719 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002720 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002721 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002722 }
Chris Masonec44a352008-04-28 15:29:52 -04002723
2724 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2725 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2726 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2727 (flags & BTRFS_BLOCK_GROUP_DUP)))
2728 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2729 return flags;
2730}
2731
Josef Bacik6a632092009-02-20 11:00:09 -05002732static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2733{
2734 struct btrfs_fs_info *info = root->fs_info;
2735 u64 alloc_profile;
2736
2737 if (data) {
2738 alloc_profile = info->avail_data_alloc_bits &
2739 info->data_alloc_profile;
2740 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2741 } else if (root == root->fs_info->chunk_root) {
2742 alloc_profile = info->avail_system_alloc_bits &
2743 info->system_alloc_profile;
2744 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2745 } else {
2746 alloc_profile = info->avail_metadata_alloc_bits &
2747 info->metadata_alloc_profile;
2748 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2749 }
2750
2751 return btrfs_reduce_alloc_profile(root, data);
2752}
2753
2754void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2755{
2756 u64 alloc_target;
2757
2758 alloc_target = btrfs_get_alloc_profile(root, 1);
2759 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2760 alloc_target);
2761}
2762
2763/*
2764 * for now this just makes sure we have at least 5% of our metadata space free
2765 * for use.
2766 */
2767int btrfs_check_metadata_free_space(struct btrfs_root *root)
2768{
2769 struct btrfs_fs_info *info = root->fs_info;
2770 struct btrfs_space_info *meta_sinfo;
2771 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002772 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002773
2774 /* get the space info for where the metadata will live */
2775 alloc_target = btrfs_get_alloc_profile(root, 0);
2776 meta_sinfo = __find_space_info(info, alloc_target);
2777
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002778again:
Josef Bacik6a632092009-02-20 11:00:09 -05002779 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002780 if (!meta_sinfo->full)
2781 thresh = meta_sinfo->total_bytes * 80;
2782 else
2783 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05002784
2785 do_div(thresh, 100);
2786
2787 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2788 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002789 struct btrfs_trans_handle *trans;
2790 if (!meta_sinfo->full) {
2791 meta_sinfo->force_alloc = 1;
2792 spin_unlock(&meta_sinfo->lock);
2793
2794 trans = btrfs_start_transaction(root, 1);
2795 if (!trans)
2796 return -ENOMEM;
2797
2798 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2799 2 * 1024 * 1024, alloc_target, 0);
2800 btrfs_end_transaction(trans, root);
2801 goto again;
2802 }
Josef Bacik6a632092009-02-20 11:00:09 -05002803 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002804
2805 if (!committed) {
2806 committed = 1;
2807 trans = btrfs_join_transaction(root, 1);
2808 if (!trans)
2809 return -ENOMEM;
2810 ret = btrfs_commit_transaction(trans, root);
2811 if (ret)
2812 return ret;
2813 goto again;
2814 }
Josef Bacik6a632092009-02-20 11:00:09 -05002815 return -ENOSPC;
2816 }
2817 spin_unlock(&meta_sinfo->lock);
2818
2819 return 0;
2820}
2821
2822/*
2823 * This will check the space that the inode allocates from to make sure we have
2824 * enough space for bytes.
2825 */
2826int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
2827 u64 bytes)
2828{
2829 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002830 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002831
2832 /* make sure bytes are sectorsize aligned */
2833 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2834
2835 data_sinfo = BTRFS_I(inode)->space_info;
2836again:
2837 /* make sure we have enough space to handle the data first */
2838 spin_lock(&data_sinfo->lock);
2839 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
2840 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
2841 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
2842 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002843 struct btrfs_trans_handle *trans;
2844
Josef Bacik6a632092009-02-20 11:00:09 -05002845 /*
2846 * if we don't have enough free bytes in this space then we need
2847 * to alloc a new chunk.
2848 */
2849 if (!data_sinfo->full) {
2850 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05002851
2852 data_sinfo->force_alloc = 1;
2853 spin_unlock(&data_sinfo->lock);
2854
2855 alloc_target = btrfs_get_alloc_profile(root, 1);
2856 trans = btrfs_start_transaction(root, 1);
2857 if (!trans)
2858 return -ENOMEM;
2859
2860 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2861 bytes + 2 * 1024 * 1024,
2862 alloc_target, 0);
2863 btrfs_end_transaction(trans, root);
2864 if (ret)
2865 return ret;
2866 goto again;
2867 }
2868 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002869
2870 /* commit the current transaction and try again */
2871 if (!committed) {
2872 committed = 1;
2873 trans = btrfs_join_transaction(root, 1);
2874 if (!trans)
2875 return -ENOMEM;
2876 ret = btrfs_commit_transaction(trans, root);
2877 if (ret)
2878 return ret;
2879 goto again;
2880 }
2881
Josef Bacik6a632092009-02-20 11:00:09 -05002882 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2883 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04002884 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07002885 "%llu total\n", (unsigned long long)bytes,
2886 (unsigned long long)data_sinfo->bytes_delalloc,
2887 (unsigned long long)data_sinfo->bytes_used,
2888 (unsigned long long)data_sinfo->bytes_reserved,
2889 (unsigned long long)data_sinfo->bytes_pinned,
2890 (unsigned long long)data_sinfo->bytes_readonly,
2891 (unsigned long long)data_sinfo->bytes_may_use,
2892 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05002893 return -ENOSPC;
2894 }
2895 data_sinfo->bytes_may_use += bytes;
2896 BTRFS_I(inode)->reserved_bytes += bytes;
2897 spin_unlock(&data_sinfo->lock);
2898
2899 return btrfs_check_metadata_free_space(root);
2900}
2901
2902/*
2903 * if there was an error for whatever reason after calling
2904 * btrfs_check_data_free_space, call this so we can cleanup the counters.
2905 */
2906void btrfs_free_reserved_data_space(struct btrfs_root *root,
2907 struct inode *inode, u64 bytes)
2908{
2909 struct btrfs_space_info *data_sinfo;
2910
2911 /* make sure bytes are sectorsize aligned */
2912 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2913
2914 data_sinfo = BTRFS_I(inode)->space_info;
2915 spin_lock(&data_sinfo->lock);
2916 data_sinfo->bytes_may_use -= bytes;
2917 BTRFS_I(inode)->reserved_bytes -= bytes;
2918 spin_unlock(&data_sinfo->lock);
2919}
2920
2921/* called when we are adding a delalloc extent to the inode's io_tree */
2922void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
2923 u64 bytes)
2924{
2925 struct btrfs_space_info *data_sinfo;
2926
2927 /* get the space info for where this inode will be storing its data */
2928 data_sinfo = BTRFS_I(inode)->space_info;
2929
2930 /* make sure we have enough space to handle the data first */
2931 spin_lock(&data_sinfo->lock);
2932 data_sinfo->bytes_delalloc += bytes;
2933
2934 /*
2935 * we are adding a delalloc extent without calling
2936 * btrfs_check_data_free_space first. This happens on a weird
2937 * writepage condition, but shouldn't hurt our accounting
2938 */
2939 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
2940 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
2941 BTRFS_I(inode)->reserved_bytes = 0;
2942 } else {
2943 data_sinfo->bytes_may_use -= bytes;
2944 BTRFS_I(inode)->reserved_bytes -= bytes;
2945 }
2946
2947 spin_unlock(&data_sinfo->lock);
2948}
2949
2950/* called when we are clearing an delalloc extent from the inode's io_tree */
2951void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2952 u64 bytes)
2953{
2954 struct btrfs_space_info *info;
2955
2956 info = BTRFS_I(inode)->space_info;
2957
2958 spin_lock(&info->lock);
2959 info->bytes_delalloc -= bytes;
2960 spin_unlock(&info->lock);
2961}
2962
Josef Bacik97e728d2009-04-21 17:40:57 -04002963static void force_metadata_allocation(struct btrfs_fs_info *info)
2964{
2965 struct list_head *head = &info->space_info;
2966 struct btrfs_space_info *found;
2967
2968 rcu_read_lock();
2969 list_for_each_entry_rcu(found, head, list) {
2970 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
2971 found->force_alloc = 1;
2972 }
2973 rcu_read_unlock();
2974}
2975
Chris Mason6324fbf2008-03-24 15:01:59 -04002976static int do_chunk_alloc(struct btrfs_trans_handle *trans,
2977 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04002978 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04002979{
2980 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04002981 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04002982 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05002983 int ret = 0;
2984
Josef Bacik97e728d2009-04-21 17:40:57 -04002985 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04002986
Yan Zheng2b820322008-11-17 21:11:30 -05002987 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04002988
Chris Mason6324fbf2008-03-24 15:01:59 -04002989 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04002990 if (!space_info) {
2991 ret = update_space_info(extent_root->fs_info, flags,
2992 0, 0, &space_info);
2993 BUG_ON(ret);
2994 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002995 BUG_ON(!space_info);
2996
Josef Bacik25179202008-10-29 14:49:05 -04002997 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04002998 if (space_info->force_alloc) {
2999 force = 1;
3000 space_info->force_alloc = 0;
3001 }
Josef Bacik25179202008-10-29 14:49:05 -04003002 if (space_info->full) {
3003 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003004 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003005 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003006
Yan Zhengc146afa2008-11-12 14:34:12 -05003007 thresh = space_info->total_bytes - space_info->bytes_readonly;
3008 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04003009 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04003010 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04003011 space_info->bytes_reserved + alloc_bytes) < thresh) {
3012 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003013 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003014 }
Josef Bacik25179202008-10-29 14:49:05 -04003015 spin_unlock(&space_info->lock);
3016
Josef Bacik97e728d2009-04-21 17:40:57 -04003017 /*
3018 * if we're doing a data chunk, go ahead and make sure that
3019 * we keep a reasonable number of metadata chunks allocated in the
3020 * FS as well.
3021 */
3022 if (flags & BTRFS_BLOCK_GROUP_DATA) {
3023 fs_info->data_chunk_allocations++;
3024 if (!(fs_info->data_chunk_allocations %
3025 fs_info->metadata_ratio))
3026 force_metadata_allocation(fs_info);
3027 }
3028
Yan Zheng2b820322008-11-17 21:11:30 -05003029 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05003030 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003031 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04003032out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003033 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003034 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003035}
3036
Chris Mason9078a3e2007-04-26 16:46:15 -04003037static int update_block_group(struct btrfs_trans_handle *trans,
3038 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04003039 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04003040 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04003041{
3042 struct btrfs_block_group_cache *cache;
3043 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04003044 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003045 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003046 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04003047
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003048 /* block accounting for super block */
3049 spin_lock(&info->delalloc_lock);
3050 old_val = btrfs_super_bytes_used(&info->super_copy);
3051 if (alloc)
3052 old_val += num_bytes;
3053 else
3054 old_val -= num_bytes;
3055 btrfs_set_super_bytes_used(&info->super_copy, old_val);
3056
3057 /* block accounting for root item */
3058 old_val = btrfs_root_used(&root->root_item);
3059 if (alloc)
3060 old_val += num_bytes;
3061 else
3062 old_val -= num_bytes;
3063 btrfs_set_root_used(&root->root_item, old_val);
3064 spin_unlock(&info->delalloc_lock);
3065
Chris Masond3977122009-01-05 21:25:51 -05003066 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003067 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003068 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04003069 return -1;
Chris Masondb945352007-10-15 16:15:53 -04003070 byte_in_group = bytenr - cache->key.objectid;
3071 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003072
Josef Bacik25179202008-10-29 14:49:05 -04003073 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003074 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003075 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04003076 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04003077 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04003078 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04003079 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003080 btrfs_set_block_group_used(&cache->item, old_val);
3081 cache->reserved -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003082 cache->space_info->bytes_used += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003083 cache->space_info->bytes_reserved -= num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003084 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05003085 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003086 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003087 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003088 } else {
Chris Masondb945352007-10-15 16:15:53 -04003089 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003090 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05003091 if (cache->ro)
3092 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003093 btrfs_set_block_group_used(&cache->item, old_val);
3094 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003095 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04003096 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003097 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003098
3099 ret = btrfs_discard_extent(root, bytenr,
3100 num_bytes);
3101 WARN_ON(ret);
3102
Josef Bacik0f9dd462008-09-23 13:14:11 -04003103 ret = btrfs_add_free_space(cache, bytenr,
3104 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003105 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04003106 }
Chris Masoncd1bc462007-04-27 10:08:34 -04003107 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003108 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003109 total -= num_bytes;
3110 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003111 }
3112 return 0;
3113}
Chris Mason6324fbf2008-03-24 15:01:59 -04003114
Chris Masona061fc82008-05-07 11:43:44 -04003115static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3116{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003117 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003118 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003119
3120 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3121 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003122 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003123
Yan Zhengd2fb3432008-12-11 16:30:39 -05003124 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003125 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003126
3127 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003128}
3129
Yan Zheng11833d62009-09-11 16:11:19 -04003130/*
3131 * this function must be called within transaction
3132 */
3133int btrfs_pin_extent(struct btrfs_root *root,
3134 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05003135{
Yan324ae4d2007-11-16 14:57:08 -05003136 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -04003137 struct btrfs_block_group_cache *cache;
Yan324ae4d2007-11-16 14:57:08 -05003138
Yan Zheng11833d62009-09-11 16:11:19 -04003139 cache = btrfs_lookup_block_group(fs_info, bytenr);
3140 BUG_ON(!cache);
Chris Masonb9473432009-03-13 11:00:37 -04003141
Yan Zheng11833d62009-09-11 16:11:19 -04003142 spin_lock(&cache->space_info->lock);
3143 spin_lock(&cache->lock);
3144 cache->pinned += num_bytes;
3145 cache->space_info->bytes_pinned += num_bytes;
3146 if (reserved) {
3147 cache->reserved -= num_bytes;
3148 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05003149 }
Yan Zheng11833d62009-09-11 16:11:19 -04003150 spin_unlock(&cache->lock);
3151 spin_unlock(&cache->space_info->lock);
3152
3153 btrfs_put_block_group(cache);
3154
3155 set_extent_dirty(fs_info->pinned_extents,
3156 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Yan324ae4d2007-11-16 14:57:08 -05003157 return 0;
3158}
Chris Mason9078a3e2007-04-26 16:46:15 -04003159
Yan Zheng11833d62009-09-11 16:11:19 -04003160static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3161 u64 num_bytes, int reserve)
Zheng Yane8569812008-09-26 10:05:48 -04003162{
Yan Zheng11833d62009-09-11 16:11:19 -04003163 spin_lock(&cache->space_info->lock);
3164 spin_lock(&cache->lock);
3165 if (reserve) {
3166 cache->reserved += num_bytes;
3167 cache->space_info->bytes_reserved += num_bytes;
3168 } else {
3169 cache->reserved -= num_bytes;
3170 cache->space_info->bytes_reserved -= num_bytes;
3171 }
3172 spin_unlock(&cache->lock);
3173 spin_unlock(&cache->space_info->lock);
3174 return 0;
3175}
Zheng Yane8569812008-09-26 10:05:48 -04003176
Yan Zheng11833d62009-09-11 16:11:19 -04003177int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3178 struct btrfs_root *root)
3179{
3180 struct btrfs_fs_info *fs_info = root->fs_info;
3181 struct btrfs_caching_control *next;
3182 struct btrfs_caching_control *caching_ctl;
3183 struct btrfs_block_group_cache *cache;
3184
3185 down_write(&fs_info->extent_commit_sem);
3186
3187 list_for_each_entry_safe(caching_ctl, next,
3188 &fs_info->caching_block_groups, list) {
3189 cache = caching_ctl->block_group;
3190 if (block_group_cache_done(cache)) {
3191 cache->last_byte_to_unpin = (u64)-1;
3192 list_del_init(&caching_ctl->list);
3193 put_caching_control(caching_ctl);
3194 } else {
3195 cache->last_byte_to_unpin = caching_ctl->progress;
3196 }
3197 }
3198
3199 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3200 fs_info->pinned_extents = &fs_info->freed_extents[1];
3201 else
3202 fs_info->pinned_extents = &fs_info->freed_extents[0];
3203
3204 up_write(&fs_info->extent_commit_sem);
3205 return 0;
3206}
3207
3208static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3209{
3210 struct btrfs_fs_info *fs_info = root->fs_info;
3211 struct btrfs_block_group_cache *cache = NULL;
3212 u64 len;
3213
3214 while (start <= end) {
3215 if (!cache ||
3216 start >= cache->key.objectid + cache->key.offset) {
3217 if (cache)
3218 btrfs_put_block_group(cache);
3219 cache = btrfs_lookup_block_group(fs_info, start);
3220 BUG_ON(!cache);
3221 }
3222
3223 len = cache->key.objectid + cache->key.offset - start;
3224 len = min(len, end + 1 - start);
3225
3226 if (start < cache->last_byte_to_unpin) {
3227 len = min(len, cache->last_byte_to_unpin - start);
3228 btrfs_add_free_space(cache, start, len);
3229 }
Josef Bacik25179202008-10-29 14:49:05 -04003230
3231 spin_lock(&cache->space_info->lock);
3232 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003233 cache->pinned -= len;
3234 cache->space_info->bytes_pinned -= len;
Josef Bacik25179202008-10-29 14:49:05 -04003235 spin_unlock(&cache->lock);
3236 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003237
3238 start += len;
3239 }
3240
3241 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003242 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04003243 return 0;
3244}
3245
3246int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04003247 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05003248{
Yan Zheng11833d62009-09-11 16:11:19 -04003249 struct btrfs_fs_info *fs_info = root->fs_info;
3250 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04003251 u64 start;
3252 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003253 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003254
Yan Zheng11833d62009-09-11 16:11:19 -04003255 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3256 unpin = &fs_info->freed_extents[1];
3257 else
3258 unpin = &fs_info->freed_extents[0];
3259
Chris Masond3977122009-01-05 21:25:51 -05003260 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003261 ret = find_first_extent_bit(unpin, 0, &start, &end,
3262 EXTENT_DIRTY);
3263 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003264 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003265
3266 ret = btrfs_discard_extent(root, start, end + 1 - start);
3267
Chris Mason1a5bc162007-10-15 16:15:26 -04003268 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04003269 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04003270 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003271 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003272
Liu Hui1f3c79a2009-01-05 15:57:51 -05003273 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003274}
3275
Zheng Yan31840ae2008-09-23 13:14:14 -04003276static int pin_down_bytes(struct btrfs_trans_handle *trans,
3277 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003278 struct btrfs_path *path,
Yan Zheng11833d62009-09-11 16:11:19 -04003279 u64 bytenr, u64 num_bytes,
3280 int is_data, int reserved,
Chris Masonb9473432009-03-13 11:00:37 -04003281 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003282{
Chris Mason1a5bc162007-10-15 16:15:26 -04003283 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003284 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003285
Zheng Yan31840ae2008-09-23 13:14:14 -04003286 if (is_data)
3287 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003288
Zheng Yan31840ae2008-09-23 13:14:14 -04003289 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3290 if (!buf)
3291 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003292
Zheng Yan31840ae2008-09-23 13:14:14 -04003293 /* we can reuse a block if it hasn't been written
3294 * and it is from this transaction. We can't
3295 * reuse anything from the tree log root because
3296 * it has tiny sub-transactions.
3297 */
3298 if (btrfs_buffer_uptodate(buf, 0) &&
3299 btrfs_try_tree_lock(buf)) {
3300 u64 header_owner = btrfs_header_owner(buf);
3301 u64 header_transid = btrfs_header_generation(buf);
3302 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3303 header_transid == trans->transid &&
3304 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003305 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003306 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003307 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003308 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003309 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003310 free_extent_buffer(buf);
3311pinit:
Yan Zheng11833d62009-09-11 16:11:19 -04003312 if (path)
3313 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003314 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003315 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
Zheng Yan31840ae2008-09-23 13:14:14 -04003316
Chris Masonbe744172007-05-06 10:15:01 -04003317 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003318 return 0;
3319}
3320
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003321static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3322 struct btrfs_root *root,
3323 u64 bytenr, u64 num_bytes, u64 parent,
3324 u64 root_objectid, u64 owner_objectid,
3325 u64 owner_offset, int refs_to_drop,
3326 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003327{
Chris Masone2fa7222007-03-12 16:22:34 -04003328 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003329 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003330 struct btrfs_fs_info *info = root->fs_info;
3331 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003332 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003333 struct btrfs_extent_item *ei;
3334 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003335 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003336 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003337 int extent_slot = 0;
3338 int found_extent = 0;
3339 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003340 u32 item_size;
3341 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003342
Chris Mason5caf2a02007-04-02 11:20:42 -04003343 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003344 if (!path)
3345 return -ENOMEM;
3346
Chris Mason3c12ac72008-04-21 12:01:38 -04003347 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003348 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003349
3350 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3351 BUG_ON(!is_data && refs_to_drop != 1);
3352
3353 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3354 bytenr, num_bytes, parent,
3355 root_objectid, owner_objectid,
3356 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003357 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003358 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003359 while (extent_slot >= 0) {
3360 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003361 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003362 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003363 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003364 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3365 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003366 found_extent = 1;
3367 break;
3368 }
3369 if (path->slots[0] - extent_slot > 5)
3370 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003371 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003372 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003373#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3374 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3375 if (found_extent && item_size < sizeof(*ei))
3376 found_extent = 0;
3377#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003378 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003379 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003380 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003381 NULL, refs_to_drop,
3382 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003383 BUG_ON(ret);
3384 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003385 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003386
3387 key.objectid = bytenr;
3388 key.type = BTRFS_EXTENT_ITEM_KEY;
3389 key.offset = num_bytes;
3390
Zheng Yan31840ae2008-09-23 13:14:14 -04003391 ret = btrfs_search_slot(trans, extent_root,
3392 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003393 if (ret) {
3394 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003395 ", was looking for %llu\n", ret,
3396 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003397 btrfs_print_leaf(extent_root, path->nodes[0]);
3398 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003399 BUG_ON(ret);
3400 extent_slot = path->slots[0];
3401 }
Chris Mason7bb86312007-12-11 09:25:06 -05003402 } else {
3403 btrfs_print_leaf(extent_root, path->nodes[0]);
3404 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003405 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003406 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003407 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003408 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003409 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003410 (unsigned long long)owner_objectid,
3411 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003412 }
Chris Mason5f39d392007-10-15 16:14:19 -04003413
3414 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003415 item_size = btrfs_item_size_nr(leaf, extent_slot);
3416#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3417 if (item_size < sizeof(*ei)) {
3418 BUG_ON(found_extent || extent_slot != path->slots[0]);
3419 ret = convert_extent_item_v0(trans, extent_root, path,
3420 owner_objectid, 0);
3421 BUG_ON(ret < 0);
3422
3423 btrfs_release_path(extent_root, path);
3424 path->leave_spinning = 1;
3425
3426 key.objectid = bytenr;
3427 key.type = BTRFS_EXTENT_ITEM_KEY;
3428 key.offset = num_bytes;
3429
3430 ret = btrfs_search_slot(trans, extent_root, &key, path,
3431 -1, 1);
3432 if (ret) {
3433 printk(KERN_ERR "umm, got %d back from search"
3434 ", was looking for %llu\n", ret,
3435 (unsigned long long)bytenr);
3436 btrfs_print_leaf(extent_root, path->nodes[0]);
3437 }
3438 BUG_ON(ret);
3439 extent_slot = path->slots[0];
3440 leaf = path->nodes[0];
3441 item_size = btrfs_item_size_nr(leaf, extent_slot);
3442 }
3443#endif
3444 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003445 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003446 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003447 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3448 struct btrfs_tree_block_info *bi;
3449 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3450 bi = (struct btrfs_tree_block_info *)(ei + 1);
3451 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003452 }
3453
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003454 refs = btrfs_extent_refs(leaf, ei);
3455 BUG_ON(refs < refs_to_drop);
3456 refs -= refs_to_drop;
3457
3458 if (refs > 0) {
3459 if (extent_op)
3460 __run_delayed_extent_op(extent_op, leaf, ei);
3461 /*
3462 * In the case of inline back ref, reference count will
3463 * be updated by remove_extent_backref
3464 */
3465 if (iref) {
3466 BUG_ON(!found_extent);
3467 } else {
3468 btrfs_set_extent_refs(leaf, ei, refs);
3469 btrfs_mark_buffer_dirty(leaf);
3470 }
3471 if (found_extent) {
3472 ret = remove_extent_backref(trans, extent_root, path,
3473 iref, refs_to_drop,
3474 is_data);
3475 BUG_ON(ret);
3476 }
3477 } else {
3478 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003479 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003480
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003481 if (found_extent) {
3482 BUG_ON(is_data && refs_to_drop !=
3483 extent_data_ref_count(root, path, iref));
3484 if (iref) {
3485 BUG_ON(path->slots[0] != extent_slot);
3486 } else {
3487 BUG_ON(path->slots[0] != extent_slot + 1);
3488 path->slots[0] = extent_slot;
3489 num_to_del = 2;
3490 }
Chris Mason78fae272007-03-25 11:35:08 -04003491 }
Chris Masonb9473432009-03-13 11:00:37 -04003492
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003493 ret = pin_down_bytes(trans, root, path, bytenr,
Yan Zheng11833d62009-09-11 16:11:19 -04003494 num_bytes, is_data, 0, &must_clean);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003495 if (ret > 0)
3496 mark_free = 1;
3497 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003498 /*
3499 * it is going to be very rare for someone to be waiting
3500 * on the block we're freeing. del_items might need to
3501 * schedule, so rather than get fancy, just force it
3502 * to blocking here
3503 */
3504 if (must_clean)
3505 btrfs_set_lock_blocking(must_clean);
3506
Chris Mason952fcca2008-02-18 16:33:44 -05003507 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3508 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003509 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003510 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003511
Chris Masonb9473432009-03-13 11:00:37 -04003512 if (must_clean) {
3513 clean_tree_block(NULL, root, must_clean);
3514 btrfs_tree_unlock(must_clean);
3515 free_extent_buffer(must_clean);
3516 }
3517
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003518 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003519 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3520 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003521 } else {
3522 invalidate_mapping_pages(info->btree_inode->i_mapping,
3523 bytenr >> PAGE_CACHE_SHIFT,
3524 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003525 }
3526
Chris Masondcbdd4d2008-12-16 13:51:01 -05003527 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3528 mark_free);
3529 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003530 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003531 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003532 return ret;
3533}
3534
3535/*
Chris Mason1887be62009-03-13 10:11:24 -04003536 * when we free an extent, it is possible (and likely) that we free the last
3537 * delayed ref for that extent as well. This searches the delayed ref tree for
3538 * a given extent, and if there are no other delayed refs to be processed, it
3539 * removes it from the tree.
3540 */
3541static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3542 struct btrfs_root *root, u64 bytenr)
3543{
3544 struct btrfs_delayed_ref_head *head;
3545 struct btrfs_delayed_ref_root *delayed_refs;
3546 struct btrfs_delayed_ref_node *ref;
3547 struct rb_node *node;
3548 int ret;
3549
3550 delayed_refs = &trans->transaction->delayed_refs;
3551 spin_lock(&delayed_refs->lock);
3552 head = btrfs_find_delayed_ref_head(trans, bytenr);
3553 if (!head)
3554 goto out;
3555
3556 node = rb_prev(&head->node.rb_node);
3557 if (!node)
3558 goto out;
3559
3560 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3561
3562 /* there are still entries for this ref, we can't drop it */
3563 if (ref->bytenr == bytenr)
3564 goto out;
3565
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003566 if (head->extent_op) {
3567 if (!head->must_insert_reserved)
3568 goto out;
3569 kfree(head->extent_op);
3570 head->extent_op = NULL;
3571 }
3572
Chris Mason1887be62009-03-13 10:11:24 -04003573 /*
3574 * waiting for the lock here would deadlock. If someone else has it
3575 * locked they are already in the process of dropping it anyway
3576 */
3577 if (!mutex_trylock(&head->mutex))
3578 goto out;
3579
3580 /*
3581 * at this point we have a head with no other entries. Go
3582 * ahead and process it.
3583 */
3584 head->node.in_tree = 0;
3585 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04003586
Chris Mason1887be62009-03-13 10:11:24 -04003587 delayed_refs->num_entries--;
3588
3589 /*
3590 * we don't take a ref on the node because we're removing it from the
3591 * tree, so we just steal the ref the tree was holding.
3592 */
Chris Masonc3e69d52009-03-13 10:17:05 -04003593 delayed_refs->num_heads--;
3594 if (list_empty(&head->cluster))
3595 delayed_refs->num_heads_ready--;
3596
3597 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04003598 spin_unlock(&delayed_refs->lock);
3599
3600 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003601 &head->node, head->extent_op,
3602 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04003603 BUG_ON(ret);
3604 btrfs_put_delayed_ref(&head->node);
3605 return 0;
3606out:
3607 spin_unlock(&delayed_refs->lock);
3608 return 0;
3609}
3610
Chris Mason925baed2008-06-25 16:01:30 -04003611int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003612 struct btrfs_root *root,
3613 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003614 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04003615{
3616 int ret;
3617
Chris Mason56bec292009-03-13 10:10:06 -04003618 /*
3619 * tree log blocks never actually go into the extent allocation
3620 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003621 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003622 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
3623 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04003624 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003625 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04003626 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003627 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
3628 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
3629 parent, root_objectid, (int)owner,
3630 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04003631 BUG_ON(ret);
3632 ret = check_ref_cleanup(trans, root, bytenr);
3633 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003634 } else {
3635 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
3636 parent, root_objectid, owner,
3637 offset, BTRFS_DROP_DELAYED_REF, NULL);
3638 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04003639 }
Chris Mason925baed2008-06-25 16:01:30 -04003640 return ret;
3641}
3642
Chris Mason87ee04e2007-11-30 11:30:34 -05003643static u64 stripe_align(struct btrfs_root *root, u64 val)
3644{
3645 u64 mask = ((u64)root->stripesize - 1);
3646 u64 ret = (val + mask) & ~mask;
3647 return ret;
3648}
3649
Chris Masonfec577f2007-02-26 10:40:21 -05003650/*
Josef Bacik817d52f2009-07-13 21:29:25 -04003651 * when we wait for progress in the block group caching, its because
3652 * our allocation attempt failed at least once. So, we must sleep
3653 * and let some progress happen before we try again.
3654 *
3655 * This function will sleep at least once waiting for new free space to
3656 * show up, and then it will check the block group free space numbers
3657 * for our min num_bytes. Another option is to have it go ahead
3658 * and look in the rbtree for a free extent of a given size, but this
3659 * is a good start.
3660 */
3661static noinline int
3662wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
3663 u64 num_bytes)
3664{
Yan Zheng11833d62009-09-11 16:11:19 -04003665 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04003666 DEFINE_WAIT(wait);
3667
Yan Zheng11833d62009-09-11 16:11:19 -04003668 caching_ctl = get_caching_control(cache);
3669 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04003670 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04003671
Yan Zheng11833d62009-09-11 16:11:19 -04003672 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04003673 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04003674
3675 put_caching_control(caching_ctl);
3676 return 0;
3677}
3678
3679static noinline int
3680wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
3681{
3682 struct btrfs_caching_control *caching_ctl;
3683 DEFINE_WAIT(wait);
3684
3685 caching_ctl = get_caching_control(cache);
3686 if (!caching_ctl)
3687 return 0;
3688
3689 wait_event(caching_ctl->wait, block_group_cache_done(cache));
3690
3691 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04003692 return 0;
3693}
3694
3695enum btrfs_loop_type {
3696 LOOP_CACHED_ONLY = 0,
3697 LOOP_CACHING_NOWAIT = 1,
3698 LOOP_CACHING_WAIT = 2,
3699 LOOP_ALLOC_CHUNK = 3,
3700 LOOP_NO_EMPTY_SIZE = 4,
3701};
3702
3703/*
Chris Masonfec577f2007-02-26 10:40:21 -05003704 * walks the btree of allocated extents and find a hole of a given size.
3705 * The key ins is changed to record the hole:
3706 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04003707 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05003708 * ins->offset == number of blocks
3709 * Any available blocks before search_start are skipped.
3710 */
Chris Masond3977122009-01-05 21:25:51 -05003711static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003712 struct btrfs_root *orig_root,
3713 u64 num_bytes, u64 empty_size,
3714 u64 search_start, u64 search_end,
3715 u64 hint_byte, struct btrfs_key *ins,
3716 u64 exclude_start, u64 exclude_nr,
3717 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05003718{
Josef Bacik80eb2342008-10-29 14:49:05 -04003719 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003720 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003721 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04003722 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04003723 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04003724 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003725 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003726 int last_ptr_loop = 0;
3727 int loop = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04003728 bool found_uncached_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05003729
Chris Masondb945352007-10-15 16:15:53 -04003730 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04003731 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04003732 ins->objectid = 0;
3733 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003734
Josef Bacik2552d172009-04-03 10:14:19 -04003735 space_info = __find_space_info(root->fs_info, data);
3736
Chris Mason0ef3e662008-05-24 14:04:53 -04003737 if (orig_root->ref_cows || empty_size)
3738 allowed_chunk_alloc = 1;
3739
Chris Mason239b14b2008-03-24 15:02:07 -04003740 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003741 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05003742 if (!btrfs_test_opt(root, SSD))
3743 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04003744 }
3745
Chris Masonfa9c0d72009-04-03 09:47:43 -04003746 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
3747 last_ptr = &root->fs_info->data_alloc_cluster;
3748 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003749
Chris Mason239b14b2008-03-24 15:02:07 -04003750 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003751 spin_lock(&last_ptr->lock);
3752 if (last_ptr->block_group)
3753 hint_byte = last_ptr->window_start;
3754 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003755 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003756
Chris Masona061fc82008-05-07 11:43:44 -04003757 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04003758 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04003759
Josef Bacik817d52f2009-07-13 21:29:25 -04003760 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003761 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003762
Josef Bacik2552d172009-04-03 10:14:19 -04003763 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04003764 block_group = btrfs_lookup_block_group(root->fs_info,
3765 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04003766 /*
3767 * we don't want to use the block group if it doesn't match our
3768 * allocation bits, or if its not cached.
3769 */
3770 if (block_group && block_group_bits(block_group, data) &&
3771 block_group_cache_done(block_group)) {
Josef Bacik2552d172009-04-03 10:14:19 -04003772 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003773 if (list_empty(&block_group->list) ||
3774 block_group->ro) {
3775 /*
3776 * someone is removing this block group,
3777 * we can't jump into the have_block_group
3778 * target because our list pointers are not
3779 * valid
3780 */
3781 btrfs_put_block_group(block_group);
3782 up_read(&space_info->groups_sem);
3783 } else
3784 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003785 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003786 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003787 }
Chris Mason42e70e72008-11-07 18:17:11 -05003788 }
Chris Mason43662112008-11-07 09:06:11 -05003789
Josef Bacik2552d172009-04-03 10:14:19 -04003790search:
Josef Bacik80eb2342008-10-29 14:49:05 -04003791 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04003792 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04003793 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04003794 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05003795
Josef Bacik2552d172009-04-03 10:14:19 -04003796 atomic_inc(&block_group->count);
3797 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05003798
Josef Bacik2552d172009-04-03 10:14:19 -04003799have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04003800 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
3801 /*
3802 * we want to start caching kthreads, but not too many
3803 * right off the bat so we don't overwhelm the system,
3804 * so only start them if there are less than 2 and we're
3805 * in the initial allocation phase.
3806 */
3807 if (loop > LOOP_CACHING_NOWAIT ||
3808 atomic_read(&space_info->caching_threads) < 2) {
3809 ret = cache_block_group(block_group);
3810 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04003811 }
Josef Bacikea6a4782008-11-20 12:16:16 -05003812 }
3813
Josef Bacik817d52f2009-07-13 21:29:25 -04003814 cached = block_group_cache_done(block_group);
3815 if (unlikely(!cached)) {
3816 found_uncached_bg = true;
3817
3818 /* if we only want cached bgs, loop */
3819 if (loop == LOOP_CACHED_ONLY)
3820 goto loop;
3821 }
3822
Josef Bacikea6a4782008-11-20 12:16:16 -05003823 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04003824 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003825
Chris Masonfa9c0d72009-04-03 09:47:43 -04003826 if (last_ptr) {
3827 /*
3828 * the refill lock keeps out other
3829 * people trying to start a new cluster
3830 */
3831 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003832 if (last_ptr->block_group &&
3833 (last_ptr->block_group->ro ||
3834 !block_group_bits(last_ptr->block_group, data))) {
3835 offset = 0;
3836 goto refill_cluster;
3837 }
3838
Chris Masonfa9c0d72009-04-03 09:47:43 -04003839 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
3840 num_bytes, search_start);
3841 if (offset) {
3842 /* we have a block, we're done */
3843 spin_unlock(&last_ptr->refill_lock);
3844 goto checks;
3845 }
3846
3847 spin_lock(&last_ptr->lock);
3848 /*
3849 * whoops, this cluster doesn't actually point to
3850 * this block group. Get a ref on the block
3851 * group is does point to and try again
3852 */
3853 if (!last_ptr_loop && last_ptr->block_group &&
3854 last_ptr->block_group != block_group) {
3855
3856 btrfs_put_block_group(block_group);
3857 block_group = last_ptr->block_group;
3858 atomic_inc(&block_group->count);
3859 spin_unlock(&last_ptr->lock);
3860 spin_unlock(&last_ptr->refill_lock);
3861
3862 last_ptr_loop = 1;
3863 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04003864 /*
3865 * we know this block group is properly
3866 * in the list because
3867 * btrfs_remove_block_group, drops the
3868 * cluster before it removes the block
3869 * group from the list
3870 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04003871 goto have_block_group;
3872 }
3873 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003874refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04003875 /*
3876 * this cluster didn't work out, free it and
3877 * start over
3878 */
3879 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3880
3881 last_ptr_loop = 0;
3882
3883 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04003884 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04003885 block_group, last_ptr,
3886 offset, num_bytes,
3887 empty_cluster + empty_size);
3888 if (ret == 0) {
3889 /*
3890 * now pull our allocation out of this
3891 * cluster
3892 */
3893 offset = btrfs_alloc_from_cluster(block_group,
3894 last_ptr, num_bytes,
3895 search_start);
3896 if (offset) {
3897 /* we found one, proceed */
3898 spin_unlock(&last_ptr->refill_lock);
3899 goto checks;
3900 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003901 } else if (!cached && loop > LOOP_CACHING_NOWAIT) {
3902 spin_unlock(&last_ptr->refill_lock);
3903
3904 wait_block_group_cache_progress(block_group,
3905 num_bytes + empty_cluster + empty_size);
3906 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003907 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003908
Chris Masonfa9c0d72009-04-03 09:47:43 -04003909 /*
3910 * at this point we either didn't find a cluster
3911 * or we weren't able to allocate a block from our
3912 * cluster. Free the cluster we've been trying
3913 * to use, and go to the next block group
3914 */
Josef Bacik817d52f2009-07-13 21:29:25 -04003915 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04003916 btrfs_return_cluster_to_free_space(NULL,
3917 last_ptr);
3918 spin_unlock(&last_ptr->refill_lock);
3919 goto loop;
3920 }
3921 spin_unlock(&last_ptr->refill_lock);
3922 }
3923
Josef Bacik6226cb02009-04-03 10:14:18 -04003924 offset = btrfs_find_space_for_alloc(block_group, search_start,
3925 num_bytes, empty_size);
Josef Bacik817d52f2009-07-13 21:29:25 -04003926 if (!offset && (cached || (!cached &&
3927 loop == LOOP_CACHING_NOWAIT))) {
Josef Bacik2552d172009-04-03 10:14:19 -04003928 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04003929 } else if (!offset && (!cached &&
3930 loop > LOOP_CACHING_NOWAIT)) {
3931 wait_block_group_cache_progress(block_group,
3932 num_bytes + empty_size);
3933 goto have_block_group;
3934 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003935checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04003936 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04003937 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04003938 if (search_start + num_bytes >= search_end) {
3939 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003940 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04003941 }
Chris Masone37c9e62007-05-09 20:13:14 -04003942
Josef Bacik2552d172009-04-03 10:14:19 -04003943 /* move on to the next group */
3944 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04003945 block_group->key.objectid + block_group->key.offset) {
3946 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003947 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04003948 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003949
Josef Bacik2552d172009-04-03 10:14:19 -04003950 if (exclude_nr > 0 &&
3951 (search_start + num_bytes > exclude_start &&
3952 search_start < exclude_start + exclude_nr)) {
3953 search_start = exclude_start + exclude_nr;
3954
Josef Bacik6226cb02009-04-03 10:14:18 -04003955 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003956 /*
3957 * if search_start is still in this block group
3958 * then we just re-search this block group
3959 */
3960 if (search_start >= block_group->key.objectid &&
3961 search_start < (block_group->key.objectid +
Josef Bacik6226cb02009-04-03 10:14:18 -04003962 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04003963 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003964 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003965 }
Josef Bacik2552d172009-04-03 10:14:19 -04003966
3967 ins->objectid = search_start;
3968 ins->offset = num_bytes;
3969
Josef Bacik6226cb02009-04-03 10:14:18 -04003970 if (offset < search_start)
3971 btrfs_add_free_space(block_group, offset,
3972 search_start - offset);
3973 BUG_ON(offset > search_start);
3974
Yan Zheng11833d62009-09-11 16:11:19 -04003975 update_reserved_extents(block_group, num_bytes, 1);
3976
Josef Bacik2552d172009-04-03 10:14:19 -04003977 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04003978 break;
3979loop:
Chris Masonfa9c0d72009-04-03 09:47:43 -04003980 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003981 }
3982 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05003983
Josef Bacik817d52f2009-07-13 21:29:25 -04003984 /* LOOP_CACHED_ONLY, only search fully cached block groups
3985 * LOOP_CACHING_NOWAIT, search partially cached block groups, but
3986 * dont wait foR them to finish caching
3987 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3988 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3989 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3990 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04003991 */
Josef Bacik817d52f2009-07-13 21:29:25 -04003992 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
3993 (found_uncached_bg || empty_size || empty_cluster ||
3994 allowed_chunk_alloc)) {
3995 if (found_uncached_bg) {
3996 found_uncached_bg = false;
3997 if (loop < LOOP_CACHING_WAIT) {
3998 loop++;
3999 goto search;
4000 }
4001 }
4002
4003 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004004 empty_size = 0;
4005 empty_cluster = 0;
4006 }
Chris Mason42e70e72008-11-07 18:17:11 -05004007
Josef Bacik2552d172009-04-03 10:14:19 -04004008 if (allowed_chunk_alloc) {
4009 ret = do_chunk_alloc(trans, root, num_bytes +
4010 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04004011 allowed_chunk_alloc = 0;
4012 } else {
4013 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004014 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004015
Josef Bacik817d52f2009-07-13 21:29:25 -04004016 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004017 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04004018 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004019 }
Josef Bacik2552d172009-04-03 10:14:19 -04004020 ret = -ENOSPC;
4021 } else if (!ins->objectid) {
4022 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04004023 }
Chris Mason0b86a832008-03-24 15:01:56 -04004024
Josef Bacik80eb2342008-10-29 14:49:05 -04004025 /* we found what we needed */
4026 if (ins->objectid) {
4027 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05004028 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04004029
Chris Masonfa9c0d72009-04-03 09:47:43 -04004030 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004031 ret = 0;
4032 }
Chris Mason0b86a832008-03-24 15:01:56 -04004033
Chris Mason0f70abe2007-02-28 16:46:22 -05004034 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004035}
Chris Masonec44a352008-04-28 15:29:52 -04004036
Josef Bacik0f9dd462008-09-23 13:14:11 -04004037static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
4038{
4039 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004040
Chris Masond3977122009-01-05 21:25:51 -05004041 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4042 (unsigned long long)(info->total_bytes - info->bytes_used -
4043 info->bytes_pinned - info->bytes_reserved),
4044 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05004045 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Joel Becker21380932009-04-21 12:38:29 -07004046 " may_use=%llu, used=%llu\n",
4047 (unsigned long long)info->total_bytes,
4048 (unsigned long long)info->bytes_pinned,
4049 (unsigned long long)info->bytes_delalloc,
4050 (unsigned long long)info->bytes_may_use,
4051 (unsigned long long)info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004052
Josef Bacik80eb2342008-10-29 14:49:05 -04004053 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05004054 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004055 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05004056 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4057 "%llu pinned %llu reserved\n",
4058 (unsigned long long)cache->key.objectid,
4059 (unsigned long long)cache->key.offset,
4060 (unsigned long long)btrfs_block_group_used(&cache->item),
4061 (unsigned long long)cache->pinned,
4062 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004063 btrfs_dump_free_space(cache, bytes);
4064 spin_unlock(&cache->lock);
4065 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004066 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004067}
Zheng Yane8569812008-09-26 10:05:48 -04004068
Yan Zheng11833d62009-09-11 16:11:19 -04004069int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4070 struct btrfs_root *root,
4071 u64 num_bytes, u64 min_alloc_size,
4072 u64 empty_size, u64 hint_byte,
4073 u64 search_end, struct btrfs_key *ins,
4074 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004075{
4076 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04004077 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04004078 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04004079
Josef Bacik6a632092009-02-20 11:00:09 -05004080 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04004081again:
Chris Mason0ef3e662008-05-24 14:04:53 -04004082 /*
4083 * the only place that sets empty_size is btrfs_realloc_node, which
4084 * is not called recursively on allocations
4085 */
4086 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04004087 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04004088 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004089 2 * 1024 * 1024,
4090 BTRFS_BLOCK_GROUP_METADATA |
4091 (info->metadata_alloc_profile &
4092 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04004093 }
4094 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004095 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04004096 }
Chris Mason0b86a832008-03-24 15:01:56 -04004097
Chris Masondb945352007-10-15 16:15:53 -04004098 WARN_ON(num_bytes < root->sectorsize);
4099 ret = find_free_extent(trans, root, num_bytes, empty_size,
4100 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04004101 trans->alloc_exclude_start,
4102 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04004103
Chris Mason98d20f62008-04-14 09:46:10 -04004104 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4105 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004106 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004107 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04004108 do_chunk_alloc(trans, root->fs_info->extent_root,
4109 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004110 goto again;
4111 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004112 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004113 struct btrfs_space_info *sinfo;
4114
4115 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004116 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4117 "wanted %llu\n", (unsigned long long)data,
4118 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004119 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04004120 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004121
4122 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004123}
4124
Chris Mason65b51a02008-08-01 15:11:20 -04004125int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4126{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004127 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004128 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004129
Josef Bacik0f9dd462008-09-23 13:14:11 -04004130 cache = btrfs_lookup_block_group(root->fs_info, start);
4131 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004132 printk(KERN_ERR "Unable to find block group for %llu\n",
4133 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004134 return -ENOSPC;
4135 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004136
4137 ret = btrfs_discard_extent(root, start, len);
4138
Josef Bacik0f9dd462008-09-23 13:14:11 -04004139 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04004140 update_reserved_extents(cache, len, 0);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004141 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04004142
Chris Masone6dcd2d2008-07-17 12:53:50 -04004143 return ret;
4144}
4145
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004146static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4147 struct btrfs_root *root,
4148 u64 parent, u64 root_objectid,
4149 u64 flags, u64 owner, u64 offset,
4150 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004151{
4152 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004153 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004154 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004155 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004156 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004157 struct extent_buffer *leaf;
4158 int type;
4159 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004160
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004161 if (parent > 0)
4162 type = BTRFS_SHARED_DATA_REF_KEY;
4163 else
4164 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004165
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004166 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004167
4168 path = btrfs_alloc_path();
4169 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004170
Chris Masonb9473432009-03-13 11:00:37 -04004171 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004172 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4173 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004174 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004175
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004176 leaf = path->nodes[0];
4177 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004178 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004179 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4180 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4181 btrfs_set_extent_flags(leaf, extent_item,
4182 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004183
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004184 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4185 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4186 if (parent > 0) {
4187 struct btrfs_shared_data_ref *ref;
4188 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4189 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4190 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4191 } else {
4192 struct btrfs_extent_data_ref *ref;
4193 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4194 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4195 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4196 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4197 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4198 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004199
4200 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004201 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004202
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004203 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4204 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05004205 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004206 printk(KERN_ERR "btrfs update block group failed for %llu "
4207 "%llu\n", (unsigned long long)ins->objectid,
4208 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004209 BUG();
4210 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004211 return ret;
4212}
4213
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004214static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4215 struct btrfs_root *root,
4216 u64 parent, u64 root_objectid,
4217 u64 flags, struct btrfs_disk_key *key,
4218 int level, struct btrfs_key *ins)
4219{
4220 int ret;
4221 struct btrfs_fs_info *fs_info = root->fs_info;
4222 struct btrfs_extent_item *extent_item;
4223 struct btrfs_tree_block_info *block_info;
4224 struct btrfs_extent_inline_ref *iref;
4225 struct btrfs_path *path;
4226 struct extent_buffer *leaf;
4227 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4228
4229 path = btrfs_alloc_path();
4230 BUG_ON(!path);
4231
4232 path->leave_spinning = 1;
4233 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4234 ins, size);
4235 BUG_ON(ret);
4236
4237 leaf = path->nodes[0];
4238 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4239 struct btrfs_extent_item);
4240 btrfs_set_extent_refs(leaf, extent_item, 1);
4241 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4242 btrfs_set_extent_flags(leaf, extent_item,
4243 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4244 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4245
4246 btrfs_set_tree_block_key(leaf, block_info, key);
4247 btrfs_set_tree_block_level(leaf, block_info, level);
4248
4249 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4250 if (parent > 0) {
4251 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4252 btrfs_set_extent_inline_ref_type(leaf, iref,
4253 BTRFS_SHARED_BLOCK_REF_KEY);
4254 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4255 } else {
4256 btrfs_set_extent_inline_ref_type(leaf, iref,
4257 BTRFS_TREE_BLOCK_REF_KEY);
4258 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4259 }
4260
4261 btrfs_mark_buffer_dirty(leaf);
4262 btrfs_free_path(path);
4263
4264 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4265 1, 0);
4266 if (ret) {
4267 printk(KERN_ERR "btrfs update block group failed for %llu "
4268 "%llu\n", (unsigned long long)ins->objectid,
4269 (unsigned long long)ins->offset);
4270 BUG();
4271 }
4272 return ret;
4273}
4274
4275int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4276 struct btrfs_root *root,
4277 u64 root_objectid, u64 owner,
4278 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004279{
4280 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04004281
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004282 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004283
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004284 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4285 0, root_objectid, owner, offset,
4286 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004287 return ret;
4288}
Chris Masone02119d2008-09-05 16:13:11 -04004289
4290/*
4291 * this is used by the tree logging recovery code. It records that
4292 * an extent has been allocated and makes sure to clear the free
4293 * space cache bits as well
4294 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004295int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4296 struct btrfs_root *root,
4297 u64 root_objectid, u64 owner, u64 offset,
4298 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004299{
4300 int ret;
4301 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04004302 struct btrfs_caching_control *caching_ctl;
4303 u64 start = ins->objectid;
4304 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04004305
Chris Masone02119d2008-09-05 16:13:11 -04004306 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04004307 cache_block_group(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004308 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004309
Yan Zheng11833d62009-09-11 16:11:19 -04004310 if (!caching_ctl) {
4311 BUG_ON(!block_group_cache_done(block_group));
4312 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4313 BUG_ON(ret);
4314 } else {
4315 mutex_lock(&caching_ctl->mutex);
4316
4317 if (start >= caching_ctl->progress) {
4318 ret = add_excluded_extent(root, start, num_bytes);
4319 BUG_ON(ret);
4320 } else if (start + num_bytes <= caching_ctl->progress) {
4321 ret = btrfs_remove_free_space(block_group,
4322 start, num_bytes);
4323 BUG_ON(ret);
4324 } else {
4325 num_bytes = caching_ctl->progress - start;
4326 ret = btrfs_remove_free_space(block_group,
4327 start, num_bytes);
4328 BUG_ON(ret);
4329
4330 start = caching_ctl->progress;
4331 num_bytes = ins->objectid + ins->offset -
4332 caching_ctl->progress;
4333 ret = add_excluded_extent(root, start, num_bytes);
4334 BUG_ON(ret);
4335 }
4336
4337 mutex_unlock(&caching_ctl->mutex);
4338 put_caching_control(caching_ctl);
4339 }
4340
4341 update_reserved_extents(block_group, ins->offset, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004342 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004343 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4344 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004345 return ret;
4346}
4347
Chris Masone6dcd2d2008-07-17 12:53:50 -04004348/*
4349 * finds a free extent and does all the dirty work required for allocation
4350 * returns the key for the extent through ins, and a tree buffer for
4351 * the first block of the extent through buf.
4352 *
4353 * returns 0 if everything worked, non-zero otherwise.
4354 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004355static int alloc_tree_block(struct btrfs_trans_handle *trans,
4356 struct btrfs_root *root,
4357 u64 num_bytes, u64 parent, u64 root_objectid,
4358 struct btrfs_disk_key *key, int level,
4359 u64 empty_size, u64 hint_byte, u64 search_end,
4360 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004361{
4362 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004363 u64 flags = 0;
4364
Yan Zheng11833d62009-09-11 16:11:19 -04004365 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4366 empty_size, hint_byte, search_end,
4367 ins, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004368 if (ret)
4369 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004370
4371 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4372 if (parent == 0)
4373 parent = ins->objectid;
4374 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4375 } else
4376 BUG_ON(parent > 0);
4377
Chris Masond00aff02008-09-11 15:54:42 -04004378 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004379 struct btrfs_delayed_extent_op *extent_op;
4380 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4381 BUG_ON(!extent_op);
4382 if (key)
4383 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4384 else
4385 memset(&extent_op->key, 0, sizeof(extent_op->key));
4386 extent_op->flags_to_set = flags;
4387 extent_op->update_key = 1;
4388 extent_op->update_flags = 1;
4389 extent_op->is_data = 0;
4390
4391 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4392 ins->offset, parent, root_objectid,
4393 level, BTRFS_ADD_DELAYED_EXTENT,
4394 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004395 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004396 }
Chris Mason925baed2008-06-25 16:01:30 -04004397 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004398}
Chris Mason65b51a02008-08-01 15:11:20 -04004399
4400struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4401 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004402 u64 bytenr, u32 blocksize,
4403 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004404{
4405 struct extent_buffer *buf;
4406
4407 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4408 if (!buf)
4409 return ERR_PTR(-ENOMEM);
4410 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004411 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004412 btrfs_tree_lock(buf);
4413 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004414
4415 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004416 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004417
Chris Masond0c803c2008-09-11 16:17:57 -04004418 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4419 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04004420 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004421 } else {
4422 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4423 buf->start + buf->len - 1, GFP_NOFS);
4424 }
Chris Mason65b51a02008-08-01 15:11:20 -04004425 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004426 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004427 return buf;
4428}
4429
Chris Masonfec577f2007-02-26 10:40:21 -05004430/*
4431 * helper function to allocate a block for a given tree
4432 * returns the tree buffer or NULL.
4433 */
Chris Mason5f39d392007-10-15 16:14:19 -04004434struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004435 struct btrfs_root *root, u32 blocksize,
4436 u64 parent, u64 root_objectid,
4437 struct btrfs_disk_key *key, int level,
4438 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004439{
Chris Masone2fa7222007-03-12 16:22:34 -04004440 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004441 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004442 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004443
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004444 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4445 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004446 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004447 BUG_ON(ret > 0);
4448 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004449 }
Chris Mason55c69072008-01-09 15:55:33 -05004450
Chris Mason4008c042009-02-12 14:09:45 -05004451 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4452 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004453 return buf;
4454}
Chris Masona28ec192007-03-06 20:08:01 -05004455
Yan Zheng2c47e6052009-06-27 21:07:35 -04004456#if 0
Chris Masone02119d2008-09-05 16:13:11 -04004457int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
4458 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04004459{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004460 u64 disk_bytenr;
4461 u64 num_bytes;
Chris Mason5f39d392007-10-15 16:14:19 -04004462 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04004463 struct btrfs_file_extent_item *fi;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004464 u32 nritems;
Chris Mason6407bf62007-03-27 06:33:00 -04004465 int i;
Chris Mason6407bf62007-03-27 06:33:00 -04004466 int ret;
4467
Chris Mason5f39d392007-10-15 16:14:19 -04004468 BUG_ON(!btrfs_is_leaf(leaf));
4469 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05004470
Chris Mason6407bf62007-03-27 06:33:00 -04004471 for (i = 0; i < nritems; i++) {
Chris Masone34a5b42008-07-22 12:08:37 -04004472 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04004473 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05004474
4475 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04004476 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04004477 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004478
Chris Mason6407bf62007-03-27 06:33:00 -04004479 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05004480
4481 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04004482 if (btrfs_file_extent_type(leaf, fi) ==
4483 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04004484 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004485
Chris Masondb945352007-10-15 16:15:53 -04004486 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05004487
4488 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04004489 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04004490 continue;
Chris Mason4a096752008-07-21 10:29:44 -04004491
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004492 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4493 ret = btrfs_free_extent(trans, root, disk_bytenr, num_bytes,
4494 leaf->start, 0, key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04004495 BUG_ON(ret);
Chris Mason6407bf62007-03-27 06:33:00 -04004496 }
4497 return 0;
4498}
4499
Chris Masond3977122009-01-05 21:25:51 -05004500static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04004501 struct btrfs_root *root,
4502 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04004503{
4504 int i;
4505 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05004506 struct btrfs_extent_info *info;
4507 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04004508
Chris Masonbd56b302009-02-04 09:27:02 -05004509 if (ref->nritems == 0)
4510 return 0;
4511
4512 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04004513 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05004514 sorted[i].bytenr = ref->extents[i].bytenr;
4515 sorted[i].slot = i;
4516 }
4517 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
4518
4519 /*
4520 * the items in the ref were sorted when the ref was inserted
4521 * into the ref cache, so this is already in order
4522 */
4523 for (i = 0; i < ref->nritems; i++) {
4524 info = ref->extents + sorted[i].slot;
Chris Mason56bec292009-03-13 10:10:06 -04004525 ret = btrfs_free_extent(trans, root, info->bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004526 info->num_bytes, ref->bytenr,
4527 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004528 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04004529
4530 atomic_inc(&root->fs_info->throttle_gen);
4531 wake_up(&root->fs_info->transaction_throttle);
4532 cond_resched();
4533
Yan Zheng31153d82008-07-28 15:32:19 -04004534 BUG_ON(ret);
4535 info++;
4536 }
Yan Zheng31153d82008-07-28 15:32:19 -04004537
Chris Mason806638b2009-02-05 09:08:14 -05004538 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04004539 return 0;
4540}
4541
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004542
Chris Mason56bec292009-03-13 10:10:06 -04004543static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
4544 struct btrfs_root *root, u64 start,
Chris Masond3977122009-01-05 21:25:51 -05004545 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04004546{
Chris Mason017e5362008-07-28 15:32:51 -04004547 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04004548
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004549 ret = btrfs_lookup_extent_refs(trans, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04004550 BUG_ON(ret);
4551
Chris Masond3977122009-01-05 21:25:51 -05004552#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04004553 /* if the refs count is one, it won't get increased again. But
4554 * if the ref count is > 1, someone may be decreasing it at
4555 * the same time we are.
4556 */
4557 if (*refs != 1) {
4558 struct extent_buffer *eb = NULL;
4559 eb = btrfs_find_create_tree_block(root, start, len);
4560 if (eb)
4561 btrfs_tree_lock(eb);
4562
4563 mutex_lock(&root->fs_info->alloc_mutex);
4564 ret = lookup_extent_ref(NULL, root, start, len, refs);
4565 BUG_ON(ret);
4566 mutex_unlock(&root->fs_info->alloc_mutex);
4567
4568 if (eb) {
4569 btrfs_tree_unlock(eb);
4570 free_extent_buffer(eb);
4571 }
4572 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05004573 printk(KERN_ERR "btrfs block %llu went down to one "
4574 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04004575 }
4576
4577 }
4578#endif
4579
Chris Masone7a84562008-06-25 16:01:31 -04004580 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04004581 return ret;
Chris Mason333db942008-06-25 16:01:30 -04004582}
4583
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004584
Chris Mason333db942008-06-25 16:01:30 -04004585/*
Chris Masonbd56b302009-02-04 09:27:02 -05004586 * this is used while deleting old snapshots, and it drops the refs
4587 * on a whole subtree starting from a level 1 node.
4588 *
4589 * The idea is to sort all the leaf pointers, and then drop the
4590 * ref on all the leaves in order. Most of the time the leaves
4591 * will have ref cache entries, so no leaf IOs will be required to
4592 * find the extents they have references on.
4593 *
4594 * For each leaf, any references it has are also dropped in order
4595 *
4596 * This ends up dropping the references in something close to optimal
4597 * order for reading and modifying the extent allocation tree.
4598 */
4599static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
4600 struct btrfs_root *root,
4601 struct btrfs_path *path)
4602{
4603 u64 bytenr;
4604 u64 root_owner;
4605 u64 root_gen;
4606 struct extent_buffer *eb = path->nodes[1];
4607 struct extent_buffer *leaf;
4608 struct btrfs_leaf_ref *ref;
4609 struct refsort *sorted = NULL;
4610 int nritems = btrfs_header_nritems(eb);
4611 int ret;
4612 int i;
4613 int refi = 0;
4614 int slot = path->slots[1];
4615 u32 blocksize = btrfs_level_size(root, 0);
4616 u32 refs;
4617
4618 if (nritems == 0)
4619 goto out;
4620
4621 root_owner = btrfs_header_owner(eb);
4622 root_gen = btrfs_header_generation(eb);
4623 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
4624
4625 /*
4626 * step one, sort all the leaf pointers so we don't scribble
4627 * randomly into the extent allocation tree
4628 */
4629 for (i = slot; i < nritems; i++) {
4630 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
4631 sorted[refi].slot = i;
4632 refi++;
4633 }
4634
4635 /*
4636 * nritems won't be zero, but if we're picking up drop_snapshot
4637 * after a crash, slot might be > 0, so double check things
4638 * just in case.
4639 */
4640 if (refi == 0)
4641 goto out;
4642
4643 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
4644
4645 /*
4646 * the first loop frees everything the leaves point to
4647 */
4648 for (i = 0; i < refi; i++) {
4649 u64 ptr_gen;
4650
4651 bytenr = sorted[i].bytenr;
4652
4653 /*
4654 * check the reference count on this leaf. If it is > 1
4655 * we just decrement it below and don't update any
4656 * of the refs the leaf points to.
4657 */
Chris Mason56bec292009-03-13 10:10:06 -04004658 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4659 blocksize, &refs);
Chris Masonbd56b302009-02-04 09:27:02 -05004660 BUG_ON(ret);
4661 if (refs != 1)
4662 continue;
4663
4664 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
4665
4666 /*
4667 * the leaf only had one reference, which means the
4668 * only thing pointing to this leaf is the snapshot
4669 * we're deleting. It isn't possible for the reference
4670 * count to increase again later
4671 *
4672 * The reference cache is checked for the leaf,
4673 * and if found we'll be able to drop any refs held by
4674 * the leaf without needing to read it in.
4675 */
4676 ref = btrfs_lookup_leaf_ref(root, bytenr);
4677 if (ref && ref->generation != ptr_gen) {
4678 btrfs_free_leaf_ref(root, ref);
4679 ref = NULL;
4680 }
4681 if (ref) {
4682 ret = cache_drop_leaf_ref(trans, root, ref);
4683 BUG_ON(ret);
4684 btrfs_remove_leaf_ref(root, ref);
4685 btrfs_free_leaf_ref(root, ref);
4686 } else {
4687 /*
4688 * the leaf wasn't in the reference cache, so
4689 * we have to read it.
4690 */
4691 leaf = read_tree_block(root, bytenr, blocksize,
4692 ptr_gen);
4693 ret = btrfs_drop_leaf_ref(trans, root, leaf);
4694 BUG_ON(ret);
4695 free_extent_buffer(leaf);
4696 }
4697 atomic_inc(&root->fs_info->throttle_gen);
4698 wake_up(&root->fs_info->transaction_throttle);
4699 cond_resched();
4700 }
4701
4702 /*
4703 * run through the loop again to free the refs on the leaves.
4704 * This is faster than doing it in the loop above because
4705 * the leaves are likely to be clustered together. We end up
4706 * working in nice chunks on the extent allocation tree.
4707 */
4708 for (i = 0; i < refi; i++) {
4709 bytenr = sorted[i].bytenr;
Chris Mason56bec292009-03-13 10:10:06 -04004710 ret = btrfs_free_extent(trans, root, bytenr,
Chris Masonbd56b302009-02-04 09:27:02 -05004711 blocksize, eb->start,
4712 root_owner, root_gen, 0, 1);
4713 BUG_ON(ret);
4714
4715 atomic_inc(&root->fs_info->throttle_gen);
4716 wake_up(&root->fs_info->transaction_throttle);
4717 cond_resched();
4718 }
4719out:
4720 kfree(sorted);
4721
4722 /*
4723 * update the path to show we've processed the entire level 1
4724 * node. This will get saved into the root's drop_snapshot_progress
4725 * field so these drops are not repeated again if this transaction
4726 * commits.
4727 */
4728 path->slots[1] = nritems;
4729 return 0;
4730}
4731
4732/*
Chris Mason9aca1d52007-03-13 11:09:37 -04004733 * helper function for drop_snapshot, this walks down the tree dropping ref
4734 * counts as it goes.
4735 */
Chris Masond3977122009-01-05 21:25:51 -05004736static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004737 struct btrfs_root *root,
4738 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05004739{
Chris Mason7bb86312007-12-11 09:25:06 -05004740 u64 root_owner;
4741 u64 root_gen;
4742 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04004743 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04004744 struct extent_buffer *next;
4745 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05004746 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04004747 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05004748 int ret;
4749 u32 refs;
4750
Chris Mason5caf2a02007-04-02 11:20:42 -04004751 WARN_ON(*level < 0);
4752 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason56bec292009-03-13 10:10:06 -04004753 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04004754 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05004755 BUG_ON(ret);
4756 if (refs > 1)
4757 goto out;
Chris Masone0115992007-06-19 16:23:05 -04004758
Chris Mason9aca1d52007-03-13 11:09:37 -04004759 /*
4760 * walk down to the last node level and free all the leaves
4761 */
Chris Masond3977122009-01-05 21:25:51 -05004762 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04004763 WARN_ON(*level < 0);
4764 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05004765 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04004766
Chris Mason5f39d392007-10-15 16:14:19 -04004767 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04004768 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04004769
Chris Mason7518a232007-03-12 12:01:18 -04004770 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04004771 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05004772 break;
Chris Masonbd56b302009-02-04 09:27:02 -05004773
4774 /* the new code goes down to level 1 and does all the
4775 * leaves pointed to that node in bulk. So, this check
4776 * for level 0 will always be false.
4777 *
4778 * But, the disk format allows the drop_snapshot_progress
4779 * field in the root to leave things in a state where
4780 * a leaf will need cleaning up here. If someone crashes
4781 * with the old code and then boots with the new code,
4782 * we might find a leaf here.
4783 */
Chris Mason6407bf62007-03-27 06:33:00 -04004784 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004785 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04004786 BUG_ON(ret);
4787 break;
4788 }
Chris Masonbd56b302009-02-04 09:27:02 -05004789
4790 /*
4791 * once we get to level one, process the whole node
4792 * at once, including everything below it.
4793 */
4794 if (*level == 1) {
4795 ret = drop_level_one_refs(trans, root, path);
4796 BUG_ON(ret);
4797 break;
4798 }
4799
Chris Masondb945352007-10-15 16:15:53 -04004800 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04004801 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04004802 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04004803
Chris Mason56bec292009-03-13 10:10:06 -04004804 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4805 blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04004806 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05004807
4808 /*
4809 * if there is more than one reference, we don't need
4810 * to read that node to drop any references it has. We
4811 * just drop the ref we hold on that node and move on to the
4812 * next slot in this level.
4813 */
Chris Mason6407bf62007-03-27 06:33:00 -04004814 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05004815 parent = path->nodes[*level];
4816 root_owner = btrfs_header_owner(parent);
4817 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05004818 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04004819
Chris Mason56bec292009-03-13 10:10:06 -04004820 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004821 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004822 root_owner, root_gen,
4823 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05004824 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04004825
4826 atomic_inc(&root->fs_info->throttle_gen);
4827 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04004828 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04004829
Chris Mason20524f02007-03-10 06:35:47 -05004830 continue;
4831 }
Chris Mason333db942008-06-25 16:01:30 -04004832
Chris Masonbd56b302009-02-04 09:27:02 -05004833 /*
4834 * we need to keep freeing things in the next level down.
4835 * read the block and loop around to process it
4836 */
4837 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04004838 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04004839 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04004840 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05004841 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04004842 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05004843 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04004844 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004845 }
4846out:
Chris Mason5caf2a02007-04-02 11:20:42 -04004847 WARN_ON(*level < 0);
4848 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05004849
4850 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05004851 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04004852 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05004853 } else {
4854 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04004855 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05004856 }
4857
Yan Zheng31153d82008-07-28 15:32:19 -04004858 blocksize = btrfs_level_size(root, *level);
4859 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05004860 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04004861
Chris Masonbd56b302009-02-04 09:27:02 -05004862 /*
4863 * cleanup and free the reference on the last node
4864 * we processed
4865 */
Chris Mason56bec292009-03-13 10:10:06 -04004866 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04004867 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004868 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004869 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05004870 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05004871
Chris Mason20524f02007-03-10 06:35:47 -05004872 *level += 1;
4873 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04004874
Chris Masone7a84562008-06-25 16:01:31 -04004875 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004876 return 0;
4877}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004878#endif
Chris Mason20524f02007-03-10 06:35:47 -05004879
Yan Zheng2c47e6052009-06-27 21:07:35 -04004880struct walk_control {
4881 u64 refs[BTRFS_MAX_LEVEL];
4882 u64 flags[BTRFS_MAX_LEVEL];
4883 struct btrfs_key update_progress;
4884 int stage;
4885 int level;
4886 int shared_level;
4887 int update_ref;
4888 int keep_locks;
4889};
4890
4891#define DROP_REFERENCE 1
4892#define UPDATE_BACKREF 2
4893
Chris Mason9aca1d52007-03-13 11:09:37 -04004894/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04004895 * hepler to process tree block while walking down the tree.
4896 *
4897 * when wc->stage == DROP_REFERENCE, this function checks
4898 * reference count of the block. if the block is shared and
4899 * we need update back refs for the subtree rooted at the
4900 * block, this function changes wc->stage to UPDATE_BACKREF
4901 *
4902 * when wc->stage == UPDATE_BACKREF, this function updates
4903 * back refs for pointers in the block.
4904 *
4905 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004906 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004907static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4908 struct btrfs_root *root,
4909 struct btrfs_path *path,
4910 struct walk_control *wc)
4911{
4912 int level = wc->level;
4913 struct extent_buffer *eb = path->nodes[level];
4914 struct btrfs_key key;
4915 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4916 int ret;
4917
4918 if (wc->stage == UPDATE_BACKREF &&
4919 btrfs_header_owner(eb) != root->root_key.objectid)
4920 return 1;
4921
4922 /*
4923 * when reference count of tree block is 1, it won't increase
4924 * again. once full backref flag is set, we never clear it.
4925 */
4926 if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4927 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) {
4928 BUG_ON(!path->locks[level]);
4929 ret = btrfs_lookup_extent_info(trans, root,
4930 eb->start, eb->len,
4931 &wc->refs[level],
4932 &wc->flags[level]);
4933 BUG_ON(ret);
4934 BUG_ON(wc->refs[level] == 0);
4935 }
4936
4937 if (wc->stage == DROP_REFERENCE &&
4938 wc->update_ref && wc->refs[level] > 1) {
4939 BUG_ON(eb == root->node);
4940 BUG_ON(path->slots[level] > 0);
4941 if (level == 0)
4942 btrfs_item_key_to_cpu(eb, &key, path->slots[level]);
4943 else
4944 btrfs_node_key_to_cpu(eb, &key, path->slots[level]);
4945 if (btrfs_header_owner(eb) == root->root_key.objectid &&
4946 btrfs_comp_cpu_keys(&key, &wc->update_progress) >= 0) {
4947 wc->stage = UPDATE_BACKREF;
4948 wc->shared_level = level;
4949 }
4950 }
4951
4952 if (wc->stage == DROP_REFERENCE) {
4953 if (wc->refs[level] > 1)
4954 return 1;
4955
4956 if (path->locks[level] && !wc->keep_locks) {
4957 btrfs_tree_unlock(eb);
4958 path->locks[level] = 0;
4959 }
4960 return 0;
4961 }
4962
4963 /* wc->stage == UPDATE_BACKREF */
4964 if (!(wc->flags[level] & flag)) {
4965 BUG_ON(!path->locks[level]);
4966 ret = btrfs_inc_ref(trans, root, eb, 1);
4967 BUG_ON(ret);
4968 ret = btrfs_dec_ref(trans, root, eb, 0);
4969 BUG_ON(ret);
4970 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
4971 eb->len, flag, 0);
4972 BUG_ON(ret);
4973 wc->flags[level] |= flag;
4974 }
4975
4976 /*
4977 * the block is shared by multiple trees, so it's not good to
4978 * keep the tree lock
4979 */
4980 if (path->locks[level] && level > 0) {
4981 btrfs_tree_unlock(eb);
4982 path->locks[level] = 0;
4983 }
4984 return 0;
4985}
4986
4987/*
4988 * hepler to process tree block while walking up the tree.
4989 *
4990 * when wc->stage == DROP_REFERENCE, this function drops
4991 * reference count on the block.
4992 *
4993 * when wc->stage == UPDATE_BACKREF, this function changes
4994 * wc->stage back to DROP_REFERENCE if we changed wc->stage
4995 * to UPDATE_BACKREF previously while processing the block.
4996 *
4997 * NOTE: return value 1 means we should stop walking up.
4998 */
4999static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5000 struct btrfs_root *root,
5001 struct btrfs_path *path,
5002 struct walk_control *wc)
5003{
5004 int ret = 0;
5005 int level = wc->level;
5006 struct extent_buffer *eb = path->nodes[level];
5007 u64 parent = 0;
5008
5009 if (wc->stage == UPDATE_BACKREF) {
5010 BUG_ON(wc->shared_level < level);
5011 if (level < wc->shared_level)
5012 goto out;
5013
5014 BUG_ON(wc->refs[level] <= 1);
5015 ret = find_next_key(path, level + 1, &wc->update_progress);
5016 if (ret > 0)
5017 wc->update_ref = 0;
5018
5019 wc->stage = DROP_REFERENCE;
5020 wc->shared_level = -1;
5021 path->slots[level] = 0;
5022
5023 /*
5024 * check reference count again if the block isn't locked.
5025 * we should start walking down the tree again if reference
5026 * count is one.
5027 */
5028 if (!path->locks[level]) {
5029 BUG_ON(level == 0);
5030 btrfs_tree_lock(eb);
5031 btrfs_set_lock_blocking(eb);
5032 path->locks[level] = 1;
5033
5034 ret = btrfs_lookup_extent_info(trans, root,
5035 eb->start, eb->len,
5036 &wc->refs[level],
5037 &wc->flags[level]);
5038 BUG_ON(ret);
5039 BUG_ON(wc->refs[level] == 0);
5040 if (wc->refs[level] == 1) {
5041 btrfs_tree_unlock(eb);
5042 path->locks[level] = 0;
5043 return 1;
5044 }
5045 } else {
5046 BUG_ON(level != 0);
5047 }
5048 }
5049
5050 /* wc->stage == DROP_REFERENCE */
5051 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5052
5053 if (wc->refs[level] == 1) {
5054 if (level == 0) {
5055 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5056 ret = btrfs_dec_ref(trans, root, eb, 1);
5057 else
5058 ret = btrfs_dec_ref(trans, root, eb, 0);
5059 BUG_ON(ret);
5060 }
5061 /* make block locked assertion in clean_tree_block happy */
5062 if (!path->locks[level] &&
5063 btrfs_header_generation(eb) == trans->transid) {
5064 btrfs_tree_lock(eb);
5065 btrfs_set_lock_blocking(eb);
5066 path->locks[level] = 1;
5067 }
5068 clean_tree_block(trans, root, eb);
5069 }
5070
5071 if (eb == root->node) {
5072 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5073 parent = eb->start;
5074 else
5075 BUG_ON(root->root_key.objectid !=
5076 btrfs_header_owner(eb));
5077 } else {
5078 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5079 parent = path->nodes[level + 1]->start;
5080 else
5081 BUG_ON(root->root_key.objectid !=
5082 btrfs_header_owner(path->nodes[level + 1]));
5083 }
5084
5085 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5086 root->root_key.objectid, level, 0);
5087 BUG_ON(ret);
5088out:
5089 wc->refs[level] = 0;
5090 wc->flags[level] = 0;
5091 return ret;
5092}
5093
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005094static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5095 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005096 struct btrfs_path *path,
5097 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005098{
5099 struct extent_buffer *next;
5100 struct extent_buffer *cur;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005101 u64 bytenr;
5102 u64 ptr_gen;
5103 u32 blocksize;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005104 int level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005105 int ret;
5106
Yan Zheng2c47e6052009-06-27 21:07:35 -04005107 while (level >= 0) {
5108 cur = path->nodes[level];
5109 BUG_ON(path->slots[level] >= btrfs_header_nritems(cur));
Yan Zhengf82d02d2008-10-29 14:49:05 -04005110
Yan Zheng2c47e6052009-06-27 21:07:35 -04005111 ret = walk_down_proc(trans, root, path, wc);
5112 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005113 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005114
Yan Zheng2c47e6052009-06-27 21:07:35 -04005115 if (level == 0)
5116 break;
5117
5118 bytenr = btrfs_node_blockptr(cur, path->slots[level]);
5119 blocksize = btrfs_level_size(root, level - 1);
5120 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[level]);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005121
5122 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
5123 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005124 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005125
Yan Zheng2c47e6052009-06-27 21:07:35 -04005126 level--;
5127 BUG_ON(level != btrfs_header_level(next));
5128 path->nodes[level] = next;
5129 path->slots[level] = 0;
5130 path->locks[level] = 1;
5131 wc->level = level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005132 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005133 return 0;
5134}
5135
Chris Masond3977122009-01-05 21:25:51 -05005136static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005137 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005138 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005139 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005140{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005141 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005142 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005143
Yan Zheng2c47e6052009-06-27 21:07:35 -04005144 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5145 while (level < max_level && path->nodes[level]) {
5146 wc->level = level;
5147 if (path->slots[level] + 1 <
5148 btrfs_header_nritems(path->nodes[level])) {
5149 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005150 return 0;
5151 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005152 ret = walk_up_proc(trans, root, path, wc);
5153 if (ret > 0)
5154 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05005155
Yan Zheng2c47e6052009-06-27 21:07:35 -04005156 if (path->locks[level]) {
5157 btrfs_tree_unlock(path->nodes[level]);
5158 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005159 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005160 free_extent_buffer(path->nodes[level]);
5161 path->nodes[level] = NULL;
5162 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005163 }
5164 }
5165 return 1;
5166}
5167
Chris Mason9aca1d52007-03-13 11:09:37 -04005168/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005169 * drop a subvolume tree.
5170 *
5171 * this function traverses the tree freeing any blocks that only
5172 * referenced by the tree.
5173 *
5174 * when a shared tree block is found. this function decreases its
5175 * reference count by one. if update_ref is true, this function
5176 * also make sure backrefs for the shared block and all lower level
5177 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04005178 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005179int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05005180{
Chris Mason5caf2a02007-04-02 11:20:42 -04005181 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005182 struct btrfs_trans_handle *trans;
5183 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005184 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005185 struct walk_control *wc;
5186 struct btrfs_key key;
5187 int err = 0;
5188 int ret;
5189 int level;
Chris Mason20524f02007-03-10 06:35:47 -05005190
Chris Mason5caf2a02007-04-02 11:20:42 -04005191 path = btrfs_alloc_path();
5192 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05005193
Yan Zheng2c47e6052009-06-27 21:07:35 -04005194 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5195 BUG_ON(!wc);
5196
5197 trans = btrfs_start_transaction(tree_root, 1);
5198
Chris Mason9f3a7422007-08-07 15:52:19 -04005199 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005200 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005201 path->nodes[level] = btrfs_lock_root_node(root);
5202 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005203 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005204 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005205 memset(&wc->update_progress, 0,
5206 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005207 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005208 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005209 memcpy(&wc->update_progress, &key,
5210 sizeof(wc->update_progress));
5211
Chris Mason6702ed42007-08-07 16:15:09 -04005212 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005213 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005214 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005215 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5216 path->lowest_level = 0;
5217 if (ret < 0) {
5218 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005219 goto out;
5220 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005221 btrfs_node_key_to_cpu(path->nodes[level], &key,
5222 path->slots[level]);
5223 WARN_ON(memcmp(&key, &wc->update_progress, sizeof(key)));
5224
Chris Mason7d9eb122008-07-08 14:19:17 -04005225 /*
5226 * unlock our path, this is safe because only this
5227 * function is allowed to delete this snapshot
5228 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005229 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005230
Yan Zheng2c47e6052009-06-27 21:07:35 -04005231 level = btrfs_header_level(root->node);
5232 while (1) {
5233 btrfs_tree_lock(path->nodes[level]);
5234 btrfs_set_lock_blocking(path->nodes[level]);
5235
5236 ret = btrfs_lookup_extent_info(trans, root,
5237 path->nodes[level]->start,
5238 path->nodes[level]->len,
5239 &wc->refs[level],
5240 &wc->flags[level]);
5241 BUG_ON(ret);
5242 BUG_ON(wc->refs[level] == 0);
5243
5244 if (level == root_item->drop_level)
5245 break;
5246
5247 btrfs_tree_unlock(path->nodes[level]);
5248 WARN_ON(wc->refs[level] != 1);
5249 level--;
5250 }
5251 }
5252
5253 wc->level = level;
5254 wc->shared_level = -1;
5255 wc->stage = DROP_REFERENCE;
5256 wc->update_ref = update_ref;
5257 wc->keep_locks = 0;
5258
5259 while (1) {
5260 ret = walk_down_tree(trans, root, path, wc);
5261 if (ret < 0) {
5262 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005263 break;
5264 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005265
5266 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5267 if (ret < 0) {
5268 err = ret;
5269 break;
5270 }
5271
5272 if (ret > 0) {
5273 BUG_ON(wc->stage != DROP_REFERENCE);
5274 break;
5275 }
5276
5277 if (wc->stage == DROP_REFERENCE) {
5278 level = wc->level;
5279 btrfs_node_key(path->nodes[level],
5280 &root_item->drop_progress,
5281 path->slots[level]);
5282 root_item->drop_level = level;
5283 }
5284
5285 BUG_ON(wc->level == 0);
5286 if (trans->transaction->in_commit ||
5287 trans->transaction->delayed_refs.flushing) {
5288 ret = btrfs_update_root(trans, tree_root,
5289 &root->root_key,
5290 root_item);
5291 BUG_ON(ret);
5292
5293 btrfs_end_transaction(trans, tree_root);
5294 trans = btrfs_start_transaction(tree_root, 1);
5295 } else {
5296 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005297 update = trans->delayed_ref_updates;
5298 trans->delayed_ref_updates = 0;
5299 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005300 btrfs_run_delayed_refs(trans, tree_root,
5301 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005302 }
Chris Mason20524f02007-03-10 06:35:47 -05005303 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005304 btrfs_release_path(root, path);
5305 BUG_ON(err);
5306
5307 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5308 BUG_ON(ret);
5309
5310 free_extent_buffer(root->node);
5311 free_extent_buffer(root->commit_root);
5312 kfree(root);
Chris Mason9f3a7422007-08-07 15:52:19 -04005313out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005314 btrfs_end_transaction(trans, tree_root);
5315 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005316 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005317 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005318}
Chris Mason9078a3e2007-04-26 16:46:15 -04005319
Yan Zheng2c47e6052009-06-27 21:07:35 -04005320/*
5321 * drop subtree rooted at tree block 'node'.
5322 *
5323 * NOTE: this function will unlock and release tree block 'node'
5324 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005325int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5326 struct btrfs_root *root,
5327 struct extent_buffer *node,
5328 struct extent_buffer *parent)
5329{
5330 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005331 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005332 int level;
5333 int parent_level;
5334 int ret = 0;
5335 int wret;
5336
Yan Zheng2c47e6052009-06-27 21:07:35 -04005337 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5338
Yan Zhengf82d02d2008-10-29 14:49:05 -04005339 path = btrfs_alloc_path();
5340 BUG_ON(!path);
5341
Yan Zheng2c47e6052009-06-27 21:07:35 -04005342 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5343 BUG_ON(!wc);
5344
Chris Masonb9447ef2009-03-09 11:45:38 -04005345 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005346 parent_level = btrfs_header_level(parent);
5347 extent_buffer_get(parent);
5348 path->nodes[parent_level] = parent;
5349 path->slots[parent_level] = btrfs_header_nritems(parent);
5350
Chris Masonb9447ef2009-03-09 11:45:38 -04005351 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005352 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005353 path->nodes[level] = node;
5354 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005355 path->locks[level] = 1;
5356
5357 wc->refs[parent_level] = 1;
5358 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5359 wc->level = level;
5360 wc->shared_level = -1;
5361 wc->stage = DROP_REFERENCE;
5362 wc->update_ref = 0;
5363 wc->keep_locks = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005364
5365 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005366 wret = walk_down_tree(trans, root, path, wc);
5367 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005368 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005369 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005370 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005371
Yan Zheng2c47e6052009-06-27 21:07:35 -04005372 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005373 if (wret < 0)
5374 ret = wret;
5375 if (wret != 0)
5376 break;
5377 }
5378
Yan Zheng2c47e6052009-06-27 21:07:35 -04005379 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005380 btrfs_free_path(path);
5381 return ret;
5382}
5383
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005384#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005385static unsigned long calc_ra(unsigned long start, unsigned long last,
5386 unsigned long nr)
5387{
5388 return min(last, start + nr - 1);
5389}
5390
Chris Masond3977122009-01-05 21:25:51 -05005391static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005392 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005393{
5394 u64 page_start;
5395 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005396 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005397 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005398 unsigned long i;
5399 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005400 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005401 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005402 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005403 unsigned int total_read = 0;
5404 unsigned int total_dirty = 0;
5405 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005406
5407 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005408
5409 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005410 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005411 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5412
Zheng Yan1a40e232008-09-26 10:09:34 -04005413 /* make sure the dirty trick played by the caller work */
5414 ret = invalidate_inode_pages2_range(inode->i_mapping,
5415 first_index, last_index);
5416 if (ret)
5417 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005418
Chris Mason4313b392008-01-03 09:08:48 -05005419 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005420
Zheng Yan1a40e232008-09-26 10:09:34 -04005421 for (i = first_index ; i <= last_index; i++) {
5422 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005423 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005424 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005425 }
5426 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005427again:
5428 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005429 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005430 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005431 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005432 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005433 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005434 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005435 if (!PageUptodate(page)) {
5436 btrfs_readpage(NULL, page);
5437 lock_page(page);
5438 if (!PageUptodate(page)) {
5439 unlock_page(page);
5440 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005441 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005442 goto out_unlock;
5443 }
5444 }
Chris Masonec44a352008-04-28 15:29:52 -04005445 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005446
Chris Masonedbd8d42007-12-21 16:27:24 -05005447 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5448 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005449 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005450
Chris Mason3eaa2882008-07-24 11:57:52 -04005451 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5452 if (ordered) {
5453 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5454 unlock_page(page);
5455 page_cache_release(page);
5456 btrfs_start_ordered_extent(inode, ordered, 1);
5457 btrfs_put_ordered_extent(ordered);
5458 goto again;
5459 }
5460 set_page_extent_mapped(page);
5461
Zheng Yan1a40e232008-09-26 10:09:34 -04005462 if (i == first_index)
5463 set_extent_bits(io_tree, page_start, page_end,
5464 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005465 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005466
Chris Masona061fc82008-05-07 11:43:44 -04005467 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005468 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005469
Chris Masond1310b22008-01-24 16:13:08 -05005470 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005471 unlock_page(page);
5472 page_cache_release(page);
5473 }
5474
5475out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005476 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005477 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005478 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005479 return ret;
5480}
5481
Chris Masond3977122009-01-05 21:25:51 -05005482static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005483 struct btrfs_key *extent_key,
5484 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005485{
Zheng Yan1a40e232008-09-26 10:09:34 -04005486 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5487 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5488 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005489 u64 start = extent_key->objectid - offset;
5490 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005491
5492 em = alloc_extent_map(GFP_NOFS);
5493 BUG_ON(!em || IS_ERR(em));
5494
Yan Zheng66435582008-10-30 14:19:50 -04005495 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005496 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005497 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005498 em->block_start = extent_key->objectid;
5499 em->bdev = root->fs_info->fs_devices->latest_bdev;
5500 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5501
5502 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005503 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005504 while (1) {
5505 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04005506 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005507 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005508 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005509 if (ret != -EEXIST) {
5510 free_extent_map(em);
5511 break;
5512 }
Yan Zheng66435582008-10-30 14:19:50 -04005513 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005514 }
Yan Zheng66435582008-10-30 14:19:50 -04005515 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005516
Yan Zheng66435582008-10-30 14:19:50 -04005517 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005518}
5519
5520struct btrfs_ref_path {
5521 u64 extent_start;
5522 u64 nodes[BTRFS_MAX_LEVEL];
5523 u64 root_objectid;
5524 u64 root_generation;
5525 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005526 u32 num_refs;
5527 int lowest_level;
5528 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005529 int shared_level;
5530
5531 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5532 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005533};
5534
5535struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005536 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005537 u64 disk_bytenr;
5538 u64 disk_num_bytes;
5539 u64 offset;
5540 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005541 u8 compression;
5542 u8 encryption;
5543 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005544};
5545
5546static int is_cowonly_root(u64 root_objectid)
5547{
5548 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5549 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5550 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5551 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005552 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5553 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005554 return 1;
5555 return 0;
5556}
5557
Chris Masond3977122009-01-05 21:25:51 -05005558static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005559 struct btrfs_root *extent_root,
5560 struct btrfs_ref_path *ref_path,
5561 int first_time)
5562{
5563 struct extent_buffer *leaf;
5564 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005565 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005566 struct btrfs_key key;
5567 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005568 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005569 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005570 int level;
5571 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005572
Zheng Yan1a40e232008-09-26 10:09:34 -04005573 path = btrfs_alloc_path();
5574 if (!path)
5575 return -ENOMEM;
5576
Zheng Yan1a40e232008-09-26 10:09:34 -04005577 if (first_time) {
5578 ref_path->lowest_level = -1;
5579 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005580 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005581 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005582 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005583walk_down:
5584 level = ref_path->current_level - 1;
5585 while (level >= -1) {
5586 u64 parent;
5587 if (level < ref_path->lowest_level)
5588 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005589
Chris Masond3977122009-01-05 21:25:51 -05005590 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005591 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005592 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005593 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005594 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005595
Zheng Yan1a40e232008-09-26 10:09:34 -04005596 parent = ref_path->nodes[level + 1];
5597 ref_path->nodes[level + 1] = 0;
5598 ref_path->current_level = level;
5599 BUG_ON(parent == 0);
5600
5601 key.objectid = bytenr;
5602 key.offset = parent + 1;
5603 key.type = BTRFS_EXTENT_REF_KEY;
5604
5605 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005606 if (ret < 0)
5607 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005608 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005609
Chris Masonedbd8d42007-12-21 16:27:24 -05005610 leaf = path->nodes[0];
5611 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005612 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005613 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005614 if (ret < 0)
5615 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005616 if (ret > 0)
5617 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005618 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005619 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005620
5621 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005622 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005623 found_key.type == BTRFS_EXTENT_REF_KEY) {
5624 if (level < ref_path->shared_level)
5625 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005626 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005627 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005628next:
5629 level--;
5630 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005631 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005632 }
5633 /* reached lowest level */
5634 ret = 1;
5635 goto out;
5636walk_up:
5637 level = ref_path->current_level;
5638 while (level < BTRFS_MAX_LEVEL - 1) {
5639 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005640
5641 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005642 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005643 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005644 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005645
Zheng Yan1a40e232008-09-26 10:09:34 -04005646 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005647
Zheng Yan1a40e232008-09-26 10:09:34 -04005648 key.objectid = bytenr;
5649 key.offset = 0;
5650 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005651
Zheng Yan1a40e232008-09-26 10:09:34 -04005652 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5653 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005654 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005655
5656 leaf = path->nodes[0];
5657 nritems = btrfs_header_nritems(leaf);
5658 if (path->slots[0] >= nritems) {
5659 ret = btrfs_next_leaf(extent_root, path);
5660 if (ret < 0)
5661 goto out;
5662 if (ret > 0) {
5663 /* the extent was freed by someone */
5664 if (ref_path->lowest_level == level)
5665 goto out;
5666 btrfs_release_path(extent_root, path);
5667 goto walk_down;
5668 }
5669 leaf = path->nodes[0];
5670 }
5671
5672 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5673 if (found_key.objectid != bytenr ||
5674 found_key.type != BTRFS_EXTENT_REF_KEY) {
5675 /* the extent was freed by someone */
5676 if (ref_path->lowest_level == level) {
5677 ret = 1;
5678 goto out;
5679 }
5680 btrfs_release_path(extent_root, path);
5681 goto walk_down;
5682 }
5683found:
5684 ref = btrfs_item_ptr(leaf, path->slots[0],
5685 struct btrfs_extent_ref);
5686 ref_objectid = btrfs_ref_objectid(leaf, ref);
5687 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5688 if (first_time) {
5689 level = (int)ref_objectid;
5690 BUG_ON(level >= BTRFS_MAX_LEVEL);
5691 ref_path->lowest_level = level;
5692 ref_path->current_level = level;
5693 ref_path->nodes[level] = bytenr;
5694 } else {
5695 WARN_ON(ref_objectid != level);
5696 }
5697 } else {
5698 WARN_ON(level != -1);
5699 }
5700 first_time = 0;
5701
5702 if (ref_path->lowest_level == level) {
5703 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005704 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5705 }
5706
5707 /*
5708 * the block is tree root or the block isn't in reference
5709 * counted tree.
5710 */
5711 if (found_key.objectid == found_key.offset ||
5712 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5713 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5714 ref_path->root_generation =
5715 btrfs_ref_generation(leaf, ref);
5716 if (level < 0) {
5717 /* special reference from the tree log */
5718 ref_path->nodes[0] = found_key.offset;
5719 ref_path->current_level = 0;
5720 }
5721 ret = 0;
5722 goto out;
5723 }
5724
5725 level++;
5726 BUG_ON(ref_path->nodes[level] != 0);
5727 ref_path->nodes[level] = found_key.offset;
5728 ref_path->current_level = level;
5729
5730 /*
5731 * the reference was created in the running transaction,
5732 * no need to continue walking up.
5733 */
5734 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5735 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5736 ref_path->root_generation =
5737 btrfs_ref_generation(leaf, ref);
5738 ret = 0;
5739 goto out;
5740 }
5741
5742 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005743 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005744 }
5745 /* reached max tree level, but no tree root found. */
5746 BUG();
5747out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005748 btrfs_free_path(path);
5749 return ret;
5750}
5751
5752static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5753 struct btrfs_root *extent_root,
5754 struct btrfs_ref_path *ref_path,
5755 u64 extent_start)
5756{
5757 memset(ref_path, 0, sizeof(*ref_path));
5758 ref_path->extent_start = extent_start;
5759
5760 return __next_ref_path(trans, extent_root, ref_path, 1);
5761}
5762
5763static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5764 struct btrfs_root *extent_root,
5765 struct btrfs_ref_path *ref_path)
5766{
5767 return __next_ref_path(trans, extent_root, ref_path, 0);
5768}
5769
Chris Masond3977122009-01-05 21:25:51 -05005770static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005771 struct btrfs_key *extent_key,
5772 u64 offset, int no_fragment,
5773 struct disk_extent **extents,
5774 int *nr_extents)
5775{
5776 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5777 struct btrfs_path *path;
5778 struct btrfs_file_extent_item *fi;
5779 struct extent_buffer *leaf;
5780 struct disk_extent *exts = *extents;
5781 struct btrfs_key found_key;
5782 u64 cur_pos;
5783 u64 last_byte;
5784 u32 nritems;
5785 int nr = 0;
5786 int max = *nr_extents;
5787 int ret;
5788
5789 WARN_ON(!no_fragment && *extents);
5790 if (!exts) {
5791 max = 1;
5792 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
5793 if (!exts)
5794 return -ENOMEM;
5795 }
5796
5797 path = btrfs_alloc_path();
5798 BUG_ON(!path);
5799
5800 cur_pos = extent_key->objectid - offset;
5801 last_byte = extent_key->objectid + extent_key->offset;
5802 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
5803 cur_pos, 0);
5804 if (ret < 0)
5805 goto out;
5806 if (ret > 0) {
5807 ret = -ENOENT;
5808 goto out;
5809 }
5810
5811 while (1) {
5812 leaf = path->nodes[0];
5813 nritems = btrfs_header_nritems(leaf);
5814 if (path->slots[0] >= nritems) {
5815 ret = btrfs_next_leaf(root, path);
5816 if (ret < 0)
5817 goto out;
5818 if (ret > 0)
5819 break;
5820 leaf = path->nodes[0];
5821 }
5822
5823 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5824 if (found_key.offset != cur_pos ||
5825 found_key.type != BTRFS_EXTENT_DATA_KEY ||
5826 found_key.objectid != reloc_inode->i_ino)
5827 break;
5828
5829 fi = btrfs_item_ptr(leaf, path->slots[0],
5830 struct btrfs_file_extent_item);
5831 if (btrfs_file_extent_type(leaf, fi) !=
5832 BTRFS_FILE_EXTENT_REG ||
5833 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5834 break;
5835
5836 if (nr == max) {
5837 struct disk_extent *old = exts;
5838 max *= 2;
5839 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
5840 memcpy(exts, old, sizeof(*exts) * nr);
5841 if (old != *extents)
5842 kfree(old);
5843 }
5844
5845 exts[nr].disk_bytenr =
5846 btrfs_file_extent_disk_bytenr(leaf, fi);
5847 exts[nr].disk_num_bytes =
5848 btrfs_file_extent_disk_num_bytes(leaf, fi);
5849 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
5850 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04005851 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
5852 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
5853 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
5854 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
5855 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04005856 BUG_ON(exts[nr].offset > 0);
5857 BUG_ON(exts[nr].compression || exts[nr].encryption);
5858 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005859
5860 cur_pos += exts[nr].num_bytes;
5861 nr++;
5862
5863 if (cur_pos + offset >= last_byte)
5864 break;
5865
5866 if (no_fragment) {
5867 ret = 1;
5868 goto out;
5869 }
5870 path->slots[0]++;
5871 }
5872
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005873 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04005874 if (cur_pos + offset < last_byte) {
5875 ret = -ENOENT;
5876 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05005877 }
5878 ret = 0;
5879out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005880 btrfs_free_path(path);
5881 if (ret) {
5882 if (exts != *extents)
5883 kfree(exts);
5884 } else {
5885 *extents = exts;
5886 *nr_extents = nr;
5887 }
5888 return ret;
5889}
5890
Chris Masond3977122009-01-05 21:25:51 -05005891static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005892 struct btrfs_root *root,
5893 struct btrfs_path *path,
5894 struct btrfs_key *extent_key,
5895 struct btrfs_key *leaf_key,
5896 struct btrfs_ref_path *ref_path,
5897 struct disk_extent *new_extents,
5898 int nr_extents)
5899{
5900 struct extent_buffer *leaf;
5901 struct btrfs_file_extent_item *fi;
5902 struct inode *inode = NULL;
5903 struct btrfs_key key;
5904 u64 lock_start = 0;
5905 u64 lock_end = 0;
5906 u64 num_bytes;
5907 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05005908 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005909 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005910 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005911 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04005912 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04005913 int ret;
5914
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005915 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04005916 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005917 if (key.objectid < ref_path->owner_objectid ||
5918 (key.objectid == ref_path->owner_objectid &&
5919 key.type < BTRFS_EXTENT_DATA_KEY)) {
5920 key.objectid = ref_path->owner_objectid;
5921 key.type = BTRFS_EXTENT_DATA_KEY;
5922 key.offset = 0;
5923 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005924 }
5925
5926 while (1) {
5927 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5928 if (ret < 0)
5929 goto out;
5930
5931 leaf = path->nodes[0];
5932 nritems = btrfs_header_nritems(leaf);
5933next:
5934 if (extent_locked && ret > 0) {
5935 /*
5936 * the file extent item was modified by someone
5937 * before the extent got locked.
5938 */
Zheng Yan1a40e232008-09-26 10:09:34 -04005939 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5940 lock_end, GFP_NOFS);
5941 extent_locked = 0;
5942 }
5943
5944 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005945 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04005946 break;
5947
5948 BUG_ON(extent_locked);
5949 ret = btrfs_next_leaf(root, path);
5950 if (ret < 0)
5951 goto out;
5952 if (ret > 0)
5953 break;
5954 leaf = path->nodes[0];
5955 nritems = btrfs_header_nritems(leaf);
5956 }
5957
5958 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5959
5960 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
5961 if ((key.objectid > ref_path->owner_objectid) ||
5962 (key.objectid == ref_path->owner_objectid &&
5963 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05005964 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005965 break;
5966 }
5967
5968 if (inode && key.objectid != inode->i_ino) {
5969 BUG_ON(extent_locked);
5970 btrfs_release_path(root, path);
5971 mutex_unlock(&inode->i_mutex);
5972 iput(inode);
5973 inode = NULL;
5974 continue;
5975 }
5976
5977 if (key.type != BTRFS_EXTENT_DATA_KEY) {
5978 path->slots[0]++;
5979 ret = 1;
5980 goto next;
5981 }
5982 fi = btrfs_item_ptr(leaf, path->slots[0],
5983 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04005984 extent_type = btrfs_file_extent_type(leaf, fi);
5985 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
5986 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04005987 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
5988 extent_key->objectid)) {
5989 path->slots[0]++;
5990 ret = 1;
5991 goto next;
5992 }
5993
5994 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5995 ext_offset = btrfs_file_extent_offset(leaf, fi);
5996
Yan Zheng86288a12009-01-21 10:49:16 -05005997 if (search_end == (u64)-1) {
5998 search_end = key.offset - ext_offset +
5999 btrfs_file_extent_ram_bytes(leaf, fi);
6000 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006001
6002 if (!extent_locked) {
6003 lock_start = key.offset;
6004 lock_end = lock_start + num_bytes - 1;
6005 } else {
Yan Zheng66435582008-10-30 14:19:50 -04006006 if (lock_start > key.offset ||
6007 lock_end + 1 < key.offset + num_bytes) {
6008 unlock_extent(&BTRFS_I(inode)->io_tree,
6009 lock_start, lock_end, GFP_NOFS);
6010 extent_locked = 0;
6011 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006012 }
6013
6014 if (!inode) {
6015 btrfs_release_path(root, path);
6016
6017 inode = btrfs_iget_locked(root->fs_info->sb,
6018 key.objectid, root);
6019 if (inode->i_state & I_NEW) {
6020 BTRFS_I(inode)->root = root;
6021 BTRFS_I(inode)->location.objectid =
6022 key.objectid;
6023 BTRFS_I(inode)->location.type =
6024 BTRFS_INODE_ITEM_KEY;
6025 BTRFS_I(inode)->location.offset = 0;
6026 btrfs_read_locked_inode(inode);
6027 unlock_new_inode(inode);
6028 }
6029 /*
6030 * some code call btrfs_commit_transaction while
6031 * holding the i_mutex, so we can't use mutex_lock
6032 * here.
6033 */
6034 if (is_bad_inode(inode) ||
6035 !mutex_trylock(&inode->i_mutex)) {
6036 iput(inode);
6037 inode = NULL;
6038 key.offset = (u64)-1;
6039 goto skip;
6040 }
6041 }
6042
6043 if (!extent_locked) {
6044 struct btrfs_ordered_extent *ordered;
6045
6046 btrfs_release_path(root, path);
6047
6048 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6049 lock_end, GFP_NOFS);
6050 ordered = btrfs_lookup_first_ordered_extent(inode,
6051 lock_end);
6052 if (ordered &&
6053 ordered->file_offset <= lock_end &&
6054 ordered->file_offset + ordered->len > lock_start) {
6055 unlock_extent(&BTRFS_I(inode)->io_tree,
6056 lock_start, lock_end, GFP_NOFS);
6057 btrfs_start_ordered_extent(inode, ordered, 1);
6058 btrfs_put_ordered_extent(ordered);
6059 key.offset += num_bytes;
6060 goto skip;
6061 }
6062 if (ordered)
6063 btrfs_put_ordered_extent(ordered);
6064
Zheng Yan1a40e232008-09-26 10:09:34 -04006065 extent_locked = 1;
6066 continue;
6067 }
6068
6069 if (nr_extents == 1) {
6070 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04006071 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6072 new_extents[0].disk_bytenr);
6073 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6074 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006075 btrfs_mark_buffer_dirty(leaf);
6076
6077 btrfs_drop_extent_cache(inode, key.offset,
6078 key.offset + num_bytes - 1, 0);
6079
6080 ret = btrfs_inc_extent_ref(trans, root,
6081 new_extents[0].disk_bytenr,
6082 new_extents[0].disk_num_bytes,
6083 leaf->start,
6084 root->root_key.objectid,
6085 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006086 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006087 BUG_ON(ret);
6088
6089 ret = btrfs_free_extent(trans, root,
6090 extent_key->objectid,
6091 extent_key->offset,
6092 leaf->start,
6093 btrfs_header_owner(leaf),
6094 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006095 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006096 BUG_ON(ret);
6097
6098 btrfs_release_path(root, path);
6099 key.offset += num_bytes;
6100 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04006101 BUG_ON(1);
6102#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006103 u64 alloc_hint;
6104 u64 extent_len;
6105 int i;
6106 /*
6107 * drop old extent pointer at first, then insert the
6108 * new pointers one bye one
6109 */
6110 btrfs_release_path(root, path);
6111 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6112 key.offset + num_bytes,
6113 key.offset, &alloc_hint);
6114 BUG_ON(ret);
6115
6116 for (i = 0; i < nr_extents; i++) {
6117 if (ext_offset >= new_extents[i].num_bytes) {
6118 ext_offset -= new_extents[i].num_bytes;
6119 continue;
6120 }
6121 extent_len = min(new_extents[i].num_bytes -
6122 ext_offset, num_bytes);
6123
6124 ret = btrfs_insert_empty_item(trans, root,
6125 path, &key,
6126 sizeof(*fi));
6127 BUG_ON(ret);
6128
6129 leaf = path->nodes[0];
6130 fi = btrfs_item_ptr(leaf, path->slots[0],
6131 struct btrfs_file_extent_item);
6132 btrfs_set_file_extent_generation(leaf, fi,
6133 trans->transid);
6134 btrfs_set_file_extent_type(leaf, fi,
6135 BTRFS_FILE_EXTENT_REG);
6136 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6137 new_extents[i].disk_bytenr);
6138 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6139 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04006140 btrfs_set_file_extent_ram_bytes(leaf, fi,
6141 new_extents[i].ram_bytes);
6142
6143 btrfs_set_file_extent_compression(leaf, fi,
6144 new_extents[i].compression);
6145 btrfs_set_file_extent_encryption(leaf, fi,
6146 new_extents[i].encryption);
6147 btrfs_set_file_extent_other_encoding(leaf, fi,
6148 new_extents[i].other_encoding);
6149
Zheng Yan1a40e232008-09-26 10:09:34 -04006150 btrfs_set_file_extent_num_bytes(leaf, fi,
6151 extent_len);
6152 ext_offset += new_extents[i].offset;
6153 btrfs_set_file_extent_offset(leaf, fi,
6154 ext_offset);
6155 btrfs_mark_buffer_dirty(leaf);
6156
6157 btrfs_drop_extent_cache(inode, key.offset,
6158 key.offset + extent_len - 1, 0);
6159
6160 ret = btrfs_inc_extent_ref(trans, root,
6161 new_extents[i].disk_bytenr,
6162 new_extents[i].disk_num_bytes,
6163 leaf->start,
6164 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006165 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006166 BUG_ON(ret);
6167 btrfs_release_path(root, path);
6168
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006169 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04006170
6171 ext_offset = 0;
6172 num_bytes -= extent_len;
6173 key.offset += extent_len;
6174
6175 if (num_bytes == 0)
6176 break;
6177 }
6178 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006179#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04006180 }
6181
6182 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006183 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6184 lock_end, GFP_NOFS);
6185 extent_locked = 0;
6186 }
6187skip:
6188 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05006189 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006190 break;
6191
6192 cond_resched();
6193 }
6194 ret = 0;
6195out:
6196 btrfs_release_path(root, path);
6197 if (inode) {
6198 mutex_unlock(&inode->i_mutex);
6199 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006200 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6201 lock_end, GFP_NOFS);
6202 }
6203 iput(inode);
6204 }
6205 return ret;
6206}
6207
Zheng Yan1a40e232008-09-26 10:09:34 -04006208int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6209 struct btrfs_root *root,
6210 struct extent_buffer *buf, u64 orig_start)
6211{
6212 int level;
6213 int ret;
6214
6215 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6216 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6217
6218 level = btrfs_header_level(buf);
6219 if (level == 0) {
6220 struct btrfs_leaf_ref *ref;
6221 struct btrfs_leaf_ref *orig_ref;
6222
6223 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6224 if (!orig_ref)
6225 return -ENOENT;
6226
6227 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6228 if (!ref) {
6229 btrfs_free_leaf_ref(root, orig_ref);
6230 return -ENOMEM;
6231 }
6232
6233 ref->nritems = orig_ref->nritems;
6234 memcpy(ref->extents, orig_ref->extents,
6235 sizeof(ref->extents[0]) * ref->nritems);
6236
6237 btrfs_free_leaf_ref(root, orig_ref);
6238
6239 ref->root_gen = trans->transid;
6240 ref->bytenr = buf->start;
6241 ref->owner = btrfs_header_owner(buf);
6242 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006243
Zheng Yan1a40e232008-09-26 10:09:34 -04006244 ret = btrfs_add_leaf_ref(root, ref, 0);
6245 WARN_ON(ret);
6246 btrfs_free_leaf_ref(root, ref);
6247 }
6248 return 0;
6249}
6250
Chris Masond3977122009-01-05 21:25:51 -05006251static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006252 struct extent_buffer *leaf,
6253 struct btrfs_block_group_cache *group,
6254 struct btrfs_root *target_root)
6255{
6256 struct btrfs_key key;
6257 struct inode *inode = NULL;
6258 struct btrfs_file_extent_item *fi;
6259 u64 num_bytes;
6260 u64 skip_objectid = 0;
6261 u32 nritems;
6262 u32 i;
6263
6264 nritems = btrfs_header_nritems(leaf);
6265 for (i = 0; i < nritems; i++) {
6266 btrfs_item_key_to_cpu(leaf, &key, i);
6267 if (key.objectid == skip_objectid ||
6268 key.type != BTRFS_EXTENT_DATA_KEY)
6269 continue;
6270 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6271 if (btrfs_file_extent_type(leaf, fi) ==
6272 BTRFS_FILE_EXTENT_INLINE)
6273 continue;
6274 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6275 continue;
6276 if (!inode || inode->i_ino != key.objectid) {
6277 iput(inode);
6278 inode = btrfs_ilookup(target_root->fs_info->sb,
6279 key.objectid, target_root, 1);
6280 }
6281 if (!inode) {
6282 skip_objectid = key.objectid;
6283 continue;
6284 }
6285 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6286
6287 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6288 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006289 btrfs_drop_extent_cache(inode, key.offset,
6290 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006291 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6292 key.offset + num_bytes - 1, GFP_NOFS);
6293 cond_resched();
6294 }
6295 iput(inode);
6296 return 0;
6297}
6298
Chris Masond3977122009-01-05 21:25:51 -05006299static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006300 struct btrfs_root *root,
6301 struct extent_buffer *leaf,
6302 struct btrfs_block_group_cache *group,
6303 struct inode *reloc_inode)
6304{
6305 struct btrfs_key key;
6306 struct btrfs_key extent_key;
6307 struct btrfs_file_extent_item *fi;
6308 struct btrfs_leaf_ref *ref;
6309 struct disk_extent *new_extent;
6310 u64 bytenr;
6311 u64 num_bytes;
6312 u32 nritems;
6313 u32 i;
6314 int ext_index;
6315 int nr_extent;
6316 int ret;
6317
6318 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6319 BUG_ON(!new_extent);
6320
6321 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6322 BUG_ON(!ref);
6323
6324 ext_index = -1;
6325 nritems = btrfs_header_nritems(leaf);
6326 for (i = 0; i < nritems; i++) {
6327 btrfs_item_key_to_cpu(leaf, &key, i);
6328 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6329 continue;
6330 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6331 if (btrfs_file_extent_type(leaf, fi) ==
6332 BTRFS_FILE_EXTENT_INLINE)
6333 continue;
6334 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6335 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6336 if (bytenr == 0)
6337 continue;
6338
6339 ext_index++;
6340 if (bytenr >= group->key.objectid + group->key.offset ||
6341 bytenr + num_bytes <= group->key.objectid)
6342 continue;
6343
6344 extent_key.objectid = bytenr;
6345 extent_key.offset = num_bytes;
6346 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6347 nr_extent = 1;
6348 ret = get_new_locations(reloc_inode, &extent_key,
6349 group->key.objectid, 1,
6350 &new_extent, &nr_extent);
6351 if (ret > 0)
6352 continue;
6353 BUG_ON(ret < 0);
6354
6355 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6356 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6357 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6358 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6359
Zheng Yan1a40e232008-09-26 10:09:34 -04006360 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6361 new_extent->disk_bytenr);
6362 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6363 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006364 btrfs_mark_buffer_dirty(leaf);
6365
6366 ret = btrfs_inc_extent_ref(trans, root,
6367 new_extent->disk_bytenr,
6368 new_extent->disk_num_bytes,
6369 leaf->start,
6370 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006371 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006372 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006373
Zheng Yan1a40e232008-09-26 10:09:34 -04006374 ret = btrfs_free_extent(trans, root,
6375 bytenr, num_bytes, leaf->start,
6376 btrfs_header_owner(leaf),
6377 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006378 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006379 BUG_ON(ret);
6380 cond_resched();
6381 }
6382 kfree(new_extent);
6383 BUG_ON(ext_index + 1 != ref->nritems);
6384 btrfs_free_leaf_ref(root, ref);
6385 return 0;
6386}
6387
Yan Zhengf82d02d2008-10-29 14:49:05 -04006388int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6389 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006390{
6391 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006392 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006393
6394 if (root->reloc_root) {
6395 reloc_root = root->reloc_root;
6396 root->reloc_root = NULL;
6397 list_add(&reloc_root->dead_list,
6398 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006399
6400 btrfs_set_root_bytenr(&reloc_root->root_item,
6401 reloc_root->node->start);
6402 btrfs_set_root_level(&root->root_item,
6403 btrfs_header_level(reloc_root->node));
6404 memset(&reloc_root->root_item.drop_progress, 0,
6405 sizeof(struct btrfs_disk_key));
6406 reloc_root->root_item.drop_level = 0;
6407
6408 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6409 &reloc_root->root_key,
6410 &reloc_root->root_item);
6411 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006412 }
6413 return 0;
6414}
6415
6416int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6417{
6418 struct btrfs_trans_handle *trans;
6419 struct btrfs_root *reloc_root;
6420 struct btrfs_root *prev_root = NULL;
6421 struct list_head dead_roots;
6422 int ret;
6423 unsigned long nr;
6424
6425 INIT_LIST_HEAD(&dead_roots);
6426 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6427
6428 while (!list_empty(&dead_roots)) {
6429 reloc_root = list_entry(dead_roots.prev,
6430 struct btrfs_root, dead_list);
6431 list_del_init(&reloc_root->dead_list);
6432
6433 BUG_ON(reloc_root->commit_root != NULL);
6434 while (1) {
6435 trans = btrfs_join_transaction(root, 1);
6436 BUG_ON(!trans);
6437
6438 mutex_lock(&root->fs_info->drop_mutex);
6439 ret = btrfs_drop_snapshot(trans, reloc_root);
6440 if (ret != -EAGAIN)
6441 break;
6442 mutex_unlock(&root->fs_info->drop_mutex);
6443
6444 nr = trans->blocks_used;
6445 ret = btrfs_end_transaction(trans, root);
6446 BUG_ON(ret);
6447 btrfs_btree_balance_dirty(root, nr);
6448 }
6449
6450 free_extent_buffer(reloc_root->node);
6451
6452 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6453 &reloc_root->root_key);
6454 BUG_ON(ret);
6455 mutex_unlock(&root->fs_info->drop_mutex);
6456
6457 nr = trans->blocks_used;
6458 ret = btrfs_end_transaction(trans, root);
6459 BUG_ON(ret);
6460 btrfs_btree_balance_dirty(root, nr);
6461
6462 kfree(prev_root);
6463 prev_root = reloc_root;
6464 }
6465 if (prev_root) {
6466 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6467 kfree(prev_root);
6468 }
6469 return 0;
6470}
6471
6472int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6473{
6474 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6475 return 0;
6476}
6477
6478int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6479{
6480 struct btrfs_root *reloc_root;
6481 struct btrfs_trans_handle *trans;
6482 struct btrfs_key location;
6483 int found;
6484 int ret;
6485
6486 mutex_lock(&root->fs_info->tree_reloc_mutex);
6487 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6488 BUG_ON(ret);
6489 found = !list_empty(&root->fs_info->dead_reloc_roots);
6490 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6491
6492 if (found) {
6493 trans = btrfs_start_transaction(root, 1);
6494 BUG_ON(!trans);
6495 ret = btrfs_commit_transaction(trans, root);
6496 BUG_ON(ret);
6497 }
6498
6499 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6500 location.offset = (u64)-1;
6501 location.type = BTRFS_ROOT_ITEM_KEY;
6502
6503 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6504 BUG_ON(!reloc_root);
6505 btrfs_orphan_cleanup(reloc_root);
6506 return 0;
6507}
6508
Chris Masond3977122009-01-05 21:25:51 -05006509static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006510 struct btrfs_root *root)
6511{
6512 struct btrfs_root *reloc_root;
6513 struct extent_buffer *eb;
6514 struct btrfs_root_item *root_item;
6515 struct btrfs_key root_key;
6516 int ret;
6517
6518 BUG_ON(!root->ref_cows);
6519 if (root->reloc_root)
6520 return 0;
6521
6522 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6523 BUG_ON(!root_item);
6524
6525 ret = btrfs_copy_root(trans, root, root->commit_root,
6526 &eb, BTRFS_TREE_RELOC_OBJECTID);
6527 BUG_ON(ret);
6528
6529 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6530 root_key.offset = root->root_key.objectid;
6531 root_key.type = BTRFS_ROOT_ITEM_KEY;
6532
6533 memcpy(root_item, &root->root_item, sizeof(root_item));
6534 btrfs_set_root_refs(root_item, 0);
6535 btrfs_set_root_bytenr(root_item, eb->start);
6536 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006537 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006538
6539 btrfs_tree_unlock(eb);
6540 free_extent_buffer(eb);
6541
6542 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6543 &root_key, root_item);
6544 BUG_ON(ret);
6545 kfree(root_item);
6546
6547 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6548 &root_key);
6549 BUG_ON(!reloc_root);
6550 reloc_root->last_trans = trans->transid;
6551 reloc_root->commit_root = NULL;
6552 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6553
6554 root->reloc_root = reloc_root;
6555 return 0;
6556}
6557
6558/*
6559 * Core function of space balance.
6560 *
6561 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006562 * counted roots. There is one reloc tree for each subvol, and all
6563 * reloc trees share same root key objectid. Reloc trees are snapshots
6564 * of the latest committed roots of subvols (root->commit_root).
6565 *
6566 * To relocate a tree block referenced by a subvol, there are two steps.
6567 * COW the block through subvol's reloc tree, then update block pointer
6568 * in the subvol to point to the new block. Since all reloc trees share
6569 * same root key objectid, doing special handing for tree blocks owned
6570 * by them is easy. Once a tree block has been COWed in one reloc tree,
6571 * we can use the resulting new block directly when the same block is
6572 * required to COW again through other reloc trees. By this way, relocated
6573 * tree blocks are shared between reloc trees, so they are also shared
6574 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006575 */
Chris Masond3977122009-01-05 21:25:51 -05006576static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006577 struct btrfs_root *root,
6578 struct btrfs_path *path,
6579 struct btrfs_key *first_key,
6580 struct btrfs_ref_path *ref_path,
6581 struct btrfs_block_group_cache *group,
6582 struct inode *reloc_inode)
6583{
6584 struct btrfs_root *reloc_root;
6585 struct extent_buffer *eb = NULL;
6586 struct btrfs_key *keys;
6587 u64 *nodes;
6588 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006589 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006590 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006591 int ret;
6592
6593 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6594 lowest_level = ref_path->owner_objectid;
6595
Yan Zhengf82d02d2008-10-29 14:49:05 -04006596 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006597 path->lowest_level = lowest_level;
6598 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6599 BUG_ON(ret < 0);
6600 path->lowest_level = 0;
6601 btrfs_release_path(root, path);
6602 return 0;
6603 }
6604
Zheng Yan1a40e232008-09-26 10:09:34 -04006605 mutex_lock(&root->fs_info->tree_reloc_mutex);
6606 ret = init_reloc_tree(trans, root);
6607 BUG_ON(ret);
6608 reloc_root = root->reloc_root;
6609
Yan Zhengf82d02d2008-10-29 14:49:05 -04006610 shared_level = ref_path->shared_level;
6611 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006612
Yan Zhengf82d02d2008-10-29 14:49:05 -04006613 keys = ref_path->node_keys;
6614 nodes = ref_path->new_nodes;
6615 memset(&keys[shared_level + 1], 0,
6616 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6617 memset(&nodes[shared_level + 1], 0,
6618 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006619
Yan Zhengf82d02d2008-10-29 14:49:05 -04006620 if (nodes[lowest_level] == 0) {
6621 path->lowest_level = lowest_level;
6622 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6623 0, 1);
6624 BUG_ON(ret);
6625 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6626 eb = path->nodes[level];
6627 if (!eb || eb == reloc_root->node)
6628 break;
6629 nodes[level] = eb->start;
6630 if (level == 0)
6631 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6632 else
6633 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6634 }
Yan Zheng2b820322008-11-17 21:11:30 -05006635 if (nodes[0] &&
6636 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006637 eb = path->nodes[0];
6638 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6639 group, reloc_inode);
6640 BUG_ON(ret);
6641 }
6642 btrfs_release_path(reloc_root, path);
6643 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006644 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006645 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006646 BUG_ON(ret);
6647 }
6648
Zheng Yan1a40e232008-09-26 10:09:34 -04006649 /*
6650 * replace tree blocks in the fs tree with tree blocks in
6651 * the reloc tree.
6652 */
6653 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6654 BUG_ON(ret < 0);
6655
6656 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006657 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6658 0, 0);
6659 BUG_ON(ret);
6660 extent_buffer_get(path->nodes[0]);
6661 eb = path->nodes[0];
6662 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006663 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6664 BUG_ON(ret);
6665 free_extent_buffer(eb);
6666 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006667
Yan Zhengf82d02d2008-10-29 14:49:05 -04006668 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006669 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006670 return 0;
6671}
6672
Chris Masond3977122009-01-05 21:25:51 -05006673static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006674 struct btrfs_root *root,
6675 struct btrfs_path *path,
6676 struct btrfs_key *first_key,
6677 struct btrfs_ref_path *ref_path)
6678{
6679 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006680
6681 ret = relocate_one_path(trans, root, path, first_key,
6682 ref_path, NULL, NULL);
6683 BUG_ON(ret);
6684
Zheng Yan1a40e232008-09-26 10:09:34 -04006685 return 0;
6686}
6687
Chris Masond3977122009-01-05 21:25:51 -05006688static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006689 struct btrfs_root *extent_root,
6690 struct btrfs_path *path,
6691 struct btrfs_key *extent_key)
6692{
6693 int ret;
6694
Zheng Yan1a40e232008-09-26 10:09:34 -04006695 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6696 if (ret)
6697 goto out;
6698 ret = btrfs_del_item(trans, extent_root, path);
6699out:
Chris Masonedbd8d42007-12-21 16:27:24 -05006700 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006701 return ret;
6702}
6703
Chris Masond3977122009-01-05 21:25:51 -05006704static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006705 struct btrfs_ref_path *ref_path)
6706{
6707 struct btrfs_key root_key;
6708
6709 root_key.objectid = ref_path->root_objectid;
6710 root_key.type = BTRFS_ROOT_ITEM_KEY;
6711 if (is_cowonly_root(ref_path->root_objectid))
6712 root_key.offset = 0;
6713 else
6714 root_key.offset = (u64)-1;
6715
6716 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6717}
6718
Chris Masond3977122009-01-05 21:25:51 -05006719static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006720 struct btrfs_path *path,
6721 struct btrfs_key *extent_key,
6722 struct btrfs_block_group_cache *group,
6723 struct inode *reloc_inode, int pass)
6724{
6725 struct btrfs_trans_handle *trans;
6726 struct btrfs_root *found_root;
6727 struct btrfs_ref_path *ref_path = NULL;
6728 struct disk_extent *new_extents = NULL;
6729 int nr_extents = 0;
6730 int loops;
6731 int ret;
6732 int level;
6733 struct btrfs_key first_key;
6734 u64 prev_block = 0;
6735
Zheng Yan1a40e232008-09-26 10:09:34 -04006736
6737 trans = btrfs_start_transaction(extent_root, 1);
6738 BUG_ON(!trans);
6739
6740 if (extent_key->objectid == 0) {
6741 ret = del_extent_zero(trans, extent_root, path, extent_key);
6742 goto out;
6743 }
6744
6745 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6746 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05006747 ret = -ENOMEM;
6748 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006749 }
6750
6751 for (loops = 0; ; loops++) {
6752 if (loops == 0) {
6753 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6754 extent_key->objectid);
6755 } else {
6756 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6757 }
6758 if (ret < 0)
6759 goto out;
6760 if (ret > 0)
6761 break;
6762
6763 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6764 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6765 continue;
6766
6767 found_root = read_ref_root(extent_root->fs_info, ref_path);
6768 BUG_ON(!found_root);
6769 /*
6770 * for reference counted tree, only process reference paths
6771 * rooted at the latest committed root.
6772 */
6773 if (found_root->ref_cows &&
6774 ref_path->root_generation != found_root->root_key.offset)
6775 continue;
6776
6777 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6778 if (pass == 0) {
6779 /*
6780 * copy data extents to new locations
6781 */
6782 u64 group_start = group->key.objectid;
6783 ret = relocate_data_extent(reloc_inode,
6784 extent_key,
6785 group_start);
6786 if (ret < 0)
6787 goto out;
6788 break;
6789 }
6790 level = 0;
6791 } else {
6792 level = ref_path->owner_objectid;
6793 }
6794
6795 if (prev_block != ref_path->nodes[level]) {
6796 struct extent_buffer *eb;
6797 u64 block_start = ref_path->nodes[level];
6798 u64 block_size = btrfs_level_size(found_root, level);
6799
6800 eb = read_tree_block(found_root, block_start,
6801 block_size, 0);
6802 btrfs_tree_lock(eb);
6803 BUG_ON(level != btrfs_header_level(eb));
6804
6805 if (level == 0)
6806 btrfs_item_key_to_cpu(eb, &first_key, 0);
6807 else
6808 btrfs_node_key_to_cpu(eb, &first_key, 0);
6809
6810 btrfs_tree_unlock(eb);
6811 free_extent_buffer(eb);
6812 prev_block = block_start;
6813 }
6814
Yan Zheng24562422009-02-12 14:14:53 -05006815 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006816 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05006817 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006818 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6819 /*
6820 * try to update data extent references while
6821 * keeping metadata shared between snapshots.
6822 */
6823 if (pass == 1) {
6824 ret = relocate_one_path(trans, found_root,
6825 path, &first_key, ref_path,
6826 group, reloc_inode);
6827 if (ret < 0)
6828 goto out;
6829 continue;
6830 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006831 /*
6832 * use fallback method to process the remaining
6833 * references.
6834 */
6835 if (!new_extents) {
6836 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04006837 new_extents = kmalloc(sizeof(*new_extents),
6838 GFP_NOFS);
6839 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006840 ret = get_new_locations(reloc_inode,
6841 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04006842 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04006843 &new_extents,
6844 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006845 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04006846 goto out;
6847 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006848 ret = replace_one_extent(trans, found_root,
6849 path, extent_key,
6850 &first_key, ref_path,
6851 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05006852 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006853 ret = relocate_tree_block(trans, found_root, path,
6854 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006855 }
6856 if (ret < 0)
6857 goto out;
6858 }
6859 ret = 0;
6860out:
6861 btrfs_end_transaction(trans, extent_root);
6862 kfree(new_extents);
6863 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006864 return ret;
6865}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006866#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006867
Chris Masonec44a352008-04-28 15:29:52 -04006868static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6869{
6870 u64 num_devices;
6871 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6872 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6873
Yan Zheng2b820322008-11-17 21:11:30 -05006874 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04006875 if (num_devices == 1) {
6876 stripped |= BTRFS_BLOCK_GROUP_DUP;
6877 stripped = flags & ~stripped;
6878
6879 /* turn raid0 into single device chunks */
6880 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6881 return stripped;
6882
6883 /* turn mirroring into duplication */
6884 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6885 BTRFS_BLOCK_GROUP_RAID10))
6886 return stripped | BTRFS_BLOCK_GROUP_DUP;
6887 return flags;
6888 } else {
6889 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006890 if (flags & stripped)
6891 return flags;
6892
6893 stripped |= BTRFS_BLOCK_GROUP_DUP;
6894 stripped = flags & ~stripped;
6895
6896 /* switch duplicated blocks with raid1 */
6897 if (flags & BTRFS_BLOCK_GROUP_DUP)
6898 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6899
6900 /* turn single device chunks into raid0 */
6901 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6902 }
6903 return flags;
6904}
6905
Christoph Hellwigb2950862008-12-02 09:54:17 -05006906static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04006907 struct btrfs_block_group_cache *shrink_block_group,
6908 int force)
6909{
6910 struct btrfs_trans_handle *trans;
6911 u64 new_alloc_flags;
6912 u64 calc;
6913
Chris Masonc286ac42008-07-22 23:06:41 -04006914 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006915 if (btrfs_block_group_used(&shrink_block_group->item) +
6916 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04006917 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006918
Chris Mason0ef3e662008-05-24 14:04:53 -04006919 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006920 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04006921
Chris Mason0ef3e662008-05-24 14:04:53 -04006922 new_alloc_flags = update_block_group_flags(root,
6923 shrink_block_group->flags);
6924 if (new_alloc_flags != shrink_block_group->flags) {
6925 calc =
6926 btrfs_block_group_used(&shrink_block_group->item);
6927 } else {
6928 calc = shrink_block_group->key.offset;
6929 }
Chris Masonc286ac42008-07-22 23:06:41 -04006930 spin_unlock(&shrink_block_group->lock);
6931
Chris Mason0ef3e662008-05-24 14:04:53 -04006932 do_chunk_alloc(trans, root->fs_info->extent_root,
6933 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04006934
Chris Mason0ef3e662008-05-24 14:04:53 -04006935 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04006936 } else
6937 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04006938 return 0;
6939}
6940
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006941
6942int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
6943 struct btrfs_block_group_cache *group)
6944
6945{
6946 __alloc_chunk_for_shrink(root, group, 1);
6947 set_block_group_readonly(group);
6948 return 0;
6949}
6950
6951#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006952static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
6953 struct btrfs_root *root,
6954 u64 objectid, u64 size)
6955{
6956 struct btrfs_path *path;
6957 struct btrfs_inode_item *item;
6958 struct extent_buffer *leaf;
6959 int ret;
6960
6961 path = btrfs_alloc_path();
6962 if (!path)
6963 return -ENOMEM;
6964
Chris Masonb9473432009-03-13 11:00:37 -04006965 path->leave_spinning = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006966 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
6967 if (ret)
6968 goto out;
6969
6970 leaf = path->nodes[0];
6971 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
6972 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
6973 btrfs_set_inode_generation(leaf, item, 1);
6974 btrfs_set_inode_size(leaf, item, size);
6975 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05006976 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006977 btrfs_mark_buffer_dirty(leaf);
6978 btrfs_release_path(root, path);
6979out:
6980 btrfs_free_path(path);
6981 return ret;
6982}
6983
Chris Masond3977122009-01-05 21:25:51 -05006984static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006985 struct btrfs_block_group_cache *group)
6986{
6987 struct inode *inode = NULL;
6988 struct btrfs_trans_handle *trans;
6989 struct btrfs_root *root;
6990 struct btrfs_key root_key;
6991 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
6992 int err = 0;
6993
6994 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6995 root_key.type = BTRFS_ROOT_ITEM_KEY;
6996 root_key.offset = (u64)-1;
6997 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
6998 if (IS_ERR(root))
6999 return ERR_CAST(root);
7000
7001 trans = btrfs_start_transaction(root, 1);
7002 BUG_ON(!trans);
7003
7004 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
7005 if (err)
7006 goto out;
7007
7008 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
7009 BUG_ON(err);
7010
7011 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04007012 group->key.offset, 0, group->key.offset,
7013 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007014 BUG_ON(err);
7015
7016 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
7017 if (inode->i_state & I_NEW) {
7018 BTRFS_I(inode)->root = root;
7019 BTRFS_I(inode)->location.objectid = objectid;
7020 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
7021 BTRFS_I(inode)->location.offset = 0;
7022 btrfs_read_locked_inode(inode);
7023 unlock_new_inode(inode);
7024 BUG_ON(is_bad_inode(inode));
7025 } else {
7026 BUG_ON(1);
7027 }
Yan Zheng17d217f2008-12-12 10:03:38 -05007028 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04007029
7030 err = btrfs_orphan_add(trans, inode);
7031out:
7032 btrfs_end_transaction(trans, root);
7033 if (err) {
7034 if (inode)
7035 iput(inode);
7036 inode = ERR_PTR(err);
7037 }
7038 return inode;
7039}
7040
Yan Zheng17d217f2008-12-12 10:03:38 -05007041int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
7042{
7043
7044 struct btrfs_ordered_sum *sums;
7045 struct btrfs_sector_sum *sector_sum;
7046 struct btrfs_ordered_extent *ordered;
7047 struct btrfs_root *root = BTRFS_I(inode)->root;
7048 struct list_head list;
7049 size_t offset;
7050 int ret;
7051 u64 disk_bytenr;
7052
7053 INIT_LIST_HEAD(&list);
7054
7055 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
7056 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
7057
7058 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05007059 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05007060 disk_bytenr + len - 1, &list);
7061
7062 while (!list_empty(&list)) {
7063 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
7064 list_del_init(&sums->list);
7065
7066 sector_sum = sums->sums;
7067 sums->bytenr = ordered->start;
7068
7069 offset = 0;
7070 while (offset < sums->len) {
7071 sector_sum->bytenr += ordered->start - disk_bytenr;
7072 sector_sum++;
7073 offset += root->sectorsize;
7074 }
7075
7076 btrfs_add_ordered_sum(inode, ordered, sums);
7077 }
7078 btrfs_put_ordered_extent(ordered);
7079 return 0;
7080}
7081
Zheng Yan1a40e232008-09-26 10:09:34 -04007082int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05007083{
7084 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05007085 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04007086 struct btrfs_fs_info *info = root->fs_info;
7087 struct extent_buffer *leaf;
7088 struct inode *reloc_inode;
7089 struct btrfs_block_group_cache *block_group;
7090 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04007091 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05007092 u64 cur_byte;
7093 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05007094 u32 nritems;
7095 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04007096 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04007097 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007098
Chris Masonedbd8d42007-12-21 16:27:24 -05007099 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04007100
7101 block_group = btrfs_lookup_block_group(info, group_start);
7102 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007103
Chris Masond3977122009-01-05 21:25:51 -05007104 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04007105 (unsigned long long)block_group->key.objectid,
7106 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04007107
Zheng Yan1a40e232008-09-26 10:09:34 -04007108 path = btrfs_alloc_path();
7109 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04007110
Zheng Yan1a40e232008-09-26 10:09:34 -04007111 reloc_inode = create_reloc_inode(info, block_group);
7112 BUG_ON(IS_ERR(reloc_inode));
7113
Zheng Yan1a40e232008-09-26 10:09:34 -04007114 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05007115 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007116
Zheng Yan1a40e232008-09-26 10:09:34 -04007117 btrfs_start_delalloc_inodes(info->tree_root);
7118 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04007119again:
Yan Zhengd899e052008-10-30 14:25:28 -04007120 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007121 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04007122 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007123 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05007124 key.offset = 0;
7125 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05007126 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05007127
Zheng Yan1a40e232008-09-26 10:09:34 -04007128 trans = btrfs_start_transaction(info->tree_root, 1);
7129 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04007130
Zheng Yan1a40e232008-09-26 10:09:34 -04007131 mutex_lock(&root->fs_info->cleaner_mutex);
7132 btrfs_clean_old_snapshots(info->tree_root);
7133 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
7134 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04007135
Chris Mason56bec292009-03-13 10:10:06 -04007136 trans = btrfs_start_transaction(info->tree_root, 1);
7137 btrfs_commit_transaction(trans, info->tree_root);
7138
Chris Masond3977122009-01-05 21:25:51 -05007139 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05007140 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7141 if (ret < 0)
7142 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04007143next:
Chris Masonedbd8d42007-12-21 16:27:24 -05007144 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05007145 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05007146 if (path->slots[0] >= nritems) {
7147 ret = btrfs_next_leaf(root, path);
7148 if (ret < 0)
7149 goto out;
7150 if (ret == 1) {
7151 ret = 0;
7152 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05007153 }
Yan73e48b22008-01-03 14:14:39 -05007154 leaf = path->nodes[0];
7155 nritems = btrfs_header_nritems(leaf);
7156 }
7157
Zheng Yan1a40e232008-09-26 10:09:34 -04007158 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05007159
Zheng Yan1a40e232008-09-26 10:09:34 -04007160 if (key.objectid >= block_group->key.objectid +
7161 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04007162 break;
7163
Chris Mason725c8462008-01-04 16:47:16 -05007164 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05007165 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007166 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05007167 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007168 continue;
Chris Mason725c8462008-01-04 16:47:16 -05007169 }
7170 progress = 1;
7171
Zheng Yan1a40e232008-09-26 10:09:34 -04007172 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
7173 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05007174 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05007175 goto next;
7176 }
Yan73e48b22008-01-03 14:14:39 -05007177
Chris Masonedbd8d42007-12-21 16:27:24 -05007178 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04007179 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05007180 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007181
7182 __alloc_chunk_for_shrink(root, block_group, 0);
7183 ret = relocate_one_extent(root, path, &key, block_group,
7184 reloc_inode, pass);
7185 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04007186 if (ret > 0)
7187 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04007188
7189 key.objectid = cur_byte;
7190 key.type = 0;
7191 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007192 }
7193
7194 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007195
7196 if (pass == 0) {
7197 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
7198 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04007199 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007200
7201 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05007202 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04007203 (unsigned long long)total_found, pass);
7204 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04007205 if (total_found == skipped && pass > 2) {
7206 iput(reloc_inode);
7207 reloc_inode = create_reloc_inode(info, block_group);
7208 pass = 0;
7209 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007210 goto again;
7211 }
7212
Zheng Yan1a40e232008-09-26 10:09:34 -04007213 /* delete reloc_inode */
7214 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04007215
Zheng Yan1a40e232008-09-26 10:09:34 -04007216 /* unpin extents in this range */
7217 trans = btrfs_start_transaction(info->tree_root, 1);
7218 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04007219
Zheng Yan1a40e232008-09-26 10:09:34 -04007220 spin_lock(&block_group->lock);
7221 WARN_ON(block_group->pinned > 0);
7222 WARN_ON(block_group->reserved > 0);
7223 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
7224 spin_unlock(&block_group->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007225 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007226 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007227out:
Zheng Yan1a40e232008-09-26 10:09:34 -04007228 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007229 return ret;
7230}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007231#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007232
Christoph Hellwigb2950862008-12-02 09:54:17 -05007233static int find_first_block_group(struct btrfs_root *root,
7234 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007235{
Chris Mason925baed2008-06-25 16:01:30 -04007236 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007237 struct btrfs_key found_key;
7238 struct extent_buffer *leaf;
7239 int slot;
7240
7241 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7242 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007243 goto out;
7244
Chris Masond3977122009-01-05 21:25:51 -05007245 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007246 slot = path->slots[0];
7247 leaf = path->nodes[0];
7248 if (slot >= btrfs_header_nritems(leaf)) {
7249 ret = btrfs_next_leaf(root, path);
7250 if (ret == 0)
7251 continue;
7252 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007253 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007254 break;
7255 }
7256 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7257
7258 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007259 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7260 ret = 0;
7261 goto out;
7262 }
Chris Mason0b86a832008-03-24 15:01:56 -04007263 path->slots[0]++;
7264 }
7265 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04007266out:
Chris Mason0b86a832008-03-24 15:01:56 -04007267 return ret;
7268}
7269
Zheng Yan1a40e232008-09-26 10:09:34 -04007270int btrfs_free_block_groups(struct btrfs_fs_info *info)
7271{
7272 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007273 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007274 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007275 struct rb_node *n;
7276
Yan Zheng11833d62009-09-11 16:11:19 -04007277 down_write(&info->extent_commit_sem);
7278 while (!list_empty(&info->caching_block_groups)) {
7279 caching_ctl = list_entry(info->caching_block_groups.next,
7280 struct btrfs_caching_control, list);
7281 list_del(&caching_ctl->list);
7282 put_caching_control(caching_ctl);
7283 }
7284 up_write(&info->extent_commit_sem);
7285
Zheng Yan1a40e232008-09-26 10:09:34 -04007286 spin_lock(&info->block_group_cache_lock);
7287 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7288 block_group = rb_entry(n, struct btrfs_block_group_cache,
7289 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007290 rb_erase(&block_group->cache_node,
7291 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007292 spin_unlock(&info->block_group_cache_lock);
7293
Josef Bacik80eb2342008-10-29 14:49:05 -04007294 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007295 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007296 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007297
Josef Bacik817d52f2009-07-13 21:29:25 -04007298 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007299 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007300
7301 btrfs_remove_free_space_cache(block_group);
7302
Yan Zhengd2fb3432008-12-11 16:30:39 -05007303 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04007304 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007305
7306 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007307 }
7308 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007309
7310 /* now that all the block groups are freed, go through and
7311 * free all the space_info structs. This is only called during
7312 * the final stages of unmount, and so we know nobody is
7313 * using them. We call synchronize_rcu() once before we start,
7314 * just to be on the safe side.
7315 */
7316 synchronize_rcu();
7317
7318 while(!list_empty(&info->space_info)) {
7319 space_info = list_entry(info->space_info.next,
7320 struct btrfs_space_info,
7321 list);
7322
7323 list_del(&space_info->list);
7324 kfree(space_info);
7325 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007326 return 0;
7327}
7328
Chris Mason9078a3e2007-04-26 16:46:15 -04007329int btrfs_read_block_groups(struct btrfs_root *root)
7330{
7331 struct btrfs_path *path;
7332 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007333 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007334 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007335 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007336 struct btrfs_key key;
7337 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007338 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007339
Chris Masonbe744172007-05-06 10:15:01 -04007340 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007341 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007342 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007343 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007344 path = btrfs_alloc_path();
7345 if (!path)
7346 return -ENOMEM;
7347
Chris Masond3977122009-01-05 21:25:51 -05007348 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007349 ret = find_first_block_group(root, path, &key);
7350 if (ret > 0) {
7351 ret = 0;
7352 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007353 }
Chris Mason0b86a832008-03-24 15:01:56 -04007354 if (ret != 0)
7355 goto error;
7356
Chris Mason5f39d392007-10-15 16:14:19 -04007357 leaf = path->nodes[0];
7358 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007359 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007360 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007361 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04007362 break;
7363 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007364
Yan Zhengd2fb3432008-12-11 16:30:39 -05007365 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007366 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007367 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007368 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007369 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007370 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007371
7372 /*
7373 * we only want to have 32k of ram per block group for keeping
7374 * track of free space, and if we pass 1/2 of that we want to
7375 * start converting things over to using bitmaps
7376 */
7377 cache->extents_thresh = ((1024 * 32) / 2) /
7378 sizeof(struct btrfs_free_space);
7379
Chris Mason5f39d392007-10-15 16:14:19 -04007380 read_extent_buffer(leaf, &cache->item,
7381 btrfs_item_ptr_offset(leaf, path->slots[0]),
7382 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007383 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007384
Chris Mason9078a3e2007-04-26 16:46:15 -04007385 key.objectid = found_key.objectid + found_key.offset;
7386 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007387 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007388 cache->sectorsize = root->sectorsize;
7389
Josef Bacik817d52f2009-07-13 21:29:25 -04007390 /*
7391 * check for two cases, either we are full, and therefore
7392 * don't need to bother with the caching work since we won't
7393 * find any space, or we are empty, and we can just add all
7394 * the space in and be done with it. This saves us _alot_ of
7395 * time, particularly in the full case.
7396 */
7397 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007398 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007399 cache->cached = BTRFS_CACHE_FINISHED;
7400 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007401 exclude_super_stripes(root, cache);
7402 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007403 cache->cached = BTRFS_CACHE_FINISHED;
7404 add_new_free_space(cache, root->fs_info,
7405 found_key.objectid,
7406 found_key.objectid +
7407 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007408 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007409 }
Chris Mason96b51792007-10-15 16:15:19 -04007410
Chris Mason6324fbf2008-03-24 15:01:59 -04007411 ret = update_space_info(info, cache->flags, found_key.offset,
7412 btrfs_block_group_used(&cache->item),
7413 &space_info);
7414 BUG_ON(ret);
7415 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04007416 down_write(&space_info->groups_sem);
7417 list_add_tail(&cache->list, &space_info->block_groups);
7418 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007419
Josef Bacik0f9dd462008-09-23 13:14:11 -04007420 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7421 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007422
7423 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007424 if (btrfs_chunk_readonly(root, cache->key.objectid))
7425 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007426 }
Chris Mason0b86a832008-03-24 15:01:56 -04007427 ret = 0;
7428error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007429 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007430 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007431}
Chris Mason6324fbf2008-03-24 15:01:59 -04007432
7433int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7434 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007435 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007436 u64 size)
7437{
7438 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007439 struct btrfs_root *extent_root;
7440 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007441
7442 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007443
Chris Mason12fcfd22009-03-24 10:24:20 -04007444 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007445
Chris Mason8f18cf12008-04-25 16:53:30 -04007446 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007447 if (!cache)
7448 return -ENOMEM;
7449
Chris Masone17cade2008-04-15 15:41:47 -04007450 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007451 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007452 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007453 cache->sectorsize = root->sectorsize;
7454
7455 /*
7456 * we only want to have 32k of ram per block group for keeping track
7457 * of free space, and if we pass 1/2 of that we want to start
7458 * converting things over to using bitmaps
7459 */
7460 cache->extents_thresh = ((1024 * 32) / 2) /
7461 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007462 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007463 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007464 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007465 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007466 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007467
Chris Mason6324fbf2008-03-24 15:01:59 -04007468 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007469 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7470 cache->flags = type;
7471 btrfs_set_block_group_flags(&cache->item, type);
7472
Yan Zheng11833d62009-09-11 16:11:19 -04007473 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007474 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007475 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007476
Josef Bacik817d52f2009-07-13 21:29:25 -04007477 add_new_free_space(cache, root->fs_info, chunk_offset,
7478 chunk_offset + size);
7479
Yan Zheng11833d62009-09-11 16:11:19 -04007480 free_excluded_extents(root, cache);
7481
Chris Mason6324fbf2008-03-24 15:01:59 -04007482 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7483 &cache->space_info);
7484 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04007485 down_write(&cache->space_info->groups_sem);
7486 list_add_tail(&cache->list, &cache->space_info->block_groups);
7487 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007488
Josef Bacik0f9dd462008-09-23 13:14:11 -04007489 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7490 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007491
Chris Mason6324fbf2008-03-24 15:01:59 -04007492 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7493 sizeof(cache->item));
7494 BUG_ON(ret);
7495
Chris Masond18a2c42008-04-04 15:40:00 -04007496 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007497
Chris Mason6324fbf2008-03-24 15:01:59 -04007498 return 0;
7499}
Zheng Yan1a40e232008-09-26 10:09:34 -04007500
7501int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7502 struct btrfs_root *root, u64 group_start)
7503{
7504 struct btrfs_path *path;
7505 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007506 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007507 struct btrfs_key key;
7508 int ret;
7509
Zheng Yan1a40e232008-09-26 10:09:34 -04007510 root = root->fs_info->extent_root;
7511
7512 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7513 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007514 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007515
7516 memcpy(&key, &block_group->key, sizeof(key));
7517
Chris Mason44fb5512009-06-04 15:34:51 -04007518 /* make sure this block group isn't part of an allocation cluster */
7519 cluster = &root->fs_info->data_alloc_cluster;
7520 spin_lock(&cluster->refill_lock);
7521 btrfs_return_cluster_to_free_space(block_group, cluster);
7522 spin_unlock(&cluster->refill_lock);
7523
7524 /*
7525 * make sure this block group isn't part of a metadata
7526 * allocation cluster
7527 */
7528 cluster = &root->fs_info->meta_alloc_cluster;
7529 spin_lock(&cluster->refill_lock);
7530 btrfs_return_cluster_to_free_space(block_group, cluster);
7531 spin_unlock(&cluster->refill_lock);
7532
Zheng Yan1a40e232008-09-26 10:09:34 -04007533 path = btrfs_alloc_path();
7534 BUG_ON(!path);
7535
Yan Zheng3dfdb932009-01-21 10:49:16 -05007536 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007537 rb_erase(&block_group->cache_node,
7538 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007539 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007540
Josef Bacik80eb2342008-10-29 14:49:05 -04007541 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007542 /*
7543 * we must use list_del_init so people can check to see if they
7544 * are still on the list after taking the semaphore
7545 */
7546 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007547 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007548
Josef Bacik817d52f2009-07-13 21:29:25 -04007549 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007550 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007551
7552 btrfs_remove_free_space_cache(block_group);
7553
Yan Zhengc146afa2008-11-12 14:34:12 -05007554 spin_lock(&block_group->space_info->lock);
7555 block_group->space_info->total_bytes -= block_group->key.offset;
7556 block_group->space_info->bytes_readonly -= block_group->key.offset;
7557 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007558
7559 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007560
Chris Masonfa9c0d72009-04-03 09:47:43 -04007561 btrfs_put_block_group(block_group);
7562 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007563
7564 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7565 if (ret > 0)
7566 ret = -EIO;
7567 if (ret < 0)
7568 goto out;
7569
7570 ret = btrfs_del_item(trans, root, path);
7571out:
7572 btrfs_free_path(path);
7573 return ret;
7574}