blob: 2c95507c9abbcc8418ede5d14be13d90ffcbc6a9 [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,
38 u64 bytenr, u64 num_bytes, int alloc,
39 int mark_free);
Yan Zheng11833d62009-09-11 16:11:19 -040040static int update_reserved_extents(struct btrfs_block_group_cache *cache,
41 u64 num_bytes, int reserve);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040042static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
43 struct btrfs_root *root,
44 u64 bytenr, u64 num_bytes, u64 parent,
45 u64 root_objectid, u64 owner_objectid,
46 u64 owner_offset, int refs_to_drop,
47 struct btrfs_delayed_extent_op *extra_op);
48static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
49 struct extent_buffer *leaf,
50 struct btrfs_extent_item *ei);
51static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
52 struct btrfs_root *root,
53 u64 parent, u64 root_objectid,
54 u64 flags, u64 owner, u64 offset,
55 struct btrfs_key *ins, int ref_mod);
56static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
57 struct btrfs_root *root,
58 u64 parent, u64 root_objectid,
59 u64 flags, struct btrfs_disk_key *key,
60 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050061static int do_chunk_alloc(struct btrfs_trans_handle *trans,
62 struct btrfs_root *extent_root, u64 alloc_bytes,
63 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040064static int pin_down_bytes(struct btrfs_trans_handle *trans,
65 struct btrfs_root *root,
66 struct btrfs_path *path,
67 u64 bytenr, u64 num_bytes,
68 int is_data, int reserved,
69 struct extent_buffer **must_clean);
70static int find_next_key(struct btrfs_path *path, int level,
71 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040072static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
73 int dump_block_groups);
Yan, Zheng424499d2010-05-16 10:46:25 -040074static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
75 struct btrfs_root *root,
76 struct btrfs_space_info *sinfo, u64 num_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -050077
Josef Bacik817d52f2009-07-13 21:29:25 -040078static noinline int
79block_group_cache_done(struct btrfs_block_group_cache *cache)
80{
81 smp_mb();
82 return cache->cached == BTRFS_CACHE_FINISHED;
83}
84
Josef Bacik0f9dd462008-09-23 13:14:11 -040085static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
86{
87 return (cache->flags & bits) == bits;
88}
89
Josef Bacik11dfe352009-11-13 20:12:59 +000090void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
91{
92 atomic_inc(&cache->count);
93}
94
95void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
96{
97 if (atomic_dec_and_test(&cache->count))
98 kfree(cache);
99}
100
Josef Bacik0f9dd462008-09-23 13:14:11 -0400101/*
102 * this adds the block group to the fs_info rb tree for the block group
103 * cache
104 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500105static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400106 struct btrfs_block_group_cache *block_group)
107{
108 struct rb_node **p;
109 struct rb_node *parent = NULL;
110 struct btrfs_block_group_cache *cache;
111
112 spin_lock(&info->block_group_cache_lock);
113 p = &info->block_group_cache_tree.rb_node;
114
115 while (*p) {
116 parent = *p;
117 cache = rb_entry(parent, struct btrfs_block_group_cache,
118 cache_node);
119 if (block_group->key.objectid < cache->key.objectid) {
120 p = &(*p)->rb_left;
121 } else if (block_group->key.objectid > cache->key.objectid) {
122 p = &(*p)->rb_right;
123 } else {
124 spin_unlock(&info->block_group_cache_lock);
125 return -EEXIST;
126 }
127 }
128
129 rb_link_node(&block_group->cache_node, parent, p);
130 rb_insert_color(&block_group->cache_node,
131 &info->block_group_cache_tree);
132 spin_unlock(&info->block_group_cache_lock);
133
134 return 0;
135}
136
137/*
138 * This will return the block group at or after bytenr if contains is 0, else
139 * it will return the block group that contains the bytenr
140 */
141static struct btrfs_block_group_cache *
142block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
143 int contains)
144{
145 struct btrfs_block_group_cache *cache, *ret = NULL;
146 struct rb_node *n;
147 u64 end, start;
148
149 spin_lock(&info->block_group_cache_lock);
150 n = info->block_group_cache_tree.rb_node;
151
152 while (n) {
153 cache = rb_entry(n, struct btrfs_block_group_cache,
154 cache_node);
155 end = cache->key.objectid + cache->key.offset - 1;
156 start = cache->key.objectid;
157
158 if (bytenr < start) {
159 if (!contains && (!ret || start < ret->key.objectid))
160 ret = cache;
161 n = n->rb_left;
162 } else if (bytenr > start) {
163 if (contains && bytenr <= end) {
164 ret = cache;
165 break;
166 }
167 n = n->rb_right;
168 } else {
169 ret = cache;
170 break;
171 }
172 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500173 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000174 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400175 spin_unlock(&info->block_group_cache_lock);
176
177 return ret;
178}
179
Yan Zheng11833d62009-09-11 16:11:19 -0400180static int add_excluded_extent(struct btrfs_root *root,
181 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400182{
Yan Zheng11833d62009-09-11 16:11:19 -0400183 u64 end = start + num_bytes - 1;
184 set_extent_bits(&root->fs_info->freed_extents[0],
185 start, end, EXTENT_UPTODATE, GFP_NOFS);
186 set_extent_bits(&root->fs_info->freed_extents[1],
187 start, end, EXTENT_UPTODATE, GFP_NOFS);
188 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400189}
190
Yan Zheng11833d62009-09-11 16:11:19 -0400191static void free_excluded_extents(struct btrfs_root *root,
192 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400193{
Yan Zheng11833d62009-09-11 16:11:19 -0400194 u64 start, end;
195
196 start = cache->key.objectid;
197 end = start + cache->key.offset - 1;
198
199 clear_extent_bits(&root->fs_info->freed_extents[0],
200 start, end, EXTENT_UPTODATE, GFP_NOFS);
201 clear_extent_bits(&root->fs_info->freed_extents[1],
202 start, end, EXTENT_UPTODATE, GFP_NOFS);
203}
204
205static int exclude_super_stripes(struct btrfs_root *root,
206 struct btrfs_block_group_cache *cache)
207{
Josef Bacik817d52f2009-07-13 21:29:25 -0400208 u64 bytenr;
209 u64 *logical;
210 int stripe_len;
211 int i, nr, ret;
212
Yan, Zheng06b23312009-11-26 09:31:11 +0000213 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
214 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
215 cache->bytes_super += stripe_len;
216 ret = add_excluded_extent(root, cache->key.objectid,
217 stripe_len);
218 BUG_ON(ret);
219 }
220
Josef Bacik817d52f2009-07-13 21:29:25 -0400221 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
222 bytenr = btrfs_sb_offset(i);
223 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
224 cache->key.objectid, bytenr,
225 0, &logical, &nr, &stripe_len);
226 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400227
Josef Bacik817d52f2009-07-13 21:29:25 -0400228 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400229 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400230 ret = add_excluded_extent(root, logical[nr],
231 stripe_len);
232 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400233 }
Yan Zheng11833d62009-09-11 16:11:19 -0400234
Josef Bacik817d52f2009-07-13 21:29:25 -0400235 kfree(logical);
236 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400237 return 0;
238}
239
Yan Zheng11833d62009-09-11 16:11:19 -0400240static struct btrfs_caching_control *
241get_caching_control(struct btrfs_block_group_cache *cache)
242{
243 struct btrfs_caching_control *ctl;
244
245 spin_lock(&cache->lock);
246 if (cache->cached != BTRFS_CACHE_STARTED) {
247 spin_unlock(&cache->lock);
248 return NULL;
249 }
250
251 ctl = cache->caching_ctl;
252 atomic_inc(&ctl->count);
253 spin_unlock(&cache->lock);
254 return ctl;
255}
256
257static void put_caching_control(struct btrfs_caching_control *ctl)
258{
259 if (atomic_dec_and_test(&ctl->count))
260 kfree(ctl);
261}
262
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263/*
264 * this is only called by cache_block_group, since we could have freed extents
265 * we need to check the pinned_extents for any extents that can't be used yet
266 * since their free space will be released as soon as the transaction commits.
267 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400268static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400269 struct btrfs_fs_info *info, u64 start, u64 end)
270{
Josef Bacik817d52f2009-07-13 21:29:25 -0400271 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400272 int ret;
273
274 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400275 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400276 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400277 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400278 if (ret)
279 break;
280
Yan, Zheng06b23312009-11-26 09:31:11 +0000281 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400282 start = extent_end + 1;
283 } else if (extent_start > start && extent_start < end) {
284 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400285 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500286 ret = btrfs_add_free_space(block_group, start,
287 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400288 BUG_ON(ret);
289 start = extent_end + 1;
290 } else {
291 break;
292 }
293 }
294
295 if (start < end) {
296 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400297 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500298 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400299 BUG_ON(ret);
300 }
301
Josef Bacik817d52f2009-07-13 21:29:25 -0400302 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400303}
304
Josef Bacik817d52f2009-07-13 21:29:25 -0400305static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400306{
Josef Bacik817d52f2009-07-13 21:29:25 -0400307 struct btrfs_block_group_cache *block_group = data;
308 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400309 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
310 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400311 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400312 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400313 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400314 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400315 u64 last = 0;
316 u32 nritems;
317 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400318
Chris Masone37c9e62007-05-09 20:13:14 -0400319 path = btrfs_alloc_path();
320 if (!path)
321 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400322
Yan Zheng11833d62009-09-11 16:11:19 -0400323 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400324 spin_lock(&block_group->space_info->lock);
325 block_group->space_info->bytes_super += block_group->bytes_super;
326 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400327
Josef Bacik817d52f2009-07-13 21:29:25 -0400328 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400329
Chris Mason5cd57b22008-06-25 16:01:30 -0400330 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 * We don't want to deadlock with somebody trying to allocate a new
332 * extent for the extent root while also trying to search the extent
333 * root to add free space. So we skip locking and search the commit
334 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400335 */
336 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400337 path->search_commit_root = 1;
338 path->reada = 2;
339
Yan Zhenge4404d62008-12-12 10:03:26 -0500340 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400341 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400342 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400343again:
Yan Zheng11833d62009-09-11 16:11:19 -0400344 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400345 /* need to make sure the commit_root doesn't disappear */
346 down_read(&fs_info->extent_commit_sem);
347
Yan Zheng11833d62009-09-11 16:11:19 -0400348 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400349 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400350 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500351
Yan Zheng11833d62009-09-11 16:11:19 -0400352 leaf = path->nodes[0];
353 nritems = btrfs_header_nritems(leaf);
354
Chris Masond3977122009-01-05 21:25:51 -0500355 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400356 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400357 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400358 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400359 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400360 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400361
Yan Zheng11833d62009-09-11 16:11:19 -0400362 if (path->slots[0] < nritems) {
363 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
364 } else {
365 ret = find_next_key(path, 0, &key);
366 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400367 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400368
Yan Zheng11833d62009-09-11 16:11:19 -0400369 caching_ctl->progress = last;
370 btrfs_release_path(extent_root, path);
371 up_read(&fs_info->extent_commit_sem);
372 mutex_unlock(&caching_ctl->mutex);
373 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400374 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400375 else
376 cond_resched();
377 goto again;
378 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400379
Yan Zheng11833d62009-09-11 16:11:19 -0400380 if (key.objectid < block_group->key.objectid) {
381 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400382 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400383 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400384
Chris Masone37c9e62007-05-09 20:13:14 -0400385 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400386 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400387 break;
Yan7d7d6062007-09-14 16:15:28 -0400388
Yan Zheng11833d62009-09-11 16:11:19 -0400389 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400390 total_found += add_new_free_space(block_group,
391 fs_info, last,
392 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400393 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400394
Yan Zheng11833d62009-09-11 16:11:19 -0400395 if (total_found > (1024 * 1024 * 2)) {
396 total_found = 0;
397 wake_up(&caching_ctl->wait);
398 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400399 }
Chris Masone37c9e62007-05-09 20:13:14 -0400400 path->slots[0]++;
401 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400402 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400403
404 total_found += add_new_free_space(block_group, fs_info, last,
405 block_group->key.objectid +
406 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400407 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400408
409 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400410 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400411 block_group->cached = BTRFS_CACHE_FINISHED;
412 spin_unlock(&block_group->lock);
413
Chris Mason54aa1f42007-06-22 14:16:25 -0400414err:
Chris Masone37c9e62007-05-09 20:13:14 -0400415 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400416 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400417
Yan Zheng11833d62009-09-11 16:11:19 -0400418 free_excluded_extents(extent_root, block_group);
419
420 mutex_unlock(&caching_ctl->mutex);
421 wake_up(&caching_ctl->wait);
422
423 put_caching_control(caching_ctl);
424 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000425 btrfs_put_block_group(block_group);
426
Josef Bacik817d52f2009-07-13 21:29:25 -0400427 return 0;
428}
429
430static int cache_block_group(struct btrfs_block_group_cache *cache)
431{
Yan Zheng11833d62009-09-11 16:11:19 -0400432 struct btrfs_fs_info *fs_info = cache->fs_info;
433 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400434 struct task_struct *tsk;
435 int ret = 0;
436
Yan Zheng11833d62009-09-11 16:11:19 -0400437 smp_mb();
438 if (cache->cached != BTRFS_CACHE_NO)
439 return 0;
440
441 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
442 BUG_ON(!caching_ctl);
443
444 INIT_LIST_HEAD(&caching_ctl->list);
445 mutex_init(&caching_ctl->mutex);
446 init_waitqueue_head(&caching_ctl->wait);
447 caching_ctl->block_group = cache;
448 caching_ctl->progress = cache->key.objectid;
449 /* one for caching kthread, one for caching block group list */
450 atomic_set(&caching_ctl->count, 2);
451
Josef Bacik817d52f2009-07-13 21:29:25 -0400452 spin_lock(&cache->lock);
453 if (cache->cached != BTRFS_CACHE_NO) {
454 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400455 kfree(caching_ctl);
456 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400457 }
Yan Zheng11833d62009-09-11 16:11:19 -0400458 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400459 cache->cached = BTRFS_CACHE_STARTED;
460 spin_unlock(&cache->lock);
461
Yan Zheng11833d62009-09-11 16:11:19 -0400462 down_write(&fs_info->extent_commit_sem);
463 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
464 up_write(&fs_info->extent_commit_sem);
465
466 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000467 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400468
Josef Bacik817d52f2009-07-13 21:29:25 -0400469 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
470 cache->key.objectid);
471 if (IS_ERR(tsk)) {
472 ret = PTR_ERR(tsk);
473 printk(KERN_ERR "error running thread %d\n", ret);
474 BUG();
475 }
476
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400477 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400478}
479
Josef Bacik0f9dd462008-09-23 13:14:11 -0400480/*
481 * return the block group that starts at or after bytenr
482 */
Chris Masond3977122009-01-05 21:25:51 -0500483static struct btrfs_block_group_cache *
484btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400485{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400486 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400487
Josef Bacik0f9dd462008-09-23 13:14:11 -0400488 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400489
Josef Bacik0f9dd462008-09-23 13:14:11 -0400490 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400491}
492
Josef Bacik0f9dd462008-09-23 13:14:11 -0400493/*
Sankar P9f556842009-05-14 13:52:22 -0400494 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400495 */
Chris Masond3977122009-01-05 21:25:51 -0500496struct btrfs_block_group_cache *btrfs_lookup_block_group(
497 struct btrfs_fs_info *info,
498 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400499{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400500 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400501
Josef Bacik0f9dd462008-09-23 13:14:11 -0400502 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400503
Josef Bacik0f9dd462008-09-23 13:14:11 -0400504 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400505}
Chris Mason0b86a832008-03-24 15:01:56 -0400506
Josef Bacik0f9dd462008-09-23 13:14:11 -0400507static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
508 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400509{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400510 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400511 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400512
Yan, Zhengb742bb82010-05-16 10:46:24 -0400513 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
514 BTRFS_BLOCK_GROUP_METADATA;
515
Chris Mason4184ea72009-03-10 12:39:20 -0400516 rcu_read_lock();
517 list_for_each_entry_rcu(found, head, list) {
518 if (found->flags == flags) {
519 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400520 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400521 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400522 }
Chris Mason4184ea72009-03-10 12:39:20 -0400523 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400524 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400525}
526
Chris Mason4184ea72009-03-10 12:39:20 -0400527/*
528 * after adding space to the filesystem, we need to clear the full flags
529 * on all the space infos.
530 */
531void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
532{
533 struct list_head *head = &info->space_info;
534 struct btrfs_space_info *found;
535
536 rcu_read_lock();
537 list_for_each_entry_rcu(found, head, list)
538 found->full = 0;
539 rcu_read_unlock();
540}
541
Josef Bacik80eb2342008-10-29 14:49:05 -0400542static u64 div_factor(u64 num, int factor)
543{
544 if (factor == 10)
545 return num;
546 num *= factor;
547 do_div(num, 10);
548 return num;
549}
550
Yan Zhengd2fb3432008-12-11 16:30:39 -0500551u64 btrfs_find_block_group(struct btrfs_root *root,
552 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400553{
Chris Mason96b51792007-10-15 16:15:19 -0400554 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400555 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500556 u64 last = max(search_hint, search_start);
557 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400558 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500559 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400560 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400561again:
Zheng Yane8569812008-09-26 10:05:48 -0400562 while (1) {
563 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400564 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400565 break;
Chris Mason96b51792007-10-15 16:15:19 -0400566
Chris Masonc286ac42008-07-22 23:06:41 -0400567 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400568 last = cache->key.objectid + cache->key.offset;
569 used = btrfs_block_group_used(&cache->item);
570
Yan Zhengd2fb3432008-12-11 16:30:39 -0500571 if ((full_search || !cache->ro) &&
572 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400573 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500574 div_factor(cache->key.offset, factor)) {
575 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400576 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400577 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400578 goto found;
579 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400580 }
Chris Masonc286ac42008-07-22 23:06:41 -0400581 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400582 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400583 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400584 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400585 if (!wrapped) {
586 last = search_start;
587 wrapped = 1;
588 goto again;
589 }
590 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400591 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400592 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400593 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400594 goto again;
595 }
Chris Masonbe744172007-05-06 10:15:01 -0400596found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500597 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400598}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400599
Chris Masone02119d2008-09-05 16:13:11 -0400600/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400601int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400602{
603 int ret;
604 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400605 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400606
Zheng Yan31840ae2008-09-23 13:14:14 -0400607 path = btrfs_alloc_path();
608 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400609 key.objectid = start;
610 key.offset = len;
611 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
612 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
613 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400614 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500615 return ret;
616}
617
Chris Masond8d5f3e2007-12-11 12:42:00 -0500618/*
619 * Back reference rules. Back refs have three main goals:
620 *
621 * 1) differentiate between all holders of references to an extent so that
622 * when a reference is dropped we can make sure it was a valid reference
623 * before freeing the extent.
624 *
625 * 2) Provide enough information to quickly find the holders of an extent
626 * if we notice a given block is corrupted or bad.
627 *
628 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
629 * maintenance. This is actually the same as #2, but with a slightly
630 * different use case.
631 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400632 * There are two kinds of back refs. The implicit back refs is optimized
633 * for pointers in non-shared tree blocks. For a given pointer in a block,
634 * back refs of this kind provide information about the block's owner tree
635 * and the pointer's key. These information allow us to find the block by
636 * b-tree searching. The full back refs is for pointers in tree blocks not
637 * referenced by their owner trees. The location of tree block is recorded
638 * in the back refs. Actually the full back refs is generic, and can be
639 * used in all cases the implicit back refs is used. The major shortcoming
640 * of the full back refs is its overhead. Every time a tree block gets
641 * COWed, we have to update back refs entry for all pointers in it.
642 *
643 * For a newly allocated tree block, we use implicit back refs for
644 * pointers in it. This means most tree related operations only involve
645 * implicit back refs. For a tree block created in old transaction, the
646 * only way to drop a reference to it is COW it. So we can detect the
647 * event that tree block loses its owner tree's reference and do the
648 * back refs conversion.
649 *
650 * When a tree block is COW'd through a tree, there are four cases:
651 *
652 * The reference count of the block is one and the tree is the block's
653 * owner tree. Nothing to do in this case.
654 *
655 * The reference count of the block is one and the tree is not the
656 * block's owner tree. In this case, full back refs is used for pointers
657 * in the block. Remove these full back refs, add implicit back refs for
658 * every pointers in the new block.
659 *
660 * The reference count of the block is greater than one and the tree is
661 * the block's owner tree. In this case, implicit back refs is used for
662 * pointers in the block. Add full back refs for every pointers in the
663 * block, increase lower level extents' reference counts. The original
664 * implicit back refs are entailed to the new block.
665 *
666 * The reference count of the block is greater than one and the tree is
667 * not the block's owner tree. Add implicit back refs for every pointer in
668 * the new block, increase lower level extents' reference count.
669 *
670 * Back Reference Key composing:
671 *
672 * The key objectid corresponds to the first byte in the extent,
673 * The key type is used to differentiate between types of back refs.
674 * There are different meanings of the key offset for different types
675 * of back refs.
676 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500677 * File extents can be referenced by:
678 *
679 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400680 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500681 * - different offsets inside a file (bookend extents in file.c)
682 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400683 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500684 *
685 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500686 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400687 * - original offset in the file
688 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400689 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400690 * The key offset for the implicit back refs is hash of the first
691 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500692 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400693 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500694 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400695 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500696 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400697 * The key offset for the implicit back refs is the first byte of
698 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500699 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400700 * When a file extent is allocated, The implicit back refs is used.
701 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500702 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400703 * (root_key.objectid, inode objectid, offset in file, 1)
704 *
705 * When a file extent is removed file truncation, we find the
706 * corresponding implicit back refs and check the following fields:
707 *
708 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500709 *
710 * Btree extents can be referenced by:
711 *
712 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500713 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400714 * Both the implicit back refs and the full back refs for tree blocks
715 * only consist of key. The key offset for the implicit back refs is
716 * objectid of block's owner tree. The key offset for the full back refs
717 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500718 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400719 * When implicit back refs is used, information about the lowest key and
720 * level of the tree block are required. These information are stored in
721 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500722 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400723
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400724#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
725static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
726 struct btrfs_root *root,
727 struct btrfs_path *path,
728 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500729{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400730 struct btrfs_extent_item *item;
731 struct btrfs_extent_item_v0 *ei0;
732 struct btrfs_extent_ref_v0 *ref0;
733 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400734 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400735 struct btrfs_key key;
736 struct btrfs_key found_key;
737 u32 new_size = sizeof(*item);
738 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500739 int ret;
740
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400741 leaf = path->nodes[0];
742 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500743
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400744 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
745 ei0 = btrfs_item_ptr(leaf, path->slots[0],
746 struct btrfs_extent_item_v0);
747 refs = btrfs_extent_refs_v0(leaf, ei0);
748
749 if (owner == (u64)-1) {
750 while (1) {
751 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
752 ret = btrfs_next_leaf(root, path);
753 if (ret < 0)
754 return ret;
755 BUG_ON(ret > 0);
756 leaf = path->nodes[0];
757 }
758 btrfs_item_key_to_cpu(leaf, &found_key,
759 path->slots[0]);
760 BUG_ON(key.objectid != found_key.objectid);
761 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
762 path->slots[0]++;
763 continue;
764 }
765 ref0 = btrfs_item_ptr(leaf, path->slots[0],
766 struct btrfs_extent_ref_v0);
767 owner = btrfs_ref_objectid_v0(leaf, ref0);
768 break;
769 }
770 }
771 btrfs_release_path(root, path);
772
773 if (owner < BTRFS_FIRST_FREE_OBJECTID)
774 new_size += sizeof(*bi);
775
776 new_size -= sizeof(*ei0);
777 ret = btrfs_search_slot(trans, root, &key, path,
778 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400779 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400780 return ret;
781 BUG_ON(ret);
782
783 ret = btrfs_extend_item(trans, root, path, new_size);
784 BUG_ON(ret);
785
786 leaf = path->nodes[0];
787 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
788 btrfs_set_extent_refs(leaf, item, refs);
789 /* FIXME: get real generation */
790 btrfs_set_extent_generation(leaf, item, 0);
791 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
792 btrfs_set_extent_flags(leaf, item,
793 BTRFS_EXTENT_FLAG_TREE_BLOCK |
794 BTRFS_BLOCK_FLAG_FULL_BACKREF);
795 bi = (struct btrfs_tree_block_info *)(item + 1);
796 /* FIXME: get first key of the block */
797 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
798 btrfs_set_tree_block_level(leaf, bi, (int)owner);
799 } else {
800 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
801 }
802 btrfs_mark_buffer_dirty(leaf);
803 return 0;
804}
805#endif
806
807static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
808{
809 u32 high_crc = ~(u32)0;
810 u32 low_crc = ~(u32)0;
811 __le64 lenum;
812
813 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100814 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400815 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100816 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400817 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100818 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400819
820 return ((u64)high_crc << 31) ^ (u64)low_crc;
821}
822
823static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
824 struct btrfs_extent_data_ref *ref)
825{
826 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
827 btrfs_extent_data_ref_objectid(leaf, ref),
828 btrfs_extent_data_ref_offset(leaf, ref));
829}
830
831static int match_extent_data_ref(struct extent_buffer *leaf,
832 struct btrfs_extent_data_ref *ref,
833 u64 root_objectid, u64 owner, u64 offset)
834{
835 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
836 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
837 btrfs_extent_data_ref_offset(leaf, ref) != offset)
838 return 0;
839 return 1;
840}
841
842static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
843 struct btrfs_root *root,
844 struct btrfs_path *path,
845 u64 bytenr, u64 parent,
846 u64 root_objectid,
847 u64 owner, u64 offset)
848{
849 struct btrfs_key key;
850 struct btrfs_extent_data_ref *ref;
851 struct extent_buffer *leaf;
852 u32 nritems;
853 int ret;
854 int recow;
855 int err = -ENOENT;
856
857 key.objectid = bytenr;
858 if (parent) {
859 key.type = BTRFS_SHARED_DATA_REF_KEY;
860 key.offset = parent;
861 } else {
862 key.type = BTRFS_EXTENT_DATA_REF_KEY;
863 key.offset = hash_extent_data_ref(root_objectid,
864 owner, offset);
865 }
866again:
867 recow = 0;
868 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
869 if (ret < 0) {
870 err = ret;
871 goto fail;
872 }
873
874 if (parent) {
875 if (!ret)
876 return 0;
877#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
878 key.type = BTRFS_EXTENT_REF_V0_KEY;
879 btrfs_release_path(root, path);
880 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
881 if (ret < 0) {
882 err = ret;
883 goto fail;
884 }
885 if (!ret)
886 return 0;
887#endif
888 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400889 }
890
891 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400892 nritems = btrfs_header_nritems(leaf);
893 while (1) {
894 if (path->slots[0] >= nritems) {
895 ret = btrfs_next_leaf(root, path);
896 if (ret < 0)
897 err = ret;
898 if (ret)
899 goto fail;
900
901 leaf = path->nodes[0];
902 nritems = btrfs_header_nritems(leaf);
903 recow = 1;
904 }
905
906 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
907 if (key.objectid != bytenr ||
908 key.type != BTRFS_EXTENT_DATA_REF_KEY)
909 goto fail;
910
911 ref = btrfs_item_ptr(leaf, path->slots[0],
912 struct btrfs_extent_data_ref);
913
914 if (match_extent_data_ref(leaf, ref, root_objectid,
915 owner, offset)) {
916 if (recow) {
917 btrfs_release_path(root, path);
918 goto again;
919 }
920 err = 0;
921 break;
922 }
923 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400924 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400925fail:
926 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400927}
928
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400929static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
930 struct btrfs_root *root,
931 struct btrfs_path *path,
932 u64 bytenr, u64 parent,
933 u64 root_objectid, u64 owner,
934 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400935{
936 struct btrfs_key key;
937 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400938 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400939 u32 num_refs;
940 int ret;
941
942 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400943 if (parent) {
944 key.type = BTRFS_SHARED_DATA_REF_KEY;
945 key.offset = parent;
946 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400947 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400948 key.type = BTRFS_EXTENT_DATA_REF_KEY;
949 key.offset = hash_extent_data_ref(root_objectid,
950 owner, offset);
951 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400952 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400953
954 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
955 if (ret && ret != -EEXIST)
956 goto fail;
957
958 leaf = path->nodes[0];
959 if (parent) {
960 struct btrfs_shared_data_ref *ref;
961 ref = btrfs_item_ptr(leaf, path->slots[0],
962 struct btrfs_shared_data_ref);
963 if (ret == 0) {
964 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
965 } else {
966 num_refs = btrfs_shared_data_ref_count(leaf, ref);
967 num_refs += refs_to_add;
968 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
969 }
970 } else {
971 struct btrfs_extent_data_ref *ref;
972 while (ret == -EEXIST) {
973 ref = btrfs_item_ptr(leaf, path->slots[0],
974 struct btrfs_extent_data_ref);
975 if (match_extent_data_ref(leaf, ref, root_objectid,
976 owner, offset))
977 break;
978 btrfs_release_path(root, path);
979 key.offset++;
980 ret = btrfs_insert_empty_item(trans, root, path, &key,
981 size);
982 if (ret && ret != -EEXIST)
983 goto fail;
984
985 leaf = path->nodes[0];
986 }
987 ref = btrfs_item_ptr(leaf, path->slots[0],
988 struct btrfs_extent_data_ref);
989 if (ret == 0) {
990 btrfs_set_extent_data_ref_root(leaf, ref,
991 root_objectid);
992 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
993 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
994 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
995 } else {
996 num_refs = btrfs_extent_data_ref_count(leaf, ref);
997 num_refs += refs_to_add;
998 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
999 }
1000 }
1001 btrfs_mark_buffer_dirty(leaf);
1002 ret = 0;
1003fail:
Chris Mason7bb86312007-12-11 09:25:06 -05001004 btrfs_release_path(root, path);
1005 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001006}
1007
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001008static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1009 struct btrfs_root *root,
1010 struct btrfs_path *path,
1011 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001012{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013 struct btrfs_key key;
1014 struct btrfs_extent_data_ref *ref1 = NULL;
1015 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001016 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001017 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001018 int ret = 0;
1019
1020 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1022
1023 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1024 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1025 struct btrfs_extent_data_ref);
1026 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1027 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1028 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1029 struct btrfs_shared_data_ref);
1030 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1031#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1032 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1033 struct btrfs_extent_ref_v0 *ref0;
1034 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1035 struct btrfs_extent_ref_v0);
1036 num_refs = btrfs_ref_count_v0(leaf, ref0);
1037#endif
1038 } else {
1039 BUG();
1040 }
1041
Chris Mason56bec292009-03-13 10:10:06 -04001042 BUG_ON(num_refs < refs_to_drop);
1043 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044
Zheng Yan31840ae2008-09-23 13:14:14 -04001045 if (num_refs == 0) {
1046 ret = btrfs_del_item(trans, root, path);
1047 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001048 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1049 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1050 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1051 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1052#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1053 else {
1054 struct btrfs_extent_ref_v0 *ref0;
1055 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1056 struct btrfs_extent_ref_v0);
1057 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1058 }
1059#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001060 btrfs_mark_buffer_dirty(leaf);
1061 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001062 return ret;
1063}
1064
1065static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1066 struct btrfs_path *path,
1067 struct btrfs_extent_inline_ref *iref)
1068{
1069 struct btrfs_key key;
1070 struct extent_buffer *leaf;
1071 struct btrfs_extent_data_ref *ref1;
1072 struct btrfs_shared_data_ref *ref2;
1073 u32 num_refs = 0;
1074
1075 leaf = path->nodes[0];
1076 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1077 if (iref) {
1078 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1079 BTRFS_EXTENT_DATA_REF_KEY) {
1080 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1081 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1082 } else {
1083 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1084 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1085 }
1086 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1087 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1088 struct btrfs_extent_data_ref);
1089 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1090 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1091 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1092 struct btrfs_shared_data_ref);
1093 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1094#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1095 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1096 struct btrfs_extent_ref_v0 *ref0;
1097 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1098 struct btrfs_extent_ref_v0);
1099 num_refs = btrfs_ref_count_v0(leaf, ref0);
1100#endif
1101 } else {
1102 WARN_ON(1);
1103 }
1104 return num_refs;
1105}
1106
1107static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1108 struct btrfs_root *root,
1109 struct btrfs_path *path,
1110 u64 bytenr, u64 parent,
1111 u64 root_objectid)
1112{
1113 struct btrfs_key key;
1114 int ret;
1115
1116 key.objectid = bytenr;
1117 if (parent) {
1118 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1119 key.offset = parent;
1120 } else {
1121 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1122 key.offset = root_objectid;
1123 }
1124
1125 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1126 if (ret > 0)
1127 ret = -ENOENT;
1128#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1129 if (ret == -ENOENT && parent) {
1130 btrfs_release_path(root, path);
1131 key.type = BTRFS_EXTENT_REF_V0_KEY;
1132 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1133 if (ret > 0)
1134 ret = -ENOENT;
1135 }
1136#endif
1137 return ret;
1138}
1139
1140static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1141 struct btrfs_root *root,
1142 struct btrfs_path *path,
1143 u64 bytenr, u64 parent,
1144 u64 root_objectid)
1145{
1146 struct btrfs_key key;
1147 int ret;
1148
1149 key.objectid = bytenr;
1150 if (parent) {
1151 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1152 key.offset = parent;
1153 } else {
1154 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1155 key.offset = root_objectid;
1156 }
1157
1158 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001159 btrfs_release_path(root, path);
1160 return ret;
1161}
1162
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001163static inline int extent_ref_type(u64 parent, u64 owner)
1164{
1165 int type;
1166 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1167 if (parent > 0)
1168 type = BTRFS_SHARED_BLOCK_REF_KEY;
1169 else
1170 type = BTRFS_TREE_BLOCK_REF_KEY;
1171 } else {
1172 if (parent > 0)
1173 type = BTRFS_SHARED_DATA_REF_KEY;
1174 else
1175 type = BTRFS_EXTENT_DATA_REF_KEY;
1176 }
1177 return type;
1178}
1179
Yan Zheng2c47e6052009-06-27 21:07:35 -04001180static int find_next_key(struct btrfs_path *path, int level,
1181 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001182
1183{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001184 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001185 if (!path->nodes[level])
1186 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001187 if (path->slots[level] + 1 >=
1188 btrfs_header_nritems(path->nodes[level]))
1189 continue;
1190 if (level == 0)
1191 btrfs_item_key_to_cpu(path->nodes[level], key,
1192 path->slots[level] + 1);
1193 else
1194 btrfs_node_key_to_cpu(path->nodes[level], key,
1195 path->slots[level] + 1);
1196 return 0;
1197 }
1198 return 1;
1199}
1200
1201/*
1202 * look for inline back ref. if back ref is found, *ref_ret is set
1203 * to the address of inline back ref, and 0 is returned.
1204 *
1205 * if back ref isn't found, *ref_ret is set to the address where it
1206 * should be inserted, and -ENOENT is returned.
1207 *
1208 * if insert is true and there are too many inline back refs, the path
1209 * points to the extent item, and -EAGAIN is returned.
1210 *
1211 * NOTE: inline back refs are ordered in the same way that back ref
1212 * items in the tree are ordered.
1213 */
1214static noinline_for_stack
1215int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1216 struct btrfs_root *root,
1217 struct btrfs_path *path,
1218 struct btrfs_extent_inline_ref **ref_ret,
1219 u64 bytenr, u64 num_bytes,
1220 u64 parent, u64 root_objectid,
1221 u64 owner, u64 offset, int insert)
1222{
1223 struct btrfs_key key;
1224 struct extent_buffer *leaf;
1225 struct btrfs_extent_item *ei;
1226 struct btrfs_extent_inline_ref *iref;
1227 u64 flags;
1228 u64 item_size;
1229 unsigned long ptr;
1230 unsigned long end;
1231 int extra_size;
1232 int type;
1233 int want;
1234 int ret;
1235 int err = 0;
1236
1237 key.objectid = bytenr;
1238 key.type = BTRFS_EXTENT_ITEM_KEY;
1239 key.offset = num_bytes;
1240
1241 want = extent_ref_type(parent, owner);
1242 if (insert) {
1243 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001244 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001245 } else
1246 extra_size = -1;
1247 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1248 if (ret < 0) {
1249 err = ret;
1250 goto out;
1251 }
1252 BUG_ON(ret);
1253
1254 leaf = path->nodes[0];
1255 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1256#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1257 if (item_size < sizeof(*ei)) {
1258 if (!insert) {
1259 err = -ENOENT;
1260 goto out;
1261 }
1262 ret = convert_extent_item_v0(trans, root, path, owner,
1263 extra_size);
1264 if (ret < 0) {
1265 err = ret;
1266 goto out;
1267 }
1268 leaf = path->nodes[0];
1269 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1270 }
1271#endif
1272 BUG_ON(item_size < sizeof(*ei));
1273
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001274 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1275 flags = btrfs_extent_flags(leaf, ei);
1276
1277 ptr = (unsigned long)(ei + 1);
1278 end = (unsigned long)ei + item_size;
1279
1280 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1281 ptr += sizeof(struct btrfs_tree_block_info);
1282 BUG_ON(ptr > end);
1283 } else {
1284 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1285 }
1286
1287 err = -ENOENT;
1288 while (1) {
1289 if (ptr >= end) {
1290 WARN_ON(ptr > end);
1291 break;
1292 }
1293 iref = (struct btrfs_extent_inline_ref *)ptr;
1294 type = btrfs_extent_inline_ref_type(leaf, iref);
1295 if (want < type)
1296 break;
1297 if (want > type) {
1298 ptr += btrfs_extent_inline_ref_size(type);
1299 continue;
1300 }
1301
1302 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1303 struct btrfs_extent_data_ref *dref;
1304 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1305 if (match_extent_data_ref(leaf, dref, root_objectid,
1306 owner, offset)) {
1307 err = 0;
1308 break;
1309 }
1310 if (hash_extent_data_ref_item(leaf, dref) <
1311 hash_extent_data_ref(root_objectid, owner, offset))
1312 break;
1313 } else {
1314 u64 ref_offset;
1315 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1316 if (parent > 0) {
1317 if (parent == ref_offset) {
1318 err = 0;
1319 break;
1320 }
1321 if (ref_offset < parent)
1322 break;
1323 } else {
1324 if (root_objectid == ref_offset) {
1325 err = 0;
1326 break;
1327 }
1328 if (ref_offset < root_objectid)
1329 break;
1330 }
1331 }
1332 ptr += btrfs_extent_inline_ref_size(type);
1333 }
1334 if (err == -ENOENT && insert) {
1335 if (item_size + extra_size >=
1336 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1337 err = -EAGAIN;
1338 goto out;
1339 }
1340 /*
1341 * To add new inline back ref, we have to make sure
1342 * there is no corresponding back ref item.
1343 * For simplicity, we just do not add new inline back
1344 * ref if there is any kind of item for this block
1345 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001346 if (find_next_key(path, 0, &key) == 0 &&
1347 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001348 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001349 err = -EAGAIN;
1350 goto out;
1351 }
1352 }
1353 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1354out:
Yan Zheng85d41982009-06-11 08:51:10 -04001355 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001356 path->keep_locks = 0;
1357 btrfs_unlock_up_safe(path, 1);
1358 }
1359 return err;
1360}
1361
1362/*
1363 * helper to add new inline back ref
1364 */
1365static noinline_for_stack
1366int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1367 struct btrfs_root *root,
1368 struct btrfs_path *path,
1369 struct btrfs_extent_inline_ref *iref,
1370 u64 parent, u64 root_objectid,
1371 u64 owner, u64 offset, int refs_to_add,
1372 struct btrfs_delayed_extent_op *extent_op)
1373{
1374 struct extent_buffer *leaf;
1375 struct btrfs_extent_item *ei;
1376 unsigned long ptr;
1377 unsigned long end;
1378 unsigned long item_offset;
1379 u64 refs;
1380 int size;
1381 int type;
1382 int ret;
1383
1384 leaf = path->nodes[0];
1385 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1386 item_offset = (unsigned long)iref - (unsigned long)ei;
1387
1388 type = extent_ref_type(parent, owner);
1389 size = btrfs_extent_inline_ref_size(type);
1390
1391 ret = btrfs_extend_item(trans, root, path, size);
1392 BUG_ON(ret);
1393
1394 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1395 refs = btrfs_extent_refs(leaf, ei);
1396 refs += refs_to_add;
1397 btrfs_set_extent_refs(leaf, ei, refs);
1398 if (extent_op)
1399 __run_delayed_extent_op(extent_op, leaf, ei);
1400
1401 ptr = (unsigned long)ei + item_offset;
1402 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1403 if (ptr < end - size)
1404 memmove_extent_buffer(leaf, ptr + size, ptr,
1405 end - size - ptr);
1406
1407 iref = (struct btrfs_extent_inline_ref *)ptr;
1408 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1409 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1410 struct btrfs_extent_data_ref *dref;
1411 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1412 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1413 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1414 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1415 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1416 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1417 struct btrfs_shared_data_ref *sref;
1418 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1419 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1420 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1421 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1422 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1423 } else {
1424 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1425 }
1426 btrfs_mark_buffer_dirty(leaf);
1427 return 0;
1428}
1429
1430static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1431 struct btrfs_root *root,
1432 struct btrfs_path *path,
1433 struct btrfs_extent_inline_ref **ref_ret,
1434 u64 bytenr, u64 num_bytes, u64 parent,
1435 u64 root_objectid, u64 owner, u64 offset)
1436{
1437 int ret;
1438
1439 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1440 bytenr, num_bytes, parent,
1441 root_objectid, owner, offset, 0);
1442 if (ret != -ENOENT)
1443 return ret;
1444
1445 btrfs_release_path(root, path);
1446 *ref_ret = NULL;
1447
1448 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1449 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1450 root_objectid);
1451 } else {
1452 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1453 root_objectid, owner, offset);
1454 }
1455 return ret;
1456}
1457
1458/*
1459 * helper to update/remove inline back ref
1460 */
1461static noinline_for_stack
1462int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1463 struct btrfs_root *root,
1464 struct btrfs_path *path,
1465 struct btrfs_extent_inline_ref *iref,
1466 int refs_to_mod,
1467 struct btrfs_delayed_extent_op *extent_op)
1468{
1469 struct extent_buffer *leaf;
1470 struct btrfs_extent_item *ei;
1471 struct btrfs_extent_data_ref *dref = NULL;
1472 struct btrfs_shared_data_ref *sref = NULL;
1473 unsigned long ptr;
1474 unsigned long end;
1475 u32 item_size;
1476 int size;
1477 int type;
1478 int ret;
1479 u64 refs;
1480
1481 leaf = path->nodes[0];
1482 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1483 refs = btrfs_extent_refs(leaf, ei);
1484 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1485 refs += refs_to_mod;
1486 btrfs_set_extent_refs(leaf, ei, refs);
1487 if (extent_op)
1488 __run_delayed_extent_op(extent_op, leaf, ei);
1489
1490 type = btrfs_extent_inline_ref_type(leaf, iref);
1491
1492 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1493 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1494 refs = btrfs_extent_data_ref_count(leaf, dref);
1495 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1496 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1497 refs = btrfs_shared_data_ref_count(leaf, sref);
1498 } else {
1499 refs = 1;
1500 BUG_ON(refs_to_mod != -1);
1501 }
1502
1503 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1504 refs += refs_to_mod;
1505
1506 if (refs > 0) {
1507 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1508 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1509 else
1510 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1511 } else {
1512 size = btrfs_extent_inline_ref_size(type);
1513 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1514 ptr = (unsigned long)iref;
1515 end = (unsigned long)ei + item_size;
1516 if (ptr + size < end)
1517 memmove_extent_buffer(leaf, ptr, ptr + size,
1518 end - ptr - size);
1519 item_size -= size;
1520 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1521 BUG_ON(ret);
1522 }
1523 btrfs_mark_buffer_dirty(leaf);
1524 return 0;
1525}
1526
1527static noinline_for_stack
1528int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1529 struct btrfs_root *root,
1530 struct btrfs_path *path,
1531 u64 bytenr, u64 num_bytes, u64 parent,
1532 u64 root_objectid, u64 owner,
1533 u64 offset, int refs_to_add,
1534 struct btrfs_delayed_extent_op *extent_op)
1535{
1536 struct btrfs_extent_inline_ref *iref;
1537 int ret;
1538
1539 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1540 bytenr, num_bytes, parent,
1541 root_objectid, owner, offset, 1);
1542 if (ret == 0) {
1543 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1544 ret = update_inline_extent_backref(trans, root, path, iref,
1545 refs_to_add, extent_op);
1546 } else if (ret == -ENOENT) {
1547 ret = setup_inline_extent_backref(trans, root, path, iref,
1548 parent, root_objectid,
1549 owner, offset, refs_to_add,
1550 extent_op);
1551 }
1552 return ret;
1553}
1554
1555static int insert_extent_backref(struct btrfs_trans_handle *trans,
1556 struct btrfs_root *root,
1557 struct btrfs_path *path,
1558 u64 bytenr, u64 parent, u64 root_objectid,
1559 u64 owner, u64 offset, int refs_to_add)
1560{
1561 int ret;
1562 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1563 BUG_ON(refs_to_add != 1);
1564 ret = insert_tree_block_ref(trans, root, path, bytenr,
1565 parent, root_objectid);
1566 } else {
1567 ret = insert_extent_data_ref(trans, root, path, bytenr,
1568 parent, root_objectid,
1569 owner, offset, refs_to_add);
1570 }
1571 return ret;
1572}
1573
1574static int remove_extent_backref(struct btrfs_trans_handle *trans,
1575 struct btrfs_root *root,
1576 struct btrfs_path *path,
1577 struct btrfs_extent_inline_ref *iref,
1578 int refs_to_drop, int is_data)
1579{
1580 int ret;
1581
1582 BUG_ON(!is_data && refs_to_drop != 1);
1583 if (iref) {
1584 ret = update_inline_extent_backref(trans, root, path, iref,
1585 -refs_to_drop, NULL);
1586 } else if (is_data) {
1587 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1588 } else {
1589 ret = btrfs_del_item(trans, root, path);
1590 }
1591 return ret;
1592}
1593
Chris Mason15916de2008-11-19 21:17:22 -05001594static void btrfs_issue_discard(struct block_device *bdev,
1595 u64 start, u64 len)
1596{
Christoph Hellwig746cd1e2009-09-12 07:35:43 +02001597 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
1598 DISCARD_FL_BARRIER);
Chris Mason15916de2008-11-19 21:17:22 -05001599}
Chris Mason15916de2008-11-19 21:17:22 -05001600
Liu Hui1f3c79a2009-01-05 15:57:51 -05001601static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1602 u64 num_bytes)
1603{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001604 int ret;
1605 u64 map_length = num_bytes;
1606 struct btrfs_multi_bio *multi = NULL;
1607
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001608 if (!btrfs_test_opt(root, DISCARD))
1609 return 0;
1610
Liu Hui1f3c79a2009-01-05 15:57:51 -05001611 /* Tell the block device(s) that the sectors can be discarded */
1612 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1613 bytenr, &map_length, &multi, 0);
1614 if (!ret) {
1615 struct btrfs_bio_stripe *stripe = multi->stripes;
1616 int i;
1617
1618 if (map_length > num_bytes)
1619 map_length = num_bytes;
1620
1621 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1622 btrfs_issue_discard(stripe->dev->bdev,
1623 stripe->physical,
1624 map_length);
1625 }
1626 kfree(multi);
1627 }
1628
1629 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001630}
1631
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001632int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1633 struct btrfs_root *root,
1634 u64 bytenr, u64 num_bytes, u64 parent,
1635 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001636{
1637 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001638 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1639 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001640
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001641 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1642 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1643 parent, root_objectid, (int)owner,
1644 BTRFS_ADD_DELAYED_REF, NULL);
1645 } else {
1646 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1647 parent, root_objectid, owner, offset,
1648 BTRFS_ADD_DELAYED_REF, NULL);
1649 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001650 return ret;
1651}
1652
Chris Mason925baed2008-06-25 16:01:30 -04001653static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001654 struct btrfs_root *root,
1655 u64 bytenr, u64 num_bytes,
1656 u64 parent, u64 root_objectid,
1657 u64 owner, u64 offset, int refs_to_add,
1658 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001659{
Chris Mason5caf2a02007-04-02 11:20:42 -04001660 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001661 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001662 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001663 u64 refs;
1664 int ret;
1665 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001666
Chris Mason5caf2a02007-04-02 11:20:42 -04001667 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001668 if (!path)
1669 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001670
Chris Mason3c12ac72008-04-21 12:01:38 -04001671 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001672 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001673 /* this will setup the path even if it fails to insert the back ref */
1674 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1675 path, bytenr, num_bytes, parent,
1676 root_objectid, owner, offset,
1677 refs_to_add, extent_op);
1678 if (ret == 0)
1679 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001680
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001681 if (ret != -EAGAIN) {
1682 err = ret;
1683 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001684 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001685
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686 leaf = path->nodes[0];
1687 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1688 refs = btrfs_extent_refs(leaf, item);
1689 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1690 if (extent_op)
1691 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001692
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001693 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001694 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001695
Chris Mason3c12ac72008-04-21 12:01:38 -04001696 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001697 path->leave_spinning = 1;
1698
Chris Mason56bec292009-03-13 10:10:06 -04001699 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001700 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001701 path, bytenr, parent, root_objectid,
1702 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001703 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001704out:
Chris Mason74493f72007-12-11 09:25:06 -05001705 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001706 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001707}
1708
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001709static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1710 struct btrfs_root *root,
1711 struct btrfs_delayed_ref_node *node,
1712 struct btrfs_delayed_extent_op *extent_op,
1713 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001714{
Chris Mason56bec292009-03-13 10:10:06 -04001715 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001716 struct btrfs_delayed_data_ref *ref;
1717 struct btrfs_key ins;
1718 u64 parent = 0;
1719 u64 ref_root = 0;
1720 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001721
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001722 ins.objectid = node->bytenr;
1723 ins.offset = node->num_bytes;
1724 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001725
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001726 ref = btrfs_delayed_node_to_data_ref(node);
1727 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1728 parent = ref->parent;
1729 else
1730 ref_root = ref->root;
1731
1732 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1733 if (extent_op) {
1734 BUG_ON(extent_op->update_key);
1735 flags |= extent_op->flags_to_set;
1736 }
1737 ret = alloc_reserved_file_extent(trans, root,
1738 parent, ref_root, flags,
1739 ref->objectid, ref->offset,
1740 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001741 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1742 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1743 node->num_bytes, parent,
1744 ref_root, ref->objectid,
1745 ref->offset, node->ref_mod,
1746 extent_op);
1747 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1748 ret = __btrfs_free_extent(trans, root, node->bytenr,
1749 node->num_bytes, parent,
1750 ref_root, ref->objectid,
1751 ref->offset, node->ref_mod,
1752 extent_op);
1753 } else {
1754 BUG();
1755 }
Chris Mason56bec292009-03-13 10:10:06 -04001756 return ret;
1757}
1758
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001759static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1760 struct extent_buffer *leaf,
1761 struct btrfs_extent_item *ei)
1762{
1763 u64 flags = btrfs_extent_flags(leaf, ei);
1764 if (extent_op->update_flags) {
1765 flags |= extent_op->flags_to_set;
1766 btrfs_set_extent_flags(leaf, ei, flags);
1767 }
1768
1769 if (extent_op->update_key) {
1770 struct btrfs_tree_block_info *bi;
1771 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1772 bi = (struct btrfs_tree_block_info *)(ei + 1);
1773 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1774 }
1775}
1776
1777static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1778 struct btrfs_root *root,
1779 struct btrfs_delayed_ref_node *node,
1780 struct btrfs_delayed_extent_op *extent_op)
1781{
1782 struct btrfs_key key;
1783 struct btrfs_path *path;
1784 struct btrfs_extent_item *ei;
1785 struct extent_buffer *leaf;
1786 u32 item_size;
1787 int ret;
1788 int err = 0;
1789
1790 path = btrfs_alloc_path();
1791 if (!path)
1792 return -ENOMEM;
1793
1794 key.objectid = node->bytenr;
1795 key.type = BTRFS_EXTENT_ITEM_KEY;
1796 key.offset = node->num_bytes;
1797
1798 path->reada = 1;
1799 path->leave_spinning = 1;
1800 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1801 path, 0, 1);
1802 if (ret < 0) {
1803 err = ret;
1804 goto out;
1805 }
1806 if (ret > 0) {
1807 err = -EIO;
1808 goto out;
1809 }
1810
1811 leaf = path->nodes[0];
1812 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1813#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1814 if (item_size < sizeof(*ei)) {
1815 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1816 path, (u64)-1, 0);
1817 if (ret < 0) {
1818 err = ret;
1819 goto out;
1820 }
1821 leaf = path->nodes[0];
1822 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1823 }
1824#endif
1825 BUG_ON(item_size < sizeof(*ei));
1826 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1827 __run_delayed_extent_op(extent_op, leaf, ei);
1828
1829 btrfs_mark_buffer_dirty(leaf);
1830out:
1831 btrfs_free_path(path);
1832 return err;
1833}
1834
1835static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1836 struct btrfs_root *root,
1837 struct btrfs_delayed_ref_node *node,
1838 struct btrfs_delayed_extent_op *extent_op,
1839 int insert_reserved)
1840{
1841 int ret = 0;
1842 struct btrfs_delayed_tree_ref *ref;
1843 struct btrfs_key ins;
1844 u64 parent = 0;
1845 u64 ref_root = 0;
1846
1847 ins.objectid = node->bytenr;
1848 ins.offset = node->num_bytes;
1849 ins.type = BTRFS_EXTENT_ITEM_KEY;
1850
1851 ref = btrfs_delayed_node_to_tree_ref(node);
1852 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1853 parent = ref->parent;
1854 else
1855 ref_root = ref->root;
1856
1857 BUG_ON(node->ref_mod != 1);
1858 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1859 BUG_ON(!extent_op || !extent_op->update_flags ||
1860 !extent_op->update_key);
1861 ret = alloc_reserved_tree_block(trans, root,
1862 parent, ref_root,
1863 extent_op->flags_to_set,
1864 &extent_op->key,
1865 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001866 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1867 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1868 node->num_bytes, parent, ref_root,
1869 ref->level, 0, 1, extent_op);
1870 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1871 ret = __btrfs_free_extent(trans, root, node->bytenr,
1872 node->num_bytes, parent, ref_root,
1873 ref->level, 0, 1, extent_op);
1874 } else {
1875 BUG();
1876 }
1877 return ret;
1878}
1879
1880
Chris Mason56bec292009-03-13 10:10:06 -04001881/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001882static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1883 struct btrfs_root *root,
1884 struct btrfs_delayed_ref_node *node,
1885 struct btrfs_delayed_extent_op *extent_op,
1886 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001887{
Josef Bacikeb099672009-02-12 09:27:38 -05001888 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001889 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001890 struct btrfs_delayed_ref_head *head;
1891 /*
1892 * we've hit the end of the chain and we were supposed
1893 * to insert this extent into the tree. But, it got
1894 * deleted before we ever needed to insert it, so all
1895 * we have to do is clean up the accounting
1896 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001897 BUG_ON(extent_op);
1898 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001899 if (insert_reserved) {
Yan Zheng11833d62009-09-11 16:11:19 -04001900 int mark_free = 0;
1901 struct extent_buffer *must_clean = NULL;
1902
1903 ret = pin_down_bytes(trans, root, NULL,
1904 node->bytenr, node->num_bytes,
1905 head->is_data, 1, &must_clean);
1906 if (ret > 0)
1907 mark_free = 1;
1908
1909 if (must_clean) {
1910 clean_tree_block(NULL, root, must_clean);
1911 btrfs_tree_unlock(must_clean);
1912 free_extent_buffer(must_clean);
1913 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001914 if (head->is_data) {
1915 ret = btrfs_del_csums(trans, root,
1916 node->bytenr,
1917 node->num_bytes);
1918 BUG_ON(ret);
1919 }
Yan Zheng11833d62009-09-11 16:11:19 -04001920 if (mark_free) {
1921 ret = btrfs_free_reserved_extent(root,
1922 node->bytenr,
1923 node->num_bytes);
1924 BUG_ON(ret);
1925 }
Chris Mason56bec292009-03-13 10:10:06 -04001926 }
Chris Mason56bec292009-03-13 10:10:06 -04001927 mutex_unlock(&head->mutex);
1928 return 0;
1929 }
Josef Bacikeb099672009-02-12 09:27:38 -05001930
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001931 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1932 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1933 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1934 insert_reserved);
1935 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1936 node->type == BTRFS_SHARED_DATA_REF_KEY)
1937 ret = run_delayed_data_ref(trans, root, node, extent_op,
1938 insert_reserved);
1939 else
1940 BUG();
1941 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001942}
1943
Chris Mason56bec292009-03-13 10:10:06 -04001944static noinline struct btrfs_delayed_ref_node *
1945select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001946{
Chris Mason56bec292009-03-13 10:10:06 -04001947 struct rb_node *node;
1948 struct btrfs_delayed_ref_node *ref;
1949 int action = BTRFS_ADD_DELAYED_REF;
1950again:
1951 /*
1952 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1953 * this prevents ref count from going down to zero when
1954 * there still are pending delayed ref.
1955 */
1956 node = rb_prev(&head->node.rb_node);
1957 while (1) {
1958 if (!node)
1959 break;
1960 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1961 rb_node);
1962 if (ref->bytenr != head->node.bytenr)
1963 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001964 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001965 return ref;
1966 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001967 }
Chris Mason56bec292009-03-13 10:10:06 -04001968 if (action == BTRFS_ADD_DELAYED_REF) {
1969 action = BTRFS_DROP_DELAYED_REF;
1970 goto again;
1971 }
1972 return NULL;
1973}
1974
Chris Masonc3e69d52009-03-13 10:17:05 -04001975static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1976 struct btrfs_root *root,
1977 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001978{
Chris Mason56bec292009-03-13 10:10:06 -04001979 struct btrfs_delayed_ref_root *delayed_refs;
1980 struct btrfs_delayed_ref_node *ref;
1981 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001982 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001983 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001984 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001985 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001986
1987 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001988 while (1) {
1989 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001990 /* pick a new head ref from the cluster list */
1991 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001992 break;
Chris Mason56bec292009-03-13 10:10:06 -04001993
Chris Masonc3e69d52009-03-13 10:17:05 -04001994 locked_ref = list_entry(cluster->next,
1995 struct btrfs_delayed_ref_head, cluster);
1996
1997 /* grab the lock that says we are going to process
1998 * all the refs for this head */
1999 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2000
2001 /*
2002 * we may have dropped the spin lock to get the head
2003 * mutex lock, and that might have given someone else
2004 * time to free the head. If that's true, it has been
2005 * removed from our list and we can move on.
2006 */
2007 if (ret == -EAGAIN) {
2008 locked_ref = NULL;
2009 count++;
2010 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002011 }
2012 }
2013
2014 /*
2015 * record the must insert reserved flag before we
2016 * drop the spin lock.
2017 */
2018 must_insert_reserved = locked_ref->must_insert_reserved;
2019 locked_ref->must_insert_reserved = 0;
2020
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002021 extent_op = locked_ref->extent_op;
2022 locked_ref->extent_op = NULL;
2023
Chris Mason56bec292009-03-13 10:10:06 -04002024 /*
2025 * locked_ref is the head node, so we have to go one
2026 * node back for any delayed ref updates
2027 */
Chris Mason56bec292009-03-13 10:10:06 -04002028 ref = select_delayed_ref(locked_ref);
2029 if (!ref) {
2030 /* All delayed refs have been processed, Go ahead
2031 * and send the head node to run_one_delayed_ref,
2032 * so that any accounting fixes can happen
2033 */
2034 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002035
2036 if (extent_op && must_insert_reserved) {
2037 kfree(extent_op);
2038 extent_op = NULL;
2039 }
2040
2041 if (extent_op) {
2042 spin_unlock(&delayed_refs->lock);
2043
2044 ret = run_delayed_extent_op(trans, root,
2045 ref, extent_op);
2046 BUG_ON(ret);
2047 kfree(extent_op);
2048
2049 cond_resched();
2050 spin_lock(&delayed_refs->lock);
2051 continue;
2052 }
2053
Chris Masonc3e69d52009-03-13 10:17:05 -04002054 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002055 locked_ref = NULL;
2056 }
2057
2058 ref->in_tree = 0;
2059 rb_erase(&ref->rb_node, &delayed_refs->root);
2060 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002061
Chris Mason56bec292009-03-13 10:10:06 -04002062 spin_unlock(&delayed_refs->lock);
2063
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002065 must_insert_reserved);
2066 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002067
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002068 btrfs_put_delayed_ref(ref);
2069 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002070 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002071
Chris Mason1887be62009-03-13 10:11:24 -04002072 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002073 spin_lock(&delayed_refs->lock);
2074 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002075 return count;
2076}
2077
2078/*
2079 * this starts processing the delayed reference count updates and
2080 * extent insertions we have queued up so far. count can be
2081 * 0, which means to process everything in the tree at the start
2082 * of the run (but not newly added entries), or it can be some target
2083 * number you'd like to process.
2084 */
2085int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2086 struct btrfs_root *root, unsigned long count)
2087{
2088 struct rb_node *node;
2089 struct btrfs_delayed_ref_root *delayed_refs;
2090 struct btrfs_delayed_ref_node *ref;
2091 struct list_head cluster;
2092 int ret;
2093 int run_all = count == (unsigned long)-1;
2094 int run_most = 0;
2095
2096 if (root == root->fs_info->extent_root)
2097 root = root->fs_info->tree_root;
2098
2099 delayed_refs = &trans->transaction->delayed_refs;
2100 INIT_LIST_HEAD(&cluster);
2101again:
2102 spin_lock(&delayed_refs->lock);
2103 if (count == 0) {
2104 count = delayed_refs->num_entries * 2;
2105 run_most = 1;
2106 }
2107 while (1) {
2108 if (!(run_all || run_most) &&
2109 delayed_refs->num_heads_ready < 64)
2110 break;
2111
2112 /*
2113 * go find something we can process in the rbtree. We start at
2114 * the beginning of the tree, and then build a cluster
2115 * of refs to process starting at the first one we are able to
2116 * lock
2117 */
2118 ret = btrfs_find_ref_cluster(trans, &cluster,
2119 delayed_refs->run_delayed_start);
2120 if (ret)
2121 break;
2122
2123 ret = run_clustered_refs(trans, root, &cluster);
2124 BUG_ON(ret < 0);
2125
2126 count -= min_t(unsigned long, ret, count);
2127
2128 if (count == 0)
2129 break;
2130 }
2131
Chris Mason56bec292009-03-13 10:10:06 -04002132 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002133 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002134 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002135 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002136 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002137
2138 while (node) {
2139 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2140 rb_node);
2141 if (btrfs_delayed_ref_is_head(ref)) {
2142 struct btrfs_delayed_ref_head *head;
2143
2144 head = btrfs_delayed_node_to_head(ref);
2145 atomic_inc(&ref->refs);
2146
2147 spin_unlock(&delayed_refs->lock);
2148 mutex_lock(&head->mutex);
2149 mutex_unlock(&head->mutex);
2150
2151 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002152 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002153 goto again;
2154 }
2155 node = rb_next(node);
2156 }
2157 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002158 schedule_timeout(1);
2159 goto again;
2160 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002161out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002162 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002163 return 0;
2164}
2165
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002166int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2167 struct btrfs_root *root,
2168 u64 bytenr, u64 num_bytes, u64 flags,
2169 int is_data)
2170{
2171 struct btrfs_delayed_extent_op *extent_op;
2172 int ret;
2173
2174 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2175 if (!extent_op)
2176 return -ENOMEM;
2177
2178 extent_op->flags_to_set = flags;
2179 extent_op->update_flags = 1;
2180 extent_op->update_key = 0;
2181 extent_op->is_data = is_data ? 1 : 0;
2182
2183 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2184 if (ret)
2185 kfree(extent_op);
2186 return ret;
2187}
2188
2189static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2190 struct btrfs_root *root,
2191 struct btrfs_path *path,
2192 u64 objectid, u64 offset, u64 bytenr)
2193{
2194 struct btrfs_delayed_ref_head *head;
2195 struct btrfs_delayed_ref_node *ref;
2196 struct btrfs_delayed_data_ref *data_ref;
2197 struct btrfs_delayed_ref_root *delayed_refs;
2198 struct rb_node *node;
2199 int ret = 0;
2200
2201 ret = -ENOENT;
2202 delayed_refs = &trans->transaction->delayed_refs;
2203 spin_lock(&delayed_refs->lock);
2204 head = btrfs_find_delayed_ref_head(trans, bytenr);
2205 if (!head)
2206 goto out;
2207
2208 if (!mutex_trylock(&head->mutex)) {
2209 atomic_inc(&head->node.refs);
2210 spin_unlock(&delayed_refs->lock);
2211
2212 btrfs_release_path(root->fs_info->extent_root, path);
2213
2214 mutex_lock(&head->mutex);
2215 mutex_unlock(&head->mutex);
2216 btrfs_put_delayed_ref(&head->node);
2217 return -EAGAIN;
2218 }
2219
2220 node = rb_prev(&head->node.rb_node);
2221 if (!node)
2222 goto out_unlock;
2223
2224 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2225
2226 if (ref->bytenr != bytenr)
2227 goto out_unlock;
2228
2229 ret = 1;
2230 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2231 goto out_unlock;
2232
2233 data_ref = btrfs_delayed_node_to_data_ref(ref);
2234
2235 node = rb_prev(node);
2236 if (node) {
2237 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2238 if (ref->bytenr == bytenr)
2239 goto out_unlock;
2240 }
2241
2242 if (data_ref->root != root->root_key.objectid ||
2243 data_ref->objectid != objectid || data_ref->offset != offset)
2244 goto out_unlock;
2245
2246 ret = 0;
2247out_unlock:
2248 mutex_unlock(&head->mutex);
2249out:
2250 spin_unlock(&delayed_refs->lock);
2251 return ret;
2252}
2253
2254static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2255 struct btrfs_root *root,
2256 struct btrfs_path *path,
2257 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002258{
2259 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002260 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002261 struct btrfs_extent_data_ref *ref;
2262 struct btrfs_extent_inline_ref *iref;
2263 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002264 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002265 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002266 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002267
Chris Masonbe20aa92007-12-17 20:14:01 -05002268 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002269 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002270 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002271
Chris Masonbe20aa92007-12-17 20:14:01 -05002272 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2273 if (ret < 0)
2274 goto out;
2275 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002276
2277 ret = -ENOENT;
2278 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002279 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002280
Zheng Yan31840ae2008-09-23 13:14:14 -04002281 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002282 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002283 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002284
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002285 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002286 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002287
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288 ret = 1;
2289 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2290#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2291 if (item_size < sizeof(*ei)) {
2292 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2293 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002294 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002295#endif
2296 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2297
2298 if (item_size != sizeof(*ei) +
2299 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2300 goto out;
2301
2302 if (btrfs_extent_generation(leaf, ei) <=
2303 btrfs_root_last_snapshot(&root->root_item))
2304 goto out;
2305
2306 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2307 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2308 BTRFS_EXTENT_DATA_REF_KEY)
2309 goto out;
2310
2311 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2312 if (btrfs_extent_refs(leaf, ei) !=
2313 btrfs_extent_data_ref_count(leaf, ref) ||
2314 btrfs_extent_data_ref_root(leaf, ref) !=
2315 root->root_key.objectid ||
2316 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2317 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2318 goto out;
2319
Yan Zhengf321e492008-07-30 09:26:11 -04002320 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002321out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002322 return ret;
2323}
2324
2325int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2326 struct btrfs_root *root,
2327 u64 objectid, u64 offset, u64 bytenr)
2328{
2329 struct btrfs_path *path;
2330 int ret;
2331 int ret2;
2332
2333 path = btrfs_alloc_path();
2334 if (!path)
2335 return -ENOENT;
2336
2337 do {
2338 ret = check_committed_ref(trans, root, path, objectid,
2339 offset, bytenr);
2340 if (ret && ret != -ENOENT)
2341 goto out;
2342
2343 ret2 = check_delayed_ref(trans, root, path, objectid,
2344 offset, bytenr);
2345 } while (ret2 == -EAGAIN);
2346
2347 if (ret2 && ret2 != -ENOENT) {
2348 ret = ret2;
2349 goto out;
2350 }
2351
2352 if (ret != -ENOENT || ret2 != -ENOENT)
2353 ret = 0;
2354out:
Yan Zhengf321e492008-07-30 09:26:11 -04002355 btrfs_free_path(path);
2356 return ret;
2357}
2358
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002359#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002360int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2361 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002362{
Chris Mason5f39d392007-10-15 16:14:19 -04002363 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002364 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002365 u64 root_gen;
2366 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002367 int i;
Chris Masondb945352007-10-15 16:15:53 -04002368 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002369 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002370 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002371
Chris Mason3768f362007-03-13 16:47:54 -04002372 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002373 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002374
Zheng Yane4657682008-09-26 10:04:53 -04002375 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2376 shared = 0;
2377 root_gen = root->root_key.offset;
2378 } else {
2379 shared = 1;
2380 root_gen = trans->transid - 1;
2381 }
2382
Chris Masondb945352007-10-15 16:15:53 -04002383 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002384 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002385
Zheng Yan31840ae2008-09-23 13:14:14 -04002386 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002387 struct btrfs_leaf_ref *ref;
2388 struct btrfs_extent_info *info;
2389
Zheng Yan31840ae2008-09-23 13:14:14 -04002390 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002391 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002392 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002393 goto out;
2394 }
2395
Zheng Yane4657682008-09-26 10:04:53 -04002396 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002397 ref->bytenr = buf->start;
2398 ref->owner = btrfs_header_owner(buf);
2399 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002400 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002401 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002402
Zheng Yan31840ae2008-09-23 13:14:14 -04002403 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002404 u64 disk_bytenr;
2405 btrfs_item_key_to_cpu(buf, &key, i);
2406 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2407 continue;
2408 fi = btrfs_item_ptr(buf, i,
2409 struct btrfs_file_extent_item);
2410 if (btrfs_file_extent_type(buf, fi) ==
2411 BTRFS_FILE_EXTENT_INLINE)
2412 continue;
2413 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2414 if (disk_bytenr == 0)
2415 continue;
2416
2417 info->bytenr = disk_bytenr;
2418 info->num_bytes =
2419 btrfs_file_extent_disk_num_bytes(buf, fi);
2420 info->objectid = key.objectid;
2421 info->offset = key.offset;
2422 info++;
2423 }
2424
Zheng Yane4657682008-09-26 10:04:53 -04002425 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002426 if (ret == -EEXIST && shared) {
2427 struct btrfs_leaf_ref *old;
2428 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2429 BUG_ON(!old);
2430 btrfs_remove_leaf_ref(root, old);
2431 btrfs_free_leaf_ref(root, old);
2432 ret = btrfs_add_leaf_ref(root, ref, shared);
2433 }
Yan Zheng31153d82008-07-28 15:32:19 -04002434 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002435 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002436 }
2437out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002438 return ret;
2439}
2440
Chris Masonb7a9f292009-02-04 09:23:45 -05002441/* when a block goes through cow, we update the reference counts of
2442 * everything that block points to. The internal pointers of the block
2443 * can be in just about any order, and it is likely to have clusters of
2444 * things that are close together and clusters of things that are not.
2445 *
2446 * To help reduce the seeks that come with updating all of these reference
2447 * counts, sort them by byte number before actual updates are done.
2448 *
2449 * struct refsort is used to match byte number to slot in the btree block.
2450 * we sort based on the byte number and then use the slot to actually
2451 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002452 *
2453 * struct refsort is smaller than strcut btrfs_item and smaller than
2454 * struct btrfs_key_ptr. Since we're currently limited to the page size
2455 * for a btree block, there's no way for a kmalloc of refsorts for a
2456 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002457 */
2458struct refsort {
2459 u64 bytenr;
2460 u32 slot;
2461};
2462
2463/*
2464 * for passing into sort()
2465 */
2466static int refsort_cmp(const void *a_void, const void *b_void)
2467{
2468 const struct refsort *a = a_void;
2469 const struct refsort *b = b_void;
2470
2471 if (a->bytenr < b->bytenr)
2472 return -1;
2473 if (a->bytenr > b->bytenr)
2474 return 1;
2475 return 0;
2476}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002477#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002478
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002479static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002480 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002481 struct extent_buffer *buf,
2482 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002483{
2484 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002485 u64 num_bytes;
2486 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002487 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002488 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002489 struct btrfs_key key;
2490 struct btrfs_file_extent_item *fi;
2491 int i;
2492 int level;
2493 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002494 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002495 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002496
2497 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002498 nritems = btrfs_header_nritems(buf);
2499 level = btrfs_header_level(buf);
2500
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002501 if (!root->ref_cows && level == 0)
2502 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002503
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002504 if (inc)
2505 process_func = btrfs_inc_extent_ref;
2506 else
2507 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002509 if (full_backref)
2510 parent = buf->start;
2511 else
2512 parent = 0;
2513
Zheng Yan31840ae2008-09-23 13:14:14 -04002514 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002515 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002516 btrfs_item_key_to_cpu(buf, &key, i);
2517 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002518 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002519 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002520 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002521 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002522 BTRFS_FILE_EXTENT_INLINE)
2523 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002524 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2525 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002526 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002527
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002528 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2529 key.offset -= btrfs_file_extent_offset(buf, fi);
2530 ret = process_func(trans, root, bytenr, num_bytes,
2531 parent, ref_root, key.objectid,
2532 key.offset);
2533 if (ret)
2534 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002535 } else {
2536 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002537 num_bytes = btrfs_level_size(root, level - 1);
2538 ret = process_func(trans, root, bytenr, num_bytes,
2539 parent, ref_root, level - 1, 0);
2540 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002541 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002542 }
2543 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002544 return 0;
2545fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002546 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002547 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002548}
2549
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002550int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2551 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002552{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002553 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2554}
Zheng Yan31840ae2008-09-23 13:14:14 -04002555
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002556int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2557 struct extent_buffer *buf, int full_backref)
2558{
2559 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002560}
2561
Chris Mason9078a3e2007-04-26 16:46:15 -04002562static int write_one_cache_group(struct btrfs_trans_handle *trans,
2563 struct btrfs_root *root,
2564 struct btrfs_path *path,
2565 struct btrfs_block_group_cache *cache)
2566{
2567 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002568 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002569 unsigned long bi;
2570 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002571
Chris Mason9078a3e2007-04-26 16:46:15 -04002572 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002573 if (ret < 0)
2574 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002575 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002576
2577 leaf = path->nodes[0];
2578 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2579 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2580 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002581 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002582fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002583 if (ret)
2584 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002585 return 0;
2586
2587}
2588
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002589static struct btrfs_block_group_cache *
2590next_block_group(struct btrfs_root *root,
2591 struct btrfs_block_group_cache *cache)
2592{
2593 struct rb_node *node;
2594 spin_lock(&root->fs_info->block_group_cache_lock);
2595 node = rb_next(&cache->cache_node);
2596 btrfs_put_block_group(cache);
2597 if (node) {
2598 cache = rb_entry(node, struct btrfs_block_group_cache,
2599 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002600 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002601 } else
2602 cache = NULL;
2603 spin_unlock(&root->fs_info->block_group_cache_lock);
2604 return cache;
2605}
2606
Chris Mason96b51792007-10-15 16:15:19 -04002607int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2608 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002609{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002610 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002611 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002612 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002613 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002614
2615 path = btrfs_alloc_path();
2616 if (!path)
2617 return -ENOMEM;
2618
Chris Masond3977122009-01-05 21:25:51 -05002619 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002620 if (last == 0) {
2621 err = btrfs_run_delayed_refs(trans, root,
2622 (unsigned long)-1);
2623 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002624 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002625
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002626 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2627 while (cache) {
2628 if (cache->dirty)
2629 break;
2630 cache = next_block_group(root, cache);
2631 }
2632 if (!cache) {
2633 if (last == 0)
2634 break;
2635 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002636 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002637 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002638
2639 cache->dirty = 0;
2640 last = cache->key.objectid + cache->key.offset;
2641
2642 err = write_one_cache_group(trans, root, path, cache);
2643 BUG_ON(err);
2644 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002645 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002646
Chris Mason9078a3e2007-04-26 16:46:15 -04002647 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002648 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002649}
2650
Yan Zhengd2fb3432008-12-11 16:30:39 -05002651int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2652{
2653 struct btrfs_block_group_cache *block_group;
2654 int readonly = 0;
2655
2656 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2657 if (!block_group || block_group->ro)
2658 readonly = 1;
2659 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002660 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002661 return readonly;
2662}
2663
Chris Mason593060d2008-03-25 16:50:33 -04002664static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2665 u64 total_bytes, u64 bytes_used,
2666 struct btrfs_space_info **space_info)
2667{
2668 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002669 int i;
2670 int factor;
2671
2672 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2673 BTRFS_BLOCK_GROUP_RAID10))
2674 factor = 2;
2675 else
2676 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002677
2678 found = __find_space_info(info, flags);
2679 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002680 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002681 found->total_bytes += total_bytes;
2682 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002683 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002684 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002685 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002686 *space_info = found;
2687 return 0;
2688 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002689 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002690 if (!found)
2691 return -ENOMEM;
2692
Yan, Zhengb742bb82010-05-16 10:46:24 -04002693 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2694 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04002695 init_rwsem(&found->groups_sem);
Josef Bacikb5cb1602010-03-12 19:28:18 +00002696 init_waitqueue_head(&found->flush_wait);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002697 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04002698 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2699 BTRFS_BLOCK_GROUP_SYSTEM |
2700 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04002701 found->total_bytes = total_bytes;
2702 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002703 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002704 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002705 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002706 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002707 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002708 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002709 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002710 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002711 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002712 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002713 return 0;
2714}
2715
Chris Mason8790d502008-04-03 16:29:03 -04002716static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2717{
2718 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002719 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002720 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002721 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002722 if (extra_flags) {
2723 if (flags & BTRFS_BLOCK_GROUP_DATA)
2724 fs_info->avail_data_alloc_bits |= extra_flags;
2725 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2726 fs_info->avail_metadata_alloc_bits |= extra_flags;
2727 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2728 fs_info->avail_system_alloc_bits |= extra_flags;
2729 }
2730}
Chris Mason593060d2008-03-25 16:50:33 -04002731
Yan Zhengc146afa2008-11-12 14:34:12 -05002732static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2733{
2734 spin_lock(&cache->space_info->lock);
2735 spin_lock(&cache->lock);
2736 if (!cache->ro) {
2737 cache->space_info->bytes_readonly += cache->key.offset -
2738 btrfs_block_group_used(&cache->item);
2739 cache->ro = 1;
2740 }
2741 spin_unlock(&cache->lock);
2742 spin_unlock(&cache->space_info->lock);
2743}
2744
Yan Zheng2b820322008-11-17 21:11:30 -05002745u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002746{
Yan Zheng2b820322008-11-17 21:11:30 -05002747 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002748
2749 if (num_devices == 1)
2750 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2751 if (num_devices < 4)
2752 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2753
Chris Masonec44a352008-04-28 15:29:52 -04002754 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2755 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002756 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002757 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002758 }
Chris Masonec44a352008-04-28 15:29:52 -04002759
2760 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002761 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002762 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002763 }
Chris Masonec44a352008-04-28 15:29:52 -04002764
2765 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2766 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2767 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2768 (flags & BTRFS_BLOCK_GROUP_DUP)))
2769 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2770 return flags;
2771}
2772
Yan, Zhengb742bb82010-05-16 10:46:24 -04002773static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05002774{
Yan, Zhengb742bb82010-05-16 10:46:24 -04002775 if (flags & BTRFS_BLOCK_GROUP_DATA)
2776 flags |= root->fs_info->avail_data_alloc_bits &
2777 root->fs_info->data_alloc_profile;
2778 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2779 flags |= root->fs_info->avail_system_alloc_bits &
2780 root->fs_info->system_alloc_profile;
2781 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
2782 flags |= root->fs_info->avail_metadata_alloc_bits &
2783 root->fs_info->metadata_alloc_profile;
2784 return btrfs_reduce_alloc_profile(root, flags);
2785}
Josef Bacik6a632092009-02-20 11:00:09 -05002786
Yan, Zhengb742bb82010-05-16 10:46:24 -04002787static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
2788{
2789 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05002790
Yan, Zhengb742bb82010-05-16 10:46:24 -04002791 if (data)
2792 flags = BTRFS_BLOCK_GROUP_DATA;
2793 else if (root == root->fs_info->chunk_root)
2794 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2795 else
2796 flags = BTRFS_BLOCK_GROUP_METADATA;
2797
2798 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05002799}
2800
2801void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2802{
2803 u64 alloc_target;
2804
2805 alloc_target = btrfs_get_alloc_profile(root, 1);
2806 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2807 alloc_target);
2808}
2809
Josef Bacik9ed74f22009-09-11 16:12:44 -04002810static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2811{
2812 u64 num_bytes;
2813 int level;
2814
2815 level = BTRFS_MAX_LEVEL - 2;
2816 /*
2817 * NOTE: these calculations are absolutely the worst possible case.
2818 * This assumes that _every_ item we insert will require a new leaf, and
2819 * that the tree has grown to its maximum level size.
2820 */
2821
2822 /*
2823 * for every item we insert we could insert both an extent item and a
2824 * extent ref item. Then for ever item we insert, we will need to cow
2825 * both the original leaf, plus the leaf to the left and right of it.
2826 *
2827 * Unless we are talking about the extent root, then we just want the
2828 * number of items * 2, since we just need the extent item plus its ref.
2829 */
2830 if (root == root->fs_info->extent_root)
2831 num_bytes = num_items * 2;
2832 else
2833 num_bytes = (num_items + (2 * num_items)) * 3;
2834
2835 /*
2836 * num_bytes is total number of leaves we could need times the leaf
2837 * size, and then for every leaf we could end up cow'ing 2 nodes per
2838 * level, down to the leaf level.
2839 */
2840 num_bytes = (num_bytes * root->leafsize) +
2841 (num_bytes * (level * 2)) * root->nodesize;
2842
2843 return num_bytes;
2844}
2845
Josef Bacik6a632092009-02-20 11:00:09 -05002846/*
Josef Bacik9ed74f22009-09-11 16:12:44 -04002847 * Unreserve metadata space for delalloc. If we have less reserved credits than
2848 * we have extents, this function does nothing.
Josef Bacik6a632092009-02-20 11:00:09 -05002849 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04002850int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2851 struct inode *inode, int num_items)
Josef Bacik6a632092009-02-20 11:00:09 -05002852{
2853 struct btrfs_fs_info *info = root->fs_info;
2854 struct btrfs_space_info *meta_sinfo;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002855 u64 num_bytes;
2856 u64 alloc_target;
2857 bool bug = false;
Josef Bacik6a632092009-02-20 11:00:09 -05002858
2859 /* get the space info for where the metadata will live */
2860 alloc_target = btrfs_get_alloc_profile(root, 0);
2861 meta_sinfo = __find_space_info(info, alloc_target);
2862
Josef Bacik9ed74f22009-09-11 16:12:44 -04002863 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2864 num_items);
2865
2866 spin_lock(&meta_sinfo->lock);
Josef Bacik32c00af2009-10-08 13:34:05 -04002867 spin_lock(&BTRFS_I(inode)->accounting_lock);
2868 if (BTRFS_I(inode)->reserved_extents <=
2869 BTRFS_I(inode)->outstanding_extents) {
2870 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002871 spin_unlock(&meta_sinfo->lock);
2872 return 0;
2873 }
Josef Bacik32c00af2009-10-08 13:34:05 -04002874 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002875
Josef Bacik287a0ab2010-03-19 18:07:23 +00002876 BTRFS_I(inode)->reserved_extents -= num_items;
Josef Bacik32c00af2009-10-08 13:34:05 -04002877 BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002878
2879 if (meta_sinfo->bytes_delalloc < num_bytes) {
2880 bug = true;
2881 meta_sinfo->bytes_delalloc = 0;
2882 } else {
2883 meta_sinfo->bytes_delalloc -= num_bytes;
2884 }
2885 spin_unlock(&meta_sinfo->lock);
2886
2887 BUG_ON(bug);
2888
2889 return 0;
2890}
2891
2892static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2893{
2894 u64 thresh;
2895
2896 thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2897 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2898 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2899 meta_sinfo->bytes_may_use;
2900
2901 thresh = meta_sinfo->total_bytes - thresh;
2902 thresh *= 80;
2903 do_div(thresh, 100);
2904 if (thresh <= meta_sinfo->bytes_delalloc)
2905 meta_sinfo->force_delalloc = 1;
2906 else
2907 meta_sinfo->force_delalloc = 0;
2908}
2909
Josef Bacike3ccfa92009-10-07 20:44:34 -04002910struct async_flush {
2911 struct btrfs_root *root;
2912 struct btrfs_space_info *info;
2913 struct btrfs_work work;
2914};
2915
2916static noinline void flush_delalloc_async(struct btrfs_work *work)
2917{
2918 struct async_flush *async;
2919 struct btrfs_root *root;
2920 struct btrfs_space_info *info;
2921
2922 async = container_of(work, struct async_flush, work);
2923 root = async->root;
2924 info = async->info;
2925
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002926 btrfs_start_delalloc_inodes(root, 0);
Josef Bacike3ccfa92009-10-07 20:44:34 -04002927 wake_up(&info->flush_wait);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002928 btrfs_wait_ordered_extents(root, 0, 0);
Josef Bacike3ccfa92009-10-07 20:44:34 -04002929
2930 spin_lock(&info->lock);
2931 info->flushing = 0;
2932 spin_unlock(&info->lock);
2933 wake_up(&info->flush_wait);
2934
2935 kfree(async);
2936}
2937
2938static void wait_on_flush(struct btrfs_space_info *info)
2939{
2940 DEFINE_WAIT(wait);
2941 u64 used;
2942
2943 while (1) {
2944 prepare_to_wait(&info->flush_wait, &wait,
2945 TASK_UNINTERRUPTIBLE);
2946 spin_lock(&info->lock);
2947 if (!info->flushing) {
2948 spin_unlock(&info->lock);
2949 break;
2950 }
2951
2952 used = info->bytes_used + info->bytes_reserved +
2953 info->bytes_pinned + info->bytes_readonly +
2954 info->bytes_super + info->bytes_root +
2955 info->bytes_may_use + info->bytes_delalloc;
2956 if (used < info->total_bytes) {
2957 spin_unlock(&info->lock);
2958 break;
2959 }
2960 spin_unlock(&info->lock);
2961 schedule();
2962 }
2963 finish_wait(&info->flush_wait, &wait);
2964}
2965
Josef Bacik32c00af2009-10-08 13:34:05 -04002966static void flush_delalloc(struct btrfs_root *root,
2967 struct btrfs_space_info *info)
2968{
Josef Bacike3ccfa92009-10-07 20:44:34 -04002969 struct async_flush *async;
Josef Bacik32c00af2009-10-08 13:34:05 -04002970 bool wait = false;
2971
2972 spin_lock(&info->lock);
2973
Josef Bacikb5cb1602010-03-12 19:28:18 +00002974 if (!info->flushing)
Josef Bacik32c00af2009-10-08 13:34:05 -04002975 info->flushing = 1;
Josef Bacikb5cb1602010-03-12 19:28:18 +00002976 else
Josef Bacik32c00af2009-10-08 13:34:05 -04002977 wait = true;
Josef Bacik32c00af2009-10-08 13:34:05 -04002978
2979 spin_unlock(&info->lock);
2980
2981 if (wait) {
Josef Bacike3ccfa92009-10-07 20:44:34 -04002982 wait_on_flush(info);
Josef Bacik32c00af2009-10-08 13:34:05 -04002983 return;
2984 }
2985
Josef Bacike3ccfa92009-10-07 20:44:34 -04002986 async = kzalloc(sizeof(*async), GFP_NOFS);
2987 if (!async)
2988 goto flush;
2989
2990 async->root = root;
2991 async->info = info;
2992 async->work.func = flush_delalloc_async;
2993
2994 btrfs_queue_worker(&root->fs_info->enospc_workers,
2995 &async->work);
2996 wait_on_flush(info);
2997 return;
2998
2999flush:
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003000 btrfs_start_delalloc_inodes(root, 0);
3001 btrfs_wait_ordered_extents(root, 0, 0);
Josef Bacik32c00af2009-10-08 13:34:05 -04003002
3003 spin_lock(&info->lock);
3004 info->flushing = 0;
3005 spin_unlock(&info->lock);
3006 wake_up(&info->flush_wait);
3007}
3008
Josef Bacik9ed74f22009-09-11 16:12:44 -04003009/*
3010 * Reserve metadata space for delalloc.
3011 */
3012int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
3013 struct inode *inode, int num_items)
3014{
3015 struct btrfs_fs_info *info = root->fs_info;
3016 struct btrfs_space_info *meta_sinfo;
3017 u64 num_bytes;
3018 u64 used;
3019 u64 alloc_target;
3020 int flushed = 0;
3021 int force_delalloc;
3022
3023 /* get the space info for where the metadata will live */
3024 alloc_target = btrfs_get_alloc_profile(root, 0);
3025 meta_sinfo = __find_space_info(info, alloc_target);
3026
3027 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
3028 num_items);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003029again:
Josef Bacik6a632092009-02-20 11:00:09 -05003030 spin_lock(&meta_sinfo->lock);
Josef Bacik6a632092009-02-20 11:00:09 -05003031
Josef Bacik9ed74f22009-09-11 16:12:44 -04003032 force_delalloc = meta_sinfo->force_delalloc;
Josef Bacik6a632092009-02-20 11:00:09 -05003033
Josef Bacik9ed74f22009-09-11 16:12:44 -04003034 if (unlikely(!meta_sinfo->bytes_root))
3035 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3036
3037 if (!flushed)
3038 meta_sinfo->bytes_delalloc += num_bytes;
3039
3040 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3041 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3042 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3043 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3044
3045 if (used > meta_sinfo->total_bytes) {
3046 flushed++;
3047
3048 if (flushed == 1) {
Yan, Zheng424499d2010-05-16 10:46:25 -04003049 if (maybe_allocate_chunk(NULL, root, meta_sinfo,
3050 num_bytes))
Josef Bacik9ed74f22009-09-11 16:12:44 -04003051 goto again;
3052 flushed++;
3053 } else {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003054 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003055 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003056
Josef Bacik9ed74f22009-09-11 16:12:44 -04003057 if (flushed == 2) {
3058 filemap_flush(inode->i_mapping);
3059 goto again;
3060 } else if (flushed == 3) {
Josef Bacik32c00af2009-10-08 13:34:05 -04003061 flush_delalloc(root, meta_sinfo);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003062 goto again;
3063 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003064 spin_lock(&meta_sinfo->lock);
3065 meta_sinfo->bytes_delalloc -= num_bytes;
Josef Bacik6a632092009-02-20 11:00:09 -05003066 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003067 printk(KERN_ERR "enospc, has %d, reserved %d\n",
Josef Bacik32c00af2009-10-08 13:34:05 -04003068 BTRFS_I(inode)->outstanding_extents,
3069 BTRFS_I(inode)->reserved_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003070 dump_space_info(meta_sinfo, 0, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003071 return -ENOSPC;
3072 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003073
Josef Bacik287a0ab2010-03-19 18:07:23 +00003074 BTRFS_I(inode)->reserved_extents += num_items;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003075 check_force_delalloc(meta_sinfo);
3076 spin_unlock(&meta_sinfo->lock);
3077
3078 if (!flushed && force_delalloc)
3079 filemap_flush(inode->i_mapping);
3080
3081 return 0;
3082}
3083
3084/*
3085 * unreserve num_items number of items worth of metadata space. This needs to
3086 * be paired with btrfs_reserve_metadata_space.
3087 *
3088 * NOTE: if you have the option, run this _AFTER_ you do a
3089 * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
3090 * oprations which will result in more used metadata, so we want to make sure we
3091 * can do that without issue.
3092 */
3093int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
3094{
3095 struct btrfs_fs_info *info = root->fs_info;
3096 struct btrfs_space_info *meta_sinfo;
3097 u64 num_bytes;
3098 u64 alloc_target;
3099 bool bug = false;
3100
3101 /* get the space info for where the metadata will live */
3102 alloc_target = btrfs_get_alloc_profile(root, 0);
3103 meta_sinfo = __find_space_info(info, alloc_target);
3104
3105 num_bytes = calculate_bytes_needed(root, num_items);
3106
3107 spin_lock(&meta_sinfo->lock);
3108 if (meta_sinfo->bytes_may_use < num_bytes) {
3109 bug = true;
3110 meta_sinfo->bytes_may_use = 0;
3111 } else {
3112 meta_sinfo->bytes_may_use -= num_bytes;
3113 }
3114 spin_unlock(&meta_sinfo->lock);
3115
3116 BUG_ON(bug);
3117
3118 return 0;
3119}
3120
3121/*
3122 * Reserve some metadata space for use. We'll calculate the worste case number
3123 * of bytes that would be needed to modify num_items number of items. If we
3124 * have space, fantastic, if not, you get -ENOSPC. Please call
3125 * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
3126 * items you reserved, since whatever metadata you needed should have already
3127 * been allocated.
3128 *
3129 * This will commit the transaction to make more space if we don't have enough
3130 * metadata space. THe only time we don't do this is if we're reserving space
3131 * inside of a transaction, then we will just return -ENOSPC and it is the
3132 * callers responsibility to handle it properly.
3133 */
3134int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3135{
3136 struct btrfs_fs_info *info = root->fs_info;
3137 struct btrfs_space_info *meta_sinfo;
3138 u64 num_bytes;
3139 u64 used;
3140 u64 alloc_target;
3141 int retries = 0;
3142
3143 /* get the space info for where the metadata will live */
3144 alloc_target = btrfs_get_alloc_profile(root, 0);
3145 meta_sinfo = __find_space_info(info, alloc_target);
3146
3147 num_bytes = calculate_bytes_needed(root, num_items);
3148again:
3149 spin_lock(&meta_sinfo->lock);
3150
3151 if (unlikely(!meta_sinfo->bytes_root))
3152 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3153
3154 if (!retries)
3155 meta_sinfo->bytes_may_use += num_bytes;
3156
3157 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3158 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3159 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3160 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3161
3162 if (used > meta_sinfo->total_bytes) {
3163 retries++;
3164 if (retries == 1) {
Yan, Zheng424499d2010-05-16 10:46:25 -04003165 if (maybe_allocate_chunk(NULL, root, meta_sinfo,
3166 num_bytes))
Josef Bacik9ed74f22009-09-11 16:12:44 -04003167 goto again;
3168 retries++;
3169 } else {
3170 spin_unlock(&meta_sinfo->lock);
3171 }
3172
3173 if (retries == 2) {
Josef Bacik32c00af2009-10-08 13:34:05 -04003174 flush_delalloc(root, meta_sinfo);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003175 goto again;
3176 }
3177 spin_lock(&meta_sinfo->lock);
3178 meta_sinfo->bytes_may_use -= num_bytes;
3179 spin_unlock(&meta_sinfo->lock);
3180
3181 dump_space_info(meta_sinfo, 0, 0);
3182 return -ENOSPC;
3183 }
3184
3185 check_force_delalloc(meta_sinfo);
Josef Bacik6a632092009-02-20 11:00:09 -05003186 spin_unlock(&meta_sinfo->lock);
3187
3188 return 0;
3189}
3190
3191/*
3192 * This will check the space that the inode allocates from to make sure we have
3193 * enough space for bytes.
3194 */
3195int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3196 u64 bytes)
3197{
3198 struct btrfs_space_info *data_sinfo;
Josef Bacikab6e2412010-03-19 14:38:13 +00003199 u64 used;
3200 int ret = 0, committed = 0, flushed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003201
3202 /* make sure bytes are sectorsize aligned */
3203 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3204
3205 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003206 if (!data_sinfo)
3207 goto alloc;
3208
Josef Bacik6a632092009-02-20 11:00:09 -05003209again:
3210 /* make sure we have enough space to handle the data first */
3211 spin_lock(&data_sinfo->lock);
Josef Bacikab6e2412010-03-19 14:38:13 +00003212 used = data_sinfo->bytes_used + data_sinfo->bytes_delalloc +
3213 data_sinfo->bytes_reserved + data_sinfo->bytes_pinned +
3214 data_sinfo->bytes_readonly + data_sinfo->bytes_may_use +
3215 data_sinfo->bytes_super;
3216
3217 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003218 struct btrfs_trans_handle *trans;
3219
Josef Bacikab6e2412010-03-19 14:38:13 +00003220 if (!flushed) {
3221 spin_unlock(&data_sinfo->lock);
3222 flush_delalloc(root, data_sinfo);
3223 flushed = 1;
3224 goto again;
3225 }
3226
Josef Bacik6a632092009-02-20 11:00:09 -05003227 /*
3228 * if we don't have enough free bytes in this space then we need
3229 * to alloc a new chunk.
3230 */
3231 if (!data_sinfo->full) {
3232 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003233
3234 data_sinfo->force_alloc = 1;
3235 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003236alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003237 alloc_target = btrfs_get_alloc_profile(root, 1);
3238 trans = btrfs_start_transaction(root, 1);
3239 if (!trans)
3240 return -ENOMEM;
3241
3242 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3243 bytes + 2 * 1024 * 1024,
3244 alloc_target, 0);
3245 btrfs_end_transaction(trans, root);
3246 if (ret)
3247 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04003248
3249 if (!data_sinfo) {
3250 btrfs_set_inode_space_info(root, inode);
3251 data_sinfo = BTRFS_I(inode)->space_info;
3252 }
Josef Bacik6a632092009-02-20 11:00:09 -05003253 goto again;
3254 }
3255 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003256
3257 /* commit the current transaction and try again */
Sage Weildd7e0b72009-09-29 18:38:44 -04003258 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003259 committed = 1;
3260 trans = btrfs_join_transaction(root, 1);
3261 if (!trans)
3262 return -ENOMEM;
3263 ret = btrfs_commit_transaction(trans, root);
3264 if (ret)
3265 return ret;
3266 goto again;
3267 }
3268
Josef Bacik6a632092009-02-20 11:00:09 -05003269 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3270 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04003271 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07003272 "%llu total\n", (unsigned long long)bytes,
3273 (unsigned long long)data_sinfo->bytes_delalloc,
3274 (unsigned long long)data_sinfo->bytes_used,
3275 (unsigned long long)data_sinfo->bytes_reserved,
3276 (unsigned long long)data_sinfo->bytes_pinned,
3277 (unsigned long long)data_sinfo->bytes_readonly,
3278 (unsigned long long)data_sinfo->bytes_may_use,
3279 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003280 return -ENOSPC;
3281 }
3282 data_sinfo->bytes_may_use += bytes;
3283 BTRFS_I(inode)->reserved_bytes += bytes;
3284 spin_unlock(&data_sinfo->lock);
3285
Josef Bacik9ed74f22009-09-11 16:12:44 -04003286 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003287}
3288
3289/*
3290 * if there was an error for whatever reason after calling
3291 * btrfs_check_data_free_space, call this so we can cleanup the counters.
3292 */
3293void btrfs_free_reserved_data_space(struct btrfs_root *root,
3294 struct inode *inode, u64 bytes)
3295{
3296 struct btrfs_space_info *data_sinfo;
3297
3298 /* make sure bytes are sectorsize aligned */
3299 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3300
3301 data_sinfo = BTRFS_I(inode)->space_info;
3302 spin_lock(&data_sinfo->lock);
3303 data_sinfo->bytes_may_use -= bytes;
3304 BTRFS_I(inode)->reserved_bytes -= bytes;
3305 spin_unlock(&data_sinfo->lock);
3306}
3307
3308/* called when we are adding a delalloc extent to the inode's io_tree */
3309void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3310 u64 bytes)
3311{
3312 struct btrfs_space_info *data_sinfo;
3313
3314 /* get the space info for where this inode will be storing its data */
3315 data_sinfo = BTRFS_I(inode)->space_info;
3316
3317 /* make sure we have enough space to handle the data first */
3318 spin_lock(&data_sinfo->lock);
3319 data_sinfo->bytes_delalloc += bytes;
3320
3321 /*
3322 * we are adding a delalloc extent without calling
3323 * btrfs_check_data_free_space first. This happens on a weird
3324 * writepage condition, but shouldn't hurt our accounting
3325 */
3326 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3327 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3328 BTRFS_I(inode)->reserved_bytes = 0;
3329 } else {
3330 data_sinfo->bytes_may_use -= bytes;
3331 BTRFS_I(inode)->reserved_bytes -= bytes;
3332 }
3333
3334 spin_unlock(&data_sinfo->lock);
3335}
3336
3337/* called when we are clearing an delalloc extent from the inode's io_tree */
3338void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3339 u64 bytes)
3340{
3341 struct btrfs_space_info *info;
3342
3343 info = BTRFS_I(inode)->space_info;
3344
3345 spin_lock(&info->lock);
3346 info->bytes_delalloc -= bytes;
3347 spin_unlock(&info->lock);
3348}
3349
Josef Bacik97e728d2009-04-21 17:40:57 -04003350static void force_metadata_allocation(struct btrfs_fs_info *info)
3351{
3352 struct list_head *head = &info->space_info;
3353 struct btrfs_space_info *found;
3354
3355 rcu_read_lock();
3356 list_for_each_entry_rcu(found, head, list) {
3357 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3358 found->force_alloc = 1;
3359 }
3360 rcu_read_unlock();
3361}
3362
Yan, Zheng424499d2010-05-16 10:46:25 -04003363static int should_alloc_chunk(struct btrfs_space_info *sinfo,
3364 u64 alloc_bytes)
3365{
3366 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3367
3368 if (sinfo->bytes_used + sinfo->bytes_reserved +
3369 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3370 return 0;
3371
3372 if (sinfo->bytes_used + sinfo->bytes_reserved +
3373 alloc_bytes < div_factor(num_bytes, 8))
3374 return 0;
3375
3376 return 1;
3377}
3378
Chris Mason6324fbf2008-03-24 15:01:59 -04003379static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3380 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003381 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003382{
3383 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003384 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Yan Zhengc146afa2008-11-12 14:34:12 -05003385 int ret = 0;
3386
Josef Bacik97e728d2009-04-21 17:40:57 -04003387 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003388
Yan Zheng2b820322008-11-17 21:11:30 -05003389 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003390
Chris Mason6324fbf2008-03-24 15:01:59 -04003391 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003392 if (!space_info) {
3393 ret = update_space_info(extent_root->fs_info, flags,
3394 0, 0, &space_info);
3395 BUG_ON(ret);
3396 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003397 BUG_ON(!space_info);
3398
Josef Bacik25179202008-10-29 14:49:05 -04003399 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003400 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003401 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003402 if (space_info->full) {
3403 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003404 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003405 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003406
Yan, Zheng424499d2010-05-16 10:46:25 -04003407 if (!force && !should_alloc_chunk(space_info, alloc_bytes)) {
Josef Bacik25179202008-10-29 14:49:05 -04003408 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003409 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003410 }
Josef Bacik25179202008-10-29 14:49:05 -04003411 spin_unlock(&space_info->lock);
3412
Josef Bacik97e728d2009-04-21 17:40:57 -04003413 /*
3414 * if we're doing a data chunk, go ahead and make sure that
3415 * we keep a reasonable number of metadata chunks allocated in the
3416 * FS as well.
3417 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003418 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003419 fs_info->data_chunk_allocations++;
3420 if (!(fs_info->data_chunk_allocations %
3421 fs_info->metadata_ratio))
3422 force_metadata_allocation(fs_info);
3423 }
3424
Yan Zheng2b820322008-11-17 21:11:30 -05003425 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003426 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003427 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003428 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003429 else
3430 ret = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003431 space_info->force_alloc = 0;
3432 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003433out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003434 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003435 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003436}
3437
Yan, Zheng424499d2010-05-16 10:46:25 -04003438static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
3439 struct btrfs_root *root,
3440 struct btrfs_space_info *sinfo, u64 num_bytes)
3441{
3442 int ret;
3443 int end_trans = 0;
3444
3445 if (sinfo->full)
3446 return 0;
3447
3448 spin_lock(&sinfo->lock);
3449 ret = should_alloc_chunk(sinfo, num_bytes + 2 * 1024 * 1024);
3450 spin_unlock(&sinfo->lock);
3451 if (!ret)
3452 return 0;
3453
3454 if (!trans) {
3455 trans = btrfs_join_transaction(root, 1);
3456 BUG_ON(IS_ERR(trans));
3457 end_trans = 1;
3458 }
3459
3460 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3461 num_bytes + 2 * 1024 * 1024,
3462 get_alloc_profile(root, sinfo->flags), 0);
3463
3464 if (end_trans)
3465 btrfs_end_transaction(trans, root);
3466
3467 return ret == 1 ? 1 : 0;
3468}
3469
Chris Mason9078a3e2007-04-26 16:46:15 -04003470static int update_block_group(struct btrfs_trans_handle *trans,
3471 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04003472 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04003473 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04003474{
3475 struct btrfs_block_group_cache *cache;
3476 struct btrfs_fs_info *info = root->fs_info;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003477 int factor;
Chris Masondb945352007-10-15 16:15:53 -04003478 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003479 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003480 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04003481
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003482 /* block accounting for super block */
3483 spin_lock(&info->delalloc_lock);
3484 old_val = btrfs_super_bytes_used(&info->super_copy);
3485 if (alloc)
3486 old_val += num_bytes;
3487 else
3488 old_val -= num_bytes;
3489 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003490 spin_unlock(&info->delalloc_lock);
3491
Chris Masond3977122009-01-05 21:25:51 -05003492 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003493 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003494 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04003495 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003496 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
3497 BTRFS_BLOCK_GROUP_RAID1 |
3498 BTRFS_BLOCK_GROUP_RAID10))
3499 factor = 2;
3500 else
3501 factor = 1;
Chris Masondb945352007-10-15 16:15:53 -04003502 byte_in_group = bytenr - cache->key.objectid;
3503 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003504
Josef Bacik25179202008-10-29 14:49:05 -04003505 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003506 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003507 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04003508 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04003509 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04003510 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04003511 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003512 btrfs_set_block_group_used(&cache->item, old_val);
3513 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003514 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003515 cache->space_info->bytes_used += num_bytes;
3516 cache->space_info->disk_used += num_bytes * factor;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003517 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05003518 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003519 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003520 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003521 } else {
Chris Masondb945352007-10-15 16:15:53 -04003522 old_val -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003523 btrfs_set_block_group_used(&cache->item, old_val);
Chris Mason6324fbf2008-03-24 15:01:59 -04003524 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003525 cache->space_info->disk_used -= num_bytes * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05003526 if (cache->ro)
3527 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003528 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003529 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04003530 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003531 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003532
3533 ret = btrfs_discard_extent(root, bytenr,
3534 num_bytes);
3535 WARN_ON(ret);
3536
Josef Bacik0f9dd462008-09-23 13:14:11 -04003537 ret = btrfs_add_free_space(cache, bytenr,
3538 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003539 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04003540 }
Chris Masoncd1bc462007-04-27 10:08:34 -04003541 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003542 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003543 total -= num_bytes;
3544 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003545 }
3546 return 0;
3547}
Chris Mason6324fbf2008-03-24 15:01:59 -04003548
Chris Masona061fc82008-05-07 11:43:44 -04003549static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3550{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003551 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003552 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003553
3554 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3555 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003556 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003557
Yan Zhengd2fb3432008-12-11 16:30:39 -05003558 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003559 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003560
3561 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003562}
3563
Yan Zheng11833d62009-09-11 16:11:19 -04003564/*
3565 * this function must be called within transaction
3566 */
3567int btrfs_pin_extent(struct btrfs_root *root,
3568 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05003569{
Yan324ae4d2007-11-16 14:57:08 -05003570 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -04003571 struct btrfs_block_group_cache *cache;
Yan324ae4d2007-11-16 14:57:08 -05003572
Yan Zheng11833d62009-09-11 16:11:19 -04003573 cache = btrfs_lookup_block_group(fs_info, bytenr);
3574 BUG_ON(!cache);
Chris Masonb9473432009-03-13 11:00:37 -04003575
Yan Zheng11833d62009-09-11 16:11:19 -04003576 spin_lock(&cache->space_info->lock);
3577 spin_lock(&cache->lock);
3578 cache->pinned += num_bytes;
3579 cache->space_info->bytes_pinned += num_bytes;
3580 if (reserved) {
3581 cache->reserved -= num_bytes;
3582 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05003583 }
Yan Zheng11833d62009-09-11 16:11:19 -04003584 spin_unlock(&cache->lock);
3585 spin_unlock(&cache->space_info->lock);
3586
3587 btrfs_put_block_group(cache);
3588
3589 set_extent_dirty(fs_info->pinned_extents,
3590 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Yan324ae4d2007-11-16 14:57:08 -05003591 return 0;
3592}
Chris Mason9078a3e2007-04-26 16:46:15 -04003593
Yan Zheng11833d62009-09-11 16:11:19 -04003594static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3595 u64 num_bytes, int reserve)
Zheng Yane8569812008-09-26 10:05:48 -04003596{
Yan Zheng11833d62009-09-11 16:11:19 -04003597 spin_lock(&cache->space_info->lock);
3598 spin_lock(&cache->lock);
3599 if (reserve) {
3600 cache->reserved += num_bytes;
3601 cache->space_info->bytes_reserved += num_bytes;
3602 } else {
3603 cache->reserved -= num_bytes;
3604 cache->space_info->bytes_reserved -= num_bytes;
3605 }
3606 spin_unlock(&cache->lock);
3607 spin_unlock(&cache->space_info->lock);
3608 return 0;
3609}
Zheng Yane8569812008-09-26 10:05:48 -04003610
Yan Zheng11833d62009-09-11 16:11:19 -04003611int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3612 struct btrfs_root *root)
3613{
3614 struct btrfs_fs_info *fs_info = root->fs_info;
3615 struct btrfs_caching_control *next;
3616 struct btrfs_caching_control *caching_ctl;
3617 struct btrfs_block_group_cache *cache;
3618
3619 down_write(&fs_info->extent_commit_sem);
3620
3621 list_for_each_entry_safe(caching_ctl, next,
3622 &fs_info->caching_block_groups, list) {
3623 cache = caching_ctl->block_group;
3624 if (block_group_cache_done(cache)) {
3625 cache->last_byte_to_unpin = (u64)-1;
3626 list_del_init(&caching_ctl->list);
3627 put_caching_control(caching_ctl);
3628 } else {
3629 cache->last_byte_to_unpin = caching_ctl->progress;
3630 }
3631 }
3632
3633 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3634 fs_info->pinned_extents = &fs_info->freed_extents[1];
3635 else
3636 fs_info->pinned_extents = &fs_info->freed_extents[0];
3637
3638 up_write(&fs_info->extent_commit_sem);
3639 return 0;
3640}
3641
3642static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3643{
3644 struct btrfs_fs_info *fs_info = root->fs_info;
3645 struct btrfs_block_group_cache *cache = NULL;
3646 u64 len;
3647
3648 while (start <= end) {
3649 if (!cache ||
3650 start >= cache->key.objectid + cache->key.offset) {
3651 if (cache)
3652 btrfs_put_block_group(cache);
3653 cache = btrfs_lookup_block_group(fs_info, start);
3654 BUG_ON(!cache);
3655 }
3656
3657 len = cache->key.objectid + cache->key.offset - start;
3658 len = min(len, end + 1 - start);
3659
3660 if (start < cache->last_byte_to_unpin) {
3661 len = min(len, cache->last_byte_to_unpin - start);
3662 btrfs_add_free_space(cache, start, len);
3663 }
Josef Bacik25179202008-10-29 14:49:05 -04003664
3665 spin_lock(&cache->space_info->lock);
3666 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003667 cache->pinned -= len;
3668 cache->space_info->bytes_pinned -= len;
Josef Bacik25179202008-10-29 14:49:05 -04003669 spin_unlock(&cache->lock);
3670 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003671
3672 start += len;
3673 }
3674
3675 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003676 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04003677 return 0;
3678}
3679
3680int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04003681 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05003682{
Yan Zheng11833d62009-09-11 16:11:19 -04003683 struct btrfs_fs_info *fs_info = root->fs_info;
3684 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04003685 u64 start;
3686 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003687 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003688
Yan Zheng11833d62009-09-11 16:11:19 -04003689 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3690 unpin = &fs_info->freed_extents[1];
3691 else
3692 unpin = &fs_info->freed_extents[0];
3693
Chris Masond3977122009-01-05 21:25:51 -05003694 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003695 ret = find_first_extent_bit(unpin, 0, &start, &end,
3696 EXTENT_DIRTY);
3697 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003698 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003699
3700 ret = btrfs_discard_extent(root, start, end + 1 - start);
3701
Chris Mason1a5bc162007-10-15 16:15:26 -04003702 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04003703 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04003704 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003705 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003706
Liu Hui1f3c79a2009-01-05 15:57:51 -05003707 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003708}
3709
Zheng Yan31840ae2008-09-23 13:14:14 -04003710static int pin_down_bytes(struct btrfs_trans_handle *trans,
3711 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003712 struct btrfs_path *path,
Yan Zheng11833d62009-09-11 16:11:19 -04003713 u64 bytenr, u64 num_bytes,
3714 int is_data, int reserved,
Chris Masonb9473432009-03-13 11:00:37 -04003715 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003716{
Chris Mason1a5bc162007-10-15 16:15:26 -04003717 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003718 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003719
Zheng Yan31840ae2008-09-23 13:14:14 -04003720 if (is_data)
3721 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003722
Chris Mason444528b2009-10-14 09:38:28 -04003723 /*
3724 * discard is sloooow, and so triggering discards on
3725 * individual btree blocks isn't a good plan. Just
3726 * pin everything in discard mode.
3727 */
3728 if (btrfs_test_opt(root, DISCARD))
3729 goto pinit;
3730
Zheng Yan31840ae2008-09-23 13:14:14 -04003731 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3732 if (!buf)
3733 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003734
Zheng Yan31840ae2008-09-23 13:14:14 -04003735 /* we can reuse a block if it hasn't been written
3736 * and it is from this transaction. We can't
3737 * reuse anything from the tree log root because
3738 * it has tiny sub-transactions.
3739 */
3740 if (btrfs_buffer_uptodate(buf, 0) &&
3741 btrfs_try_tree_lock(buf)) {
3742 u64 header_owner = btrfs_header_owner(buf);
3743 u64 header_transid = btrfs_header_generation(buf);
3744 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3745 header_transid == trans->transid &&
3746 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003747 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003748 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003749 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003750 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003751 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003752 free_extent_buffer(buf);
3753pinit:
Yan Zheng11833d62009-09-11 16:11:19 -04003754 if (path)
3755 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003756 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003757 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
Zheng Yan31840ae2008-09-23 13:14:14 -04003758
Chris Masonbe744172007-05-06 10:15:01 -04003759 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003760 return 0;
3761}
3762
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003763static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3764 struct btrfs_root *root,
3765 u64 bytenr, u64 num_bytes, u64 parent,
3766 u64 root_objectid, u64 owner_objectid,
3767 u64 owner_offset, int refs_to_drop,
3768 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003769{
Chris Masone2fa7222007-03-12 16:22:34 -04003770 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003771 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003772 struct btrfs_fs_info *info = root->fs_info;
3773 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003774 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003775 struct btrfs_extent_item *ei;
3776 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003777 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003778 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003779 int extent_slot = 0;
3780 int found_extent = 0;
3781 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003782 u32 item_size;
3783 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003784
Chris Mason5caf2a02007-04-02 11:20:42 -04003785 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003786 if (!path)
3787 return -ENOMEM;
3788
Chris Mason3c12ac72008-04-21 12:01:38 -04003789 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003790 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003791
3792 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3793 BUG_ON(!is_data && refs_to_drop != 1);
3794
3795 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3796 bytenr, num_bytes, parent,
3797 root_objectid, owner_objectid,
3798 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003799 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003800 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003801 while (extent_slot >= 0) {
3802 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003803 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003804 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003805 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003806 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3807 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003808 found_extent = 1;
3809 break;
3810 }
3811 if (path->slots[0] - extent_slot > 5)
3812 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003813 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003814 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003815#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3816 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3817 if (found_extent && item_size < sizeof(*ei))
3818 found_extent = 0;
3819#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003820 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003821 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003822 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003823 NULL, refs_to_drop,
3824 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003825 BUG_ON(ret);
3826 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003827 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003828
3829 key.objectid = bytenr;
3830 key.type = BTRFS_EXTENT_ITEM_KEY;
3831 key.offset = num_bytes;
3832
Zheng Yan31840ae2008-09-23 13:14:14 -04003833 ret = btrfs_search_slot(trans, extent_root,
3834 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003835 if (ret) {
3836 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003837 ", was looking for %llu\n", ret,
3838 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003839 btrfs_print_leaf(extent_root, path->nodes[0]);
3840 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003841 BUG_ON(ret);
3842 extent_slot = path->slots[0];
3843 }
Chris Mason7bb86312007-12-11 09:25:06 -05003844 } else {
3845 btrfs_print_leaf(extent_root, path->nodes[0]);
3846 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003847 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003848 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003849 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003850 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003851 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003852 (unsigned long long)owner_objectid,
3853 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003854 }
Chris Mason5f39d392007-10-15 16:14:19 -04003855
3856 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003857 item_size = btrfs_item_size_nr(leaf, extent_slot);
3858#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3859 if (item_size < sizeof(*ei)) {
3860 BUG_ON(found_extent || extent_slot != path->slots[0]);
3861 ret = convert_extent_item_v0(trans, extent_root, path,
3862 owner_objectid, 0);
3863 BUG_ON(ret < 0);
3864
3865 btrfs_release_path(extent_root, path);
3866 path->leave_spinning = 1;
3867
3868 key.objectid = bytenr;
3869 key.type = BTRFS_EXTENT_ITEM_KEY;
3870 key.offset = num_bytes;
3871
3872 ret = btrfs_search_slot(trans, extent_root, &key, path,
3873 -1, 1);
3874 if (ret) {
3875 printk(KERN_ERR "umm, got %d back from search"
3876 ", was looking for %llu\n", ret,
3877 (unsigned long long)bytenr);
3878 btrfs_print_leaf(extent_root, path->nodes[0]);
3879 }
3880 BUG_ON(ret);
3881 extent_slot = path->slots[0];
3882 leaf = path->nodes[0];
3883 item_size = btrfs_item_size_nr(leaf, extent_slot);
3884 }
3885#endif
3886 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003887 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003888 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003889 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3890 struct btrfs_tree_block_info *bi;
3891 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3892 bi = (struct btrfs_tree_block_info *)(ei + 1);
3893 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003894 }
3895
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003896 refs = btrfs_extent_refs(leaf, ei);
3897 BUG_ON(refs < refs_to_drop);
3898 refs -= refs_to_drop;
3899
3900 if (refs > 0) {
3901 if (extent_op)
3902 __run_delayed_extent_op(extent_op, leaf, ei);
3903 /*
3904 * In the case of inline back ref, reference count will
3905 * be updated by remove_extent_backref
3906 */
3907 if (iref) {
3908 BUG_ON(!found_extent);
3909 } else {
3910 btrfs_set_extent_refs(leaf, ei, refs);
3911 btrfs_mark_buffer_dirty(leaf);
3912 }
3913 if (found_extent) {
3914 ret = remove_extent_backref(trans, extent_root, path,
3915 iref, refs_to_drop,
3916 is_data);
3917 BUG_ON(ret);
3918 }
3919 } else {
3920 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003921 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003922
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003923 if (found_extent) {
3924 BUG_ON(is_data && refs_to_drop !=
3925 extent_data_ref_count(root, path, iref));
3926 if (iref) {
3927 BUG_ON(path->slots[0] != extent_slot);
3928 } else {
3929 BUG_ON(path->slots[0] != extent_slot + 1);
3930 path->slots[0] = extent_slot;
3931 num_to_del = 2;
3932 }
Chris Mason78fae272007-03-25 11:35:08 -04003933 }
Chris Masonb9473432009-03-13 11:00:37 -04003934
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003935 ret = pin_down_bytes(trans, root, path, bytenr,
Yan Zheng11833d62009-09-11 16:11:19 -04003936 num_bytes, is_data, 0, &must_clean);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003937 if (ret > 0)
3938 mark_free = 1;
3939 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003940 /*
3941 * it is going to be very rare for someone to be waiting
3942 * on the block we're freeing. del_items might need to
3943 * schedule, so rather than get fancy, just force it
3944 * to blocking here
3945 */
3946 if (must_clean)
3947 btrfs_set_lock_blocking(must_clean);
3948
Chris Mason952fcca2008-02-18 16:33:44 -05003949 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3950 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003951 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003952 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003953
Chris Masonb9473432009-03-13 11:00:37 -04003954 if (must_clean) {
3955 clean_tree_block(NULL, root, must_clean);
3956 btrfs_tree_unlock(must_clean);
3957 free_extent_buffer(must_clean);
3958 }
3959
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003960 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003961 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3962 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003963 } else {
3964 invalidate_mapping_pages(info->btree_inode->i_mapping,
3965 bytenr >> PAGE_CACHE_SHIFT,
3966 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003967 }
3968
Chris Masondcbdd4d2008-12-16 13:51:01 -05003969 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3970 mark_free);
3971 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003972 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003973 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003974 return ret;
3975}
3976
3977/*
Chris Mason1887be62009-03-13 10:11:24 -04003978 * when we free an extent, it is possible (and likely) that we free the last
3979 * delayed ref for that extent as well. This searches the delayed ref tree for
3980 * a given extent, and if there are no other delayed refs to be processed, it
3981 * removes it from the tree.
3982 */
3983static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3984 struct btrfs_root *root, u64 bytenr)
3985{
3986 struct btrfs_delayed_ref_head *head;
3987 struct btrfs_delayed_ref_root *delayed_refs;
3988 struct btrfs_delayed_ref_node *ref;
3989 struct rb_node *node;
3990 int ret;
3991
3992 delayed_refs = &trans->transaction->delayed_refs;
3993 spin_lock(&delayed_refs->lock);
3994 head = btrfs_find_delayed_ref_head(trans, bytenr);
3995 if (!head)
3996 goto out;
3997
3998 node = rb_prev(&head->node.rb_node);
3999 if (!node)
4000 goto out;
4001
4002 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4003
4004 /* there are still entries for this ref, we can't drop it */
4005 if (ref->bytenr == bytenr)
4006 goto out;
4007
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004008 if (head->extent_op) {
4009 if (!head->must_insert_reserved)
4010 goto out;
4011 kfree(head->extent_op);
4012 head->extent_op = NULL;
4013 }
4014
Chris Mason1887be62009-03-13 10:11:24 -04004015 /*
4016 * waiting for the lock here would deadlock. If someone else has it
4017 * locked they are already in the process of dropping it anyway
4018 */
4019 if (!mutex_trylock(&head->mutex))
4020 goto out;
4021
4022 /*
4023 * at this point we have a head with no other entries. Go
4024 * ahead and process it.
4025 */
4026 head->node.in_tree = 0;
4027 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004028
Chris Mason1887be62009-03-13 10:11:24 -04004029 delayed_refs->num_entries--;
4030
4031 /*
4032 * we don't take a ref on the node because we're removing it from the
4033 * tree, so we just steal the ref the tree was holding.
4034 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004035 delayed_refs->num_heads--;
4036 if (list_empty(&head->cluster))
4037 delayed_refs->num_heads_ready--;
4038
4039 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004040 spin_unlock(&delayed_refs->lock);
4041
4042 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004043 &head->node, head->extent_op,
4044 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04004045 BUG_ON(ret);
4046 btrfs_put_delayed_ref(&head->node);
4047 return 0;
4048out:
4049 spin_unlock(&delayed_refs->lock);
4050 return 0;
4051}
4052
Chris Mason925baed2008-06-25 16:01:30 -04004053int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004054 struct btrfs_root *root,
4055 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004056 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004057{
4058 int ret;
4059
Chris Mason56bec292009-03-13 10:10:06 -04004060 /*
4061 * tree log blocks never actually go into the extent allocation
4062 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004063 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004064 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4065 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004066 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004067 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004068 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004069 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4070 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4071 parent, root_objectid, (int)owner,
4072 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004073 BUG_ON(ret);
4074 ret = check_ref_cleanup(trans, root, bytenr);
4075 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004076 } else {
4077 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4078 parent, root_objectid, owner,
4079 offset, BTRFS_DROP_DELAYED_REF, NULL);
4080 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004081 }
Chris Mason925baed2008-06-25 16:01:30 -04004082 return ret;
4083}
4084
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00004085int btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4086 struct btrfs_root *root,
4087 u64 bytenr, u32 blocksize,
4088 u64 parent, u64 root_objectid, int level)
4089{
4090 u64 used;
4091 spin_lock(&root->node_lock);
4092 used = btrfs_root_used(&root->root_item) - blocksize;
4093 btrfs_set_root_used(&root->root_item, used);
4094 spin_unlock(&root->node_lock);
4095
4096 return btrfs_free_extent(trans, root, bytenr, blocksize,
4097 parent, root_objectid, level, 0);
4098}
4099
Chris Mason87ee04e2007-11-30 11:30:34 -05004100static u64 stripe_align(struct btrfs_root *root, u64 val)
4101{
4102 u64 mask = ((u64)root->stripesize - 1);
4103 u64 ret = (val + mask) & ~mask;
4104 return ret;
4105}
4106
Chris Masonfec577f2007-02-26 10:40:21 -05004107/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004108 * when we wait for progress in the block group caching, its because
4109 * our allocation attempt failed at least once. So, we must sleep
4110 * and let some progress happen before we try again.
4111 *
4112 * This function will sleep at least once waiting for new free space to
4113 * show up, and then it will check the block group free space numbers
4114 * for our min num_bytes. Another option is to have it go ahead
4115 * and look in the rbtree for a free extent of a given size, but this
4116 * is a good start.
4117 */
4118static noinline int
4119wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4120 u64 num_bytes)
4121{
Yan Zheng11833d62009-09-11 16:11:19 -04004122 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004123 DEFINE_WAIT(wait);
4124
Yan Zheng11833d62009-09-11 16:11:19 -04004125 caching_ctl = get_caching_control(cache);
4126 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004127 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004128
Yan Zheng11833d62009-09-11 16:11:19 -04004129 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004130 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004131
4132 put_caching_control(caching_ctl);
4133 return 0;
4134}
4135
4136static noinline int
4137wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4138{
4139 struct btrfs_caching_control *caching_ctl;
4140 DEFINE_WAIT(wait);
4141
4142 caching_ctl = get_caching_control(cache);
4143 if (!caching_ctl)
4144 return 0;
4145
4146 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4147
4148 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004149 return 0;
4150}
4151
Yan, Zhengb742bb82010-05-16 10:46:24 -04004152static int get_block_group_index(struct btrfs_block_group_cache *cache)
4153{
4154 int index;
4155 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4156 index = 0;
4157 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4158 index = 1;
4159 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4160 index = 2;
4161 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4162 index = 3;
4163 else
4164 index = 4;
4165 return index;
4166}
4167
Josef Bacik817d52f2009-07-13 21:29:25 -04004168enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004169 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004170 LOOP_CACHING_NOWAIT = 1,
4171 LOOP_CACHING_WAIT = 2,
4172 LOOP_ALLOC_CHUNK = 3,
4173 LOOP_NO_EMPTY_SIZE = 4,
4174};
4175
4176/*
Chris Masonfec577f2007-02-26 10:40:21 -05004177 * walks the btree of allocated extents and find a hole of a given size.
4178 * The key ins is changed to record the hole:
4179 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004180 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004181 * ins->offset == number of blocks
4182 * Any available blocks before search_start are skipped.
4183 */
Chris Masond3977122009-01-05 21:25:51 -05004184static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004185 struct btrfs_root *orig_root,
4186 u64 num_bytes, u64 empty_size,
4187 u64 search_start, u64 search_end,
4188 u64 hint_byte, struct btrfs_key *ins,
4189 u64 exclude_start, u64 exclude_nr,
4190 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004191{
Josef Bacik80eb2342008-10-29 14:49:05 -04004192 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004193 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004194 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004195 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004196 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004197 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004198 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004199 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004200 int last_ptr_loop = 0;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004201 int index = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004202 int loop = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004203 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004204 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004205 bool failed_alloc = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004206 u64 ideal_cache_percent = 0;
4207 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004208
Chris Masondb945352007-10-15 16:15:53 -04004209 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004210 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004211 ins->objectid = 0;
4212 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004213
Josef Bacik2552d172009-04-03 10:14:19 -04004214 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004215 if (!space_info) {
4216 printk(KERN_ERR "No space info for %d\n", data);
4217 return -ENOSPC;
4218 }
Josef Bacik2552d172009-04-03 10:14:19 -04004219
Chris Mason0ef3e662008-05-24 14:04:53 -04004220 if (orig_root->ref_cows || empty_size)
4221 allowed_chunk_alloc = 1;
4222
Chris Mason239b14b2008-03-24 15:02:07 -04004223 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004224 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004225 if (!btrfs_test_opt(root, SSD))
4226 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004227 }
4228
Chris Masonfa9c0d72009-04-03 09:47:43 -04004229 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4230 last_ptr = &root->fs_info->data_alloc_cluster;
4231 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004232
Chris Mason239b14b2008-03-24 15:02:07 -04004233 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004234 spin_lock(&last_ptr->lock);
4235 if (last_ptr->block_group)
4236 hint_byte = last_ptr->window_start;
4237 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004238 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004239
Chris Masona061fc82008-05-07 11:43:44 -04004240 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004241 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004242
Josef Bacik817d52f2009-07-13 21:29:25 -04004243 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004244 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004245
Josef Bacik2552d172009-04-03 10:14:19 -04004246 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004247ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004248 block_group = btrfs_lookup_block_group(root->fs_info,
4249 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004250 /*
4251 * we don't want to use the block group if it doesn't match our
4252 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004253 *
4254 * However if we are re-searching with an ideal block group
4255 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004256 */
4257 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004258 (block_group->cached != BTRFS_CACHE_NO ||
4259 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004260 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004261 if (list_empty(&block_group->list) ||
4262 block_group->ro) {
4263 /*
4264 * someone is removing this block group,
4265 * we can't jump into the have_block_group
4266 * target because our list pointers are not
4267 * valid
4268 */
4269 btrfs_put_block_group(block_group);
4270 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004271 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004272 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004273 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004274 }
Josef Bacik2552d172009-04-03 10:14:19 -04004275 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004276 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004277 }
Chris Mason42e70e72008-11-07 18:17:11 -05004278 }
Josef Bacik2552d172009-04-03 10:14:19 -04004279search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004280 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004281 list_for_each_entry(block_group, &space_info->block_groups[index],
4282 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004283 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004284 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004285
Josef Bacik11dfe352009-11-13 20:12:59 +00004286 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004287 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004288
Josef Bacik2552d172009-04-03 10:14:19 -04004289have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004290 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004291 u64 free_percent;
4292
4293 free_percent = btrfs_block_group_used(&block_group->item);
4294 free_percent *= 100;
4295 free_percent = div64_u64(free_percent,
4296 block_group->key.offset);
4297 free_percent = 100 - free_percent;
4298 if (free_percent > ideal_cache_percent &&
4299 likely(!block_group->ro)) {
4300 ideal_cache_offset = block_group->key.objectid;
4301 ideal_cache_percent = free_percent;
4302 }
4303
Josef Bacik817d52f2009-07-13 21:29:25 -04004304 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05004305 * We only want to start kthread caching if we are at
4306 * the point where we will wait for caching to make
4307 * progress, or if our ideal search is over and we've
4308 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04004309 */
4310 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05004311 (loop > LOOP_FIND_IDEAL &&
4312 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004313 ret = cache_block_group(block_group);
4314 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004315 }
Josef Bacikccf0e722009-11-10 21:23:48 -05004316 found_uncached_bg = true;
4317
4318 /*
4319 * If loop is set for cached only, try the next block
4320 * group.
4321 */
4322 if (loop == LOOP_FIND_IDEAL)
4323 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05004324 }
4325
Josef Bacik817d52f2009-07-13 21:29:25 -04004326 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05004327 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04004328 found_uncached_bg = true;
4329
Josef Bacikea6a4782008-11-20 12:16:16 -05004330 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004331 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004332
Josef Bacik0a243252009-09-11 16:11:20 -04004333 /*
4334 * Ok we want to try and use the cluster allocator, so lets look
4335 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4336 * have tried the cluster allocator plenty of times at this
4337 * point and not have found anything, so we are likely way too
4338 * fragmented for the clustering stuff to find anything, so lets
4339 * just skip it and let the allocator find whatever block it can
4340 * find
4341 */
4342 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004343 /*
4344 * the refill lock keeps out other
4345 * people trying to start a new cluster
4346 */
4347 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004348 if (last_ptr->block_group &&
4349 (last_ptr->block_group->ro ||
4350 !block_group_bits(last_ptr->block_group, data))) {
4351 offset = 0;
4352 goto refill_cluster;
4353 }
4354
Chris Masonfa9c0d72009-04-03 09:47:43 -04004355 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4356 num_bytes, search_start);
4357 if (offset) {
4358 /* we have a block, we're done */
4359 spin_unlock(&last_ptr->refill_lock);
4360 goto checks;
4361 }
4362
4363 spin_lock(&last_ptr->lock);
4364 /*
4365 * whoops, this cluster doesn't actually point to
4366 * this block group. Get a ref on the block
4367 * group is does point to and try again
4368 */
4369 if (!last_ptr_loop && last_ptr->block_group &&
4370 last_ptr->block_group != block_group) {
4371
4372 btrfs_put_block_group(block_group);
4373 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00004374 btrfs_get_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004375 spin_unlock(&last_ptr->lock);
4376 spin_unlock(&last_ptr->refill_lock);
4377
4378 last_ptr_loop = 1;
4379 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04004380 /*
4381 * we know this block group is properly
4382 * in the list because
4383 * btrfs_remove_block_group, drops the
4384 * cluster before it removes the block
4385 * group from the list
4386 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04004387 goto have_block_group;
4388 }
4389 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004390refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04004391 /*
4392 * this cluster didn't work out, free it and
4393 * start over
4394 */
4395 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4396
4397 last_ptr_loop = 0;
4398
4399 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04004400 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04004401 block_group, last_ptr,
4402 offset, num_bytes,
4403 empty_cluster + empty_size);
4404 if (ret == 0) {
4405 /*
4406 * now pull our allocation out of this
4407 * cluster
4408 */
4409 offset = btrfs_alloc_from_cluster(block_group,
4410 last_ptr, num_bytes,
4411 search_start);
4412 if (offset) {
4413 /* we found one, proceed */
4414 spin_unlock(&last_ptr->refill_lock);
4415 goto checks;
4416 }
Josef Bacik0a243252009-09-11 16:11:20 -04004417 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4418 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004419 spin_unlock(&last_ptr->refill_lock);
4420
Josef Bacik0a243252009-09-11 16:11:20 -04004421 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004422 wait_block_group_cache_progress(block_group,
4423 num_bytes + empty_cluster + empty_size);
4424 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004425 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004426
Chris Masonfa9c0d72009-04-03 09:47:43 -04004427 /*
4428 * at this point we either didn't find a cluster
4429 * or we weren't able to allocate a block from our
4430 * cluster. Free the cluster we've been trying
4431 * to use, and go to the next block group
4432 */
Josef Bacik0a243252009-09-11 16:11:20 -04004433 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004434 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04004435 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004436 }
4437
Josef Bacik6226cb02009-04-03 10:14:18 -04004438 offset = btrfs_find_space_for_alloc(block_group, search_start,
4439 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004440 /*
4441 * If we didn't find a chunk, and we haven't failed on this
4442 * block group before, and this block group is in the middle of
4443 * caching and we are ok with waiting, then go ahead and wait
4444 * for progress to be made, and set failed_alloc to true.
4445 *
4446 * If failed_alloc is true then we've already waited on this
4447 * block group once and should move on to the next block group.
4448 */
4449 if (!offset && !failed_alloc && !cached &&
4450 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004451 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004452 num_bytes + empty_size);
4453 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004454 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004455 } else if (!offset) {
4456 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04004457 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004458checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04004459 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04004460 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04004461 if (search_start + num_bytes >= search_end) {
4462 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004463 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004464 }
Chris Masone37c9e62007-05-09 20:13:14 -04004465
Josef Bacik2552d172009-04-03 10:14:19 -04004466 /* move on to the next group */
4467 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04004468 block_group->key.objectid + block_group->key.offset) {
4469 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004470 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004471 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004472
Josef Bacik2552d172009-04-03 10:14:19 -04004473 if (exclude_nr > 0 &&
4474 (search_start + num_bytes > exclude_start &&
4475 search_start < exclude_start + exclude_nr)) {
4476 search_start = exclude_start + exclude_nr;
4477
Josef Bacik6226cb02009-04-03 10:14:18 -04004478 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004479 /*
4480 * if search_start is still in this block group
4481 * then we just re-search this block group
4482 */
4483 if (search_start >= block_group->key.objectid &&
4484 search_start < (block_group->key.objectid +
Josef Bacik6226cb02009-04-03 10:14:18 -04004485 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04004486 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04004487 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004488 }
Josef Bacik2552d172009-04-03 10:14:19 -04004489
4490 ins->objectid = search_start;
4491 ins->offset = num_bytes;
4492
Josef Bacik6226cb02009-04-03 10:14:18 -04004493 if (offset < search_start)
4494 btrfs_add_free_space(block_group, offset,
4495 search_start - offset);
4496 BUG_ON(offset > search_start);
4497
Yan Zheng11833d62009-09-11 16:11:19 -04004498 update_reserved_extents(block_group, num_bytes, 1);
4499
Josef Bacik2552d172009-04-03 10:14:19 -04004500 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04004501 break;
4502loop:
Josef Bacik0a243252009-09-11 16:11:20 -04004503 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004504 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004505 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d72009-04-03 09:47:43 -04004506 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004507 }
4508 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004509
Yan, Zhengb742bb82010-05-16 10:46:24 -04004510 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
4511 goto search;
4512
Josef Bacikccf0e722009-11-10 21:23:48 -05004513 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
4514 * for them to make caching progress. Also
4515 * determine the best possible bg to cache
4516 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
4517 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04004518 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4519 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4520 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4521 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04004522 */
Josef Bacik817d52f2009-07-13 21:29:25 -04004523 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4524 (found_uncached_bg || empty_size || empty_cluster ||
4525 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004526 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004527 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004528 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004529 loop++;
4530 if (!ideal_cache_percent &&
4531 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04004532 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05004533
4534 /*
4535 * 1 of the following 2 things have happened so far
4536 *
4537 * 1) We found an ideal block group for caching that
4538 * is mostly full and will cache quickly, so we might
4539 * as well wait for it.
4540 *
4541 * 2) We searched for cached only and we didn't find
4542 * anything, and we didn't start any caching kthreads
4543 * either, so chances are we will loop through and
4544 * start a couple caching kthreads, and then come back
4545 * around and just wait for them. This will be slower
4546 * because we will have 2 caching kthreads reading at
4547 * the same time when we could have just started one
4548 * and waited for it to get far enough to give us an
4549 * allocation, so go ahead and go to the wait caching
4550 * loop.
4551 */
4552 loop = LOOP_CACHING_WAIT;
4553 search_start = ideal_cache_offset;
4554 ideal_cache_percent = 0;
4555 goto ideal_cache;
4556 } else if (loop == LOOP_FIND_IDEAL) {
4557 /*
4558 * Didn't find a uncached bg, wait on anything we find
4559 * next.
4560 */
4561 loop = LOOP_CACHING_WAIT;
4562 goto search;
4563 }
4564
4565 if (loop < LOOP_CACHING_WAIT) {
4566 loop++;
4567 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04004568 }
4569
4570 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004571 empty_size = 0;
4572 empty_cluster = 0;
4573 }
Chris Mason42e70e72008-11-07 18:17:11 -05004574
Josef Bacik2552d172009-04-03 10:14:19 -04004575 if (allowed_chunk_alloc) {
4576 ret = do_chunk_alloc(trans, root, num_bytes +
4577 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04004578 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004579 done_chunk_alloc = 1;
4580 } else if (!done_chunk_alloc) {
Josef Bacik2552d172009-04-03 10:14:19 -04004581 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004582 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004583
Josef Bacik817d52f2009-07-13 21:29:25 -04004584 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004585 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04004586 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004587 }
Josef Bacik2552d172009-04-03 10:14:19 -04004588 ret = -ENOSPC;
4589 } else if (!ins->objectid) {
4590 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04004591 }
Chris Mason0b86a832008-03-24 15:01:56 -04004592
Josef Bacik80eb2342008-10-29 14:49:05 -04004593 /* we found what we needed */
4594 if (ins->objectid) {
4595 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05004596 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04004597
Chris Masonfa9c0d72009-04-03 09:47:43 -04004598 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004599 ret = 0;
4600 }
Chris Mason0b86a832008-03-24 15:01:56 -04004601
Chris Mason0f70abe2007-02-28 16:46:22 -05004602 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004603}
Chris Masonec44a352008-04-28 15:29:52 -04004604
Josef Bacik9ed74f22009-09-11 16:12:44 -04004605static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4606 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04004607{
4608 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004609 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004610
Josef Bacik9ed74f22009-09-11 16:12:44 -04004611 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05004612 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4613 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04004614 info->bytes_pinned - info->bytes_reserved -
4615 info->bytes_super),
Chris Masond3977122009-01-05 21:25:51 -05004616 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05004617 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Josef Bacik9ed74f22009-09-11 16:12:44 -04004618 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4619 "\n",
Joel Becker21380932009-04-21 12:38:29 -07004620 (unsigned long long)info->total_bytes,
4621 (unsigned long long)info->bytes_pinned,
4622 (unsigned long long)info->bytes_delalloc,
4623 (unsigned long long)info->bytes_may_use,
Josef Bacik9ed74f22009-09-11 16:12:44 -04004624 (unsigned long long)info->bytes_used,
4625 (unsigned long long)info->bytes_root,
4626 (unsigned long long)info->bytes_super,
4627 (unsigned long long)info->bytes_reserved);
4628 spin_unlock(&info->lock);
4629
4630 if (!dump_block_groups)
4631 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004632
Josef Bacik80eb2342008-10-29 14:49:05 -04004633 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004634again:
4635 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004636 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05004637 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4638 "%llu pinned %llu reserved\n",
4639 (unsigned long long)cache->key.objectid,
4640 (unsigned long long)cache->key.offset,
4641 (unsigned long long)btrfs_block_group_used(&cache->item),
4642 (unsigned long long)cache->pinned,
4643 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004644 btrfs_dump_free_space(cache, bytes);
4645 spin_unlock(&cache->lock);
4646 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04004647 if (++index < BTRFS_NR_RAID_TYPES)
4648 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04004649 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004650}
Zheng Yane8569812008-09-26 10:05:48 -04004651
Yan Zheng11833d62009-09-11 16:11:19 -04004652int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4653 struct btrfs_root *root,
4654 u64 num_bytes, u64 min_alloc_size,
4655 u64 empty_size, u64 hint_byte,
4656 u64 search_end, struct btrfs_key *ins,
4657 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004658{
4659 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04004660 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004661
Josef Bacik6a632092009-02-20 11:00:09 -05004662 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04004663again:
Chris Mason0ef3e662008-05-24 14:04:53 -04004664 /*
4665 * the only place that sets empty_size is btrfs_realloc_node, which
4666 * is not called recursively on allocations
4667 */
Josef Bacik83d3c962009-12-07 21:45:59 +00004668 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04004669 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004670 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04004671
Chris Masondb945352007-10-15 16:15:53 -04004672 WARN_ON(num_bytes < root->sectorsize);
4673 ret = find_free_extent(trans, root, num_bytes, empty_size,
4674 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04004675 trans->alloc_exclude_start,
4676 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04004677
Chris Mason98d20f62008-04-14 09:46:10 -04004678 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4679 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004680 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004681 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04004682 do_chunk_alloc(trans, root->fs_info->extent_root,
4683 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004684 goto again;
4685 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004686 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004687 struct btrfs_space_info *sinfo;
4688
4689 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004690 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4691 "wanted %llu\n", (unsigned long long)data,
4692 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004693 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04004694 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004695
4696 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004697}
4698
Chris Mason65b51a02008-08-01 15:11:20 -04004699int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4700{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004701 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004702 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004703
Josef Bacik0f9dd462008-09-23 13:14:11 -04004704 cache = btrfs_lookup_block_group(root->fs_info, start);
4705 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004706 printk(KERN_ERR "Unable to find block group for %llu\n",
4707 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004708 return -ENOSPC;
4709 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004710
4711 ret = btrfs_discard_extent(root, start, len);
4712
Josef Bacik0f9dd462008-09-23 13:14:11 -04004713 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04004714 update_reserved_extents(cache, len, 0);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004715 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04004716
Chris Masone6dcd2d2008-07-17 12:53:50 -04004717 return ret;
4718}
4719
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004720static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4721 struct btrfs_root *root,
4722 u64 parent, u64 root_objectid,
4723 u64 flags, u64 owner, u64 offset,
4724 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004725{
4726 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004727 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004728 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004729 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004730 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004731 struct extent_buffer *leaf;
4732 int type;
4733 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004734
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004735 if (parent > 0)
4736 type = BTRFS_SHARED_DATA_REF_KEY;
4737 else
4738 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004739
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004740 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004741
4742 path = btrfs_alloc_path();
4743 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004744
Chris Masonb9473432009-03-13 11:00:37 -04004745 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004746 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4747 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004748 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004749
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004750 leaf = path->nodes[0];
4751 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004752 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004753 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4754 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4755 btrfs_set_extent_flags(leaf, extent_item,
4756 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004757
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004758 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4759 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4760 if (parent > 0) {
4761 struct btrfs_shared_data_ref *ref;
4762 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4763 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4764 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4765 } else {
4766 struct btrfs_extent_data_ref *ref;
4767 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4768 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4769 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4770 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4771 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4772 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004773
4774 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004775 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004776
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004777 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4778 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05004779 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004780 printk(KERN_ERR "btrfs update block group failed for %llu "
4781 "%llu\n", (unsigned long long)ins->objectid,
4782 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004783 BUG();
4784 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004785 return ret;
4786}
4787
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004788static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4789 struct btrfs_root *root,
4790 u64 parent, u64 root_objectid,
4791 u64 flags, struct btrfs_disk_key *key,
4792 int level, struct btrfs_key *ins)
4793{
4794 int ret;
4795 struct btrfs_fs_info *fs_info = root->fs_info;
4796 struct btrfs_extent_item *extent_item;
4797 struct btrfs_tree_block_info *block_info;
4798 struct btrfs_extent_inline_ref *iref;
4799 struct btrfs_path *path;
4800 struct extent_buffer *leaf;
4801 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4802
4803 path = btrfs_alloc_path();
4804 BUG_ON(!path);
4805
4806 path->leave_spinning = 1;
4807 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4808 ins, size);
4809 BUG_ON(ret);
4810
4811 leaf = path->nodes[0];
4812 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4813 struct btrfs_extent_item);
4814 btrfs_set_extent_refs(leaf, extent_item, 1);
4815 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4816 btrfs_set_extent_flags(leaf, extent_item,
4817 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4818 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4819
4820 btrfs_set_tree_block_key(leaf, block_info, key);
4821 btrfs_set_tree_block_level(leaf, block_info, level);
4822
4823 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4824 if (parent > 0) {
4825 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4826 btrfs_set_extent_inline_ref_type(leaf, iref,
4827 BTRFS_SHARED_BLOCK_REF_KEY);
4828 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4829 } else {
4830 btrfs_set_extent_inline_ref_type(leaf, iref,
4831 BTRFS_TREE_BLOCK_REF_KEY);
4832 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4833 }
4834
4835 btrfs_mark_buffer_dirty(leaf);
4836 btrfs_free_path(path);
4837
4838 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4839 1, 0);
4840 if (ret) {
4841 printk(KERN_ERR "btrfs update block group failed for %llu "
4842 "%llu\n", (unsigned long long)ins->objectid,
4843 (unsigned long long)ins->offset);
4844 BUG();
4845 }
4846 return ret;
4847}
4848
4849int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4850 struct btrfs_root *root,
4851 u64 root_objectid, u64 owner,
4852 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004853{
4854 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04004855
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004856 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004857
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004858 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4859 0, root_objectid, owner, offset,
4860 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004861 return ret;
4862}
Chris Masone02119d2008-09-05 16:13:11 -04004863
4864/*
4865 * this is used by the tree logging recovery code. It records that
4866 * an extent has been allocated and makes sure to clear the free
4867 * space cache bits as well
4868 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004869int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4870 struct btrfs_root *root,
4871 u64 root_objectid, u64 owner, u64 offset,
4872 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004873{
4874 int ret;
4875 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04004876 struct btrfs_caching_control *caching_ctl;
4877 u64 start = ins->objectid;
4878 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04004879
Chris Masone02119d2008-09-05 16:13:11 -04004880 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04004881 cache_block_group(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004882 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004883
Yan Zheng11833d62009-09-11 16:11:19 -04004884 if (!caching_ctl) {
4885 BUG_ON(!block_group_cache_done(block_group));
4886 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4887 BUG_ON(ret);
4888 } else {
4889 mutex_lock(&caching_ctl->mutex);
4890
4891 if (start >= caching_ctl->progress) {
4892 ret = add_excluded_extent(root, start, num_bytes);
4893 BUG_ON(ret);
4894 } else if (start + num_bytes <= caching_ctl->progress) {
4895 ret = btrfs_remove_free_space(block_group,
4896 start, num_bytes);
4897 BUG_ON(ret);
4898 } else {
4899 num_bytes = caching_ctl->progress - start;
4900 ret = btrfs_remove_free_space(block_group,
4901 start, num_bytes);
4902 BUG_ON(ret);
4903
4904 start = caching_ctl->progress;
4905 num_bytes = ins->objectid + ins->offset -
4906 caching_ctl->progress;
4907 ret = add_excluded_extent(root, start, num_bytes);
4908 BUG_ON(ret);
4909 }
4910
4911 mutex_unlock(&caching_ctl->mutex);
4912 put_caching_control(caching_ctl);
4913 }
4914
4915 update_reserved_extents(block_group, ins->offset, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004916 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004917 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4918 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004919 return ret;
4920}
4921
Chris Masone6dcd2d2008-07-17 12:53:50 -04004922/*
4923 * finds a free extent and does all the dirty work required for allocation
4924 * returns the key for the extent through ins, and a tree buffer for
4925 * the first block of the extent through buf.
4926 *
4927 * returns 0 if everything worked, non-zero otherwise.
4928 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004929static int alloc_tree_block(struct btrfs_trans_handle *trans,
4930 struct btrfs_root *root,
4931 u64 num_bytes, u64 parent, u64 root_objectid,
4932 struct btrfs_disk_key *key, int level,
4933 u64 empty_size, u64 hint_byte, u64 search_end,
4934 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004935{
4936 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004937 u64 flags = 0;
4938
Yan Zheng11833d62009-09-11 16:11:19 -04004939 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4940 empty_size, hint_byte, search_end,
4941 ins, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004942 if (ret)
4943 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004944
4945 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4946 if (parent == 0)
4947 parent = ins->objectid;
4948 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4949 } else
4950 BUG_ON(parent > 0);
4951
Chris Masond00aff02008-09-11 15:54:42 -04004952 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004953 struct btrfs_delayed_extent_op *extent_op;
4954 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4955 BUG_ON(!extent_op);
4956 if (key)
4957 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4958 else
4959 memset(&extent_op->key, 0, sizeof(extent_op->key));
4960 extent_op->flags_to_set = flags;
4961 extent_op->update_key = 1;
4962 extent_op->update_flags = 1;
4963 extent_op->is_data = 0;
4964
4965 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4966 ins->offset, parent, root_objectid,
4967 level, BTRFS_ADD_DELAYED_EXTENT,
4968 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004969 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004970 }
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00004971
4972 if (root_objectid == root->root_key.objectid) {
4973 u64 used;
4974 spin_lock(&root->node_lock);
4975 used = btrfs_root_used(&root->root_item) + num_bytes;
4976 btrfs_set_root_used(&root->root_item, used);
4977 spin_unlock(&root->node_lock);
4978 }
Chris Mason925baed2008-06-25 16:01:30 -04004979 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004980}
Chris Mason65b51a02008-08-01 15:11:20 -04004981
4982struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4983 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004984 u64 bytenr, u32 blocksize,
4985 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004986{
4987 struct extent_buffer *buf;
4988
4989 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4990 if (!buf)
4991 return ERR_PTR(-ENOMEM);
4992 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004993 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004994 btrfs_tree_lock(buf);
4995 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004996
4997 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004998 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004999
Chris Masond0c803c2008-09-11 16:17:57 -04005000 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005001 /*
5002 * we allow two log transactions at a time, use different
5003 * EXENT bit to differentiate dirty pages.
5004 */
5005 if (root->log_transid % 2 == 0)
5006 set_extent_dirty(&root->dirty_log_pages, buf->start,
5007 buf->start + buf->len - 1, GFP_NOFS);
5008 else
5009 set_extent_new(&root->dirty_log_pages, buf->start,
5010 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005011 } else {
5012 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5013 buf->start + buf->len - 1, GFP_NOFS);
5014 }
Chris Mason65b51a02008-08-01 15:11:20 -04005015 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005016 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005017 return buf;
5018}
5019
Chris Masonfec577f2007-02-26 10:40:21 -05005020/*
5021 * helper function to allocate a block for a given tree
5022 * returns the tree buffer or NULL.
5023 */
Chris Mason5f39d392007-10-15 16:14:19 -04005024struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005025 struct btrfs_root *root, u32 blocksize,
5026 u64 parent, u64 root_objectid,
5027 struct btrfs_disk_key *key, int level,
5028 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005029{
Chris Masone2fa7222007-03-12 16:22:34 -04005030 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05005031 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005032 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05005033
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005034 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
5035 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05005036 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04005037 BUG_ON(ret > 0);
5038 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005039 }
Chris Mason55c69072008-01-09 15:55:33 -05005040
Chris Mason4008c042009-02-12 14:09:45 -05005041 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5042 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05005043 return buf;
5044}
Chris Masona28ec192007-03-06 20:08:01 -05005045
Yan Zheng2c47e6052009-06-27 21:07:35 -04005046struct walk_control {
5047 u64 refs[BTRFS_MAX_LEVEL];
5048 u64 flags[BTRFS_MAX_LEVEL];
5049 struct btrfs_key update_progress;
5050 int stage;
5051 int level;
5052 int shared_level;
5053 int update_ref;
5054 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005055 int reada_slot;
5056 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005057};
5058
5059#define DROP_REFERENCE 1
5060#define UPDATE_BACKREF 2
5061
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005062static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5063 struct btrfs_root *root,
5064 struct walk_control *wc,
5065 struct btrfs_path *path)
5066{
5067 u64 bytenr;
5068 u64 generation;
5069 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005070 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005071 u64 last = 0;
5072 u32 nritems;
5073 u32 blocksize;
5074 struct btrfs_key key;
5075 struct extent_buffer *eb;
5076 int ret;
5077 int slot;
5078 int nread = 0;
5079
5080 if (path->slots[wc->level] < wc->reada_slot) {
5081 wc->reada_count = wc->reada_count * 2 / 3;
5082 wc->reada_count = max(wc->reada_count, 2);
5083 } else {
5084 wc->reada_count = wc->reada_count * 3 / 2;
5085 wc->reada_count = min_t(int, wc->reada_count,
5086 BTRFS_NODEPTRS_PER_BLOCK(root));
5087 }
5088
5089 eb = path->nodes[wc->level];
5090 nritems = btrfs_header_nritems(eb);
5091 blocksize = btrfs_level_size(root, wc->level - 1);
5092
5093 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5094 if (nread >= wc->reada_count)
5095 break;
5096
5097 cond_resched();
5098 bytenr = btrfs_node_blockptr(eb, slot);
5099 generation = btrfs_node_ptr_generation(eb, slot);
5100
5101 if (slot == path->slots[wc->level])
5102 goto reada;
5103
5104 if (wc->stage == UPDATE_BACKREF &&
5105 generation <= root->root_key.offset)
5106 continue;
5107
Yan, Zheng94fcca92009-10-09 09:25:16 -04005108 /* We don't lock the tree block, it's OK to be racy here */
5109 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5110 &refs, &flags);
5111 BUG_ON(ret);
5112 BUG_ON(refs == 0);
5113
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005114 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005115 if (refs == 1)
5116 goto reada;
5117
Yan, Zheng94fcca92009-10-09 09:25:16 -04005118 if (wc->level == 1 &&
5119 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5120 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005121 if (!wc->update_ref ||
5122 generation <= root->root_key.offset)
5123 continue;
5124 btrfs_node_key_to_cpu(eb, &key, slot);
5125 ret = btrfs_comp_cpu_keys(&key,
5126 &wc->update_progress);
5127 if (ret < 0)
5128 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005129 } else {
5130 if (wc->level == 1 &&
5131 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5132 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005133 }
5134reada:
5135 ret = readahead_tree_block(root, bytenr, blocksize,
5136 generation);
5137 if (ret)
5138 break;
5139 last = bytenr + blocksize;
5140 nread++;
5141 }
5142 wc->reada_slot = slot;
5143}
5144
Chris Mason9aca1d52007-03-13 11:09:37 -04005145/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005146 * hepler to process tree block while walking down the tree.
5147 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005148 * when wc->stage == UPDATE_BACKREF, this function updates
5149 * back refs for pointers in the block.
5150 *
5151 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005152 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005153static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5154 struct btrfs_root *root,
5155 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005156 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005157{
5158 int level = wc->level;
5159 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005160 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5161 int ret;
5162
5163 if (wc->stage == UPDATE_BACKREF &&
5164 btrfs_header_owner(eb) != root->root_key.objectid)
5165 return 1;
5166
5167 /*
5168 * when reference count of tree block is 1, it won't increase
5169 * again. once full backref flag is set, we never clear it.
5170 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005171 if (lookup_info &&
5172 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5173 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005174 BUG_ON(!path->locks[level]);
5175 ret = btrfs_lookup_extent_info(trans, root,
5176 eb->start, eb->len,
5177 &wc->refs[level],
5178 &wc->flags[level]);
5179 BUG_ON(ret);
5180 BUG_ON(wc->refs[level] == 0);
5181 }
5182
Yan Zheng2c47e6052009-06-27 21:07:35 -04005183 if (wc->stage == DROP_REFERENCE) {
5184 if (wc->refs[level] > 1)
5185 return 1;
5186
5187 if (path->locks[level] && !wc->keep_locks) {
5188 btrfs_tree_unlock(eb);
5189 path->locks[level] = 0;
5190 }
5191 return 0;
5192 }
5193
5194 /* wc->stage == UPDATE_BACKREF */
5195 if (!(wc->flags[level] & flag)) {
5196 BUG_ON(!path->locks[level]);
5197 ret = btrfs_inc_ref(trans, root, eb, 1);
5198 BUG_ON(ret);
5199 ret = btrfs_dec_ref(trans, root, eb, 0);
5200 BUG_ON(ret);
5201 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5202 eb->len, flag, 0);
5203 BUG_ON(ret);
5204 wc->flags[level] |= flag;
5205 }
5206
5207 /*
5208 * the block is shared by multiple trees, so it's not good to
5209 * keep the tree lock
5210 */
5211 if (path->locks[level] && level > 0) {
5212 btrfs_tree_unlock(eb);
5213 path->locks[level] = 0;
5214 }
5215 return 0;
5216}
5217
5218/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005219 * hepler to process tree block pointer.
5220 *
5221 * when wc->stage == DROP_REFERENCE, this function checks
5222 * reference count of the block pointed to. if the block
5223 * is shared and we need update back refs for the subtree
5224 * rooted at the block, this function changes wc->stage to
5225 * UPDATE_BACKREF. if the block is shared and there is no
5226 * need to update back, this function drops the reference
5227 * to the block.
5228 *
5229 * NOTE: return value 1 means we should stop walking down.
5230 */
5231static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5232 struct btrfs_root *root,
5233 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005234 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005235{
5236 u64 bytenr;
5237 u64 generation;
5238 u64 parent;
5239 u32 blocksize;
5240 struct btrfs_key key;
5241 struct extent_buffer *next;
5242 int level = wc->level;
5243 int reada = 0;
5244 int ret = 0;
5245
5246 generation = btrfs_node_ptr_generation(path->nodes[level],
5247 path->slots[level]);
5248 /*
5249 * if the lower level block was created before the snapshot
5250 * was created, we know there is no need to update back refs
5251 * for the subtree
5252 */
5253 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005254 generation <= root->root_key.offset) {
5255 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005256 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005257 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005258
5259 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5260 blocksize = btrfs_level_size(root, level - 1);
5261
5262 next = btrfs_find_tree_block(root, bytenr, blocksize);
5263 if (!next) {
5264 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00005265 if (!next)
5266 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005267 reada = 1;
5268 }
5269 btrfs_tree_lock(next);
5270 btrfs_set_lock_blocking(next);
5271
Yan, Zheng94fcca92009-10-09 09:25:16 -04005272 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5273 &wc->refs[level - 1],
5274 &wc->flags[level - 1]);
5275 BUG_ON(ret);
5276 BUG_ON(wc->refs[level - 1] == 0);
5277 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005278
Yan, Zheng94fcca92009-10-09 09:25:16 -04005279 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005280 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005281 if (level == 1 &&
5282 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5283 goto skip;
5284
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005285 if (!wc->update_ref ||
5286 generation <= root->root_key.offset)
5287 goto skip;
5288
5289 btrfs_node_key_to_cpu(path->nodes[level], &key,
5290 path->slots[level]);
5291 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5292 if (ret < 0)
5293 goto skip;
5294
5295 wc->stage = UPDATE_BACKREF;
5296 wc->shared_level = level - 1;
5297 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005298 } else {
5299 if (level == 1 &&
5300 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5301 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005302 }
5303
5304 if (!btrfs_buffer_uptodate(next, generation)) {
5305 btrfs_tree_unlock(next);
5306 free_extent_buffer(next);
5307 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005308 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005309 }
5310
5311 if (!next) {
5312 if (reada && level == 1)
5313 reada_walk_down(trans, root, wc, path);
5314 next = read_tree_block(root, bytenr, blocksize, generation);
5315 btrfs_tree_lock(next);
5316 btrfs_set_lock_blocking(next);
5317 }
5318
5319 level--;
5320 BUG_ON(level != btrfs_header_level(next));
5321 path->nodes[level] = next;
5322 path->slots[level] = 0;
5323 path->locks[level] = 1;
5324 wc->level = level;
5325 if (wc->level == 1)
5326 wc->reada_slot = 0;
5327 return 0;
5328skip:
5329 wc->refs[level - 1] = 0;
5330 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005331 if (wc->stage == DROP_REFERENCE) {
5332 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5333 parent = path->nodes[level]->start;
5334 } else {
5335 BUG_ON(root->root_key.objectid !=
5336 btrfs_header_owner(path->nodes[level]));
5337 parent = 0;
5338 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005339
Yan, Zheng94fcca92009-10-09 09:25:16 -04005340 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5341 root->root_key.objectid, level - 1, 0);
5342 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005343 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005344 btrfs_tree_unlock(next);
5345 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04005346 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005347 return 1;
5348}
5349
5350/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005351 * hepler to process tree block while walking up the tree.
5352 *
5353 * when wc->stage == DROP_REFERENCE, this function drops
5354 * reference count on the block.
5355 *
5356 * when wc->stage == UPDATE_BACKREF, this function changes
5357 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5358 * to UPDATE_BACKREF previously while processing the block.
5359 *
5360 * NOTE: return value 1 means we should stop walking up.
5361 */
5362static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5363 struct btrfs_root *root,
5364 struct btrfs_path *path,
5365 struct walk_control *wc)
5366{
5367 int ret = 0;
5368 int level = wc->level;
5369 struct extent_buffer *eb = path->nodes[level];
5370 u64 parent = 0;
5371
5372 if (wc->stage == UPDATE_BACKREF) {
5373 BUG_ON(wc->shared_level < level);
5374 if (level < wc->shared_level)
5375 goto out;
5376
Yan Zheng2c47e6052009-06-27 21:07:35 -04005377 ret = find_next_key(path, level + 1, &wc->update_progress);
5378 if (ret > 0)
5379 wc->update_ref = 0;
5380
5381 wc->stage = DROP_REFERENCE;
5382 wc->shared_level = -1;
5383 path->slots[level] = 0;
5384
5385 /*
5386 * check reference count again if the block isn't locked.
5387 * we should start walking down the tree again if reference
5388 * count is one.
5389 */
5390 if (!path->locks[level]) {
5391 BUG_ON(level == 0);
5392 btrfs_tree_lock(eb);
5393 btrfs_set_lock_blocking(eb);
5394 path->locks[level] = 1;
5395
5396 ret = btrfs_lookup_extent_info(trans, root,
5397 eb->start, eb->len,
5398 &wc->refs[level],
5399 &wc->flags[level]);
5400 BUG_ON(ret);
5401 BUG_ON(wc->refs[level] == 0);
5402 if (wc->refs[level] == 1) {
5403 btrfs_tree_unlock(eb);
5404 path->locks[level] = 0;
5405 return 1;
5406 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005407 }
5408 }
5409
5410 /* wc->stage == DROP_REFERENCE */
5411 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5412
5413 if (wc->refs[level] == 1) {
5414 if (level == 0) {
5415 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5416 ret = btrfs_dec_ref(trans, root, eb, 1);
5417 else
5418 ret = btrfs_dec_ref(trans, root, eb, 0);
5419 BUG_ON(ret);
5420 }
5421 /* make block locked assertion in clean_tree_block happy */
5422 if (!path->locks[level] &&
5423 btrfs_header_generation(eb) == trans->transid) {
5424 btrfs_tree_lock(eb);
5425 btrfs_set_lock_blocking(eb);
5426 path->locks[level] = 1;
5427 }
5428 clean_tree_block(trans, root, eb);
5429 }
5430
5431 if (eb == root->node) {
5432 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5433 parent = eb->start;
5434 else
5435 BUG_ON(root->root_key.objectid !=
5436 btrfs_header_owner(eb));
5437 } else {
5438 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5439 parent = path->nodes[level + 1]->start;
5440 else
5441 BUG_ON(root->root_key.objectid !=
5442 btrfs_header_owner(path->nodes[level + 1]));
5443 }
5444
5445 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5446 root->root_key.objectid, level, 0);
5447 BUG_ON(ret);
5448out:
5449 wc->refs[level] = 0;
5450 wc->flags[level] = 0;
5451 return ret;
5452}
5453
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005454static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5455 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005456 struct btrfs_path *path,
5457 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005458{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005459 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005460 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005461 int ret;
5462
Yan Zheng2c47e6052009-06-27 21:07:35 -04005463 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005464 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005465 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005466 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005467
Yan Zheng2c47e6052009-06-27 21:07:35 -04005468 if (level == 0)
5469 break;
5470
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005471 if (path->slots[level] >=
5472 btrfs_header_nritems(path->nodes[level]))
5473 break;
5474
Yan, Zheng94fcca92009-10-09 09:25:16 -04005475 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005476 if (ret > 0) {
5477 path->slots[level]++;
5478 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00005479 } else if (ret < 0)
5480 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005481 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005482 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005483 return 0;
5484}
5485
Chris Masond3977122009-01-05 21:25:51 -05005486static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005487 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005488 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005489 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005490{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005491 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005492 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005493
Yan Zheng2c47e6052009-06-27 21:07:35 -04005494 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5495 while (level < max_level && path->nodes[level]) {
5496 wc->level = level;
5497 if (path->slots[level] + 1 <
5498 btrfs_header_nritems(path->nodes[level])) {
5499 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005500 return 0;
5501 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005502 ret = walk_up_proc(trans, root, path, wc);
5503 if (ret > 0)
5504 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05005505
Yan Zheng2c47e6052009-06-27 21:07:35 -04005506 if (path->locks[level]) {
5507 btrfs_tree_unlock(path->nodes[level]);
5508 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005509 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005510 free_extent_buffer(path->nodes[level]);
5511 path->nodes[level] = NULL;
5512 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005513 }
5514 }
5515 return 1;
5516}
5517
Chris Mason9aca1d52007-03-13 11:09:37 -04005518/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005519 * drop a subvolume tree.
5520 *
5521 * this function traverses the tree freeing any blocks that only
5522 * referenced by the tree.
5523 *
5524 * when a shared tree block is found. this function decreases its
5525 * reference count by one. if update_ref is true, this function
5526 * also make sure backrefs for the shared block and all lower level
5527 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04005528 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005529int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05005530{
Chris Mason5caf2a02007-04-02 11:20:42 -04005531 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005532 struct btrfs_trans_handle *trans;
5533 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005534 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005535 struct walk_control *wc;
5536 struct btrfs_key key;
5537 int err = 0;
5538 int ret;
5539 int level;
Chris Mason20524f02007-03-10 06:35:47 -05005540
Chris Mason5caf2a02007-04-02 11:20:42 -04005541 path = btrfs_alloc_path();
5542 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05005543
Yan Zheng2c47e6052009-06-27 21:07:35 -04005544 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5545 BUG_ON(!wc);
5546
5547 trans = btrfs_start_transaction(tree_root, 1);
5548
Chris Mason9f3a7422007-08-07 15:52:19 -04005549 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005550 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005551 path->nodes[level] = btrfs_lock_root_node(root);
5552 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005553 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005554 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005555 memset(&wc->update_progress, 0,
5556 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005557 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005558 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005559 memcpy(&wc->update_progress, &key,
5560 sizeof(wc->update_progress));
5561
Chris Mason6702ed42007-08-07 16:15:09 -04005562 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005563 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005564 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005565 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5566 path->lowest_level = 0;
5567 if (ret < 0) {
5568 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005569 goto out;
5570 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005571 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005572
Chris Mason7d9eb122008-07-08 14:19:17 -04005573 /*
5574 * unlock our path, this is safe because only this
5575 * function is allowed to delete this snapshot
5576 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005577 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005578
Yan Zheng2c47e6052009-06-27 21:07:35 -04005579 level = btrfs_header_level(root->node);
5580 while (1) {
5581 btrfs_tree_lock(path->nodes[level]);
5582 btrfs_set_lock_blocking(path->nodes[level]);
5583
5584 ret = btrfs_lookup_extent_info(trans, root,
5585 path->nodes[level]->start,
5586 path->nodes[level]->len,
5587 &wc->refs[level],
5588 &wc->flags[level]);
5589 BUG_ON(ret);
5590 BUG_ON(wc->refs[level] == 0);
5591
5592 if (level == root_item->drop_level)
5593 break;
5594
5595 btrfs_tree_unlock(path->nodes[level]);
5596 WARN_ON(wc->refs[level] != 1);
5597 level--;
5598 }
5599 }
5600
5601 wc->level = level;
5602 wc->shared_level = -1;
5603 wc->stage = DROP_REFERENCE;
5604 wc->update_ref = update_ref;
5605 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005606 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005607
5608 while (1) {
5609 ret = walk_down_tree(trans, root, path, wc);
5610 if (ret < 0) {
5611 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005612 break;
5613 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005614
5615 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5616 if (ret < 0) {
5617 err = ret;
5618 break;
5619 }
5620
5621 if (ret > 0) {
5622 BUG_ON(wc->stage != DROP_REFERENCE);
5623 break;
5624 }
5625
5626 if (wc->stage == DROP_REFERENCE) {
5627 level = wc->level;
5628 btrfs_node_key(path->nodes[level],
5629 &root_item->drop_progress,
5630 path->slots[level]);
5631 root_item->drop_level = level;
5632 }
5633
5634 BUG_ON(wc->level == 0);
5635 if (trans->transaction->in_commit ||
5636 trans->transaction->delayed_refs.flushing) {
5637 ret = btrfs_update_root(trans, tree_root,
5638 &root->root_key,
5639 root_item);
5640 BUG_ON(ret);
5641
5642 btrfs_end_transaction(trans, tree_root);
5643 trans = btrfs_start_transaction(tree_root, 1);
5644 } else {
5645 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005646 update = trans->delayed_ref_updates;
5647 trans->delayed_ref_updates = 0;
5648 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005649 btrfs_run_delayed_refs(trans, tree_root,
5650 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005651 }
Chris Mason20524f02007-03-10 06:35:47 -05005652 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005653 btrfs_release_path(root, path);
5654 BUG_ON(err);
5655
5656 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5657 BUG_ON(ret);
5658
Yan, Zheng76dda932009-09-21 16:00:26 -04005659 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5660 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5661 NULL, NULL);
5662 BUG_ON(ret < 0);
5663 if (ret > 0) {
5664 ret = btrfs_del_orphan_item(trans, tree_root,
5665 root->root_key.objectid);
5666 BUG_ON(ret);
5667 }
5668 }
5669
5670 if (root->in_radix) {
5671 btrfs_free_fs_root(tree_root->fs_info, root);
5672 } else {
5673 free_extent_buffer(root->node);
5674 free_extent_buffer(root->commit_root);
5675 kfree(root);
5676 }
Chris Mason9f3a7422007-08-07 15:52:19 -04005677out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005678 btrfs_end_transaction(trans, tree_root);
5679 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005680 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005681 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005682}
Chris Mason9078a3e2007-04-26 16:46:15 -04005683
Yan Zheng2c47e6052009-06-27 21:07:35 -04005684/*
5685 * drop subtree rooted at tree block 'node'.
5686 *
5687 * NOTE: this function will unlock and release tree block 'node'
5688 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005689int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5690 struct btrfs_root *root,
5691 struct extent_buffer *node,
5692 struct extent_buffer *parent)
5693{
5694 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005695 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005696 int level;
5697 int parent_level;
5698 int ret = 0;
5699 int wret;
5700
Yan Zheng2c47e6052009-06-27 21:07:35 -04005701 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5702
Yan Zhengf82d02d2008-10-29 14:49:05 -04005703 path = btrfs_alloc_path();
5704 BUG_ON(!path);
5705
Yan Zheng2c47e6052009-06-27 21:07:35 -04005706 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5707 BUG_ON(!wc);
5708
Chris Masonb9447ef2009-03-09 11:45:38 -04005709 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005710 parent_level = btrfs_header_level(parent);
5711 extent_buffer_get(parent);
5712 path->nodes[parent_level] = parent;
5713 path->slots[parent_level] = btrfs_header_nritems(parent);
5714
Chris Masonb9447ef2009-03-09 11:45:38 -04005715 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005716 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005717 path->nodes[level] = node;
5718 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005719 path->locks[level] = 1;
5720
5721 wc->refs[parent_level] = 1;
5722 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5723 wc->level = level;
5724 wc->shared_level = -1;
5725 wc->stage = DROP_REFERENCE;
5726 wc->update_ref = 0;
5727 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005728 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005729
5730 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005731 wret = walk_down_tree(trans, root, path, wc);
5732 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005733 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005734 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005735 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005736
Yan Zheng2c47e6052009-06-27 21:07:35 -04005737 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005738 if (wret < 0)
5739 ret = wret;
5740 if (wret != 0)
5741 break;
5742 }
5743
Yan Zheng2c47e6052009-06-27 21:07:35 -04005744 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005745 btrfs_free_path(path);
5746 return ret;
5747}
5748
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005749#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005750static unsigned long calc_ra(unsigned long start, unsigned long last,
5751 unsigned long nr)
5752{
5753 return min(last, start + nr - 1);
5754}
5755
Chris Masond3977122009-01-05 21:25:51 -05005756static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005757 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005758{
5759 u64 page_start;
5760 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005761 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005762 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005763 unsigned long i;
5764 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005765 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005766 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005767 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005768 unsigned int total_read = 0;
5769 unsigned int total_dirty = 0;
5770 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005771
5772 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005773
5774 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005775 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005776 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5777
Zheng Yan1a40e232008-09-26 10:09:34 -04005778 /* make sure the dirty trick played by the caller work */
5779 ret = invalidate_inode_pages2_range(inode->i_mapping,
5780 first_index, last_index);
5781 if (ret)
5782 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005783
Chris Mason4313b392008-01-03 09:08:48 -05005784 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005785
Zheng Yan1a40e232008-09-26 10:09:34 -04005786 for (i = first_index ; i <= last_index; i++) {
5787 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005788 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005789 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005790 }
5791 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005792again:
5793 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005794 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005795 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005796 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005797 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005798 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005799 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005800 if (!PageUptodate(page)) {
5801 btrfs_readpage(NULL, page);
5802 lock_page(page);
5803 if (!PageUptodate(page)) {
5804 unlock_page(page);
5805 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005806 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005807 goto out_unlock;
5808 }
5809 }
Chris Masonec44a352008-04-28 15:29:52 -04005810 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005811
Chris Masonedbd8d42007-12-21 16:27:24 -05005812 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5813 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005814 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005815
Chris Mason3eaa2882008-07-24 11:57:52 -04005816 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5817 if (ordered) {
5818 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5819 unlock_page(page);
5820 page_cache_release(page);
5821 btrfs_start_ordered_extent(inode, ordered, 1);
5822 btrfs_put_ordered_extent(ordered);
5823 goto again;
5824 }
5825 set_page_extent_mapped(page);
5826
Zheng Yan1a40e232008-09-26 10:09:34 -04005827 if (i == first_index)
5828 set_extent_bits(io_tree, page_start, page_end,
5829 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005830 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005831
Chris Masona061fc82008-05-07 11:43:44 -04005832 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005833 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005834
Chris Masond1310b22008-01-24 16:13:08 -05005835 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005836 unlock_page(page);
5837 page_cache_release(page);
5838 }
5839
5840out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005841 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005842 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005843 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005844 return ret;
5845}
5846
Chris Masond3977122009-01-05 21:25:51 -05005847static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005848 struct btrfs_key *extent_key,
5849 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005850{
Zheng Yan1a40e232008-09-26 10:09:34 -04005851 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5852 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5853 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005854 u64 start = extent_key->objectid - offset;
5855 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005856
5857 em = alloc_extent_map(GFP_NOFS);
5858 BUG_ON(!em || IS_ERR(em));
5859
Yan Zheng66435582008-10-30 14:19:50 -04005860 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005861 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005862 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005863 em->block_start = extent_key->objectid;
5864 em->bdev = root->fs_info->fs_devices->latest_bdev;
5865 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5866
5867 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005868 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005869 while (1) {
5870 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04005871 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005872 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005873 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005874 if (ret != -EEXIST) {
5875 free_extent_map(em);
5876 break;
5877 }
Yan Zheng66435582008-10-30 14:19:50 -04005878 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005879 }
Yan Zheng66435582008-10-30 14:19:50 -04005880 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005881
Yan Zheng66435582008-10-30 14:19:50 -04005882 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005883}
5884
5885struct btrfs_ref_path {
5886 u64 extent_start;
5887 u64 nodes[BTRFS_MAX_LEVEL];
5888 u64 root_objectid;
5889 u64 root_generation;
5890 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005891 u32 num_refs;
5892 int lowest_level;
5893 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005894 int shared_level;
5895
5896 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5897 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005898};
5899
5900struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005901 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005902 u64 disk_bytenr;
5903 u64 disk_num_bytes;
5904 u64 offset;
5905 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005906 u8 compression;
5907 u8 encryption;
5908 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005909};
5910
5911static int is_cowonly_root(u64 root_objectid)
5912{
5913 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5914 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5915 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5916 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005917 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5918 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005919 return 1;
5920 return 0;
5921}
5922
Chris Masond3977122009-01-05 21:25:51 -05005923static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005924 struct btrfs_root *extent_root,
5925 struct btrfs_ref_path *ref_path,
5926 int first_time)
5927{
5928 struct extent_buffer *leaf;
5929 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005930 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005931 struct btrfs_key key;
5932 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005933 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005934 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005935 int level;
5936 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005937
Zheng Yan1a40e232008-09-26 10:09:34 -04005938 path = btrfs_alloc_path();
5939 if (!path)
5940 return -ENOMEM;
5941
Zheng Yan1a40e232008-09-26 10:09:34 -04005942 if (first_time) {
5943 ref_path->lowest_level = -1;
5944 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005945 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005946 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005947 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005948walk_down:
5949 level = ref_path->current_level - 1;
5950 while (level >= -1) {
5951 u64 parent;
5952 if (level < ref_path->lowest_level)
5953 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005954
Chris Masond3977122009-01-05 21:25:51 -05005955 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005956 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005957 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005958 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005959 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005960
Zheng Yan1a40e232008-09-26 10:09:34 -04005961 parent = ref_path->nodes[level + 1];
5962 ref_path->nodes[level + 1] = 0;
5963 ref_path->current_level = level;
5964 BUG_ON(parent == 0);
5965
5966 key.objectid = bytenr;
5967 key.offset = parent + 1;
5968 key.type = BTRFS_EXTENT_REF_KEY;
5969
5970 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005971 if (ret < 0)
5972 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005973 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005974
Chris Masonedbd8d42007-12-21 16:27:24 -05005975 leaf = path->nodes[0];
5976 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005977 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005978 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005979 if (ret < 0)
5980 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005981 if (ret > 0)
5982 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005983 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005984 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005985
5986 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005987 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005988 found_key.type == BTRFS_EXTENT_REF_KEY) {
5989 if (level < ref_path->shared_level)
5990 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005991 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005992 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005993next:
5994 level--;
5995 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005996 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005997 }
5998 /* reached lowest level */
5999 ret = 1;
6000 goto out;
6001walk_up:
6002 level = ref_path->current_level;
6003 while (level < BTRFS_MAX_LEVEL - 1) {
6004 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006005
6006 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006007 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006008 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006009 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006010
Zheng Yan1a40e232008-09-26 10:09:34 -04006011 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006012
Zheng Yan1a40e232008-09-26 10:09:34 -04006013 key.objectid = bytenr;
6014 key.offset = 0;
6015 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006016
Zheng Yan1a40e232008-09-26 10:09:34 -04006017 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6018 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006019 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006020
6021 leaf = path->nodes[0];
6022 nritems = btrfs_header_nritems(leaf);
6023 if (path->slots[0] >= nritems) {
6024 ret = btrfs_next_leaf(extent_root, path);
6025 if (ret < 0)
6026 goto out;
6027 if (ret > 0) {
6028 /* the extent was freed by someone */
6029 if (ref_path->lowest_level == level)
6030 goto out;
6031 btrfs_release_path(extent_root, path);
6032 goto walk_down;
6033 }
6034 leaf = path->nodes[0];
6035 }
6036
6037 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6038 if (found_key.objectid != bytenr ||
6039 found_key.type != BTRFS_EXTENT_REF_KEY) {
6040 /* the extent was freed by someone */
6041 if (ref_path->lowest_level == level) {
6042 ret = 1;
6043 goto out;
6044 }
6045 btrfs_release_path(extent_root, path);
6046 goto walk_down;
6047 }
6048found:
6049 ref = btrfs_item_ptr(leaf, path->slots[0],
6050 struct btrfs_extent_ref);
6051 ref_objectid = btrfs_ref_objectid(leaf, ref);
6052 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6053 if (first_time) {
6054 level = (int)ref_objectid;
6055 BUG_ON(level >= BTRFS_MAX_LEVEL);
6056 ref_path->lowest_level = level;
6057 ref_path->current_level = level;
6058 ref_path->nodes[level] = bytenr;
6059 } else {
6060 WARN_ON(ref_objectid != level);
6061 }
6062 } else {
6063 WARN_ON(level != -1);
6064 }
6065 first_time = 0;
6066
6067 if (ref_path->lowest_level == level) {
6068 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006069 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6070 }
6071
6072 /*
6073 * the block is tree root or the block isn't in reference
6074 * counted tree.
6075 */
6076 if (found_key.objectid == found_key.offset ||
6077 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6078 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6079 ref_path->root_generation =
6080 btrfs_ref_generation(leaf, ref);
6081 if (level < 0) {
6082 /* special reference from the tree log */
6083 ref_path->nodes[0] = found_key.offset;
6084 ref_path->current_level = 0;
6085 }
6086 ret = 0;
6087 goto out;
6088 }
6089
6090 level++;
6091 BUG_ON(ref_path->nodes[level] != 0);
6092 ref_path->nodes[level] = found_key.offset;
6093 ref_path->current_level = level;
6094
6095 /*
6096 * the reference was created in the running transaction,
6097 * no need to continue walking up.
6098 */
6099 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6100 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6101 ref_path->root_generation =
6102 btrfs_ref_generation(leaf, ref);
6103 ret = 0;
6104 goto out;
6105 }
6106
6107 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006108 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006109 }
6110 /* reached max tree level, but no tree root found. */
6111 BUG();
6112out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006113 btrfs_free_path(path);
6114 return ret;
6115}
6116
6117static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6118 struct btrfs_root *extent_root,
6119 struct btrfs_ref_path *ref_path,
6120 u64 extent_start)
6121{
6122 memset(ref_path, 0, sizeof(*ref_path));
6123 ref_path->extent_start = extent_start;
6124
6125 return __next_ref_path(trans, extent_root, ref_path, 1);
6126}
6127
6128static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6129 struct btrfs_root *extent_root,
6130 struct btrfs_ref_path *ref_path)
6131{
6132 return __next_ref_path(trans, extent_root, ref_path, 0);
6133}
6134
Chris Masond3977122009-01-05 21:25:51 -05006135static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006136 struct btrfs_key *extent_key,
6137 u64 offset, int no_fragment,
6138 struct disk_extent **extents,
6139 int *nr_extents)
6140{
6141 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6142 struct btrfs_path *path;
6143 struct btrfs_file_extent_item *fi;
6144 struct extent_buffer *leaf;
6145 struct disk_extent *exts = *extents;
6146 struct btrfs_key found_key;
6147 u64 cur_pos;
6148 u64 last_byte;
6149 u32 nritems;
6150 int nr = 0;
6151 int max = *nr_extents;
6152 int ret;
6153
6154 WARN_ON(!no_fragment && *extents);
6155 if (!exts) {
6156 max = 1;
6157 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6158 if (!exts)
6159 return -ENOMEM;
6160 }
6161
6162 path = btrfs_alloc_path();
6163 BUG_ON(!path);
6164
6165 cur_pos = extent_key->objectid - offset;
6166 last_byte = extent_key->objectid + extent_key->offset;
6167 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6168 cur_pos, 0);
6169 if (ret < 0)
6170 goto out;
6171 if (ret > 0) {
6172 ret = -ENOENT;
6173 goto out;
6174 }
6175
6176 while (1) {
6177 leaf = path->nodes[0];
6178 nritems = btrfs_header_nritems(leaf);
6179 if (path->slots[0] >= nritems) {
6180 ret = btrfs_next_leaf(root, path);
6181 if (ret < 0)
6182 goto out;
6183 if (ret > 0)
6184 break;
6185 leaf = path->nodes[0];
6186 }
6187
6188 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6189 if (found_key.offset != cur_pos ||
6190 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6191 found_key.objectid != reloc_inode->i_ino)
6192 break;
6193
6194 fi = btrfs_item_ptr(leaf, path->slots[0],
6195 struct btrfs_file_extent_item);
6196 if (btrfs_file_extent_type(leaf, fi) !=
6197 BTRFS_FILE_EXTENT_REG ||
6198 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6199 break;
6200
6201 if (nr == max) {
6202 struct disk_extent *old = exts;
6203 max *= 2;
6204 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6205 memcpy(exts, old, sizeof(*exts) * nr);
6206 if (old != *extents)
6207 kfree(old);
6208 }
6209
6210 exts[nr].disk_bytenr =
6211 btrfs_file_extent_disk_bytenr(leaf, fi);
6212 exts[nr].disk_num_bytes =
6213 btrfs_file_extent_disk_num_bytes(leaf, fi);
6214 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6215 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04006216 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6217 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6218 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6219 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6220 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04006221 BUG_ON(exts[nr].offset > 0);
6222 BUG_ON(exts[nr].compression || exts[nr].encryption);
6223 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006224
6225 cur_pos += exts[nr].num_bytes;
6226 nr++;
6227
6228 if (cur_pos + offset >= last_byte)
6229 break;
6230
6231 if (no_fragment) {
6232 ret = 1;
6233 goto out;
6234 }
6235 path->slots[0]++;
6236 }
6237
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006238 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04006239 if (cur_pos + offset < last_byte) {
6240 ret = -ENOENT;
6241 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05006242 }
6243 ret = 0;
6244out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006245 btrfs_free_path(path);
6246 if (ret) {
6247 if (exts != *extents)
6248 kfree(exts);
6249 } else {
6250 *extents = exts;
6251 *nr_extents = nr;
6252 }
6253 return ret;
6254}
6255
Chris Masond3977122009-01-05 21:25:51 -05006256static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006257 struct btrfs_root *root,
6258 struct btrfs_path *path,
6259 struct btrfs_key *extent_key,
6260 struct btrfs_key *leaf_key,
6261 struct btrfs_ref_path *ref_path,
6262 struct disk_extent *new_extents,
6263 int nr_extents)
6264{
6265 struct extent_buffer *leaf;
6266 struct btrfs_file_extent_item *fi;
6267 struct inode *inode = NULL;
6268 struct btrfs_key key;
6269 u64 lock_start = 0;
6270 u64 lock_end = 0;
6271 u64 num_bytes;
6272 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05006273 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006274 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006275 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006276 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04006277 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04006278 int ret;
6279
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006280 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04006281 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006282 if (key.objectid < ref_path->owner_objectid ||
6283 (key.objectid == ref_path->owner_objectid &&
6284 key.type < BTRFS_EXTENT_DATA_KEY)) {
6285 key.objectid = ref_path->owner_objectid;
6286 key.type = BTRFS_EXTENT_DATA_KEY;
6287 key.offset = 0;
6288 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006289 }
6290
6291 while (1) {
6292 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6293 if (ret < 0)
6294 goto out;
6295
6296 leaf = path->nodes[0];
6297 nritems = btrfs_header_nritems(leaf);
6298next:
6299 if (extent_locked && ret > 0) {
6300 /*
6301 * the file extent item was modified by someone
6302 * before the extent got locked.
6303 */
Zheng Yan1a40e232008-09-26 10:09:34 -04006304 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6305 lock_end, GFP_NOFS);
6306 extent_locked = 0;
6307 }
6308
6309 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006310 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04006311 break;
6312
6313 BUG_ON(extent_locked);
6314 ret = btrfs_next_leaf(root, path);
6315 if (ret < 0)
6316 goto out;
6317 if (ret > 0)
6318 break;
6319 leaf = path->nodes[0];
6320 nritems = btrfs_header_nritems(leaf);
6321 }
6322
6323 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6324
6325 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6326 if ((key.objectid > ref_path->owner_objectid) ||
6327 (key.objectid == ref_path->owner_objectid &&
6328 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05006329 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006330 break;
6331 }
6332
6333 if (inode && key.objectid != inode->i_ino) {
6334 BUG_ON(extent_locked);
6335 btrfs_release_path(root, path);
6336 mutex_unlock(&inode->i_mutex);
6337 iput(inode);
6338 inode = NULL;
6339 continue;
6340 }
6341
6342 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6343 path->slots[0]++;
6344 ret = 1;
6345 goto next;
6346 }
6347 fi = btrfs_item_ptr(leaf, path->slots[0],
6348 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04006349 extent_type = btrfs_file_extent_type(leaf, fi);
6350 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6351 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04006352 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6353 extent_key->objectid)) {
6354 path->slots[0]++;
6355 ret = 1;
6356 goto next;
6357 }
6358
6359 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6360 ext_offset = btrfs_file_extent_offset(leaf, fi);
6361
Yan Zheng86288a12009-01-21 10:49:16 -05006362 if (search_end == (u64)-1) {
6363 search_end = key.offset - ext_offset +
6364 btrfs_file_extent_ram_bytes(leaf, fi);
6365 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006366
6367 if (!extent_locked) {
6368 lock_start = key.offset;
6369 lock_end = lock_start + num_bytes - 1;
6370 } else {
Yan Zheng66435582008-10-30 14:19:50 -04006371 if (lock_start > key.offset ||
6372 lock_end + 1 < key.offset + num_bytes) {
6373 unlock_extent(&BTRFS_I(inode)->io_tree,
6374 lock_start, lock_end, GFP_NOFS);
6375 extent_locked = 0;
6376 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006377 }
6378
6379 if (!inode) {
6380 btrfs_release_path(root, path);
6381
6382 inode = btrfs_iget_locked(root->fs_info->sb,
6383 key.objectid, root);
6384 if (inode->i_state & I_NEW) {
6385 BTRFS_I(inode)->root = root;
6386 BTRFS_I(inode)->location.objectid =
6387 key.objectid;
6388 BTRFS_I(inode)->location.type =
6389 BTRFS_INODE_ITEM_KEY;
6390 BTRFS_I(inode)->location.offset = 0;
6391 btrfs_read_locked_inode(inode);
6392 unlock_new_inode(inode);
6393 }
6394 /*
6395 * some code call btrfs_commit_transaction while
6396 * holding the i_mutex, so we can't use mutex_lock
6397 * here.
6398 */
6399 if (is_bad_inode(inode) ||
6400 !mutex_trylock(&inode->i_mutex)) {
6401 iput(inode);
6402 inode = NULL;
6403 key.offset = (u64)-1;
6404 goto skip;
6405 }
6406 }
6407
6408 if (!extent_locked) {
6409 struct btrfs_ordered_extent *ordered;
6410
6411 btrfs_release_path(root, path);
6412
6413 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6414 lock_end, GFP_NOFS);
6415 ordered = btrfs_lookup_first_ordered_extent(inode,
6416 lock_end);
6417 if (ordered &&
6418 ordered->file_offset <= lock_end &&
6419 ordered->file_offset + ordered->len > lock_start) {
6420 unlock_extent(&BTRFS_I(inode)->io_tree,
6421 lock_start, lock_end, GFP_NOFS);
6422 btrfs_start_ordered_extent(inode, ordered, 1);
6423 btrfs_put_ordered_extent(ordered);
6424 key.offset += num_bytes;
6425 goto skip;
6426 }
6427 if (ordered)
6428 btrfs_put_ordered_extent(ordered);
6429
Zheng Yan1a40e232008-09-26 10:09:34 -04006430 extent_locked = 1;
6431 continue;
6432 }
6433
6434 if (nr_extents == 1) {
6435 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04006436 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6437 new_extents[0].disk_bytenr);
6438 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6439 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006440 btrfs_mark_buffer_dirty(leaf);
6441
6442 btrfs_drop_extent_cache(inode, key.offset,
6443 key.offset + num_bytes - 1, 0);
6444
6445 ret = btrfs_inc_extent_ref(trans, root,
6446 new_extents[0].disk_bytenr,
6447 new_extents[0].disk_num_bytes,
6448 leaf->start,
6449 root->root_key.objectid,
6450 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006451 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006452 BUG_ON(ret);
6453
6454 ret = btrfs_free_extent(trans, root,
6455 extent_key->objectid,
6456 extent_key->offset,
6457 leaf->start,
6458 btrfs_header_owner(leaf),
6459 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006460 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006461 BUG_ON(ret);
6462
6463 btrfs_release_path(root, path);
6464 key.offset += num_bytes;
6465 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04006466 BUG_ON(1);
6467#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006468 u64 alloc_hint;
6469 u64 extent_len;
6470 int i;
6471 /*
6472 * drop old extent pointer at first, then insert the
6473 * new pointers one bye one
6474 */
6475 btrfs_release_path(root, path);
6476 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6477 key.offset + num_bytes,
6478 key.offset, &alloc_hint);
6479 BUG_ON(ret);
6480
6481 for (i = 0; i < nr_extents; i++) {
6482 if (ext_offset >= new_extents[i].num_bytes) {
6483 ext_offset -= new_extents[i].num_bytes;
6484 continue;
6485 }
6486 extent_len = min(new_extents[i].num_bytes -
6487 ext_offset, num_bytes);
6488
6489 ret = btrfs_insert_empty_item(trans, root,
6490 path, &key,
6491 sizeof(*fi));
6492 BUG_ON(ret);
6493
6494 leaf = path->nodes[0];
6495 fi = btrfs_item_ptr(leaf, path->slots[0],
6496 struct btrfs_file_extent_item);
6497 btrfs_set_file_extent_generation(leaf, fi,
6498 trans->transid);
6499 btrfs_set_file_extent_type(leaf, fi,
6500 BTRFS_FILE_EXTENT_REG);
6501 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6502 new_extents[i].disk_bytenr);
6503 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6504 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04006505 btrfs_set_file_extent_ram_bytes(leaf, fi,
6506 new_extents[i].ram_bytes);
6507
6508 btrfs_set_file_extent_compression(leaf, fi,
6509 new_extents[i].compression);
6510 btrfs_set_file_extent_encryption(leaf, fi,
6511 new_extents[i].encryption);
6512 btrfs_set_file_extent_other_encoding(leaf, fi,
6513 new_extents[i].other_encoding);
6514
Zheng Yan1a40e232008-09-26 10:09:34 -04006515 btrfs_set_file_extent_num_bytes(leaf, fi,
6516 extent_len);
6517 ext_offset += new_extents[i].offset;
6518 btrfs_set_file_extent_offset(leaf, fi,
6519 ext_offset);
6520 btrfs_mark_buffer_dirty(leaf);
6521
6522 btrfs_drop_extent_cache(inode, key.offset,
6523 key.offset + extent_len - 1, 0);
6524
6525 ret = btrfs_inc_extent_ref(trans, root,
6526 new_extents[i].disk_bytenr,
6527 new_extents[i].disk_num_bytes,
6528 leaf->start,
6529 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006530 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006531 BUG_ON(ret);
6532 btrfs_release_path(root, path);
6533
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006534 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04006535
6536 ext_offset = 0;
6537 num_bytes -= extent_len;
6538 key.offset += extent_len;
6539
6540 if (num_bytes == 0)
6541 break;
6542 }
6543 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006544#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04006545 }
6546
6547 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006548 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6549 lock_end, GFP_NOFS);
6550 extent_locked = 0;
6551 }
6552skip:
6553 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05006554 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006555 break;
6556
6557 cond_resched();
6558 }
6559 ret = 0;
6560out:
6561 btrfs_release_path(root, path);
6562 if (inode) {
6563 mutex_unlock(&inode->i_mutex);
6564 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006565 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6566 lock_end, GFP_NOFS);
6567 }
6568 iput(inode);
6569 }
6570 return ret;
6571}
6572
Zheng Yan1a40e232008-09-26 10:09:34 -04006573int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6574 struct btrfs_root *root,
6575 struct extent_buffer *buf, u64 orig_start)
6576{
6577 int level;
6578 int ret;
6579
6580 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6581 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6582
6583 level = btrfs_header_level(buf);
6584 if (level == 0) {
6585 struct btrfs_leaf_ref *ref;
6586 struct btrfs_leaf_ref *orig_ref;
6587
6588 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6589 if (!orig_ref)
6590 return -ENOENT;
6591
6592 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6593 if (!ref) {
6594 btrfs_free_leaf_ref(root, orig_ref);
6595 return -ENOMEM;
6596 }
6597
6598 ref->nritems = orig_ref->nritems;
6599 memcpy(ref->extents, orig_ref->extents,
6600 sizeof(ref->extents[0]) * ref->nritems);
6601
6602 btrfs_free_leaf_ref(root, orig_ref);
6603
6604 ref->root_gen = trans->transid;
6605 ref->bytenr = buf->start;
6606 ref->owner = btrfs_header_owner(buf);
6607 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006608
Zheng Yan1a40e232008-09-26 10:09:34 -04006609 ret = btrfs_add_leaf_ref(root, ref, 0);
6610 WARN_ON(ret);
6611 btrfs_free_leaf_ref(root, ref);
6612 }
6613 return 0;
6614}
6615
Chris Masond3977122009-01-05 21:25:51 -05006616static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006617 struct extent_buffer *leaf,
6618 struct btrfs_block_group_cache *group,
6619 struct btrfs_root *target_root)
6620{
6621 struct btrfs_key key;
6622 struct inode *inode = NULL;
6623 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006624 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04006625 u64 num_bytes;
6626 u64 skip_objectid = 0;
6627 u32 nritems;
6628 u32 i;
6629
6630 nritems = btrfs_header_nritems(leaf);
6631 for (i = 0; i < nritems; i++) {
6632 btrfs_item_key_to_cpu(leaf, &key, i);
6633 if (key.objectid == skip_objectid ||
6634 key.type != BTRFS_EXTENT_DATA_KEY)
6635 continue;
6636 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6637 if (btrfs_file_extent_type(leaf, fi) ==
6638 BTRFS_FILE_EXTENT_INLINE)
6639 continue;
6640 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6641 continue;
6642 if (!inode || inode->i_ino != key.objectid) {
6643 iput(inode);
6644 inode = btrfs_ilookup(target_root->fs_info->sb,
6645 key.objectid, target_root, 1);
6646 }
6647 if (!inode) {
6648 skip_objectid = key.objectid;
6649 continue;
6650 }
6651 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6652
Josef Bacik2ac55d42010-02-03 19:33:23 +00006653 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
6654 key.offset + num_bytes - 1, 0, &cached_state,
6655 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006656 btrfs_drop_extent_cache(inode, key.offset,
6657 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006658 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
6659 key.offset + num_bytes - 1, &cached_state,
6660 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006661 cond_resched();
6662 }
6663 iput(inode);
6664 return 0;
6665}
6666
Chris Masond3977122009-01-05 21:25:51 -05006667static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006668 struct btrfs_root *root,
6669 struct extent_buffer *leaf,
6670 struct btrfs_block_group_cache *group,
6671 struct inode *reloc_inode)
6672{
6673 struct btrfs_key key;
6674 struct btrfs_key extent_key;
6675 struct btrfs_file_extent_item *fi;
6676 struct btrfs_leaf_ref *ref;
6677 struct disk_extent *new_extent;
6678 u64 bytenr;
6679 u64 num_bytes;
6680 u32 nritems;
6681 u32 i;
6682 int ext_index;
6683 int nr_extent;
6684 int ret;
6685
6686 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6687 BUG_ON(!new_extent);
6688
6689 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6690 BUG_ON(!ref);
6691
6692 ext_index = -1;
6693 nritems = btrfs_header_nritems(leaf);
6694 for (i = 0; i < nritems; i++) {
6695 btrfs_item_key_to_cpu(leaf, &key, i);
6696 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6697 continue;
6698 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6699 if (btrfs_file_extent_type(leaf, fi) ==
6700 BTRFS_FILE_EXTENT_INLINE)
6701 continue;
6702 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6703 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6704 if (bytenr == 0)
6705 continue;
6706
6707 ext_index++;
6708 if (bytenr >= group->key.objectid + group->key.offset ||
6709 bytenr + num_bytes <= group->key.objectid)
6710 continue;
6711
6712 extent_key.objectid = bytenr;
6713 extent_key.offset = num_bytes;
6714 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6715 nr_extent = 1;
6716 ret = get_new_locations(reloc_inode, &extent_key,
6717 group->key.objectid, 1,
6718 &new_extent, &nr_extent);
6719 if (ret > 0)
6720 continue;
6721 BUG_ON(ret < 0);
6722
6723 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6724 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6725 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6726 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6727
Zheng Yan1a40e232008-09-26 10:09:34 -04006728 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6729 new_extent->disk_bytenr);
6730 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6731 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006732 btrfs_mark_buffer_dirty(leaf);
6733
6734 ret = btrfs_inc_extent_ref(trans, root,
6735 new_extent->disk_bytenr,
6736 new_extent->disk_num_bytes,
6737 leaf->start,
6738 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006739 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006740 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006741
Zheng Yan1a40e232008-09-26 10:09:34 -04006742 ret = btrfs_free_extent(trans, root,
6743 bytenr, num_bytes, leaf->start,
6744 btrfs_header_owner(leaf),
6745 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006746 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006747 BUG_ON(ret);
6748 cond_resched();
6749 }
6750 kfree(new_extent);
6751 BUG_ON(ext_index + 1 != ref->nritems);
6752 btrfs_free_leaf_ref(root, ref);
6753 return 0;
6754}
6755
Yan Zhengf82d02d2008-10-29 14:49:05 -04006756int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6757 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006758{
6759 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006760 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006761
6762 if (root->reloc_root) {
6763 reloc_root = root->reloc_root;
6764 root->reloc_root = NULL;
6765 list_add(&reloc_root->dead_list,
6766 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006767
6768 btrfs_set_root_bytenr(&reloc_root->root_item,
6769 reloc_root->node->start);
6770 btrfs_set_root_level(&root->root_item,
6771 btrfs_header_level(reloc_root->node));
6772 memset(&reloc_root->root_item.drop_progress, 0,
6773 sizeof(struct btrfs_disk_key));
6774 reloc_root->root_item.drop_level = 0;
6775
6776 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6777 &reloc_root->root_key,
6778 &reloc_root->root_item);
6779 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006780 }
6781 return 0;
6782}
6783
6784int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6785{
6786 struct btrfs_trans_handle *trans;
6787 struct btrfs_root *reloc_root;
6788 struct btrfs_root *prev_root = NULL;
6789 struct list_head dead_roots;
6790 int ret;
6791 unsigned long nr;
6792
6793 INIT_LIST_HEAD(&dead_roots);
6794 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6795
6796 while (!list_empty(&dead_roots)) {
6797 reloc_root = list_entry(dead_roots.prev,
6798 struct btrfs_root, dead_list);
6799 list_del_init(&reloc_root->dead_list);
6800
6801 BUG_ON(reloc_root->commit_root != NULL);
6802 while (1) {
6803 trans = btrfs_join_transaction(root, 1);
6804 BUG_ON(!trans);
6805
6806 mutex_lock(&root->fs_info->drop_mutex);
6807 ret = btrfs_drop_snapshot(trans, reloc_root);
6808 if (ret != -EAGAIN)
6809 break;
6810 mutex_unlock(&root->fs_info->drop_mutex);
6811
6812 nr = trans->blocks_used;
6813 ret = btrfs_end_transaction(trans, root);
6814 BUG_ON(ret);
6815 btrfs_btree_balance_dirty(root, nr);
6816 }
6817
6818 free_extent_buffer(reloc_root->node);
6819
6820 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6821 &reloc_root->root_key);
6822 BUG_ON(ret);
6823 mutex_unlock(&root->fs_info->drop_mutex);
6824
6825 nr = trans->blocks_used;
6826 ret = btrfs_end_transaction(trans, root);
6827 BUG_ON(ret);
6828 btrfs_btree_balance_dirty(root, nr);
6829
6830 kfree(prev_root);
6831 prev_root = reloc_root;
6832 }
6833 if (prev_root) {
6834 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6835 kfree(prev_root);
6836 }
6837 return 0;
6838}
6839
6840int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6841{
6842 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6843 return 0;
6844}
6845
6846int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6847{
6848 struct btrfs_root *reloc_root;
6849 struct btrfs_trans_handle *trans;
6850 struct btrfs_key location;
6851 int found;
6852 int ret;
6853
6854 mutex_lock(&root->fs_info->tree_reloc_mutex);
6855 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6856 BUG_ON(ret);
6857 found = !list_empty(&root->fs_info->dead_reloc_roots);
6858 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6859
6860 if (found) {
6861 trans = btrfs_start_transaction(root, 1);
6862 BUG_ON(!trans);
6863 ret = btrfs_commit_transaction(trans, root);
6864 BUG_ON(ret);
6865 }
6866
6867 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6868 location.offset = (u64)-1;
6869 location.type = BTRFS_ROOT_ITEM_KEY;
6870
6871 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6872 BUG_ON(!reloc_root);
6873 btrfs_orphan_cleanup(reloc_root);
6874 return 0;
6875}
6876
Chris Masond3977122009-01-05 21:25:51 -05006877static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006878 struct btrfs_root *root)
6879{
6880 struct btrfs_root *reloc_root;
6881 struct extent_buffer *eb;
6882 struct btrfs_root_item *root_item;
6883 struct btrfs_key root_key;
6884 int ret;
6885
6886 BUG_ON(!root->ref_cows);
6887 if (root->reloc_root)
6888 return 0;
6889
6890 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6891 BUG_ON(!root_item);
6892
6893 ret = btrfs_copy_root(trans, root, root->commit_root,
6894 &eb, BTRFS_TREE_RELOC_OBJECTID);
6895 BUG_ON(ret);
6896
6897 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6898 root_key.offset = root->root_key.objectid;
6899 root_key.type = BTRFS_ROOT_ITEM_KEY;
6900
6901 memcpy(root_item, &root->root_item, sizeof(root_item));
6902 btrfs_set_root_refs(root_item, 0);
6903 btrfs_set_root_bytenr(root_item, eb->start);
6904 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006905 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006906
6907 btrfs_tree_unlock(eb);
6908 free_extent_buffer(eb);
6909
6910 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6911 &root_key, root_item);
6912 BUG_ON(ret);
6913 kfree(root_item);
6914
6915 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6916 &root_key);
6917 BUG_ON(!reloc_root);
6918 reloc_root->last_trans = trans->transid;
6919 reloc_root->commit_root = NULL;
6920 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6921
6922 root->reloc_root = reloc_root;
6923 return 0;
6924}
6925
6926/*
6927 * Core function of space balance.
6928 *
6929 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006930 * counted roots. There is one reloc tree for each subvol, and all
6931 * reloc trees share same root key objectid. Reloc trees are snapshots
6932 * of the latest committed roots of subvols (root->commit_root).
6933 *
6934 * To relocate a tree block referenced by a subvol, there are two steps.
6935 * COW the block through subvol's reloc tree, then update block pointer
6936 * in the subvol to point to the new block. Since all reloc trees share
6937 * same root key objectid, doing special handing for tree blocks owned
6938 * by them is easy. Once a tree block has been COWed in one reloc tree,
6939 * we can use the resulting new block directly when the same block is
6940 * required to COW again through other reloc trees. By this way, relocated
6941 * tree blocks are shared between reloc trees, so they are also shared
6942 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006943 */
Chris Masond3977122009-01-05 21:25:51 -05006944static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006945 struct btrfs_root *root,
6946 struct btrfs_path *path,
6947 struct btrfs_key *first_key,
6948 struct btrfs_ref_path *ref_path,
6949 struct btrfs_block_group_cache *group,
6950 struct inode *reloc_inode)
6951{
6952 struct btrfs_root *reloc_root;
6953 struct extent_buffer *eb = NULL;
6954 struct btrfs_key *keys;
6955 u64 *nodes;
6956 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006957 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006958 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006959 int ret;
6960
6961 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6962 lowest_level = ref_path->owner_objectid;
6963
Yan Zhengf82d02d2008-10-29 14:49:05 -04006964 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006965 path->lowest_level = lowest_level;
6966 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6967 BUG_ON(ret < 0);
6968 path->lowest_level = 0;
6969 btrfs_release_path(root, path);
6970 return 0;
6971 }
6972
Zheng Yan1a40e232008-09-26 10:09:34 -04006973 mutex_lock(&root->fs_info->tree_reloc_mutex);
6974 ret = init_reloc_tree(trans, root);
6975 BUG_ON(ret);
6976 reloc_root = root->reloc_root;
6977
Yan Zhengf82d02d2008-10-29 14:49:05 -04006978 shared_level = ref_path->shared_level;
6979 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006980
Yan Zhengf82d02d2008-10-29 14:49:05 -04006981 keys = ref_path->node_keys;
6982 nodes = ref_path->new_nodes;
6983 memset(&keys[shared_level + 1], 0,
6984 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6985 memset(&nodes[shared_level + 1], 0,
6986 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006987
Yan Zhengf82d02d2008-10-29 14:49:05 -04006988 if (nodes[lowest_level] == 0) {
6989 path->lowest_level = lowest_level;
6990 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6991 0, 1);
6992 BUG_ON(ret);
6993 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6994 eb = path->nodes[level];
6995 if (!eb || eb == reloc_root->node)
6996 break;
6997 nodes[level] = eb->start;
6998 if (level == 0)
6999 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7000 else
7001 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7002 }
Yan Zheng2b820322008-11-17 21:11:30 -05007003 if (nodes[0] &&
7004 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007005 eb = path->nodes[0];
7006 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7007 group, reloc_inode);
7008 BUG_ON(ret);
7009 }
7010 btrfs_release_path(reloc_root, path);
7011 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007012 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007013 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007014 BUG_ON(ret);
7015 }
7016
Zheng Yan1a40e232008-09-26 10:09:34 -04007017 /*
7018 * replace tree blocks in the fs tree with tree blocks in
7019 * the reloc tree.
7020 */
7021 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7022 BUG_ON(ret < 0);
7023
7024 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007025 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7026 0, 0);
7027 BUG_ON(ret);
7028 extent_buffer_get(path->nodes[0]);
7029 eb = path->nodes[0];
7030 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007031 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7032 BUG_ON(ret);
7033 free_extent_buffer(eb);
7034 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007035
Yan Zhengf82d02d2008-10-29 14:49:05 -04007036 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007037 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007038 return 0;
7039}
7040
Chris Masond3977122009-01-05 21:25:51 -05007041static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007042 struct btrfs_root *root,
7043 struct btrfs_path *path,
7044 struct btrfs_key *first_key,
7045 struct btrfs_ref_path *ref_path)
7046{
7047 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007048
7049 ret = relocate_one_path(trans, root, path, first_key,
7050 ref_path, NULL, NULL);
7051 BUG_ON(ret);
7052
Zheng Yan1a40e232008-09-26 10:09:34 -04007053 return 0;
7054}
7055
Chris Masond3977122009-01-05 21:25:51 -05007056static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007057 struct btrfs_root *extent_root,
7058 struct btrfs_path *path,
7059 struct btrfs_key *extent_key)
7060{
7061 int ret;
7062
Zheng Yan1a40e232008-09-26 10:09:34 -04007063 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7064 if (ret)
7065 goto out;
7066 ret = btrfs_del_item(trans, extent_root, path);
7067out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007068 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007069 return ret;
7070}
7071
Chris Masond3977122009-01-05 21:25:51 -05007072static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007073 struct btrfs_ref_path *ref_path)
7074{
7075 struct btrfs_key root_key;
7076
7077 root_key.objectid = ref_path->root_objectid;
7078 root_key.type = BTRFS_ROOT_ITEM_KEY;
7079 if (is_cowonly_root(ref_path->root_objectid))
7080 root_key.offset = 0;
7081 else
7082 root_key.offset = (u64)-1;
7083
7084 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7085}
7086
Chris Masond3977122009-01-05 21:25:51 -05007087static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007088 struct btrfs_path *path,
7089 struct btrfs_key *extent_key,
7090 struct btrfs_block_group_cache *group,
7091 struct inode *reloc_inode, int pass)
7092{
7093 struct btrfs_trans_handle *trans;
7094 struct btrfs_root *found_root;
7095 struct btrfs_ref_path *ref_path = NULL;
7096 struct disk_extent *new_extents = NULL;
7097 int nr_extents = 0;
7098 int loops;
7099 int ret;
7100 int level;
7101 struct btrfs_key first_key;
7102 u64 prev_block = 0;
7103
Zheng Yan1a40e232008-09-26 10:09:34 -04007104
7105 trans = btrfs_start_transaction(extent_root, 1);
7106 BUG_ON(!trans);
7107
7108 if (extent_key->objectid == 0) {
7109 ret = del_extent_zero(trans, extent_root, path, extent_key);
7110 goto out;
7111 }
7112
7113 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7114 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007115 ret = -ENOMEM;
7116 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007117 }
7118
7119 for (loops = 0; ; loops++) {
7120 if (loops == 0) {
7121 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7122 extent_key->objectid);
7123 } else {
7124 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7125 }
7126 if (ret < 0)
7127 goto out;
7128 if (ret > 0)
7129 break;
7130
7131 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7132 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7133 continue;
7134
7135 found_root = read_ref_root(extent_root->fs_info, ref_path);
7136 BUG_ON(!found_root);
7137 /*
7138 * for reference counted tree, only process reference paths
7139 * rooted at the latest committed root.
7140 */
7141 if (found_root->ref_cows &&
7142 ref_path->root_generation != found_root->root_key.offset)
7143 continue;
7144
7145 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7146 if (pass == 0) {
7147 /*
7148 * copy data extents to new locations
7149 */
7150 u64 group_start = group->key.objectid;
7151 ret = relocate_data_extent(reloc_inode,
7152 extent_key,
7153 group_start);
7154 if (ret < 0)
7155 goto out;
7156 break;
7157 }
7158 level = 0;
7159 } else {
7160 level = ref_path->owner_objectid;
7161 }
7162
7163 if (prev_block != ref_path->nodes[level]) {
7164 struct extent_buffer *eb;
7165 u64 block_start = ref_path->nodes[level];
7166 u64 block_size = btrfs_level_size(found_root, level);
7167
7168 eb = read_tree_block(found_root, block_start,
7169 block_size, 0);
7170 btrfs_tree_lock(eb);
7171 BUG_ON(level != btrfs_header_level(eb));
7172
7173 if (level == 0)
7174 btrfs_item_key_to_cpu(eb, &first_key, 0);
7175 else
7176 btrfs_node_key_to_cpu(eb, &first_key, 0);
7177
7178 btrfs_tree_unlock(eb);
7179 free_extent_buffer(eb);
7180 prev_block = block_start;
7181 }
7182
Yan Zheng24562422009-02-12 14:14:53 -05007183 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007184 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05007185 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007186 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7187 /*
7188 * try to update data extent references while
7189 * keeping metadata shared between snapshots.
7190 */
7191 if (pass == 1) {
7192 ret = relocate_one_path(trans, found_root,
7193 path, &first_key, ref_path,
7194 group, reloc_inode);
7195 if (ret < 0)
7196 goto out;
7197 continue;
7198 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007199 /*
7200 * use fallback method to process the remaining
7201 * references.
7202 */
7203 if (!new_extents) {
7204 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04007205 new_extents = kmalloc(sizeof(*new_extents),
7206 GFP_NOFS);
7207 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007208 ret = get_new_locations(reloc_inode,
7209 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04007210 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04007211 &new_extents,
7212 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007213 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04007214 goto out;
7215 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007216 ret = replace_one_extent(trans, found_root,
7217 path, extent_key,
7218 &first_key, ref_path,
7219 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05007220 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007221 ret = relocate_tree_block(trans, found_root, path,
7222 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007223 }
7224 if (ret < 0)
7225 goto out;
7226 }
7227 ret = 0;
7228out:
7229 btrfs_end_transaction(trans, extent_root);
7230 kfree(new_extents);
7231 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007232 return ret;
7233}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007234#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007235
Chris Masonec44a352008-04-28 15:29:52 -04007236static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7237{
7238 u64 num_devices;
7239 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7240 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7241
Yan Zheng2b820322008-11-17 21:11:30 -05007242 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04007243 if (num_devices == 1) {
7244 stripped |= BTRFS_BLOCK_GROUP_DUP;
7245 stripped = flags & ~stripped;
7246
7247 /* turn raid0 into single device chunks */
7248 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7249 return stripped;
7250
7251 /* turn mirroring into duplication */
7252 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7253 BTRFS_BLOCK_GROUP_RAID10))
7254 return stripped | BTRFS_BLOCK_GROUP_DUP;
7255 return flags;
7256 } else {
7257 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007258 if (flags & stripped)
7259 return flags;
7260
7261 stripped |= BTRFS_BLOCK_GROUP_DUP;
7262 stripped = flags & ~stripped;
7263
7264 /* switch duplicated blocks with raid1 */
7265 if (flags & BTRFS_BLOCK_GROUP_DUP)
7266 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7267
7268 /* turn single device chunks into raid0 */
7269 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7270 }
7271 return flags;
7272}
7273
Christoph Hellwigb2950862008-12-02 09:54:17 -05007274static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04007275 struct btrfs_block_group_cache *shrink_block_group,
7276 int force)
7277{
7278 struct btrfs_trans_handle *trans;
7279 u64 new_alloc_flags;
7280 u64 calc;
7281
Chris Masonc286ac42008-07-22 23:06:41 -04007282 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007283 if (btrfs_block_group_used(&shrink_block_group->item) +
7284 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04007285 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04007286
Chris Mason0ef3e662008-05-24 14:04:53 -04007287 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007288 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04007289
Chris Mason0ef3e662008-05-24 14:04:53 -04007290 new_alloc_flags = update_block_group_flags(root,
7291 shrink_block_group->flags);
7292 if (new_alloc_flags != shrink_block_group->flags) {
7293 calc =
7294 btrfs_block_group_used(&shrink_block_group->item);
7295 } else {
7296 calc = shrink_block_group->key.offset;
7297 }
Chris Masonc286ac42008-07-22 23:06:41 -04007298 spin_unlock(&shrink_block_group->lock);
7299
Chris Mason0ef3e662008-05-24 14:04:53 -04007300 do_chunk_alloc(trans, root->fs_info->extent_root,
7301 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04007302
Chris Mason0ef3e662008-05-24 14:04:53 -04007303 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04007304 } else
7305 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04007306 return 0;
7307}
7308
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007309
7310int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
7311 struct btrfs_block_group_cache *group)
7312
7313{
7314 __alloc_chunk_for_shrink(root, group, 1);
7315 set_block_group_readonly(group);
7316 return 0;
7317}
7318
Josef Bacikba1bf482009-09-11 16:11:19 -04007319/*
7320 * checks to see if its even possible to relocate this block group.
7321 *
7322 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7323 * ok to go ahead and try.
7324 */
7325int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007326{
Zheng Yan1a40e232008-09-26 10:09:34 -04007327 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007328 struct btrfs_space_info *space_info;
7329 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7330 struct btrfs_device *device;
7331 int full = 0;
7332 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007333
Josef Bacikba1bf482009-09-11 16:11:19 -04007334 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007335
Josef Bacikba1bf482009-09-11 16:11:19 -04007336 /* odd, couldn't find the block group, leave it alone */
7337 if (!block_group)
7338 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007339
Josef Bacikba1bf482009-09-11 16:11:19 -04007340 /* no bytes used, we're good */
7341 if (!btrfs_block_group_used(&block_group->item))
7342 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007343
Josef Bacikba1bf482009-09-11 16:11:19 -04007344 space_info = block_group->space_info;
7345 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007346
Josef Bacikba1bf482009-09-11 16:11:19 -04007347 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007348
Josef Bacikba1bf482009-09-11 16:11:19 -04007349 /*
7350 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007351 * relocate it unless we're able to allocate a new chunk below.
7352 *
7353 * Otherwise, we need to make sure we have room in the space to handle
7354 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007355 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007356 if ((space_info->total_bytes != block_group->key.offset) &&
7357 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04007358 space_info->bytes_pinned + space_info->bytes_readonly +
7359 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04007360 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007361 spin_unlock(&space_info->lock);
7362 goto out;
7363 }
7364 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007365
Josef Bacikba1bf482009-09-11 16:11:19 -04007366 /*
7367 * ok we don't have enough space, but maybe we have free space on our
7368 * devices to allocate new chunks for relocation, so loop through our
7369 * alloc devices and guess if we have enough space. However, if we
7370 * were marked as full, then we know there aren't enough chunks, and we
7371 * can just return.
7372 */
7373 ret = -1;
7374 if (full)
7375 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05007376
Josef Bacikba1bf482009-09-11 16:11:19 -04007377 mutex_lock(&root->fs_info->chunk_mutex);
7378 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7379 u64 min_free = btrfs_block_group_used(&block_group->item);
7380 u64 dev_offset, max_avail;
Chris Masonea8c2812008-08-04 23:17:27 -04007381
Josef Bacikba1bf482009-09-11 16:11:19 -04007382 /*
7383 * check to make sure we can actually find a chunk with enough
7384 * space to fit our block group in.
7385 */
7386 if (device->total_bytes > device->bytes_used + min_free) {
7387 ret = find_free_dev_extent(NULL, device, min_free,
7388 &dev_offset, &max_avail);
7389 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05007390 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04007391 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007392 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007393 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007394 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007395out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007396 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007397 return ret;
7398}
7399
Christoph Hellwigb2950862008-12-02 09:54:17 -05007400static int find_first_block_group(struct btrfs_root *root,
7401 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007402{
Chris Mason925baed2008-06-25 16:01:30 -04007403 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007404 struct btrfs_key found_key;
7405 struct extent_buffer *leaf;
7406 int slot;
7407
7408 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7409 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007410 goto out;
7411
Chris Masond3977122009-01-05 21:25:51 -05007412 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007413 slot = path->slots[0];
7414 leaf = path->nodes[0];
7415 if (slot >= btrfs_header_nritems(leaf)) {
7416 ret = btrfs_next_leaf(root, path);
7417 if (ret == 0)
7418 continue;
7419 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007420 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007421 break;
7422 }
7423 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7424
7425 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007426 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7427 ret = 0;
7428 goto out;
7429 }
Chris Mason0b86a832008-03-24 15:01:56 -04007430 path->slots[0]++;
7431 }
Chris Mason925baed2008-06-25 16:01:30 -04007432out:
Chris Mason0b86a832008-03-24 15:01:56 -04007433 return ret;
7434}
7435
Zheng Yan1a40e232008-09-26 10:09:34 -04007436int btrfs_free_block_groups(struct btrfs_fs_info *info)
7437{
7438 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007439 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007440 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007441 struct rb_node *n;
7442
Yan Zheng11833d62009-09-11 16:11:19 -04007443 down_write(&info->extent_commit_sem);
7444 while (!list_empty(&info->caching_block_groups)) {
7445 caching_ctl = list_entry(info->caching_block_groups.next,
7446 struct btrfs_caching_control, list);
7447 list_del(&caching_ctl->list);
7448 put_caching_control(caching_ctl);
7449 }
7450 up_write(&info->extent_commit_sem);
7451
Zheng Yan1a40e232008-09-26 10:09:34 -04007452 spin_lock(&info->block_group_cache_lock);
7453 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7454 block_group = rb_entry(n, struct btrfs_block_group_cache,
7455 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007456 rb_erase(&block_group->cache_node,
7457 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007458 spin_unlock(&info->block_group_cache_lock);
7459
Josef Bacik80eb2342008-10-29 14:49:05 -04007460 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007461 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007462 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007463
Josef Bacik817d52f2009-07-13 21:29:25 -04007464 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007465 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007466
7467 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007468 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007469
7470 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007471 }
7472 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007473
7474 /* now that all the block groups are freed, go through and
7475 * free all the space_info structs. This is only called during
7476 * the final stages of unmount, and so we know nobody is
7477 * using them. We call synchronize_rcu() once before we start,
7478 * just to be on the safe side.
7479 */
7480 synchronize_rcu();
7481
7482 while(!list_empty(&info->space_info)) {
7483 space_info = list_entry(info->space_info.next,
7484 struct btrfs_space_info,
7485 list);
7486
7487 list_del(&space_info->list);
7488 kfree(space_info);
7489 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007490 return 0;
7491}
7492
Yan, Zhengb742bb82010-05-16 10:46:24 -04007493static void __link_block_group(struct btrfs_space_info *space_info,
7494 struct btrfs_block_group_cache *cache)
7495{
7496 int index = get_block_group_index(cache);
7497
7498 down_write(&space_info->groups_sem);
7499 list_add_tail(&cache->list, &space_info->block_groups[index]);
7500 up_write(&space_info->groups_sem);
7501}
7502
Chris Mason9078a3e2007-04-26 16:46:15 -04007503int btrfs_read_block_groups(struct btrfs_root *root)
7504{
7505 struct btrfs_path *path;
7506 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007507 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007508 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007509 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007510 struct btrfs_key key;
7511 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007512 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007513
Chris Masonbe744172007-05-06 10:15:01 -04007514 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007515 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007516 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007517 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007518 path = btrfs_alloc_path();
7519 if (!path)
7520 return -ENOMEM;
7521
Chris Masond3977122009-01-05 21:25:51 -05007522 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007523 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007524 if (ret > 0)
7525 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007526 if (ret != 0)
7527 goto error;
7528
Chris Mason5f39d392007-10-15 16:14:19 -04007529 leaf = path->nodes[0];
7530 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007531 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007532 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007533 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04007534 break;
7535 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007536
Yan Zhengd2fb3432008-12-11 16:30:39 -05007537 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007538 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007539 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007540 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007541 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007542 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007543
7544 /*
7545 * we only want to have 32k of ram per block group for keeping
7546 * track of free space, and if we pass 1/2 of that we want to
7547 * start converting things over to using bitmaps
7548 */
7549 cache->extents_thresh = ((1024 * 32) / 2) /
7550 sizeof(struct btrfs_free_space);
7551
Chris Mason5f39d392007-10-15 16:14:19 -04007552 read_extent_buffer(leaf, &cache->item,
7553 btrfs_item_ptr_offset(leaf, path->slots[0]),
7554 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007555 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007556
Chris Mason9078a3e2007-04-26 16:46:15 -04007557 key.objectid = found_key.objectid + found_key.offset;
7558 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007559 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007560 cache->sectorsize = root->sectorsize;
7561
Josef Bacik817d52f2009-07-13 21:29:25 -04007562 /*
7563 * check for two cases, either we are full, and therefore
7564 * don't need to bother with the caching work since we won't
7565 * find any space, or we are empty, and we can just add all
7566 * the space in and be done with it. This saves us _alot_ of
7567 * time, particularly in the full case.
7568 */
7569 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04007570 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04007571 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007572 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007573 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007574 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007575 exclude_super_stripes(root, cache);
7576 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007577 cache->cached = BTRFS_CACHE_FINISHED;
7578 add_new_free_space(cache, root->fs_info,
7579 found_key.objectid,
7580 found_key.objectid +
7581 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007582 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007583 }
Chris Mason96b51792007-10-15 16:15:19 -04007584
Chris Mason6324fbf2008-03-24 15:01:59 -04007585 ret = update_space_info(info, cache->flags, found_key.offset,
7586 btrfs_block_group_used(&cache->item),
7587 &space_info);
7588 BUG_ON(ret);
7589 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007590 spin_lock(&cache->space_info->lock);
7591 cache->space_info->bytes_super += cache->bytes_super;
7592 spin_unlock(&cache->space_info->lock);
7593
Yan, Zhengb742bb82010-05-16 10:46:24 -04007594 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007595
Josef Bacik0f9dd462008-09-23 13:14:11 -04007596 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7597 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007598
7599 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007600 if (btrfs_chunk_readonly(root, cache->key.objectid))
7601 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007602 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007603
7604 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7605 if (!(get_alloc_profile(root, space_info->flags) &
7606 (BTRFS_BLOCK_GROUP_RAID10 |
7607 BTRFS_BLOCK_GROUP_RAID1 |
7608 BTRFS_BLOCK_GROUP_DUP)))
7609 continue;
7610 /*
7611 * avoid allocating from un-mirrored block group if there are
7612 * mirrored block groups.
7613 */
7614 list_for_each_entry(cache, &space_info->block_groups[3], list)
7615 set_block_group_readonly(cache);
7616 list_for_each_entry(cache, &space_info->block_groups[4], list)
7617 set_block_group_readonly(cache);
7618 }
Chris Mason0b86a832008-03-24 15:01:56 -04007619 ret = 0;
7620error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007621 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007622 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007623}
Chris Mason6324fbf2008-03-24 15:01:59 -04007624
7625int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7626 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007627 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007628 u64 size)
7629{
7630 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007631 struct btrfs_root *extent_root;
7632 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007633
7634 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007635
Chris Mason12fcfd22009-03-24 10:24:20 -04007636 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007637
Chris Mason8f18cf12008-04-25 16:53:30 -04007638 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007639 if (!cache)
7640 return -ENOMEM;
7641
Chris Masone17cade2008-04-15 15:41:47 -04007642 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007643 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007644 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007645 cache->sectorsize = root->sectorsize;
7646
7647 /*
7648 * we only want to have 32k of ram per block group for keeping track
7649 * of free space, and if we pass 1/2 of that we want to start
7650 * converting things over to using bitmaps
7651 */
7652 cache->extents_thresh = ((1024 * 32) / 2) /
7653 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007654 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007655 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007656 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007657 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007658 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007659
Chris Mason6324fbf2008-03-24 15:01:59 -04007660 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007661 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7662 cache->flags = type;
7663 btrfs_set_block_group_flags(&cache->item, type);
7664
Yan Zheng11833d62009-09-11 16:11:19 -04007665 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007666 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007667 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007668
Josef Bacik817d52f2009-07-13 21:29:25 -04007669 add_new_free_space(cache, root->fs_info, chunk_offset,
7670 chunk_offset + size);
7671
Yan Zheng11833d62009-09-11 16:11:19 -04007672 free_excluded_extents(root, cache);
7673
Chris Mason6324fbf2008-03-24 15:01:59 -04007674 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7675 &cache->space_info);
7676 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007677
7678 spin_lock(&cache->space_info->lock);
7679 cache->space_info->bytes_super += cache->bytes_super;
7680 spin_unlock(&cache->space_info->lock);
7681
Yan, Zhengb742bb82010-05-16 10:46:24 -04007682 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007683
Josef Bacik0f9dd462008-09-23 13:14:11 -04007684 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7685 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007686
Chris Mason6324fbf2008-03-24 15:01:59 -04007687 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7688 sizeof(cache->item));
7689 BUG_ON(ret);
7690
Chris Masond18a2c42008-04-04 15:40:00 -04007691 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007692
Chris Mason6324fbf2008-03-24 15:01:59 -04007693 return 0;
7694}
Zheng Yan1a40e232008-09-26 10:09:34 -04007695
7696int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7697 struct btrfs_root *root, u64 group_start)
7698{
7699 struct btrfs_path *path;
7700 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007701 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007702 struct btrfs_key key;
7703 int ret;
7704
Zheng Yan1a40e232008-09-26 10:09:34 -04007705 root = root->fs_info->extent_root;
7706
7707 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7708 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007709 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007710
7711 memcpy(&key, &block_group->key, sizeof(key));
7712
Chris Mason44fb5512009-06-04 15:34:51 -04007713 /* make sure this block group isn't part of an allocation cluster */
7714 cluster = &root->fs_info->data_alloc_cluster;
7715 spin_lock(&cluster->refill_lock);
7716 btrfs_return_cluster_to_free_space(block_group, cluster);
7717 spin_unlock(&cluster->refill_lock);
7718
7719 /*
7720 * make sure this block group isn't part of a metadata
7721 * allocation cluster
7722 */
7723 cluster = &root->fs_info->meta_alloc_cluster;
7724 spin_lock(&cluster->refill_lock);
7725 btrfs_return_cluster_to_free_space(block_group, cluster);
7726 spin_unlock(&cluster->refill_lock);
7727
Zheng Yan1a40e232008-09-26 10:09:34 -04007728 path = btrfs_alloc_path();
7729 BUG_ON(!path);
7730
Yan Zheng3dfdb932009-01-21 10:49:16 -05007731 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007732 rb_erase(&block_group->cache_node,
7733 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007734 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007735
Josef Bacik80eb2342008-10-29 14:49:05 -04007736 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007737 /*
7738 * we must use list_del_init so people can check to see if they
7739 * are still on the list after taking the semaphore
7740 */
7741 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007742 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007743
Josef Bacik817d52f2009-07-13 21:29:25 -04007744 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007745 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007746
7747 btrfs_remove_free_space_cache(block_group);
7748
Yan Zhengc146afa2008-11-12 14:34:12 -05007749 spin_lock(&block_group->space_info->lock);
7750 block_group->space_info->total_bytes -= block_group->key.offset;
7751 block_group->space_info->bytes_readonly -= block_group->key.offset;
7752 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007753
7754 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007755
Chris Masonfa9c0d72009-04-03 09:47:43 -04007756 btrfs_put_block_group(block_group);
7757 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007758
7759 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7760 if (ret > 0)
7761 ret = -EIO;
7762 if (ret < 0)
7763 goto out;
7764
7765 ret = btrfs_del_item(trans, root, path);
7766out:
7767 btrfs_free_path(path);
7768 return ret;
7769}