blob: a4b2b03cd682b74cb5417765c466bb1ea5647da5 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050025#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050026#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027#include "ctree.h"
28#include "disk-io.h"
29#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040030#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040032#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040033#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Josef Bacikf3465ca2008-11-12 14:19:50 -050035static int update_block_group(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 u64 bytenr, u64 num_bytes, int alloc,
38 int mark_free);
Yan Zheng11833d62009-09-11 16:11:19 -040039static int update_reserved_extents(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040041static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root,
43 u64 bytenr, u64 num_bytes, u64 parent,
44 u64 root_objectid, u64 owner_objectid,
45 u64 owner_offset, int refs_to_drop,
46 struct btrfs_delayed_extent_op *extra_op);
47static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
48 struct extent_buffer *leaf,
49 struct btrfs_extent_item *ei);
50static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root,
52 u64 parent, u64 root_objectid,
53 u64 flags, u64 owner, u64 offset,
54 struct btrfs_key *ins, int ref_mod);
55static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 parent, u64 root_objectid,
58 u64 flags, struct btrfs_disk_key *key,
59 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050060static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61 struct btrfs_root *extent_root, u64 alloc_bytes,
62 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040063static int pin_down_bytes(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root,
65 struct btrfs_path *path,
66 u64 bytenr, u64 num_bytes,
67 int is_data, int reserved,
68 struct extent_buffer **must_clean);
69static int find_next_key(struct btrfs_path *path, int level,
70 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040071static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
72 int dump_block_groups);
Josef Bacik6a632092009-02-20 11:00:09 -050073
Josef Bacik817d52f2009-07-13 21:29:25 -040074static noinline int
75block_group_cache_done(struct btrfs_block_group_cache *cache)
76{
77 smp_mb();
78 return cache->cached == BTRFS_CACHE_FINISHED;
79}
80
Josef Bacik0f9dd462008-09-23 13:14:11 -040081static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
82{
83 return (cache->flags & bits) == bits;
84}
85
86/*
87 * this adds the block group to the fs_info rb tree for the block group
88 * cache
89 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050090static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040091 struct btrfs_block_group_cache *block_group)
92{
93 struct rb_node **p;
94 struct rb_node *parent = NULL;
95 struct btrfs_block_group_cache *cache;
96
97 spin_lock(&info->block_group_cache_lock);
98 p = &info->block_group_cache_tree.rb_node;
99
100 while (*p) {
101 parent = *p;
102 cache = rb_entry(parent, struct btrfs_block_group_cache,
103 cache_node);
104 if (block_group->key.objectid < cache->key.objectid) {
105 p = &(*p)->rb_left;
106 } else if (block_group->key.objectid > cache->key.objectid) {
107 p = &(*p)->rb_right;
108 } else {
109 spin_unlock(&info->block_group_cache_lock);
110 return -EEXIST;
111 }
112 }
113
114 rb_link_node(&block_group->cache_node, parent, p);
115 rb_insert_color(&block_group->cache_node,
116 &info->block_group_cache_tree);
117 spin_unlock(&info->block_group_cache_lock);
118
119 return 0;
120}
121
122/*
123 * This will return the block group at or after bytenr if contains is 0, else
124 * it will return the block group that contains the bytenr
125 */
126static struct btrfs_block_group_cache *
127block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
128 int contains)
129{
130 struct btrfs_block_group_cache *cache, *ret = NULL;
131 struct rb_node *n;
132 u64 end, start;
133
134 spin_lock(&info->block_group_cache_lock);
135 n = info->block_group_cache_tree.rb_node;
136
137 while (n) {
138 cache = rb_entry(n, struct btrfs_block_group_cache,
139 cache_node);
140 end = cache->key.objectid + cache->key.offset - 1;
141 start = cache->key.objectid;
142
143 if (bytenr < start) {
144 if (!contains && (!ret || start < ret->key.objectid))
145 ret = cache;
146 n = n->rb_left;
147 } else if (bytenr > start) {
148 if (contains && bytenr <= end) {
149 ret = cache;
150 break;
151 }
152 n = n->rb_right;
153 } else {
154 ret = cache;
155 break;
156 }
157 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500158 if (ret)
159 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400160 spin_unlock(&info->block_group_cache_lock);
161
162 return ret;
163}
164
Yan Zheng11833d62009-09-11 16:11:19 -0400165static int add_excluded_extent(struct btrfs_root *root,
166 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400167{
Yan Zheng11833d62009-09-11 16:11:19 -0400168 u64 end = start + num_bytes - 1;
169 set_extent_bits(&root->fs_info->freed_extents[0],
170 start, end, EXTENT_UPTODATE, GFP_NOFS);
171 set_extent_bits(&root->fs_info->freed_extents[1],
172 start, end, EXTENT_UPTODATE, GFP_NOFS);
173 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400174}
175
Yan Zheng11833d62009-09-11 16:11:19 -0400176static void free_excluded_extents(struct btrfs_root *root,
177 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400178{
Yan Zheng11833d62009-09-11 16:11:19 -0400179 u64 start, end;
180
181 start = cache->key.objectid;
182 end = start + cache->key.offset - 1;
183
184 clear_extent_bits(&root->fs_info->freed_extents[0],
185 start, end, EXTENT_UPTODATE, GFP_NOFS);
186 clear_extent_bits(&root->fs_info->freed_extents[1],
187 start, end, EXTENT_UPTODATE, GFP_NOFS);
188}
189
190static int exclude_super_stripes(struct btrfs_root *root,
191 struct btrfs_block_group_cache *cache)
192{
Josef Bacik817d52f2009-07-13 21:29:25 -0400193 u64 bytenr;
194 u64 *logical;
195 int stripe_len;
196 int i, nr, ret;
197
198 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
199 bytenr = btrfs_sb_offset(i);
200 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
201 cache->key.objectid, bytenr,
202 0, &logical, &nr, &stripe_len);
203 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400204
Josef Bacik817d52f2009-07-13 21:29:25 -0400205 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400206 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400207 ret = add_excluded_extent(root, logical[nr],
208 stripe_len);
209 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400210 }
Yan Zheng11833d62009-09-11 16:11:19 -0400211
Josef Bacik817d52f2009-07-13 21:29:25 -0400212 kfree(logical);
213 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400214 return 0;
215}
216
Yan Zheng11833d62009-09-11 16:11:19 -0400217static struct btrfs_caching_control *
218get_caching_control(struct btrfs_block_group_cache *cache)
219{
220 struct btrfs_caching_control *ctl;
221
222 spin_lock(&cache->lock);
223 if (cache->cached != BTRFS_CACHE_STARTED) {
224 spin_unlock(&cache->lock);
225 return NULL;
226 }
227
228 ctl = cache->caching_ctl;
229 atomic_inc(&ctl->count);
230 spin_unlock(&cache->lock);
231 return ctl;
232}
233
234static void put_caching_control(struct btrfs_caching_control *ctl)
235{
236 if (atomic_dec_and_test(&ctl->count))
237 kfree(ctl);
238}
239
Josef Bacik0f9dd462008-09-23 13:14:11 -0400240/*
241 * this is only called by cache_block_group, since we could have freed extents
242 * we need to check the pinned_extents for any extents that can't be used yet
243 * since their free space will be released as soon as the transaction commits.
244 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400245static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400246 struct btrfs_fs_info *info, u64 start, u64 end)
247{
Josef Bacik817d52f2009-07-13 21:29:25 -0400248 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400249 int ret;
250
251 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400252 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400253 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400254 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400255 if (ret)
256 break;
257
258 if (extent_start == start) {
259 start = extent_end + 1;
260 } else if (extent_start > start && extent_start < end) {
261 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400262 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500263 ret = btrfs_add_free_space(block_group, start,
264 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265 BUG_ON(ret);
266 start = extent_end + 1;
267 } else {
268 break;
269 }
270 }
271
272 if (start < end) {
273 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400274 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500275 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400276 BUG_ON(ret);
277 }
278
Josef Bacik817d52f2009-07-13 21:29:25 -0400279 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400280}
281
Josef Bacik817d52f2009-07-13 21:29:25 -0400282static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400283{
Josef Bacik817d52f2009-07-13 21:29:25 -0400284 struct btrfs_block_group_cache *block_group = data;
285 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400286 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
287 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400288 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400289 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400290 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400291 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400292 u64 last = 0;
293 u32 nritems;
294 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400295
Chris Masone37c9e62007-05-09 20:13:14 -0400296 path = btrfs_alloc_path();
297 if (!path)
298 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400299
Yan Zheng11833d62009-09-11 16:11:19 -0400300 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400301 spin_lock(&block_group->space_info->lock);
302 block_group->space_info->bytes_super += block_group->bytes_super;
303 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400304
Josef Bacik817d52f2009-07-13 21:29:25 -0400305 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400306
Chris Mason5cd57b22008-06-25 16:01:30 -0400307 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400308 * We don't want to deadlock with somebody trying to allocate a new
309 * extent for the extent root while also trying to search the extent
310 * root to add free space. So we skip locking and search the commit
311 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400312 */
313 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400314 path->search_commit_root = 1;
315 path->reada = 2;
316
Yan Zhenge4404d62008-12-12 10:03:26 -0500317 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400318 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400319 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400320again:
Yan Zheng11833d62009-09-11 16:11:19 -0400321 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400322 /* need to make sure the commit_root doesn't disappear */
323 down_read(&fs_info->extent_commit_sem);
324
Yan Zheng11833d62009-09-11 16:11:19 -0400325 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400326 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400327 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500328
Yan Zheng11833d62009-09-11 16:11:19 -0400329 leaf = path->nodes[0];
330 nritems = btrfs_header_nritems(leaf);
331
Chris Masond3977122009-01-05 21:25:51 -0500332 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400333 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400334 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400335 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400336 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400337 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400338
Yan Zheng11833d62009-09-11 16:11:19 -0400339 if (path->slots[0] < nritems) {
340 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
341 } else {
342 ret = find_next_key(path, 0, &key);
343 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400344 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400345
Yan Zheng11833d62009-09-11 16:11:19 -0400346 caching_ctl->progress = last;
347 btrfs_release_path(extent_root, path);
348 up_read(&fs_info->extent_commit_sem);
349 mutex_unlock(&caching_ctl->mutex);
350 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400351 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400352 else
353 cond_resched();
354 goto again;
355 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400356
Yan Zheng11833d62009-09-11 16:11:19 -0400357 if (key.objectid < block_group->key.objectid) {
358 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400359 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400360 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400361
Chris Masone37c9e62007-05-09 20:13:14 -0400362 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400363 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400364 break;
Yan7d7d6062007-09-14 16:15:28 -0400365
Yan Zheng11833d62009-09-11 16:11:19 -0400366 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400367 total_found += add_new_free_space(block_group,
368 fs_info, last,
369 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400370 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400371
Yan Zheng11833d62009-09-11 16:11:19 -0400372 if (total_found > (1024 * 1024 * 2)) {
373 total_found = 0;
374 wake_up(&caching_ctl->wait);
375 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400376 }
Chris Masone37c9e62007-05-09 20:13:14 -0400377 path->slots[0]++;
378 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400379 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400380
381 total_found += add_new_free_space(block_group, fs_info, last,
382 block_group->key.objectid +
383 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400384 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400385
386 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400387 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400388 block_group->cached = BTRFS_CACHE_FINISHED;
389 spin_unlock(&block_group->lock);
390
Chris Mason54aa1f42007-06-22 14:16:25 -0400391err:
Chris Masone37c9e62007-05-09 20:13:14 -0400392 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400393 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400394
Yan Zheng11833d62009-09-11 16:11:19 -0400395 free_excluded_extents(extent_root, block_group);
396
397 mutex_unlock(&caching_ctl->mutex);
398 wake_up(&caching_ctl->wait);
399
400 put_caching_control(caching_ctl);
401 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik817d52f2009-07-13 21:29:25 -0400402 return 0;
403}
404
405static int cache_block_group(struct btrfs_block_group_cache *cache)
406{
Yan Zheng11833d62009-09-11 16:11:19 -0400407 struct btrfs_fs_info *fs_info = cache->fs_info;
408 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400409 struct task_struct *tsk;
410 int ret = 0;
411
Yan Zheng11833d62009-09-11 16:11:19 -0400412 smp_mb();
413 if (cache->cached != BTRFS_CACHE_NO)
414 return 0;
415
416 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
417 BUG_ON(!caching_ctl);
418
419 INIT_LIST_HEAD(&caching_ctl->list);
420 mutex_init(&caching_ctl->mutex);
421 init_waitqueue_head(&caching_ctl->wait);
422 caching_ctl->block_group = cache;
423 caching_ctl->progress = cache->key.objectid;
424 /* one for caching kthread, one for caching block group list */
425 atomic_set(&caching_ctl->count, 2);
426
Josef Bacik817d52f2009-07-13 21:29:25 -0400427 spin_lock(&cache->lock);
428 if (cache->cached != BTRFS_CACHE_NO) {
429 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400430 kfree(caching_ctl);
431 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400432 }
Yan Zheng11833d62009-09-11 16:11:19 -0400433 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400434 cache->cached = BTRFS_CACHE_STARTED;
435 spin_unlock(&cache->lock);
436
Yan Zheng11833d62009-09-11 16:11:19 -0400437 down_write(&fs_info->extent_commit_sem);
438 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
439 up_write(&fs_info->extent_commit_sem);
440
441 atomic_inc(&cache->space_info->caching_threads);
442
Josef Bacik817d52f2009-07-13 21:29:25 -0400443 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
444 cache->key.objectid);
445 if (IS_ERR(tsk)) {
446 ret = PTR_ERR(tsk);
447 printk(KERN_ERR "error running thread %d\n", ret);
448 BUG();
449 }
450
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400451 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400452}
453
Josef Bacik0f9dd462008-09-23 13:14:11 -0400454/*
455 * return the block group that starts at or after bytenr
456 */
Chris Masond3977122009-01-05 21:25:51 -0500457static struct btrfs_block_group_cache *
458btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400459{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400460 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400461
Josef Bacik0f9dd462008-09-23 13:14:11 -0400462 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400463
Josef Bacik0f9dd462008-09-23 13:14:11 -0400464 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400465}
466
Josef Bacik0f9dd462008-09-23 13:14:11 -0400467/*
Sankar P9f556842009-05-14 13:52:22 -0400468 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400469 */
Chris Masond3977122009-01-05 21:25:51 -0500470struct btrfs_block_group_cache *btrfs_lookup_block_group(
471 struct btrfs_fs_info *info,
472 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400473{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400474 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400475
Josef Bacik0f9dd462008-09-23 13:14:11 -0400476 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400477
Josef Bacik0f9dd462008-09-23 13:14:11 -0400478 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400479}
Chris Mason0b86a832008-03-24 15:01:56 -0400480
Chris Masonfa9c0d792009-04-03 09:47:43 -0400481void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
Yan Zhengd2fb3432008-12-11 16:30:39 -0500482{
483 if (atomic_dec_and_test(&cache->count))
484 kfree(cache);
485}
486
Josef Bacik0f9dd462008-09-23 13:14:11 -0400487static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
488 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400489{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400490 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400491 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400492
493 rcu_read_lock();
494 list_for_each_entry_rcu(found, head, list) {
495 if (found->flags == flags) {
496 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400497 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400498 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400499 }
Chris Mason4184ea72009-03-10 12:39:20 -0400500 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400501 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400502}
503
Chris Mason4184ea72009-03-10 12:39:20 -0400504/*
505 * after adding space to the filesystem, we need to clear the full flags
506 * on all the space infos.
507 */
508void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
509{
510 struct list_head *head = &info->space_info;
511 struct btrfs_space_info *found;
512
513 rcu_read_lock();
514 list_for_each_entry_rcu(found, head, list)
515 found->full = 0;
516 rcu_read_unlock();
517}
518
Josef Bacik80eb2342008-10-29 14:49:05 -0400519static u64 div_factor(u64 num, int factor)
520{
521 if (factor == 10)
522 return num;
523 num *= factor;
524 do_div(num, 10);
525 return num;
526}
527
Yan Zhengd2fb3432008-12-11 16:30:39 -0500528u64 btrfs_find_block_group(struct btrfs_root *root,
529 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400530{
Chris Mason96b51792007-10-15 16:15:19 -0400531 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400532 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500533 u64 last = max(search_hint, search_start);
534 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400535 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500536 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400537 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400538again:
Zheng Yane8569812008-09-26 10:05:48 -0400539 while (1) {
540 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400541 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400542 break;
Chris Mason96b51792007-10-15 16:15:19 -0400543
Chris Masonc286ac42008-07-22 23:06:41 -0400544 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400545 last = cache->key.objectid + cache->key.offset;
546 used = btrfs_block_group_used(&cache->item);
547
Yan Zhengd2fb3432008-12-11 16:30:39 -0500548 if ((full_search || !cache->ro) &&
549 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400550 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500551 div_factor(cache->key.offset, factor)) {
552 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400553 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400554 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400555 goto found;
556 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400557 }
Chris Masonc286ac42008-07-22 23:06:41 -0400558 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400559 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400560 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400561 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400562 if (!wrapped) {
563 last = search_start;
564 wrapped = 1;
565 goto again;
566 }
567 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400568 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400569 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400570 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400571 goto again;
572 }
Chris Masonbe744172007-05-06 10:15:01 -0400573found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500574 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400575}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400576
Chris Masone02119d2008-09-05 16:13:11 -0400577/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400578int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400579{
580 int ret;
581 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400582 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400583
Zheng Yan31840ae2008-09-23 13:14:14 -0400584 path = btrfs_alloc_path();
585 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400586 key.objectid = start;
587 key.offset = len;
588 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
589 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
590 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400591 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500592 return ret;
593}
594
Chris Masond8d5f3e2007-12-11 12:42:00 -0500595/*
596 * Back reference rules. Back refs have three main goals:
597 *
598 * 1) differentiate between all holders of references to an extent so that
599 * when a reference is dropped we can make sure it was a valid reference
600 * before freeing the extent.
601 *
602 * 2) Provide enough information to quickly find the holders of an extent
603 * if we notice a given block is corrupted or bad.
604 *
605 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
606 * maintenance. This is actually the same as #2, but with a slightly
607 * different use case.
608 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400609 * There are two kinds of back refs. The implicit back refs is optimized
610 * for pointers in non-shared tree blocks. For a given pointer in a block,
611 * back refs of this kind provide information about the block's owner tree
612 * and the pointer's key. These information allow us to find the block by
613 * b-tree searching. The full back refs is for pointers in tree blocks not
614 * referenced by their owner trees. The location of tree block is recorded
615 * in the back refs. Actually the full back refs is generic, and can be
616 * used in all cases the implicit back refs is used. The major shortcoming
617 * of the full back refs is its overhead. Every time a tree block gets
618 * COWed, we have to update back refs entry for all pointers in it.
619 *
620 * For a newly allocated tree block, we use implicit back refs for
621 * pointers in it. This means most tree related operations only involve
622 * implicit back refs. For a tree block created in old transaction, the
623 * only way to drop a reference to it is COW it. So we can detect the
624 * event that tree block loses its owner tree's reference and do the
625 * back refs conversion.
626 *
627 * When a tree block is COW'd through a tree, there are four cases:
628 *
629 * The reference count of the block is one and the tree is the block's
630 * owner tree. Nothing to do in this case.
631 *
632 * The reference count of the block is one and the tree is not the
633 * block's owner tree. In this case, full back refs is used for pointers
634 * in the block. Remove these full back refs, add implicit back refs for
635 * every pointers in the new block.
636 *
637 * The reference count of the block is greater than one and the tree is
638 * the block's owner tree. In this case, implicit back refs is used for
639 * pointers in the block. Add full back refs for every pointers in the
640 * block, increase lower level extents' reference counts. The original
641 * implicit back refs are entailed to the new block.
642 *
643 * The reference count of the block is greater than one and the tree is
644 * not the block's owner tree. Add implicit back refs for every pointer in
645 * the new block, increase lower level extents' reference count.
646 *
647 * Back Reference Key composing:
648 *
649 * The key objectid corresponds to the first byte in the extent,
650 * The key type is used to differentiate between types of back refs.
651 * There are different meanings of the key offset for different types
652 * of back refs.
653 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500654 * File extents can be referenced by:
655 *
656 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400657 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500658 * - different offsets inside a file (bookend extents in file.c)
659 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400660 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500661 *
662 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500663 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400664 * - original offset in the file
665 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400666 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400667 * The key offset for the implicit back refs is hash of the first
668 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500669 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400670 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500671 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400672 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500673 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400674 * The key offset for the implicit back refs is the first byte of
675 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500676 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400677 * When a file extent is allocated, The implicit back refs is used.
678 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500679 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400680 * (root_key.objectid, inode objectid, offset in file, 1)
681 *
682 * When a file extent is removed file truncation, we find the
683 * corresponding implicit back refs and check the following fields:
684 *
685 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500686 *
687 * Btree extents can be referenced by:
688 *
689 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500690 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400691 * Both the implicit back refs and the full back refs for tree blocks
692 * only consist of key. The key offset for the implicit back refs is
693 * objectid of block's owner tree. The key offset for the full back refs
694 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500695 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400696 * When implicit back refs is used, information about the lowest key and
697 * level of the tree block are required. These information are stored in
698 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500699 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400700
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400701#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
702static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
703 struct btrfs_root *root,
704 struct btrfs_path *path,
705 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500706{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400707 struct btrfs_extent_item *item;
708 struct btrfs_extent_item_v0 *ei0;
709 struct btrfs_extent_ref_v0 *ref0;
710 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400711 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400712 struct btrfs_key key;
713 struct btrfs_key found_key;
714 u32 new_size = sizeof(*item);
715 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500716 int ret;
717
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400718 leaf = path->nodes[0];
719 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500720
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400721 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
722 ei0 = btrfs_item_ptr(leaf, path->slots[0],
723 struct btrfs_extent_item_v0);
724 refs = btrfs_extent_refs_v0(leaf, ei0);
725
726 if (owner == (u64)-1) {
727 while (1) {
728 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
729 ret = btrfs_next_leaf(root, path);
730 if (ret < 0)
731 return ret;
732 BUG_ON(ret > 0);
733 leaf = path->nodes[0];
734 }
735 btrfs_item_key_to_cpu(leaf, &found_key,
736 path->slots[0]);
737 BUG_ON(key.objectid != found_key.objectid);
738 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
739 path->slots[0]++;
740 continue;
741 }
742 ref0 = btrfs_item_ptr(leaf, path->slots[0],
743 struct btrfs_extent_ref_v0);
744 owner = btrfs_ref_objectid_v0(leaf, ref0);
745 break;
746 }
747 }
748 btrfs_release_path(root, path);
749
750 if (owner < BTRFS_FIRST_FREE_OBJECTID)
751 new_size += sizeof(*bi);
752
753 new_size -= sizeof(*ei0);
754 ret = btrfs_search_slot(trans, root, &key, path,
755 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400756 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400757 return ret;
758 BUG_ON(ret);
759
760 ret = btrfs_extend_item(trans, root, path, new_size);
761 BUG_ON(ret);
762
763 leaf = path->nodes[0];
764 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
765 btrfs_set_extent_refs(leaf, item, refs);
766 /* FIXME: get real generation */
767 btrfs_set_extent_generation(leaf, item, 0);
768 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
769 btrfs_set_extent_flags(leaf, item,
770 BTRFS_EXTENT_FLAG_TREE_BLOCK |
771 BTRFS_BLOCK_FLAG_FULL_BACKREF);
772 bi = (struct btrfs_tree_block_info *)(item + 1);
773 /* FIXME: get first key of the block */
774 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
775 btrfs_set_tree_block_level(leaf, bi, (int)owner);
776 } else {
777 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
778 }
779 btrfs_mark_buffer_dirty(leaf);
780 return 0;
781}
782#endif
783
784static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
785{
786 u32 high_crc = ~(u32)0;
787 u32 low_crc = ~(u32)0;
788 __le64 lenum;
789
790 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100791 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400792 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100793 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400794 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100795 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400796
797 return ((u64)high_crc << 31) ^ (u64)low_crc;
798}
799
800static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
801 struct btrfs_extent_data_ref *ref)
802{
803 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
804 btrfs_extent_data_ref_objectid(leaf, ref),
805 btrfs_extent_data_ref_offset(leaf, ref));
806}
807
808static int match_extent_data_ref(struct extent_buffer *leaf,
809 struct btrfs_extent_data_ref *ref,
810 u64 root_objectid, u64 owner, u64 offset)
811{
812 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
813 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
814 btrfs_extent_data_ref_offset(leaf, ref) != offset)
815 return 0;
816 return 1;
817}
818
819static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
820 struct btrfs_root *root,
821 struct btrfs_path *path,
822 u64 bytenr, u64 parent,
823 u64 root_objectid,
824 u64 owner, u64 offset)
825{
826 struct btrfs_key key;
827 struct btrfs_extent_data_ref *ref;
828 struct extent_buffer *leaf;
829 u32 nritems;
830 int ret;
831 int recow;
832 int err = -ENOENT;
833
834 key.objectid = bytenr;
835 if (parent) {
836 key.type = BTRFS_SHARED_DATA_REF_KEY;
837 key.offset = parent;
838 } else {
839 key.type = BTRFS_EXTENT_DATA_REF_KEY;
840 key.offset = hash_extent_data_ref(root_objectid,
841 owner, offset);
842 }
843again:
844 recow = 0;
845 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
846 if (ret < 0) {
847 err = ret;
848 goto fail;
849 }
850
851 if (parent) {
852 if (!ret)
853 return 0;
854#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
855 key.type = BTRFS_EXTENT_REF_V0_KEY;
856 btrfs_release_path(root, path);
857 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
858 if (ret < 0) {
859 err = ret;
860 goto fail;
861 }
862 if (!ret)
863 return 0;
864#endif
865 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400866 }
867
868 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400869 nritems = btrfs_header_nritems(leaf);
870 while (1) {
871 if (path->slots[0] >= nritems) {
872 ret = btrfs_next_leaf(root, path);
873 if (ret < 0)
874 err = ret;
875 if (ret)
876 goto fail;
877
878 leaf = path->nodes[0];
879 nritems = btrfs_header_nritems(leaf);
880 recow = 1;
881 }
882
883 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
884 if (key.objectid != bytenr ||
885 key.type != BTRFS_EXTENT_DATA_REF_KEY)
886 goto fail;
887
888 ref = btrfs_item_ptr(leaf, path->slots[0],
889 struct btrfs_extent_data_ref);
890
891 if (match_extent_data_ref(leaf, ref, root_objectid,
892 owner, offset)) {
893 if (recow) {
894 btrfs_release_path(root, path);
895 goto again;
896 }
897 err = 0;
898 break;
899 }
900 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400901 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902fail:
903 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400904}
905
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400906static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
907 struct btrfs_root *root,
908 struct btrfs_path *path,
909 u64 bytenr, u64 parent,
910 u64 root_objectid, u64 owner,
911 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400912{
913 struct btrfs_key key;
914 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400915 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400916 u32 num_refs;
917 int ret;
918
919 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400920 if (parent) {
921 key.type = BTRFS_SHARED_DATA_REF_KEY;
922 key.offset = parent;
923 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400924 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400925 key.type = BTRFS_EXTENT_DATA_REF_KEY;
926 key.offset = hash_extent_data_ref(root_objectid,
927 owner, offset);
928 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400929 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400930
931 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
932 if (ret && ret != -EEXIST)
933 goto fail;
934
935 leaf = path->nodes[0];
936 if (parent) {
937 struct btrfs_shared_data_ref *ref;
938 ref = btrfs_item_ptr(leaf, path->slots[0],
939 struct btrfs_shared_data_ref);
940 if (ret == 0) {
941 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
942 } else {
943 num_refs = btrfs_shared_data_ref_count(leaf, ref);
944 num_refs += refs_to_add;
945 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
946 }
947 } else {
948 struct btrfs_extent_data_ref *ref;
949 while (ret == -EEXIST) {
950 ref = btrfs_item_ptr(leaf, path->slots[0],
951 struct btrfs_extent_data_ref);
952 if (match_extent_data_ref(leaf, ref, root_objectid,
953 owner, offset))
954 break;
955 btrfs_release_path(root, path);
956 key.offset++;
957 ret = btrfs_insert_empty_item(trans, root, path, &key,
958 size);
959 if (ret && ret != -EEXIST)
960 goto fail;
961
962 leaf = path->nodes[0];
963 }
964 ref = btrfs_item_ptr(leaf, path->slots[0],
965 struct btrfs_extent_data_ref);
966 if (ret == 0) {
967 btrfs_set_extent_data_ref_root(leaf, ref,
968 root_objectid);
969 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
970 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
971 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
972 } else {
973 num_refs = btrfs_extent_data_ref_count(leaf, ref);
974 num_refs += refs_to_add;
975 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
976 }
977 }
978 btrfs_mark_buffer_dirty(leaf);
979 ret = 0;
980fail:
Chris Mason7bb86312007-12-11 09:25:06 -0500981 btrfs_release_path(root, path);
982 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500983}
984
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
986 struct btrfs_root *root,
987 struct btrfs_path *path,
988 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -0400989{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400990 struct btrfs_key key;
991 struct btrfs_extent_data_ref *ref1 = NULL;
992 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400993 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400994 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400995 int ret = 0;
996
997 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400998 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
999
1000 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1001 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1002 struct btrfs_extent_data_ref);
1003 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1004 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1005 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_shared_data_ref);
1007 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1008#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1009 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1010 struct btrfs_extent_ref_v0 *ref0;
1011 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1012 struct btrfs_extent_ref_v0);
1013 num_refs = btrfs_ref_count_v0(leaf, ref0);
1014#endif
1015 } else {
1016 BUG();
1017 }
1018
Chris Mason56bec292009-03-13 10:10:06 -04001019 BUG_ON(num_refs < refs_to_drop);
1020 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021
Zheng Yan31840ae2008-09-23 13:14:14 -04001022 if (num_refs == 0) {
1023 ret = btrfs_del_item(trans, root, path);
1024 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001025 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1026 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1027 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1028 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1029#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1030 else {
1031 struct btrfs_extent_ref_v0 *ref0;
1032 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1033 struct btrfs_extent_ref_v0);
1034 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1035 }
1036#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001037 btrfs_mark_buffer_dirty(leaf);
1038 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001039 return ret;
1040}
1041
1042static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1043 struct btrfs_path *path,
1044 struct btrfs_extent_inline_ref *iref)
1045{
1046 struct btrfs_key key;
1047 struct extent_buffer *leaf;
1048 struct btrfs_extent_data_ref *ref1;
1049 struct btrfs_shared_data_ref *ref2;
1050 u32 num_refs = 0;
1051
1052 leaf = path->nodes[0];
1053 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1054 if (iref) {
1055 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1056 BTRFS_EXTENT_DATA_REF_KEY) {
1057 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1058 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1059 } else {
1060 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1061 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1062 }
1063 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1064 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1065 struct btrfs_extent_data_ref);
1066 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1067 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1068 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1069 struct btrfs_shared_data_ref);
1070 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1071#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1072 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1073 struct btrfs_extent_ref_v0 *ref0;
1074 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1075 struct btrfs_extent_ref_v0);
1076 num_refs = btrfs_ref_count_v0(leaf, ref0);
1077#endif
1078 } else {
1079 WARN_ON(1);
1080 }
1081 return num_refs;
1082}
1083
1084static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1085 struct btrfs_root *root,
1086 struct btrfs_path *path,
1087 u64 bytenr, u64 parent,
1088 u64 root_objectid)
1089{
1090 struct btrfs_key key;
1091 int ret;
1092
1093 key.objectid = bytenr;
1094 if (parent) {
1095 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1096 key.offset = parent;
1097 } else {
1098 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1099 key.offset = root_objectid;
1100 }
1101
1102 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1103 if (ret > 0)
1104 ret = -ENOENT;
1105#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1106 if (ret == -ENOENT && parent) {
1107 btrfs_release_path(root, path);
1108 key.type = BTRFS_EXTENT_REF_V0_KEY;
1109 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1110 if (ret > 0)
1111 ret = -ENOENT;
1112 }
1113#endif
1114 return ret;
1115}
1116
1117static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1118 struct btrfs_root *root,
1119 struct btrfs_path *path,
1120 u64 bytenr, u64 parent,
1121 u64 root_objectid)
1122{
1123 struct btrfs_key key;
1124 int ret;
1125
1126 key.objectid = bytenr;
1127 if (parent) {
1128 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1129 key.offset = parent;
1130 } else {
1131 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1132 key.offset = root_objectid;
1133 }
1134
1135 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001136 btrfs_release_path(root, path);
1137 return ret;
1138}
1139
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001140static inline int extent_ref_type(u64 parent, u64 owner)
1141{
1142 int type;
1143 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1144 if (parent > 0)
1145 type = BTRFS_SHARED_BLOCK_REF_KEY;
1146 else
1147 type = BTRFS_TREE_BLOCK_REF_KEY;
1148 } else {
1149 if (parent > 0)
1150 type = BTRFS_SHARED_DATA_REF_KEY;
1151 else
1152 type = BTRFS_EXTENT_DATA_REF_KEY;
1153 }
1154 return type;
1155}
1156
Yan Zheng2c47e6052009-06-27 21:07:35 -04001157static int find_next_key(struct btrfs_path *path, int level,
1158 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001159
1160{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001161 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001162 if (!path->nodes[level])
1163 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001164 if (path->slots[level] + 1 >=
1165 btrfs_header_nritems(path->nodes[level]))
1166 continue;
1167 if (level == 0)
1168 btrfs_item_key_to_cpu(path->nodes[level], key,
1169 path->slots[level] + 1);
1170 else
1171 btrfs_node_key_to_cpu(path->nodes[level], key,
1172 path->slots[level] + 1);
1173 return 0;
1174 }
1175 return 1;
1176}
1177
1178/*
1179 * look for inline back ref. if back ref is found, *ref_ret is set
1180 * to the address of inline back ref, and 0 is returned.
1181 *
1182 * if back ref isn't found, *ref_ret is set to the address where it
1183 * should be inserted, and -ENOENT is returned.
1184 *
1185 * if insert is true and there are too many inline back refs, the path
1186 * points to the extent item, and -EAGAIN is returned.
1187 *
1188 * NOTE: inline back refs are ordered in the same way that back ref
1189 * items in the tree are ordered.
1190 */
1191static noinline_for_stack
1192int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1193 struct btrfs_root *root,
1194 struct btrfs_path *path,
1195 struct btrfs_extent_inline_ref **ref_ret,
1196 u64 bytenr, u64 num_bytes,
1197 u64 parent, u64 root_objectid,
1198 u64 owner, u64 offset, int insert)
1199{
1200 struct btrfs_key key;
1201 struct extent_buffer *leaf;
1202 struct btrfs_extent_item *ei;
1203 struct btrfs_extent_inline_ref *iref;
1204 u64 flags;
1205 u64 item_size;
1206 unsigned long ptr;
1207 unsigned long end;
1208 int extra_size;
1209 int type;
1210 int want;
1211 int ret;
1212 int err = 0;
1213
1214 key.objectid = bytenr;
1215 key.type = BTRFS_EXTENT_ITEM_KEY;
1216 key.offset = num_bytes;
1217
1218 want = extent_ref_type(parent, owner);
1219 if (insert) {
1220 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001221 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001222 } else
1223 extra_size = -1;
1224 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1225 if (ret < 0) {
1226 err = ret;
1227 goto out;
1228 }
1229 BUG_ON(ret);
1230
1231 leaf = path->nodes[0];
1232 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1233#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1234 if (item_size < sizeof(*ei)) {
1235 if (!insert) {
1236 err = -ENOENT;
1237 goto out;
1238 }
1239 ret = convert_extent_item_v0(trans, root, path, owner,
1240 extra_size);
1241 if (ret < 0) {
1242 err = ret;
1243 goto out;
1244 }
1245 leaf = path->nodes[0];
1246 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1247 }
1248#endif
1249 BUG_ON(item_size < sizeof(*ei));
1250
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001251 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1252 flags = btrfs_extent_flags(leaf, ei);
1253
1254 ptr = (unsigned long)(ei + 1);
1255 end = (unsigned long)ei + item_size;
1256
1257 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1258 ptr += sizeof(struct btrfs_tree_block_info);
1259 BUG_ON(ptr > end);
1260 } else {
1261 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1262 }
1263
1264 err = -ENOENT;
1265 while (1) {
1266 if (ptr >= end) {
1267 WARN_ON(ptr > end);
1268 break;
1269 }
1270 iref = (struct btrfs_extent_inline_ref *)ptr;
1271 type = btrfs_extent_inline_ref_type(leaf, iref);
1272 if (want < type)
1273 break;
1274 if (want > type) {
1275 ptr += btrfs_extent_inline_ref_size(type);
1276 continue;
1277 }
1278
1279 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1280 struct btrfs_extent_data_ref *dref;
1281 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1282 if (match_extent_data_ref(leaf, dref, root_objectid,
1283 owner, offset)) {
1284 err = 0;
1285 break;
1286 }
1287 if (hash_extent_data_ref_item(leaf, dref) <
1288 hash_extent_data_ref(root_objectid, owner, offset))
1289 break;
1290 } else {
1291 u64 ref_offset;
1292 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1293 if (parent > 0) {
1294 if (parent == ref_offset) {
1295 err = 0;
1296 break;
1297 }
1298 if (ref_offset < parent)
1299 break;
1300 } else {
1301 if (root_objectid == ref_offset) {
1302 err = 0;
1303 break;
1304 }
1305 if (ref_offset < root_objectid)
1306 break;
1307 }
1308 }
1309 ptr += btrfs_extent_inline_ref_size(type);
1310 }
1311 if (err == -ENOENT && insert) {
1312 if (item_size + extra_size >=
1313 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1314 err = -EAGAIN;
1315 goto out;
1316 }
1317 /*
1318 * To add new inline back ref, we have to make sure
1319 * there is no corresponding back ref item.
1320 * For simplicity, we just do not add new inline back
1321 * ref if there is any kind of item for this block
1322 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001323 if (find_next_key(path, 0, &key) == 0 &&
1324 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001325 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001326 err = -EAGAIN;
1327 goto out;
1328 }
1329 }
1330 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1331out:
Yan Zheng85d41982009-06-11 08:51:10 -04001332 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001333 path->keep_locks = 0;
1334 btrfs_unlock_up_safe(path, 1);
1335 }
1336 return err;
1337}
1338
1339/*
1340 * helper to add new inline back ref
1341 */
1342static noinline_for_stack
1343int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1344 struct btrfs_root *root,
1345 struct btrfs_path *path,
1346 struct btrfs_extent_inline_ref *iref,
1347 u64 parent, u64 root_objectid,
1348 u64 owner, u64 offset, int refs_to_add,
1349 struct btrfs_delayed_extent_op *extent_op)
1350{
1351 struct extent_buffer *leaf;
1352 struct btrfs_extent_item *ei;
1353 unsigned long ptr;
1354 unsigned long end;
1355 unsigned long item_offset;
1356 u64 refs;
1357 int size;
1358 int type;
1359 int ret;
1360
1361 leaf = path->nodes[0];
1362 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1363 item_offset = (unsigned long)iref - (unsigned long)ei;
1364
1365 type = extent_ref_type(parent, owner);
1366 size = btrfs_extent_inline_ref_size(type);
1367
1368 ret = btrfs_extend_item(trans, root, path, size);
1369 BUG_ON(ret);
1370
1371 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1372 refs = btrfs_extent_refs(leaf, ei);
1373 refs += refs_to_add;
1374 btrfs_set_extent_refs(leaf, ei, refs);
1375 if (extent_op)
1376 __run_delayed_extent_op(extent_op, leaf, ei);
1377
1378 ptr = (unsigned long)ei + item_offset;
1379 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1380 if (ptr < end - size)
1381 memmove_extent_buffer(leaf, ptr + size, ptr,
1382 end - size - ptr);
1383
1384 iref = (struct btrfs_extent_inline_ref *)ptr;
1385 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1386 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1387 struct btrfs_extent_data_ref *dref;
1388 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1389 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1390 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1391 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1392 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1393 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1394 struct btrfs_shared_data_ref *sref;
1395 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1396 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1397 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1398 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1399 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1400 } else {
1401 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1402 }
1403 btrfs_mark_buffer_dirty(leaf);
1404 return 0;
1405}
1406
1407static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1408 struct btrfs_root *root,
1409 struct btrfs_path *path,
1410 struct btrfs_extent_inline_ref **ref_ret,
1411 u64 bytenr, u64 num_bytes, u64 parent,
1412 u64 root_objectid, u64 owner, u64 offset)
1413{
1414 int ret;
1415
1416 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1417 bytenr, num_bytes, parent,
1418 root_objectid, owner, offset, 0);
1419 if (ret != -ENOENT)
1420 return ret;
1421
1422 btrfs_release_path(root, path);
1423 *ref_ret = NULL;
1424
1425 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1426 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1427 root_objectid);
1428 } else {
1429 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1430 root_objectid, owner, offset);
1431 }
1432 return ret;
1433}
1434
1435/*
1436 * helper to update/remove inline back ref
1437 */
1438static noinline_for_stack
1439int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1440 struct btrfs_root *root,
1441 struct btrfs_path *path,
1442 struct btrfs_extent_inline_ref *iref,
1443 int refs_to_mod,
1444 struct btrfs_delayed_extent_op *extent_op)
1445{
1446 struct extent_buffer *leaf;
1447 struct btrfs_extent_item *ei;
1448 struct btrfs_extent_data_ref *dref = NULL;
1449 struct btrfs_shared_data_ref *sref = NULL;
1450 unsigned long ptr;
1451 unsigned long end;
1452 u32 item_size;
1453 int size;
1454 int type;
1455 int ret;
1456 u64 refs;
1457
1458 leaf = path->nodes[0];
1459 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1460 refs = btrfs_extent_refs(leaf, ei);
1461 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1462 refs += refs_to_mod;
1463 btrfs_set_extent_refs(leaf, ei, refs);
1464 if (extent_op)
1465 __run_delayed_extent_op(extent_op, leaf, ei);
1466
1467 type = btrfs_extent_inline_ref_type(leaf, iref);
1468
1469 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1470 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1471 refs = btrfs_extent_data_ref_count(leaf, dref);
1472 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1473 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1474 refs = btrfs_shared_data_ref_count(leaf, sref);
1475 } else {
1476 refs = 1;
1477 BUG_ON(refs_to_mod != -1);
1478 }
1479
1480 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1481 refs += refs_to_mod;
1482
1483 if (refs > 0) {
1484 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1485 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1486 else
1487 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1488 } else {
1489 size = btrfs_extent_inline_ref_size(type);
1490 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1491 ptr = (unsigned long)iref;
1492 end = (unsigned long)ei + item_size;
1493 if (ptr + size < end)
1494 memmove_extent_buffer(leaf, ptr, ptr + size,
1495 end - ptr - size);
1496 item_size -= size;
1497 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1498 BUG_ON(ret);
1499 }
1500 btrfs_mark_buffer_dirty(leaf);
1501 return 0;
1502}
1503
1504static noinline_for_stack
1505int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1506 struct btrfs_root *root,
1507 struct btrfs_path *path,
1508 u64 bytenr, u64 num_bytes, u64 parent,
1509 u64 root_objectid, u64 owner,
1510 u64 offset, int refs_to_add,
1511 struct btrfs_delayed_extent_op *extent_op)
1512{
1513 struct btrfs_extent_inline_ref *iref;
1514 int ret;
1515
1516 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1517 bytenr, num_bytes, parent,
1518 root_objectid, owner, offset, 1);
1519 if (ret == 0) {
1520 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1521 ret = update_inline_extent_backref(trans, root, path, iref,
1522 refs_to_add, extent_op);
1523 } else if (ret == -ENOENT) {
1524 ret = setup_inline_extent_backref(trans, root, path, iref,
1525 parent, root_objectid,
1526 owner, offset, refs_to_add,
1527 extent_op);
1528 }
1529 return ret;
1530}
1531
1532static int insert_extent_backref(struct btrfs_trans_handle *trans,
1533 struct btrfs_root *root,
1534 struct btrfs_path *path,
1535 u64 bytenr, u64 parent, u64 root_objectid,
1536 u64 owner, u64 offset, int refs_to_add)
1537{
1538 int ret;
1539 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1540 BUG_ON(refs_to_add != 1);
1541 ret = insert_tree_block_ref(trans, root, path, bytenr,
1542 parent, root_objectid);
1543 } else {
1544 ret = insert_extent_data_ref(trans, root, path, bytenr,
1545 parent, root_objectid,
1546 owner, offset, refs_to_add);
1547 }
1548 return ret;
1549}
1550
1551static int remove_extent_backref(struct btrfs_trans_handle *trans,
1552 struct btrfs_root *root,
1553 struct btrfs_path *path,
1554 struct btrfs_extent_inline_ref *iref,
1555 int refs_to_drop, int is_data)
1556{
1557 int ret;
1558
1559 BUG_ON(!is_data && refs_to_drop != 1);
1560 if (iref) {
1561 ret = update_inline_extent_backref(trans, root, path, iref,
1562 -refs_to_drop, NULL);
1563 } else if (is_data) {
1564 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1565 } else {
1566 ret = btrfs_del_item(trans, root, path);
1567 }
1568 return ret;
1569}
1570
Chris Mason4b4e25f2008-11-20 10:22:27 -05001571#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001572static void btrfs_issue_discard(struct block_device *bdev,
1573 u64 start, u64 len)
1574{
Chris Mason15916de2008-11-19 21:17:22 -05001575 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -05001576}
Chris Mason4b4e25f2008-11-20 10:22:27 -05001577#endif
Chris Mason15916de2008-11-19 21:17:22 -05001578
Liu Hui1f3c79a2009-01-05 15:57:51 -05001579static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1580 u64 num_bytes)
1581{
1582#ifdef BIO_RW_DISCARD
1583 int ret;
1584 u64 map_length = num_bytes;
1585 struct btrfs_multi_bio *multi = NULL;
1586
1587 /* Tell the block device(s) that the sectors can be discarded */
1588 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1589 bytenr, &map_length, &multi, 0);
1590 if (!ret) {
1591 struct btrfs_bio_stripe *stripe = multi->stripes;
1592 int i;
1593
1594 if (map_length > num_bytes)
1595 map_length = num_bytes;
1596
1597 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1598 btrfs_issue_discard(stripe->dev->bdev,
1599 stripe->physical,
1600 map_length);
1601 }
1602 kfree(multi);
1603 }
1604
1605 return ret;
1606#else
1607 return 0;
1608#endif
1609}
1610
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001611int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1612 struct btrfs_root *root,
1613 u64 bytenr, u64 num_bytes, u64 parent,
1614 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001615{
1616 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001617 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1618 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001619
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001620 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1621 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1622 parent, root_objectid, (int)owner,
1623 BTRFS_ADD_DELAYED_REF, NULL);
1624 } else {
1625 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1626 parent, root_objectid, owner, offset,
1627 BTRFS_ADD_DELAYED_REF, NULL);
1628 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001629 return ret;
1630}
1631
Chris Mason925baed2008-06-25 16:01:30 -04001632static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001633 struct btrfs_root *root,
1634 u64 bytenr, u64 num_bytes,
1635 u64 parent, u64 root_objectid,
1636 u64 owner, u64 offset, int refs_to_add,
1637 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001638{
Chris Mason5caf2a02007-04-02 11:20:42 -04001639 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001640 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001641 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001642 u64 refs;
1643 int ret;
1644 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001645
Chris Mason5caf2a02007-04-02 11:20:42 -04001646 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001647 if (!path)
1648 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001649
Chris Mason3c12ac72008-04-21 12:01:38 -04001650 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001651 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001652 /* this will setup the path even if it fails to insert the back ref */
1653 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1654 path, bytenr, num_bytes, parent,
1655 root_objectid, owner, offset,
1656 refs_to_add, extent_op);
1657 if (ret == 0)
1658 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001659
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001660 if (ret != -EAGAIN) {
1661 err = ret;
1662 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001663 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001664
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001665 leaf = path->nodes[0];
1666 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1667 refs = btrfs_extent_refs(leaf, item);
1668 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1669 if (extent_op)
1670 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001671
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001672 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001673 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001674
Chris Mason3c12ac72008-04-21 12:01:38 -04001675 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001676 path->leave_spinning = 1;
1677
Chris Mason56bec292009-03-13 10:10:06 -04001678 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001679 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001680 path, bytenr, parent, root_objectid,
1681 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001682 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001683out:
Chris Mason74493f72007-12-11 09:25:06 -05001684 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001685 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001686}
1687
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001688static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1689 struct btrfs_root *root,
1690 struct btrfs_delayed_ref_node *node,
1691 struct btrfs_delayed_extent_op *extent_op,
1692 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001693{
Chris Mason56bec292009-03-13 10:10:06 -04001694 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001695 struct btrfs_delayed_data_ref *ref;
1696 struct btrfs_key ins;
1697 u64 parent = 0;
1698 u64 ref_root = 0;
1699 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001700
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001701 ins.objectid = node->bytenr;
1702 ins.offset = node->num_bytes;
1703 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001704
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001705 ref = btrfs_delayed_node_to_data_ref(node);
1706 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1707 parent = ref->parent;
1708 else
1709 ref_root = ref->root;
1710
1711 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1712 if (extent_op) {
1713 BUG_ON(extent_op->update_key);
1714 flags |= extent_op->flags_to_set;
1715 }
1716 ret = alloc_reserved_file_extent(trans, root,
1717 parent, ref_root, flags,
1718 ref->objectid, ref->offset,
1719 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001720 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1721 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1722 node->num_bytes, parent,
1723 ref_root, ref->objectid,
1724 ref->offset, node->ref_mod,
1725 extent_op);
1726 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1727 ret = __btrfs_free_extent(trans, root, node->bytenr,
1728 node->num_bytes, parent,
1729 ref_root, ref->objectid,
1730 ref->offset, node->ref_mod,
1731 extent_op);
1732 } else {
1733 BUG();
1734 }
Chris Mason56bec292009-03-13 10:10:06 -04001735 return ret;
1736}
1737
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001738static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1739 struct extent_buffer *leaf,
1740 struct btrfs_extent_item *ei)
1741{
1742 u64 flags = btrfs_extent_flags(leaf, ei);
1743 if (extent_op->update_flags) {
1744 flags |= extent_op->flags_to_set;
1745 btrfs_set_extent_flags(leaf, ei, flags);
1746 }
1747
1748 if (extent_op->update_key) {
1749 struct btrfs_tree_block_info *bi;
1750 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1751 bi = (struct btrfs_tree_block_info *)(ei + 1);
1752 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1753 }
1754}
1755
1756static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1757 struct btrfs_root *root,
1758 struct btrfs_delayed_ref_node *node,
1759 struct btrfs_delayed_extent_op *extent_op)
1760{
1761 struct btrfs_key key;
1762 struct btrfs_path *path;
1763 struct btrfs_extent_item *ei;
1764 struct extent_buffer *leaf;
1765 u32 item_size;
1766 int ret;
1767 int err = 0;
1768
1769 path = btrfs_alloc_path();
1770 if (!path)
1771 return -ENOMEM;
1772
1773 key.objectid = node->bytenr;
1774 key.type = BTRFS_EXTENT_ITEM_KEY;
1775 key.offset = node->num_bytes;
1776
1777 path->reada = 1;
1778 path->leave_spinning = 1;
1779 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1780 path, 0, 1);
1781 if (ret < 0) {
1782 err = ret;
1783 goto out;
1784 }
1785 if (ret > 0) {
1786 err = -EIO;
1787 goto out;
1788 }
1789
1790 leaf = path->nodes[0];
1791 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1792#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1793 if (item_size < sizeof(*ei)) {
1794 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1795 path, (u64)-1, 0);
1796 if (ret < 0) {
1797 err = ret;
1798 goto out;
1799 }
1800 leaf = path->nodes[0];
1801 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1802 }
1803#endif
1804 BUG_ON(item_size < sizeof(*ei));
1805 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1806 __run_delayed_extent_op(extent_op, leaf, ei);
1807
1808 btrfs_mark_buffer_dirty(leaf);
1809out:
1810 btrfs_free_path(path);
1811 return err;
1812}
1813
1814static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1815 struct btrfs_root *root,
1816 struct btrfs_delayed_ref_node *node,
1817 struct btrfs_delayed_extent_op *extent_op,
1818 int insert_reserved)
1819{
1820 int ret = 0;
1821 struct btrfs_delayed_tree_ref *ref;
1822 struct btrfs_key ins;
1823 u64 parent = 0;
1824 u64 ref_root = 0;
1825
1826 ins.objectid = node->bytenr;
1827 ins.offset = node->num_bytes;
1828 ins.type = BTRFS_EXTENT_ITEM_KEY;
1829
1830 ref = btrfs_delayed_node_to_tree_ref(node);
1831 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1832 parent = ref->parent;
1833 else
1834 ref_root = ref->root;
1835
1836 BUG_ON(node->ref_mod != 1);
1837 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1838 BUG_ON(!extent_op || !extent_op->update_flags ||
1839 !extent_op->update_key);
1840 ret = alloc_reserved_tree_block(trans, root,
1841 parent, ref_root,
1842 extent_op->flags_to_set,
1843 &extent_op->key,
1844 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001845 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1846 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1847 node->num_bytes, parent, ref_root,
1848 ref->level, 0, 1, extent_op);
1849 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1850 ret = __btrfs_free_extent(trans, root, node->bytenr,
1851 node->num_bytes, parent, ref_root,
1852 ref->level, 0, 1, extent_op);
1853 } else {
1854 BUG();
1855 }
1856 return ret;
1857}
1858
1859
Chris Mason56bec292009-03-13 10:10:06 -04001860/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001861static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1862 struct btrfs_root *root,
1863 struct btrfs_delayed_ref_node *node,
1864 struct btrfs_delayed_extent_op *extent_op,
1865 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001866{
Josef Bacikeb099672009-02-12 09:27:38 -05001867 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001868 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001869 struct btrfs_delayed_ref_head *head;
1870 /*
1871 * we've hit the end of the chain and we were supposed
1872 * to insert this extent into the tree. But, it got
1873 * deleted before we ever needed to insert it, so all
1874 * we have to do is clean up the accounting
1875 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876 BUG_ON(extent_op);
1877 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001878 if (insert_reserved) {
Yan Zheng11833d62009-09-11 16:11:19 -04001879 int mark_free = 0;
1880 struct extent_buffer *must_clean = NULL;
1881
1882 ret = pin_down_bytes(trans, root, NULL,
1883 node->bytenr, node->num_bytes,
1884 head->is_data, 1, &must_clean);
1885 if (ret > 0)
1886 mark_free = 1;
1887
1888 if (must_clean) {
1889 clean_tree_block(NULL, root, must_clean);
1890 btrfs_tree_unlock(must_clean);
1891 free_extent_buffer(must_clean);
1892 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001893 if (head->is_data) {
1894 ret = btrfs_del_csums(trans, root,
1895 node->bytenr,
1896 node->num_bytes);
1897 BUG_ON(ret);
1898 }
Yan Zheng11833d62009-09-11 16:11:19 -04001899 if (mark_free) {
1900 ret = btrfs_free_reserved_extent(root,
1901 node->bytenr,
1902 node->num_bytes);
1903 BUG_ON(ret);
1904 }
Chris Mason56bec292009-03-13 10:10:06 -04001905 }
Chris Mason56bec292009-03-13 10:10:06 -04001906 mutex_unlock(&head->mutex);
1907 return 0;
1908 }
Josef Bacikeb099672009-02-12 09:27:38 -05001909
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001910 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1911 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1912 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1913 insert_reserved);
1914 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1915 node->type == BTRFS_SHARED_DATA_REF_KEY)
1916 ret = run_delayed_data_ref(trans, root, node, extent_op,
1917 insert_reserved);
1918 else
1919 BUG();
1920 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001921}
1922
Chris Mason56bec292009-03-13 10:10:06 -04001923static noinline struct btrfs_delayed_ref_node *
1924select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001925{
Chris Mason56bec292009-03-13 10:10:06 -04001926 struct rb_node *node;
1927 struct btrfs_delayed_ref_node *ref;
1928 int action = BTRFS_ADD_DELAYED_REF;
1929again:
1930 /*
1931 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1932 * this prevents ref count from going down to zero when
1933 * there still are pending delayed ref.
1934 */
1935 node = rb_prev(&head->node.rb_node);
1936 while (1) {
1937 if (!node)
1938 break;
1939 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1940 rb_node);
1941 if (ref->bytenr != head->node.bytenr)
1942 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001943 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001944 return ref;
1945 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001946 }
Chris Mason56bec292009-03-13 10:10:06 -04001947 if (action == BTRFS_ADD_DELAYED_REF) {
1948 action = BTRFS_DROP_DELAYED_REF;
1949 goto again;
1950 }
1951 return NULL;
1952}
1953
Chris Masonc3e69d52009-03-13 10:17:05 -04001954static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1955 struct btrfs_root *root,
1956 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001957{
Chris Mason56bec292009-03-13 10:10:06 -04001958 struct btrfs_delayed_ref_root *delayed_refs;
1959 struct btrfs_delayed_ref_node *ref;
1960 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001961 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001962 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001963 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001964 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001965
1966 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001967 while (1) {
1968 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001969 /* pick a new head ref from the cluster list */
1970 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001971 break;
Chris Mason56bec292009-03-13 10:10:06 -04001972
Chris Masonc3e69d52009-03-13 10:17:05 -04001973 locked_ref = list_entry(cluster->next,
1974 struct btrfs_delayed_ref_head, cluster);
1975
1976 /* grab the lock that says we are going to process
1977 * all the refs for this head */
1978 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1979
1980 /*
1981 * we may have dropped the spin lock to get the head
1982 * mutex lock, and that might have given someone else
1983 * time to free the head. If that's true, it has been
1984 * removed from our list and we can move on.
1985 */
1986 if (ret == -EAGAIN) {
1987 locked_ref = NULL;
1988 count++;
1989 continue;
Chris Mason56bec292009-03-13 10:10:06 -04001990 }
1991 }
1992
1993 /*
1994 * record the must insert reserved flag before we
1995 * drop the spin lock.
1996 */
1997 must_insert_reserved = locked_ref->must_insert_reserved;
1998 locked_ref->must_insert_reserved = 0;
1999
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002000 extent_op = locked_ref->extent_op;
2001 locked_ref->extent_op = NULL;
2002
Chris Mason56bec292009-03-13 10:10:06 -04002003 /*
2004 * locked_ref is the head node, so we have to go one
2005 * node back for any delayed ref updates
2006 */
Chris Mason56bec292009-03-13 10:10:06 -04002007 ref = select_delayed_ref(locked_ref);
2008 if (!ref) {
2009 /* All delayed refs have been processed, Go ahead
2010 * and send the head node to run_one_delayed_ref,
2011 * so that any accounting fixes can happen
2012 */
2013 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002014
2015 if (extent_op && must_insert_reserved) {
2016 kfree(extent_op);
2017 extent_op = NULL;
2018 }
2019
2020 if (extent_op) {
2021 spin_unlock(&delayed_refs->lock);
2022
2023 ret = run_delayed_extent_op(trans, root,
2024 ref, extent_op);
2025 BUG_ON(ret);
2026 kfree(extent_op);
2027
2028 cond_resched();
2029 spin_lock(&delayed_refs->lock);
2030 continue;
2031 }
2032
Chris Masonc3e69d52009-03-13 10:17:05 -04002033 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002034 locked_ref = NULL;
2035 }
2036
2037 ref->in_tree = 0;
2038 rb_erase(&ref->rb_node, &delayed_refs->root);
2039 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002040
Chris Mason56bec292009-03-13 10:10:06 -04002041 spin_unlock(&delayed_refs->lock);
2042
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002043 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002044 must_insert_reserved);
2045 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002046
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002047 btrfs_put_delayed_ref(ref);
2048 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002049 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002050
Chris Mason1887be62009-03-13 10:11:24 -04002051 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002052 spin_lock(&delayed_refs->lock);
2053 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002054 return count;
2055}
2056
2057/*
2058 * this starts processing the delayed reference count updates and
2059 * extent insertions we have queued up so far. count can be
2060 * 0, which means to process everything in the tree at the start
2061 * of the run (but not newly added entries), or it can be some target
2062 * number you'd like to process.
2063 */
2064int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2065 struct btrfs_root *root, unsigned long count)
2066{
2067 struct rb_node *node;
2068 struct btrfs_delayed_ref_root *delayed_refs;
2069 struct btrfs_delayed_ref_node *ref;
2070 struct list_head cluster;
2071 int ret;
2072 int run_all = count == (unsigned long)-1;
2073 int run_most = 0;
2074
2075 if (root == root->fs_info->extent_root)
2076 root = root->fs_info->tree_root;
2077
2078 delayed_refs = &trans->transaction->delayed_refs;
2079 INIT_LIST_HEAD(&cluster);
2080again:
2081 spin_lock(&delayed_refs->lock);
2082 if (count == 0) {
2083 count = delayed_refs->num_entries * 2;
2084 run_most = 1;
2085 }
2086 while (1) {
2087 if (!(run_all || run_most) &&
2088 delayed_refs->num_heads_ready < 64)
2089 break;
2090
2091 /*
2092 * go find something we can process in the rbtree. We start at
2093 * the beginning of the tree, and then build a cluster
2094 * of refs to process starting at the first one we are able to
2095 * lock
2096 */
2097 ret = btrfs_find_ref_cluster(trans, &cluster,
2098 delayed_refs->run_delayed_start);
2099 if (ret)
2100 break;
2101
2102 ret = run_clustered_refs(trans, root, &cluster);
2103 BUG_ON(ret < 0);
2104
2105 count -= min_t(unsigned long, ret, count);
2106
2107 if (count == 0)
2108 break;
2109 }
2110
Chris Mason56bec292009-03-13 10:10:06 -04002111 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002112 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002113 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002114 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002115 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002116
2117 while (node) {
2118 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2119 rb_node);
2120 if (btrfs_delayed_ref_is_head(ref)) {
2121 struct btrfs_delayed_ref_head *head;
2122
2123 head = btrfs_delayed_node_to_head(ref);
2124 atomic_inc(&ref->refs);
2125
2126 spin_unlock(&delayed_refs->lock);
2127 mutex_lock(&head->mutex);
2128 mutex_unlock(&head->mutex);
2129
2130 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002131 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002132 goto again;
2133 }
2134 node = rb_next(node);
2135 }
2136 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002137 schedule_timeout(1);
2138 goto again;
2139 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002140out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002141 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002142 return 0;
2143}
2144
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002145int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2146 struct btrfs_root *root,
2147 u64 bytenr, u64 num_bytes, u64 flags,
2148 int is_data)
2149{
2150 struct btrfs_delayed_extent_op *extent_op;
2151 int ret;
2152
2153 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2154 if (!extent_op)
2155 return -ENOMEM;
2156
2157 extent_op->flags_to_set = flags;
2158 extent_op->update_flags = 1;
2159 extent_op->update_key = 0;
2160 extent_op->is_data = is_data ? 1 : 0;
2161
2162 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2163 if (ret)
2164 kfree(extent_op);
2165 return ret;
2166}
2167
2168static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2169 struct btrfs_root *root,
2170 struct btrfs_path *path,
2171 u64 objectid, u64 offset, u64 bytenr)
2172{
2173 struct btrfs_delayed_ref_head *head;
2174 struct btrfs_delayed_ref_node *ref;
2175 struct btrfs_delayed_data_ref *data_ref;
2176 struct btrfs_delayed_ref_root *delayed_refs;
2177 struct rb_node *node;
2178 int ret = 0;
2179
2180 ret = -ENOENT;
2181 delayed_refs = &trans->transaction->delayed_refs;
2182 spin_lock(&delayed_refs->lock);
2183 head = btrfs_find_delayed_ref_head(trans, bytenr);
2184 if (!head)
2185 goto out;
2186
2187 if (!mutex_trylock(&head->mutex)) {
2188 atomic_inc(&head->node.refs);
2189 spin_unlock(&delayed_refs->lock);
2190
2191 btrfs_release_path(root->fs_info->extent_root, path);
2192
2193 mutex_lock(&head->mutex);
2194 mutex_unlock(&head->mutex);
2195 btrfs_put_delayed_ref(&head->node);
2196 return -EAGAIN;
2197 }
2198
2199 node = rb_prev(&head->node.rb_node);
2200 if (!node)
2201 goto out_unlock;
2202
2203 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2204
2205 if (ref->bytenr != bytenr)
2206 goto out_unlock;
2207
2208 ret = 1;
2209 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2210 goto out_unlock;
2211
2212 data_ref = btrfs_delayed_node_to_data_ref(ref);
2213
2214 node = rb_prev(node);
2215 if (node) {
2216 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2217 if (ref->bytenr == bytenr)
2218 goto out_unlock;
2219 }
2220
2221 if (data_ref->root != root->root_key.objectid ||
2222 data_ref->objectid != objectid || data_ref->offset != offset)
2223 goto out_unlock;
2224
2225 ret = 0;
2226out_unlock:
2227 mutex_unlock(&head->mutex);
2228out:
2229 spin_unlock(&delayed_refs->lock);
2230 return ret;
2231}
2232
2233static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2234 struct btrfs_root *root,
2235 struct btrfs_path *path,
2236 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002237{
2238 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002239 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002240 struct btrfs_extent_data_ref *ref;
2241 struct btrfs_extent_inline_ref *iref;
2242 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002243 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002244 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002245 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002246
Chris Masonbe20aa92007-12-17 20:14:01 -05002247 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002248 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002249 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002250
Chris Masonbe20aa92007-12-17 20:14:01 -05002251 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2252 if (ret < 0)
2253 goto out;
2254 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002255
2256 ret = -ENOENT;
2257 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002258 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002259
Zheng Yan31840ae2008-09-23 13:14:14 -04002260 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002261 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002262 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002263
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002264 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002265 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002266
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002267 ret = 1;
2268 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2269#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2270 if (item_size < sizeof(*ei)) {
2271 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2272 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002273 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002274#endif
2275 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2276
2277 if (item_size != sizeof(*ei) +
2278 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2279 goto out;
2280
2281 if (btrfs_extent_generation(leaf, ei) <=
2282 btrfs_root_last_snapshot(&root->root_item))
2283 goto out;
2284
2285 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2286 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2287 BTRFS_EXTENT_DATA_REF_KEY)
2288 goto out;
2289
2290 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2291 if (btrfs_extent_refs(leaf, ei) !=
2292 btrfs_extent_data_ref_count(leaf, ref) ||
2293 btrfs_extent_data_ref_root(leaf, ref) !=
2294 root->root_key.objectid ||
2295 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2296 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2297 goto out;
2298
Yan Zhengf321e492008-07-30 09:26:11 -04002299 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002300out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002301 return ret;
2302}
2303
2304int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2305 struct btrfs_root *root,
2306 u64 objectid, u64 offset, u64 bytenr)
2307{
2308 struct btrfs_path *path;
2309 int ret;
2310 int ret2;
2311
2312 path = btrfs_alloc_path();
2313 if (!path)
2314 return -ENOENT;
2315
2316 do {
2317 ret = check_committed_ref(trans, root, path, objectid,
2318 offset, bytenr);
2319 if (ret && ret != -ENOENT)
2320 goto out;
2321
2322 ret2 = check_delayed_ref(trans, root, path, objectid,
2323 offset, bytenr);
2324 } while (ret2 == -EAGAIN);
2325
2326 if (ret2 && ret2 != -ENOENT) {
2327 ret = ret2;
2328 goto out;
2329 }
2330
2331 if (ret != -ENOENT || ret2 != -ENOENT)
2332 ret = 0;
2333out:
Yan Zhengf321e492008-07-30 09:26:11 -04002334 btrfs_free_path(path);
2335 return ret;
2336}
2337
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002338#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002339int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2340 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002341{
Chris Mason5f39d392007-10-15 16:14:19 -04002342 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002343 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002344 u64 root_gen;
2345 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002346 int i;
Chris Masondb945352007-10-15 16:15:53 -04002347 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002348 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002349 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002350
Chris Mason3768f362007-03-13 16:47:54 -04002351 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002352 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002353
Zheng Yane4657682008-09-26 10:04:53 -04002354 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2355 shared = 0;
2356 root_gen = root->root_key.offset;
2357 } else {
2358 shared = 1;
2359 root_gen = trans->transid - 1;
2360 }
2361
Chris Masondb945352007-10-15 16:15:53 -04002362 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002363 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002364
Zheng Yan31840ae2008-09-23 13:14:14 -04002365 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002366 struct btrfs_leaf_ref *ref;
2367 struct btrfs_extent_info *info;
2368
Zheng Yan31840ae2008-09-23 13:14:14 -04002369 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002370 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002371 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002372 goto out;
2373 }
2374
Zheng Yane4657682008-09-26 10:04:53 -04002375 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002376 ref->bytenr = buf->start;
2377 ref->owner = btrfs_header_owner(buf);
2378 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002379 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002380 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002381
Zheng Yan31840ae2008-09-23 13:14:14 -04002382 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002383 u64 disk_bytenr;
2384 btrfs_item_key_to_cpu(buf, &key, i);
2385 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2386 continue;
2387 fi = btrfs_item_ptr(buf, i,
2388 struct btrfs_file_extent_item);
2389 if (btrfs_file_extent_type(buf, fi) ==
2390 BTRFS_FILE_EXTENT_INLINE)
2391 continue;
2392 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2393 if (disk_bytenr == 0)
2394 continue;
2395
2396 info->bytenr = disk_bytenr;
2397 info->num_bytes =
2398 btrfs_file_extent_disk_num_bytes(buf, fi);
2399 info->objectid = key.objectid;
2400 info->offset = key.offset;
2401 info++;
2402 }
2403
Zheng Yane4657682008-09-26 10:04:53 -04002404 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002405 if (ret == -EEXIST && shared) {
2406 struct btrfs_leaf_ref *old;
2407 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2408 BUG_ON(!old);
2409 btrfs_remove_leaf_ref(root, old);
2410 btrfs_free_leaf_ref(root, old);
2411 ret = btrfs_add_leaf_ref(root, ref, shared);
2412 }
Yan Zheng31153d82008-07-28 15:32:19 -04002413 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002414 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002415 }
2416out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002417 return ret;
2418}
2419
Chris Masonb7a9f292009-02-04 09:23:45 -05002420/* when a block goes through cow, we update the reference counts of
2421 * everything that block points to. The internal pointers of the block
2422 * can be in just about any order, and it is likely to have clusters of
2423 * things that are close together and clusters of things that are not.
2424 *
2425 * To help reduce the seeks that come with updating all of these reference
2426 * counts, sort them by byte number before actual updates are done.
2427 *
2428 * struct refsort is used to match byte number to slot in the btree block.
2429 * we sort based on the byte number and then use the slot to actually
2430 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002431 *
2432 * struct refsort is smaller than strcut btrfs_item and smaller than
2433 * struct btrfs_key_ptr. Since we're currently limited to the page size
2434 * for a btree block, there's no way for a kmalloc of refsorts for a
2435 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002436 */
2437struct refsort {
2438 u64 bytenr;
2439 u32 slot;
2440};
2441
2442/*
2443 * for passing into sort()
2444 */
2445static int refsort_cmp(const void *a_void, const void *b_void)
2446{
2447 const struct refsort *a = a_void;
2448 const struct refsort *b = b_void;
2449
2450 if (a->bytenr < b->bytenr)
2451 return -1;
2452 if (a->bytenr > b->bytenr)
2453 return 1;
2454 return 0;
2455}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002456#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002457
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002458static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002459 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002460 struct extent_buffer *buf,
2461 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002462{
2463 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002464 u64 num_bytes;
2465 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002466 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002467 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002468 struct btrfs_key key;
2469 struct btrfs_file_extent_item *fi;
2470 int i;
2471 int level;
2472 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002473 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002474 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002475
2476 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002477 nritems = btrfs_header_nritems(buf);
2478 level = btrfs_header_level(buf);
2479
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002480 if (!root->ref_cows && level == 0)
2481 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002482
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002483 if (inc)
2484 process_func = btrfs_inc_extent_ref;
2485 else
2486 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002487
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002488 if (full_backref)
2489 parent = buf->start;
2490 else
2491 parent = 0;
2492
Zheng Yan31840ae2008-09-23 13:14:14 -04002493 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002494 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002495 btrfs_item_key_to_cpu(buf, &key, i);
2496 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002497 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002498 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002499 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002500 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002501 BTRFS_FILE_EXTENT_INLINE)
2502 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002503 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2504 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002505 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002506
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002507 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2508 key.offset -= btrfs_file_extent_offset(buf, fi);
2509 ret = process_func(trans, root, bytenr, num_bytes,
2510 parent, ref_root, key.objectid,
2511 key.offset);
2512 if (ret)
2513 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002514 } else {
2515 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002516 num_bytes = btrfs_level_size(root, level - 1);
2517 ret = process_func(trans, root, bytenr, num_bytes,
2518 parent, ref_root, level - 1, 0);
2519 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002520 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002521 }
2522 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002523 return 0;
2524fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002525 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002526 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002527}
2528
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002529int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2530 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002531{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002532 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2533}
Zheng Yan31840ae2008-09-23 13:14:14 -04002534
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002535int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2536 struct extent_buffer *buf, int full_backref)
2537{
2538 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002539}
2540
Chris Mason9078a3e2007-04-26 16:46:15 -04002541static int write_one_cache_group(struct btrfs_trans_handle *trans,
2542 struct btrfs_root *root,
2543 struct btrfs_path *path,
2544 struct btrfs_block_group_cache *cache)
2545{
2546 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002547 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002548 unsigned long bi;
2549 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002550
Chris Mason9078a3e2007-04-26 16:46:15 -04002551 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002552 if (ret < 0)
2553 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002554 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002555
2556 leaf = path->nodes[0];
2557 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2558 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2559 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002560 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002561fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002562 if (ret)
2563 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002564 return 0;
2565
2566}
2567
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002568static struct btrfs_block_group_cache *
2569next_block_group(struct btrfs_root *root,
2570 struct btrfs_block_group_cache *cache)
2571{
2572 struct rb_node *node;
2573 spin_lock(&root->fs_info->block_group_cache_lock);
2574 node = rb_next(&cache->cache_node);
2575 btrfs_put_block_group(cache);
2576 if (node) {
2577 cache = rb_entry(node, struct btrfs_block_group_cache,
2578 cache_node);
2579 atomic_inc(&cache->count);
2580 } else
2581 cache = NULL;
2582 spin_unlock(&root->fs_info->block_group_cache_lock);
2583 return cache;
2584}
2585
Chris Mason96b51792007-10-15 16:15:19 -04002586int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2587 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002588{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002589 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002590 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002591 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002592 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002593
2594 path = btrfs_alloc_path();
2595 if (!path)
2596 return -ENOMEM;
2597
Chris Masond3977122009-01-05 21:25:51 -05002598 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002599 if (last == 0) {
2600 err = btrfs_run_delayed_refs(trans, root,
2601 (unsigned long)-1);
2602 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002603 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002604
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002605 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2606 while (cache) {
2607 if (cache->dirty)
2608 break;
2609 cache = next_block_group(root, cache);
2610 }
2611 if (!cache) {
2612 if (last == 0)
2613 break;
2614 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002615 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002616 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002617
2618 cache->dirty = 0;
2619 last = cache->key.objectid + cache->key.offset;
2620
2621 err = write_one_cache_group(trans, root, path, cache);
2622 BUG_ON(err);
2623 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002624 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002625
Chris Mason9078a3e2007-04-26 16:46:15 -04002626 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002627 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002628}
2629
Yan Zhengd2fb3432008-12-11 16:30:39 -05002630int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2631{
2632 struct btrfs_block_group_cache *block_group;
2633 int readonly = 0;
2634
2635 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2636 if (!block_group || block_group->ro)
2637 readonly = 1;
2638 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002639 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002640 return readonly;
2641}
2642
Chris Mason593060d2008-03-25 16:50:33 -04002643static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2644 u64 total_bytes, u64 bytes_used,
2645 struct btrfs_space_info **space_info)
2646{
2647 struct btrfs_space_info *found;
2648
2649 found = __find_space_info(info, flags);
2650 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002651 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002652 found->total_bytes += total_bytes;
2653 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002654 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002655 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002656 *space_info = found;
2657 return 0;
2658 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002659 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002660 if (!found)
2661 return -ENOMEM;
2662
Josef Bacik0f9dd462008-09-23 13:14:11 -04002663 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002664 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002665 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002666 found->flags = flags;
2667 found->total_bytes = total_bytes;
2668 found->bytes_used = bytes_used;
2669 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002670 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002671 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002672 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002673 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002674 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002675 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002676 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002677 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002678 return 0;
2679}
2680
Chris Mason8790d502008-04-03 16:29:03 -04002681static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2682{
2683 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002684 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002685 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002686 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002687 if (extra_flags) {
2688 if (flags & BTRFS_BLOCK_GROUP_DATA)
2689 fs_info->avail_data_alloc_bits |= extra_flags;
2690 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2691 fs_info->avail_metadata_alloc_bits |= extra_flags;
2692 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2693 fs_info->avail_system_alloc_bits |= extra_flags;
2694 }
2695}
Chris Mason593060d2008-03-25 16:50:33 -04002696
Yan Zhengc146afa2008-11-12 14:34:12 -05002697static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2698{
2699 spin_lock(&cache->space_info->lock);
2700 spin_lock(&cache->lock);
2701 if (!cache->ro) {
2702 cache->space_info->bytes_readonly += cache->key.offset -
2703 btrfs_block_group_used(&cache->item);
2704 cache->ro = 1;
2705 }
2706 spin_unlock(&cache->lock);
2707 spin_unlock(&cache->space_info->lock);
2708}
2709
Yan Zheng2b820322008-11-17 21:11:30 -05002710u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002711{
Yan Zheng2b820322008-11-17 21:11:30 -05002712 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002713
2714 if (num_devices == 1)
2715 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2716 if (num_devices < 4)
2717 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2718
Chris Masonec44a352008-04-28 15:29:52 -04002719 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2720 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002721 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002722 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002723 }
Chris Masonec44a352008-04-28 15:29:52 -04002724
2725 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002726 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002727 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002728 }
Chris Masonec44a352008-04-28 15:29:52 -04002729
2730 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2731 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2732 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2733 (flags & BTRFS_BLOCK_GROUP_DUP)))
2734 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2735 return flags;
2736}
2737
Josef Bacik6a632092009-02-20 11:00:09 -05002738static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2739{
2740 struct btrfs_fs_info *info = root->fs_info;
2741 u64 alloc_profile;
2742
2743 if (data) {
2744 alloc_profile = info->avail_data_alloc_bits &
2745 info->data_alloc_profile;
2746 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2747 } else if (root == root->fs_info->chunk_root) {
2748 alloc_profile = info->avail_system_alloc_bits &
2749 info->system_alloc_profile;
2750 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2751 } else {
2752 alloc_profile = info->avail_metadata_alloc_bits &
2753 info->metadata_alloc_profile;
2754 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2755 }
2756
2757 return btrfs_reduce_alloc_profile(root, data);
2758}
2759
2760void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2761{
2762 u64 alloc_target;
2763
2764 alloc_target = btrfs_get_alloc_profile(root, 1);
2765 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2766 alloc_target);
2767}
2768
Josef Bacik9ed74f22009-09-11 16:12:44 -04002769static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2770{
2771 u64 num_bytes;
2772 int level;
2773
2774 level = BTRFS_MAX_LEVEL - 2;
2775 /*
2776 * NOTE: these calculations are absolutely the worst possible case.
2777 * This assumes that _every_ item we insert will require a new leaf, and
2778 * that the tree has grown to its maximum level size.
2779 */
2780
2781 /*
2782 * for every item we insert we could insert both an extent item and a
2783 * extent ref item. Then for ever item we insert, we will need to cow
2784 * both the original leaf, plus the leaf to the left and right of it.
2785 *
2786 * Unless we are talking about the extent root, then we just want the
2787 * number of items * 2, since we just need the extent item plus its ref.
2788 */
2789 if (root == root->fs_info->extent_root)
2790 num_bytes = num_items * 2;
2791 else
2792 num_bytes = (num_items + (2 * num_items)) * 3;
2793
2794 /*
2795 * num_bytes is total number of leaves we could need times the leaf
2796 * size, and then for every leaf we could end up cow'ing 2 nodes per
2797 * level, down to the leaf level.
2798 */
2799 num_bytes = (num_bytes * root->leafsize) +
2800 (num_bytes * (level * 2)) * root->nodesize;
2801
2802 return num_bytes;
2803}
2804
Josef Bacik6a632092009-02-20 11:00:09 -05002805/*
Josef Bacik9ed74f22009-09-11 16:12:44 -04002806 * Unreserve metadata space for delalloc. If we have less reserved credits than
2807 * we have extents, this function does nothing.
Josef Bacik6a632092009-02-20 11:00:09 -05002808 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04002809int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2810 struct inode *inode, int num_items)
Josef Bacik6a632092009-02-20 11:00:09 -05002811{
2812 struct btrfs_fs_info *info = root->fs_info;
2813 struct btrfs_space_info *meta_sinfo;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002814 u64 num_bytes;
2815 u64 alloc_target;
2816 bool bug = false;
Josef Bacik6a632092009-02-20 11:00:09 -05002817
2818 /* get the space info for where the metadata will live */
2819 alloc_target = btrfs_get_alloc_profile(root, 0);
2820 meta_sinfo = __find_space_info(info, alloc_target);
2821
Josef Bacik9ed74f22009-09-11 16:12:44 -04002822 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2823 num_items);
2824
2825 spin_lock(&meta_sinfo->lock);
2826 if (BTRFS_I(inode)->delalloc_reserved_extents <=
2827 BTRFS_I(inode)->delalloc_extents) {
2828 spin_unlock(&meta_sinfo->lock);
2829 return 0;
2830 }
2831
2832 BTRFS_I(inode)->delalloc_reserved_extents--;
2833 BUG_ON(BTRFS_I(inode)->delalloc_reserved_extents < 0);
2834
2835 if (meta_sinfo->bytes_delalloc < num_bytes) {
2836 bug = true;
2837 meta_sinfo->bytes_delalloc = 0;
2838 } else {
2839 meta_sinfo->bytes_delalloc -= num_bytes;
2840 }
2841 spin_unlock(&meta_sinfo->lock);
2842
2843 BUG_ON(bug);
2844
2845 return 0;
2846}
2847
2848static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2849{
2850 u64 thresh;
2851
2852 thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2853 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2854 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2855 meta_sinfo->bytes_may_use;
2856
2857 thresh = meta_sinfo->total_bytes - thresh;
2858 thresh *= 80;
2859 do_div(thresh, 100);
2860 if (thresh <= meta_sinfo->bytes_delalloc)
2861 meta_sinfo->force_delalloc = 1;
2862 else
2863 meta_sinfo->force_delalloc = 0;
2864}
2865
2866static int maybe_allocate_chunk(struct btrfs_root *root,
2867 struct btrfs_space_info *info)
2868{
2869 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
2870 struct btrfs_trans_handle *trans;
2871 bool wait = false;
2872 int ret = 0;
2873 u64 min_metadata;
2874 u64 free_space;
2875
2876 free_space = btrfs_super_total_bytes(disk_super);
2877 /*
2878 * we allow the metadata to grow to a max of either 5gb or 5% of the
2879 * space in the volume.
2880 */
2881 min_metadata = min((u64)5 * 1024 * 1024 * 1024,
2882 div64_u64(free_space * 5, 100));
2883 if (info->total_bytes >= min_metadata) {
2884 spin_unlock(&info->lock);
2885 return 0;
2886 }
2887
2888 if (info->full) {
2889 spin_unlock(&info->lock);
2890 return 0;
2891 }
2892
2893 if (!info->allocating_chunk) {
2894 info->force_alloc = 1;
2895 info->allocating_chunk = 1;
2896 init_waitqueue_head(&info->wait);
2897 } else {
2898 wait = true;
2899 }
2900
2901 spin_unlock(&info->lock);
2902
2903 if (wait) {
2904 wait_event(info->wait,
2905 !info->allocating_chunk);
2906 return 1;
2907 }
2908
2909 trans = btrfs_start_transaction(root, 1);
2910 if (!trans) {
2911 ret = -ENOMEM;
2912 goto out;
2913 }
2914
2915 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2916 4096 + 2 * 1024 * 1024,
2917 info->flags, 0);
2918 btrfs_end_transaction(trans, root);
2919 if (ret)
2920 goto out;
2921out:
2922 spin_lock(&info->lock);
2923 info->allocating_chunk = 0;
2924 spin_unlock(&info->lock);
2925 wake_up(&info->wait);
2926
2927 if (ret)
2928 return 0;
2929 return 1;
2930}
2931
2932/*
2933 * Reserve metadata space for delalloc.
2934 */
2935int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
2936 struct inode *inode, int num_items)
2937{
2938 struct btrfs_fs_info *info = root->fs_info;
2939 struct btrfs_space_info *meta_sinfo;
2940 u64 num_bytes;
2941 u64 used;
2942 u64 alloc_target;
2943 int flushed = 0;
2944 int force_delalloc;
2945
2946 /* get the space info for where the metadata will live */
2947 alloc_target = btrfs_get_alloc_profile(root, 0);
2948 meta_sinfo = __find_space_info(info, alloc_target);
2949
2950 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2951 num_items);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002952again:
Josef Bacik6a632092009-02-20 11:00:09 -05002953 spin_lock(&meta_sinfo->lock);
Josef Bacik6a632092009-02-20 11:00:09 -05002954
Josef Bacik9ed74f22009-09-11 16:12:44 -04002955 force_delalloc = meta_sinfo->force_delalloc;
Josef Bacik6a632092009-02-20 11:00:09 -05002956
Josef Bacik9ed74f22009-09-11 16:12:44 -04002957 if (unlikely(!meta_sinfo->bytes_root))
2958 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
2959
2960 if (!flushed)
2961 meta_sinfo->bytes_delalloc += num_bytes;
2962
2963 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2964 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2965 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2966 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
2967
2968 if (used > meta_sinfo->total_bytes) {
2969 flushed++;
2970
2971 if (flushed == 1) {
2972 if (maybe_allocate_chunk(root, meta_sinfo))
2973 goto again;
2974 flushed++;
2975 } else {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002976 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002977 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002978
Josef Bacik9ed74f22009-09-11 16:12:44 -04002979 if (flushed == 2) {
2980 filemap_flush(inode->i_mapping);
2981 goto again;
2982 } else if (flushed == 3) {
2983 btrfs_start_delalloc_inodes(root);
2984 btrfs_wait_ordered_extents(root, 0);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002985 goto again;
2986 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04002987 spin_lock(&meta_sinfo->lock);
2988 meta_sinfo->bytes_delalloc -= num_bytes;
Josef Bacik6a632092009-02-20 11:00:09 -05002989 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002990 printk(KERN_ERR "enospc, has %d, reserved %d\n",
2991 BTRFS_I(inode)->delalloc_extents,
2992 BTRFS_I(inode)->delalloc_reserved_extents);
2993 dump_space_info(meta_sinfo, 0, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05002994 return -ENOSPC;
2995 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04002996
2997 BTRFS_I(inode)->delalloc_reserved_extents++;
2998 check_force_delalloc(meta_sinfo);
2999 spin_unlock(&meta_sinfo->lock);
3000
3001 if (!flushed && force_delalloc)
3002 filemap_flush(inode->i_mapping);
3003
3004 return 0;
3005}
3006
3007/*
3008 * unreserve num_items number of items worth of metadata space. This needs to
3009 * be paired with btrfs_reserve_metadata_space.
3010 *
3011 * NOTE: if you have the option, run this _AFTER_ you do a
3012 * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
3013 * oprations which will result in more used metadata, so we want to make sure we
3014 * can do that without issue.
3015 */
3016int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
3017{
3018 struct btrfs_fs_info *info = root->fs_info;
3019 struct btrfs_space_info *meta_sinfo;
3020 u64 num_bytes;
3021 u64 alloc_target;
3022 bool bug = false;
3023
3024 /* get the space info for where the metadata will live */
3025 alloc_target = btrfs_get_alloc_profile(root, 0);
3026 meta_sinfo = __find_space_info(info, alloc_target);
3027
3028 num_bytes = calculate_bytes_needed(root, num_items);
3029
3030 spin_lock(&meta_sinfo->lock);
3031 if (meta_sinfo->bytes_may_use < num_bytes) {
3032 bug = true;
3033 meta_sinfo->bytes_may_use = 0;
3034 } else {
3035 meta_sinfo->bytes_may_use -= num_bytes;
3036 }
3037 spin_unlock(&meta_sinfo->lock);
3038
3039 BUG_ON(bug);
3040
3041 return 0;
3042}
3043
3044/*
3045 * Reserve some metadata space for use. We'll calculate the worste case number
3046 * of bytes that would be needed to modify num_items number of items. If we
3047 * have space, fantastic, if not, you get -ENOSPC. Please call
3048 * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
3049 * items you reserved, since whatever metadata you needed should have already
3050 * been allocated.
3051 *
3052 * This will commit the transaction to make more space if we don't have enough
3053 * metadata space. THe only time we don't do this is if we're reserving space
3054 * inside of a transaction, then we will just return -ENOSPC and it is the
3055 * callers responsibility to handle it properly.
3056 */
3057int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3058{
3059 struct btrfs_fs_info *info = root->fs_info;
3060 struct btrfs_space_info *meta_sinfo;
3061 u64 num_bytes;
3062 u64 used;
3063 u64 alloc_target;
3064 int retries = 0;
3065
3066 /* get the space info for where the metadata will live */
3067 alloc_target = btrfs_get_alloc_profile(root, 0);
3068 meta_sinfo = __find_space_info(info, alloc_target);
3069
3070 num_bytes = calculate_bytes_needed(root, num_items);
3071again:
3072 spin_lock(&meta_sinfo->lock);
3073
3074 if (unlikely(!meta_sinfo->bytes_root))
3075 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3076
3077 if (!retries)
3078 meta_sinfo->bytes_may_use += num_bytes;
3079
3080 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3081 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3082 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3083 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3084
3085 if (used > meta_sinfo->total_bytes) {
3086 retries++;
3087 if (retries == 1) {
3088 if (maybe_allocate_chunk(root, meta_sinfo))
3089 goto again;
3090 retries++;
3091 } else {
3092 spin_unlock(&meta_sinfo->lock);
3093 }
3094
3095 if (retries == 2) {
3096 btrfs_start_delalloc_inodes(root);
3097 btrfs_wait_ordered_extents(root, 0);
3098 goto again;
3099 }
3100 spin_lock(&meta_sinfo->lock);
3101 meta_sinfo->bytes_may_use -= num_bytes;
3102 spin_unlock(&meta_sinfo->lock);
3103
3104 dump_space_info(meta_sinfo, 0, 0);
3105 return -ENOSPC;
3106 }
3107
3108 check_force_delalloc(meta_sinfo);
Josef Bacik6a632092009-02-20 11:00:09 -05003109 spin_unlock(&meta_sinfo->lock);
3110
3111 return 0;
3112}
3113
3114/*
3115 * This will check the space that the inode allocates from to make sure we have
3116 * enough space for bytes.
3117 */
3118int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3119 u64 bytes)
3120{
3121 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003122 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003123
3124 /* make sure bytes are sectorsize aligned */
3125 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3126
3127 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003128 if (!data_sinfo)
3129 goto alloc;
3130
Josef Bacik6a632092009-02-20 11:00:09 -05003131again:
3132 /* make sure we have enough space to handle the data first */
3133 spin_lock(&data_sinfo->lock);
3134 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
3135 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
3136 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
Josef Bacik1b2da372009-09-11 16:11:20 -04003137 data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003138 struct btrfs_trans_handle *trans;
3139
Josef Bacik6a632092009-02-20 11:00:09 -05003140 /*
3141 * if we don't have enough free bytes in this space then we need
3142 * to alloc a new chunk.
3143 */
3144 if (!data_sinfo->full) {
3145 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003146
3147 data_sinfo->force_alloc = 1;
3148 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003149alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003150 alloc_target = btrfs_get_alloc_profile(root, 1);
3151 trans = btrfs_start_transaction(root, 1);
3152 if (!trans)
3153 return -ENOMEM;
3154
3155 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3156 bytes + 2 * 1024 * 1024,
3157 alloc_target, 0);
3158 btrfs_end_transaction(trans, root);
3159 if (ret)
3160 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04003161
3162 if (!data_sinfo) {
3163 btrfs_set_inode_space_info(root, inode);
3164 data_sinfo = BTRFS_I(inode)->space_info;
3165 }
Josef Bacik6a632092009-02-20 11:00:09 -05003166 goto again;
3167 }
3168 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003169
3170 /* commit the current transaction and try again */
3171 if (!committed) {
3172 committed = 1;
3173 trans = btrfs_join_transaction(root, 1);
3174 if (!trans)
3175 return -ENOMEM;
3176 ret = btrfs_commit_transaction(trans, root);
3177 if (ret)
3178 return ret;
3179 goto again;
3180 }
3181
Josef Bacik6a632092009-02-20 11:00:09 -05003182 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3183 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04003184 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07003185 "%llu total\n", (unsigned long long)bytes,
3186 (unsigned long long)data_sinfo->bytes_delalloc,
3187 (unsigned long long)data_sinfo->bytes_used,
3188 (unsigned long long)data_sinfo->bytes_reserved,
3189 (unsigned long long)data_sinfo->bytes_pinned,
3190 (unsigned long long)data_sinfo->bytes_readonly,
3191 (unsigned long long)data_sinfo->bytes_may_use,
3192 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003193 return -ENOSPC;
3194 }
3195 data_sinfo->bytes_may_use += bytes;
3196 BTRFS_I(inode)->reserved_bytes += bytes;
3197 spin_unlock(&data_sinfo->lock);
3198
Josef Bacik9ed74f22009-09-11 16:12:44 -04003199 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003200}
3201
3202/*
3203 * if there was an error for whatever reason after calling
3204 * btrfs_check_data_free_space, call this so we can cleanup the counters.
3205 */
3206void btrfs_free_reserved_data_space(struct btrfs_root *root,
3207 struct inode *inode, u64 bytes)
3208{
3209 struct btrfs_space_info *data_sinfo;
3210
3211 /* make sure bytes are sectorsize aligned */
3212 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3213
3214 data_sinfo = BTRFS_I(inode)->space_info;
3215 spin_lock(&data_sinfo->lock);
3216 data_sinfo->bytes_may_use -= bytes;
3217 BTRFS_I(inode)->reserved_bytes -= bytes;
3218 spin_unlock(&data_sinfo->lock);
3219}
3220
3221/* called when we are adding a delalloc extent to the inode's io_tree */
3222void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3223 u64 bytes)
3224{
3225 struct btrfs_space_info *data_sinfo;
3226
3227 /* get the space info for where this inode will be storing its data */
3228 data_sinfo = BTRFS_I(inode)->space_info;
3229
3230 /* make sure we have enough space to handle the data first */
3231 spin_lock(&data_sinfo->lock);
3232 data_sinfo->bytes_delalloc += bytes;
3233
3234 /*
3235 * we are adding a delalloc extent without calling
3236 * btrfs_check_data_free_space first. This happens on a weird
3237 * writepage condition, but shouldn't hurt our accounting
3238 */
3239 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3240 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3241 BTRFS_I(inode)->reserved_bytes = 0;
3242 } else {
3243 data_sinfo->bytes_may_use -= bytes;
3244 BTRFS_I(inode)->reserved_bytes -= bytes;
3245 }
3246
3247 spin_unlock(&data_sinfo->lock);
3248}
3249
3250/* called when we are clearing an delalloc extent from the inode's io_tree */
3251void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3252 u64 bytes)
3253{
3254 struct btrfs_space_info *info;
3255
3256 info = BTRFS_I(inode)->space_info;
3257
3258 spin_lock(&info->lock);
3259 info->bytes_delalloc -= bytes;
3260 spin_unlock(&info->lock);
3261}
3262
Josef Bacik97e728d2009-04-21 17:40:57 -04003263static void force_metadata_allocation(struct btrfs_fs_info *info)
3264{
3265 struct list_head *head = &info->space_info;
3266 struct btrfs_space_info *found;
3267
3268 rcu_read_lock();
3269 list_for_each_entry_rcu(found, head, list) {
3270 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3271 found->force_alloc = 1;
3272 }
3273 rcu_read_unlock();
3274}
3275
Chris Mason6324fbf2008-03-24 15:01:59 -04003276static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3277 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003278 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003279{
3280 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003281 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003282 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05003283 int ret = 0;
3284
Josef Bacik97e728d2009-04-21 17:40:57 -04003285 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003286
Yan Zheng2b820322008-11-17 21:11:30 -05003287 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003288
Chris Mason6324fbf2008-03-24 15:01:59 -04003289 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003290 if (!space_info) {
3291 ret = update_space_info(extent_root->fs_info, flags,
3292 0, 0, &space_info);
3293 BUG_ON(ret);
3294 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003295 BUG_ON(!space_info);
3296
Josef Bacik25179202008-10-29 14:49:05 -04003297 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003298 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003299 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003300 if (space_info->full) {
3301 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003302 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003303 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003304
Yan Zhengc146afa2008-11-12 14:34:12 -05003305 thresh = space_info->total_bytes - space_info->bytes_readonly;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003306 thresh = div_factor(thresh, 8);
Chris Mason0ef3e662008-05-24 14:04:53 -04003307 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04003308 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04003309 space_info->bytes_reserved + alloc_bytes) < thresh) {
3310 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003311 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003312 }
Josef Bacik25179202008-10-29 14:49:05 -04003313 spin_unlock(&space_info->lock);
3314
Josef Bacik97e728d2009-04-21 17:40:57 -04003315 /*
3316 * if we're doing a data chunk, go ahead and make sure that
3317 * we keep a reasonable number of metadata chunks allocated in the
3318 * FS as well.
3319 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003320 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003321 fs_info->data_chunk_allocations++;
3322 if (!(fs_info->data_chunk_allocations %
3323 fs_info->metadata_ratio))
3324 force_metadata_allocation(fs_info);
3325 }
3326
Yan Zheng2b820322008-11-17 21:11:30 -05003327 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003328 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003329 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003330 space_info->full = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003331 space_info->force_alloc = 0;
3332 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003333out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003334 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003335 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003336}
3337
Chris Mason9078a3e2007-04-26 16:46:15 -04003338static int update_block_group(struct btrfs_trans_handle *trans,
3339 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04003340 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04003341 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04003342{
3343 struct btrfs_block_group_cache *cache;
3344 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04003345 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003346 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003347 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04003348
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003349 /* block accounting for super block */
3350 spin_lock(&info->delalloc_lock);
3351 old_val = btrfs_super_bytes_used(&info->super_copy);
3352 if (alloc)
3353 old_val += num_bytes;
3354 else
3355 old_val -= num_bytes;
3356 btrfs_set_super_bytes_used(&info->super_copy, old_val);
3357
3358 /* block accounting for root item */
3359 old_val = btrfs_root_used(&root->root_item);
3360 if (alloc)
3361 old_val += num_bytes;
3362 else
3363 old_val -= num_bytes;
3364 btrfs_set_root_used(&root->root_item, old_val);
3365 spin_unlock(&info->delalloc_lock);
3366
Chris Masond3977122009-01-05 21:25:51 -05003367 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003368 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003369 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04003370 return -1;
Chris Masondb945352007-10-15 16:15:53 -04003371 byte_in_group = bytenr - cache->key.objectid;
3372 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003373
Josef Bacik25179202008-10-29 14:49:05 -04003374 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003375 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003376 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04003377 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04003378 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04003379 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04003380 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003381 btrfs_set_block_group_used(&cache->item, old_val);
3382 cache->reserved -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003383 cache->space_info->bytes_used += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003384 cache->space_info->bytes_reserved -= num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003385 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05003386 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003387 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003388 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003389 } else {
Chris Masondb945352007-10-15 16:15:53 -04003390 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003391 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05003392 if (cache->ro)
3393 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003394 btrfs_set_block_group_used(&cache->item, old_val);
3395 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003396 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04003397 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003398 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003399
3400 ret = btrfs_discard_extent(root, bytenr,
3401 num_bytes);
3402 WARN_ON(ret);
3403
Josef Bacik0f9dd462008-09-23 13:14:11 -04003404 ret = btrfs_add_free_space(cache, bytenr,
3405 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003406 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04003407 }
Chris Masoncd1bc462007-04-27 10:08:34 -04003408 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003409 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003410 total -= num_bytes;
3411 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003412 }
3413 return 0;
3414}
Chris Mason6324fbf2008-03-24 15:01:59 -04003415
Chris Masona061fc82008-05-07 11:43:44 -04003416static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3417{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003418 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003419 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003420
3421 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3422 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003423 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003424
Yan Zhengd2fb3432008-12-11 16:30:39 -05003425 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003426 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003427
3428 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003429}
3430
Yan Zheng11833d62009-09-11 16:11:19 -04003431/*
3432 * this function must be called within transaction
3433 */
3434int btrfs_pin_extent(struct btrfs_root *root,
3435 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05003436{
Yan324ae4d2007-11-16 14:57:08 -05003437 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -04003438 struct btrfs_block_group_cache *cache;
Yan324ae4d2007-11-16 14:57:08 -05003439
Yan Zheng11833d62009-09-11 16:11:19 -04003440 cache = btrfs_lookup_block_group(fs_info, bytenr);
3441 BUG_ON(!cache);
Chris Masonb9473432009-03-13 11:00:37 -04003442
Yan Zheng11833d62009-09-11 16:11:19 -04003443 spin_lock(&cache->space_info->lock);
3444 spin_lock(&cache->lock);
3445 cache->pinned += num_bytes;
3446 cache->space_info->bytes_pinned += num_bytes;
3447 if (reserved) {
3448 cache->reserved -= num_bytes;
3449 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05003450 }
Yan Zheng11833d62009-09-11 16:11:19 -04003451 spin_unlock(&cache->lock);
3452 spin_unlock(&cache->space_info->lock);
3453
3454 btrfs_put_block_group(cache);
3455
3456 set_extent_dirty(fs_info->pinned_extents,
3457 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Yan324ae4d2007-11-16 14:57:08 -05003458 return 0;
3459}
Chris Mason9078a3e2007-04-26 16:46:15 -04003460
Yan Zheng11833d62009-09-11 16:11:19 -04003461static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3462 u64 num_bytes, int reserve)
Zheng Yane8569812008-09-26 10:05:48 -04003463{
Yan Zheng11833d62009-09-11 16:11:19 -04003464 spin_lock(&cache->space_info->lock);
3465 spin_lock(&cache->lock);
3466 if (reserve) {
3467 cache->reserved += num_bytes;
3468 cache->space_info->bytes_reserved += num_bytes;
3469 } else {
3470 cache->reserved -= num_bytes;
3471 cache->space_info->bytes_reserved -= num_bytes;
3472 }
3473 spin_unlock(&cache->lock);
3474 spin_unlock(&cache->space_info->lock);
3475 return 0;
3476}
Zheng Yane8569812008-09-26 10:05:48 -04003477
Yan Zheng11833d62009-09-11 16:11:19 -04003478int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3479 struct btrfs_root *root)
3480{
3481 struct btrfs_fs_info *fs_info = root->fs_info;
3482 struct btrfs_caching_control *next;
3483 struct btrfs_caching_control *caching_ctl;
3484 struct btrfs_block_group_cache *cache;
3485
3486 down_write(&fs_info->extent_commit_sem);
3487
3488 list_for_each_entry_safe(caching_ctl, next,
3489 &fs_info->caching_block_groups, list) {
3490 cache = caching_ctl->block_group;
3491 if (block_group_cache_done(cache)) {
3492 cache->last_byte_to_unpin = (u64)-1;
3493 list_del_init(&caching_ctl->list);
3494 put_caching_control(caching_ctl);
3495 } else {
3496 cache->last_byte_to_unpin = caching_ctl->progress;
3497 }
3498 }
3499
3500 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3501 fs_info->pinned_extents = &fs_info->freed_extents[1];
3502 else
3503 fs_info->pinned_extents = &fs_info->freed_extents[0];
3504
3505 up_write(&fs_info->extent_commit_sem);
3506 return 0;
3507}
3508
3509static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3510{
3511 struct btrfs_fs_info *fs_info = root->fs_info;
3512 struct btrfs_block_group_cache *cache = NULL;
3513 u64 len;
3514
3515 while (start <= end) {
3516 if (!cache ||
3517 start >= cache->key.objectid + cache->key.offset) {
3518 if (cache)
3519 btrfs_put_block_group(cache);
3520 cache = btrfs_lookup_block_group(fs_info, start);
3521 BUG_ON(!cache);
3522 }
3523
3524 len = cache->key.objectid + cache->key.offset - start;
3525 len = min(len, end + 1 - start);
3526
3527 if (start < cache->last_byte_to_unpin) {
3528 len = min(len, cache->last_byte_to_unpin - start);
3529 btrfs_add_free_space(cache, start, len);
3530 }
Josef Bacik25179202008-10-29 14:49:05 -04003531
3532 spin_lock(&cache->space_info->lock);
3533 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003534 cache->pinned -= len;
3535 cache->space_info->bytes_pinned -= len;
Josef Bacik25179202008-10-29 14:49:05 -04003536 spin_unlock(&cache->lock);
3537 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003538
3539 start += len;
3540 }
3541
3542 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003543 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04003544 return 0;
3545}
3546
3547int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04003548 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05003549{
Yan Zheng11833d62009-09-11 16:11:19 -04003550 struct btrfs_fs_info *fs_info = root->fs_info;
3551 struct extent_io_tree *unpin;
Chris Mason1a5bc1672007-10-15 16:15:26 -04003552 u64 start;
3553 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003554 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003555
Yan Zheng11833d62009-09-11 16:11:19 -04003556 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3557 unpin = &fs_info->freed_extents[1];
3558 else
3559 unpin = &fs_info->freed_extents[0];
3560
Chris Masond3977122009-01-05 21:25:51 -05003561 while (1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04003562 ret = find_first_extent_bit(unpin, 0, &start, &end,
3563 EXTENT_DIRTY);
3564 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003565 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003566
3567 ret = btrfs_discard_extent(root, start, end + 1 - start);
3568
Chris Mason1a5bc1672007-10-15 16:15:26 -04003569 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04003570 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04003571 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003572 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003573
Liu Hui1f3c79a2009-01-05 15:57:51 -05003574 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003575}
3576
Zheng Yan31840ae2008-09-23 13:14:14 -04003577static int pin_down_bytes(struct btrfs_trans_handle *trans,
3578 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003579 struct btrfs_path *path,
Yan Zheng11833d62009-09-11 16:11:19 -04003580 u64 bytenr, u64 num_bytes,
3581 int is_data, int reserved,
Chris Masonb9473432009-03-13 11:00:37 -04003582 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003583{
Chris Mason1a5bc1672007-10-15 16:15:26 -04003584 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003585 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003586
Zheng Yan31840ae2008-09-23 13:14:14 -04003587 if (is_data)
3588 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003589
Zheng Yan31840ae2008-09-23 13:14:14 -04003590 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3591 if (!buf)
3592 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003593
Zheng Yan31840ae2008-09-23 13:14:14 -04003594 /* we can reuse a block if it hasn't been written
3595 * and it is from this transaction. We can't
3596 * reuse anything from the tree log root because
3597 * it has tiny sub-transactions.
3598 */
3599 if (btrfs_buffer_uptodate(buf, 0) &&
3600 btrfs_try_tree_lock(buf)) {
3601 u64 header_owner = btrfs_header_owner(buf);
3602 u64 header_transid = btrfs_header_generation(buf);
3603 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3604 header_transid == trans->transid &&
3605 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003606 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003607 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003608 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003609 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003610 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003611 free_extent_buffer(buf);
3612pinit:
Yan Zheng11833d62009-09-11 16:11:19 -04003613 if (path)
3614 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003615 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003616 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
Zheng Yan31840ae2008-09-23 13:14:14 -04003617
Chris Masonbe744172007-05-06 10:15:01 -04003618 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003619 return 0;
3620}
3621
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003622static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3623 struct btrfs_root *root,
3624 u64 bytenr, u64 num_bytes, u64 parent,
3625 u64 root_objectid, u64 owner_objectid,
3626 u64 owner_offset, int refs_to_drop,
3627 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003628{
Chris Masone2fa7222007-03-12 16:22:34 -04003629 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003630 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003631 struct btrfs_fs_info *info = root->fs_info;
3632 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003633 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003634 struct btrfs_extent_item *ei;
3635 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003636 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003637 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003638 int extent_slot = 0;
3639 int found_extent = 0;
3640 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003641 u32 item_size;
3642 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003643
Chris Mason5caf2a02007-04-02 11:20:42 -04003644 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003645 if (!path)
3646 return -ENOMEM;
3647
Chris Mason3c12ac72008-04-21 12:01:38 -04003648 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003649 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003650
3651 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3652 BUG_ON(!is_data && refs_to_drop != 1);
3653
3654 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3655 bytenr, num_bytes, parent,
3656 root_objectid, owner_objectid,
3657 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003658 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003659 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003660 while (extent_slot >= 0) {
3661 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003662 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003663 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003664 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003665 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3666 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003667 found_extent = 1;
3668 break;
3669 }
3670 if (path->slots[0] - extent_slot > 5)
3671 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003672 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003673 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003674#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3675 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3676 if (found_extent && item_size < sizeof(*ei))
3677 found_extent = 0;
3678#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003679 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003680 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003681 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003682 NULL, refs_to_drop,
3683 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003684 BUG_ON(ret);
3685 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003686 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003687
3688 key.objectid = bytenr;
3689 key.type = BTRFS_EXTENT_ITEM_KEY;
3690 key.offset = num_bytes;
3691
Zheng Yan31840ae2008-09-23 13:14:14 -04003692 ret = btrfs_search_slot(trans, extent_root,
3693 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003694 if (ret) {
3695 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003696 ", was looking for %llu\n", ret,
3697 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003698 btrfs_print_leaf(extent_root, path->nodes[0]);
3699 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003700 BUG_ON(ret);
3701 extent_slot = path->slots[0];
3702 }
Chris Mason7bb86312007-12-11 09:25:06 -05003703 } else {
3704 btrfs_print_leaf(extent_root, path->nodes[0]);
3705 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003706 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003707 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003708 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003709 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003710 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003711 (unsigned long long)owner_objectid,
3712 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003713 }
Chris Mason5f39d392007-10-15 16:14:19 -04003714
3715 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003716 item_size = btrfs_item_size_nr(leaf, extent_slot);
3717#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3718 if (item_size < sizeof(*ei)) {
3719 BUG_ON(found_extent || extent_slot != path->slots[0]);
3720 ret = convert_extent_item_v0(trans, extent_root, path,
3721 owner_objectid, 0);
3722 BUG_ON(ret < 0);
3723
3724 btrfs_release_path(extent_root, path);
3725 path->leave_spinning = 1;
3726
3727 key.objectid = bytenr;
3728 key.type = BTRFS_EXTENT_ITEM_KEY;
3729 key.offset = num_bytes;
3730
3731 ret = btrfs_search_slot(trans, extent_root, &key, path,
3732 -1, 1);
3733 if (ret) {
3734 printk(KERN_ERR "umm, got %d back from search"
3735 ", was looking for %llu\n", ret,
3736 (unsigned long long)bytenr);
3737 btrfs_print_leaf(extent_root, path->nodes[0]);
3738 }
3739 BUG_ON(ret);
3740 extent_slot = path->slots[0];
3741 leaf = path->nodes[0];
3742 item_size = btrfs_item_size_nr(leaf, extent_slot);
3743 }
3744#endif
3745 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003746 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003747 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003748 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3749 struct btrfs_tree_block_info *bi;
3750 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3751 bi = (struct btrfs_tree_block_info *)(ei + 1);
3752 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003753 }
3754
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003755 refs = btrfs_extent_refs(leaf, ei);
3756 BUG_ON(refs < refs_to_drop);
3757 refs -= refs_to_drop;
3758
3759 if (refs > 0) {
3760 if (extent_op)
3761 __run_delayed_extent_op(extent_op, leaf, ei);
3762 /*
3763 * In the case of inline back ref, reference count will
3764 * be updated by remove_extent_backref
3765 */
3766 if (iref) {
3767 BUG_ON(!found_extent);
3768 } else {
3769 btrfs_set_extent_refs(leaf, ei, refs);
3770 btrfs_mark_buffer_dirty(leaf);
3771 }
3772 if (found_extent) {
3773 ret = remove_extent_backref(trans, extent_root, path,
3774 iref, refs_to_drop,
3775 is_data);
3776 BUG_ON(ret);
3777 }
3778 } else {
3779 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003780 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003781
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003782 if (found_extent) {
3783 BUG_ON(is_data && refs_to_drop !=
3784 extent_data_ref_count(root, path, iref));
3785 if (iref) {
3786 BUG_ON(path->slots[0] != extent_slot);
3787 } else {
3788 BUG_ON(path->slots[0] != extent_slot + 1);
3789 path->slots[0] = extent_slot;
3790 num_to_del = 2;
3791 }
Chris Mason78fae272007-03-25 11:35:08 -04003792 }
Chris Masonb9473432009-03-13 11:00:37 -04003793
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003794 ret = pin_down_bytes(trans, root, path, bytenr,
Yan Zheng11833d62009-09-11 16:11:19 -04003795 num_bytes, is_data, 0, &must_clean);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003796 if (ret > 0)
3797 mark_free = 1;
3798 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003799 /*
3800 * it is going to be very rare for someone to be waiting
3801 * on the block we're freeing. del_items might need to
3802 * schedule, so rather than get fancy, just force it
3803 * to blocking here
3804 */
3805 if (must_clean)
3806 btrfs_set_lock_blocking(must_clean);
3807
Chris Mason952fcca2008-02-18 16:33:44 -05003808 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3809 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003810 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003811 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003812
Chris Masonb9473432009-03-13 11:00:37 -04003813 if (must_clean) {
3814 clean_tree_block(NULL, root, must_clean);
3815 btrfs_tree_unlock(must_clean);
3816 free_extent_buffer(must_clean);
3817 }
3818
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003819 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003820 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3821 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003822 } else {
3823 invalidate_mapping_pages(info->btree_inode->i_mapping,
3824 bytenr >> PAGE_CACHE_SHIFT,
3825 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003826 }
3827
Chris Masondcbdd4d2008-12-16 13:51:01 -05003828 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3829 mark_free);
3830 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003831 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003832 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003833 return ret;
3834}
3835
3836/*
Chris Mason1887be62009-03-13 10:11:24 -04003837 * when we free an extent, it is possible (and likely) that we free the last
3838 * delayed ref for that extent as well. This searches the delayed ref tree for
3839 * a given extent, and if there are no other delayed refs to be processed, it
3840 * removes it from the tree.
3841 */
3842static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3843 struct btrfs_root *root, u64 bytenr)
3844{
3845 struct btrfs_delayed_ref_head *head;
3846 struct btrfs_delayed_ref_root *delayed_refs;
3847 struct btrfs_delayed_ref_node *ref;
3848 struct rb_node *node;
3849 int ret;
3850
3851 delayed_refs = &trans->transaction->delayed_refs;
3852 spin_lock(&delayed_refs->lock);
3853 head = btrfs_find_delayed_ref_head(trans, bytenr);
3854 if (!head)
3855 goto out;
3856
3857 node = rb_prev(&head->node.rb_node);
3858 if (!node)
3859 goto out;
3860
3861 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3862
3863 /* there are still entries for this ref, we can't drop it */
3864 if (ref->bytenr == bytenr)
3865 goto out;
3866
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003867 if (head->extent_op) {
3868 if (!head->must_insert_reserved)
3869 goto out;
3870 kfree(head->extent_op);
3871 head->extent_op = NULL;
3872 }
3873
Chris Mason1887be62009-03-13 10:11:24 -04003874 /*
3875 * waiting for the lock here would deadlock. If someone else has it
3876 * locked they are already in the process of dropping it anyway
3877 */
3878 if (!mutex_trylock(&head->mutex))
3879 goto out;
3880
3881 /*
3882 * at this point we have a head with no other entries. Go
3883 * ahead and process it.
3884 */
3885 head->node.in_tree = 0;
3886 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04003887
Chris Mason1887be62009-03-13 10:11:24 -04003888 delayed_refs->num_entries--;
3889
3890 /*
3891 * we don't take a ref on the node because we're removing it from the
3892 * tree, so we just steal the ref the tree was holding.
3893 */
Chris Masonc3e69d52009-03-13 10:17:05 -04003894 delayed_refs->num_heads--;
3895 if (list_empty(&head->cluster))
3896 delayed_refs->num_heads_ready--;
3897
3898 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04003899 spin_unlock(&delayed_refs->lock);
3900
3901 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003902 &head->node, head->extent_op,
3903 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04003904 BUG_ON(ret);
3905 btrfs_put_delayed_ref(&head->node);
3906 return 0;
3907out:
3908 spin_unlock(&delayed_refs->lock);
3909 return 0;
3910}
3911
Chris Mason925baed2008-06-25 16:01:30 -04003912int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003913 struct btrfs_root *root,
3914 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003915 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04003916{
3917 int ret;
3918
Chris Mason56bec292009-03-13 10:10:06 -04003919 /*
3920 * tree log blocks never actually go into the extent allocation
3921 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003922 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003923 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
3924 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04003925 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003926 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04003927 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003928 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
3929 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
3930 parent, root_objectid, (int)owner,
3931 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04003932 BUG_ON(ret);
3933 ret = check_ref_cleanup(trans, root, bytenr);
3934 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003935 } else {
3936 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
3937 parent, root_objectid, owner,
3938 offset, BTRFS_DROP_DELAYED_REF, NULL);
3939 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04003940 }
Chris Mason925baed2008-06-25 16:01:30 -04003941 return ret;
3942}
3943
Chris Mason87ee04e2007-11-30 11:30:34 -05003944static u64 stripe_align(struct btrfs_root *root, u64 val)
3945{
3946 u64 mask = ((u64)root->stripesize - 1);
3947 u64 ret = (val + mask) & ~mask;
3948 return ret;
3949}
3950
Chris Masonfec577f2007-02-26 10:40:21 -05003951/*
Josef Bacik817d52f2009-07-13 21:29:25 -04003952 * when we wait for progress in the block group caching, its because
3953 * our allocation attempt failed at least once. So, we must sleep
3954 * and let some progress happen before we try again.
3955 *
3956 * This function will sleep at least once waiting for new free space to
3957 * show up, and then it will check the block group free space numbers
3958 * for our min num_bytes. Another option is to have it go ahead
3959 * and look in the rbtree for a free extent of a given size, but this
3960 * is a good start.
3961 */
3962static noinline int
3963wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
3964 u64 num_bytes)
3965{
Yan Zheng11833d62009-09-11 16:11:19 -04003966 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04003967 DEFINE_WAIT(wait);
3968
Yan Zheng11833d62009-09-11 16:11:19 -04003969 caching_ctl = get_caching_control(cache);
3970 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04003971 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04003972
Yan Zheng11833d62009-09-11 16:11:19 -04003973 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04003974 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04003975
3976 put_caching_control(caching_ctl);
3977 return 0;
3978}
3979
3980static noinline int
3981wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
3982{
3983 struct btrfs_caching_control *caching_ctl;
3984 DEFINE_WAIT(wait);
3985
3986 caching_ctl = get_caching_control(cache);
3987 if (!caching_ctl)
3988 return 0;
3989
3990 wait_event(caching_ctl->wait, block_group_cache_done(cache));
3991
3992 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04003993 return 0;
3994}
3995
3996enum btrfs_loop_type {
3997 LOOP_CACHED_ONLY = 0,
3998 LOOP_CACHING_NOWAIT = 1,
3999 LOOP_CACHING_WAIT = 2,
4000 LOOP_ALLOC_CHUNK = 3,
4001 LOOP_NO_EMPTY_SIZE = 4,
4002};
4003
4004/*
Chris Masonfec577f2007-02-26 10:40:21 -05004005 * walks the btree of allocated extents and find a hole of a given size.
4006 * The key ins is changed to record the hole:
4007 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004008 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004009 * ins->offset == number of blocks
4010 * Any available blocks before search_start are skipped.
4011 */
Chris Masond3977122009-01-05 21:25:51 -05004012static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004013 struct btrfs_root *orig_root,
4014 u64 num_bytes, u64 empty_size,
4015 u64 search_start, u64 search_end,
4016 u64 hint_byte, struct btrfs_key *ins,
4017 u64 exclude_start, u64 exclude_nr,
4018 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004019{
Josef Bacik80eb2342008-10-29 14:49:05 -04004020 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004021 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004022 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004023 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004024 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004025 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004026 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004027 int last_ptr_loop = 0;
4028 int loop = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004029 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004030 bool failed_cluster_refill = false;
Chris Masonfec577f2007-02-26 10:40:21 -05004031
Chris Masondb945352007-10-15 16:15:53 -04004032 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004033 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004034 ins->objectid = 0;
4035 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004036
Josef Bacik2552d172009-04-03 10:14:19 -04004037 space_info = __find_space_info(root->fs_info, data);
4038
Chris Mason0ef3e662008-05-24 14:04:53 -04004039 if (orig_root->ref_cows || empty_size)
4040 allowed_chunk_alloc = 1;
4041
Chris Mason239b14b2008-03-24 15:02:07 -04004042 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004043 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004044 if (!btrfs_test_opt(root, SSD))
4045 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004046 }
4047
Chris Masonfa9c0d792009-04-03 09:47:43 -04004048 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4049 last_ptr = &root->fs_info->data_alloc_cluster;
4050 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004051
Chris Mason239b14b2008-03-24 15:02:07 -04004052 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004053 spin_lock(&last_ptr->lock);
4054 if (last_ptr->block_group)
4055 hint_byte = last_ptr->window_start;
4056 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004057 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004058
Chris Masona061fc82008-05-07 11:43:44 -04004059 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004060 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004061
Josef Bacik817d52f2009-07-13 21:29:25 -04004062 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004063 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004064
Josef Bacik2552d172009-04-03 10:14:19 -04004065 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04004066 block_group = btrfs_lookup_block_group(root->fs_info,
4067 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004068 /*
4069 * we don't want to use the block group if it doesn't match our
4070 * allocation bits, or if its not cached.
4071 */
4072 if (block_group && block_group_bits(block_group, data) &&
4073 block_group_cache_done(block_group)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004074 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004075 if (list_empty(&block_group->list) ||
4076 block_group->ro) {
4077 /*
4078 * someone is removing this block group,
4079 * we can't jump into the have_block_group
4080 * target because our list pointers are not
4081 * valid
4082 */
4083 btrfs_put_block_group(block_group);
4084 up_read(&space_info->groups_sem);
4085 } else
4086 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04004087 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004088 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004089 }
Chris Mason42e70e72008-11-07 18:17:11 -05004090 }
Chris Mason43662112008-11-07 09:06:11 -05004091
Josef Bacik2552d172009-04-03 10:14:19 -04004092search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004093 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04004094 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004095 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004096 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004097
Josef Bacik2552d172009-04-03 10:14:19 -04004098 atomic_inc(&block_group->count);
4099 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004100
Josef Bacik2552d172009-04-03 10:14:19 -04004101have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004102 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
4103 /*
4104 * we want to start caching kthreads, but not too many
4105 * right off the bat so we don't overwhelm the system,
4106 * so only start them if there are less than 2 and we're
4107 * in the initial allocation phase.
4108 */
4109 if (loop > LOOP_CACHING_NOWAIT ||
4110 atomic_read(&space_info->caching_threads) < 2) {
4111 ret = cache_block_group(block_group);
4112 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004113 }
Josef Bacikea6a4782008-11-20 12:16:16 -05004114 }
4115
Josef Bacik817d52f2009-07-13 21:29:25 -04004116 cached = block_group_cache_done(block_group);
4117 if (unlikely(!cached)) {
4118 found_uncached_bg = true;
4119
4120 /* if we only want cached bgs, loop */
4121 if (loop == LOOP_CACHED_ONLY)
4122 goto loop;
4123 }
4124
Josef Bacikea6a4782008-11-20 12:16:16 -05004125 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004126 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004127
Josef Bacik0a243252009-09-11 16:11:20 -04004128 /*
4129 * Ok we want to try and use the cluster allocator, so lets look
4130 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4131 * have tried the cluster allocator plenty of times at this
4132 * point and not have found anything, so we are likely way too
4133 * fragmented for the clustering stuff to find anything, so lets
4134 * just skip it and let the allocator find whatever block it can
4135 * find
4136 */
4137 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004138 /*
4139 * the refill lock keeps out other
4140 * people trying to start a new cluster
4141 */
4142 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004143 if (last_ptr->block_group &&
4144 (last_ptr->block_group->ro ||
4145 !block_group_bits(last_ptr->block_group, data))) {
4146 offset = 0;
4147 goto refill_cluster;
4148 }
4149
Chris Masonfa9c0d792009-04-03 09:47:43 -04004150 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4151 num_bytes, search_start);
4152 if (offset) {
4153 /* we have a block, we're done */
4154 spin_unlock(&last_ptr->refill_lock);
4155 goto checks;
4156 }
4157
4158 spin_lock(&last_ptr->lock);
4159 /*
4160 * whoops, this cluster doesn't actually point to
4161 * this block group. Get a ref on the block
4162 * group is does point to and try again
4163 */
4164 if (!last_ptr_loop && last_ptr->block_group &&
4165 last_ptr->block_group != block_group) {
4166
4167 btrfs_put_block_group(block_group);
4168 block_group = last_ptr->block_group;
4169 atomic_inc(&block_group->count);
4170 spin_unlock(&last_ptr->lock);
4171 spin_unlock(&last_ptr->refill_lock);
4172
4173 last_ptr_loop = 1;
4174 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04004175 /*
4176 * we know this block group is properly
4177 * in the list because
4178 * btrfs_remove_block_group, drops the
4179 * cluster before it removes the block
4180 * group from the list
4181 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04004182 goto have_block_group;
4183 }
4184 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004185refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04004186 /*
4187 * this cluster didn't work out, free it and
4188 * start over
4189 */
4190 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4191
4192 last_ptr_loop = 0;
4193
4194 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04004195 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04004196 block_group, last_ptr,
4197 offset, num_bytes,
4198 empty_cluster + empty_size);
4199 if (ret == 0) {
4200 /*
4201 * now pull our allocation out of this
4202 * cluster
4203 */
4204 offset = btrfs_alloc_from_cluster(block_group,
4205 last_ptr, num_bytes,
4206 search_start);
4207 if (offset) {
4208 /* we found one, proceed */
4209 spin_unlock(&last_ptr->refill_lock);
4210 goto checks;
4211 }
Josef Bacik0a243252009-09-11 16:11:20 -04004212 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4213 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004214 spin_unlock(&last_ptr->refill_lock);
4215
Josef Bacik0a243252009-09-11 16:11:20 -04004216 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004217 wait_block_group_cache_progress(block_group,
4218 num_bytes + empty_cluster + empty_size);
4219 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004220 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004221
Chris Masonfa9c0d792009-04-03 09:47:43 -04004222 /*
4223 * at this point we either didn't find a cluster
4224 * or we weren't able to allocate a block from our
4225 * cluster. Free the cluster we've been trying
4226 * to use, and go to the next block group
4227 */
Josef Bacik0a243252009-09-11 16:11:20 -04004228 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004229 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04004230 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004231 }
4232
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004233 offset = btrfs_find_space_for_alloc(block_group, search_start,
4234 num_bytes, empty_size);
Josef Bacik817d52f2009-07-13 21:29:25 -04004235 if (!offset && (cached || (!cached &&
4236 loop == LOOP_CACHING_NOWAIT))) {
Josef Bacik2552d172009-04-03 10:14:19 -04004237 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04004238 } else if (!offset && (!cached &&
4239 loop > LOOP_CACHING_NOWAIT)) {
4240 wait_block_group_cache_progress(block_group,
4241 num_bytes + empty_size);
4242 goto have_block_group;
4243 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004244checks:
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004245 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04004246 /* move on to the next group */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004247 if (search_start + num_bytes >= search_end) {
4248 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004249 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004250 }
Chris Masone37c9e62007-05-09 20:13:14 -04004251
Josef Bacik2552d172009-04-03 10:14:19 -04004252 /* move on to the next group */
4253 if (search_start + num_bytes >
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004254 block_group->key.objectid + block_group->key.offset) {
4255 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004256 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004257 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004258
Josef Bacik2552d172009-04-03 10:14:19 -04004259 if (exclude_nr > 0 &&
4260 (search_start + num_bytes > exclude_start &&
4261 search_start < exclude_start + exclude_nr)) {
4262 search_start = exclude_start + exclude_nr;
4263
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004264 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004265 /*
4266 * if search_start is still in this block group
4267 * then we just re-search this block group
4268 */
4269 if (search_start >= block_group->key.objectid &&
4270 search_start < (block_group->key.objectid +
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004271 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04004272 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04004273 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004274 }
Josef Bacik2552d172009-04-03 10:14:19 -04004275
4276 ins->objectid = search_start;
4277 ins->offset = num_bytes;
4278
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004279 if (offset < search_start)
4280 btrfs_add_free_space(block_group, offset,
4281 search_start - offset);
4282 BUG_ON(offset > search_start);
4283
Yan Zheng11833d62009-09-11 16:11:19 -04004284 update_reserved_extents(block_group, num_bytes, 1);
4285
Josef Bacik2552d172009-04-03 10:14:19 -04004286 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04004287 break;
4288loop:
Josef Bacik0a243252009-09-11 16:11:20 -04004289 failed_cluster_refill = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004290 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004291 }
4292 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004293
Josef Bacik817d52f2009-07-13 21:29:25 -04004294 /* LOOP_CACHED_ONLY, only search fully cached block groups
4295 * LOOP_CACHING_NOWAIT, search partially cached block groups, but
4296 * dont wait foR them to finish caching
4297 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4298 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4299 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4300 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04004301 */
Josef Bacik817d52f2009-07-13 21:29:25 -04004302 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4303 (found_uncached_bg || empty_size || empty_cluster ||
4304 allowed_chunk_alloc)) {
4305 if (found_uncached_bg) {
4306 found_uncached_bg = false;
4307 if (loop < LOOP_CACHING_WAIT) {
4308 loop++;
4309 goto search;
4310 }
4311 }
4312
4313 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004314 empty_size = 0;
4315 empty_cluster = 0;
4316 }
Chris Mason42e70e72008-11-07 18:17:11 -05004317
Josef Bacik2552d172009-04-03 10:14:19 -04004318 if (allowed_chunk_alloc) {
4319 ret = do_chunk_alloc(trans, root, num_bytes +
4320 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04004321 allowed_chunk_alloc = 0;
4322 } else {
4323 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004324 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004325
Josef Bacik817d52f2009-07-13 21:29:25 -04004326 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004327 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04004328 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004329 }
Josef Bacik2552d172009-04-03 10:14:19 -04004330 ret = -ENOSPC;
4331 } else if (!ins->objectid) {
4332 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04004333 }
Chris Mason0b86a832008-03-24 15:01:56 -04004334
Josef Bacik80eb2342008-10-29 14:49:05 -04004335 /* we found what we needed */
4336 if (ins->objectid) {
4337 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05004338 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04004339
Chris Masonfa9c0d792009-04-03 09:47:43 -04004340 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004341 ret = 0;
4342 }
Chris Mason0b86a832008-03-24 15:01:56 -04004343
Chris Mason0f70abe2007-02-28 16:46:22 -05004344 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004345}
Chris Masonec44a352008-04-28 15:29:52 -04004346
Josef Bacik9ed74f22009-09-11 16:12:44 -04004347static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4348 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04004349{
4350 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004351
Josef Bacik9ed74f22009-09-11 16:12:44 -04004352 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05004353 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4354 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04004355 info->bytes_pinned - info->bytes_reserved -
4356 info->bytes_super),
Chris Masond3977122009-01-05 21:25:51 -05004357 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05004358 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Josef Bacik9ed74f22009-09-11 16:12:44 -04004359 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4360 "\n",
Joel Becker21380932009-04-21 12:38:29 -07004361 (unsigned long long)info->total_bytes,
4362 (unsigned long long)info->bytes_pinned,
4363 (unsigned long long)info->bytes_delalloc,
4364 (unsigned long long)info->bytes_may_use,
Josef Bacik9ed74f22009-09-11 16:12:44 -04004365 (unsigned long long)info->bytes_used,
4366 (unsigned long long)info->bytes_root,
4367 (unsigned long long)info->bytes_super,
4368 (unsigned long long)info->bytes_reserved);
4369 spin_unlock(&info->lock);
4370
4371 if (!dump_block_groups)
4372 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004373
Josef Bacik80eb2342008-10-29 14:49:05 -04004374 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05004375 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004376 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05004377 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4378 "%llu pinned %llu reserved\n",
4379 (unsigned long long)cache->key.objectid,
4380 (unsigned long long)cache->key.offset,
4381 (unsigned long long)btrfs_block_group_used(&cache->item),
4382 (unsigned long long)cache->pinned,
4383 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004384 btrfs_dump_free_space(cache, bytes);
4385 spin_unlock(&cache->lock);
4386 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004387 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004388}
Zheng Yane8569812008-09-26 10:05:48 -04004389
Yan Zheng11833d62009-09-11 16:11:19 -04004390int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4391 struct btrfs_root *root,
4392 u64 num_bytes, u64 min_alloc_size,
4393 u64 empty_size, u64 hint_byte,
4394 u64 search_end, struct btrfs_key *ins,
4395 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004396{
4397 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04004398 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04004399 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04004400
Josef Bacik6a632092009-02-20 11:00:09 -05004401 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04004402again:
Chris Mason0ef3e662008-05-24 14:04:53 -04004403 /*
4404 * the only place that sets empty_size is btrfs_realloc_node, which
4405 * is not called recursively on allocations
4406 */
4407 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04004408 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04004409 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004410 2 * 1024 * 1024,
4411 BTRFS_BLOCK_GROUP_METADATA |
4412 (info->metadata_alloc_profile &
4413 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04004414 }
4415 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004416 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04004417 }
Chris Mason0b86a832008-03-24 15:01:56 -04004418
Chris Masondb945352007-10-15 16:15:53 -04004419 WARN_ON(num_bytes < root->sectorsize);
4420 ret = find_free_extent(trans, root, num_bytes, empty_size,
4421 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04004422 trans->alloc_exclude_start,
4423 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04004424
Chris Mason98d20f62008-04-14 09:46:10 -04004425 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4426 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004427 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004428 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04004429 do_chunk_alloc(trans, root->fs_info->extent_root,
4430 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004431 goto again;
4432 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004433 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004434 struct btrfs_space_info *sinfo;
4435
4436 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004437 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4438 "wanted %llu\n", (unsigned long long)data,
4439 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004440 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04004441 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004442
4443 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004444}
4445
Chris Mason65b51a02008-08-01 15:11:20 -04004446int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4447{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004448 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004449 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004450
Josef Bacik0f9dd462008-09-23 13:14:11 -04004451 cache = btrfs_lookup_block_group(root->fs_info, start);
4452 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004453 printk(KERN_ERR "Unable to find block group for %llu\n",
4454 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004455 return -ENOSPC;
4456 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004457
4458 ret = btrfs_discard_extent(root, start, len);
4459
Josef Bacik0f9dd462008-09-23 13:14:11 -04004460 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04004461 update_reserved_extents(cache, len, 0);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004462 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04004463
Chris Masone6dcd2d2008-07-17 12:53:50 -04004464 return ret;
4465}
4466
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004467static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4468 struct btrfs_root *root,
4469 u64 parent, u64 root_objectid,
4470 u64 flags, u64 owner, u64 offset,
4471 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004472{
4473 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004474 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004475 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004476 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004477 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004478 struct extent_buffer *leaf;
4479 int type;
4480 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004481
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004482 if (parent > 0)
4483 type = BTRFS_SHARED_DATA_REF_KEY;
4484 else
4485 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004486
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004487 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004488
4489 path = btrfs_alloc_path();
4490 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004491
Chris Masonb9473432009-03-13 11:00:37 -04004492 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004493 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4494 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004495 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004496
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004497 leaf = path->nodes[0];
4498 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004499 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004500 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4501 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4502 btrfs_set_extent_flags(leaf, extent_item,
4503 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004504
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004505 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4506 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4507 if (parent > 0) {
4508 struct btrfs_shared_data_ref *ref;
4509 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4510 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4511 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4512 } else {
4513 struct btrfs_extent_data_ref *ref;
4514 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4515 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4516 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4517 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4518 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4519 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004520
4521 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004522 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004523
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004524 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4525 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05004526 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004527 printk(KERN_ERR "btrfs update block group failed for %llu "
4528 "%llu\n", (unsigned long long)ins->objectid,
4529 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004530 BUG();
4531 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004532 return ret;
4533}
4534
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004535static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4536 struct btrfs_root *root,
4537 u64 parent, u64 root_objectid,
4538 u64 flags, struct btrfs_disk_key *key,
4539 int level, struct btrfs_key *ins)
4540{
4541 int ret;
4542 struct btrfs_fs_info *fs_info = root->fs_info;
4543 struct btrfs_extent_item *extent_item;
4544 struct btrfs_tree_block_info *block_info;
4545 struct btrfs_extent_inline_ref *iref;
4546 struct btrfs_path *path;
4547 struct extent_buffer *leaf;
4548 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4549
4550 path = btrfs_alloc_path();
4551 BUG_ON(!path);
4552
4553 path->leave_spinning = 1;
4554 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4555 ins, size);
4556 BUG_ON(ret);
4557
4558 leaf = path->nodes[0];
4559 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4560 struct btrfs_extent_item);
4561 btrfs_set_extent_refs(leaf, extent_item, 1);
4562 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4563 btrfs_set_extent_flags(leaf, extent_item,
4564 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4565 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4566
4567 btrfs_set_tree_block_key(leaf, block_info, key);
4568 btrfs_set_tree_block_level(leaf, block_info, level);
4569
4570 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4571 if (parent > 0) {
4572 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4573 btrfs_set_extent_inline_ref_type(leaf, iref,
4574 BTRFS_SHARED_BLOCK_REF_KEY);
4575 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4576 } else {
4577 btrfs_set_extent_inline_ref_type(leaf, iref,
4578 BTRFS_TREE_BLOCK_REF_KEY);
4579 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4580 }
4581
4582 btrfs_mark_buffer_dirty(leaf);
4583 btrfs_free_path(path);
4584
4585 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4586 1, 0);
4587 if (ret) {
4588 printk(KERN_ERR "btrfs update block group failed for %llu "
4589 "%llu\n", (unsigned long long)ins->objectid,
4590 (unsigned long long)ins->offset);
4591 BUG();
4592 }
4593 return ret;
4594}
4595
4596int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4597 struct btrfs_root *root,
4598 u64 root_objectid, u64 owner,
4599 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004600{
4601 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04004602
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004603 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004604
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004605 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4606 0, root_objectid, owner, offset,
4607 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004608 return ret;
4609}
Chris Masone02119d2008-09-05 16:13:11 -04004610
4611/*
4612 * this is used by the tree logging recovery code. It records that
4613 * an extent has been allocated and makes sure to clear the free
4614 * space cache bits as well
4615 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004616int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4617 struct btrfs_root *root,
4618 u64 root_objectid, u64 owner, u64 offset,
4619 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004620{
4621 int ret;
4622 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04004623 struct btrfs_caching_control *caching_ctl;
4624 u64 start = ins->objectid;
4625 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04004626
Chris Masone02119d2008-09-05 16:13:11 -04004627 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04004628 cache_block_group(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004629 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004630
Yan Zheng11833d62009-09-11 16:11:19 -04004631 if (!caching_ctl) {
4632 BUG_ON(!block_group_cache_done(block_group));
4633 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4634 BUG_ON(ret);
4635 } else {
4636 mutex_lock(&caching_ctl->mutex);
4637
4638 if (start >= caching_ctl->progress) {
4639 ret = add_excluded_extent(root, start, num_bytes);
4640 BUG_ON(ret);
4641 } else if (start + num_bytes <= caching_ctl->progress) {
4642 ret = btrfs_remove_free_space(block_group,
4643 start, num_bytes);
4644 BUG_ON(ret);
4645 } else {
4646 num_bytes = caching_ctl->progress - start;
4647 ret = btrfs_remove_free_space(block_group,
4648 start, num_bytes);
4649 BUG_ON(ret);
4650
4651 start = caching_ctl->progress;
4652 num_bytes = ins->objectid + ins->offset -
4653 caching_ctl->progress;
4654 ret = add_excluded_extent(root, start, num_bytes);
4655 BUG_ON(ret);
4656 }
4657
4658 mutex_unlock(&caching_ctl->mutex);
4659 put_caching_control(caching_ctl);
4660 }
4661
4662 update_reserved_extents(block_group, ins->offset, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004663 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004664 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4665 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004666 return ret;
4667}
4668
Chris Masone6dcd2d2008-07-17 12:53:50 -04004669/*
4670 * finds a free extent and does all the dirty work required for allocation
4671 * returns the key for the extent through ins, and a tree buffer for
4672 * the first block of the extent through buf.
4673 *
4674 * returns 0 if everything worked, non-zero otherwise.
4675 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004676static int alloc_tree_block(struct btrfs_trans_handle *trans,
4677 struct btrfs_root *root,
4678 u64 num_bytes, u64 parent, u64 root_objectid,
4679 struct btrfs_disk_key *key, int level,
4680 u64 empty_size, u64 hint_byte, u64 search_end,
4681 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004682{
4683 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004684 u64 flags = 0;
4685
Yan Zheng11833d62009-09-11 16:11:19 -04004686 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4687 empty_size, hint_byte, search_end,
4688 ins, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004689 if (ret)
4690 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004691
4692 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4693 if (parent == 0)
4694 parent = ins->objectid;
4695 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4696 } else
4697 BUG_ON(parent > 0);
4698
Chris Masond00aff02008-09-11 15:54:42 -04004699 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004700 struct btrfs_delayed_extent_op *extent_op;
4701 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4702 BUG_ON(!extent_op);
4703 if (key)
4704 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4705 else
4706 memset(&extent_op->key, 0, sizeof(extent_op->key));
4707 extent_op->flags_to_set = flags;
4708 extent_op->update_key = 1;
4709 extent_op->update_flags = 1;
4710 extent_op->is_data = 0;
4711
4712 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4713 ins->offset, parent, root_objectid,
4714 level, BTRFS_ADD_DELAYED_EXTENT,
4715 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004716 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004717 }
Chris Mason925baed2008-06-25 16:01:30 -04004718 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004719}
Chris Mason65b51a02008-08-01 15:11:20 -04004720
4721struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4722 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004723 u64 bytenr, u32 blocksize,
4724 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004725{
4726 struct extent_buffer *buf;
4727
4728 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4729 if (!buf)
4730 return ERR_PTR(-ENOMEM);
4731 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004732 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004733 btrfs_tree_lock(buf);
4734 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004735
4736 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004737 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004738
Chris Masond0c803c2008-09-11 16:17:57 -04004739 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4740 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04004741 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004742 } else {
4743 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4744 buf->start + buf->len - 1, GFP_NOFS);
4745 }
Chris Mason65b51a02008-08-01 15:11:20 -04004746 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004747 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004748 return buf;
4749}
4750
Chris Masonfec577f2007-02-26 10:40:21 -05004751/*
4752 * helper function to allocate a block for a given tree
4753 * returns the tree buffer or NULL.
4754 */
Chris Mason5f39d392007-10-15 16:14:19 -04004755struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004756 struct btrfs_root *root, u32 blocksize,
4757 u64 parent, u64 root_objectid,
4758 struct btrfs_disk_key *key, int level,
4759 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004760{
Chris Masone2fa7222007-03-12 16:22:34 -04004761 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004762 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004763 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004764
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004765 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4766 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004767 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004768 BUG_ON(ret > 0);
4769 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004770 }
Chris Mason55c69072008-01-09 15:55:33 -05004771
Chris Mason4008c042009-02-12 14:09:45 -05004772 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4773 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004774 return buf;
4775}
Chris Masona28ec192007-03-06 20:08:01 -05004776
Yan Zheng2c47e6052009-06-27 21:07:35 -04004777struct walk_control {
4778 u64 refs[BTRFS_MAX_LEVEL];
4779 u64 flags[BTRFS_MAX_LEVEL];
4780 struct btrfs_key update_progress;
4781 int stage;
4782 int level;
4783 int shared_level;
4784 int update_ref;
4785 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004786 int reada_slot;
4787 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004788};
4789
4790#define DROP_REFERENCE 1
4791#define UPDATE_BACKREF 2
4792
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004793static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4794 struct btrfs_root *root,
4795 struct walk_control *wc,
4796 struct btrfs_path *path)
4797{
4798 u64 bytenr;
4799 u64 generation;
4800 u64 refs;
4801 u64 last = 0;
4802 u32 nritems;
4803 u32 blocksize;
4804 struct btrfs_key key;
4805 struct extent_buffer *eb;
4806 int ret;
4807 int slot;
4808 int nread = 0;
4809
4810 if (path->slots[wc->level] < wc->reada_slot) {
4811 wc->reada_count = wc->reada_count * 2 / 3;
4812 wc->reada_count = max(wc->reada_count, 2);
4813 } else {
4814 wc->reada_count = wc->reada_count * 3 / 2;
4815 wc->reada_count = min_t(int, wc->reada_count,
4816 BTRFS_NODEPTRS_PER_BLOCK(root));
4817 }
4818
4819 eb = path->nodes[wc->level];
4820 nritems = btrfs_header_nritems(eb);
4821 blocksize = btrfs_level_size(root, wc->level - 1);
4822
4823 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4824 if (nread >= wc->reada_count)
4825 break;
4826
4827 cond_resched();
4828 bytenr = btrfs_node_blockptr(eb, slot);
4829 generation = btrfs_node_ptr_generation(eb, slot);
4830
4831 if (slot == path->slots[wc->level])
4832 goto reada;
4833
4834 if (wc->stage == UPDATE_BACKREF &&
4835 generation <= root->root_key.offset)
4836 continue;
4837
4838 if (wc->stage == DROP_REFERENCE) {
4839 ret = btrfs_lookup_extent_info(trans, root,
4840 bytenr, blocksize,
4841 &refs, NULL);
4842 BUG_ON(ret);
4843 BUG_ON(refs == 0);
4844 if (refs == 1)
4845 goto reada;
4846
4847 if (!wc->update_ref ||
4848 generation <= root->root_key.offset)
4849 continue;
4850 btrfs_node_key_to_cpu(eb, &key, slot);
4851 ret = btrfs_comp_cpu_keys(&key,
4852 &wc->update_progress);
4853 if (ret < 0)
4854 continue;
4855 }
4856reada:
4857 ret = readahead_tree_block(root, bytenr, blocksize,
4858 generation);
4859 if (ret)
4860 break;
4861 last = bytenr + blocksize;
4862 nread++;
4863 }
4864 wc->reada_slot = slot;
4865}
4866
Chris Mason9aca1d52007-03-13 11:09:37 -04004867/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04004868 * hepler to process tree block while walking down the tree.
4869 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04004870 * when wc->stage == UPDATE_BACKREF, this function updates
4871 * back refs for pointers in the block.
4872 *
4873 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004874 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004875static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4876 struct btrfs_root *root,
4877 struct btrfs_path *path,
4878 struct walk_control *wc)
4879{
4880 int level = wc->level;
4881 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04004882 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4883 int ret;
4884
4885 if (wc->stage == UPDATE_BACKREF &&
4886 btrfs_header_owner(eb) != root->root_key.objectid)
4887 return 1;
4888
4889 /*
4890 * when reference count of tree block is 1, it won't increase
4891 * again. once full backref flag is set, we never clear it.
4892 */
4893 if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4894 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) {
4895 BUG_ON(!path->locks[level]);
4896 ret = btrfs_lookup_extent_info(trans, root,
4897 eb->start, eb->len,
4898 &wc->refs[level],
4899 &wc->flags[level]);
4900 BUG_ON(ret);
4901 BUG_ON(wc->refs[level] == 0);
4902 }
4903
Yan Zheng2c47e6052009-06-27 21:07:35 -04004904 if (wc->stage == DROP_REFERENCE) {
4905 if (wc->refs[level] > 1)
4906 return 1;
4907
4908 if (path->locks[level] && !wc->keep_locks) {
4909 btrfs_tree_unlock(eb);
4910 path->locks[level] = 0;
4911 }
4912 return 0;
4913 }
4914
4915 /* wc->stage == UPDATE_BACKREF */
4916 if (!(wc->flags[level] & flag)) {
4917 BUG_ON(!path->locks[level]);
4918 ret = btrfs_inc_ref(trans, root, eb, 1);
4919 BUG_ON(ret);
4920 ret = btrfs_dec_ref(trans, root, eb, 0);
4921 BUG_ON(ret);
4922 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
4923 eb->len, flag, 0);
4924 BUG_ON(ret);
4925 wc->flags[level] |= flag;
4926 }
4927
4928 /*
4929 * the block is shared by multiple trees, so it's not good to
4930 * keep the tree lock
4931 */
4932 if (path->locks[level] && level > 0) {
4933 btrfs_tree_unlock(eb);
4934 path->locks[level] = 0;
4935 }
4936 return 0;
4937}
4938
4939/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004940 * hepler to process tree block pointer.
4941 *
4942 * when wc->stage == DROP_REFERENCE, this function checks
4943 * reference count of the block pointed to. if the block
4944 * is shared and we need update back refs for the subtree
4945 * rooted at the block, this function changes wc->stage to
4946 * UPDATE_BACKREF. if the block is shared and there is no
4947 * need to update back, this function drops the reference
4948 * to the block.
4949 *
4950 * NOTE: return value 1 means we should stop walking down.
4951 */
4952static noinline int do_walk_down(struct btrfs_trans_handle *trans,
4953 struct btrfs_root *root,
4954 struct btrfs_path *path,
4955 struct walk_control *wc)
4956{
4957 u64 bytenr;
4958 u64 generation;
4959 u64 parent;
4960 u32 blocksize;
4961 struct btrfs_key key;
4962 struct extent_buffer *next;
4963 int level = wc->level;
4964 int reada = 0;
4965 int ret = 0;
4966
4967 generation = btrfs_node_ptr_generation(path->nodes[level],
4968 path->slots[level]);
4969 /*
4970 * if the lower level block was created before the snapshot
4971 * was created, we know there is no need to update back refs
4972 * for the subtree
4973 */
4974 if (wc->stage == UPDATE_BACKREF &&
4975 generation <= root->root_key.offset)
4976 return 1;
4977
4978 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
4979 blocksize = btrfs_level_size(root, level - 1);
4980
4981 next = btrfs_find_tree_block(root, bytenr, blocksize);
4982 if (!next) {
4983 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
4984 reada = 1;
4985 }
4986 btrfs_tree_lock(next);
4987 btrfs_set_lock_blocking(next);
4988
4989 if (wc->stage == DROP_REFERENCE) {
4990 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
4991 &wc->refs[level - 1],
4992 &wc->flags[level - 1]);
4993 BUG_ON(ret);
4994 BUG_ON(wc->refs[level - 1] == 0);
4995
4996 if (wc->refs[level - 1] > 1) {
4997 if (!wc->update_ref ||
4998 generation <= root->root_key.offset)
4999 goto skip;
5000
5001 btrfs_node_key_to_cpu(path->nodes[level], &key,
5002 path->slots[level]);
5003 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5004 if (ret < 0)
5005 goto skip;
5006
5007 wc->stage = UPDATE_BACKREF;
5008 wc->shared_level = level - 1;
5009 }
5010 }
5011
5012 if (!btrfs_buffer_uptodate(next, generation)) {
5013 btrfs_tree_unlock(next);
5014 free_extent_buffer(next);
5015 next = NULL;
5016 }
5017
5018 if (!next) {
5019 if (reada && level == 1)
5020 reada_walk_down(trans, root, wc, path);
5021 next = read_tree_block(root, bytenr, blocksize, generation);
5022 btrfs_tree_lock(next);
5023 btrfs_set_lock_blocking(next);
5024 }
5025
5026 level--;
5027 BUG_ON(level != btrfs_header_level(next));
5028 path->nodes[level] = next;
5029 path->slots[level] = 0;
5030 path->locks[level] = 1;
5031 wc->level = level;
5032 if (wc->level == 1)
5033 wc->reada_slot = 0;
5034 return 0;
5035skip:
5036 wc->refs[level - 1] = 0;
5037 wc->flags[level - 1] = 0;
5038
5039 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5040 parent = path->nodes[level]->start;
5041 } else {
5042 BUG_ON(root->root_key.objectid !=
5043 btrfs_header_owner(path->nodes[level]));
5044 parent = 0;
5045 }
5046
5047 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5048 root->root_key.objectid, level - 1, 0);
5049 BUG_ON(ret);
5050
5051 btrfs_tree_unlock(next);
5052 free_extent_buffer(next);
5053 return 1;
5054}
5055
5056/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005057 * hepler to process tree block while walking up the tree.
5058 *
5059 * when wc->stage == DROP_REFERENCE, this function drops
5060 * reference count on the block.
5061 *
5062 * when wc->stage == UPDATE_BACKREF, this function changes
5063 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5064 * to UPDATE_BACKREF previously while processing the block.
5065 *
5066 * NOTE: return value 1 means we should stop walking up.
5067 */
5068static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5069 struct btrfs_root *root,
5070 struct btrfs_path *path,
5071 struct walk_control *wc)
5072{
5073 int ret = 0;
5074 int level = wc->level;
5075 struct extent_buffer *eb = path->nodes[level];
5076 u64 parent = 0;
5077
5078 if (wc->stage == UPDATE_BACKREF) {
5079 BUG_ON(wc->shared_level < level);
5080 if (level < wc->shared_level)
5081 goto out;
5082
Yan Zheng2c47e6052009-06-27 21:07:35 -04005083 ret = find_next_key(path, level + 1, &wc->update_progress);
5084 if (ret > 0)
5085 wc->update_ref = 0;
5086
5087 wc->stage = DROP_REFERENCE;
5088 wc->shared_level = -1;
5089 path->slots[level] = 0;
5090
5091 /*
5092 * check reference count again if the block isn't locked.
5093 * we should start walking down the tree again if reference
5094 * count is one.
5095 */
5096 if (!path->locks[level]) {
5097 BUG_ON(level == 0);
5098 btrfs_tree_lock(eb);
5099 btrfs_set_lock_blocking(eb);
5100 path->locks[level] = 1;
5101
5102 ret = btrfs_lookup_extent_info(trans, root,
5103 eb->start, eb->len,
5104 &wc->refs[level],
5105 &wc->flags[level]);
5106 BUG_ON(ret);
5107 BUG_ON(wc->refs[level] == 0);
5108 if (wc->refs[level] == 1) {
5109 btrfs_tree_unlock(eb);
5110 path->locks[level] = 0;
5111 return 1;
5112 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005113 }
5114 }
5115
5116 /* wc->stage == DROP_REFERENCE */
5117 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5118
5119 if (wc->refs[level] == 1) {
5120 if (level == 0) {
5121 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5122 ret = btrfs_dec_ref(trans, root, eb, 1);
5123 else
5124 ret = btrfs_dec_ref(trans, root, eb, 0);
5125 BUG_ON(ret);
5126 }
5127 /* make block locked assertion in clean_tree_block happy */
5128 if (!path->locks[level] &&
5129 btrfs_header_generation(eb) == trans->transid) {
5130 btrfs_tree_lock(eb);
5131 btrfs_set_lock_blocking(eb);
5132 path->locks[level] = 1;
5133 }
5134 clean_tree_block(trans, root, eb);
5135 }
5136
5137 if (eb == root->node) {
5138 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5139 parent = eb->start;
5140 else
5141 BUG_ON(root->root_key.objectid !=
5142 btrfs_header_owner(eb));
5143 } else {
5144 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5145 parent = path->nodes[level + 1]->start;
5146 else
5147 BUG_ON(root->root_key.objectid !=
5148 btrfs_header_owner(path->nodes[level + 1]));
5149 }
5150
5151 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5152 root->root_key.objectid, level, 0);
5153 BUG_ON(ret);
5154out:
5155 wc->refs[level] = 0;
5156 wc->flags[level] = 0;
5157 return ret;
5158}
5159
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005160static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5161 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005162 struct btrfs_path *path,
5163 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005164{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005165 int level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005166 int ret;
5167
Yan Zheng2c47e6052009-06-27 21:07:35 -04005168 while (level >= 0) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005169 if (path->slots[level] >=
5170 btrfs_header_nritems(path->nodes[level]))
5171 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005172
Yan Zheng2c47e6052009-06-27 21:07:35 -04005173 ret = walk_down_proc(trans, root, path, wc);
5174 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005175 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005176
Yan Zheng2c47e6052009-06-27 21:07:35 -04005177 if (level == 0)
5178 break;
5179
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005180 ret = do_walk_down(trans, root, path, wc);
5181 if (ret > 0) {
5182 path->slots[level]++;
5183 continue;
5184 }
5185 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005186 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005187 return 0;
5188}
5189
Chris Masond3977122009-01-05 21:25:51 -05005190static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005191 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005192 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005193 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005194{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005195 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005196 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005197
Yan Zheng2c47e6052009-06-27 21:07:35 -04005198 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5199 while (level < max_level && path->nodes[level]) {
5200 wc->level = level;
5201 if (path->slots[level] + 1 <
5202 btrfs_header_nritems(path->nodes[level])) {
5203 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005204 return 0;
5205 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005206 ret = walk_up_proc(trans, root, path, wc);
5207 if (ret > 0)
5208 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05005209
Yan Zheng2c47e6052009-06-27 21:07:35 -04005210 if (path->locks[level]) {
5211 btrfs_tree_unlock(path->nodes[level]);
5212 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005213 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005214 free_extent_buffer(path->nodes[level]);
5215 path->nodes[level] = NULL;
5216 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005217 }
5218 }
5219 return 1;
5220}
5221
Chris Mason9aca1d52007-03-13 11:09:37 -04005222/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005223 * drop a subvolume tree.
5224 *
5225 * this function traverses the tree freeing any blocks that only
5226 * referenced by the tree.
5227 *
5228 * when a shared tree block is found. this function decreases its
5229 * reference count by one. if update_ref is true, this function
5230 * also make sure backrefs for the shared block and all lower level
5231 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04005232 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005233int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05005234{
Chris Mason5caf2a02007-04-02 11:20:42 -04005235 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005236 struct btrfs_trans_handle *trans;
5237 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005238 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005239 struct walk_control *wc;
5240 struct btrfs_key key;
5241 int err = 0;
5242 int ret;
5243 int level;
Chris Mason20524f02007-03-10 06:35:47 -05005244
Chris Mason5caf2a02007-04-02 11:20:42 -04005245 path = btrfs_alloc_path();
5246 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05005247
Yan Zheng2c47e6052009-06-27 21:07:35 -04005248 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5249 BUG_ON(!wc);
5250
5251 trans = btrfs_start_transaction(tree_root, 1);
5252
Chris Mason9f3a7422007-08-07 15:52:19 -04005253 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005254 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005255 path->nodes[level] = btrfs_lock_root_node(root);
5256 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005257 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005258 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005259 memset(&wc->update_progress, 0,
5260 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005261 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005262 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005263 memcpy(&wc->update_progress, &key,
5264 sizeof(wc->update_progress));
5265
Chris Mason6702ed42007-08-07 16:15:09 -04005266 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005267 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005268 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005269 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5270 path->lowest_level = 0;
5271 if (ret < 0) {
5272 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005273 goto out;
5274 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005275 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005276
Chris Mason7d9eb122008-07-08 14:19:17 -04005277 /*
5278 * unlock our path, this is safe because only this
5279 * function is allowed to delete this snapshot
5280 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005281 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005282
Yan Zheng2c47e6052009-06-27 21:07:35 -04005283 level = btrfs_header_level(root->node);
5284 while (1) {
5285 btrfs_tree_lock(path->nodes[level]);
5286 btrfs_set_lock_blocking(path->nodes[level]);
5287
5288 ret = btrfs_lookup_extent_info(trans, root,
5289 path->nodes[level]->start,
5290 path->nodes[level]->len,
5291 &wc->refs[level],
5292 &wc->flags[level]);
5293 BUG_ON(ret);
5294 BUG_ON(wc->refs[level] == 0);
5295
5296 if (level == root_item->drop_level)
5297 break;
5298
5299 btrfs_tree_unlock(path->nodes[level]);
5300 WARN_ON(wc->refs[level] != 1);
5301 level--;
5302 }
5303 }
5304
5305 wc->level = level;
5306 wc->shared_level = -1;
5307 wc->stage = DROP_REFERENCE;
5308 wc->update_ref = update_ref;
5309 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005310 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005311
5312 while (1) {
5313 ret = walk_down_tree(trans, root, path, wc);
5314 if (ret < 0) {
5315 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005316 break;
5317 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005318
5319 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5320 if (ret < 0) {
5321 err = ret;
5322 break;
5323 }
5324
5325 if (ret > 0) {
5326 BUG_ON(wc->stage != DROP_REFERENCE);
5327 break;
5328 }
5329
5330 if (wc->stage == DROP_REFERENCE) {
5331 level = wc->level;
5332 btrfs_node_key(path->nodes[level],
5333 &root_item->drop_progress,
5334 path->slots[level]);
5335 root_item->drop_level = level;
5336 }
5337
5338 BUG_ON(wc->level == 0);
5339 if (trans->transaction->in_commit ||
5340 trans->transaction->delayed_refs.flushing) {
5341 ret = btrfs_update_root(trans, tree_root,
5342 &root->root_key,
5343 root_item);
5344 BUG_ON(ret);
5345
5346 btrfs_end_transaction(trans, tree_root);
5347 trans = btrfs_start_transaction(tree_root, 1);
5348 } else {
5349 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005350 update = trans->delayed_ref_updates;
5351 trans->delayed_ref_updates = 0;
5352 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005353 btrfs_run_delayed_refs(trans, tree_root,
5354 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005355 }
Chris Mason20524f02007-03-10 06:35:47 -05005356 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005357 btrfs_release_path(root, path);
5358 BUG_ON(err);
5359
5360 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5361 BUG_ON(ret);
5362
Yan, Zheng76dda932009-09-21 16:00:26 -04005363 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5364 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5365 NULL, NULL);
5366 BUG_ON(ret < 0);
5367 if (ret > 0) {
5368 ret = btrfs_del_orphan_item(trans, tree_root,
5369 root->root_key.objectid);
5370 BUG_ON(ret);
5371 }
5372 }
5373
5374 if (root->in_radix) {
5375 btrfs_free_fs_root(tree_root->fs_info, root);
5376 } else {
5377 free_extent_buffer(root->node);
5378 free_extent_buffer(root->commit_root);
5379 kfree(root);
5380 }
Chris Mason9f3a7422007-08-07 15:52:19 -04005381out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005382 btrfs_end_transaction(trans, tree_root);
5383 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005384 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005385 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005386}
Chris Mason9078a3e2007-04-26 16:46:15 -04005387
Yan Zheng2c47e6052009-06-27 21:07:35 -04005388/*
5389 * drop subtree rooted at tree block 'node'.
5390 *
5391 * NOTE: this function will unlock and release tree block 'node'
5392 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005393int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5394 struct btrfs_root *root,
5395 struct extent_buffer *node,
5396 struct extent_buffer *parent)
5397{
5398 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005399 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005400 int level;
5401 int parent_level;
5402 int ret = 0;
5403 int wret;
5404
Yan Zheng2c47e6052009-06-27 21:07:35 -04005405 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5406
Yan Zhengf82d02d2008-10-29 14:49:05 -04005407 path = btrfs_alloc_path();
5408 BUG_ON(!path);
5409
Yan Zheng2c47e6052009-06-27 21:07:35 -04005410 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5411 BUG_ON(!wc);
5412
Chris Masonb9447ef2009-03-09 11:45:38 -04005413 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005414 parent_level = btrfs_header_level(parent);
5415 extent_buffer_get(parent);
5416 path->nodes[parent_level] = parent;
5417 path->slots[parent_level] = btrfs_header_nritems(parent);
5418
Chris Masonb9447ef2009-03-09 11:45:38 -04005419 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005420 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005421 path->nodes[level] = node;
5422 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005423 path->locks[level] = 1;
5424
5425 wc->refs[parent_level] = 1;
5426 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5427 wc->level = level;
5428 wc->shared_level = -1;
5429 wc->stage = DROP_REFERENCE;
5430 wc->update_ref = 0;
5431 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005432 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005433
5434 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005435 wret = walk_down_tree(trans, root, path, wc);
5436 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005437 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005438 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005439 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005440
Yan Zheng2c47e6052009-06-27 21:07:35 -04005441 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005442 if (wret < 0)
5443 ret = wret;
5444 if (wret != 0)
5445 break;
5446 }
5447
Yan Zheng2c47e6052009-06-27 21:07:35 -04005448 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005449 btrfs_free_path(path);
5450 return ret;
5451}
5452
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005453#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005454static unsigned long calc_ra(unsigned long start, unsigned long last,
5455 unsigned long nr)
5456{
5457 return min(last, start + nr - 1);
5458}
5459
Chris Masond3977122009-01-05 21:25:51 -05005460static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005461 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005462{
5463 u64 page_start;
5464 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005465 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005466 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005467 unsigned long i;
5468 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005469 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005470 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005471 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005472 unsigned int total_read = 0;
5473 unsigned int total_dirty = 0;
5474 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005475
5476 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005477
5478 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005479 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005480 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5481
Zheng Yan1a40e232008-09-26 10:09:34 -04005482 /* make sure the dirty trick played by the caller work */
5483 ret = invalidate_inode_pages2_range(inode->i_mapping,
5484 first_index, last_index);
5485 if (ret)
5486 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005487
Chris Mason4313b392008-01-03 09:08:48 -05005488 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005489
Zheng Yan1a40e232008-09-26 10:09:34 -04005490 for (i = first_index ; i <= last_index; i++) {
5491 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005492 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005493 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005494 }
5495 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005496again:
5497 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005498 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005499 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005500 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005501 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005502 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005503 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005504 if (!PageUptodate(page)) {
5505 btrfs_readpage(NULL, page);
5506 lock_page(page);
5507 if (!PageUptodate(page)) {
5508 unlock_page(page);
5509 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005510 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005511 goto out_unlock;
5512 }
5513 }
Chris Masonec44a352008-04-28 15:29:52 -04005514 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005515
Chris Masonedbd8d42007-12-21 16:27:24 -05005516 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5517 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005518 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005519
Chris Mason3eaa2882008-07-24 11:57:52 -04005520 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5521 if (ordered) {
5522 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5523 unlock_page(page);
5524 page_cache_release(page);
5525 btrfs_start_ordered_extent(inode, ordered, 1);
5526 btrfs_put_ordered_extent(ordered);
5527 goto again;
5528 }
5529 set_page_extent_mapped(page);
5530
Zheng Yan1a40e232008-09-26 10:09:34 -04005531 if (i == first_index)
5532 set_extent_bits(io_tree, page_start, page_end,
5533 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005534 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005535
Chris Masona061fc82008-05-07 11:43:44 -04005536 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005537 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005538
Chris Masond1310b22008-01-24 16:13:08 -05005539 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005540 unlock_page(page);
5541 page_cache_release(page);
5542 }
5543
5544out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005545 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005546 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005547 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005548 return ret;
5549}
5550
Chris Masond3977122009-01-05 21:25:51 -05005551static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005552 struct btrfs_key *extent_key,
5553 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005554{
Zheng Yan1a40e232008-09-26 10:09:34 -04005555 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5556 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5557 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005558 u64 start = extent_key->objectid - offset;
5559 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005560
5561 em = alloc_extent_map(GFP_NOFS);
5562 BUG_ON(!em || IS_ERR(em));
5563
Yan Zheng66435582008-10-30 14:19:50 -04005564 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005565 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005566 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005567 em->block_start = extent_key->objectid;
5568 em->bdev = root->fs_info->fs_devices->latest_bdev;
5569 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5570
5571 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005572 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005573 while (1) {
5574 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04005575 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005576 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005577 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005578 if (ret != -EEXIST) {
5579 free_extent_map(em);
5580 break;
5581 }
Yan Zheng66435582008-10-30 14:19:50 -04005582 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005583 }
Yan Zheng66435582008-10-30 14:19:50 -04005584 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005585
Yan Zheng66435582008-10-30 14:19:50 -04005586 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005587}
5588
5589struct btrfs_ref_path {
5590 u64 extent_start;
5591 u64 nodes[BTRFS_MAX_LEVEL];
5592 u64 root_objectid;
5593 u64 root_generation;
5594 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005595 u32 num_refs;
5596 int lowest_level;
5597 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005598 int shared_level;
5599
5600 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5601 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005602};
5603
5604struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005605 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005606 u64 disk_bytenr;
5607 u64 disk_num_bytes;
5608 u64 offset;
5609 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005610 u8 compression;
5611 u8 encryption;
5612 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005613};
5614
5615static int is_cowonly_root(u64 root_objectid)
5616{
5617 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5618 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5619 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5620 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005621 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5622 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005623 return 1;
5624 return 0;
5625}
5626
Chris Masond3977122009-01-05 21:25:51 -05005627static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005628 struct btrfs_root *extent_root,
5629 struct btrfs_ref_path *ref_path,
5630 int first_time)
5631{
5632 struct extent_buffer *leaf;
5633 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005634 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005635 struct btrfs_key key;
5636 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005637 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005638 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005639 int level;
5640 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005641
Zheng Yan1a40e232008-09-26 10:09:34 -04005642 path = btrfs_alloc_path();
5643 if (!path)
5644 return -ENOMEM;
5645
Zheng Yan1a40e232008-09-26 10:09:34 -04005646 if (first_time) {
5647 ref_path->lowest_level = -1;
5648 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005649 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005650 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005651 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005652walk_down:
5653 level = ref_path->current_level - 1;
5654 while (level >= -1) {
5655 u64 parent;
5656 if (level < ref_path->lowest_level)
5657 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005658
Chris Masond3977122009-01-05 21:25:51 -05005659 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005660 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005661 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005662 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005663 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005664
Zheng Yan1a40e232008-09-26 10:09:34 -04005665 parent = ref_path->nodes[level + 1];
5666 ref_path->nodes[level + 1] = 0;
5667 ref_path->current_level = level;
5668 BUG_ON(parent == 0);
5669
5670 key.objectid = bytenr;
5671 key.offset = parent + 1;
5672 key.type = BTRFS_EXTENT_REF_KEY;
5673
5674 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005675 if (ret < 0)
5676 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005677 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005678
Chris Masonedbd8d42007-12-21 16:27:24 -05005679 leaf = path->nodes[0];
5680 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005681 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005682 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005683 if (ret < 0)
5684 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005685 if (ret > 0)
5686 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005687 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005688 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005689
5690 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005691 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005692 found_key.type == BTRFS_EXTENT_REF_KEY) {
5693 if (level < ref_path->shared_level)
5694 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005695 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005696 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005697next:
5698 level--;
5699 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005700 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005701 }
5702 /* reached lowest level */
5703 ret = 1;
5704 goto out;
5705walk_up:
5706 level = ref_path->current_level;
5707 while (level < BTRFS_MAX_LEVEL - 1) {
5708 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005709
5710 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005711 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005712 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005713 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005714
Zheng Yan1a40e232008-09-26 10:09:34 -04005715 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005716
Zheng Yan1a40e232008-09-26 10:09:34 -04005717 key.objectid = bytenr;
5718 key.offset = 0;
5719 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005720
Zheng Yan1a40e232008-09-26 10:09:34 -04005721 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5722 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005723 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005724
5725 leaf = path->nodes[0];
5726 nritems = btrfs_header_nritems(leaf);
5727 if (path->slots[0] >= nritems) {
5728 ret = btrfs_next_leaf(extent_root, path);
5729 if (ret < 0)
5730 goto out;
5731 if (ret > 0) {
5732 /* the extent was freed by someone */
5733 if (ref_path->lowest_level == level)
5734 goto out;
5735 btrfs_release_path(extent_root, path);
5736 goto walk_down;
5737 }
5738 leaf = path->nodes[0];
5739 }
5740
5741 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5742 if (found_key.objectid != bytenr ||
5743 found_key.type != BTRFS_EXTENT_REF_KEY) {
5744 /* the extent was freed by someone */
5745 if (ref_path->lowest_level == level) {
5746 ret = 1;
5747 goto out;
5748 }
5749 btrfs_release_path(extent_root, path);
5750 goto walk_down;
5751 }
5752found:
5753 ref = btrfs_item_ptr(leaf, path->slots[0],
5754 struct btrfs_extent_ref);
5755 ref_objectid = btrfs_ref_objectid(leaf, ref);
5756 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5757 if (first_time) {
5758 level = (int)ref_objectid;
5759 BUG_ON(level >= BTRFS_MAX_LEVEL);
5760 ref_path->lowest_level = level;
5761 ref_path->current_level = level;
5762 ref_path->nodes[level] = bytenr;
5763 } else {
5764 WARN_ON(ref_objectid != level);
5765 }
5766 } else {
5767 WARN_ON(level != -1);
5768 }
5769 first_time = 0;
5770
5771 if (ref_path->lowest_level == level) {
5772 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005773 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5774 }
5775
5776 /*
5777 * the block is tree root or the block isn't in reference
5778 * counted tree.
5779 */
5780 if (found_key.objectid == found_key.offset ||
5781 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5782 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5783 ref_path->root_generation =
5784 btrfs_ref_generation(leaf, ref);
5785 if (level < 0) {
5786 /* special reference from the tree log */
5787 ref_path->nodes[0] = found_key.offset;
5788 ref_path->current_level = 0;
5789 }
5790 ret = 0;
5791 goto out;
5792 }
5793
5794 level++;
5795 BUG_ON(ref_path->nodes[level] != 0);
5796 ref_path->nodes[level] = found_key.offset;
5797 ref_path->current_level = level;
5798
5799 /*
5800 * the reference was created in the running transaction,
5801 * no need to continue walking up.
5802 */
5803 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5804 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5805 ref_path->root_generation =
5806 btrfs_ref_generation(leaf, ref);
5807 ret = 0;
5808 goto out;
5809 }
5810
5811 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005812 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005813 }
5814 /* reached max tree level, but no tree root found. */
5815 BUG();
5816out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005817 btrfs_free_path(path);
5818 return ret;
5819}
5820
5821static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5822 struct btrfs_root *extent_root,
5823 struct btrfs_ref_path *ref_path,
5824 u64 extent_start)
5825{
5826 memset(ref_path, 0, sizeof(*ref_path));
5827 ref_path->extent_start = extent_start;
5828
5829 return __next_ref_path(trans, extent_root, ref_path, 1);
5830}
5831
5832static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5833 struct btrfs_root *extent_root,
5834 struct btrfs_ref_path *ref_path)
5835{
5836 return __next_ref_path(trans, extent_root, ref_path, 0);
5837}
5838
Chris Masond3977122009-01-05 21:25:51 -05005839static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005840 struct btrfs_key *extent_key,
5841 u64 offset, int no_fragment,
5842 struct disk_extent **extents,
5843 int *nr_extents)
5844{
5845 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5846 struct btrfs_path *path;
5847 struct btrfs_file_extent_item *fi;
5848 struct extent_buffer *leaf;
5849 struct disk_extent *exts = *extents;
5850 struct btrfs_key found_key;
5851 u64 cur_pos;
5852 u64 last_byte;
5853 u32 nritems;
5854 int nr = 0;
5855 int max = *nr_extents;
5856 int ret;
5857
5858 WARN_ON(!no_fragment && *extents);
5859 if (!exts) {
5860 max = 1;
5861 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
5862 if (!exts)
5863 return -ENOMEM;
5864 }
5865
5866 path = btrfs_alloc_path();
5867 BUG_ON(!path);
5868
5869 cur_pos = extent_key->objectid - offset;
5870 last_byte = extent_key->objectid + extent_key->offset;
5871 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
5872 cur_pos, 0);
5873 if (ret < 0)
5874 goto out;
5875 if (ret > 0) {
5876 ret = -ENOENT;
5877 goto out;
5878 }
5879
5880 while (1) {
5881 leaf = path->nodes[0];
5882 nritems = btrfs_header_nritems(leaf);
5883 if (path->slots[0] >= nritems) {
5884 ret = btrfs_next_leaf(root, path);
5885 if (ret < 0)
5886 goto out;
5887 if (ret > 0)
5888 break;
5889 leaf = path->nodes[0];
5890 }
5891
5892 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5893 if (found_key.offset != cur_pos ||
5894 found_key.type != BTRFS_EXTENT_DATA_KEY ||
5895 found_key.objectid != reloc_inode->i_ino)
5896 break;
5897
5898 fi = btrfs_item_ptr(leaf, path->slots[0],
5899 struct btrfs_file_extent_item);
5900 if (btrfs_file_extent_type(leaf, fi) !=
5901 BTRFS_FILE_EXTENT_REG ||
5902 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5903 break;
5904
5905 if (nr == max) {
5906 struct disk_extent *old = exts;
5907 max *= 2;
5908 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
5909 memcpy(exts, old, sizeof(*exts) * nr);
5910 if (old != *extents)
5911 kfree(old);
5912 }
5913
5914 exts[nr].disk_bytenr =
5915 btrfs_file_extent_disk_bytenr(leaf, fi);
5916 exts[nr].disk_num_bytes =
5917 btrfs_file_extent_disk_num_bytes(leaf, fi);
5918 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
5919 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04005920 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
5921 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
5922 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
5923 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
5924 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04005925 BUG_ON(exts[nr].offset > 0);
5926 BUG_ON(exts[nr].compression || exts[nr].encryption);
5927 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005928
5929 cur_pos += exts[nr].num_bytes;
5930 nr++;
5931
5932 if (cur_pos + offset >= last_byte)
5933 break;
5934
5935 if (no_fragment) {
5936 ret = 1;
5937 goto out;
5938 }
5939 path->slots[0]++;
5940 }
5941
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005942 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04005943 if (cur_pos + offset < last_byte) {
5944 ret = -ENOENT;
5945 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05005946 }
5947 ret = 0;
5948out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005949 btrfs_free_path(path);
5950 if (ret) {
5951 if (exts != *extents)
5952 kfree(exts);
5953 } else {
5954 *extents = exts;
5955 *nr_extents = nr;
5956 }
5957 return ret;
5958}
5959
Chris Masond3977122009-01-05 21:25:51 -05005960static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005961 struct btrfs_root *root,
5962 struct btrfs_path *path,
5963 struct btrfs_key *extent_key,
5964 struct btrfs_key *leaf_key,
5965 struct btrfs_ref_path *ref_path,
5966 struct disk_extent *new_extents,
5967 int nr_extents)
5968{
5969 struct extent_buffer *leaf;
5970 struct btrfs_file_extent_item *fi;
5971 struct inode *inode = NULL;
5972 struct btrfs_key key;
5973 u64 lock_start = 0;
5974 u64 lock_end = 0;
5975 u64 num_bytes;
5976 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05005977 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005978 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005979 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005980 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04005981 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04005982 int ret;
5983
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005984 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04005985 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005986 if (key.objectid < ref_path->owner_objectid ||
5987 (key.objectid == ref_path->owner_objectid &&
5988 key.type < BTRFS_EXTENT_DATA_KEY)) {
5989 key.objectid = ref_path->owner_objectid;
5990 key.type = BTRFS_EXTENT_DATA_KEY;
5991 key.offset = 0;
5992 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005993 }
5994
5995 while (1) {
5996 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5997 if (ret < 0)
5998 goto out;
5999
6000 leaf = path->nodes[0];
6001 nritems = btrfs_header_nritems(leaf);
6002next:
6003 if (extent_locked && ret > 0) {
6004 /*
6005 * the file extent item was modified by someone
6006 * before the extent got locked.
6007 */
Zheng Yan1a40e232008-09-26 10:09:34 -04006008 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6009 lock_end, GFP_NOFS);
6010 extent_locked = 0;
6011 }
6012
6013 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006014 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04006015 break;
6016
6017 BUG_ON(extent_locked);
6018 ret = btrfs_next_leaf(root, path);
6019 if (ret < 0)
6020 goto out;
6021 if (ret > 0)
6022 break;
6023 leaf = path->nodes[0];
6024 nritems = btrfs_header_nritems(leaf);
6025 }
6026
6027 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6028
6029 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6030 if ((key.objectid > ref_path->owner_objectid) ||
6031 (key.objectid == ref_path->owner_objectid &&
6032 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05006033 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006034 break;
6035 }
6036
6037 if (inode && key.objectid != inode->i_ino) {
6038 BUG_ON(extent_locked);
6039 btrfs_release_path(root, path);
6040 mutex_unlock(&inode->i_mutex);
6041 iput(inode);
6042 inode = NULL;
6043 continue;
6044 }
6045
6046 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6047 path->slots[0]++;
6048 ret = 1;
6049 goto next;
6050 }
6051 fi = btrfs_item_ptr(leaf, path->slots[0],
6052 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04006053 extent_type = btrfs_file_extent_type(leaf, fi);
6054 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6055 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04006056 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6057 extent_key->objectid)) {
6058 path->slots[0]++;
6059 ret = 1;
6060 goto next;
6061 }
6062
6063 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6064 ext_offset = btrfs_file_extent_offset(leaf, fi);
6065
Yan Zheng86288a12009-01-21 10:49:16 -05006066 if (search_end == (u64)-1) {
6067 search_end = key.offset - ext_offset +
6068 btrfs_file_extent_ram_bytes(leaf, fi);
6069 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006070
6071 if (!extent_locked) {
6072 lock_start = key.offset;
6073 lock_end = lock_start + num_bytes - 1;
6074 } else {
Yan Zheng66435582008-10-30 14:19:50 -04006075 if (lock_start > key.offset ||
6076 lock_end + 1 < key.offset + num_bytes) {
6077 unlock_extent(&BTRFS_I(inode)->io_tree,
6078 lock_start, lock_end, GFP_NOFS);
6079 extent_locked = 0;
6080 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006081 }
6082
6083 if (!inode) {
6084 btrfs_release_path(root, path);
6085
6086 inode = btrfs_iget_locked(root->fs_info->sb,
6087 key.objectid, root);
6088 if (inode->i_state & I_NEW) {
6089 BTRFS_I(inode)->root = root;
6090 BTRFS_I(inode)->location.objectid =
6091 key.objectid;
6092 BTRFS_I(inode)->location.type =
6093 BTRFS_INODE_ITEM_KEY;
6094 BTRFS_I(inode)->location.offset = 0;
6095 btrfs_read_locked_inode(inode);
6096 unlock_new_inode(inode);
6097 }
6098 /*
6099 * some code call btrfs_commit_transaction while
6100 * holding the i_mutex, so we can't use mutex_lock
6101 * here.
6102 */
6103 if (is_bad_inode(inode) ||
6104 !mutex_trylock(&inode->i_mutex)) {
6105 iput(inode);
6106 inode = NULL;
6107 key.offset = (u64)-1;
6108 goto skip;
6109 }
6110 }
6111
6112 if (!extent_locked) {
6113 struct btrfs_ordered_extent *ordered;
6114
6115 btrfs_release_path(root, path);
6116
6117 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6118 lock_end, GFP_NOFS);
6119 ordered = btrfs_lookup_first_ordered_extent(inode,
6120 lock_end);
6121 if (ordered &&
6122 ordered->file_offset <= lock_end &&
6123 ordered->file_offset + ordered->len > lock_start) {
6124 unlock_extent(&BTRFS_I(inode)->io_tree,
6125 lock_start, lock_end, GFP_NOFS);
6126 btrfs_start_ordered_extent(inode, ordered, 1);
6127 btrfs_put_ordered_extent(ordered);
6128 key.offset += num_bytes;
6129 goto skip;
6130 }
6131 if (ordered)
6132 btrfs_put_ordered_extent(ordered);
6133
Zheng Yan1a40e232008-09-26 10:09:34 -04006134 extent_locked = 1;
6135 continue;
6136 }
6137
6138 if (nr_extents == 1) {
6139 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04006140 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6141 new_extents[0].disk_bytenr);
6142 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6143 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006144 btrfs_mark_buffer_dirty(leaf);
6145
6146 btrfs_drop_extent_cache(inode, key.offset,
6147 key.offset + num_bytes - 1, 0);
6148
6149 ret = btrfs_inc_extent_ref(trans, root,
6150 new_extents[0].disk_bytenr,
6151 new_extents[0].disk_num_bytes,
6152 leaf->start,
6153 root->root_key.objectid,
6154 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006155 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006156 BUG_ON(ret);
6157
6158 ret = btrfs_free_extent(trans, root,
6159 extent_key->objectid,
6160 extent_key->offset,
6161 leaf->start,
6162 btrfs_header_owner(leaf),
6163 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006164 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006165 BUG_ON(ret);
6166
6167 btrfs_release_path(root, path);
6168 key.offset += num_bytes;
6169 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04006170 BUG_ON(1);
6171#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006172 u64 alloc_hint;
6173 u64 extent_len;
6174 int i;
6175 /*
6176 * drop old extent pointer at first, then insert the
6177 * new pointers one bye one
6178 */
6179 btrfs_release_path(root, path);
6180 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6181 key.offset + num_bytes,
6182 key.offset, &alloc_hint);
6183 BUG_ON(ret);
6184
6185 for (i = 0; i < nr_extents; i++) {
6186 if (ext_offset >= new_extents[i].num_bytes) {
6187 ext_offset -= new_extents[i].num_bytes;
6188 continue;
6189 }
6190 extent_len = min(new_extents[i].num_bytes -
6191 ext_offset, num_bytes);
6192
6193 ret = btrfs_insert_empty_item(trans, root,
6194 path, &key,
6195 sizeof(*fi));
6196 BUG_ON(ret);
6197
6198 leaf = path->nodes[0];
6199 fi = btrfs_item_ptr(leaf, path->slots[0],
6200 struct btrfs_file_extent_item);
6201 btrfs_set_file_extent_generation(leaf, fi,
6202 trans->transid);
6203 btrfs_set_file_extent_type(leaf, fi,
6204 BTRFS_FILE_EXTENT_REG);
6205 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6206 new_extents[i].disk_bytenr);
6207 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6208 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04006209 btrfs_set_file_extent_ram_bytes(leaf, fi,
6210 new_extents[i].ram_bytes);
6211
6212 btrfs_set_file_extent_compression(leaf, fi,
6213 new_extents[i].compression);
6214 btrfs_set_file_extent_encryption(leaf, fi,
6215 new_extents[i].encryption);
6216 btrfs_set_file_extent_other_encoding(leaf, fi,
6217 new_extents[i].other_encoding);
6218
Zheng Yan1a40e232008-09-26 10:09:34 -04006219 btrfs_set_file_extent_num_bytes(leaf, fi,
6220 extent_len);
6221 ext_offset += new_extents[i].offset;
6222 btrfs_set_file_extent_offset(leaf, fi,
6223 ext_offset);
6224 btrfs_mark_buffer_dirty(leaf);
6225
6226 btrfs_drop_extent_cache(inode, key.offset,
6227 key.offset + extent_len - 1, 0);
6228
6229 ret = btrfs_inc_extent_ref(trans, root,
6230 new_extents[i].disk_bytenr,
6231 new_extents[i].disk_num_bytes,
6232 leaf->start,
6233 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006234 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006235 BUG_ON(ret);
6236 btrfs_release_path(root, path);
6237
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006238 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04006239
6240 ext_offset = 0;
6241 num_bytes -= extent_len;
6242 key.offset += extent_len;
6243
6244 if (num_bytes == 0)
6245 break;
6246 }
6247 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006248#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04006249 }
6250
6251 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006252 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6253 lock_end, GFP_NOFS);
6254 extent_locked = 0;
6255 }
6256skip:
6257 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05006258 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006259 break;
6260
6261 cond_resched();
6262 }
6263 ret = 0;
6264out:
6265 btrfs_release_path(root, path);
6266 if (inode) {
6267 mutex_unlock(&inode->i_mutex);
6268 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006269 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6270 lock_end, GFP_NOFS);
6271 }
6272 iput(inode);
6273 }
6274 return ret;
6275}
6276
Zheng Yan1a40e232008-09-26 10:09:34 -04006277int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6278 struct btrfs_root *root,
6279 struct extent_buffer *buf, u64 orig_start)
6280{
6281 int level;
6282 int ret;
6283
6284 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6285 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6286
6287 level = btrfs_header_level(buf);
6288 if (level == 0) {
6289 struct btrfs_leaf_ref *ref;
6290 struct btrfs_leaf_ref *orig_ref;
6291
6292 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6293 if (!orig_ref)
6294 return -ENOENT;
6295
6296 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6297 if (!ref) {
6298 btrfs_free_leaf_ref(root, orig_ref);
6299 return -ENOMEM;
6300 }
6301
6302 ref->nritems = orig_ref->nritems;
6303 memcpy(ref->extents, orig_ref->extents,
6304 sizeof(ref->extents[0]) * ref->nritems);
6305
6306 btrfs_free_leaf_ref(root, orig_ref);
6307
6308 ref->root_gen = trans->transid;
6309 ref->bytenr = buf->start;
6310 ref->owner = btrfs_header_owner(buf);
6311 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006312
Zheng Yan1a40e232008-09-26 10:09:34 -04006313 ret = btrfs_add_leaf_ref(root, ref, 0);
6314 WARN_ON(ret);
6315 btrfs_free_leaf_ref(root, ref);
6316 }
6317 return 0;
6318}
6319
Chris Masond3977122009-01-05 21:25:51 -05006320static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006321 struct extent_buffer *leaf,
6322 struct btrfs_block_group_cache *group,
6323 struct btrfs_root *target_root)
6324{
6325 struct btrfs_key key;
6326 struct inode *inode = NULL;
6327 struct btrfs_file_extent_item *fi;
6328 u64 num_bytes;
6329 u64 skip_objectid = 0;
6330 u32 nritems;
6331 u32 i;
6332
6333 nritems = btrfs_header_nritems(leaf);
6334 for (i = 0; i < nritems; i++) {
6335 btrfs_item_key_to_cpu(leaf, &key, i);
6336 if (key.objectid == skip_objectid ||
6337 key.type != BTRFS_EXTENT_DATA_KEY)
6338 continue;
6339 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6340 if (btrfs_file_extent_type(leaf, fi) ==
6341 BTRFS_FILE_EXTENT_INLINE)
6342 continue;
6343 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6344 continue;
6345 if (!inode || inode->i_ino != key.objectid) {
6346 iput(inode);
6347 inode = btrfs_ilookup(target_root->fs_info->sb,
6348 key.objectid, target_root, 1);
6349 }
6350 if (!inode) {
6351 skip_objectid = key.objectid;
6352 continue;
6353 }
6354 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6355
6356 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6357 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006358 btrfs_drop_extent_cache(inode, key.offset,
6359 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006360 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6361 key.offset + num_bytes - 1, GFP_NOFS);
6362 cond_resched();
6363 }
6364 iput(inode);
6365 return 0;
6366}
6367
Chris Masond3977122009-01-05 21:25:51 -05006368static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006369 struct btrfs_root *root,
6370 struct extent_buffer *leaf,
6371 struct btrfs_block_group_cache *group,
6372 struct inode *reloc_inode)
6373{
6374 struct btrfs_key key;
6375 struct btrfs_key extent_key;
6376 struct btrfs_file_extent_item *fi;
6377 struct btrfs_leaf_ref *ref;
6378 struct disk_extent *new_extent;
6379 u64 bytenr;
6380 u64 num_bytes;
6381 u32 nritems;
6382 u32 i;
6383 int ext_index;
6384 int nr_extent;
6385 int ret;
6386
6387 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6388 BUG_ON(!new_extent);
6389
6390 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6391 BUG_ON(!ref);
6392
6393 ext_index = -1;
6394 nritems = btrfs_header_nritems(leaf);
6395 for (i = 0; i < nritems; i++) {
6396 btrfs_item_key_to_cpu(leaf, &key, i);
6397 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6398 continue;
6399 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6400 if (btrfs_file_extent_type(leaf, fi) ==
6401 BTRFS_FILE_EXTENT_INLINE)
6402 continue;
6403 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6404 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6405 if (bytenr == 0)
6406 continue;
6407
6408 ext_index++;
6409 if (bytenr >= group->key.objectid + group->key.offset ||
6410 bytenr + num_bytes <= group->key.objectid)
6411 continue;
6412
6413 extent_key.objectid = bytenr;
6414 extent_key.offset = num_bytes;
6415 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6416 nr_extent = 1;
6417 ret = get_new_locations(reloc_inode, &extent_key,
6418 group->key.objectid, 1,
6419 &new_extent, &nr_extent);
6420 if (ret > 0)
6421 continue;
6422 BUG_ON(ret < 0);
6423
6424 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6425 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6426 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6427 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6428
Zheng Yan1a40e232008-09-26 10:09:34 -04006429 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6430 new_extent->disk_bytenr);
6431 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6432 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006433 btrfs_mark_buffer_dirty(leaf);
6434
6435 ret = btrfs_inc_extent_ref(trans, root,
6436 new_extent->disk_bytenr,
6437 new_extent->disk_num_bytes,
6438 leaf->start,
6439 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006440 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006441 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006442
Zheng Yan1a40e232008-09-26 10:09:34 -04006443 ret = btrfs_free_extent(trans, root,
6444 bytenr, num_bytes, leaf->start,
6445 btrfs_header_owner(leaf),
6446 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006447 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006448 BUG_ON(ret);
6449 cond_resched();
6450 }
6451 kfree(new_extent);
6452 BUG_ON(ext_index + 1 != ref->nritems);
6453 btrfs_free_leaf_ref(root, ref);
6454 return 0;
6455}
6456
Yan Zhengf82d02d2008-10-29 14:49:05 -04006457int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6458 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006459{
6460 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006461 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006462
6463 if (root->reloc_root) {
6464 reloc_root = root->reloc_root;
6465 root->reloc_root = NULL;
6466 list_add(&reloc_root->dead_list,
6467 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006468
6469 btrfs_set_root_bytenr(&reloc_root->root_item,
6470 reloc_root->node->start);
6471 btrfs_set_root_level(&root->root_item,
6472 btrfs_header_level(reloc_root->node));
6473 memset(&reloc_root->root_item.drop_progress, 0,
6474 sizeof(struct btrfs_disk_key));
6475 reloc_root->root_item.drop_level = 0;
6476
6477 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6478 &reloc_root->root_key,
6479 &reloc_root->root_item);
6480 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006481 }
6482 return 0;
6483}
6484
6485int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6486{
6487 struct btrfs_trans_handle *trans;
6488 struct btrfs_root *reloc_root;
6489 struct btrfs_root *prev_root = NULL;
6490 struct list_head dead_roots;
6491 int ret;
6492 unsigned long nr;
6493
6494 INIT_LIST_HEAD(&dead_roots);
6495 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6496
6497 while (!list_empty(&dead_roots)) {
6498 reloc_root = list_entry(dead_roots.prev,
6499 struct btrfs_root, dead_list);
6500 list_del_init(&reloc_root->dead_list);
6501
6502 BUG_ON(reloc_root->commit_root != NULL);
6503 while (1) {
6504 trans = btrfs_join_transaction(root, 1);
6505 BUG_ON(!trans);
6506
6507 mutex_lock(&root->fs_info->drop_mutex);
6508 ret = btrfs_drop_snapshot(trans, reloc_root);
6509 if (ret != -EAGAIN)
6510 break;
6511 mutex_unlock(&root->fs_info->drop_mutex);
6512
6513 nr = trans->blocks_used;
6514 ret = btrfs_end_transaction(trans, root);
6515 BUG_ON(ret);
6516 btrfs_btree_balance_dirty(root, nr);
6517 }
6518
6519 free_extent_buffer(reloc_root->node);
6520
6521 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6522 &reloc_root->root_key);
6523 BUG_ON(ret);
6524 mutex_unlock(&root->fs_info->drop_mutex);
6525
6526 nr = trans->blocks_used;
6527 ret = btrfs_end_transaction(trans, root);
6528 BUG_ON(ret);
6529 btrfs_btree_balance_dirty(root, nr);
6530
6531 kfree(prev_root);
6532 prev_root = reloc_root;
6533 }
6534 if (prev_root) {
6535 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6536 kfree(prev_root);
6537 }
6538 return 0;
6539}
6540
6541int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6542{
6543 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6544 return 0;
6545}
6546
6547int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6548{
6549 struct btrfs_root *reloc_root;
6550 struct btrfs_trans_handle *trans;
6551 struct btrfs_key location;
6552 int found;
6553 int ret;
6554
6555 mutex_lock(&root->fs_info->tree_reloc_mutex);
6556 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6557 BUG_ON(ret);
6558 found = !list_empty(&root->fs_info->dead_reloc_roots);
6559 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6560
6561 if (found) {
6562 trans = btrfs_start_transaction(root, 1);
6563 BUG_ON(!trans);
6564 ret = btrfs_commit_transaction(trans, root);
6565 BUG_ON(ret);
6566 }
6567
6568 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6569 location.offset = (u64)-1;
6570 location.type = BTRFS_ROOT_ITEM_KEY;
6571
6572 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6573 BUG_ON(!reloc_root);
6574 btrfs_orphan_cleanup(reloc_root);
6575 return 0;
6576}
6577
Chris Masond3977122009-01-05 21:25:51 -05006578static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006579 struct btrfs_root *root)
6580{
6581 struct btrfs_root *reloc_root;
6582 struct extent_buffer *eb;
6583 struct btrfs_root_item *root_item;
6584 struct btrfs_key root_key;
6585 int ret;
6586
6587 BUG_ON(!root->ref_cows);
6588 if (root->reloc_root)
6589 return 0;
6590
6591 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6592 BUG_ON(!root_item);
6593
6594 ret = btrfs_copy_root(trans, root, root->commit_root,
6595 &eb, BTRFS_TREE_RELOC_OBJECTID);
6596 BUG_ON(ret);
6597
6598 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6599 root_key.offset = root->root_key.objectid;
6600 root_key.type = BTRFS_ROOT_ITEM_KEY;
6601
6602 memcpy(root_item, &root->root_item, sizeof(root_item));
6603 btrfs_set_root_refs(root_item, 0);
6604 btrfs_set_root_bytenr(root_item, eb->start);
6605 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006606 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006607
6608 btrfs_tree_unlock(eb);
6609 free_extent_buffer(eb);
6610
6611 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6612 &root_key, root_item);
6613 BUG_ON(ret);
6614 kfree(root_item);
6615
6616 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6617 &root_key);
6618 BUG_ON(!reloc_root);
6619 reloc_root->last_trans = trans->transid;
6620 reloc_root->commit_root = NULL;
6621 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6622
6623 root->reloc_root = reloc_root;
6624 return 0;
6625}
6626
6627/*
6628 * Core function of space balance.
6629 *
6630 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006631 * counted roots. There is one reloc tree for each subvol, and all
6632 * reloc trees share same root key objectid. Reloc trees are snapshots
6633 * of the latest committed roots of subvols (root->commit_root).
6634 *
6635 * To relocate a tree block referenced by a subvol, there are two steps.
6636 * COW the block through subvol's reloc tree, then update block pointer
6637 * in the subvol to point to the new block. Since all reloc trees share
6638 * same root key objectid, doing special handing for tree blocks owned
6639 * by them is easy. Once a tree block has been COWed in one reloc tree,
6640 * we can use the resulting new block directly when the same block is
6641 * required to COW again through other reloc trees. By this way, relocated
6642 * tree blocks are shared between reloc trees, so they are also shared
6643 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006644 */
Chris Masond3977122009-01-05 21:25:51 -05006645static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006646 struct btrfs_root *root,
6647 struct btrfs_path *path,
6648 struct btrfs_key *first_key,
6649 struct btrfs_ref_path *ref_path,
6650 struct btrfs_block_group_cache *group,
6651 struct inode *reloc_inode)
6652{
6653 struct btrfs_root *reloc_root;
6654 struct extent_buffer *eb = NULL;
6655 struct btrfs_key *keys;
6656 u64 *nodes;
6657 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006658 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006659 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006660 int ret;
6661
6662 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6663 lowest_level = ref_path->owner_objectid;
6664
Yan Zhengf82d02d2008-10-29 14:49:05 -04006665 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006666 path->lowest_level = lowest_level;
6667 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6668 BUG_ON(ret < 0);
6669 path->lowest_level = 0;
6670 btrfs_release_path(root, path);
6671 return 0;
6672 }
6673
Zheng Yan1a40e232008-09-26 10:09:34 -04006674 mutex_lock(&root->fs_info->tree_reloc_mutex);
6675 ret = init_reloc_tree(trans, root);
6676 BUG_ON(ret);
6677 reloc_root = root->reloc_root;
6678
Yan Zhengf82d02d2008-10-29 14:49:05 -04006679 shared_level = ref_path->shared_level;
6680 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006681
Yan Zhengf82d02d2008-10-29 14:49:05 -04006682 keys = ref_path->node_keys;
6683 nodes = ref_path->new_nodes;
6684 memset(&keys[shared_level + 1], 0,
6685 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6686 memset(&nodes[shared_level + 1], 0,
6687 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006688
Yan Zhengf82d02d2008-10-29 14:49:05 -04006689 if (nodes[lowest_level] == 0) {
6690 path->lowest_level = lowest_level;
6691 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6692 0, 1);
6693 BUG_ON(ret);
6694 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6695 eb = path->nodes[level];
6696 if (!eb || eb == reloc_root->node)
6697 break;
6698 nodes[level] = eb->start;
6699 if (level == 0)
6700 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6701 else
6702 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6703 }
Yan Zheng2b820322008-11-17 21:11:30 -05006704 if (nodes[0] &&
6705 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006706 eb = path->nodes[0];
6707 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6708 group, reloc_inode);
6709 BUG_ON(ret);
6710 }
6711 btrfs_release_path(reloc_root, path);
6712 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006713 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006714 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006715 BUG_ON(ret);
6716 }
6717
Zheng Yan1a40e232008-09-26 10:09:34 -04006718 /*
6719 * replace tree blocks in the fs tree with tree blocks in
6720 * the reloc tree.
6721 */
6722 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6723 BUG_ON(ret < 0);
6724
6725 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006726 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6727 0, 0);
6728 BUG_ON(ret);
6729 extent_buffer_get(path->nodes[0]);
6730 eb = path->nodes[0];
6731 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006732 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6733 BUG_ON(ret);
6734 free_extent_buffer(eb);
6735 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006736
Yan Zhengf82d02d2008-10-29 14:49:05 -04006737 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006738 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006739 return 0;
6740}
6741
Chris Masond3977122009-01-05 21:25:51 -05006742static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006743 struct btrfs_root *root,
6744 struct btrfs_path *path,
6745 struct btrfs_key *first_key,
6746 struct btrfs_ref_path *ref_path)
6747{
6748 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006749
6750 ret = relocate_one_path(trans, root, path, first_key,
6751 ref_path, NULL, NULL);
6752 BUG_ON(ret);
6753
Zheng Yan1a40e232008-09-26 10:09:34 -04006754 return 0;
6755}
6756
Chris Masond3977122009-01-05 21:25:51 -05006757static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006758 struct btrfs_root *extent_root,
6759 struct btrfs_path *path,
6760 struct btrfs_key *extent_key)
6761{
6762 int ret;
6763
Zheng Yan1a40e232008-09-26 10:09:34 -04006764 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6765 if (ret)
6766 goto out;
6767 ret = btrfs_del_item(trans, extent_root, path);
6768out:
Chris Masonedbd8d42007-12-21 16:27:24 -05006769 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006770 return ret;
6771}
6772
Chris Masond3977122009-01-05 21:25:51 -05006773static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006774 struct btrfs_ref_path *ref_path)
6775{
6776 struct btrfs_key root_key;
6777
6778 root_key.objectid = ref_path->root_objectid;
6779 root_key.type = BTRFS_ROOT_ITEM_KEY;
6780 if (is_cowonly_root(ref_path->root_objectid))
6781 root_key.offset = 0;
6782 else
6783 root_key.offset = (u64)-1;
6784
6785 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6786}
6787
Chris Masond3977122009-01-05 21:25:51 -05006788static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006789 struct btrfs_path *path,
6790 struct btrfs_key *extent_key,
6791 struct btrfs_block_group_cache *group,
6792 struct inode *reloc_inode, int pass)
6793{
6794 struct btrfs_trans_handle *trans;
6795 struct btrfs_root *found_root;
6796 struct btrfs_ref_path *ref_path = NULL;
6797 struct disk_extent *new_extents = NULL;
6798 int nr_extents = 0;
6799 int loops;
6800 int ret;
6801 int level;
6802 struct btrfs_key first_key;
6803 u64 prev_block = 0;
6804
Zheng Yan1a40e232008-09-26 10:09:34 -04006805
6806 trans = btrfs_start_transaction(extent_root, 1);
6807 BUG_ON(!trans);
6808
6809 if (extent_key->objectid == 0) {
6810 ret = del_extent_zero(trans, extent_root, path, extent_key);
6811 goto out;
6812 }
6813
6814 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6815 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05006816 ret = -ENOMEM;
6817 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006818 }
6819
6820 for (loops = 0; ; loops++) {
6821 if (loops == 0) {
6822 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6823 extent_key->objectid);
6824 } else {
6825 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6826 }
6827 if (ret < 0)
6828 goto out;
6829 if (ret > 0)
6830 break;
6831
6832 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6833 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6834 continue;
6835
6836 found_root = read_ref_root(extent_root->fs_info, ref_path);
6837 BUG_ON(!found_root);
6838 /*
6839 * for reference counted tree, only process reference paths
6840 * rooted at the latest committed root.
6841 */
6842 if (found_root->ref_cows &&
6843 ref_path->root_generation != found_root->root_key.offset)
6844 continue;
6845
6846 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6847 if (pass == 0) {
6848 /*
6849 * copy data extents to new locations
6850 */
6851 u64 group_start = group->key.objectid;
6852 ret = relocate_data_extent(reloc_inode,
6853 extent_key,
6854 group_start);
6855 if (ret < 0)
6856 goto out;
6857 break;
6858 }
6859 level = 0;
6860 } else {
6861 level = ref_path->owner_objectid;
6862 }
6863
6864 if (prev_block != ref_path->nodes[level]) {
6865 struct extent_buffer *eb;
6866 u64 block_start = ref_path->nodes[level];
6867 u64 block_size = btrfs_level_size(found_root, level);
6868
6869 eb = read_tree_block(found_root, block_start,
6870 block_size, 0);
6871 btrfs_tree_lock(eb);
6872 BUG_ON(level != btrfs_header_level(eb));
6873
6874 if (level == 0)
6875 btrfs_item_key_to_cpu(eb, &first_key, 0);
6876 else
6877 btrfs_node_key_to_cpu(eb, &first_key, 0);
6878
6879 btrfs_tree_unlock(eb);
6880 free_extent_buffer(eb);
6881 prev_block = block_start;
6882 }
6883
Yan Zheng24562422009-02-12 14:14:53 -05006884 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006885 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05006886 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006887 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6888 /*
6889 * try to update data extent references while
6890 * keeping metadata shared between snapshots.
6891 */
6892 if (pass == 1) {
6893 ret = relocate_one_path(trans, found_root,
6894 path, &first_key, ref_path,
6895 group, reloc_inode);
6896 if (ret < 0)
6897 goto out;
6898 continue;
6899 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006900 /*
6901 * use fallback method to process the remaining
6902 * references.
6903 */
6904 if (!new_extents) {
6905 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04006906 new_extents = kmalloc(sizeof(*new_extents),
6907 GFP_NOFS);
6908 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006909 ret = get_new_locations(reloc_inode,
6910 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04006911 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04006912 &new_extents,
6913 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006914 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04006915 goto out;
6916 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006917 ret = replace_one_extent(trans, found_root,
6918 path, extent_key,
6919 &first_key, ref_path,
6920 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05006921 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006922 ret = relocate_tree_block(trans, found_root, path,
6923 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006924 }
6925 if (ret < 0)
6926 goto out;
6927 }
6928 ret = 0;
6929out:
6930 btrfs_end_transaction(trans, extent_root);
6931 kfree(new_extents);
6932 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006933 return ret;
6934}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006935#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006936
Chris Masonec44a352008-04-28 15:29:52 -04006937static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6938{
6939 u64 num_devices;
6940 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6941 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6942
Yan Zheng2b820322008-11-17 21:11:30 -05006943 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04006944 if (num_devices == 1) {
6945 stripped |= BTRFS_BLOCK_GROUP_DUP;
6946 stripped = flags & ~stripped;
6947
6948 /* turn raid0 into single device chunks */
6949 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6950 return stripped;
6951
6952 /* turn mirroring into duplication */
6953 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6954 BTRFS_BLOCK_GROUP_RAID10))
6955 return stripped | BTRFS_BLOCK_GROUP_DUP;
6956 return flags;
6957 } else {
6958 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006959 if (flags & stripped)
6960 return flags;
6961
6962 stripped |= BTRFS_BLOCK_GROUP_DUP;
6963 stripped = flags & ~stripped;
6964
6965 /* switch duplicated blocks with raid1 */
6966 if (flags & BTRFS_BLOCK_GROUP_DUP)
6967 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6968
6969 /* turn single device chunks into raid0 */
6970 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6971 }
6972 return flags;
6973}
6974
Christoph Hellwigb2950862008-12-02 09:54:17 -05006975static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04006976 struct btrfs_block_group_cache *shrink_block_group,
6977 int force)
6978{
6979 struct btrfs_trans_handle *trans;
6980 u64 new_alloc_flags;
6981 u64 calc;
6982
Chris Masonc286ac42008-07-22 23:06:41 -04006983 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006984 if (btrfs_block_group_used(&shrink_block_group->item) +
6985 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04006986 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006987
Chris Mason0ef3e662008-05-24 14:04:53 -04006988 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006989 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04006990
Chris Mason0ef3e662008-05-24 14:04:53 -04006991 new_alloc_flags = update_block_group_flags(root,
6992 shrink_block_group->flags);
6993 if (new_alloc_flags != shrink_block_group->flags) {
6994 calc =
6995 btrfs_block_group_used(&shrink_block_group->item);
6996 } else {
6997 calc = shrink_block_group->key.offset;
6998 }
Chris Masonc286ac42008-07-22 23:06:41 -04006999 spin_unlock(&shrink_block_group->lock);
7000
Chris Mason0ef3e662008-05-24 14:04:53 -04007001 do_chunk_alloc(trans, root->fs_info->extent_root,
7002 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04007003
Chris Mason0ef3e662008-05-24 14:04:53 -04007004 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04007005 } else
7006 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04007007 return 0;
7008}
7009
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007010
7011int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
7012 struct btrfs_block_group_cache *group)
7013
7014{
7015 __alloc_chunk_for_shrink(root, group, 1);
7016 set_block_group_readonly(group);
7017 return 0;
7018}
7019
Josef Bacikba1bf482009-09-11 16:11:19 -04007020/*
7021 * checks to see if its even possible to relocate this block group.
7022 *
7023 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7024 * ok to go ahead and try.
7025 */
7026int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7027{
7028 struct btrfs_block_group_cache *block_group;
7029 struct btrfs_space_info *space_info;
7030 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7031 struct btrfs_device *device;
7032 int full = 0;
7033 int ret = 0;
7034
7035 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
7036
7037 /* odd, couldn't find the block group, leave it alone */
7038 if (!block_group)
7039 return -1;
7040
7041 /* no bytes used, we're good */
7042 if (!btrfs_block_group_used(&block_group->item))
7043 goto out;
7044
7045 space_info = block_group->space_info;
7046 spin_lock(&space_info->lock);
7047
7048 full = space_info->full;
7049
7050 /*
7051 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007052 * relocate it unless we're able to allocate a new chunk below.
7053 *
7054 * Otherwise, we need to make sure we have room in the space to handle
7055 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007056 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007057 if ((space_info->total_bytes != block_group->key.offset) &&
7058 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04007059 space_info->bytes_pinned + space_info->bytes_readonly +
7060 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04007061 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007062 spin_unlock(&space_info->lock);
7063 goto out;
7064 }
7065 spin_unlock(&space_info->lock);
7066
7067 /*
7068 * ok we don't have enough space, but maybe we have free space on our
7069 * devices to allocate new chunks for relocation, so loop through our
7070 * alloc devices and guess if we have enough space. However, if we
7071 * were marked as full, then we know there aren't enough chunks, and we
7072 * can just return.
7073 */
7074 ret = -1;
7075 if (full)
7076 goto out;
7077
7078 mutex_lock(&root->fs_info->chunk_mutex);
7079 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7080 u64 min_free = btrfs_block_group_used(&block_group->item);
7081 u64 dev_offset, max_avail;
7082
7083 /*
7084 * check to make sure we can actually find a chunk with enough
7085 * space to fit our block group in.
7086 */
7087 if (device->total_bytes > device->bytes_used + min_free) {
7088 ret = find_free_dev_extent(NULL, device, min_free,
7089 &dev_offset, &max_avail);
7090 if (!ret)
7091 break;
7092 ret = -1;
7093 }
7094 }
7095 mutex_unlock(&root->fs_info->chunk_mutex);
7096out:
7097 btrfs_put_block_group(block_group);
7098 return ret;
7099}
7100
Christoph Hellwigb2950862008-12-02 09:54:17 -05007101static int find_first_block_group(struct btrfs_root *root,
7102 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007103{
Chris Mason925baed2008-06-25 16:01:30 -04007104 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007105 struct btrfs_key found_key;
7106 struct extent_buffer *leaf;
7107 int slot;
7108
7109 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7110 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007111 goto out;
7112
Chris Masond3977122009-01-05 21:25:51 -05007113 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007114 slot = path->slots[0];
7115 leaf = path->nodes[0];
7116 if (slot >= btrfs_header_nritems(leaf)) {
7117 ret = btrfs_next_leaf(root, path);
7118 if (ret == 0)
7119 continue;
7120 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007121 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007122 break;
7123 }
7124 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7125
7126 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007127 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7128 ret = 0;
7129 goto out;
7130 }
Chris Mason0b86a832008-03-24 15:01:56 -04007131 path->slots[0]++;
7132 }
7133 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04007134out:
Chris Mason0b86a832008-03-24 15:01:56 -04007135 return ret;
7136}
7137
Zheng Yan1a40e232008-09-26 10:09:34 -04007138int btrfs_free_block_groups(struct btrfs_fs_info *info)
7139{
7140 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007141 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007142 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007143 struct rb_node *n;
7144
Yan Zheng11833d62009-09-11 16:11:19 -04007145 down_write(&info->extent_commit_sem);
7146 while (!list_empty(&info->caching_block_groups)) {
7147 caching_ctl = list_entry(info->caching_block_groups.next,
7148 struct btrfs_caching_control, list);
7149 list_del(&caching_ctl->list);
7150 put_caching_control(caching_ctl);
7151 }
7152 up_write(&info->extent_commit_sem);
7153
Zheng Yan1a40e232008-09-26 10:09:34 -04007154 spin_lock(&info->block_group_cache_lock);
7155 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7156 block_group = rb_entry(n, struct btrfs_block_group_cache,
7157 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007158 rb_erase(&block_group->cache_node,
7159 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007160 spin_unlock(&info->block_group_cache_lock);
7161
Josef Bacik80eb2342008-10-29 14:49:05 -04007162 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007163 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007164 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007165
Josef Bacik817d52f2009-07-13 21:29:25 -04007166 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007167 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007168
7169 btrfs_remove_free_space_cache(block_group);
7170
Yan Zhengd2fb3432008-12-11 16:30:39 -05007171 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04007172 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007173
7174 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007175 }
7176 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007177
7178 /* now that all the block groups are freed, go through and
7179 * free all the space_info structs. This is only called during
7180 * the final stages of unmount, and so we know nobody is
7181 * using them. We call synchronize_rcu() once before we start,
7182 * just to be on the safe side.
7183 */
7184 synchronize_rcu();
7185
7186 while(!list_empty(&info->space_info)) {
7187 space_info = list_entry(info->space_info.next,
7188 struct btrfs_space_info,
7189 list);
7190
7191 list_del(&space_info->list);
7192 kfree(space_info);
7193 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007194 return 0;
7195}
7196
Chris Mason9078a3e2007-04-26 16:46:15 -04007197int btrfs_read_block_groups(struct btrfs_root *root)
7198{
7199 struct btrfs_path *path;
7200 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007201 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007202 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007203 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007204 struct btrfs_key key;
7205 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007206 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007207
Chris Masonbe744172007-05-06 10:15:01 -04007208 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007209 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007210 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007211 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007212 path = btrfs_alloc_path();
7213 if (!path)
7214 return -ENOMEM;
7215
Chris Masond3977122009-01-05 21:25:51 -05007216 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007217 ret = find_first_block_group(root, path, &key);
7218 if (ret > 0) {
7219 ret = 0;
7220 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007221 }
Chris Mason0b86a832008-03-24 15:01:56 -04007222 if (ret != 0)
7223 goto error;
7224
Chris Mason5f39d392007-10-15 16:14:19 -04007225 leaf = path->nodes[0];
7226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007227 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007228 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007229 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04007230 break;
7231 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007232
Yan Zhengd2fb3432008-12-11 16:30:39 -05007233 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007234 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04007235 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007236 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007237 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007238 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007239
7240 /*
7241 * we only want to have 32k of ram per block group for keeping
7242 * track of free space, and if we pass 1/2 of that we want to
7243 * start converting things over to using bitmaps
7244 */
7245 cache->extents_thresh = ((1024 * 32) / 2) /
7246 sizeof(struct btrfs_free_space);
7247
Chris Mason5f39d392007-10-15 16:14:19 -04007248 read_extent_buffer(leaf, &cache->item,
7249 btrfs_item_ptr_offset(leaf, path->slots[0]),
7250 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007251 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007252
Chris Mason9078a3e2007-04-26 16:46:15 -04007253 key.objectid = found_key.objectid + found_key.offset;
7254 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007255 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007256 cache->sectorsize = root->sectorsize;
7257
Josef Bacik817d52f2009-07-13 21:29:25 -04007258 /*
7259 * check for two cases, either we are full, and therefore
7260 * don't need to bother with the caching work since we won't
7261 * find any space, or we are empty, and we can just add all
7262 * the space in and be done with it. This saves us _alot_ of
7263 * time, particularly in the full case.
7264 */
7265 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04007266 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04007267 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007268 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007269 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007270 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007271 exclude_super_stripes(root, cache);
7272 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007273 cache->cached = BTRFS_CACHE_FINISHED;
7274 add_new_free_space(cache, root->fs_info,
7275 found_key.objectid,
7276 found_key.objectid +
7277 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007278 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007279 }
Chris Mason96b51792007-10-15 16:15:19 -04007280
Chris Mason6324fbf2008-03-24 15:01:59 -04007281 ret = update_space_info(info, cache->flags, found_key.offset,
7282 btrfs_block_group_used(&cache->item),
7283 &space_info);
7284 BUG_ON(ret);
7285 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007286 spin_lock(&cache->space_info->lock);
7287 cache->space_info->bytes_super += cache->bytes_super;
7288 spin_unlock(&cache->space_info->lock);
7289
Josef Bacik80eb2342008-10-29 14:49:05 -04007290 down_write(&space_info->groups_sem);
7291 list_add_tail(&cache->list, &space_info->block_groups);
7292 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007293
Josef Bacik0f9dd462008-09-23 13:14:11 -04007294 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7295 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007296
7297 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007298 if (btrfs_chunk_readonly(root, cache->key.objectid))
7299 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007300 }
Chris Mason0b86a832008-03-24 15:01:56 -04007301 ret = 0;
7302error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007303 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007304 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007305}
Chris Mason6324fbf2008-03-24 15:01:59 -04007306
7307int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7308 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007309 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007310 u64 size)
7311{
7312 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007313 struct btrfs_root *extent_root;
7314 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007315
7316 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007317
Chris Mason12fcfd22009-03-24 10:24:20 -04007318 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007319
Chris Mason8f18cf12008-04-25 16:53:30 -04007320 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007321 if (!cache)
7322 return -ENOMEM;
7323
Chris Masone17cade2008-04-15 15:41:47 -04007324 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007325 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007326 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007327 cache->sectorsize = root->sectorsize;
7328
7329 /*
7330 * we only want to have 32k of ram per block group for keeping track
7331 * of free space, and if we pass 1/2 of that we want to start
7332 * converting things over to using bitmaps
7333 */
7334 cache->extents_thresh = ((1024 * 32) / 2) /
7335 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007336 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007337 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04007338 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007339 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007340 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007341
Chris Mason6324fbf2008-03-24 15:01:59 -04007342 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007343 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7344 cache->flags = type;
7345 btrfs_set_block_group_flags(&cache->item, type);
7346
Yan Zheng11833d62009-09-11 16:11:19 -04007347 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007348 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007349 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007350
Josef Bacik817d52f2009-07-13 21:29:25 -04007351 add_new_free_space(cache, root->fs_info, chunk_offset,
7352 chunk_offset + size);
7353
Yan Zheng11833d62009-09-11 16:11:19 -04007354 free_excluded_extents(root, cache);
7355
Chris Mason6324fbf2008-03-24 15:01:59 -04007356 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7357 &cache->space_info);
7358 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007359
7360 spin_lock(&cache->space_info->lock);
7361 cache->space_info->bytes_super += cache->bytes_super;
7362 spin_unlock(&cache->space_info->lock);
7363
Josef Bacik80eb2342008-10-29 14:49:05 -04007364 down_write(&cache->space_info->groups_sem);
7365 list_add_tail(&cache->list, &cache->space_info->block_groups);
7366 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007367
Josef Bacik0f9dd462008-09-23 13:14:11 -04007368 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7369 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007370
Chris Mason6324fbf2008-03-24 15:01:59 -04007371 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7372 sizeof(cache->item));
7373 BUG_ON(ret);
7374
Chris Masond18a2c42008-04-04 15:40:00 -04007375 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007376
Chris Mason6324fbf2008-03-24 15:01:59 -04007377 return 0;
7378}
Zheng Yan1a40e232008-09-26 10:09:34 -04007379
7380int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7381 struct btrfs_root *root, u64 group_start)
7382{
7383 struct btrfs_path *path;
7384 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007385 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007386 struct btrfs_key key;
7387 int ret;
7388
Zheng Yan1a40e232008-09-26 10:09:34 -04007389 root = root->fs_info->extent_root;
7390
7391 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7392 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007393 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007394
7395 memcpy(&key, &block_group->key, sizeof(key));
7396
Chris Mason44fb5512009-06-04 15:34:51 -04007397 /* make sure this block group isn't part of an allocation cluster */
7398 cluster = &root->fs_info->data_alloc_cluster;
7399 spin_lock(&cluster->refill_lock);
7400 btrfs_return_cluster_to_free_space(block_group, cluster);
7401 spin_unlock(&cluster->refill_lock);
7402
7403 /*
7404 * make sure this block group isn't part of a metadata
7405 * allocation cluster
7406 */
7407 cluster = &root->fs_info->meta_alloc_cluster;
7408 spin_lock(&cluster->refill_lock);
7409 btrfs_return_cluster_to_free_space(block_group, cluster);
7410 spin_unlock(&cluster->refill_lock);
7411
Zheng Yan1a40e232008-09-26 10:09:34 -04007412 path = btrfs_alloc_path();
7413 BUG_ON(!path);
7414
Yan Zheng3dfdb932009-01-21 10:49:16 -05007415 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007416 rb_erase(&block_group->cache_node,
7417 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007418 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007419
Josef Bacik80eb2342008-10-29 14:49:05 -04007420 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007421 /*
7422 * we must use list_del_init so people can check to see if they
7423 * are still on the list after taking the semaphore
7424 */
7425 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007426 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007427
Josef Bacik817d52f2009-07-13 21:29:25 -04007428 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007429 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007430
7431 btrfs_remove_free_space_cache(block_group);
7432
Yan Zhengc146afa2008-11-12 14:34:12 -05007433 spin_lock(&block_group->space_info->lock);
7434 block_group->space_info->total_bytes -= block_group->key.offset;
7435 block_group->space_info->bytes_readonly -= block_group->key.offset;
7436 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007437
7438 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007439
Chris Masonfa9c0d792009-04-03 09:47:43 -04007440 btrfs_put_block_group(block_group);
7441 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007442
7443 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7444 if (ret > 0)
7445 ret = -EIO;
7446 if (ret < 0)
7447 goto out;
7448
7449 ret = btrfs_del_item(trans, root, path);
7450out:
7451 btrfs_free_path(path);
7452 return ret;
7453}