blob: bf7782fc59399f03db5d598232ad2060faa6380a [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050025#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050026#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027#include "ctree.h"
28#include "disk-io.h"
29#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040030#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040032#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040033#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Josef Bacikf3465ca2008-11-12 14:19:50 -050035static int update_block_group(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 u64 bytenr, u64 num_bytes, int alloc,
38 int mark_free);
Yan Zheng11833d62009-09-11 16:11:19 -040039static int update_reserved_extents(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040041static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root,
43 u64 bytenr, u64 num_bytes, u64 parent,
44 u64 root_objectid, u64 owner_objectid,
45 u64 owner_offset, int refs_to_drop,
46 struct btrfs_delayed_extent_op *extra_op);
47static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
48 struct extent_buffer *leaf,
49 struct btrfs_extent_item *ei);
50static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root,
52 u64 parent, u64 root_objectid,
53 u64 flags, u64 owner, u64 offset,
54 struct btrfs_key *ins, int ref_mod);
55static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 parent, u64 root_objectid,
58 u64 flags, struct btrfs_disk_key *key,
59 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050060static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61 struct btrfs_root *extent_root, u64 alloc_bytes,
62 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040063static int pin_down_bytes(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root,
65 struct btrfs_path *path,
66 u64 bytenr, u64 num_bytes,
67 int is_data, int reserved,
68 struct extent_buffer **must_clean);
69static int find_next_key(struct btrfs_path *path, int level,
70 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040071static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
72 int dump_block_groups);
Josef Bacik6a632092009-02-20 11:00:09 -050073
Josef Bacik817d52f2009-07-13 21:29:25 -040074static noinline int
75block_group_cache_done(struct btrfs_block_group_cache *cache)
76{
77 smp_mb();
78 return cache->cached == BTRFS_CACHE_FINISHED;
79}
80
Josef Bacik0f9dd462008-09-23 13:14:11 -040081static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
82{
83 return (cache->flags & bits) == bits;
84}
85
86/*
87 * this adds the block group to the fs_info rb tree for the block group
88 * cache
89 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050090static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040091 struct btrfs_block_group_cache *block_group)
92{
93 struct rb_node **p;
94 struct rb_node *parent = NULL;
95 struct btrfs_block_group_cache *cache;
96
97 spin_lock(&info->block_group_cache_lock);
98 p = &info->block_group_cache_tree.rb_node;
99
100 while (*p) {
101 parent = *p;
102 cache = rb_entry(parent, struct btrfs_block_group_cache,
103 cache_node);
104 if (block_group->key.objectid < cache->key.objectid) {
105 p = &(*p)->rb_left;
106 } else if (block_group->key.objectid > cache->key.objectid) {
107 p = &(*p)->rb_right;
108 } else {
109 spin_unlock(&info->block_group_cache_lock);
110 return -EEXIST;
111 }
112 }
113
114 rb_link_node(&block_group->cache_node, parent, p);
115 rb_insert_color(&block_group->cache_node,
116 &info->block_group_cache_tree);
117 spin_unlock(&info->block_group_cache_lock);
118
119 return 0;
120}
121
122/*
123 * This will return the block group at or after bytenr if contains is 0, else
124 * it will return the block group that contains the bytenr
125 */
126static struct btrfs_block_group_cache *
127block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
128 int contains)
129{
130 struct btrfs_block_group_cache *cache, *ret = NULL;
131 struct rb_node *n;
132 u64 end, start;
133
134 spin_lock(&info->block_group_cache_lock);
135 n = info->block_group_cache_tree.rb_node;
136
137 while (n) {
138 cache = rb_entry(n, struct btrfs_block_group_cache,
139 cache_node);
140 end = cache->key.objectid + cache->key.offset - 1;
141 start = cache->key.objectid;
142
143 if (bytenr < start) {
144 if (!contains && (!ret || start < ret->key.objectid))
145 ret = cache;
146 n = n->rb_left;
147 } else if (bytenr > start) {
148 if (contains && bytenr <= end) {
149 ret = cache;
150 break;
151 }
152 n = n->rb_right;
153 } else {
154 ret = cache;
155 break;
156 }
157 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500158 if (ret)
159 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400160 spin_unlock(&info->block_group_cache_lock);
161
162 return ret;
163}
164
Yan Zheng11833d62009-09-11 16:11:19 -0400165static int add_excluded_extent(struct btrfs_root *root,
166 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400167{
Yan Zheng11833d62009-09-11 16:11:19 -0400168 u64 end = start + num_bytes - 1;
169 set_extent_bits(&root->fs_info->freed_extents[0],
170 start, end, EXTENT_UPTODATE, GFP_NOFS);
171 set_extent_bits(&root->fs_info->freed_extents[1],
172 start, end, EXTENT_UPTODATE, GFP_NOFS);
173 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400174}
175
Yan Zheng11833d62009-09-11 16:11:19 -0400176static void free_excluded_extents(struct btrfs_root *root,
177 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400178{
Yan Zheng11833d62009-09-11 16:11:19 -0400179 u64 start, end;
180
181 start = cache->key.objectid;
182 end = start + cache->key.offset - 1;
183
184 clear_extent_bits(&root->fs_info->freed_extents[0],
185 start, end, EXTENT_UPTODATE, GFP_NOFS);
186 clear_extent_bits(&root->fs_info->freed_extents[1],
187 start, end, EXTENT_UPTODATE, GFP_NOFS);
188}
189
190static int exclude_super_stripes(struct btrfs_root *root,
191 struct btrfs_block_group_cache *cache)
192{
Josef Bacik817d52f2009-07-13 21:29:25 -0400193 u64 bytenr;
194 u64 *logical;
195 int stripe_len;
196 int i, nr, ret;
197
198 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
199 bytenr = btrfs_sb_offset(i);
200 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
201 cache->key.objectid, bytenr,
202 0, &logical, &nr, &stripe_len);
203 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400204
Josef Bacik817d52f2009-07-13 21:29:25 -0400205 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400206 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400207 ret = add_excluded_extent(root, logical[nr],
208 stripe_len);
209 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400210 }
Yan Zheng11833d62009-09-11 16:11:19 -0400211
Josef Bacik817d52f2009-07-13 21:29:25 -0400212 kfree(logical);
213 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400214 return 0;
215}
216
Yan Zheng11833d62009-09-11 16:11:19 -0400217static struct btrfs_caching_control *
218get_caching_control(struct btrfs_block_group_cache *cache)
219{
220 struct btrfs_caching_control *ctl;
221
222 spin_lock(&cache->lock);
223 if (cache->cached != BTRFS_CACHE_STARTED) {
224 spin_unlock(&cache->lock);
225 return NULL;
226 }
227
228 ctl = cache->caching_ctl;
229 atomic_inc(&ctl->count);
230 spin_unlock(&cache->lock);
231 return ctl;
232}
233
234static void put_caching_control(struct btrfs_caching_control *ctl)
235{
236 if (atomic_dec_and_test(&ctl->count))
237 kfree(ctl);
238}
239
Josef Bacik0f9dd462008-09-23 13:14:11 -0400240/*
241 * this is only called by cache_block_group, since we could have freed extents
242 * we need to check the pinned_extents for any extents that can't be used yet
243 * since their free space will be released as soon as the transaction commits.
244 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400245static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400246 struct btrfs_fs_info *info, u64 start, u64 end)
247{
Josef Bacik817d52f2009-07-13 21:29:25 -0400248 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400249 int ret;
250
251 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400252 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400253 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400254 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400255 if (ret)
256 break;
257
258 if (extent_start == start) {
259 start = extent_end + 1;
260 } else if (extent_start > start && extent_start < end) {
261 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400262 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500263 ret = btrfs_add_free_space(block_group, start,
264 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265 BUG_ON(ret);
266 start = extent_end + 1;
267 } else {
268 break;
269 }
270 }
271
272 if (start < end) {
273 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400274 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500275 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400276 BUG_ON(ret);
277 }
278
Josef Bacik817d52f2009-07-13 21:29:25 -0400279 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400280}
281
Josef Bacik817d52f2009-07-13 21:29:25 -0400282static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400283{
Josef Bacik817d52f2009-07-13 21:29:25 -0400284 struct btrfs_block_group_cache *block_group = data;
285 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400286 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
287 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400288 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400289 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400290 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400291 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400292 u64 last = 0;
293 u32 nritems;
294 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400295
Chris Masone37c9e62007-05-09 20:13:14 -0400296 path = btrfs_alloc_path();
297 if (!path)
298 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400299
Yan Zheng11833d62009-09-11 16:11:19 -0400300 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400301 spin_lock(&block_group->space_info->lock);
302 block_group->space_info->bytes_super += block_group->bytes_super;
303 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400304
Josef Bacik817d52f2009-07-13 21:29:25 -0400305 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400306
Chris Mason5cd57b22008-06-25 16:01:30 -0400307 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400308 * We don't want to deadlock with somebody trying to allocate a new
309 * extent for the extent root while also trying to search the extent
310 * root to add free space. So we skip locking and search the commit
311 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400312 */
313 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400314 path->search_commit_root = 1;
315 path->reada = 2;
316
Yan Zhenge4404d62008-12-12 10:03:26 -0500317 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400318 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400319 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400320again:
Yan Zheng11833d62009-09-11 16:11:19 -0400321 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400322 /* need to make sure the commit_root doesn't disappear */
323 down_read(&fs_info->extent_commit_sem);
324
Yan Zheng11833d62009-09-11 16:11:19 -0400325 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400326 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400327 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500328
Yan Zheng11833d62009-09-11 16:11:19 -0400329 leaf = path->nodes[0];
330 nritems = btrfs_header_nritems(leaf);
331
Chris Masond3977122009-01-05 21:25:51 -0500332 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400333 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400334 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400335 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400336 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400337 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400338
Yan Zheng11833d62009-09-11 16:11:19 -0400339 if (path->slots[0] < nritems) {
340 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
341 } else {
342 ret = find_next_key(path, 0, &key);
343 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400344 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400345
Yan Zheng11833d62009-09-11 16:11:19 -0400346 caching_ctl->progress = last;
347 btrfs_release_path(extent_root, path);
348 up_read(&fs_info->extent_commit_sem);
349 mutex_unlock(&caching_ctl->mutex);
350 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400351 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400352 else
353 cond_resched();
354 goto again;
355 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400356
Yan Zheng11833d62009-09-11 16:11:19 -0400357 if (key.objectid < block_group->key.objectid) {
358 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400359 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400360 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400361
Chris Masone37c9e62007-05-09 20:13:14 -0400362 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400363 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400364 break;
Yan7d7d6062007-09-14 16:15:28 -0400365
Yan Zheng11833d62009-09-11 16:11:19 -0400366 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400367 total_found += add_new_free_space(block_group,
368 fs_info, last,
369 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400370 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400371
Yan Zheng11833d62009-09-11 16:11:19 -0400372 if (total_found > (1024 * 1024 * 2)) {
373 total_found = 0;
374 wake_up(&caching_ctl->wait);
375 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400376 }
Chris Masone37c9e62007-05-09 20:13:14 -0400377 path->slots[0]++;
378 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400379 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400380
381 total_found += add_new_free_space(block_group, fs_info, last,
382 block_group->key.objectid +
383 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400384 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400385
386 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400387 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400388 block_group->cached = BTRFS_CACHE_FINISHED;
389 spin_unlock(&block_group->lock);
390
Chris Mason54aa1f42007-06-22 14:16:25 -0400391err:
Chris Masone37c9e62007-05-09 20:13:14 -0400392 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400393 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400394
Yan Zheng11833d62009-09-11 16:11:19 -0400395 free_excluded_extents(extent_root, block_group);
396
397 mutex_unlock(&caching_ctl->mutex);
398 wake_up(&caching_ctl->wait);
399
400 put_caching_control(caching_ctl);
401 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik817d52f2009-07-13 21:29:25 -0400402 return 0;
403}
404
405static int cache_block_group(struct btrfs_block_group_cache *cache)
406{
Yan Zheng11833d62009-09-11 16:11:19 -0400407 struct btrfs_fs_info *fs_info = cache->fs_info;
408 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400409 struct task_struct *tsk;
410 int ret = 0;
411
Yan Zheng11833d62009-09-11 16:11:19 -0400412 smp_mb();
413 if (cache->cached != BTRFS_CACHE_NO)
414 return 0;
415
416 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
417 BUG_ON(!caching_ctl);
418
419 INIT_LIST_HEAD(&caching_ctl->list);
420 mutex_init(&caching_ctl->mutex);
421 init_waitqueue_head(&caching_ctl->wait);
422 caching_ctl->block_group = cache;
423 caching_ctl->progress = cache->key.objectid;
424 /* one for caching kthread, one for caching block group list */
425 atomic_set(&caching_ctl->count, 2);
426
Josef Bacik817d52f2009-07-13 21:29:25 -0400427 spin_lock(&cache->lock);
428 if (cache->cached != BTRFS_CACHE_NO) {
429 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400430 kfree(caching_ctl);
431 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400432 }
Yan Zheng11833d62009-09-11 16:11:19 -0400433 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400434 cache->cached = BTRFS_CACHE_STARTED;
435 spin_unlock(&cache->lock);
436
Yan Zheng11833d62009-09-11 16:11:19 -0400437 down_write(&fs_info->extent_commit_sem);
438 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
439 up_write(&fs_info->extent_commit_sem);
440
441 atomic_inc(&cache->space_info->caching_threads);
442
Josef Bacik817d52f2009-07-13 21:29:25 -0400443 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
444 cache->key.objectid);
445 if (IS_ERR(tsk)) {
446 ret = PTR_ERR(tsk);
447 printk(KERN_ERR "error running thread %d\n", ret);
448 BUG();
449 }
450
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400451 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400452}
453
Josef Bacik0f9dd462008-09-23 13:14:11 -0400454/*
455 * return the block group that starts at or after bytenr
456 */
Chris Masond3977122009-01-05 21:25:51 -0500457static struct btrfs_block_group_cache *
458btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400459{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400460 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400461
Josef Bacik0f9dd462008-09-23 13:14:11 -0400462 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400463
Josef Bacik0f9dd462008-09-23 13:14:11 -0400464 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400465}
466
Josef Bacik0f9dd462008-09-23 13:14:11 -0400467/*
Sankar P9f556842009-05-14 13:52:22 -0400468 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400469 */
Chris Masond3977122009-01-05 21:25:51 -0500470struct btrfs_block_group_cache *btrfs_lookup_block_group(
471 struct btrfs_fs_info *info,
472 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400473{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400474 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400475
Josef Bacik0f9dd462008-09-23 13:14:11 -0400476 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400477
Josef Bacik0f9dd462008-09-23 13:14:11 -0400478 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400479}
Chris Mason0b86a832008-03-24 15:01:56 -0400480
Chris Masonfa9c0d792009-04-03 09:47:43 -0400481void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
Yan Zhengd2fb3432008-12-11 16:30:39 -0500482{
483 if (atomic_dec_and_test(&cache->count))
484 kfree(cache);
485}
486
Josef Bacik0f9dd462008-09-23 13:14:11 -0400487static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
488 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400489{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400490 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400491 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400492
493 rcu_read_lock();
494 list_for_each_entry_rcu(found, head, list) {
495 if (found->flags == flags) {
496 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400497 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400498 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400499 }
Chris Mason4184ea72009-03-10 12:39:20 -0400500 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400501 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400502}
503
Chris Mason4184ea72009-03-10 12:39:20 -0400504/*
505 * after adding space to the filesystem, we need to clear the full flags
506 * on all the space infos.
507 */
508void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
509{
510 struct list_head *head = &info->space_info;
511 struct btrfs_space_info *found;
512
513 rcu_read_lock();
514 list_for_each_entry_rcu(found, head, list)
515 found->full = 0;
516 rcu_read_unlock();
517}
518
Josef Bacik80eb2342008-10-29 14:49:05 -0400519static u64 div_factor(u64 num, int factor)
520{
521 if (factor == 10)
522 return num;
523 num *= factor;
524 do_div(num, 10);
525 return num;
526}
527
Yan Zhengd2fb3432008-12-11 16:30:39 -0500528u64 btrfs_find_block_group(struct btrfs_root *root,
529 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400530{
Chris Mason96b51792007-10-15 16:15:19 -0400531 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400532 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500533 u64 last = max(search_hint, search_start);
534 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400535 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500536 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400537 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400538again:
Zheng Yane8569812008-09-26 10:05:48 -0400539 while (1) {
540 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400541 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400542 break;
Chris Mason96b51792007-10-15 16:15:19 -0400543
Chris Masonc286ac42008-07-22 23:06:41 -0400544 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400545 last = cache->key.objectid + cache->key.offset;
546 used = btrfs_block_group_used(&cache->item);
547
Yan Zhengd2fb3432008-12-11 16:30:39 -0500548 if ((full_search || !cache->ro) &&
549 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400550 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500551 div_factor(cache->key.offset, factor)) {
552 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400553 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400554 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400555 goto found;
556 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400557 }
Chris Masonc286ac42008-07-22 23:06:41 -0400558 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400559 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400560 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400561 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400562 if (!wrapped) {
563 last = search_start;
564 wrapped = 1;
565 goto again;
566 }
567 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400568 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400569 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400570 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400571 goto again;
572 }
Chris Masonbe744172007-05-06 10:15:01 -0400573found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500574 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400575}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400576
Chris Masone02119d2008-09-05 16:13:11 -0400577/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400578int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400579{
580 int ret;
581 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400582 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400583
Zheng Yan31840ae2008-09-23 13:14:14 -0400584 path = btrfs_alloc_path();
585 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400586 key.objectid = start;
587 key.offset = len;
588 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
589 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
590 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400591 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500592 return ret;
593}
594
Chris Masond8d5f3e2007-12-11 12:42:00 -0500595/*
596 * Back reference rules. Back refs have three main goals:
597 *
598 * 1) differentiate between all holders of references to an extent so that
599 * when a reference is dropped we can make sure it was a valid reference
600 * before freeing the extent.
601 *
602 * 2) Provide enough information to quickly find the holders of an extent
603 * if we notice a given block is corrupted or bad.
604 *
605 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
606 * maintenance. This is actually the same as #2, but with a slightly
607 * different use case.
608 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400609 * There are two kinds of back refs. The implicit back refs is optimized
610 * for pointers in non-shared tree blocks. For a given pointer in a block,
611 * back refs of this kind provide information about the block's owner tree
612 * and the pointer's key. These information allow us to find the block by
613 * b-tree searching. The full back refs is for pointers in tree blocks not
614 * referenced by their owner trees. The location of tree block is recorded
615 * in the back refs. Actually the full back refs is generic, and can be
616 * used in all cases the implicit back refs is used. The major shortcoming
617 * of the full back refs is its overhead. Every time a tree block gets
618 * COWed, we have to update back refs entry for all pointers in it.
619 *
620 * For a newly allocated tree block, we use implicit back refs for
621 * pointers in it. This means most tree related operations only involve
622 * implicit back refs. For a tree block created in old transaction, the
623 * only way to drop a reference to it is COW it. So we can detect the
624 * event that tree block loses its owner tree's reference and do the
625 * back refs conversion.
626 *
627 * When a tree block is COW'd through a tree, there are four cases:
628 *
629 * The reference count of the block is one and the tree is the block's
630 * owner tree. Nothing to do in this case.
631 *
632 * The reference count of the block is one and the tree is not the
633 * block's owner tree. In this case, full back refs is used for pointers
634 * in the block. Remove these full back refs, add implicit back refs for
635 * every pointers in the new block.
636 *
637 * The reference count of the block is greater than one and the tree is
638 * the block's owner tree. In this case, implicit back refs is used for
639 * pointers in the block. Add full back refs for every pointers in the
640 * block, increase lower level extents' reference counts. The original
641 * implicit back refs are entailed to the new block.
642 *
643 * The reference count of the block is greater than one and the tree is
644 * not the block's owner tree. Add implicit back refs for every pointer in
645 * the new block, increase lower level extents' reference count.
646 *
647 * Back Reference Key composing:
648 *
649 * The key objectid corresponds to the first byte in the extent,
650 * The key type is used to differentiate between types of back refs.
651 * There are different meanings of the key offset for different types
652 * of back refs.
653 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500654 * File extents can be referenced by:
655 *
656 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400657 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500658 * - different offsets inside a file (bookend extents in file.c)
659 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400660 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500661 *
662 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500663 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400664 * - original offset in the file
665 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400666 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400667 * The key offset for the implicit back refs is hash of the first
668 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500669 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400670 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500671 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400672 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500673 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400674 * The key offset for the implicit back refs is the first byte of
675 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500676 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400677 * When a file extent is allocated, The implicit back refs is used.
678 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500679 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400680 * (root_key.objectid, inode objectid, offset in file, 1)
681 *
682 * When a file extent is removed file truncation, we find the
683 * corresponding implicit back refs and check the following fields:
684 *
685 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500686 *
687 * Btree extents can be referenced by:
688 *
689 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500690 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400691 * Both the implicit back refs and the full back refs for tree blocks
692 * only consist of key. The key offset for the implicit back refs is
693 * objectid of block's owner tree. The key offset for the full back refs
694 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500695 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400696 * When implicit back refs is used, information about the lowest key and
697 * level of the tree block are required. These information are stored in
698 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500699 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400700
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400701#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
702static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
703 struct btrfs_root *root,
704 struct btrfs_path *path,
705 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500706{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400707 struct btrfs_extent_item *item;
708 struct btrfs_extent_item_v0 *ei0;
709 struct btrfs_extent_ref_v0 *ref0;
710 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400711 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400712 struct btrfs_key key;
713 struct btrfs_key found_key;
714 u32 new_size = sizeof(*item);
715 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500716 int ret;
717
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400718 leaf = path->nodes[0];
719 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500720
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400721 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
722 ei0 = btrfs_item_ptr(leaf, path->slots[0],
723 struct btrfs_extent_item_v0);
724 refs = btrfs_extent_refs_v0(leaf, ei0);
725
726 if (owner == (u64)-1) {
727 while (1) {
728 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
729 ret = btrfs_next_leaf(root, path);
730 if (ret < 0)
731 return ret;
732 BUG_ON(ret > 0);
733 leaf = path->nodes[0];
734 }
735 btrfs_item_key_to_cpu(leaf, &found_key,
736 path->slots[0]);
737 BUG_ON(key.objectid != found_key.objectid);
738 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
739 path->slots[0]++;
740 continue;
741 }
742 ref0 = btrfs_item_ptr(leaf, path->slots[0],
743 struct btrfs_extent_ref_v0);
744 owner = btrfs_ref_objectid_v0(leaf, ref0);
745 break;
746 }
747 }
748 btrfs_release_path(root, path);
749
750 if (owner < BTRFS_FIRST_FREE_OBJECTID)
751 new_size += sizeof(*bi);
752
753 new_size -= sizeof(*ei0);
754 ret = btrfs_search_slot(trans, root, &key, path,
755 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400756 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400757 return ret;
758 BUG_ON(ret);
759
760 ret = btrfs_extend_item(trans, root, path, new_size);
761 BUG_ON(ret);
762
763 leaf = path->nodes[0];
764 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
765 btrfs_set_extent_refs(leaf, item, refs);
766 /* FIXME: get real generation */
767 btrfs_set_extent_generation(leaf, item, 0);
768 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
769 btrfs_set_extent_flags(leaf, item,
770 BTRFS_EXTENT_FLAG_TREE_BLOCK |
771 BTRFS_BLOCK_FLAG_FULL_BACKREF);
772 bi = (struct btrfs_tree_block_info *)(item + 1);
773 /* FIXME: get first key of the block */
774 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
775 btrfs_set_tree_block_level(leaf, bi, (int)owner);
776 } else {
777 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
778 }
779 btrfs_mark_buffer_dirty(leaf);
780 return 0;
781}
782#endif
783
784static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
785{
786 u32 high_crc = ~(u32)0;
787 u32 low_crc = ~(u32)0;
788 __le64 lenum;
789
790 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100791 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400792 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100793 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400794 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100795 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400796
797 return ((u64)high_crc << 31) ^ (u64)low_crc;
798}
799
800static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
801 struct btrfs_extent_data_ref *ref)
802{
803 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
804 btrfs_extent_data_ref_objectid(leaf, ref),
805 btrfs_extent_data_ref_offset(leaf, ref));
806}
807
808static int match_extent_data_ref(struct extent_buffer *leaf,
809 struct btrfs_extent_data_ref *ref,
810 u64 root_objectid, u64 owner, u64 offset)
811{
812 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
813 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
814 btrfs_extent_data_ref_offset(leaf, ref) != offset)
815 return 0;
816 return 1;
817}
818
819static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
820 struct btrfs_root *root,
821 struct btrfs_path *path,
822 u64 bytenr, u64 parent,
823 u64 root_objectid,
824 u64 owner, u64 offset)
825{
826 struct btrfs_key key;
827 struct btrfs_extent_data_ref *ref;
828 struct extent_buffer *leaf;
829 u32 nritems;
830 int ret;
831 int recow;
832 int err = -ENOENT;
833
834 key.objectid = bytenr;
835 if (parent) {
836 key.type = BTRFS_SHARED_DATA_REF_KEY;
837 key.offset = parent;
838 } else {
839 key.type = BTRFS_EXTENT_DATA_REF_KEY;
840 key.offset = hash_extent_data_ref(root_objectid,
841 owner, offset);
842 }
843again:
844 recow = 0;
845 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
846 if (ret < 0) {
847 err = ret;
848 goto fail;
849 }
850
851 if (parent) {
852 if (!ret)
853 return 0;
854#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
855 key.type = BTRFS_EXTENT_REF_V0_KEY;
856 btrfs_release_path(root, path);
857 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
858 if (ret < 0) {
859 err = ret;
860 goto fail;
861 }
862 if (!ret)
863 return 0;
864#endif
865 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400866 }
867
868 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400869 nritems = btrfs_header_nritems(leaf);
870 while (1) {
871 if (path->slots[0] >= nritems) {
872 ret = btrfs_next_leaf(root, path);
873 if (ret < 0)
874 err = ret;
875 if (ret)
876 goto fail;
877
878 leaf = path->nodes[0];
879 nritems = btrfs_header_nritems(leaf);
880 recow = 1;
881 }
882
883 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
884 if (key.objectid != bytenr ||
885 key.type != BTRFS_EXTENT_DATA_REF_KEY)
886 goto fail;
887
888 ref = btrfs_item_ptr(leaf, path->slots[0],
889 struct btrfs_extent_data_ref);
890
891 if (match_extent_data_ref(leaf, ref, root_objectid,
892 owner, offset)) {
893 if (recow) {
894 btrfs_release_path(root, path);
895 goto again;
896 }
897 err = 0;
898 break;
899 }
900 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400901 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902fail:
903 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400904}
905
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400906static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
907 struct btrfs_root *root,
908 struct btrfs_path *path,
909 u64 bytenr, u64 parent,
910 u64 root_objectid, u64 owner,
911 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400912{
913 struct btrfs_key key;
914 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400915 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400916 u32 num_refs;
917 int ret;
918
919 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400920 if (parent) {
921 key.type = BTRFS_SHARED_DATA_REF_KEY;
922 key.offset = parent;
923 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400924 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400925 key.type = BTRFS_EXTENT_DATA_REF_KEY;
926 key.offset = hash_extent_data_ref(root_objectid,
927 owner, offset);
928 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400929 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400930
931 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
932 if (ret && ret != -EEXIST)
933 goto fail;
934
935 leaf = path->nodes[0];
936 if (parent) {
937 struct btrfs_shared_data_ref *ref;
938 ref = btrfs_item_ptr(leaf, path->slots[0],
939 struct btrfs_shared_data_ref);
940 if (ret == 0) {
941 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
942 } else {
943 num_refs = btrfs_shared_data_ref_count(leaf, ref);
944 num_refs += refs_to_add;
945 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
946 }
947 } else {
948 struct btrfs_extent_data_ref *ref;
949 while (ret == -EEXIST) {
950 ref = btrfs_item_ptr(leaf, path->slots[0],
951 struct btrfs_extent_data_ref);
952 if (match_extent_data_ref(leaf, ref, root_objectid,
953 owner, offset))
954 break;
955 btrfs_release_path(root, path);
956 key.offset++;
957 ret = btrfs_insert_empty_item(trans, root, path, &key,
958 size);
959 if (ret && ret != -EEXIST)
960 goto fail;
961
962 leaf = path->nodes[0];
963 }
964 ref = btrfs_item_ptr(leaf, path->slots[0],
965 struct btrfs_extent_data_ref);
966 if (ret == 0) {
967 btrfs_set_extent_data_ref_root(leaf, ref,
968 root_objectid);
969 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
970 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
971 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
972 } else {
973 num_refs = btrfs_extent_data_ref_count(leaf, ref);
974 num_refs += refs_to_add;
975 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
976 }
977 }
978 btrfs_mark_buffer_dirty(leaf);
979 ret = 0;
980fail:
Chris Mason7bb86312007-12-11 09:25:06 -0500981 btrfs_release_path(root, path);
982 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500983}
984
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
986 struct btrfs_root *root,
987 struct btrfs_path *path,
988 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -0400989{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400990 struct btrfs_key key;
991 struct btrfs_extent_data_ref *ref1 = NULL;
992 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400993 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400994 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400995 int ret = 0;
996
997 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400998 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
999
1000 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1001 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1002 struct btrfs_extent_data_ref);
1003 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1004 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1005 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_shared_data_ref);
1007 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1008#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1009 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1010 struct btrfs_extent_ref_v0 *ref0;
1011 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1012 struct btrfs_extent_ref_v0);
1013 num_refs = btrfs_ref_count_v0(leaf, ref0);
1014#endif
1015 } else {
1016 BUG();
1017 }
1018
Chris Mason56bec292009-03-13 10:10:06 -04001019 BUG_ON(num_refs < refs_to_drop);
1020 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021
Zheng Yan31840ae2008-09-23 13:14:14 -04001022 if (num_refs == 0) {
1023 ret = btrfs_del_item(trans, root, path);
1024 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001025 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1026 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1027 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1028 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1029#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1030 else {
1031 struct btrfs_extent_ref_v0 *ref0;
1032 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1033 struct btrfs_extent_ref_v0);
1034 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1035 }
1036#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001037 btrfs_mark_buffer_dirty(leaf);
1038 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001039 return ret;
1040}
1041
1042static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1043 struct btrfs_path *path,
1044 struct btrfs_extent_inline_ref *iref)
1045{
1046 struct btrfs_key key;
1047 struct extent_buffer *leaf;
1048 struct btrfs_extent_data_ref *ref1;
1049 struct btrfs_shared_data_ref *ref2;
1050 u32 num_refs = 0;
1051
1052 leaf = path->nodes[0];
1053 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1054 if (iref) {
1055 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1056 BTRFS_EXTENT_DATA_REF_KEY) {
1057 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1058 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1059 } else {
1060 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1061 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1062 }
1063 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1064 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1065 struct btrfs_extent_data_ref);
1066 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1067 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1068 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1069 struct btrfs_shared_data_ref);
1070 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1071#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1072 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1073 struct btrfs_extent_ref_v0 *ref0;
1074 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1075 struct btrfs_extent_ref_v0);
1076 num_refs = btrfs_ref_count_v0(leaf, ref0);
1077#endif
1078 } else {
1079 WARN_ON(1);
1080 }
1081 return num_refs;
1082}
1083
1084static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1085 struct btrfs_root *root,
1086 struct btrfs_path *path,
1087 u64 bytenr, u64 parent,
1088 u64 root_objectid)
1089{
1090 struct btrfs_key key;
1091 int ret;
1092
1093 key.objectid = bytenr;
1094 if (parent) {
1095 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1096 key.offset = parent;
1097 } else {
1098 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1099 key.offset = root_objectid;
1100 }
1101
1102 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1103 if (ret > 0)
1104 ret = -ENOENT;
1105#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1106 if (ret == -ENOENT && parent) {
1107 btrfs_release_path(root, path);
1108 key.type = BTRFS_EXTENT_REF_V0_KEY;
1109 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1110 if (ret > 0)
1111 ret = -ENOENT;
1112 }
1113#endif
1114 return ret;
1115}
1116
1117static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1118 struct btrfs_root *root,
1119 struct btrfs_path *path,
1120 u64 bytenr, u64 parent,
1121 u64 root_objectid)
1122{
1123 struct btrfs_key key;
1124 int ret;
1125
1126 key.objectid = bytenr;
1127 if (parent) {
1128 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1129 key.offset = parent;
1130 } else {
1131 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1132 key.offset = root_objectid;
1133 }
1134
1135 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001136 btrfs_release_path(root, path);
1137 return ret;
1138}
1139
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001140static inline int extent_ref_type(u64 parent, u64 owner)
1141{
1142 int type;
1143 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1144 if (parent > 0)
1145 type = BTRFS_SHARED_BLOCK_REF_KEY;
1146 else
1147 type = BTRFS_TREE_BLOCK_REF_KEY;
1148 } else {
1149 if (parent > 0)
1150 type = BTRFS_SHARED_DATA_REF_KEY;
1151 else
1152 type = BTRFS_EXTENT_DATA_REF_KEY;
1153 }
1154 return type;
1155}
1156
Yan Zheng2c47e6052009-06-27 21:07:35 -04001157static int find_next_key(struct btrfs_path *path, int level,
1158 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001159
1160{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001161 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001162 if (!path->nodes[level])
1163 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001164 if (path->slots[level] + 1 >=
1165 btrfs_header_nritems(path->nodes[level]))
1166 continue;
1167 if (level == 0)
1168 btrfs_item_key_to_cpu(path->nodes[level], key,
1169 path->slots[level] + 1);
1170 else
1171 btrfs_node_key_to_cpu(path->nodes[level], key,
1172 path->slots[level] + 1);
1173 return 0;
1174 }
1175 return 1;
1176}
1177
1178/*
1179 * look for inline back ref. if back ref is found, *ref_ret is set
1180 * to the address of inline back ref, and 0 is returned.
1181 *
1182 * if back ref isn't found, *ref_ret is set to the address where it
1183 * should be inserted, and -ENOENT is returned.
1184 *
1185 * if insert is true and there are too many inline back refs, the path
1186 * points to the extent item, and -EAGAIN is returned.
1187 *
1188 * NOTE: inline back refs are ordered in the same way that back ref
1189 * items in the tree are ordered.
1190 */
1191static noinline_for_stack
1192int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1193 struct btrfs_root *root,
1194 struct btrfs_path *path,
1195 struct btrfs_extent_inline_ref **ref_ret,
1196 u64 bytenr, u64 num_bytes,
1197 u64 parent, u64 root_objectid,
1198 u64 owner, u64 offset, int insert)
1199{
1200 struct btrfs_key key;
1201 struct extent_buffer *leaf;
1202 struct btrfs_extent_item *ei;
1203 struct btrfs_extent_inline_ref *iref;
1204 u64 flags;
1205 u64 item_size;
1206 unsigned long ptr;
1207 unsigned long end;
1208 int extra_size;
1209 int type;
1210 int want;
1211 int ret;
1212 int err = 0;
1213
1214 key.objectid = bytenr;
1215 key.type = BTRFS_EXTENT_ITEM_KEY;
1216 key.offset = num_bytes;
1217
1218 want = extent_ref_type(parent, owner);
1219 if (insert) {
1220 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001221 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001222 } else
1223 extra_size = -1;
1224 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1225 if (ret < 0) {
1226 err = ret;
1227 goto out;
1228 }
1229 BUG_ON(ret);
1230
1231 leaf = path->nodes[0];
1232 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1233#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1234 if (item_size < sizeof(*ei)) {
1235 if (!insert) {
1236 err = -ENOENT;
1237 goto out;
1238 }
1239 ret = convert_extent_item_v0(trans, root, path, owner,
1240 extra_size);
1241 if (ret < 0) {
1242 err = ret;
1243 goto out;
1244 }
1245 leaf = path->nodes[0];
1246 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1247 }
1248#endif
1249 BUG_ON(item_size < sizeof(*ei));
1250
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001251 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1252 flags = btrfs_extent_flags(leaf, ei);
1253
1254 ptr = (unsigned long)(ei + 1);
1255 end = (unsigned long)ei + item_size;
1256
1257 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1258 ptr += sizeof(struct btrfs_tree_block_info);
1259 BUG_ON(ptr > end);
1260 } else {
1261 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1262 }
1263
1264 err = -ENOENT;
1265 while (1) {
1266 if (ptr >= end) {
1267 WARN_ON(ptr > end);
1268 break;
1269 }
1270 iref = (struct btrfs_extent_inline_ref *)ptr;
1271 type = btrfs_extent_inline_ref_type(leaf, iref);
1272 if (want < type)
1273 break;
1274 if (want > type) {
1275 ptr += btrfs_extent_inline_ref_size(type);
1276 continue;
1277 }
1278
1279 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1280 struct btrfs_extent_data_ref *dref;
1281 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1282 if (match_extent_data_ref(leaf, dref, root_objectid,
1283 owner, offset)) {
1284 err = 0;
1285 break;
1286 }
1287 if (hash_extent_data_ref_item(leaf, dref) <
1288 hash_extent_data_ref(root_objectid, owner, offset))
1289 break;
1290 } else {
1291 u64 ref_offset;
1292 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1293 if (parent > 0) {
1294 if (parent == ref_offset) {
1295 err = 0;
1296 break;
1297 }
1298 if (ref_offset < parent)
1299 break;
1300 } else {
1301 if (root_objectid == ref_offset) {
1302 err = 0;
1303 break;
1304 }
1305 if (ref_offset < root_objectid)
1306 break;
1307 }
1308 }
1309 ptr += btrfs_extent_inline_ref_size(type);
1310 }
1311 if (err == -ENOENT && insert) {
1312 if (item_size + extra_size >=
1313 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1314 err = -EAGAIN;
1315 goto out;
1316 }
1317 /*
1318 * To add new inline back ref, we have to make sure
1319 * there is no corresponding back ref item.
1320 * For simplicity, we just do not add new inline back
1321 * ref if there is any kind of item for this block
1322 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001323 if (find_next_key(path, 0, &key) == 0 &&
1324 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001325 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001326 err = -EAGAIN;
1327 goto out;
1328 }
1329 }
1330 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1331out:
Yan Zheng85d41982009-06-11 08:51:10 -04001332 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001333 path->keep_locks = 0;
1334 btrfs_unlock_up_safe(path, 1);
1335 }
1336 return err;
1337}
1338
1339/*
1340 * helper to add new inline back ref
1341 */
1342static noinline_for_stack
1343int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1344 struct btrfs_root *root,
1345 struct btrfs_path *path,
1346 struct btrfs_extent_inline_ref *iref,
1347 u64 parent, u64 root_objectid,
1348 u64 owner, u64 offset, int refs_to_add,
1349 struct btrfs_delayed_extent_op *extent_op)
1350{
1351 struct extent_buffer *leaf;
1352 struct btrfs_extent_item *ei;
1353 unsigned long ptr;
1354 unsigned long end;
1355 unsigned long item_offset;
1356 u64 refs;
1357 int size;
1358 int type;
1359 int ret;
1360
1361 leaf = path->nodes[0];
1362 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1363 item_offset = (unsigned long)iref - (unsigned long)ei;
1364
1365 type = extent_ref_type(parent, owner);
1366 size = btrfs_extent_inline_ref_size(type);
1367
1368 ret = btrfs_extend_item(trans, root, path, size);
1369 BUG_ON(ret);
1370
1371 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1372 refs = btrfs_extent_refs(leaf, ei);
1373 refs += refs_to_add;
1374 btrfs_set_extent_refs(leaf, ei, refs);
1375 if (extent_op)
1376 __run_delayed_extent_op(extent_op, leaf, ei);
1377
1378 ptr = (unsigned long)ei + item_offset;
1379 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1380 if (ptr < end - size)
1381 memmove_extent_buffer(leaf, ptr + size, ptr,
1382 end - size - ptr);
1383
1384 iref = (struct btrfs_extent_inline_ref *)ptr;
1385 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1386 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1387 struct btrfs_extent_data_ref *dref;
1388 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1389 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1390 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1391 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1392 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1393 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1394 struct btrfs_shared_data_ref *sref;
1395 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1396 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1397 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1398 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1399 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1400 } else {
1401 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1402 }
1403 btrfs_mark_buffer_dirty(leaf);
1404 return 0;
1405}
1406
1407static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1408 struct btrfs_root *root,
1409 struct btrfs_path *path,
1410 struct btrfs_extent_inline_ref **ref_ret,
1411 u64 bytenr, u64 num_bytes, u64 parent,
1412 u64 root_objectid, u64 owner, u64 offset)
1413{
1414 int ret;
1415
1416 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1417 bytenr, num_bytes, parent,
1418 root_objectid, owner, offset, 0);
1419 if (ret != -ENOENT)
1420 return ret;
1421
1422 btrfs_release_path(root, path);
1423 *ref_ret = NULL;
1424
1425 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1426 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1427 root_objectid);
1428 } else {
1429 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1430 root_objectid, owner, offset);
1431 }
1432 return ret;
1433}
1434
1435/*
1436 * helper to update/remove inline back ref
1437 */
1438static noinline_for_stack
1439int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1440 struct btrfs_root *root,
1441 struct btrfs_path *path,
1442 struct btrfs_extent_inline_ref *iref,
1443 int refs_to_mod,
1444 struct btrfs_delayed_extent_op *extent_op)
1445{
1446 struct extent_buffer *leaf;
1447 struct btrfs_extent_item *ei;
1448 struct btrfs_extent_data_ref *dref = NULL;
1449 struct btrfs_shared_data_ref *sref = NULL;
1450 unsigned long ptr;
1451 unsigned long end;
1452 u32 item_size;
1453 int size;
1454 int type;
1455 int ret;
1456 u64 refs;
1457
1458 leaf = path->nodes[0];
1459 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1460 refs = btrfs_extent_refs(leaf, ei);
1461 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1462 refs += refs_to_mod;
1463 btrfs_set_extent_refs(leaf, ei, refs);
1464 if (extent_op)
1465 __run_delayed_extent_op(extent_op, leaf, ei);
1466
1467 type = btrfs_extent_inline_ref_type(leaf, iref);
1468
1469 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1470 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1471 refs = btrfs_extent_data_ref_count(leaf, dref);
1472 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1473 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1474 refs = btrfs_shared_data_ref_count(leaf, sref);
1475 } else {
1476 refs = 1;
1477 BUG_ON(refs_to_mod != -1);
1478 }
1479
1480 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1481 refs += refs_to_mod;
1482
1483 if (refs > 0) {
1484 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1485 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1486 else
1487 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1488 } else {
1489 size = btrfs_extent_inline_ref_size(type);
1490 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1491 ptr = (unsigned long)iref;
1492 end = (unsigned long)ei + item_size;
1493 if (ptr + size < end)
1494 memmove_extent_buffer(leaf, ptr, ptr + size,
1495 end - ptr - size);
1496 item_size -= size;
1497 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1498 BUG_ON(ret);
1499 }
1500 btrfs_mark_buffer_dirty(leaf);
1501 return 0;
1502}
1503
1504static noinline_for_stack
1505int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1506 struct btrfs_root *root,
1507 struct btrfs_path *path,
1508 u64 bytenr, u64 num_bytes, u64 parent,
1509 u64 root_objectid, u64 owner,
1510 u64 offset, int refs_to_add,
1511 struct btrfs_delayed_extent_op *extent_op)
1512{
1513 struct btrfs_extent_inline_ref *iref;
1514 int ret;
1515
1516 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1517 bytenr, num_bytes, parent,
1518 root_objectid, owner, offset, 1);
1519 if (ret == 0) {
1520 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1521 ret = update_inline_extent_backref(trans, root, path, iref,
1522 refs_to_add, extent_op);
1523 } else if (ret == -ENOENT) {
1524 ret = setup_inline_extent_backref(trans, root, path, iref,
1525 parent, root_objectid,
1526 owner, offset, refs_to_add,
1527 extent_op);
1528 }
1529 return ret;
1530}
1531
1532static int insert_extent_backref(struct btrfs_trans_handle *trans,
1533 struct btrfs_root *root,
1534 struct btrfs_path *path,
1535 u64 bytenr, u64 parent, u64 root_objectid,
1536 u64 owner, u64 offset, int refs_to_add)
1537{
1538 int ret;
1539 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1540 BUG_ON(refs_to_add != 1);
1541 ret = insert_tree_block_ref(trans, root, path, bytenr,
1542 parent, root_objectid);
1543 } else {
1544 ret = insert_extent_data_ref(trans, root, path, bytenr,
1545 parent, root_objectid,
1546 owner, offset, refs_to_add);
1547 }
1548 return ret;
1549}
1550
1551static int remove_extent_backref(struct btrfs_trans_handle *trans,
1552 struct btrfs_root *root,
1553 struct btrfs_path *path,
1554 struct btrfs_extent_inline_ref *iref,
1555 int refs_to_drop, int is_data)
1556{
1557 int ret;
1558
1559 BUG_ON(!is_data && refs_to_drop != 1);
1560 if (iref) {
1561 ret = update_inline_extent_backref(trans, root, path, iref,
1562 -refs_to_drop, NULL);
1563 } else if (is_data) {
1564 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1565 } else {
1566 ret = btrfs_del_item(trans, root, path);
1567 }
1568 return ret;
1569}
1570
Chris Mason4b4e25f2008-11-20 10:22:27 -05001571#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001572static void btrfs_issue_discard(struct block_device *bdev,
1573 u64 start, u64 len)
1574{
Chris Mason15916de2008-11-19 21:17:22 -05001575 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -05001576}
Chris Mason4b4e25f2008-11-20 10:22:27 -05001577#endif
Chris Mason15916de2008-11-19 21:17:22 -05001578
Liu Hui1f3c79a2009-01-05 15:57:51 -05001579static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1580 u64 num_bytes)
1581{
1582#ifdef BIO_RW_DISCARD
1583 int ret;
1584 u64 map_length = num_bytes;
1585 struct btrfs_multi_bio *multi = NULL;
1586
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001587 if (!btrfs_test_opt(root, DISCARD))
1588 return 0;
1589
Liu Hui1f3c79a2009-01-05 15:57:51 -05001590 /* Tell the block device(s) that the sectors can be discarded */
1591 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1592 bytenr, &map_length, &multi, 0);
1593 if (!ret) {
1594 struct btrfs_bio_stripe *stripe = multi->stripes;
1595 int i;
1596
1597 if (map_length > num_bytes)
1598 map_length = num_bytes;
1599
1600 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1601 btrfs_issue_discard(stripe->dev->bdev,
1602 stripe->physical,
1603 map_length);
1604 }
1605 kfree(multi);
1606 }
1607
1608 return ret;
1609#else
1610 return 0;
1611#endif
1612}
1613
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001614int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1615 struct btrfs_root *root,
1616 u64 bytenr, u64 num_bytes, u64 parent,
1617 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001618{
1619 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001620 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1621 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001622
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001623 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1624 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1625 parent, root_objectid, (int)owner,
1626 BTRFS_ADD_DELAYED_REF, NULL);
1627 } else {
1628 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1629 parent, root_objectid, owner, offset,
1630 BTRFS_ADD_DELAYED_REF, NULL);
1631 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001632 return ret;
1633}
1634
Chris Mason925baed2008-06-25 16:01:30 -04001635static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001636 struct btrfs_root *root,
1637 u64 bytenr, u64 num_bytes,
1638 u64 parent, u64 root_objectid,
1639 u64 owner, u64 offset, int refs_to_add,
1640 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001641{
Chris Mason5caf2a02007-04-02 11:20:42 -04001642 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001643 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001644 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001645 u64 refs;
1646 int ret;
1647 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001648
Chris Mason5caf2a02007-04-02 11:20:42 -04001649 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001650 if (!path)
1651 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001652
Chris Mason3c12ac72008-04-21 12:01:38 -04001653 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001654 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001655 /* this will setup the path even if it fails to insert the back ref */
1656 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1657 path, bytenr, num_bytes, parent,
1658 root_objectid, owner, offset,
1659 refs_to_add, extent_op);
1660 if (ret == 0)
1661 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001662
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001663 if (ret != -EAGAIN) {
1664 err = ret;
1665 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001666 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001667
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668 leaf = path->nodes[0];
1669 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1670 refs = btrfs_extent_refs(leaf, item);
1671 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1672 if (extent_op)
1673 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001674
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001675 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001676 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001677
Chris Mason3c12ac72008-04-21 12:01:38 -04001678 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001679 path->leave_spinning = 1;
1680
Chris Mason56bec292009-03-13 10:10:06 -04001681 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001682 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001683 path, bytenr, parent, root_objectid,
1684 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001685 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686out:
Chris Mason74493f72007-12-11 09:25:06 -05001687 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001688 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001689}
1690
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001691static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1692 struct btrfs_root *root,
1693 struct btrfs_delayed_ref_node *node,
1694 struct btrfs_delayed_extent_op *extent_op,
1695 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001696{
Chris Mason56bec292009-03-13 10:10:06 -04001697 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698 struct btrfs_delayed_data_ref *ref;
1699 struct btrfs_key ins;
1700 u64 parent = 0;
1701 u64 ref_root = 0;
1702 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001703
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001704 ins.objectid = node->bytenr;
1705 ins.offset = node->num_bytes;
1706 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001707
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001708 ref = btrfs_delayed_node_to_data_ref(node);
1709 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1710 parent = ref->parent;
1711 else
1712 ref_root = ref->root;
1713
1714 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1715 if (extent_op) {
1716 BUG_ON(extent_op->update_key);
1717 flags |= extent_op->flags_to_set;
1718 }
1719 ret = alloc_reserved_file_extent(trans, root,
1720 parent, ref_root, flags,
1721 ref->objectid, ref->offset,
1722 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001723 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1724 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1725 node->num_bytes, parent,
1726 ref_root, ref->objectid,
1727 ref->offset, node->ref_mod,
1728 extent_op);
1729 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1730 ret = __btrfs_free_extent(trans, root, node->bytenr,
1731 node->num_bytes, parent,
1732 ref_root, ref->objectid,
1733 ref->offset, node->ref_mod,
1734 extent_op);
1735 } else {
1736 BUG();
1737 }
Chris Mason56bec292009-03-13 10:10:06 -04001738 return ret;
1739}
1740
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001741static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1742 struct extent_buffer *leaf,
1743 struct btrfs_extent_item *ei)
1744{
1745 u64 flags = btrfs_extent_flags(leaf, ei);
1746 if (extent_op->update_flags) {
1747 flags |= extent_op->flags_to_set;
1748 btrfs_set_extent_flags(leaf, ei, flags);
1749 }
1750
1751 if (extent_op->update_key) {
1752 struct btrfs_tree_block_info *bi;
1753 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1754 bi = (struct btrfs_tree_block_info *)(ei + 1);
1755 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1756 }
1757}
1758
1759static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1760 struct btrfs_root *root,
1761 struct btrfs_delayed_ref_node *node,
1762 struct btrfs_delayed_extent_op *extent_op)
1763{
1764 struct btrfs_key key;
1765 struct btrfs_path *path;
1766 struct btrfs_extent_item *ei;
1767 struct extent_buffer *leaf;
1768 u32 item_size;
1769 int ret;
1770 int err = 0;
1771
1772 path = btrfs_alloc_path();
1773 if (!path)
1774 return -ENOMEM;
1775
1776 key.objectid = node->bytenr;
1777 key.type = BTRFS_EXTENT_ITEM_KEY;
1778 key.offset = node->num_bytes;
1779
1780 path->reada = 1;
1781 path->leave_spinning = 1;
1782 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1783 path, 0, 1);
1784 if (ret < 0) {
1785 err = ret;
1786 goto out;
1787 }
1788 if (ret > 0) {
1789 err = -EIO;
1790 goto out;
1791 }
1792
1793 leaf = path->nodes[0];
1794 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1795#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1796 if (item_size < sizeof(*ei)) {
1797 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1798 path, (u64)-1, 0);
1799 if (ret < 0) {
1800 err = ret;
1801 goto out;
1802 }
1803 leaf = path->nodes[0];
1804 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1805 }
1806#endif
1807 BUG_ON(item_size < sizeof(*ei));
1808 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1809 __run_delayed_extent_op(extent_op, leaf, ei);
1810
1811 btrfs_mark_buffer_dirty(leaf);
1812out:
1813 btrfs_free_path(path);
1814 return err;
1815}
1816
1817static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1818 struct btrfs_root *root,
1819 struct btrfs_delayed_ref_node *node,
1820 struct btrfs_delayed_extent_op *extent_op,
1821 int insert_reserved)
1822{
1823 int ret = 0;
1824 struct btrfs_delayed_tree_ref *ref;
1825 struct btrfs_key ins;
1826 u64 parent = 0;
1827 u64 ref_root = 0;
1828
1829 ins.objectid = node->bytenr;
1830 ins.offset = node->num_bytes;
1831 ins.type = BTRFS_EXTENT_ITEM_KEY;
1832
1833 ref = btrfs_delayed_node_to_tree_ref(node);
1834 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1835 parent = ref->parent;
1836 else
1837 ref_root = ref->root;
1838
1839 BUG_ON(node->ref_mod != 1);
1840 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1841 BUG_ON(!extent_op || !extent_op->update_flags ||
1842 !extent_op->update_key);
1843 ret = alloc_reserved_tree_block(trans, root,
1844 parent, ref_root,
1845 extent_op->flags_to_set,
1846 &extent_op->key,
1847 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001848 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1849 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1850 node->num_bytes, parent, ref_root,
1851 ref->level, 0, 1, extent_op);
1852 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1853 ret = __btrfs_free_extent(trans, root, node->bytenr,
1854 node->num_bytes, parent, ref_root,
1855 ref->level, 0, 1, extent_op);
1856 } else {
1857 BUG();
1858 }
1859 return ret;
1860}
1861
1862
Chris Mason56bec292009-03-13 10:10:06 -04001863/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001864static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1865 struct btrfs_root *root,
1866 struct btrfs_delayed_ref_node *node,
1867 struct btrfs_delayed_extent_op *extent_op,
1868 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001869{
Josef Bacikeb099672009-02-12 09:27:38 -05001870 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001871 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001872 struct btrfs_delayed_ref_head *head;
1873 /*
1874 * we've hit the end of the chain and we were supposed
1875 * to insert this extent into the tree. But, it got
1876 * deleted before we ever needed to insert it, so all
1877 * we have to do is clean up the accounting
1878 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001879 BUG_ON(extent_op);
1880 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001881 if (insert_reserved) {
Yan Zheng11833d62009-09-11 16:11:19 -04001882 int mark_free = 0;
1883 struct extent_buffer *must_clean = NULL;
1884
1885 ret = pin_down_bytes(trans, root, NULL,
1886 node->bytenr, node->num_bytes,
1887 head->is_data, 1, &must_clean);
1888 if (ret > 0)
1889 mark_free = 1;
1890
1891 if (must_clean) {
1892 clean_tree_block(NULL, root, must_clean);
1893 btrfs_tree_unlock(must_clean);
1894 free_extent_buffer(must_clean);
1895 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001896 if (head->is_data) {
1897 ret = btrfs_del_csums(trans, root,
1898 node->bytenr,
1899 node->num_bytes);
1900 BUG_ON(ret);
1901 }
Yan Zheng11833d62009-09-11 16:11:19 -04001902 if (mark_free) {
1903 ret = btrfs_free_reserved_extent(root,
1904 node->bytenr,
1905 node->num_bytes);
1906 BUG_ON(ret);
1907 }
Chris Mason56bec292009-03-13 10:10:06 -04001908 }
Chris Mason56bec292009-03-13 10:10:06 -04001909 mutex_unlock(&head->mutex);
1910 return 0;
1911 }
Josef Bacikeb099672009-02-12 09:27:38 -05001912
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001913 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1914 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1915 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1916 insert_reserved);
1917 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1918 node->type == BTRFS_SHARED_DATA_REF_KEY)
1919 ret = run_delayed_data_ref(trans, root, node, extent_op,
1920 insert_reserved);
1921 else
1922 BUG();
1923 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001924}
1925
Chris Mason56bec292009-03-13 10:10:06 -04001926static noinline struct btrfs_delayed_ref_node *
1927select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001928{
Chris Mason56bec292009-03-13 10:10:06 -04001929 struct rb_node *node;
1930 struct btrfs_delayed_ref_node *ref;
1931 int action = BTRFS_ADD_DELAYED_REF;
1932again:
1933 /*
1934 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1935 * this prevents ref count from going down to zero when
1936 * there still are pending delayed ref.
1937 */
1938 node = rb_prev(&head->node.rb_node);
1939 while (1) {
1940 if (!node)
1941 break;
1942 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1943 rb_node);
1944 if (ref->bytenr != head->node.bytenr)
1945 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001946 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001947 return ref;
1948 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001949 }
Chris Mason56bec292009-03-13 10:10:06 -04001950 if (action == BTRFS_ADD_DELAYED_REF) {
1951 action = BTRFS_DROP_DELAYED_REF;
1952 goto again;
1953 }
1954 return NULL;
1955}
1956
Chris Masonc3e69d52009-03-13 10:17:05 -04001957static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1958 struct btrfs_root *root,
1959 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001960{
Chris Mason56bec292009-03-13 10:10:06 -04001961 struct btrfs_delayed_ref_root *delayed_refs;
1962 struct btrfs_delayed_ref_node *ref;
1963 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001964 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001965 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001966 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001967 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001968
1969 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001970 while (1) {
1971 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001972 /* pick a new head ref from the cluster list */
1973 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001974 break;
Chris Mason56bec292009-03-13 10:10:06 -04001975
Chris Masonc3e69d52009-03-13 10:17:05 -04001976 locked_ref = list_entry(cluster->next,
1977 struct btrfs_delayed_ref_head, cluster);
1978
1979 /* grab the lock that says we are going to process
1980 * all the refs for this head */
1981 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1982
1983 /*
1984 * we may have dropped the spin lock to get the head
1985 * mutex lock, and that might have given someone else
1986 * time to free the head. If that's true, it has been
1987 * removed from our list and we can move on.
1988 */
1989 if (ret == -EAGAIN) {
1990 locked_ref = NULL;
1991 count++;
1992 continue;
Chris Mason56bec292009-03-13 10:10:06 -04001993 }
1994 }
1995
1996 /*
1997 * record the must insert reserved flag before we
1998 * drop the spin lock.
1999 */
2000 must_insert_reserved = locked_ref->must_insert_reserved;
2001 locked_ref->must_insert_reserved = 0;
2002
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002003 extent_op = locked_ref->extent_op;
2004 locked_ref->extent_op = NULL;
2005
Chris Mason56bec292009-03-13 10:10:06 -04002006 /*
2007 * locked_ref is the head node, so we have to go one
2008 * node back for any delayed ref updates
2009 */
Chris Mason56bec292009-03-13 10:10:06 -04002010 ref = select_delayed_ref(locked_ref);
2011 if (!ref) {
2012 /* All delayed refs have been processed, Go ahead
2013 * and send the head node to run_one_delayed_ref,
2014 * so that any accounting fixes can happen
2015 */
2016 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002017
2018 if (extent_op && must_insert_reserved) {
2019 kfree(extent_op);
2020 extent_op = NULL;
2021 }
2022
2023 if (extent_op) {
2024 spin_unlock(&delayed_refs->lock);
2025
2026 ret = run_delayed_extent_op(trans, root,
2027 ref, extent_op);
2028 BUG_ON(ret);
2029 kfree(extent_op);
2030
2031 cond_resched();
2032 spin_lock(&delayed_refs->lock);
2033 continue;
2034 }
2035
Chris Masonc3e69d52009-03-13 10:17:05 -04002036 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002037 locked_ref = NULL;
2038 }
2039
2040 ref->in_tree = 0;
2041 rb_erase(&ref->rb_node, &delayed_refs->root);
2042 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002043
Chris Mason56bec292009-03-13 10:10:06 -04002044 spin_unlock(&delayed_refs->lock);
2045
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002046 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002047 must_insert_reserved);
2048 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002049
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002050 btrfs_put_delayed_ref(ref);
2051 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002052 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002053
Chris Mason1887be62009-03-13 10:11:24 -04002054 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002055 spin_lock(&delayed_refs->lock);
2056 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002057 return count;
2058}
2059
2060/*
2061 * this starts processing the delayed reference count updates and
2062 * extent insertions we have queued up so far. count can be
2063 * 0, which means to process everything in the tree at the start
2064 * of the run (but not newly added entries), or it can be some target
2065 * number you'd like to process.
2066 */
2067int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2068 struct btrfs_root *root, unsigned long count)
2069{
2070 struct rb_node *node;
2071 struct btrfs_delayed_ref_root *delayed_refs;
2072 struct btrfs_delayed_ref_node *ref;
2073 struct list_head cluster;
2074 int ret;
2075 int run_all = count == (unsigned long)-1;
2076 int run_most = 0;
2077
2078 if (root == root->fs_info->extent_root)
2079 root = root->fs_info->tree_root;
2080
2081 delayed_refs = &trans->transaction->delayed_refs;
2082 INIT_LIST_HEAD(&cluster);
2083again:
2084 spin_lock(&delayed_refs->lock);
2085 if (count == 0) {
2086 count = delayed_refs->num_entries * 2;
2087 run_most = 1;
2088 }
2089 while (1) {
2090 if (!(run_all || run_most) &&
2091 delayed_refs->num_heads_ready < 64)
2092 break;
2093
2094 /*
2095 * go find something we can process in the rbtree. We start at
2096 * the beginning of the tree, and then build a cluster
2097 * of refs to process starting at the first one we are able to
2098 * lock
2099 */
2100 ret = btrfs_find_ref_cluster(trans, &cluster,
2101 delayed_refs->run_delayed_start);
2102 if (ret)
2103 break;
2104
2105 ret = run_clustered_refs(trans, root, &cluster);
2106 BUG_ON(ret < 0);
2107
2108 count -= min_t(unsigned long, ret, count);
2109
2110 if (count == 0)
2111 break;
2112 }
2113
Chris Mason56bec292009-03-13 10:10:06 -04002114 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002115 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002116 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002117 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002118 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002119
2120 while (node) {
2121 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2122 rb_node);
2123 if (btrfs_delayed_ref_is_head(ref)) {
2124 struct btrfs_delayed_ref_head *head;
2125
2126 head = btrfs_delayed_node_to_head(ref);
2127 atomic_inc(&ref->refs);
2128
2129 spin_unlock(&delayed_refs->lock);
2130 mutex_lock(&head->mutex);
2131 mutex_unlock(&head->mutex);
2132
2133 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002134 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002135 goto again;
2136 }
2137 node = rb_next(node);
2138 }
2139 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002140 schedule_timeout(1);
2141 goto again;
2142 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002143out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002144 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002145 return 0;
2146}
2147
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002148int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2149 struct btrfs_root *root,
2150 u64 bytenr, u64 num_bytes, u64 flags,
2151 int is_data)
2152{
2153 struct btrfs_delayed_extent_op *extent_op;
2154 int ret;
2155
2156 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2157 if (!extent_op)
2158 return -ENOMEM;
2159
2160 extent_op->flags_to_set = flags;
2161 extent_op->update_flags = 1;
2162 extent_op->update_key = 0;
2163 extent_op->is_data = is_data ? 1 : 0;
2164
2165 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2166 if (ret)
2167 kfree(extent_op);
2168 return ret;
2169}
2170
2171static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2172 struct btrfs_root *root,
2173 struct btrfs_path *path,
2174 u64 objectid, u64 offset, u64 bytenr)
2175{
2176 struct btrfs_delayed_ref_head *head;
2177 struct btrfs_delayed_ref_node *ref;
2178 struct btrfs_delayed_data_ref *data_ref;
2179 struct btrfs_delayed_ref_root *delayed_refs;
2180 struct rb_node *node;
2181 int ret = 0;
2182
2183 ret = -ENOENT;
2184 delayed_refs = &trans->transaction->delayed_refs;
2185 spin_lock(&delayed_refs->lock);
2186 head = btrfs_find_delayed_ref_head(trans, bytenr);
2187 if (!head)
2188 goto out;
2189
2190 if (!mutex_trylock(&head->mutex)) {
2191 atomic_inc(&head->node.refs);
2192 spin_unlock(&delayed_refs->lock);
2193
2194 btrfs_release_path(root->fs_info->extent_root, path);
2195
2196 mutex_lock(&head->mutex);
2197 mutex_unlock(&head->mutex);
2198 btrfs_put_delayed_ref(&head->node);
2199 return -EAGAIN;
2200 }
2201
2202 node = rb_prev(&head->node.rb_node);
2203 if (!node)
2204 goto out_unlock;
2205
2206 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2207
2208 if (ref->bytenr != bytenr)
2209 goto out_unlock;
2210
2211 ret = 1;
2212 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2213 goto out_unlock;
2214
2215 data_ref = btrfs_delayed_node_to_data_ref(ref);
2216
2217 node = rb_prev(node);
2218 if (node) {
2219 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2220 if (ref->bytenr == bytenr)
2221 goto out_unlock;
2222 }
2223
2224 if (data_ref->root != root->root_key.objectid ||
2225 data_ref->objectid != objectid || data_ref->offset != offset)
2226 goto out_unlock;
2227
2228 ret = 0;
2229out_unlock:
2230 mutex_unlock(&head->mutex);
2231out:
2232 spin_unlock(&delayed_refs->lock);
2233 return ret;
2234}
2235
2236static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2237 struct btrfs_root *root,
2238 struct btrfs_path *path,
2239 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002240{
2241 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002242 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002243 struct btrfs_extent_data_ref *ref;
2244 struct btrfs_extent_inline_ref *iref;
2245 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002246 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002247 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002248 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002249
Chris Masonbe20aa92007-12-17 20:14:01 -05002250 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002251 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002252 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002253
Chris Masonbe20aa92007-12-17 20:14:01 -05002254 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2255 if (ret < 0)
2256 goto out;
2257 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002258
2259 ret = -ENOENT;
2260 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002261 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002262
Zheng Yan31840ae2008-09-23 13:14:14 -04002263 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002264 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002265 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002266
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002267 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002268 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002269
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002270 ret = 1;
2271 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2272#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2273 if (item_size < sizeof(*ei)) {
2274 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2275 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002276 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002277#endif
2278 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2279
2280 if (item_size != sizeof(*ei) +
2281 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2282 goto out;
2283
2284 if (btrfs_extent_generation(leaf, ei) <=
2285 btrfs_root_last_snapshot(&root->root_item))
2286 goto out;
2287
2288 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2289 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2290 BTRFS_EXTENT_DATA_REF_KEY)
2291 goto out;
2292
2293 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2294 if (btrfs_extent_refs(leaf, ei) !=
2295 btrfs_extent_data_ref_count(leaf, ref) ||
2296 btrfs_extent_data_ref_root(leaf, ref) !=
2297 root->root_key.objectid ||
2298 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2299 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2300 goto out;
2301
Yan Zhengf321e492008-07-30 09:26:11 -04002302 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002303out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002304 return ret;
2305}
2306
2307int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2308 struct btrfs_root *root,
2309 u64 objectid, u64 offset, u64 bytenr)
2310{
2311 struct btrfs_path *path;
2312 int ret;
2313 int ret2;
2314
2315 path = btrfs_alloc_path();
2316 if (!path)
2317 return -ENOENT;
2318
2319 do {
2320 ret = check_committed_ref(trans, root, path, objectid,
2321 offset, bytenr);
2322 if (ret && ret != -ENOENT)
2323 goto out;
2324
2325 ret2 = check_delayed_ref(trans, root, path, objectid,
2326 offset, bytenr);
2327 } while (ret2 == -EAGAIN);
2328
2329 if (ret2 && ret2 != -ENOENT) {
2330 ret = ret2;
2331 goto out;
2332 }
2333
2334 if (ret != -ENOENT || ret2 != -ENOENT)
2335 ret = 0;
2336out:
Yan Zhengf321e492008-07-30 09:26:11 -04002337 btrfs_free_path(path);
2338 return ret;
2339}
2340
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002341#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002342int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2343 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002344{
Chris Mason5f39d392007-10-15 16:14:19 -04002345 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002346 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002347 u64 root_gen;
2348 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002349 int i;
Chris Masondb945352007-10-15 16:15:53 -04002350 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002351 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002352 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002353
Chris Mason3768f362007-03-13 16:47:54 -04002354 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002355 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002356
Zheng Yane4657682008-09-26 10:04:53 -04002357 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2358 shared = 0;
2359 root_gen = root->root_key.offset;
2360 } else {
2361 shared = 1;
2362 root_gen = trans->transid - 1;
2363 }
2364
Chris Masondb945352007-10-15 16:15:53 -04002365 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002366 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002367
Zheng Yan31840ae2008-09-23 13:14:14 -04002368 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002369 struct btrfs_leaf_ref *ref;
2370 struct btrfs_extent_info *info;
2371
Zheng Yan31840ae2008-09-23 13:14:14 -04002372 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002373 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002374 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002375 goto out;
2376 }
2377
Zheng Yane4657682008-09-26 10:04:53 -04002378 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002379 ref->bytenr = buf->start;
2380 ref->owner = btrfs_header_owner(buf);
2381 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002382 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002383 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002384
Zheng Yan31840ae2008-09-23 13:14:14 -04002385 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002386 u64 disk_bytenr;
2387 btrfs_item_key_to_cpu(buf, &key, i);
2388 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2389 continue;
2390 fi = btrfs_item_ptr(buf, i,
2391 struct btrfs_file_extent_item);
2392 if (btrfs_file_extent_type(buf, fi) ==
2393 BTRFS_FILE_EXTENT_INLINE)
2394 continue;
2395 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2396 if (disk_bytenr == 0)
2397 continue;
2398
2399 info->bytenr = disk_bytenr;
2400 info->num_bytes =
2401 btrfs_file_extent_disk_num_bytes(buf, fi);
2402 info->objectid = key.objectid;
2403 info->offset = key.offset;
2404 info++;
2405 }
2406
Zheng Yane4657682008-09-26 10:04:53 -04002407 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002408 if (ret == -EEXIST && shared) {
2409 struct btrfs_leaf_ref *old;
2410 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2411 BUG_ON(!old);
2412 btrfs_remove_leaf_ref(root, old);
2413 btrfs_free_leaf_ref(root, old);
2414 ret = btrfs_add_leaf_ref(root, ref, shared);
2415 }
Yan Zheng31153d82008-07-28 15:32:19 -04002416 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002417 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002418 }
2419out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002420 return ret;
2421}
2422
Chris Masonb7a9f292009-02-04 09:23:45 -05002423/* when a block goes through cow, we update the reference counts of
2424 * everything that block points to. The internal pointers of the block
2425 * can be in just about any order, and it is likely to have clusters of
2426 * things that are close together and clusters of things that are not.
2427 *
2428 * To help reduce the seeks that come with updating all of these reference
2429 * counts, sort them by byte number before actual updates are done.
2430 *
2431 * struct refsort is used to match byte number to slot in the btree block.
2432 * we sort based on the byte number and then use the slot to actually
2433 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002434 *
2435 * struct refsort is smaller than strcut btrfs_item and smaller than
2436 * struct btrfs_key_ptr. Since we're currently limited to the page size
2437 * for a btree block, there's no way for a kmalloc of refsorts for a
2438 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002439 */
2440struct refsort {
2441 u64 bytenr;
2442 u32 slot;
2443};
2444
2445/*
2446 * for passing into sort()
2447 */
2448static int refsort_cmp(const void *a_void, const void *b_void)
2449{
2450 const struct refsort *a = a_void;
2451 const struct refsort *b = b_void;
2452
2453 if (a->bytenr < b->bytenr)
2454 return -1;
2455 if (a->bytenr > b->bytenr)
2456 return 1;
2457 return 0;
2458}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002459#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002460
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002461static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002462 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002463 struct extent_buffer *buf,
2464 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002465{
2466 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002467 u64 num_bytes;
2468 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002469 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002470 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002471 struct btrfs_key key;
2472 struct btrfs_file_extent_item *fi;
2473 int i;
2474 int level;
2475 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002476 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002477 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002478
2479 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002480 nritems = btrfs_header_nritems(buf);
2481 level = btrfs_header_level(buf);
2482
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002483 if (!root->ref_cows && level == 0)
2484 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002485
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002486 if (inc)
2487 process_func = btrfs_inc_extent_ref;
2488 else
2489 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002490
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002491 if (full_backref)
2492 parent = buf->start;
2493 else
2494 parent = 0;
2495
Zheng Yan31840ae2008-09-23 13:14:14 -04002496 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002497 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002498 btrfs_item_key_to_cpu(buf, &key, i);
2499 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002500 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002501 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002502 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002503 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002504 BTRFS_FILE_EXTENT_INLINE)
2505 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002506 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2507 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002508 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002509
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002510 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2511 key.offset -= btrfs_file_extent_offset(buf, fi);
2512 ret = process_func(trans, root, bytenr, num_bytes,
2513 parent, ref_root, key.objectid,
2514 key.offset);
2515 if (ret)
2516 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002517 } else {
2518 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002519 num_bytes = btrfs_level_size(root, level - 1);
2520 ret = process_func(trans, root, bytenr, num_bytes,
2521 parent, ref_root, level - 1, 0);
2522 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002523 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002524 }
2525 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002526 return 0;
2527fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002528 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002529 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002530}
2531
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002532int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2533 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002534{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002535 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2536}
Zheng Yan31840ae2008-09-23 13:14:14 -04002537
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002538int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2539 struct extent_buffer *buf, int full_backref)
2540{
2541 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002542}
2543
Chris Mason9078a3e2007-04-26 16:46:15 -04002544static int write_one_cache_group(struct btrfs_trans_handle *trans,
2545 struct btrfs_root *root,
2546 struct btrfs_path *path,
2547 struct btrfs_block_group_cache *cache)
2548{
2549 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002550 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002551 unsigned long bi;
2552 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002553
Chris Mason9078a3e2007-04-26 16:46:15 -04002554 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002555 if (ret < 0)
2556 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002557 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002558
2559 leaf = path->nodes[0];
2560 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2561 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2562 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002563 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002564fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002565 if (ret)
2566 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002567 return 0;
2568
2569}
2570
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002571static struct btrfs_block_group_cache *
2572next_block_group(struct btrfs_root *root,
2573 struct btrfs_block_group_cache *cache)
2574{
2575 struct rb_node *node;
2576 spin_lock(&root->fs_info->block_group_cache_lock);
2577 node = rb_next(&cache->cache_node);
2578 btrfs_put_block_group(cache);
2579 if (node) {
2580 cache = rb_entry(node, struct btrfs_block_group_cache,
2581 cache_node);
2582 atomic_inc(&cache->count);
2583 } else
2584 cache = NULL;
2585 spin_unlock(&root->fs_info->block_group_cache_lock);
2586 return cache;
2587}
2588
Chris Mason96b51792007-10-15 16:15:19 -04002589int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2590 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002591{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002592 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002593 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002594 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002595 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002596
2597 path = btrfs_alloc_path();
2598 if (!path)
2599 return -ENOMEM;
2600
Chris Masond3977122009-01-05 21:25:51 -05002601 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002602 if (last == 0) {
2603 err = btrfs_run_delayed_refs(trans, root,
2604 (unsigned long)-1);
2605 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002606 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002607
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002608 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2609 while (cache) {
2610 if (cache->dirty)
2611 break;
2612 cache = next_block_group(root, cache);
2613 }
2614 if (!cache) {
2615 if (last == 0)
2616 break;
2617 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002618 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002619 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002620
2621 cache->dirty = 0;
2622 last = cache->key.objectid + cache->key.offset;
2623
2624 err = write_one_cache_group(trans, root, path, cache);
2625 BUG_ON(err);
2626 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002627 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002628
Chris Mason9078a3e2007-04-26 16:46:15 -04002629 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002630 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002631}
2632
Yan Zhengd2fb3432008-12-11 16:30:39 -05002633int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2634{
2635 struct btrfs_block_group_cache *block_group;
2636 int readonly = 0;
2637
2638 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2639 if (!block_group || block_group->ro)
2640 readonly = 1;
2641 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002642 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002643 return readonly;
2644}
2645
Chris Mason593060d2008-03-25 16:50:33 -04002646static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2647 u64 total_bytes, u64 bytes_used,
2648 struct btrfs_space_info **space_info)
2649{
2650 struct btrfs_space_info *found;
2651
2652 found = __find_space_info(info, flags);
2653 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002654 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002655 found->total_bytes += total_bytes;
2656 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002657 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002658 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002659 *space_info = found;
2660 return 0;
2661 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002662 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002663 if (!found)
2664 return -ENOMEM;
2665
Josef Bacik0f9dd462008-09-23 13:14:11 -04002666 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002667 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002668 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002669 found->flags = flags;
2670 found->total_bytes = total_bytes;
2671 found->bytes_used = bytes_used;
2672 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002673 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002674 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002675 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002676 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002677 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002678 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002679 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002680 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002681 return 0;
2682}
2683
Chris Mason8790d502008-04-03 16:29:03 -04002684static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2685{
2686 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002687 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002688 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002689 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002690 if (extra_flags) {
2691 if (flags & BTRFS_BLOCK_GROUP_DATA)
2692 fs_info->avail_data_alloc_bits |= extra_flags;
2693 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2694 fs_info->avail_metadata_alloc_bits |= extra_flags;
2695 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2696 fs_info->avail_system_alloc_bits |= extra_flags;
2697 }
2698}
Chris Mason593060d2008-03-25 16:50:33 -04002699
Yan Zhengc146afa2008-11-12 14:34:12 -05002700static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2701{
2702 spin_lock(&cache->space_info->lock);
2703 spin_lock(&cache->lock);
2704 if (!cache->ro) {
2705 cache->space_info->bytes_readonly += cache->key.offset -
2706 btrfs_block_group_used(&cache->item);
2707 cache->ro = 1;
2708 }
2709 spin_unlock(&cache->lock);
2710 spin_unlock(&cache->space_info->lock);
2711}
2712
Yan Zheng2b820322008-11-17 21:11:30 -05002713u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002714{
Yan Zheng2b820322008-11-17 21:11:30 -05002715 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002716
2717 if (num_devices == 1)
2718 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2719 if (num_devices < 4)
2720 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2721
Chris Masonec44a352008-04-28 15:29:52 -04002722 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2723 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002724 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002725 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002726 }
Chris Masonec44a352008-04-28 15:29:52 -04002727
2728 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002729 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002730 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002731 }
Chris Masonec44a352008-04-28 15:29:52 -04002732
2733 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2734 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2735 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2736 (flags & BTRFS_BLOCK_GROUP_DUP)))
2737 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2738 return flags;
2739}
2740
Josef Bacik6a632092009-02-20 11:00:09 -05002741static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2742{
2743 struct btrfs_fs_info *info = root->fs_info;
2744 u64 alloc_profile;
2745
2746 if (data) {
2747 alloc_profile = info->avail_data_alloc_bits &
2748 info->data_alloc_profile;
2749 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2750 } else if (root == root->fs_info->chunk_root) {
2751 alloc_profile = info->avail_system_alloc_bits &
2752 info->system_alloc_profile;
2753 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2754 } else {
2755 alloc_profile = info->avail_metadata_alloc_bits &
2756 info->metadata_alloc_profile;
2757 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2758 }
2759
2760 return btrfs_reduce_alloc_profile(root, data);
2761}
2762
2763void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2764{
2765 u64 alloc_target;
2766
2767 alloc_target = btrfs_get_alloc_profile(root, 1);
2768 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2769 alloc_target);
2770}
2771
Josef Bacik9ed74f22009-09-11 16:12:44 -04002772static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2773{
2774 u64 num_bytes;
2775 int level;
2776
2777 level = BTRFS_MAX_LEVEL - 2;
2778 /*
2779 * NOTE: these calculations are absolutely the worst possible case.
2780 * This assumes that _every_ item we insert will require a new leaf, and
2781 * that the tree has grown to its maximum level size.
2782 */
2783
2784 /*
2785 * for every item we insert we could insert both an extent item and a
2786 * extent ref item. Then for ever item we insert, we will need to cow
2787 * both the original leaf, plus the leaf to the left and right of it.
2788 *
2789 * Unless we are talking about the extent root, then we just want the
2790 * number of items * 2, since we just need the extent item plus its ref.
2791 */
2792 if (root == root->fs_info->extent_root)
2793 num_bytes = num_items * 2;
2794 else
2795 num_bytes = (num_items + (2 * num_items)) * 3;
2796
2797 /*
2798 * num_bytes is total number of leaves we could need times the leaf
2799 * size, and then for every leaf we could end up cow'ing 2 nodes per
2800 * level, down to the leaf level.
2801 */
2802 num_bytes = (num_bytes * root->leafsize) +
2803 (num_bytes * (level * 2)) * root->nodesize;
2804
2805 return num_bytes;
2806}
2807
Josef Bacik6a632092009-02-20 11:00:09 -05002808/*
Josef Bacik9ed74f22009-09-11 16:12:44 -04002809 * Unreserve metadata space for delalloc. If we have less reserved credits than
2810 * we have extents, this function does nothing.
Josef Bacik6a632092009-02-20 11:00:09 -05002811 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04002812int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2813 struct inode *inode, int num_items)
Josef Bacik6a632092009-02-20 11:00:09 -05002814{
2815 struct btrfs_fs_info *info = root->fs_info;
2816 struct btrfs_space_info *meta_sinfo;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002817 u64 num_bytes;
2818 u64 alloc_target;
2819 bool bug = false;
Josef Bacik6a632092009-02-20 11:00:09 -05002820
2821 /* get the space info for where the metadata will live */
2822 alloc_target = btrfs_get_alloc_profile(root, 0);
2823 meta_sinfo = __find_space_info(info, alloc_target);
2824
Josef Bacik9ed74f22009-09-11 16:12:44 -04002825 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2826 num_items);
2827
2828 spin_lock(&meta_sinfo->lock);
Josef Bacik32c00af2009-10-08 13:34:05 -04002829 spin_lock(&BTRFS_I(inode)->accounting_lock);
2830 if (BTRFS_I(inode)->reserved_extents <=
2831 BTRFS_I(inode)->outstanding_extents) {
2832 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002833 spin_unlock(&meta_sinfo->lock);
2834 return 0;
2835 }
Josef Bacik32c00af2009-10-08 13:34:05 -04002836 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002837
Josef Bacik32c00af2009-10-08 13:34:05 -04002838 BTRFS_I(inode)->reserved_extents--;
2839 BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002840
2841 if (meta_sinfo->bytes_delalloc < num_bytes) {
2842 bug = true;
2843 meta_sinfo->bytes_delalloc = 0;
2844 } else {
2845 meta_sinfo->bytes_delalloc -= num_bytes;
2846 }
2847 spin_unlock(&meta_sinfo->lock);
2848
2849 BUG_ON(bug);
2850
2851 return 0;
2852}
2853
2854static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2855{
2856 u64 thresh;
2857
2858 thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2859 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2860 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2861 meta_sinfo->bytes_may_use;
2862
2863 thresh = meta_sinfo->total_bytes - thresh;
2864 thresh *= 80;
2865 do_div(thresh, 100);
2866 if (thresh <= meta_sinfo->bytes_delalloc)
2867 meta_sinfo->force_delalloc = 1;
2868 else
2869 meta_sinfo->force_delalloc = 0;
2870}
2871
Josef Bacike3ccfa92009-10-07 20:44:34 -04002872struct async_flush {
2873 struct btrfs_root *root;
2874 struct btrfs_space_info *info;
2875 struct btrfs_work work;
2876};
2877
2878static noinline void flush_delalloc_async(struct btrfs_work *work)
2879{
2880 struct async_flush *async;
2881 struct btrfs_root *root;
2882 struct btrfs_space_info *info;
2883
2884 async = container_of(work, struct async_flush, work);
2885 root = async->root;
2886 info = async->info;
2887
2888 btrfs_start_delalloc_inodes(root);
2889 wake_up(&info->flush_wait);
2890 btrfs_wait_ordered_extents(root, 0);
2891
2892 spin_lock(&info->lock);
2893 info->flushing = 0;
2894 spin_unlock(&info->lock);
2895 wake_up(&info->flush_wait);
2896
2897 kfree(async);
2898}
2899
2900static void wait_on_flush(struct btrfs_space_info *info)
2901{
2902 DEFINE_WAIT(wait);
2903 u64 used;
2904
2905 while (1) {
2906 prepare_to_wait(&info->flush_wait, &wait,
2907 TASK_UNINTERRUPTIBLE);
2908 spin_lock(&info->lock);
2909 if (!info->flushing) {
2910 spin_unlock(&info->lock);
2911 break;
2912 }
2913
2914 used = info->bytes_used + info->bytes_reserved +
2915 info->bytes_pinned + info->bytes_readonly +
2916 info->bytes_super + info->bytes_root +
2917 info->bytes_may_use + info->bytes_delalloc;
2918 if (used < info->total_bytes) {
2919 spin_unlock(&info->lock);
2920 break;
2921 }
2922 spin_unlock(&info->lock);
2923 schedule();
2924 }
2925 finish_wait(&info->flush_wait, &wait);
2926}
2927
Josef Bacik32c00af2009-10-08 13:34:05 -04002928static void flush_delalloc(struct btrfs_root *root,
2929 struct btrfs_space_info *info)
2930{
Josef Bacike3ccfa92009-10-07 20:44:34 -04002931 struct async_flush *async;
Josef Bacik32c00af2009-10-08 13:34:05 -04002932 bool wait = false;
2933
2934 spin_lock(&info->lock);
2935
2936 if (!info->flushing) {
2937 info->flushing = 1;
2938 init_waitqueue_head(&info->flush_wait);
2939 } else {
2940 wait = true;
2941 }
2942
2943 spin_unlock(&info->lock);
2944
2945 if (wait) {
Josef Bacike3ccfa92009-10-07 20:44:34 -04002946 wait_on_flush(info);
Josef Bacik32c00af2009-10-08 13:34:05 -04002947 return;
2948 }
2949
Josef Bacike3ccfa92009-10-07 20:44:34 -04002950 async = kzalloc(sizeof(*async), GFP_NOFS);
2951 if (!async)
2952 goto flush;
2953
2954 async->root = root;
2955 async->info = info;
2956 async->work.func = flush_delalloc_async;
2957
2958 btrfs_queue_worker(&root->fs_info->enospc_workers,
2959 &async->work);
2960 wait_on_flush(info);
2961 return;
2962
2963flush:
Josef Bacik32c00af2009-10-08 13:34:05 -04002964 btrfs_start_delalloc_inodes(root);
2965 btrfs_wait_ordered_extents(root, 0);
2966
2967 spin_lock(&info->lock);
2968 info->flushing = 0;
2969 spin_unlock(&info->lock);
2970 wake_up(&info->flush_wait);
2971}
2972
Josef Bacik9ed74f22009-09-11 16:12:44 -04002973static int maybe_allocate_chunk(struct btrfs_root *root,
2974 struct btrfs_space_info *info)
2975{
2976 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
2977 struct btrfs_trans_handle *trans;
2978 bool wait = false;
2979 int ret = 0;
2980 u64 min_metadata;
2981 u64 free_space;
2982
2983 free_space = btrfs_super_total_bytes(disk_super);
2984 /*
2985 * we allow the metadata to grow to a max of either 5gb or 5% of the
2986 * space in the volume.
2987 */
2988 min_metadata = min((u64)5 * 1024 * 1024 * 1024,
2989 div64_u64(free_space * 5, 100));
2990 if (info->total_bytes >= min_metadata) {
2991 spin_unlock(&info->lock);
2992 return 0;
2993 }
2994
2995 if (info->full) {
2996 spin_unlock(&info->lock);
2997 return 0;
2998 }
2999
3000 if (!info->allocating_chunk) {
3001 info->force_alloc = 1;
3002 info->allocating_chunk = 1;
Josef Bacike3ccfa92009-10-07 20:44:34 -04003003 init_waitqueue_head(&info->allocate_wait);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003004 } else {
3005 wait = true;
3006 }
3007
3008 spin_unlock(&info->lock);
3009
3010 if (wait) {
Josef Bacike3ccfa92009-10-07 20:44:34 -04003011 wait_event(info->allocate_wait,
Josef Bacik9ed74f22009-09-11 16:12:44 -04003012 !info->allocating_chunk);
3013 return 1;
3014 }
3015
3016 trans = btrfs_start_transaction(root, 1);
3017 if (!trans) {
3018 ret = -ENOMEM;
3019 goto out;
3020 }
3021
3022 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3023 4096 + 2 * 1024 * 1024,
3024 info->flags, 0);
3025 btrfs_end_transaction(trans, root);
3026 if (ret)
3027 goto out;
3028out:
3029 spin_lock(&info->lock);
3030 info->allocating_chunk = 0;
3031 spin_unlock(&info->lock);
Josef Bacike3ccfa92009-10-07 20:44:34 -04003032 wake_up(&info->allocate_wait);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003033
3034 if (ret)
3035 return 0;
3036 return 1;
3037}
3038
3039/*
3040 * Reserve metadata space for delalloc.
3041 */
3042int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
3043 struct inode *inode, int num_items)
3044{
3045 struct btrfs_fs_info *info = root->fs_info;
3046 struct btrfs_space_info *meta_sinfo;
3047 u64 num_bytes;
3048 u64 used;
3049 u64 alloc_target;
3050 int flushed = 0;
3051 int force_delalloc;
3052
3053 /* get the space info for where the metadata will live */
3054 alloc_target = btrfs_get_alloc_profile(root, 0);
3055 meta_sinfo = __find_space_info(info, alloc_target);
3056
3057 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
3058 num_items);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003059again:
Josef Bacik6a632092009-02-20 11:00:09 -05003060 spin_lock(&meta_sinfo->lock);
Josef Bacik6a632092009-02-20 11:00:09 -05003061
Josef Bacik9ed74f22009-09-11 16:12:44 -04003062 force_delalloc = meta_sinfo->force_delalloc;
Josef Bacik6a632092009-02-20 11:00:09 -05003063
Josef Bacik9ed74f22009-09-11 16:12:44 -04003064 if (unlikely(!meta_sinfo->bytes_root))
3065 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3066
3067 if (!flushed)
3068 meta_sinfo->bytes_delalloc += num_bytes;
3069
3070 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3071 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3072 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3073 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3074
3075 if (used > meta_sinfo->total_bytes) {
3076 flushed++;
3077
3078 if (flushed == 1) {
3079 if (maybe_allocate_chunk(root, meta_sinfo))
3080 goto again;
3081 flushed++;
3082 } else {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003083 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003084 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003085
Josef Bacik9ed74f22009-09-11 16:12:44 -04003086 if (flushed == 2) {
3087 filemap_flush(inode->i_mapping);
3088 goto again;
3089 } else if (flushed == 3) {
Josef Bacik32c00af2009-10-08 13:34:05 -04003090 flush_delalloc(root, meta_sinfo);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003091 goto again;
3092 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003093 spin_lock(&meta_sinfo->lock);
3094 meta_sinfo->bytes_delalloc -= num_bytes;
Josef Bacik6a632092009-02-20 11:00:09 -05003095 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003096 printk(KERN_ERR "enospc, has %d, reserved %d\n",
Josef Bacik32c00af2009-10-08 13:34:05 -04003097 BTRFS_I(inode)->outstanding_extents,
3098 BTRFS_I(inode)->reserved_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003099 dump_space_info(meta_sinfo, 0, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003100 return -ENOSPC;
3101 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003102
Josef Bacik32c00af2009-10-08 13:34:05 -04003103 BTRFS_I(inode)->reserved_extents++;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003104 check_force_delalloc(meta_sinfo);
3105 spin_unlock(&meta_sinfo->lock);
3106
3107 if (!flushed && force_delalloc)
3108 filemap_flush(inode->i_mapping);
3109
3110 return 0;
3111}
3112
3113/*
3114 * unreserve num_items number of items worth of metadata space. This needs to
3115 * be paired with btrfs_reserve_metadata_space.
3116 *
3117 * NOTE: if you have the option, run this _AFTER_ you do a
3118 * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
3119 * oprations which will result in more used metadata, so we want to make sure we
3120 * can do that without issue.
3121 */
3122int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
3123{
3124 struct btrfs_fs_info *info = root->fs_info;
3125 struct btrfs_space_info *meta_sinfo;
3126 u64 num_bytes;
3127 u64 alloc_target;
3128 bool bug = false;
3129
3130 /* get the space info for where the metadata will live */
3131 alloc_target = btrfs_get_alloc_profile(root, 0);
3132 meta_sinfo = __find_space_info(info, alloc_target);
3133
3134 num_bytes = calculate_bytes_needed(root, num_items);
3135
3136 spin_lock(&meta_sinfo->lock);
3137 if (meta_sinfo->bytes_may_use < num_bytes) {
3138 bug = true;
3139 meta_sinfo->bytes_may_use = 0;
3140 } else {
3141 meta_sinfo->bytes_may_use -= num_bytes;
3142 }
3143 spin_unlock(&meta_sinfo->lock);
3144
3145 BUG_ON(bug);
3146
3147 return 0;
3148}
3149
3150/*
3151 * Reserve some metadata space for use. We'll calculate the worste case number
3152 * of bytes that would be needed to modify num_items number of items. If we
3153 * have space, fantastic, if not, you get -ENOSPC. Please call
3154 * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
3155 * items you reserved, since whatever metadata you needed should have already
3156 * been allocated.
3157 *
3158 * This will commit the transaction to make more space if we don't have enough
3159 * metadata space. THe only time we don't do this is if we're reserving space
3160 * inside of a transaction, then we will just return -ENOSPC and it is the
3161 * callers responsibility to handle it properly.
3162 */
3163int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3164{
3165 struct btrfs_fs_info *info = root->fs_info;
3166 struct btrfs_space_info *meta_sinfo;
3167 u64 num_bytes;
3168 u64 used;
3169 u64 alloc_target;
3170 int retries = 0;
3171
3172 /* get the space info for where the metadata will live */
3173 alloc_target = btrfs_get_alloc_profile(root, 0);
3174 meta_sinfo = __find_space_info(info, alloc_target);
3175
3176 num_bytes = calculate_bytes_needed(root, num_items);
3177again:
3178 spin_lock(&meta_sinfo->lock);
3179
3180 if (unlikely(!meta_sinfo->bytes_root))
3181 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3182
3183 if (!retries)
3184 meta_sinfo->bytes_may_use += num_bytes;
3185
3186 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3187 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3188 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3189 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3190
3191 if (used > meta_sinfo->total_bytes) {
3192 retries++;
3193 if (retries == 1) {
3194 if (maybe_allocate_chunk(root, meta_sinfo))
3195 goto again;
3196 retries++;
3197 } else {
3198 spin_unlock(&meta_sinfo->lock);
3199 }
3200
3201 if (retries == 2) {
Josef Bacik32c00af2009-10-08 13:34:05 -04003202 flush_delalloc(root, meta_sinfo);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003203 goto again;
3204 }
3205 spin_lock(&meta_sinfo->lock);
3206 meta_sinfo->bytes_may_use -= num_bytes;
3207 spin_unlock(&meta_sinfo->lock);
3208
3209 dump_space_info(meta_sinfo, 0, 0);
3210 return -ENOSPC;
3211 }
3212
3213 check_force_delalloc(meta_sinfo);
Josef Bacik6a632092009-02-20 11:00:09 -05003214 spin_unlock(&meta_sinfo->lock);
3215
3216 return 0;
3217}
3218
3219/*
3220 * This will check the space that the inode allocates from to make sure we have
3221 * enough space for bytes.
3222 */
3223int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3224 u64 bytes)
3225{
3226 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003227 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003228
3229 /* make sure bytes are sectorsize aligned */
3230 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3231
3232 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003233 if (!data_sinfo)
3234 goto alloc;
3235
Josef Bacik6a632092009-02-20 11:00:09 -05003236again:
3237 /* make sure we have enough space to handle the data first */
3238 spin_lock(&data_sinfo->lock);
3239 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
3240 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
3241 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
Josef Bacik1b2da372009-09-11 16:11:20 -04003242 data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003243 struct btrfs_trans_handle *trans;
3244
Josef Bacik6a632092009-02-20 11:00:09 -05003245 /*
3246 * if we don't have enough free bytes in this space then we need
3247 * to alloc a new chunk.
3248 */
3249 if (!data_sinfo->full) {
3250 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003251
3252 data_sinfo->force_alloc = 1;
3253 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003254alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003255 alloc_target = btrfs_get_alloc_profile(root, 1);
3256 trans = btrfs_start_transaction(root, 1);
3257 if (!trans)
3258 return -ENOMEM;
3259
3260 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3261 bytes + 2 * 1024 * 1024,
3262 alloc_target, 0);
3263 btrfs_end_transaction(trans, root);
3264 if (ret)
3265 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04003266
3267 if (!data_sinfo) {
3268 btrfs_set_inode_space_info(root, inode);
3269 data_sinfo = BTRFS_I(inode)->space_info;
3270 }
Josef Bacik6a632092009-02-20 11:00:09 -05003271 goto again;
3272 }
3273 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003274
3275 /* commit the current transaction and try again */
Sage Weildd7e0b72009-09-29 18:38:44 -04003276 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003277 committed = 1;
3278 trans = btrfs_join_transaction(root, 1);
3279 if (!trans)
3280 return -ENOMEM;
3281 ret = btrfs_commit_transaction(trans, root);
3282 if (ret)
3283 return ret;
3284 goto again;
3285 }
3286
Josef Bacik6a632092009-02-20 11:00:09 -05003287 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3288 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04003289 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07003290 "%llu total\n", (unsigned long long)bytes,
3291 (unsigned long long)data_sinfo->bytes_delalloc,
3292 (unsigned long long)data_sinfo->bytes_used,
3293 (unsigned long long)data_sinfo->bytes_reserved,
3294 (unsigned long long)data_sinfo->bytes_pinned,
3295 (unsigned long long)data_sinfo->bytes_readonly,
3296 (unsigned long long)data_sinfo->bytes_may_use,
3297 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003298 return -ENOSPC;
3299 }
3300 data_sinfo->bytes_may_use += bytes;
3301 BTRFS_I(inode)->reserved_bytes += bytes;
3302 spin_unlock(&data_sinfo->lock);
3303
Josef Bacik9ed74f22009-09-11 16:12:44 -04003304 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003305}
3306
3307/*
3308 * if there was an error for whatever reason after calling
3309 * btrfs_check_data_free_space, call this so we can cleanup the counters.
3310 */
3311void btrfs_free_reserved_data_space(struct btrfs_root *root,
3312 struct inode *inode, u64 bytes)
3313{
3314 struct btrfs_space_info *data_sinfo;
3315
3316 /* make sure bytes are sectorsize aligned */
3317 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3318
3319 data_sinfo = BTRFS_I(inode)->space_info;
3320 spin_lock(&data_sinfo->lock);
3321 data_sinfo->bytes_may_use -= bytes;
3322 BTRFS_I(inode)->reserved_bytes -= bytes;
3323 spin_unlock(&data_sinfo->lock);
3324}
3325
3326/* called when we are adding a delalloc extent to the inode's io_tree */
3327void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3328 u64 bytes)
3329{
3330 struct btrfs_space_info *data_sinfo;
3331
3332 /* get the space info for where this inode will be storing its data */
3333 data_sinfo = BTRFS_I(inode)->space_info;
3334
3335 /* make sure we have enough space to handle the data first */
3336 spin_lock(&data_sinfo->lock);
3337 data_sinfo->bytes_delalloc += bytes;
3338
3339 /*
3340 * we are adding a delalloc extent without calling
3341 * btrfs_check_data_free_space first. This happens on a weird
3342 * writepage condition, but shouldn't hurt our accounting
3343 */
3344 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3345 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3346 BTRFS_I(inode)->reserved_bytes = 0;
3347 } else {
3348 data_sinfo->bytes_may_use -= bytes;
3349 BTRFS_I(inode)->reserved_bytes -= bytes;
3350 }
3351
3352 spin_unlock(&data_sinfo->lock);
3353}
3354
3355/* called when we are clearing an delalloc extent from the inode's io_tree */
3356void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3357 u64 bytes)
3358{
3359 struct btrfs_space_info *info;
3360
3361 info = BTRFS_I(inode)->space_info;
3362
3363 spin_lock(&info->lock);
3364 info->bytes_delalloc -= bytes;
3365 spin_unlock(&info->lock);
3366}
3367
Josef Bacik97e728d2009-04-21 17:40:57 -04003368static void force_metadata_allocation(struct btrfs_fs_info *info)
3369{
3370 struct list_head *head = &info->space_info;
3371 struct btrfs_space_info *found;
3372
3373 rcu_read_lock();
3374 list_for_each_entry_rcu(found, head, list) {
3375 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3376 found->force_alloc = 1;
3377 }
3378 rcu_read_unlock();
3379}
3380
Chris Mason6324fbf2008-03-24 15:01:59 -04003381static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3382 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003383 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003384{
3385 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003386 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003387 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05003388 int ret = 0;
3389
Josef Bacik97e728d2009-04-21 17:40:57 -04003390 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003391
Yan Zheng2b820322008-11-17 21:11:30 -05003392 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003393
Chris Mason6324fbf2008-03-24 15:01:59 -04003394 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003395 if (!space_info) {
3396 ret = update_space_info(extent_root->fs_info, flags,
3397 0, 0, &space_info);
3398 BUG_ON(ret);
3399 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003400 BUG_ON(!space_info);
3401
Josef Bacik25179202008-10-29 14:49:05 -04003402 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003403 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003404 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003405 if (space_info->full) {
3406 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003407 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003408 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003409
Yan Zhengc146afa2008-11-12 14:34:12 -05003410 thresh = space_info->total_bytes - space_info->bytes_readonly;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003411 thresh = div_factor(thresh, 8);
Chris Mason0ef3e662008-05-24 14:04:53 -04003412 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04003413 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04003414 space_info->bytes_reserved + alloc_bytes) < thresh) {
3415 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003416 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003417 }
Josef Bacik25179202008-10-29 14:49:05 -04003418 spin_unlock(&space_info->lock);
3419
Josef Bacik97e728d2009-04-21 17:40:57 -04003420 /*
3421 * if we're doing a data chunk, go ahead and make sure that
3422 * we keep a reasonable number of metadata chunks allocated in the
3423 * FS as well.
3424 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003425 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003426 fs_info->data_chunk_allocations++;
3427 if (!(fs_info->data_chunk_allocations %
3428 fs_info->metadata_ratio))
3429 force_metadata_allocation(fs_info);
3430 }
3431
Yan Zheng2b820322008-11-17 21:11:30 -05003432 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003433 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003434 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003435 space_info->full = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003436 space_info->force_alloc = 0;
3437 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003438out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003439 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003440 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003441}
3442
Chris Mason9078a3e2007-04-26 16:46:15 -04003443static int update_block_group(struct btrfs_trans_handle *trans,
3444 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04003445 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04003446 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04003447{
3448 struct btrfs_block_group_cache *cache;
3449 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04003450 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003451 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003452 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04003453
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003454 /* block accounting for super block */
3455 spin_lock(&info->delalloc_lock);
3456 old_val = btrfs_super_bytes_used(&info->super_copy);
3457 if (alloc)
3458 old_val += num_bytes;
3459 else
3460 old_val -= num_bytes;
3461 btrfs_set_super_bytes_used(&info->super_copy, old_val);
3462
3463 /* block accounting for root item */
3464 old_val = btrfs_root_used(&root->root_item);
3465 if (alloc)
3466 old_val += num_bytes;
3467 else
3468 old_val -= num_bytes;
3469 btrfs_set_root_used(&root->root_item, old_val);
3470 spin_unlock(&info->delalloc_lock);
3471
Chris Masond3977122009-01-05 21:25:51 -05003472 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003473 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003474 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04003475 return -1;
Chris Masondb945352007-10-15 16:15:53 -04003476 byte_in_group = bytenr - cache->key.objectid;
3477 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003478
Josef Bacik25179202008-10-29 14:49:05 -04003479 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003480 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003481 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04003482 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04003483 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04003484 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04003485 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003486 btrfs_set_block_group_used(&cache->item, old_val);
3487 cache->reserved -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003488 cache->space_info->bytes_used += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003489 cache->space_info->bytes_reserved -= num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003490 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05003491 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003492 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003493 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003494 } else {
Chris Masondb945352007-10-15 16:15:53 -04003495 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003496 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05003497 if (cache->ro)
3498 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003499 btrfs_set_block_group_used(&cache->item, old_val);
3500 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003501 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04003502 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003503 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003504
3505 ret = btrfs_discard_extent(root, bytenr,
3506 num_bytes);
3507 WARN_ON(ret);
3508
Josef Bacik0f9dd462008-09-23 13:14:11 -04003509 ret = btrfs_add_free_space(cache, bytenr,
3510 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003511 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04003512 }
Chris Masoncd1bc462007-04-27 10:08:34 -04003513 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003514 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003515 total -= num_bytes;
3516 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003517 }
3518 return 0;
3519}
Chris Mason6324fbf2008-03-24 15:01:59 -04003520
Chris Masona061fc82008-05-07 11:43:44 -04003521static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3522{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003523 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003524 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003525
3526 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3527 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003528 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003529
Yan Zhengd2fb3432008-12-11 16:30:39 -05003530 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003531 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003532
3533 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003534}
3535
Yan Zheng11833d62009-09-11 16:11:19 -04003536/*
3537 * this function must be called within transaction
3538 */
3539int btrfs_pin_extent(struct btrfs_root *root,
3540 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05003541{
Yan324ae4d2007-11-16 14:57:08 -05003542 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -04003543 struct btrfs_block_group_cache *cache;
Yan324ae4d2007-11-16 14:57:08 -05003544
Yan Zheng11833d62009-09-11 16:11:19 -04003545 cache = btrfs_lookup_block_group(fs_info, bytenr);
3546 BUG_ON(!cache);
Chris Masonb9473432009-03-13 11:00:37 -04003547
Yan Zheng11833d62009-09-11 16:11:19 -04003548 spin_lock(&cache->space_info->lock);
3549 spin_lock(&cache->lock);
3550 cache->pinned += num_bytes;
3551 cache->space_info->bytes_pinned += num_bytes;
3552 if (reserved) {
3553 cache->reserved -= num_bytes;
3554 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05003555 }
Yan Zheng11833d62009-09-11 16:11:19 -04003556 spin_unlock(&cache->lock);
3557 spin_unlock(&cache->space_info->lock);
3558
3559 btrfs_put_block_group(cache);
3560
3561 set_extent_dirty(fs_info->pinned_extents,
3562 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Yan324ae4d2007-11-16 14:57:08 -05003563 return 0;
3564}
Chris Mason9078a3e2007-04-26 16:46:15 -04003565
Yan Zheng11833d62009-09-11 16:11:19 -04003566static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3567 u64 num_bytes, int reserve)
Zheng Yane8569812008-09-26 10:05:48 -04003568{
Yan Zheng11833d62009-09-11 16:11:19 -04003569 spin_lock(&cache->space_info->lock);
3570 spin_lock(&cache->lock);
3571 if (reserve) {
3572 cache->reserved += num_bytes;
3573 cache->space_info->bytes_reserved += num_bytes;
3574 } else {
3575 cache->reserved -= num_bytes;
3576 cache->space_info->bytes_reserved -= num_bytes;
3577 }
3578 spin_unlock(&cache->lock);
3579 spin_unlock(&cache->space_info->lock);
3580 return 0;
3581}
Zheng Yane8569812008-09-26 10:05:48 -04003582
Yan Zheng11833d62009-09-11 16:11:19 -04003583int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3584 struct btrfs_root *root)
3585{
3586 struct btrfs_fs_info *fs_info = root->fs_info;
3587 struct btrfs_caching_control *next;
3588 struct btrfs_caching_control *caching_ctl;
3589 struct btrfs_block_group_cache *cache;
3590
3591 down_write(&fs_info->extent_commit_sem);
3592
3593 list_for_each_entry_safe(caching_ctl, next,
3594 &fs_info->caching_block_groups, list) {
3595 cache = caching_ctl->block_group;
3596 if (block_group_cache_done(cache)) {
3597 cache->last_byte_to_unpin = (u64)-1;
3598 list_del_init(&caching_ctl->list);
3599 put_caching_control(caching_ctl);
3600 } else {
3601 cache->last_byte_to_unpin = caching_ctl->progress;
3602 }
3603 }
3604
3605 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3606 fs_info->pinned_extents = &fs_info->freed_extents[1];
3607 else
3608 fs_info->pinned_extents = &fs_info->freed_extents[0];
3609
3610 up_write(&fs_info->extent_commit_sem);
3611 return 0;
3612}
3613
3614static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3615{
3616 struct btrfs_fs_info *fs_info = root->fs_info;
3617 struct btrfs_block_group_cache *cache = NULL;
3618 u64 len;
3619
3620 while (start <= end) {
3621 if (!cache ||
3622 start >= cache->key.objectid + cache->key.offset) {
3623 if (cache)
3624 btrfs_put_block_group(cache);
3625 cache = btrfs_lookup_block_group(fs_info, start);
3626 BUG_ON(!cache);
3627 }
3628
3629 len = cache->key.objectid + cache->key.offset - start;
3630 len = min(len, end + 1 - start);
3631
3632 if (start < cache->last_byte_to_unpin) {
3633 len = min(len, cache->last_byte_to_unpin - start);
3634 btrfs_add_free_space(cache, start, len);
3635 }
Josef Bacik25179202008-10-29 14:49:05 -04003636
3637 spin_lock(&cache->space_info->lock);
3638 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003639 cache->pinned -= len;
3640 cache->space_info->bytes_pinned -= len;
Josef Bacik25179202008-10-29 14:49:05 -04003641 spin_unlock(&cache->lock);
3642 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003643
3644 start += len;
3645 }
3646
3647 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003648 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04003649 return 0;
3650}
3651
3652int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04003653 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05003654{
Yan Zheng11833d62009-09-11 16:11:19 -04003655 struct btrfs_fs_info *fs_info = root->fs_info;
3656 struct extent_io_tree *unpin;
Chris Mason1a5bc1672007-10-15 16:15:26 -04003657 u64 start;
3658 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003659 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003660
Yan Zheng11833d62009-09-11 16:11:19 -04003661 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3662 unpin = &fs_info->freed_extents[1];
3663 else
3664 unpin = &fs_info->freed_extents[0];
3665
Chris Masond3977122009-01-05 21:25:51 -05003666 while (1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04003667 ret = find_first_extent_bit(unpin, 0, &start, &end,
3668 EXTENT_DIRTY);
3669 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003670 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003671
3672 ret = btrfs_discard_extent(root, start, end + 1 - start);
3673
Chris Mason1a5bc1672007-10-15 16:15:26 -04003674 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04003675 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04003676 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003677 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003678
Liu Hui1f3c79a2009-01-05 15:57:51 -05003679 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003680}
3681
Zheng Yan31840ae2008-09-23 13:14:14 -04003682static int pin_down_bytes(struct btrfs_trans_handle *trans,
3683 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003684 struct btrfs_path *path,
Yan Zheng11833d62009-09-11 16:11:19 -04003685 u64 bytenr, u64 num_bytes,
3686 int is_data, int reserved,
Chris Masonb9473432009-03-13 11:00:37 -04003687 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003688{
Chris Mason1a5bc1672007-10-15 16:15:26 -04003689 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003690 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003691
Zheng Yan31840ae2008-09-23 13:14:14 -04003692 if (is_data)
3693 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003694
Zheng Yan31840ae2008-09-23 13:14:14 -04003695 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3696 if (!buf)
3697 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003698
Zheng Yan31840ae2008-09-23 13:14:14 -04003699 /* we can reuse a block if it hasn't been written
3700 * and it is from this transaction. We can't
3701 * reuse anything from the tree log root because
3702 * it has tiny sub-transactions.
3703 */
3704 if (btrfs_buffer_uptodate(buf, 0) &&
3705 btrfs_try_tree_lock(buf)) {
3706 u64 header_owner = btrfs_header_owner(buf);
3707 u64 header_transid = btrfs_header_generation(buf);
3708 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3709 header_transid == trans->transid &&
3710 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003711 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003712 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003713 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003714 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003715 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003716 free_extent_buffer(buf);
3717pinit:
Yan Zheng11833d62009-09-11 16:11:19 -04003718 if (path)
3719 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003720 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003721 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
Zheng Yan31840ae2008-09-23 13:14:14 -04003722
Chris Masonbe744172007-05-06 10:15:01 -04003723 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003724 return 0;
3725}
3726
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003727static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3728 struct btrfs_root *root,
3729 u64 bytenr, u64 num_bytes, u64 parent,
3730 u64 root_objectid, u64 owner_objectid,
3731 u64 owner_offset, int refs_to_drop,
3732 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003733{
Chris Masone2fa7222007-03-12 16:22:34 -04003734 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003735 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003736 struct btrfs_fs_info *info = root->fs_info;
3737 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003738 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003739 struct btrfs_extent_item *ei;
3740 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003741 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003742 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003743 int extent_slot = 0;
3744 int found_extent = 0;
3745 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003746 u32 item_size;
3747 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003748
Chris Mason5caf2a02007-04-02 11:20:42 -04003749 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003750 if (!path)
3751 return -ENOMEM;
3752
Chris Mason3c12ac72008-04-21 12:01:38 -04003753 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003754 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003755
3756 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3757 BUG_ON(!is_data && refs_to_drop != 1);
3758
3759 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3760 bytenr, num_bytes, parent,
3761 root_objectid, owner_objectid,
3762 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003763 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003764 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003765 while (extent_slot >= 0) {
3766 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003767 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003768 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003769 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003770 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3771 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003772 found_extent = 1;
3773 break;
3774 }
3775 if (path->slots[0] - extent_slot > 5)
3776 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003777 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003778 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003779#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3780 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3781 if (found_extent && item_size < sizeof(*ei))
3782 found_extent = 0;
3783#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003784 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003785 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003786 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003787 NULL, refs_to_drop,
3788 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003789 BUG_ON(ret);
3790 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003791 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003792
3793 key.objectid = bytenr;
3794 key.type = BTRFS_EXTENT_ITEM_KEY;
3795 key.offset = num_bytes;
3796
Zheng Yan31840ae2008-09-23 13:14:14 -04003797 ret = btrfs_search_slot(trans, extent_root,
3798 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003799 if (ret) {
3800 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003801 ", was looking for %llu\n", ret,
3802 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003803 btrfs_print_leaf(extent_root, path->nodes[0]);
3804 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003805 BUG_ON(ret);
3806 extent_slot = path->slots[0];
3807 }
Chris Mason7bb86312007-12-11 09:25:06 -05003808 } else {
3809 btrfs_print_leaf(extent_root, path->nodes[0]);
3810 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003811 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003812 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003813 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003814 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003815 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003816 (unsigned long long)owner_objectid,
3817 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003818 }
Chris Mason5f39d392007-10-15 16:14:19 -04003819
3820 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003821 item_size = btrfs_item_size_nr(leaf, extent_slot);
3822#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3823 if (item_size < sizeof(*ei)) {
3824 BUG_ON(found_extent || extent_slot != path->slots[0]);
3825 ret = convert_extent_item_v0(trans, extent_root, path,
3826 owner_objectid, 0);
3827 BUG_ON(ret < 0);
3828
3829 btrfs_release_path(extent_root, path);
3830 path->leave_spinning = 1;
3831
3832 key.objectid = bytenr;
3833 key.type = BTRFS_EXTENT_ITEM_KEY;
3834 key.offset = num_bytes;
3835
3836 ret = btrfs_search_slot(trans, extent_root, &key, path,
3837 -1, 1);
3838 if (ret) {
3839 printk(KERN_ERR "umm, got %d back from search"
3840 ", was looking for %llu\n", ret,
3841 (unsigned long long)bytenr);
3842 btrfs_print_leaf(extent_root, path->nodes[0]);
3843 }
3844 BUG_ON(ret);
3845 extent_slot = path->slots[0];
3846 leaf = path->nodes[0];
3847 item_size = btrfs_item_size_nr(leaf, extent_slot);
3848 }
3849#endif
3850 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003851 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003852 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003853 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3854 struct btrfs_tree_block_info *bi;
3855 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3856 bi = (struct btrfs_tree_block_info *)(ei + 1);
3857 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003858 }
3859
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003860 refs = btrfs_extent_refs(leaf, ei);
3861 BUG_ON(refs < refs_to_drop);
3862 refs -= refs_to_drop;
3863
3864 if (refs > 0) {
3865 if (extent_op)
3866 __run_delayed_extent_op(extent_op, leaf, ei);
3867 /*
3868 * In the case of inline back ref, reference count will
3869 * be updated by remove_extent_backref
3870 */
3871 if (iref) {
3872 BUG_ON(!found_extent);
3873 } else {
3874 btrfs_set_extent_refs(leaf, ei, refs);
3875 btrfs_mark_buffer_dirty(leaf);
3876 }
3877 if (found_extent) {
3878 ret = remove_extent_backref(trans, extent_root, path,
3879 iref, refs_to_drop,
3880 is_data);
3881 BUG_ON(ret);
3882 }
3883 } else {
3884 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003885 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003886
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003887 if (found_extent) {
3888 BUG_ON(is_data && refs_to_drop !=
3889 extent_data_ref_count(root, path, iref));
3890 if (iref) {
3891 BUG_ON(path->slots[0] != extent_slot);
3892 } else {
3893 BUG_ON(path->slots[0] != extent_slot + 1);
3894 path->slots[0] = extent_slot;
3895 num_to_del = 2;
3896 }
Chris Mason78fae272007-03-25 11:35:08 -04003897 }
Chris Masonb9473432009-03-13 11:00:37 -04003898
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003899 ret = pin_down_bytes(trans, root, path, bytenr,
Yan Zheng11833d62009-09-11 16:11:19 -04003900 num_bytes, is_data, 0, &must_clean);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003901 if (ret > 0)
3902 mark_free = 1;
3903 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003904 /*
3905 * it is going to be very rare for someone to be waiting
3906 * on the block we're freeing. del_items might need to
3907 * schedule, so rather than get fancy, just force it
3908 * to blocking here
3909 */
3910 if (must_clean)
3911 btrfs_set_lock_blocking(must_clean);
3912
Chris Mason952fcca2008-02-18 16:33:44 -05003913 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3914 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003915 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003916 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003917
Chris Masonb9473432009-03-13 11:00:37 -04003918 if (must_clean) {
3919 clean_tree_block(NULL, root, must_clean);
3920 btrfs_tree_unlock(must_clean);
3921 free_extent_buffer(must_clean);
3922 }
3923
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003924 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003925 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3926 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003927 } else {
3928 invalidate_mapping_pages(info->btree_inode->i_mapping,
3929 bytenr >> PAGE_CACHE_SHIFT,
3930 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003931 }
3932
Chris Masondcbdd4d2008-12-16 13:51:01 -05003933 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3934 mark_free);
3935 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003936 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003937 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003938 return ret;
3939}
3940
3941/*
Chris Mason1887be62009-03-13 10:11:24 -04003942 * when we free an extent, it is possible (and likely) that we free the last
3943 * delayed ref for that extent as well. This searches the delayed ref tree for
3944 * a given extent, and if there are no other delayed refs to be processed, it
3945 * removes it from the tree.
3946 */
3947static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3948 struct btrfs_root *root, u64 bytenr)
3949{
3950 struct btrfs_delayed_ref_head *head;
3951 struct btrfs_delayed_ref_root *delayed_refs;
3952 struct btrfs_delayed_ref_node *ref;
3953 struct rb_node *node;
3954 int ret;
3955
3956 delayed_refs = &trans->transaction->delayed_refs;
3957 spin_lock(&delayed_refs->lock);
3958 head = btrfs_find_delayed_ref_head(trans, bytenr);
3959 if (!head)
3960 goto out;
3961
3962 node = rb_prev(&head->node.rb_node);
3963 if (!node)
3964 goto out;
3965
3966 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3967
3968 /* there are still entries for this ref, we can't drop it */
3969 if (ref->bytenr == bytenr)
3970 goto out;
3971
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003972 if (head->extent_op) {
3973 if (!head->must_insert_reserved)
3974 goto out;
3975 kfree(head->extent_op);
3976 head->extent_op = NULL;
3977 }
3978
Chris Mason1887be62009-03-13 10:11:24 -04003979 /*
3980 * waiting for the lock here would deadlock. If someone else has it
3981 * locked they are already in the process of dropping it anyway
3982 */
3983 if (!mutex_trylock(&head->mutex))
3984 goto out;
3985
3986 /*
3987 * at this point we have a head with no other entries. Go
3988 * ahead and process it.
3989 */
3990 head->node.in_tree = 0;
3991 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04003992
Chris Mason1887be62009-03-13 10:11:24 -04003993 delayed_refs->num_entries--;
3994
3995 /*
3996 * we don't take a ref on the node because we're removing it from the
3997 * tree, so we just steal the ref the tree was holding.
3998 */
Chris Masonc3e69d52009-03-13 10:17:05 -04003999 delayed_refs->num_heads--;
4000 if (list_empty(&head->cluster))
4001 delayed_refs->num_heads_ready--;
4002
4003 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004004 spin_unlock(&delayed_refs->lock);
4005
4006 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004007 &head->node, head->extent_op,
4008 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04004009 BUG_ON(ret);
4010 btrfs_put_delayed_ref(&head->node);
4011 return 0;
4012out:
4013 spin_unlock(&delayed_refs->lock);
4014 return 0;
4015}
4016
Chris Mason925baed2008-06-25 16:01:30 -04004017int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004018 struct btrfs_root *root,
4019 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004020 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004021{
4022 int ret;
4023
Chris Mason56bec292009-03-13 10:10:06 -04004024 /*
4025 * tree log blocks never actually go into the extent allocation
4026 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004027 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004028 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4029 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004030 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004031 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004032 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004033 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4034 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4035 parent, root_objectid, (int)owner,
4036 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004037 BUG_ON(ret);
4038 ret = check_ref_cleanup(trans, root, bytenr);
4039 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004040 } else {
4041 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4042 parent, root_objectid, owner,
4043 offset, BTRFS_DROP_DELAYED_REF, NULL);
4044 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004045 }
Chris Mason925baed2008-06-25 16:01:30 -04004046 return ret;
4047}
4048
Chris Mason87ee04e2007-11-30 11:30:34 -05004049static u64 stripe_align(struct btrfs_root *root, u64 val)
4050{
4051 u64 mask = ((u64)root->stripesize - 1);
4052 u64 ret = (val + mask) & ~mask;
4053 return ret;
4054}
4055
Chris Masonfec577f2007-02-26 10:40:21 -05004056/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004057 * when we wait for progress in the block group caching, its because
4058 * our allocation attempt failed at least once. So, we must sleep
4059 * and let some progress happen before we try again.
4060 *
4061 * This function will sleep at least once waiting for new free space to
4062 * show up, and then it will check the block group free space numbers
4063 * for our min num_bytes. Another option is to have it go ahead
4064 * and look in the rbtree for a free extent of a given size, but this
4065 * is a good start.
4066 */
4067static noinline int
4068wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4069 u64 num_bytes)
4070{
Yan Zheng11833d62009-09-11 16:11:19 -04004071 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004072 DEFINE_WAIT(wait);
4073
Yan Zheng11833d62009-09-11 16:11:19 -04004074 caching_ctl = get_caching_control(cache);
4075 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004076 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004077
Yan Zheng11833d62009-09-11 16:11:19 -04004078 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004079 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004080
4081 put_caching_control(caching_ctl);
4082 return 0;
4083}
4084
4085static noinline int
4086wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4087{
4088 struct btrfs_caching_control *caching_ctl;
4089 DEFINE_WAIT(wait);
4090
4091 caching_ctl = get_caching_control(cache);
4092 if (!caching_ctl)
4093 return 0;
4094
4095 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4096
4097 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004098 return 0;
4099}
4100
4101enum btrfs_loop_type {
4102 LOOP_CACHED_ONLY = 0,
4103 LOOP_CACHING_NOWAIT = 1,
4104 LOOP_CACHING_WAIT = 2,
4105 LOOP_ALLOC_CHUNK = 3,
4106 LOOP_NO_EMPTY_SIZE = 4,
4107};
4108
4109/*
Chris Masonfec577f2007-02-26 10:40:21 -05004110 * walks the btree of allocated extents and find a hole of a given size.
4111 * The key ins is changed to record the hole:
4112 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004113 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004114 * ins->offset == number of blocks
4115 * Any available blocks before search_start are skipped.
4116 */
Chris Masond3977122009-01-05 21:25:51 -05004117static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004118 struct btrfs_root *orig_root,
4119 u64 num_bytes, u64 empty_size,
4120 u64 search_start, u64 search_end,
4121 u64 hint_byte, struct btrfs_key *ins,
4122 u64 exclude_start, u64 exclude_nr,
4123 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004124{
Josef Bacik80eb2342008-10-29 14:49:05 -04004125 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004126 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004127 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004128 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004129 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004130 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004131 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004132 int last_ptr_loop = 0;
4133 int loop = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004134 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004135 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004136 bool failed_alloc = false;
Chris Masonfec577f2007-02-26 10:40:21 -05004137
Chris Masondb945352007-10-15 16:15:53 -04004138 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004139 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004140 ins->objectid = 0;
4141 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004142
Josef Bacik2552d172009-04-03 10:14:19 -04004143 space_info = __find_space_info(root->fs_info, data);
4144
Chris Mason0ef3e662008-05-24 14:04:53 -04004145 if (orig_root->ref_cows || empty_size)
4146 allowed_chunk_alloc = 1;
4147
Chris Mason239b14b2008-03-24 15:02:07 -04004148 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004149 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004150 if (!btrfs_test_opt(root, SSD))
4151 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004152 }
4153
Chris Masonfa9c0d792009-04-03 09:47:43 -04004154 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4155 last_ptr = &root->fs_info->data_alloc_cluster;
4156 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004157
Chris Mason239b14b2008-03-24 15:02:07 -04004158 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004159 spin_lock(&last_ptr->lock);
4160 if (last_ptr->block_group)
4161 hint_byte = last_ptr->window_start;
4162 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004163 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004164
Chris Masona061fc82008-05-07 11:43:44 -04004165 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004166 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004167
Josef Bacik817d52f2009-07-13 21:29:25 -04004168 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004169 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004170
Josef Bacik2552d172009-04-03 10:14:19 -04004171 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04004172 block_group = btrfs_lookup_block_group(root->fs_info,
4173 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004174 /*
4175 * we don't want to use the block group if it doesn't match our
4176 * allocation bits, or if its not cached.
4177 */
4178 if (block_group && block_group_bits(block_group, data) &&
4179 block_group_cache_done(block_group)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004180 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004181 if (list_empty(&block_group->list) ||
4182 block_group->ro) {
4183 /*
4184 * someone is removing this block group,
4185 * we can't jump into the have_block_group
4186 * target because our list pointers are not
4187 * valid
4188 */
4189 btrfs_put_block_group(block_group);
4190 up_read(&space_info->groups_sem);
4191 } else
4192 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04004193 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004194 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004195 }
Chris Mason42e70e72008-11-07 18:17:11 -05004196 }
Chris Mason43662112008-11-07 09:06:11 -05004197
Josef Bacik2552d172009-04-03 10:14:19 -04004198search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004199 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04004200 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004201 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004202 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004203
Josef Bacik2552d172009-04-03 10:14:19 -04004204 atomic_inc(&block_group->count);
4205 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004206
Josef Bacik2552d172009-04-03 10:14:19 -04004207have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004208 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
4209 /*
4210 * we want to start caching kthreads, but not too many
4211 * right off the bat so we don't overwhelm the system,
4212 * so only start them if there are less than 2 and we're
4213 * in the initial allocation phase.
4214 */
4215 if (loop > LOOP_CACHING_NOWAIT ||
4216 atomic_read(&space_info->caching_threads) < 2) {
4217 ret = cache_block_group(block_group);
4218 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004219 }
Josef Bacikea6a4782008-11-20 12:16:16 -05004220 }
4221
Josef Bacik817d52f2009-07-13 21:29:25 -04004222 cached = block_group_cache_done(block_group);
4223 if (unlikely(!cached)) {
4224 found_uncached_bg = true;
4225
4226 /* if we only want cached bgs, loop */
4227 if (loop == LOOP_CACHED_ONLY)
4228 goto loop;
4229 }
4230
Josef Bacikea6a4782008-11-20 12:16:16 -05004231 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004232 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004233
Josef Bacik0a243252009-09-11 16:11:20 -04004234 /*
4235 * Ok we want to try and use the cluster allocator, so lets look
4236 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4237 * have tried the cluster allocator plenty of times at this
4238 * point and not have found anything, so we are likely way too
4239 * fragmented for the clustering stuff to find anything, so lets
4240 * just skip it and let the allocator find whatever block it can
4241 * find
4242 */
4243 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004244 /*
4245 * the refill lock keeps out other
4246 * people trying to start a new cluster
4247 */
4248 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004249 if (last_ptr->block_group &&
4250 (last_ptr->block_group->ro ||
4251 !block_group_bits(last_ptr->block_group, data))) {
4252 offset = 0;
4253 goto refill_cluster;
4254 }
4255
Chris Masonfa9c0d792009-04-03 09:47:43 -04004256 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4257 num_bytes, search_start);
4258 if (offset) {
4259 /* we have a block, we're done */
4260 spin_unlock(&last_ptr->refill_lock);
4261 goto checks;
4262 }
4263
4264 spin_lock(&last_ptr->lock);
4265 /*
4266 * whoops, this cluster doesn't actually point to
4267 * this block group. Get a ref on the block
4268 * group is does point to and try again
4269 */
4270 if (!last_ptr_loop && last_ptr->block_group &&
4271 last_ptr->block_group != block_group) {
4272
4273 btrfs_put_block_group(block_group);
4274 block_group = last_ptr->block_group;
4275 atomic_inc(&block_group->count);
4276 spin_unlock(&last_ptr->lock);
4277 spin_unlock(&last_ptr->refill_lock);
4278
4279 last_ptr_loop = 1;
4280 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04004281 /*
4282 * we know this block group is properly
4283 * in the list because
4284 * btrfs_remove_block_group, drops the
4285 * cluster before it removes the block
4286 * group from the list
4287 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04004288 goto have_block_group;
4289 }
4290 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004291refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04004292 /*
4293 * this cluster didn't work out, free it and
4294 * start over
4295 */
4296 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4297
4298 last_ptr_loop = 0;
4299
4300 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04004301 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04004302 block_group, last_ptr,
4303 offset, num_bytes,
4304 empty_cluster + empty_size);
4305 if (ret == 0) {
4306 /*
4307 * now pull our allocation out of this
4308 * cluster
4309 */
4310 offset = btrfs_alloc_from_cluster(block_group,
4311 last_ptr, num_bytes,
4312 search_start);
4313 if (offset) {
4314 /* we found one, proceed */
4315 spin_unlock(&last_ptr->refill_lock);
4316 goto checks;
4317 }
Josef Bacik0a243252009-09-11 16:11:20 -04004318 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4319 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004320 spin_unlock(&last_ptr->refill_lock);
4321
Josef Bacik0a243252009-09-11 16:11:20 -04004322 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004323 wait_block_group_cache_progress(block_group,
4324 num_bytes + empty_cluster + empty_size);
4325 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004326 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004327
Chris Masonfa9c0d792009-04-03 09:47:43 -04004328 /*
4329 * at this point we either didn't find a cluster
4330 * or we weren't able to allocate a block from our
4331 * cluster. Free the cluster we've been trying
4332 * to use, and go to the next block group
4333 */
Josef Bacik0a243252009-09-11 16:11:20 -04004334 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004335 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04004336 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004337 }
4338
Josef Bacik6226cb02009-04-03 10:14:18 -04004339 offset = btrfs_find_space_for_alloc(block_group, search_start,
4340 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004341 /*
4342 * If we didn't find a chunk, and we haven't failed on this
4343 * block group before, and this block group is in the middle of
4344 * caching and we are ok with waiting, then go ahead and wait
4345 * for progress to be made, and set failed_alloc to true.
4346 *
4347 * If failed_alloc is true then we've already waited on this
4348 * block group once and should move on to the next block group.
4349 */
4350 if (!offset && !failed_alloc && !cached &&
4351 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004352 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004353 num_bytes + empty_size);
4354 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004355 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004356 } else if (!offset) {
4357 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04004358 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004359checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04004360 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04004361 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04004362 if (search_start + num_bytes >= search_end) {
4363 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004364 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004365 }
Chris Masone37c9e62007-05-09 20:13:14 -04004366
Josef Bacik2552d172009-04-03 10:14:19 -04004367 /* move on to the next group */
4368 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04004369 block_group->key.objectid + block_group->key.offset) {
4370 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004371 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004372 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004373
Josef Bacik2552d172009-04-03 10:14:19 -04004374 if (exclude_nr > 0 &&
4375 (search_start + num_bytes > exclude_start &&
4376 search_start < exclude_start + exclude_nr)) {
4377 search_start = exclude_start + exclude_nr;
4378
Josef Bacik6226cb02009-04-03 10:14:18 -04004379 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004380 /*
4381 * if search_start is still in this block group
4382 * then we just re-search this block group
4383 */
4384 if (search_start >= block_group->key.objectid &&
4385 search_start < (block_group->key.objectid +
Josef Bacik6226cb02009-04-03 10:14:18 -04004386 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04004387 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04004388 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004389 }
Josef Bacik2552d172009-04-03 10:14:19 -04004390
4391 ins->objectid = search_start;
4392 ins->offset = num_bytes;
4393
Josef Bacik6226cb02009-04-03 10:14:18 -04004394 if (offset < search_start)
4395 btrfs_add_free_space(block_group, offset,
4396 search_start - offset);
4397 BUG_ON(offset > search_start);
4398
Yan Zheng11833d62009-09-11 16:11:19 -04004399 update_reserved_extents(block_group, num_bytes, 1);
4400
Josef Bacik2552d172009-04-03 10:14:19 -04004401 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04004402 break;
4403loop:
Josef Bacik0a243252009-09-11 16:11:20 -04004404 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004405 failed_alloc = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004406 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004407 }
4408 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004409
Josef Bacik817d52f2009-07-13 21:29:25 -04004410 /* LOOP_CACHED_ONLY, only search fully cached block groups
4411 * LOOP_CACHING_NOWAIT, search partially cached block groups, but
4412 * dont wait foR them to finish caching
4413 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4414 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4415 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4416 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04004417 */
Josef Bacik817d52f2009-07-13 21:29:25 -04004418 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4419 (found_uncached_bg || empty_size || empty_cluster ||
4420 allowed_chunk_alloc)) {
4421 if (found_uncached_bg) {
4422 found_uncached_bg = false;
4423 if (loop < LOOP_CACHING_WAIT) {
4424 loop++;
4425 goto search;
4426 }
4427 }
4428
4429 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004430 empty_size = 0;
4431 empty_cluster = 0;
4432 }
Chris Mason42e70e72008-11-07 18:17:11 -05004433
Josef Bacik2552d172009-04-03 10:14:19 -04004434 if (allowed_chunk_alloc) {
4435 ret = do_chunk_alloc(trans, root, num_bytes +
4436 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04004437 allowed_chunk_alloc = 0;
4438 } else {
4439 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004440 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004441
Josef Bacik817d52f2009-07-13 21:29:25 -04004442 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004443 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04004444 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004445 }
Josef Bacik2552d172009-04-03 10:14:19 -04004446 ret = -ENOSPC;
4447 } else if (!ins->objectid) {
4448 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04004449 }
Chris Mason0b86a832008-03-24 15:01:56 -04004450
Josef Bacik80eb2342008-10-29 14:49:05 -04004451 /* we found what we needed */
4452 if (ins->objectid) {
4453 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05004454 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04004455
Chris Masonfa9c0d792009-04-03 09:47:43 -04004456 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004457 ret = 0;
4458 }
Chris Mason0b86a832008-03-24 15:01:56 -04004459
Chris Mason0f70abe2007-02-28 16:46:22 -05004460 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004461}
Chris Masonec44a352008-04-28 15:29:52 -04004462
Josef Bacik9ed74f22009-09-11 16:12:44 -04004463static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4464 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04004465{
4466 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004467
Josef Bacik9ed74f22009-09-11 16:12:44 -04004468 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05004469 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4470 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04004471 info->bytes_pinned - info->bytes_reserved -
4472 info->bytes_super),
Chris Masond3977122009-01-05 21:25:51 -05004473 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05004474 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Josef Bacik9ed74f22009-09-11 16:12:44 -04004475 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4476 "\n",
Joel Becker21380932009-04-21 12:38:29 -07004477 (unsigned long long)info->total_bytes,
4478 (unsigned long long)info->bytes_pinned,
4479 (unsigned long long)info->bytes_delalloc,
4480 (unsigned long long)info->bytes_may_use,
Josef Bacik9ed74f22009-09-11 16:12:44 -04004481 (unsigned long long)info->bytes_used,
4482 (unsigned long long)info->bytes_root,
4483 (unsigned long long)info->bytes_super,
4484 (unsigned long long)info->bytes_reserved);
4485 spin_unlock(&info->lock);
4486
4487 if (!dump_block_groups)
4488 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004489
Josef Bacik80eb2342008-10-29 14:49:05 -04004490 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05004491 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004492 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05004493 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4494 "%llu pinned %llu reserved\n",
4495 (unsigned long long)cache->key.objectid,
4496 (unsigned long long)cache->key.offset,
4497 (unsigned long long)btrfs_block_group_used(&cache->item),
4498 (unsigned long long)cache->pinned,
4499 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004500 btrfs_dump_free_space(cache, bytes);
4501 spin_unlock(&cache->lock);
4502 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004503 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004504}
Zheng Yane8569812008-09-26 10:05:48 -04004505
Yan Zheng11833d62009-09-11 16:11:19 -04004506int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4507 struct btrfs_root *root,
4508 u64 num_bytes, u64 min_alloc_size,
4509 u64 empty_size, u64 hint_byte,
4510 u64 search_end, struct btrfs_key *ins,
4511 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004512{
4513 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04004514 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04004515 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04004516
Josef Bacik6a632092009-02-20 11:00:09 -05004517 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04004518again:
Chris Mason0ef3e662008-05-24 14:04:53 -04004519 /*
4520 * the only place that sets empty_size is btrfs_realloc_node, which
4521 * is not called recursively on allocations
4522 */
4523 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04004524 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04004525 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004526 2 * 1024 * 1024,
4527 BTRFS_BLOCK_GROUP_METADATA |
4528 (info->metadata_alloc_profile &
4529 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04004530 }
4531 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004532 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04004533 }
Chris Mason0b86a832008-03-24 15:01:56 -04004534
Chris Masondb945352007-10-15 16:15:53 -04004535 WARN_ON(num_bytes < root->sectorsize);
4536 ret = find_free_extent(trans, root, num_bytes, empty_size,
4537 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04004538 trans->alloc_exclude_start,
4539 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04004540
Chris Mason98d20f62008-04-14 09:46:10 -04004541 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4542 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004543 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004544 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04004545 do_chunk_alloc(trans, root->fs_info->extent_root,
4546 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004547 goto again;
4548 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004549 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004550 struct btrfs_space_info *sinfo;
4551
4552 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004553 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4554 "wanted %llu\n", (unsigned long long)data,
4555 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004556 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04004557 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004558
4559 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004560}
4561
Chris Mason65b51a02008-08-01 15:11:20 -04004562int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4563{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004564 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004565 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004566
Josef Bacik0f9dd462008-09-23 13:14:11 -04004567 cache = btrfs_lookup_block_group(root->fs_info, start);
4568 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004569 printk(KERN_ERR "Unable to find block group for %llu\n",
4570 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004571 return -ENOSPC;
4572 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004573
4574 ret = btrfs_discard_extent(root, start, len);
4575
Josef Bacik0f9dd462008-09-23 13:14:11 -04004576 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04004577 update_reserved_extents(cache, len, 0);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004578 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04004579
Chris Masone6dcd2d2008-07-17 12:53:50 -04004580 return ret;
4581}
4582
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004583static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4584 struct btrfs_root *root,
4585 u64 parent, u64 root_objectid,
4586 u64 flags, u64 owner, u64 offset,
4587 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004588{
4589 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004590 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004591 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004592 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004593 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004594 struct extent_buffer *leaf;
4595 int type;
4596 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004597
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004598 if (parent > 0)
4599 type = BTRFS_SHARED_DATA_REF_KEY;
4600 else
4601 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004602
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004603 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004604
4605 path = btrfs_alloc_path();
4606 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004607
Chris Masonb9473432009-03-13 11:00:37 -04004608 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004609 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4610 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004611 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004612
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004613 leaf = path->nodes[0];
4614 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004615 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004616 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4617 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4618 btrfs_set_extent_flags(leaf, extent_item,
4619 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004620
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004621 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4622 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4623 if (parent > 0) {
4624 struct btrfs_shared_data_ref *ref;
4625 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4626 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4627 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4628 } else {
4629 struct btrfs_extent_data_ref *ref;
4630 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4631 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4632 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4633 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4634 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4635 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004636
4637 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004638 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004639
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004640 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4641 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05004642 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004643 printk(KERN_ERR "btrfs update block group failed for %llu "
4644 "%llu\n", (unsigned long long)ins->objectid,
4645 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004646 BUG();
4647 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004648 return ret;
4649}
4650
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004651static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4652 struct btrfs_root *root,
4653 u64 parent, u64 root_objectid,
4654 u64 flags, struct btrfs_disk_key *key,
4655 int level, struct btrfs_key *ins)
4656{
4657 int ret;
4658 struct btrfs_fs_info *fs_info = root->fs_info;
4659 struct btrfs_extent_item *extent_item;
4660 struct btrfs_tree_block_info *block_info;
4661 struct btrfs_extent_inline_ref *iref;
4662 struct btrfs_path *path;
4663 struct extent_buffer *leaf;
4664 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4665
4666 path = btrfs_alloc_path();
4667 BUG_ON(!path);
4668
4669 path->leave_spinning = 1;
4670 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4671 ins, size);
4672 BUG_ON(ret);
4673
4674 leaf = path->nodes[0];
4675 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4676 struct btrfs_extent_item);
4677 btrfs_set_extent_refs(leaf, extent_item, 1);
4678 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4679 btrfs_set_extent_flags(leaf, extent_item,
4680 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4681 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4682
4683 btrfs_set_tree_block_key(leaf, block_info, key);
4684 btrfs_set_tree_block_level(leaf, block_info, level);
4685
4686 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4687 if (parent > 0) {
4688 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4689 btrfs_set_extent_inline_ref_type(leaf, iref,
4690 BTRFS_SHARED_BLOCK_REF_KEY);
4691 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4692 } else {
4693 btrfs_set_extent_inline_ref_type(leaf, iref,
4694 BTRFS_TREE_BLOCK_REF_KEY);
4695 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4696 }
4697
4698 btrfs_mark_buffer_dirty(leaf);
4699 btrfs_free_path(path);
4700
4701 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4702 1, 0);
4703 if (ret) {
4704 printk(KERN_ERR "btrfs update block group failed for %llu "
4705 "%llu\n", (unsigned long long)ins->objectid,
4706 (unsigned long long)ins->offset);
4707 BUG();
4708 }
4709 return ret;
4710}
4711
4712int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4713 struct btrfs_root *root,
4714 u64 root_objectid, u64 owner,
4715 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004716{
4717 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04004718
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004719 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004720
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004721 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4722 0, root_objectid, owner, offset,
4723 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004724 return ret;
4725}
Chris Masone02119d2008-09-05 16:13:11 -04004726
4727/*
4728 * this is used by the tree logging recovery code. It records that
4729 * an extent has been allocated and makes sure to clear the free
4730 * space cache bits as well
4731 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004732int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4733 struct btrfs_root *root,
4734 u64 root_objectid, u64 owner, u64 offset,
4735 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004736{
4737 int ret;
4738 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04004739 struct btrfs_caching_control *caching_ctl;
4740 u64 start = ins->objectid;
4741 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04004742
Chris Masone02119d2008-09-05 16:13:11 -04004743 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04004744 cache_block_group(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004745 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004746
Yan Zheng11833d62009-09-11 16:11:19 -04004747 if (!caching_ctl) {
4748 BUG_ON(!block_group_cache_done(block_group));
4749 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4750 BUG_ON(ret);
4751 } else {
4752 mutex_lock(&caching_ctl->mutex);
4753
4754 if (start >= caching_ctl->progress) {
4755 ret = add_excluded_extent(root, start, num_bytes);
4756 BUG_ON(ret);
4757 } else if (start + num_bytes <= caching_ctl->progress) {
4758 ret = btrfs_remove_free_space(block_group,
4759 start, num_bytes);
4760 BUG_ON(ret);
4761 } else {
4762 num_bytes = caching_ctl->progress - start;
4763 ret = btrfs_remove_free_space(block_group,
4764 start, num_bytes);
4765 BUG_ON(ret);
4766
4767 start = caching_ctl->progress;
4768 num_bytes = ins->objectid + ins->offset -
4769 caching_ctl->progress;
4770 ret = add_excluded_extent(root, start, num_bytes);
4771 BUG_ON(ret);
4772 }
4773
4774 mutex_unlock(&caching_ctl->mutex);
4775 put_caching_control(caching_ctl);
4776 }
4777
4778 update_reserved_extents(block_group, ins->offset, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004779 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004780 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4781 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004782 return ret;
4783}
4784
Chris Masone6dcd2d2008-07-17 12:53:50 -04004785/*
4786 * finds a free extent and does all the dirty work required for allocation
4787 * returns the key for the extent through ins, and a tree buffer for
4788 * the first block of the extent through buf.
4789 *
4790 * returns 0 if everything worked, non-zero otherwise.
4791 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004792static int alloc_tree_block(struct btrfs_trans_handle *trans,
4793 struct btrfs_root *root,
4794 u64 num_bytes, u64 parent, u64 root_objectid,
4795 struct btrfs_disk_key *key, int level,
4796 u64 empty_size, u64 hint_byte, u64 search_end,
4797 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004798{
4799 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004800 u64 flags = 0;
4801
Yan Zheng11833d62009-09-11 16:11:19 -04004802 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4803 empty_size, hint_byte, search_end,
4804 ins, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004805 if (ret)
4806 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004807
4808 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4809 if (parent == 0)
4810 parent = ins->objectid;
4811 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4812 } else
4813 BUG_ON(parent > 0);
4814
Chris Masond00aff02008-09-11 15:54:42 -04004815 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004816 struct btrfs_delayed_extent_op *extent_op;
4817 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4818 BUG_ON(!extent_op);
4819 if (key)
4820 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4821 else
4822 memset(&extent_op->key, 0, sizeof(extent_op->key));
4823 extent_op->flags_to_set = flags;
4824 extent_op->update_key = 1;
4825 extent_op->update_flags = 1;
4826 extent_op->is_data = 0;
4827
4828 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4829 ins->offset, parent, root_objectid,
4830 level, BTRFS_ADD_DELAYED_EXTENT,
4831 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004832 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004833 }
Chris Mason925baed2008-06-25 16:01:30 -04004834 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004835}
Chris Mason65b51a02008-08-01 15:11:20 -04004836
4837struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4838 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004839 u64 bytenr, u32 blocksize,
4840 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004841{
4842 struct extent_buffer *buf;
4843
4844 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4845 if (!buf)
4846 return ERR_PTR(-ENOMEM);
4847 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004848 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004849 btrfs_tree_lock(buf);
4850 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004851
4852 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004853 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004854
Chris Masond0c803c2008-09-11 16:17:57 -04004855 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4856 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04004857 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004858 } else {
4859 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4860 buf->start + buf->len - 1, GFP_NOFS);
4861 }
Chris Mason65b51a02008-08-01 15:11:20 -04004862 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004863 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004864 return buf;
4865}
4866
Chris Masonfec577f2007-02-26 10:40:21 -05004867/*
4868 * helper function to allocate a block for a given tree
4869 * returns the tree buffer or NULL.
4870 */
Chris Mason5f39d392007-10-15 16:14:19 -04004871struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004872 struct btrfs_root *root, u32 blocksize,
4873 u64 parent, u64 root_objectid,
4874 struct btrfs_disk_key *key, int level,
4875 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004876{
Chris Masone2fa7222007-03-12 16:22:34 -04004877 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004878 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004879 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004880
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004881 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4882 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004883 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004884 BUG_ON(ret > 0);
4885 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004886 }
Chris Mason55c69072008-01-09 15:55:33 -05004887
Chris Mason4008c042009-02-12 14:09:45 -05004888 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4889 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004890 return buf;
4891}
Chris Masona28ec192007-03-06 20:08:01 -05004892
Yan Zheng2c47e6052009-06-27 21:07:35 -04004893struct walk_control {
4894 u64 refs[BTRFS_MAX_LEVEL];
4895 u64 flags[BTRFS_MAX_LEVEL];
4896 struct btrfs_key update_progress;
4897 int stage;
4898 int level;
4899 int shared_level;
4900 int update_ref;
4901 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004902 int reada_slot;
4903 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004904};
4905
4906#define DROP_REFERENCE 1
4907#define UPDATE_BACKREF 2
4908
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004909static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4910 struct btrfs_root *root,
4911 struct walk_control *wc,
4912 struct btrfs_path *path)
4913{
4914 u64 bytenr;
4915 u64 generation;
4916 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004917 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004918 u64 last = 0;
4919 u32 nritems;
4920 u32 blocksize;
4921 struct btrfs_key key;
4922 struct extent_buffer *eb;
4923 int ret;
4924 int slot;
4925 int nread = 0;
4926
4927 if (path->slots[wc->level] < wc->reada_slot) {
4928 wc->reada_count = wc->reada_count * 2 / 3;
4929 wc->reada_count = max(wc->reada_count, 2);
4930 } else {
4931 wc->reada_count = wc->reada_count * 3 / 2;
4932 wc->reada_count = min_t(int, wc->reada_count,
4933 BTRFS_NODEPTRS_PER_BLOCK(root));
4934 }
4935
4936 eb = path->nodes[wc->level];
4937 nritems = btrfs_header_nritems(eb);
4938 blocksize = btrfs_level_size(root, wc->level - 1);
4939
4940 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4941 if (nread >= wc->reada_count)
4942 break;
4943
4944 cond_resched();
4945 bytenr = btrfs_node_blockptr(eb, slot);
4946 generation = btrfs_node_ptr_generation(eb, slot);
4947
4948 if (slot == path->slots[wc->level])
4949 goto reada;
4950
4951 if (wc->stage == UPDATE_BACKREF &&
4952 generation <= root->root_key.offset)
4953 continue;
4954
Yan, Zheng94fcca92009-10-09 09:25:16 -04004955 /* We don't lock the tree block, it's OK to be racy here */
4956 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
4957 &refs, &flags);
4958 BUG_ON(ret);
4959 BUG_ON(refs == 0);
4960
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004961 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004962 if (refs == 1)
4963 goto reada;
4964
Yan, Zheng94fcca92009-10-09 09:25:16 -04004965 if (wc->level == 1 &&
4966 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4967 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004968 if (!wc->update_ref ||
4969 generation <= root->root_key.offset)
4970 continue;
4971 btrfs_node_key_to_cpu(eb, &key, slot);
4972 ret = btrfs_comp_cpu_keys(&key,
4973 &wc->update_progress);
4974 if (ret < 0)
4975 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004976 } else {
4977 if (wc->level == 1 &&
4978 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4979 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004980 }
4981reada:
4982 ret = readahead_tree_block(root, bytenr, blocksize,
4983 generation);
4984 if (ret)
4985 break;
4986 last = bytenr + blocksize;
4987 nread++;
4988 }
4989 wc->reada_slot = slot;
4990}
4991
Chris Mason9aca1d52007-03-13 11:09:37 -04004992/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04004993 * hepler to process tree block while walking down the tree.
4994 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04004995 * when wc->stage == UPDATE_BACKREF, this function updates
4996 * back refs for pointers in the block.
4997 *
4998 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004999 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005000static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5001 struct btrfs_root *root,
5002 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005003 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005004{
5005 int level = wc->level;
5006 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005007 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5008 int ret;
5009
5010 if (wc->stage == UPDATE_BACKREF &&
5011 btrfs_header_owner(eb) != root->root_key.objectid)
5012 return 1;
5013
5014 /*
5015 * when reference count of tree block is 1, it won't increase
5016 * again. once full backref flag is set, we never clear it.
5017 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005018 if (lookup_info &&
5019 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5020 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005021 BUG_ON(!path->locks[level]);
5022 ret = btrfs_lookup_extent_info(trans, root,
5023 eb->start, eb->len,
5024 &wc->refs[level],
5025 &wc->flags[level]);
5026 BUG_ON(ret);
5027 BUG_ON(wc->refs[level] == 0);
5028 }
5029
Yan Zheng2c47e6052009-06-27 21:07:35 -04005030 if (wc->stage == DROP_REFERENCE) {
5031 if (wc->refs[level] > 1)
5032 return 1;
5033
5034 if (path->locks[level] && !wc->keep_locks) {
5035 btrfs_tree_unlock(eb);
5036 path->locks[level] = 0;
5037 }
5038 return 0;
5039 }
5040
5041 /* wc->stage == UPDATE_BACKREF */
5042 if (!(wc->flags[level] & flag)) {
5043 BUG_ON(!path->locks[level]);
5044 ret = btrfs_inc_ref(trans, root, eb, 1);
5045 BUG_ON(ret);
5046 ret = btrfs_dec_ref(trans, root, eb, 0);
5047 BUG_ON(ret);
5048 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5049 eb->len, flag, 0);
5050 BUG_ON(ret);
5051 wc->flags[level] |= flag;
5052 }
5053
5054 /*
5055 * the block is shared by multiple trees, so it's not good to
5056 * keep the tree lock
5057 */
5058 if (path->locks[level] && level > 0) {
5059 btrfs_tree_unlock(eb);
5060 path->locks[level] = 0;
5061 }
5062 return 0;
5063}
5064
5065/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005066 * hepler to process tree block pointer.
5067 *
5068 * when wc->stage == DROP_REFERENCE, this function checks
5069 * reference count of the block pointed to. if the block
5070 * is shared and we need update back refs for the subtree
5071 * rooted at the block, this function changes wc->stage to
5072 * UPDATE_BACKREF. if the block is shared and there is no
5073 * need to update back, this function drops the reference
5074 * to the block.
5075 *
5076 * NOTE: return value 1 means we should stop walking down.
5077 */
5078static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5079 struct btrfs_root *root,
5080 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005081 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005082{
5083 u64 bytenr;
5084 u64 generation;
5085 u64 parent;
5086 u32 blocksize;
5087 struct btrfs_key key;
5088 struct extent_buffer *next;
5089 int level = wc->level;
5090 int reada = 0;
5091 int ret = 0;
5092
5093 generation = btrfs_node_ptr_generation(path->nodes[level],
5094 path->slots[level]);
5095 /*
5096 * if the lower level block was created before the snapshot
5097 * was created, we know there is no need to update back refs
5098 * for the subtree
5099 */
5100 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005101 generation <= root->root_key.offset) {
5102 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005103 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005104 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005105
5106 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5107 blocksize = btrfs_level_size(root, level - 1);
5108
5109 next = btrfs_find_tree_block(root, bytenr, blocksize);
5110 if (!next) {
5111 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
5112 reada = 1;
5113 }
5114 btrfs_tree_lock(next);
5115 btrfs_set_lock_blocking(next);
5116
Yan, Zheng94fcca92009-10-09 09:25:16 -04005117 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5118 &wc->refs[level - 1],
5119 &wc->flags[level - 1]);
5120 BUG_ON(ret);
5121 BUG_ON(wc->refs[level - 1] == 0);
5122 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005123
Yan, Zheng94fcca92009-10-09 09:25:16 -04005124 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005125 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005126 if (level == 1 &&
5127 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5128 goto skip;
5129
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005130 if (!wc->update_ref ||
5131 generation <= root->root_key.offset)
5132 goto skip;
5133
5134 btrfs_node_key_to_cpu(path->nodes[level], &key,
5135 path->slots[level]);
5136 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5137 if (ret < 0)
5138 goto skip;
5139
5140 wc->stage = UPDATE_BACKREF;
5141 wc->shared_level = level - 1;
5142 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005143 } else {
5144 if (level == 1 &&
5145 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5146 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005147 }
5148
5149 if (!btrfs_buffer_uptodate(next, generation)) {
5150 btrfs_tree_unlock(next);
5151 free_extent_buffer(next);
5152 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005153 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005154 }
5155
5156 if (!next) {
5157 if (reada && level == 1)
5158 reada_walk_down(trans, root, wc, path);
5159 next = read_tree_block(root, bytenr, blocksize, generation);
5160 btrfs_tree_lock(next);
5161 btrfs_set_lock_blocking(next);
5162 }
5163
5164 level--;
5165 BUG_ON(level != btrfs_header_level(next));
5166 path->nodes[level] = next;
5167 path->slots[level] = 0;
5168 path->locks[level] = 1;
5169 wc->level = level;
5170 if (wc->level == 1)
5171 wc->reada_slot = 0;
5172 return 0;
5173skip:
5174 wc->refs[level - 1] = 0;
5175 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005176 if (wc->stage == DROP_REFERENCE) {
5177 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5178 parent = path->nodes[level]->start;
5179 } else {
5180 BUG_ON(root->root_key.objectid !=
5181 btrfs_header_owner(path->nodes[level]));
5182 parent = 0;
5183 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005184
Yan, Zheng94fcca92009-10-09 09:25:16 -04005185 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5186 root->root_key.objectid, level - 1, 0);
5187 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005188 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005189 btrfs_tree_unlock(next);
5190 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04005191 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005192 return 1;
5193}
5194
5195/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005196 * hepler to process tree block while walking up the tree.
5197 *
5198 * when wc->stage == DROP_REFERENCE, this function drops
5199 * reference count on the block.
5200 *
5201 * when wc->stage == UPDATE_BACKREF, this function changes
5202 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5203 * to UPDATE_BACKREF previously while processing the block.
5204 *
5205 * NOTE: return value 1 means we should stop walking up.
5206 */
5207static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5208 struct btrfs_root *root,
5209 struct btrfs_path *path,
5210 struct walk_control *wc)
5211{
5212 int ret = 0;
5213 int level = wc->level;
5214 struct extent_buffer *eb = path->nodes[level];
5215 u64 parent = 0;
5216
5217 if (wc->stage == UPDATE_BACKREF) {
5218 BUG_ON(wc->shared_level < level);
5219 if (level < wc->shared_level)
5220 goto out;
5221
Yan Zheng2c47e6052009-06-27 21:07:35 -04005222 ret = find_next_key(path, level + 1, &wc->update_progress);
5223 if (ret > 0)
5224 wc->update_ref = 0;
5225
5226 wc->stage = DROP_REFERENCE;
5227 wc->shared_level = -1;
5228 path->slots[level] = 0;
5229
5230 /*
5231 * check reference count again if the block isn't locked.
5232 * we should start walking down the tree again if reference
5233 * count is one.
5234 */
5235 if (!path->locks[level]) {
5236 BUG_ON(level == 0);
5237 btrfs_tree_lock(eb);
5238 btrfs_set_lock_blocking(eb);
5239 path->locks[level] = 1;
5240
5241 ret = btrfs_lookup_extent_info(trans, root,
5242 eb->start, eb->len,
5243 &wc->refs[level],
5244 &wc->flags[level]);
5245 BUG_ON(ret);
5246 BUG_ON(wc->refs[level] == 0);
5247 if (wc->refs[level] == 1) {
5248 btrfs_tree_unlock(eb);
5249 path->locks[level] = 0;
5250 return 1;
5251 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005252 }
5253 }
5254
5255 /* wc->stage == DROP_REFERENCE */
5256 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5257
5258 if (wc->refs[level] == 1) {
5259 if (level == 0) {
5260 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5261 ret = btrfs_dec_ref(trans, root, eb, 1);
5262 else
5263 ret = btrfs_dec_ref(trans, root, eb, 0);
5264 BUG_ON(ret);
5265 }
5266 /* make block locked assertion in clean_tree_block happy */
5267 if (!path->locks[level] &&
5268 btrfs_header_generation(eb) == trans->transid) {
5269 btrfs_tree_lock(eb);
5270 btrfs_set_lock_blocking(eb);
5271 path->locks[level] = 1;
5272 }
5273 clean_tree_block(trans, root, eb);
5274 }
5275
5276 if (eb == root->node) {
5277 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5278 parent = eb->start;
5279 else
5280 BUG_ON(root->root_key.objectid !=
5281 btrfs_header_owner(eb));
5282 } else {
5283 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5284 parent = path->nodes[level + 1]->start;
5285 else
5286 BUG_ON(root->root_key.objectid !=
5287 btrfs_header_owner(path->nodes[level + 1]));
5288 }
5289
5290 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5291 root->root_key.objectid, level, 0);
5292 BUG_ON(ret);
5293out:
5294 wc->refs[level] = 0;
5295 wc->flags[level] = 0;
5296 return ret;
5297}
5298
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005299static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5300 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005301 struct btrfs_path *path,
5302 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005303{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005304 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005305 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005306 int ret;
5307
Yan Zheng2c47e6052009-06-27 21:07:35 -04005308 while (level >= 0) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005309 if (path->slots[level] >=
5310 btrfs_header_nritems(path->nodes[level]))
5311 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005312
Yan, Zheng94fcca92009-10-09 09:25:16 -04005313 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005314 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005315 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005316
Yan Zheng2c47e6052009-06-27 21:07:35 -04005317 if (level == 0)
5318 break;
5319
Yan, Zheng94fcca92009-10-09 09:25:16 -04005320 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005321 if (ret > 0) {
5322 path->slots[level]++;
5323 continue;
5324 }
5325 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005326 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005327 return 0;
5328}
5329
Chris Masond3977122009-01-05 21:25:51 -05005330static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005331 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005332 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005333 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005334{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005335 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005336 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005337
Yan Zheng2c47e6052009-06-27 21:07:35 -04005338 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5339 while (level < max_level && path->nodes[level]) {
5340 wc->level = level;
5341 if (path->slots[level] + 1 <
5342 btrfs_header_nritems(path->nodes[level])) {
5343 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005344 return 0;
5345 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005346 ret = walk_up_proc(trans, root, path, wc);
5347 if (ret > 0)
5348 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05005349
Yan Zheng2c47e6052009-06-27 21:07:35 -04005350 if (path->locks[level]) {
5351 btrfs_tree_unlock(path->nodes[level]);
5352 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005353 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005354 free_extent_buffer(path->nodes[level]);
5355 path->nodes[level] = NULL;
5356 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005357 }
5358 }
5359 return 1;
5360}
5361
Chris Mason9aca1d52007-03-13 11:09:37 -04005362/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005363 * drop a subvolume tree.
5364 *
5365 * this function traverses the tree freeing any blocks that only
5366 * referenced by the tree.
5367 *
5368 * when a shared tree block is found. this function decreases its
5369 * reference count by one. if update_ref is true, this function
5370 * also make sure backrefs for the shared block and all lower level
5371 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04005372 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005373int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05005374{
Chris Mason5caf2a02007-04-02 11:20:42 -04005375 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005376 struct btrfs_trans_handle *trans;
5377 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005378 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005379 struct walk_control *wc;
5380 struct btrfs_key key;
5381 int err = 0;
5382 int ret;
5383 int level;
Chris Mason20524f02007-03-10 06:35:47 -05005384
Chris Mason5caf2a02007-04-02 11:20:42 -04005385 path = btrfs_alloc_path();
5386 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05005387
Yan Zheng2c47e6052009-06-27 21:07:35 -04005388 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5389 BUG_ON(!wc);
5390
5391 trans = btrfs_start_transaction(tree_root, 1);
5392
Chris Mason9f3a7422007-08-07 15:52:19 -04005393 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005394 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005395 path->nodes[level] = btrfs_lock_root_node(root);
5396 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005397 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005398 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005399 memset(&wc->update_progress, 0,
5400 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005401 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005402 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005403 memcpy(&wc->update_progress, &key,
5404 sizeof(wc->update_progress));
5405
Chris Mason6702ed42007-08-07 16:15:09 -04005406 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005407 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005408 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005409 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5410 path->lowest_level = 0;
5411 if (ret < 0) {
5412 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005413 goto out;
5414 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005415 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005416
Chris Mason7d9eb122008-07-08 14:19:17 -04005417 /*
5418 * unlock our path, this is safe because only this
5419 * function is allowed to delete this snapshot
5420 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005421 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005422
Yan Zheng2c47e6052009-06-27 21:07:35 -04005423 level = btrfs_header_level(root->node);
5424 while (1) {
5425 btrfs_tree_lock(path->nodes[level]);
5426 btrfs_set_lock_blocking(path->nodes[level]);
5427
5428 ret = btrfs_lookup_extent_info(trans, root,
5429 path->nodes[level]->start,
5430 path->nodes[level]->len,
5431 &wc->refs[level],
5432 &wc->flags[level]);
5433 BUG_ON(ret);
5434 BUG_ON(wc->refs[level] == 0);
5435
5436 if (level == root_item->drop_level)
5437 break;
5438
5439 btrfs_tree_unlock(path->nodes[level]);
5440 WARN_ON(wc->refs[level] != 1);
5441 level--;
5442 }
5443 }
5444
5445 wc->level = level;
5446 wc->shared_level = -1;
5447 wc->stage = DROP_REFERENCE;
5448 wc->update_ref = update_ref;
5449 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005450 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005451
5452 while (1) {
5453 ret = walk_down_tree(trans, root, path, wc);
5454 if (ret < 0) {
5455 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005456 break;
5457 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005458
5459 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5460 if (ret < 0) {
5461 err = ret;
5462 break;
5463 }
5464
5465 if (ret > 0) {
5466 BUG_ON(wc->stage != DROP_REFERENCE);
5467 break;
5468 }
5469
5470 if (wc->stage == DROP_REFERENCE) {
5471 level = wc->level;
5472 btrfs_node_key(path->nodes[level],
5473 &root_item->drop_progress,
5474 path->slots[level]);
5475 root_item->drop_level = level;
5476 }
5477
5478 BUG_ON(wc->level == 0);
5479 if (trans->transaction->in_commit ||
5480 trans->transaction->delayed_refs.flushing) {
5481 ret = btrfs_update_root(trans, tree_root,
5482 &root->root_key,
5483 root_item);
5484 BUG_ON(ret);
5485
5486 btrfs_end_transaction(trans, tree_root);
5487 trans = btrfs_start_transaction(tree_root, 1);
5488 } else {
5489 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005490 update = trans->delayed_ref_updates;
5491 trans->delayed_ref_updates = 0;
5492 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005493 btrfs_run_delayed_refs(trans, tree_root,
5494 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005495 }
Chris Mason20524f02007-03-10 06:35:47 -05005496 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005497 btrfs_release_path(root, path);
5498 BUG_ON(err);
5499
5500 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5501 BUG_ON(ret);
5502
Yan, Zheng76dda932009-09-21 16:00:26 -04005503 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5504 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5505 NULL, NULL);
5506 BUG_ON(ret < 0);
5507 if (ret > 0) {
5508 ret = btrfs_del_orphan_item(trans, tree_root,
5509 root->root_key.objectid);
5510 BUG_ON(ret);
5511 }
5512 }
5513
5514 if (root->in_radix) {
5515 btrfs_free_fs_root(tree_root->fs_info, root);
5516 } else {
5517 free_extent_buffer(root->node);
5518 free_extent_buffer(root->commit_root);
5519 kfree(root);
5520 }
Chris Mason9f3a7422007-08-07 15:52:19 -04005521out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005522 btrfs_end_transaction(trans, tree_root);
5523 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005524 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005525 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005526}
Chris Mason9078a3e2007-04-26 16:46:15 -04005527
Yan Zheng2c47e6052009-06-27 21:07:35 -04005528/*
5529 * drop subtree rooted at tree block 'node'.
5530 *
5531 * NOTE: this function will unlock and release tree block 'node'
5532 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005533int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5534 struct btrfs_root *root,
5535 struct extent_buffer *node,
5536 struct extent_buffer *parent)
5537{
5538 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005539 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005540 int level;
5541 int parent_level;
5542 int ret = 0;
5543 int wret;
5544
Yan Zheng2c47e6052009-06-27 21:07:35 -04005545 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5546
Yan Zhengf82d02d2008-10-29 14:49:05 -04005547 path = btrfs_alloc_path();
5548 BUG_ON(!path);
5549
Yan Zheng2c47e6052009-06-27 21:07:35 -04005550 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5551 BUG_ON(!wc);
5552
Chris Masonb9447ef2009-03-09 11:45:38 -04005553 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005554 parent_level = btrfs_header_level(parent);
5555 extent_buffer_get(parent);
5556 path->nodes[parent_level] = parent;
5557 path->slots[parent_level] = btrfs_header_nritems(parent);
5558
Chris Masonb9447ef2009-03-09 11:45:38 -04005559 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005560 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005561 path->nodes[level] = node;
5562 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005563 path->locks[level] = 1;
5564
5565 wc->refs[parent_level] = 1;
5566 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5567 wc->level = level;
5568 wc->shared_level = -1;
5569 wc->stage = DROP_REFERENCE;
5570 wc->update_ref = 0;
5571 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005572 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005573
5574 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005575 wret = walk_down_tree(trans, root, path, wc);
5576 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005577 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005578 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005579 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005580
Yan Zheng2c47e6052009-06-27 21:07:35 -04005581 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005582 if (wret < 0)
5583 ret = wret;
5584 if (wret != 0)
5585 break;
5586 }
5587
Yan Zheng2c47e6052009-06-27 21:07:35 -04005588 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005589 btrfs_free_path(path);
5590 return ret;
5591}
5592
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005593#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005594static unsigned long calc_ra(unsigned long start, unsigned long last,
5595 unsigned long nr)
5596{
5597 return min(last, start + nr - 1);
5598}
5599
Chris Masond3977122009-01-05 21:25:51 -05005600static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005601 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005602{
5603 u64 page_start;
5604 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005605 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005606 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005607 unsigned long i;
5608 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005609 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005610 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005611 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005612 unsigned int total_read = 0;
5613 unsigned int total_dirty = 0;
5614 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005615
5616 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005617
5618 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005619 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005620 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5621
Zheng Yan1a40e232008-09-26 10:09:34 -04005622 /* make sure the dirty trick played by the caller work */
5623 ret = invalidate_inode_pages2_range(inode->i_mapping,
5624 first_index, last_index);
5625 if (ret)
5626 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005627
Chris Mason4313b392008-01-03 09:08:48 -05005628 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005629
Zheng Yan1a40e232008-09-26 10:09:34 -04005630 for (i = first_index ; i <= last_index; i++) {
5631 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005632 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005633 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005634 }
5635 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005636again:
5637 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005638 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005639 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005640 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005641 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005642 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005643 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005644 if (!PageUptodate(page)) {
5645 btrfs_readpage(NULL, page);
5646 lock_page(page);
5647 if (!PageUptodate(page)) {
5648 unlock_page(page);
5649 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005650 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005651 goto out_unlock;
5652 }
5653 }
Chris Masonec44a352008-04-28 15:29:52 -04005654 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005655
Chris Masonedbd8d42007-12-21 16:27:24 -05005656 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5657 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005658 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005659
Chris Mason3eaa2882008-07-24 11:57:52 -04005660 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5661 if (ordered) {
5662 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5663 unlock_page(page);
5664 page_cache_release(page);
5665 btrfs_start_ordered_extent(inode, ordered, 1);
5666 btrfs_put_ordered_extent(ordered);
5667 goto again;
5668 }
5669 set_page_extent_mapped(page);
5670
Zheng Yan1a40e232008-09-26 10:09:34 -04005671 if (i == first_index)
5672 set_extent_bits(io_tree, page_start, page_end,
5673 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005674 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005675
Chris Masona061fc82008-05-07 11:43:44 -04005676 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005677 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005678
Chris Masond1310b22008-01-24 16:13:08 -05005679 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005680 unlock_page(page);
5681 page_cache_release(page);
5682 }
5683
5684out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005685 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005686 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005687 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005688 return ret;
5689}
5690
Chris Masond3977122009-01-05 21:25:51 -05005691static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005692 struct btrfs_key *extent_key,
5693 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005694{
Zheng Yan1a40e232008-09-26 10:09:34 -04005695 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5696 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5697 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005698 u64 start = extent_key->objectid - offset;
5699 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005700
5701 em = alloc_extent_map(GFP_NOFS);
5702 BUG_ON(!em || IS_ERR(em));
5703
Yan Zheng66435582008-10-30 14:19:50 -04005704 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005705 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005706 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005707 em->block_start = extent_key->objectid;
5708 em->bdev = root->fs_info->fs_devices->latest_bdev;
5709 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5710
5711 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005712 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005713 while (1) {
5714 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04005715 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005716 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005717 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005718 if (ret != -EEXIST) {
5719 free_extent_map(em);
5720 break;
5721 }
Yan Zheng66435582008-10-30 14:19:50 -04005722 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005723 }
Yan Zheng66435582008-10-30 14:19:50 -04005724 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005725
Yan Zheng66435582008-10-30 14:19:50 -04005726 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005727}
5728
5729struct btrfs_ref_path {
5730 u64 extent_start;
5731 u64 nodes[BTRFS_MAX_LEVEL];
5732 u64 root_objectid;
5733 u64 root_generation;
5734 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005735 u32 num_refs;
5736 int lowest_level;
5737 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005738 int shared_level;
5739
5740 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5741 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005742};
5743
5744struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005745 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005746 u64 disk_bytenr;
5747 u64 disk_num_bytes;
5748 u64 offset;
5749 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005750 u8 compression;
5751 u8 encryption;
5752 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005753};
5754
5755static int is_cowonly_root(u64 root_objectid)
5756{
5757 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5758 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5759 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5760 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005761 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5762 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005763 return 1;
5764 return 0;
5765}
5766
Chris Masond3977122009-01-05 21:25:51 -05005767static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005768 struct btrfs_root *extent_root,
5769 struct btrfs_ref_path *ref_path,
5770 int first_time)
5771{
5772 struct extent_buffer *leaf;
5773 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005774 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005775 struct btrfs_key key;
5776 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005777 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005778 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005779 int level;
5780 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005781
Zheng Yan1a40e232008-09-26 10:09:34 -04005782 path = btrfs_alloc_path();
5783 if (!path)
5784 return -ENOMEM;
5785
Zheng Yan1a40e232008-09-26 10:09:34 -04005786 if (first_time) {
5787 ref_path->lowest_level = -1;
5788 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005789 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005790 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005791 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005792walk_down:
5793 level = ref_path->current_level - 1;
5794 while (level >= -1) {
5795 u64 parent;
5796 if (level < ref_path->lowest_level)
5797 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005798
Chris Masond3977122009-01-05 21:25:51 -05005799 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005800 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005801 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005802 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005803 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005804
Zheng Yan1a40e232008-09-26 10:09:34 -04005805 parent = ref_path->nodes[level + 1];
5806 ref_path->nodes[level + 1] = 0;
5807 ref_path->current_level = level;
5808 BUG_ON(parent == 0);
5809
5810 key.objectid = bytenr;
5811 key.offset = parent + 1;
5812 key.type = BTRFS_EXTENT_REF_KEY;
5813
5814 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005815 if (ret < 0)
5816 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005817 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005818
Chris Masonedbd8d42007-12-21 16:27:24 -05005819 leaf = path->nodes[0];
5820 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005821 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005822 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005823 if (ret < 0)
5824 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005825 if (ret > 0)
5826 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005827 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005828 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005829
5830 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005831 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005832 found_key.type == BTRFS_EXTENT_REF_KEY) {
5833 if (level < ref_path->shared_level)
5834 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005835 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005836 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005837next:
5838 level--;
5839 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005840 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005841 }
5842 /* reached lowest level */
5843 ret = 1;
5844 goto out;
5845walk_up:
5846 level = ref_path->current_level;
5847 while (level < BTRFS_MAX_LEVEL - 1) {
5848 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005849
5850 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005851 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005852 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005853 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005854
Zheng Yan1a40e232008-09-26 10:09:34 -04005855 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005856
Zheng Yan1a40e232008-09-26 10:09:34 -04005857 key.objectid = bytenr;
5858 key.offset = 0;
5859 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005860
Zheng Yan1a40e232008-09-26 10:09:34 -04005861 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5862 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005863 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005864
5865 leaf = path->nodes[0];
5866 nritems = btrfs_header_nritems(leaf);
5867 if (path->slots[0] >= nritems) {
5868 ret = btrfs_next_leaf(extent_root, path);
5869 if (ret < 0)
5870 goto out;
5871 if (ret > 0) {
5872 /* the extent was freed by someone */
5873 if (ref_path->lowest_level == level)
5874 goto out;
5875 btrfs_release_path(extent_root, path);
5876 goto walk_down;
5877 }
5878 leaf = path->nodes[0];
5879 }
5880
5881 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5882 if (found_key.objectid != bytenr ||
5883 found_key.type != BTRFS_EXTENT_REF_KEY) {
5884 /* the extent was freed by someone */
5885 if (ref_path->lowest_level == level) {
5886 ret = 1;
5887 goto out;
5888 }
5889 btrfs_release_path(extent_root, path);
5890 goto walk_down;
5891 }
5892found:
5893 ref = btrfs_item_ptr(leaf, path->slots[0],
5894 struct btrfs_extent_ref);
5895 ref_objectid = btrfs_ref_objectid(leaf, ref);
5896 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5897 if (first_time) {
5898 level = (int)ref_objectid;
5899 BUG_ON(level >= BTRFS_MAX_LEVEL);
5900 ref_path->lowest_level = level;
5901 ref_path->current_level = level;
5902 ref_path->nodes[level] = bytenr;
5903 } else {
5904 WARN_ON(ref_objectid != level);
5905 }
5906 } else {
5907 WARN_ON(level != -1);
5908 }
5909 first_time = 0;
5910
5911 if (ref_path->lowest_level == level) {
5912 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005913 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5914 }
5915
5916 /*
5917 * the block is tree root or the block isn't in reference
5918 * counted tree.
5919 */
5920 if (found_key.objectid == found_key.offset ||
5921 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5922 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5923 ref_path->root_generation =
5924 btrfs_ref_generation(leaf, ref);
5925 if (level < 0) {
5926 /* special reference from the tree log */
5927 ref_path->nodes[0] = found_key.offset;
5928 ref_path->current_level = 0;
5929 }
5930 ret = 0;
5931 goto out;
5932 }
5933
5934 level++;
5935 BUG_ON(ref_path->nodes[level] != 0);
5936 ref_path->nodes[level] = found_key.offset;
5937 ref_path->current_level = level;
5938
5939 /*
5940 * the reference was created in the running transaction,
5941 * no need to continue walking up.
5942 */
5943 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5944 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5945 ref_path->root_generation =
5946 btrfs_ref_generation(leaf, ref);
5947 ret = 0;
5948 goto out;
5949 }
5950
5951 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005952 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005953 }
5954 /* reached max tree level, but no tree root found. */
5955 BUG();
5956out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005957 btrfs_free_path(path);
5958 return ret;
5959}
5960
5961static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5962 struct btrfs_root *extent_root,
5963 struct btrfs_ref_path *ref_path,
5964 u64 extent_start)
5965{
5966 memset(ref_path, 0, sizeof(*ref_path));
5967 ref_path->extent_start = extent_start;
5968
5969 return __next_ref_path(trans, extent_root, ref_path, 1);
5970}
5971
5972static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5973 struct btrfs_root *extent_root,
5974 struct btrfs_ref_path *ref_path)
5975{
5976 return __next_ref_path(trans, extent_root, ref_path, 0);
5977}
5978
Chris Masond3977122009-01-05 21:25:51 -05005979static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005980 struct btrfs_key *extent_key,
5981 u64 offset, int no_fragment,
5982 struct disk_extent **extents,
5983 int *nr_extents)
5984{
5985 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5986 struct btrfs_path *path;
5987 struct btrfs_file_extent_item *fi;
5988 struct extent_buffer *leaf;
5989 struct disk_extent *exts = *extents;
5990 struct btrfs_key found_key;
5991 u64 cur_pos;
5992 u64 last_byte;
5993 u32 nritems;
5994 int nr = 0;
5995 int max = *nr_extents;
5996 int ret;
5997
5998 WARN_ON(!no_fragment && *extents);
5999 if (!exts) {
6000 max = 1;
6001 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6002 if (!exts)
6003 return -ENOMEM;
6004 }
6005
6006 path = btrfs_alloc_path();
6007 BUG_ON(!path);
6008
6009 cur_pos = extent_key->objectid - offset;
6010 last_byte = extent_key->objectid + extent_key->offset;
6011 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6012 cur_pos, 0);
6013 if (ret < 0)
6014 goto out;
6015 if (ret > 0) {
6016 ret = -ENOENT;
6017 goto out;
6018 }
6019
6020 while (1) {
6021 leaf = path->nodes[0];
6022 nritems = btrfs_header_nritems(leaf);
6023 if (path->slots[0] >= nritems) {
6024 ret = btrfs_next_leaf(root, path);
6025 if (ret < 0)
6026 goto out;
6027 if (ret > 0)
6028 break;
6029 leaf = path->nodes[0];
6030 }
6031
6032 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6033 if (found_key.offset != cur_pos ||
6034 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6035 found_key.objectid != reloc_inode->i_ino)
6036 break;
6037
6038 fi = btrfs_item_ptr(leaf, path->slots[0],
6039 struct btrfs_file_extent_item);
6040 if (btrfs_file_extent_type(leaf, fi) !=
6041 BTRFS_FILE_EXTENT_REG ||
6042 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6043 break;
6044
6045 if (nr == max) {
6046 struct disk_extent *old = exts;
6047 max *= 2;
6048 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6049 memcpy(exts, old, sizeof(*exts) * nr);
6050 if (old != *extents)
6051 kfree(old);
6052 }
6053
6054 exts[nr].disk_bytenr =
6055 btrfs_file_extent_disk_bytenr(leaf, fi);
6056 exts[nr].disk_num_bytes =
6057 btrfs_file_extent_disk_num_bytes(leaf, fi);
6058 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6059 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04006060 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6061 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6062 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6063 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6064 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04006065 BUG_ON(exts[nr].offset > 0);
6066 BUG_ON(exts[nr].compression || exts[nr].encryption);
6067 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006068
6069 cur_pos += exts[nr].num_bytes;
6070 nr++;
6071
6072 if (cur_pos + offset >= last_byte)
6073 break;
6074
6075 if (no_fragment) {
6076 ret = 1;
6077 goto out;
6078 }
6079 path->slots[0]++;
6080 }
6081
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006082 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04006083 if (cur_pos + offset < last_byte) {
6084 ret = -ENOENT;
6085 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05006086 }
6087 ret = 0;
6088out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006089 btrfs_free_path(path);
6090 if (ret) {
6091 if (exts != *extents)
6092 kfree(exts);
6093 } else {
6094 *extents = exts;
6095 *nr_extents = nr;
6096 }
6097 return ret;
6098}
6099
Chris Masond3977122009-01-05 21:25:51 -05006100static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006101 struct btrfs_root *root,
6102 struct btrfs_path *path,
6103 struct btrfs_key *extent_key,
6104 struct btrfs_key *leaf_key,
6105 struct btrfs_ref_path *ref_path,
6106 struct disk_extent *new_extents,
6107 int nr_extents)
6108{
6109 struct extent_buffer *leaf;
6110 struct btrfs_file_extent_item *fi;
6111 struct inode *inode = NULL;
6112 struct btrfs_key key;
6113 u64 lock_start = 0;
6114 u64 lock_end = 0;
6115 u64 num_bytes;
6116 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05006117 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006118 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006119 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006120 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04006121 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04006122 int ret;
6123
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006124 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04006125 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006126 if (key.objectid < ref_path->owner_objectid ||
6127 (key.objectid == ref_path->owner_objectid &&
6128 key.type < BTRFS_EXTENT_DATA_KEY)) {
6129 key.objectid = ref_path->owner_objectid;
6130 key.type = BTRFS_EXTENT_DATA_KEY;
6131 key.offset = 0;
6132 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006133 }
6134
6135 while (1) {
6136 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6137 if (ret < 0)
6138 goto out;
6139
6140 leaf = path->nodes[0];
6141 nritems = btrfs_header_nritems(leaf);
6142next:
6143 if (extent_locked && ret > 0) {
6144 /*
6145 * the file extent item was modified by someone
6146 * before the extent got locked.
6147 */
Zheng Yan1a40e232008-09-26 10:09:34 -04006148 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6149 lock_end, GFP_NOFS);
6150 extent_locked = 0;
6151 }
6152
6153 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006154 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04006155 break;
6156
6157 BUG_ON(extent_locked);
6158 ret = btrfs_next_leaf(root, path);
6159 if (ret < 0)
6160 goto out;
6161 if (ret > 0)
6162 break;
6163 leaf = path->nodes[0];
6164 nritems = btrfs_header_nritems(leaf);
6165 }
6166
6167 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6168
6169 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6170 if ((key.objectid > ref_path->owner_objectid) ||
6171 (key.objectid == ref_path->owner_objectid &&
6172 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05006173 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006174 break;
6175 }
6176
6177 if (inode && key.objectid != inode->i_ino) {
6178 BUG_ON(extent_locked);
6179 btrfs_release_path(root, path);
6180 mutex_unlock(&inode->i_mutex);
6181 iput(inode);
6182 inode = NULL;
6183 continue;
6184 }
6185
6186 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6187 path->slots[0]++;
6188 ret = 1;
6189 goto next;
6190 }
6191 fi = btrfs_item_ptr(leaf, path->slots[0],
6192 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04006193 extent_type = btrfs_file_extent_type(leaf, fi);
6194 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6195 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04006196 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6197 extent_key->objectid)) {
6198 path->slots[0]++;
6199 ret = 1;
6200 goto next;
6201 }
6202
6203 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6204 ext_offset = btrfs_file_extent_offset(leaf, fi);
6205
Yan Zheng86288a12009-01-21 10:49:16 -05006206 if (search_end == (u64)-1) {
6207 search_end = key.offset - ext_offset +
6208 btrfs_file_extent_ram_bytes(leaf, fi);
6209 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006210
6211 if (!extent_locked) {
6212 lock_start = key.offset;
6213 lock_end = lock_start + num_bytes - 1;
6214 } else {
Yan Zheng66435582008-10-30 14:19:50 -04006215 if (lock_start > key.offset ||
6216 lock_end + 1 < key.offset + num_bytes) {
6217 unlock_extent(&BTRFS_I(inode)->io_tree,
6218 lock_start, lock_end, GFP_NOFS);
6219 extent_locked = 0;
6220 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006221 }
6222
6223 if (!inode) {
6224 btrfs_release_path(root, path);
6225
6226 inode = btrfs_iget_locked(root->fs_info->sb,
6227 key.objectid, root);
6228 if (inode->i_state & I_NEW) {
6229 BTRFS_I(inode)->root = root;
6230 BTRFS_I(inode)->location.objectid =
6231 key.objectid;
6232 BTRFS_I(inode)->location.type =
6233 BTRFS_INODE_ITEM_KEY;
6234 BTRFS_I(inode)->location.offset = 0;
6235 btrfs_read_locked_inode(inode);
6236 unlock_new_inode(inode);
6237 }
6238 /*
6239 * some code call btrfs_commit_transaction while
6240 * holding the i_mutex, so we can't use mutex_lock
6241 * here.
6242 */
6243 if (is_bad_inode(inode) ||
6244 !mutex_trylock(&inode->i_mutex)) {
6245 iput(inode);
6246 inode = NULL;
6247 key.offset = (u64)-1;
6248 goto skip;
6249 }
6250 }
6251
6252 if (!extent_locked) {
6253 struct btrfs_ordered_extent *ordered;
6254
6255 btrfs_release_path(root, path);
6256
6257 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6258 lock_end, GFP_NOFS);
6259 ordered = btrfs_lookup_first_ordered_extent(inode,
6260 lock_end);
6261 if (ordered &&
6262 ordered->file_offset <= lock_end &&
6263 ordered->file_offset + ordered->len > lock_start) {
6264 unlock_extent(&BTRFS_I(inode)->io_tree,
6265 lock_start, lock_end, GFP_NOFS);
6266 btrfs_start_ordered_extent(inode, ordered, 1);
6267 btrfs_put_ordered_extent(ordered);
6268 key.offset += num_bytes;
6269 goto skip;
6270 }
6271 if (ordered)
6272 btrfs_put_ordered_extent(ordered);
6273
Zheng Yan1a40e232008-09-26 10:09:34 -04006274 extent_locked = 1;
6275 continue;
6276 }
6277
6278 if (nr_extents == 1) {
6279 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04006280 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6281 new_extents[0].disk_bytenr);
6282 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6283 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006284 btrfs_mark_buffer_dirty(leaf);
6285
6286 btrfs_drop_extent_cache(inode, key.offset,
6287 key.offset + num_bytes - 1, 0);
6288
6289 ret = btrfs_inc_extent_ref(trans, root,
6290 new_extents[0].disk_bytenr,
6291 new_extents[0].disk_num_bytes,
6292 leaf->start,
6293 root->root_key.objectid,
6294 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006295 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006296 BUG_ON(ret);
6297
6298 ret = btrfs_free_extent(trans, root,
6299 extent_key->objectid,
6300 extent_key->offset,
6301 leaf->start,
6302 btrfs_header_owner(leaf),
6303 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006304 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006305 BUG_ON(ret);
6306
6307 btrfs_release_path(root, path);
6308 key.offset += num_bytes;
6309 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04006310 BUG_ON(1);
6311#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006312 u64 alloc_hint;
6313 u64 extent_len;
6314 int i;
6315 /*
6316 * drop old extent pointer at first, then insert the
6317 * new pointers one bye one
6318 */
6319 btrfs_release_path(root, path);
6320 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6321 key.offset + num_bytes,
6322 key.offset, &alloc_hint);
6323 BUG_ON(ret);
6324
6325 for (i = 0; i < nr_extents; i++) {
6326 if (ext_offset >= new_extents[i].num_bytes) {
6327 ext_offset -= new_extents[i].num_bytes;
6328 continue;
6329 }
6330 extent_len = min(new_extents[i].num_bytes -
6331 ext_offset, num_bytes);
6332
6333 ret = btrfs_insert_empty_item(trans, root,
6334 path, &key,
6335 sizeof(*fi));
6336 BUG_ON(ret);
6337
6338 leaf = path->nodes[0];
6339 fi = btrfs_item_ptr(leaf, path->slots[0],
6340 struct btrfs_file_extent_item);
6341 btrfs_set_file_extent_generation(leaf, fi,
6342 trans->transid);
6343 btrfs_set_file_extent_type(leaf, fi,
6344 BTRFS_FILE_EXTENT_REG);
6345 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6346 new_extents[i].disk_bytenr);
6347 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6348 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04006349 btrfs_set_file_extent_ram_bytes(leaf, fi,
6350 new_extents[i].ram_bytes);
6351
6352 btrfs_set_file_extent_compression(leaf, fi,
6353 new_extents[i].compression);
6354 btrfs_set_file_extent_encryption(leaf, fi,
6355 new_extents[i].encryption);
6356 btrfs_set_file_extent_other_encoding(leaf, fi,
6357 new_extents[i].other_encoding);
6358
Zheng Yan1a40e232008-09-26 10:09:34 -04006359 btrfs_set_file_extent_num_bytes(leaf, fi,
6360 extent_len);
6361 ext_offset += new_extents[i].offset;
6362 btrfs_set_file_extent_offset(leaf, fi,
6363 ext_offset);
6364 btrfs_mark_buffer_dirty(leaf);
6365
6366 btrfs_drop_extent_cache(inode, key.offset,
6367 key.offset + extent_len - 1, 0);
6368
6369 ret = btrfs_inc_extent_ref(trans, root,
6370 new_extents[i].disk_bytenr,
6371 new_extents[i].disk_num_bytes,
6372 leaf->start,
6373 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006374 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006375 BUG_ON(ret);
6376 btrfs_release_path(root, path);
6377
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006378 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04006379
6380 ext_offset = 0;
6381 num_bytes -= extent_len;
6382 key.offset += extent_len;
6383
6384 if (num_bytes == 0)
6385 break;
6386 }
6387 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006388#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04006389 }
6390
6391 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006392 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6393 lock_end, GFP_NOFS);
6394 extent_locked = 0;
6395 }
6396skip:
6397 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05006398 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006399 break;
6400
6401 cond_resched();
6402 }
6403 ret = 0;
6404out:
6405 btrfs_release_path(root, path);
6406 if (inode) {
6407 mutex_unlock(&inode->i_mutex);
6408 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006409 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6410 lock_end, GFP_NOFS);
6411 }
6412 iput(inode);
6413 }
6414 return ret;
6415}
6416
Zheng Yan1a40e232008-09-26 10:09:34 -04006417int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6418 struct btrfs_root *root,
6419 struct extent_buffer *buf, u64 orig_start)
6420{
6421 int level;
6422 int ret;
6423
6424 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6425 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6426
6427 level = btrfs_header_level(buf);
6428 if (level == 0) {
6429 struct btrfs_leaf_ref *ref;
6430 struct btrfs_leaf_ref *orig_ref;
6431
6432 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6433 if (!orig_ref)
6434 return -ENOENT;
6435
6436 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6437 if (!ref) {
6438 btrfs_free_leaf_ref(root, orig_ref);
6439 return -ENOMEM;
6440 }
6441
6442 ref->nritems = orig_ref->nritems;
6443 memcpy(ref->extents, orig_ref->extents,
6444 sizeof(ref->extents[0]) * ref->nritems);
6445
6446 btrfs_free_leaf_ref(root, orig_ref);
6447
6448 ref->root_gen = trans->transid;
6449 ref->bytenr = buf->start;
6450 ref->owner = btrfs_header_owner(buf);
6451 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006452
Zheng Yan1a40e232008-09-26 10:09:34 -04006453 ret = btrfs_add_leaf_ref(root, ref, 0);
6454 WARN_ON(ret);
6455 btrfs_free_leaf_ref(root, ref);
6456 }
6457 return 0;
6458}
6459
Chris Masond3977122009-01-05 21:25:51 -05006460static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006461 struct extent_buffer *leaf,
6462 struct btrfs_block_group_cache *group,
6463 struct btrfs_root *target_root)
6464{
6465 struct btrfs_key key;
6466 struct inode *inode = NULL;
6467 struct btrfs_file_extent_item *fi;
6468 u64 num_bytes;
6469 u64 skip_objectid = 0;
6470 u32 nritems;
6471 u32 i;
6472
6473 nritems = btrfs_header_nritems(leaf);
6474 for (i = 0; i < nritems; i++) {
6475 btrfs_item_key_to_cpu(leaf, &key, i);
6476 if (key.objectid == skip_objectid ||
6477 key.type != BTRFS_EXTENT_DATA_KEY)
6478 continue;
6479 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6480 if (btrfs_file_extent_type(leaf, fi) ==
6481 BTRFS_FILE_EXTENT_INLINE)
6482 continue;
6483 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6484 continue;
6485 if (!inode || inode->i_ino != key.objectid) {
6486 iput(inode);
6487 inode = btrfs_ilookup(target_root->fs_info->sb,
6488 key.objectid, target_root, 1);
6489 }
6490 if (!inode) {
6491 skip_objectid = key.objectid;
6492 continue;
6493 }
6494 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6495
6496 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6497 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006498 btrfs_drop_extent_cache(inode, key.offset,
6499 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006500 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6501 key.offset + num_bytes - 1, GFP_NOFS);
6502 cond_resched();
6503 }
6504 iput(inode);
6505 return 0;
6506}
6507
Chris Masond3977122009-01-05 21:25:51 -05006508static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006509 struct btrfs_root *root,
6510 struct extent_buffer *leaf,
6511 struct btrfs_block_group_cache *group,
6512 struct inode *reloc_inode)
6513{
6514 struct btrfs_key key;
6515 struct btrfs_key extent_key;
6516 struct btrfs_file_extent_item *fi;
6517 struct btrfs_leaf_ref *ref;
6518 struct disk_extent *new_extent;
6519 u64 bytenr;
6520 u64 num_bytes;
6521 u32 nritems;
6522 u32 i;
6523 int ext_index;
6524 int nr_extent;
6525 int ret;
6526
6527 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6528 BUG_ON(!new_extent);
6529
6530 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6531 BUG_ON(!ref);
6532
6533 ext_index = -1;
6534 nritems = btrfs_header_nritems(leaf);
6535 for (i = 0; i < nritems; i++) {
6536 btrfs_item_key_to_cpu(leaf, &key, i);
6537 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6538 continue;
6539 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6540 if (btrfs_file_extent_type(leaf, fi) ==
6541 BTRFS_FILE_EXTENT_INLINE)
6542 continue;
6543 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6544 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6545 if (bytenr == 0)
6546 continue;
6547
6548 ext_index++;
6549 if (bytenr >= group->key.objectid + group->key.offset ||
6550 bytenr + num_bytes <= group->key.objectid)
6551 continue;
6552
6553 extent_key.objectid = bytenr;
6554 extent_key.offset = num_bytes;
6555 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6556 nr_extent = 1;
6557 ret = get_new_locations(reloc_inode, &extent_key,
6558 group->key.objectid, 1,
6559 &new_extent, &nr_extent);
6560 if (ret > 0)
6561 continue;
6562 BUG_ON(ret < 0);
6563
6564 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6565 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6566 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6567 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6568
Zheng Yan1a40e232008-09-26 10:09:34 -04006569 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6570 new_extent->disk_bytenr);
6571 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6572 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006573 btrfs_mark_buffer_dirty(leaf);
6574
6575 ret = btrfs_inc_extent_ref(trans, root,
6576 new_extent->disk_bytenr,
6577 new_extent->disk_num_bytes,
6578 leaf->start,
6579 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006580 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006581 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006582
Zheng Yan1a40e232008-09-26 10:09:34 -04006583 ret = btrfs_free_extent(trans, root,
6584 bytenr, num_bytes, leaf->start,
6585 btrfs_header_owner(leaf),
6586 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006587 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006588 BUG_ON(ret);
6589 cond_resched();
6590 }
6591 kfree(new_extent);
6592 BUG_ON(ext_index + 1 != ref->nritems);
6593 btrfs_free_leaf_ref(root, ref);
6594 return 0;
6595}
6596
Yan Zhengf82d02d2008-10-29 14:49:05 -04006597int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6598 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006599{
6600 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006601 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006602
6603 if (root->reloc_root) {
6604 reloc_root = root->reloc_root;
6605 root->reloc_root = NULL;
6606 list_add(&reloc_root->dead_list,
6607 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006608
6609 btrfs_set_root_bytenr(&reloc_root->root_item,
6610 reloc_root->node->start);
6611 btrfs_set_root_level(&root->root_item,
6612 btrfs_header_level(reloc_root->node));
6613 memset(&reloc_root->root_item.drop_progress, 0,
6614 sizeof(struct btrfs_disk_key));
6615 reloc_root->root_item.drop_level = 0;
6616
6617 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6618 &reloc_root->root_key,
6619 &reloc_root->root_item);
6620 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006621 }
6622 return 0;
6623}
6624
6625int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6626{
6627 struct btrfs_trans_handle *trans;
6628 struct btrfs_root *reloc_root;
6629 struct btrfs_root *prev_root = NULL;
6630 struct list_head dead_roots;
6631 int ret;
6632 unsigned long nr;
6633
6634 INIT_LIST_HEAD(&dead_roots);
6635 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6636
6637 while (!list_empty(&dead_roots)) {
6638 reloc_root = list_entry(dead_roots.prev,
6639 struct btrfs_root, dead_list);
6640 list_del_init(&reloc_root->dead_list);
6641
6642 BUG_ON(reloc_root->commit_root != NULL);
6643 while (1) {
6644 trans = btrfs_join_transaction(root, 1);
6645 BUG_ON(!trans);
6646
6647 mutex_lock(&root->fs_info->drop_mutex);
6648 ret = btrfs_drop_snapshot(trans, reloc_root);
6649 if (ret != -EAGAIN)
6650 break;
6651 mutex_unlock(&root->fs_info->drop_mutex);
6652
6653 nr = trans->blocks_used;
6654 ret = btrfs_end_transaction(trans, root);
6655 BUG_ON(ret);
6656 btrfs_btree_balance_dirty(root, nr);
6657 }
6658
6659 free_extent_buffer(reloc_root->node);
6660
6661 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6662 &reloc_root->root_key);
6663 BUG_ON(ret);
6664 mutex_unlock(&root->fs_info->drop_mutex);
6665
6666 nr = trans->blocks_used;
6667 ret = btrfs_end_transaction(trans, root);
6668 BUG_ON(ret);
6669 btrfs_btree_balance_dirty(root, nr);
6670
6671 kfree(prev_root);
6672 prev_root = reloc_root;
6673 }
6674 if (prev_root) {
6675 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6676 kfree(prev_root);
6677 }
6678 return 0;
6679}
6680
6681int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6682{
6683 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6684 return 0;
6685}
6686
6687int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6688{
6689 struct btrfs_root *reloc_root;
6690 struct btrfs_trans_handle *trans;
6691 struct btrfs_key location;
6692 int found;
6693 int ret;
6694
6695 mutex_lock(&root->fs_info->tree_reloc_mutex);
6696 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6697 BUG_ON(ret);
6698 found = !list_empty(&root->fs_info->dead_reloc_roots);
6699 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6700
6701 if (found) {
6702 trans = btrfs_start_transaction(root, 1);
6703 BUG_ON(!trans);
6704 ret = btrfs_commit_transaction(trans, root);
6705 BUG_ON(ret);
6706 }
6707
6708 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6709 location.offset = (u64)-1;
6710 location.type = BTRFS_ROOT_ITEM_KEY;
6711
6712 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6713 BUG_ON(!reloc_root);
6714 btrfs_orphan_cleanup(reloc_root);
6715 return 0;
6716}
6717
Chris Masond3977122009-01-05 21:25:51 -05006718static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006719 struct btrfs_root *root)
6720{
6721 struct btrfs_root *reloc_root;
6722 struct extent_buffer *eb;
6723 struct btrfs_root_item *root_item;
6724 struct btrfs_key root_key;
6725 int ret;
6726
6727 BUG_ON(!root->ref_cows);
6728 if (root->reloc_root)
6729 return 0;
6730
6731 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6732 BUG_ON(!root_item);
6733
6734 ret = btrfs_copy_root(trans, root, root->commit_root,
6735 &eb, BTRFS_TREE_RELOC_OBJECTID);
6736 BUG_ON(ret);
6737
6738 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6739 root_key.offset = root->root_key.objectid;
6740 root_key.type = BTRFS_ROOT_ITEM_KEY;
6741
6742 memcpy(root_item, &root->root_item, sizeof(root_item));
6743 btrfs_set_root_refs(root_item, 0);
6744 btrfs_set_root_bytenr(root_item, eb->start);
6745 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006746 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006747
6748 btrfs_tree_unlock(eb);
6749 free_extent_buffer(eb);
6750
6751 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6752 &root_key, root_item);
6753 BUG_ON(ret);
6754 kfree(root_item);
6755
6756 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6757 &root_key);
6758 BUG_ON(!reloc_root);
6759 reloc_root->last_trans = trans->transid;
6760 reloc_root->commit_root = NULL;
6761 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6762
6763 root->reloc_root = reloc_root;
6764 return 0;
6765}
6766
6767/*
6768 * Core function of space balance.
6769 *
6770 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006771 * counted roots. There is one reloc tree for each subvol, and all
6772 * reloc trees share same root key objectid. Reloc trees are snapshots
6773 * of the latest committed roots of subvols (root->commit_root).
6774 *
6775 * To relocate a tree block referenced by a subvol, there are two steps.
6776 * COW the block through subvol's reloc tree, then update block pointer
6777 * in the subvol to point to the new block. Since all reloc trees share
6778 * same root key objectid, doing special handing for tree blocks owned
6779 * by them is easy. Once a tree block has been COWed in one reloc tree,
6780 * we can use the resulting new block directly when the same block is
6781 * required to COW again through other reloc trees. By this way, relocated
6782 * tree blocks are shared between reloc trees, so they are also shared
6783 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006784 */
Chris Masond3977122009-01-05 21:25:51 -05006785static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006786 struct btrfs_root *root,
6787 struct btrfs_path *path,
6788 struct btrfs_key *first_key,
6789 struct btrfs_ref_path *ref_path,
6790 struct btrfs_block_group_cache *group,
6791 struct inode *reloc_inode)
6792{
6793 struct btrfs_root *reloc_root;
6794 struct extent_buffer *eb = NULL;
6795 struct btrfs_key *keys;
6796 u64 *nodes;
6797 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006798 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006799 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006800 int ret;
6801
6802 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6803 lowest_level = ref_path->owner_objectid;
6804
Yan Zhengf82d02d2008-10-29 14:49:05 -04006805 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006806 path->lowest_level = lowest_level;
6807 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6808 BUG_ON(ret < 0);
6809 path->lowest_level = 0;
6810 btrfs_release_path(root, path);
6811 return 0;
6812 }
6813
Zheng Yan1a40e232008-09-26 10:09:34 -04006814 mutex_lock(&root->fs_info->tree_reloc_mutex);
6815 ret = init_reloc_tree(trans, root);
6816 BUG_ON(ret);
6817 reloc_root = root->reloc_root;
6818
Yan Zhengf82d02d2008-10-29 14:49:05 -04006819 shared_level = ref_path->shared_level;
6820 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006821
Yan Zhengf82d02d2008-10-29 14:49:05 -04006822 keys = ref_path->node_keys;
6823 nodes = ref_path->new_nodes;
6824 memset(&keys[shared_level + 1], 0,
6825 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6826 memset(&nodes[shared_level + 1], 0,
6827 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006828
Yan Zhengf82d02d2008-10-29 14:49:05 -04006829 if (nodes[lowest_level] == 0) {
6830 path->lowest_level = lowest_level;
6831 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6832 0, 1);
6833 BUG_ON(ret);
6834 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6835 eb = path->nodes[level];
6836 if (!eb || eb == reloc_root->node)
6837 break;
6838 nodes[level] = eb->start;
6839 if (level == 0)
6840 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6841 else
6842 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6843 }
Yan Zheng2b820322008-11-17 21:11:30 -05006844 if (nodes[0] &&
6845 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006846 eb = path->nodes[0];
6847 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6848 group, reloc_inode);
6849 BUG_ON(ret);
6850 }
6851 btrfs_release_path(reloc_root, path);
6852 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006853 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006854 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006855 BUG_ON(ret);
6856 }
6857
Zheng Yan1a40e232008-09-26 10:09:34 -04006858 /*
6859 * replace tree blocks in the fs tree with tree blocks in
6860 * the reloc tree.
6861 */
6862 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6863 BUG_ON(ret < 0);
6864
6865 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006866 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6867 0, 0);
6868 BUG_ON(ret);
6869 extent_buffer_get(path->nodes[0]);
6870 eb = path->nodes[0];
6871 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006872 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6873 BUG_ON(ret);
6874 free_extent_buffer(eb);
6875 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006876
Yan Zhengf82d02d2008-10-29 14:49:05 -04006877 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006878 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006879 return 0;
6880}
6881
Chris Masond3977122009-01-05 21:25:51 -05006882static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006883 struct btrfs_root *root,
6884 struct btrfs_path *path,
6885 struct btrfs_key *first_key,
6886 struct btrfs_ref_path *ref_path)
6887{
6888 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006889
6890 ret = relocate_one_path(trans, root, path, first_key,
6891 ref_path, NULL, NULL);
6892 BUG_ON(ret);
6893
Zheng Yan1a40e232008-09-26 10:09:34 -04006894 return 0;
6895}
6896
Chris Masond3977122009-01-05 21:25:51 -05006897static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006898 struct btrfs_root *extent_root,
6899 struct btrfs_path *path,
6900 struct btrfs_key *extent_key)
6901{
6902 int ret;
6903
Zheng Yan1a40e232008-09-26 10:09:34 -04006904 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6905 if (ret)
6906 goto out;
6907 ret = btrfs_del_item(trans, extent_root, path);
6908out:
Chris Masonedbd8d42007-12-21 16:27:24 -05006909 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006910 return ret;
6911}
6912
Chris Masond3977122009-01-05 21:25:51 -05006913static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006914 struct btrfs_ref_path *ref_path)
6915{
6916 struct btrfs_key root_key;
6917
6918 root_key.objectid = ref_path->root_objectid;
6919 root_key.type = BTRFS_ROOT_ITEM_KEY;
6920 if (is_cowonly_root(ref_path->root_objectid))
6921 root_key.offset = 0;
6922 else
6923 root_key.offset = (u64)-1;
6924
6925 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6926}
6927
Chris Masond3977122009-01-05 21:25:51 -05006928static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006929 struct btrfs_path *path,
6930 struct btrfs_key *extent_key,
6931 struct btrfs_block_group_cache *group,
6932 struct inode *reloc_inode, int pass)
6933{
6934 struct btrfs_trans_handle *trans;
6935 struct btrfs_root *found_root;
6936 struct btrfs_ref_path *ref_path = NULL;
6937 struct disk_extent *new_extents = NULL;
6938 int nr_extents = 0;
6939 int loops;
6940 int ret;
6941 int level;
6942 struct btrfs_key first_key;
6943 u64 prev_block = 0;
6944
Zheng Yan1a40e232008-09-26 10:09:34 -04006945
6946 trans = btrfs_start_transaction(extent_root, 1);
6947 BUG_ON(!trans);
6948
6949 if (extent_key->objectid == 0) {
6950 ret = del_extent_zero(trans, extent_root, path, extent_key);
6951 goto out;
6952 }
6953
6954 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6955 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05006956 ret = -ENOMEM;
6957 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006958 }
6959
6960 for (loops = 0; ; loops++) {
6961 if (loops == 0) {
6962 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6963 extent_key->objectid);
6964 } else {
6965 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6966 }
6967 if (ret < 0)
6968 goto out;
6969 if (ret > 0)
6970 break;
6971
6972 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6973 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6974 continue;
6975
6976 found_root = read_ref_root(extent_root->fs_info, ref_path);
6977 BUG_ON(!found_root);
6978 /*
6979 * for reference counted tree, only process reference paths
6980 * rooted at the latest committed root.
6981 */
6982 if (found_root->ref_cows &&
6983 ref_path->root_generation != found_root->root_key.offset)
6984 continue;
6985
6986 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6987 if (pass == 0) {
6988 /*
6989 * copy data extents to new locations
6990 */
6991 u64 group_start = group->key.objectid;
6992 ret = relocate_data_extent(reloc_inode,
6993 extent_key,
6994 group_start);
6995 if (ret < 0)
6996 goto out;
6997 break;
6998 }
6999 level = 0;
7000 } else {
7001 level = ref_path->owner_objectid;
7002 }
7003
7004 if (prev_block != ref_path->nodes[level]) {
7005 struct extent_buffer *eb;
7006 u64 block_start = ref_path->nodes[level];
7007 u64 block_size = btrfs_level_size(found_root, level);
7008
7009 eb = read_tree_block(found_root, block_start,
7010 block_size, 0);
7011 btrfs_tree_lock(eb);
7012 BUG_ON(level != btrfs_header_level(eb));
7013
7014 if (level == 0)
7015 btrfs_item_key_to_cpu(eb, &first_key, 0);
7016 else
7017 btrfs_node_key_to_cpu(eb, &first_key, 0);
7018
7019 btrfs_tree_unlock(eb);
7020 free_extent_buffer(eb);
7021 prev_block = block_start;
7022 }
7023
Yan Zheng24562422009-02-12 14:14:53 -05007024 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007025 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05007026 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007027 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7028 /*
7029 * try to update data extent references while
7030 * keeping metadata shared between snapshots.
7031 */
7032 if (pass == 1) {
7033 ret = relocate_one_path(trans, found_root,
7034 path, &first_key, ref_path,
7035 group, reloc_inode);
7036 if (ret < 0)
7037 goto out;
7038 continue;
7039 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007040 /*
7041 * use fallback method to process the remaining
7042 * references.
7043 */
7044 if (!new_extents) {
7045 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04007046 new_extents = kmalloc(sizeof(*new_extents),
7047 GFP_NOFS);
7048 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007049 ret = get_new_locations(reloc_inode,
7050 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04007051 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04007052 &new_extents,
7053 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007054 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04007055 goto out;
7056 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007057 ret = replace_one_extent(trans, found_root,
7058 path, extent_key,
7059 &first_key, ref_path,
7060 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05007061 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007062 ret = relocate_tree_block(trans, found_root, path,
7063 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007064 }
7065 if (ret < 0)
7066 goto out;
7067 }
7068 ret = 0;
7069out:
7070 btrfs_end_transaction(trans, extent_root);
7071 kfree(new_extents);
7072 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007073 return ret;
7074}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007075#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007076
Chris Masonec44a352008-04-28 15:29:52 -04007077static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7078{
7079 u64 num_devices;
7080 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7081 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7082
Yan Zheng2b820322008-11-17 21:11:30 -05007083 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04007084 if (num_devices == 1) {
7085 stripped |= BTRFS_BLOCK_GROUP_DUP;
7086 stripped = flags & ~stripped;
7087
7088 /* turn raid0 into single device chunks */
7089 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7090 return stripped;
7091
7092 /* turn mirroring into duplication */
7093 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7094 BTRFS_BLOCK_GROUP_RAID10))
7095 return stripped | BTRFS_BLOCK_GROUP_DUP;
7096 return flags;
7097 } else {
7098 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007099 if (flags & stripped)
7100 return flags;
7101
7102 stripped |= BTRFS_BLOCK_GROUP_DUP;
7103 stripped = flags & ~stripped;
7104
7105 /* switch duplicated blocks with raid1 */
7106 if (flags & BTRFS_BLOCK_GROUP_DUP)
7107 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7108
7109 /* turn single device chunks into raid0 */
7110 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7111 }
7112 return flags;
7113}
7114
Christoph Hellwigb2950862008-12-02 09:54:17 -05007115static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04007116 struct btrfs_block_group_cache *shrink_block_group,
7117 int force)
7118{
7119 struct btrfs_trans_handle *trans;
7120 u64 new_alloc_flags;
7121 u64 calc;
7122
Chris Masonc286ac42008-07-22 23:06:41 -04007123 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007124 if (btrfs_block_group_used(&shrink_block_group->item) +
7125 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04007126 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04007127
Chris Mason0ef3e662008-05-24 14:04:53 -04007128 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007129 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04007130
Chris Mason0ef3e662008-05-24 14:04:53 -04007131 new_alloc_flags = update_block_group_flags(root,
7132 shrink_block_group->flags);
7133 if (new_alloc_flags != shrink_block_group->flags) {
7134 calc =
7135 btrfs_block_group_used(&shrink_block_group->item);
7136 } else {
7137 calc = shrink_block_group->key.offset;
7138 }
Chris Masonc286ac42008-07-22 23:06:41 -04007139 spin_unlock(&shrink_block_group->lock);
7140
Chris Mason0ef3e662008-05-24 14:04:53 -04007141 do_chunk_alloc(trans, root->fs_info->extent_root,
7142 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04007143
Chris Mason0ef3e662008-05-24 14:04:53 -04007144 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04007145 } else
7146 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04007147 return 0;
7148}
7149
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007150
7151int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
7152 struct btrfs_block_group_cache *group)
7153
7154{
7155 __alloc_chunk_for_shrink(root, group, 1);
7156 set_block_group_readonly(group);
7157 return 0;
7158}
7159
Josef Bacikba1bf482009-09-11 16:11:19 -04007160/*
7161 * checks to see if its even possible to relocate this block group.
7162 *
7163 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7164 * ok to go ahead and try.
7165 */
7166int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7167{
7168 struct btrfs_block_group_cache *block_group;
7169 struct btrfs_space_info *space_info;
7170 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7171 struct btrfs_device *device;
7172 int full = 0;
7173 int ret = 0;
7174
7175 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
7176
7177 /* odd, couldn't find the block group, leave it alone */
7178 if (!block_group)
7179 return -1;
7180
7181 /* no bytes used, we're good */
7182 if (!btrfs_block_group_used(&block_group->item))
7183 goto out;
7184
7185 space_info = block_group->space_info;
7186 spin_lock(&space_info->lock);
7187
7188 full = space_info->full;
7189
7190 /*
7191 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007192 * relocate it unless we're able to allocate a new chunk below.
7193 *
7194 * Otherwise, we need to make sure we have room in the space to handle
7195 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007196 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007197 if ((space_info->total_bytes != block_group->key.offset) &&
7198 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04007199 space_info->bytes_pinned + space_info->bytes_readonly +
7200 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04007201 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007202 spin_unlock(&space_info->lock);
7203 goto out;
7204 }
7205 spin_unlock(&space_info->lock);
7206
7207 /*
7208 * ok we don't have enough space, but maybe we have free space on our
7209 * devices to allocate new chunks for relocation, so loop through our
7210 * alloc devices and guess if we have enough space. However, if we
7211 * were marked as full, then we know there aren't enough chunks, and we
7212 * can just return.
7213 */
7214 ret = -1;
7215 if (full)
7216 goto out;
7217
7218 mutex_lock(&root->fs_info->chunk_mutex);
7219 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7220 u64 min_free = btrfs_block_group_used(&block_group->item);
7221 u64 dev_offset, max_avail;
7222
7223 /*
7224 * check to make sure we can actually find a chunk with enough
7225 * space to fit our block group in.
7226 */
7227 if (device->total_bytes > device->bytes_used + min_free) {
7228 ret = find_free_dev_extent(NULL, device, min_free,
7229 &dev_offset, &max_avail);
7230 if (!ret)
7231 break;
7232 ret = -1;
7233 }
7234 }
7235 mutex_unlock(&root->fs_info->chunk_mutex);
7236out:
7237 btrfs_put_block_group(block_group);
7238 return ret;
7239}
7240
Christoph Hellwigb2950862008-12-02 09:54:17 -05007241static int find_first_block_group(struct btrfs_root *root,
7242 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007243{
Chris Mason925baed2008-06-25 16:01:30 -04007244 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007245 struct btrfs_key found_key;
7246 struct extent_buffer *leaf;
7247 int slot;
7248
7249 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7250 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007251 goto out;
7252
Chris Masond3977122009-01-05 21:25:51 -05007253 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007254 slot = path->slots[0];
7255 leaf = path->nodes[0];
7256 if (slot >= btrfs_header_nritems(leaf)) {
7257 ret = btrfs_next_leaf(root, path);
7258 if (ret == 0)
7259 continue;
7260 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007261 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007262 break;
7263 }
7264 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7265
7266 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007267 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7268 ret = 0;
7269 goto out;
7270 }
Chris Mason0b86a832008-03-24 15:01:56 -04007271 path->slots[0]++;
7272 }
7273 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04007274out:
Chris Mason0b86a832008-03-24 15:01:56 -04007275 return ret;
7276}
7277
Zheng Yan1a40e232008-09-26 10:09:34 -04007278int btrfs_free_block_groups(struct btrfs_fs_info *info)
7279{
7280 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007281 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007282 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007283 struct rb_node *n;
7284
Yan Zheng11833d62009-09-11 16:11:19 -04007285 down_write(&info->extent_commit_sem);
7286 while (!list_empty(&info->caching_block_groups)) {
7287 caching_ctl = list_entry(info->caching_block_groups.next,
7288 struct btrfs_caching_control, list);
7289 list_del(&caching_ctl->list);
7290 put_caching_control(caching_ctl);
7291 }
7292 up_write(&info->extent_commit_sem);
7293
Zheng Yan1a40e232008-09-26 10:09:34 -04007294 spin_lock(&info->block_group_cache_lock);
7295 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7296 block_group = rb_entry(n, struct btrfs_block_group_cache,
7297 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007298 rb_erase(&block_group->cache_node,
7299 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007300 spin_unlock(&info->block_group_cache_lock);
7301
Josef Bacik80eb2342008-10-29 14:49:05 -04007302 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007303 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007304 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007305
Josef Bacik817d52f2009-07-13 21:29:25 -04007306 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007307 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007308
7309 btrfs_remove_free_space_cache(block_group);
7310
Yan Zhengd2fb3432008-12-11 16:30:39 -05007311 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04007312 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007313
7314 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007315 }
7316 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007317
7318 /* now that all the block groups are freed, go through and
7319 * free all the space_info structs. This is only called during
7320 * the final stages of unmount, and so we know nobody is
7321 * using them. We call synchronize_rcu() once before we start,
7322 * just to be on the safe side.
7323 */
7324 synchronize_rcu();
7325
7326 while(!list_empty(&info->space_info)) {
7327 space_info = list_entry(info->space_info.next,
7328 struct btrfs_space_info,
7329 list);
7330
7331 list_del(&space_info->list);
7332 kfree(space_info);
7333 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007334 return 0;
7335}
7336
Chris Mason9078a3e2007-04-26 16:46:15 -04007337int btrfs_read_block_groups(struct btrfs_root *root)
7338{
7339 struct btrfs_path *path;
7340 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007341 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007342 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007343 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007344 struct btrfs_key key;
7345 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007346 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007347
Chris Masonbe744172007-05-06 10:15:01 -04007348 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007349 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007350 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007351 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007352 path = btrfs_alloc_path();
7353 if (!path)
7354 return -ENOMEM;
7355
Chris Masond3977122009-01-05 21:25:51 -05007356 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007357 ret = find_first_block_group(root, path, &key);
7358 if (ret > 0) {
7359 ret = 0;
7360 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007361 }
Chris Mason0b86a832008-03-24 15:01:56 -04007362 if (ret != 0)
7363 goto error;
7364
Chris Mason5f39d392007-10-15 16:14:19 -04007365 leaf = path->nodes[0];
7366 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007367 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007368 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007369 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04007370 break;
7371 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007372
Yan Zhengd2fb3432008-12-11 16:30:39 -05007373 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007374 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007375 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007376 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007377 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007378 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007379
7380 /*
7381 * we only want to have 32k of ram per block group for keeping
7382 * track of free space, and if we pass 1/2 of that we want to
7383 * start converting things over to using bitmaps
7384 */
7385 cache->extents_thresh = ((1024 * 32) / 2) /
7386 sizeof(struct btrfs_free_space);
7387
Chris Mason5f39d392007-10-15 16:14:19 -04007388 read_extent_buffer(leaf, &cache->item,
7389 btrfs_item_ptr_offset(leaf, path->slots[0]),
7390 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007391 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007392
Chris Mason9078a3e2007-04-26 16:46:15 -04007393 key.objectid = found_key.objectid + found_key.offset;
7394 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007395 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007396 cache->sectorsize = root->sectorsize;
7397
Josef Bacik817d52f2009-07-13 21:29:25 -04007398 /*
7399 * check for two cases, either we are full, and therefore
7400 * don't need to bother with the caching work since we won't
7401 * find any space, or we are empty, and we can just add all
7402 * the space in and be done with it. This saves us _alot_ of
7403 * time, particularly in the full case.
7404 */
7405 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04007406 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04007407 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007408 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007409 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007410 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007411 exclude_super_stripes(root, cache);
7412 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007413 cache->cached = BTRFS_CACHE_FINISHED;
7414 add_new_free_space(cache, root->fs_info,
7415 found_key.objectid,
7416 found_key.objectid +
7417 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007418 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007419 }
Chris Mason96b51792007-10-15 16:15:19 -04007420
Chris Mason6324fbf2008-03-24 15:01:59 -04007421 ret = update_space_info(info, cache->flags, found_key.offset,
7422 btrfs_block_group_used(&cache->item),
7423 &space_info);
7424 BUG_ON(ret);
7425 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007426 spin_lock(&cache->space_info->lock);
7427 cache->space_info->bytes_super += cache->bytes_super;
7428 spin_unlock(&cache->space_info->lock);
7429
Josef Bacik80eb2342008-10-29 14:49:05 -04007430 down_write(&space_info->groups_sem);
7431 list_add_tail(&cache->list, &space_info->block_groups);
7432 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007433
Josef Bacik0f9dd462008-09-23 13:14:11 -04007434 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7435 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007436
7437 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007438 if (btrfs_chunk_readonly(root, cache->key.objectid))
7439 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007440 }
Chris Mason0b86a832008-03-24 15:01:56 -04007441 ret = 0;
7442error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007443 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007444 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007445}
Chris Mason6324fbf2008-03-24 15:01:59 -04007446
7447int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7448 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007449 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007450 u64 size)
7451{
7452 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007453 struct btrfs_root *extent_root;
7454 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007455
7456 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007457
Chris Mason12fcfd22009-03-24 10:24:20 -04007458 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007459
Chris Mason8f18cf12008-04-25 16:53:30 -04007460 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007461 if (!cache)
7462 return -ENOMEM;
7463
Chris Masone17cade2008-04-15 15:41:47 -04007464 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007465 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007466 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007467 cache->sectorsize = root->sectorsize;
7468
7469 /*
7470 * we only want to have 32k of ram per block group for keeping track
7471 * of free space, and if we pass 1/2 of that we want to start
7472 * converting things over to using bitmaps
7473 */
7474 cache->extents_thresh = ((1024 * 32) / 2) /
7475 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007476 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007477 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007478 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007479 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007480 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007481
Chris Mason6324fbf2008-03-24 15:01:59 -04007482 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007483 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7484 cache->flags = type;
7485 btrfs_set_block_group_flags(&cache->item, type);
7486
Yan Zheng11833d62009-09-11 16:11:19 -04007487 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007488 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007489 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007490
Josef Bacik817d52f2009-07-13 21:29:25 -04007491 add_new_free_space(cache, root->fs_info, chunk_offset,
7492 chunk_offset + size);
7493
Yan Zheng11833d62009-09-11 16:11:19 -04007494 free_excluded_extents(root, cache);
7495
Chris Mason6324fbf2008-03-24 15:01:59 -04007496 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7497 &cache->space_info);
7498 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007499
7500 spin_lock(&cache->space_info->lock);
7501 cache->space_info->bytes_super += cache->bytes_super;
7502 spin_unlock(&cache->space_info->lock);
7503
Josef Bacik80eb2342008-10-29 14:49:05 -04007504 down_write(&cache->space_info->groups_sem);
7505 list_add_tail(&cache->list, &cache->space_info->block_groups);
7506 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007507
Josef Bacik0f9dd462008-09-23 13:14:11 -04007508 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7509 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007510
Chris Mason6324fbf2008-03-24 15:01:59 -04007511 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7512 sizeof(cache->item));
7513 BUG_ON(ret);
7514
Chris Masond18a2c42008-04-04 15:40:00 -04007515 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007516
Chris Mason6324fbf2008-03-24 15:01:59 -04007517 return 0;
7518}
Zheng Yan1a40e232008-09-26 10:09:34 -04007519
7520int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7521 struct btrfs_root *root, u64 group_start)
7522{
7523 struct btrfs_path *path;
7524 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007525 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007526 struct btrfs_key key;
7527 int ret;
7528
Zheng Yan1a40e232008-09-26 10:09:34 -04007529 root = root->fs_info->extent_root;
7530
7531 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7532 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007533 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007534
7535 memcpy(&key, &block_group->key, sizeof(key));
7536
Chris Mason44fb5512009-06-04 15:34:51 -04007537 /* make sure this block group isn't part of an allocation cluster */
7538 cluster = &root->fs_info->data_alloc_cluster;
7539 spin_lock(&cluster->refill_lock);
7540 btrfs_return_cluster_to_free_space(block_group, cluster);
7541 spin_unlock(&cluster->refill_lock);
7542
7543 /*
7544 * make sure this block group isn't part of a metadata
7545 * allocation cluster
7546 */
7547 cluster = &root->fs_info->meta_alloc_cluster;
7548 spin_lock(&cluster->refill_lock);
7549 btrfs_return_cluster_to_free_space(block_group, cluster);
7550 spin_unlock(&cluster->refill_lock);
7551
Zheng Yan1a40e232008-09-26 10:09:34 -04007552 path = btrfs_alloc_path();
7553 BUG_ON(!path);
7554
Yan Zheng3dfdb932009-01-21 10:49:16 -05007555 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007556 rb_erase(&block_group->cache_node,
7557 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007558 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007559
Josef Bacik80eb2342008-10-29 14:49:05 -04007560 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007561 /*
7562 * we must use list_del_init so people can check to see if they
7563 * are still on the list after taking the semaphore
7564 */
7565 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007566 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007567
Josef Bacik817d52f2009-07-13 21:29:25 -04007568 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007569 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007570
7571 btrfs_remove_free_space_cache(block_group);
7572
Yan Zhengc146afa2008-11-12 14:34:12 -05007573 spin_lock(&block_group->space_info->lock);
7574 block_group->space_info->total_bytes -= block_group->key.offset;
7575 block_group->space_info->bytes_readonly -= block_group->key.offset;
7576 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007577
7578 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007579
Chris Masonfa9c0d792009-04-03 09:47:43 -04007580 btrfs_put_block_group(block_group);
7581 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007582
7583 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7584 if (ret > 0)
7585 ret = -EIO;
7586 if (ret < 0)
7587 goto out;
7588
7589 ret = btrfs_del_item(trans, root, path);
7590out:
7591 btrfs_free_path(path);
7592 return ret;
7593}