blob: 359a754c782cd8338f0a1bf0d456306e3240c32c [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050025#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050026#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027#include "ctree.h"
28#include "disk-io.h"
29#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040030#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040032#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040033#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Josef Bacikf3465ca2008-11-12 14:19:50 -050035static int update_block_group(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 u64 bytenr, u64 num_bytes, int alloc,
38 int mark_free);
Yan Zheng11833d62009-09-11 16:11:19 -040039static int update_reserved_extents(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040041static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root,
43 u64 bytenr, u64 num_bytes, u64 parent,
44 u64 root_objectid, u64 owner_objectid,
45 u64 owner_offset, int refs_to_drop,
46 struct btrfs_delayed_extent_op *extra_op);
47static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
48 struct extent_buffer *leaf,
49 struct btrfs_extent_item *ei);
50static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root,
52 u64 parent, u64 root_objectid,
53 u64 flags, u64 owner, u64 offset,
54 struct btrfs_key *ins, int ref_mod);
55static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 parent, u64 root_objectid,
58 u64 flags, struct btrfs_disk_key *key,
59 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050060static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61 struct btrfs_root *extent_root, u64 alloc_bytes,
62 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040063static int pin_down_bytes(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root,
65 struct btrfs_path *path,
66 u64 bytenr, u64 num_bytes,
67 int is_data, int reserved,
68 struct extent_buffer **must_clean);
69static int find_next_key(struct btrfs_path *path, int level,
70 struct btrfs_key *key);
Josef 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 Masonfa9c0d72009-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 Masonfa9c0d72009-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 Masonfa9c0d72009-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{
Christoph Hellwig746cd1e2009-09-12 07:35:43 +02001575 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
1576 DISCARD_FL_BARRIER);
Chris Mason15916de2008-11-19 21:17:22 -05001577}
Chris Mason4b4e25f2008-11-20 10:22:27 -05001578#endif
Chris Mason15916de2008-11-19 21:17:22 -05001579
Liu Hui1f3c79a2009-01-05 15:57:51 -05001580static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1581 u64 num_bytes)
1582{
1583#ifdef BIO_RW_DISCARD
1584 int ret;
1585 u64 map_length = num_bytes;
1586 struct btrfs_multi_bio *multi = NULL;
1587
1588 /* Tell the block device(s) that the sectors can be discarded */
1589 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1590 bytenr, &map_length, &multi, 0);
1591 if (!ret) {
1592 struct btrfs_bio_stripe *stripe = multi->stripes;
1593 int i;
1594
1595 if (map_length > num_bytes)
1596 map_length = num_bytes;
1597
1598 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1599 btrfs_issue_discard(stripe->dev->bdev,
1600 stripe->physical,
1601 map_length);
1602 }
1603 kfree(multi);
1604 }
1605
1606 return ret;
1607#else
1608 return 0;
1609#endif
1610}
1611
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001612int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1613 struct btrfs_root *root,
1614 u64 bytenr, u64 num_bytes, u64 parent,
1615 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001616{
1617 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001618 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1619 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001620
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001621 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1622 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1623 parent, root_objectid, (int)owner,
1624 BTRFS_ADD_DELAYED_REF, NULL);
1625 } else {
1626 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1627 parent, root_objectid, owner, offset,
1628 BTRFS_ADD_DELAYED_REF, NULL);
1629 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001630 return ret;
1631}
1632
Chris Mason925baed2008-06-25 16:01:30 -04001633static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001634 struct btrfs_root *root,
1635 u64 bytenr, u64 num_bytes,
1636 u64 parent, u64 root_objectid,
1637 u64 owner, u64 offset, int refs_to_add,
1638 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001639{
Chris Mason5caf2a02007-04-02 11:20:42 -04001640 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001641 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001642 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001643 u64 refs;
1644 int ret;
1645 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001646
Chris Mason5caf2a02007-04-02 11:20:42 -04001647 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001648 if (!path)
1649 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001650
Chris Mason3c12ac72008-04-21 12:01:38 -04001651 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001652 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001653 /* this will setup the path even if it fails to insert the back ref */
1654 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1655 path, bytenr, num_bytes, parent,
1656 root_objectid, owner, offset,
1657 refs_to_add, extent_op);
1658 if (ret == 0)
1659 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001660
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001661 if (ret != -EAGAIN) {
1662 err = ret;
1663 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001664 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001665
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001666 leaf = path->nodes[0];
1667 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1668 refs = btrfs_extent_refs(leaf, item);
1669 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1670 if (extent_op)
1671 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001672
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001673 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001674 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001675
Chris Mason3c12ac72008-04-21 12:01:38 -04001676 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001677 path->leave_spinning = 1;
1678
Chris Mason56bec292009-03-13 10:10:06 -04001679 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001680 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001681 path, bytenr, parent, root_objectid,
1682 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001683 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001684out:
Chris Mason74493f72007-12-11 09:25:06 -05001685 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001687}
1688
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001689static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1690 struct btrfs_root *root,
1691 struct btrfs_delayed_ref_node *node,
1692 struct btrfs_delayed_extent_op *extent_op,
1693 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001694{
Chris Mason56bec292009-03-13 10:10:06 -04001695 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001696 struct btrfs_delayed_data_ref *ref;
1697 struct btrfs_key ins;
1698 u64 parent = 0;
1699 u64 ref_root = 0;
1700 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001701
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001702 ins.objectid = node->bytenr;
1703 ins.offset = node->num_bytes;
1704 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001705
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001706 ref = btrfs_delayed_node_to_data_ref(node);
1707 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1708 parent = ref->parent;
1709 else
1710 ref_root = ref->root;
1711
1712 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1713 if (extent_op) {
1714 BUG_ON(extent_op->update_key);
1715 flags |= extent_op->flags_to_set;
1716 }
1717 ret = alloc_reserved_file_extent(trans, root,
1718 parent, ref_root, flags,
1719 ref->objectid, ref->offset,
1720 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001721 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1722 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1723 node->num_bytes, parent,
1724 ref_root, ref->objectid,
1725 ref->offset, node->ref_mod,
1726 extent_op);
1727 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1728 ret = __btrfs_free_extent(trans, root, node->bytenr,
1729 node->num_bytes, parent,
1730 ref_root, ref->objectid,
1731 ref->offset, node->ref_mod,
1732 extent_op);
1733 } else {
1734 BUG();
1735 }
Chris Mason56bec292009-03-13 10:10:06 -04001736 return ret;
1737}
1738
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001739static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1740 struct extent_buffer *leaf,
1741 struct btrfs_extent_item *ei)
1742{
1743 u64 flags = btrfs_extent_flags(leaf, ei);
1744 if (extent_op->update_flags) {
1745 flags |= extent_op->flags_to_set;
1746 btrfs_set_extent_flags(leaf, ei, flags);
1747 }
1748
1749 if (extent_op->update_key) {
1750 struct btrfs_tree_block_info *bi;
1751 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1752 bi = (struct btrfs_tree_block_info *)(ei + 1);
1753 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1754 }
1755}
1756
1757static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1758 struct btrfs_root *root,
1759 struct btrfs_delayed_ref_node *node,
1760 struct btrfs_delayed_extent_op *extent_op)
1761{
1762 struct btrfs_key key;
1763 struct btrfs_path *path;
1764 struct btrfs_extent_item *ei;
1765 struct extent_buffer *leaf;
1766 u32 item_size;
1767 int ret;
1768 int err = 0;
1769
1770 path = btrfs_alloc_path();
1771 if (!path)
1772 return -ENOMEM;
1773
1774 key.objectid = node->bytenr;
1775 key.type = BTRFS_EXTENT_ITEM_KEY;
1776 key.offset = node->num_bytes;
1777
1778 path->reada = 1;
1779 path->leave_spinning = 1;
1780 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1781 path, 0, 1);
1782 if (ret < 0) {
1783 err = ret;
1784 goto out;
1785 }
1786 if (ret > 0) {
1787 err = -EIO;
1788 goto out;
1789 }
1790
1791 leaf = path->nodes[0];
1792 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1793#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1794 if (item_size < sizeof(*ei)) {
1795 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1796 path, (u64)-1, 0);
1797 if (ret < 0) {
1798 err = ret;
1799 goto out;
1800 }
1801 leaf = path->nodes[0];
1802 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1803 }
1804#endif
1805 BUG_ON(item_size < sizeof(*ei));
1806 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1807 __run_delayed_extent_op(extent_op, leaf, ei);
1808
1809 btrfs_mark_buffer_dirty(leaf);
1810out:
1811 btrfs_free_path(path);
1812 return err;
1813}
1814
1815static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1816 struct btrfs_root *root,
1817 struct btrfs_delayed_ref_node *node,
1818 struct btrfs_delayed_extent_op *extent_op,
1819 int insert_reserved)
1820{
1821 int ret = 0;
1822 struct btrfs_delayed_tree_ref *ref;
1823 struct btrfs_key ins;
1824 u64 parent = 0;
1825 u64 ref_root = 0;
1826
1827 ins.objectid = node->bytenr;
1828 ins.offset = node->num_bytes;
1829 ins.type = BTRFS_EXTENT_ITEM_KEY;
1830
1831 ref = btrfs_delayed_node_to_tree_ref(node);
1832 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1833 parent = ref->parent;
1834 else
1835 ref_root = ref->root;
1836
1837 BUG_ON(node->ref_mod != 1);
1838 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1839 BUG_ON(!extent_op || !extent_op->update_flags ||
1840 !extent_op->update_key);
1841 ret = alloc_reserved_tree_block(trans, root,
1842 parent, ref_root,
1843 extent_op->flags_to_set,
1844 &extent_op->key,
1845 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001846 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1847 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1848 node->num_bytes, parent, ref_root,
1849 ref->level, 0, 1, extent_op);
1850 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1851 ret = __btrfs_free_extent(trans, root, node->bytenr,
1852 node->num_bytes, parent, ref_root,
1853 ref->level, 0, 1, extent_op);
1854 } else {
1855 BUG();
1856 }
1857 return ret;
1858}
1859
1860
Chris Mason56bec292009-03-13 10:10:06 -04001861/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001862static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1863 struct btrfs_root *root,
1864 struct btrfs_delayed_ref_node *node,
1865 struct btrfs_delayed_extent_op *extent_op,
1866 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001867{
Josef Bacikeb099672009-02-12 09:27:38 -05001868 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001869 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001870 struct btrfs_delayed_ref_head *head;
1871 /*
1872 * we've hit the end of the chain and we were supposed
1873 * to insert this extent into the tree. But, it got
1874 * deleted before we ever needed to insert it, so all
1875 * we have to do is clean up the accounting
1876 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001877 BUG_ON(extent_op);
1878 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001879 if (insert_reserved) {
Yan Zheng11833d62009-09-11 16:11:19 -04001880 int mark_free = 0;
1881 struct extent_buffer *must_clean = NULL;
1882
1883 ret = pin_down_bytes(trans, root, NULL,
1884 node->bytenr, node->num_bytes,
1885 head->is_data, 1, &must_clean);
1886 if (ret > 0)
1887 mark_free = 1;
1888
1889 if (must_clean) {
1890 clean_tree_block(NULL, root, must_clean);
1891 btrfs_tree_unlock(must_clean);
1892 free_extent_buffer(must_clean);
1893 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001894 if (head->is_data) {
1895 ret = btrfs_del_csums(trans, root,
1896 node->bytenr,
1897 node->num_bytes);
1898 BUG_ON(ret);
1899 }
Yan Zheng11833d62009-09-11 16:11:19 -04001900 if (mark_free) {
1901 ret = btrfs_free_reserved_extent(root,
1902 node->bytenr,
1903 node->num_bytes);
1904 BUG_ON(ret);
1905 }
Chris Mason56bec292009-03-13 10:10:06 -04001906 }
Chris Mason56bec292009-03-13 10:10:06 -04001907 mutex_unlock(&head->mutex);
1908 return 0;
1909 }
Josef Bacikeb099672009-02-12 09:27:38 -05001910
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001911 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1912 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1913 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1914 insert_reserved);
1915 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1916 node->type == BTRFS_SHARED_DATA_REF_KEY)
1917 ret = run_delayed_data_ref(trans, root, node, extent_op,
1918 insert_reserved);
1919 else
1920 BUG();
1921 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001922}
1923
Chris Mason56bec292009-03-13 10:10:06 -04001924static noinline struct btrfs_delayed_ref_node *
1925select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001926{
Chris Mason56bec292009-03-13 10:10:06 -04001927 struct rb_node *node;
1928 struct btrfs_delayed_ref_node *ref;
1929 int action = BTRFS_ADD_DELAYED_REF;
1930again:
1931 /*
1932 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1933 * this prevents ref count from going down to zero when
1934 * there still are pending delayed ref.
1935 */
1936 node = rb_prev(&head->node.rb_node);
1937 while (1) {
1938 if (!node)
1939 break;
1940 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1941 rb_node);
1942 if (ref->bytenr != head->node.bytenr)
1943 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001944 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001945 return ref;
1946 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001947 }
Chris Mason56bec292009-03-13 10:10:06 -04001948 if (action == BTRFS_ADD_DELAYED_REF) {
1949 action = BTRFS_DROP_DELAYED_REF;
1950 goto again;
1951 }
1952 return NULL;
1953}
1954
Chris Masonc3e69d52009-03-13 10:17:05 -04001955static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1956 struct btrfs_root *root,
1957 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001958{
Chris Mason56bec292009-03-13 10:10:06 -04001959 struct btrfs_delayed_ref_root *delayed_refs;
1960 struct btrfs_delayed_ref_node *ref;
1961 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001962 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001963 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001964 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001965 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001966
1967 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001968 while (1) {
1969 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001970 /* pick a new head ref from the cluster list */
1971 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001972 break;
Chris Mason56bec292009-03-13 10:10:06 -04001973
Chris Masonc3e69d52009-03-13 10:17:05 -04001974 locked_ref = list_entry(cluster->next,
1975 struct btrfs_delayed_ref_head, cluster);
1976
1977 /* grab the lock that says we are going to process
1978 * all the refs for this head */
1979 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1980
1981 /*
1982 * we may have dropped the spin lock to get the head
1983 * mutex lock, and that might have given someone else
1984 * time to free the head. If that's true, it has been
1985 * removed from our list and we can move on.
1986 */
1987 if (ret == -EAGAIN) {
1988 locked_ref = NULL;
1989 count++;
1990 continue;
Chris Mason56bec292009-03-13 10:10:06 -04001991 }
1992 }
1993
1994 /*
1995 * record the must insert reserved flag before we
1996 * drop the spin lock.
1997 */
1998 must_insert_reserved = locked_ref->must_insert_reserved;
1999 locked_ref->must_insert_reserved = 0;
2000
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002001 extent_op = locked_ref->extent_op;
2002 locked_ref->extent_op = NULL;
2003
Chris Mason56bec292009-03-13 10:10:06 -04002004 /*
2005 * locked_ref is the head node, so we have to go one
2006 * node back for any delayed ref updates
2007 */
Chris Mason56bec292009-03-13 10:10:06 -04002008 ref = select_delayed_ref(locked_ref);
2009 if (!ref) {
2010 /* All delayed refs have been processed, Go ahead
2011 * and send the head node to run_one_delayed_ref,
2012 * so that any accounting fixes can happen
2013 */
2014 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002015
2016 if (extent_op && must_insert_reserved) {
2017 kfree(extent_op);
2018 extent_op = NULL;
2019 }
2020
2021 if (extent_op) {
2022 spin_unlock(&delayed_refs->lock);
2023
2024 ret = run_delayed_extent_op(trans, root,
2025 ref, extent_op);
2026 BUG_ON(ret);
2027 kfree(extent_op);
2028
2029 cond_resched();
2030 spin_lock(&delayed_refs->lock);
2031 continue;
2032 }
2033
Chris Masonc3e69d52009-03-13 10:17:05 -04002034 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002035 locked_ref = NULL;
2036 }
2037
2038 ref->in_tree = 0;
2039 rb_erase(&ref->rb_node, &delayed_refs->root);
2040 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002041
Chris Mason56bec292009-03-13 10:10:06 -04002042 spin_unlock(&delayed_refs->lock);
2043
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002044 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002045 must_insert_reserved);
2046 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002047
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002048 btrfs_put_delayed_ref(ref);
2049 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002050 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002051
Chris Mason1887be62009-03-13 10:11:24 -04002052 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002053 spin_lock(&delayed_refs->lock);
2054 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002055 return count;
2056}
2057
2058/*
2059 * this starts processing the delayed reference count updates and
2060 * extent insertions we have queued up so far. count can be
2061 * 0, which means to process everything in the tree at the start
2062 * of the run (but not newly added entries), or it can be some target
2063 * number you'd like to process.
2064 */
2065int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2066 struct btrfs_root *root, unsigned long count)
2067{
2068 struct rb_node *node;
2069 struct btrfs_delayed_ref_root *delayed_refs;
2070 struct btrfs_delayed_ref_node *ref;
2071 struct list_head cluster;
2072 int ret;
2073 int run_all = count == (unsigned long)-1;
2074 int run_most = 0;
2075
2076 if (root == root->fs_info->extent_root)
2077 root = root->fs_info->tree_root;
2078
2079 delayed_refs = &trans->transaction->delayed_refs;
2080 INIT_LIST_HEAD(&cluster);
2081again:
2082 spin_lock(&delayed_refs->lock);
2083 if (count == 0) {
2084 count = delayed_refs->num_entries * 2;
2085 run_most = 1;
2086 }
2087 while (1) {
2088 if (!(run_all || run_most) &&
2089 delayed_refs->num_heads_ready < 64)
2090 break;
2091
2092 /*
2093 * go find something we can process in the rbtree. We start at
2094 * the beginning of the tree, and then build a cluster
2095 * of refs to process starting at the first one we are able to
2096 * lock
2097 */
2098 ret = btrfs_find_ref_cluster(trans, &cluster,
2099 delayed_refs->run_delayed_start);
2100 if (ret)
2101 break;
2102
2103 ret = run_clustered_refs(trans, root, &cluster);
2104 BUG_ON(ret < 0);
2105
2106 count -= min_t(unsigned long, ret, count);
2107
2108 if (count == 0)
2109 break;
2110 }
2111
Chris Mason56bec292009-03-13 10:10:06 -04002112 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002113 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002114 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002115 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002116 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002117
2118 while (node) {
2119 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2120 rb_node);
2121 if (btrfs_delayed_ref_is_head(ref)) {
2122 struct btrfs_delayed_ref_head *head;
2123
2124 head = btrfs_delayed_node_to_head(ref);
2125 atomic_inc(&ref->refs);
2126
2127 spin_unlock(&delayed_refs->lock);
2128 mutex_lock(&head->mutex);
2129 mutex_unlock(&head->mutex);
2130
2131 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002132 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002133 goto again;
2134 }
2135 node = rb_next(node);
2136 }
2137 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002138 schedule_timeout(1);
2139 goto again;
2140 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002141out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002142 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002143 return 0;
2144}
2145
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002146int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2147 struct btrfs_root *root,
2148 u64 bytenr, u64 num_bytes, u64 flags,
2149 int is_data)
2150{
2151 struct btrfs_delayed_extent_op *extent_op;
2152 int ret;
2153
2154 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2155 if (!extent_op)
2156 return -ENOMEM;
2157
2158 extent_op->flags_to_set = flags;
2159 extent_op->update_flags = 1;
2160 extent_op->update_key = 0;
2161 extent_op->is_data = is_data ? 1 : 0;
2162
2163 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2164 if (ret)
2165 kfree(extent_op);
2166 return ret;
2167}
2168
2169static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2170 struct btrfs_root *root,
2171 struct btrfs_path *path,
2172 u64 objectid, u64 offset, u64 bytenr)
2173{
2174 struct btrfs_delayed_ref_head *head;
2175 struct btrfs_delayed_ref_node *ref;
2176 struct btrfs_delayed_data_ref *data_ref;
2177 struct btrfs_delayed_ref_root *delayed_refs;
2178 struct rb_node *node;
2179 int ret = 0;
2180
2181 ret = -ENOENT;
2182 delayed_refs = &trans->transaction->delayed_refs;
2183 spin_lock(&delayed_refs->lock);
2184 head = btrfs_find_delayed_ref_head(trans, bytenr);
2185 if (!head)
2186 goto out;
2187
2188 if (!mutex_trylock(&head->mutex)) {
2189 atomic_inc(&head->node.refs);
2190 spin_unlock(&delayed_refs->lock);
2191
2192 btrfs_release_path(root->fs_info->extent_root, path);
2193
2194 mutex_lock(&head->mutex);
2195 mutex_unlock(&head->mutex);
2196 btrfs_put_delayed_ref(&head->node);
2197 return -EAGAIN;
2198 }
2199
2200 node = rb_prev(&head->node.rb_node);
2201 if (!node)
2202 goto out_unlock;
2203
2204 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2205
2206 if (ref->bytenr != bytenr)
2207 goto out_unlock;
2208
2209 ret = 1;
2210 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2211 goto out_unlock;
2212
2213 data_ref = btrfs_delayed_node_to_data_ref(ref);
2214
2215 node = rb_prev(node);
2216 if (node) {
2217 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2218 if (ref->bytenr == bytenr)
2219 goto out_unlock;
2220 }
2221
2222 if (data_ref->root != root->root_key.objectid ||
2223 data_ref->objectid != objectid || data_ref->offset != offset)
2224 goto out_unlock;
2225
2226 ret = 0;
2227out_unlock:
2228 mutex_unlock(&head->mutex);
2229out:
2230 spin_unlock(&delayed_refs->lock);
2231 return ret;
2232}
2233
2234static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2235 struct btrfs_root *root,
2236 struct btrfs_path *path,
2237 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002238{
2239 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002240 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002241 struct btrfs_extent_data_ref *ref;
2242 struct btrfs_extent_inline_ref *iref;
2243 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002244 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002245 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002246 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002247
Chris Masonbe20aa92007-12-17 20:14:01 -05002248 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002249 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002250 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002251
Chris Masonbe20aa92007-12-17 20:14:01 -05002252 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2253 if (ret < 0)
2254 goto out;
2255 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002256
2257 ret = -ENOENT;
2258 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002259 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002260
Zheng Yan31840ae2008-09-23 13:14:14 -04002261 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002262 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002263 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002264
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002265 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002266 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002267
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002268 ret = 1;
2269 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2270#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2271 if (item_size < sizeof(*ei)) {
2272 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2273 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002274 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002275#endif
2276 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2277
2278 if (item_size != sizeof(*ei) +
2279 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2280 goto out;
2281
2282 if (btrfs_extent_generation(leaf, ei) <=
2283 btrfs_root_last_snapshot(&root->root_item))
2284 goto out;
2285
2286 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2287 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2288 BTRFS_EXTENT_DATA_REF_KEY)
2289 goto out;
2290
2291 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2292 if (btrfs_extent_refs(leaf, ei) !=
2293 btrfs_extent_data_ref_count(leaf, ref) ||
2294 btrfs_extent_data_ref_root(leaf, ref) !=
2295 root->root_key.objectid ||
2296 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2297 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2298 goto out;
2299
Yan Zhengf321e492008-07-30 09:26:11 -04002300 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002301out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002302 return ret;
2303}
2304
2305int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2306 struct btrfs_root *root,
2307 u64 objectid, u64 offset, u64 bytenr)
2308{
2309 struct btrfs_path *path;
2310 int ret;
2311 int ret2;
2312
2313 path = btrfs_alloc_path();
2314 if (!path)
2315 return -ENOENT;
2316
2317 do {
2318 ret = check_committed_ref(trans, root, path, objectid,
2319 offset, bytenr);
2320 if (ret && ret != -ENOENT)
2321 goto out;
2322
2323 ret2 = check_delayed_ref(trans, root, path, objectid,
2324 offset, bytenr);
2325 } while (ret2 == -EAGAIN);
2326
2327 if (ret2 && ret2 != -ENOENT) {
2328 ret = ret2;
2329 goto out;
2330 }
2331
2332 if (ret != -ENOENT || ret2 != -ENOENT)
2333 ret = 0;
2334out:
Yan Zhengf321e492008-07-30 09:26:11 -04002335 btrfs_free_path(path);
2336 return ret;
2337}
2338
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002339#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002340int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2341 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002342{
Chris Mason5f39d392007-10-15 16:14:19 -04002343 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002344 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002345 u64 root_gen;
2346 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002347 int i;
Chris Masondb945352007-10-15 16:15:53 -04002348 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002349 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002350 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002351
Chris Mason3768f362007-03-13 16:47:54 -04002352 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002353 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002354
Zheng Yane4657682008-09-26 10:04:53 -04002355 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2356 shared = 0;
2357 root_gen = root->root_key.offset;
2358 } else {
2359 shared = 1;
2360 root_gen = trans->transid - 1;
2361 }
2362
Chris Masondb945352007-10-15 16:15:53 -04002363 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002364 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002365
Zheng Yan31840ae2008-09-23 13:14:14 -04002366 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002367 struct btrfs_leaf_ref *ref;
2368 struct btrfs_extent_info *info;
2369
Zheng Yan31840ae2008-09-23 13:14:14 -04002370 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002371 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002372 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002373 goto out;
2374 }
2375
Zheng Yane4657682008-09-26 10:04:53 -04002376 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002377 ref->bytenr = buf->start;
2378 ref->owner = btrfs_header_owner(buf);
2379 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002380 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002381 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002382
Zheng Yan31840ae2008-09-23 13:14:14 -04002383 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002384 u64 disk_bytenr;
2385 btrfs_item_key_to_cpu(buf, &key, i);
2386 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2387 continue;
2388 fi = btrfs_item_ptr(buf, i,
2389 struct btrfs_file_extent_item);
2390 if (btrfs_file_extent_type(buf, fi) ==
2391 BTRFS_FILE_EXTENT_INLINE)
2392 continue;
2393 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2394 if (disk_bytenr == 0)
2395 continue;
2396
2397 info->bytenr = disk_bytenr;
2398 info->num_bytes =
2399 btrfs_file_extent_disk_num_bytes(buf, fi);
2400 info->objectid = key.objectid;
2401 info->offset = key.offset;
2402 info++;
2403 }
2404
Zheng Yane4657682008-09-26 10:04:53 -04002405 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002406 if (ret == -EEXIST && shared) {
2407 struct btrfs_leaf_ref *old;
2408 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2409 BUG_ON(!old);
2410 btrfs_remove_leaf_ref(root, old);
2411 btrfs_free_leaf_ref(root, old);
2412 ret = btrfs_add_leaf_ref(root, ref, shared);
2413 }
Yan Zheng31153d82008-07-28 15:32:19 -04002414 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002415 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002416 }
2417out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002418 return ret;
2419}
2420
Chris Masonb7a9f292009-02-04 09:23:45 -05002421/* when a block goes through cow, we update the reference counts of
2422 * everything that block points to. The internal pointers of the block
2423 * can be in just about any order, and it is likely to have clusters of
2424 * things that are close together and clusters of things that are not.
2425 *
2426 * To help reduce the seeks that come with updating all of these reference
2427 * counts, sort them by byte number before actual updates are done.
2428 *
2429 * struct refsort is used to match byte number to slot in the btree block.
2430 * we sort based on the byte number and then use the slot to actually
2431 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002432 *
2433 * struct refsort is smaller than strcut btrfs_item and smaller than
2434 * struct btrfs_key_ptr. Since we're currently limited to the page size
2435 * for a btree block, there's no way for a kmalloc of refsorts for a
2436 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002437 */
2438struct refsort {
2439 u64 bytenr;
2440 u32 slot;
2441};
2442
2443/*
2444 * for passing into sort()
2445 */
2446static int refsort_cmp(const void *a_void, const void *b_void)
2447{
2448 const struct refsort *a = a_void;
2449 const struct refsort *b = b_void;
2450
2451 if (a->bytenr < b->bytenr)
2452 return -1;
2453 if (a->bytenr > b->bytenr)
2454 return 1;
2455 return 0;
2456}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002457#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002458
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002459static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002460 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002461 struct extent_buffer *buf,
2462 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002463{
2464 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002465 u64 num_bytes;
2466 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002467 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002468 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002469 struct btrfs_key key;
2470 struct btrfs_file_extent_item *fi;
2471 int i;
2472 int level;
2473 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002474 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002475 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002476
2477 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002478 nritems = btrfs_header_nritems(buf);
2479 level = btrfs_header_level(buf);
2480
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002481 if (!root->ref_cows && level == 0)
2482 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002483
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002484 if (inc)
2485 process_func = btrfs_inc_extent_ref;
2486 else
2487 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002488
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002489 if (full_backref)
2490 parent = buf->start;
2491 else
2492 parent = 0;
2493
Zheng Yan31840ae2008-09-23 13:14:14 -04002494 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002495 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002496 btrfs_item_key_to_cpu(buf, &key, i);
2497 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002498 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002499 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002500 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002501 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002502 BTRFS_FILE_EXTENT_INLINE)
2503 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002504 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2505 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002506 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002507
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002508 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2509 key.offset -= btrfs_file_extent_offset(buf, fi);
2510 ret = process_func(trans, root, bytenr, num_bytes,
2511 parent, ref_root, key.objectid,
2512 key.offset);
2513 if (ret)
2514 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002515 } else {
2516 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002517 num_bytes = btrfs_level_size(root, level - 1);
2518 ret = process_func(trans, root, bytenr, num_bytes,
2519 parent, ref_root, level - 1, 0);
2520 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002521 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002522 }
2523 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002524 return 0;
2525fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002526 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002527 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002528}
2529
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002530int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2531 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002532{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002533 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2534}
Zheng Yan31840ae2008-09-23 13:14:14 -04002535
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002536int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2537 struct extent_buffer *buf, int full_backref)
2538{
2539 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002540}
2541
Chris Mason9078a3e2007-04-26 16:46:15 -04002542static int write_one_cache_group(struct btrfs_trans_handle *trans,
2543 struct btrfs_root *root,
2544 struct btrfs_path *path,
2545 struct btrfs_block_group_cache *cache)
2546{
2547 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002548 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002549 unsigned long bi;
2550 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002551
Chris Mason9078a3e2007-04-26 16:46:15 -04002552 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002553 if (ret < 0)
2554 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002555 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002556
2557 leaf = path->nodes[0];
2558 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2559 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2560 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002561 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002562fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002563 if (ret)
2564 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002565 return 0;
2566
2567}
2568
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002569static struct btrfs_block_group_cache *
2570next_block_group(struct btrfs_root *root,
2571 struct btrfs_block_group_cache *cache)
2572{
2573 struct rb_node *node;
2574 spin_lock(&root->fs_info->block_group_cache_lock);
2575 node = rb_next(&cache->cache_node);
2576 btrfs_put_block_group(cache);
2577 if (node) {
2578 cache = rb_entry(node, struct btrfs_block_group_cache,
2579 cache_node);
2580 atomic_inc(&cache->count);
2581 } else
2582 cache = NULL;
2583 spin_unlock(&root->fs_info->block_group_cache_lock);
2584 return cache;
2585}
2586
Chris Mason96b51792007-10-15 16:15:19 -04002587int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2588 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002589{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002590 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002591 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002592 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002593 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002594
2595 path = btrfs_alloc_path();
2596 if (!path)
2597 return -ENOMEM;
2598
Chris Masond3977122009-01-05 21:25:51 -05002599 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002600 if (last == 0) {
2601 err = btrfs_run_delayed_refs(trans, root,
2602 (unsigned long)-1);
2603 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002604 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002605
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002606 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2607 while (cache) {
2608 if (cache->dirty)
2609 break;
2610 cache = next_block_group(root, cache);
2611 }
2612 if (!cache) {
2613 if (last == 0)
2614 break;
2615 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002616 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002617 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002618
2619 cache->dirty = 0;
2620 last = cache->key.objectid + cache->key.offset;
2621
2622 err = write_one_cache_group(trans, root, path, cache);
2623 BUG_ON(err);
2624 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002625 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002626
Chris Mason9078a3e2007-04-26 16:46:15 -04002627 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002628 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002629}
2630
Yan Zhengd2fb3432008-12-11 16:30:39 -05002631int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2632{
2633 struct btrfs_block_group_cache *block_group;
2634 int readonly = 0;
2635
2636 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2637 if (!block_group || block_group->ro)
2638 readonly = 1;
2639 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002640 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002641 return readonly;
2642}
2643
Chris Mason593060d2008-03-25 16:50:33 -04002644static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2645 u64 total_bytes, u64 bytes_used,
2646 struct btrfs_space_info **space_info)
2647{
2648 struct btrfs_space_info *found;
2649
2650 found = __find_space_info(info, flags);
2651 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002652 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002653 found->total_bytes += total_bytes;
2654 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002655 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002656 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002657 *space_info = found;
2658 return 0;
2659 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002660 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002661 if (!found)
2662 return -ENOMEM;
2663
Josef Bacik0f9dd462008-09-23 13:14:11 -04002664 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002665 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002666 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002667 found->flags = flags;
2668 found->total_bytes = total_bytes;
2669 found->bytes_used = bytes_used;
2670 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002671 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002672 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002673 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002674 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002675 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002676 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002677 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002678 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002679 return 0;
2680}
2681
Chris Mason8790d502008-04-03 16:29:03 -04002682static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2683{
2684 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002685 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002686 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002687 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002688 if (extra_flags) {
2689 if (flags & BTRFS_BLOCK_GROUP_DATA)
2690 fs_info->avail_data_alloc_bits |= extra_flags;
2691 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2692 fs_info->avail_metadata_alloc_bits |= extra_flags;
2693 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2694 fs_info->avail_system_alloc_bits |= extra_flags;
2695 }
2696}
Chris Mason593060d2008-03-25 16:50:33 -04002697
Yan Zhengc146afa2008-11-12 14:34:12 -05002698static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2699{
2700 spin_lock(&cache->space_info->lock);
2701 spin_lock(&cache->lock);
2702 if (!cache->ro) {
2703 cache->space_info->bytes_readonly += cache->key.offset -
2704 btrfs_block_group_used(&cache->item);
2705 cache->ro = 1;
2706 }
2707 spin_unlock(&cache->lock);
2708 spin_unlock(&cache->space_info->lock);
2709}
2710
Yan Zheng2b820322008-11-17 21:11:30 -05002711u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002712{
Yan Zheng2b820322008-11-17 21:11:30 -05002713 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002714
2715 if (num_devices == 1)
2716 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2717 if (num_devices < 4)
2718 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2719
Chris Masonec44a352008-04-28 15:29:52 -04002720 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2721 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002722 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002723 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002724 }
Chris Masonec44a352008-04-28 15:29:52 -04002725
2726 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002727 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002728 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002729 }
Chris Masonec44a352008-04-28 15:29:52 -04002730
2731 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2732 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2733 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2734 (flags & BTRFS_BLOCK_GROUP_DUP)))
2735 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2736 return flags;
2737}
2738
Josef Bacik6a632092009-02-20 11:00:09 -05002739static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2740{
2741 struct btrfs_fs_info *info = root->fs_info;
2742 u64 alloc_profile;
2743
2744 if (data) {
2745 alloc_profile = info->avail_data_alloc_bits &
2746 info->data_alloc_profile;
2747 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2748 } else if (root == root->fs_info->chunk_root) {
2749 alloc_profile = info->avail_system_alloc_bits &
2750 info->system_alloc_profile;
2751 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2752 } else {
2753 alloc_profile = info->avail_metadata_alloc_bits &
2754 info->metadata_alloc_profile;
2755 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2756 }
2757
2758 return btrfs_reduce_alloc_profile(root, data);
2759}
2760
2761void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2762{
2763 u64 alloc_target;
2764
2765 alloc_target = btrfs_get_alloc_profile(root, 1);
2766 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2767 alloc_target);
2768}
2769
Josef Bacik9ed74f22009-09-11 16:12:44 -04002770static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2771{
2772 u64 num_bytes;
2773 int level;
2774
2775 level = BTRFS_MAX_LEVEL - 2;
2776 /*
2777 * NOTE: these calculations are absolutely the worst possible case.
2778 * This assumes that _every_ item we insert will require a new leaf, and
2779 * that the tree has grown to its maximum level size.
2780 */
2781
2782 /*
2783 * for every item we insert we could insert both an extent item and a
2784 * extent ref item. Then for ever item we insert, we will need to cow
2785 * both the original leaf, plus the leaf to the left and right of it.
2786 *
2787 * Unless we are talking about the extent root, then we just want the
2788 * number of items * 2, since we just need the extent item plus its ref.
2789 */
2790 if (root == root->fs_info->extent_root)
2791 num_bytes = num_items * 2;
2792 else
2793 num_bytes = (num_items + (2 * num_items)) * 3;
2794
2795 /*
2796 * num_bytes is total number of leaves we could need times the leaf
2797 * size, and then for every leaf we could end up cow'ing 2 nodes per
2798 * level, down to the leaf level.
2799 */
2800 num_bytes = (num_bytes * root->leafsize) +
2801 (num_bytes * (level * 2)) * root->nodesize;
2802
2803 return num_bytes;
2804}
2805
Josef Bacik6a632092009-02-20 11:00:09 -05002806/*
Josef Bacik9ed74f22009-09-11 16:12:44 -04002807 * Unreserve metadata space for delalloc. If we have less reserved credits than
2808 * we have extents, this function does nothing.
Josef Bacik6a632092009-02-20 11:00:09 -05002809 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04002810int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2811 struct inode *inode, int num_items)
Josef Bacik6a632092009-02-20 11:00:09 -05002812{
2813 struct btrfs_fs_info *info = root->fs_info;
2814 struct btrfs_space_info *meta_sinfo;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002815 u64 num_bytes;
2816 u64 alloc_target;
2817 bool bug = false;
Josef Bacik6a632092009-02-20 11:00:09 -05002818
2819 /* get the space info for where the metadata will live */
2820 alloc_target = btrfs_get_alloc_profile(root, 0);
2821 meta_sinfo = __find_space_info(info, alloc_target);
2822
Josef Bacik9ed74f22009-09-11 16:12:44 -04002823 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2824 num_items);
2825
2826 spin_lock(&meta_sinfo->lock);
2827 if (BTRFS_I(inode)->delalloc_reserved_extents <=
2828 BTRFS_I(inode)->delalloc_extents) {
2829 spin_unlock(&meta_sinfo->lock);
2830 return 0;
2831 }
2832
2833 BTRFS_I(inode)->delalloc_reserved_extents--;
2834 BUG_ON(BTRFS_I(inode)->delalloc_reserved_extents < 0);
2835
2836 if (meta_sinfo->bytes_delalloc < num_bytes) {
2837 bug = true;
2838 meta_sinfo->bytes_delalloc = 0;
2839 } else {
2840 meta_sinfo->bytes_delalloc -= num_bytes;
2841 }
2842 spin_unlock(&meta_sinfo->lock);
2843
2844 BUG_ON(bug);
2845
2846 return 0;
2847}
2848
2849static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2850{
2851 u64 thresh;
2852
2853 thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2854 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2855 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2856 meta_sinfo->bytes_may_use;
2857
2858 thresh = meta_sinfo->total_bytes - thresh;
2859 thresh *= 80;
2860 do_div(thresh, 100);
2861 if (thresh <= meta_sinfo->bytes_delalloc)
2862 meta_sinfo->force_delalloc = 1;
2863 else
2864 meta_sinfo->force_delalloc = 0;
2865}
2866
2867static int maybe_allocate_chunk(struct btrfs_root *root,
2868 struct btrfs_space_info *info)
2869{
2870 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
2871 struct btrfs_trans_handle *trans;
2872 bool wait = false;
2873 int ret = 0;
2874 u64 min_metadata;
2875 u64 free_space;
2876
2877 free_space = btrfs_super_total_bytes(disk_super);
2878 /*
2879 * we allow the metadata to grow to a max of either 5gb or 5% of the
2880 * space in the volume.
2881 */
2882 min_metadata = min((u64)5 * 1024 * 1024 * 1024,
2883 div64_u64(free_space * 5, 100));
2884 if (info->total_bytes >= min_metadata) {
2885 spin_unlock(&info->lock);
2886 return 0;
2887 }
2888
2889 if (info->full) {
2890 spin_unlock(&info->lock);
2891 return 0;
2892 }
2893
2894 if (!info->allocating_chunk) {
2895 info->force_alloc = 1;
2896 info->allocating_chunk = 1;
2897 init_waitqueue_head(&info->wait);
2898 } else {
2899 wait = true;
2900 }
2901
2902 spin_unlock(&info->lock);
2903
2904 if (wait) {
2905 wait_event(info->wait,
2906 !info->allocating_chunk);
2907 return 1;
2908 }
2909
2910 trans = btrfs_start_transaction(root, 1);
2911 if (!trans) {
2912 ret = -ENOMEM;
2913 goto out;
2914 }
2915
2916 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2917 4096 + 2 * 1024 * 1024,
2918 info->flags, 0);
2919 btrfs_end_transaction(trans, root);
2920 if (ret)
2921 goto out;
2922out:
2923 spin_lock(&info->lock);
2924 info->allocating_chunk = 0;
2925 spin_unlock(&info->lock);
2926 wake_up(&info->wait);
2927
2928 if (ret)
2929 return 0;
2930 return 1;
2931}
2932
2933/*
2934 * Reserve metadata space for delalloc.
2935 */
2936int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
2937 struct inode *inode, int num_items)
2938{
2939 struct btrfs_fs_info *info = root->fs_info;
2940 struct btrfs_space_info *meta_sinfo;
2941 u64 num_bytes;
2942 u64 used;
2943 u64 alloc_target;
2944 int flushed = 0;
2945 int force_delalloc;
2946
2947 /* get the space info for where the metadata will live */
2948 alloc_target = btrfs_get_alloc_profile(root, 0);
2949 meta_sinfo = __find_space_info(info, alloc_target);
2950
2951 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2952 num_items);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002953again:
Josef Bacik6a632092009-02-20 11:00:09 -05002954 spin_lock(&meta_sinfo->lock);
Josef Bacik6a632092009-02-20 11:00:09 -05002955
Josef Bacik9ed74f22009-09-11 16:12:44 -04002956 force_delalloc = meta_sinfo->force_delalloc;
Josef Bacik6a632092009-02-20 11:00:09 -05002957
Josef Bacik9ed74f22009-09-11 16:12:44 -04002958 if (unlikely(!meta_sinfo->bytes_root))
2959 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
2960
2961 if (!flushed)
2962 meta_sinfo->bytes_delalloc += num_bytes;
2963
2964 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2965 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2966 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2967 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
2968
2969 if (used > meta_sinfo->total_bytes) {
2970 flushed++;
2971
2972 if (flushed == 1) {
2973 if (maybe_allocate_chunk(root, meta_sinfo))
2974 goto again;
2975 flushed++;
2976 } else {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002977 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002978 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002979
Josef Bacik9ed74f22009-09-11 16:12:44 -04002980 if (flushed == 2) {
2981 filemap_flush(inode->i_mapping);
2982 goto again;
2983 } else if (flushed == 3) {
2984 btrfs_start_delalloc_inodes(root);
2985 btrfs_wait_ordered_extents(root, 0);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002986 goto again;
2987 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04002988 spin_lock(&meta_sinfo->lock);
2989 meta_sinfo->bytes_delalloc -= num_bytes;
Josef Bacik6a632092009-02-20 11:00:09 -05002990 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002991 printk(KERN_ERR "enospc, has %d, reserved %d\n",
2992 BTRFS_I(inode)->delalloc_extents,
2993 BTRFS_I(inode)->delalloc_reserved_extents);
2994 dump_space_info(meta_sinfo, 0, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05002995 return -ENOSPC;
2996 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04002997
2998 BTRFS_I(inode)->delalloc_reserved_extents++;
2999 check_force_delalloc(meta_sinfo);
3000 spin_unlock(&meta_sinfo->lock);
3001
3002 if (!flushed && force_delalloc)
3003 filemap_flush(inode->i_mapping);
3004
3005 return 0;
3006}
3007
3008/*
3009 * unreserve num_items number of items worth of metadata space. This needs to
3010 * be paired with btrfs_reserve_metadata_space.
3011 *
3012 * NOTE: if you have the option, run this _AFTER_ you do a
3013 * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
3014 * oprations which will result in more used metadata, so we want to make sure we
3015 * can do that without issue.
3016 */
3017int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
3018{
3019 struct btrfs_fs_info *info = root->fs_info;
3020 struct btrfs_space_info *meta_sinfo;
3021 u64 num_bytes;
3022 u64 alloc_target;
3023 bool bug = false;
3024
3025 /* get the space info for where the metadata will live */
3026 alloc_target = btrfs_get_alloc_profile(root, 0);
3027 meta_sinfo = __find_space_info(info, alloc_target);
3028
3029 num_bytes = calculate_bytes_needed(root, num_items);
3030
3031 spin_lock(&meta_sinfo->lock);
3032 if (meta_sinfo->bytes_may_use < num_bytes) {
3033 bug = true;
3034 meta_sinfo->bytes_may_use = 0;
3035 } else {
3036 meta_sinfo->bytes_may_use -= num_bytes;
3037 }
3038 spin_unlock(&meta_sinfo->lock);
3039
3040 BUG_ON(bug);
3041
3042 return 0;
3043}
3044
3045/*
3046 * Reserve some metadata space for use. We'll calculate the worste case number
3047 * of bytes that would be needed to modify num_items number of items. If we
3048 * have space, fantastic, if not, you get -ENOSPC. Please call
3049 * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
3050 * items you reserved, since whatever metadata you needed should have already
3051 * been allocated.
3052 *
3053 * This will commit the transaction to make more space if we don't have enough
3054 * metadata space. THe only time we don't do this is if we're reserving space
3055 * inside of a transaction, then we will just return -ENOSPC and it is the
3056 * callers responsibility to handle it properly.
3057 */
3058int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3059{
3060 struct btrfs_fs_info *info = root->fs_info;
3061 struct btrfs_space_info *meta_sinfo;
3062 u64 num_bytes;
3063 u64 used;
3064 u64 alloc_target;
3065 int retries = 0;
3066
3067 /* get the space info for where the metadata will live */
3068 alloc_target = btrfs_get_alloc_profile(root, 0);
3069 meta_sinfo = __find_space_info(info, alloc_target);
3070
3071 num_bytes = calculate_bytes_needed(root, num_items);
3072again:
3073 spin_lock(&meta_sinfo->lock);
3074
3075 if (unlikely(!meta_sinfo->bytes_root))
3076 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3077
3078 if (!retries)
3079 meta_sinfo->bytes_may_use += num_bytes;
3080
3081 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3082 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3083 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3084 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3085
3086 if (used > meta_sinfo->total_bytes) {
3087 retries++;
3088 if (retries == 1) {
3089 if (maybe_allocate_chunk(root, meta_sinfo))
3090 goto again;
3091 retries++;
3092 } else {
3093 spin_unlock(&meta_sinfo->lock);
3094 }
3095
3096 if (retries == 2) {
3097 btrfs_start_delalloc_inodes(root);
3098 btrfs_wait_ordered_extents(root, 0);
3099 goto again;
3100 }
3101 spin_lock(&meta_sinfo->lock);
3102 meta_sinfo->bytes_may_use -= num_bytes;
3103 spin_unlock(&meta_sinfo->lock);
3104
3105 dump_space_info(meta_sinfo, 0, 0);
3106 return -ENOSPC;
3107 }
3108
3109 check_force_delalloc(meta_sinfo);
Josef Bacik6a632092009-02-20 11:00:09 -05003110 spin_unlock(&meta_sinfo->lock);
3111
3112 return 0;
3113}
3114
3115/*
3116 * This will check the space that the inode allocates from to make sure we have
3117 * enough space for bytes.
3118 */
3119int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3120 u64 bytes)
3121{
3122 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003123 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003124
3125 /* make sure bytes are sectorsize aligned */
3126 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3127
3128 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003129 if (!data_sinfo)
3130 goto alloc;
3131
Josef Bacik6a632092009-02-20 11:00:09 -05003132again:
3133 /* make sure we have enough space to handle the data first */
3134 spin_lock(&data_sinfo->lock);
3135 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
3136 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
3137 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
Josef Bacik1b2da372009-09-11 16:11:20 -04003138 data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003139 struct btrfs_trans_handle *trans;
3140
Josef Bacik6a632092009-02-20 11:00:09 -05003141 /*
3142 * if we don't have enough free bytes in this space then we need
3143 * to alloc a new chunk.
3144 */
3145 if (!data_sinfo->full) {
3146 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003147
3148 data_sinfo->force_alloc = 1;
3149 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003150alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003151 alloc_target = btrfs_get_alloc_profile(root, 1);
3152 trans = btrfs_start_transaction(root, 1);
3153 if (!trans)
3154 return -ENOMEM;
3155
3156 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3157 bytes + 2 * 1024 * 1024,
3158 alloc_target, 0);
3159 btrfs_end_transaction(trans, root);
3160 if (ret)
3161 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04003162
3163 if (!data_sinfo) {
3164 btrfs_set_inode_space_info(root, inode);
3165 data_sinfo = BTRFS_I(inode)->space_info;
3166 }
Josef Bacik6a632092009-02-20 11:00:09 -05003167 goto again;
3168 }
3169 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003170
3171 /* commit the current transaction and try again */
Sage Weildd7e0b72009-09-29 18:38:44 -04003172 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003173 committed = 1;
3174 trans = btrfs_join_transaction(root, 1);
3175 if (!trans)
3176 return -ENOMEM;
3177 ret = btrfs_commit_transaction(trans, root);
3178 if (ret)
3179 return ret;
3180 goto again;
3181 }
3182
Josef Bacik6a632092009-02-20 11:00:09 -05003183 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3184 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04003185 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07003186 "%llu total\n", (unsigned long long)bytes,
3187 (unsigned long long)data_sinfo->bytes_delalloc,
3188 (unsigned long long)data_sinfo->bytes_used,
3189 (unsigned long long)data_sinfo->bytes_reserved,
3190 (unsigned long long)data_sinfo->bytes_pinned,
3191 (unsigned long long)data_sinfo->bytes_readonly,
3192 (unsigned long long)data_sinfo->bytes_may_use,
3193 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003194 return -ENOSPC;
3195 }
3196 data_sinfo->bytes_may_use += bytes;
3197 BTRFS_I(inode)->reserved_bytes += bytes;
3198 spin_unlock(&data_sinfo->lock);
3199
Josef Bacik9ed74f22009-09-11 16:12:44 -04003200 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003201}
3202
3203/*
3204 * if there was an error for whatever reason after calling
3205 * btrfs_check_data_free_space, call this so we can cleanup the counters.
3206 */
3207void btrfs_free_reserved_data_space(struct btrfs_root *root,
3208 struct inode *inode, u64 bytes)
3209{
3210 struct btrfs_space_info *data_sinfo;
3211
3212 /* make sure bytes are sectorsize aligned */
3213 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3214
3215 data_sinfo = BTRFS_I(inode)->space_info;
3216 spin_lock(&data_sinfo->lock);
3217 data_sinfo->bytes_may_use -= bytes;
3218 BTRFS_I(inode)->reserved_bytes -= bytes;
3219 spin_unlock(&data_sinfo->lock);
3220}
3221
3222/* called when we are adding a delalloc extent to the inode's io_tree */
3223void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3224 u64 bytes)
3225{
3226 struct btrfs_space_info *data_sinfo;
3227
3228 /* get the space info for where this inode will be storing its data */
3229 data_sinfo = BTRFS_I(inode)->space_info;
3230
3231 /* make sure we have enough space to handle the data first */
3232 spin_lock(&data_sinfo->lock);
3233 data_sinfo->bytes_delalloc += bytes;
3234
3235 /*
3236 * we are adding a delalloc extent without calling
3237 * btrfs_check_data_free_space first. This happens on a weird
3238 * writepage condition, but shouldn't hurt our accounting
3239 */
3240 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3241 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3242 BTRFS_I(inode)->reserved_bytes = 0;
3243 } else {
3244 data_sinfo->bytes_may_use -= bytes;
3245 BTRFS_I(inode)->reserved_bytes -= bytes;
3246 }
3247
3248 spin_unlock(&data_sinfo->lock);
3249}
3250
3251/* called when we are clearing an delalloc extent from the inode's io_tree */
3252void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3253 u64 bytes)
3254{
3255 struct btrfs_space_info *info;
3256
3257 info = BTRFS_I(inode)->space_info;
3258
3259 spin_lock(&info->lock);
3260 info->bytes_delalloc -= bytes;
3261 spin_unlock(&info->lock);
3262}
3263
Josef Bacik97e728d2009-04-21 17:40:57 -04003264static void force_metadata_allocation(struct btrfs_fs_info *info)
3265{
3266 struct list_head *head = &info->space_info;
3267 struct btrfs_space_info *found;
3268
3269 rcu_read_lock();
3270 list_for_each_entry_rcu(found, head, list) {
3271 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3272 found->force_alloc = 1;
3273 }
3274 rcu_read_unlock();
3275}
3276
Chris Mason6324fbf2008-03-24 15:01:59 -04003277static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3278 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003279 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003280{
3281 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003282 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003283 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05003284 int ret = 0;
3285
Josef Bacik97e728d2009-04-21 17:40:57 -04003286 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003287
Yan Zheng2b820322008-11-17 21:11:30 -05003288 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003289
Chris Mason6324fbf2008-03-24 15:01:59 -04003290 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003291 if (!space_info) {
3292 ret = update_space_info(extent_root->fs_info, flags,
3293 0, 0, &space_info);
3294 BUG_ON(ret);
3295 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003296 BUG_ON(!space_info);
3297
Josef Bacik25179202008-10-29 14:49:05 -04003298 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003299 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003300 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003301 if (space_info->full) {
3302 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003303 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003304 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003305
Yan Zhengc146afa2008-11-12 14:34:12 -05003306 thresh = space_info->total_bytes - space_info->bytes_readonly;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003307 thresh = div_factor(thresh, 8);
Chris Mason0ef3e662008-05-24 14:04:53 -04003308 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04003309 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04003310 space_info->bytes_reserved + alloc_bytes) < thresh) {
3311 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003312 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003313 }
Josef Bacik25179202008-10-29 14:49:05 -04003314 spin_unlock(&space_info->lock);
3315
Josef Bacik97e728d2009-04-21 17:40:57 -04003316 /*
3317 * if we're doing a data chunk, go ahead and make sure that
3318 * we keep a reasonable number of metadata chunks allocated in the
3319 * FS as well.
3320 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003321 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003322 fs_info->data_chunk_allocations++;
3323 if (!(fs_info->data_chunk_allocations %
3324 fs_info->metadata_ratio))
3325 force_metadata_allocation(fs_info);
3326 }
3327
Yan Zheng2b820322008-11-17 21:11:30 -05003328 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003329 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003330 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003331 space_info->full = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003332 space_info->force_alloc = 0;
3333 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003334out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003335 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003336 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003337}
3338
Chris Mason9078a3e2007-04-26 16:46:15 -04003339static int update_block_group(struct btrfs_trans_handle *trans,
3340 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04003341 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04003342 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04003343{
3344 struct btrfs_block_group_cache *cache;
3345 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04003346 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003347 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003348 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04003349
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003350 /* block accounting for super block */
3351 spin_lock(&info->delalloc_lock);
3352 old_val = btrfs_super_bytes_used(&info->super_copy);
3353 if (alloc)
3354 old_val += num_bytes;
3355 else
3356 old_val -= num_bytes;
3357 btrfs_set_super_bytes_used(&info->super_copy, old_val);
3358
3359 /* block accounting for root item */
3360 old_val = btrfs_root_used(&root->root_item);
3361 if (alloc)
3362 old_val += num_bytes;
3363 else
3364 old_val -= num_bytes;
3365 btrfs_set_root_used(&root->root_item, old_val);
3366 spin_unlock(&info->delalloc_lock);
3367
Chris Masond3977122009-01-05 21:25:51 -05003368 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003369 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003370 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04003371 return -1;
Chris Masondb945352007-10-15 16:15:53 -04003372 byte_in_group = bytenr - cache->key.objectid;
3373 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003374
Josef Bacik25179202008-10-29 14:49:05 -04003375 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003376 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003377 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04003378 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04003379 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04003380 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04003381 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003382 btrfs_set_block_group_used(&cache->item, old_val);
3383 cache->reserved -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003384 cache->space_info->bytes_used += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003385 cache->space_info->bytes_reserved -= num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003386 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05003387 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003388 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003389 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003390 } else {
Chris Masondb945352007-10-15 16:15:53 -04003391 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003392 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05003393 if (cache->ro)
3394 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003395 btrfs_set_block_group_used(&cache->item, old_val);
3396 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003397 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04003398 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003399 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003400
3401 ret = btrfs_discard_extent(root, bytenr,
3402 num_bytes);
3403 WARN_ON(ret);
3404
Josef Bacik0f9dd462008-09-23 13:14:11 -04003405 ret = btrfs_add_free_space(cache, bytenr,
3406 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003407 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04003408 }
Chris Masoncd1bc462007-04-27 10:08:34 -04003409 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003410 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003411 total -= num_bytes;
3412 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003413 }
3414 return 0;
3415}
Chris Mason6324fbf2008-03-24 15:01:59 -04003416
Chris Masona061fc82008-05-07 11:43:44 -04003417static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3418{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003419 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003420 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003421
3422 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3423 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003424 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003425
Yan Zhengd2fb3432008-12-11 16:30:39 -05003426 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003427 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003428
3429 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003430}
3431
Yan Zheng11833d62009-09-11 16:11:19 -04003432/*
3433 * this function must be called within transaction
3434 */
3435int btrfs_pin_extent(struct btrfs_root *root,
3436 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05003437{
Yan324ae4d2007-11-16 14:57:08 -05003438 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -04003439 struct btrfs_block_group_cache *cache;
Yan324ae4d2007-11-16 14:57:08 -05003440
Yan Zheng11833d62009-09-11 16:11:19 -04003441 cache = btrfs_lookup_block_group(fs_info, bytenr);
3442 BUG_ON(!cache);
Chris Masonb9473432009-03-13 11:00:37 -04003443
Yan Zheng11833d62009-09-11 16:11:19 -04003444 spin_lock(&cache->space_info->lock);
3445 spin_lock(&cache->lock);
3446 cache->pinned += num_bytes;
3447 cache->space_info->bytes_pinned += num_bytes;
3448 if (reserved) {
3449 cache->reserved -= num_bytes;
3450 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05003451 }
Yan Zheng11833d62009-09-11 16:11:19 -04003452 spin_unlock(&cache->lock);
3453 spin_unlock(&cache->space_info->lock);
3454
3455 btrfs_put_block_group(cache);
3456
3457 set_extent_dirty(fs_info->pinned_extents,
3458 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Yan324ae4d2007-11-16 14:57:08 -05003459 return 0;
3460}
Chris Mason9078a3e2007-04-26 16:46:15 -04003461
Yan Zheng11833d62009-09-11 16:11:19 -04003462static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3463 u64 num_bytes, int reserve)
Zheng Yane8569812008-09-26 10:05:48 -04003464{
Yan Zheng11833d62009-09-11 16:11:19 -04003465 spin_lock(&cache->space_info->lock);
3466 spin_lock(&cache->lock);
3467 if (reserve) {
3468 cache->reserved += num_bytes;
3469 cache->space_info->bytes_reserved += num_bytes;
3470 } else {
3471 cache->reserved -= num_bytes;
3472 cache->space_info->bytes_reserved -= num_bytes;
3473 }
3474 spin_unlock(&cache->lock);
3475 spin_unlock(&cache->space_info->lock);
3476 return 0;
3477}
Zheng Yane8569812008-09-26 10:05:48 -04003478
Yan Zheng11833d62009-09-11 16:11:19 -04003479int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3480 struct btrfs_root *root)
3481{
3482 struct btrfs_fs_info *fs_info = root->fs_info;
3483 struct btrfs_caching_control *next;
3484 struct btrfs_caching_control *caching_ctl;
3485 struct btrfs_block_group_cache *cache;
3486
3487 down_write(&fs_info->extent_commit_sem);
3488
3489 list_for_each_entry_safe(caching_ctl, next,
3490 &fs_info->caching_block_groups, list) {
3491 cache = caching_ctl->block_group;
3492 if (block_group_cache_done(cache)) {
3493 cache->last_byte_to_unpin = (u64)-1;
3494 list_del_init(&caching_ctl->list);
3495 put_caching_control(caching_ctl);
3496 } else {
3497 cache->last_byte_to_unpin = caching_ctl->progress;
3498 }
3499 }
3500
3501 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3502 fs_info->pinned_extents = &fs_info->freed_extents[1];
3503 else
3504 fs_info->pinned_extents = &fs_info->freed_extents[0];
3505
3506 up_write(&fs_info->extent_commit_sem);
3507 return 0;
3508}
3509
3510static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3511{
3512 struct btrfs_fs_info *fs_info = root->fs_info;
3513 struct btrfs_block_group_cache *cache = NULL;
3514 u64 len;
3515
3516 while (start <= end) {
3517 if (!cache ||
3518 start >= cache->key.objectid + cache->key.offset) {
3519 if (cache)
3520 btrfs_put_block_group(cache);
3521 cache = btrfs_lookup_block_group(fs_info, start);
3522 BUG_ON(!cache);
3523 }
3524
3525 len = cache->key.objectid + cache->key.offset - start;
3526 len = min(len, end + 1 - start);
3527
3528 if (start < cache->last_byte_to_unpin) {
3529 len = min(len, cache->last_byte_to_unpin - start);
3530 btrfs_add_free_space(cache, start, len);
3531 }
Josef Bacik25179202008-10-29 14:49:05 -04003532
3533 spin_lock(&cache->space_info->lock);
3534 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003535 cache->pinned -= len;
3536 cache->space_info->bytes_pinned -= len;
Josef Bacik25179202008-10-29 14:49:05 -04003537 spin_unlock(&cache->lock);
3538 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003539
3540 start += len;
3541 }
3542
3543 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003544 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04003545 return 0;
3546}
3547
3548int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04003549 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05003550{
Yan Zheng11833d62009-09-11 16:11:19 -04003551 struct btrfs_fs_info *fs_info = root->fs_info;
3552 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04003553 u64 start;
3554 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003555 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003556
Yan Zheng11833d62009-09-11 16:11:19 -04003557 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3558 unpin = &fs_info->freed_extents[1];
3559 else
3560 unpin = &fs_info->freed_extents[0];
3561
Chris Masond3977122009-01-05 21:25:51 -05003562 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003563 ret = find_first_extent_bit(unpin, 0, &start, &end,
3564 EXTENT_DIRTY);
3565 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003566 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003567
3568 ret = btrfs_discard_extent(root, start, end + 1 - start);
3569
Chris Mason1a5bc162007-10-15 16:15:26 -04003570 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04003571 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04003572 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003573 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003574
Liu Hui1f3c79a2009-01-05 15:57:51 -05003575 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003576}
3577
Zheng Yan31840ae2008-09-23 13:14:14 -04003578static int pin_down_bytes(struct btrfs_trans_handle *trans,
3579 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003580 struct btrfs_path *path,
Yan Zheng11833d62009-09-11 16:11:19 -04003581 u64 bytenr, u64 num_bytes,
3582 int is_data, int reserved,
Chris Masonb9473432009-03-13 11:00:37 -04003583 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003584{
Chris Mason1a5bc162007-10-15 16:15:26 -04003585 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003586 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003587
Zheng Yan31840ae2008-09-23 13:14:14 -04003588 if (is_data)
3589 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003590
Zheng Yan31840ae2008-09-23 13:14:14 -04003591 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3592 if (!buf)
3593 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003594
Zheng Yan31840ae2008-09-23 13:14:14 -04003595 /* we can reuse a block if it hasn't been written
3596 * and it is from this transaction. We can't
3597 * reuse anything from the tree log root because
3598 * it has tiny sub-transactions.
3599 */
3600 if (btrfs_buffer_uptodate(buf, 0) &&
3601 btrfs_try_tree_lock(buf)) {
3602 u64 header_owner = btrfs_header_owner(buf);
3603 u64 header_transid = btrfs_header_generation(buf);
3604 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3605 header_transid == trans->transid &&
3606 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003607 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003608 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003609 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003610 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003611 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003612 free_extent_buffer(buf);
3613pinit:
Yan Zheng11833d62009-09-11 16:11:19 -04003614 if (path)
3615 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003616 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003617 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
Zheng Yan31840ae2008-09-23 13:14:14 -04003618
Chris Masonbe744172007-05-06 10:15:01 -04003619 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003620 return 0;
3621}
3622
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003623static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3624 struct btrfs_root *root,
3625 u64 bytenr, u64 num_bytes, u64 parent,
3626 u64 root_objectid, u64 owner_objectid,
3627 u64 owner_offset, int refs_to_drop,
3628 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003629{
Chris Masone2fa7222007-03-12 16:22:34 -04003630 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003631 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003632 struct btrfs_fs_info *info = root->fs_info;
3633 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003634 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003635 struct btrfs_extent_item *ei;
3636 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003637 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003638 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003639 int extent_slot = 0;
3640 int found_extent = 0;
3641 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003642 u32 item_size;
3643 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003644
Chris Mason5caf2a02007-04-02 11:20:42 -04003645 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003646 if (!path)
3647 return -ENOMEM;
3648
Chris Mason3c12ac72008-04-21 12:01:38 -04003649 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003650 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003651
3652 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3653 BUG_ON(!is_data && refs_to_drop != 1);
3654
3655 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3656 bytenr, num_bytes, parent,
3657 root_objectid, owner_objectid,
3658 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003659 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003660 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003661 while (extent_slot >= 0) {
3662 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003663 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003664 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003665 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003666 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3667 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003668 found_extent = 1;
3669 break;
3670 }
3671 if (path->slots[0] - extent_slot > 5)
3672 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003673 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003674 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003675#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3676 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3677 if (found_extent && item_size < sizeof(*ei))
3678 found_extent = 0;
3679#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003680 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003681 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003682 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003683 NULL, refs_to_drop,
3684 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003685 BUG_ON(ret);
3686 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003687 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003688
3689 key.objectid = bytenr;
3690 key.type = BTRFS_EXTENT_ITEM_KEY;
3691 key.offset = num_bytes;
3692
Zheng Yan31840ae2008-09-23 13:14:14 -04003693 ret = btrfs_search_slot(trans, extent_root,
3694 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003695 if (ret) {
3696 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003697 ", was looking for %llu\n", ret,
3698 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003699 btrfs_print_leaf(extent_root, path->nodes[0]);
3700 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003701 BUG_ON(ret);
3702 extent_slot = path->slots[0];
3703 }
Chris Mason7bb86312007-12-11 09:25:06 -05003704 } else {
3705 btrfs_print_leaf(extent_root, path->nodes[0]);
3706 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003707 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003708 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003709 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003710 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003711 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003712 (unsigned long long)owner_objectid,
3713 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003714 }
Chris Mason5f39d392007-10-15 16:14:19 -04003715
3716 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003717 item_size = btrfs_item_size_nr(leaf, extent_slot);
3718#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3719 if (item_size < sizeof(*ei)) {
3720 BUG_ON(found_extent || extent_slot != path->slots[0]);
3721 ret = convert_extent_item_v0(trans, extent_root, path,
3722 owner_objectid, 0);
3723 BUG_ON(ret < 0);
3724
3725 btrfs_release_path(extent_root, path);
3726 path->leave_spinning = 1;
3727
3728 key.objectid = bytenr;
3729 key.type = BTRFS_EXTENT_ITEM_KEY;
3730 key.offset = num_bytes;
3731
3732 ret = btrfs_search_slot(trans, extent_root, &key, path,
3733 -1, 1);
3734 if (ret) {
3735 printk(KERN_ERR "umm, got %d back from search"
3736 ", was looking for %llu\n", ret,
3737 (unsigned long long)bytenr);
3738 btrfs_print_leaf(extent_root, path->nodes[0]);
3739 }
3740 BUG_ON(ret);
3741 extent_slot = path->slots[0];
3742 leaf = path->nodes[0];
3743 item_size = btrfs_item_size_nr(leaf, extent_slot);
3744 }
3745#endif
3746 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003747 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003748 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003749 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3750 struct btrfs_tree_block_info *bi;
3751 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3752 bi = (struct btrfs_tree_block_info *)(ei + 1);
3753 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003754 }
3755
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003756 refs = btrfs_extent_refs(leaf, ei);
3757 BUG_ON(refs < refs_to_drop);
3758 refs -= refs_to_drop;
3759
3760 if (refs > 0) {
3761 if (extent_op)
3762 __run_delayed_extent_op(extent_op, leaf, ei);
3763 /*
3764 * In the case of inline back ref, reference count will
3765 * be updated by remove_extent_backref
3766 */
3767 if (iref) {
3768 BUG_ON(!found_extent);
3769 } else {
3770 btrfs_set_extent_refs(leaf, ei, refs);
3771 btrfs_mark_buffer_dirty(leaf);
3772 }
3773 if (found_extent) {
3774 ret = remove_extent_backref(trans, extent_root, path,
3775 iref, refs_to_drop,
3776 is_data);
3777 BUG_ON(ret);
3778 }
3779 } else {
3780 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003781 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003782
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003783 if (found_extent) {
3784 BUG_ON(is_data && refs_to_drop !=
3785 extent_data_ref_count(root, path, iref));
3786 if (iref) {
3787 BUG_ON(path->slots[0] != extent_slot);
3788 } else {
3789 BUG_ON(path->slots[0] != extent_slot + 1);
3790 path->slots[0] = extent_slot;
3791 num_to_del = 2;
3792 }
Chris Mason78fae272007-03-25 11:35:08 -04003793 }
Chris Masonb9473432009-03-13 11:00:37 -04003794
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003795 ret = pin_down_bytes(trans, root, path, bytenr,
Yan Zheng11833d62009-09-11 16:11:19 -04003796 num_bytes, is_data, 0, &must_clean);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003797 if (ret > 0)
3798 mark_free = 1;
3799 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003800 /*
3801 * it is going to be very rare for someone to be waiting
3802 * on the block we're freeing. del_items might need to
3803 * schedule, so rather than get fancy, just force it
3804 * to blocking here
3805 */
3806 if (must_clean)
3807 btrfs_set_lock_blocking(must_clean);
3808
Chris Mason952fcca2008-02-18 16:33:44 -05003809 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3810 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003811 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003812 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003813
Chris Masonb9473432009-03-13 11:00:37 -04003814 if (must_clean) {
3815 clean_tree_block(NULL, root, must_clean);
3816 btrfs_tree_unlock(must_clean);
3817 free_extent_buffer(must_clean);
3818 }
3819
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003820 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003821 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3822 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003823 } else {
3824 invalidate_mapping_pages(info->btree_inode->i_mapping,
3825 bytenr >> PAGE_CACHE_SHIFT,
3826 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003827 }
3828
Chris Masondcbdd4d2008-12-16 13:51:01 -05003829 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3830 mark_free);
3831 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003832 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003833 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003834 return ret;
3835}
3836
3837/*
Chris Mason1887be62009-03-13 10:11:24 -04003838 * when we free an extent, it is possible (and likely) that we free the last
3839 * delayed ref for that extent as well. This searches the delayed ref tree for
3840 * a given extent, and if there are no other delayed refs to be processed, it
3841 * removes it from the tree.
3842 */
3843static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3844 struct btrfs_root *root, u64 bytenr)
3845{
3846 struct btrfs_delayed_ref_head *head;
3847 struct btrfs_delayed_ref_root *delayed_refs;
3848 struct btrfs_delayed_ref_node *ref;
3849 struct rb_node *node;
3850 int ret;
3851
3852 delayed_refs = &trans->transaction->delayed_refs;
3853 spin_lock(&delayed_refs->lock);
3854 head = btrfs_find_delayed_ref_head(trans, bytenr);
3855 if (!head)
3856 goto out;
3857
3858 node = rb_prev(&head->node.rb_node);
3859 if (!node)
3860 goto out;
3861
3862 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3863
3864 /* there are still entries for this ref, we can't drop it */
3865 if (ref->bytenr == bytenr)
3866 goto out;
3867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003868 if (head->extent_op) {
3869 if (!head->must_insert_reserved)
3870 goto out;
3871 kfree(head->extent_op);
3872 head->extent_op = NULL;
3873 }
3874
Chris Mason1887be62009-03-13 10:11:24 -04003875 /*
3876 * waiting for the lock here would deadlock. If someone else has it
3877 * locked they are already in the process of dropping it anyway
3878 */
3879 if (!mutex_trylock(&head->mutex))
3880 goto out;
3881
3882 /*
3883 * at this point we have a head with no other entries. Go
3884 * ahead and process it.
3885 */
3886 head->node.in_tree = 0;
3887 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04003888
Chris Mason1887be62009-03-13 10:11:24 -04003889 delayed_refs->num_entries--;
3890
3891 /*
3892 * we don't take a ref on the node because we're removing it from the
3893 * tree, so we just steal the ref the tree was holding.
3894 */
Chris Masonc3e69d52009-03-13 10:17:05 -04003895 delayed_refs->num_heads--;
3896 if (list_empty(&head->cluster))
3897 delayed_refs->num_heads_ready--;
3898
3899 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04003900 spin_unlock(&delayed_refs->lock);
3901
3902 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003903 &head->node, head->extent_op,
3904 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04003905 BUG_ON(ret);
3906 btrfs_put_delayed_ref(&head->node);
3907 return 0;
3908out:
3909 spin_unlock(&delayed_refs->lock);
3910 return 0;
3911}
3912
Chris Mason925baed2008-06-25 16:01:30 -04003913int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003914 struct btrfs_root *root,
3915 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003916 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04003917{
3918 int ret;
3919
Chris Mason56bec292009-03-13 10:10:06 -04003920 /*
3921 * tree log blocks never actually go into the extent allocation
3922 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003923 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003924 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
3925 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04003926 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003927 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04003928 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003929 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
3930 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
3931 parent, root_objectid, (int)owner,
3932 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04003933 BUG_ON(ret);
3934 ret = check_ref_cleanup(trans, root, bytenr);
3935 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003936 } else {
3937 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
3938 parent, root_objectid, owner,
3939 offset, BTRFS_DROP_DELAYED_REF, NULL);
3940 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04003941 }
Chris Mason925baed2008-06-25 16:01:30 -04003942 return ret;
3943}
3944
Chris Mason87ee04e2007-11-30 11:30:34 -05003945static u64 stripe_align(struct btrfs_root *root, u64 val)
3946{
3947 u64 mask = ((u64)root->stripesize - 1);
3948 u64 ret = (val + mask) & ~mask;
3949 return ret;
3950}
3951
Chris Masonfec577f2007-02-26 10:40:21 -05003952/*
Josef Bacik817d52f2009-07-13 21:29:25 -04003953 * when we wait for progress in the block group caching, its because
3954 * our allocation attempt failed at least once. So, we must sleep
3955 * and let some progress happen before we try again.
3956 *
3957 * This function will sleep at least once waiting for new free space to
3958 * show up, and then it will check the block group free space numbers
3959 * for our min num_bytes. Another option is to have it go ahead
3960 * and look in the rbtree for a free extent of a given size, but this
3961 * is a good start.
3962 */
3963static noinline int
3964wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
3965 u64 num_bytes)
3966{
Yan Zheng11833d62009-09-11 16:11:19 -04003967 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04003968 DEFINE_WAIT(wait);
3969
Yan Zheng11833d62009-09-11 16:11:19 -04003970 caching_ctl = get_caching_control(cache);
3971 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04003972 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04003973
Yan Zheng11833d62009-09-11 16:11:19 -04003974 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04003975 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04003976
3977 put_caching_control(caching_ctl);
3978 return 0;
3979}
3980
3981static noinline int
3982wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
3983{
3984 struct btrfs_caching_control *caching_ctl;
3985 DEFINE_WAIT(wait);
3986
3987 caching_ctl = get_caching_control(cache);
3988 if (!caching_ctl)
3989 return 0;
3990
3991 wait_event(caching_ctl->wait, block_group_cache_done(cache));
3992
3993 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04003994 return 0;
3995}
3996
3997enum btrfs_loop_type {
3998 LOOP_CACHED_ONLY = 0,
3999 LOOP_CACHING_NOWAIT = 1,
4000 LOOP_CACHING_WAIT = 2,
4001 LOOP_ALLOC_CHUNK = 3,
4002 LOOP_NO_EMPTY_SIZE = 4,
4003};
4004
4005/*
Chris Masonfec577f2007-02-26 10:40:21 -05004006 * walks the btree of allocated extents and find a hole of a given size.
4007 * The key ins is changed to record the hole:
4008 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004009 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004010 * ins->offset == number of blocks
4011 * Any available blocks before search_start are skipped.
4012 */
Chris Masond3977122009-01-05 21:25:51 -05004013static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004014 struct btrfs_root *orig_root,
4015 u64 num_bytes, u64 empty_size,
4016 u64 search_start, u64 search_end,
4017 u64 hint_byte, struct btrfs_key *ins,
4018 u64 exclude_start, u64 exclude_nr,
4019 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004020{
Josef Bacik80eb2342008-10-29 14:49:05 -04004021 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004022 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004023 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004024 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004025 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004026 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004027 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004028 int last_ptr_loop = 0;
4029 int loop = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004030 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004031 bool failed_cluster_refill = false;
Chris Masonfec577f2007-02-26 10:40:21 -05004032
Chris Masondb945352007-10-15 16:15:53 -04004033 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004034 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004035 ins->objectid = 0;
4036 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004037
Josef Bacik2552d172009-04-03 10:14:19 -04004038 space_info = __find_space_info(root->fs_info, data);
4039
Chris Mason0ef3e662008-05-24 14:04:53 -04004040 if (orig_root->ref_cows || empty_size)
4041 allowed_chunk_alloc = 1;
4042
Chris Mason239b14b2008-03-24 15:02:07 -04004043 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004044 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004045 if (!btrfs_test_opt(root, SSD))
4046 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004047 }
4048
Chris Masonfa9c0d72009-04-03 09:47:43 -04004049 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4050 last_ptr = &root->fs_info->data_alloc_cluster;
4051 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004052
Chris Mason239b14b2008-03-24 15:02:07 -04004053 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004054 spin_lock(&last_ptr->lock);
4055 if (last_ptr->block_group)
4056 hint_byte = last_ptr->window_start;
4057 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004058 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004059
Chris Masona061fc82008-05-07 11:43:44 -04004060 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004061 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004062
Josef Bacik817d52f2009-07-13 21:29:25 -04004063 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004064 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004065
Josef Bacik2552d172009-04-03 10:14:19 -04004066 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04004067 block_group = btrfs_lookup_block_group(root->fs_info,
4068 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004069 /*
4070 * we don't want to use the block group if it doesn't match our
4071 * allocation bits, or if its not cached.
4072 */
4073 if (block_group && block_group_bits(block_group, data) &&
4074 block_group_cache_done(block_group)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004075 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004076 if (list_empty(&block_group->list) ||
4077 block_group->ro) {
4078 /*
4079 * someone is removing this block group,
4080 * we can't jump into the have_block_group
4081 * target because our list pointers are not
4082 * valid
4083 */
4084 btrfs_put_block_group(block_group);
4085 up_read(&space_info->groups_sem);
4086 } else
4087 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04004088 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004089 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004090 }
Chris Mason42e70e72008-11-07 18:17:11 -05004091 }
Chris Mason43662112008-11-07 09:06:11 -05004092
Josef Bacik2552d172009-04-03 10:14:19 -04004093search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004094 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04004095 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004096 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004097 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004098
Josef Bacik2552d172009-04-03 10:14:19 -04004099 atomic_inc(&block_group->count);
4100 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004101
Josef Bacik2552d172009-04-03 10:14:19 -04004102have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004103 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
4104 /*
4105 * we want to start caching kthreads, but not too many
4106 * right off the bat so we don't overwhelm the system,
4107 * so only start them if there are less than 2 and we're
4108 * in the initial allocation phase.
4109 */
4110 if (loop > LOOP_CACHING_NOWAIT ||
4111 atomic_read(&space_info->caching_threads) < 2) {
4112 ret = cache_block_group(block_group);
4113 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004114 }
Josef Bacikea6a4782008-11-20 12:16:16 -05004115 }
4116
Josef Bacik817d52f2009-07-13 21:29:25 -04004117 cached = block_group_cache_done(block_group);
4118 if (unlikely(!cached)) {
4119 found_uncached_bg = true;
4120
4121 /* if we only want cached bgs, loop */
4122 if (loop == LOOP_CACHED_ONLY)
4123 goto loop;
4124 }
4125
Josef Bacikea6a4782008-11-20 12:16:16 -05004126 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004127 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004128
Josef Bacik0a243252009-09-11 16:11:20 -04004129 /*
4130 * Ok we want to try and use the cluster allocator, so lets look
4131 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4132 * have tried the cluster allocator plenty of times at this
4133 * point and not have found anything, so we are likely way too
4134 * fragmented for the clustering stuff to find anything, so lets
4135 * just skip it and let the allocator find whatever block it can
4136 * find
4137 */
4138 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004139 /*
4140 * the refill lock keeps out other
4141 * people trying to start a new cluster
4142 */
4143 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004144 if (last_ptr->block_group &&
4145 (last_ptr->block_group->ro ||
4146 !block_group_bits(last_ptr->block_group, data))) {
4147 offset = 0;
4148 goto refill_cluster;
4149 }
4150
Chris Masonfa9c0d72009-04-03 09:47:43 -04004151 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4152 num_bytes, search_start);
4153 if (offset) {
4154 /* we have a block, we're done */
4155 spin_unlock(&last_ptr->refill_lock);
4156 goto checks;
4157 }
4158
4159 spin_lock(&last_ptr->lock);
4160 /*
4161 * whoops, this cluster doesn't actually point to
4162 * this block group. Get a ref on the block
4163 * group is does point to and try again
4164 */
4165 if (!last_ptr_loop && last_ptr->block_group &&
4166 last_ptr->block_group != block_group) {
4167
4168 btrfs_put_block_group(block_group);
4169 block_group = last_ptr->block_group;
4170 atomic_inc(&block_group->count);
4171 spin_unlock(&last_ptr->lock);
4172 spin_unlock(&last_ptr->refill_lock);
4173
4174 last_ptr_loop = 1;
4175 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04004176 /*
4177 * we know this block group is properly
4178 * in the list because
4179 * btrfs_remove_block_group, drops the
4180 * cluster before it removes the block
4181 * group from the list
4182 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04004183 goto have_block_group;
4184 }
4185 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004186refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04004187 /*
4188 * this cluster didn't work out, free it and
4189 * start over
4190 */
4191 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4192
4193 last_ptr_loop = 0;
4194
4195 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04004196 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04004197 block_group, last_ptr,
4198 offset, num_bytes,
4199 empty_cluster + empty_size);
4200 if (ret == 0) {
4201 /*
4202 * now pull our allocation out of this
4203 * cluster
4204 */
4205 offset = btrfs_alloc_from_cluster(block_group,
4206 last_ptr, num_bytes,
4207 search_start);
4208 if (offset) {
4209 /* we found one, proceed */
4210 spin_unlock(&last_ptr->refill_lock);
4211 goto checks;
4212 }
Josef Bacik0a243252009-09-11 16:11:20 -04004213 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4214 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004215 spin_unlock(&last_ptr->refill_lock);
4216
Josef Bacik0a243252009-09-11 16:11:20 -04004217 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004218 wait_block_group_cache_progress(block_group,
4219 num_bytes + empty_cluster + empty_size);
4220 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004221 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004222
Chris Masonfa9c0d72009-04-03 09:47:43 -04004223 /*
4224 * at this point we either didn't find a cluster
4225 * or we weren't able to allocate a block from our
4226 * cluster. Free the cluster we've been trying
4227 * to use, and go to the next block group
4228 */
Josef Bacik0a243252009-09-11 16:11:20 -04004229 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004230 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04004231 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004232 }
4233
Josef Bacik6226cb02009-04-03 10:14:18 -04004234 offset = btrfs_find_space_for_alloc(block_group, search_start,
4235 num_bytes, empty_size);
Josef Bacik817d52f2009-07-13 21:29:25 -04004236 if (!offset && (cached || (!cached &&
4237 loop == LOOP_CACHING_NOWAIT))) {
Josef Bacik2552d172009-04-03 10:14:19 -04004238 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04004239 } else if (!offset && (!cached &&
4240 loop > LOOP_CACHING_NOWAIT)) {
4241 wait_block_group_cache_progress(block_group,
4242 num_bytes + empty_size);
4243 goto have_block_group;
4244 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004245checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04004246 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04004247 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04004248 if (search_start + num_bytes >= search_end) {
4249 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004250 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004251 }
Chris Masone37c9e62007-05-09 20:13:14 -04004252
Josef Bacik2552d172009-04-03 10:14:19 -04004253 /* move on to the next group */
4254 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04004255 block_group->key.objectid + block_group->key.offset) {
4256 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004257 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004258 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004259
Josef Bacik2552d172009-04-03 10:14:19 -04004260 if (exclude_nr > 0 &&
4261 (search_start + num_bytes > exclude_start &&
4262 search_start < exclude_start + exclude_nr)) {
4263 search_start = exclude_start + exclude_nr;
4264
Josef Bacik6226cb02009-04-03 10:14:18 -04004265 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004266 /*
4267 * if search_start is still in this block group
4268 * then we just re-search this block group
4269 */
4270 if (search_start >= block_group->key.objectid &&
4271 search_start < (block_group->key.objectid +
Josef Bacik6226cb02009-04-03 10:14:18 -04004272 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04004273 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04004274 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004275 }
Josef Bacik2552d172009-04-03 10:14:19 -04004276
4277 ins->objectid = search_start;
4278 ins->offset = num_bytes;
4279
Josef Bacik6226cb02009-04-03 10:14:18 -04004280 if (offset < search_start)
4281 btrfs_add_free_space(block_group, offset,
4282 search_start - offset);
4283 BUG_ON(offset > search_start);
4284
Yan Zheng11833d62009-09-11 16:11:19 -04004285 update_reserved_extents(block_group, num_bytes, 1);
4286
Josef Bacik2552d172009-04-03 10:14:19 -04004287 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04004288 break;
4289loop:
Josef Bacik0a243252009-09-11 16:11:20 -04004290 failed_cluster_refill = false;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004291 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004292 }
4293 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004294
Josef Bacik817d52f2009-07-13 21:29:25 -04004295 /* LOOP_CACHED_ONLY, only search fully cached block groups
4296 * LOOP_CACHING_NOWAIT, search partially cached block groups, but
4297 * dont wait foR them to finish caching
4298 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4299 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4300 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4301 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04004302 */
Josef Bacik817d52f2009-07-13 21:29:25 -04004303 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4304 (found_uncached_bg || empty_size || empty_cluster ||
4305 allowed_chunk_alloc)) {
4306 if (found_uncached_bg) {
4307 found_uncached_bg = false;
4308 if (loop < LOOP_CACHING_WAIT) {
4309 loop++;
4310 goto search;
4311 }
4312 }
4313
4314 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004315 empty_size = 0;
4316 empty_cluster = 0;
4317 }
Chris Mason42e70e72008-11-07 18:17:11 -05004318
Josef Bacik2552d172009-04-03 10:14:19 -04004319 if (allowed_chunk_alloc) {
4320 ret = do_chunk_alloc(trans, root, num_bytes +
4321 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04004322 allowed_chunk_alloc = 0;
4323 } else {
4324 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004325 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004326
Josef Bacik817d52f2009-07-13 21:29:25 -04004327 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004328 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04004329 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004330 }
Josef Bacik2552d172009-04-03 10:14:19 -04004331 ret = -ENOSPC;
4332 } else if (!ins->objectid) {
4333 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04004334 }
Chris Mason0b86a832008-03-24 15:01:56 -04004335
Josef Bacik80eb2342008-10-29 14:49:05 -04004336 /* we found what we needed */
4337 if (ins->objectid) {
4338 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05004339 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04004340
Chris Masonfa9c0d72009-04-03 09:47:43 -04004341 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004342 ret = 0;
4343 }
Chris Mason0b86a832008-03-24 15:01:56 -04004344
Chris Mason0f70abe2007-02-28 16:46:22 -05004345 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004346}
Chris Masonec44a352008-04-28 15:29:52 -04004347
Josef Bacik9ed74f22009-09-11 16:12:44 -04004348static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4349 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04004350{
4351 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004352
Josef Bacik9ed74f22009-09-11 16:12:44 -04004353 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05004354 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4355 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04004356 info->bytes_pinned - info->bytes_reserved -
4357 info->bytes_super),
Chris Masond3977122009-01-05 21:25:51 -05004358 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05004359 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Josef Bacik9ed74f22009-09-11 16:12:44 -04004360 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4361 "\n",
Joel Becker21380932009-04-21 12:38:29 -07004362 (unsigned long long)info->total_bytes,
4363 (unsigned long long)info->bytes_pinned,
4364 (unsigned long long)info->bytes_delalloc,
4365 (unsigned long long)info->bytes_may_use,
Josef Bacik9ed74f22009-09-11 16:12:44 -04004366 (unsigned long long)info->bytes_used,
4367 (unsigned long long)info->bytes_root,
4368 (unsigned long long)info->bytes_super,
4369 (unsigned long long)info->bytes_reserved);
4370 spin_unlock(&info->lock);
4371
4372 if (!dump_block_groups)
4373 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004374
Josef Bacik80eb2342008-10-29 14:49:05 -04004375 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05004376 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004377 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05004378 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4379 "%llu pinned %llu reserved\n",
4380 (unsigned long long)cache->key.objectid,
4381 (unsigned long long)cache->key.offset,
4382 (unsigned long long)btrfs_block_group_used(&cache->item),
4383 (unsigned long long)cache->pinned,
4384 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004385 btrfs_dump_free_space(cache, bytes);
4386 spin_unlock(&cache->lock);
4387 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004388 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004389}
Zheng Yane8569812008-09-26 10:05:48 -04004390
Yan Zheng11833d62009-09-11 16:11:19 -04004391int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4392 struct btrfs_root *root,
4393 u64 num_bytes, u64 min_alloc_size,
4394 u64 empty_size, u64 hint_byte,
4395 u64 search_end, struct btrfs_key *ins,
4396 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004397{
4398 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04004399 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04004400 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04004401
Josef Bacik6a632092009-02-20 11:00:09 -05004402 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04004403again:
Chris Mason0ef3e662008-05-24 14:04:53 -04004404 /*
4405 * the only place that sets empty_size is btrfs_realloc_node, which
4406 * is not called recursively on allocations
4407 */
4408 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04004409 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04004410 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004411 2 * 1024 * 1024,
4412 BTRFS_BLOCK_GROUP_METADATA |
4413 (info->metadata_alloc_profile &
4414 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04004415 }
4416 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004417 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04004418 }
Chris Mason0b86a832008-03-24 15:01:56 -04004419
Chris Masondb945352007-10-15 16:15:53 -04004420 WARN_ON(num_bytes < root->sectorsize);
4421 ret = find_free_extent(trans, root, num_bytes, empty_size,
4422 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04004423 trans->alloc_exclude_start,
4424 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04004425
Chris Mason98d20f62008-04-14 09:46:10 -04004426 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4427 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004428 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004429 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04004430 do_chunk_alloc(trans, root->fs_info->extent_root,
4431 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004432 goto again;
4433 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004434 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004435 struct btrfs_space_info *sinfo;
4436
4437 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004438 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4439 "wanted %llu\n", (unsigned long long)data,
4440 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004441 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04004442 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004443
4444 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004445}
4446
Chris Mason65b51a02008-08-01 15:11:20 -04004447int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4448{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004449 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004450 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004451
Josef Bacik0f9dd462008-09-23 13:14:11 -04004452 cache = btrfs_lookup_block_group(root->fs_info, start);
4453 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004454 printk(KERN_ERR "Unable to find block group for %llu\n",
4455 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004456 return -ENOSPC;
4457 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004458
4459 ret = btrfs_discard_extent(root, start, len);
4460
Josef Bacik0f9dd462008-09-23 13:14:11 -04004461 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04004462 update_reserved_extents(cache, len, 0);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004463 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04004464
Chris Masone6dcd2d2008-07-17 12:53:50 -04004465 return ret;
4466}
4467
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004468static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4469 struct btrfs_root *root,
4470 u64 parent, u64 root_objectid,
4471 u64 flags, u64 owner, u64 offset,
4472 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004473{
4474 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004475 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004476 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004477 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004478 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004479 struct extent_buffer *leaf;
4480 int type;
4481 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004482
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004483 if (parent > 0)
4484 type = BTRFS_SHARED_DATA_REF_KEY;
4485 else
4486 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004487
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004488 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004489
4490 path = btrfs_alloc_path();
4491 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004492
Chris Masonb9473432009-03-13 11:00:37 -04004493 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004494 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4495 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004496 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004497
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004498 leaf = path->nodes[0];
4499 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004500 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004501 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4502 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4503 btrfs_set_extent_flags(leaf, extent_item,
4504 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004505
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004506 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4507 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4508 if (parent > 0) {
4509 struct btrfs_shared_data_ref *ref;
4510 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4511 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4512 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4513 } else {
4514 struct btrfs_extent_data_ref *ref;
4515 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4516 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4517 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4518 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4519 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4520 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004521
4522 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004523 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004524
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004525 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4526 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05004527 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004528 printk(KERN_ERR "btrfs update block group failed for %llu "
4529 "%llu\n", (unsigned long long)ins->objectid,
4530 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004531 BUG();
4532 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004533 return ret;
4534}
4535
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004536static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4537 struct btrfs_root *root,
4538 u64 parent, u64 root_objectid,
4539 u64 flags, struct btrfs_disk_key *key,
4540 int level, struct btrfs_key *ins)
4541{
4542 int ret;
4543 struct btrfs_fs_info *fs_info = root->fs_info;
4544 struct btrfs_extent_item *extent_item;
4545 struct btrfs_tree_block_info *block_info;
4546 struct btrfs_extent_inline_ref *iref;
4547 struct btrfs_path *path;
4548 struct extent_buffer *leaf;
4549 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4550
4551 path = btrfs_alloc_path();
4552 BUG_ON(!path);
4553
4554 path->leave_spinning = 1;
4555 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4556 ins, size);
4557 BUG_ON(ret);
4558
4559 leaf = path->nodes[0];
4560 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4561 struct btrfs_extent_item);
4562 btrfs_set_extent_refs(leaf, extent_item, 1);
4563 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4564 btrfs_set_extent_flags(leaf, extent_item,
4565 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4566 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4567
4568 btrfs_set_tree_block_key(leaf, block_info, key);
4569 btrfs_set_tree_block_level(leaf, block_info, level);
4570
4571 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4572 if (parent > 0) {
4573 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4574 btrfs_set_extent_inline_ref_type(leaf, iref,
4575 BTRFS_SHARED_BLOCK_REF_KEY);
4576 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4577 } else {
4578 btrfs_set_extent_inline_ref_type(leaf, iref,
4579 BTRFS_TREE_BLOCK_REF_KEY);
4580 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4581 }
4582
4583 btrfs_mark_buffer_dirty(leaf);
4584 btrfs_free_path(path);
4585
4586 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4587 1, 0);
4588 if (ret) {
4589 printk(KERN_ERR "btrfs update block group failed for %llu "
4590 "%llu\n", (unsigned long long)ins->objectid,
4591 (unsigned long long)ins->offset);
4592 BUG();
4593 }
4594 return ret;
4595}
4596
4597int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4598 struct btrfs_root *root,
4599 u64 root_objectid, u64 owner,
4600 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004601{
4602 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04004603
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004604 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004605
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004606 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4607 0, root_objectid, owner, offset,
4608 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004609 return ret;
4610}
Chris Masone02119d2008-09-05 16:13:11 -04004611
4612/*
4613 * this is used by the tree logging recovery code. It records that
4614 * an extent has been allocated and makes sure to clear the free
4615 * space cache bits as well
4616 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004617int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4618 struct btrfs_root *root,
4619 u64 root_objectid, u64 owner, u64 offset,
4620 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004621{
4622 int ret;
4623 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04004624 struct btrfs_caching_control *caching_ctl;
4625 u64 start = ins->objectid;
4626 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04004627
Chris Masone02119d2008-09-05 16:13:11 -04004628 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04004629 cache_block_group(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004630 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004631
Yan Zheng11833d62009-09-11 16:11:19 -04004632 if (!caching_ctl) {
4633 BUG_ON(!block_group_cache_done(block_group));
4634 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4635 BUG_ON(ret);
4636 } else {
4637 mutex_lock(&caching_ctl->mutex);
4638
4639 if (start >= caching_ctl->progress) {
4640 ret = add_excluded_extent(root, start, num_bytes);
4641 BUG_ON(ret);
4642 } else if (start + num_bytes <= caching_ctl->progress) {
4643 ret = btrfs_remove_free_space(block_group,
4644 start, num_bytes);
4645 BUG_ON(ret);
4646 } else {
4647 num_bytes = caching_ctl->progress - start;
4648 ret = btrfs_remove_free_space(block_group,
4649 start, num_bytes);
4650 BUG_ON(ret);
4651
4652 start = caching_ctl->progress;
4653 num_bytes = ins->objectid + ins->offset -
4654 caching_ctl->progress;
4655 ret = add_excluded_extent(root, start, num_bytes);
4656 BUG_ON(ret);
4657 }
4658
4659 mutex_unlock(&caching_ctl->mutex);
4660 put_caching_control(caching_ctl);
4661 }
4662
4663 update_reserved_extents(block_group, ins->offset, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004664 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004665 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4666 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004667 return ret;
4668}
4669
Chris Masone6dcd2d2008-07-17 12:53:50 -04004670/*
4671 * finds a free extent and does all the dirty work required for allocation
4672 * returns the key for the extent through ins, and a tree buffer for
4673 * the first block of the extent through buf.
4674 *
4675 * returns 0 if everything worked, non-zero otherwise.
4676 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004677static int alloc_tree_block(struct btrfs_trans_handle *trans,
4678 struct btrfs_root *root,
4679 u64 num_bytes, u64 parent, u64 root_objectid,
4680 struct btrfs_disk_key *key, int level,
4681 u64 empty_size, u64 hint_byte, u64 search_end,
4682 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004683{
4684 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004685 u64 flags = 0;
4686
Yan Zheng11833d62009-09-11 16:11:19 -04004687 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4688 empty_size, hint_byte, search_end,
4689 ins, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004690 if (ret)
4691 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004692
4693 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4694 if (parent == 0)
4695 parent = ins->objectid;
4696 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4697 } else
4698 BUG_ON(parent > 0);
4699
Chris Masond00aff02008-09-11 15:54:42 -04004700 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004701 struct btrfs_delayed_extent_op *extent_op;
4702 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4703 BUG_ON(!extent_op);
4704 if (key)
4705 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4706 else
4707 memset(&extent_op->key, 0, sizeof(extent_op->key));
4708 extent_op->flags_to_set = flags;
4709 extent_op->update_key = 1;
4710 extent_op->update_flags = 1;
4711 extent_op->is_data = 0;
4712
4713 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4714 ins->offset, parent, root_objectid,
4715 level, BTRFS_ADD_DELAYED_EXTENT,
4716 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004717 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004718 }
Chris Mason925baed2008-06-25 16:01:30 -04004719 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004720}
Chris Mason65b51a02008-08-01 15:11:20 -04004721
4722struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4723 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004724 u64 bytenr, u32 blocksize,
4725 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004726{
4727 struct extent_buffer *buf;
4728
4729 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4730 if (!buf)
4731 return ERR_PTR(-ENOMEM);
4732 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004733 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004734 btrfs_tree_lock(buf);
4735 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004736
4737 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004738 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004739
Chris Masond0c803c2008-09-11 16:17:57 -04004740 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4741 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04004742 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004743 } else {
4744 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4745 buf->start + buf->len - 1, GFP_NOFS);
4746 }
Chris Mason65b51a02008-08-01 15:11:20 -04004747 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004748 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004749 return buf;
4750}
4751
Chris Masonfec577f2007-02-26 10:40:21 -05004752/*
4753 * helper function to allocate a block for a given tree
4754 * returns the tree buffer or NULL.
4755 */
Chris Mason5f39d392007-10-15 16:14:19 -04004756struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004757 struct btrfs_root *root, u32 blocksize,
4758 u64 parent, u64 root_objectid,
4759 struct btrfs_disk_key *key, int level,
4760 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004761{
Chris Masone2fa7222007-03-12 16:22:34 -04004762 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004763 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004764 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004765
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004766 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4767 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004768 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004769 BUG_ON(ret > 0);
4770 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004771 }
Chris Mason55c69072008-01-09 15:55:33 -05004772
Chris Mason4008c042009-02-12 14:09:45 -05004773 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4774 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004775 return buf;
4776}
Chris Masona28ec192007-03-06 20:08:01 -05004777
Yan Zheng2c47e6052009-06-27 21:07:35 -04004778struct walk_control {
4779 u64 refs[BTRFS_MAX_LEVEL];
4780 u64 flags[BTRFS_MAX_LEVEL];
4781 struct btrfs_key update_progress;
4782 int stage;
4783 int level;
4784 int shared_level;
4785 int update_ref;
4786 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004787 int reada_slot;
4788 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004789};
4790
4791#define DROP_REFERENCE 1
4792#define UPDATE_BACKREF 2
4793
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004794static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4795 struct btrfs_root *root,
4796 struct walk_control *wc,
4797 struct btrfs_path *path)
4798{
4799 u64 bytenr;
4800 u64 generation;
4801 u64 refs;
4802 u64 last = 0;
4803 u32 nritems;
4804 u32 blocksize;
4805 struct btrfs_key key;
4806 struct extent_buffer *eb;
4807 int ret;
4808 int slot;
4809 int nread = 0;
4810
4811 if (path->slots[wc->level] < wc->reada_slot) {
4812 wc->reada_count = wc->reada_count * 2 / 3;
4813 wc->reada_count = max(wc->reada_count, 2);
4814 } else {
4815 wc->reada_count = wc->reada_count * 3 / 2;
4816 wc->reada_count = min_t(int, wc->reada_count,
4817 BTRFS_NODEPTRS_PER_BLOCK(root));
4818 }
4819
4820 eb = path->nodes[wc->level];
4821 nritems = btrfs_header_nritems(eb);
4822 blocksize = btrfs_level_size(root, wc->level - 1);
4823
4824 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4825 if (nread >= wc->reada_count)
4826 break;
4827
4828 cond_resched();
4829 bytenr = btrfs_node_blockptr(eb, slot);
4830 generation = btrfs_node_ptr_generation(eb, slot);
4831
4832 if (slot == path->slots[wc->level])
4833 goto reada;
4834
4835 if (wc->stage == UPDATE_BACKREF &&
4836 generation <= root->root_key.offset)
4837 continue;
4838
4839 if (wc->stage == DROP_REFERENCE) {
4840 ret = btrfs_lookup_extent_info(trans, root,
4841 bytenr, blocksize,
4842 &refs, NULL);
4843 BUG_ON(ret);
4844 BUG_ON(refs == 0);
4845 if (refs == 1)
4846 goto reada;
4847
4848 if (!wc->update_ref ||
4849 generation <= root->root_key.offset)
4850 continue;
4851 btrfs_node_key_to_cpu(eb, &key, slot);
4852 ret = btrfs_comp_cpu_keys(&key,
4853 &wc->update_progress);
4854 if (ret < 0)
4855 continue;
4856 }
4857reada:
4858 ret = readahead_tree_block(root, bytenr, blocksize,
4859 generation);
4860 if (ret)
4861 break;
4862 last = bytenr + blocksize;
4863 nread++;
4864 }
4865 wc->reada_slot = slot;
4866}
4867
Chris Mason9aca1d52007-03-13 11:09:37 -04004868/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04004869 * hepler to process tree block while walking down the tree.
4870 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04004871 * when wc->stage == UPDATE_BACKREF, this function updates
4872 * back refs for pointers in the block.
4873 *
4874 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004875 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004876static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4877 struct btrfs_root *root,
4878 struct btrfs_path *path,
4879 struct walk_control *wc)
4880{
4881 int level = wc->level;
4882 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04004883 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4884 int ret;
4885
4886 if (wc->stage == UPDATE_BACKREF &&
4887 btrfs_header_owner(eb) != root->root_key.objectid)
4888 return 1;
4889
4890 /*
4891 * when reference count of tree block is 1, it won't increase
4892 * again. once full backref flag is set, we never clear it.
4893 */
4894 if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4895 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) {
4896 BUG_ON(!path->locks[level]);
4897 ret = btrfs_lookup_extent_info(trans, root,
4898 eb->start, eb->len,
4899 &wc->refs[level],
4900 &wc->flags[level]);
4901 BUG_ON(ret);
4902 BUG_ON(wc->refs[level] == 0);
4903 }
4904
Yan Zheng2c47e6052009-06-27 21:07:35 -04004905 if (wc->stage == DROP_REFERENCE) {
4906 if (wc->refs[level] > 1)
4907 return 1;
4908
4909 if (path->locks[level] && !wc->keep_locks) {
4910 btrfs_tree_unlock(eb);
4911 path->locks[level] = 0;
4912 }
4913 return 0;
4914 }
4915
4916 /* wc->stage == UPDATE_BACKREF */
4917 if (!(wc->flags[level] & flag)) {
4918 BUG_ON(!path->locks[level]);
4919 ret = btrfs_inc_ref(trans, root, eb, 1);
4920 BUG_ON(ret);
4921 ret = btrfs_dec_ref(trans, root, eb, 0);
4922 BUG_ON(ret);
4923 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
4924 eb->len, flag, 0);
4925 BUG_ON(ret);
4926 wc->flags[level] |= flag;
4927 }
4928
4929 /*
4930 * the block is shared by multiple trees, so it's not good to
4931 * keep the tree lock
4932 */
4933 if (path->locks[level] && level > 0) {
4934 btrfs_tree_unlock(eb);
4935 path->locks[level] = 0;
4936 }
4937 return 0;
4938}
4939
4940/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004941 * hepler to process tree block pointer.
4942 *
4943 * when wc->stage == DROP_REFERENCE, this function checks
4944 * reference count of the block pointed to. if the block
4945 * is shared and we need update back refs for the subtree
4946 * rooted at the block, this function changes wc->stage to
4947 * UPDATE_BACKREF. if the block is shared and there is no
4948 * need to update back, this function drops the reference
4949 * to the block.
4950 *
4951 * NOTE: return value 1 means we should stop walking down.
4952 */
4953static noinline int do_walk_down(struct btrfs_trans_handle *trans,
4954 struct btrfs_root *root,
4955 struct btrfs_path *path,
4956 struct walk_control *wc)
4957{
4958 u64 bytenr;
4959 u64 generation;
4960 u64 parent;
4961 u32 blocksize;
4962 struct btrfs_key key;
4963 struct extent_buffer *next;
4964 int level = wc->level;
4965 int reada = 0;
4966 int ret = 0;
4967
4968 generation = btrfs_node_ptr_generation(path->nodes[level],
4969 path->slots[level]);
4970 /*
4971 * if the lower level block was created before the snapshot
4972 * was created, we know there is no need to update back refs
4973 * for the subtree
4974 */
4975 if (wc->stage == UPDATE_BACKREF &&
4976 generation <= root->root_key.offset)
4977 return 1;
4978
4979 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
4980 blocksize = btrfs_level_size(root, level - 1);
4981
4982 next = btrfs_find_tree_block(root, bytenr, blocksize);
4983 if (!next) {
4984 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
4985 reada = 1;
4986 }
4987 btrfs_tree_lock(next);
4988 btrfs_set_lock_blocking(next);
4989
4990 if (wc->stage == DROP_REFERENCE) {
4991 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
4992 &wc->refs[level - 1],
4993 &wc->flags[level - 1]);
4994 BUG_ON(ret);
4995 BUG_ON(wc->refs[level - 1] == 0);
4996
4997 if (wc->refs[level - 1] > 1) {
4998 if (!wc->update_ref ||
4999 generation <= root->root_key.offset)
5000 goto skip;
5001
5002 btrfs_node_key_to_cpu(path->nodes[level], &key,
5003 path->slots[level]);
5004 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5005 if (ret < 0)
5006 goto skip;
5007
5008 wc->stage = UPDATE_BACKREF;
5009 wc->shared_level = level - 1;
5010 }
5011 }
5012
5013 if (!btrfs_buffer_uptodate(next, generation)) {
5014 btrfs_tree_unlock(next);
5015 free_extent_buffer(next);
5016 next = NULL;
5017 }
5018
5019 if (!next) {
5020 if (reada && level == 1)
5021 reada_walk_down(trans, root, wc, path);
5022 next = read_tree_block(root, bytenr, blocksize, generation);
5023 btrfs_tree_lock(next);
5024 btrfs_set_lock_blocking(next);
5025 }
5026
5027 level--;
5028 BUG_ON(level != btrfs_header_level(next));
5029 path->nodes[level] = next;
5030 path->slots[level] = 0;
5031 path->locks[level] = 1;
5032 wc->level = level;
5033 if (wc->level == 1)
5034 wc->reada_slot = 0;
5035 return 0;
5036skip:
5037 wc->refs[level - 1] = 0;
5038 wc->flags[level - 1] = 0;
5039
5040 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5041 parent = path->nodes[level]->start;
5042 } else {
5043 BUG_ON(root->root_key.objectid !=
5044 btrfs_header_owner(path->nodes[level]));
5045 parent = 0;
5046 }
5047
5048 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5049 root->root_key.objectid, level - 1, 0);
5050 BUG_ON(ret);
5051
5052 btrfs_tree_unlock(next);
5053 free_extent_buffer(next);
5054 return 1;
5055}
5056
5057/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005058 * hepler to process tree block while walking up the tree.
5059 *
5060 * when wc->stage == DROP_REFERENCE, this function drops
5061 * reference count on the block.
5062 *
5063 * when wc->stage == UPDATE_BACKREF, this function changes
5064 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5065 * to UPDATE_BACKREF previously while processing the block.
5066 *
5067 * NOTE: return value 1 means we should stop walking up.
5068 */
5069static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5070 struct btrfs_root *root,
5071 struct btrfs_path *path,
5072 struct walk_control *wc)
5073{
5074 int ret = 0;
5075 int level = wc->level;
5076 struct extent_buffer *eb = path->nodes[level];
5077 u64 parent = 0;
5078
5079 if (wc->stage == UPDATE_BACKREF) {
5080 BUG_ON(wc->shared_level < level);
5081 if (level < wc->shared_level)
5082 goto out;
5083
Yan Zheng2c47e6052009-06-27 21:07:35 -04005084 ret = find_next_key(path, level + 1, &wc->update_progress);
5085 if (ret > 0)
5086 wc->update_ref = 0;
5087
5088 wc->stage = DROP_REFERENCE;
5089 wc->shared_level = -1;
5090 path->slots[level] = 0;
5091
5092 /*
5093 * check reference count again if the block isn't locked.
5094 * we should start walking down the tree again if reference
5095 * count is one.
5096 */
5097 if (!path->locks[level]) {
5098 BUG_ON(level == 0);
5099 btrfs_tree_lock(eb);
5100 btrfs_set_lock_blocking(eb);
5101 path->locks[level] = 1;
5102
5103 ret = btrfs_lookup_extent_info(trans, root,
5104 eb->start, eb->len,
5105 &wc->refs[level],
5106 &wc->flags[level]);
5107 BUG_ON(ret);
5108 BUG_ON(wc->refs[level] == 0);
5109 if (wc->refs[level] == 1) {
5110 btrfs_tree_unlock(eb);
5111 path->locks[level] = 0;
5112 return 1;
5113 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005114 }
5115 }
5116
5117 /* wc->stage == DROP_REFERENCE */
5118 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5119
5120 if (wc->refs[level] == 1) {
5121 if (level == 0) {
5122 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5123 ret = btrfs_dec_ref(trans, root, eb, 1);
5124 else
5125 ret = btrfs_dec_ref(trans, root, eb, 0);
5126 BUG_ON(ret);
5127 }
5128 /* make block locked assertion in clean_tree_block happy */
5129 if (!path->locks[level] &&
5130 btrfs_header_generation(eb) == trans->transid) {
5131 btrfs_tree_lock(eb);
5132 btrfs_set_lock_blocking(eb);
5133 path->locks[level] = 1;
5134 }
5135 clean_tree_block(trans, root, eb);
5136 }
5137
5138 if (eb == root->node) {
5139 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5140 parent = eb->start;
5141 else
5142 BUG_ON(root->root_key.objectid !=
5143 btrfs_header_owner(eb));
5144 } else {
5145 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5146 parent = path->nodes[level + 1]->start;
5147 else
5148 BUG_ON(root->root_key.objectid !=
5149 btrfs_header_owner(path->nodes[level + 1]));
5150 }
5151
5152 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5153 root->root_key.objectid, level, 0);
5154 BUG_ON(ret);
5155out:
5156 wc->refs[level] = 0;
5157 wc->flags[level] = 0;
5158 return ret;
5159}
5160
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005161static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5162 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005163 struct btrfs_path *path,
5164 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005165{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005166 int level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005167 int ret;
5168
Yan Zheng2c47e6052009-06-27 21:07:35 -04005169 while (level >= 0) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005170 if (path->slots[level] >=
5171 btrfs_header_nritems(path->nodes[level]))
5172 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005173
Yan Zheng2c47e6052009-06-27 21:07:35 -04005174 ret = walk_down_proc(trans, root, path, wc);
5175 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005176 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005177
Yan Zheng2c47e6052009-06-27 21:07:35 -04005178 if (level == 0)
5179 break;
5180
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005181 ret = do_walk_down(trans, root, path, wc);
5182 if (ret > 0) {
5183 path->slots[level]++;
5184 continue;
5185 }
5186 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005187 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005188 return 0;
5189}
5190
Chris Masond3977122009-01-05 21:25:51 -05005191static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005192 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005193 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005194 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005195{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005196 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005197 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005198
Yan Zheng2c47e6052009-06-27 21:07:35 -04005199 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5200 while (level < max_level && path->nodes[level]) {
5201 wc->level = level;
5202 if (path->slots[level] + 1 <
5203 btrfs_header_nritems(path->nodes[level])) {
5204 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005205 return 0;
5206 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005207 ret = walk_up_proc(trans, root, path, wc);
5208 if (ret > 0)
5209 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05005210
Yan Zheng2c47e6052009-06-27 21:07:35 -04005211 if (path->locks[level]) {
5212 btrfs_tree_unlock(path->nodes[level]);
5213 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005214 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005215 free_extent_buffer(path->nodes[level]);
5216 path->nodes[level] = NULL;
5217 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005218 }
5219 }
5220 return 1;
5221}
5222
Chris Mason9aca1d52007-03-13 11:09:37 -04005223/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005224 * drop a subvolume tree.
5225 *
5226 * this function traverses the tree freeing any blocks that only
5227 * referenced by the tree.
5228 *
5229 * when a shared tree block is found. this function decreases its
5230 * reference count by one. if update_ref is true, this function
5231 * also make sure backrefs for the shared block and all lower level
5232 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04005233 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005234int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05005235{
Chris Mason5caf2a02007-04-02 11:20:42 -04005236 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005237 struct btrfs_trans_handle *trans;
5238 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005239 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005240 struct walk_control *wc;
5241 struct btrfs_key key;
5242 int err = 0;
5243 int ret;
5244 int level;
Chris Mason20524f02007-03-10 06:35:47 -05005245
Chris Mason5caf2a02007-04-02 11:20:42 -04005246 path = btrfs_alloc_path();
5247 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05005248
Yan Zheng2c47e6052009-06-27 21:07:35 -04005249 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5250 BUG_ON(!wc);
5251
5252 trans = btrfs_start_transaction(tree_root, 1);
5253
Chris Mason9f3a7422007-08-07 15:52:19 -04005254 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005255 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005256 path->nodes[level] = btrfs_lock_root_node(root);
5257 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005258 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005259 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005260 memset(&wc->update_progress, 0,
5261 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005262 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005263 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005264 memcpy(&wc->update_progress, &key,
5265 sizeof(wc->update_progress));
5266
Chris Mason6702ed42007-08-07 16:15:09 -04005267 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005268 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005269 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005270 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5271 path->lowest_level = 0;
5272 if (ret < 0) {
5273 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005274 goto out;
5275 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005276 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005277
Chris Mason7d9eb122008-07-08 14:19:17 -04005278 /*
5279 * unlock our path, this is safe because only this
5280 * function is allowed to delete this snapshot
5281 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005282 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005283
Yan Zheng2c47e6052009-06-27 21:07:35 -04005284 level = btrfs_header_level(root->node);
5285 while (1) {
5286 btrfs_tree_lock(path->nodes[level]);
5287 btrfs_set_lock_blocking(path->nodes[level]);
5288
5289 ret = btrfs_lookup_extent_info(trans, root,
5290 path->nodes[level]->start,
5291 path->nodes[level]->len,
5292 &wc->refs[level],
5293 &wc->flags[level]);
5294 BUG_ON(ret);
5295 BUG_ON(wc->refs[level] == 0);
5296
5297 if (level == root_item->drop_level)
5298 break;
5299
5300 btrfs_tree_unlock(path->nodes[level]);
5301 WARN_ON(wc->refs[level] != 1);
5302 level--;
5303 }
5304 }
5305
5306 wc->level = level;
5307 wc->shared_level = -1;
5308 wc->stage = DROP_REFERENCE;
5309 wc->update_ref = update_ref;
5310 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005311 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005312
5313 while (1) {
5314 ret = walk_down_tree(trans, root, path, wc);
5315 if (ret < 0) {
5316 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005317 break;
5318 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005319
5320 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5321 if (ret < 0) {
5322 err = ret;
5323 break;
5324 }
5325
5326 if (ret > 0) {
5327 BUG_ON(wc->stage != DROP_REFERENCE);
5328 break;
5329 }
5330
5331 if (wc->stage == DROP_REFERENCE) {
5332 level = wc->level;
5333 btrfs_node_key(path->nodes[level],
5334 &root_item->drop_progress,
5335 path->slots[level]);
5336 root_item->drop_level = level;
5337 }
5338
5339 BUG_ON(wc->level == 0);
5340 if (trans->transaction->in_commit ||
5341 trans->transaction->delayed_refs.flushing) {
5342 ret = btrfs_update_root(trans, tree_root,
5343 &root->root_key,
5344 root_item);
5345 BUG_ON(ret);
5346
5347 btrfs_end_transaction(trans, tree_root);
5348 trans = btrfs_start_transaction(tree_root, 1);
5349 } else {
5350 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005351 update = trans->delayed_ref_updates;
5352 trans->delayed_ref_updates = 0;
5353 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005354 btrfs_run_delayed_refs(trans, tree_root,
5355 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005356 }
Chris Mason20524f02007-03-10 06:35:47 -05005357 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005358 btrfs_release_path(root, path);
5359 BUG_ON(err);
5360
5361 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5362 BUG_ON(ret);
5363
Yan, Zheng76dda932009-09-21 16:00:26 -04005364 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5365 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5366 NULL, NULL);
5367 BUG_ON(ret < 0);
5368 if (ret > 0) {
5369 ret = btrfs_del_orphan_item(trans, tree_root,
5370 root->root_key.objectid);
5371 BUG_ON(ret);
5372 }
5373 }
5374
5375 if (root->in_radix) {
5376 btrfs_free_fs_root(tree_root->fs_info, root);
5377 } else {
5378 free_extent_buffer(root->node);
5379 free_extent_buffer(root->commit_root);
5380 kfree(root);
5381 }
Chris Mason9f3a7422007-08-07 15:52:19 -04005382out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005383 btrfs_end_transaction(trans, tree_root);
5384 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005385 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005386 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005387}
Chris Mason9078a3e2007-04-26 16:46:15 -04005388
Yan Zheng2c47e6052009-06-27 21:07:35 -04005389/*
5390 * drop subtree rooted at tree block 'node'.
5391 *
5392 * NOTE: this function will unlock and release tree block 'node'
5393 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005394int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5395 struct btrfs_root *root,
5396 struct extent_buffer *node,
5397 struct extent_buffer *parent)
5398{
5399 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005400 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005401 int level;
5402 int parent_level;
5403 int ret = 0;
5404 int wret;
5405
Yan Zheng2c47e6052009-06-27 21:07:35 -04005406 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5407
Yan Zhengf82d02d2008-10-29 14:49:05 -04005408 path = btrfs_alloc_path();
5409 BUG_ON(!path);
5410
Yan Zheng2c47e6052009-06-27 21:07:35 -04005411 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5412 BUG_ON(!wc);
5413
Chris Masonb9447ef2009-03-09 11:45:38 -04005414 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005415 parent_level = btrfs_header_level(parent);
5416 extent_buffer_get(parent);
5417 path->nodes[parent_level] = parent;
5418 path->slots[parent_level] = btrfs_header_nritems(parent);
5419
Chris Masonb9447ef2009-03-09 11:45:38 -04005420 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005421 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005422 path->nodes[level] = node;
5423 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005424 path->locks[level] = 1;
5425
5426 wc->refs[parent_level] = 1;
5427 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5428 wc->level = level;
5429 wc->shared_level = -1;
5430 wc->stage = DROP_REFERENCE;
5431 wc->update_ref = 0;
5432 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005433 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005434
5435 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005436 wret = walk_down_tree(trans, root, path, wc);
5437 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005438 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005439 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005440 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005441
Yan Zheng2c47e6052009-06-27 21:07:35 -04005442 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005443 if (wret < 0)
5444 ret = wret;
5445 if (wret != 0)
5446 break;
5447 }
5448
Yan Zheng2c47e6052009-06-27 21:07:35 -04005449 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005450 btrfs_free_path(path);
5451 return ret;
5452}
5453
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005454#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005455static unsigned long calc_ra(unsigned long start, unsigned long last,
5456 unsigned long nr)
5457{
5458 return min(last, start + nr - 1);
5459}
5460
Chris Masond3977122009-01-05 21:25:51 -05005461static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005462 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005463{
5464 u64 page_start;
5465 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005466 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005467 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005468 unsigned long i;
5469 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005470 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005471 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005472 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005473 unsigned int total_read = 0;
5474 unsigned int total_dirty = 0;
5475 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005476
5477 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005478
5479 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005480 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005481 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5482
Zheng Yan1a40e232008-09-26 10:09:34 -04005483 /* make sure the dirty trick played by the caller work */
5484 ret = invalidate_inode_pages2_range(inode->i_mapping,
5485 first_index, last_index);
5486 if (ret)
5487 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005488
Chris Mason4313b392008-01-03 09:08:48 -05005489 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005490
Zheng Yan1a40e232008-09-26 10:09:34 -04005491 for (i = first_index ; i <= last_index; i++) {
5492 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005493 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005494 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005495 }
5496 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005497again:
5498 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005499 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005500 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005501 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005502 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005503 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005504 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005505 if (!PageUptodate(page)) {
5506 btrfs_readpage(NULL, page);
5507 lock_page(page);
5508 if (!PageUptodate(page)) {
5509 unlock_page(page);
5510 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005511 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005512 goto out_unlock;
5513 }
5514 }
Chris Masonec44a352008-04-28 15:29:52 -04005515 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005516
Chris Masonedbd8d42007-12-21 16:27:24 -05005517 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5518 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005519 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005520
Chris Mason3eaa2882008-07-24 11:57:52 -04005521 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5522 if (ordered) {
5523 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5524 unlock_page(page);
5525 page_cache_release(page);
5526 btrfs_start_ordered_extent(inode, ordered, 1);
5527 btrfs_put_ordered_extent(ordered);
5528 goto again;
5529 }
5530 set_page_extent_mapped(page);
5531
Zheng Yan1a40e232008-09-26 10:09:34 -04005532 if (i == first_index)
5533 set_extent_bits(io_tree, page_start, page_end,
5534 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005535 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005536
Chris Masona061fc82008-05-07 11:43:44 -04005537 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005538 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005539
Chris Masond1310b22008-01-24 16:13:08 -05005540 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005541 unlock_page(page);
5542 page_cache_release(page);
5543 }
5544
5545out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005546 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005547 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005548 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005549 return ret;
5550}
5551
Chris Masond3977122009-01-05 21:25:51 -05005552static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005553 struct btrfs_key *extent_key,
5554 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005555{
Zheng Yan1a40e232008-09-26 10:09:34 -04005556 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5557 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5558 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005559 u64 start = extent_key->objectid - offset;
5560 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005561
5562 em = alloc_extent_map(GFP_NOFS);
5563 BUG_ON(!em || IS_ERR(em));
5564
Yan Zheng66435582008-10-30 14:19:50 -04005565 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005566 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005567 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005568 em->block_start = extent_key->objectid;
5569 em->bdev = root->fs_info->fs_devices->latest_bdev;
5570 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5571
5572 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005573 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005574 while (1) {
5575 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04005576 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005577 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005578 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005579 if (ret != -EEXIST) {
5580 free_extent_map(em);
5581 break;
5582 }
Yan Zheng66435582008-10-30 14:19:50 -04005583 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005584 }
Yan Zheng66435582008-10-30 14:19:50 -04005585 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005586
Yan Zheng66435582008-10-30 14:19:50 -04005587 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005588}
5589
5590struct btrfs_ref_path {
5591 u64 extent_start;
5592 u64 nodes[BTRFS_MAX_LEVEL];
5593 u64 root_objectid;
5594 u64 root_generation;
5595 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005596 u32 num_refs;
5597 int lowest_level;
5598 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005599 int shared_level;
5600
5601 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5602 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005603};
5604
5605struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005606 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005607 u64 disk_bytenr;
5608 u64 disk_num_bytes;
5609 u64 offset;
5610 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005611 u8 compression;
5612 u8 encryption;
5613 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005614};
5615
5616static int is_cowonly_root(u64 root_objectid)
5617{
5618 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5619 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5620 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5621 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005622 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5623 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005624 return 1;
5625 return 0;
5626}
5627
Chris Masond3977122009-01-05 21:25:51 -05005628static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005629 struct btrfs_root *extent_root,
5630 struct btrfs_ref_path *ref_path,
5631 int first_time)
5632{
5633 struct extent_buffer *leaf;
5634 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005635 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005636 struct btrfs_key key;
5637 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005638 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005639 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005640 int level;
5641 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005642
Zheng Yan1a40e232008-09-26 10:09:34 -04005643 path = btrfs_alloc_path();
5644 if (!path)
5645 return -ENOMEM;
5646
Zheng Yan1a40e232008-09-26 10:09:34 -04005647 if (first_time) {
5648 ref_path->lowest_level = -1;
5649 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005650 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005651 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005652 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005653walk_down:
5654 level = ref_path->current_level - 1;
5655 while (level >= -1) {
5656 u64 parent;
5657 if (level < ref_path->lowest_level)
5658 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005659
Chris Masond3977122009-01-05 21:25:51 -05005660 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005661 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005662 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005663 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005664 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005665
Zheng Yan1a40e232008-09-26 10:09:34 -04005666 parent = ref_path->nodes[level + 1];
5667 ref_path->nodes[level + 1] = 0;
5668 ref_path->current_level = level;
5669 BUG_ON(parent == 0);
5670
5671 key.objectid = bytenr;
5672 key.offset = parent + 1;
5673 key.type = BTRFS_EXTENT_REF_KEY;
5674
5675 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005676 if (ret < 0)
5677 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005678 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005679
Chris Masonedbd8d42007-12-21 16:27:24 -05005680 leaf = path->nodes[0];
5681 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005682 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005683 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005684 if (ret < 0)
5685 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005686 if (ret > 0)
5687 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005688 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005689 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005690
5691 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005692 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005693 found_key.type == BTRFS_EXTENT_REF_KEY) {
5694 if (level < ref_path->shared_level)
5695 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005696 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005697 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005698next:
5699 level--;
5700 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005701 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005702 }
5703 /* reached lowest level */
5704 ret = 1;
5705 goto out;
5706walk_up:
5707 level = ref_path->current_level;
5708 while (level < BTRFS_MAX_LEVEL - 1) {
5709 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005710
5711 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005712 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005713 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005714 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005715
Zheng Yan1a40e232008-09-26 10:09:34 -04005716 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005717
Zheng Yan1a40e232008-09-26 10:09:34 -04005718 key.objectid = bytenr;
5719 key.offset = 0;
5720 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005721
Zheng Yan1a40e232008-09-26 10:09:34 -04005722 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5723 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005724 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005725
5726 leaf = path->nodes[0];
5727 nritems = btrfs_header_nritems(leaf);
5728 if (path->slots[0] >= nritems) {
5729 ret = btrfs_next_leaf(extent_root, path);
5730 if (ret < 0)
5731 goto out;
5732 if (ret > 0) {
5733 /* the extent was freed by someone */
5734 if (ref_path->lowest_level == level)
5735 goto out;
5736 btrfs_release_path(extent_root, path);
5737 goto walk_down;
5738 }
5739 leaf = path->nodes[0];
5740 }
5741
5742 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5743 if (found_key.objectid != bytenr ||
5744 found_key.type != BTRFS_EXTENT_REF_KEY) {
5745 /* the extent was freed by someone */
5746 if (ref_path->lowest_level == level) {
5747 ret = 1;
5748 goto out;
5749 }
5750 btrfs_release_path(extent_root, path);
5751 goto walk_down;
5752 }
5753found:
5754 ref = btrfs_item_ptr(leaf, path->slots[0],
5755 struct btrfs_extent_ref);
5756 ref_objectid = btrfs_ref_objectid(leaf, ref);
5757 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5758 if (first_time) {
5759 level = (int)ref_objectid;
5760 BUG_ON(level >= BTRFS_MAX_LEVEL);
5761 ref_path->lowest_level = level;
5762 ref_path->current_level = level;
5763 ref_path->nodes[level] = bytenr;
5764 } else {
5765 WARN_ON(ref_objectid != level);
5766 }
5767 } else {
5768 WARN_ON(level != -1);
5769 }
5770 first_time = 0;
5771
5772 if (ref_path->lowest_level == level) {
5773 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005774 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5775 }
5776
5777 /*
5778 * the block is tree root or the block isn't in reference
5779 * counted tree.
5780 */
5781 if (found_key.objectid == found_key.offset ||
5782 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5783 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5784 ref_path->root_generation =
5785 btrfs_ref_generation(leaf, ref);
5786 if (level < 0) {
5787 /* special reference from the tree log */
5788 ref_path->nodes[0] = found_key.offset;
5789 ref_path->current_level = 0;
5790 }
5791 ret = 0;
5792 goto out;
5793 }
5794
5795 level++;
5796 BUG_ON(ref_path->nodes[level] != 0);
5797 ref_path->nodes[level] = found_key.offset;
5798 ref_path->current_level = level;
5799
5800 /*
5801 * the reference was created in the running transaction,
5802 * no need to continue walking up.
5803 */
5804 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5805 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5806 ref_path->root_generation =
5807 btrfs_ref_generation(leaf, ref);
5808 ret = 0;
5809 goto out;
5810 }
5811
5812 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005813 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005814 }
5815 /* reached max tree level, but no tree root found. */
5816 BUG();
5817out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005818 btrfs_free_path(path);
5819 return ret;
5820}
5821
5822static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5823 struct btrfs_root *extent_root,
5824 struct btrfs_ref_path *ref_path,
5825 u64 extent_start)
5826{
5827 memset(ref_path, 0, sizeof(*ref_path));
5828 ref_path->extent_start = extent_start;
5829
5830 return __next_ref_path(trans, extent_root, ref_path, 1);
5831}
5832
5833static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5834 struct btrfs_root *extent_root,
5835 struct btrfs_ref_path *ref_path)
5836{
5837 return __next_ref_path(trans, extent_root, ref_path, 0);
5838}
5839
Chris Masond3977122009-01-05 21:25:51 -05005840static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005841 struct btrfs_key *extent_key,
5842 u64 offset, int no_fragment,
5843 struct disk_extent **extents,
5844 int *nr_extents)
5845{
5846 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5847 struct btrfs_path *path;
5848 struct btrfs_file_extent_item *fi;
5849 struct extent_buffer *leaf;
5850 struct disk_extent *exts = *extents;
5851 struct btrfs_key found_key;
5852 u64 cur_pos;
5853 u64 last_byte;
5854 u32 nritems;
5855 int nr = 0;
5856 int max = *nr_extents;
5857 int ret;
5858
5859 WARN_ON(!no_fragment && *extents);
5860 if (!exts) {
5861 max = 1;
5862 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
5863 if (!exts)
5864 return -ENOMEM;
5865 }
5866
5867 path = btrfs_alloc_path();
5868 BUG_ON(!path);
5869
5870 cur_pos = extent_key->objectid - offset;
5871 last_byte = extent_key->objectid + extent_key->offset;
5872 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
5873 cur_pos, 0);
5874 if (ret < 0)
5875 goto out;
5876 if (ret > 0) {
5877 ret = -ENOENT;
5878 goto out;
5879 }
5880
5881 while (1) {
5882 leaf = path->nodes[0];
5883 nritems = btrfs_header_nritems(leaf);
5884 if (path->slots[0] >= nritems) {
5885 ret = btrfs_next_leaf(root, path);
5886 if (ret < 0)
5887 goto out;
5888 if (ret > 0)
5889 break;
5890 leaf = path->nodes[0];
5891 }
5892
5893 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5894 if (found_key.offset != cur_pos ||
5895 found_key.type != BTRFS_EXTENT_DATA_KEY ||
5896 found_key.objectid != reloc_inode->i_ino)
5897 break;
5898
5899 fi = btrfs_item_ptr(leaf, path->slots[0],
5900 struct btrfs_file_extent_item);
5901 if (btrfs_file_extent_type(leaf, fi) !=
5902 BTRFS_FILE_EXTENT_REG ||
5903 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5904 break;
5905
5906 if (nr == max) {
5907 struct disk_extent *old = exts;
5908 max *= 2;
5909 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
5910 memcpy(exts, old, sizeof(*exts) * nr);
5911 if (old != *extents)
5912 kfree(old);
5913 }
5914
5915 exts[nr].disk_bytenr =
5916 btrfs_file_extent_disk_bytenr(leaf, fi);
5917 exts[nr].disk_num_bytes =
5918 btrfs_file_extent_disk_num_bytes(leaf, fi);
5919 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
5920 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04005921 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
5922 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
5923 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
5924 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
5925 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04005926 BUG_ON(exts[nr].offset > 0);
5927 BUG_ON(exts[nr].compression || exts[nr].encryption);
5928 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005929
5930 cur_pos += exts[nr].num_bytes;
5931 nr++;
5932
5933 if (cur_pos + offset >= last_byte)
5934 break;
5935
5936 if (no_fragment) {
5937 ret = 1;
5938 goto out;
5939 }
5940 path->slots[0]++;
5941 }
5942
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005943 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04005944 if (cur_pos + offset < last_byte) {
5945 ret = -ENOENT;
5946 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05005947 }
5948 ret = 0;
5949out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005950 btrfs_free_path(path);
5951 if (ret) {
5952 if (exts != *extents)
5953 kfree(exts);
5954 } else {
5955 *extents = exts;
5956 *nr_extents = nr;
5957 }
5958 return ret;
5959}
5960
Chris Masond3977122009-01-05 21:25:51 -05005961static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005962 struct btrfs_root *root,
5963 struct btrfs_path *path,
5964 struct btrfs_key *extent_key,
5965 struct btrfs_key *leaf_key,
5966 struct btrfs_ref_path *ref_path,
5967 struct disk_extent *new_extents,
5968 int nr_extents)
5969{
5970 struct extent_buffer *leaf;
5971 struct btrfs_file_extent_item *fi;
5972 struct inode *inode = NULL;
5973 struct btrfs_key key;
5974 u64 lock_start = 0;
5975 u64 lock_end = 0;
5976 u64 num_bytes;
5977 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05005978 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005979 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005980 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005981 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04005982 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04005983 int ret;
5984
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005985 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04005986 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005987 if (key.objectid < ref_path->owner_objectid ||
5988 (key.objectid == ref_path->owner_objectid &&
5989 key.type < BTRFS_EXTENT_DATA_KEY)) {
5990 key.objectid = ref_path->owner_objectid;
5991 key.type = BTRFS_EXTENT_DATA_KEY;
5992 key.offset = 0;
5993 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005994 }
5995
5996 while (1) {
5997 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5998 if (ret < 0)
5999 goto out;
6000
6001 leaf = path->nodes[0];
6002 nritems = btrfs_header_nritems(leaf);
6003next:
6004 if (extent_locked && ret > 0) {
6005 /*
6006 * the file extent item was modified by someone
6007 * before the extent got locked.
6008 */
Zheng Yan1a40e232008-09-26 10:09:34 -04006009 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6010 lock_end, GFP_NOFS);
6011 extent_locked = 0;
6012 }
6013
6014 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006015 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04006016 break;
6017
6018 BUG_ON(extent_locked);
6019 ret = btrfs_next_leaf(root, path);
6020 if (ret < 0)
6021 goto out;
6022 if (ret > 0)
6023 break;
6024 leaf = path->nodes[0];
6025 nritems = btrfs_header_nritems(leaf);
6026 }
6027
6028 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6029
6030 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6031 if ((key.objectid > ref_path->owner_objectid) ||
6032 (key.objectid == ref_path->owner_objectid &&
6033 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05006034 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006035 break;
6036 }
6037
6038 if (inode && key.objectid != inode->i_ino) {
6039 BUG_ON(extent_locked);
6040 btrfs_release_path(root, path);
6041 mutex_unlock(&inode->i_mutex);
6042 iput(inode);
6043 inode = NULL;
6044 continue;
6045 }
6046
6047 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6048 path->slots[0]++;
6049 ret = 1;
6050 goto next;
6051 }
6052 fi = btrfs_item_ptr(leaf, path->slots[0],
6053 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04006054 extent_type = btrfs_file_extent_type(leaf, fi);
6055 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6056 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04006057 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6058 extent_key->objectid)) {
6059 path->slots[0]++;
6060 ret = 1;
6061 goto next;
6062 }
6063
6064 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6065 ext_offset = btrfs_file_extent_offset(leaf, fi);
6066
Yan Zheng86288a12009-01-21 10:49:16 -05006067 if (search_end == (u64)-1) {
6068 search_end = key.offset - ext_offset +
6069 btrfs_file_extent_ram_bytes(leaf, fi);
6070 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006071
6072 if (!extent_locked) {
6073 lock_start = key.offset;
6074 lock_end = lock_start + num_bytes - 1;
6075 } else {
Yan Zheng66435582008-10-30 14:19:50 -04006076 if (lock_start > key.offset ||
6077 lock_end + 1 < key.offset + num_bytes) {
6078 unlock_extent(&BTRFS_I(inode)->io_tree,
6079 lock_start, lock_end, GFP_NOFS);
6080 extent_locked = 0;
6081 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006082 }
6083
6084 if (!inode) {
6085 btrfs_release_path(root, path);
6086
6087 inode = btrfs_iget_locked(root->fs_info->sb,
6088 key.objectid, root);
6089 if (inode->i_state & I_NEW) {
6090 BTRFS_I(inode)->root = root;
6091 BTRFS_I(inode)->location.objectid =
6092 key.objectid;
6093 BTRFS_I(inode)->location.type =
6094 BTRFS_INODE_ITEM_KEY;
6095 BTRFS_I(inode)->location.offset = 0;
6096 btrfs_read_locked_inode(inode);
6097 unlock_new_inode(inode);
6098 }
6099 /*
6100 * some code call btrfs_commit_transaction while
6101 * holding the i_mutex, so we can't use mutex_lock
6102 * here.
6103 */
6104 if (is_bad_inode(inode) ||
6105 !mutex_trylock(&inode->i_mutex)) {
6106 iput(inode);
6107 inode = NULL;
6108 key.offset = (u64)-1;
6109 goto skip;
6110 }
6111 }
6112
6113 if (!extent_locked) {
6114 struct btrfs_ordered_extent *ordered;
6115
6116 btrfs_release_path(root, path);
6117
6118 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6119 lock_end, GFP_NOFS);
6120 ordered = btrfs_lookup_first_ordered_extent(inode,
6121 lock_end);
6122 if (ordered &&
6123 ordered->file_offset <= lock_end &&
6124 ordered->file_offset + ordered->len > lock_start) {
6125 unlock_extent(&BTRFS_I(inode)->io_tree,
6126 lock_start, lock_end, GFP_NOFS);
6127 btrfs_start_ordered_extent(inode, ordered, 1);
6128 btrfs_put_ordered_extent(ordered);
6129 key.offset += num_bytes;
6130 goto skip;
6131 }
6132 if (ordered)
6133 btrfs_put_ordered_extent(ordered);
6134
Zheng Yan1a40e232008-09-26 10:09:34 -04006135 extent_locked = 1;
6136 continue;
6137 }
6138
6139 if (nr_extents == 1) {
6140 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04006141 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6142 new_extents[0].disk_bytenr);
6143 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6144 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006145 btrfs_mark_buffer_dirty(leaf);
6146
6147 btrfs_drop_extent_cache(inode, key.offset,
6148 key.offset + num_bytes - 1, 0);
6149
6150 ret = btrfs_inc_extent_ref(trans, root,
6151 new_extents[0].disk_bytenr,
6152 new_extents[0].disk_num_bytes,
6153 leaf->start,
6154 root->root_key.objectid,
6155 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006156 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006157 BUG_ON(ret);
6158
6159 ret = btrfs_free_extent(trans, root,
6160 extent_key->objectid,
6161 extent_key->offset,
6162 leaf->start,
6163 btrfs_header_owner(leaf),
6164 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006165 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006166 BUG_ON(ret);
6167
6168 btrfs_release_path(root, path);
6169 key.offset += num_bytes;
6170 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04006171 BUG_ON(1);
6172#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006173 u64 alloc_hint;
6174 u64 extent_len;
6175 int i;
6176 /*
6177 * drop old extent pointer at first, then insert the
6178 * new pointers one bye one
6179 */
6180 btrfs_release_path(root, path);
6181 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6182 key.offset + num_bytes,
6183 key.offset, &alloc_hint);
6184 BUG_ON(ret);
6185
6186 for (i = 0; i < nr_extents; i++) {
6187 if (ext_offset >= new_extents[i].num_bytes) {
6188 ext_offset -= new_extents[i].num_bytes;
6189 continue;
6190 }
6191 extent_len = min(new_extents[i].num_bytes -
6192 ext_offset, num_bytes);
6193
6194 ret = btrfs_insert_empty_item(trans, root,
6195 path, &key,
6196 sizeof(*fi));
6197 BUG_ON(ret);
6198
6199 leaf = path->nodes[0];
6200 fi = btrfs_item_ptr(leaf, path->slots[0],
6201 struct btrfs_file_extent_item);
6202 btrfs_set_file_extent_generation(leaf, fi,
6203 trans->transid);
6204 btrfs_set_file_extent_type(leaf, fi,
6205 BTRFS_FILE_EXTENT_REG);
6206 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6207 new_extents[i].disk_bytenr);
6208 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6209 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04006210 btrfs_set_file_extent_ram_bytes(leaf, fi,
6211 new_extents[i].ram_bytes);
6212
6213 btrfs_set_file_extent_compression(leaf, fi,
6214 new_extents[i].compression);
6215 btrfs_set_file_extent_encryption(leaf, fi,
6216 new_extents[i].encryption);
6217 btrfs_set_file_extent_other_encoding(leaf, fi,
6218 new_extents[i].other_encoding);
6219
Zheng Yan1a40e232008-09-26 10:09:34 -04006220 btrfs_set_file_extent_num_bytes(leaf, fi,
6221 extent_len);
6222 ext_offset += new_extents[i].offset;
6223 btrfs_set_file_extent_offset(leaf, fi,
6224 ext_offset);
6225 btrfs_mark_buffer_dirty(leaf);
6226
6227 btrfs_drop_extent_cache(inode, key.offset,
6228 key.offset + extent_len - 1, 0);
6229
6230 ret = btrfs_inc_extent_ref(trans, root,
6231 new_extents[i].disk_bytenr,
6232 new_extents[i].disk_num_bytes,
6233 leaf->start,
6234 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006235 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006236 BUG_ON(ret);
6237 btrfs_release_path(root, path);
6238
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006239 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04006240
6241 ext_offset = 0;
6242 num_bytes -= extent_len;
6243 key.offset += extent_len;
6244
6245 if (num_bytes == 0)
6246 break;
6247 }
6248 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006249#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04006250 }
6251
6252 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006253 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6254 lock_end, GFP_NOFS);
6255 extent_locked = 0;
6256 }
6257skip:
6258 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05006259 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006260 break;
6261
6262 cond_resched();
6263 }
6264 ret = 0;
6265out:
6266 btrfs_release_path(root, path);
6267 if (inode) {
6268 mutex_unlock(&inode->i_mutex);
6269 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006270 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6271 lock_end, GFP_NOFS);
6272 }
6273 iput(inode);
6274 }
6275 return ret;
6276}
6277
Zheng Yan1a40e232008-09-26 10:09:34 -04006278int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6279 struct btrfs_root *root,
6280 struct extent_buffer *buf, u64 orig_start)
6281{
6282 int level;
6283 int ret;
6284
6285 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6286 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6287
6288 level = btrfs_header_level(buf);
6289 if (level == 0) {
6290 struct btrfs_leaf_ref *ref;
6291 struct btrfs_leaf_ref *orig_ref;
6292
6293 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6294 if (!orig_ref)
6295 return -ENOENT;
6296
6297 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6298 if (!ref) {
6299 btrfs_free_leaf_ref(root, orig_ref);
6300 return -ENOMEM;
6301 }
6302
6303 ref->nritems = orig_ref->nritems;
6304 memcpy(ref->extents, orig_ref->extents,
6305 sizeof(ref->extents[0]) * ref->nritems);
6306
6307 btrfs_free_leaf_ref(root, orig_ref);
6308
6309 ref->root_gen = trans->transid;
6310 ref->bytenr = buf->start;
6311 ref->owner = btrfs_header_owner(buf);
6312 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006313
Zheng Yan1a40e232008-09-26 10:09:34 -04006314 ret = btrfs_add_leaf_ref(root, ref, 0);
6315 WARN_ON(ret);
6316 btrfs_free_leaf_ref(root, ref);
6317 }
6318 return 0;
6319}
6320
Chris Masond3977122009-01-05 21:25:51 -05006321static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006322 struct extent_buffer *leaf,
6323 struct btrfs_block_group_cache *group,
6324 struct btrfs_root *target_root)
6325{
6326 struct btrfs_key key;
6327 struct inode *inode = NULL;
6328 struct btrfs_file_extent_item *fi;
6329 u64 num_bytes;
6330 u64 skip_objectid = 0;
6331 u32 nritems;
6332 u32 i;
6333
6334 nritems = btrfs_header_nritems(leaf);
6335 for (i = 0; i < nritems; i++) {
6336 btrfs_item_key_to_cpu(leaf, &key, i);
6337 if (key.objectid == skip_objectid ||
6338 key.type != BTRFS_EXTENT_DATA_KEY)
6339 continue;
6340 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6341 if (btrfs_file_extent_type(leaf, fi) ==
6342 BTRFS_FILE_EXTENT_INLINE)
6343 continue;
6344 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6345 continue;
6346 if (!inode || inode->i_ino != key.objectid) {
6347 iput(inode);
6348 inode = btrfs_ilookup(target_root->fs_info->sb,
6349 key.objectid, target_root, 1);
6350 }
6351 if (!inode) {
6352 skip_objectid = key.objectid;
6353 continue;
6354 }
6355 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6356
6357 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6358 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006359 btrfs_drop_extent_cache(inode, key.offset,
6360 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006361 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6362 key.offset + num_bytes - 1, GFP_NOFS);
6363 cond_resched();
6364 }
6365 iput(inode);
6366 return 0;
6367}
6368
Chris Masond3977122009-01-05 21:25:51 -05006369static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006370 struct btrfs_root *root,
6371 struct extent_buffer *leaf,
6372 struct btrfs_block_group_cache *group,
6373 struct inode *reloc_inode)
6374{
6375 struct btrfs_key key;
6376 struct btrfs_key extent_key;
6377 struct btrfs_file_extent_item *fi;
6378 struct btrfs_leaf_ref *ref;
6379 struct disk_extent *new_extent;
6380 u64 bytenr;
6381 u64 num_bytes;
6382 u32 nritems;
6383 u32 i;
6384 int ext_index;
6385 int nr_extent;
6386 int ret;
6387
6388 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6389 BUG_ON(!new_extent);
6390
6391 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6392 BUG_ON(!ref);
6393
6394 ext_index = -1;
6395 nritems = btrfs_header_nritems(leaf);
6396 for (i = 0; i < nritems; i++) {
6397 btrfs_item_key_to_cpu(leaf, &key, i);
6398 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6399 continue;
6400 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6401 if (btrfs_file_extent_type(leaf, fi) ==
6402 BTRFS_FILE_EXTENT_INLINE)
6403 continue;
6404 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6405 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6406 if (bytenr == 0)
6407 continue;
6408
6409 ext_index++;
6410 if (bytenr >= group->key.objectid + group->key.offset ||
6411 bytenr + num_bytes <= group->key.objectid)
6412 continue;
6413
6414 extent_key.objectid = bytenr;
6415 extent_key.offset = num_bytes;
6416 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6417 nr_extent = 1;
6418 ret = get_new_locations(reloc_inode, &extent_key,
6419 group->key.objectid, 1,
6420 &new_extent, &nr_extent);
6421 if (ret > 0)
6422 continue;
6423 BUG_ON(ret < 0);
6424
6425 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6426 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6427 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6428 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6429
Zheng Yan1a40e232008-09-26 10:09:34 -04006430 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6431 new_extent->disk_bytenr);
6432 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6433 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006434 btrfs_mark_buffer_dirty(leaf);
6435
6436 ret = btrfs_inc_extent_ref(trans, root,
6437 new_extent->disk_bytenr,
6438 new_extent->disk_num_bytes,
6439 leaf->start,
6440 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006441 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006442 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006443
Zheng Yan1a40e232008-09-26 10:09:34 -04006444 ret = btrfs_free_extent(trans, root,
6445 bytenr, num_bytes, leaf->start,
6446 btrfs_header_owner(leaf),
6447 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006448 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006449 BUG_ON(ret);
6450 cond_resched();
6451 }
6452 kfree(new_extent);
6453 BUG_ON(ext_index + 1 != ref->nritems);
6454 btrfs_free_leaf_ref(root, ref);
6455 return 0;
6456}
6457
Yan Zhengf82d02d2008-10-29 14:49:05 -04006458int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6459 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006460{
6461 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006462 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006463
6464 if (root->reloc_root) {
6465 reloc_root = root->reloc_root;
6466 root->reloc_root = NULL;
6467 list_add(&reloc_root->dead_list,
6468 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006469
6470 btrfs_set_root_bytenr(&reloc_root->root_item,
6471 reloc_root->node->start);
6472 btrfs_set_root_level(&root->root_item,
6473 btrfs_header_level(reloc_root->node));
6474 memset(&reloc_root->root_item.drop_progress, 0,
6475 sizeof(struct btrfs_disk_key));
6476 reloc_root->root_item.drop_level = 0;
6477
6478 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6479 &reloc_root->root_key,
6480 &reloc_root->root_item);
6481 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006482 }
6483 return 0;
6484}
6485
6486int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6487{
6488 struct btrfs_trans_handle *trans;
6489 struct btrfs_root *reloc_root;
6490 struct btrfs_root *prev_root = NULL;
6491 struct list_head dead_roots;
6492 int ret;
6493 unsigned long nr;
6494
6495 INIT_LIST_HEAD(&dead_roots);
6496 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6497
6498 while (!list_empty(&dead_roots)) {
6499 reloc_root = list_entry(dead_roots.prev,
6500 struct btrfs_root, dead_list);
6501 list_del_init(&reloc_root->dead_list);
6502
6503 BUG_ON(reloc_root->commit_root != NULL);
6504 while (1) {
6505 trans = btrfs_join_transaction(root, 1);
6506 BUG_ON(!trans);
6507
6508 mutex_lock(&root->fs_info->drop_mutex);
6509 ret = btrfs_drop_snapshot(trans, reloc_root);
6510 if (ret != -EAGAIN)
6511 break;
6512 mutex_unlock(&root->fs_info->drop_mutex);
6513
6514 nr = trans->blocks_used;
6515 ret = btrfs_end_transaction(trans, root);
6516 BUG_ON(ret);
6517 btrfs_btree_balance_dirty(root, nr);
6518 }
6519
6520 free_extent_buffer(reloc_root->node);
6521
6522 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6523 &reloc_root->root_key);
6524 BUG_ON(ret);
6525 mutex_unlock(&root->fs_info->drop_mutex);
6526
6527 nr = trans->blocks_used;
6528 ret = btrfs_end_transaction(trans, root);
6529 BUG_ON(ret);
6530 btrfs_btree_balance_dirty(root, nr);
6531
6532 kfree(prev_root);
6533 prev_root = reloc_root;
6534 }
6535 if (prev_root) {
6536 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6537 kfree(prev_root);
6538 }
6539 return 0;
6540}
6541
6542int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6543{
6544 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6545 return 0;
6546}
6547
6548int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6549{
6550 struct btrfs_root *reloc_root;
6551 struct btrfs_trans_handle *trans;
6552 struct btrfs_key location;
6553 int found;
6554 int ret;
6555
6556 mutex_lock(&root->fs_info->tree_reloc_mutex);
6557 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6558 BUG_ON(ret);
6559 found = !list_empty(&root->fs_info->dead_reloc_roots);
6560 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6561
6562 if (found) {
6563 trans = btrfs_start_transaction(root, 1);
6564 BUG_ON(!trans);
6565 ret = btrfs_commit_transaction(trans, root);
6566 BUG_ON(ret);
6567 }
6568
6569 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6570 location.offset = (u64)-1;
6571 location.type = BTRFS_ROOT_ITEM_KEY;
6572
6573 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6574 BUG_ON(!reloc_root);
6575 btrfs_orphan_cleanup(reloc_root);
6576 return 0;
6577}
6578
Chris Masond3977122009-01-05 21:25:51 -05006579static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006580 struct btrfs_root *root)
6581{
6582 struct btrfs_root *reloc_root;
6583 struct extent_buffer *eb;
6584 struct btrfs_root_item *root_item;
6585 struct btrfs_key root_key;
6586 int ret;
6587
6588 BUG_ON(!root->ref_cows);
6589 if (root->reloc_root)
6590 return 0;
6591
6592 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6593 BUG_ON(!root_item);
6594
6595 ret = btrfs_copy_root(trans, root, root->commit_root,
6596 &eb, BTRFS_TREE_RELOC_OBJECTID);
6597 BUG_ON(ret);
6598
6599 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6600 root_key.offset = root->root_key.objectid;
6601 root_key.type = BTRFS_ROOT_ITEM_KEY;
6602
6603 memcpy(root_item, &root->root_item, sizeof(root_item));
6604 btrfs_set_root_refs(root_item, 0);
6605 btrfs_set_root_bytenr(root_item, eb->start);
6606 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006607 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006608
6609 btrfs_tree_unlock(eb);
6610 free_extent_buffer(eb);
6611
6612 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6613 &root_key, root_item);
6614 BUG_ON(ret);
6615 kfree(root_item);
6616
6617 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6618 &root_key);
6619 BUG_ON(!reloc_root);
6620 reloc_root->last_trans = trans->transid;
6621 reloc_root->commit_root = NULL;
6622 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6623
6624 root->reloc_root = reloc_root;
6625 return 0;
6626}
6627
6628/*
6629 * Core function of space balance.
6630 *
6631 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006632 * counted roots. There is one reloc tree for each subvol, and all
6633 * reloc trees share same root key objectid. Reloc trees are snapshots
6634 * of the latest committed roots of subvols (root->commit_root).
6635 *
6636 * To relocate a tree block referenced by a subvol, there are two steps.
6637 * COW the block through subvol's reloc tree, then update block pointer
6638 * in the subvol to point to the new block. Since all reloc trees share
6639 * same root key objectid, doing special handing for tree blocks owned
6640 * by them is easy. Once a tree block has been COWed in one reloc tree,
6641 * we can use the resulting new block directly when the same block is
6642 * required to COW again through other reloc trees. By this way, relocated
6643 * tree blocks are shared between reloc trees, so they are also shared
6644 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006645 */
Chris Masond3977122009-01-05 21:25:51 -05006646static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006647 struct btrfs_root *root,
6648 struct btrfs_path *path,
6649 struct btrfs_key *first_key,
6650 struct btrfs_ref_path *ref_path,
6651 struct btrfs_block_group_cache *group,
6652 struct inode *reloc_inode)
6653{
6654 struct btrfs_root *reloc_root;
6655 struct extent_buffer *eb = NULL;
6656 struct btrfs_key *keys;
6657 u64 *nodes;
6658 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006659 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006660 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006661 int ret;
6662
6663 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6664 lowest_level = ref_path->owner_objectid;
6665
Yan Zhengf82d02d2008-10-29 14:49:05 -04006666 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006667 path->lowest_level = lowest_level;
6668 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6669 BUG_ON(ret < 0);
6670 path->lowest_level = 0;
6671 btrfs_release_path(root, path);
6672 return 0;
6673 }
6674
Zheng Yan1a40e232008-09-26 10:09:34 -04006675 mutex_lock(&root->fs_info->tree_reloc_mutex);
6676 ret = init_reloc_tree(trans, root);
6677 BUG_ON(ret);
6678 reloc_root = root->reloc_root;
6679
Yan Zhengf82d02d2008-10-29 14:49:05 -04006680 shared_level = ref_path->shared_level;
6681 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006682
Yan Zhengf82d02d2008-10-29 14:49:05 -04006683 keys = ref_path->node_keys;
6684 nodes = ref_path->new_nodes;
6685 memset(&keys[shared_level + 1], 0,
6686 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6687 memset(&nodes[shared_level + 1], 0,
6688 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006689
Yan Zhengf82d02d2008-10-29 14:49:05 -04006690 if (nodes[lowest_level] == 0) {
6691 path->lowest_level = lowest_level;
6692 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6693 0, 1);
6694 BUG_ON(ret);
6695 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6696 eb = path->nodes[level];
6697 if (!eb || eb == reloc_root->node)
6698 break;
6699 nodes[level] = eb->start;
6700 if (level == 0)
6701 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6702 else
6703 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6704 }
Yan Zheng2b820322008-11-17 21:11:30 -05006705 if (nodes[0] &&
6706 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006707 eb = path->nodes[0];
6708 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6709 group, reloc_inode);
6710 BUG_ON(ret);
6711 }
6712 btrfs_release_path(reloc_root, path);
6713 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006714 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006715 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006716 BUG_ON(ret);
6717 }
6718
Zheng Yan1a40e232008-09-26 10:09:34 -04006719 /*
6720 * replace tree blocks in the fs tree with tree blocks in
6721 * the reloc tree.
6722 */
6723 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6724 BUG_ON(ret < 0);
6725
6726 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006727 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6728 0, 0);
6729 BUG_ON(ret);
6730 extent_buffer_get(path->nodes[0]);
6731 eb = path->nodes[0];
6732 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006733 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6734 BUG_ON(ret);
6735 free_extent_buffer(eb);
6736 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006737
Yan Zhengf82d02d2008-10-29 14:49:05 -04006738 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006739 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006740 return 0;
6741}
6742
Chris Masond3977122009-01-05 21:25:51 -05006743static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006744 struct btrfs_root *root,
6745 struct btrfs_path *path,
6746 struct btrfs_key *first_key,
6747 struct btrfs_ref_path *ref_path)
6748{
6749 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006750
6751 ret = relocate_one_path(trans, root, path, first_key,
6752 ref_path, NULL, NULL);
6753 BUG_ON(ret);
6754
Zheng Yan1a40e232008-09-26 10:09:34 -04006755 return 0;
6756}
6757
Chris Masond3977122009-01-05 21:25:51 -05006758static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006759 struct btrfs_root *extent_root,
6760 struct btrfs_path *path,
6761 struct btrfs_key *extent_key)
6762{
6763 int ret;
6764
Zheng Yan1a40e232008-09-26 10:09:34 -04006765 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6766 if (ret)
6767 goto out;
6768 ret = btrfs_del_item(trans, extent_root, path);
6769out:
Chris Masonedbd8d42007-12-21 16:27:24 -05006770 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006771 return ret;
6772}
6773
Chris Masond3977122009-01-05 21:25:51 -05006774static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006775 struct btrfs_ref_path *ref_path)
6776{
6777 struct btrfs_key root_key;
6778
6779 root_key.objectid = ref_path->root_objectid;
6780 root_key.type = BTRFS_ROOT_ITEM_KEY;
6781 if (is_cowonly_root(ref_path->root_objectid))
6782 root_key.offset = 0;
6783 else
6784 root_key.offset = (u64)-1;
6785
6786 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6787}
6788
Chris Masond3977122009-01-05 21:25:51 -05006789static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006790 struct btrfs_path *path,
6791 struct btrfs_key *extent_key,
6792 struct btrfs_block_group_cache *group,
6793 struct inode *reloc_inode, int pass)
6794{
6795 struct btrfs_trans_handle *trans;
6796 struct btrfs_root *found_root;
6797 struct btrfs_ref_path *ref_path = NULL;
6798 struct disk_extent *new_extents = NULL;
6799 int nr_extents = 0;
6800 int loops;
6801 int ret;
6802 int level;
6803 struct btrfs_key first_key;
6804 u64 prev_block = 0;
6805
Zheng Yan1a40e232008-09-26 10:09:34 -04006806
6807 trans = btrfs_start_transaction(extent_root, 1);
6808 BUG_ON(!trans);
6809
6810 if (extent_key->objectid == 0) {
6811 ret = del_extent_zero(trans, extent_root, path, extent_key);
6812 goto out;
6813 }
6814
6815 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6816 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05006817 ret = -ENOMEM;
6818 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006819 }
6820
6821 for (loops = 0; ; loops++) {
6822 if (loops == 0) {
6823 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6824 extent_key->objectid);
6825 } else {
6826 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6827 }
6828 if (ret < 0)
6829 goto out;
6830 if (ret > 0)
6831 break;
6832
6833 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6834 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6835 continue;
6836
6837 found_root = read_ref_root(extent_root->fs_info, ref_path);
6838 BUG_ON(!found_root);
6839 /*
6840 * for reference counted tree, only process reference paths
6841 * rooted at the latest committed root.
6842 */
6843 if (found_root->ref_cows &&
6844 ref_path->root_generation != found_root->root_key.offset)
6845 continue;
6846
6847 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6848 if (pass == 0) {
6849 /*
6850 * copy data extents to new locations
6851 */
6852 u64 group_start = group->key.objectid;
6853 ret = relocate_data_extent(reloc_inode,
6854 extent_key,
6855 group_start);
6856 if (ret < 0)
6857 goto out;
6858 break;
6859 }
6860 level = 0;
6861 } else {
6862 level = ref_path->owner_objectid;
6863 }
6864
6865 if (prev_block != ref_path->nodes[level]) {
6866 struct extent_buffer *eb;
6867 u64 block_start = ref_path->nodes[level];
6868 u64 block_size = btrfs_level_size(found_root, level);
6869
6870 eb = read_tree_block(found_root, block_start,
6871 block_size, 0);
6872 btrfs_tree_lock(eb);
6873 BUG_ON(level != btrfs_header_level(eb));
6874
6875 if (level == 0)
6876 btrfs_item_key_to_cpu(eb, &first_key, 0);
6877 else
6878 btrfs_node_key_to_cpu(eb, &first_key, 0);
6879
6880 btrfs_tree_unlock(eb);
6881 free_extent_buffer(eb);
6882 prev_block = block_start;
6883 }
6884
Yan Zheng24562422009-02-12 14:14:53 -05006885 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006886 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05006887 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006888 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6889 /*
6890 * try to update data extent references while
6891 * keeping metadata shared between snapshots.
6892 */
6893 if (pass == 1) {
6894 ret = relocate_one_path(trans, found_root,
6895 path, &first_key, ref_path,
6896 group, reloc_inode);
6897 if (ret < 0)
6898 goto out;
6899 continue;
6900 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006901 /*
6902 * use fallback method to process the remaining
6903 * references.
6904 */
6905 if (!new_extents) {
6906 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04006907 new_extents = kmalloc(sizeof(*new_extents),
6908 GFP_NOFS);
6909 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006910 ret = get_new_locations(reloc_inode,
6911 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04006912 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04006913 &new_extents,
6914 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006915 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04006916 goto out;
6917 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006918 ret = replace_one_extent(trans, found_root,
6919 path, extent_key,
6920 &first_key, ref_path,
6921 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05006922 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006923 ret = relocate_tree_block(trans, found_root, path,
6924 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006925 }
6926 if (ret < 0)
6927 goto out;
6928 }
6929 ret = 0;
6930out:
6931 btrfs_end_transaction(trans, extent_root);
6932 kfree(new_extents);
6933 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006934 return ret;
6935}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006936#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006937
Chris Masonec44a352008-04-28 15:29:52 -04006938static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6939{
6940 u64 num_devices;
6941 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6942 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6943
Yan Zheng2b820322008-11-17 21:11:30 -05006944 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04006945 if (num_devices == 1) {
6946 stripped |= BTRFS_BLOCK_GROUP_DUP;
6947 stripped = flags & ~stripped;
6948
6949 /* turn raid0 into single device chunks */
6950 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6951 return stripped;
6952
6953 /* turn mirroring into duplication */
6954 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6955 BTRFS_BLOCK_GROUP_RAID10))
6956 return stripped | BTRFS_BLOCK_GROUP_DUP;
6957 return flags;
6958 } else {
6959 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006960 if (flags & stripped)
6961 return flags;
6962
6963 stripped |= BTRFS_BLOCK_GROUP_DUP;
6964 stripped = flags & ~stripped;
6965
6966 /* switch duplicated blocks with raid1 */
6967 if (flags & BTRFS_BLOCK_GROUP_DUP)
6968 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6969
6970 /* turn single device chunks into raid0 */
6971 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6972 }
6973 return flags;
6974}
6975
Christoph Hellwigb2950862008-12-02 09:54:17 -05006976static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04006977 struct btrfs_block_group_cache *shrink_block_group,
6978 int force)
6979{
6980 struct btrfs_trans_handle *trans;
6981 u64 new_alloc_flags;
6982 u64 calc;
6983
Chris Masonc286ac42008-07-22 23:06:41 -04006984 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006985 if (btrfs_block_group_used(&shrink_block_group->item) +
6986 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04006987 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006988
Chris Mason0ef3e662008-05-24 14:04:53 -04006989 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006990 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04006991
Chris Mason0ef3e662008-05-24 14:04:53 -04006992 new_alloc_flags = update_block_group_flags(root,
6993 shrink_block_group->flags);
6994 if (new_alloc_flags != shrink_block_group->flags) {
6995 calc =
6996 btrfs_block_group_used(&shrink_block_group->item);
6997 } else {
6998 calc = shrink_block_group->key.offset;
6999 }
Chris Masonc286ac42008-07-22 23:06:41 -04007000 spin_unlock(&shrink_block_group->lock);
7001
Chris Mason0ef3e662008-05-24 14:04:53 -04007002 do_chunk_alloc(trans, root->fs_info->extent_root,
7003 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04007004
Chris Mason0ef3e662008-05-24 14:04:53 -04007005 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04007006 } else
7007 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04007008 return 0;
7009}
7010
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007011
7012int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
7013 struct btrfs_block_group_cache *group)
7014
7015{
7016 __alloc_chunk_for_shrink(root, group, 1);
7017 set_block_group_readonly(group);
7018 return 0;
7019}
7020
Josef Bacikba1bf482009-09-11 16:11:19 -04007021/*
7022 * checks to see if its even possible to relocate this block group.
7023 *
7024 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7025 * ok to go ahead and try.
7026 */
7027int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007028{
Zheng Yan1a40e232008-09-26 10:09:34 -04007029 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007030 struct btrfs_space_info *space_info;
7031 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7032 struct btrfs_device *device;
7033 int full = 0;
7034 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007035
Josef Bacikba1bf482009-09-11 16:11:19 -04007036 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007037
Josef Bacikba1bf482009-09-11 16:11:19 -04007038 /* odd, couldn't find the block group, leave it alone */
7039 if (!block_group)
7040 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007041
Josef Bacikba1bf482009-09-11 16:11:19 -04007042 /* no bytes used, we're good */
7043 if (!btrfs_block_group_used(&block_group->item))
7044 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007045
Josef Bacikba1bf482009-09-11 16:11:19 -04007046 space_info = block_group->space_info;
7047 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007048
Josef Bacikba1bf482009-09-11 16:11:19 -04007049 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007050
Josef Bacikba1bf482009-09-11 16:11:19 -04007051 /*
7052 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007053 * relocate it unless we're able to allocate a new chunk below.
7054 *
7055 * Otherwise, we need to make sure we have room in the space to handle
7056 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007057 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007058 if ((space_info->total_bytes != block_group->key.offset) &&
7059 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04007060 space_info->bytes_pinned + space_info->bytes_readonly +
7061 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04007062 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007063 spin_unlock(&space_info->lock);
7064 goto out;
7065 }
7066 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007067
Josef Bacikba1bf482009-09-11 16:11:19 -04007068 /*
7069 * ok we don't have enough space, but maybe we have free space on our
7070 * devices to allocate new chunks for relocation, so loop through our
7071 * alloc devices and guess if we have enough space. However, if we
7072 * were marked as full, then we know there aren't enough chunks, and we
7073 * can just return.
7074 */
7075 ret = -1;
7076 if (full)
7077 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05007078
Josef Bacikba1bf482009-09-11 16:11:19 -04007079 mutex_lock(&root->fs_info->chunk_mutex);
7080 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7081 u64 min_free = btrfs_block_group_used(&block_group->item);
7082 u64 dev_offset, max_avail;
Chris Masonea8c2812008-08-04 23:17:27 -04007083
Josef Bacikba1bf482009-09-11 16:11:19 -04007084 /*
7085 * check to make sure we can actually find a chunk with enough
7086 * space to fit our block group in.
7087 */
7088 if (device->total_bytes > device->bytes_used + min_free) {
7089 ret = find_free_dev_extent(NULL, device, min_free,
7090 &dev_offset, &max_avail);
7091 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05007092 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04007093 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007094 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007095 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007096 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007097out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007098 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007099 return ret;
7100}
7101
Christoph Hellwigb2950862008-12-02 09:54:17 -05007102static int find_first_block_group(struct btrfs_root *root,
7103 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007104{
Chris Mason925baed2008-06-25 16:01:30 -04007105 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007106 struct btrfs_key found_key;
7107 struct extent_buffer *leaf;
7108 int slot;
7109
7110 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7111 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007112 goto out;
7113
Chris Masond3977122009-01-05 21:25:51 -05007114 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007115 slot = path->slots[0];
7116 leaf = path->nodes[0];
7117 if (slot >= btrfs_header_nritems(leaf)) {
7118 ret = btrfs_next_leaf(root, path);
7119 if (ret == 0)
7120 continue;
7121 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007122 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007123 break;
7124 }
7125 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7126
7127 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007128 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7129 ret = 0;
7130 goto out;
7131 }
Chris Mason0b86a832008-03-24 15:01:56 -04007132 path->slots[0]++;
7133 }
7134 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04007135out:
Chris Mason0b86a832008-03-24 15:01:56 -04007136 return ret;
7137}
7138
Zheng Yan1a40e232008-09-26 10:09:34 -04007139int btrfs_free_block_groups(struct btrfs_fs_info *info)
7140{
7141 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007142 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007143 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007144 struct rb_node *n;
7145
Yan Zheng11833d62009-09-11 16:11:19 -04007146 down_write(&info->extent_commit_sem);
7147 while (!list_empty(&info->caching_block_groups)) {
7148 caching_ctl = list_entry(info->caching_block_groups.next,
7149 struct btrfs_caching_control, list);
7150 list_del(&caching_ctl->list);
7151 put_caching_control(caching_ctl);
7152 }
7153 up_write(&info->extent_commit_sem);
7154
Zheng Yan1a40e232008-09-26 10:09:34 -04007155 spin_lock(&info->block_group_cache_lock);
7156 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7157 block_group = rb_entry(n, struct btrfs_block_group_cache,
7158 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007159 rb_erase(&block_group->cache_node,
7160 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007161 spin_unlock(&info->block_group_cache_lock);
7162
Josef Bacik80eb2342008-10-29 14:49:05 -04007163 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007164 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007165 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007166
Josef Bacik817d52f2009-07-13 21:29:25 -04007167 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007168 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007169
7170 btrfs_remove_free_space_cache(block_group);
7171
Yan Zhengd2fb3432008-12-11 16:30:39 -05007172 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04007173 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007174
7175 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007176 }
7177 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007178
7179 /* now that all the block groups are freed, go through and
7180 * free all the space_info structs. This is only called during
7181 * the final stages of unmount, and so we know nobody is
7182 * using them. We call synchronize_rcu() once before we start,
7183 * just to be on the safe side.
7184 */
7185 synchronize_rcu();
7186
7187 while(!list_empty(&info->space_info)) {
7188 space_info = list_entry(info->space_info.next,
7189 struct btrfs_space_info,
7190 list);
7191
7192 list_del(&space_info->list);
7193 kfree(space_info);
7194 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007195 return 0;
7196}
7197
Chris Mason9078a3e2007-04-26 16:46:15 -04007198int btrfs_read_block_groups(struct btrfs_root *root)
7199{
7200 struct btrfs_path *path;
7201 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007202 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007203 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007204 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007205 struct btrfs_key key;
7206 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007207 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007208
Chris Masonbe744172007-05-06 10:15:01 -04007209 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007210 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007211 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007212 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007213 path = btrfs_alloc_path();
7214 if (!path)
7215 return -ENOMEM;
7216
Chris Masond3977122009-01-05 21:25:51 -05007217 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007218 ret = find_first_block_group(root, path, &key);
7219 if (ret > 0) {
7220 ret = 0;
7221 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007222 }
Chris Mason0b86a832008-03-24 15:01:56 -04007223 if (ret != 0)
7224 goto error;
7225
Chris Mason5f39d392007-10-15 16:14:19 -04007226 leaf = path->nodes[0];
7227 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007228 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007229 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007230 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04007231 break;
7232 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007233
Yan Zhengd2fb3432008-12-11 16:30:39 -05007234 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007235 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007236 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007237 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007238 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007239 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007240
7241 /*
7242 * we only want to have 32k of ram per block group for keeping
7243 * track of free space, and if we pass 1/2 of that we want to
7244 * start converting things over to using bitmaps
7245 */
7246 cache->extents_thresh = ((1024 * 32) / 2) /
7247 sizeof(struct btrfs_free_space);
7248
Chris Mason5f39d392007-10-15 16:14:19 -04007249 read_extent_buffer(leaf, &cache->item,
7250 btrfs_item_ptr_offset(leaf, path->slots[0]),
7251 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007252 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007253
Chris Mason9078a3e2007-04-26 16:46:15 -04007254 key.objectid = found_key.objectid + found_key.offset;
7255 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007256 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007257 cache->sectorsize = root->sectorsize;
7258
Josef Bacik817d52f2009-07-13 21:29:25 -04007259 /*
7260 * check for two cases, either we are full, and therefore
7261 * don't need to bother with the caching work since we won't
7262 * find any space, or we are empty, and we can just add all
7263 * the space in and be done with it. This saves us _alot_ of
7264 * time, particularly in the full case.
7265 */
7266 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04007267 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04007268 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007269 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007270 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007271 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007272 exclude_super_stripes(root, cache);
7273 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007274 cache->cached = BTRFS_CACHE_FINISHED;
7275 add_new_free_space(cache, root->fs_info,
7276 found_key.objectid,
7277 found_key.objectid +
7278 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007279 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007280 }
Chris Mason96b51792007-10-15 16:15:19 -04007281
Chris Mason6324fbf2008-03-24 15:01:59 -04007282 ret = update_space_info(info, cache->flags, found_key.offset,
7283 btrfs_block_group_used(&cache->item),
7284 &space_info);
7285 BUG_ON(ret);
7286 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007287 spin_lock(&cache->space_info->lock);
7288 cache->space_info->bytes_super += cache->bytes_super;
7289 spin_unlock(&cache->space_info->lock);
7290
Josef Bacik80eb2342008-10-29 14:49:05 -04007291 down_write(&space_info->groups_sem);
7292 list_add_tail(&cache->list, &space_info->block_groups);
7293 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007294
Josef Bacik0f9dd462008-09-23 13:14:11 -04007295 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7296 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007297
7298 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007299 if (btrfs_chunk_readonly(root, cache->key.objectid))
7300 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007301 }
Chris Mason0b86a832008-03-24 15:01:56 -04007302 ret = 0;
7303error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007304 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007305 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007306}
Chris Mason6324fbf2008-03-24 15:01:59 -04007307
7308int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7309 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007310 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007311 u64 size)
7312{
7313 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007314 struct btrfs_root *extent_root;
7315 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007316
7317 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007318
Chris Mason12fcfd22009-03-24 10:24:20 -04007319 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007320
Chris Mason8f18cf12008-04-25 16:53:30 -04007321 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007322 if (!cache)
7323 return -ENOMEM;
7324
Chris Masone17cade2008-04-15 15:41:47 -04007325 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007326 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007327 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007328 cache->sectorsize = root->sectorsize;
7329
7330 /*
7331 * we only want to have 32k of ram per block group for keeping track
7332 * of free space, and if we pass 1/2 of that we want to start
7333 * converting things over to using bitmaps
7334 */
7335 cache->extents_thresh = ((1024 * 32) / 2) /
7336 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007337 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007338 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007339 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007340 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007341 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007342
Chris Mason6324fbf2008-03-24 15:01:59 -04007343 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007344 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7345 cache->flags = type;
7346 btrfs_set_block_group_flags(&cache->item, type);
7347
Yan Zheng11833d62009-09-11 16:11:19 -04007348 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007349 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007350 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007351
Josef Bacik817d52f2009-07-13 21:29:25 -04007352 add_new_free_space(cache, root->fs_info, chunk_offset,
7353 chunk_offset + size);
7354
Yan Zheng11833d62009-09-11 16:11:19 -04007355 free_excluded_extents(root, cache);
7356
Chris Mason6324fbf2008-03-24 15:01:59 -04007357 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7358 &cache->space_info);
7359 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007360
7361 spin_lock(&cache->space_info->lock);
7362 cache->space_info->bytes_super += cache->bytes_super;
7363 spin_unlock(&cache->space_info->lock);
7364
Josef Bacik80eb2342008-10-29 14:49:05 -04007365 down_write(&cache->space_info->groups_sem);
7366 list_add_tail(&cache->list, &cache->space_info->block_groups);
7367 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007368
Josef Bacik0f9dd462008-09-23 13:14:11 -04007369 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7370 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007371
Chris Mason6324fbf2008-03-24 15:01:59 -04007372 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7373 sizeof(cache->item));
7374 BUG_ON(ret);
7375
Chris Masond18a2c42008-04-04 15:40:00 -04007376 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007377
Chris Mason6324fbf2008-03-24 15:01:59 -04007378 return 0;
7379}
Zheng Yan1a40e232008-09-26 10:09:34 -04007380
7381int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7382 struct btrfs_root *root, u64 group_start)
7383{
7384 struct btrfs_path *path;
7385 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007386 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007387 struct btrfs_key key;
7388 int ret;
7389
Zheng Yan1a40e232008-09-26 10:09:34 -04007390 root = root->fs_info->extent_root;
7391
7392 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7393 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007394 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007395
7396 memcpy(&key, &block_group->key, sizeof(key));
7397
Chris Mason44fb5512009-06-04 15:34:51 -04007398 /* make sure this block group isn't part of an allocation cluster */
7399 cluster = &root->fs_info->data_alloc_cluster;
7400 spin_lock(&cluster->refill_lock);
7401 btrfs_return_cluster_to_free_space(block_group, cluster);
7402 spin_unlock(&cluster->refill_lock);
7403
7404 /*
7405 * make sure this block group isn't part of a metadata
7406 * allocation cluster
7407 */
7408 cluster = &root->fs_info->meta_alloc_cluster;
7409 spin_lock(&cluster->refill_lock);
7410 btrfs_return_cluster_to_free_space(block_group, cluster);
7411 spin_unlock(&cluster->refill_lock);
7412
Zheng Yan1a40e232008-09-26 10:09:34 -04007413 path = btrfs_alloc_path();
7414 BUG_ON(!path);
7415
Yan Zheng3dfdb932009-01-21 10:49:16 -05007416 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007417 rb_erase(&block_group->cache_node,
7418 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007419 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007420
Josef Bacik80eb2342008-10-29 14:49:05 -04007421 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007422 /*
7423 * we must use list_del_init so people can check to see if they
7424 * are still on the list after taking the semaphore
7425 */
7426 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007427 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007428
Josef Bacik817d52f2009-07-13 21:29:25 -04007429 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007430 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007431
7432 btrfs_remove_free_space_cache(block_group);
7433
Yan Zhengc146afa2008-11-12 14:34:12 -05007434 spin_lock(&block_group->space_info->lock);
7435 block_group->space_info->total_bytes -= block_group->key.offset;
7436 block_group->space_info->bytes_readonly -= block_group->key.offset;
7437 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007438
7439 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007440
Chris Masonfa9c0d72009-04-03 09:47:43 -04007441 btrfs_put_block_group(block_group);
7442 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007443
7444 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7445 if (ret > 0)
7446 ret = -EIO;
7447 if (ret < 0)
7448 goto out;
7449
7450 ret = btrfs_del_item(trans, root, path);
7451out:
7452 btrfs_free_path(path);
7453 return ret;
7454}