blob: b1822e752b4aa335ae1c44b0c7a0e703275ce510 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050027#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040031#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040033#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040034#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Josef Bacikf3465ca2008-11-12 14:19:50 -050036static int update_block_group(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040038 u64 bytenr, u64 num_bytes, int alloc);
39static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve, int sinfo);
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 find_next_key(struct btrfs_path *path, int level,
64 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040065static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
66 int dump_block_groups);
Josef Bacik6a632092009-02-20 11:00:09 -050067
Josef Bacik817d52f2009-07-13 21:29:25 -040068static noinline int
69block_group_cache_done(struct btrfs_block_group_cache *cache)
70{
71 smp_mb();
72 return cache->cached == BTRFS_CACHE_FINISHED;
73}
74
Josef Bacik0f9dd462008-09-23 13:14:11 -040075static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
76{
77 return (cache->flags & bits) == bits;
78}
79
Josef Bacik11dfe352009-11-13 20:12:59 +000080void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
81{
82 atomic_inc(&cache->count);
83}
84
85void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
86{
Yan, Zhengf0486c62010-05-16 10:46:25 -040087 if (atomic_dec_and_test(&cache->count)) {
88 WARN_ON(cache->pinned > 0);
89 WARN_ON(cache->reserved > 0);
90 WARN_ON(cache->reserved_pinned > 0);
Josef Bacik11dfe352009-11-13 20:12:59 +000091 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -040092 }
Josef Bacik11dfe352009-11-13 20:12:59 +000093}
94
Josef Bacik0f9dd462008-09-23 13:14:11 -040095/*
96 * this adds the block group to the fs_info rb tree for the block group
97 * cache
98 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050099static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400100 struct btrfs_block_group_cache *block_group)
101{
102 struct rb_node **p;
103 struct rb_node *parent = NULL;
104 struct btrfs_block_group_cache *cache;
105
106 spin_lock(&info->block_group_cache_lock);
107 p = &info->block_group_cache_tree.rb_node;
108
109 while (*p) {
110 parent = *p;
111 cache = rb_entry(parent, struct btrfs_block_group_cache,
112 cache_node);
113 if (block_group->key.objectid < cache->key.objectid) {
114 p = &(*p)->rb_left;
115 } else if (block_group->key.objectid > cache->key.objectid) {
116 p = &(*p)->rb_right;
117 } else {
118 spin_unlock(&info->block_group_cache_lock);
119 return -EEXIST;
120 }
121 }
122
123 rb_link_node(&block_group->cache_node, parent, p);
124 rb_insert_color(&block_group->cache_node,
125 &info->block_group_cache_tree);
126 spin_unlock(&info->block_group_cache_lock);
127
128 return 0;
129}
130
131/*
132 * This will return the block group at or after bytenr if contains is 0, else
133 * it will return the block group that contains the bytenr
134 */
135static struct btrfs_block_group_cache *
136block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
137 int contains)
138{
139 struct btrfs_block_group_cache *cache, *ret = NULL;
140 struct rb_node *n;
141 u64 end, start;
142
143 spin_lock(&info->block_group_cache_lock);
144 n = info->block_group_cache_tree.rb_node;
145
146 while (n) {
147 cache = rb_entry(n, struct btrfs_block_group_cache,
148 cache_node);
149 end = cache->key.objectid + cache->key.offset - 1;
150 start = cache->key.objectid;
151
152 if (bytenr < start) {
153 if (!contains && (!ret || start < ret->key.objectid))
154 ret = cache;
155 n = n->rb_left;
156 } else if (bytenr > start) {
157 if (contains && bytenr <= end) {
158 ret = cache;
159 break;
160 }
161 n = n->rb_right;
162 } else {
163 ret = cache;
164 break;
165 }
166 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500167 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000168 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400169 spin_unlock(&info->block_group_cache_lock);
170
171 return ret;
172}
173
Yan Zheng11833d62009-09-11 16:11:19 -0400174static int add_excluded_extent(struct btrfs_root *root,
175 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400176{
Yan Zheng11833d62009-09-11 16:11:19 -0400177 u64 end = start + num_bytes - 1;
178 set_extent_bits(&root->fs_info->freed_extents[0],
179 start, end, EXTENT_UPTODATE, GFP_NOFS);
180 set_extent_bits(&root->fs_info->freed_extents[1],
181 start, end, EXTENT_UPTODATE, GFP_NOFS);
182 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400183}
184
Yan Zheng11833d62009-09-11 16:11:19 -0400185static void free_excluded_extents(struct btrfs_root *root,
186 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400187{
Yan Zheng11833d62009-09-11 16:11:19 -0400188 u64 start, end;
189
190 start = cache->key.objectid;
191 end = start + cache->key.offset - 1;
192
193 clear_extent_bits(&root->fs_info->freed_extents[0],
194 start, end, EXTENT_UPTODATE, GFP_NOFS);
195 clear_extent_bits(&root->fs_info->freed_extents[1],
196 start, end, EXTENT_UPTODATE, GFP_NOFS);
197}
198
199static int exclude_super_stripes(struct btrfs_root *root,
200 struct btrfs_block_group_cache *cache)
201{
Josef Bacik817d52f2009-07-13 21:29:25 -0400202 u64 bytenr;
203 u64 *logical;
204 int stripe_len;
205 int i, nr, ret;
206
Yan, Zheng06b23312009-11-26 09:31:11 +0000207 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
208 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
209 cache->bytes_super += stripe_len;
210 ret = add_excluded_extent(root, cache->key.objectid,
211 stripe_len);
212 BUG_ON(ret);
213 }
214
Josef Bacik817d52f2009-07-13 21:29:25 -0400215 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
216 bytenr = btrfs_sb_offset(i);
217 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
218 cache->key.objectid, bytenr,
219 0, &logical, &nr, &stripe_len);
220 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400221
Josef Bacik817d52f2009-07-13 21:29:25 -0400222 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400223 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400224 ret = add_excluded_extent(root, logical[nr],
225 stripe_len);
226 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400227 }
Yan Zheng11833d62009-09-11 16:11:19 -0400228
Josef Bacik817d52f2009-07-13 21:29:25 -0400229 kfree(logical);
230 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400231 return 0;
232}
233
Yan Zheng11833d62009-09-11 16:11:19 -0400234static struct btrfs_caching_control *
235get_caching_control(struct btrfs_block_group_cache *cache)
236{
237 struct btrfs_caching_control *ctl;
238
239 spin_lock(&cache->lock);
240 if (cache->cached != BTRFS_CACHE_STARTED) {
241 spin_unlock(&cache->lock);
242 return NULL;
243 }
244
245 ctl = cache->caching_ctl;
246 atomic_inc(&ctl->count);
247 spin_unlock(&cache->lock);
248 return ctl;
249}
250
251static void put_caching_control(struct btrfs_caching_control *ctl)
252{
253 if (atomic_dec_and_test(&ctl->count))
254 kfree(ctl);
255}
256
Josef Bacik0f9dd462008-09-23 13:14:11 -0400257/*
258 * this is only called by cache_block_group, since we could have freed extents
259 * we need to check the pinned_extents for any extents that can't be used yet
260 * since their free space will be released as soon as the transaction commits.
261 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400262static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263 struct btrfs_fs_info *info, u64 start, u64 end)
264{
Josef Bacik817d52f2009-07-13 21:29:25 -0400265 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400266 int ret;
267
268 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400269 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400270 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400271 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400272 if (ret)
273 break;
274
Yan, Zheng06b23312009-11-26 09:31:11 +0000275 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400276 start = extent_end + 1;
277 } else if (extent_start > start && extent_start < end) {
278 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400279 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500280 ret = btrfs_add_free_space(block_group, start,
281 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400282 BUG_ON(ret);
283 start = extent_end + 1;
284 } else {
285 break;
286 }
287 }
288
289 if (start < end) {
290 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400291 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500292 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400293 BUG_ON(ret);
294 }
295
Josef Bacik817d52f2009-07-13 21:29:25 -0400296 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400297}
298
Josef Bacik817d52f2009-07-13 21:29:25 -0400299static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400300{
Josef Bacik817d52f2009-07-13 21:29:25 -0400301 struct btrfs_block_group_cache *block_group = data;
302 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400303 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
304 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400305 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400306 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400307 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400308 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400309 u64 last = 0;
310 u32 nritems;
311 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400312
Chris Masone37c9e62007-05-09 20:13:14 -0400313 path = btrfs_alloc_path();
314 if (!path)
315 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400316
Yan Zheng11833d62009-09-11 16:11:19 -0400317 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400318 spin_lock(&block_group->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400319 block_group->space_info->bytes_readonly += block_group->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -0400320 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400321
Josef Bacik817d52f2009-07-13 21:29:25 -0400322 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400323
Chris Mason5cd57b22008-06-25 16:01:30 -0400324 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400325 * We don't want to deadlock with somebody trying to allocate a new
326 * extent for the extent root while also trying to search the extent
327 * root to add free space. So we skip locking and search the commit
328 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400329 */
330 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 path->search_commit_root = 1;
332 path->reada = 2;
333
Yan Zhenge4404d62008-12-12 10:03:26 -0500334 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400335 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400336 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400337again:
Yan Zheng11833d62009-09-11 16:11:19 -0400338 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400339 /* need to make sure the commit_root doesn't disappear */
340 down_read(&fs_info->extent_commit_sem);
341
Yan Zheng11833d62009-09-11 16:11:19 -0400342 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400343 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400344 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500345
Yan Zheng11833d62009-09-11 16:11:19 -0400346 leaf = path->nodes[0];
347 nritems = btrfs_header_nritems(leaf);
348
Chris Masond3977122009-01-05 21:25:51 -0500349 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400350 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400351 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400352 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400353 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400354 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400355
Yan Zheng11833d62009-09-11 16:11:19 -0400356 if (path->slots[0] < nritems) {
357 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
358 } else {
359 ret = find_next_key(path, 0, &key);
360 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400361 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400362
Yan Zheng11833d62009-09-11 16:11:19 -0400363 caching_ctl->progress = last;
364 btrfs_release_path(extent_root, path);
365 up_read(&fs_info->extent_commit_sem);
366 mutex_unlock(&caching_ctl->mutex);
367 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400368 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400369 else
370 cond_resched();
371 goto again;
372 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400373
Yan Zheng11833d62009-09-11 16:11:19 -0400374 if (key.objectid < block_group->key.objectid) {
375 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400376 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400377 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400378
Chris Masone37c9e62007-05-09 20:13:14 -0400379 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400380 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400381 break;
Yan7d7d6062007-09-14 16:15:28 -0400382
Yan Zheng11833d62009-09-11 16:11:19 -0400383 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400384 total_found += add_new_free_space(block_group,
385 fs_info, last,
386 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400387 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400388
Yan Zheng11833d62009-09-11 16:11:19 -0400389 if (total_found > (1024 * 1024 * 2)) {
390 total_found = 0;
391 wake_up(&caching_ctl->wait);
392 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400393 }
Chris Masone37c9e62007-05-09 20:13:14 -0400394 path->slots[0]++;
395 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400396 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400397
398 total_found += add_new_free_space(block_group, fs_info, last,
399 block_group->key.objectid +
400 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400401 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400402
403 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400404 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400405 block_group->cached = BTRFS_CACHE_FINISHED;
406 spin_unlock(&block_group->lock);
407
Chris Mason54aa1f42007-06-22 14:16:25 -0400408err:
Chris Masone37c9e62007-05-09 20:13:14 -0400409 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400410 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400411
Yan Zheng11833d62009-09-11 16:11:19 -0400412 free_excluded_extents(extent_root, block_group);
413
414 mutex_unlock(&caching_ctl->mutex);
415 wake_up(&caching_ctl->wait);
416
417 put_caching_control(caching_ctl);
418 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000419 btrfs_put_block_group(block_group);
420
Josef Bacik817d52f2009-07-13 21:29:25 -0400421 return 0;
422}
423
424static int cache_block_group(struct btrfs_block_group_cache *cache)
425{
Yan Zheng11833d62009-09-11 16:11:19 -0400426 struct btrfs_fs_info *fs_info = cache->fs_info;
427 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400428 struct task_struct *tsk;
429 int ret = 0;
430
Yan Zheng11833d62009-09-11 16:11:19 -0400431 smp_mb();
432 if (cache->cached != BTRFS_CACHE_NO)
433 return 0;
434
435 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
436 BUG_ON(!caching_ctl);
437
438 INIT_LIST_HEAD(&caching_ctl->list);
439 mutex_init(&caching_ctl->mutex);
440 init_waitqueue_head(&caching_ctl->wait);
441 caching_ctl->block_group = cache;
442 caching_ctl->progress = cache->key.objectid;
443 /* one for caching kthread, one for caching block group list */
444 atomic_set(&caching_ctl->count, 2);
445
Josef Bacik817d52f2009-07-13 21:29:25 -0400446 spin_lock(&cache->lock);
447 if (cache->cached != BTRFS_CACHE_NO) {
448 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400449 kfree(caching_ctl);
450 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400451 }
Yan Zheng11833d62009-09-11 16:11:19 -0400452 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400453 cache->cached = BTRFS_CACHE_STARTED;
454 spin_unlock(&cache->lock);
455
Yan Zheng11833d62009-09-11 16:11:19 -0400456 down_write(&fs_info->extent_commit_sem);
457 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
458 up_write(&fs_info->extent_commit_sem);
459
460 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000461 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400462
Josef Bacik817d52f2009-07-13 21:29:25 -0400463 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
464 cache->key.objectid);
465 if (IS_ERR(tsk)) {
466 ret = PTR_ERR(tsk);
467 printk(KERN_ERR "error running thread %d\n", ret);
468 BUG();
469 }
470
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400471 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400472}
473
Josef Bacik0f9dd462008-09-23 13:14:11 -0400474/*
475 * return the block group that starts at or after bytenr
476 */
Chris Masond3977122009-01-05 21:25:51 -0500477static struct btrfs_block_group_cache *
478btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400479{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400480 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400481
Josef Bacik0f9dd462008-09-23 13:14:11 -0400482 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400483
Josef Bacik0f9dd462008-09-23 13:14:11 -0400484 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400485}
486
Josef Bacik0f9dd462008-09-23 13:14:11 -0400487/*
Sankar P9f556842009-05-14 13:52:22 -0400488 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400489 */
Chris Masond3977122009-01-05 21:25:51 -0500490struct btrfs_block_group_cache *btrfs_lookup_block_group(
491 struct btrfs_fs_info *info,
492 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400493{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400494 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400495
Josef Bacik0f9dd462008-09-23 13:14:11 -0400496 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400497
Josef Bacik0f9dd462008-09-23 13:14:11 -0400498 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400499}
Chris Mason0b86a832008-03-24 15:01:56 -0400500
Josef Bacik0f9dd462008-09-23 13:14:11 -0400501static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
502 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400503{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400504 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400505 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400506
Yan, Zhengb742bb82010-05-16 10:46:24 -0400507 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
508 BTRFS_BLOCK_GROUP_METADATA;
509
Chris Mason4184ea72009-03-10 12:39:20 -0400510 rcu_read_lock();
511 list_for_each_entry_rcu(found, head, list) {
512 if (found->flags == flags) {
513 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400514 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400515 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400516 }
Chris Mason4184ea72009-03-10 12:39:20 -0400517 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400518 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400519}
520
Chris Mason4184ea72009-03-10 12:39:20 -0400521/*
522 * after adding space to the filesystem, we need to clear the full flags
523 * on all the space infos.
524 */
525void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
526{
527 struct list_head *head = &info->space_info;
528 struct btrfs_space_info *found;
529
530 rcu_read_lock();
531 list_for_each_entry_rcu(found, head, list)
532 found->full = 0;
533 rcu_read_unlock();
534}
535
Josef Bacik80eb2342008-10-29 14:49:05 -0400536static u64 div_factor(u64 num, int factor)
537{
538 if (factor == 10)
539 return num;
540 num *= factor;
541 do_div(num, 10);
542 return num;
543}
544
Yan Zhengd2fb3432008-12-11 16:30:39 -0500545u64 btrfs_find_block_group(struct btrfs_root *root,
546 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400547{
Chris Mason96b51792007-10-15 16:15:19 -0400548 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400549 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500550 u64 last = max(search_hint, search_start);
551 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400552 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500553 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400554 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400555again:
Zheng Yane8569812008-09-26 10:05:48 -0400556 while (1) {
557 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400558 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400559 break;
Chris Mason96b51792007-10-15 16:15:19 -0400560
Chris Masonc286ac42008-07-22 23:06:41 -0400561 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400562 last = cache->key.objectid + cache->key.offset;
563 used = btrfs_block_group_used(&cache->item);
564
Yan Zhengd2fb3432008-12-11 16:30:39 -0500565 if ((full_search || !cache->ro) &&
566 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400567 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500568 div_factor(cache->key.offset, factor)) {
569 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400570 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400571 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400572 goto found;
573 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400574 }
Chris Masonc286ac42008-07-22 23:06:41 -0400575 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400576 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400577 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400578 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400579 if (!wrapped) {
580 last = search_start;
581 wrapped = 1;
582 goto again;
583 }
584 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400585 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400586 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400587 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400588 goto again;
589 }
Chris Masonbe744172007-05-06 10:15:01 -0400590found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500591 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400592}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400593
Chris Masone02119d2008-09-05 16:13:11 -0400594/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400595int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400596{
597 int ret;
598 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400599 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400600
Zheng Yan31840ae2008-09-23 13:14:14 -0400601 path = btrfs_alloc_path();
602 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400603 key.objectid = start;
604 key.offset = len;
605 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
606 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
607 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400608 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500609 return ret;
610}
611
Chris Masond8d5f3e2007-12-11 12:42:00 -0500612/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400613 * helper function to lookup reference count and flags of extent.
614 *
615 * the head node for delayed ref is used to store the sum of all the
616 * reference count modifications queued up in the rbtree. the head
617 * node may also store the extent flags to set. This way you can check
618 * to see what the reference count and extent flags would be if all of
619 * the delayed refs are not processed.
620 */
621int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
622 struct btrfs_root *root, u64 bytenr,
623 u64 num_bytes, u64 *refs, u64 *flags)
624{
625 struct btrfs_delayed_ref_head *head;
626 struct btrfs_delayed_ref_root *delayed_refs;
627 struct btrfs_path *path;
628 struct btrfs_extent_item *ei;
629 struct extent_buffer *leaf;
630 struct btrfs_key key;
631 u32 item_size;
632 u64 num_refs;
633 u64 extent_flags;
634 int ret;
635
636 path = btrfs_alloc_path();
637 if (!path)
638 return -ENOMEM;
639
640 key.objectid = bytenr;
641 key.type = BTRFS_EXTENT_ITEM_KEY;
642 key.offset = num_bytes;
643 if (!trans) {
644 path->skip_locking = 1;
645 path->search_commit_root = 1;
646 }
647again:
648 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
649 &key, path, 0, 0);
650 if (ret < 0)
651 goto out_free;
652
653 if (ret == 0) {
654 leaf = path->nodes[0];
655 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
656 if (item_size >= sizeof(*ei)) {
657 ei = btrfs_item_ptr(leaf, path->slots[0],
658 struct btrfs_extent_item);
659 num_refs = btrfs_extent_refs(leaf, ei);
660 extent_flags = btrfs_extent_flags(leaf, ei);
661 } else {
662#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
663 struct btrfs_extent_item_v0 *ei0;
664 BUG_ON(item_size != sizeof(*ei0));
665 ei0 = btrfs_item_ptr(leaf, path->slots[0],
666 struct btrfs_extent_item_v0);
667 num_refs = btrfs_extent_refs_v0(leaf, ei0);
668 /* FIXME: this isn't correct for data */
669 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
670#else
671 BUG();
672#endif
673 }
674 BUG_ON(num_refs == 0);
675 } else {
676 num_refs = 0;
677 extent_flags = 0;
678 ret = 0;
679 }
680
681 if (!trans)
682 goto out;
683
684 delayed_refs = &trans->transaction->delayed_refs;
685 spin_lock(&delayed_refs->lock);
686 head = btrfs_find_delayed_ref_head(trans, bytenr);
687 if (head) {
688 if (!mutex_trylock(&head->mutex)) {
689 atomic_inc(&head->node.refs);
690 spin_unlock(&delayed_refs->lock);
691
692 btrfs_release_path(root->fs_info->extent_root, path);
693
694 mutex_lock(&head->mutex);
695 mutex_unlock(&head->mutex);
696 btrfs_put_delayed_ref(&head->node);
697 goto again;
698 }
699 if (head->extent_op && head->extent_op->update_flags)
700 extent_flags |= head->extent_op->flags_to_set;
701 else
702 BUG_ON(num_refs == 0);
703
704 num_refs += head->node.ref_mod;
705 mutex_unlock(&head->mutex);
706 }
707 spin_unlock(&delayed_refs->lock);
708out:
709 WARN_ON(num_refs == 0);
710 if (refs)
711 *refs = num_refs;
712 if (flags)
713 *flags = extent_flags;
714out_free:
715 btrfs_free_path(path);
716 return ret;
717}
718
719/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500720 * Back reference rules. Back refs have three main goals:
721 *
722 * 1) differentiate between all holders of references to an extent so that
723 * when a reference is dropped we can make sure it was a valid reference
724 * before freeing the extent.
725 *
726 * 2) Provide enough information to quickly find the holders of an extent
727 * if we notice a given block is corrupted or bad.
728 *
729 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
730 * maintenance. This is actually the same as #2, but with a slightly
731 * different use case.
732 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400733 * There are two kinds of back refs. The implicit back refs is optimized
734 * for pointers in non-shared tree blocks. For a given pointer in a block,
735 * back refs of this kind provide information about the block's owner tree
736 * and the pointer's key. These information allow us to find the block by
737 * b-tree searching. The full back refs is for pointers in tree blocks not
738 * referenced by their owner trees. The location of tree block is recorded
739 * in the back refs. Actually the full back refs is generic, and can be
740 * used in all cases the implicit back refs is used. The major shortcoming
741 * of the full back refs is its overhead. Every time a tree block gets
742 * COWed, we have to update back refs entry for all pointers in it.
743 *
744 * For a newly allocated tree block, we use implicit back refs for
745 * pointers in it. This means most tree related operations only involve
746 * implicit back refs. For a tree block created in old transaction, the
747 * only way to drop a reference to it is COW it. So we can detect the
748 * event that tree block loses its owner tree's reference and do the
749 * back refs conversion.
750 *
751 * When a tree block is COW'd through a tree, there are four cases:
752 *
753 * The reference count of the block is one and the tree is the block's
754 * owner tree. Nothing to do in this case.
755 *
756 * The reference count of the block is one and the tree is not the
757 * block's owner tree. In this case, full back refs is used for pointers
758 * in the block. Remove these full back refs, add implicit back refs for
759 * every pointers in the new block.
760 *
761 * The reference count of the block is greater than one and the tree is
762 * the block's owner tree. In this case, implicit back refs is used for
763 * pointers in the block. Add full back refs for every pointers in the
764 * block, increase lower level extents' reference counts. The original
765 * implicit back refs are entailed to the new block.
766 *
767 * The reference count of the block is greater than one and the tree is
768 * not the block's owner tree. Add implicit back refs for every pointer in
769 * the new block, increase lower level extents' reference count.
770 *
771 * Back Reference Key composing:
772 *
773 * The key objectid corresponds to the first byte in the extent,
774 * The key type is used to differentiate between types of back refs.
775 * There are different meanings of the key offset for different types
776 * of back refs.
777 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500778 * File extents can be referenced by:
779 *
780 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400781 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500782 * - different offsets inside a file (bookend extents in file.c)
783 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400784 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500785 *
786 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500787 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400788 * - original offset in the file
789 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400790 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400791 * The key offset for the implicit back refs is hash of the first
792 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500793 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400794 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500795 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400796 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500797 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400798 * The key offset for the implicit back refs is the first byte of
799 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500800 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400801 * When a file extent is allocated, The implicit back refs is used.
802 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500803 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400804 * (root_key.objectid, inode objectid, offset in file, 1)
805 *
806 * When a file extent is removed file truncation, we find the
807 * corresponding implicit back refs and check the following fields:
808 *
809 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500810 *
811 * Btree extents can be referenced by:
812 *
813 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500814 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400815 * Both the implicit back refs and the full back refs for tree blocks
816 * only consist of key. The key offset for the implicit back refs is
817 * objectid of block's owner tree. The key offset for the full back refs
818 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500819 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400820 * When implicit back refs is used, information about the lowest key and
821 * level of the tree block are required. These information are stored in
822 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500823 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400824
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400825#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
826static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
827 struct btrfs_root *root,
828 struct btrfs_path *path,
829 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500830{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400831 struct btrfs_extent_item *item;
832 struct btrfs_extent_item_v0 *ei0;
833 struct btrfs_extent_ref_v0 *ref0;
834 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400835 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400836 struct btrfs_key key;
837 struct btrfs_key found_key;
838 u32 new_size = sizeof(*item);
839 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500840 int ret;
841
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400842 leaf = path->nodes[0];
843 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500844
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400845 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
846 ei0 = btrfs_item_ptr(leaf, path->slots[0],
847 struct btrfs_extent_item_v0);
848 refs = btrfs_extent_refs_v0(leaf, ei0);
849
850 if (owner == (u64)-1) {
851 while (1) {
852 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
853 ret = btrfs_next_leaf(root, path);
854 if (ret < 0)
855 return ret;
856 BUG_ON(ret > 0);
857 leaf = path->nodes[0];
858 }
859 btrfs_item_key_to_cpu(leaf, &found_key,
860 path->slots[0]);
861 BUG_ON(key.objectid != found_key.objectid);
862 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
863 path->slots[0]++;
864 continue;
865 }
866 ref0 = btrfs_item_ptr(leaf, path->slots[0],
867 struct btrfs_extent_ref_v0);
868 owner = btrfs_ref_objectid_v0(leaf, ref0);
869 break;
870 }
871 }
872 btrfs_release_path(root, path);
873
874 if (owner < BTRFS_FIRST_FREE_OBJECTID)
875 new_size += sizeof(*bi);
876
877 new_size -= sizeof(*ei0);
878 ret = btrfs_search_slot(trans, root, &key, path,
879 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400880 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881 return ret;
882 BUG_ON(ret);
883
884 ret = btrfs_extend_item(trans, root, path, new_size);
885 BUG_ON(ret);
886
887 leaf = path->nodes[0];
888 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
889 btrfs_set_extent_refs(leaf, item, refs);
890 /* FIXME: get real generation */
891 btrfs_set_extent_generation(leaf, item, 0);
892 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
893 btrfs_set_extent_flags(leaf, item,
894 BTRFS_EXTENT_FLAG_TREE_BLOCK |
895 BTRFS_BLOCK_FLAG_FULL_BACKREF);
896 bi = (struct btrfs_tree_block_info *)(item + 1);
897 /* FIXME: get first key of the block */
898 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
899 btrfs_set_tree_block_level(leaf, bi, (int)owner);
900 } else {
901 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
902 }
903 btrfs_mark_buffer_dirty(leaf);
904 return 0;
905}
906#endif
907
908static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
909{
910 u32 high_crc = ~(u32)0;
911 u32 low_crc = ~(u32)0;
912 __le64 lenum;
913
914 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100915 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400916 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100917 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100919 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400920
921 return ((u64)high_crc << 31) ^ (u64)low_crc;
922}
923
924static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
925 struct btrfs_extent_data_ref *ref)
926{
927 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
928 btrfs_extent_data_ref_objectid(leaf, ref),
929 btrfs_extent_data_ref_offset(leaf, ref));
930}
931
932static int match_extent_data_ref(struct extent_buffer *leaf,
933 struct btrfs_extent_data_ref *ref,
934 u64 root_objectid, u64 owner, u64 offset)
935{
936 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
937 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
938 btrfs_extent_data_ref_offset(leaf, ref) != offset)
939 return 0;
940 return 1;
941}
942
943static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
944 struct btrfs_root *root,
945 struct btrfs_path *path,
946 u64 bytenr, u64 parent,
947 u64 root_objectid,
948 u64 owner, u64 offset)
949{
950 struct btrfs_key key;
951 struct btrfs_extent_data_ref *ref;
952 struct extent_buffer *leaf;
953 u32 nritems;
954 int ret;
955 int recow;
956 int err = -ENOENT;
957
958 key.objectid = bytenr;
959 if (parent) {
960 key.type = BTRFS_SHARED_DATA_REF_KEY;
961 key.offset = parent;
962 } else {
963 key.type = BTRFS_EXTENT_DATA_REF_KEY;
964 key.offset = hash_extent_data_ref(root_objectid,
965 owner, offset);
966 }
967again:
968 recow = 0;
969 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
970 if (ret < 0) {
971 err = ret;
972 goto fail;
973 }
974
975 if (parent) {
976 if (!ret)
977 return 0;
978#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
979 key.type = BTRFS_EXTENT_REF_V0_KEY;
980 btrfs_release_path(root, path);
981 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
982 if (ret < 0) {
983 err = ret;
984 goto fail;
985 }
986 if (!ret)
987 return 0;
988#endif
989 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400990 }
991
992 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400993 nritems = btrfs_header_nritems(leaf);
994 while (1) {
995 if (path->slots[0] >= nritems) {
996 ret = btrfs_next_leaf(root, path);
997 if (ret < 0)
998 err = ret;
999 if (ret)
1000 goto fail;
1001
1002 leaf = path->nodes[0];
1003 nritems = btrfs_header_nritems(leaf);
1004 recow = 1;
1005 }
1006
1007 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1008 if (key.objectid != bytenr ||
1009 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1010 goto fail;
1011
1012 ref = btrfs_item_ptr(leaf, path->slots[0],
1013 struct btrfs_extent_data_ref);
1014
1015 if (match_extent_data_ref(leaf, ref, root_objectid,
1016 owner, offset)) {
1017 if (recow) {
1018 btrfs_release_path(root, path);
1019 goto again;
1020 }
1021 err = 0;
1022 break;
1023 }
1024 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001025 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001026fail:
1027 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001028}
1029
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001030static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1031 struct btrfs_root *root,
1032 struct btrfs_path *path,
1033 u64 bytenr, u64 parent,
1034 u64 root_objectid, u64 owner,
1035 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001036{
1037 struct btrfs_key key;
1038 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001039 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001040 u32 num_refs;
1041 int ret;
1042
1043 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044 if (parent) {
1045 key.type = BTRFS_SHARED_DATA_REF_KEY;
1046 key.offset = parent;
1047 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001048 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001049 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1050 key.offset = hash_extent_data_ref(root_objectid,
1051 owner, offset);
1052 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001053 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001054
1055 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1056 if (ret && ret != -EEXIST)
1057 goto fail;
1058
1059 leaf = path->nodes[0];
1060 if (parent) {
1061 struct btrfs_shared_data_ref *ref;
1062 ref = btrfs_item_ptr(leaf, path->slots[0],
1063 struct btrfs_shared_data_ref);
1064 if (ret == 0) {
1065 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1066 } else {
1067 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1068 num_refs += refs_to_add;
1069 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1070 }
1071 } else {
1072 struct btrfs_extent_data_ref *ref;
1073 while (ret == -EEXIST) {
1074 ref = btrfs_item_ptr(leaf, path->slots[0],
1075 struct btrfs_extent_data_ref);
1076 if (match_extent_data_ref(leaf, ref, root_objectid,
1077 owner, offset))
1078 break;
1079 btrfs_release_path(root, path);
1080 key.offset++;
1081 ret = btrfs_insert_empty_item(trans, root, path, &key,
1082 size);
1083 if (ret && ret != -EEXIST)
1084 goto fail;
1085
1086 leaf = path->nodes[0];
1087 }
1088 ref = btrfs_item_ptr(leaf, path->slots[0],
1089 struct btrfs_extent_data_ref);
1090 if (ret == 0) {
1091 btrfs_set_extent_data_ref_root(leaf, ref,
1092 root_objectid);
1093 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1094 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1095 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1096 } else {
1097 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1098 num_refs += refs_to_add;
1099 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1100 }
1101 }
1102 btrfs_mark_buffer_dirty(leaf);
1103 ret = 0;
1104fail:
Chris Mason7bb86312007-12-11 09:25:06 -05001105 btrfs_release_path(root, path);
1106 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001107}
1108
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001109static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1110 struct btrfs_root *root,
1111 struct btrfs_path *path,
1112 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001113{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001114 struct btrfs_key key;
1115 struct btrfs_extent_data_ref *ref1 = NULL;
1116 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001117 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001118 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001119 int ret = 0;
1120
1121 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001122 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1123
1124 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1125 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1126 struct btrfs_extent_data_ref);
1127 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1128 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1129 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1130 struct btrfs_shared_data_ref);
1131 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1132#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1133 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1134 struct btrfs_extent_ref_v0 *ref0;
1135 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1136 struct btrfs_extent_ref_v0);
1137 num_refs = btrfs_ref_count_v0(leaf, ref0);
1138#endif
1139 } else {
1140 BUG();
1141 }
1142
Chris Mason56bec292009-03-13 10:10:06 -04001143 BUG_ON(num_refs < refs_to_drop);
1144 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001145
Zheng Yan31840ae2008-09-23 13:14:14 -04001146 if (num_refs == 0) {
1147 ret = btrfs_del_item(trans, root, path);
1148 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001149 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1150 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1151 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1152 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1153#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1154 else {
1155 struct btrfs_extent_ref_v0 *ref0;
1156 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1157 struct btrfs_extent_ref_v0);
1158 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1159 }
1160#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001161 btrfs_mark_buffer_dirty(leaf);
1162 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001163 return ret;
1164}
1165
1166static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1167 struct btrfs_path *path,
1168 struct btrfs_extent_inline_ref *iref)
1169{
1170 struct btrfs_key key;
1171 struct extent_buffer *leaf;
1172 struct btrfs_extent_data_ref *ref1;
1173 struct btrfs_shared_data_ref *ref2;
1174 u32 num_refs = 0;
1175
1176 leaf = path->nodes[0];
1177 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1178 if (iref) {
1179 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1180 BTRFS_EXTENT_DATA_REF_KEY) {
1181 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1182 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1183 } else {
1184 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1185 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1186 }
1187 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1188 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1189 struct btrfs_extent_data_ref);
1190 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1191 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1192 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1193 struct btrfs_shared_data_ref);
1194 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1195#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1196 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1197 struct btrfs_extent_ref_v0 *ref0;
1198 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1199 struct btrfs_extent_ref_v0);
1200 num_refs = btrfs_ref_count_v0(leaf, ref0);
1201#endif
1202 } else {
1203 WARN_ON(1);
1204 }
1205 return num_refs;
1206}
1207
1208static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1209 struct btrfs_root *root,
1210 struct btrfs_path *path,
1211 u64 bytenr, u64 parent,
1212 u64 root_objectid)
1213{
1214 struct btrfs_key key;
1215 int ret;
1216
1217 key.objectid = bytenr;
1218 if (parent) {
1219 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1220 key.offset = parent;
1221 } else {
1222 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1223 key.offset = root_objectid;
1224 }
1225
1226 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1227 if (ret > 0)
1228 ret = -ENOENT;
1229#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1230 if (ret == -ENOENT && parent) {
1231 btrfs_release_path(root, path);
1232 key.type = BTRFS_EXTENT_REF_V0_KEY;
1233 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1234 if (ret > 0)
1235 ret = -ENOENT;
1236 }
1237#endif
1238 return ret;
1239}
1240
1241static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1242 struct btrfs_root *root,
1243 struct btrfs_path *path,
1244 u64 bytenr, u64 parent,
1245 u64 root_objectid)
1246{
1247 struct btrfs_key key;
1248 int ret;
1249
1250 key.objectid = bytenr;
1251 if (parent) {
1252 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1253 key.offset = parent;
1254 } else {
1255 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1256 key.offset = root_objectid;
1257 }
1258
1259 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001260 btrfs_release_path(root, path);
1261 return ret;
1262}
1263
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001264static inline int extent_ref_type(u64 parent, u64 owner)
1265{
1266 int type;
1267 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1268 if (parent > 0)
1269 type = BTRFS_SHARED_BLOCK_REF_KEY;
1270 else
1271 type = BTRFS_TREE_BLOCK_REF_KEY;
1272 } else {
1273 if (parent > 0)
1274 type = BTRFS_SHARED_DATA_REF_KEY;
1275 else
1276 type = BTRFS_EXTENT_DATA_REF_KEY;
1277 }
1278 return type;
1279}
1280
Yan Zheng2c47e6052009-06-27 21:07:35 -04001281static int find_next_key(struct btrfs_path *path, int level,
1282 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001283
1284{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001285 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001286 if (!path->nodes[level])
1287 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001288 if (path->slots[level] + 1 >=
1289 btrfs_header_nritems(path->nodes[level]))
1290 continue;
1291 if (level == 0)
1292 btrfs_item_key_to_cpu(path->nodes[level], key,
1293 path->slots[level] + 1);
1294 else
1295 btrfs_node_key_to_cpu(path->nodes[level], key,
1296 path->slots[level] + 1);
1297 return 0;
1298 }
1299 return 1;
1300}
1301
1302/*
1303 * look for inline back ref. if back ref is found, *ref_ret is set
1304 * to the address of inline back ref, and 0 is returned.
1305 *
1306 * if back ref isn't found, *ref_ret is set to the address where it
1307 * should be inserted, and -ENOENT is returned.
1308 *
1309 * if insert is true and there are too many inline back refs, the path
1310 * points to the extent item, and -EAGAIN is returned.
1311 *
1312 * NOTE: inline back refs are ordered in the same way that back ref
1313 * items in the tree are ordered.
1314 */
1315static noinline_for_stack
1316int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1317 struct btrfs_root *root,
1318 struct btrfs_path *path,
1319 struct btrfs_extent_inline_ref **ref_ret,
1320 u64 bytenr, u64 num_bytes,
1321 u64 parent, u64 root_objectid,
1322 u64 owner, u64 offset, int insert)
1323{
1324 struct btrfs_key key;
1325 struct extent_buffer *leaf;
1326 struct btrfs_extent_item *ei;
1327 struct btrfs_extent_inline_ref *iref;
1328 u64 flags;
1329 u64 item_size;
1330 unsigned long ptr;
1331 unsigned long end;
1332 int extra_size;
1333 int type;
1334 int want;
1335 int ret;
1336 int err = 0;
1337
1338 key.objectid = bytenr;
1339 key.type = BTRFS_EXTENT_ITEM_KEY;
1340 key.offset = num_bytes;
1341
1342 want = extent_ref_type(parent, owner);
1343 if (insert) {
1344 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001345 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001346 } else
1347 extra_size = -1;
1348 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1349 if (ret < 0) {
1350 err = ret;
1351 goto out;
1352 }
1353 BUG_ON(ret);
1354
1355 leaf = path->nodes[0];
1356 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1357#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1358 if (item_size < sizeof(*ei)) {
1359 if (!insert) {
1360 err = -ENOENT;
1361 goto out;
1362 }
1363 ret = convert_extent_item_v0(trans, root, path, owner,
1364 extra_size);
1365 if (ret < 0) {
1366 err = ret;
1367 goto out;
1368 }
1369 leaf = path->nodes[0];
1370 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1371 }
1372#endif
1373 BUG_ON(item_size < sizeof(*ei));
1374
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001375 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1376 flags = btrfs_extent_flags(leaf, ei);
1377
1378 ptr = (unsigned long)(ei + 1);
1379 end = (unsigned long)ei + item_size;
1380
1381 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1382 ptr += sizeof(struct btrfs_tree_block_info);
1383 BUG_ON(ptr > end);
1384 } else {
1385 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1386 }
1387
1388 err = -ENOENT;
1389 while (1) {
1390 if (ptr >= end) {
1391 WARN_ON(ptr > end);
1392 break;
1393 }
1394 iref = (struct btrfs_extent_inline_ref *)ptr;
1395 type = btrfs_extent_inline_ref_type(leaf, iref);
1396 if (want < type)
1397 break;
1398 if (want > type) {
1399 ptr += btrfs_extent_inline_ref_size(type);
1400 continue;
1401 }
1402
1403 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1404 struct btrfs_extent_data_ref *dref;
1405 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1406 if (match_extent_data_ref(leaf, dref, root_objectid,
1407 owner, offset)) {
1408 err = 0;
1409 break;
1410 }
1411 if (hash_extent_data_ref_item(leaf, dref) <
1412 hash_extent_data_ref(root_objectid, owner, offset))
1413 break;
1414 } else {
1415 u64 ref_offset;
1416 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1417 if (parent > 0) {
1418 if (parent == ref_offset) {
1419 err = 0;
1420 break;
1421 }
1422 if (ref_offset < parent)
1423 break;
1424 } else {
1425 if (root_objectid == ref_offset) {
1426 err = 0;
1427 break;
1428 }
1429 if (ref_offset < root_objectid)
1430 break;
1431 }
1432 }
1433 ptr += btrfs_extent_inline_ref_size(type);
1434 }
1435 if (err == -ENOENT && insert) {
1436 if (item_size + extra_size >=
1437 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1438 err = -EAGAIN;
1439 goto out;
1440 }
1441 /*
1442 * To add new inline back ref, we have to make sure
1443 * there is no corresponding back ref item.
1444 * For simplicity, we just do not add new inline back
1445 * ref if there is any kind of item for this block
1446 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001447 if (find_next_key(path, 0, &key) == 0 &&
1448 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001449 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001450 err = -EAGAIN;
1451 goto out;
1452 }
1453 }
1454 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1455out:
Yan Zheng85d41982009-06-11 08:51:10 -04001456 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001457 path->keep_locks = 0;
1458 btrfs_unlock_up_safe(path, 1);
1459 }
1460 return err;
1461}
1462
1463/*
1464 * helper to add new inline back ref
1465 */
1466static noinline_for_stack
1467int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1468 struct btrfs_root *root,
1469 struct btrfs_path *path,
1470 struct btrfs_extent_inline_ref *iref,
1471 u64 parent, u64 root_objectid,
1472 u64 owner, u64 offset, int refs_to_add,
1473 struct btrfs_delayed_extent_op *extent_op)
1474{
1475 struct extent_buffer *leaf;
1476 struct btrfs_extent_item *ei;
1477 unsigned long ptr;
1478 unsigned long end;
1479 unsigned long item_offset;
1480 u64 refs;
1481 int size;
1482 int type;
1483 int ret;
1484
1485 leaf = path->nodes[0];
1486 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1487 item_offset = (unsigned long)iref - (unsigned long)ei;
1488
1489 type = extent_ref_type(parent, owner);
1490 size = btrfs_extent_inline_ref_size(type);
1491
1492 ret = btrfs_extend_item(trans, root, path, size);
1493 BUG_ON(ret);
1494
1495 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1496 refs = btrfs_extent_refs(leaf, ei);
1497 refs += refs_to_add;
1498 btrfs_set_extent_refs(leaf, ei, refs);
1499 if (extent_op)
1500 __run_delayed_extent_op(extent_op, leaf, ei);
1501
1502 ptr = (unsigned long)ei + item_offset;
1503 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1504 if (ptr < end - size)
1505 memmove_extent_buffer(leaf, ptr + size, ptr,
1506 end - size - ptr);
1507
1508 iref = (struct btrfs_extent_inline_ref *)ptr;
1509 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1510 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1511 struct btrfs_extent_data_ref *dref;
1512 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1513 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1514 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1515 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1516 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1517 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1518 struct btrfs_shared_data_ref *sref;
1519 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1520 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1521 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1522 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1523 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1524 } else {
1525 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1526 }
1527 btrfs_mark_buffer_dirty(leaf);
1528 return 0;
1529}
1530
1531static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1532 struct btrfs_root *root,
1533 struct btrfs_path *path,
1534 struct btrfs_extent_inline_ref **ref_ret,
1535 u64 bytenr, u64 num_bytes, u64 parent,
1536 u64 root_objectid, u64 owner, u64 offset)
1537{
1538 int ret;
1539
1540 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1541 bytenr, num_bytes, parent,
1542 root_objectid, owner, offset, 0);
1543 if (ret != -ENOENT)
1544 return ret;
1545
1546 btrfs_release_path(root, path);
1547 *ref_ret = NULL;
1548
1549 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1550 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1551 root_objectid);
1552 } else {
1553 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1554 root_objectid, owner, offset);
1555 }
1556 return ret;
1557}
1558
1559/*
1560 * helper to update/remove inline back ref
1561 */
1562static noinline_for_stack
1563int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1564 struct btrfs_root *root,
1565 struct btrfs_path *path,
1566 struct btrfs_extent_inline_ref *iref,
1567 int refs_to_mod,
1568 struct btrfs_delayed_extent_op *extent_op)
1569{
1570 struct extent_buffer *leaf;
1571 struct btrfs_extent_item *ei;
1572 struct btrfs_extent_data_ref *dref = NULL;
1573 struct btrfs_shared_data_ref *sref = NULL;
1574 unsigned long ptr;
1575 unsigned long end;
1576 u32 item_size;
1577 int size;
1578 int type;
1579 int ret;
1580 u64 refs;
1581
1582 leaf = path->nodes[0];
1583 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1584 refs = btrfs_extent_refs(leaf, ei);
1585 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1586 refs += refs_to_mod;
1587 btrfs_set_extent_refs(leaf, ei, refs);
1588 if (extent_op)
1589 __run_delayed_extent_op(extent_op, leaf, ei);
1590
1591 type = btrfs_extent_inline_ref_type(leaf, iref);
1592
1593 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1594 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1595 refs = btrfs_extent_data_ref_count(leaf, dref);
1596 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1597 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1598 refs = btrfs_shared_data_ref_count(leaf, sref);
1599 } else {
1600 refs = 1;
1601 BUG_ON(refs_to_mod != -1);
1602 }
1603
1604 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1605 refs += refs_to_mod;
1606
1607 if (refs > 0) {
1608 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1609 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1610 else
1611 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1612 } else {
1613 size = btrfs_extent_inline_ref_size(type);
1614 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1615 ptr = (unsigned long)iref;
1616 end = (unsigned long)ei + item_size;
1617 if (ptr + size < end)
1618 memmove_extent_buffer(leaf, ptr, ptr + size,
1619 end - ptr - size);
1620 item_size -= size;
1621 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1622 BUG_ON(ret);
1623 }
1624 btrfs_mark_buffer_dirty(leaf);
1625 return 0;
1626}
1627
1628static noinline_for_stack
1629int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1630 struct btrfs_root *root,
1631 struct btrfs_path *path,
1632 u64 bytenr, u64 num_bytes, u64 parent,
1633 u64 root_objectid, u64 owner,
1634 u64 offset, int refs_to_add,
1635 struct btrfs_delayed_extent_op *extent_op)
1636{
1637 struct btrfs_extent_inline_ref *iref;
1638 int ret;
1639
1640 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1641 bytenr, num_bytes, parent,
1642 root_objectid, owner, offset, 1);
1643 if (ret == 0) {
1644 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1645 ret = update_inline_extent_backref(trans, root, path, iref,
1646 refs_to_add, extent_op);
1647 } else if (ret == -ENOENT) {
1648 ret = setup_inline_extent_backref(trans, root, path, iref,
1649 parent, root_objectid,
1650 owner, offset, refs_to_add,
1651 extent_op);
1652 }
1653 return ret;
1654}
1655
1656static int insert_extent_backref(struct btrfs_trans_handle *trans,
1657 struct btrfs_root *root,
1658 struct btrfs_path *path,
1659 u64 bytenr, u64 parent, u64 root_objectid,
1660 u64 owner, u64 offset, int refs_to_add)
1661{
1662 int ret;
1663 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1664 BUG_ON(refs_to_add != 1);
1665 ret = insert_tree_block_ref(trans, root, path, bytenr,
1666 parent, root_objectid);
1667 } else {
1668 ret = insert_extent_data_ref(trans, root, path, bytenr,
1669 parent, root_objectid,
1670 owner, offset, refs_to_add);
1671 }
1672 return ret;
1673}
1674
1675static int remove_extent_backref(struct btrfs_trans_handle *trans,
1676 struct btrfs_root *root,
1677 struct btrfs_path *path,
1678 struct btrfs_extent_inline_ref *iref,
1679 int refs_to_drop, int is_data)
1680{
1681 int ret;
1682
1683 BUG_ON(!is_data && refs_to_drop != 1);
1684 if (iref) {
1685 ret = update_inline_extent_backref(trans, root, path, iref,
1686 -refs_to_drop, NULL);
1687 } else if (is_data) {
1688 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1689 } else {
1690 ret = btrfs_del_item(trans, root, path);
1691 }
1692 return ret;
1693}
1694
Chris Mason15916de2008-11-19 21:17:22 -05001695static void btrfs_issue_discard(struct block_device *bdev,
1696 u64 start, u64 len)
1697{
Christoph Hellwig746cd1e2009-09-12 07:35:43 +02001698 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
1699 DISCARD_FL_BARRIER);
Chris Mason15916de2008-11-19 21:17:22 -05001700}
Chris Mason15916de2008-11-19 21:17:22 -05001701
Liu Hui1f3c79a2009-01-05 15:57:51 -05001702static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1703 u64 num_bytes)
1704{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001705 int ret;
1706 u64 map_length = num_bytes;
1707 struct btrfs_multi_bio *multi = NULL;
1708
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001709 if (!btrfs_test_opt(root, DISCARD))
1710 return 0;
1711
Liu Hui1f3c79a2009-01-05 15:57:51 -05001712 /* Tell the block device(s) that the sectors can be discarded */
1713 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1714 bytenr, &map_length, &multi, 0);
1715 if (!ret) {
1716 struct btrfs_bio_stripe *stripe = multi->stripes;
1717 int i;
1718
1719 if (map_length > num_bytes)
1720 map_length = num_bytes;
1721
1722 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1723 btrfs_issue_discard(stripe->dev->bdev,
1724 stripe->physical,
1725 map_length);
1726 }
1727 kfree(multi);
1728 }
1729
1730 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001731}
1732
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001733int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1734 struct btrfs_root *root,
1735 u64 bytenr, u64 num_bytes, u64 parent,
1736 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001737{
1738 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001739 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1740 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001741
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001742 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1743 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1744 parent, root_objectid, (int)owner,
1745 BTRFS_ADD_DELAYED_REF, NULL);
1746 } else {
1747 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1748 parent, root_objectid, owner, offset,
1749 BTRFS_ADD_DELAYED_REF, NULL);
1750 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001751 return ret;
1752}
1753
Chris Mason925baed2008-06-25 16:01:30 -04001754static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001755 struct btrfs_root *root,
1756 u64 bytenr, u64 num_bytes,
1757 u64 parent, u64 root_objectid,
1758 u64 owner, u64 offset, int refs_to_add,
1759 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001760{
Chris Mason5caf2a02007-04-02 11:20:42 -04001761 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001762 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001763 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001764 u64 refs;
1765 int ret;
1766 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001767
Chris Mason5caf2a02007-04-02 11:20:42 -04001768 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001769 if (!path)
1770 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001771
Chris Mason3c12ac72008-04-21 12:01:38 -04001772 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001773 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001774 /* this will setup the path even if it fails to insert the back ref */
1775 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1776 path, bytenr, num_bytes, parent,
1777 root_objectid, owner, offset,
1778 refs_to_add, extent_op);
1779 if (ret == 0)
1780 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001781
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001782 if (ret != -EAGAIN) {
1783 err = ret;
1784 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001785 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001786
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001787 leaf = path->nodes[0];
1788 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1789 refs = btrfs_extent_refs(leaf, item);
1790 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1791 if (extent_op)
1792 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001793
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001794 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001795 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001796
Chris Mason3c12ac72008-04-21 12:01:38 -04001797 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001798 path->leave_spinning = 1;
1799
Chris Mason56bec292009-03-13 10:10:06 -04001800 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001801 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001802 path, bytenr, parent, root_objectid,
1803 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001804 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001805out:
Chris Mason74493f72007-12-11 09:25:06 -05001806 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001807 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001808}
1809
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001810static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1811 struct btrfs_root *root,
1812 struct btrfs_delayed_ref_node *node,
1813 struct btrfs_delayed_extent_op *extent_op,
1814 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001815{
Chris Mason56bec292009-03-13 10:10:06 -04001816 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001817 struct btrfs_delayed_data_ref *ref;
1818 struct btrfs_key ins;
1819 u64 parent = 0;
1820 u64 ref_root = 0;
1821 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001822
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001823 ins.objectid = node->bytenr;
1824 ins.offset = node->num_bytes;
1825 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001826
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001827 ref = btrfs_delayed_node_to_data_ref(node);
1828 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1829 parent = ref->parent;
1830 else
1831 ref_root = ref->root;
1832
1833 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1834 if (extent_op) {
1835 BUG_ON(extent_op->update_key);
1836 flags |= extent_op->flags_to_set;
1837 }
1838 ret = alloc_reserved_file_extent(trans, root,
1839 parent, ref_root, flags,
1840 ref->objectid, ref->offset,
1841 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001842 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1843 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1844 node->num_bytes, parent,
1845 ref_root, ref->objectid,
1846 ref->offset, node->ref_mod,
1847 extent_op);
1848 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1849 ret = __btrfs_free_extent(trans, root, node->bytenr,
1850 node->num_bytes, parent,
1851 ref_root, ref->objectid,
1852 ref->offset, node->ref_mod,
1853 extent_op);
1854 } else {
1855 BUG();
1856 }
Chris Mason56bec292009-03-13 10:10:06 -04001857 return ret;
1858}
1859
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001860static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1861 struct extent_buffer *leaf,
1862 struct btrfs_extent_item *ei)
1863{
1864 u64 flags = btrfs_extent_flags(leaf, ei);
1865 if (extent_op->update_flags) {
1866 flags |= extent_op->flags_to_set;
1867 btrfs_set_extent_flags(leaf, ei, flags);
1868 }
1869
1870 if (extent_op->update_key) {
1871 struct btrfs_tree_block_info *bi;
1872 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1873 bi = (struct btrfs_tree_block_info *)(ei + 1);
1874 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1875 }
1876}
1877
1878static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1879 struct btrfs_root *root,
1880 struct btrfs_delayed_ref_node *node,
1881 struct btrfs_delayed_extent_op *extent_op)
1882{
1883 struct btrfs_key key;
1884 struct btrfs_path *path;
1885 struct btrfs_extent_item *ei;
1886 struct extent_buffer *leaf;
1887 u32 item_size;
1888 int ret;
1889 int err = 0;
1890
1891 path = btrfs_alloc_path();
1892 if (!path)
1893 return -ENOMEM;
1894
1895 key.objectid = node->bytenr;
1896 key.type = BTRFS_EXTENT_ITEM_KEY;
1897 key.offset = node->num_bytes;
1898
1899 path->reada = 1;
1900 path->leave_spinning = 1;
1901 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1902 path, 0, 1);
1903 if (ret < 0) {
1904 err = ret;
1905 goto out;
1906 }
1907 if (ret > 0) {
1908 err = -EIO;
1909 goto out;
1910 }
1911
1912 leaf = path->nodes[0];
1913 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1914#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1915 if (item_size < sizeof(*ei)) {
1916 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1917 path, (u64)-1, 0);
1918 if (ret < 0) {
1919 err = ret;
1920 goto out;
1921 }
1922 leaf = path->nodes[0];
1923 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1924 }
1925#endif
1926 BUG_ON(item_size < sizeof(*ei));
1927 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1928 __run_delayed_extent_op(extent_op, leaf, ei);
1929
1930 btrfs_mark_buffer_dirty(leaf);
1931out:
1932 btrfs_free_path(path);
1933 return err;
1934}
1935
1936static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1937 struct btrfs_root *root,
1938 struct btrfs_delayed_ref_node *node,
1939 struct btrfs_delayed_extent_op *extent_op,
1940 int insert_reserved)
1941{
1942 int ret = 0;
1943 struct btrfs_delayed_tree_ref *ref;
1944 struct btrfs_key ins;
1945 u64 parent = 0;
1946 u64 ref_root = 0;
1947
1948 ins.objectid = node->bytenr;
1949 ins.offset = node->num_bytes;
1950 ins.type = BTRFS_EXTENT_ITEM_KEY;
1951
1952 ref = btrfs_delayed_node_to_tree_ref(node);
1953 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1954 parent = ref->parent;
1955 else
1956 ref_root = ref->root;
1957
1958 BUG_ON(node->ref_mod != 1);
1959 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1960 BUG_ON(!extent_op || !extent_op->update_flags ||
1961 !extent_op->update_key);
1962 ret = alloc_reserved_tree_block(trans, root,
1963 parent, ref_root,
1964 extent_op->flags_to_set,
1965 &extent_op->key,
1966 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001967 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1968 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1969 node->num_bytes, parent, ref_root,
1970 ref->level, 0, 1, extent_op);
1971 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1972 ret = __btrfs_free_extent(trans, root, node->bytenr,
1973 node->num_bytes, parent, ref_root,
1974 ref->level, 0, 1, extent_op);
1975 } else {
1976 BUG();
1977 }
1978 return ret;
1979}
1980
Chris Mason56bec292009-03-13 10:10:06 -04001981/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001982static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1983 struct btrfs_root *root,
1984 struct btrfs_delayed_ref_node *node,
1985 struct btrfs_delayed_extent_op *extent_op,
1986 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001987{
Josef Bacikeb099672009-02-12 09:27:38 -05001988 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001989 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001990 struct btrfs_delayed_ref_head *head;
1991 /*
1992 * we've hit the end of the chain and we were supposed
1993 * to insert this extent into the tree. But, it got
1994 * deleted before we ever needed to insert it, so all
1995 * we have to do is clean up the accounting
1996 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001997 BUG_ON(extent_op);
1998 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001999 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002000 btrfs_pin_extent(root, node->bytenr,
2001 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002002 if (head->is_data) {
2003 ret = btrfs_del_csums(trans, root,
2004 node->bytenr,
2005 node->num_bytes);
2006 BUG_ON(ret);
2007 }
Chris Mason56bec292009-03-13 10:10:06 -04002008 }
Chris Mason56bec292009-03-13 10:10:06 -04002009 mutex_unlock(&head->mutex);
2010 return 0;
2011 }
Josef Bacikeb099672009-02-12 09:27:38 -05002012
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002013 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2014 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2015 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2016 insert_reserved);
2017 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2018 node->type == BTRFS_SHARED_DATA_REF_KEY)
2019 ret = run_delayed_data_ref(trans, root, node, extent_op,
2020 insert_reserved);
2021 else
2022 BUG();
2023 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002024}
2025
Chris Mason56bec292009-03-13 10:10:06 -04002026static noinline struct btrfs_delayed_ref_node *
2027select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002028{
Chris Mason56bec292009-03-13 10:10:06 -04002029 struct rb_node *node;
2030 struct btrfs_delayed_ref_node *ref;
2031 int action = BTRFS_ADD_DELAYED_REF;
2032again:
2033 /*
2034 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2035 * this prevents ref count from going down to zero when
2036 * there still are pending delayed ref.
2037 */
2038 node = rb_prev(&head->node.rb_node);
2039 while (1) {
2040 if (!node)
2041 break;
2042 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2043 rb_node);
2044 if (ref->bytenr != head->node.bytenr)
2045 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002046 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002047 return ref;
2048 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002049 }
Chris Mason56bec292009-03-13 10:10:06 -04002050 if (action == BTRFS_ADD_DELAYED_REF) {
2051 action = BTRFS_DROP_DELAYED_REF;
2052 goto again;
2053 }
2054 return NULL;
2055}
2056
Chris Masonc3e69d52009-03-13 10:17:05 -04002057static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2058 struct btrfs_root *root,
2059 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002060{
Chris Mason56bec292009-03-13 10:10:06 -04002061 struct btrfs_delayed_ref_root *delayed_refs;
2062 struct btrfs_delayed_ref_node *ref;
2063 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002065 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002066 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002067 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002068
2069 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002070 while (1) {
2071 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002072 /* pick a new head ref from the cluster list */
2073 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002074 break;
Chris Mason56bec292009-03-13 10:10:06 -04002075
Chris Masonc3e69d52009-03-13 10:17:05 -04002076 locked_ref = list_entry(cluster->next,
2077 struct btrfs_delayed_ref_head, cluster);
2078
2079 /* grab the lock that says we are going to process
2080 * all the refs for this head */
2081 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2082
2083 /*
2084 * we may have dropped the spin lock to get the head
2085 * mutex lock, and that might have given someone else
2086 * time to free the head. If that's true, it has been
2087 * removed from our list and we can move on.
2088 */
2089 if (ret == -EAGAIN) {
2090 locked_ref = NULL;
2091 count++;
2092 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002093 }
2094 }
2095
2096 /*
2097 * record the must insert reserved flag before we
2098 * drop the spin lock.
2099 */
2100 must_insert_reserved = locked_ref->must_insert_reserved;
2101 locked_ref->must_insert_reserved = 0;
2102
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002103 extent_op = locked_ref->extent_op;
2104 locked_ref->extent_op = NULL;
2105
Chris Mason56bec292009-03-13 10:10:06 -04002106 /*
2107 * locked_ref is the head node, so we have to go one
2108 * node back for any delayed ref updates
2109 */
Chris Mason56bec292009-03-13 10:10:06 -04002110 ref = select_delayed_ref(locked_ref);
2111 if (!ref) {
2112 /* All delayed refs have been processed, Go ahead
2113 * and send the head node to run_one_delayed_ref,
2114 * so that any accounting fixes can happen
2115 */
2116 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002117
2118 if (extent_op && must_insert_reserved) {
2119 kfree(extent_op);
2120 extent_op = NULL;
2121 }
2122
2123 if (extent_op) {
2124 spin_unlock(&delayed_refs->lock);
2125
2126 ret = run_delayed_extent_op(trans, root,
2127 ref, extent_op);
2128 BUG_ON(ret);
2129 kfree(extent_op);
2130
2131 cond_resched();
2132 spin_lock(&delayed_refs->lock);
2133 continue;
2134 }
2135
Chris Masonc3e69d52009-03-13 10:17:05 -04002136 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002137 locked_ref = NULL;
2138 }
2139
2140 ref->in_tree = 0;
2141 rb_erase(&ref->rb_node, &delayed_refs->root);
2142 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002143
Chris Mason56bec292009-03-13 10:10:06 -04002144 spin_unlock(&delayed_refs->lock);
2145
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002146 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002147 must_insert_reserved);
2148 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002149
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002150 btrfs_put_delayed_ref(ref);
2151 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002152 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002153
Chris Mason1887be62009-03-13 10:11:24 -04002154 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002155 spin_lock(&delayed_refs->lock);
2156 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002157 return count;
2158}
2159
2160/*
2161 * this starts processing the delayed reference count updates and
2162 * extent insertions we have queued up so far. count can be
2163 * 0, which means to process everything in the tree at the start
2164 * of the run (but not newly added entries), or it can be some target
2165 * number you'd like to process.
2166 */
2167int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2168 struct btrfs_root *root, unsigned long count)
2169{
2170 struct rb_node *node;
2171 struct btrfs_delayed_ref_root *delayed_refs;
2172 struct btrfs_delayed_ref_node *ref;
2173 struct list_head cluster;
2174 int ret;
2175 int run_all = count == (unsigned long)-1;
2176 int run_most = 0;
2177
2178 if (root == root->fs_info->extent_root)
2179 root = root->fs_info->tree_root;
2180
2181 delayed_refs = &trans->transaction->delayed_refs;
2182 INIT_LIST_HEAD(&cluster);
2183again:
2184 spin_lock(&delayed_refs->lock);
2185 if (count == 0) {
2186 count = delayed_refs->num_entries * 2;
2187 run_most = 1;
2188 }
2189 while (1) {
2190 if (!(run_all || run_most) &&
2191 delayed_refs->num_heads_ready < 64)
2192 break;
2193
2194 /*
2195 * go find something we can process in the rbtree. We start at
2196 * the beginning of the tree, and then build a cluster
2197 * of refs to process starting at the first one we are able to
2198 * lock
2199 */
2200 ret = btrfs_find_ref_cluster(trans, &cluster,
2201 delayed_refs->run_delayed_start);
2202 if (ret)
2203 break;
2204
2205 ret = run_clustered_refs(trans, root, &cluster);
2206 BUG_ON(ret < 0);
2207
2208 count -= min_t(unsigned long, ret, count);
2209
2210 if (count == 0)
2211 break;
2212 }
2213
Chris Mason56bec292009-03-13 10:10:06 -04002214 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002215 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002216 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002217 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002218 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002219
2220 while (node) {
2221 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2222 rb_node);
2223 if (btrfs_delayed_ref_is_head(ref)) {
2224 struct btrfs_delayed_ref_head *head;
2225
2226 head = btrfs_delayed_node_to_head(ref);
2227 atomic_inc(&ref->refs);
2228
2229 spin_unlock(&delayed_refs->lock);
2230 mutex_lock(&head->mutex);
2231 mutex_unlock(&head->mutex);
2232
2233 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002234 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002235 goto again;
2236 }
2237 node = rb_next(node);
2238 }
2239 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002240 schedule_timeout(1);
2241 goto again;
2242 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002243out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002244 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002245 return 0;
2246}
2247
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002248int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2249 struct btrfs_root *root,
2250 u64 bytenr, u64 num_bytes, u64 flags,
2251 int is_data)
2252{
2253 struct btrfs_delayed_extent_op *extent_op;
2254 int ret;
2255
2256 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2257 if (!extent_op)
2258 return -ENOMEM;
2259
2260 extent_op->flags_to_set = flags;
2261 extent_op->update_flags = 1;
2262 extent_op->update_key = 0;
2263 extent_op->is_data = is_data ? 1 : 0;
2264
2265 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2266 if (ret)
2267 kfree(extent_op);
2268 return ret;
2269}
2270
2271static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2272 struct btrfs_root *root,
2273 struct btrfs_path *path,
2274 u64 objectid, u64 offset, u64 bytenr)
2275{
2276 struct btrfs_delayed_ref_head *head;
2277 struct btrfs_delayed_ref_node *ref;
2278 struct btrfs_delayed_data_ref *data_ref;
2279 struct btrfs_delayed_ref_root *delayed_refs;
2280 struct rb_node *node;
2281 int ret = 0;
2282
2283 ret = -ENOENT;
2284 delayed_refs = &trans->transaction->delayed_refs;
2285 spin_lock(&delayed_refs->lock);
2286 head = btrfs_find_delayed_ref_head(trans, bytenr);
2287 if (!head)
2288 goto out;
2289
2290 if (!mutex_trylock(&head->mutex)) {
2291 atomic_inc(&head->node.refs);
2292 spin_unlock(&delayed_refs->lock);
2293
2294 btrfs_release_path(root->fs_info->extent_root, path);
2295
2296 mutex_lock(&head->mutex);
2297 mutex_unlock(&head->mutex);
2298 btrfs_put_delayed_ref(&head->node);
2299 return -EAGAIN;
2300 }
2301
2302 node = rb_prev(&head->node.rb_node);
2303 if (!node)
2304 goto out_unlock;
2305
2306 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2307
2308 if (ref->bytenr != bytenr)
2309 goto out_unlock;
2310
2311 ret = 1;
2312 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2313 goto out_unlock;
2314
2315 data_ref = btrfs_delayed_node_to_data_ref(ref);
2316
2317 node = rb_prev(node);
2318 if (node) {
2319 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2320 if (ref->bytenr == bytenr)
2321 goto out_unlock;
2322 }
2323
2324 if (data_ref->root != root->root_key.objectid ||
2325 data_ref->objectid != objectid || data_ref->offset != offset)
2326 goto out_unlock;
2327
2328 ret = 0;
2329out_unlock:
2330 mutex_unlock(&head->mutex);
2331out:
2332 spin_unlock(&delayed_refs->lock);
2333 return ret;
2334}
2335
2336static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2337 struct btrfs_root *root,
2338 struct btrfs_path *path,
2339 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002340{
2341 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002342 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002343 struct btrfs_extent_data_ref *ref;
2344 struct btrfs_extent_inline_ref *iref;
2345 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002346 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002347 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002348 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002349
Chris Masonbe20aa92007-12-17 20:14:01 -05002350 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002351 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002352 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002353
Chris Masonbe20aa92007-12-17 20:14:01 -05002354 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2355 if (ret < 0)
2356 goto out;
2357 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002358
2359 ret = -ENOENT;
2360 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002361 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002362
Zheng Yan31840ae2008-09-23 13:14:14 -04002363 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002364 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002365 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002366
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002367 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002368 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002369
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002370 ret = 1;
2371 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2372#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2373 if (item_size < sizeof(*ei)) {
2374 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2375 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002376 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002377#endif
2378 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2379
2380 if (item_size != sizeof(*ei) +
2381 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2382 goto out;
2383
2384 if (btrfs_extent_generation(leaf, ei) <=
2385 btrfs_root_last_snapshot(&root->root_item))
2386 goto out;
2387
2388 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2389 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2390 BTRFS_EXTENT_DATA_REF_KEY)
2391 goto out;
2392
2393 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2394 if (btrfs_extent_refs(leaf, ei) !=
2395 btrfs_extent_data_ref_count(leaf, ref) ||
2396 btrfs_extent_data_ref_root(leaf, ref) !=
2397 root->root_key.objectid ||
2398 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2399 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2400 goto out;
2401
Yan Zhengf321e492008-07-30 09:26:11 -04002402 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002403out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002404 return ret;
2405}
2406
2407int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2408 struct btrfs_root *root,
2409 u64 objectid, u64 offset, u64 bytenr)
2410{
2411 struct btrfs_path *path;
2412 int ret;
2413 int ret2;
2414
2415 path = btrfs_alloc_path();
2416 if (!path)
2417 return -ENOENT;
2418
2419 do {
2420 ret = check_committed_ref(trans, root, path, objectid,
2421 offset, bytenr);
2422 if (ret && ret != -ENOENT)
2423 goto out;
2424
2425 ret2 = check_delayed_ref(trans, root, path, objectid,
2426 offset, bytenr);
2427 } while (ret2 == -EAGAIN);
2428
2429 if (ret2 && ret2 != -ENOENT) {
2430 ret = ret2;
2431 goto out;
2432 }
2433
2434 if (ret != -ENOENT || ret2 != -ENOENT)
2435 ret = 0;
2436out:
Yan Zhengf321e492008-07-30 09:26:11 -04002437 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002438 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2439 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002440 return ret;
2441}
2442
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002443#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002444int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2445 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002446{
Chris Mason5f39d392007-10-15 16:14:19 -04002447 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002448 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002449 u64 root_gen;
2450 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002451 int i;
Chris Masondb945352007-10-15 16:15:53 -04002452 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002453 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002454 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002455
Chris Mason3768f362007-03-13 16:47:54 -04002456 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002457 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002458
Zheng Yane4657682008-09-26 10:04:53 -04002459 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2460 shared = 0;
2461 root_gen = root->root_key.offset;
2462 } else {
2463 shared = 1;
2464 root_gen = trans->transid - 1;
2465 }
2466
Chris Masondb945352007-10-15 16:15:53 -04002467 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002468 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002469
Zheng Yan31840ae2008-09-23 13:14:14 -04002470 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002471 struct btrfs_leaf_ref *ref;
2472 struct btrfs_extent_info *info;
2473
Zheng Yan31840ae2008-09-23 13:14:14 -04002474 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002475 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002476 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002477 goto out;
2478 }
2479
Zheng Yane4657682008-09-26 10:04:53 -04002480 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002481 ref->bytenr = buf->start;
2482 ref->owner = btrfs_header_owner(buf);
2483 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002484 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002485 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002486
Zheng Yan31840ae2008-09-23 13:14:14 -04002487 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002488 u64 disk_bytenr;
2489 btrfs_item_key_to_cpu(buf, &key, i);
2490 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2491 continue;
2492 fi = btrfs_item_ptr(buf, i,
2493 struct btrfs_file_extent_item);
2494 if (btrfs_file_extent_type(buf, fi) ==
2495 BTRFS_FILE_EXTENT_INLINE)
2496 continue;
2497 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2498 if (disk_bytenr == 0)
2499 continue;
2500
2501 info->bytenr = disk_bytenr;
2502 info->num_bytes =
2503 btrfs_file_extent_disk_num_bytes(buf, fi);
2504 info->objectid = key.objectid;
2505 info->offset = key.offset;
2506 info++;
2507 }
2508
Zheng Yane4657682008-09-26 10:04:53 -04002509 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002510 if (ret == -EEXIST && shared) {
2511 struct btrfs_leaf_ref *old;
2512 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2513 BUG_ON(!old);
2514 btrfs_remove_leaf_ref(root, old);
2515 btrfs_free_leaf_ref(root, old);
2516 ret = btrfs_add_leaf_ref(root, ref, shared);
2517 }
Yan Zheng31153d82008-07-28 15:32:19 -04002518 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002519 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002520 }
2521out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002522 return ret;
2523}
2524
Chris Masonb7a9f292009-02-04 09:23:45 -05002525/* when a block goes through cow, we update the reference counts of
2526 * everything that block points to. The internal pointers of the block
2527 * can be in just about any order, and it is likely to have clusters of
2528 * things that are close together and clusters of things that are not.
2529 *
2530 * To help reduce the seeks that come with updating all of these reference
2531 * counts, sort them by byte number before actual updates are done.
2532 *
2533 * struct refsort is used to match byte number to slot in the btree block.
2534 * we sort based on the byte number and then use the slot to actually
2535 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002536 *
2537 * struct refsort is smaller than strcut btrfs_item and smaller than
2538 * struct btrfs_key_ptr. Since we're currently limited to the page size
2539 * for a btree block, there's no way for a kmalloc of refsorts for a
2540 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002541 */
2542struct refsort {
2543 u64 bytenr;
2544 u32 slot;
2545};
2546
2547/*
2548 * for passing into sort()
2549 */
2550static int refsort_cmp(const void *a_void, const void *b_void)
2551{
2552 const struct refsort *a = a_void;
2553 const struct refsort *b = b_void;
2554
2555 if (a->bytenr < b->bytenr)
2556 return -1;
2557 if (a->bytenr > b->bytenr)
2558 return 1;
2559 return 0;
2560}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002561#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002562
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002563static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002564 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002565 struct extent_buffer *buf,
2566 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002567{
2568 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002569 u64 num_bytes;
2570 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002571 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002572 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002573 struct btrfs_key key;
2574 struct btrfs_file_extent_item *fi;
2575 int i;
2576 int level;
2577 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002578 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002579 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002580
2581 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002582 nritems = btrfs_header_nritems(buf);
2583 level = btrfs_header_level(buf);
2584
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002585 if (!root->ref_cows && level == 0)
2586 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002587
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002588 if (inc)
2589 process_func = btrfs_inc_extent_ref;
2590 else
2591 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002592
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002593 if (full_backref)
2594 parent = buf->start;
2595 else
2596 parent = 0;
2597
Zheng Yan31840ae2008-09-23 13:14:14 -04002598 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002599 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002600 btrfs_item_key_to_cpu(buf, &key, i);
2601 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002602 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002603 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002604 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002605 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002606 BTRFS_FILE_EXTENT_INLINE)
2607 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002608 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2609 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002610 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002611
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002612 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2613 key.offset -= btrfs_file_extent_offset(buf, fi);
2614 ret = process_func(trans, root, bytenr, num_bytes,
2615 parent, ref_root, key.objectid,
2616 key.offset);
2617 if (ret)
2618 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002619 } else {
2620 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002621 num_bytes = btrfs_level_size(root, level - 1);
2622 ret = process_func(trans, root, bytenr, num_bytes,
2623 parent, ref_root, level - 1, 0);
2624 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002625 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002626 }
2627 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002628 return 0;
2629fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002630 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002631 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002632}
2633
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002634int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2635 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002636{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002637 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2638}
Zheng Yan31840ae2008-09-23 13:14:14 -04002639
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002640int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2641 struct extent_buffer *buf, int full_backref)
2642{
2643 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002644}
2645
Chris Mason9078a3e2007-04-26 16:46:15 -04002646static int write_one_cache_group(struct btrfs_trans_handle *trans,
2647 struct btrfs_root *root,
2648 struct btrfs_path *path,
2649 struct btrfs_block_group_cache *cache)
2650{
2651 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002652 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002653 unsigned long bi;
2654 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002655
Chris Mason9078a3e2007-04-26 16:46:15 -04002656 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002657 if (ret < 0)
2658 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002659 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002660
2661 leaf = path->nodes[0];
2662 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2663 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2664 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002665 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002666fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002667 if (ret)
2668 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002669 return 0;
2670
2671}
2672
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002673static struct btrfs_block_group_cache *
2674next_block_group(struct btrfs_root *root,
2675 struct btrfs_block_group_cache *cache)
2676{
2677 struct rb_node *node;
2678 spin_lock(&root->fs_info->block_group_cache_lock);
2679 node = rb_next(&cache->cache_node);
2680 btrfs_put_block_group(cache);
2681 if (node) {
2682 cache = rb_entry(node, struct btrfs_block_group_cache,
2683 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002684 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002685 } else
2686 cache = NULL;
2687 spin_unlock(&root->fs_info->block_group_cache_lock);
2688 return cache;
2689}
2690
Chris Mason96b51792007-10-15 16:15:19 -04002691int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2692 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002693{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002694 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002695 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002696 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002697 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002698
2699 path = btrfs_alloc_path();
2700 if (!path)
2701 return -ENOMEM;
2702
Chris Masond3977122009-01-05 21:25:51 -05002703 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002704 if (last == 0) {
2705 err = btrfs_run_delayed_refs(trans, root,
2706 (unsigned long)-1);
2707 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002708 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002709
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002710 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2711 while (cache) {
2712 if (cache->dirty)
2713 break;
2714 cache = next_block_group(root, cache);
2715 }
2716 if (!cache) {
2717 if (last == 0)
2718 break;
2719 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002720 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002721 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002722
2723 cache->dirty = 0;
2724 last = cache->key.objectid + cache->key.offset;
2725
2726 err = write_one_cache_group(trans, root, path, cache);
2727 BUG_ON(err);
2728 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002729 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002730
Chris Mason9078a3e2007-04-26 16:46:15 -04002731 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002732 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002733}
2734
Yan Zhengd2fb3432008-12-11 16:30:39 -05002735int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2736{
2737 struct btrfs_block_group_cache *block_group;
2738 int readonly = 0;
2739
2740 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2741 if (!block_group || block_group->ro)
2742 readonly = 1;
2743 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002744 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002745 return readonly;
2746}
2747
Chris Mason593060d2008-03-25 16:50:33 -04002748static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2749 u64 total_bytes, u64 bytes_used,
2750 struct btrfs_space_info **space_info)
2751{
2752 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002753 int i;
2754 int factor;
2755
2756 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2757 BTRFS_BLOCK_GROUP_RAID10))
2758 factor = 2;
2759 else
2760 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002761
2762 found = __find_space_info(info, flags);
2763 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002764 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002765 found->total_bytes += total_bytes;
2766 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002767 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002768 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002769 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002770 *space_info = found;
2771 return 0;
2772 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002773 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002774 if (!found)
2775 return -ENOMEM;
2776
Yan, Zhengb742bb82010-05-16 10:46:24 -04002777 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2778 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04002779 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002780 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04002781 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2782 BTRFS_BLOCK_GROUP_SYSTEM |
2783 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04002784 found->total_bytes = total_bytes;
2785 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002786 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002787 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002788 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002789 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002790 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002791 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002792 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002793 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002794 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002795 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002796 return 0;
2797}
2798
Chris Mason8790d502008-04-03 16:29:03 -04002799static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2800{
2801 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002802 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002803 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002804 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002805 if (extra_flags) {
2806 if (flags & BTRFS_BLOCK_GROUP_DATA)
2807 fs_info->avail_data_alloc_bits |= extra_flags;
2808 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2809 fs_info->avail_metadata_alloc_bits |= extra_flags;
2810 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2811 fs_info->avail_system_alloc_bits |= extra_flags;
2812 }
2813}
Chris Mason593060d2008-03-25 16:50:33 -04002814
Yan Zheng2b820322008-11-17 21:11:30 -05002815u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002816{
Yan Zheng2b820322008-11-17 21:11:30 -05002817 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002818
2819 if (num_devices == 1)
2820 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2821 if (num_devices < 4)
2822 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2823
Chris Masonec44a352008-04-28 15:29:52 -04002824 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2825 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002826 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002827 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002828 }
Chris Masonec44a352008-04-28 15:29:52 -04002829
2830 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002831 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002832 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002833 }
Chris Masonec44a352008-04-28 15:29:52 -04002834
2835 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2836 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2837 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2838 (flags & BTRFS_BLOCK_GROUP_DUP)))
2839 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2840 return flags;
2841}
2842
Yan, Zhengb742bb82010-05-16 10:46:24 -04002843static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05002844{
Yan, Zhengb742bb82010-05-16 10:46:24 -04002845 if (flags & BTRFS_BLOCK_GROUP_DATA)
2846 flags |= root->fs_info->avail_data_alloc_bits &
2847 root->fs_info->data_alloc_profile;
2848 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2849 flags |= root->fs_info->avail_system_alloc_bits &
2850 root->fs_info->system_alloc_profile;
2851 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
2852 flags |= root->fs_info->avail_metadata_alloc_bits &
2853 root->fs_info->metadata_alloc_profile;
2854 return btrfs_reduce_alloc_profile(root, flags);
2855}
Josef Bacik6a632092009-02-20 11:00:09 -05002856
Yan, Zhengb742bb82010-05-16 10:46:24 -04002857static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
2858{
2859 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05002860
Yan, Zhengb742bb82010-05-16 10:46:24 -04002861 if (data)
2862 flags = BTRFS_BLOCK_GROUP_DATA;
2863 else if (root == root->fs_info->chunk_root)
2864 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2865 else
2866 flags = BTRFS_BLOCK_GROUP_METADATA;
2867
2868 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05002869}
2870
2871void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2872{
Josef Bacik6a632092009-02-20 11:00:09 -05002873 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002874 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05002875}
2876
Josef Bacik9ed74f22009-09-11 16:12:44 -04002877/*
Josef Bacik6a632092009-02-20 11:00:09 -05002878 * This will check the space that the inode allocates from to make sure we have
2879 * enough space for bytes.
2880 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002881int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05002882{
2883 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002884 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e2412010-03-19 14:38:13 +00002885 u64 used;
Yan, Zheng5da9d012010-05-16 10:46:25 -04002886 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002887
2888 /* make sure bytes are sectorsize aligned */
2889 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2890
2891 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04002892 if (!data_sinfo)
2893 goto alloc;
2894
Josef Bacik6a632092009-02-20 11:00:09 -05002895again:
2896 /* make sure we have enough space to handle the data first */
2897 spin_lock(&data_sinfo->lock);
Josef Bacikab6e2412010-03-19 14:38:13 +00002898 used = data_sinfo->bytes_used + data_sinfo->bytes_delalloc +
2899 data_sinfo->bytes_reserved + data_sinfo->bytes_pinned +
2900 data_sinfo->bytes_readonly + data_sinfo->bytes_may_use +
2901 data_sinfo->bytes_super;
2902
2903 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002904 struct btrfs_trans_handle *trans;
2905
Josef Bacik6a632092009-02-20 11:00:09 -05002906 /*
2907 * if we don't have enough free bytes in this space then we need
2908 * to alloc a new chunk.
2909 */
2910 if (!data_sinfo->full) {
2911 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05002912
2913 data_sinfo->force_alloc = 1;
2914 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04002915alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05002916 alloc_target = btrfs_get_alloc_profile(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002917 trans = btrfs_join_transaction(root, 1);
2918 if (IS_ERR(trans))
2919 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05002920
2921 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2922 bytes + 2 * 1024 * 1024,
2923 alloc_target, 0);
2924 btrfs_end_transaction(trans, root);
2925 if (ret)
2926 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04002927
2928 if (!data_sinfo) {
2929 btrfs_set_inode_space_info(root, inode);
2930 data_sinfo = BTRFS_I(inode)->space_info;
2931 }
Josef Bacik6a632092009-02-20 11:00:09 -05002932 goto again;
2933 }
2934 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002935
2936 /* commit the current transaction and try again */
Sage Weildd7e0b72009-09-29 18:38:44 -04002937 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002938 committed = 1;
2939 trans = btrfs_join_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002940 if (IS_ERR(trans))
2941 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002942 ret = btrfs_commit_transaction(trans, root);
2943 if (ret)
2944 return ret;
2945 goto again;
2946 }
2947
Josef Bacik6a632092009-02-20 11:00:09 -05002948 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2949 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04002950 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07002951 "%llu total\n", (unsigned long long)bytes,
2952 (unsigned long long)data_sinfo->bytes_delalloc,
2953 (unsigned long long)data_sinfo->bytes_used,
2954 (unsigned long long)data_sinfo->bytes_reserved,
2955 (unsigned long long)data_sinfo->bytes_pinned,
2956 (unsigned long long)data_sinfo->bytes_readonly,
2957 (unsigned long long)data_sinfo->bytes_may_use,
2958 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05002959 return -ENOSPC;
2960 }
2961 data_sinfo->bytes_may_use += bytes;
2962 BTRFS_I(inode)->reserved_bytes += bytes;
2963 spin_unlock(&data_sinfo->lock);
2964
Josef Bacik9ed74f22009-09-11 16:12:44 -04002965 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002966}
2967
2968/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002969 * called when we are clearing an delalloc extent from the
2970 * inode's io_tree or there was an error for whatever reason
2971 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05002972 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002973void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05002974{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002975 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05002976 struct btrfs_space_info *data_sinfo;
2977
2978 /* make sure bytes are sectorsize aligned */
2979 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2980
2981 data_sinfo = BTRFS_I(inode)->space_info;
2982 spin_lock(&data_sinfo->lock);
2983 data_sinfo->bytes_may_use -= bytes;
2984 BTRFS_I(inode)->reserved_bytes -= bytes;
2985 spin_unlock(&data_sinfo->lock);
2986}
2987
Josef Bacik97e728d2009-04-21 17:40:57 -04002988static void force_metadata_allocation(struct btrfs_fs_info *info)
2989{
2990 struct list_head *head = &info->space_info;
2991 struct btrfs_space_info *found;
2992
2993 rcu_read_lock();
2994 list_for_each_entry_rcu(found, head, list) {
2995 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
2996 found->force_alloc = 1;
2997 }
2998 rcu_read_unlock();
2999}
3000
Yan, Zheng424499d2010-05-16 10:46:25 -04003001static int should_alloc_chunk(struct btrfs_space_info *sinfo,
3002 u64 alloc_bytes)
3003{
3004 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3005
3006 if (sinfo->bytes_used + sinfo->bytes_reserved +
3007 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3008 return 0;
3009
3010 if (sinfo->bytes_used + sinfo->bytes_reserved +
3011 alloc_bytes < div_factor(num_bytes, 8))
3012 return 0;
3013
3014 return 1;
3015}
3016
Chris Mason6324fbf2008-03-24 15:01:59 -04003017static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3018 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003019 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003020{
3021 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003022 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Yan Zhengc146afa2008-11-12 14:34:12 -05003023 int ret = 0;
3024
Josef Bacik97e728d2009-04-21 17:40:57 -04003025 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003026
Yan Zheng2b820322008-11-17 21:11:30 -05003027 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003028
Chris Mason6324fbf2008-03-24 15:01:59 -04003029 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003030 if (!space_info) {
3031 ret = update_space_info(extent_root->fs_info, flags,
3032 0, 0, &space_info);
3033 BUG_ON(ret);
3034 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003035 BUG_ON(!space_info);
3036
Josef Bacik25179202008-10-29 14:49:05 -04003037 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003038 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003039 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003040 if (space_info->full) {
3041 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003042 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003043 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003044
Yan, Zheng424499d2010-05-16 10:46:25 -04003045 if (!force && !should_alloc_chunk(space_info, alloc_bytes)) {
Josef Bacik25179202008-10-29 14:49:05 -04003046 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003047 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003048 }
Josef Bacik25179202008-10-29 14:49:05 -04003049 spin_unlock(&space_info->lock);
3050
Josef Bacik97e728d2009-04-21 17:40:57 -04003051 /*
3052 * if we're doing a data chunk, go ahead and make sure that
3053 * we keep a reasonable number of metadata chunks allocated in the
3054 * FS as well.
3055 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003056 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003057 fs_info->data_chunk_allocations++;
3058 if (!(fs_info->data_chunk_allocations %
3059 fs_info->metadata_ratio))
3060 force_metadata_allocation(fs_info);
3061 }
3062
Yan Zheng2b820322008-11-17 21:11:30 -05003063 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003064 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003065 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003066 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003067 else
3068 ret = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003069 space_info->force_alloc = 0;
3070 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003071out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003072 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003073 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003074}
3075
Yan, Zheng424499d2010-05-16 10:46:25 -04003076static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
3077 struct btrfs_root *root,
3078 struct btrfs_space_info *sinfo, u64 num_bytes)
3079{
3080 int ret;
3081 int end_trans = 0;
3082
3083 if (sinfo->full)
3084 return 0;
3085
3086 spin_lock(&sinfo->lock);
3087 ret = should_alloc_chunk(sinfo, num_bytes + 2 * 1024 * 1024);
3088 spin_unlock(&sinfo->lock);
3089 if (!ret)
3090 return 0;
3091
3092 if (!trans) {
3093 trans = btrfs_join_transaction(root, 1);
3094 BUG_ON(IS_ERR(trans));
3095 end_trans = 1;
3096 }
3097
3098 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3099 num_bytes + 2 * 1024 * 1024,
3100 get_alloc_profile(root, sinfo->flags), 0);
3101
3102 if (end_trans)
3103 btrfs_end_transaction(trans, root);
3104
3105 return ret == 1 ? 1 : 0;
3106}
3107
Yan, Zheng5da9d012010-05-16 10:46:25 -04003108/*
3109 * shrink metadata reservation for delalloc
3110 */
3111static int shrink_delalloc(struct btrfs_trans_handle *trans,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003112 struct btrfs_root *root, u64 to_reclaim)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003113{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003114 struct btrfs_block_rsv *block_rsv;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003115 u64 reserved;
3116 u64 max_reclaim;
3117 u64 reclaimed = 0;
3118 int pause = 1;
3119 int ret;
3120
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003121 block_rsv = &root->fs_info->delalloc_block_rsv;
3122 spin_lock(&block_rsv->lock);
3123 reserved = block_rsv->reserved;
3124 spin_unlock(&block_rsv->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003125
3126 if (reserved == 0)
3127 return 0;
3128
3129 max_reclaim = min(reserved, to_reclaim);
3130
3131 while (1) {
3132 ret = btrfs_start_one_delalloc_inode(root, trans ? 1 : 0);
3133 if (!ret) {
3134 __set_current_state(TASK_INTERRUPTIBLE);
3135 schedule_timeout(pause);
3136 pause <<= 1;
3137 if (pause > HZ / 10)
3138 pause = HZ / 10;
3139 } else {
3140 pause = 1;
3141 }
3142
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003143 spin_lock(&block_rsv->lock);
3144 if (reserved > block_rsv->reserved)
3145 reclaimed = reserved - block_rsv->reserved;
3146 reserved = block_rsv->reserved;
3147 spin_unlock(&block_rsv->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003148
3149 if (reserved == 0 || reclaimed >= max_reclaim)
3150 break;
3151
3152 if (trans && trans->transaction->blocked)
3153 return -EAGAIN;
3154 }
3155 return reclaimed >= to_reclaim;
3156}
3157
Yan, Zhengf0486c62010-05-16 10:46:25 -04003158static int should_retry_reserve(struct btrfs_trans_handle *trans,
3159 struct btrfs_root *root,
3160 struct btrfs_block_rsv *block_rsv,
3161 u64 num_bytes, int *retries)
3162{
3163 struct btrfs_space_info *space_info = block_rsv->space_info;
3164 int ret;
3165
3166 if ((*retries) > 2)
3167 return -ENOSPC;
3168
3169 ret = maybe_allocate_chunk(trans, root, space_info, num_bytes);
3170 if (ret)
3171 return 1;
3172
3173 if (trans && trans->transaction->in_commit)
3174 return -ENOSPC;
3175
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003176 ret = shrink_delalloc(trans, root, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003177 if (ret)
3178 return ret;
3179
3180 spin_lock(&space_info->lock);
3181 if (space_info->bytes_pinned < num_bytes)
3182 ret = 1;
3183 spin_unlock(&space_info->lock);
3184 if (ret)
3185 return -ENOSPC;
3186
3187 (*retries)++;
3188
3189 if (trans)
3190 return -EAGAIN;
3191
3192 trans = btrfs_join_transaction(root, 1);
3193 BUG_ON(IS_ERR(trans));
3194 ret = btrfs_commit_transaction(trans, root);
3195 BUG_ON(ret);
3196
3197 return 1;
3198}
3199
3200static int reserve_metadata_bytes(struct btrfs_block_rsv *block_rsv,
3201 u64 num_bytes)
3202{
3203 struct btrfs_space_info *space_info = block_rsv->space_info;
3204 u64 unused;
3205 int ret = -ENOSPC;
3206
3207 spin_lock(&space_info->lock);
3208 unused = space_info->bytes_used + space_info->bytes_reserved +
3209 space_info->bytes_pinned + space_info->bytes_readonly;
3210
3211 if (unused < space_info->total_bytes)
3212 unused = space_info->total_bytes - unused;
3213 else
3214 unused = 0;
3215
3216 if (unused >= num_bytes) {
3217 if (block_rsv->priority >= 10) {
3218 space_info->bytes_reserved += num_bytes;
3219 ret = 0;
3220 } else {
3221 if ((unused + block_rsv->reserved) *
3222 block_rsv->priority >=
3223 (num_bytes + block_rsv->reserved) * 10) {
3224 space_info->bytes_reserved += num_bytes;
3225 ret = 0;
3226 }
3227 }
3228 }
3229 spin_unlock(&space_info->lock);
3230
3231 return ret;
3232}
3233
3234static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3235 struct btrfs_root *root)
3236{
3237 struct btrfs_block_rsv *block_rsv;
3238 if (root->ref_cows)
3239 block_rsv = trans->block_rsv;
3240 else
3241 block_rsv = root->block_rsv;
3242
3243 if (!block_rsv)
3244 block_rsv = &root->fs_info->empty_block_rsv;
3245
3246 return block_rsv;
3247}
3248
3249static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3250 u64 num_bytes)
3251{
3252 int ret = -ENOSPC;
3253 spin_lock(&block_rsv->lock);
3254 if (block_rsv->reserved >= num_bytes) {
3255 block_rsv->reserved -= num_bytes;
3256 if (block_rsv->reserved < block_rsv->size)
3257 block_rsv->full = 0;
3258 ret = 0;
3259 }
3260 spin_unlock(&block_rsv->lock);
3261 return ret;
3262}
3263
3264static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3265 u64 num_bytes, int update_size)
3266{
3267 spin_lock(&block_rsv->lock);
3268 block_rsv->reserved += num_bytes;
3269 if (update_size)
3270 block_rsv->size += num_bytes;
3271 else if (block_rsv->reserved >= block_rsv->size)
3272 block_rsv->full = 1;
3273 spin_unlock(&block_rsv->lock);
3274}
3275
3276void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3277 struct btrfs_block_rsv *dest, u64 num_bytes)
3278{
3279 struct btrfs_space_info *space_info = block_rsv->space_info;
3280
3281 spin_lock(&block_rsv->lock);
3282 if (num_bytes == (u64)-1)
3283 num_bytes = block_rsv->size;
3284 block_rsv->size -= num_bytes;
3285 if (block_rsv->reserved >= block_rsv->size) {
3286 num_bytes = block_rsv->reserved - block_rsv->size;
3287 block_rsv->reserved = block_rsv->size;
3288 block_rsv->full = 1;
3289 } else {
3290 num_bytes = 0;
3291 }
3292 spin_unlock(&block_rsv->lock);
3293
3294 if (num_bytes > 0) {
3295 if (dest) {
3296 block_rsv_add_bytes(dest, num_bytes, 0);
3297 } else {
3298 spin_lock(&space_info->lock);
3299 space_info->bytes_reserved -= num_bytes;
3300 spin_unlock(&space_info->lock);
3301 }
3302 }
3303}
3304
3305static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3306 struct btrfs_block_rsv *dst, u64 num_bytes)
3307{
3308 int ret;
3309
3310 ret = block_rsv_use_bytes(src, num_bytes);
3311 if (ret)
3312 return ret;
3313
3314 block_rsv_add_bytes(dst, num_bytes, 1);
3315 return 0;
3316}
3317
3318void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3319{
3320 memset(rsv, 0, sizeof(*rsv));
3321 spin_lock_init(&rsv->lock);
3322 atomic_set(&rsv->usage, 1);
3323 rsv->priority = 6;
3324 INIT_LIST_HEAD(&rsv->list);
3325}
3326
3327struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3328{
3329 struct btrfs_block_rsv *block_rsv;
3330 struct btrfs_fs_info *fs_info = root->fs_info;
3331 u64 alloc_target;
3332
3333 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3334 if (!block_rsv)
3335 return NULL;
3336
3337 btrfs_init_block_rsv(block_rsv);
3338
3339 alloc_target = btrfs_get_alloc_profile(root, 0);
3340 block_rsv->space_info = __find_space_info(fs_info,
3341 BTRFS_BLOCK_GROUP_METADATA);
3342
3343 return block_rsv;
3344}
3345
3346void btrfs_free_block_rsv(struct btrfs_root *root,
3347 struct btrfs_block_rsv *rsv)
3348{
3349 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3350 btrfs_block_rsv_release(root, rsv, (u64)-1);
3351 if (!rsv->durable)
3352 kfree(rsv);
3353 }
3354}
3355
3356/*
3357 * make the block_rsv struct be able to capture freed space.
3358 * the captured space will re-add to the the block_rsv struct
3359 * after transaction commit
3360 */
3361void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3362 struct btrfs_block_rsv *block_rsv)
3363{
3364 block_rsv->durable = 1;
3365 mutex_lock(&fs_info->durable_block_rsv_mutex);
3366 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3367 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3368}
3369
3370int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3371 struct btrfs_root *root,
3372 struct btrfs_block_rsv *block_rsv,
3373 u64 num_bytes, int *retries)
3374{
3375 int ret;
3376
3377 if (num_bytes == 0)
3378 return 0;
3379again:
3380 ret = reserve_metadata_bytes(block_rsv, num_bytes);
3381 if (!ret) {
3382 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3383 return 0;
3384 }
3385
3386 ret = should_retry_reserve(trans, root, block_rsv, num_bytes, retries);
3387 if (ret > 0)
3388 goto again;
3389
3390 return ret;
3391}
3392
3393int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3394 struct btrfs_root *root,
3395 struct btrfs_block_rsv *block_rsv,
3396 u64 min_reserved, int min_factor)
3397{
3398 u64 num_bytes = 0;
3399 int commit_trans = 0;
3400 int ret = -ENOSPC;
3401
3402 if (!block_rsv)
3403 return 0;
3404
3405 spin_lock(&block_rsv->lock);
3406 if (min_factor > 0)
3407 num_bytes = div_factor(block_rsv->size, min_factor);
3408 if (min_reserved > num_bytes)
3409 num_bytes = min_reserved;
3410
3411 if (block_rsv->reserved >= num_bytes) {
3412 ret = 0;
3413 } else {
3414 num_bytes -= block_rsv->reserved;
3415 if (block_rsv->durable &&
3416 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3417 commit_trans = 1;
3418 }
3419 spin_unlock(&block_rsv->lock);
3420 if (!ret)
3421 return 0;
3422
3423 if (block_rsv->refill_used) {
3424 ret = reserve_metadata_bytes(block_rsv, num_bytes);
3425 if (!ret) {
3426 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3427 return 0;
3428 }
3429 }
3430
3431 if (commit_trans) {
3432 if (trans)
3433 return -EAGAIN;
3434
3435 trans = btrfs_join_transaction(root, 1);
3436 BUG_ON(IS_ERR(trans));
3437 ret = btrfs_commit_transaction(trans, root);
3438 return 0;
3439 }
3440
3441 WARN_ON(1);
3442 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
3443 block_rsv->size, block_rsv->reserved,
3444 block_rsv->freed[0], block_rsv->freed[1]);
3445
3446 return -ENOSPC;
3447}
3448
3449int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3450 struct btrfs_block_rsv *dst_rsv,
3451 u64 num_bytes)
3452{
3453 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3454}
3455
3456void btrfs_block_rsv_release(struct btrfs_root *root,
3457 struct btrfs_block_rsv *block_rsv,
3458 u64 num_bytes)
3459{
3460 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3461 if (global_rsv->full || global_rsv == block_rsv ||
3462 block_rsv->space_info != global_rsv->space_info)
3463 global_rsv = NULL;
3464 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3465}
3466
3467static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3468{
3469 struct btrfs_space_info *space_info;
3470
3471 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3472 fs_info->chunk_block_rsv.space_info = space_info;
3473 fs_info->chunk_block_rsv.priority = 10;
3474
3475 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3476 fs_info->trans_block_rsv.space_info = space_info;
3477 fs_info->empty_block_rsv.space_info = space_info;
3478 fs_info->empty_block_rsv.priority = 10;
3479
3480 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
3481}
3482
Yan, Zhenga22285a2010-05-16 10:48:46 -04003483static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3484{
3485 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3486 3 * num_items;
3487}
3488
3489int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3490 struct btrfs_root *root,
3491 int num_items, int *retries)
3492{
3493 u64 num_bytes;
3494 int ret;
3495
3496 if (num_items == 0 || root->fs_info->chunk_root == root)
3497 return 0;
3498
3499 num_bytes = calc_trans_metadata_size(root, num_items);
3500 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
3501 num_bytes, retries);
3502 if (!ret) {
3503 trans->bytes_reserved += num_bytes;
3504 trans->block_rsv = &root->fs_info->trans_block_rsv;
3505 }
3506 return ret;
3507}
3508
3509void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3510 struct btrfs_root *root)
3511{
3512 if (!trans->bytes_reserved)
3513 return;
3514
3515 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3516 btrfs_block_rsv_release(root, trans->block_rsv,
3517 trans->bytes_reserved);
3518 trans->bytes_reserved = 0;
3519}
3520
3521int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3522 struct btrfs_pending_snapshot *pending)
3523{
3524 struct btrfs_root *root = pending->root;
3525 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3526 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3527 /*
3528 * two for root back/forward refs, two for directory entries
3529 * and one for root of the snapshot.
3530 */
3531 u64 num_bytes = calc_trans_metadata_size(root, 5);
3532 dst_rsv->space_info = src_rsv->space_info;
3533 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3534}
3535
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003536static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3537{
3538 return num_bytes >>= 3;
3539}
3540
3541int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3542{
3543 struct btrfs_root *root = BTRFS_I(inode)->root;
3544 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3545 u64 to_reserve;
3546 int nr_extents;
3547 int retries = 0;
3548 int ret;
3549
3550 if (btrfs_transaction_in_commit(root->fs_info))
3551 schedule_timeout(1);
3552
3553 num_bytes = ALIGN(num_bytes, root->sectorsize);
3554again:
3555 spin_lock(&BTRFS_I(inode)->accounting_lock);
3556 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
3557 if (nr_extents > BTRFS_I(inode)->reserved_extents) {
3558 nr_extents -= BTRFS_I(inode)->reserved_extents;
3559 to_reserve = calc_trans_metadata_size(root, nr_extents);
3560 } else {
3561 nr_extents = 0;
3562 to_reserve = 0;
3563 }
3564
3565 to_reserve += calc_csum_metadata_size(inode, num_bytes);
3566 ret = reserve_metadata_bytes(block_rsv, to_reserve);
3567 if (ret) {
3568 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3569 ret = should_retry_reserve(NULL, root, block_rsv, to_reserve,
3570 &retries);
3571 if (ret > 0)
3572 goto again;
3573 return ret;
3574 }
3575
3576 BTRFS_I(inode)->reserved_extents += nr_extents;
3577 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
3578 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3579
3580 block_rsv_add_bytes(block_rsv, to_reserve, 1);
3581
3582 if (block_rsv->size > 512 * 1024 * 1024)
3583 shrink_delalloc(NULL, root, to_reserve);
3584
3585 return 0;
3586}
3587
3588void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
3589{
3590 struct btrfs_root *root = BTRFS_I(inode)->root;
3591 u64 to_free;
3592 int nr_extents;
3593
3594 num_bytes = ALIGN(num_bytes, root->sectorsize);
3595 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
3596
3597 spin_lock(&BTRFS_I(inode)->accounting_lock);
3598 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
3599 if (nr_extents < BTRFS_I(inode)->reserved_extents) {
3600 nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents;
3601 BTRFS_I(inode)->reserved_extents -= nr_extents;
3602 } else {
3603 nr_extents = 0;
3604 }
3605 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3606
3607 to_free = calc_csum_metadata_size(inode, num_bytes);
3608 if (nr_extents > 0)
3609 to_free += calc_trans_metadata_size(root, nr_extents);
3610
3611 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
3612 to_free);
3613}
3614
3615int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
3616{
3617 int ret;
3618
3619 ret = btrfs_check_data_free_space(inode, num_bytes);
3620 if (ret)
3621 return ret;
3622
3623 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
3624 if (ret) {
3625 btrfs_free_reserved_data_space(inode, num_bytes);
3626 return ret;
3627 }
3628
3629 return 0;
3630}
3631
3632void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
3633{
3634 btrfs_delalloc_release_metadata(inode, num_bytes);
3635 btrfs_free_reserved_data_space(inode, num_bytes);
3636}
3637
Chris Mason9078a3e2007-04-26 16:46:15 -04003638static int update_block_group(struct btrfs_trans_handle *trans,
3639 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003640 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04003641{
3642 struct btrfs_block_group_cache *cache;
3643 struct btrfs_fs_info *info = root->fs_info;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003644 int factor;
Chris Masondb945352007-10-15 16:15:53 -04003645 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003646 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003647 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04003648
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003649 /* block accounting for super block */
3650 spin_lock(&info->delalloc_lock);
3651 old_val = btrfs_super_bytes_used(&info->super_copy);
3652 if (alloc)
3653 old_val += num_bytes;
3654 else
3655 old_val -= num_bytes;
3656 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003657 spin_unlock(&info->delalloc_lock);
3658
Chris Masond3977122009-01-05 21:25:51 -05003659 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003660 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003661 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04003662 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003663 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
3664 BTRFS_BLOCK_GROUP_RAID1 |
3665 BTRFS_BLOCK_GROUP_RAID10))
3666 factor = 2;
3667 else
3668 factor = 1;
Chris Masondb945352007-10-15 16:15:53 -04003669 byte_in_group = bytenr - cache->key.objectid;
3670 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003671
Josef Bacik25179202008-10-29 14:49:05 -04003672 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003673 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003674 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04003675 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04003676 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04003677 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04003678 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003679 btrfs_set_block_group_used(&cache->item, old_val);
3680 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003681 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003682 cache->space_info->bytes_used += num_bytes;
3683 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04003684 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003685 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003686 } else {
Chris Masondb945352007-10-15 16:15:53 -04003687 old_val -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003688 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003689 cache->pinned += num_bytes;
3690 cache->space_info->bytes_pinned += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003691 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003692 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04003693 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003694 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003695
Yan, Zhengf0486c62010-05-16 10:46:25 -04003696 set_extent_dirty(info->pinned_extents,
3697 bytenr, bytenr + num_bytes - 1,
3698 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04003699 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003700 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003701 total -= num_bytes;
3702 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003703 }
3704 return 0;
3705}
Chris Mason6324fbf2008-03-24 15:01:59 -04003706
Chris Masona061fc82008-05-07 11:43:44 -04003707static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3708{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003709 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003710 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003711
3712 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3713 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003714 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003715
Yan Zhengd2fb3432008-12-11 16:30:39 -05003716 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003717 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003718
3719 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003720}
3721
Yan, Zhengf0486c62010-05-16 10:46:25 -04003722static int pin_down_extent(struct btrfs_root *root,
3723 struct btrfs_block_group_cache *cache,
3724 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05003725{
Yan Zheng11833d62009-09-11 16:11:19 -04003726 spin_lock(&cache->space_info->lock);
3727 spin_lock(&cache->lock);
3728 cache->pinned += num_bytes;
3729 cache->space_info->bytes_pinned += num_bytes;
3730 if (reserved) {
3731 cache->reserved -= num_bytes;
3732 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05003733 }
Yan Zheng11833d62009-09-11 16:11:19 -04003734 spin_unlock(&cache->lock);
3735 spin_unlock(&cache->space_info->lock);
3736
Yan, Zhengf0486c62010-05-16 10:46:25 -04003737 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
3738 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05003739 return 0;
3740}
Chris Mason9078a3e2007-04-26 16:46:15 -04003741
Yan, Zhengf0486c62010-05-16 10:46:25 -04003742/*
3743 * this function must be called within transaction
3744 */
3745int btrfs_pin_extent(struct btrfs_root *root,
3746 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04003747{
Yan, Zhengf0486c62010-05-16 10:46:25 -04003748 struct btrfs_block_group_cache *cache;
3749
3750 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
3751 BUG_ON(!cache);
3752
3753 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
3754
3755 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04003756 return 0;
3757}
Zheng Yane8569812008-09-26 10:05:48 -04003758
Yan, Zhengf0486c62010-05-16 10:46:25 -04003759/*
3760 * update size of reserved extents. this function may return -EAGAIN
3761 * if 'reserve' is true or 'sinfo' is false.
3762 */
3763static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
3764 u64 num_bytes, int reserve, int sinfo)
3765{
3766 int ret = 0;
3767 if (sinfo) {
3768 struct btrfs_space_info *space_info = cache->space_info;
3769 spin_lock(&space_info->lock);
3770 spin_lock(&cache->lock);
3771 if (reserve) {
3772 if (cache->ro) {
3773 ret = -EAGAIN;
3774 } else {
3775 cache->reserved += num_bytes;
3776 space_info->bytes_reserved += num_bytes;
3777 }
3778 } else {
3779 if (cache->ro)
3780 space_info->bytes_readonly += num_bytes;
3781 cache->reserved -= num_bytes;
3782 space_info->bytes_reserved -= num_bytes;
3783 }
3784 spin_unlock(&cache->lock);
3785 spin_unlock(&space_info->lock);
3786 } else {
3787 spin_lock(&cache->lock);
3788 if (cache->ro) {
3789 ret = -EAGAIN;
3790 } else {
3791 if (reserve)
3792 cache->reserved += num_bytes;
3793 else
3794 cache->reserved -= num_bytes;
3795 }
3796 spin_unlock(&cache->lock);
3797 }
3798 return ret;
3799}
3800
Yan Zheng11833d62009-09-11 16:11:19 -04003801int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3802 struct btrfs_root *root)
3803{
3804 struct btrfs_fs_info *fs_info = root->fs_info;
3805 struct btrfs_caching_control *next;
3806 struct btrfs_caching_control *caching_ctl;
3807 struct btrfs_block_group_cache *cache;
3808
3809 down_write(&fs_info->extent_commit_sem);
3810
3811 list_for_each_entry_safe(caching_ctl, next,
3812 &fs_info->caching_block_groups, list) {
3813 cache = caching_ctl->block_group;
3814 if (block_group_cache_done(cache)) {
3815 cache->last_byte_to_unpin = (u64)-1;
3816 list_del_init(&caching_ctl->list);
3817 put_caching_control(caching_ctl);
3818 } else {
3819 cache->last_byte_to_unpin = caching_ctl->progress;
3820 }
3821 }
3822
3823 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3824 fs_info->pinned_extents = &fs_info->freed_extents[1];
3825 else
3826 fs_info->pinned_extents = &fs_info->freed_extents[0];
3827
3828 up_write(&fs_info->extent_commit_sem);
3829 return 0;
3830}
3831
3832static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3833{
3834 struct btrfs_fs_info *fs_info = root->fs_info;
3835 struct btrfs_block_group_cache *cache = NULL;
3836 u64 len;
3837
3838 while (start <= end) {
3839 if (!cache ||
3840 start >= cache->key.objectid + cache->key.offset) {
3841 if (cache)
3842 btrfs_put_block_group(cache);
3843 cache = btrfs_lookup_block_group(fs_info, start);
3844 BUG_ON(!cache);
3845 }
3846
3847 len = cache->key.objectid + cache->key.offset - start;
3848 len = min(len, end + 1 - start);
3849
3850 if (start < cache->last_byte_to_unpin) {
3851 len = min(len, cache->last_byte_to_unpin - start);
3852 btrfs_add_free_space(cache, start, len);
3853 }
Josef Bacik25179202008-10-29 14:49:05 -04003854
Yan, Zhengf0486c62010-05-16 10:46:25 -04003855 start += len;
3856
Josef Bacik25179202008-10-29 14:49:05 -04003857 spin_lock(&cache->space_info->lock);
3858 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003859 cache->pinned -= len;
3860 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003861 if (cache->ro) {
3862 cache->space_info->bytes_readonly += len;
3863 } else if (cache->reserved_pinned > 0) {
3864 len = min(len, cache->reserved_pinned);
3865 cache->reserved_pinned -= len;
3866 cache->space_info->bytes_reserved += len;
3867 }
Josef Bacik25179202008-10-29 14:49:05 -04003868 spin_unlock(&cache->lock);
3869 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003870 }
3871
3872 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003873 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04003874 return 0;
3875}
3876
3877int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04003878 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05003879{
Yan Zheng11833d62009-09-11 16:11:19 -04003880 struct btrfs_fs_info *fs_info = root->fs_info;
3881 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003882 struct btrfs_block_rsv *block_rsv;
3883 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04003884 u64 start;
3885 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003886 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05003887 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003888
Yan Zheng11833d62009-09-11 16:11:19 -04003889 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3890 unpin = &fs_info->freed_extents[1];
3891 else
3892 unpin = &fs_info->freed_extents[0];
3893
Chris Masond3977122009-01-05 21:25:51 -05003894 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003895 ret = find_first_extent_bit(unpin, 0, &start, &end,
3896 EXTENT_DIRTY);
3897 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003898 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003899
3900 ret = btrfs_discard_extent(root, start, end + 1 - start);
3901
Chris Mason1a5bc162007-10-15 16:15:26 -04003902 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04003903 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04003904 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003905 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003906
Yan, Zhengf0486c62010-05-16 10:46:25 -04003907 mutex_lock(&fs_info->durable_block_rsv_mutex);
3908 list_for_each_entry_safe(block_rsv, next_rsv,
3909 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05003910
Yan, Zhengf0486c62010-05-16 10:46:25 -04003911 idx = trans->transid & 0x1;
3912 if (block_rsv->freed[idx] > 0) {
3913 block_rsv_add_bytes(block_rsv,
3914 block_rsv->freed[idx], 0);
3915 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04003916 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04003917 if (atomic_read(&block_rsv->usage) == 0) {
3918 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003919
Yan, Zhengf0486c62010-05-16 10:46:25 -04003920 if (block_rsv->freed[0] == 0 &&
3921 block_rsv->freed[1] == 0) {
3922 list_del_init(&block_rsv->list);
3923 kfree(block_rsv);
3924 }
3925 } else {
3926 btrfs_block_rsv_release(root, block_rsv, 0);
3927 }
3928 }
3929 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3930
Chris Masone20d96d2007-03-22 12:13:20 -04003931 return 0;
3932}
3933
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003934static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3935 struct btrfs_root *root,
3936 u64 bytenr, u64 num_bytes, u64 parent,
3937 u64 root_objectid, u64 owner_objectid,
3938 u64 owner_offset, int refs_to_drop,
3939 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003940{
Chris Masone2fa7222007-03-12 16:22:34 -04003941 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003942 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003943 struct btrfs_fs_info *info = root->fs_info;
3944 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003945 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003946 struct btrfs_extent_item *ei;
3947 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003948 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003949 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003950 int extent_slot = 0;
3951 int found_extent = 0;
3952 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003953 u32 item_size;
3954 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003955
Chris Mason5caf2a02007-04-02 11:20:42 -04003956 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003957 if (!path)
3958 return -ENOMEM;
3959
Chris Mason3c12ac72008-04-21 12:01:38 -04003960 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003961 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003962
3963 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3964 BUG_ON(!is_data && refs_to_drop != 1);
3965
3966 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3967 bytenr, num_bytes, parent,
3968 root_objectid, owner_objectid,
3969 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003970 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003971 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003972 while (extent_slot >= 0) {
3973 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003974 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003975 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003976 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003977 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3978 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003979 found_extent = 1;
3980 break;
3981 }
3982 if (path->slots[0] - extent_slot > 5)
3983 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003984 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003985 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003986#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3987 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3988 if (found_extent && item_size < sizeof(*ei))
3989 found_extent = 0;
3990#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003991 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003992 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003993 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003994 NULL, refs_to_drop,
3995 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003996 BUG_ON(ret);
3997 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003998 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003999
4000 key.objectid = bytenr;
4001 key.type = BTRFS_EXTENT_ITEM_KEY;
4002 key.offset = num_bytes;
4003
Zheng Yan31840ae2008-09-23 13:14:14 -04004004 ret = btrfs_search_slot(trans, extent_root,
4005 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004006 if (ret) {
4007 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004008 ", was looking for %llu\n", ret,
4009 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004010 btrfs_print_leaf(extent_root, path->nodes[0]);
4011 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004012 BUG_ON(ret);
4013 extent_slot = path->slots[0];
4014 }
Chris Mason7bb86312007-12-11 09:25:06 -05004015 } else {
4016 btrfs_print_leaf(extent_root, path->nodes[0]);
4017 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004018 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004019 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004020 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004021 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004022 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004023 (unsigned long long)owner_objectid,
4024 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004025 }
Chris Mason5f39d392007-10-15 16:14:19 -04004026
4027 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004028 item_size = btrfs_item_size_nr(leaf, extent_slot);
4029#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4030 if (item_size < sizeof(*ei)) {
4031 BUG_ON(found_extent || extent_slot != path->slots[0]);
4032 ret = convert_extent_item_v0(trans, extent_root, path,
4033 owner_objectid, 0);
4034 BUG_ON(ret < 0);
4035
4036 btrfs_release_path(extent_root, path);
4037 path->leave_spinning = 1;
4038
4039 key.objectid = bytenr;
4040 key.type = BTRFS_EXTENT_ITEM_KEY;
4041 key.offset = num_bytes;
4042
4043 ret = btrfs_search_slot(trans, extent_root, &key, path,
4044 -1, 1);
4045 if (ret) {
4046 printk(KERN_ERR "umm, got %d back from search"
4047 ", was looking for %llu\n", ret,
4048 (unsigned long long)bytenr);
4049 btrfs_print_leaf(extent_root, path->nodes[0]);
4050 }
4051 BUG_ON(ret);
4052 extent_slot = path->slots[0];
4053 leaf = path->nodes[0];
4054 item_size = btrfs_item_size_nr(leaf, extent_slot);
4055 }
4056#endif
4057 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004058 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004059 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004060 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4061 struct btrfs_tree_block_info *bi;
4062 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4063 bi = (struct btrfs_tree_block_info *)(ei + 1);
4064 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004065 }
4066
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004067 refs = btrfs_extent_refs(leaf, ei);
4068 BUG_ON(refs < refs_to_drop);
4069 refs -= refs_to_drop;
4070
4071 if (refs > 0) {
4072 if (extent_op)
4073 __run_delayed_extent_op(extent_op, leaf, ei);
4074 /*
4075 * In the case of inline back ref, reference count will
4076 * be updated by remove_extent_backref
4077 */
4078 if (iref) {
4079 BUG_ON(!found_extent);
4080 } else {
4081 btrfs_set_extent_refs(leaf, ei, refs);
4082 btrfs_mark_buffer_dirty(leaf);
4083 }
4084 if (found_extent) {
4085 ret = remove_extent_backref(trans, extent_root, path,
4086 iref, refs_to_drop,
4087 is_data);
4088 BUG_ON(ret);
4089 }
4090 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004091 if (found_extent) {
4092 BUG_ON(is_data && refs_to_drop !=
4093 extent_data_ref_count(root, path, iref));
4094 if (iref) {
4095 BUG_ON(path->slots[0] != extent_slot);
4096 } else {
4097 BUG_ON(path->slots[0] != extent_slot + 1);
4098 path->slots[0] = extent_slot;
4099 num_to_del = 2;
4100 }
Chris Mason78fae272007-03-25 11:35:08 -04004101 }
Chris Masonb9473432009-03-13 11:00:37 -04004102
Chris Mason952fcca2008-02-18 16:33:44 -05004103 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4104 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004105 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04004106 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01004107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004108 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004109 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4110 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004111 } else {
4112 invalidate_mapping_pages(info->btree_inode->i_mapping,
4113 bytenr >> PAGE_CACHE_SHIFT,
4114 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004115 }
4116
Yan, Zhengf0486c62010-05-16 10:46:25 -04004117 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004118 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004119 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004120 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004121 return ret;
4122}
4123
4124/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004125 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004126 * delayed ref for that extent as well. This searches the delayed ref tree for
4127 * a given extent, and if there are no other delayed refs to be processed, it
4128 * removes it from the tree.
4129 */
4130static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4131 struct btrfs_root *root, u64 bytenr)
4132{
4133 struct btrfs_delayed_ref_head *head;
4134 struct btrfs_delayed_ref_root *delayed_refs;
4135 struct btrfs_delayed_ref_node *ref;
4136 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004137 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004138
4139 delayed_refs = &trans->transaction->delayed_refs;
4140 spin_lock(&delayed_refs->lock);
4141 head = btrfs_find_delayed_ref_head(trans, bytenr);
4142 if (!head)
4143 goto out;
4144
4145 node = rb_prev(&head->node.rb_node);
4146 if (!node)
4147 goto out;
4148
4149 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4150
4151 /* there are still entries for this ref, we can't drop it */
4152 if (ref->bytenr == bytenr)
4153 goto out;
4154
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004155 if (head->extent_op) {
4156 if (!head->must_insert_reserved)
4157 goto out;
4158 kfree(head->extent_op);
4159 head->extent_op = NULL;
4160 }
4161
Chris Mason1887be62009-03-13 10:11:24 -04004162 /*
4163 * waiting for the lock here would deadlock. If someone else has it
4164 * locked they are already in the process of dropping it anyway
4165 */
4166 if (!mutex_trylock(&head->mutex))
4167 goto out;
4168
4169 /*
4170 * at this point we have a head with no other entries. Go
4171 * ahead and process it.
4172 */
4173 head->node.in_tree = 0;
4174 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004175
Chris Mason1887be62009-03-13 10:11:24 -04004176 delayed_refs->num_entries--;
4177
4178 /*
4179 * we don't take a ref on the node because we're removing it from the
4180 * tree, so we just steal the ref the tree was holding.
4181 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004182 delayed_refs->num_heads--;
4183 if (list_empty(&head->cluster))
4184 delayed_refs->num_heads_ready--;
4185
4186 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004187 spin_unlock(&delayed_refs->lock);
4188
Yan, Zhengf0486c62010-05-16 10:46:25 -04004189 BUG_ON(head->extent_op);
4190 if (head->must_insert_reserved)
4191 ret = 1;
4192
4193 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004194 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004195 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004196out:
4197 spin_unlock(&delayed_refs->lock);
4198 return 0;
4199}
4200
Yan, Zhengf0486c62010-05-16 10:46:25 -04004201void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4202 struct btrfs_root *root,
4203 struct extent_buffer *buf,
4204 u64 parent, int last_ref)
4205{
4206 struct btrfs_block_rsv *block_rsv;
4207 struct btrfs_block_group_cache *cache = NULL;
4208 int ret;
4209
4210 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4211 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4212 parent, root->root_key.objectid,
4213 btrfs_header_level(buf),
4214 BTRFS_DROP_DELAYED_REF, NULL);
4215 BUG_ON(ret);
4216 }
4217
4218 if (!last_ref)
4219 return;
4220
4221 block_rsv = get_block_rsv(trans, root);
4222 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
4223 BUG_ON(block_rsv->space_info != cache->space_info);
4224
4225 if (btrfs_header_generation(buf) == trans->transid) {
4226 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4227 ret = check_ref_cleanup(trans, root, buf->start);
4228 if (!ret)
4229 goto pin;
4230 }
4231
4232 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4233 pin_down_extent(root, cache, buf->start, buf->len, 1);
4234 goto pin;
4235 }
4236
4237 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4238
4239 btrfs_add_free_space(cache, buf->start, buf->len);
4240 ret = update_reserved_bytes(cache, buf->len, 0, 0);
4241 if (ret == -EAGAIN) {
4242 /* block group became read-only */
4243 update_reserved_bytes(cache, buf->len, 0, 1);
4244 goto out;
4245 }
4246
4247 ret = 1;
4248 spin_lock(&block_rsv->lock);
4249 if (block_rsv->reserved < block_rsv->size) {
4250 block_rsv->reserved += buf->len;
4251 ret = 0;
4252 }
4253 spin_unlock(&block_rsv->lock);
4254
4255 if (ret) {
4256 spin_lock(&cache->space_info->lock);
4257 cache->space_info->bytes_reserved -= buf->len;
4258 spin_unlock(&cache->space_info->lock);
4259 }
4260 goto out;
4261 }
4262pin:
4263 if (block_rsv->durable && !cache->ro) {
4264 ret = 0;
4265 spin_lock(&cache->lock);
4266 if (!cache->ro) {
4267 cache->reserved_pinned += buf->len;
4268 ret = 1;
4269 }
4270 spin_unlock(&cache->lock);
4271
4272 if (ret) {
4273 spin_lock(&block_rsv->lock);
4274 block_rsv->freed[trans->transid & 0x1] += buf->len;
4275 spin_unlock(&block_rsv->lock);
4276 }
4277 }
4278out:
4279 btrfs_put_block_group(cache);
4280}
4281
Chris Mason925baed2008-06-25 16:01:30 -04004282int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004283 struct btrfs_root *root,
4284 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004285 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004286{
4287 int ret;
4288
Chris Mason56bec292009-03-13 10:10:06 -04004289 /*
4290 * tree log blocks never actually go into the extent allocation
4291 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004292 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004293 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4294 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004295 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004296 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004297 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004298 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4299 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4300 parent, root_objectid, (int)owner,
4301 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004302 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004303 } else {
4304 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4305 parent, root_objectid, owner,
4306 offset, BTRFS_DROP_DELAYED_REF, NULL);
4307 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004308 }
Chris Mason925baed2008-06-25 16:01:30 -04004309 return ret;
4310}
4311
Chris Mason87ee04e2007-11-30 11:30:34 -05004312static u64 stripe_align(struct btrfs_root *root, u64 val)
4313{
4314 u64 mask = ((u64)root->stripesize - 1);
4315 u64 ret = (val + mask) & ~mask;
4316 return ret;
4317}
4318
Chris Masonfec577f2007-02-26 10:40:21 -05004319/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004320 * when we wait for progress in the block group caching, its because
4321 * our allocation attempt failed at least once. So, we must sleep
4322 * and let some progress happen before we try again.
4323 *
4324 * This function will sleep at least once waiting for new free space to
4325 * show up, and then it will check the block group free space numbers
4326 * for our min num_bytes. Another option is to have it go ahead
4327 * and look in the rbtree for a free extent of a given size, but this
4328 * is a good start.
4329 */
4330static noinline int
4331wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4332 u64 num_bytes)
4333{
Yan Zheng11833d62009-09-11 16:11:19 -04004334 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004335 DEFINE_WAIT(wait);
4336
Yan Zheng11833d62009-09-11 16:11:19 -04004337 caching_ctl = get_caching_control(cache);
4338 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004339 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004340
Yan Zheng11833d62009-09-11 16:11:19 -04004341 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004342 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004343
4344 put_caching_control(caching_ctl);
4345 return 0;
4346}
4347
4348static noinline int
4349wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4350{
4351 struct btrfs_caching_control *caching_ctl;
4352 DEFINE_WAIT(wait);
4353
4354 caching_ctl = get_caching_control(cache);
4355 if (!caching_ctl)
4356 return 0;
4357
4358 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4359
4360 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004361 return 0;
4362}
4363
Yan, Zhengb742bb82010-05-16 10:46:24 -04004364static int get_block_group_index(struct btrfs_block_group_cache *cache)
4365{
4366 int index;
4367 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4368 index = 0;
4369 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4370 index = 1;
4371 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4372 index = 2;
4373 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4374 index = 3;
4375 else
4376 index = 4;
4377 return index;
4378}
4379
Josef Bacik817d52f2009-07-13 21:29:25 -04004380enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004381 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004382 LOOP_CACHING_NOWAIT = 1,
4383 LOOP_CACHING_WAIT = 2,
4384 LOOP_ALLOC_CHUNK = 3,
4385 LOOP_NO_EMPTY_SIZE = 4,
4386};
4387
4388/*
Chris Masonfec577f2007-02-26 10:40:21 -05004389 * walks the btree of allocated extents and find a hole of a given size.
4390 * The key ins is changed to record the hole:
4391 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004392 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004393 * ins->offset == number of blocks
4394 * Any available blocks before search_start are skipped.
4395 */
Chris Masond3977122009-01-05 21:25:51 -05004396static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004397 struct btrfs_root *orig_root,
4398 u64 num_bytes, u64 empty_size,
4399 u64 search_start, u64 search_end,
4400 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004401 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004402{
Josef Bacik80eb2342008-10-29 14:49:05 -04004403 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004404 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004405 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004406 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004407 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004408 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004409 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004410 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004411 int last_ptr_loop = 0;
4412 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004413 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004414 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004415 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004416 bool failed_alloc = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004417 u64 ideal_cache_percent = 0;
4418 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004419
Chris Masondb945352007-10-15 16:15:53 -04004420 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004421 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004422 ins->objectid = 0;
4423 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004424
Josef Bacik2552d172009-04-03 10:14:19 -04004425 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004426 if (!space_info) {
4427 printk(KERN_ERR "No space info for %d\n", data);
4428 return -ENOSPC;
4429 }
Josef Bacik2552d172009-04-03 10:14:19 -04004430
Chris Mason0ef3e662008-05-24 14:04:53 -04004431 if (orig_root->ref_cows || empty_size)
4432 allowed_chunk_alloc = 1;
4433
Chris Mason239b14b2008-03-24 15:02:07 -04004434 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004435 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004436 if (!btrfs_test_opt(root, SSD))
4437 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004438 }
4439
Chris Masonfa9c0d72009-04-03 09:47:43 -04004440 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4441 last_ptr = &root->fs_info->data_alloc_cluster;
4442 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004443
Chris Mason239b14b2008-03-24 15:02:07 -04004444 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004445 spin_lock(&last_ptr->lock);
4446 if (last_ptr->block_group)
4447 hint_byte = last_ptr->window_start;
4448 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004449 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004450
Chris Masona061fc82008-05-07 11:43:44 -04004451 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004452 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004453
Josef Bacik817d52f2009-07-13 21:29:25 -04004454 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004455 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004456
Josef Bacik2552d172009-04-03 10:14:19 -04004457 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004458ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004459 block_group = btrfs_lookup_block_group(root->fs_info,
4460 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004461 /*
4462 * we don't want to use the block group if it doesn't match our
4463 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004464 *
4465 * However if we are re-searching with an ideal block group
4466 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004467 */
4468 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004469 (block_group->cached != BTRFS_CACHE_NO ||
4470 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004471 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004472 if (list_empty(&block_group->list) ||
4473 block_group->ro) {
4474 /*
4475 * someone is removing this block group,
4476 * we can't jump into the have_block_group
4477 * target because our list pointers are not
4478 * valid
4479 */
4480 btrfs_put_block_group(block_group);
4481 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004482 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004483 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004484 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004485 }
Josef Bacik2552d172009-04-03 10:14:19 -04004486 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004487 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004488 }
Chris Mason42e70e72008-11-07 18:17:11 -05004489 }
Josef Bacik2552d172009-04-03 10:14:19 -04004490search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004491 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004492 list_for_each_entry(block_group, &space_info->block_groups[index],
4493 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004494 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004495 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004496
Josef Bacik11dfe352009-11-13 20:12:59 +00004497 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004498 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004499
Josef Bacik2552d172009-04-03 10:14:19 -04004500have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004501 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004502 u64 free_percent;
4503
4504 free_percent = btrfs_block_group_used(&block_group->item);
4505 free_percent *= 100;
4506 free_percent = div64_u64(free_percent,
4507 block_group->key.offset);
4508 free_percent = 100 - free_percent;
4509 if (free_percent > ideal_cache_percent &&
4510 likely(!block_group->ro)) {
4511 ideal_cache_offset = block_group->key.objectid;
4512 ideal_cache_percent = free_percent;
4513 }
4514
Josef Bacik817d52f2009-07-13 21:29:25 -04004515 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05004516 * We only want to start kthread caching if we are at
4517 * the point where we will wait for caching to make
4518 * progress, or if our ideal search is over and we've
4519 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04004520 */
4521 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05004522 (loop > LOOP_FIND_IDEAL &&
4523 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004524 ret = cache_block_group(block_group);
4525 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004526 }
Josef Bacikccf0e722009-11-10 21:23:48 -05004527 found_uncached_bg = true;
4528
4529 /*
4530 * If loop is set for cached only, try the next block
4531 * group.
4532 */
4533 if (loop == LOOP_FIND_IDEAL)
4534 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05004535 }
4536
Josef Bacik817d52f2009-07-13 21:29:25 -04004537 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05004538 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04004539 found_uncached_bg = true;
4540
Josef Bacikea6a4782008-11-20 12:16:16 -05004541 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004542 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004543
Josef Bacik0a243252009-09-11 16:11:20 -04004544 /*
4545 * Ok we want to try and use the cluster allocator, so lets look
4546 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4547 * have tried the cluster allocator plenty of times at this
4548 * point and not have found anything, so we are likely way too
4549 * fragmented for the clustering stuff to find anything, so lets
4550 * just skip it and let the allocator find whatever block it can
4551 * find
4552 */
4553 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004554 /*
4555 * the refill lock keeps out other
4556 * people trying to start a new cluster
4557 */
4558 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004559 if (last_ptr->block_group &&
4560 (last_ptr->block_group->ro ||
4561 !block_group_bits(last_ptr->block_group, data))) {
4562 offset = 0;
4563 goto refill_cluster;
4564 }
4565
Chris Masonfa9c0d72009-04-03 09:47:43 -04004566 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4567 num_bytes, search_start);
4568 if (offset) {
4569 /* we have a block, we're done */
4570 spin_unlock(&last_ptr->refill_lock);
4571 goto checks;
4572 }
4573
4574 spin_lock(&last_ptr->lock);
4575 /*
4576 * whoops, this cluster doesn't actually point to
4577 * this block group. Get a ref on the block
4578 * group is does point to and try again
4579 */
4580 if (!last_ptr_loop && last_ptr->block_group &&
4581 last_ptr->block_group != block_group) {
4582
4583 btrfs_put_block_group(block_group);
4584 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00004585 btrfs_get_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004586 spin_unlock(&last_ptr->lock);
4587 spin_unlock(&last_ptr->refill_lock);
4588
4589 last_ptr_loop = 1;
4590 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04004591 /*
4592 * we know this block group is properly
4593 * in the list because
4594 * btrfs_remove_block_group, drops the
4595 * cluster before it removes the block
4596 * group from the list
4597 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04004598 goto have_block_group;
4599 }
4600 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004601refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04004602 /*
4603 * this cluster didn't work out, free it and
4604 * start over
4605 */
4606 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4607
4608 last_ptr_loop = 0;
4609
4610 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04004611 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04004612 block_group, last_ptr,
4613 offset, num_bytes,
4614 empty_cluster + empty_size);
4615 if (ret == 0) {
4616 /*
4617 * now pull our allocation out of this
4618 * cluster
4619 */
4620 offset = btrfs_alloc_from_cluster(block_group,
4621 last_ptr, num_bytes,
4622 search_start);
4623 if (offset) {
4624 /* we found one, proceed */
4625 spin_unlock(&last_ptr->refill_lock);
4626 goto checks;
4627 }
Josef Bacik0a243252009-09-11 16:11:20 -04004628 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4629 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004630 spin_unlock(&last_ptr->refill_lock);
4631
Josef Bacik0a243252009-09-11 16:11:20 -04004632 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004633 wait_block_group_cache_progress(block_group,
4634 num_bytes + empty_cluster + empty_size);
4635 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004636 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004637
Chris Masonfa9c0d72009-04-03 09:47:43 -04004638 /*
4639 * at this point we either didn't find a cluster
4640 * or we weren't able to allocate a block from our
4641 * cluster. Free the cluster we've been trying
4642 * to use, and go to the next block group
4643 */
Josef Bacik0a243252009-09-11 16:11:20 -04004644 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004645 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04004646 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004647 }
4648
Josef Bacik6226cb02009-04-03 10:14:18 -04004649 offset = btrfs_find_space_for_alloc(block_group, search_start,
4650 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004651 /*
4652 * If we didn't find a chunk, and we haven't failed on this
4653 * block group before, and this block group is in the middle of
4654 * caching and we are ok with waiting, then go ahead and wait
4655 * for progress to be made, and set failed_alloc to true.
4656 *
4657 * If failed_alloc is true then we've already waited on this
4658 * block group once and should move on to the next block group.
4659 */
4660 if (!offset && !failed_alloc && !cached &&
4661 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004662 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004663 num_bytes + empty_size);
4664 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004665 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004666 } else if (!offset) {
4667 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04004668 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004669checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04004670 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04004671 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04004672 if (search_start + num_bytes >= search_end) {
4673 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004674 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004675 }
Chris Masone37c9e62007-05-09 20:13:14 -04004676
Josef Bacik2552d172009-04-03 10:14:19 -04004677 /* move on to the next group */
4678 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04004679 block_group->key.objectid + block_group->key.offset) {
4680 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004681 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004682 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004683
Josef Bacik2552d172009-04-03 10:14:19 -04004684 ins->objectid = search_start;
4685 ins->offset = num_bytes;
4686
Josef Bacik6226cb02009-04-03 10:14:18 -04004687 if (offset < search_start)
4688 btrfs_add_free_space(block_group, offset,
4689 search_start - offset);
4690 BUG_ON(offset > search_start);
4691
Yan, Zhengf0486c62010-05-16 10:46:25 -04004692 ret = update_reserved_bytes(block_group, num_bytes, 1,
4693 (data & BTRFS_BLOCK_GROUP_DATA));
4694 if (ret == -EAGAIN) {
4695 btrfs_add_free_space(block_group, offset, num_bytes);
4696 goto loop;
4697 }
Yan Zheng11833d62009-09-11 16:11:19 -04004698
Josef Bacik2552d172009-04-03 10:14:19 -04004699 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04004700 ins->objectid = search_start;
4701 ins->offset = num_bytes;
4702
4703 if (offset < search_start)
4704 btrfs_add_free_space(block_group, offset,
4705 search_start - offset);
4706 BUG_ON(offset > search_start);
Josef Bacik2552d172009-04-03 10:14:19 -04004707 break;
4708loop:
Josef Bacik0a243252009-09-11 16:11:20 -04004709 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004710 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004711 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d72009-04-03 09:47:43 -04004712 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004713 }
4714 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004715
Yan, Zhengb742bb82010-05-16 10:46:24 -04004716 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
4717 goto search;
4718
Josef Bacikccf0e722009-11-10 21:23:48 -05004719 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
4720 * for them to make caching progress. Also
4721 * determine the best possible bg to cache
4722 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
4723 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04004724 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4725 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4726 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4727 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04004728 */
Josef Bacik817d52f2009-07-13 21:29:25 -04004729 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4730 (found_uncached_bg || empty_size || empty_cluster ||
4731 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004732 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004733 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004734 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004735 loop++;
4736 if (!ideal_cache_percent &&
4737 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04004738 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05004739
4740 /*
4741 * 1 of the following 2 things have happened so far
4742 *
4743 * 1) We found an ideal block group for caching that
4744 * is mostly full and will cache quickly, so we might
4745 * as well wait for it.
4746 *
4747 * 2) We searched for cached only and we didn't find
4748 * anything, and we didn't start any caching kthreads
4749 * either, so chances are we will loop through and
4750 * start a couple caching kthreads, and then come back
4751 * around and just wait for them. This will be slower
4752 * because we will have 2 caching kthreads reading at
4753 * the same time when we could have just started one
4754 * and waited for it to get far enough to give us an
4755 * allocation, so go ahead and go to the wait caching
4756 * loop.
4757 */
4758 loop = LOOP_CACHING_WAIT;
4759 search_start = ideal_cache_offset;
4760 ideal_cache_percent = 0;
4761 goto ideal_cache;
4762 } else if (loop == LOOP_FIND_IDEAL) {
4763 /*
4764 * Didn't find a uncached bg, wait on anything we find
4765 * next.
4766 */
4767 loop = LOOP_CACHING_WAIT;
4768 goto search;
4769 }
4770
4771 if (loop < LOOP_CACHING_WAIT) {
4772 loop++;
4773 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04004774 }
4775
4776 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004777 empty_size = 0;
4778 empty_cluster = 0;
4779 }
Chris Mason42e70e72008-11-07 18:17:11 -05004780
Josef Bacik2552d172009-04-03 10:14:19 -04004781 if (allowed_chunk_alloc) {
4782 ret = do_chunk_alloc(trans, root, num_bytes +
4783 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04004784 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004785 done_chunk_alloc = 1;
4786 } else if (!done_chunk_alloc) {
Josef Bacik2552d172009-04-03 10:14:19 -04004787 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004788 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004789
Josef Bacik817d52f2009-07-13 21:29:25 -04004790 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004791 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04004792 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004793 }
Josef Bacik2552d172009-04-03 10:14:19 -04004794 ret = -ENOSPC;
4795 } else if (!ins->objectid) {
4796 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04004797 }
Chris Mason0b86a832008-03-24 15:01:56 -04004798
Josef Bacik80eb2342008-10-29 14:49:05 -04004799 /* we found what we needed */
4800 if (ins->objectid) {
4801 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05004802 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04004803
Chris Masonfa9c0d72009-04-03 09:47:43 -04004804 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004805 ret = 0;
4806 }
Chris Mason0b86a832008-03-24 15:01:56 -04004807
Chris Mason0f70abe2007-02-28 16:46:22 -05004808 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004809}
Chris Masonec44a352008-04-28 15:29:52 -04004810
Josef Bacik9ed74f22009-09-11 16:12:44 -04004811static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4812 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04004813{
4814 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004815 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004816
Josef Bacik9ed74f22009-09-11 16:12:44 -04004817 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05004818 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4819 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04004820 info->bytes_pinned - info->bytes_reserved -
4821 info->bytes_super),
Chris Masond3977122009-01-05 21:25:51 -05004822 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05004823 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Josef Bacik9ed74f22009-09-11 16:12:44 -04004824 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4825 "\n",
Joel Becker21380932009-04-21 12:38:29 -07004826 (unsigned long long)info->total_bytes,
4827 (unsigned long long)info->bytes_pinned,
4828 (unsigned long long)info->bytes_delalloc,
4829 (unsigned long long)info->bytes_may_use,
Josef Bacik9ed74f22009-09-11 16:12:44 -04004830 (unsigned long long)info->bytes_used,
4831 (unsigned long long)info->bytes_root,
4832 (unsigned long long)info->bytes_super,
4833 (unsigned long long)info->bytes_reserved);
4834 spin_unlock(&info->lock);
4835
4836 if (!dump_block_groups)
4837 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004838
Josef Bacik80eb2342008-10-29 14:49:05 -04004839 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004840again:
4841 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004842 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05004843 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4844 "%llu pinned %llu reserved\n",
4845 (unsigned long long)cache->key.objectid,
4846 (unsigned long long)cache->key.offset,
4847 (unsigned long long)btrfs_block_group_used(&cache->item),
4848 (unsigned long long)cache->pinned,
4849 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004850 btrfs_dump_free_space(cache, bytes);
4851 spin_unlock(&cache->lock);
4852 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04004853 if (++index < BTRFS_NR_RAID_TYPES)
4854 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04004855 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004856}
Zheng Yane8569812008-09-26 10:05:48 -04004857
Yan Zheng11833d62009-09-11 16:11:19 -04004858int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4859 struct btrfs_root *root,
4860 u64 num_bytes, u64 min_alloc_size,
4861 u64 empty_size, u64 hint_byte,
4862 u64 search_end, struct btrfs_key *ins,
4863 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004864{
4865 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04004866 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004867
Josef Bacik6a632092009-02-20 11:00:09 -05004868 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04004869again:
Chris Mason0ef3e662008-05-24 14:04:53 -04004870 /*
4871 * the only place that sets empty_size is btrfs_realloc_node, which
4872 * is not called recursively on allocations
4873 */
Josef Bacik83d3c962009-12-07 21:45:59 +00004874 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04004875 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004876 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04004877
Chris Masondb945352007-10-15 16:15:53 -04004878 WARN_ON(num_bytes < root->sectorsize);
4879 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004880 search_start, search_end, hint_byte,
4881 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04004882
Chris Mason98d20f62008-04-14 09:46:10 -04004883 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4884 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004885 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004886 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04004887 do_chunk_alloc(trans, root->fs_info->extent_root,
4888 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004889 goto again;
4890 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004891 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004892 struct btrfs_space_info *sinfo;
4893
4894 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004895 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4896 "wanted %llu\n", (unsigned long long)data,
4897 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004898 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04004899 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004900
4901 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004902}
4903
Chris Mason65b51a02008-08-01 15:11:20 -04004904int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4905{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004906 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004907 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004908
Josef Bacik0f9dd462008-09-23 13:14:11 -04004909 cache = btrfs_lookup_block_group(root->fs_info, start);
4910 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004911 printk(KERN_ERR "Unable to find block group for %llu\n",
4912 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004913 return -ENOSPC;
4914 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004915
4916 ret = btrfs_discard_extent(root, start, len);
4917
Josef Bacik0f9dd462008-09-23 13:14:11 -04004918 btrfs_add_free_space(cache, start, len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004919 update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004920 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04004921
Chris Masone6dcd2d2008-07-17 12:53:50 -04004922 return ret;
4923}
4924
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004925static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4926 struct btrfs_root *root,
4927 u64 parent, u64 root_objectid,
4928 u64 flags, u64 owner, u64 offset,
4929 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004930{
4931 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004932 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004933 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004934 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004935 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004936 struct extent_buffer *leaf;
4937 int type;
4938 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004939
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004940 if (parent > 0)
4941 type = BTRFS_SHARED_DATA_REF_KEY;
4942 else
4943 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004944
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004945 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004946
4947 path = btrfs_alloc_path();
4948 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004949
Chris Masonb9473432009-03-13 11:00:37 -04004950 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004951 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4952 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004953 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004954
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004955 leaf = path->nodes[0];
4956 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004957 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004958 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4959 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4960 btrfs_set_extent_flags(leaf, extent_item,
4961 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004962
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004963 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4964 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4965 if (parent > 0) {
4966 struct btrfs_shared_data_ref *ref;
4967 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4968 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4969 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4970 } else {
4971 struct btrfs_extent_data_ref *ref;
4972 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4973 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4974 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4975 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4976 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4977 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004978
4979 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004980 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004981
Yan, Zhengf0486c62010-05-16 10:46:25 -04004982 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05004983 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004984 printk(KERN_ERR "btrfs update block group failed for %llu "
4985 "%llu\n", (unsigned long long)ins->objectid,
4986 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004987 BUG();
4988 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004989 return ret;
4990}
4991
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004992static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4993 struct btrfs_root *root,
4994 u64 parent, u64 root_objectid,
4995 u64 flags, struct btrfs_disk_key *key,
4996 int level, struct btrfs_key *ins)
4997{
4998 int ret;
4999 struct btrfs_fs_info *fs_info = root->fs_info;
5000 struct btrfs_extent_item *extent_item;
5001 struct btrfs_tree_block_info *block_info;
5002 struct btrfs_extent_inline_ref *iref;
5003 struct btrfs_path *path;
5004 struct extent_buffer *leaf;
5005 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5006
5007 path = btrfs_alloc_path();
5008 BUG_ON(!path);
5009
5010 path->leave_spinning = 1;
5011 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5012 ins, size);
5013 BUG_ON(ret);
5014
5015 leaf = path->nodes[0];
5016 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5017 struct btrfs_extent_item);
5018 btrfs_set_extent_refs(leaf, extent_item, 1);
5019 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5020 btrfs_set_extent_flags(leaf, extent_item,
5021 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5022 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5023
5024 btrfs_set_tree_block_key(leaf, block_info, key);
5025 btrfs_set_tree_block_level(leaf, block_info, level);
5026
5027 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5028 if (parent > 0) {
5029 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5030 btrfs_set_extent_inline_ref_type(leaf, iref,
5031 BTRFS_SHARED_BLOCK_REF_KEY);
5032 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5033 } else {
5034 btrfs_set_extent_inline_ref_type(leaf, iref,
5035 BTRFS_TREE_BLOCK_REF_KEY);
5036 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5037 }
5038
5039 btrfs_mark_buffer_dirty(leaf);
5040 btrfs_free_path(path);
5041
Yan, Zhengf0486c62010-05-16 10:46:25 -04005042 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005043 if (ret) {
5044 printk(KERN_ERR "btrfs update block group failed for %llu "
5045 "%llu\n", (unsigned long long)ins->objectid,
5046 (unsigned long long)ins->offset);
5047 BUG();
5048 }
5049 return ret;
5050}
5051
5052int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5053 struct btrfs_root *root,
5054 u64 root_objectid, u64 owner,
5055 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005056{
5057 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005058
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005059 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005060
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005061 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5062 0, root_objectid, owner, offset,
5063 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005064 return ret;
5065}
Chris Masone02119d2008-09-05 16:13:11 -04005066
5067/*
5068 * this is used by the tree logging recovery code. It records that
5069 * an extent has been allocated and makes sure to clear the free
5070 * space cache bits as well
5071 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005072int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5073 struct btrfs_root *root,
5074 u64 root_objectid, u64 owner, u64 offset,
5075 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005076{
5077 int ret;
5078 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005079 struct btrfs_caching_control *caching_ctl;
5080 u64 start = ins->objectid;
5081 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005082
Chris Masone02119d2008-09-05 16:13:11 -04005083 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04005084 cache_block_group(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04005085 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005086
Yan Zheng11833d62009-09-11 16:11:19 -04005087 if (!caching_ctl) {
5088 BUG_ON(!block_group_cache_done(block_group));
5089 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5090 BUG_ON(ret);
5091 } else {
5092 mutex_lock(&caching_ctl->mutex);
5093
5094 if (start >= caching_ctl->progress) {
5095 ret = add_excluded_extent(root, start, num_bytes);
5096 BUG_ON(ret);
5097 } else if (start + num_bytes <= caching_ctl->progress) {
5098 ret = btrfs_remove_free_space(block_group,
5099 start, num_bytes);
5100 BUG_ON(ret);
5101 } else {
5102 num_bytes = caching_ctl->progress - start;
5103 ret = btrfs_remove_free_space(block_group,
5104 start, num_bytes);
5105 BUG_ON(ret);
5106
5107 start = caching_ctl->progress;
5108 num_bytes = ins->objectid + ins->offset -
5109 caching_ctl->progress;
5110 ret = add_excluded_extent(root, start, num_bytes);
5111 BUG_ON(ret);
5112 }
5113
5114 mutex_unlock(&caching_ctl->mutex);
5115 put_caching_control(caching_ctl);
5116 }
5117
Yan, Zhengf0486c62010-05-16 10:46:25 -04005118 ret = update_reserved_bytes(block_group, ins->offset, 1, 1);
5119 BUG_ON(ret);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005120 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005121 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5122 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005123 return ret;
5124}
5125
Chris Mason65b51a02008-08-01 15:11:20 -04005126struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5127 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005128 u64 bytenr, u32 blocksize,
5129 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005130{
5131 struct extent_buffer *buf;
5132
5133 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5134 if (!buf)
5135 return ERR_PTR(-ENOMEM);
5136 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005137 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005138 btrfs_tree_lock(buf);
5139 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005140
5141 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005142 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005143
Chris Masond0c803c2008-09-11 16:17:57 -04005144 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005145 /*
5146 * we allow two log transactions at a time, use different
5147 * EXENT bit to differentiate dirty pages.
5148 */
5149 if (root->log_transid % 2 == 0)
5150 set_extent_dirty(&root->dirty_log_pages, buf->start,
5151 buf->start + buf->len - 1, GFP_NOFS);
5152 else
5153 set_extent_new(&root->dirty_log_pages, buf->start,
5154 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005155 } else {
5156 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5157 buf->start + buf->len - 1, GFP_NOFS);
5158 }
Chris Mason65b51a02008-08-01 15:11:20 -04005159 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005160 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005161 return buf;
5162}
5163
Yan, Zhengf0486c62010-05-16 10:46:25 -04005164static struct btrfs_block_rsv *
5165use_block_rsv(struct btrfs_trans_handle *trans,
5166 struct btrfs_root *root, u32 blocksize)
5167{
5168 struct btrfs_block_rsv *block_rsv;
5169 int ret;
5170
5171 block_rsv = get_block_rsv(trans, root);
5172
5173 if (block_rsv->size == 0) {
5174 ret = reserve_metadata_bytes(block_rsv, blocksize);
5175 if (ret)
5176 return ERR_PTR(ret);
5177 return block_rsv;
5178 }
5179
5180 ret = block_rsv_use_bytes(block_rsv, blocksize);
5181 if (!ret)
5182 return block_rsv;
5183
5184 WARN_ON(1);
5185 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
5186 block_rsv->size, block_rsv->reserved,
5187 block_rsv->freed[0], block_rsv->freed[1]);
5188
5189 return ERR_PTR(-ENOSPC);
5190}
5191
5192static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5193{
5194 block_rsv_add_bytes(block_rsv, blocksize, 0);
5195 block_rsv_release_bytes(block_rsv, NULL, 0);
5196}
5197
Chris Masonfec577f2007-02-26 10:40:21 -05005198/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005199 * finds a free extent and does all the dirty work required for allocation
5200 * returns the key for the extent through ins, and a tree buffer for
5201 * the first block of the extent through buf.
5202 *
Chris Masonfec577f2007-02-26 10:40:21 -05005203 * returns the tree buffer or NULL.
5204 */
Chris Mason5f39d392007-10-15 16:14:19 -04005205struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005206 struct btrfs_root *root, u32 blocksize,
5207 u64 parent, u64 root_objectid,
5208 struct btrfs_disk_key *key, int level,
5209 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005210{
Chris Masone2fa7222007-03-12 16:22:34 -04005211 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005212 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005213 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005214 u64 flags = 0;
5215 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005216
Yan, Zhengf0486c62010-05-16 10:46:25 -04005217
5218 block_rsv = use_block_rsv(trans, root, blocksize);
5219 if (IS_ERR(block_rsv))
5220 return ERR_CAST(block_rsv);
5221
5222 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5223 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005224 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005225 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005226 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005227 }
Chris Mason55c69072008-01-09 15:55:33 -05005228
Chris Mason4008c042009-02-12 14:09:45 -05005229 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5230 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005231 BUG_ON(IS_ERR(buf));
5232
5233 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5234 if (parent == 0)
5235 parent = ins.objectid;
5236 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5237 } else
5238 BUG_ON(parent > 0);
5239
5240 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5241 struct btrfs_delayed_extent_op *extent_op;
5242 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5243 BUG_ON(!extent_op);
5244 if (key)
5245 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5246 else
5247 memset(&extent_op->key, 0, sizeof(extent_op->key));
5248 extent_op->flags_to_set = flags;
5249 extent_op->update_key = 1;
5250 extent_op->update_flags = 1;
5251 extent_op->is_data = 0;
5252
5253 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5254 ins.offset, parent, root_objectid,
5255 level, BTRFS_ADD_DELAYED_EXTENT,
5256 extent_op);
5257 BUG_ON(ret);
5258 }
Chris Masonfec577f2007-02-26 10:40:21 -05005259 return buf;
5260}
Chris Masona28ec192007-03-06 20:08:01 -05005261
Yan Zheng2c47e6052009-06-27 21:07:35 -04005262struct walk_control {
5263 u64 refs[BTRFS_MAX_LEVEL];
5264 u64 flags[BTRFS_MAX_LEVEL];
5265 struct btrfs_key update_progress;
5266 int stage;
5267 int level;
5268 int shared_level;
5269 int update_ref;
5270 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005271 int reada_slot;
5272 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005273};
5274
5275#define DROP_REFERENCE 1
5276#define UPDATE_BACKREF 2
5277
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005278static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5279 struct btrfs_root *root,
5280 struct walk_control *wc,
5281 struct btrfs_path *path)
5282{
5283 u64 bytenr;
5284 u64 generation;
5285 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005286 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005287 u64 last = 0;
5288 u32 nritems;
5289 u32 blocksize;
5290 struct btrfs_key key;
5291 struct extent_buffer *eb;
5292 int ret;
5293 int slot;
5294 int nread = 0;
5295
5296 if (path->slots[wc->level] < wc->reada_slot) {
5297 wc->reada_count = wc->reada_count * 2 / 3;
5298 wc->reada_count = max(wc->reada_count, 2);
5299 } else {
5300 wc->reada_count = wc->reada_count * 3 / 2;
5301 wc->reada_count = min_t(int, wc->reada_count,
5302 BTRFS_NODEPTRS_PER_BLOCK(root));
5303 }
5304
5305 eb = path->nodes[wc->level];
5306 nritems = btrfs_header_nritems(eb);
5307 blocksize = btrfs_level_size(root, wc->level - 1);
5308
5309 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5310 if (nread >= wc->reada_count)
5311 break;
5312
5313 cond_resched();
5314 bytenr = btrfs_node_blockptr(eb, slot);
5315 generation = btrfs_node_ptr_generation(eb, slot);
5316
5317 if (slot == path->slots[wc->level])
5318 goto reada;
5319
5320 if (wc->stage == UPDATE_BACKREF &&
5321 generation <= root->root_key.offset)
5322 continue;
5323
Yan, Zheng94fcca92009-10-09 09:25:16 -04005324 /* We don't lock the tree block, it's OK to be racy here */
5325 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5326 &refs, &flags);
5327 BUG_ON(ret);
5328 BUG_ON(refs == 0);
5329
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005330 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005331 if (refs == 1)
5332 goto reada;
5333
Yan, Zheng94fcca92009-10-09 09:25:16 -04005334 if (wc->level == 1 &&
5335 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5336 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005337 if (!wc->update_ref ||
5338 generation <= root->root_key.offset)
5339 continue;
5340 btrfs_node_key_to_cpu(eb, &key, slot);
5341 ret = btrfs_comp_cpu_keys(&key,
5342 &wc->update_progress);
5343 if (ret < 0)
5344 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005345 } else {
5346 if (wc->level == 1 &&
5347 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5348 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005349 }
5350reada:
5351 ret = readahead_tree_block(root, bytenr, blocksize,
5352 generation);
5353 if (ret)
5354 break;
5355 last = bytenr + blocksize;
5356 nread++;
5357 }
5358 wc->reada_slot = slot;
5359}
5360
Chris Mason9aca1d52007-03-13 11:09:37 -04005361/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005362 * hepler to process tree block while walking down the tree.
5363 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005364 * when wc->stage == UPDATE_BACKREF, this function updates
5365 * back refs for pointers in the block.
5366 *
5367 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005368 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005369static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5370 struct btrfs_root *root,
5371 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005372 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005373{
5374 int level = wc->level;
5375 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005376 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5377 int ret;
5378
5379 if (wc->stage == UPDATE_BACKREF &&
5380 btrfs_header_owner(eb) != root->root_key.objectid)
5381 return 1;
5382
5383 /*
5384 * when reference count of tree block is 1, it won't increase
5385 * again. once full backref flag is set, we never clear it.
5386 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005387 if (lookup_info &&
5388 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5389 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005390 BUG_ON(!path->locks[level]);
5391 ret = btrfs_lookup_extent_info(trans, root,
5392 eb->start, eb->len,
5393 &wc->refs[level],
5394 &wc->flags[level]);
5395 BUG_ON(ret);
5396 BUG_ON(wc->refs[level] == 0);
5397 }
5398
Yan Zheng2c47e6052009-06-27 21:07:35 -04005399 if (wc->stage == DROP_REFERENCE) {
5400 if (wc->refs[level] > 1)
5401 return 1;
5402
5403 if (path->locks[level] && !wc->keep_locks) {
5404 btrfs_tree_unlock(eb);
5405 path->locks[level] = 0;
5406 }
5407 return 0;
5408 }
5409
5410 /* wc->stage == UPDATE_BACKREF */
5411 if (!(wc->flags[level] & flag)) {
5412 BUG_ON(!path->locks[level]);
5413 ret = btrfs_inc_ref(trans, root, eb, 1);
5414 BUG_ON(ret);
5415 ret = btrfs_dec_ref(trans, root, eb, 0);
5416 BUG_ON(ret);
5417 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5418 eb->len, flag, 0);
5419 BUG_ON(ret);
5420 wc->flags[level] |= flag;
5421 }
5422
5423 /*
5424 * the block is shared by multiple trees, so it's not good to
5425 * keep the tree lock
5426 */
5427 if (path->locks[level] && level > 0) {
5428 btrfs_tree_unlock(eb);
5429 path->locks[level] = 0;
5430 }
5431 return 0;
5432}
5433
5434/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005435 * hepler to process tree block pointer.
5436 *
5437 * when wc->stage == DROP_REFERENCE, this function checks
5438 * reference count of the block pointed to. if the block
5439 * is shared and we need update back refs for the subtree
5440 * rooted at the block, this function changes wc->stage to
5441 * UPDATE_BACKREF. if the block is shared and there is no
5442 * need to update back, this function drops the reference
5443 * to the block.
5444 *
5445 * NOTE: return value 1 means we should stop walking down.
5446 */
5447static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5448 struct btrfs_root *root,
5449 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005450 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005451{
5452 u64 bytenr;
5453 u64 generation;
5454 u64 parent;
5455 u32 blocksize;
5456 struct btrfs_key key;
5457 struct extent_buffer *next;
5458 int level = wc->level;
5459 int reada = 0;
5460 int ret = 0;
5461
5462 generation = btrfs_node_ptr_generation(path->nodes[level],
5463 path->slots[level]);
5464 /*
5465 * if the lower level block was created before the snapshot
5466 * was created, we know there is no need to update back refs
5467 * for the subtree
5468 */
5469 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005470 generation <= root->root_key.offset) {
5471 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005472 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005473 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005474
5475 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5476 blocksize = btrfs_level_size(root, level - 1);
5477
5478 next = btrfs_find_tree_block(root, bytenr, blocksize);
5479 if (!next) {
5480 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00005481 if (!next)
5482 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005483 reada = 1;
5484 }
5485 btrfs_tree_lock(next);
5486 btrfs_set_lock_blocking(next);
5487
Yan, Zheng94fcca92009-10-09 09:25:16 -04005488 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5489 &wc->refs[level - 1],
5490 &wc->flags[level - 1]);
5491 BUG_ON(ret);
5492 BUG_ON(wc->refs[level - 1] == 0);
5493 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005494
Yan, Zheng94fcca92009-10-09 09:25:16 -04005495 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005496 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005497 if (level == 1 &&
5498 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5499 goto skip;
5500
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005501 if (!wc->update_ref ||
5502 generation <= root->root_key.offset)
5503 goto skip;
5504
5505 btrfs_node_key_to_cpu(path->nodes[level], &key,
5506 path->slots[level]);
5507 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5508 if (ret < 0)
5509 goto skip;
5510
5511 wc->stage = UPDATE_BACKREF;
5512 wc->shared_level = level - 1;
5513 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005514 } else {
5515 if (level == 1 &&
5516 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5517 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005518 }
5519
5520 if (!btrfs_buffer_uptodate(next, generation)) {
5521 btrfs_tree_unlock(next);
5522 free_extent_buffer(next);
5523 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005524 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005525 }
5526
5527 if (!next) {
5528 if (reada && level == 1)
5529 reada_walk_down(trans, root, wc, path);
5530 next = read_tree_block(root, bytenr, blocksize, generation);
5531 btrfs_tree_lock(next);
5532 btrfs_set_lock_blocking(next);
5533 }
5534
5535 level--;
5536 BUG_ON(level != btrfs_header_level(next));
5537 path->nodes[level] = next;
5538 path->slots[level] = 0;
5539 path->locks[level] = 1;
5540 wc->level = level;
5541 if (wc->level == 1)
5542 wc->reada_slot = 0;
5543 return 0;
5544skip:
5545 wc->refs[level - 1] = 0;
5546 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005547 if (wc->stage == DROP_REFERENCE) {
5548 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5549 parent = path->nodes[level]->start;
5550 } else {
5551 BUG_ON(root->root_key.objectid !=
5552 btrfs_header_owner(path->nodes[level]));
5553 parent = 0;
5554 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005555
Yan, Zheng94fcca92009-10-09 09:25:16 -04005556 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5557 root->root_key.objectid, level - 1, 0);
5558 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005559 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005560 btrfs_tree_unlock(next);
5561 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04005562 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005563 return 1;
5564}
5565
5566/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005567 * hepler to process tree block while walking up the tree.
5568 *
5569 * when wc->stage == DROP_REFERENCE, this function drops
5570 * reference count on the block.
5571 *
5572 * when wc->stage == UPDATE_BACKREF, this function changes
5573 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5574 * to UPDATE_BACKREF previously while processing the block.
5575 *
5576 * NOTE: return value 1 means we should stop walking up.
5577 */
5578static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5579 struct btrfs_root *root,
5580 struct btrfs_path *path,
5581 struct walk_control *wc)
5582{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005583 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005584 int level = wc->level;
5585 struct extent_buffer *eb = path->nodes[level];
5586 u64 parent = 0;
5587
5588 if (wc->stage == UPDATE_BACKREF) {
5589 BUG_ON(wc->shared_level < level);
5590 if (level < wc->shared_level)
5591 goto out;
5592
Yan Zheng2c47e6052009-06-27 21:07:35 -04005593 ret = find_next_key(path, level + 1, &wc->update_progress);
5594 if (ret > 0)
5595 wc->update_ref = 0;
5596
5597 wc->stage = DROP_REFERENCE;
5598 wc->shared_level = -1;
5599 path->slots[level] = 0;
5600
5601 /*
5602 * check reference count again if the block isn't locked.
5603 * we should start walking down the tree again if reference
5604 * count is one.
5605 */
5606 if (!path->locks[level]) {
5607 BUG_ON(level == 0);
5608 btrfs_tree_lock(eb);
5609 btrfs_set_lock_blocking(eb);
5610 path->locks[level] = 1;
5611
5612 ret = btrfs_lookup_extent_info(trans, root,
5613 eb->start, eb->len,
5614 &wc->refs[level],
5615 &wc->flags[level]);
5616 BUG_ON(ret);
5617 BUG_ON(wc->refs[level] == 0);
5618 if (wc->refs[level] == 1) {
5619 btrfs_tree_unlock(eb);
5620 path->locks[level] = 0;
5621 return 1;
5622 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005623 }
5624 }
5625
5626 /* wc->stage == DROP_REFERENCE */
5627 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5628
5629 if (wc->refs[level] == 1) {
5630 if (level == 0) {
5631 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5632 ret = btrfs_dec_ref(trans, root, eb, 1);
5633 else
5634 ret = btrfs_dec_ref(trans, root, eb, 0);
5635 BUG_ON(ret);
5636 }
5637 /* make block locked assertion in clean_tree_block happy */
5638 if (!path->locks[level] &&
5639 btrfs_header_generation(eb) == trans->transid) {
5640 btrfs_tree_lock(eb);
5641 btrfs_set_lock_blocking(eb);
5642 path->locks[level] = 1;
5643 }
5644 clean_tree_block(trans, root, eb);
5645 }
5646
5647 if (eb == root->node) {
5648 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5649 parent = eb->start;
5650 else
5651 BUG_ON(root->root_key.objectid !=
5652 btrfs_header_owner(eb));
5653 } else {
5654 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5655 parent = path->nodes[level + 1]->start;
5656 else
5657 BUG_ON(root->root_key.objectid !=
5658 btrfs_header_owner(path->nodes[level + 1]));
5659 }
5660
Yan, Zhengf0486c62010-05-16 10:46:25 -04005661 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005662out:
5663 wc->refs[level] = 0;
5664 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005665 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005666}
5667
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005668static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5669 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005670 struct btrfs_path *path,
5671 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005672{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005673 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005674 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005675 int ret;
5676
Yan Zheng2c47e6052009-06-27 21:07:35 -04005677 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005678 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005679 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005680 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005681
Yan Zheng2c47e6052009-06-27 21:07:35 -04005682 if (level == 0)
5683 break;
5684
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005685 if (path->slots[level] >=
5686 btrfs_header_nritems(path->nodes[level]))
5687 break;
5688
Yan, Zheng94fcca92009-10-09 09:25:16 -04005689 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005690 if (ret > 0) {
5691 path->slots[level]++;
5692 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00005693 } else if (ret < 0)
5694 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005695 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005696 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005697 return 0;
5698}
5699
Chris Masond3977122009-01-05 21:25:51 -05005700static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005701 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005702 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005703 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005704{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005705 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005706 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005707
Yan Zheng2c47e6052009-06-27 21:07:35 -04005708 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5709 while (level < max_level && path->nodes[level]) {
5710 wc->level = level;
5711 if (path->slots[level] + 1 <
5712 btrfs_header_nritems(path->nodes[level])) {
5713 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005714 return 0;
5715 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005716 ret = walk_up_proc(trans, root, path, wc);
5717 if (ret > 0)
5718 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05005719
Yan Zheng2c47e6052009-06-27 21:07:35 -04005720 if (path->locks[level]) {
5721 btrfs_tree_unlock(path->nodes[level]);
5722 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005723 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005724 free_extent_buffer(path->nodes[level]);
5725 path->nodes[level] = NULL;
5726 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005727 }
5728 }
5729 return 1;
5730}
5731
Chris Mason9aca1d52007-03-13 11:09:37 -04005732/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005733 * drop a subvolume tree.
5734 *
5735 * this function traverses the tree freeing any blocks that only
5736 * referenced by the tree.
5737 *
5738 * when a shared tree block is found. this function decreases its
5739 * reference count by one. if update_ref is true, this function
5740 * also make sure backrefs for the shared block and all lower level
5741 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04005742 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005743int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05005744{
Chris Mason5caf2a02007-04-02 11:20:42 -04005745 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005746 struct btrfs_trans_handle *trans;
5747 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005748 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005749 struct walk_control *wc;
5750 struct btrfs_key key;
5751 int err = 0;
5752 int ret;
5753 int level;
Chris Mason20524f02007-03-10 06:35:47 -05005754
Chris Mason5caf2a02007-04-02 11:20:42 -04005755 path = btrfs_alloc_path();
5756 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05005757
Yan Zheng2c47e6052009-06-27 21:07:35 -04005758 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5759 BUG_ON(!wc);
5760
Yan, Zhenga22285a2010-05-16 10:48:46 -04005761 trans = btrfs_start_transaction(tree_root, 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005762
Chris Mason9f3a7422007-08-07 15:52:19 -04005763 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005764 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005765 path->nodes[level] = btrfs_lock_root_node(root);
5766 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005767 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005768 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005769 memset(&wc->update_progress, 0,
5770 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005771 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005772 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005773 memcpy(&wc->update_progress, &key,
5774 sizeof(wc->update_progress));
5775
Chris Mason6702ed42007-08-07 16:15:09 -04005776 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005777 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005778 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005779 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5780 path->lowest_level = 0;
5781 if (ret < 0) {
5782 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005783 goto out;
5784 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005785 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005786
Chris Mason7d9eb122008-07-08 14:19:17 -04005787 /*
5788 * unlock our path, this is safe because only this
5789 * function is allowed to delete this snapshot
5790 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005791 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005792
Yan Zheng2c47e6052009-06-27 21:07:35 -04005793 level = btrfs_header_level(root->node);
5794 while (1) {
5795 btrfs_tree_lock(path->nodes[level]);
5796 btrfs_set_lock_blocking(path->nodes[level]);
5797
5798 ret = btrfs_lookup_extent_info(trans, root,
5799 path->nodes[level]->start,
5800 path->nodes[level]->len,
5801 &wc->refs[level],
5802 &wc->flags[level]);
5803 BUG_ON(ret);
5804 BUG_ON(wc->refs[level] == 0);
5805
5806 if (level == root_item->drop_level)
5807 break;
5808
5809 btrfs_tree_unlock(path->nodes[level]);
5810 WARN_ON(wc->refs[level] != 1);
5811 level--;
5812 }
5813 }
5814
5815 wc->level = level;
5816 wc->shared_level = -1;
5817 wc->stage = DROP_REFERENCE;
5818 wc->update_ref = update_ref;
5819 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005820 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005821
5822 while (1) {
5823 ret = walk_down_tree(trans, root, path, wc);
5824 if (ret < 0) {
5825 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005826 break;
5827 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005828
5829 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5830 if (ret < 0) {
5831 err = ret;
5832 break;
5833 }
5834
5835 if (ret > 0) {
5836 BUG_ON(wc->stage != DROP_REFERENCE);
5837 break;
5838 }
5839
5840 if (wc->stage == DROP_REFERENCE) {
5841 level = wc->level;
5842 btrfs_node_key(path->nodes[level],
5843 &root_item->drop_progress,
5844 path->slots[level]);
5845 root_item->drop_level = level;
5846 }
5847
5848 BUG_ON(wc->level == 0);
5849 if (trans->transaction->in_commit ||
5850 trans->transaction->delayed_refs.flushing) {
5851 ret = btrfs_update_root(trans, tree_root,
5852 &root->root_key,
5853 root_item);
5854 BUG_ON(ret);
5855
5856 btrfs_end_transaction(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005857 trans = btrfs_start_transaction(tree_root, 0);
5858 if (IS_ERR(trans))
5859 return PTR_ERR(trans);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005860 } else {
5861 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005862 update = trans->delayed_ref_updates;
5863 trans->delayed_ref_updates = 0;
5864 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005865 btrfs_run_delayed_refs(trans, tree_root,
5866 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005867 }
Chris Mason20524f02007-03-10 06:35:47 -05005868 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005869 btrfs_release_path(root, path);
5870 BUG_ON(err);
5871
5872 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5873 BUG_ON(ret);
5874
Yan, Zheng76dda932009-09-21 16:00:26 -04005875 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5876 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5877 NULL, NULL);
5878 BUG_ON(ret < 0);
5879 if (ret > 0) {
5880 ret = btrfs_del_orphan_item(trans, tree_root,
5881 root->root_key.objectid);
5882 BUG_ON(ret);
5883 }
5884 }
5885
5886 if (root->in_radix) {
5887 btrfs_free_fs_root(tree_root->fs_info, root);
5888 } else {
5889 free_extent_buffer(root->node);
5890 free_extent_buffer(root->commit_root);
5891 kfree(root);
5892 }
Chris Mason9f3a7422007-08-07 15:52:19 -04005893out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005894 btrfs_end_transaction(trans, tree_root);
5895 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005896 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005897 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005898}
Chris Mason9078a3e2007-04-26 16:46:15 -04005899
Yan Zheng2c47e6052009-06-27 21:07:35 -04005900/*
5901 * drop subtree rooted at tree block 'node'.
5902 *
5903 * NOTE: this function will unlock and release tree block 'node'
5904 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005905int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5906 struct btrfs_root *root,
5907 struct extent_buffer *node,
5908 struct extent_buffer *parent)
5909{
5910 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005911 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005912 int level;
5913 int parent_level;
5914 int ret = 0;
5915 int wret;
5916
Yan Zheng2c47e6052009-06-27 21:07:35 -04005917 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5918
Yan Zhengf82d02d2008-10-29 14:49:05 -04005919 path = btrfs_alloc_path();
5920 BUG_ON(!path);
5921
Yan Zheng2c47e6052009-06-27 21:07:35 -04005922 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5923 BUG_ON(!wc);
5924
Chris Masonb9447ef2009-03-09 11:45:38 -04005925 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005926 parent_level = btrfs_header_level(parent);
5927 extent_buffer_get(parent);
5928 path->nodes[parent_level] = parent;
5929 path->slots[parent_level] = btrfs_header_nritems(parent);
5930
Chris Masonb9447ef2009-03-09 11:45:38 -04005931 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005932 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005933 path->nodes[level] = node;
5934 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005935 path->locks[level] = 1;
5936
5937 wc->refs[parent_level] = 1;
5938 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5939 wc->level = level;
5940 wc->shared_level = -1;
5941 wc->stage = DROP_REFERENCE;
5942 wc->update_ref = 0;
5943 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005944 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005945
5946 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005947 wret = walk_down_tree(trans, root, path, wc);
5948 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005949 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005950 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005951 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005952
Yan Zheng2c47e6052009-06-27 21:07:35 -04005953 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005954 if (wret < 0)
5955 ret = wret;
5956 if (wret != 0)
5957 break;
5958 }
5959
Yan Zheng2c47e6052009-06-27 21:07:35 -04005960 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005961 btrfs_free_path(path);
5962 return ret;
5963}
5964
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005965#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005966static unsigned long calc_ra(unsigned long start, unsigned long last,
5967 unsigned long nr)
5968{
5969 return min(last, start + nr - 1);
5970}
5971
Chris Masond3977122009-01-05 21:25:51 -05005972static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005973 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005974{
5975 u64 page_start;
5976 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005977 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005978 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005979 unsigned long i;
5980 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005981 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005982 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005983 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005984 unsigned int total_read = 0;
5985 unsigned int total_dirty = 0;
5986 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005987
5988 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005989
5990 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005991 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005992 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5993
Zheng Yan1a40e232008-09-26 10:09:34 -04005994 /* make sure the dirty trick played by the caller work */
5995 ret = invalidate_inode_pages2_range(inode->i_mapping,
5996 first_index, last_index);
5997 if (ret)
5998 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005999
Chris Mason4313b392008-01-03 09:08:48 -05006000 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05006001
Zheng Yan1a40e232008-09-26 10:09:34 -04006002 for (i = first_index ; i <= last_index; i++) {
6003 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04006004 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04006005 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04006006 }
6007 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04006008again:
6009 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04006010 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05006011 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04006012 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006013 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006014 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04006015 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006016 if (!PageUptodate(page)) {
6017 btrfs_readpage(NULL, page);
6018 lock_page(page);
6019 if (!PageUptodate(page)) {
6020 unlock_page(page);
6021 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006022 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05006023 goto out_unlock;
6024 }
6025 }
Chris Masonec44a352008-04-28 15:29:52 -04006026 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04006027
Chris Masonedbd8d42007-12-21 16:27:24 -05006028 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6029 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006030 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006031
Chris Mason3eaa2882008-07-24 11:57:52 -04006032 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6033 if (ordered) {
6034 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6035 unlock_page(page);
6036 page_cache_release(page);
6037 btrfs_start_ordered_extent(inode, ordered, 1);
6038 btrfs_put_ordered_extent(ordered);
6039 goto again;
6040 }
6041 set_page_extent_mapped(page);
6042
Zheng Yan1a40e232008-09-26 10:09:34 -04006043 if (i == first_index)
6044 set_extent_bits(io_tree, page_start, page_end,
6045 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006046 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04006047
Chris Masona061fc82008-05-07 11:43:44 -04006048 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006049 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006050
Chris Masond1310b22008-01-24 16:13:08 -05006051 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006052 unlock_page(page);
6053 page_cache_release(page);
6054 }
6055
6056out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04006057 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05006058 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006059 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04006060 return ret;
6061}
6062
Chris Masond3977122009-01-05 21:25:51 -05006063static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006064 struct btrfs_key *extent_key,
6065 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05006066{
Zheng Yan1a40e232008-09-26 10:09:34 -04006067 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6068 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6069 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04006070 u64 start = extent_key->objectid - offset;
6071 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006072
6073 em = alloc_extent_map(GFP_NOFS);
6074 BUG_ON(!em || IS_ERR(em));
6075
Yan Zheng66435582008-10-30 14:19:50 -04006076 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006077 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04006078 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04006079 em->block_start = extent_key->objectid;
6080 em->bdev = root->fs_info->fs_devices->latest_bdev;
6081 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6082
6083 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04006084 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006085 while (1) {
6086 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04006087 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006088 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04006089 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006090 if (ret != -EEXIST) {
6091 free_extent_map(em);
6092 break;
6093 }
Yan Zheng66435582008-10-30 14:19:50 -04006094 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006095 }
Yan Zheng66435582008-10-30 14:19:50 -04006096 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006097
Yan Zheng66435582008-10-30 14:19:50 -04006098 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04006099}
6100
6101struct btrfs_ref_path {
6102 u64 extent_start;
6103 u64 nodes[BTRFS_MAX_LEVEL];
6104 u64 root_objectid;
6105 u64 root_generation;
6106 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006107 u32 num_refs;
6108 int lowest_level;
6109 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006110 int shared_level;
6111
6112 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6113 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04006114};
6115
6116struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04006117 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04006118 u64 disk_bytenr;
6119 u64 disk_num_bytes;
6120 u64 offset;
6121 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04006122 u8 compression;
6123 u8 encryption;
6124 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04006125};
6126
6127static int is_cowonly_root(u64 root_objectid)
6128{
6129 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6130 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6131 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6132 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05006133 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6134 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04006135 return 1;
6136 return 0;
6137}
6138
Chris Masond3977122009-01-05 21:25:51 -05006139static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006140 struct btrfs_root *extent_root,
6141 struct btrfs_ref_path *ref_path,
6142 int first_time)
6143{
6144 struct extent_buffer *leaf;
6145 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05006146 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05006147 struct btrfs_key key;
6148 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04006149 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05006150 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04006151 int level;
6152 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006153
Zheng Yan1a40e232008-09-26 10:09:34 -04006154 path = btrfs_alloc_path();
6155 if (!path)
6156 return -ENOMEM;
6157
Zheng Yan1a40e232008-09-26 10:09:34 -04006158 if (first_time) {
6159 ref_path->lowest_level = -1;
6160 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006161 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006162 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04006163 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006164walk_down:
6165 level = ref_path->current_level - 1;
6166 while (level >= -1) {
6167 u64 parent;
6168 if (level < ref_path->lowest_level)
6169 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006170
Chris Masond3977122009-01-05 21:25:51 -05006171 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006172 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006173 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006174 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006175 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006176
Zheng Yan1a40e232008-09-26 10:09:34 -04006177 parent = ref_path->nodes[level + 1];
6178 ref_path->nodes[level + 1] = 0;
6179 ref_path->current_level = level;
6180 BUG_ON(parent == 0);
6181
6182 key.objectid = bytenr;
6183 key.offset = parent + 1;
6184 key.type = BTRFS_EXTENT_REF_KEY;
6185
6186 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006187 if (ret < 0)
6188 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006189 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006190
Chris Masonedbd8d42007-12-21 16:27:24 -05006191 leaf = path->nodes[0];
6192 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04006193 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04006194 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04006195 if (ret < 0)
6196 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006197 if (ret > 0)
6198 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04006199 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04006200 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006201
6202 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04006203 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04006204 found_key.type == BTRFS_EXTENT_REF_KEY) {
6205 if (level < ref_path->shared_level)
6206 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006207 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006208 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006209next:
6210 level--;
6211 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006212 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006213 }
6214 /* reached lowest level */
6215 ret = 1;
6216 goto out;
6217walk_up:
6218 level = ref_path->current_level;
6219 while (level < BTRFS_MAX_LEVEL - 1) {
6220 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006221
6222 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006223 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006224 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006225 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006226
Zheng Yan1a40e232008-09-26 10:09:34 -04006227 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006228
Zheng Yan1a40e232008-09-26 10:09:34 -04006229 key.objectid = bytenr;
6230 key.offset = 0;
6231 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006232
Zheng Yan1a40e232008-09-26 10:09:34 -04006233 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6234 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006235 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006236
6237 leaf = path->nodes[0];
6238 nritems = btrfs_header_nritems(leaf);
6239 if (path->slots[0] >= nritems) {
6240 ret = btrfs_next_leaf(extent_root, path);
6241 if (ret < 0)
6242 goto out;
6243 if (ret > 0) {
6244 /* the extent was freed by someone */
6245 if (ref_path->lowest_level == level)
6246 goto out;
6247 btrfs_release_path(extent_root, path);
6248 goto walk_down;
6249 }
6250 leaf = path->nodes[0];
6251 }
6252
6253 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6254 if (found_key.objectid != bytenr ||
6255 found_key.type != BTRFS_EXTENT_REF_KEY) {
6256 /* the extent was freed by someone */
6257 if (ref_path->lowest_level == level) {
6258 ret = 1;
6259 goto out;
6260 }
6261 btrfs_release_path(extent_root, path);
6262 goto walk_down;
6263 }
6264found:
6265 ref = btrfs_item_ptr(leaf, path->slots[0],
6266 struct btrfs_extent_ref);
6267 ref_objectid = btrfs_ref_objectid(leaf, ref);
6268 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6269 if (first_time) {
6270 level = (int)ref_objectid;
6271 BUG_ON(level >= BTRFS_MAX_LEVEL);
6272 ref_path->lowest_level = level;
6273 ref_path->current_level = level;
6274 ref_path->nodes[level] = bytenr;
6275 } else {
6276 WARN_ON(ref_objectid != level);
6277 }
6278 } else {
6279 WARN_ON(level != -1);
6280 }
6281 first_time = 0;
6282
6283 if (ref_path->lowest_level == level) {
6284 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006285 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6286 }
6287
6288 /*
6289 * the block is tree root or the block isn't in reference
6290 * counted tree.
6291 */
6292 if (found_key.objectid == found_key.offset ||
6293 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6294 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6295 ref_path->root_generation =
6296 btrfs_ref_generation(leaf, ref);
6297 if (level < 0) {
6298 /* special reference from the tree log */
6299 ref_path->nodes[0] = found_key.offset;
6300 ref_path->current_level = 0;
6301 }
6302 ret = 0;
6303 goto out;
6304 }
6305
6306 level++;
6307 BUG_ON(ref_path->nodes[level] != 0);
6308 ref_path->nodes[level] = found_key.offset;
6309 ref_path->current_level = level;
6310
6311 /*
6312 * the reference was created in the running transaction,
6313 * no need to continue walking up.
6314 */
6315 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6316 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6317 ref_path->root_generation =
6318 btrfs_ref_generation(leaf, ref);
6319 ret = 0;
6320 goto out;
6321 }
6322
6323 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006324 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006325 }
6326 /* reached max tree level, but no tree root found. */
6327 BUG();
6328out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006329 btrfs_free_path(path);
6330 return ret;
6331}
6332
6333static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6334 struct btrfs_root *extent_root,
6335 struct btrfs_ref_path *ref_path,
6336 u64 extent_start)
6337{
6338 memset(ref_path, 0, sizeof(*ref_path));
6339 ref_path->extent_start = extent_start;
6340
6341 return __next_ref_path(trans, extent_root, ref_path, 1);
6342}
6343
6344static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6345 struct btrfs_root *extent_root,
6346 struct btrfs_ref_path *ref_path)
6347{
6348 return __next_ref_path(trans, extent_root, ref_path, 0);
6349}
6350
Chris Masond3977122009-01-05 21:25:51 -05006351static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006352 struct btrfs_key *extent_key,
6353 u64 offset, int no_fragment,
6354 struct disk_extent **extents,
6355 int *nr_extents)
6356{
6357 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6358 struct btrfs_path *path;
6359 struct btrfs_file_extent_item *fi;
6360 struct extent_buffer *leaf;
6361 struct disk_extent *exts = *extents;
6362 struct btrfs_key found_key;
6363 u64 cur_pos;
6364 u64 last_byte;
6365 u32 nritems;
6366 int nr = 0;
6367 int max = *nr_extents;
6368 int ret;
6369
6370 WARN_ON(!no_fragment && *extents);
6371 if (!exts) {
6372 max = 1;
6373 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6374 if (!exts)
6375 return -ENOMEM;
6376 }
6377
6378 path = btrfs_alloc_path();
6379 BUG_ON(!path);
6380
6381 cur_pos = extent_key->objectid - offset;
6382 last_byte = extent_key->objectid + extent_key->offset;
6383 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6384 cur_pos, 0);
6385 if (ret < 0)
6386 goto out;
6387 if (ret > 0) {
6388 ret = -ENOENT;
6389 goto out;
6390 }
6391
6392 while (1) {
6393 leaf = path->nodes[0];
6394 nritems = btrfs_header_nritems(leaf);
6395 if (path->slots[0] >= nritems) {
6396 ret = btrfs_next_leaf(root, path);
6397 if (ret < 0)
6398 goto out;
6399 if (ret > 0)
6400 break;
6401 leaf = path->nodes[0];
6402 }
6403
6404 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6405 if (found_key.offset != cur_pos ||
6406 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6407 found_key.objectid != reloc_inode->i_ino)
6408 break;
6409
6410 fi = btrfs_item_ptr(leaf, path->slots[0],
6411 struct btrfs_file_extent_item);
6412 if (btrfs_file_extent_type(leaf, fi) !=
6413 BTRFS_FILE_EXTENT_REG ||
6414 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6415 break;
6416
6417 if (nr == max) {
6418 struct disk_extent *old = exts;
6419 max *= 2;
6420 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6421 memcpy(exts, old, sizeof(*exts) * nr);
6422 if (old != *extents)
6423 kfree(old);
6424 }
6425
6426 exts[nr].disk_bytenr =
6427 btrfs_file_extent_disk_bytenr(leaf, fi);
6428 exts[nr].disk_num_bytes =
6429 btrfs_file_extent_disk_num_bytes(leaf, fi);
6430 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6431 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04006432 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6433 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6434 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6435 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6436 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04006437 BUG_ON(exts[nr].offset > 0);
6438 BUG_ON(exts[nr].compression || exts[nr].encryption);
6439 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006440
6441 cur_pos += exts[nr].num_bytes;
6442 nr++;
6443
6444 if (cur_pos + offset >= last_byte)
6445 break;
6446
6447 if (no_fragment) {
6448 ret = 1;
6449 goto out;
6450 }
6451 path->slots[0]++;
6452 }
6453
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006454 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04006455 if (cur_pos + offset < last_byte) {
6456 ret = -ENOENT;
6457 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05006458 }
6459 ret = 0;
6460out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006461 btrfs_free_path(path);
6462 if (ret) {
6463 if (exts != *extents)
6464 kfree(exts);
6465 } else {
6466 *extents = exts;
6467 *nr_extents = nr;
6468 }
6469 return ret;
6470}
6471
Chris Masond3977122009-01-05 21:25:51 -05006472static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006473 struct btrfs_root *root,
6474 struct btrfs_path *path,
6475 struct btrfs_key *extent_key,
6476 struct btrfs_key *leaf_key,
6477 struct btrfs_ref_path *ref_path,
6478 struct disk_extent *new_extents,
6479 int nr_extents)
6480{
6481 struct extent_buffer *leaf;
6482 struct btrfs_file_extent_item *fi;
6483 struct inode *inode = NULL;
6484 struct btrfs_key key;
6485 u64 lock_start = 0;
6486 u64 lock_end = 0;
6487 u64 num_bytes;
6488 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05006489 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006490 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006491 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006492 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04006493 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04006494 int ret;
6495
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006496 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04006497 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006498 if (key.objectid < ref_path->owner_objectid ||
6499 (key.objectid == ref_path->owner_objectid &&
6500 key.type < BTRFS_EXTENT_DATA_KEY)) {
6501 key.objectid = ref_path->owner_objectid;
6502 key.type = BTRFS_EXTENT_DATA_KEY;
6503 key.offset = 0;
6504 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006505 }
6506
6507 while (1) {
6508 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6509 if (ret < 0)
6510 goto out;
6511
6512 leaf = path->nodes[0];
6513 nritems = btrfs_header_nritems(leaf);
6514next:
6515 if (extent_locked && ret > 0) {
6516 /*
6517 * the file extent item was modified by someone
6518 * before the extent got locked.
6519 */
Zheng Yan1a40e232008-09-26 10:09:34 -04006520 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6521 lock_end, GFP_NOFS);
6522 extent_locked = 0;
6523 }
6524
6525 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006526 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04006527 break;
6528
6529 BUG_ON(extent_locked);
6530 ret = btrfs_next_leaf(root, path);
6531 if (ret < 0)
6532 goto out;
6533 if (ret > 0)
6534 break;
6535 leaf = path->nodes[0];
6536 nritems = btrfs_header_nritems(leaf);
6537 }
6538
6539 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6540
6541 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6542 if ((key.objectid > ref_path->owner_objectid) ||
6543 (key.objectid == ref_path->owner_objectid &&
6544 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05006545 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006546 break;
6547 }
6548
6549 if (inode && key.objectid != inode->i_ino) {
6550 BUG_ON(extent_locked);
6551 btrfs_release_path(root, path);
6552 mutex_unlock(&inode->i_mutex);
6553 iput(inode);
6554 inode = NULL;
6555 continue;
6556 }
6557
6558 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6559 path->slots[0]++;
6560 ret = 1;
6561 goto next;
6562 }
6563 fi = btrfs_item_ptr(leaf, path->slots[0],
6564 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04006565 extent_type = btrfs_file_extent_type(leaf, fi);
6566 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6567 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04006568 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6569 extent_key->objectid)) {
6570 path->slots[0]++;
6571 ret = 1;
6572 goto next;
6573 }
6574
6575 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6576 ext_offset = btrfs_file_extent_offset(leaf, fi);
6577
Yan Zheng86288a12009-01-21 10:49:16 -05006578 if (search_end == (u64)-1) {
6579 search_end = key.offset - ext_offset +
6580 btrfs_file_extent_ram_bytes(leaf, fi);
6581 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006582
6583 if (!extent_locked) {
6584 lock_start = key.offset;
6585 lock_end = lock_start + num_bytes - 1;
6586 } else {
Yan Zheng66435582008-10-30 14:19:50 -04006587 if (lock_start > key.offset ||
6588 lock_end + 1 < key.offset + num_bytes) {
6589 unlock_extent(&BTRFS_I(inode)->io_tree,
6590 lock_start, lock_end, GFP_NOFS);
6591 extent_locked = 0;
6592 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006593 }
6594
6595 if (!inode) {
6596 btrfs_release_path(root, path);
6597
6598 inode = btrfs_iget_locked(root->fs_info->sb,
6599 key.objectid, root);
6600 if (inode->i_state & I_NEW) {
6601 BTRFS_I(inode)->root = root;
6602 BTRFS_I(inode)->location.objectid =
6603 key.objectid;
6604 BTRFS_I(inode)->location.type =
6605 BTRFS_INODE_ITEM_KEY;
6606 BTRFS_I(inode)->location.offset = 0;
6607 btrfs_read_locked_inode(inode);
6608 unlock_new_inode(inode);
6609 }
6610 /*
6611 * some code call btrfs_commit_transaction while
6612 * holding the i_mutex, so we can't use mutex_lock
6613 * here.
6614 */
6615 if (is_bad_inode(inode) ||
6616 !mutex_trylock(&inode->i_mutex)) {
6617 iput(inode);
6618 inode = NULL;
6619 key.offset = (u64)-1;
6620 goto skip;
6621 }
6622 }
6623
6624 if (!extent_locked) {
6625 struct btrfs_ordered_extent *ordered;
6626
6627 btrfs_release_path(root, path);
6628
6629 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6630 lock_end, GFP_NOFS);
6631 ordered = btrfs_lookup_first_ordered_extent(inode,
6632 lock_end);
6633 if (ordered &&
6634 ordered->file_offset <= lock_end &&
6635 ordered->file_offset + ordered->len > lock_start) {
6636 unlock_extent(&BTRFS_I(inode)->io_tree,
6637 lock_start, lock_end, GFP_NOFS);
6638 btrfs_start_ordered_extent(inode, ordered, 1);
6639 btrfs_put_ordered_extent(ordered);
6640 key.offset += num_bytes;
6641 goto skip;
6642 }
6643 if (ordered)
6644 btrfs_put_ordered_extent(ordered);
6645
Zheng Yan1a40e232008-09-26 10:09:34 -04006646 extent_locked = 1;
6647 continue;
6648 }
6649
6650 if (nr_extents == 1) {
6651 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04006652 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6653 new_extents[0].disk_bytenr);
6654 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6655 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006656 btrfs_mark_buffer_dirty(leaf);
6657
6658 btrfs_drop_extent_cache(inode, key.offset,
6659 key.offset + num_bytes - 1, 0);
6660
6661 ret = btrfs_inc_extent_ref(trans, root,
6662 new_extents[0].disk_bytenr,
6663 new_extents[0].disk_num_bytes,
6664 leaf->start,
6665 root->root_key.objectid,
6666 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006667 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006668 BUG_ON(ret);
6669
6670 ret = btrfs_free_extent(trans, root,
6671 extent_key->objectid,
6672 extent_key->offset,
6673 leaf->start,
6674 btrfs_header_owner(leaf),
6675 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006676 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006677 BUG_ON(ret);
6678
6679 btrfs_release_path(root, path);
6680 key.offset += num_bytes;
6681 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04006682 BUG_ON(1);
6683#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006684 u64 alloc_hint;
6685 u64 extent_len;
6686 int i;
6687 /*
6688 * drop old extent pointer at first, then insert the
6689 * new pointers one bye one
6690 */
6691 btrfs_release_path(root, path);
6692 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6693 key.offset + num_bytes,
6694 key.offset, &alloc_hint);
6695 BUG_ON(ret);
6696
6697 for (i = 0; i < nr_extents; i++) {
6698 if (ext_offset >= new_extents[i].num_bytes) {
6699 ext_offset -= new_extents[i].num_bytes;
6700 continue;
6701 }
6702 extent_len = min(new_extents[i].num_bytes -
6703 ext_offset, num_bytes);
6704
6705 ret = btrfs_insert_empty_item(trans, root,
6706 path, &key,
6707 sizeof(*fi));
6708 BUG_ON(ret);
6709
6710 leaf = path->nodes[0];
6711 fi = btrfs_item_ptr(leaf, path->slots[0],
6712 struct btrfs_file_extent_item);
6713 btrfs_set_file_extent_generation(leaf, fi,
6714 trans->transid);
6715 btrfs_set_file_extent_type(leaf, fi,
6716 BTRFS_FILE_EXTENT_REG);
6717 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6718 new_extents[i].disk_bytenr);
6719 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6720 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04006721 btrfs_set_file_extent_ram_bytes(leaf, fi,
6722 new_extents[i].ram_bytes);
6723
6724 btrfs_set_file_extent_compression(leaf, fi,
6725 new_extents[i].compression);
6726 btrfs_set_file_extent_encryption(leaf, fi,
6727 new_extents[i].encryption);
6728 btrfs_set_file_extent_other_encoding(leaf, fi,
6729 new_extents[i].other_encoding);
6730
Zheng Yan1a40e232008-09-26 10:09:34 -04006731 btrfs_set_file_extent_num_bytes(leaf, fi,
6732 extent_len);
6733 ext_offset += new_extents[i].offset;
6734 btrfs_set_file_extent_offset(leaf, fi,
6735 ext_offset);
6736 btrfs_mark_buffer_dirty(leaf);
6737
6738 btrfs_drop_extent_cache(inode, key.offset,
6739 key.offset + extent_len - 1, 0);
6740
6741 ret = btrfs_inc_extent_ref(trans, root,
6742 new_extents[i].disk_bytenr,
6743 new_extents[i].disk_num_bytes,
6744 leaf->start,
6745 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006746 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006747 BUG_ON(ret);
6748 btrfs_release_path(root, path);
6749
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006750 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04006751
6752 ext_offset = 0;
6753 num_bytes -= extent_len;
6754 key.offset += extent_len;
6755
6756 if (num_bytes == 0)
6757 break;
6758 }
6759 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006760#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04006761 }
6762
6763 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006764 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6765 lock_end, GFP_NOFS);
6766 extent_locked = 0;
6767 }
6768skip:
6769 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05006770 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006771 break;
6772
6773 cond_resched();
6774 }
6775 ret = 0;
6776out:
6777 btrfs_release_path(root, path);
6778 if (inode) {
6779 mutex_unlock(&inode->i_mutex);
6780 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006781 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6782 lock_end, GFP_NOFS);
6783 }
6784 iput(inode);
6785 }
6786 return ret;
6787}
6788
Zheng Yan1a40e232008-09-26 10:09:34 -04006789int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6790 struct btrfs_root *root,
6791 struct extent_buffer *buf, u64 orig_start)
6792{
6793 int level;
6794 int ret;
6795
6796 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6797 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6798
6799 level = btrfs_header_level(buf);
6800 if (level == 0) {
6801 struct btrfs_leaf_ref *ref;
6802 struct btrfs_leaf_ref *orig_ref;
6803
6804 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6805 if (!orig_ref)
6806 return -ENOENT;
6807
6808 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6809 if (!ref) {
6810 btrfs_free_leaf_ref(root, orig_ref);
6811 return -ENOMEM;
6812 }
6813
6814 ref->nritems = orig_ref->nritems;
6815 memcpy(ref->extents, orig_ref->extents,
6816 sizeof(ref->extents[0]) * ref->nritems);
6817
6818 btrfs_free_leaf_ref(root, orig_ref);
6819
6820 ref->root_gen = trans->transid;
6821 ref->bytenr = buf->start;
6822 ref->owner = btrfs_header_owner(buf);
6823 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006824
Zheng Yan1a40e232008-09-26 10:09:34 -04006825 ret = btrfs_add_leaf_ref(root, ref, 0);
6826 WARN_ON(ret);
6827 btrfs_free_leaf_ref(root, ref);
6828 }
6829 return 0;
6830}
6831
Chris Masond3977122009-01-05 21:25:51 -05006832static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006833 struct extent_buffer *leaf,
6834 struct btrfs_block_group_cache *group,
6835 struct btrfs_root *target_root)
6836{
6837 struct btrfs_key key;
6838 struct inode *inode = NULL;
6839 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006840 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04006841 u64 num_bytes;
6842 u64 skip_objectid = 0;
6843 u32 nritems;
6844 u32 i;
6845
6846 nritems = btrfs_header_nritems(leaf);
6847 for (i = 0; i < nritems; i++) {
6848 btrfs_item_key_to_cpu(leaf, &key, i);
6849 if (key.objectid == skip_objectid ||
6850 key.type != BTRFS_EXTENT_DATA_KEY)
6851 continue;
6852 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6853 if (btrfs_file_extent_type(leaf, fi) ==
6854 BTRFS_FILE_EXTENT_INLINE)
6855 continue;
6856 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6857 continue;
6858 if (!inode || inode->i_ino != key.objectid) {
6859 iput(inode);
6860 inode = btrfs_ilookup(target_root->fs_info->sb,
6861 key.objectid, target_root, 1);
6862 }
6863 if (!inode) {
6864 skip_objectid = key.objectid;
6865 continue;
6866 }
6867 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6868
Josef Bacik2ac55d42010-02-03 19:33:23 +00006869 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
6870 key.offset + num_bytes - 1, 0, &cached_state,
6871 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006872 btrfs_drop_extent_cache(inode, key.offset,
6873 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006874 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
6875 key.offset + num_bytes - 1, &cached_state,
6876 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006877 cond_resched();
6878 }
6879 iput(inode);
6880 return 0;
6881}
6882
Chris Masond3977122009-01-05 21:25:51 -05006883static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006884 struct btrfs_root *root,
6885 struct extent_buffer *leaf,
6886 struct btrfs_block_group_cache *group,
6887 struct inode *reloc_inode)
6888{
6889 struct btrfs_key key;
6890 struct btrfs_key extent_key;
6891 struct btrfs_file_extent_item *fi;
6892 struct btrfs_leaf_ref *ref;
6893 struct disk_extent *new_extent;
6894 u64 bytenr;
6895 u64 num_bytes;
6896 u32 nritems;
6897 u32 i;
6898 int ext_index;
6899 int nr_extent;
6900 int ret;
6901
6902 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6903 BUG_ON(!new_extent);
6904
6905 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6906 BUG_ON(!ref);
6907
6908 ext_index = -1;
6909 nritems = btrfs_header_nritems(leaf);
6910 for (i = 0; i < nritems; i++) {
6911 btrfs_item_key_to_cpu(leaf, &key, i);
6912 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6913 continue;
6914 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6915 if (btrfs_file_extent_type(leaf, fi) ==
6916 BTRFS_FILE_EXTENT_INLINE)
6917 continue;
6918 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6919 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6920 if (bytenr == 0)
6921 continue;
6922
6923 ext_index++;
6924 if (bytenr >= group->key.objectid + group->key.offset ||
6925 bytenr + num_bytes <= group->key.objectid)
6926 continue;
6927
6928 extent_key.objectid = bytenr;
6929 extent_key.offset = num_bytes;
6930 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6931 nr_extent = 1;
6932 ret = get_new_locations(reloc_inode, &extent_key,
6933 group->key.objectid, 1,
6934 &new_extent, &nr_extent);
6935 if (ret > 0)
6936 continue;
6937 BUG_ON(ret < 0);
6938
6939 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6940 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6941 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6942 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6943
Zheng Yan1a40e232008-09-26 10:09:34 -04006944 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6945 new_extent->disk_bytenr);
6946 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6947 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006948 btrfs_mark_buffer_dirty(leaf);
6949
6950 ret = btrfs_inc_extent_ref(trans, root,
6951 new_extent->disk_bytenr,
6952 new_extent->disk_num_bytes,
6953 leaf->start,
6954 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006955 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006956 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006957
Zheng Yan1a40e232008-09-26 10:09:34 -04006958 ret = btrfs_free_extent(trans, root,
6959 bytenr, num_bytes, leaf->start,
6960 btrfs_header_owner(leaf),
6961 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006962 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006963 BUG_ON(ret);
6964 cond_resched();
6965 }
6966 kfree(new_extent);
6967 BUG_ON(ext_index + 1 != ref->nritems);
6968 btrfs_free_leaf_ref(root, ref);
6969 return 0;
6970}
6971
Yan Zhengf82d02d2008-10-29 14:49:05 -04006972int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6973 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006974{
6975 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006976 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006977
6978 if (root->reloc_root) {
6979 reloc_root = root->reloc_root;
6980 root->reloc_root = NULL;
6981 list_add(&reloc_root->dead_list,
6982 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006983
6984 btrfs_set_root_bytenr(&reloc_root->root_item,
6985 reloc_root->node->start);
6986 btrfs_set_root_level(&root->root_item,
6987 btrfs_header_level(reloc_root->node));
6988 memset(&reloc_root->root_item.drop_progress, 0,
6989 sizeof(struct btrfs_disk_key));
6990 reloc_root->root_item.drop_level = 0;
6991
6992 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6993 &reloc_root->root_key,
6994 &reloc_root->root_item);
6995 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006996 }
6997 return 0;
6998}
6999
7000int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7001{
7002 struct btrfs_trans_handle *trans;
7003 struct btrfs_root *reloc_root;
7004 struct btrfs_root *prev_root = NULL;
7005 struct list_head dead_roots;
7006 int ret;
7007 unsigned long nr;
7008
7009 INIT_LIST_HEAD(&dead_roots);
7010 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7011
7012 while (!list_empty(&dead_roots)) {
7013 reloc_root = list_entry(dead_roots.prev,
7014 struct btrfs_root, dead_list);
7015 list_del_init(&reloc_root->dead_list);
7016
7017 BUG_ON(reloc_root->commit_root != NULL);
7018 while (1) {
7019 trans = btrfs_join_transaction(root, 1);
7020 BUG_ON(!trans);
7021
7022 mutex_lock(&root->fs_info->drop_mutex);
7023 ret = btrfs_drop_snapshot(trans, reloc_root);
7024 if (ret != -EAGAIN)
7025 break;
7026 mutex_unlock(&root->fs_info->drop_mutex);
7027
7028 nr = trans->blocks_used;
7029 ret = btrfs_end_transaction(trans, root);
7030 BUG_ON(ret);
7031 btrfs_btree_balance_dirty(root, nr);
7032 }
7033
7034 free_extent_buffer(reloc_root->node);
7035
7036 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7037 &reloc_root->root_key);
7038 BUG_ON(ret);
7039 mutex_unlock(&root->fs_info->drop_mutex);
7040
7041 nr = trans->blocks_used;
7042 ret = btrfs_end_transaction(trans, root);
7043 BUG_ON(ret);
7044 btrfs_btree_balance_dirty(root, nr);
7045
7046 kfree(prev_root);
7047 prev_root = reloc_root;
7048 }
7049 if (prev_root) {
7050 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7051 kfree(prev_root);
7052 }
7053 return 0;
7054}
7055
7056int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7057{
7058 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7059 return 0;
7060}
7061
7062int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7063{
7064 struct btrfs_root *reloc_root;
7065 struct btrfs_trans_handle *trans;
7066 struct btrfs_key location;
7067 int found;
7068 int ret;
7069
7070 mutex_lock(&root->fs_info->tree_reloc_mutex);
7071 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7072 BUG_ON(ret);
7073 found = !list_empty(&root->fs_info->dead_reloc_roots);
7074 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7075
7076 if (found) {
7077 trans = btrfs_start_transaction(root, 1);
7078 BUG_ON(!trans);
7079 ret = btrfs_commit_transaction(trans, root);
7080 BUG_ON(ret);
7081 }
7082
7083 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7084 location.offset = (u64)-1;
7085 location.type = BTRFS_ROOT_ITEM_KEY;
7086
7087 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7088 BUG_ON(!reloc_root);
7089 btrfs_orphan_cleanup(reloc_root);
7090 return 0;
7091}
7092
Chris Masond3977122009-01-05 21:25:51 -05007093static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007094 struct btrfs_root *root)
7095{
7096 struct btrfs_root *reloc_root;
7097 struct extent_buffer *eb;
7098 struct btrfs_root_item *root_item;
7099 struct btrfs_key root_key;
7100 int ret;
7101
7102 BUG_ON(!root->ref_cows);
7103 if (root->reloc_root)
7104 return 0;
7105
7106 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7107 BUG_ON(!root_item);
7108
7109 ret = btrfs_copy_root(trans, root, root->commit_root,
7110 &eb, BTRFS_TREE_RELOC_OBJECTID);
7111 BUG_ON(ret);
7112
7113 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7114 root_key.offset = root->root_key.objectid;
7115 root_key.type = BTRFS_ROOT_ITEM_KEY;
7116
7117 memcpy(root_item, &root->root_item, sizeof(root_item));
7118 btrfs_set_root_refs(root_item, 0);
7119 btrfs_set_root_bytenr(root_item, eb->start);
7120 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04007121 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007122
7123 btrfs_tree_unlock(eb);
7124 free_extent_buffer(eb);
7125
7126 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7127 &root_key, root_item);
7128 BUG_ON(ret);
7129 kfree(root_item);
7130
7131 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7132 &root_key);
7133 BUG_ON(!reloc_root);
7134 reloc_root->last_trans = trans->transid;
7135 reloc_root->commit_root = NULL;
7136 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7137
7138 root->reloc_root = reloc_root;
7139 return 0;
7140}
7141
7142/*
7143 * Core function of space balance.
7144 *
7145 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04007146 * counted roots. There is one reloc tree for each subvol, and all
7147 * reloc trees share same root key objectid. Reloc trees are snapshots
7148 * of the latest committed roots of subvols (root->commit_root).
7149 *
7150 * To relocate a tree block referenced by a subvol, there are two steps.
7151 * COW the block through subvol's reloc tree, then update block pointer
7152 * in the subvol to point to the new block. Since all reloc trees share
7153 * same root key objectid, doing special handing for tree blocks owned
7154 * by them is easy. Once a tree block has been COWed in one reloc tree,
7155 * we can use the resulting new block directly when the same block is
7156 * required to COW again through other reloc trees. By this way, relocated
7157 * tree blocks are shared between reloc trees, so they are also shared
7158 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04007159 */
Chris Masond3977122009-01-05 21:25:51 -05007160static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007161 struct btrfs_root *root,
7162 struct btrfs_path *path,
7163 struct btrfs_key *first_key,
7164 struct btrfs_ref_path *ref_path,
7165 struct btrfs_block_group_cache *group,
7166 struct inode *reloc_inode)
7167{
7168 struct btrfs_root *reloc_root;
7169 struct extent_buffer *eb = NULL;
7170 struct btrfs_key *keys;
7171 u64 *nodes;
7172 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007173 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04007174 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007175 int ret;
7176
7177 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7178 lowest_level = ref_path->owner_objectid;
7179
Yan Zhengf82d02d2008-10-29 14:49:05 -04007180 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007181 path->lowest_level = lowest_level;
7182 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7183 BUG_ON(ret < 0);
7184 path->lowest_level = 0;
7185 btrfs_release_path(root, path);
7186 return 0;
7187 }
7188
Zheng Yan1a40e232008-09-26 10:09:34 -04007189 mutex_lock(&root->fs_info->tree_reloc_mutex);
7190 ret = init_reloc_tree(trans, root);
7191 BUG_ON(ret);
7192 reloc_root = root->reloc_root;
7193
Yan Zhengf82d02d2008-10-29 14:49:05 -04007194 shared_level = ref_path->shared_level;
7195 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007196
Yan Zhengf82d02d2008-10-29 14:49:05 -04007197 keys = ref_path->node_keys;
7198 nodes = ref_path->new_nodes;
7199 memset(&keys[shared_level + 1], 0,
7200 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7201 memset(&nodes[shared_level + 1], 0,
7202 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04007203
Yan Zhengf82d02d2008-10-29 14:49:05 -04007204 if (nodes[lowest_level] == 0) {
7205 path->lowest_level = lowest_level;
7206 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7207 0, 1);
7208 BUG_ON(ret);
7209 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7210 eb = path->nodes[level];
7211 if (!eb || eb == reloc_root->node)
7212 break;
7213 nodes[level] = eb->start;
7214 if (level == 0)
7215 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7216 else
7217 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7218 }
Yan Zheng2b820322008-11-17 21:11:30 -05007219 if (nodes[0] &&
7220 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007221 eb = path->nodes[0];
7222 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7223 group, reloc_inode);
7224 BUG_ON(ret);
7225 }
7226 btrfs_release_path(reloc_root, path);
7227 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007228 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007229 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007230 BUG_ON(ret);
7231 }
7232
Zheng Yan1a40e232008-09-26 10:09:34 -04007233 /*
7234 * replace tree blocks in the fs tree with tree blocks in
7235 * the reloc tree.
7236 */
7237 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7238 BUG_ON(ret < 0);
7239
7240 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007241 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7242 0, 0);
7243 BUG_ON(ret);
7244 extent_buffer_get(path->nodes[0]);
7245 eb = path->nodes[0];
7246 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007247 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7248 BUG_ON(ret);
7249 free_extent_buffer(eb);
7250 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007251
Yan Zhengf82d02d2008-10-29 14:49:05 -04007252 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007253 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007254 return 0;
7255}
7256
Chris Masond3977122009-01-05 21:25:51 -05007257static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007258 struct btrfs_root *root,
7259 struct btrfs_path *path,
7260 struct btrfs_key *first_key,
7261 struct btrfs_ref_path *ref_path)
7262{
7263 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007264
7265 ret = relocate_one_path(trans, root, path, first_key,
7266 ref_path, NULL, NULL);
7267 BUG_ON(ret);
7268
Zheng Yan1a40e232008-09-26 10:09:34 -04007269 return 0;
7270}
7271
Chris Masond3977122009-01-05 21:25:51 -05007272static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007273 struct btrfs_root *extent_root,
7274 struct btrfs_path *path,
7275 struct btrfs_key *extent_key)
7276{
7277 int ret;
7278
Zheng Yan1a40e232008-09-26 10:09:34 -04007279 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7280 if (ret)
7281 goto out;
7282 ret = btrfs_del_item(trans, extent_root, path);
7283out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007284 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007285 return ret;
7286}
7287
Chris Masond3977122009-01-05 21:25:51 -05007288static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007289 struct btrfs_ref_path *ref_path)
7290{
7291 struct btrfs_key root_key;
7292
7293 root_key.objectid = ref_path->root_objectid;
7294 root_key.type = BTRFS_ROOT_ITEM_KEY;
7295 if (is_cowonly_root(ref_path->root_objectid))
7296 root_key.offset = 0;
7297 else
7298 root_key.offset = (u64)-1;
7299
7300 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7301}
7302
Chris Masond3977122009-01-05 21:25:51 -05007303static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007304 struct btrfs_path *path,
7305 struct btrfs_key *extent_key,
7306 struct btrfs_block_group_cache *group,
7307 struct inode *reloc_inode, int pass)
7308{
7309 struct btrfs_trans_handle *trans;
7310 struct btrfs_root *found_root;
7311 struct btrfs_ref_path *ref_path = NULL;
7312 struct disk_extent *new_extents = NULL;
7313 int nr_extents = 0;
7314 int loops;
7315 int ret;
7316 int level;
7317 struct btrfs_key first_key;
7318 u64 prev_block = 0;
7319
Zheng Yan1a40e232008-09-26 10:09:34 -04007320
7321 trans = btrfs_start_transaction(extent_root, 1);
7322 BUG_ON(!trans);
7323
7324 if (extent_key->objectid == 0) {
7325 ret = del_extent_zero(trans, extent_root, path, extent_key);
7326 goto out;
7327 }
7328
7329 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7330 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007331 ret = -ENOMEM;
7332 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007333 }
7334
7335 for (loops = 0; ; loops++) {
7336 if (loops == 0) {
7337 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7338 extent_key->objectid);
7339 } else {
7340 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7341 }
7342 if (ret < 0)
7343 goto out;
7344 if (ret > 0)
7345 break;
7346
7347 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7348 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7349 continue;
7350
7351 found_root = read_ref_root(extent_root->fs_info, ref_path);
7352 BUG_ON(!found_root);
7353 /*
7354 * for reference counted tree, only process reference paths
7355 * rooted at the latest committed root.
7356 */
7357 if (found_root->ref_cows &&
7358 ref_path->root_generation != found_root->root_key.offset)
7359 continue;
7360
7361 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7362 if (pass == 0) {
7363 /*
7364 * copy data extents to new locations
7365 */
7366 u64 group_start = group->key.objectid;
7367 ret = relocate_data_extent(reloc_inode,
7368 extent_key,
7369 group_start);
7370 if (ret < 0)
7371 goto out;
7372 break;
7373 }
7374 level = 0;
7375 } else {
7376 level = ref_path->owner_objectid;
7377 }
7378
7379 if (prev_block != ref_path->nodes[level]) {
7380 struct extent_buffer *eb;
7381 u64 block_start = ref_path->nodes[level];
7382 u64 block_size = btrfs_level_size(found_root, level);
7383
7384 eb = read_tree_block(found_root, block_start,
7385 block_size, 0);
7386 btrfs_tree_lock(eb);
7387 BUG_ON(level != btrfs_header_level(eb));
7388
7389 if (level == 0)
7390 btrfs_item_key_to_cpu(eb, &first_key, 0);
7391 else
7392 btrfs_node_key_to_cpu(eb, &first_key, 0);
7393
7394 btrfs_tree_unlock(eb);
7395 free_extent_buffer(eb);
7396 prev_block = block_start;
7397 }
7398
Yan Zheng24562422009-02-12 14:14:53 -05007399 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007400 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05007401 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007402 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7403 /*
7404 * try to update data extent references while
7405 * keeping metadata shared between snapshots.
7406 */
7407 if (pass == 1) {
7408 ret = relocate_one_path(trans, found_root,
7409 path, &first_key, ref_path,
7410 group, reloc_inode);
7411 if (ret < 0)
7412 goto out;
7413 continue;
7414 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007415 /*
7416 * use fallback method to process the remaining
7417 * references.
7418 */
7419 if (!new_extents) {
7420 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04007421 new_extents = kmalloc(sizeof(*new_extents),
7422 GFP_NOFS);
7423 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007424 ret = get_new_locations(reloc_inode,
7425 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04007426 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04007427 &new_extents,
7428 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007429 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04007430 goto out;
7431 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007432 ret = replace_one_extent(trans, found_root,
7433 path, extent_key,
7434 &first_key, ref_path,
7435 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05007436 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007437 ret = relocate_tree_block(trans, found_root, path,
7438 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007439 }
7440 if (ret < 0)
7441 goto out;
7442 }
7443 ret = 0;
7444out:
7445 btrfs_end_transaction(trans, extent_root);
7446 kfree(new_extents);
7447 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007448 return ret;
7449}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007450#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007451
Chris Masonec44a352008-04-28 15:29:52 -04007452static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7453{
7454 u64 num_devices;
7455 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7456 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7457
Yan Zheng2b820322008-11-17 21:11:30 -05007458 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04007459 if (num_devices == 1) {
7460 stripped |= BTRFS_BLOCK_GROUP_DUP;
7461 stripped = flags & ~stripped;
7462
7463 /* turn raid0 into single device chunks */
7464 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7465 return stripped;
7466
7467 /* turn mirroring into duplication */
7468 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7469 BTRFS_BLOCK_GROUP_RAID10))
7470 return stripped | BTRFS_BLOCK_GROUP_DUP;
7471 return flags;
7472 } else {
7473 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007474 if (flags & stripped)
7475 return flags;
7476
7477 stripped |= BTRFS_BLOCK_GROUP_DUP;
7478 stripped = flags & ~stripped;
7479
7480 /* switch duplicated blocks with raid1 */
7481 if (flags & BTRFS_BLOCK_GROUP_DUP)
7482 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7483
7484 /* turn single device chunks into raid0 */
7485 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7486 }
7487 return flags;
7488}
7489
Yan, Zhengf0486c62010-05-16 10:46:25 -04007490static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04007491{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007492 struct btrfs_space_info *sinfo = cache->space_info;
7493 u64 num_bytes;
7494 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007495
Yan, Zhengf0486c62010-05-16 10:46:25 -04007496 if (cache->ro)
7497 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04007498
Yan, Zhengf0486c62010-05-16 10:46:25 -04007499 spin_lock(&sinfo->lock);
7500 spin_lock(&cache->lock);
7501 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7502 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007503
Yan, Zhengf0486c62010-05-16 10:46:25 -04007504 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
7505 sinfo->bytes_may_use + sinfo->bytes_readonly +
7506 cache->reserved_pinned + num_bytes < sinfo->total_bytes) {
7507 sinfo->bytes_readonly += num_bytes;
7508 sinfo->bytes_reserved += cache->reserved_pinned;
7509 cache->reserved_pinned = 0;
7510 cache->ro = 1;
7511 ret = 0;
7512 }
7513 spin_unlock(&cache->lock);
7514 spin_unlock(&sinfo->lock);
7515 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007516}
7517
Yan, Zhengf0486c62010-05-16 10:46:25 -04007518int btrfs_set_block_group_ro(struct btrfs_root *root,
7519 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007520
7521{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007522 struct btrfs_trans_handle *trans;
7523 u64 alloc_flags;
7524 int ret;
7525
7526 BUG_ON(cache->ro);
7527
7528 trans = btrfs_join_transaction(root, 1);
7529 BUG_ON(IS_ERR(trans));
7530
7531 alloc_flags = update_block_group_flags(root, cache->flags);
7532 if (alloc_flags != cache->flags)
7533 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7534
7535 ret = set_block_group_ro(cache);
7536 if (!ret)
7537 goto out;
7538 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
7539 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7540 if (ret < 0)
7541 goto out;
7542 ret = set_block_group_ro(cache);
7543out:
7544 btrfs_end_transaction(trans, root);
7545 return ret;
7546}
7547
7548int btrfs_set_block_group_rw(struct btrfs_root *root,
7549 struct btrfs_block_group_cache *cache)
7550{
7551 struct btrfs_space_info *sinfo = cache->space_info;
7552 u64 num_bytes;
7553
7554 BUG_ON(!cache->ro);
7555
7556 spin_lock(&sinfo->lock);
7557 spin_lock(&cache->lock);
7558 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7559 cache->bytes_super - btrfs_block_group_used(&cache->item);
7560 sinfo->bytes_readonly -= num_bytes;
7561 cache->ro = 0;
7562 spin_unlock(&cache->lock);
7563 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007564 return 0;
7565}
7566
Josef Bacikba1bf482009-09-11 16:11:19 -04007567/*
7568 * checks to see if its even possible to relocate this block group.
7569 *
7570 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7571 * ok to go ahead and try.
7572 */
7573int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007574{
Zheng Yan1a40e232008-09-26 10:09:34 -04007575 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007576 struct btrfs_space_info *space_info;
7577 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7578 struct btrfs_device *device;
7579 int full = 0;
7580 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007581
Josef Bacikba1bf482009-09-11 16:11:19 -04007582 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007583
Josef Bacikba1bf482009-09-11 16:11:19 -04007584 /* odd, couldn't find the block group, leave it alone */
7585 if (!block_group)
7586 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007587
Josef Bacikba1bf482009-09-11 16:11:19 -04007588 /* no bytes used, we're good */
7589 if (!btrfs_block_group_used(&block_group->item))
7590 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007591
Josef Bacikba1bf482009-09-11 16:11:19 -04007592 space_info = block_group->space_info;
7593 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007594
Josef Bacikba1bf482009-09-11 16:11:19 -04007595 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007596
Josef Bacikba1bf482009-09-11 16:11:19 -04007597 /*
7598 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007599 * relocate it unless we're able to allocate a new chunk below.
7600 *
7601 * Otherwise, we need to make sure we have room in the space to handle
7602 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007603 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007604 if ((space_info->total_bytes != block_group->key.offset) &&
7605 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04007606 space_info->bytes_pinned + space_info->bytes_readonly +
7607 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04007608 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007609 spin_unlock(&space_info->lock);
7610 goto out;
7611 }
7612 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007613
Josef Bacikba1bf482009-09-11 16:11:19 -04007614 /*
7615 * ok we don't have enough space, but maybe we have free space on our
7616 * devices to allocate new chunks for relocation, so loop through our
7617 * alloc devices and guess if we have enough space. However, if we
7618 * were marked as full, then we know there aren't enough chunks, and we
7619 * can just return.
7620 */
7621 ret = -1;
7622 if (full)
7623 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05007624
Josef Bacikba1bf482009-09-11 16:11:19 -04007625 mutex_lock(&root->fs_info->chunk_mutex);
7626 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7627 u64 min_free = btrfs_block_group_used(&block_group->item);
7628 u64 dev_offset, max_avail;
Chris Masonea8c2812008-08-04 23:17:27 -04007629
Josef Bacikba1bf482009-09-11 16:11:19 -04007630 /*
7631 * check to make sure we can actually find a chunk with enough
7632 * space to fit our block group in.
7633 */
7634 if (device->total_bytes > device->bytes_used + min_free) {
7635 ret = find_free_dev_extent(NULL, device, min_free,
7636 &dev_offset, &max_avail);
7637 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05007638 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04007639 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007640 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007641 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007642 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007643out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007644 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007645 return ret;
7646}
7647
Christoph Hellwigb2950862008-12-02 09:54:17 -05007648static int find_first_block_group(struct btrfs_root *root,
7649 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007650{
Chris Mason925baed2008-06-25 16:01:30 -04007651 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007652 struct btrfs_key found_key;
7653 struct extent_buffer *leaf;
7654 int slot;
7655
7656 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7657 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007658 goto out;
7659
Chris Masond3977122009-01-05 21:25:51 -05007660 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007661 slot = path->slots[0];
7662 leaf = path->nodes[0];
7663 if (slot >= btrfs_header_nritems(leaf)) {
7664 ret = btrfs_next_leaf(root, path);
7665 if (ret == 0)
7666 continue;
7667 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007668 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007669 break;
7670 }
7671 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7672
7673 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007674 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7675 ret = 0;
7676 goto out;
7677 }
Chris Mason0b86a832008-03-24 15:01:56 -04007678 path->slots[0]++;
7679 }
Chris Mason925baed2008-06-25 16:01:30 -04007680out:
Chris Mason0b86a832008-03-24 15:01:56 -04007681 return ret;
7682}
7683
Zheng Yan1a40e232008-09-26 10:09:34 -04007684int btrfs_free_block_groups(struct btrfs_fs_info *info)
7685{
7686 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007687 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007688 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007689 struct rb_node *n;
7690
Yan Zheng11833d62009-09-11 16:11:19 -04007691 down_write(&info->extent_commit_sem);
7692 while (!list_empty(&info->caching_block_groups)) {
7693 caching_ctl = list_entry(info->caching_block_groups.next,
7694 struct btrfs_caching_control, list);
7695 list_del(&caching_ctl->list);
7696 put_caching_control(caching_ctl);
7697 }
7698 up_write(&info->extent_commit_sem);
7699
Zheng Yan1a40e232008-09-26 10:09:34 -04007700 spin_lock(&info->block_group_cache_lock);
7701 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7702 block_group = rb_entry(n, struct btrfs_block_group_cache,
7703 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007704 rb_erase(&block_group->cache_node,
7705 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007706 spin_unlock(&info->block_group_cache_lock);
7707
Josef Bacik80eb2342008-10-29 14:49:05 -04007708 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007709 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007710 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007711
Josef Bacik817d52f2009-07-13 21:29:25 -04007712 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007713 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007714
7715 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007716 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007717
7718 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007719 }
7720 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007721
7722 /* now that all the block groups are freed, go through and
7723 * free all the space_info structs. This is only called during
7724 * the final stages of unmount, and so we know nobody is
7725 * using them. We call synchronize_rcu() once before we start,
7726 * just to be on the safe side.
7727 */
7728 synchronize_rcu();
7729
7730 while(!list_empty(&info->space_info)) {
7731 space_info = list_entry(info->space_info.next,
7732 struct btrfs_space_info,
7733 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007734 if (space_info->bytes_pinned > 0 ||
7735 space_info->bytes_reserved > 0) {
7736 WARN_ON(1);
7737 dump_space_info(space_info, 0, 0);
7738 }
Chris Mason4184ea72009-03-10 12:39:20 -04007739 list_del(&space_info->list);
7740 kfree(space_info);
7741 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007742 return 0;
7743}
7744
Yan, Zhengb742bb82010-05-16 10:46:24 -04007745static void __link_block_group(struct btrfs_space_info *space_info,
7746 struct btrfs_block_group_cache *cache)
7747{
7748 int index = get_block_group_index(cache);
7749
7750 down_write(&space_info->groups_sem);
7751 list_add_tail(&cache->list, &space_info->block_groups[index]);
7752 up_write(&space_info->groups_sem);
7753}
7754
Chris Mason9078a3e2007-04-26 16:46:15 -04007755int btrfs_read_block_groups(struct btrfs_root *root)
7756{
7757 struct btrfs_path *path;
7758 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007759 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007760 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007761 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007762 struct btrfs_key key;
7763 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007764 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007765
Chris Masonbe744172007-05-06 10:15:01 -04007766 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007767 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007768 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007769 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007770 path = btrfs_alloc_path();
7771 if (!path)
7772 return -ENOMEM;
7773
Chris Masond3977122009-01-05 21:25:51 -05007774 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007775 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007776 if (ret > 0)
7777 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007778 if (ret != 0)
7779 goto error;
7780
Chris Mason5f39d392007-10-15 16:14:19 -04007781 leaf = path->nodes[0];
7782 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007783 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007784 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007785 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007786 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007787 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007788
Yan Zhengd2fb3432008-12-11 16:30:39 -05007789 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007790 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007791 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007792 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007793 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007794 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007795
7796 /*
7797 * we only want to have 32k of ram per block group for keeping
7798 * track of free space, and if we pass 1/2 of that we want to
7799 * start converting things over to using bitmaps
7800 */
7801 cache->extents_thresh = ((1024 * 32) / 2) /
7802 sizeof(struct btrfs_free_space);
7803
Chris Mason5f39d392007-10-15 16:14:19 -04007804 read_extent_buffer(leaf, &cache->item,
7805 btrfs_item_ptr_offset(leaf, path->slots[0]),
7806 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007807 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007808
Chris Mason9078a3e2007-04-26 16:46:15 -04007809 key.objectid = found_key.objectid + found_key.offset;
7810 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007811 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007812 cache->sectorsize = root->sectorsize;
7813
Josef Bacik817d52f2009-07-13 21:29:25 -04007814 /*
7815 * check for two cases, either we are full, and therefore
7816 * don't need to bother with the caching work since we won't
7817 * find any space, or we are empty, and we can just add all
7818 * the space in and be done with it. This saves us _alot_ of
7819 * time, particularly in the full case.
7820 */
7821 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04007822 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04007823 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007824 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007825 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007826 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007827 exclude_super_stripes(root, cache);
7828 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007829 cache->cached = BTRFS_CACHE_FINISHED;
7830 add_new_free_space(cache, root->fs_info,
7831 found_key.objectid,
7832 found_key.objectid +
7833 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007834 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007835 }
Chris Mason96b51792007-10-15 16:15:19 -04007836
Chris Mason6324fbf2008-03-24 15:01:59 -04007837 ret = update_space_info(info, cache->flags, found_key.offset,
7838 btrfs_block_group_used(&cache->item),
7839 &space_info);
7840 BUG_ON(ret);
7841 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007842 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007843 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007844 spin_unlock(&cache->space_info->lock);
7845
Yan, Zhengb742bb82010-05-16 10:46:24 -04007846 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007847
Josef Bacik0f9dd462008-09-23 13:14:11 -04007848 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7849 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007850
7851 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007852 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04007853 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007854 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007855
7856 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7857 if (!(get_alloc_profile(root, space_info->flags) &
7858 (BTRFS_BLOCK_GROUP_RAID10 |
7859 BTRFS_BLOCK_GROUP_RAID1 |
7860 BTRFS_BLOCK_GROUP_DUP)))
7861 continue;
7862 /*
7863 * avoid allocating from un-mirrored block group if there are
7864 * mirrored block groups.
7865 */
7866 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007867 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007868 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007869 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007870 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007871
7872 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007873 ret = 0;
7874error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007875 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007876 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007877}
Chris Mason6324fbf2008-03-24 15:01:59 -04007878
7879int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7880 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007881 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007882 u64 size)
7883{
7884 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007885 struct btrfs_root *extent_root;
7886 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007887
7888 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007889
Chris Mason12fcfd22009-03-24 10:24:20 -04007890 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007891
Chris Mason8f18cf12008-04-25 16:53:30 -04007892 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007893 if (!cache)
7894 return -ENOMEM;
7895
Chris Masone17cade2008-04-15 15:41:47 -04007896 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007897 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007898 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007899 cache->sectorsize = root->sectorsize;
7900
7901 /*
7902 * we only want to have 32k of ram per block group for keeping track
7903 * of free space, and if we pass 1/2 of that we want to start
7904 * converting things over to using bitmaps
7905 */
7906 cache->extents_thresh = ((1024 * 32) / 2) /
7907 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007908 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007909 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007910 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007911 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007912 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007913
Chris Mason6324fbf2008-03-24 15:01:59 -04007914 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007915 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7916 cache->flags = type;
7917 btrfs_set_block_group_flags(&cache->item, type);
7918
Yan Zheng11833d62009-09-11 16:11:19 -04007919 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007920 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007921 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007922
Josef Bacik817d52f2009-07-13 21:29:25 -04007923 add_new_free_space(cache, root->fs_info, chunk_offset,
7924 chunk_offset + size);
7925
Yan Zheng11833d62009-09-11 16:11:19 -04007926 free_excluded_extents(root, cache);
7927
Chris Mason6324fbf2008-03-24 15:01:59 -04007928 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7929 &cache->space_info);
7930 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007931
7932 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007933 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007934 spin_unlock(&cache->space_info->lock);
7935
Yan, Zhengb742bb82010-05-16 10:46:24 -04007936 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007937
Josef Bacik0f9dd462008-09-23 13:14:11 -04007938 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7939 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007940
Chris Mason6324fbf2008-03-24 15:01:59 -04007941 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7942 sizeof(cache->item));
7943 BUG_ON(ret);
7944
Chris Masond18a2c42008-04-04 15:40:00 -04007945 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007946
Chris Mason6324fbf2008-03-24 15:01:59 -04007947 return 0;
7948}
Zheng Yan1a40e232008-09-26 10:09:34 -04007949
7950int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7951 struct btrfs_root *root, u64 group_start)
7952{
7953 struct btrfs_path *path;
7954 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007955 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007956 struct btrfs_key key;
7957 int ret;
7958
Zheng Yan1a40e232008-09-26 10:09:34 -04007959 root = root->fs_info->extent_root;
7960
7961 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7962 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007963 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007964
7965 memcpy(&key, &block_group->key, sizeof(key));
7966
Chris Mason44fb5512009-06-04 15:34:51 -04007967 /* make sure this block group isn't part of an allocation cluster */
7968 cluster = &root->fs_info->data_alloc_cluster;
7969 spin_lock(&cluster->refill_lock);
7970 btrfs_return_cluster_to_free_space(block_group, cluster);
7971 spin_unlock(&cluster->refill_lock);
7972
7973 /*
7974 * make sure this block group isn't part of a metadata
7975 * allocation cluster
7976 */
7977 cluster = &root->fs_info->meta_alloc_cluster;
7978 spin_lock(&cluster->refill_lock);
7979 btrfs_return_cluster_to_free_space(block_group, cluster);
7980 spin_unlock(&cluster->refill_lock);
7981
Zheng Yan1a40e232008-09-26 10:09:34 -04007982 path = btrfs_alloc_path();
7983 BUG_ON(!path);
7984
Yan Zheng3dfdb932009-01-21 10:49:16 -05007985 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007986 rb_erase(&block_group->cache_node,
7987 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007988 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007989
Josef Bacik80eb2342008-10-29 14:49:05 -04007990 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007991 /*
7992 * we must use list_del_init so people can check to see if they
7993 * are still on the list after taking the semaphore
7994 */
7995 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007996 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007997
Josef Bacik817d52f2009-07-13 21:29:25 -04007998 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007999 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008000
8001 btrfs_remove_free_space_cache(block_group);
8002
Yan Zhengc146afa2008-11-12 14:34:12 -05008003 spin_lock(&block_group->space_info->lock);
8004 block_group->space_info->total_bytes -= block_group->key.offset;
8005 block_group->space_info->bytes_readonly -= block_group->key.offset;
8006 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008007
8008 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008009
Chris Masonfa9c0d72009-04-03 09:47:43 -04008010 btrfs_put_block_group(block_group);
8011 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008012
8013 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8014 if (ret > 0)
8015 ret = -EIO;
8016 if (ret < 0)
8017 goto out;
8018
8019 ret = btrfs_del_item(trans, root, path);
8020out:
8021 btrfs_free_path(path);
8022 return ret;
8023}