blob: fa2907d531a2fb7a3cb0486fb53fe18fc290ed7b [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 Masonfa9c0d792009-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);
Josef Bacik6a632092009-02-20 11:00:09 -050074
Josef Bacik817d52f2009-07-13 21:29:25 -040075static noinline int
76block_group_cache_done(struct btrfs_block_group_cache *cache)
77{
78 smp_mb();
79 return cache->cached == BTRFS_CACHE_FINISHED;
80}
81
Josef Bacik0f9dd462008-09-23 13:14:11 -040082static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
83{
84 return (cache->flags & bits) == bits;
85}
86
Josef Bacik11dfe352009-11-13 20:12:59 +000087void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
88{
89 atomic_inc(&cache->count);
90}
91
92void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
93{
94 if (atomic_dec_and_test(&cache->count))
95 kfree(cache);
96}
97
Josef Bacik0f9dd462008-09-23 13:14:11 -040098/*
99 * this adds the block group to the fs_info rb tree for the block group
100 * cache
101 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500102static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400103 struct btrfs_block_group_cache *block_group)
104{
105 struct rb_node **p;
106 struct rb_node *parent = NULL;
107 struct btrfs_block_group_cache *cache;
108
109 spin_lock(&info->block_group_cache_lock);
110 p = &info->block_group_cache_tree.rb_node;
111
112 while (*p) {
113 parent = *p;
114 cache = rb_entry(parent, struct btrfs_block_group_cache,
115 cache_node);
116 if (block_group->key.objectid < cache->key.objectid) {
117 p = &(*p)->rb_left;
118 } else if (block_group->key.objectid > cache->key.objectid) {
119 p = &(*p)->rb_right;
120 } else {
121 spin_unlock(&info->block_group_cache_lock);
122 return -EEXIST;
123 }
124 }
125
126 rb_link_node(&block_group->cache_node, parent, p);
127 rb_insert_color(&block_group->cache_node,
128 &info->block_group_cache_tree);
129 spin_unlock(&info->block_group_cache_lock);
130
131 return 0;
132}
133
134/*
135 * This will return the block group at or after bytenr if contains is 0, else
136 * it will return the block group that contains the bytenr
137 */
138static struct btrfs_block_group_cache *
139block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
140 int contains)
141{
142 struct btrfs_block_group_cache *cache, *ret = NULL;
143 struct rb_node *n;
144 u64 end, start;
145
146 spin_lock(&info->block_group_cache_lock);
147 n = info->block_group_cache_tree.rb_node;
148
149 while (n) {
150 cache = rb_entry(n, struct btrfs_block_group_cache,
151 cache_node);
152 end = cache->key.objectid + cache->key.offset - 1;
153 start = cache->key.objectid;
154
155 if (bytenr < start) {
156 if (!contains && (!ret || start < ret->key.objectid))
157 ret = cache;
158 n = n->rb_left;
159 } else if (bytenr > start) {
160 if (contains && bytenr <= end) {
161 ret = cache;
162 break;
163 }
164 n = n->rb_right;
165 } else {
166 ret = cache;
167 break;
168 }
169 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500170 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000171 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400172 spin_unlock(&info->block_group_cache_lock);
173
174 return ret;
175}
176
Yan Zheng11833d62009-09-11 16:11:19 -0400177static int add_excluded_extent(struct btrfs_root *root,
178 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400179{
Yan Zheng11833d62009-09-11 16:11:19 -0400180 u64 end = start + num_bytes - 1;
181 set_extent_bits(&root->fs_info->freed_extents[0],
182 start, end, EXTENT_UPTODATE, GFP_NOFS);
183 set_extent_bits(&root->fs_info->freed_extents[1],
184 start, end, EXTENT_UPTODATE, GFP_NOFS);
185 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400186}
187
Yan Zheng11833d62009-09-11 16:11:19 -0400188static void free_excluded_extents(struct btrfs_root *root,
189 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400190{
Yan Zheng11833d62009-09-11 16:11:19 -0400191 u64 start, end;
192
193 start = cache->key.objectid;
194 end = start + cache->key.offset - 1;
195
196 clear_extent_bits(&root->fs_info->freed_extents[0],
197 start, end, EXTENT_UPTODATE, GFP_NOFS);
198 clear_extent_bits(&root->fs_info->freed_extents[1],
199 start, end, EXTENT_UPTODATE, GFP_NOFS);
200}
201
202static int exclude_super_stripes(struct btrfs_root *root,
203 struct btrfs_block_group_cache *cache)
204{
Josef Bacik817d52f2009-07-13 21:29:25 -0400205 u64 bytenr;
206 u64 *logical;
207 int stripe_len;
208 int i, nr, ret;
209
Yan, Zheng06b23312009-11-26 09:31:11 +0000210 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
211 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
212 cache->bytes_super += stripe_len;
213 ret = add_excluded_extent(root, cache->key.objectid,
214 stripe_len);
215 BUG_ON(ret);
216 }
217
Josef Bacik817d52f2009-07-13 21:29:25 -0400218 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
219 bytenr = btrfs_sb_offset(i);
220 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
221 cache->key.objectid, bytenr,
222 0, &logical, &nr, &stripe_len);
223 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400224
Josef Bacik817d52f2009-07-13 21:29:25 -0400225 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400226 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400227 ret = add_excluded_extent(root, logical[nr],
228 stripe_len);
229 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400230 }
Yan Zheng11833d62009-09-11 16:11:19 -0400231
Josef Bacik817d52f2009-07-13 21:29:25 -0400232 kfree(logical);
233 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400234 return 0;
235}
236
Yan Zheng11833d62009-09-11 16:11:19 -0400237static struct btrfs_caching_control *
238get_caching_control(struct btrfs_block_group_cache *cache)
239{
240 struct btrfs_caching_control *ctl;
241
242 spin_lock(&cache->lock);
243 if (cache->cached != BTRFS_CACHE_STARTED) {
244 spin_unlock(&cache->lock);
245 return NULL;
246 }
247
248 ctl = cache->caching_ctl;
249 atomic_inc(&ctl->count);
250 spin_unlock(&cache->lock);
251 return ctl;
252}
253
254static void put_caching_control(struct btrfs_caching_control *ctl)
255{
256 if (atomic_dec_and_test(&ctl->count))
257 kfree(ctl);
258}
259
Josef Bacik0f9dd462008-09-23 13:14:11 -0400260/*
261 * this is only called by cache_block_group, since we could have freed extents
262 * we need to check the pinned_extents for any extents that can't be used yet
263 * since their free space will be released as soon as the transaction commits.
264 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400265static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400266 struct btrfs_fs_info *info, u64 start, u64 end)
267{
Josef Bacik817d52f2009-07-13 21:29:25 -0400268 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400269 int ret;
270
271 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400272 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400273 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400274 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400275 if (ret)
276 break;
277
Yan, Zheng06b23312009-11-26 09:31:11 +0000278 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400279 start = extent_end + 1;
280 } else if (extent_start > start && extent_start < end) {
281 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400282 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500283 ret = btrfs_add_free_space(block_group, start,
284 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400285 BUG_ON(ret);
286 start = extent_end + 1;
287 } else {
288 break;
289 }
290 }
291
292 if (start < end) {
293 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400294 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500295 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400296 BUG_ON(ret);
297 }
298
Josef Bacik817d52f2009-07-13 21:29:25 -0400299 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300}
301
Josef Bacik817d52f2009-07-13 21:29:25 -0400302static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400303{
Josef Bacik817d52f2009-07-13 21:29:25 -0400304 struct btrfs_block_group_cache *block_group = data;
305 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400306 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
307 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400308 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400309 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400310 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400311 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400312 u64 last = 0;
313 u32 nritems;
314 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400315
Chris Masone37c9e62007-05-09 20:13:14 -0400316 path = btrfs_alloc_path();
317 if (!path)
318 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400319
Yan Zheng11833d62009-09-11 16:11:19 -0400320 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400321 spin_lock(&block_group->space_info->lock);
322 block_group->space_info->bytes_super += block_group->bytes_super;
323 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400324
Josef Bacik817d52f2009-07-13 21:29:25 -0400325 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400326
Chris Mason5cd57b22008-06-25 16:01:30 -0400327 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400328 * We don't want to deadlock with somebody trying to allocate a new
329 * extent for the extent root while also trying to search the extent
330 * root to add free space. So we skip locking and search the commit
331 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400332 */
333 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400334 path->search_commit_root = 1;
335 path->reada = 2;
336
Yan Zhenge4404d62008-12-12 10:03:26 -0500337 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400338 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400339 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400340again:
Yan Zheng11833d62009-09-11 16:11:19 -0400341 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400342 /* need to make sure the commit_root doesn't disappear */
343 down_read(&fs_info->extent_commit_sem);
344
Yan Zheng11833d62009-09-11 16:11:19 -0400345 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400346 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400347 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500348
Yan Zheng11833d62009-09-11 16:11:19 -0400349 leaf = path->nodes[0];
350 nritems = btrfs_header_nritems(leaf);
351
Chris Masond3977122009-01-05 21:25:51 -0500352 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400353 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400354 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400355 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400356 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400357 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400358
Yan Zheng11833d62009-09-11 16:11:19 -0400359 if (path->slots[0] < nritems) {
360 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
361 } else {
362 ret = find_next_key(path, 0, &key);
363 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400364 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400365
Yan Zheng11833d62009-09-11 16:11:19 -0400366 caching_ctl->progress = last;
367 btrfs_release_path(extent_root, path);
368 up_read(&fs_info->extent_commit_sem);
369 mutex_unlock(&caching_ctl->mutex);
370 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400371 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400372 else
373 cond_resched();
374 goto again;
375 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400376
Yan Zheng11833d62009-09-11 16:11:19 -0400377 if (key.objectid < block_group->key.objectid) {
378 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400379 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400380 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400381
Chris Masone37c9e62007-05-09 20:13:14 -0400382 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400383 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400384 break;
Yan7d7d6062007-09-14 16:15:28 -0400385
Yan Zheng11833d62009-09-11 16:11:19 -0400386 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400387 total_found += add_new_free_space(block_group,
388 fs_info, last,
389 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400390 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400391
Yan Zheng11833d62009-09-11 16:11:19 -0400392 if (total_found > (1024 * 1024 * 2)) {
393 total_found = 0;
394 wake_up(&caching_ctl->wait);
395 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400396 }
Chris Masone37c9e62007-05-09 20:13:14 -0400397 path->slots[0]++;
398 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400399 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400400
401 total_found += add_new_free_space(block_group, fs_info, last,
402 block_group->key.objectid +
403 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400404 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400405
406 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400407 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400408 block_group->cached = BTRFS_CACHE_FINISHED;
409 spin_unlock(&block_group->lock);
410
Chris Mason54aa1f42007-06-22 14:16:25 -0400411err:
Chris Masone37c9e62007-05-09 20:13:14 -0400412 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400413 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400414
Yan Zheng11833d62009-09-11 16:11:19 -0400415 free_excluded_extents(extent_root, block_group);
416
417 mutex_unlock(&caching_ctl->mutex);
418 wake_up(&caching_ctl->wait);
419
420 put_caching_control(caching_ctl);
421 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000422 btrfs_put_block_group(block_group);
423
Josef Bacik817d52f2009-07-13 21:29:25 -0400424 return 0;
425}
426
427static int cache_block_group(struct btrfs_block_group_cache *cache)
428{
Yan Zheng11833d62009-09-11 16:11:19 -0400429 struct btrfs_fs_info *fs_info = cache->fs_info;
430 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400431 struct task_struct *tsk;
432 int ret = 0;
433
Yan Zheng11833d62009-09-11 16:11:19 -0400434 smp_mb();
435 if (cache->cached != BTRFS_CACHE_NO)
436 return 0;
437
438 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
439 BUG_ON(!caching_ctl);
440
441 INIT_LIST_HEAD(&caching_ctl->list);
442 mutex_init(&caching_ctl->mutex);
443 init_waitqueue_head(&caching_ctl->wait);
444 caching_ctl->block_group = cache;
445 caching_ctl->progress = cache->key.objectid;
446 /* one for caching kthread, one for caching block group list */
447 atomic_set(&caching_ctl->count, 2);
448
Josef Bacik817d52f2009-07-13 21:29:25 -0400449 spin_lock(&cache->lock);
450 if (cache->cached != BTRFS_CACHE_NO) {
451 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400452 kfree(caching_ctl);
453 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400454 }
Yan Zheng11833d62009-09-11 16:11:19 -0400455 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400456 cache->cached = BTRFS_CACHE_STARTED;
457 spin_unlock(&cache->lock);
458
Yan Zheng11833d62009-09-11 16:11:19 -0400459 down_write(&fs_info->extent_commit_sem);
460 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
461 up_write(&fs_info->extent_commit_sem);
462
463 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000464 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400465
Josef Bacik817d52f2009-07-13 21:29:25 -0400466 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
467 cache->key.objectid);
468 if (IS_ERR(tsk)) {
469 ret = PTR_ERR(tsk);
470 printk(KERN_ERR "error running thread %d\n", ret);
471 BUG();
472 }
473
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400474 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400475}
476
Josef Bacik0f9dd462008-09-23 13:14:11 -0400477/*
478 * return the block group that starts at or after bytenr
479 */
Chris Masond3977122009-01-05 21:25:51 -0500480static struct btrfs_block_group_cache *
481btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400482{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400483 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400484
Josef Bacik0f9dd462008-09-23 13:14:11 -0400485 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400486
Josef Bacik0f9dd462008-09-23 13:14:11 -0400487 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400488}
489
Josef Bacik0f9dd462008-09-23 13:14:11 -0400490/*
Sankar P9f556842009-05-14 13:52:22 -0400491 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400492 */
Chris Masond3977122009-01-05 21:25:51 -0500493struct btrfs_block_group_cache *btrfs_lookup_block_group(
494 struct btrfs_fs_info *info,
495 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400496{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400497 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400498
Josef Bacik0f9dd462008-09-23 13:14:11 -0400499 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400500
Josef Bacik0f9dd462008-09-23 13:14:11 -0400501 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400502}
Chris Mason0b86a832008-03-24 15:01:56 -0400503
Josef Bacik0f9dd462008-09-23 13:14:11 -0400504static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
505 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400506{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400507 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400508 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400509
510 rcu_read_lock();
511 list_for_each_entry_rcu(found, head, list) {
512 if (found->flags == flags) {
513 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400514 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400515 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400516 }
Chris Mason4184ea72009-03-10 12:39:20 -0400517 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400518 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400519}
520
Chris Mason4184ea72009-03-10 12:39:20 -0400521/*
522 * after adding space to the filesystem, we need to clear the full flags
523 * on all the space infos.
524 */
525void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
526{
527 struct list_head *head = &info->space_info;
528 struct btrfs_space_info *found;
529
530 rcu_read_lock();
531 list_for_each_entry_rcu(found, head, list)
532 found->full = 0;
533 rcu_read_unlock();
534}
535
Josef Bacik80eb2342008-10-29 14:49:05 -0400536static u64 div_factor(u64 num, int factor)
537{
538 if (factor == 10)
539 return num;
540 num *= factor;
541 do_div(num, 10);
542 return num;
543}
544
Yan Zhengd2fb3432008-12-11 16:30:39 -0500545u64 btrfs_find_block_group(struct btrfs_root *root,
546 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400547{
Chris Mason96b51792007-10-15 16:15:19 -0400548 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400549 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500550 u64 last = max(search_hint, search_start);
551 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400552 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500553 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400554 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400555again:
Zheng Yane8569812008-09-26 10:05:48 -0400556 while (1) {
557 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400558 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400559 break;
Chris Mason96b51792007-10-15 16:15:19 -0400560
Chris Masonc286ac42008-07-22 23:06:41 -0400561 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400562 last = cache->key.objectid + cache->key.offset;
563 used = btrfs_block_group_used(&cache->item);
564
Yan Zhengd2fb3432008-12-11 16:30:39 -0500565 if ((full_search || !cache->ro) &&
566 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400567 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500568 div_factor(cache->key.offset, factor)) {
569 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400570 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400571 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400572 goto found;
573 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400574 }
Chris Masonc286ac42008-07-22 23:06:41 -0400575 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400576 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400577 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400578 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400579 if (!wrapped) {
580 last = search_start;
581 wrapped = 1;
582 goto again;
583 }
584 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400585 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400586 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400587 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400588 goto again;
589 }
Chris Masonbe744172007-05-06 10:15:01 -0400590found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500591 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400592}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400593
Chris Masone02119d2008-09-05 16:13:11 -0400594/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400595int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400596{
597 int ret;
598 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400599 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400600
Zheng Yan31840ae2008-09-23 13:14:14 -0400601 path = btrfs_alloc_path();
602 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400603 key.objectid = start;
604 key.offset = len;
605 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
606 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
607 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400608 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500609 return ret;
610}
611
Chris Masond8d5f3e2007-12-11 12:42:00 -0500612/*
613 * Back reference rules. Back refs have three main goals:
614 *
615 * 1) differentiate between all holders of references to an extent so that
616 * when a reference is dropped we can make sure it was a valid reference
617 * before freeing the extent.
618 *
619 * 2) Provide enough information to quickly find the holders of an extent
620 * if we notice a given block is corrupted or bad.
621 *
622 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
623 * maintenance. This is actually the same as #2, but with a slightly
624 * different use case.
625 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400626 * There are two kinds of back refs. The implicit back refs is optimized
627 * for pointers in non-shared tree blocks. For a given pointer in a block,
628 * back refs of this kind provide information about the block's owner tree
629 * and the pointer's key. These information allow us to find the block by
630 * b-tree searching. The full back refs is for pointers in tree blocks not
631 * referenced by their owner trees. The location of tree block is recorded
632 * in the back refs. Actually the full back refs is generic, and can be
633 * used in all cases the implicit back refs is used. The major shortcoming
634 * of the full back refs is its overhead. Every time a tree block gets
635 * COWed, we have to update back refs entry for all pointers in it.
636 *
637 * For a newly allocated tree block, we use implicit back refs for
638 * pointers in it. This means most tree related operations only involve
639 * implicit back refs. For a tree block created in old transaction, the
640 * only way to drop a reference to it is COW it. So we can detect the
641 * event that tree block loses its owner tree's reference and do the
642 * back refs conversion.
643 *
644 * When a tree block is COW'd through a tree, there are four cases:
645 *
646 * The reference count of the block is one and the tree is the block's
647 * owner tree. Nothing to do in this case.
648 *
649 * The reference count of the block is one and the tree is not the
650 * block's owner tree. In this case, full back refs is used for pointers
651 * in the block. Remove these full back refs, add implicit back refs for
652 * every pointers in the new block.
653 *
654 * The reference count of the block is greater than one and the tree is
655 * the block's owner tree. In this case, implicit back refs is used for
656 * pointers in the block. Add full back refs for every pointers in the
657 * block, increase lower level extents' reference counts. The original
658 * implicit back refs are entailed to the new block.
659 *
660 * The reference count of the block is greater than one and the tree is
661 * not the block's owner tree. Add implicit back refs for every pointer in
662 * the new block, increase lower level extents' reference count.
663 *
664 * Back Reference Key composing:
665 *
666 * The key objectid corresponds to the first byte in the extent,
667 * The key type is used to differentiate between types of back refs.
668 * There are different meanings of the key offset for different types
669 * of back refs.
670 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500671 * File extents can be referenced by:
672 *
673 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400674 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500675 * - different offsets inside a file (bookend extents in file.c)
676 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400677 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500678 *
679 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500680 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400681 * - original offset in the file
682 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400683 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400684 * The key offset for the implicit back refs is hash of the first
685 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500686 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400687 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500688 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400689 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500690 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400691 * The key offset for the implicit back refs is the first byte of
692 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500693 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400694 * When a file extent is allocated, The implicit back refs is used.
695 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500696 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400697 * (root_key.objectid, inode objectid, offset in file, 1)
698 *
699 * When a file extent is removed file truncation, we find the
700 * corresponding implicit back refs and check the following fields:
701 *
702 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500703 *
704 * Btree extents can be referenced by:
705 *
706 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500707 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400708 * Both the implicit back refs and the full back refs for tree blocks
709 * only consist of key. The key offset for the implicit back refs is
710 * objectid of block's owner tree. The key offset for the full back refs
711 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500712 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400713 * When implicit back refs is used, information about the lowest key and
714 * level of the tree block are required. These information are stored in
715 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500716 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400717
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400718#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
719static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
720 struct btrfs_root *root,
721 struct btrfs_path *path,
722 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500723{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400724 struct btrfs_extent_item *item;
725 struct btrfs_extent_item_v0 *ei0;
726 struct btrfs_extent_ref_v0 *ref0;
727 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400728 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400729 struct btrfs_key key;
730 struct btrfs_key found_key;
731 u32 new_size = sizeof(*item);
732 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500733 int ret;
734
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400735 leaf = path->nodes[0];
736 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500737
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400738 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
739 ei0 = btrfs_item_ptr(leaf, path->slots[0],
740 struct btrfs_extent_item_v0);
741 refs = btrfs_extent_refs_v0(leaf, ei0);
742
743 if (owner == (u64)-1) {
744 while (1) {
745 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
746 ret = btrfs_next_leaf(root, path);
747 if (ret < 0)
748 return ret;
749 BUG_ON(ret > 0);
750 leaf = path->nodes[0];
751 }
752 btrfs_item_key_to_cpu(leaf, &found_key,
753 path->slots[0]);
754 BUG_ON(key.objectid != found_key.objectid);
755 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
756 path->slots[0]++;
757 continue;
758 }
759 ref0 = btrfs_item_ptr(leaf, path->slots[0],
760 struct btrfs_extent_ref_v0);
761 owner = btrfs_ref_objectid_v0(leaf, ref0);
762 break;
763 }
764 }
765 btrfs_release_path(root, path);
766
767 if (owner < BTRFS_FIRST_FREE_OBJECTID)
768 new_size += sizeof(*bi);
769
770 new_size -= sizeof(*ei0);
771 ret = btrfs_search_slot(trans, root, &key, path,
772 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400773 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400774 return ret;
775 BUG_ON(ret);
776
777 ret = btrfs_extend_item(trans, root, path, new_size);
778 BUG_ON(ret);
779
780 leaf = path->nodes[0];
781 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
782 btrfs_set_extent_refs(leaf, item, refs);
783 /* FIXME: get real generation */
784 btrfs_set_extent_generation(leaf, item, 0);
785 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
786 btrfs_set_extent_flags(leaf, item,
787 BTRFS_EXTENT_FLAG_TREE_BLOCK |
788 BTRFS_BLOCK_FLAG_FULL_BACKREF);
789 bi = (struct btrfs_tree_block_info *)(item + 1);
790 /* FIXME: get first key of the block */
791 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
792 btrfs_set_tree_block_level(leaf, bi, (int)owner);
793 } else {
794 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
795 }
796 btrfs_mark_buffer_dirty(leaf);
797 return 0;
798}
799#endif
800
801static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
802{
803 u32 high_crc = ~(u32)0;
804 u32 low_crc = ~(u32)0;
805 __le64 lenum;
806
807 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100808 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400809 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100810 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400811 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100812 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400813
814 return ((u64)high_crc << 31) ^ (u64)low_crc;
815}
816
817static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
818 struct btrfs_extent_data_ref *ref)
819{
820 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
821 btrfs_extent_data_ref_objectid(leaf, ref),
822 btrfs_extent_data_ref_offset(leaf, ref));
823}
824
825static int match_extent_data_ref(struct extent_buffer *leaf,
826 struct btrfs_extent_data_ref *ref,
827 u64 root_objectid, u64 owner, u64 offset)
828{
829 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
830 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
831 btrfs_extent_data_ref_offset(leaf, ref) != offset)
832 return 0;
833 return 1;
834}
835
836static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
837 struct btrfs_root *root,
838 struct btrfs_path *path,
839 u64 bytenr, u64 parent,
840 u64 root_objectid,
841 u64 owner, u64 offset)
842{
843 struct btrfs_key key;
844 struct btrfs_extent_data_ref *ref;
845 struct extent_buffer *leaf;
846 u32 nritems;
847 int ret;
848 int recow;
849 int err = -ENOENT;
850
851 key.objectid = bytenr;
852 if (parent) {
853 key.type = BTRFS_SHARED_DATA_REF_KEY;
854 key.offset = parent;
855 } else {
856 key.type = BTRFS_EXTENT_DATA_REF_KEY;
857 key.offset = hash_extent_data_ref(root_objectid,
858 owner, offset);
859 }
860again:
861 recow = 0;
862 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
863 if (ret < 0) {
864 err = ret;
865 goto fail;
866 }
867
868 if (parent) {
869 if (!ret)
870 return 0;
871#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
872 key.type = BTRFS_EXTENT_REF_V0_KEY;
873 btrfs_release_path(root, path);
874 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
875 if (ret < 0) {
876 err = ret;
877 goto fail;
878 }
879 if (!ret)
880 return 0;
881#endif
882 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400883 }
884
885 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886 nritems = btrfs_header_nritems(leaf);
887 while (1) {
888 if (path->slots[0] >= nritems) {
889 ret = btrfs_next_leaf(root, path);
890 if (ret < 0)
891 err = ret;
892 if (ret)
893 goto fail;
894
895 leaf = path->nodes[0];
896 nritems = btrfs_header_nritems(leaf);
897 recow = 1;
898 }
899
900 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
901 if (key.objectid != bytenr ||
902 key.type != BTRFS_EXTENT_DATA_REF_KEY)
903 goto fail;
904
905 ref = btrfs_item_ptr(leaf, path->slots[0],
906 struct btrfs_extent_data_ref);
907
908 if (match_extent_data_ref(leaf, ref, root_objectid,
909 owner, offset)) {
910 if (recow) {
911 btrfs_release_path(root, path);
912 goto again;
913 }
914 err = 0;
915 break;
916 }
917 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400918 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400919fail:
920 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400921}
922
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400923static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
924 struct btrfs_root *root,
925 struct btrfs_path *path,
926 u64 bytenr, u64 parent,
927 u64 root_objectid, u64 owner,
928 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400929{
930 struct btrfs_key key;
931 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400932 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400933 u32 num_refs;
934 int ret;
935
936 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400937 if (parent) {
938 key.type = BTRFS_SHARED_DATA_REF_KEY;
939 key.offset = parent;
940 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400941 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400942 key.type = BTRFS_EXTENT_DATA_REF_KEY;
943 key.offset = hash_extent_data_ref(root_objectid,
944 owner, offset);
945 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400946 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400947
948 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
949 if (ret && ret != -EEXIST)
950 goto fail;
951
952 leaf = path->nodes[0];
953 if (parent) {
954 struct btrfs_shared_data_ref *ref;
955 ref = btrfs_item_ptr(leaf, path->slots[0],
956 struct btrfs_shared_data_ref);
957 if (ret == 0) {
958 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
959 } else {
960 num_refs = btrfs_shared_data_ref_count(leaf, ref);
961 num_refs += refs_to_add;
962 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
963 }
964 } else {
965 struct btrfs_extent_data_ref *ref;
966 while (ret == -EEXIST) {
967 ref = btrfs_item_ptr(leaf, path->slots[0],
968 struct btrfs_extent_data_ref);
969 if (match_extent_data_ref(leaf, ref, root_objectid,
970 owner, offset))
971 break;
972 btrfs_release_path(root, path);
973 key.offset++;
974 ret = btrfs_insert_empty_item(trans, root, path, &key,
975 size);
976 if (ret && ret != -EEXIST)
977 goto fail;
978
979 leaf = path->nodes[0];
980 }
981 ref = btrfs_item_ptr(leaf, path->slots[0],
982 struct btrfs_extent_data_ref);
983 if (ret == 0) {
984 btrfs_set_extent_data_ref_root(leaf, ref,
985 root_objectid);
986 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
987 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
988 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
989 } else {
990 num_refs = btrfs_extent_data_ref_count(leaf, ref);
991 num_refs += refs_to_add;
992 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
993 }
994 }
995 btrfs_mark_buffer_dirty(leaf);
996 ret = 0;
997fail:
Chris Mason7bb86312007-12-11 09:25:06 -0500998 btrfs_release_path(root, path);
999 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001000}
1001
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1003 struct btrfs_root *root,
1004 struct btrfs_path *path,
1005 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001006{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001007 struct btrfs_key key;
1008 struct btrfs_extent_data_ref *ref1 = NULL;
1009 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001010 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001011 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001012 int ret = 0;
1013
1014 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1016
1017 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1018 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1019 struct btrfs_extent_data_ref);
1020 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1021 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1022 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1023 struct btrfs_shared_data_ref);
1024 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1025#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1026 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1027 struct btrfs_extent_ref_v0 *ref0;
1028 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1029 struct btrfs_extent_ref_v0);
1030 num_refs = btrfs_ref_count_v0(leaf, ref0);
1031#endif
1032 } else {
1033 BUG();
1034 }
1035
Chris Mason56bec292009-03-13 10:10:06 -04001036 BUG_ON(num_refs < refs_to_drop);
1037 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001038
Zheng Yan31840ae2008-09-23 13:14:14 -04001039 if (num_refs == 0) {
1040 ret = btrfs_del_item(trans, root, path);
1041 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001042 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1043 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1044 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1045 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1046#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1047 else {
1048 struct btrfs_extent_ref_v0 *ref0;
1049 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1050 struct btrfs_extent_ref_v0);
1051 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1052 }
1053#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001054 btrfs_mark_buffer_dirty(leaf);
1055 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001056 return ret;
1057}
1058
1059static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1060 struct btrfs_path *path,
1061 struct btrfs_extent_inline_ref *iref)
1062{
1063 struct btrfs_key key;
1064 struct extent_buffer *leaf;
1065 struct btrfs_extent_data_ref *ref1;
1066 struct btrfs_shared_data_ref *ref2;
1067 u32 num_refs = 0;
1068
1069 leaf = path->nodes[0];
1070 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1071 if (iref) {
1072 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1073 BTRFS_EXTENT_DATA_REF_KEY) {
1074 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1075 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1076 } else {
1077 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1078 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1079 }
1080 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1081 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1082 struct btrfs_extent_data_ref);
1083 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1084 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1085 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1086 struct btrfs_shared_data_ref);
1087 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1088#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1089 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1090 struct btrfs_extent_ref_v0 *ref0;
1091 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1092 struct btrfs_extent_ref_v0);
1093 num_refs = btrfs_ref_count_v0(leaf, ref0);
1094#endif
1095 } else {
1096 WARN_ON(1);
1097 }
1098 return num_refs;
1099}
1100
1101static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1102 struct btrfs_root *root,
1103 struct btrfs_path *path,
1104 u64 bytenr, u64 parent,
1105 u64 root_objectid)
1106{
1107 struct btrfs_key key;
1108 int ret;
1109
1110 key.objectid = bytenr;
1111 if (parent) {
1112 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1113 key.offset = parent;
1114 } else {
1115 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1116 key.offset = root_objectid;
1117 }
1118
1119 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1120 if (ret > 0)
1121 ret = -ENOENT;
1122#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1123 if (ret == -ENOENT && parent) {
1124 btrfs_release_path(root, path);
1125 key.type = BTRFS_EXTENT_REF_V0_KEY;
1126 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1127 if (ret > 0)
1128 ret = -ENOENT;
1129 }
1130#endif
1131 return ret;
1132}
1133
1134static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1135 struct btrfs_root *root,
1136 struct btrfs_path *path,
1137 u64 bytenr, u64 parent,
1138 u64 root_objectid)
1139{
1140 struct btrfs_key key;
1141 int ret;
1142
1143 key.objectid = bytenr;
1144 if (parent) {
1145 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1146 key.offset = parent;
1147 } else {
1148 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1149 key.offset = root_objectid;
1150 }
1151
1152 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001153 btrfs_release_path(root, path);
1154 return ret;
1155}
1156
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001157static inline int extent_ref_type(u64 parent, u64 owner)
1158{
1159 int type;
1160 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1161 if (parent > 0)
1162 type = BTRFS_SHARED_BLOCK_REF_KEY;
1163 else
1164 type = BTRFS_TREE_BLOCK_REF_KEY;
1165 } else {
1166 if (parent > 0)
1167 type = BTRFS_SHARED_DATA_REF_KEY;
1168 else
1169 type = BTRFS_EXTENT_DATA_REF_KEY;
1170 }
1171 return type;
1172}
1173
Yan Zheng2c47e6052009-06-27 21:07:35 -04001174static int find_next_key(struct btrfs_path *path, int level,
1175 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001176
1177{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001178 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001179 if (!path->nodes[level])
1180 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001181 if (path->slots[level] + 1 >=
1182 btrfs_header_nritems(path->nodes[level]))
1183 continue;
1184 if (level == 0)
1185 btrfs_item_key_to_cpu(path->nodes[level], key,
1186 path->slots[level] + 1);
1187 else
1188 btrfs_node_key_to_cpu(path->nodes[level], key,
1189 path->slots[level] + 1);
1190 return 0;
1191 }
1192 return 1;
1193}
1194
1195/*
1196 * look for inline back ref. if back ref is found, *ref_ret is set
1197 * to the address of inline back ref, and 0 is returned.
1198 *
1199 * if back ref isn't found, *ref_ret is set to the address where it
1200 * should be inserted, and -ENOENT is returned.
1201 *
1202 * if insert is true and there are too many inline back refs, the path
1203 * points to the extent item, and -EAGAIN is returned.
1204 *
1205 * NOTE: inline back refs are ordered in the same way that back ref
1206 * items in the tree are ordered.
1207 */
1208static noinline_for_stack
1209int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1210 struct btrfs_root *root,
1211 struct btrfs_path *path,
1212 struct btrfs_extent_inline_ref **ref_ret,
1213 u64 bytenr, u64 num_bytes,
1214 u64 parent, u64 root_objectid,
1215 u64 owner, u64 offset, int insert)
1216{
1217 struct btrfs_key key;
1218 struct extent_buffer *leaf;
1219 struct btrfs_extent_item *ei;
1220 struct btrfs_extent_inline_ref *iref;
1221 u64 flags;
1222 u64 item_size;
1223 unsigned long ptr;
1224 unsigned long end;
1225 int extra_size;
1226 int type;
1227 int want;
1228 int ret;
1229 int err = 0;
1230
1231 key.objectid = bytenr;
1232 key.type = BTRFS_EXTENT_ITEM_KEY;
1233 key.offset = num_bytes;
1234
1235 want = extent_ref_type(parent, owner);
1236 if (insert) {
1237 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001238 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001239 } else
1240 extra_size = -1;
1241 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1242 if (ret < 0) {
1243 err = ret;
1244 goto out;
1245 }
1246 BUG_ON(ret);
1247
1248 leaf = path->nodes[0];
1249 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1250#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1251 if (item_size < sizeof(*ei)) {
1252 if (!insert) {
1253 err = -ENOENT;
1254 goto out;
1255 }
1256 ret = convert_extent_item_v0(trans, root, path, owner,
1257 extra_size);
1258 if (ret < 0) {
1259 err = ret;
1260 goto out;
1261 }
1262 leaf = path->nodes[0];
1263 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1264 }
1265#endif
1266 BUG_ON(item_size < sizeof(*ei));
1267
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1269 flags = btrfs_extent_flags(leaf, ei);
1270
1271 ptr = (unsigned long)(ei + 1);
1272 end = (unsigned long)ei + item_size;
1273
1274 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1275 ptr += sizeof(struct btrfs_tree_block_info);
1276 BUG_ON(ptr > end);
1277 } else {
1278 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1279 }
1280
1281 err = -ENOENT;
1282 while (1) {
1283 if (ptr >= end) {
1284 WARN_ON(ptr > end);
1285 break;
1286 }
1287 iref = (struct btrfs_extent_inline_ref *)ptr;
1288 type = btrfs_extent_inline_ref_type(leaf, iref);
1289 if (want < type)
1290 break;
1291 if (want > type) {
1292 ptr += btrfs_extent_inline_ref_size(type);
1293 continue;
1294 }
1295
1296 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1297 struct btrfs_extent_data_ref *dref;
1298 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1299 if (match_extent_data_ref(leaf, dref, root_objectid,
1300 owner, offset)) {
1301 err = 0;
1302 break;
1303 }
1304 if (hash_extent_data_ref_item(leaf, dref) <
1305 hash_extent_data_ref(root_objectid, owner, offset))
1306 break;
1307 } else {
1308 u64 ref_offset;
1309 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1310 if (parent > 0) {
1311 if (parent == ref_offset) {
1312 err = 0;
1313 break;
1314 }
1315 if (ref_offset < parent)
1316 break;
1317 } else {
1318 if (root_objectid == ref_offset) {
1319 err = 0;
1320 break;
1321 }
1322 if (ref_offset < root_objectid)
1323 break;
1324 }
1325 }
1326 ptr += btrfs_extent_inline_ref_size(type);
1327 }
1328 if (err == -ENOENT && insert) {
1329 if (item_size + extra_size >=
1330 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1331 err = -EAGAIN;
1332 goto out;
1333 }
1334 /*
1335 * To add new inline back ref, we have to make sure
1336 * there is no corresponding back ref item.
1337 * For simplicity, we just do not add new inline back
1338 * ref if there is any kind of item for this block
1339 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001340 if (find_next_key(path, 0, &key) == 0 &&
1341 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001342 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001343 err = -EAGAIN;
1344 goto out;
1345 }
1346 }
1347 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1348out:
Yan Zheng85d41982009-06-11 08:51:10 -04001349 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001350 path->keep_locks = 0;
1351 btrfs_unlock_up_safe(path, 1);
1352 }
1353 return err;
1354}
1355
1356/*
1357 * helper to add new inline back ref
1358 */
1359static noinline_for_stack
1360int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1361 struct btrfs_root *root,
1362 struct btrfs_path *path,
1363 struct btrfs_extent_inline_ref *iref,
1364 u64 parent, u64 root_objectid,
1365 u64 owner, u64 offset, int refs_to_add,
1366 struct btrfs_delayed_extent_op *extent_op)
1367{
1368 struct extent_buffer *leaf;
1369 struct btrfs_extent_item *ei;
1370 unsigned long ptr;
1371 unsigned long end;
1372 unsigned long item_offset;
1373 u64 refs;
1374 int size;
1375 int type;
1376 int ret;
1377
1378 leaf = path->nodes[0];
1379 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1380 item_offset = (unsigned long)iref - (unsigned long)ei;
1381
1382 type = extent_ref_type(parent, owner);
1383 size = btrfs_extent_inline_ref_size(type);
1384
1385 ret = btrfs_extend_item(trans, root, path, size);
1386 BUG_ON(ret);
1387
1388 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1389 refs = btrfs_extent_refs(leaf, ei);
1390 refs += refs_to_add;
1391 btrfs_set_extent_refs(leaf, ei, refs);
1392 if (extent_op)
1393 __run_delayed_extent_op(extent_op, leaf, ei);
1394
1395 ptr = (unsigned long)ei + item_offset;
1396 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1397 if (ptr < end - size)
1398 memmove_extent_buffer(leaf, ptr + size, ptr,
1399 end - size - ptr);
1400
1401 iref = (struct btrfs_extent_inline_ref *)ptr;
1402 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1403 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1404 struct btrfs_extent_data_ref *dref;
1405 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1406 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1407 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1408 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1409 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1410 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1411 struct btrfs_shared_data_ref *sref;
1412 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1413 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1414 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1415 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1416 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1417 } else {
1418 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1419 }
1420 btrfs_mark_buffer_dirty(leaf);
1421 return 0;
1422}
1423
1424static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1425 struct btrfs_root *root,
1426 struct btrfs_path *path,
1427 struct btrfs_extent_inline_ref **ref_ret,
1428 u64 bytenr, u64 num_bytes, u64 parent,
1429 u64 root_objectid, u64 owner, u64 offset)
1430{
1431 int ret;
1432
1433 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1434 bytenr, num_bytes, parent,
1435 root_objectid, owner, offset, 0);
1436 if (ret != -ENOENT)
1437 return ret;
1438
1439 btrfs_release_path(root, path);
1440 *ref_ret = NULL;
1441
1442 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1443 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1444 root_objectid);
1445 } else {
1446 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1447 root_objectid, owner, offset);
1448 }
1449 return ret;
1450}
1451
1452/*
1453 * helper to update/remove inline back ref
1454 */
1455static noinline_for_stack
1456int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1457 struct btrfs_root *root,
1458 struct btrfs_path *path,
1459 struct btrfs_extent_inline_ref *iref,
1460 int refs_to_mod,
1461 struct btrfs_delayed_extent_op *extent_op)
1462{
1463 struct extent_buffer *leaf;
1464 struct btrfs_extent_item *ei;
1465 struct btrfs_extent_data_ref *dref = NULL;
1466 struct btrfs_shared_data_ref *sref = NULL;
1467 unsigned long ptr;
1468 unsigned long end;
1469 u32 item_size;
1470 int size;
1471 int type;
1472 int ret;
1473 u64 refs;
1474
1475 leaf = path->nodes[0];
1476 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1477 refs = btrfs_extent_refs(leaf, ei);
1478 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1479 refs += refs_to_mod;
1480 btrfs_set_extent_refs(leaf, ei, refs);
1481 if (extent_op)
1482 __run_delayed_extent_op(extent_op, leaf, ei);
1483
1484 type = btrfs_extent_inline_ref_type(leaf, iref);
1485
1486 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1487 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1488 refs = btrfs_extent_data_ref_count(leaf, dref);
1489 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1490 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1491 refs = btrfs_shared_data_ref_count(leaf, sref);
1492 } else {
1493 refs = 1;
1494 BUG_ON(refs_to_mod != -1);
1495 }
1496
1497 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1498 refs += refs_to_mod;
1499
1500 if (refs > 0) {
1501 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1502 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1503 else
1504 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1505 } else {
1506 size = btrfs_extent_inline_ref_size(type);
1507 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1508 ptr = (unsigned long)iref;
1509 end = (unsigned long)ei + item_size;
1510 if (ptr + size < end)
1511 memmove_extent_buffer(leaf, ptr, ptr + size,
1512 end - ptr - size);
1513 item_size -= size;
1514 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1515 BUG_ON(ret);
1516 }
1517 btrfs_mark_buffer_dirty(leaf);
1518 return 0;
1519}
1520
1521static noinline_for_stack
1522int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1523 struct btrfs_root *root,
1524 struct btrfs_path *path,
1525 u64 bytenr, u64 num_bytes, u64 parent,
1526 u64 root_objectid, u64 owner,
1527 u64 offset, int refs_to_add,
1528 struct btrfs_delayed_extent_op *extent_op)
1529{
1530 struct btrfs_extent_inline_ref *iref;
1531 int ret;
1532
1533 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1534 bytenr, num_bytes, parent,
1535 root_objectid, owner, offset, 1);
1536 if (ret == 0) {
1537 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1538 ret = update_inline_extent_backref(trans, root, path, iref,
1539 refs_to_add, extent_op);
1540 } else if (ret == -ENOENT) {
1541 ret = setup_inline_extent_backref(trans, root, path, iref,
1542 parent, root_objectid,
1543 owner, offset, refs_to_add,
1544 extent_op);
1545 }
1546 return ret;
1547}
1548
1549static int insert_extent_backref(struct btrfs_trans_handle *trans,
1550 struct btrfs_root *root,
1551 struct btrfs_path *path,
1552 u64 bytenr, u64 parent, u64 root_objectid,
1553 u64 owner, u64 offset, int refs_to_add)
1554{
1555 int ret;
1556 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1557 BUG_ON(refs_to_add != 1);
1558 ret = insert_tree_block_ref(trans, root, path, bytenr,
1559 parent, root_objectid);
1560 } else {
1561 ret = insert_extent_data_ref(trans, root, path, bytenr,
1562 parent, root_objectid,
1563 owner, offset, refs_to_add);
1564 }
1565 return ret;
1566}
1567
1568static int remove_extent_backref(struct btrfs_trans_handle *trans,
1569 struct btrfs_root *root,
1570 struct btrfs_path *path,
1571 struct btrfs_extent_inline_ref *iref,
1572 int refs_to_drop, int is_data)
1573{
1574 int ret;
1575
1576 BUG_ON(!is_data && refs_to_drop != 1);
1577 if (iref) {
1578 ret = update_inline_extent_backref(trans, root, path, iref,
1579 -refs_to_drop, NULL);
1580 } else if (is_data) {
1581 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1582 } else {
1583 ret = btrfs_del_item(trans, root, path);
1584 }
1585 return ret;
1586}
1587
Chris Mason15916de2008-11-19 21:17:22 -05001588static void btrfs_issue_discard(struct block_device *bdev,
1589 u64 start, u64 len)
1590{
Christoph Hellwig746cd1e2009-09-12 07:35:43 +02001591 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
1592 DISCARD_FL_BARRIER);
Chris Mason15916de2008-11-19 21:17:22 -05001593}
Chris Mason15916de2008-11-19 21:17:22 -05001594
Liu Hui1f3c79a2009-01-05 15:57:51 -05001595static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1596 u64 num_bytes)
1597{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001598 int ret;
1599 u64 map_length = num_bytes;
1600 struct btrfs_multi_bio *multi = NULL;
1601
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001602 if (!btrfs_test_opt(root, DISCARD))
1603 return 0;
1604
Liu Hui1f3c79a2009-01-05 15:57:51 -05001605 /* Tell the block device(s) that the sectors can be discarded */
1606 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1607 bytenr, &map_length, &multi, 0);
1608 if (!ret) {
1609 struct btrfs_bio_stripe *stripe = multi->stripes;
1610 int i;
1611
1612 if (map_length > num_bytes)
1613 map_length = num_bytes;
1614
1615 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1616 btrfs_issue_discard(stripe->dev->bdev,
1617 stripe->physical,
1618 map_length);
1619 }
1620 kfree(multi);
1621 }
1622
1623 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001624}
1625
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001626int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1627 struct btrfs_root *root,
1628 u64 bytenr, u64 num_bytes, u64 parent,
1629 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001630{
1631 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001632 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1633 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001634
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001635 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1636 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1637 parent, root_objectid, (int)owner,
1638 BTRFS_ADD_DELAYED_REF, NULL);
1639 } else {
1640 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1641 parent, root_objectid, owner, offset,
1642 BTRFS_ADD_DELAYED_REF, NULL);
1643 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001644 return ret;
1645}
1646
Chris Mason925baed2008-06-25 16:01:30 -04001647static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001648 struct btrfs_root *root,
1649 u64 bytenr, u64 num_bytes,
1650 u64 parent, u64 root_objectid,
1651 u64 owner, u64 offset, int refs_to_add,
1652 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001653{
Chris Mason5caf2a02007-04-02 11:20:42 -04001654 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001655 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001656 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001657 u64 refs;
1658 int ret;
1659 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001660
Chris Mason5caf2a02007-04-02 11:20:42 -04001661 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001662 if (!path)
1663 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001664
Chris Mason3c12ac72008-04-21 12:01:38 -04001665 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001666 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001667 /* this will setup the path even if it fails to insert the back ref */
1668 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1669 path, bytenr, num_bytes, parent,
1670 root_objectid, owner, offset,
1671 refs_to_add, extent_op);
1672 if (ret == 0)
1673 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001674
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001675 if (ret != -EAGAIN) {
1676 err = ret;
1677 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001678 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001679
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001680 leaf = path->nodes[0];
1681 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1682 refs = btrfs_extent_refs(leaf, item);
1683 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1684 if (extent_op)
1685 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001686
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001687 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001688 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001689
Chris Mason3c12ac72008-04-21 12:01:38 -04001690 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001691 path->leave_spinning = 1;
1692
Chris Mason56bec292009-03-13 10:10:06 -04001693 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001694 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001695 path, bytenr, parent, root_objectid,
1696 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001697 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698out:
Chris Mason74493f72007-12-11 09:25:06 -05001699 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001700 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001701}
1702
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001703static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1704 struct btrfs_root *root,
1705 struct btrfs_delayed_ref_node *node,
1706 struct btrfs_delayed_extent_op *extent_op,
1707 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001708{
Chris Mason56bec292009-03-13 10:10:06 -04001709 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001710 struct btrfs_delayed_data_ref *ref;
1711 struct btrfs_key ins;
1712 u64 parent = 0;
1713 u64 ref_root = 0;
1714 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001715
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001716 ins.objectid = node->bytenr;
1717 ins.offset = node->num_bytes;
1718 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001719
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001720 ref = btrfs_delayed_node_to_data_ref(node);
1721 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1722 parent = ref->parent;
1723 else
1724 ref_root = ref->root;
1725
1726 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1727 if (extent_op) {
1728 BUG_ON(extent_op->update_key);
1729 flags |= extent_op->flags_to_set;
1730 }
1731 ret = alloc_reserved_file_extent(trans, root,
1732 parent, ref_root, flags,
1733 ref->objectid, ref->offset,
1734 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001735 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1736 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1737 node->num_bytes, parent,
1738 ref_root, ref->objectid,
1739 ref->offset, node->ref_mod,
1740 extent_op);
1741 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1742 ret = __btrfs_free_extent(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 {
1748 BUG();
1749 }
Chris Mason56bec292009-03-13 10:10:06 -04001750 return ret;
1751}
1752
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001753static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1754 struct extent_buffer *leaf,
1755 struct btrfs_extent_item *ei)
1756{
1757 u64 flags = btrfs_extent_flags(leaf, ei);
1758 if (extent_op->update_flags) {
1759 flags |= extent_op->flags_to_set;
1760 btrfs_set_extent_flags(leaf, ei, flags);
1761 }
1762
1763 if (extent_op->update_key) {
1764 struct btrfs_tree_block_info *bi;
1765 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1766 bi = (struct btrfs_tree_block_info *)(ei + 1);
1767 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1768 }
1769}
1770
1771static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1772 struct btrfs_root *root,
1773 struct btrfs_delayed_ref_node *node,
1774 struct btrfs_delayed_extent_op *extent_op)
1775{
1776 struct btrfs_key key;
1777 struct btrfs_path *path;
1778 struct btrfs_extent_item *ei;
1779 struct extent_buffer *leaf;
1780 u32 item_size;
1781 int ret;
1782 int err = 0;
1783
1784 path = btrfs_alloc_path();
1785 if (!path)
1786 return -ENOMEM;
1787
1788 key.objectid = node->bytenr;
1789 key.type = BTRFS_EXTENT_ITEM_KEY;
1790 key.offset = node->num_bytes;
1791
1792 path->reada = 1;
1793 path->leave_spinning = 1;
1794 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1795 path, 0, 1);
1796 if (ret < 0) {
1797 err = ret;
1798 goto out;
1799 }
1800 if (ret > 0) {
1801 err = -EIO;
1802 goto out;
1803 }
1804
1805 leaf = path->nodes[0];
1806 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1807#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1808 if (item_size < sizeof(*ei)) {
1809 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1810 path, (u64)-1, 0);
1811 if (ret < 0) {
1812 err = ret;
1813 goto out;
1814 }
1815 leaf = path->nodes[0];
1816 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1817 }
1818#endif
1819 BUG_ON(item_size < sizeof(*ei));
1820 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1821 __run_delayed_extent_op(extent_op, leaf, ei);
1822
1823 btrfs_mark_buffer_dirty(leaf);
1824out:
1825 btrfs_free_path(path);
1826 return err;
1827}
1828
1829static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1830 struct btrfs_root *root,
1831 struct btrfs_delayed_ref_node *node,
1832 struct btrfs_delayed_extent_op *extent_op,
1833 int insert_reserved)
1834{
1835 int ret = 0;
1836 struct btrfs_delayed_tree_ref *ref;
1837 struct btrfs_key ins;
1838 u64 parent = 0;
1839 u64 ref_root = 0;
1840
1841 ins.objectid = node->bytenr;
1842 ins.offset = node->num_bytes;
1843 ins.type = BTRFS_EXTENT_ITEM_KEY;
1844
1845 ref = btrfs_delayed_node_to_tree_ref(node);
1846 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1847 parent = ref->parent;
1848 else
1849 ref_root = ref->root;
1850
1851 BUG_ON(node->ref_mod != 1);
1852 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1853 BUG_ON(!extent_op || !extent_op->update_flags ||
1854 !extent_op->update_key);
1855 ret = alloc_reserved_tree_block(trans, root,
1856 parent, ref_root,
1857 extent_op->flags_to_set,
1858 &extent_op->key,
1859 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001860 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1861 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1862 node->num_bytes, parent, ref_root,
1863 ref->level, 0, 1, extent_op);
1864 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1865 ret = __btrfs_free_extent(trans, root, node->bytenr,
1866 node->num_bytes, parent, ref_root,
1867 ref->level, 0, 1, extent_op);
1868 } else {
1869 BUG();
1870 }
1871 return ret;
1872}
1873
1874
Chris Mason56bec292009-03-13 10:10:06 -04001875/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1877 struct btrfs_root *root,
1878 struct btrfs_delayed_ref_node *node,
1879 struct btrfs_delayed_extent_op *extent_op,
1880 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001881{
Josef Bacikeb099672009-02-12 09:27:38 -05001882 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001883 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001884 struct btrfs_delayed_ref_head *head;
1885 /*
1886 * we've hit the end of the chain and we were supposed
1887 * to insert this extent into the tree. But, it got
1888 * deleted before we ever needed to insert it, so all
1889 * we have to do is clean up the accounting
1890 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001891 BUG_ON(extent_op);
1892 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001893 if (insert_reserved) {
Yan Zheng11833d62009-09-11 16:11:19 -04001894 int mark_free = 0;
1895 struct extent_buffer *must_clean = NULL;
1896
1897 ret = pin_down_bytes(trans, root, NULL,
1898 node->bytenr, node->num_bytes,
1899 head->is_data, 1, &must_clean);
1900 if (ret > 0)
1901 mark_free = 1;
1902
1903 if (must_clean) {
1904 clean_tree_block(NULL, root, must_clean);
1905 btrfs_tree_unlock(must_clean);
1906 free_extent_buffer(must_clean);
1907 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001908 if (head->is_data) {
1909 ret = btrfs_del_csums(trans, root,
1910 node->bytenr,
1911 node->num_bytes);
1912 BUG_ON(ret);
1913 }
Yan Zheng11833d62009-09-11 16:11:19 -04001914 if (mark_free) {
1915 ret = btrfs_free_reserved_extent(root,
1916 node->bytenr,
1917 node->num_bytes);
1918 BUG_ON(ret);
1919 }
Chris Mason56bec292009-03-13 10:10:06 -04001920 }
Chris Mason56bec292009-03-13 10:10:06 -04001921 mutex_unlock(&head->mutex);
1922 return 0;
1923 }
Josef Bacikeb099672009-02-12 09:27:38 -05001924
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001925 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1926 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1927 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1928 insert_reserved);
1929 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1930 node->type == BTRFS_SHARED_DATA_REF_KEY)
1931 ret = run_delayed_data_ref(trans, root, node, extent_op,
1932 insert_reserved);
1933 else
1934 BUG();
1935 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001936}
1937
Chris Mason56bec292009-03-13 10:10:06 -04001938static noinline struct btrfs_delayed_ref_node *
1939select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001940{
Chris Mason56bec292009-03-13 10:10:06 -04001941 struct rb_node *node;
1942 struct btrfs_delayed_ref_node *ref;
1943 int action = BTRFS_ADD_DELAYED_REF;
1944again:
1945 /*
1946 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1947 * this prevents ref count from going down to zero when
1948 * there still are pending delayed ref.
1949 */
1950 node = rb_prev(&head->node.rb_node);
1951 while (1) {
1952 if (!node)
1953 break;
1954 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1955 rb_node);
1956 if (ref->bytenr != head->node.bytenr)
1957 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001958 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001959 return ref;
1960 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001961 }
Chris Mason56bec292009-03-13 10:10:06 -04001962 if (action == BTRFS_ADD_DELAYED_REF) {
1963 action = BTRFS_DROP_DELAYED_REF;
1964 goto again;
1965 }
1966 return NULL;
1967}
1968
Chris Masonc3e69d52009-03-13 10:17:05 -04001969static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1970 struct btrfs_root *root,
1971 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001972{
Chris Mason56bec292009-03-13 10:10:06 -04001973 struct btrfs_delayed_ref_root *delayed_refs;
1974 struct btrfs_delayed_ref_node *ref;
1975 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001976 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001977 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001978 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001979 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001980
1981 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001982 while (1) {
1983 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001984 /* pick a new head ref from the cluster list */
1985 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001986 break;
Chris Mason56bec292009-03-13 10:10:06 -04001987
Chris Masonc3e69d52009-03-13 10:17:05 -04001988 locked_ref = list_entry(cluster->next,
1989 struct btrfs_delayed_ref_head, cluster);
1990
1991 /* grab the lock that says we are going to process
1992 * all the refs for this head */
1993 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1994
1995 /*
1996 * we may have dropped the spin lock to get the head
1997 * mutex lock, and that might have given someone else
1998 * time to free the head. If that's true, it has been
1999 * removed from our list and we can move on.
2000 */
2001 if (ret == -EAGAIN) {
2002 locked_ref = NULL;
2003 count++;
2004 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002005 }
2006 }
2007
2008 /*
2009 * record the must insert reserved flag before we
2010 * drop the spin lock.
2011 */
2012 must_insert_reserved = locked_ref->must_insert_reserved;
2013 locked_ref->must_insert_reserved = 0;
2014
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002015 extent_op = locked_ref->extent_op;
2016 locked_ref->extent_op = NULL;
2017
Chris Mason56bec292009-03-13 10:10:06 -04002018 /*
2019 * locked_ref is the head node, so we have to go one
2020 * node back for any delayed ref updates
2021 */
Chris Mason56bec292009-03-13 10:10:06 -04002022 ref = select_delayed_ref(locked_ref);
2023 if (!ref) {
2024 /* All delayed refs have been processed, Go ahead
2025 * and send the head node to run_one_delayed_ref,
2026 * so that any accounting fixes can happen
2027 */
2028 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002029
2030 if (extent_op && must_insert_reserved) {
2031 kfree(extent_op);
2032 extent_op = NULL;
2033 }
2034
2035 if (extent_op) {
2036 spin_unlock(&delayed_refs->lock);
2037
2038 ret = run_delayed_extent_op(trans, root,
2039 ref, extent_op);
2040 BUG_ON(ret);
2041 kfree(extent_op);
2042
2043 cond_resched();
2044 spin_lock(&delayed_refs->lock);
2045 continue;
2046 }
2047
Chris Masonc3e69d52009-03-13 10:17:05 -04002048 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002049 locked_ref = NULL;
2050 }
2051
2052 ref->in_tree = 0;
2053 rb_erase(&ref->rb_node, &delayed_refs->root);
2054 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002055
Chris Mason56bec292009-03-13 10:10:06 -04002056 spin_unlock(&delayed_refs->lock);
2057
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002058 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002059 must_insert_reserved);
2060 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002061
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002062 btrfs_put_delayed_ref(ref);
2063 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002064 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002065
Chris Mason1887be62009-03-13 10:11:24 -04002066 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002067 spin_lock(&delayed_refs->lock);
2068 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002069 return count;
2070}
2071
2072/*
2073 * this starts processing the delayed reference count updates and
2074 * extent insertions we have queued up so far. count can be
2075 * 0, which means to process everything in the tree at the start
2076 * of the run (but not newly added entries), or it can be some target
2077 * number you'd like to process.
2078 */
2079int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2080 struct btrfs_root *root, unsigned long count)
2081{
2082 struct rb_node *node;
2083 struct btrfs_delayed_ref_root *delayed_refs;
2084 struct btrfs_delayed_ref_node *ref;
2085 struct list_head cluster;
2086 int ret;
2087 int run_all = count == (unsigned long)-1;
2088 int run_most = 0;
2089
2090 if (root == root->fs_info->extent_root)
2091 root = root->fs_info->tree_root;
2092
2093 delayed_refs = &trans->transaction->delayed_refs;
2094 INIT_LIST_HEAD(&cluster);
2095again:
2096 spin_lock(&delayed_refs->lock);
2097 if (count == 0) {
2098 count = delayed_refs->num_entries * 2;
2099 run_most = 1;
2100 }
2101 while (1) {
2102 if (!(run_all || run_most) &&
2103 delayed_refs->num_heads_ready < 64)
2104 break;
2105
2106 /*
2107 * go find something we can process in the rbtree. We start at
2108 * the beginning of the tree, and then build a cluster
2109 * of refs to process starting at the first one we are able to
2110 * lock
2111 */
2112 ret = btrfs_find_ref_cluster(trans, &cluster,
2113 delayed_refs->run_delayed_start);
2114 if (ret)
2115 break;
2116
2117 ret = run_clustered_refs(trans, root, &cluster);
2118 BUG_ON(ret < 0);
2119
2120 count -= min_t(unsigned long, ret, count);
2121
2122 if (count == 0)
2123 break;
2124 }
2125
Chris Mason56bec292009-03-13 10:10:06 -04002126 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002127 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002128 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002129 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002130 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002131
2132 while (node) {
2133 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2134 rb_node);
2135 if (btrfs_delayed_ref_is_head(ref)) {
2136 struct btrfs_delayed_ref_head *head;
2137
2138 head = btrfs_delayed_node_to_head(ref);
2139 atomic_inc(&ref->refs);
2140
2141 spin_unlock(&delayed_refs->lock);
2142 mutex_lock(&head->mutex);
2143 mutex_unlock(&head->mutex);
2144
2145 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002146 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002147 goto again;
2148 }
2149 node = rb_next(node);
2150 }
2151 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002152 schedule_timeout(1);
2153 goto again;
2154 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002155out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002156 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002157 return 0;
2158}
2159
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002160int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2161 struct btrfs_root *root,
2162 u64 bytenr, u64 num_bytes, u64 flags,
2163 int is_data)
2164{
2165 struct btrfs_delayed_extent_op *extent_op;
2166 int ret;
2167
2168 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2169 if (!extent_op)
2170 return -ENOMEM;
2171
2172 extent_op->flags_to_set = flags;
2173 extent_op->update_flags = 1;
2174 extent_op->update_key = 0;
2175 extent_op->is_data = is_data ? 1 : 0;
2176
2177 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2178 if (ret)
2179 kfree(extent_op);
2180 return ret;
2181}
2182
2183static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2184 struct btrfs_root *root,
2185 struct btrfs_path *path,
2186 u64 objectid, u64 offset, u64 bytenr)
2187{
2188 struct btrfs_delayed_ref_head *head;
2189 struct btrfs_delayed_ref_node *ref;
2190 struct btrfs_delayed_data_ref *data_ref;
2191 struct btrfs_delayed_ref_root *delayed_refs;
2192 struct rb_node *node;
2193 int ret = 0;
2194
2195 ret = -ENOENT;
2196 delayed_refs = &trans->transaction->delayed_refs;
2197 spin_lock(&delayed_refs->lock);
2198 head = btrfs_find_delayed_ref_head(trans, bytenr);
2199 if (!head)
2200 goto out;
2201
2202 if (!mutex_trylock(&head->mutex)) {
2203 atomic_inc(&head->node.refs);
2204 spin_unlock(&delayed_refs->lock);
2205
2206 btrfs_release_path(root->fs_info->extent_root, path);
2207
2208 mutex_lock(&head->mutex);
2209 mutex_unlock(&head->mutex);
2210 btrfs_put_delayed_ref(&head->node);
2211 return -EAGAIN;
2212 }
2213
2214 node = rb_prev(&head->node.rb_node);
2215 if (!node)
2216 goto out_unlock;
2217
2218 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2219
2220 if (ref->bytenr != bytenr)
2221 goto out_unlock;
2222
2223 ret = 1;
2224 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2225 goto out_unlock;
2226
2227 data_ref = btrfs_delayed_node_to_data_ref(ref);
2228
2229 node = rb_prev(node);
2230 if (node) {
2231 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2232 if (ref->bytenr == bytenr)
2233 goto out_unlock;
2234 }
2235
2236 if (data_ref->root != root->root_key.objectid ||
2237 data_ref->objectid != objectid || data_ref->offset != offset)
2238 goto out_unlock;
2239
2240 ret = 0;
2241out_unlock:
2242 mutex_unlock(&head->mutex);
2243out:
2244 spin_unlock(&delayed_refs->lock);
2245 return ret;
2246}
2247
2248static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2249 struct btrfs_root *root,
2250 struct btrfs_path *path,
2251 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002252{
2253 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002254 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002255 struct btrfs_extent_data_ref *ref;
2256 struct btrfs_extent_inline_ref *iref;
2257 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002258 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002259 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002260 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002261
Chris Masonbe20aa92007-12-17 20:14:01 -05002262 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002263 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002264 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002265
Chris Masonbe20aa92007-12-17 20:14:01 -05002266 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2267 if (ret < 0)
2268 goto out;
2269 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002270
2271 ret = -ENOENT;
2272 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002273 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002274
Zheng Yan31840ae2008-09-23 13:14:14 -04002275 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002276 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002277 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002278
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002279 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002280 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002281
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002282 ret = 1;
2283 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2284#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2285 if (item_size < sizeof(*ei)) {
2286 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2287 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002288 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002289#endif
2290 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2291
2292 if (item_size != sizeof(*ei) +
2293 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2294 goto out;
2295
2296 if (btrfs_extent_generation(leaf, ei) <=
2297 btrfs_root_last_snapshot(&root->root_item))
2298 goto out;
2299
2300 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2301 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2302 BTRFS_EXTENT_DATA_REF_KEY)
2303 goto out;
2304
2305 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2306 if (btrfs_extent_refs(leaf, ei) !=
2307 btrfs_extent_data_ref_count(leaf, ref) ||
2308 btrfs_extent_data_ref_root(leaf, ref) !=
2309 root->root_key.objectid ||
2310 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2311 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2312 goto out;
2313
Yan Zhengf321e492008-07-30 09:26:11 -04002314 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002315out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002316 return ret;
2317}
2318
2319int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2320 struct btrfs_root *root,
2321 u64 objectid, u64 offset, u64 bytenr)
2322{
2323 struct btrfs_path *path;
2324 int ret;
2325 int ret2;
2326
2327 path = btrfs_alloc_path();
2328 if (!path)
2329 return -ENOENT;
2330
2331 do {
2332 ret = check_committed_ref(trans, root, path, objectid,
2333 offset, bytenr);
2334 if (ret && ret != -ENOENT)
2335 goto out;
2336
2337 ret2 = check_delayed_ref(trans, root, path, objectid,
2338 offset, bytenr);
2339 } while (ret2 == -EAGAIN);
2340
2341 if (ret2 && ret2 != -ENOENT) {
2342 ret = ret2;
2343 goto out;
2344 }
2345
2346 if (ret != -ENOENT || ret2 != -ENOENT)
2347 ret = 0;
2348out:
Yan Zhengf321e492008-07-30 09:26:11 -04002349 btrfs_free_path(path);
2350 return ret;
2351}
2352
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002353#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002354int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2355 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002356{
Chris Mason5f39d392007-10-15 16:14:19 -04002357 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002358 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002359 u64 root_gen;
2360 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002361 int i;
Chris Masondb945352007-10-15 16:15:53 -04002362 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002363 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002364 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002365
Chris Mason3768f362007-03-13 16:47:54 -04002366 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002367 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002368
Zheng Yane4657682008-09-26 10:04:53 -04002369 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2370 shared = 0;
2371 root_gen = root->root_key.offset;
2372 } else {
2373 shared = 1;
2374 root_gen = trans->transid - 1;
2375 }
2376
Chris Masondb945352007-10-15 16:15:53 -04002377 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002378 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002379
Zheng Yan31840ae2008-09-23 13:14:14 -04002380 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002381 struct btrfs_leaf_ref *ref;
2382 struct btrfs_extent_info *info;
2383
Zheng Yan31840ae2008-09-23 13:14:14 -04002384 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002385 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002386 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002387 goto out;
2388 }
2389
Zheng Yane4657682008-09-26 10:04:53 -04002390 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002391 ref->bytenr = buf->start;
2392 ref->owner = btrfs_header_owner(buf);
2393 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002394 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002395 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002396
Zheng Yan31840ae2008-09-23 13:14:14 -04002397 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002398 u64 disk_bytenr;
2399 btrfs_item_key_to_cpu(buf, &key, i);
2400 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2401 continue;
2402 fi = btrfs_item_ptr(buf, i,
2403 struct btrfs_file_extent_item);
2404 if (btrfs_file_extent_type(buf, fi) ==
2405 BTRFS_FILE_EXTENT_INLINE)
2406 continue;
2407 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2408 if (disk_bytenr == 0)
2409 continue;
2410
2411 info->bytenr = disk_bytenr;
2412 info->num_bytes =
2413 btrfs_file_extent_disk_num_bytes(buf, fi);
2414 info->objectid = key.objectid;
2415 info->offset = key.offset;
2416 info++;
2417 }
2418
Zheng Yane4657682008-09-26 10:04:53 -04002419 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002420 if (ret == -EEXIST && shared) {
2421 struct btrfs_leaf_ref *old;
2422 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2423 BUG_ON(!old);
2424 btrfs_remove_leaf_ref(root, old);
2425 btrfs_free_leaf_ref(root, old);
2426 ret = btrfs_add_leaf_ref(root, ref, shared);
2427 }
Yan Zheng31153d82008-07-28 15:32:19 -04002428 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002429 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002430 }
2431out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002432 return ret;
2433}
2434
Chris Masonb7a9f292009-02-04 09:23:45 -05002435/* when a block goes through cow, we update the reference counts of
2436 * everything that block points to. The internal pointers of the block
2437 * can be in just about any order, and it is likely to have clusters of
2438 * things that are close together and clusters of things that are not.
2439 *
2440 * To help reduce the seeks that come with updating all of these reference
2441 * counts, sort them by byte number before actual updates are done.
2442 *
2443 * struct refsort is used to match byte number to slot in the btree block.
2444 * we sort based on the byte number and then use the slot to actually
2445 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002446 *
2447 * struct refsort is smaller than strcut btrfs_item and smaller than
2448 * struct btrfs_key_ptr. Since we're currently limited to the page size
2449 * for a btree block, there's no way for a kmalloc of refsorts for a
2450 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002451 */
2452struct refsort {
2453 u64 bytenr;
2454 u32 slot;
2455};
2456
2457/*
2458 * for passing into sort()
2459 */
2460static int refsort_cmp(const void *a_void, const void *b_void)
2461{
2462 const struct refsort *a = a_void;
2463 const struct refsort *b = b_void;
2464
2465 if (a->bytenr < b->bytenr)
2466 return -1;
2467 if (a->bytenr > b->bytenr)
2468 return 1;
2469 return 0;
2470}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002471#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002472
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002473static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002474 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002475 struct extent_buffer *buf,
2476 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002477{
2478 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002479 u64 num_bytes;
2480 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002481 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002482 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002483 struct btrfs_key key;
2484 struct btrfs_file_extent_item *fi;
2485 int i;
2486 int level;
2487 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002488 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002489 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002490
2491 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002492 nritems = btrfs_header_nritems(buf);
2493 level = btrfs_header_level(buf);
2494
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002495 if (!root->ref_cows && level == 0)
2496 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002497
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002498 if (inc)
2499 process_func = btrfs_inc_extent_ref;
2500 else
2501 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002502
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002503 if (full_backref)
2504 parent = buf->start;
2505 else
2506 parent = 0;
2507
Zheng Yan31840ae2008-09-23 13:14:14 -04002508 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002509 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002510 btrfs_item_key_to_cpu(buf, &key, i);
2511 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002512 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002513 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002514 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002515 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002516 BTRFS_FILE_EXTENT_INLINE)
2517 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002518 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2519 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002520 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002521
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002522 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2523 key.offset -= btrfs_file_extent_offset(buf, fi);
2524 ret = process_func(trans, root, bytenr, num_bytes,
2525 parent, ref_root, key.objectid,
2526 key.offset);
2527 if (ret)
2528 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002529 } else {
2530 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002531 num_bytes = btrfs_level_size(root, level - 1);
2532 ret = process_func(trans, root, bytenr, num_bytes,
2533 parent, ref_root, level - 1, 0);
2534 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002535 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002536 }
2537 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002538 return 0;
2539fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002540 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002541 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002542}
2543
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002544int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2545 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002546{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002547 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2548}
Zheng Yan31840ae2008-09-23 13:14:14 -04002549
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002550int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2551 struct extent_buffer *buf, int full_backref)
2552{
2553 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002554}
2555
Chris Mason9078a3e2007-04-26 16:46:15 -04002556static int write_one_cache_group(struct btrfs_trans_handle *trans,
2557 struct btrfs_root *root,
2558 struct btrfs_path *path,
2559 struct btrfs_block_group_cache *cache)
2560{
2561 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002562 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002563 unsigned long bi;
2564 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002565
Chris Mason9078a3e2007-04-26 16:46:15 -04002566 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002567 if (ret < 0)
2568 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002569 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002570
2571 leaf = path->nodes[0];
2572 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2573 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2574 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002575 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002576fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002577 if (ret)
2578 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002579 return 0;
2580
2581}
2582
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002583static struct btrfs_block_group_cache *
2584next_block_group(struct btrfs_root *root,
2585 struct btrfs_block_group_cache *cache)
2586{
2587 struct rb_node *node;
2588 spin_lock(&root->fs_info->block_group_cache_lock);
2589 node = rb_next(&cache->cache_node);
2590 btrfs_put_block_group(cache);
2591 if (node) {
2592 cache = rb_entry(node, struct btrfs_block_group_cache,
2593 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002594 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002595 } else
2596 cache = NULL;
2597 spin_unlock(&root->fs_info->block_group_cache_lock);
2598 return cache;
2599}
2600
Chris Mason96b51792007-10-15 16:15:19 -04002601int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2602 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002603{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002604 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002605 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002606 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002607 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002608
2609 path = btrfs_alloc_path();
2610 if (!path)
2611 return -ENOMEM;
2612
Chris Masond3977122009-01-05 21:25:51 -05002613 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002614 if (last == 0) {
2615 err = btrfs_run_delayed_refs(trans, root,
2616 (unsigned long)-1);
2617 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002618 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002619
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002620 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2621 while (cache) {
2622 if (cache->dirty)
2623 break;
2624 cache = next_block_group(root, cache);
2625 }
2626 if (!cache) {
2627 if (last == 0)
2628 break;
2629 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002630 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002631 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002632
2633 cache->dirty = 0;
2634 last = cache->key.objectid + cache->key.offset;
2635
2636 err = write_one_cache_group(trans, root, path, cache);
2637 BUG_ON(err);
2638 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002639 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002640
Chris Mason9078a3e2007-04-26 16:46:15 -04002641 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002642 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002643}
2644
Yan Zhengd2fb3432008-12-11 16:30:39 -05002645int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2646{
2647 struct btrfs_block_group_cache *block_group;
2648 int readonly = 0;
2649
2650 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2651 if (!block_group || block_group->ro)
2652 readonly = 1;
2653 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002654 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002655 return readonly;
2656}
2657
Chris Mason593060d2008-03-25 16:50:33 -04002658static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2659 u64 total_bytes, u64 bytes_used,
2660 struct btrfs_space_info **space_info)
2661{
2662 struct btrfs_space_info *found;
2663
2664 found = __find_space_info(info, flags);
2665 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002666 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002667 found->total_bytes += total_bytes;
2668 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002669 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002670 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002671 *space_info = found;
2672 return 0;
2673 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002674 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002675 if (!found)
2676 return -ENOMEM;
2677
Josef Bacik0f9dd462008-09-23 13:14:11 -04002678 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002679 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002680 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002681 found->flags = flags;
2682 found->total_bytes = total_bytes;
2683 found->bytes_used = bytes_used;
2684 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002685 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002686 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002687 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002688 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002689 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002690 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002691 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002692 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002693 return 0;
2694}
2695
Chris Mason8790d502008-04-03 16:29:03 -04002696static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2697{
2698 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002699 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002700 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002701 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002702 if (extra_flags) {
2703 if (flags & BTRFS_BLOCK_GROUP_DATA)
2704 fs_info->avail_data_alloc_bits |= extra_flags;
2705 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2706 fs_info->avail_metadata_alloc_bits |= extra_flags;
2707 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2708 fs_info->avail_system_alloc_bits |= extra_flags;
2709 }
2710}
Chris Mason593060d2008-03-25 16:50:33 -04002711
Yan Zhengc146afa2008-11-12 14:34:12 -05002712static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2713{
2714 spin_lock(&cache->space_info->lock);
2715 spin_lock(&cache->lock);
2716 if (!cache->ro) {
2717 cache->space_info->bytes_readonly += cache->key.offset -
2718 btrfs_block_group_used(&cache->item);
2719 cache->ro = 1;
2720 }
2721 spin_unlock(&cache->lock);
2722 spin_unlock(&cache->space_info->lock);
2723}
2724
Yan Zheng2b820322008-11-17 21:11:30 -05002725u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002726{
Yan Zheng2b820322008-11-17 21:11:30 -05002727 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002728
2729 if (num_devices == 1)
2730 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2731 if (num_devices < 4)
2732 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2733
Chris Masonec44a352008-04-28 15:29:52 -04002734 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2735 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002736 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002737 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002738 }
Chris Masonec44a352008-04-28 15:29:52 -04002739
2740 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002741 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002742 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002743 }
Chris Masonec44a352008-04-28 15:29:52 -04002744
2745 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2746 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2747 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2748 (flags & BTRFS_BLOCK_GROUP_DUP)))
2749 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2750 return flags;
2751}
2752
Josef Bacik6a632092009-02-20 11:00:09 -05002753static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2754{
2755 struct btrfs_fs_info *info = root->fs_info;
2756 u64 alloc_profile;
2757
2758 if (data) {
2759 alloc_profile = info->avail_data_alloc_bits &
2760 info->data_alloc_profile;
2761 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2762 } else if (root == root->fs_info->chunk_root) {
2763 alloc_profile = info->avail_system_alloc_bits &
2764 info->system_alloc_profile;
2765 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2766 } else {
2767 alloc_profile = info->avail_metadata_alloc_bits &
2768 info->metadata_alloc_profile;
2769 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2770 }
2771
2772 return btrfs_reduce_alloc_profile(root, data);
2773}
2774
2775void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2776{
2777 u64 alloc_target;
2778
2779 alloc_target = btrfs_get_alloc_profile(root, 1);
2780 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2781 alloc_target);
2782}
2783
Josef Bacik9ed74f22009-09-11 16:12:44 -04002784static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2785{
2786 u64 num_bytes;
2787 int level;
2788
2789 level = BTRFS_MAX_LEVEL - 2;
2790 /*
2791 * NOTE: these calculations are absolutely the worst possible case.
2792 * This assumes that _every_ item we insert will require a new leaf, and
2793 * that the tree has grown to its maximum level size.
2794 */
2795
2796 /*
2797 * for every item we insert we could insert both an extent item and a
2798 * extent ref item. Then for ever item we insert, we will need to cow
2799 * both the original leaf, plus the leaf to the left and right of it.
2800 *
2801 * Unless we are talking about the extent root, then we just want the
2802 * number of items * 2, since we just need the extent item plus its ref.
2803 */
2804 if (root == root->fs_info->extent_root)
2805 num_bytes = num_items * 2;
2806 else
2807 num_bytes = (num_items + (2 * num_items)) * 3;
2808
2809 /*
2810 * num_bytes is total number of leaves we could need times the leaf
2811 * size, and then for every leaf we could end up cow'ing 2 nodes per
2812 * level, down to the leaf level.
2813 */
2814 num_bytes = (num_bytes * root->leafsize) +
2815 (num_bytes * (level * 2)) * root->nodesize;
2816
2817 return num_bytes;
2818}
2819
Josef Bacik6a632092009-02-20 11:00:09 -05002820/*
Josef Bacik9ed74f22009-09-11 16:12:44 -04002821 * Unreserve metadata space for delalloc. If we have less reserved credits than
2822 * we have extents, this function does nothing.
Josef Bacik6a632092009-02-20 11:00:09 -05002823 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04002824int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2825 struct inode *inode, int num_items)
Josef Bacik6a632092009-02-20 11:00:09 -05002826{
2827 struct btrfs_fs_info *info = root->fs_info;
2828 struct btrfs_space_info *meta_sinfo;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002829 u64 num_bytes;
2830 u64 alloc_target;
2831 bool bug = false;
Josef Bacik6a632092009-02-20 11:00:09 -05002832
2833 /* get the space info for where the metadata will live */
2834 alloc_target = btrfs_get_alloc_profile(root, 0);
2835 meta_sinfo = __find_space_info(info, alloc_target);
2836
Josef Bacik9ed74f22009-09-11 16:12:44 -04002837 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2838 num_items);
2839
2840 spin_lock(&meta_sinfo->lock);
Josef Bacik32c00af2009-10-08 13:34:05 -04002841 spin_lock(&BTRFS_I(inode)->accounting_lock);
2842 if (BTRFS_I(inode)->reserved_extents <=
2843 BTRFS_I(inode)->outstanding_extents) {
2844 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002845 spin_unlock(&meta_sinfo->lock);
2846 return 0;
2847 }
Josef Bacik32c00af2009-10-08 13:34:05 -04002848 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002849
Josef Bacik32c00af2009-10-08 13:34:05 -04002850 BTRFS_I(inode)->reserved_extents--;
2851 BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002852
2853 if (meta_sinfo->bytes_delalloc < num_bytes) {
2854 bug = true;
2855 meta_sinfo->bytes_delalloc = 0;
2856 } else {
2857 meta_sinfo->bytes_delalloc -= num_bytes;
2858 }
2859 spin_unlock(&meta_sinfo->lock);
2860
2861 BUG_ON(bug);
2862
2863 return 0;
2864}
2865
2866static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2867{
2868 u64 thresh;
2869
2870 thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2871 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2872 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2873 meta_sinfo->bytes_may_use;
2874
2875 thresh = meta_sinfo->total_bytes - thresh;
2876 thresh *= 80;
2877 do_div(thresh, 100);
2878 if (thresh <= meta_sinfo->bytes_delalloc)
2879 meta_sinfo->force_delalloc = 1;
2880 else
2881 meta_sinfo->force_delalloc = 0;
2882}
2883
Josef Bacike3ccfa92009-10-07 20:44:34 -04002884struct async_flush {
2885 struct btrfs_root *root;
2886 struct btrfs_space_info *info;
2887 struct btrfs_work work;
2888};
2889
2890static noinline void flush_delalloc_async(struct btrfs_work *work)
2891{
2892 struct async_flush *async;
2893 struct btrfs_root *root;
2894 struct btrfs_space_info *info;
2895
2896 async = container_of(work, struct async_flush, work);
2897 root = async->root;
2898 info = async->info;
2899
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002900 btrfs_start_delalloc_inodes(root, 0);
Josef Bacike3ccfa92009-10-07 20:44:34 -04002901 wake_up(&info->flush_wait);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002902 btrfs_wait_ordered_extents(root, 0, 0);
Josef Bacike3ccfa92009-10-07 20:44:34 -04002903
2904 spin_lock(&info->lock);
2905 info->flushing = 0;
2906 spin_unlock(&info->lock);
2907 wake_up(&info->flush_wait);
2908
2909 kfree(async);
2910}
2911
2912static void wait_on_flush(struct btrfs_space_info *info)
2913{
2914 DEFINE_WAIT(wait);
2915 u64 used;
2916
2917 while (1) {
2918 prepare_to_wait(&info->flush_wait, &wait,
2919 TASK_UNINTERRUPTIBLE);
2920 spin_lock(&info->lock);
2921 if (!info->flushing) {
2922 spin_unlock(&info->lock);
2923 break;
2924 }
2925
2926 used = info->bytes_used + info->bytes_reserved +
2927 info->bytes_pinned + info->bytes_readonly +
2928 info->bytes_super + info->bytes_root +
2929 info->bytes_may_use + info->bytes_delalloc;
2930 if (used < info->total_bytes) {
2931 spin_unlock(&info->lock);
2932 break;
2933 }
2934 spin_unlock(&info->lock);
2935 schedule();
2936 }
2937 finish_wait(&info->flush_wait, &wait);
2938}
2939
Josef Bacik32c00af2009-10-08 13:34:05 -04002940static void flush_delalloc(struct btrfs_root *root,
2941 struct btrfs_space_info *info)
2942{
Josef Bacike3ccfa92009-10-07 20:44:34 -04002943 struct async_flush *async;
Josef Bacik32c00af2009-10-08 13:34:05 -04002944 bool wait = false;
2945
2946 spin_lock(&info->lock);
2947
2948 if (!info->flushing) {
2949 info->flushing = 1;
2950 init_waitqueue_head(&info->flush_wait);
2951 } else {
2952 wait = true;
2953 }
2954
2955 spin_unlock(&info->lock);
2956
2957 if (wait) {
Josef Bacike3ccfa92009-10-07 20:44:34 -04002958 wait_on_flush(info);
Josef Bacik32c00af2009-10-08 13:34:05 -04002959 return;
2960 }
2961
Josef Bacike3ccfa92009-10-07 20:44:34 -04002962 async = kzalloc(sizeof(*async), GFP_NOFS);
2963 if (!async)
2964 goto flush;
2965
2966 async->root = root;
2967 async->info = info;
2968 async->work.func = flush_delalloc_async;
2969
2970 btrfs_queue_worker(&root->fs_info->enospc_workers,
2971 &async->work);
2972 wait_on_flush(info);
2973 return;
2974
2975flush:
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002976 btrfs_start_delalloc_inodes(root, 0);
2977 btrfs_wait_ordered_extents(root, 0, 0);
Josef Bacik32c00af2009-10-08 13:34:05 -04002978
2979 spin_lock(&info->lock);
2980 info->flushing = 0;
2981 spin_unlock(&info->lock);
2982 wake_up(&info->flush_wait);
2983}
2984
Josef Bacik9ed74f22009-09-11 16:12:44 -04002985static int maybe_allocate_chunk(struct btrfs_root *root,
2986 struct btrfs_space_info *info)
2987{
2988 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
2989 struct btrfs_trans_handle *trans;
2990 bool wait = false;
2991 int ret = 0;
2992 u64 min_metadata;
2993 u64 free_space;
2994
2995 free_space = btrfs_super_total_bytes(disk_super);
2996 /*
Chris Mason33b25802009-11-11 10:16:57 -05002997 * we allow the metadata to grow to a max of either 10gb or 5% of the
Josef Bacik9ed74f22009-09-11 16:12:44 -04002998 * space in the volume.
2999 */
Chris Mason33b25802009-11-11 10:16:57 -05003000 min_metadata = min((u64)10 * 1024 * 1024 * 1024,
Josef Bacik9ed74f22009-09-11 16:12:44 -04003001 div64_u64(free_space * 5, 100));
3002 if (info->total_bytes >= min_metadata) {
3003 spin_unlock(&info->lock);
3004 return 0;
3005 }
3006
3007 if (info->full) {
3008 spin_unlock(&info->lock);
3009 return 0;
3010 }
3011
3012 if (!info->allocating_chunk) {
3013 info->force_alloc = 1;
3014 info->allocating_chunk = 1;
Josef Bacike3ccfa92009-10-07 20:44:34 -04003015 init_waitqueue_head(&info->allocate_wait);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003016 } else {
3017 wait = true;
3018 }
3019
3020 spin_unlock(&info->lock);
3021
3022 if (wait) {
Josef Bacike3ccfa92009-10-07 20:44:34 -04003023 wait_event(info->allocate_wait,
Josef Bacik9ed74f22009-09-11 16:12:44 -04003024 !info->allocating_chunk);
3025 return 1;
3026 }
3027
3028 trans = btrfs_start_transaction(root, 1);
3029 if (!trans) {
3030 ret = -ENOMEM;
3031 goto out;
3032 }
3033
3034 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3035 4096 + 2 * 1024 * 1024,
3036 info->flags, 0);
3037 btrfs_end_transaction(trans, root);
3038 if (ret)
3039 goto out;
3040out:
3041 spin_lock(&info->lock);
3042 info->allocating_chunk = 0;
3043 spin_unlock(&info->lock);
Josef Bacike3ccfa92009-10-07 20:44:34 -04003044 wake_up(&info->allocate_wait);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003045
3046 if (ret)
3047 return 0;
3048 return 1;
3049}
3050
3051/*
3052 * Reserve metadata space for delalloc.
3053 */
3054int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
3055 struct inode *inode, int num_items)
3056{
3057 struct btrfs_fs_info *info = root->fs_info;
3058 struct btrfs_space_info *meta_sinfo;
3059 u64 num_bytes;
3060 u64 used;
3061 u64 alloc_target;
3062 int flushed = 0;
3063 int force_delalloc;
3064
3065 /* get the space info for where the metadata will live */
3066 alloc_target = btrfs_get_alloc_profile(root, 0);
3067 meta_sinfo = __find_space_info(info, alloc_target);
3068
3069 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
3070 num_items);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003071again:
Josef Bacik6a632092009-02-20 11:00:09 -05003072 spin_lock(&meta_sinfo->lock);
Josef Bacik6a632092009-02-20 11:00:09 -05003073
Josef Bacik9ed74f22009-09-11 16:12:44 -04003074 force_delalloc = meta_sinfo->force_delalloc;
Josef Bacik6a632092009-02-20 11:00:09 -05003075
Josef Bacik9ed74f22009-09-11 16:12:44 -04003076 if (unlikely(!meta_sinfo->bytes_root))
3077 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3078
3079 if (!flushed)
3080 meta_sinfo->bytes_delalloc += num_bytes;
3081
3082 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3083 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3084 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3085 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3086
3087 if (used > meta_sinfo->total_bytes) {
3088 flushed++;
3089
3090 if (flushed == 1) {
3091 if (maybe_allocate_chunk(root, meta_sinfo))
3092 goto again;
3093 flushed++;
3094 } else {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003095 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003096 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003097
Josef Bacik9ed74f22009-09-11 16:12:44 -04003098 if (flushed == 2) {
3099 filemap_flush(inode->i_mapping);
3100 goto again;
3101 } else if (flushed == 3) {
Josef Bacik32c00af2009-10-08 13:34:05 -04003102 flush_delalloc(root, meta_sinfo);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003103 goto again;
3104 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003105 spin_lock(&meta_sinfo->lock);
3106 meta_sinfo->bytes_delalloc -= num_bytes;
Josef Bacik6a632092009-02-20 11:00:09 -05003107 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003108 printk(KERN_ERR "enospc, has %d, reserved %d\n",
Josef Bacik32c00af2009-10-08 13:34:05 -04003109 BTRFS_I(inode)->outstanding_extents,
3110 BTRFS_I(inode)->reserved_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003111 dump_space_info(meta_sinfo, 0, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003112 return -ENOSPC;
3113 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003114
Josef Bacik32c00af2009-10-08 13:34:05 -04003115 BTRFS_I(inode)->reserved_extents++;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003116 check_force_delalloc(meta_sinfo);
3117 spin_unlock(&meta_sinfo->lock);
3118
3119 if (!flushed && force_delalloc)
3120 filemap_flush(inode->i_mapping);
3121
3122 return 0;
3123}
3124
3125/*
3126 * unreserve num_items number of items worth of metadata space. This needs to
3127 * be paired with btrfs_reserve_metadata_space.
3128 *
3129 * NOTE: if you have the option, run this _AFTER_ you do a
3130 * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
3131 * oprations which will result in more used metadata, so we want to make sure we
3132 * can do that without issue.
3133 */
3134int btrfs_unreserve_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 alloc_target;
3140 bool bug = false;
3141
3142 /* get the space info for where the metadata will live */
3143 alloc_target = btrfs_get_alloc_profile(root, 0);
3144 meta_sinfo = __find_space_info(info, alloc_target);
3145
3146 num_bytes = calculate_bytes_needed(root, num_items);
3147
3148 spin_lock(&meta_sinfo->lock);
3149 if (meta_sinfo->bytes_may_use < num_bytes) {
3150 bug = true;
3151 meta_sinfo->bytes_may_use = 0;
3152 } else {
3153 meta_sinfo->bytes_may_use -= num_bytes;
3154 }
3155 spin_unlock(&meta_sinfo->lock);
3156
3157 BUG_ON(bug);
3158
3159 return 0;
3160}
3161
3162/*
3163 * Reserve some metadata space for use. We'll calculate the worste case number
3164 * of bytes that would be needed to modify num_items number of items. If we
3165 * have space, fantastic, if not, you get -ENOSPC. Please call
3166 * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
3167 * items you reserved, since whatever metadata you needed should have already
3168 * been allocated.
3169 *
3170 * This will commit the transaction to make more space if we don't have enough
3171 * metadata space. THe only time we don't do this is if we're reserving space
3172 * inside of a transaction, then we will just return -ENOSPC and it is the
3173 * callers responsibility to handle it properly.
3174 */
3175int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3176{
3177 struct btrfs_fs_info *info = root->fs_info;
3178 struct btrfs_space_info *meta_sinfo;
3179 u64 num_bytes;
3180 u64 used;
3181 u64 alloc_target;
3182 int retries = 0;
3183
3184 /* get the space info for where the metadata will live */
3185 alloc_target = btrfs_get_alloc_profile(root, 0);
3186 meta_sinfo = __find_space_info(info, alloc_target);
3187
3188 num_bytes = calculate_bytes_needed(root, num_items);
3189again:
3190 spin_lock(&meta_sinfo->lock);
3191
3192 if (unlikely(!meta_sinfo->bytes_root))
3193 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3194
3195 if (!retries)
3196 meta_sinfo->bytes_may_use += num_bytes;
3197
3198 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3199 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3200 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3201 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3202
3203 if (used > meta_sinfo->total_bytes) {
3204 retries++;
3205 if (retries == 1) {
3206 if (maybe_allocate_chunk(root, meta_sinfo))
3207 goto again;
3208 retries++;
3209 } else {
3210 spin_unlock(&meta_sinfo->lock);
3211 }
3212
3213 if (retries == 2) {
Josef Bacik32c00af2009-10-08 13:34:05 -04003214 flush_delalloc(root, meta_sinfo);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003215 goto again;
3216 }
3217 spin_lock(&meta_sinfo->lock);
3218 meta_sinfo->bytes_may_use -= num_bytes;
3219 spin_unlock(&meta_sinfo->lock);
3220
3221 dump_space_info(meta_sinfo, 0, 0);
3222 return -ENOSPC;
3223 }
3224
3225 check_force_delalloc(meta_sinfo);
Josef Bacik6a632092009-02-20 11:00:09 -05003226 spin_unlock(&meta_sinfo->lock);
3227
3228 return 0;
3229}
3230
3231/*
3232 * This will check the space that the inode allocates from to make sure we have
3233 * enough space for bytes.
3234 */
3235int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3236 u64 bytes)
3237{
3238 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003239 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003240
3241 /* make sure bytes are sectorsize aligned */
3242 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3243
3244 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003245 if (!data_sinfo)
3246 goto alloc;
3247
Josef Bacik6a632092009-02-20 11:00:09 -05003248again:
3249 /* make sure we have enough space to handle the data first */
3250 spin_lock(&data_sinfo->lock);
3251 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
3252 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
3253 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
Josef Bacik1b2da372009-09-11 16:11:20 -04003254 data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003255 struct btrfs_trans_handle *trans;
3256
Josef Bacik6a632092009-02-20 11:00:09 -05003257 /*
3258 * if we don't have enough free bytes in this space then we need
3259 * to alloc a new chunk.
3260 */
3261 if (!data_sinfo->full) {
3262 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003263
3264 data_sinfo->force_alloc = 1;
3265 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003266alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003267 alloc_target = btrfs_get_alloc_profile(root, 1);
3268 trans = btrfs_start_transaction(root, 1);
3269 if (!trans)
3270 return -ENOMEM;
3271
3272 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3273 bytes + 2 * 1024 * 1024,
3274 alloc_target, 0);
3275 btrfs_end_transaction(trans, root);
3276 if (ret)
3277 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04003278
3279 if (!data_sinfo) {
3280 btrfs_set_inode_space_info(root, inode);
3281 data_sinfo = BTRFS_I(inode)->space_info;
3282 }
Josef Bacik6a632092009-02-20 11:00:09 -05003283 goto again;
3284 }
3285 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003286
3287 /* commit the current transaction and try again */
Sage Weildd7e0b72009-09-29 18:38:44 -04003288 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003289 committed = 1;
3290 trans = btrfs_join_transaction(root, 1);
3291 if (!trans)
3292 return -ENOMEM;
3293 ret = btrfs_commit_transaction(trans, root);
3294 if (ret)
3295 return ret;
3296 goto again;
3297 }
3298
Josef Bacik6a632092009-02-20 11:00:09 -05003299 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3300 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04003301 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07003302 "%llu total\n", (unsigned long long)bytes,
3303 (unsigned long long)data_sinfo->bytes_delalloc,
3304 (unsigned long long)data_sinfo->bytes_used,
3305 (unsigned long long)data_sinfo->bytes_reserved,
3306 (unsigned long long)data_sinfo->bytes_pinned,
3307 (unsigned long long)data_sinfo->bytes_readonly,
3308 (unsigned long long)data_sinfo->bytes_may_use,
3309 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003310 return -ENOSPC;
3311 }
3312 data_sinfo->bytes_may_use += bytes;
3313 BTRFS_I(inode)->reserved_bytes += bytes;
3314 spin_unlock(&data_sinfo->lock);
3315
Josef Bacik9ed74f22009-09-11 16:12:44 -04003316 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003317}
3318
3319/*
3320 * if there was an error for whatever reason after calling
3321 * btrfs_check_data_free_space, call this so we can cleanup the counters.
3322 */
3323void btrfs_free_reserved_data_space(struct btrfs_root *root,
3324 struct inode *inode, u64 bytes)
3325{
3326 struct btrfs_space_info *data_sinfo;
3327
3328 /* make sure bytes are sectorsize aligned */
3329 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3330
3331 data_sinfo = BTRFS_I(inode)->space_info;
3332 spin_lock(&data_sinfo->lock);
3333 data_sinfo->bytes_may_use -= bytes;
3334 BTRFS_I(inode)->reserved_bytes -= bytes;
3335 spin_unlock(&data_sinfo->lock);
3336}
3337
3338/* called when we are adding a delalloc extent to the inode's io_tree */
3339void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3340 u64 bytes)
3341{
3342 struct btrfs_space_info *data_sinfo;
3343
3344 /* get the space info for where this inode will be storing its data */
3345 data_sinfo = BTRFS_I(inode)->space_info;
3346
3347 /* make sure we have enough space to handle the data first */
3348 spin_lock(&data_sinfo->lock);
3349 data_sinfo->bytes_delalloc += bytes;
3350
3351 /*
3352 * we are adding a delalloc extent without calling
3353 * btrfs_check_data_free_space first. This happens on a weird
3354 * writepage condition, but shouldn't hurt our accounting
3355 */
3356 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3357 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3358 BTRFS_I(inode)->reserved_bytes = 0;
3359 } else {
3360 data_sinfo->bytes_may_use -= bytes;
3361 BTRFS_I(inode)->reserved_bytes -= bytes;
3362 }
3363
3364 spin_unlock(&data_sinfo->lock);
3365}
3366
3367/* called when we are clearing an delalloc extent from the inode's io_tree */
3368void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3369 u64 bytes)
3370{
3371 struct btrfs_space_info *info;
3372
3373 info = BTRFS_I(inode)->space_info;
3374
3375 spin_lock(&info->lock);
3376 info->bytes_delalloc -= bytes;
3377 spin_unlock(&info->lock);
3378}
3379
Josef Bacik97e728d2009-04-21 17:40:57 -04003380static void force_metadata_allocation(struct btrfs_fs_info *info)
3381{
3382 struct list_head *head = &info->space_info;
3383 struct btrfs_space_info *found;
3384
3385 rcu_read_lock();
3386 list_for_each_entry_rcu(found, head, list) {
3387 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3388 found->force_alloc = 1;
3389 }
3390 rcu_read_unlock();
3391}
3392
Chris Mason6324fbf2008-03-24 15:01:59 -04003393static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3394 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003395 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003396{
3397 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003398 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003399 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05003400 int ret = 0;
3401
Josef Bacik97e728d2009-04-21 17:40:57 -04003402 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003403
Yan Zheng2b820322008-11-17 21:11:30 -05003404 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003405
Chris Mason6324fbf2008-03-24 15:01:59 -04003406 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003407 if (!space_info) {
3408 ret = update_space_info(extent_root->fs_info, flags,
3409 0, 0, &space_info);
3410 BUG_ON(ret);
3411 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003412 BUG_ON(!space_info);
3413
Josef Bacik25179202008-10-29 14:49:05 -04003414 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003415 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003416 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003417 if (space_info->full) {
3418 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003419 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003420 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003421
Yan Zhengc146afa2008-11-12 14:34:12 -05003422 thresh = space_info->total_bytes - space_info->bytes_readonly;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003423 thresh = div_factor(thresh, 8);
Chris Mason0ef3e662008-05-24 14:04:53 -04003424 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04003425 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04003426 space_info->bytes_reserved + alloc_bytes) < thresh) {
3427 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003428 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003429 }
Josef Bacik25179202008-10-29 14:49:05 -04003430 spin_unlock(&space_info->lock);
3431
Josef Bacik97e728d2009-04-21 17:40:57 -04003432 /*
3433 * if we're doing a data chunk, go ahead and make sure that
3434 * we keep a reasonable number of metadata chunks allocated in the
3435 * FS as well.
3436 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003437 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003438 fs_info->data_chunk_allocations++;
3439 if (!(fs_info->data_chunk_allocations %
3440 fs_info->metadata_ratio))
3441 force_metadata_allocation(fs_info);
3442 }
3443
Yan Zheng2b820322008-11-17 21:11:30 -05003444 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003445 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003446 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003447 space_info->full = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003448 space_info->force_alloc = 0;
3449 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003450out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003451 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003452 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003453}
3454
Chris Mason9078a3e2007-04-26 16:46:15 -04003455static int update_block_group(struct btrfs_trans_handle *trans,
3456 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04003457 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04003458 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04003459{
3460 struct btrfs_block_group_cache *cache;
3461 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04003462 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003463 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003464 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04003465
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003466 /* block accounting for super block */
3467 spin_lock(&info->delalloc_lock);
3468 old_val = btrfs_super_bytes_used(&info->super_copy);
3469 if (alloc)
3470 old_val += num_bytes;
3471 else
3472 old_val -= num_bytes;
3473 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003474 spin_unlock(&info->delalloc_lock);
3475
Chris Masond3977122009-01-05 21:25:51 -05003476 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003477 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003478 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04003479 return -1;
Chris Masondb945352007-10-15 16:15:53 -04003480 byte_in_group = bytenr - cache->key.objectid;
3481 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003482
Josef Bacik25179202008-10-29 14:49:05 -04003483 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003484 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003485 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04003486 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04003487 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04003488 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04003489 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003490 btrfs_set_block_group_used(&cache->item, old_val);
3491 cache->reserved -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003492 cache->space_info->bytes_used += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003493 cache->space_info->bytes_reserved -= num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003494 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05003495 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003496 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003497 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003498 } else {
Chris Masondb945352007-10-15 16:15:53 -04003499 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003500 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05003501 if (cache->ro)
3502 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003503 btrfs_set_block_group_used(&cache->item, old_val);
3504 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003505 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04003506 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003507 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003508
3509 ret = btrfs_discard_extent(root, bytenr,
3510 num_bytes);
3511 WARN_ON(ret);
3512
Josef Bacik0f9dd462008-09-23 13:14:11 -04003513 ret = btrfs_add_free_space(cache, bytenr,
3514 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003515 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04003516 }
Chris Masoncd1bc462007-04-27 10:08:34 -04003517 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003518 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003519 total -= num_bytes;
3520 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003521 }
3522 return 0;
3523}
Chris Mason6324fbf2008-03-24 15:01:59 -04003524
Chris Masona061fc82008-05-07 11:43:44 -04003525static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3526{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003527 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003528 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003529
3530 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3531 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003532 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003533
Yan Zhengd2fb3432008-12-11 16:30:39 -05003534 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003535 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003536
3537 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003538}
3539
Yan Zheng11833d62009-09-11 16:11:19 -04003540/*
3541 * this function must be called within transaction
3542 */
3543int btrfs_pin_extent(struct btrfs_root *root,
3544 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05003545{
Yan324ae4d2007-11-16 14:57:08 -05003546 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -04003547 struct btrfs_block_group_cache *cache;
Yan324ae4d2007-11-16 14:57:08 -05003548
Yan Zheng11833d62009-09-11 16:11:19 -04003549 cache = btrfs_lookup_block_group(fs_info, bytenr);
3550 BUG_ON(!cache);
Chris Masonb9473432009-03-13 11:00:37 -04003551
Yan Zheng11833d62009-09-11 16:11:19 -04003552 spin_lock(&cache->space_info->lock);
3553 spin_lock(&cache->lock);
3554 cache->pinned += num_bytes;
3555 cache->space_info->bytes_pinned += num_bytes;
3556 if (reserved) {
3557 cache->reserved -= num_bytes;
3558 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05003559 }
Yan Zheng11833d62009-09-11 16:11:19 -04003560 spin_unlock(&cache->lock);
3561 spin_unlock(&cache->space_info->lock);
3562
3563 btrfs_put_block_group(cache);
3564
3565 set_extent_dirty(fs_info->pinned_extents,
3566 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Yan324ae4d2007-11-16 14:57:08 -05003567 return 0;
3568}
Chris Mason9078a3e2007-04-26 16:46:15 -04003569
Yan Zheng11833d62009-09-11 16:11:19 -04003570static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3571 u64 num_bytes, int reserve)
Zheng Yane8569812008-09-26 10:05:48 -04003572{
Yan Zheng11833d62009-09-11 16:11:19 -04003573 spin_lock(&cache->space_info->lock);
3574 spin_lock(&cache->lock);
3575 if (reserve) {
3576 cache->reserved += num_bytes;
3577 cache->space_info->bytes_reserved += num_bytes;
3578 } else {
3579 cache->reserved -= num_bytes;
3580 cache->space_info->bytes_reserved -= num_bytes;
3581 }
3582 spin_unlock(&cache->lock);
3583 spin_unlock(&cache->space_info->lock);
3584 return 0;
3585}
Zheng Yane8569812008-09-26 10:05:48 -04003586
Yan Zheng11833d62009-09-11 16:11:19 -04003587int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3588 struct btrfs_root *root)
3589{
3590 struct btrfs_fs_info *fs_info = root->fs_info;
3591 struct btrfs_caching_control *next;
3592 struct btrfs_caching_control *caching_ctl;
3593 struct btrfs_block_group_cache *cache;
3594
3595 down_write(&fs_info->extent_commit_sem);
3596
3597 list_for_each_entry_safe(caching_ctl, next,
3598 &fs_info->caching_block_groups, list) {
3599 cache = caching_ctl->block_group;
3600 if (block_group_cache_done(cache)) {
3601 cache->last_byte_to_unpin = (u64)-1;
3602 list_del_init(&caching_ctl->list);
3603 put_caching_control(caching_ctl);
3604 } else {
3605 cache->last_byte_to_unpin = caching_ctl->progress;
3606 }
3607 }
3608
3609 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3610 fs_info->pinned_extents = &fs_info->freed_extents[1];
3611 else
3612 fs_info->pinned_extents = &fs_info->freed_extents[0];
3613
3614 up_write(&fs_info->extent_commit_sem);
3615 return 0;
3616}
3617
3618static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3619{
3620 struct btrfs_fs_info *fs_info = root->fs_info;
3621 struct btrfs_block_group_cache *cache = NULL;
3622 u64 len;
3623
3624 while (start <= end) {
3625 if (!cache ||
3626 start >= cache->key.objectid + cache->key.offset) {
3627 if (cache)
3628 btrfs_put_block_group(cache);
3629 cache = btrfs_lookup_block_group(fs_info, start);
3630 BUG_ON(!cache);
3631 }
3632
3633 len = cache->key.objectid + cache->key.offset - start;
3634 len = min(len, end + 1 - start);
3635
3636 if (start < cache->last_byte_to_unpin) {
3637 len = min(len, cache->last_byte_to_unpin - start);
3638 btrfs_add_free_space(cache, start, len);
3639 }
Josef Bacik25179202008-10-29 14:49:05 -04003640
3641 spin_lock(&cache->space_info->lock);
3642 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003643 cache->pinned -= len;
3644 cache->space_info->bytes_pinned -= len;
Josef Bacik25179202008-10-29 14:49:05 -04003645 spin_unlock(&cache->lock);
3646 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003647
3648 start += len;
3649 }
3650
3651 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003652 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04003653 return 0;
3654}
3655
3656int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04003657 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05003658{
Yan Zheng11833d62009-09-11 16:11:19 -04003659 struct btrfs_fs_info *fs_info = root->fs_info;
3660 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04003661 u64 start;
3662 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003663 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003664
Yan Zheng11833d62009-09-11 16:11:19 -04003665 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3666 unpin = &fs_info->freed_extents[1];
3667 else
3668 unpin = &fs_info->freed_extents[0];
3669
Chris Masond3977122009-01-05 21:25:51 -05003670 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003671 ret = find_first_extent_bit(unpin, 0, &start, &end,
3672 EXTENT_DIRTY);
3673 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003674 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003675
3676 ret = btrfs_discard_extent(root, start, end + 1 - start);
3677
Chris Mason1a5bc162007-10-15 16:15:26 -04003678 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04003679 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04003680 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003681 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003682
Liu Hui1f3c79a2009-01-05 15:57:51 -05003683 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003684}
3685
Zheng Yan31840ae2008-09-23 13:14:14 -04003686static int pin_down_bytes(struct btrfs_trans_handle *trans,
3687 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003688 struct btrfs_path *path,
Yan Zheng11833d62009-09-11 16:11:19 -04003689 u64 bytenr, u64 num_bytes,
3690 int is_data, int reserved,
Chris Masonb9473432009-03-13 11:00:37 -04003691 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003692{
Chris Mason1a5bc162007-10-15 16:15:26 -04003693 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003694 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003695
Zheng Yan31840ae2008-09-23 13:14:14 -04003696 if (is_data)
3697 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003698
Chris Mason444528b2009-10-14 09:38:28 -04003699 /*
3700 * discard is sloooow, and so triggering discards on
3701 * individual btree blocks isn't a good plan. Just
3702 * pin everything in discard mode.
3703 */
3704 if (btrfs_test_opt(root, DISCARD))
3705 goto pinit;
3706
Zheng Yan31840ae2008-09-23 13:14:14 -04003707 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3708 if (!buf)
3709 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003710
Zheng Yan31840ae2008-09-23 13:14:14 -04003711 /* we can reuse a block if it hasn't been written
3712 * and it is from this transaction. We can't
3713 * reuse anything from the tree log root because
3714 * it has tiny sub-transactions.
3715 */
3716 if (btrfs_buffer_uptodate(buf, 0) &&
3717 btrfs_try_tree_lock(buf)) {
3718 u64 header_owner = btrfs_header_owner(buf);
3719 u64 header_transid = btrfs_header_generation(buf);
3720 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3721 header_transid == trans->transid &&
3722 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003723 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003724 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003725 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003726 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003727 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003728 free_extent_buffer(buf);
3729pinit:
Yan Zheng11833d62009-09-11 16:11:19 -04003730 if (path)
3731 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003732 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003733 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
Zheng Yan31840ae2008-09-23 13:14:14 -04003734
Chris Masonbe744172007-05-06 10:15:01 -04003735 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003736 return 0;
3737}
3738
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003739static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3740 struct btrfs_root *root,
3741 u64 bytenr, u64 num_bytes, u64 parent,
3742 u64 root_objectid, u64 owner_objectid,
3743 u64 owner_offset, int refs_to_drop,
3744 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003745{
Chris Masone2fa7222007-03-12 16:22:34 -04003746 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003747 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003748 struct btrfs_fs_info *info = root->fs_info;
3749 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003750 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003751 struct btrfs_extent_item *ei;
3752 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003753 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003754 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003755 int extent_slot = 0;
3756 int found_extent = 0;
3757 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003758 u32 item_size;
3759 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003760
Chris Mason5caf2a02007-04-02 11:20:42 -04003761 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003762 if (!path)
3763 return -ENOMEM;
3764
Chris Mason3c12ac72008-04-21 12:01:38 -04003765 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003766 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003767
3768 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3769 BUG_ON(!is_data && refs_to_drop != 1);
3770
3771 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3772 bytenr, num_bytes, parent,
3773 root_objectid, owner_objectid,
3774 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003775 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003776 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003777 while (extent_slot >= 0) {
3778 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003779 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003780 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003781 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003782 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3783 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003784 found_extent = 1;
3785 break;
3786 }
3787 if (path->slots[0] - extent_slot > 5)
3788 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003789 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003790 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003791#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3792 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3793 if (found_extent && item_size < sizeof(*ei))
3794 found_extent = 0;
3795#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003796 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003797 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003798 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003799 NULL, refs_to_drop,
3800 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003801 BUG_ON(ret);
3802 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003803 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003804
3805 key.objectid = bytenr;
3806 key.type = BTRFS_EXTENT_ITEM_KEY;
3807 key.offset = num_bytes;
3808
Zheng Yan31840ae2008-09-23 13:14:14 -04003809 ret = btrfs_search_slot(trans, extent_root,
3810 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003811 if (ret) {
3812 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003813 ", was looking for %llu\n", ret,
3814 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003815 btrfs_print_leaf(extent_root, path->nodes[0]);
3816 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003817 BUG_ON(ret);
3818 extent_slot = path->slots[0];
3819 }
Chris Mason7bb86312007-12-11 09:25:06 -05003820 } else {
3821 btrfs_print_leaf(extent_root, path->nodes[0]);
3822 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003823 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003824 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003825 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003826 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003827 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003828 (unsigned long long)owner_objectid,
3829 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003830 }
Chris Mason5f39d392007-10-15 16:14:19 -04003831
3832 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003833 item_size = btrfs_item_size_nr(leaf, extent_slot);
3834#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3835 if (item_size < sizeof(*ei)) {
3836 BUG_ON(found_extent || extent_slot != path->slots[0]);
3837 ret = convert_extent_item_v0(trans, extent_root, path,
3838 owner_objectid, 0);
3839 BUG_ON(ret < 0);
3840
3841 btrfs_release_path(extent_root, path);
3842 path->leave_spinning = 1;
3843
3844 key.objectid = bytenr;
3845 key.type = BTRFS_EXTENT_ITEM_KEY;
3846 key.offset = num_bytes;
3847
3848 ret = btrfs_search_slot(trans, extent_root, &key, path,
3849 -1, 1);
3850 if (ret) {
3851 printk(KERN_ERR "umm, got %d back from search"
3852 ", was looking for %llu\n", ret,
3853 (unsigned long long)bytenr);
3854 btrfs_print_leaf(extent_root, path->nodes[0]);
3855 }
3856 BUG_ON(ret);
3857 extent_slot = path->slots[0];
3858 leaf = path->nodes[0];
3859 item_size = btrfs_item_size_nr(leaf, extent_slot);
3860 }
3861#endif
3862 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003863 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003864 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003865 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3866 struct btrfs_tree_block_info *bi;
3867 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3868 bi = (struct btrfs_tree_block_info *)(ei + 1);
3869 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003870 }
3871
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003872 refs = btrfs_extent_refs(leaf, ei);
3873 BUG_ON(refs < refs_to_drop);
3874 refs -= refs_to_drop;
3875
3876 if (refs > 0) {
3877 if (extent_op)
3878 __run_delayed_extent_op(extent_op, leaf, ei);
3879 /*
3880 * In the case of inline back ref, reference count will
3881 * be updated by remove_extent_backref
3882 */
3883 if (iref) {
3884 BUG_ON(!found_extent);
3885 } else {
3886 btrfs_set_extent_refs(leaf, ei, refs);
3887 btrfs_mark_buffer_dirty(leaf);
3888 }
3889 if (found_extent) {
3890 ret = remove_extent_backref(trans, extent_root, path,
3891 iref, refs_to_drop,
3892 is_data);
3893 BUG_ON(ret);
3894 }
3895 } else {
3896 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003897 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003898
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003899 if (found_extent) {
3900 BUG_ON(is_data && refs_to_drop !=
3901 extent_data_ref_count(root, path, iref));
3902 if (iref) {
3903 BUG_ON(path->slots[0] != extent_slot);
3904 } else {
3905 BUG_ON(path->slots[0] != extent_slot + 1);
3906 path->slots[0] = extent_slot;
3907 num_to_del = 2;
3908 }
Chris Mason78fae272007-03-25 11:35:08 -04003909 }
Chris Masonb9473432009-03-13 11:00:37 -04003910
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003911 ret = pin_down_bytes(trans, root, path, bytenr,
Yan Zheng11833d62009-09-11 16:11:19 -04003912 num_bytes, is_data, 0, &must_clean);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003913 if (ret > 0)
3914 mark_free = 1;
3915 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003916 /*
3917 * it is going to be very rare for someone to be waiting
3918 * on the block we're freeing. del_items might need to
3919 * schedule, so rather than get fancy, just force it
3920 * to blocking here
3921 */
3922 if (must_clean)
3923 btrfs_set_lock_blocking(must_clean);
3924
Chris Mason952fcca2008-02-18 16:33:44 -05003925 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3926 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003927 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003928 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003929
Chris Masonb9473432009-03-13 11:00:37 -04003930 if (must_clean) {
3931 clean_tree_block(NULL, root, must_clean);
3932 btrfs_tree_unlock(must_clean);
3933 free_extent_buffer(must_clean);
3934 }
3935
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003936 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003937 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3938 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003939 } else {
3940 invalidate_mapping_pages(info->btree_inode->i_mapping,
3941 bytenr >> PAGE_CACHE_SHIFT,
3942 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003943 }
3944
Chris Masondcbdd4d2008-12-16 13:51:01 -05003945 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3946 mark_free);
3947 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003948 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003949 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003950 return ret;
3951}
3952
3953/*
Chris Mason1887be62009-03-13 10:11:24 -04003954 * when we free an extent, it is possible (and likely) that we free the last
3955 * delayed ref for that extent as well. This searches the delayed ref tree for
3956 * a given extent, and if there are no other delayed refs to be processed, it
3957 * removes it from the tree.
3958 */
3959static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3960 struct btrfs_root *root, u64 bytenr)
3961{
3962 struct btrfs_delayed_ref_head *head;
3963 struct btrfs_delayed_ref_root *delayed_refs;
3964 struct btrfs_delayed_ref_node *ref;
3965 struct rb_node *node;
3966 int ret;
3967
3968 delayed_refs = &trans->transaction->delayed_refs;
3969 spin_lock(&delayed_refs->lock);
3970 head = btrfs_find_delayed_ref_head(trans, bytenr);
3971 if (!head)
3972 goto out;
3973
3974 node = rb_prev(&head->node.rb_node);
3975 if (!node)
3976 goto out;
3977
3978 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3979
3980 /* there are still entries for this ref, we can't drop it */
3981 if (ref->bytenr == bytenr)
3982 goto out;
3983
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003984 if (head->extent_op) {
3985 if (!head->must_insert_reserved)
3986 goto out;
3987 kfree(head->extent_op);
3988 head->extent_op = NULL;
3989 }
3990
Chris Mason1887be62009-03-13 10:11:24 -04003991 /*
3992 * waiting for the lock here would deadlock. If someone else has it
3993 * locked they are already in the process of dropping it anyway
3994 */
3995 if (!mutex_trylock(&head->mutex))
3996 goto out;
3997
3998 /*
3999 * at this point we have a head with no other entries. Go
4000 * ahead and process it.
4001 */
4002 head->node.in_tree = 0;
4003 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004004
Chris Mason1887be62009-03-13 10:11:24 -04004005 delayed_refs->num_entries--;
4006
4007 /*
4008 * we don't take a ref on the node because we're removing it from the
4009 * tree, so we just steal the ref the tree was holding.
4010 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004011 delayed_refs->num_heads--;
4012 if (list_empty(&head->cluster))
4013 delayed_refs->num_heads_ready--;
4014
4015 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004016 spin_unlock(&delayed_refs->lock);
4017
4018 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004019 &head->node, head->extent_op,
4020 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04004021 BUG_ON(ret);
4022 btrfs_put_delayed_ref(&head->node);
4023 return 0;
4024out:
4025 spin_unlock(&delayed_refs->lock);
4026 return 0;
4027}
4028
Chris Mason925baed2008-06-25 16:01:30 -04004029int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004030 struct btrfs_root *root,
4031 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004032 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004033{
4034 int ret;
4035
Chris Mason56bec292009-03-13 10:10:06 -04004036 /*
4037 * tree log blocks never actually go into the extent allocation
4038 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004039 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004040 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4041 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004042 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004043 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004044 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004045 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4046 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4047 parent, root_objectid, (int)owner,
4048 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004049 BUG_ON(ret);
4050 ret = check_ref_cleanup(trans, root, bytenr);
4051 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004052 } else {
4053 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4054 parent, root_objectid, owner,
4055 offset, BTRFS_DROP_DELAYED_REF, NULL);
4056 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004057 }
Chris Mason925baed2008-06-25 16:01:30 -04004058 return ret;
4059}
4060
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00004061int btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4062 struct btrfs_root *root,
4063 u64 bytenr, u32 blocksize,
4064 u64 parent, u64 root_objectid, int level)
4065{
4066 u64 used;
4067 spin_lock(&root->node_lock);
4068 used = btrfs_root_used(&root->root_item) - blocksize;
4069 btrfs_set_root_used(&root->root_item, used);
4070 spin_unlock(&root->node_lock);
4071
4072 return btrfs_free_extent(trans, root, bytenr, blocksize,
4073 parent, root_objectid, level, 0);
4074}
4075
Chris Mason87ee04e2007-11-30 11:30:34 -05004076static u64 stripe_align(struct btrfs_root *root, u64 val)
4077{
4078 u64 mask = ((u64)root->stripesize - 1);
4079 u64 ret = (val + mask) & ~mask;
4080 return ret;
4081}
4082
Chris Masonfec577f2007-02-26 10:40:21 -05004083/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004084 * when we wait for progress in the block group caching, its because
4085 * our allocation attempt failed at least once. So, we must sleep
4086 * and let some progress happen before we try again.
4087 *
4088 * This function will sleep at least once waiting for new free space to
4089 * show up, and then it will check the block group free space numbers
4090 * for our min num_bytes. Another option is to have it go ahead
4091 * and look in the rbtree for a free extent of a given size, but this
4092 * is a good start.
4093 */
4094static noinline int
4095wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4096 u64 num_bytes)
4097{
Yan Zheng11833d62009-09-11 16:11:19 -04004098 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004099 DEFINE_WAIT(wait);
4100
Yan Zheng11833d62009-09-11 16:11:19 -04004101 caching_ctl = get_caching_control(cache);
4102 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004103 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004104
Yan Zheng11833d62009-09-11 16:11:19 -04004105 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004106 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004107
4108 put_caching_control(caching_ctl);
4109 return 0;
4110}
4111
4112static noinline int
4113wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4114{
4115 struct btrfs_caching_control *caching_ctl;
4116 DEFINE_WAIT(wait);
4117
4118 caching_ctl = get_caching_control(cache);
4119 if (!caching_ctl)
4120 return 0;
4121
4122 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4123
4124 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004125 return 0;
4126}
4127
4128enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004129 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004130 LOOP_CACHING_NOWAIT = 1,
4131 LOOP_CACHING_WAIT = 2,
4132 LOOP_ALLOC_CHUNK = 3,
4133 LOOP_NO_EMPTY_SIZE = 4,
4134};
4135
4136/*
Chris Masonfec577f2007-02-26 10:40:21 -05004137 * walks the btree of allocated extents and find a hole of a given size.
4138 * The key ins is changed to record the hole:
4139 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004140 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004141 * ins->offset == number of blocks
4142 * Any available blocks before search_start are skipped.
4143 */
Chris Masond3977122009-01-05 21:25:51 -05004144static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004145 struct btrfs_root *orig_root,
4146 u64 num_bytes, u64 empty_size,
4147 u64 search_start, u64 search_end,
4148 u64 hint_byte, struct btrfs_key *ins,
4149 u64 exclude_start, u64 exclude_nr,
4150 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004151{
Josef Bacik80eb2342008-10-29 14:49:05 -04004152 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004153 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004154 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004155 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004156 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004157 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004158 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004159 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004160 int last_ptr_loop = 0;
4161 int loop = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004162 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004163 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004164 bool failed_alloc = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004165 u64 ideal_cache_percent = 0;
4166 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004167
Chris Masondb945352007-10-15 16:15:53 -04004168 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004169 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004170 ins->objectid = 0;
4171 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004172
Josef Bacik2552d172009-04-03 10:14:19 -04004173 space_info = __find_space_info(root->fs_info, data);
4174
Chris Mason0ef3e662008-05-24 14:04:53 -04004175 if (orig_root->ref_cows || empty_size)
4176 allowed_chunk_alloc = 1;
4177
Chris Mason239b14b2008-03-24 15:02:07 -04004178 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004179 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004180 if (!btrfs_test_opt(root, SSD))
4181 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004182 }
4183
Chris Masonfa9c0d792009-04-03 09:47:43 -04004184 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4185 last_ptr = &root->fs_info->data_alloc_cluster;
4186 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004187
Chris Mason239b14b2008-03-24 15:02:07 -04004188 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004189 spin_lock(&last_ptr->lock);
4190 if (last_ptr->block_group)
4191 hint_byte = last_ptr->window_start;
4192 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004193 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004194
Chris Masona061fc82008-05-07 11:43:44 -04004195 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004196 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004197
Josef Bacik817d52f2009-07-13 21:29:25 -04004198 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004199 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004200
Josef Bacik2552d172009-04-03 10:14:19 -04004201 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004202ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004203 block_group = btrfs_lookup_block_group(root->fs_info,
4204 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004205 /*
4206 * we don't want to use the block group if it doesn't match our
4207 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004208 *
4209 * However if we are re-searching with an ideal block group
4210 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004211 */
4212 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004213 (block_group->cached != BTRFS_CACHE_NO ||
4214 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004215 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004216 if (list_empty(&block_group->list) ||
4217 block_group->ro) {
4218 /*
4219 * someone is removing this block group,
4220 * we can't jump into the have_block_group
4221 * target because our list pointers are not
4222 * valid
4223 */
4224 btrfs_put_block_group(block_group);
4225 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004226 } else {
Chris Mason44fb5512009-06-04 15:34:51 -04004227 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004228 }
Josef Bacik2552d172009-04-03 10:14:19 -04004229 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004230 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004231 }
Chris Mason42e70e72008-11-07 18:17:11 -05004232 }
Josef Bacik2552d172009-04-03 10:14:19 -04004233search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004234 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04004235 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004236 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004237 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004238
Josef Bacik11dfe352009-11-13 20:12:59 +00004239 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004240 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004241
Josef Bacik2552d172009-04-03 10:14:19 -04004242have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004243 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004244 u64 free_percent;
4245
4246 free_percent = btrfs_block_group_used(&block_group->item);
4247 free_percent *= 100;
4248 free_percent = div64_u64(free_percent,
4249 block_group->key.offset);
4250 free_percent = 100 - free_percent;
4251 if (free_percent > ideal_cache_percent &&
4252 likely(!block_group->ro)) {
4253 ideal_cache_offset = block_group->key.objectid;
4254 ideal_cache_percent = free_percent;
4255 }
4256
Josef Bacik817d52f2009-07-13 21:29:25 -04004257 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05004258 * We only want to start kthread caching if we are at
4259 * the point where we will wait for caching to make
4260 * progress, or if our ideal search is over and we've
4261 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04004262 */
4263 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05004264 (loop > LOOP_FIND_IDEAL &&
4265 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004266 ret = cache_block_group(block_group);
4267 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004268 }
Josef Bacikccf0e722009-11-10 21:23:48 -05004269 found_uncached_bg = true;
4270
4271 /*
4272 * If loop is set for cached only, try the next block
4273 * group.
4274 */
4275 if (loop == LOOP_FIND_IDEAL)
4276 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05004277 }
4278
Josef Bacik817d52f2009-07-13 21:29:25 -04004279 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05004280 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04004281 found_uncached_bg = true;
4282
Josef Bacikea6a4782008-11-20 12:16:16 -05004283 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004284 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004285
Josef Bacik0a243252009-09-11 16:11:20 -04004286 /*
4287 * Ok we want to try and use the cluster allocator, so lets look
4288 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4289 * have tried the cluster allocator plenty of times at this
4290 * point and not have found anything, so we are likely way too
4291 * fragmented for the clustering stuff to find anything, so lets
4292 * just skip it and let the allocator find whatever block it can
4293 * find
4294 */
4295 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004296 /*
4297 * the refill lock keeps out other
4298 * people trying to start a new cluster
4299 */
4300 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004301 if (last_ptr->block_group &&
4302 (last_ptr->block_group->ro ||
4303 !block_group_bits(last_ptr->block_group, data))) {
4304 offset = 0;
4305 goto refill_cluster;
4306 }
4307
Chris Masonfa9c0d792009-04-03 09:47:43 -04004308 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4309 num_bytes, search_start);
4310 if (offset) {
4311 /* we have a block, we're done */
4312 spin_unlock(&last_ptr->refill_lock);
4313 goto checks;
4314 }
4315
4316 spin_lock(&last_ptr->lock);
4317 /*
4318 * whoops, this cluster doesn't actually point to
4319 * this block group. Get a ref on the block
4320 * group is does point to and try again
4321 */
4322 if (!last_ptr_loop && last_ptr->block_group &&
4323 last_ptr->block_group != block_group) {
4324
4325 btrfs_put_block_group(block_group);
4326 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00004327 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004328 spin_unlock(&last_ptr->lock);
4329 spin_unlock(&last_ptr->refill_lock);
4330
4331 last_ptr_loop = 1;
4332 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04004333 /*
4334 * we know this block group is properly
4335 * in the list because
4336 * btrfs_remove_block_group, drops the
4337 * cluster before it removes the block
4338 * group from the list
4339 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04004340 goto have_block_group;
4341 }
4342 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004343refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04004344 /*
4345 * this cluster didn't work out, free it and
4346 * start over
4347 */
4348 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4349
4350 last_ptr_loop = 0;
4351
4352 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04004353 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04004354 block_group, last_ptr,
4355 offset, num_bytes,
4356 empty_cluster + empty_size);
4357 if (ret == 0) {
4358 /*
4359 * now pull our allocation out of this
4360 * cluster
4361 */
4362 offset = btrfs_alloc_from_cluster(block_group,
4363 last_ptr, num_bytes,
4364 search_start);
4365 if (offset) {
4366 /* we found one, proceed */
4367 spin_unlock(&last_ptr->refill_lock);
4368 goto checks;
4369 }
Josef Bacik0a243252009-09-11 16:11:20 -04004370 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4371 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004372 spin_unlock(&last_ptr->refill_lock);
4373
Josef Bacik0a243252009-09-11 16:11:20 -04004374 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004375 wait_block_group_cache_progress(block_group,
4376 num_bytes + empty_cluster + empty_size);
4377 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004378 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004379
Chris Masonfa9c0d792009-04-03 09:47:43 -04004380 /*
4381 * at this point we either didn't find a cluster
4382 * or we weren't able to allocate a block from our
4383 * cluster. Free the cluster we've been trying
4384 * to use, and go to the next block group
4385 */
Josef Bacik0a243252009-09-11 16:11:20 -04004386 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004387 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04004388 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004389 }
4390
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004391 offset = btrfs_find_space_for_alloc(block_group, search_start,
4392 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004393 /*
4394 * If we didn't find a chunk, and we haven't failed on this
4395 * block group before, and this block group is in the middle of
4396 * caching and we are ok with waiting, then go ahead and wait
4397 * for progress to be made, and set failed_alloc to true.
4398 *
4399 * If failed_alloc is true then we've already waited on this
4400 * block group once and should move on to the next block group.
4401 */
4402 if (!offset && !failed_alloc && !cached &&
4403 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004404 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004405 num_bytes + empty_size);
4406 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004407 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004408 } else if (!offset) {
4409 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04004410 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004411checks:
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004412 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04004413 /* move on to the next group */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004414 if (search_start + num_bytes >= search_end) {
4415 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004416 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004417 }
Chris Masone37c9e62007-05-09 20:13:14 -04004418
Josef Bacik2552d172009-04-03 10:14:19 -04004419 /* move on to the next group */
4420 if (search_start + num_bytes >
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004421 block_group->key.objectid + block_group->key.offset) {
4422 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004423 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004424 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004425
Josef Bacik2552d172009-04-03 10:14:19 -04004426 if (exclude_nr > 0 &&
4427 (search_start + num_bytes > exclude_start &&
4428 search_start < exclude_start + exclude_nr)) {
4429 search_start = exclude_start + exclude_nr;
4430
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004431 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004432 /*
4433 * if search_start is still in this block group
4434 * then we just re-search this block group
4435 */
4436 if (search_start >= block_group->key.objectid &&
4437 search_start < (block_group->key.objectid +
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004438 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04004439 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04004440 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004441 }
Josef Bacik2552d172009-04-03 10:14:19 -04004442
4443 ins->objectid = search_start;
4444 ins->offset = num_bytes;
4445
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004446 if (offset < search_start)
4447 btrfs_add_free_space(block_group, offset,
4448 search_start - offset);
4449 BUG_ON(offset > search_start);
4450
Yan Zheng11833d62009-09-11 16:11:19 -04004451 update_reserved_extents(block_group, num_bytes, 1);
4452
Josef Bacik2552d172009-04-03 10:14:19 -04004453 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04004454 break;
4455loop:
Josef Bacik0a243252009-09-11 16:11:20 -04004456 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004457 failed_alloc = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004458 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004459 }
4460 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004461
Josef Bacikccf0e722009-11-10 21:23:48 -05004462 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
4463 * for them to make caching progress. Also
4464 * determine the best possible bg to cache
4465 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
4466 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04004467 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4468 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4469 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4470 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04004471 */
Josef Bacik817d52f2009-07-13 21:29:25 -04004472 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4473 (found_uncached_bg || empty_size || empty_cluster ||
4474 allowed_chunk_alloc)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004475 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004476 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004477 loop++;
4478 if (!ideal_cache_percent &&
4479 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04004480 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05004481
4482 /*
4483 * 1 of the following 2 things have happened so far
4484 *
4485 * 1) We found an ideal block group for caching that
4486 * is mostly full and will cache quickly, so we might
4487 * as well wait for it.
4488 *
4489 * 2) We searched for cached only and we didn't find
4490 * anything, and we didn't start any caching kthreads
4491 * either, so chances are we will loop through and
4492 * start a couple caching kthreads, and then come back
4493 * around and just wait for them. This will be slower
4494 * because we will have 2 caching kthreads reading at
4495 * the same time when we could have just started one
4496 * and waited for it to get far enough to give us an
4497 * allocation, so go ahead and go to the wait caching
4498 * loop.
4499 */
4500 loop = LOOP_CACHING_WAIT;
4501 search_start = ideal_cache_offset;
4502 ideal_cache_percent = 0;
4503 goto ideal_cache;
4504 } else if (loop == LOOP_FIND_IDEAL) {
4505 /*
4506 * Didn't find a uncached bg, wait on anything we find
4507 * next.
4508 */
4509 loop = LOOP_CACHING_WAIT;
4510 goto search;
4511 }
4512
4513 if (loop < LOOP_CACHING_WAIT) {
4514 loop++;
4515 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04004516 }
4517
4518 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004519 empty_size = 0;
4520 empty_cluster = 0;
4521 }
Chris Mason42e70e72008-11-07 18:17:11 -05004522
Josef Bacik2552d172009-04-03 10:14:19 -04004523 if (allowed_chunk_alloc) {
4524 ret = do_chunk_alloc(trans, root, num_bytes +
4525 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04004526 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004527 done_chunk_alloc = 1;
4528 } else if (!done_chunk_alloc) {
Josef Bacik2552d172009-04-03 10:14:19 -04004529 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004530 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004531
Josef Bacik817d52f2009-07-13 21:29:25 -04004532 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004533 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04004534 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004535 }
Josef Bacik2552d172009-04-03 10:14:19 -04004536 ret = -ENOSPC;
4537 } else if (!ins->objectid) {
4538 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04004539 }
Chris Mason0b86a832008-03-24 15:01:56 -04004540
Josef Bacik80eb2342008-10-29 14:49:05 -04004541 /* we found what we needed */
4542 if (ins->objectid) {
4543 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05004544 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04004545
Chris Masonfa9c0d792009-04-03 09:47:43 -04004546 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004547 ret = 0;
4548 }
Chris Mason0b86a832008-03-24 15:01:56 -04004549
Chris Mason0f70abe2007-02-28 16:46:22 -05004550 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004551}
Chris Masonec44a352008-04-28 15:29:52 -04004552
Josef Bacik9ed74f22009-09-11 16:12:44 -04004553static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4554 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04004555{
4556 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004557
Josef Bacik9ed74f22009-09-11 16:12:44 -04004558 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05004559 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4560 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04004561 info->bytes_pinned - info->bytes_reserved -
4562 info->bytes_super),
Chris Masond3977122009-01-05 21:25:51 -05004563 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05004564 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Josef Bacik9ed74f22009-09-11 16:12:44 -04004565 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4566 "\n",
Joel Becker21380932009-04-21 12:38:29 -07004567 (unsigned long long)info->total_bytes,
4568 (unsigned long long)info->bytes_pinned,
4569 (unsigned long long)info->bytes_delalloc,
4570 (unsigned long long)info->bytes_may_use,
Josef Bacik9ed74f22009-09-11 16:12:44 -04004571 (unsigned long long)info->bytes_used,
4572 (unsigned long long)info->bytes_root,
4573 (unsigned long long)info->bytes_super,
4574 (unsigned long long)info->bytes_reserved);
4575 spin_unlock(&info->lock);
4576
4577 if (!dump_block_groups)
4578 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004579
Josef Bacik80eb2342008-10-29 14:49:05 -04004580 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05004581 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004582 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05004583 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4584 "%llu pinned %llu reserved\n",
4585 (unsigned long long)cache->key.objectid,
4586 (unsigned long long)cache->key.offset,
4587 (unsigned long long)btrfs_block_group_used(&cache->item),
4588 (unsigned long long)cache->pinned,
4589 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004590 btrfs_dump_free_space(cache, bytes);
4591 spin_unlock(&cache->lock);
4592 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004593 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004594}
Zheng Yane8569812008-09-26 10:05:48 -04004595
Yan Zheng11833d62009-09-11 16:11:19 -04004596int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4597 struct btrfs_root *root,
4598 u64 num_bytes, u64 min_alloc_size,
4599 u64 empty_size, u64 hint_byte,
4600 u64 search_end, struct btrfs_key *ins,
4601 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004602{
4603 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04004604 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004605
Josef Bacik6a632092009-02-20 11:00:09 -05004606 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04004607again:
Chris Mason0ef3e662008-05-24 14:04:53 -04004608 /*
4609 * the only place that sets empty_size is btrfs_realloc_node, which
4610 * is not called recursively on allocations
4611 */
Josef Bacik83d3c962009-12-07 21:45:59 +00004612 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04004613 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004614 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04004615
Chris Masondb945352007-10-15 16:15:53 -04004616 WARN_ON(num_bytes < root->sectorsize);
4617 ret = find_free_extent(trans, root, num_bytes, empty_size,
4618 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04004619 trans->alloc_exclude_start,
4620 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04004621
Chris Mason98d20f62008-04-14 09:46:10 -04004622 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4623 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004624 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004625 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04004626 do_chunk_alloc(trans, root->fs_info->extent_root,
4627 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004628 goto again;
4629 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004630 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004631 struct btrfs_space_info *sinfo;
4632
4633 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004634 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4635 "wanted %llu\n", (unsigned long long)data,
4636 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004637 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04004638 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004639
4640 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004641}
4642
Chris Mason65b51a02008-08-01 15:11:20 -04004643int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4644{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004645 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004646 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004647
Josef Bacik0f9dd462008-09-23 13:14:11 -04004648 cache = btrfs_lookup_block_group(root->fs_info, start);
4649 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004650 printk(KERN_ERR "Unable to find block group for %llu\n",
4651 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004652 return -ENOSPC;
4653 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004654
4655 ret = btrfs_discard_extent(root, start, len);
4656
Josef Bacik0f9dd462008-09-23 13:14:11 -04004657 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04004658 update_reserved_extents(cache, len, 0);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004659 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04004660
Chris Masone6dcd2d2008-07-17 12:53:50 -04004661 return ret;
4662}
4663
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004664static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4665 struct btrfs_root *root,
4666 u64 parent, u64 root_objectid,
4667 u64 flags, u64 owner, u64 offset,
4668 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004669{
4670 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004671 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004672 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004673 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004674 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004675 struct extent_buffer *leaf;
4676 int type;
4677 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004678
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004679 if (parent > 0)
4680 type = BTRFS_SHARED_DATA_REF_KEY;
4681 else
4682 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004683
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004684 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004685
4686 path = btrfs_alloc_path();
4687 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004688
Chris Masonb9473432009-03-13 11:00:37 -04004689 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004690 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4691 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004692 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004693
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004694 leaf = path->nodes[0];
4695 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004696 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004697 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4698 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4699 btrfs_set_extent_flags(leaf, extent_item,
4700 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004701
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004702 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4703 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4704 if (parent > 0) {
4705 struct btrfs_shared_data_ref *ref;
4706 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4707 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4708 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4709 } else {
4710 struct btrfs_extent_data_ref *ref;
4711 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4712 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4713 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4714 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4715 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4716 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004717
4718 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004719 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004720
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004721 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4722 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05004723 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004724 printk(KERN_ERR "btrfs update block group failed for %llu "
4725 "%llu\n", (unsigned long long)ins->objectid,
4726 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004727 BUG();
4728 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004729 return ret;
4730}
4731
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004732static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4733 struct btrfs_root *root,
4734 u64 parent, u64 root_objectid,
4735 u64 flags, struct btrfs_disk_key *key,
4736 int level, struct btrfs_key *ins)
4737{
4738 int ret;
4739 struct btrfs_fs_info *fs_info = root->fs_info;
4740 struct btrfs_extent_item *extent_item;
4741 struct btrfs_tree_block_info *block_info;
4742 struct btrfs_extent_inline_ref *iref;
4743 struct btrfs_path *path;
4744 struct extent_buffer *leaf;
4745 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4746
4747 path = btrfs_alloc_path();
4748 BUG_ON(!path);
4749
4750 path->leave_spinning = 1;
4751 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4752 ins, size);
4753 BUG_ON(ret);
4754
4755 leaf = path->nodes[0];
4756 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4757 struct btrfs_extent_item);
4758 btrfs_set_extent_refs(leaf, extent_item, 1);
4759 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4760 btrfs_set_extent_flags(leaf, extent_item,
4761 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4762 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4763
4764 btrfs_set_tree_block_key(leaf, block_info, key);
4765 btrfs_set_tree_block_level(leaf, block_info, level);
4766
4767 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4768 if (parent > 0) {
4769 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4770 btrfs_set_extent_inline_ref_type(leaf, iref,
4771 BTRFS_SHARED_BLOCK_REF_KEY);
4772 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4773 } else {
4774 btrfs_set_extent_inline_ref_type(leaf, iref,
4775 BTRFS_TREE_BLOCK_REF_KEY);
4776 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4777 }
4778
4779 btrfs_mark_buffer_dirty(leaf);
4780 btrfs_free_path(path);
4781
4782 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4783 1, 0);
4784 if (ret) {
4785 printk(KERN_ERR "btrfs update block group failed for %llu "
4786 "%llu\n", (unsigned long long)ins->objectid,
4787 (unsigned long long)ins->offset);
4788 BUG();
4789 }
4790 return ret;
4791}
4792
4793int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4794 struct btrfs_root *root,
4795 u64 root_objectid, u64 owner,
4796 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004797{
4798 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04004799
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004800 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004801
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004802 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4803 0, root_objectid, owner, offset,
4804 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004805 return ret;
4806}
Chris Masone02119d2008-09-05 16:13:11 -04004807
4808/*
4809 * this is used by the tree logging recovery code. It records that
4810 * an extent has been allocated and makes sure to clear the free
4811 * space cache bits as well
4812 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004813int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4814 struct btrfs_root *root,
4815 u64 root_objectid, u64 owner, u64 offset,
4816 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004817{
4818 int ret;
4819 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04004820 struct btrfs_caching_control *caching_ctl;
4821 u64 start = ins->objectid;
4822 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04004823
Chris Masone02119d2008-09-05 16:13:11 -04004824 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04004825 cache_block_group(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004826 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004827
Yan Zheng11833d62009-09-11 16:11:19 -04004828 if (!caching_ctl) {
4829 BUG_ON(!block_group_cache_done(block_group));
4830 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4831 BUG_ON(ret);
4832 } else {
4833 mutex_lock(&caching_ctl->mutex);
4834
4835 if (start >= caching_ctl->progress) {
4836 ret = add_excluded_extent(root, start, num_bytes);
4837 BUG_ON(ret);
4838 } else if (start + num_bytes <= caching_ctl->progress) {
4839 ret = btrfs_remove_free_space(block_group,
4840 start, num_bytes);
4841 BUG_ON(ret);
4842 } else {
4843 num_bytes = caching_ctl->progress - start;
4844 ret = btrfs_remove_free_space(block_group,
4845 start, num_bytes);
4846 BUG_ON(ret);
4847
4848 start = caching_ctl->progress;
4849 num_bytes = ins->objectid + ins->offset -
4850 caching_ctl->progress;
4851 ret = add_excluded_extent(root, start, num_bytes);
4852 BUG_ON(ret);
4853 }
4854
4855 mutex_unlock(&caching_ctl->mutex);
4856 put_caching_control(caching_ctl);
4857 }
4858
4859 update_reserved_extents(block_group, ins->offset, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004860 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004861 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4862 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004863 return ret;
4864}
4865
Chris Masone6dcd2d2008-07-17 12:53:50 -04004866/*
4867 * finds a free extent and does all the dirty work required for allocation
4868 * returns the key for the extent through ins, and a tree buffer for
4869 * the first block of the extent through buf.
4870 *
4871 * returns 0 if everything worked, non-zero otherwise.
4872 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004873static int alloc_tree_block(struct btrfs_trans_handle *trans,
4874 struct btrfs_root *root,
4875 u64 num_bytes, u64 parent, u64 root_objectid,
4876 struct btrfs_disk_key *key, int level,
4877 u64 empty_size, u64 hint_byte, u64 search_end,
4878 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004879{
4880 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004881 u64 flags = 0;
4882
Yan Zheng11833d62009-09-11 16:11:19 -04004883 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4884 empty_size, hint_byte, search_end,
4885 ins, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004886 if (ret)
4887 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004888
4889 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4890 if (parent == 0)
4891 parent = ins->objectid;
4892 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4893 } else
4894 BUG_ON(parent > 0);
4895
Chris Masond00aff02008-09-11 15:54:42 -04004896 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004897 struct btrfs_delayed_extent_op *extent_op;
4898 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4899 BUG_ON(!extent_op);
4900 if (key)
4901 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4902 else
4903 memset(&extent_op->key, 0, sizeof(extent_op->key));
4904 extent_op->flags_to_set = flags;
4905 extent_op->update_key = 1;
4906 extent_op->update_flags = 1;
4907 extent_op->is_data = 0;
4908
4909 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4910 ins->offset, parent, root_objectid,
4911 level, BTRFS_ADD_DELAYED_EXTENT,
4912 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004913 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004914 }
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00004915
4916 if (root_objectid == root->root_key.objectid) {
4917 u64 used;
4918 spin_lock(&root->node_lock);
4919 used = btrfs_root_used(&root->root_item) + num_bytes;
4920 btrfs_set_root_used(&root->root_item, used);
4921 spin_unlock(&root->node_lock);
4922 }
Chris Mason925baed2008-06-25 16:01:30 -04004923 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004924}
Chris Mason65b51a02008-08-01 15:11:20 -04004925
4926struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4927 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004928 u64 bytenr, u32 blocksize,
4929 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004930{
4931 struct extent_buffer *buf;
4932
4933 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4934 if (!buf)
4935 return ERR_PTR(-ENOMEM);
4936 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004937 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004938 btrfs_tree_lock(buf);
4939 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004940
4941 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004942 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004943
Chris Masond0c803c2008-09-11 16:17:57 -04004944 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004945 /*
4946 * we allow two log transactions at a time, use different
4947 * EXENT bit to differentiate dirty pages.
4948 */
4949 if (root->log_transid % 2 == 0)
4950 set_extent_dirty(&root->dirty_log_pages, buf->start,
4951 buf->start + buf->len - 1, GFP_NOFS);
4952 else
4953 set_extent_new(&root->dirty_log_pages, buf->start,
4954 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004955 } else {
4956 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4957 buf->start + buf->len - 1, GFP_NOFS);
4958 }
Chris Mason65b51a02008-08-01 15:11:20 -04004959 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004960 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004961 return buf;
4962}
4963
Chris Masonfec577f2007-02-26 10:40:21 -05004964/*
4965 * helper function to allocate a block for a given tree
4966 * returns the tree buffer or NULL.
4967 */
Chris Mason5f39d392007-10-15 16:14:19 -04004968struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004969 struct btrfs_root *root, u32 blocksize,
4970 u64 parent, u64 root_objectid,
4971 struct btrfs_disk_key *key, int level,
4972 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004973{
Chris Masone2fa7222007-03-12 16:22:34 -04004974 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004975 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004976 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004977
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004978 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4979 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004980 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004981 BUG_ON(ret > 0);
4982 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004983 }
Chris Mason55c69072008-01-09 15:55:33 -05004984
Chris Mason4008c042009-02-12 14:09:45 -05004985 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4986 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004987 return buf;
4988}
Chris Masona28ec192007-03-06 20:08:01 -05004989
Yan Zheng2c47e6052009-06-27 21:07:35 -04004990struct walk_control {
4991 u64 refs[BTRFS_MAX_LEVEL];
4992 u64 flags[BTRFS_MAX_LEVEL];
4993 struct btrfs_key update_progress;
4994 int stage;
4995 int level;
4996 int shared_level;
4997 int update_ref;
4998 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004999 int reada_slot;
5000 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005001};
5002
5003#define DROP_REFERENCE 1
5004#define UPDATE_BACKREF 2
5005
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005006static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5007 struct btrfs_root *root,
5008 struct walk_control *wc,
5009 struct btrfs_path *path)
5010{
5011 u64 bytenr;
5012 u64 generation;
5013 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005014 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005015 u64 last = 0;
5016 u32 nritems;
5017 u32 blocksize;
5018 struct btrfs_key key;
5019 struct extent_buffer *eb;
5020 int ret;
5021 int slot;
5022 int nread = 0;
5023
5024 if (path->slots[wc->level] < wc->reada_slot) {
5025 wc->reada_count = wc->reada_count * 2 / 3;
5026 wc->reada_count = max(wc->reada_count, 2);
5027 } else {
5028 wc->reada_count = wc->reada_count * 3 / 2;
5029 wc->reada_count = min_t(int, wc->reada_count,
5030 BTRFS_NODEPTRS_PER_BLOCK(root));
5031 }
5032
5033 eb = path->nodes[wc->level];
5034 nritems = btrfs_header_nritems(eb);
5035 blocksize = btrfs_level_size(root, wc->level - 1);
5036
5037 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5038 if (nread >= wc->reada_count)
5039 break;
5040
5041 cond_resched();
5042 bytenr = btrfs_node_blockptr(eb, slot);
5043 generation = btrfs_node_ptr_generation(eb, slot);
5044
5045 if (slot == path->slots[wc->level])
5046 goto reada;
5047
5048 if (wc->stage == UPDATE_BACKREF &&
5049 generation <= root->root_key.offset)
5050 continue;
5051
Yan, Zheng94fcca92009-10-09 09:25:16 -04005052 /* We don't lock the tree block, it's OK to be racy here */
5053 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5054 &refs, &flags);
5055 BUG_ON(ret);
5056 BUG_ON(refs == 0);
5057
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005058 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005059 if (refs == 1)
5060 goto reada;
5061
Yan, Zheng94fcca92009-10-09 09:25:16 -04005062 if (wc->level == 1 &&
5063 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5064 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005065 if (!wc->update_ref ||
5066 generation <= root->root_key.offset)
5067 continue;
5068 btrfs_node_key_to_cpu(eb, &key, slot);
5069 ret = btrfs_comp_cpu_keys(&key,
5070 &wc->update_progress);
5071 if (ret < 0)
5072 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005073 } else {
5074 if (wc->level == 1 &&
5075 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5076 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005077 }
5078reada:
5079 ret = readahead_tree_block(root, bytenr, blocksize,
5080 generation);
5081 if (ret)
5082 break;
5083 last = bytenr + blocksize;
5084 nread++;
5085 }
5086 wc->reada_slot = slot;
5087}
5088
Chris Mason9aca1d52007-03-13 11:09:37 -04005089/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005090 * hepler to process tree block while walking down the tree.
5091 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005092 * when wc->stage == UPDATE_BACKREF, this function updates
5093 * back refs for pointers in the block.
5094 *
5095 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005096 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005097static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5098 struct btrfs_root *root,
5099 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005100 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005101{
5102 int level = wc->level;
5103 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005104 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5105 int ret;
5106
5107 if (wc->stage == UPDATE_BACKREF &&
5108 btrfs_header_owner(eb) != root->root_key.objectid)
5109 return 1;
5110
5111 /*
5112 * when reference count of tree block is 1, it won't increase
5113 * again. once full backref flag is set, we never clear it.
5114 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005115 if (lookup_info &&
5116 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5117 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005118 BUG_ON(!path->locks[level]);
5119 ret = btrfs_lookup_extent_info(trans, root,
5120 eb->start, eb->len,
5121 &wc->refs[level],
5122 &wc->flags[level]);
5123 BUG_ON(ret);
5124 BUG_ON(wc->refs[level] == 0);
5125 }
5126
Yan Zheng2c47e6052009-06-27 21:07:35 -04005127 if (wc->stage == DROP_REFERENCE) {
5128 if (wc->refs[level] > 1)
5129 return 1;
5130
5131 if (path->locks[level] && !wc->keep_locks) {
5132 btrfs_tree_unlock(eb);
5133 path->locks[level] = 0;
5134 }
5135 return 0;
5136 }
5137
5138 /* wc->stage == UPDATE_BACKREF */
5139 if (!(wc->flags[level] & flag)) {
5140 BUG_ON(!path->locks[level]);
5141 ret = btrfs_inc_ref(trans, root, eb, 1);
5142 BUG_ON(ret);
5143 ret = btrfs_dec_ref(trans, root, eb, 0);
5144 BUG_ON(ret);
5145 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5146 eb->len, flag, 0);
5147 BUG_ON(ret);
5148 wc->flags[level] |= flag;
5149 }
5150
5151 /*
5152 * the block is shared by multiple trees, so it's not good to
5153 * keep the tree lock
5154 */
5155 if (path->locks[level] && level > 0) {
5156 btrfs_tree_unlock(eb);
5157 path->locks[level] = 0;
5158 }
5159 return 0;
5160}
5161
5162/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005163 * hepler to process tree block pointer.
5164 *
5165 * when wc->stage == DROP_REFERENCE, this function checks
5166 * reference count of the block pointed to. if the block
5167 * is shared and we need update back refs for the subtree
5168 * rooted at the block, this function changes wc->stage to
5169 * UPDATE_BACKREF. if the block is shared and there is no
5170 * need to update back, this function drops the reference
5171 * to the block.
5172 *
5173 * NOTE: return value 1 means we should stop walking down.
5174 */
5175static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5176 struct btrfs_root *root,
5177 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005178 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005179{
5180 u64 bytenr;
5181 u64 generation;
5182 u64 parent;
5183 u32 blocksize;
5184 struct btrfs_key key;
5185 struct extent_buffer *next;
5186 int level = wc->level;
5187 int reada = 0;
5188 int ret = 0;
5189
5190 generation = btrfs_node_ptr_generation(path->nodes[level],
5191 path->slots[level]);
5192 /*
5193 * if the lower level block was created before the snapshot
5194 * was created, we know there is no need to update back refs
5195 * for the subtree
5196 */
5197 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005198 generation <= root->root_key.offset) {
5199 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005200 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005201 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005202
5203 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5204 blocksize = btrfs_level_size(root, level - 1);
5205
5206 next = btrfs_find_tree_block(root, bytenr, blocksize);
5207 if (!next) {
5208 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
5209 reada = 1;
5210 }
5211 btrfs_tree_lock(next);
5212 btrfs_set_lock_blocking(next);
5213
Yan, Zheng94fcca92009-10-09 09:25:16 -04005214 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5215 &wc->refs[level - 1],
5216 &wc->flags[level - 1]);
5217 BUG_ON(ret);
5218 BUG_ON(wc->refs[level - 1] == 0);
5219 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005220
Yan, Zheng94fcca92009-10-09 09:25:16 -04005221 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005222 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005223 if (level == 1 &&
5224 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5225 goto skip;
5226
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005227 if (!wc->update_ref ||
5228 generation <= root->root_key.offset)
5229 goto skip;
5230
5231 btrfs_node_key_to_cpu(path->nodes[level], &key,
5232 path->slots[level]);
5233 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5234 if (ret < 0)
5235 goto skip;
5236
5237 wc->stage = UPDATE_BACKREF;
5238 wc->shared_level = level - 1;
5239 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005240 } else {
5241 if (level == 1 &&
5242 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5243 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005244 }
5245
5246 if (!btrfs_buffer_uptodate(next, generation)) {
5247 btrfs_tree_unlock(next);
5248 free_extent_buffer(next);
5249 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005250 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005251 }
5252
5253 if (!next) {
5254 if (reada && level == 1)
5255 reada_walk_down(trans, root, wc, path);
5256 next = read_tree_block(root, bytenr, blocksize, generation);
5257 btrfs_tree_lock(next);
5258 btrfs_set_lock_blocking(next);
5259 }
5260
5261 level--;
5262 BUG_ON(level != btrfs_header_level(next));
5263 path->nodes[level] = next;
5264 path->slots[level] = 0;
5265 path->locks[level] = 1;
5266 wc->level = level;
5267 if (wc->level == 1)
5268 wc->reada_slot = 0;
5269 return 0;
5270skip:
5271 wc->refs[level - 1] = 0;
5272 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005273 if (wc->stage == DROP_REFERENCE) {
5274 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5275 parent = path->nodes[level]->start;
5276 } else {
5277 BUG_ON(root->root_key.objectid !=
5278 btrfs_header_owner(path->nodes[level]));
5279 parent = 0;
5280 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005281
Yan, Zheng94fcca92009-10-09 09:25:16 -04005282 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5283 root->root_key.objectid, level - 1, 0);
5284 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005285 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005286 btrfs_tree_unlock(next);
5287 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04005288 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005289 return 1;
5290}
5291
5292/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005293 * hepler to process tree block while walking up the tree.
5294 *
5295 * when wc->stage == DROP_REFERENCE, this function drops
5296 * reference count on the block.
5297 *
5298 * when wc->stage == UPDATE_BACKREF, this function changes
5299 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5300 * to UPDATE_BACKREF previously while processing the block.
5301 *
5302 * NOTE: return value 1 means we should stop walking up.
5303 */
5304static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5305 struct btrfs_root *root,
5306 struct btrfs_path *path,
5307 struct walk_control *wc)
5308{
5309 int ret = 0;
5310 int level = wc->level;
5311 struct extent_buffer *eb = path->nodes[level];
5312 u64 parent = 0;
5313
5314 if (wc->stage == UPDATE_BACKREF) {
5315 BUG_ON(wc->shared_level < level);
5316 if (level < wc->shared_level)
5317 goto out;
5318
Yan Zheng2c47e6052009-06-27 21:07:35 -04005319 ret = find_next_key(path, level + 1, &wc->update_progress);
5320 if (ret > 0)
5321 wc->update_ref = 0;
5322
5323 wc->stage = DROP_REFERENCE;
5324 wc->shared_level = -1;
5325 path->slots[level] = 0;
5326
5327 /*
5328 * check reference count again if the block isn't locked.
5329 * we should start walking down the tree again if reference
5330 * count is one.
5331 */
5332 if (!path->locks[level]) {
5333 BUG_ON(level == 0);
5334 btrfs_tree_lock(eb);
5335 btrfs_set_lock_blocking(eb);
5336 path->locks[level] = 1;
5337
5338 ret = btrfs_lookup_extent_info(trans, root,
5339 eb->start, eb->len,
5340 &wc->refs[level],
5341 &wc->flags[level]);
5342 BUG_ON(ret);
5343 BUG_ON(wc->refs[level] == 0);
5344 if (wc->refs[level] == 1) {
5345 btrfs_tree_unlock(eb);
5346 path->locks[level] = 0;
5347 return 1;
5348 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005349 }
5350 }
5351
5352 /* wc->stage == DROP_REFERENCE */
5353 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5354
5355 if (wc->refs[level] == 1) {
5356 if (level == 0) {
5357 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5358 ret = btrfs_dec_ref(trans, root, eb, 1);
5359 else
5360 ret = btrfs_dec_ref(trans, root, eb, 0);
5361 BUG_ON(ret);
5362 }
5363 /* make block locked assertion in clean_tree_block happy */
5364 if (!path->locks[level] &&
5365 btrfs_header_generation(eb) == trans->transid) {
5366 btrfs_tree_lock(eb);
5367 btrfs_set_lock_blocking(eb);
5368 path->locks[level] = 1;
5369 }
5370 clean_tree_block(trans, root, eb);
5371 }
5372
5373 if (eb == root->node) {
5374 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5375 parent = eb->start;
5376 else
5377 BUG_ON(root->root_key.objectid !=
5378 btrfs_header_owner(eb));
5379 } else {
5380 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5381 parent = path->nodes[level + 1]->start;
5382 else
5383 BUG_ON(root->root_key.objectid !=
5384 btrfs_header_owner(path->nodes[level + 1]));
5385 }
5386
5387 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5388 root->root_key.objectid, level, 0);
5389 BUG_ON(ret);
5390out:
5391 wc->refs[level] = 0;
5392 wc->flags[level] = 0;
5393 return ret;
5394}
5395
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005396static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5397 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005398 struct btrfs_path *path,
5399 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005400{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005401 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005402 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005403 int ret;
5404
Yan Zheng2c47e6052009-06-27 21:07:35 -04005405 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005406 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005407 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005408 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005409
Yan Zheng2c47e6052009-06-27 21:07:35 -04005410 if (level == 0)
5411 break;
5412
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005413 if (path->slots[level] >=
5414 btrfs_header_nritems(path->nodes[level]))
5415 break;
5416
Yan, Zheng94fcca92009-10-09 09:25:16 -04005417 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005418 if (ret > 0) {
5419 path->slots[level]++;
5420 continue;
5421 }
5422 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005423 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005424 return 0;
5425}
5426
Chris Masond3977122009-01-05 21:25:51 -05005427static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005428 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005429 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005430 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005431{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005432 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005433 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005434
Yan Zheng2c47e6052009-06-27 21:07:35 -04005435 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5436 while (level < max_level && path->nodes[level]) {
5437 wc->level = level;
5438 if (path->slots[level] + 1 <
5439 btrfs_header_nritems(path->nodes[level])) {
5440 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005441 return 0;
5442 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005443 ret = walk_up_proc(trans, root, path, wc);
5444 if (ret > 0)
5445 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05005446
Yan Zheng2c47e6052009-06-27 21:07:35 -04005447 if (path->locks[level]) {
5448 btrfs_tree_unlock(path->nodes[level]);
5449 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005450 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005451 free_extent_buffer(path->nodes[level]);
5452 path->nodes[level] = NULL;
5453 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005454 }
5455 }
5456 return 1;
5457}
5458
Chris Mason9aca1d52007-03-13 11:09:37 -04005459/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005460 * drop a subvolume tree.
5461 *
5462 * this function traverses the tree freeing any blocks that only
5463 * referenced by the tree.
5464 *
5465 * when a shared tree block is found. this function decreases its
5466 * reference count by one. if update_ref is true, this function
5467 * also make sure backrefs for the shared block and all lower level
5468 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04005469 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005470int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05005471{
Chris Mason5caf2a02007-04-02 11:20:42 -04005472 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005473 struct btrfs_trans_handle *trans;
5474 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005475 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005476 struct walk_control *wc;
5477 struct btrfs_key key;
5478 int err = 0;
5479 int ret;
5480 int level;
Chris Mason20524f02007-03-10 06:35:47 -05005481
Chris Mason5caf2a02007-04-02 11:20:42 -04005482 path = btrfs_alloc_path();
5483 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05005484
Yan Zheng2c47e6052009-06-27 21:07:35 -04005485 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5486 BUG_ON(!wc);
5487
5488 trans = btrfs_start_transaction(tree_root, 1);
5489
Chris Mason9f3a7422007-08-07 15:52:19 -04005490 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005491 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005492 path->nodes[level] = btrfs_lock_root_node(root);
5493 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005494 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005495 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005496 memset(&wc->update_progress, 0,
5497 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005498 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005499 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005500 memcpy(&wc->update_progress, &key,
5501 sizeof(wc->update_progress));
5502
Chris Mason6702ed42007-08-07 16:15:09 -04005503 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005504 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005505 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005506 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5507 path->lowest_level = 0;
5508 if (ret < 0) {
5509 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005510 goto out;
5511 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005512 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005513
Chris Mason7d9eb122008-07-08 14:19:17 -04005514 /*
5515 * unlock our path, this is safe because only this
5516 * function is allowed to delete this snapshot
5517 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005518 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005519
Yan Zheng2c47e6052009-06-27 21:07:35 -04005520 level = btrfs_header_level(root->node);
5521 while (1) {
5522 btrfs_tree_lock(path->nodes[level]);
5523 btrfs_set_lock_blocking(path->nodes[level]);
5524
5525 ret = btrfs_lookup_extent_info(trans, root,
5526 path->nodes[level]->start,
5527 path->nodes[level]->len,
5528 &wc->refs[level],
5529 &wc->flags[level]);
5530 BUG_ON(ret);
5531 BUG_ON(wc->refs[level] == 0);
5532
5533 if (level == root_item->drop_level)
5534 break;
5535
5536 btrfs_tree_unlock(path->nodes[level]);
5537 WARN_ON(wc->refs[level] != 1);
5538 level--;
5539 }
5540 }
5541
5542 wc->level = level;
5543 wc->shared_level = -1;
5544 wc->stage = DROP_REFERENCE;
5545 wc->update_ref = update_ref;
5546 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005547 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005548
5549 while (1) {
5550 ret = walk_down_tree(trans, root, path, wc);
5551 if (ret < 0) {
5552 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005553 break;
5554 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005555
5556 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5557 if (ret < 0) {
5558 err = ret;
5559 break;
5560 }
5561
5562 if (ret > 0) {
5563 BUG_ON(wc->stage != DROP_REFERENCE);
5564 break;
5565 }
5566
5567 if (wc->stage == DROP_REFERENCE) {
5568 level = wc->level;
5569 btrfs_node_key(path->nodes[level],
5570 &root_item->drop_progress,
5571 path->slots[level]);
5572 root_item->drop_level = level;
5573 }
5574
5575 BUG_ON(wc->level == 0);
5576 if (trans->transaction->in_commit ||
5577 trans->transaction->delayed_refs.flushing) {
5578 ret = btrfs_update_root(trans, tree_root,
5579 &root->root_key,
5580 root_item);
5581 BUG_ON(ret);
5582
5583 btrfs_end_transaction(trans, tree_root);
5584 trans = btrfs_start_transaction(tree_root, 1);
5585 } else {
5586 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005587 update = trans->delayed_ref_updates;
5588 trans->delayed_ref_updates = 0;
5589 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005590 btrfs_run_delayed_refs(trans, tree_root,
5591 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005592 }
Chris Mason20524f02007-03-10 06:35:47 -05005593 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005594 btrfs_release_path(root, path);
5595 BUG_ON(err);
5596
5597 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5598 BUG_ON(ret);
5599
Yan, Zheng76dda932009-09-21 16:00:26 -04005600 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5601 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5602 NULL, NULL);
5603 BUG_ON(ret < 0);
5604 if (ret > 0) {
5605 ret = btrfs_del_orphan_item(trans, tree_root,
5606 root->root_key.objectid);
5607 BUG_ON(ret);
5608 }
5609 }
5610
5611 if (root->in_radix) {
5612 btrfs_free_fs_root(tree_root->fs_info, root);
5613 } else {
5614 free_extent_buffer(root->node);
5615 free_extent_buffer(root->commit_root);
5616 kfree(root);
5617 }
Chris Mason9f3a7422007-08-07 15:52:19 -04005618out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005619 btrfs_end_transaction(trans, tree_root);
5620 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005621 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005622 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005623}
Chris Mason9078a3e2007-04-26 16:46:15 -04005624
Yan Zheng2c47e6052009-06-27 21:07:35 -04005625/*
5626 * drop subtree rooted at tree block 'node'.
5627 *
5628 * NOTE: this function will unlock and release tree block 'node'
5629 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005630int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5631 struct btrfs_root *root,
5632 struct extent_buffer *node,
5633 struct extent_buffer *parent)
5634{
5635 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005636 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005637 int level;
5638 int parent_level;
5639 int ret = 0;
5640 int wret;
5641
Yan Zheng2c47e6052009-06-27 21:07:35 -04005642 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5643
Yan Zhengf82d02d2008-10-29 14:49:05 -04005644 path = btrfs_alloc_path();
5645 BUG_ON(!path);
5646
Yan Zheng2c47e6052009-06-27 21:07:35 -04005647 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5648 BUG_ON(!wc);
5649
Chris Masonb9447ef2009-03-09 11:45:38 -04005650 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005651 parent_level = btrfs_header_level(parent);
5652 extent_buffer_get(parent);
5653 path->nodes[parent_level] = parent;
5654 path->slots[parent_level] = btrfs_header_nritems(parent);
5655
Chris Masonb9447ef2009-03-09 11:45:38 -04005656 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005657 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005658 path->nodes[level] = node;
5659 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005660 path->locks[level] = 1;
5661
5662 wc->refs[parent_level] = 1;
5663 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5664 wc->level = level;
5665 wc->shared_level = -1;
5666 wc->stage = DROP_REFERENCE;
5667 wc->update_ref = 0;
5668 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005669 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005670
5671 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005672 wret = walk_down_tree(trans, root, path, wc);
5673 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005674 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005675 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005676 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005677
Yan Zheng2c47e6052009-06-27 21:07:35 -04005678 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005679 if (wret < 0)
5680 ret = wret;
5681 if (wret != 0)
5682 break;
5683 }
5684
Yan Zheng2c47e6052009-06-27 21:07:35 -04005685 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005686 btrfs_free_path(path);
5687 return ret;
5688}
5689
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005690#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005691static unsigned long calc_ra(unsigned long start, unsigned long last,
5692 unsigned long nr)
5693{
5694 return min(last, start + nr - 1);
5695}
5696
Chris Masond3977122009-01-05 21:25:51 -05005697static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005698 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005699{
5700 u64 page_start;
5701 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005702 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005703 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005704 unsigned long i;
5705 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005706 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005707 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005708 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005709 unsigned int total_read = 0;
5710 unsigned int total_dirty = 0;
5711 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005712
5713 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005714
5715 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005716 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005717 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5718
Zheng Yan1a40e232008-09-26 10:09:34 -04005719 /* make sure the dirty trick played by the caller work */
5720 ret = invalidate_inode_pages2_range(inode->i_mapping,
5721 first_index, last_index);
5722 if (ret)
5723 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005724
Chris Mason4313b392008-01-03 09:08:48 -05005725 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005726
Zheng Yan1a40e232008-09-26 10:09:34 -04005727 for (i = first_index ; i <= last_index; i++) {
5728 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005729 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005730 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005731 }
5732 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005733again:
5734 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005735 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005736 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005737 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005738 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005739 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005740 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005741 if (!PageUptodate(page)) {
5742 btrfs_readpage(NULL, page);
5743 lock_page(page);
5744 if (!PageUptodate(page)) {
5745 unlock_page(page);
5746 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005747 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005748 goto out_unlock;
5749 }
5750 }
Chris Masonec44a352008-04-28 15:29:52 -04005751 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005752
Chris Masonedbd8d42007-12-21 16:27:24 -05005753 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5754 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005755 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005756
Chris Mason3eaa2882008-07-24 11:57:52 -04005757 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5758 if (ordered) {
5759 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5760 unlock_page(page);
5761 page_cache_release(page);
5762 btrfs_start_ordered_extent(inode, ordered, 1);
5763 btrfs_put_ordered_extent(ordered);
5764 goto again;
5765 }
5766 set_page_extent_mapped(page);
5767
Zheng Yan1a40e232008-09-26 10:09:34 -04005768 if (i == first_index)
5769 set_extent_bits(io_tree, page_start, page_end,
5770 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005771 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005772
Chris Masona061fc82008-05-07 11:43:44 -04005773 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005774 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005775
Chris Masond1310b22008-01-24 16:13:08 -05005776 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005777 unlock_page(page);
5778 page_cache_release(page);
5779 }
5780
5781out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005782 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005783 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005784 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005785 return ret;
5786}
5787
Chris Masond3977122009-01-05 21:25:51 -05005788static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005789 struct btrfs_key *extent_key,
5790 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005791{
Zheng Yan1a40e232008-09-26 10:09:34 -04005792 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5793 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5794 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005795 u64 start = extent_key->objectid - offset;
5796 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005797
5798 em = alloc_extent_map(GFP_NOFS);
5799 BUG_ON(!em || IS_ERR(em));
5800
Yan Zheng66435582008-10-30 14:19:50 -04005801 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005802 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005803 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005804 em->block_start = extent_key->objectid;
5805 em->bdev = root->fs_info->fs_devices->latest_bdev;
5806 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5807
5808 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005809 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005810 while (1) {
5811 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04005812 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005813 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005814 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005815 if (ret != -EEXIST) {
5816 free_extent_map(em);
5817 break;
5818 }
Yan Zheng66435582008-10-30 14:19:50 -04005819 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005820 }
Yan Zheng66435582008-10-30 14:19:50 -04005821 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005822
Yan Zheng66435582008-10-30 14:19:50 -04005823 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005824}
5825
5826struct btrfs_ref_path {
5827 u64 extent_start;
5828 u64 nodes[BTRFS_MAX_LEVEL];
5829 u64 root_objectid;
5830 u64 root_generation;
5831 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005832 u32 num_refs;
5833 int lowest_level;
5834 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005835 int shared_level;
5836
5837 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5838 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005839};
5840
5841struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005842 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005843 u64 disk_bytenr;
5844 u64 disk_num_bytes;
5845 u64 offset;
5846 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005847 u8 compression;
5848 u8 encryption;
5849 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005850};
5851
5852static int is_cowonly_root(u64 root_objectid)
5853{
5854 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5855 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5856 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5857 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005858 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5859 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005860 return 1;
5861 return 0;
5862}
5863
Chris Masond3977122009-01-05 21:25:51 -05005864static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005865 struct btrfs_root *extent_root,
5866 struct btrfs_ref_path *ref_path,
5867 int first_time)
5868{
5869 struct extent_buffer *leaf;
5870 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005871 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005872 struct btrfs_key key;
5873 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005874 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005875 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005876 int level;
5877 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005878
Zheng Yan1a40e232008-09-26 10:09:34 -04005879 path = btrfs_alloc_path();
5880 if (!path)
5881 return -ENOMEM;
5882
Zheng Yan1a40e232008-09-26 10:09:34 -04005883 if (first_time) {
5884 ref_path->lowest_level = -1;
5885 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005886 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005887 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005888 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005889walk_down:
5890 level = ref_path->current_level - 1;
5891 while (level >= -1) {
5892 u64 parent;
5893 if (level < ref_path->lowest_level)
5894 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005895
Chris Masond3977122009-01-05 21:25:51 -05005896 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005897 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005898 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005899 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005900 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005901
Zheng Yan1a40e232008-09-26 10:09:34 -04005902 parent = ref_path->nodes[level + 1];
5903 ref_path->nodes[level + 1] = 0;
5904 ref_path->current_level = level;
5905 BUG_ON(parent == 0);
5906
5907 key.objectid = bytenr;
5908 key.offset = parent + 1;
5909 key.type = BTRFS_EXTENT_REF_KEY;
5910
5911 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005912 if (ret < 0)
5913 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005914 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005915
Chris Masonedbd8d42007-12-21 16:27:24 -05005916 leaf = path->nodes[0];
5917 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005918 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005919 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005920 if (ret < 0)
5921 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005922 if (ret > 0)
5923 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005924 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005925 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005926
5927 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005928 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005929 found_key.type == BTRFS_EXTENT_REF_KEY) {
5930 if (level < ref_path->shared_level)
5931 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005932 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005933 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005934next:
5935 level--;
5936 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005937 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005938 }
5939 /* reached lowest level */
5940 ret = 1;
5941 goto out;
5942walk_up:
5943 level = ref_path->current_level;
5944 while (level < BTRFS_MAX_LEVEL - 1) {
5945 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005946
5947 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005948 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005949 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005950 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005951
Zheng Yan1a40e232008-09-26 10:09:34 -04005952 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005953
Zheng Yan1a40e232008-09-26 10:09:34 -04005954 key.objectid = bytenr;
5955 key.offset = 0;
5956 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005957
Zheng Yan1a40e232008-09-26 10:09:34 -04005958 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5959 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005960 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005961
5962 leaf = path->nodes[0];
5963 nritems = btrfs_header_nritems(leaf);
5964 if (path->slots[0] >= nritems) {
5965 ret = btrfs_next_leaf(extent_root, path);
5966 if (ret < 0)
5967 goto out;
5968 if (ret > 0) {
5969 /* the extent was freed by someone */
5970 if (ref_path->lowest_level == level)
5971 goto out;
5972 btrfs_release_path(extent_root, path);
5973 goto walk_down;
5974 }
5975 leaf = path->nodes[0];
5976 }
5977
5978 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5979 if (found_key.objectid != bytenr ||
5980 found_key.type != BTRFS_EXTENT_REF_KEY) {
5981 /* the extent was freed by someone */
5982 if (ref_path->lowest_level == level) {
5983 ret = 1;
5984 goto out;
5985 }
5986 btrfs_release_path(extent_root, path);
5987 goto walk_down;
5988 }
5989found:
5990 ref = btrfs_item_ptr(leaf, path->slots[0],
5991 struct btrfs_extent_ref);
5992 ref_objectid = btrfs_ref_objectid(leaf, ref);
5993 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5994 if (first_time) {
5995 level = (int)ref_objectid;
5996 BUG_ON(level >= BTRFS_MAX_LEVEL);
5997 ref_path->lowest_level = level;
5998 ref_path->current_level = level;
5999 ref_path->nodes[level] = bytenr;
6000 } else {
6001 WARN_ON(ref_objectid != level);
6002 }
6003 } else {
6004 WARN_ON(level != -1);
6005 }
6006 first_time = 0;
6007
6008 if (ref_path->lowest_level == level) {
6009 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006010 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6011 }
6012
6013 /*
6014 * the block is tree root or the block isn't in reference
6015 * counted tree.
6016 */
6017 if (found_key.objectid == found_key.offset ||
6018 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6019 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6020 ref_path->root_generation =
6021 btrfs_ref_generation(leaf, ref);
6022 if (level < 0) {
6023 /* special reference from the tree log */
6024 ref_path->nodes[0] = found_key.offset;
6025 ref_path->current_level = 0;
6026 }
6027 ret = 0;
6028 goto out;
6029 }
6030
6031 level++;
6032 BUG_ON(ref_path->nodes[level] != 0);
6033 ref_path->nodes[level] = found_key.offset;
6034 ref_path->current_level = level;
6035
6036 /*
6037 * the reference was created in the running transaction,
6038 * no need to continue walking up.
6039 */
6040 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6041 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6042 ref_path->root_generation =
6043 btrfs_ref_generation(leaf, ref);
6044 ret = 0;
6045 goto out;
6046 }
6047
6048 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006049 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006050 }
6051 /* reached max tree level, but no tree root found. */
6052 BUG();
6053out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006054 btrfs_free_path(path);
6055 return ret;
6056}
6057
6058static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6059 struct btrfs_root *extent_root,
6060 struct btrfs_ref_path *ref_path,
6061 u64 extent_start)
6062{
6063 memset(ref_path, 0, sizeof(*ref_path));
6064 ref_path->extent_start = extent_start;
6065
6066 return __next_ref_path(trans, extent_root, ref_path, 1);
6067}
6068
6069static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6070 struct btrfs_root *extent_root,
6071 struct btrfs_ref_path *ref_path)
6072{
6073 return __next_ref_path(trans, extent_root, ref_path, 0);
6074}
6075
Chris Masond3977122009-01-05 21:25:51 -05006076static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006077 struct btrfs_key *extent_key,
6078 u64 offset, int no_fragment,
6079 struct disk_extent **extents,
6080 int *nr_extents)
6081{
6082 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6083 struct btrfs_path *path;
6084 struct btrfs_file_extent_item *fi;
6085 struct extent_buffer *leaf;
6086 struct disk_extent *exts = *extents;
6087 struct btrfs_key found_key;
6088 u64 cur_pos;
6089 u64 last_byte;
6090 u32 nritems;
6091 int nr = 0;
6092 int max = *nr_extents;
6093 int ret;
6094
6095 WARN_ON(!no_fragment && *extents);
6096 if (!exts) {
6097 max = 1;
6098 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6099 if (!exts)
6100 return -ENOMEM;
6101 }
6102
6103 path = btrfs_alloc_path();
6104 BUG_ON(!path);
6105
6106 cur_pos = extent_key->objectid - offset;
6107 last_byte = extent_key->objectid + extent_key->offset;
6108 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6109 cur_pos, 0);
6110 if (ret < 0)
6111 goto out;
6112 if (ret > 0) {
6113 ret = -ENOENT;
6114 goto out;
6115 }
6116
6117 while (1) {
6118 leaf = path->nodes[0];
6119 nritems = btrfs_header_nritems(leaf);
6120 if (path->slots[0] >= nritems) {
6121 ret = btrfs_next_leaf(root, path);
6122 if (ret < 0)
6123 goto out;
6124 if (ret > 0)
6125 break;
6126 leaf = path->nodes[0];
6127 }
6128
6129 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6130 if (found_key.offset != cur_pos ||
6131 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6132 found_key.objectid != reloc_inode->i_ino)
6133 break;
6134
6135 fi = btrfs_item_ptr(leaf, path->slots[0],
6136 struct btrfs_file_extent_item);
6137 if (btrfs_file_extent_type(leaf, fi) !=
6138 BTRFS_FILE_EXTENT_REG ||
6139 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6140 break;
6141
6142 if (nr == max) {
6143 struct disk_extent *old = exts;
6144 max *= 2;
6145 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6146 memcpy(exts, old, sizeof(*exts) * nr);
6147 if (old != *extents)
6148 kfree(old);
6149 }
6150
6151 exts[nr].disk_bytenr =
6152 btrfs_file_extent_disk_bytenr(leaf, fi);
6153 exts[nr].disk_num_bytes =
6154 btrfs_file_extent_disk_num_bytes(leaf, fi);
6155 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6156 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04006157 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6158 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6159 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6160 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6161 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04006162 BUG_ON(exts[nr].offset > 0);
6163 BUG_ON(exts[nr].compression || exts[nr].encryption);
6164 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006165
6166 cur_pos += exts[nr].num_bytes;
6167 nr++;
6168
6169 if (cur_pos + offset >= last_byte)
6170 break;
6171
6172 if (no_fragment) {
6173 ret = 1;
6174 goto out;
6175 }
6176 path->slots[0]++;
6177 }
6178
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006179 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04006180 if (cur_pos + offset < last_byte) {
6181 ret = -ENOENT;
6182 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05006183 }
6184 ret = 0;
6185out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006186 btrfs_free_path(path);
6187 if (ret) {
6188 if (exts != *extents)
6189 kfree(exts);
6190 } else {
6191 *extents = exts;
6192 *nr_extents = nr;
6193 }
6194 return ret;
6195}
6196
Chris Masond3977122009-01-05 21:25:51 -05006197static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006198 struct btrfs_root *root,
6199 struct btrfs_path *path,
6200 struct btrfs_key *extent_key,
6201 struct btrfs_key *leaf_key,
6202 struct btrfs_ref_path *ref_path,
6203 struct disk_extent *new_extents,
6204 int nr_extents)
6205{
6206 struct extent_buffer *leaf;
6207 struct btrfs_file_extent_item *fi;
6208 struct inode *inode = NULL;
6209 struct btrfs_key key;
6210 u64 lock_start = 0;
6211 u64 lock_end = 0;
6212 u64 num_bytes;
6213 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05006214 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006215 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006216 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006217 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04006218 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04006219 int ret;
6220
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006221 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04006222 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006223 if (key.objectid < ref_path->owner_objectid ||
6224 (key.objectid == ref_path->owner_objectid &&
6225 key.type < BTRFS_EXTENT_DATA_KEY)) {
6226 key.objectid = ref_path->owner_objectid;
6227 key.type = BTRFS_EXTENT_DATA_KEY;
6228 key.offset = 0;
6229 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006230 }
6231
6232 while (1) {
6233 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6234 if (ret < 0)
6235 goto out;
6236
6237 leaf = path->nodes[0];
6238 nritems = btrfs_header_nritems(leaf);
6239next:
6240 if (extent_locked && ret > 0) {
6241 /*
6242 * the file extent item was modified by someone
6243 * before the extent got locked.
6244 */
Zheng Yan1a40e232008-09-26 10:09:34 -04006245 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6246 lock_end, GFP_NOFS);
6247 extent_locked = 0;
6248 }
6249
6250 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006251 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04006252 break;
6253
6254 BUG_ON(extent_locked);
6255 ret = btrfs_next_leaf(root, path);
6256 if (ret < 0)
6257 goto out;
6258 if (ret > 0)
6259 break;
6260 leaf = path->nodes[0];
6261 nritems = btrfs_header_nritems(leaf);
6262 }
6263
6264 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6265
6266 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6267 if ((key.objectid > ref_path->owner_objectid) ||
6268 (key.objectid == ref_path->owner_objectid &&
6269 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05006270 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006271 break;
6272 }
6273
6274 if (inode && key.objectid != inode->i_ino) {
6275 BUG_ON(extent_locked);
6276 btrfs_release_path(root, path);
6277 mutex_unlock(&inode->i_mutex);
6278 iput(inode);
6279 inode = NULL;
6280 continue;
6281 }
6282
6283 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6284 path->slots[0]++;
6285 ret = 1;
6286 goto next;
6287 }
6288 fi = btrfs_item_ptr(leaf, path->slots[0],
6289 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04006290 extent_type = btrfs_file_extent_type(leaf, fi);
6291 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6292 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04006293 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6294 extent_key->objectid)) {
6295 path->slots[0]++;
6296 ret = 1;
6297 goto next;
6298 }
6299
6300 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6301 ext_offset = btrfs_file_extent_offset(leaf, fi);
6302
Yan Zheng86288a12009-01-21 10:49:16 -05006303 if (search_end == (u64)-1) {
6304 search_end = key.offset - ext_offset +
6305 btrfs_file_extent_ram_bytes(leaf, fi);
6306 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006307
6308 if (!extent_locked) {
6309 lock_start = key.offset;
6310 lock_end = lock_start + num_bytes - 1;
6311 } else {
Yan Zheng66435582008-10-30 14:19:50 -04006312 if (lock_start > key.offset ||
6313 lock_end + 1 < key.offset + num_bytes) {
6314 unlock_extent(&BTRFS_I(inode)->io_tree,
6315 lock_start, lock_end, GFP_NOFS);
6316 extent_locked = 0;
6317 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006318 }
6319
6320 if (!inode) {
6321 btrfs_release_path(root, path);
6322
6323 inode = btrfs_iget_locked(root->fs_info->sb,
6324 key.objectid, root);
6325 if (inode->i_state & I_NEW) {
6326 BTRFS_I(inode)->root = root;
6327 BTRFS_I(inode)->location.objectid =
6328 key.objectid;
6329 BTRFS_I(inode)->location.type =
6330 BTRFS_INODE_ITEM_KEY;
6331 BTRFS_I(inode)->location.offset = 0;
6332 btrfs_read_locked_inode(inode);
6333 unlock_new_inode(inode);
6334 }
6335 /*
6336 * some code call btrfs_commit_transaction while
6337 * holding the i_mutex, so we can't use mutex_lock
6338 * here.
6339 */
6340 if (is_bad_inode(inode) ||
6341 !mutex_trylock(&inode->i_mutex)) {
6342 iput(inode);
6343 inode = NULL;
6344 key.offset = (u64)-1;
6345 goto skip;
6346 }
6347 }
6348
6349 if (!extent_locked) {
6350 struct btrfs_ordered_extent *ordered;
6351
6352 btrfs_release_path(root, path);
6353
6354 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6355 lock_end, GFP_NOFS);
6356 ordered = btrfs_lookup_first_ordered_extent(inode,
6357 lock_end);
6358 if (ordered &&
6359 ordered->file_offset <= lock_end &&
6360 ordered->file_offset + ordered->len > lock_start) {
6361 unlock_extent(&BTRFS_I(inode)->io_tree,
6362 lock_start, lock_end, GFP_NOFS);
6363 btrfs_start_ordered_extent(inode, ordered, 1);
6364 btrfs_put_ordered_extent(ordered);
6365 key.offset += num_bytes;
6366 goto skip;
6367 }
6368 if (ordered)
6369 btrfs_put_ordered_extent(ordered);
6370
Zheng Yan1a40e232008-09-26 10:09:34 -04006371 extent_locked = 1;
6372 continue;
6373 }
6374
6375 if (nr_extents == 1) {
6376 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04006377 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6378 new_extents[0].disk_bytenr);
6379 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6380 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006381 btrfs_mark_buffer_dirty(leaf);
6382
6383 btrfs_drop_extent_cache(inode, key.offset,
6384 key.offset + num_bytes - 1, 0);
6385
6386 ret = btrfs_inc_extent_ref(trans, root,
6387 new_extents[0].disk_bytenr,
6388 new_extents[0].disk_num_bytes,
6389 leaf->start,
6390 root->root_key.objectid,
6391 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006392 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006393 BUG_ON(ret);
6394
6395 ret = btrfs_free_extent(trans, root,
6396 extent_key->objectid,
6397 extent_key->offset,
6398 leaf->start,
6399 btrfs_header_owner(leaf),
6400 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006401 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006402 BUG_ON(ret);
6403
6404 btrfs_release_path(root, path);
6405 key.offset += num_bytes;
6406 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04006407 BUG_ON(1);
6408#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006409 u64 alloc_hint;
6410 u64 extent_len;
6411 int i;
6412 /*
6413 * drop old extent pointer at first, then insert the
6414 * new pointers one bye one
6415 */
6416 btrfs_release_path(root, path);
6417 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6418 key.offset + num_bytes,
6419 key.offset, &alloc_hint);
6420 BUG_ON(ret);
6421
6422 for (i = 0; i < nr_extents; i++) {
6423 if (ext_offset >= new_extents[i].num_bytes) {
6424 ext_offset -= new_extents[i].num_bytes;
6425 continue;
6426 }
6427 extent_len = min(new_extents[i].num_bytes -
6428 ext_offset, num_bytes);
6429
6430 ret = btrfs_insert_empty_item(trans, root,
6431 path, &key,
6432 sizeof(*fi));
6433 BUG_ON(ret);
6434
6435 leaf = path->nodes[0];
6436 fi = btrfs_item_ptr(leaf, path->slots[0],
6437 struct btrfs_file_extent_item);
6438 btrfs_set_file_extent_generation(leaf, fi,
6439 trans->transid);
6440 btrfs_set_file_extent_type(leaf, fi,
6441 BTRFS_FILE_EXTENT_REG);
6442 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6443 new_extents[i].disk_bytenr);
6444 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6445 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04006446 btrfs_set_file_extent_ram_bytes(leaf, fi,
6447 new_extents[i].ram_bytes);
6448
6449 btrfs_set_file_extent_compression(leaf, fi,
6450 new_extents[i].compression);
6451 btrfs_set_file_extent_encryption(leaf, fi,
6452 new_extents[i].encryption);
6453 btrfs_set_file_extent_other_encoding(leaf, fi,
6454 new_extents[i].other_encoding);
6455
Zheng Yan1a40e232008-09-26 10:09:34 -04006456 btrfs_set_file_extent_num_bytes(leaf, fi,
6457 extent_len);
6458 ext_offset += new_extents[i].offset;
6459 btrfs_set_file_extent_offset(leaf, fi,
6460 ext_offset);
6461 btrfs_mark_buffer_dirty(leaf);
6462
6463 btrfs_drop_extent_cache(inode, key.offset,
6464 key.offset + extent_len - 1, 0);
6465
6466 ret = btrfs_inc_extent_ref(trans, root,
6467 new_extents[i].disk_bytenr,
6468 new_extents[i].disk_num_bytes,
6469 leaf->start,
6470 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006471 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006472 BUG_ON(ret);
6473 btrfs_release_path(root, path);
6474
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006475 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04006476
6477 ext_offset = 0;
6478 num_bytes -= extent_len;
6479 key.offset += extent_len;
6480
6481 if (num_bytes == 0)
6482 break;
6483 }
6484 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006485#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04006486 }
6487
6488 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006489 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6490 lock_end, GFP_NOFS);
6491 extent_locked = 0;
6492 }
6493skip:
6494 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05006495 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006496 break;
6497
6498 cond_resched();
6499 }
6500 ret = 0;
6501out:
6502 btrfs_release_path(root, path);
6503 if (inode) {
6504 mutex_unlock(&inode->i_mutex);
6505 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006506 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6507 lock_end, GFP_NOFS);
6508 }
6509 iput(inode);
6510 }
6511 return ret;
6512}
6513
Zheng Yan1a40e232008-09-26 10:09:34 -04006514int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6515 struct btrfs_root *root,
6516 struct extent_buffer *buf, u64 orig_start)
6517{
6518 int level;
6519 int ret;
6520
6521 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6522 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6523
6524 level = btrfs_header_level(buf);
6525 if (level == 0) {
6526 struct btrfs_leaf_ref *ref;
6527 struct btrfs_leaf_ref *orig_ref;
6528
6529 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6530 if (!orig_ref)
6531 return -ENOENT;
6532
6533 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6534 if (!ref) {
6535 btrfs_free_leaf_ref(root, orig_ref);
6536 return -ENOMEM;
6537 }
6538
6539 ref->nritems = orig_ref->nritems;
6540 memcpy(ref->extents, orig_ref->extents,
6541 sizeof(ref->extents[0]) * ref->nritems);
6542
6543 btrfs_free_leaf_ref(root, orig_ref);
6544
6545 ref->root_gen = trans->transid;
6546 ref->bytenr = buf->start;
6547 ref->owner = btrfs_header_owner(buf);
6548 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006549
Zheng Yan1a40e232008-09-26 10:09:34 -04006550 ret = btrfs_add_leaf_ref(root, ref, 0);
6551 WARN_ON(ret);
6552 btrfs_free_leaf_ref(root, ref);
6553 }
6554 return 0;
6555}
6556
Chris Masond3977122009-01-05 21:25:51 -05006557static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006558 struct extent_buffer *leaf,
6559 struct btrfs_block_group_cache *group,
6560 struct btrfs_root *target_root)
6561{
6562 struct btrfs_key key;
6563 struct inode *inode = NULL;
6564 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006565 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04006566 u64 num_bytes;
6567 u64 skip_objectid = 0;
6568 u32 nritems;
6569 u32 i;
6570
6571 nritems = btrfs_header_nritems(leaf);
6572 for (i = 0; i < nritems; i++) {
6573 btrfs_item_key_to_cpu(leaf, &key, i);
6574 if (key.objectid == skip_objectid ||
6575 key.type != BTRFS_EXTENT_DATA_KEY)
6576 continue;
6577 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6578 if (btrfs_file_extent_type(leaf, fi) ==
6579 BTRFS_FILE_EXTENT_INLINE)
6580 continue;
6581 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6582 continue;
6583 if (!inode || inode->i_ino != key.objectid) {
6584 iput(inode);
6585 inode = btrfs_ilookup(target_root->fs_info->sb,
6586 key.objectid, target_root, 1);
6587 }
6588 if (!inode) {
6589 skip_objectid = key.objectid;
6590 continue;
6591 }
6592 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6593
Josef Bacik2ac55d42010-02-03 19:33:23 +00006594 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
6595 key.offset + num_bytes - 1, 0, &cached_state,
6596 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006597 btrfs_drop_extent_cache(inode, key.offset,
6598 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006599 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
6600 key.offset + num_bytes - 1, &cached_state,
6601 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006602 cond_resched();
6603 }
6604 iput(inode);
6605 return 0;
6606}
6607
Chris Masond3977122009-01-05 21:25:51 -05006608static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006609 struct btrfs_root *root,
6610 struct extent_buffer *leaf,
6611 struct btrfs_block_group_cache *group,
6612 struct inode *reloc_inode)
6613{
6614 struct btrfs_key key;
6615 struct btrfs_key extent_key;
6616 struct btrfs_file_extent_item *fi;
6617 struct btrfs_leaf_ref *ref;
6618 struct disk_extent *new_extent;
6619 u64 bytenr;
6620 u64 num_bytes;
6621 u32 nritems;
6622 u32 i;
6623 int ext_index;
6624 int nr_extent;
6625 int ret;
6626
6627 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6628 BUG_ON(!new_extent);
6629
6630 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6631 BUG_ON(!ref);
6632
6633 ext_index = -1;
6634 nritems = btrfs_header_nritems(leaf);
6635 for (i = 0; i < nritems; i++) {
6636 btrfs_item_key_to_cpu(leaf, &key, i);
6637 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6638 continue;
6639 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6640 if (btrfs_file_extent_type(leaf, fi) ==
6641 BTRFS_FILE_EXTENT_INLINE)
6642 continue;
6643 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6644 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6645 if (bytenr == 0)
6646 continue;
6647
6648 ext_index++;
6649 if (bytenr >= group->key.objectid + group->key.offset ||
6650 bytenr + num_bytes <= group->key.objectid)
6651 continue;
6652
6653 extent_key.objectid = bytenr;
6654 extent_key.offset = num_bytes;
6655 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6656 nr_extent = 1;
6657 ret = get_new_locations(reloc_inode, &extent_key,
6658 group->key.objectid, 1,
6659 &new_extent, &nr_extent);
6660 if (ret > 0)
6661 continue;
6662 BUG_ON(ret < 0);
6663
6664 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6665 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6666 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6667 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6668
Zheng Yan1a40e232008-09-26 10:09:34 -04006669 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6670 new_extent->disk_bytenr);
6671 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6672 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006673 btrfs_mark_buffer_dirty(leaf);
6674
6675 ret = btrfs_inc_extent_ref(trans, root,
6676 new_extent->disk_bytenr,
6677 new_extent->disk_num_bytes,
6678 leaf->start,
6679 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006680 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006681 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006682
Zheng Yan1a40e232008-09-26 10:09:34 -04006683 ret = btrfs_free_extent(trans, root,
6684 bytenr, num_bytes, leaf->start,
6685 btrfs_header_owner(leaf),
6686 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006687 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006688 BUG_ON(ret);
6689 cond_resched();
6690 }
6691 kfree(new_extent);
6692 BUG_ON(ext_index + 1 != ref->nritems);
6693 btrfs_free_leaf_ref(root, ref);
6694 return 0;
6695}
6696
Yan Zhengf82d02d2008-10-29 14:49:05 -04006697int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6698 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006699{
6700 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006701 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006702
6703 if (root->reloc_root) {
6704 reloc_root = root->reloc_root;
6705 root->reloc_root = NULL;
6706 list_add(&reloc_root->dead_list,
6707 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006708
6709 btrfs_set_root_bytenr(&reloc_root->root_item,
6710 reloc_root->node->start);
6711 btrfs_set_root_level(&root->root_item,
6712 btrfs_header_level(reloc_root->node));
6713 memset(&reloc_root->root_item.drop_progress, 0,
6714 sizeof(struct btrfs_disk_key));
6715 reloc_root->root_item.drop_level = 0;
6716
6717 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6718 &reloc_root->root_key,
6719 &reloc_root->root_item);
6720 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006721 }
6722 return 0;
6723}
6724
6725int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6726{
6727 struct btrfs_trans_handle *trans;
6728 struct btrfs_root *reloc_root;
6729 struct btrfs_root *prev_root = NULL;
6730 struct list_head dead_roots;
6731 int ret;
6732 unsigned long nr;
6733
6734 INIT_LIST_HEAD(&dead_roots);
6735 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6736
6737 while (!list_empty(&dead_roots)) {
6738 reloc_root = list_entry(dead_roots.prev,
6739 struct btrfs_root, dead_list);
6740 list_del_init(&reloc_root->dead_list);
6741
6742 BUG_ON(reloc_root->commit_root != NULL);
6743 while (1) {
6744 trans = btrfs_join_transaction(root, 1);
6745 BUG_ON(!trans);
6746
6747 mutex_lock(&root->fs_info->drop_mutex);
6748 ret = btrfs_drop_snapshot(trans, reloc_root);
6749 if (ret != -EAGAIN)
6750 break;
6751 mutex_unlock(&root->fs_info->drop_mutex);
6752
6753 nr = trans->blocks_used;
6754 ret = btrfs_end_transaction(trans, root);
6755 BUG_ON(ret);
6756 btrfs_btree_balance_dirty(root, nr);
6757 }
6758
6759 free_extent_buffer(reloc_root->node);
6760
6761 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6762 &reloc_root->root_key);
6763 BUG_ON(ret);
6764 mutex_unlock(&root->fs_info->drop_mutex);
6765
6766 nr = trans->blocks_used;
6767 ret = btrfs_end_transaction(trans, root);
6768 BUG_ON(ret);
6769 btrfs_btree_balance_dirty(root, nr);
6770
6771 kfree(prev_root);
6772 prev_root = reloc_root;
6773 }
6774 if (prev_root) {
6775 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6776 kfree(prev_root);
6777 }
6778 return 0;
6779}
6780
6781int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6782{
6783 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6784 return 0;
6785}
6786
6787int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6788{
6789 struct btrfs_root *reloc_root;
6790 struct btrfs_trans_handle *trans;
6791 struct btrfs_key location;
6792 int found;
6793 int ret;
6794
6795 mutex_lock(&root->fs_info->tree_reloc_mutex);
6796 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6797 BUG_ON(ret);
6798 found = !list_empty(&root->fs_info->dead_reloc_roots);
6799 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6800
6801 if (found) {
6802 trans = btrfs_start_transaction(root, 1);
6803 BUG_ON(!trans);
6804 ret = btrfs_commit_transaction(trans, root);
6805 BUG_ON(ret);
6806 }
6807
6808 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6809 location.offset = (u64)-1;
6810 location.type = BTRFS_ROOT_ITEM_KEY;
6811
6812 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6813 BUG_ON(!reloc_root);
6814 btrfs_orphan_cleanup(reloc_root);
6815 return 0;
6816}
6817
Chris Masond3977122009-01-05 21:25:51 -05006818static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006819 struct btrfs_root *root)
6820{
6821 struct btrfs_root *reloc_root;
6822 struct extent_buffer *eb;
6823 struct btrfs_root_item *root_item;
6824 struct btrfs_key root_key;
6825 int ret;
6826
6827 BUG_ON(!root->ref_cows);
6828 if (root->reloc_root)
6829 return 0;
6830
6831 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6832 BUG_ON(!root_item);
6833
6834 ret = btrfs_copy_root(trans, root, root->commit_root,
6835 &eb, BTRFS_TREE_RELOC_OBJECTID);
6836 BUG_ON(ret);
6837
6838 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6839 root_key.offset = root->root_key.objectid;
6840 root_key.type = BTRFS_ROOT_ITEM_KEY;
6841
6842 memcpy(root_item, &root->root_item, sizeof(root_item));
6843 btrfs_set_root_refs(root_item, 0);
6844 btrfs_set_root_bytenr(root_item, eb->start);
6845 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006846 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006847
6848 btrfs_tree_unlock(eb);
6849 free_extent_buffer(eb);
6850
6851 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6852 &root_key, root_item);
6853 BUG_ON(ret);
6854 kfree(root_item);
6855
6856 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6857 &root_key);
6858 BUG_ON(!reloc_root);
6859 reloc_root->last_trans = trans->transid;
6860 reloc_root->commit_root = NULL;
6861 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6862
6863 root->reloc_root = reloc_root;
6864 return 0;
6865}
6866
6867/*
6868 * Core function of space balance.
6869 *
6870 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006871 * counted roots. There is one reloc tree for each subvol, and all
6872 * reloc trees share same root key objectid. Reloc trees are snapshots
6873 * of the latest committed roots of subvols (root->commit_root).
6874 *
6875 * To relocate a tree block referenced by a subvol, there are two steps.
6876 * COW the block through subvol's reloc tree, then update block pointer
6877 * in the subvol to point to the new block. Since all reloc trees share
6878 * same root key objectid, doing special handing for tree blocks owned
6879 * by them is easy. Once a tree block has been COWed in one reloc tree,
6880 * we can use the resulting new block directly when the same block is
6881 * required to COW again through other reloc trees. By this way, relocated
6882 * tree blocks are shared between reloc trees, so they are also shared
6883 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006884 */
Chris Masond3977122009-01-05 21:25:51 -05006885static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006886 struct btrfs_root *root,
6887 struct btrfs_path *path,
6888 struct btrfs_key *first_key,
6889 struct btrfs_ref_path *ref_path,
6890 struct btrfs_block_group_cache *group,
6891 struct inode *reloc_inode)
6892{
6893 struct btrfs_root *reloc_root;
6894 struct extent_buffer *eb = NULL;
6895 struct btrfs_key *keys;
6896 u64 *nodes;
6897 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006898 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006899 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006900 int ret;
6901
6902 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6903 lowest_level = ref_path->owner_objectid;
6904
Yan Zhengf82d02d2008-10-29 14:49:05 -04006905 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006906 path->lowest_level = lowest_level;
6907 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6908 BUG_ON(ret < 0);
6909 path->lowest_level = 0;
6910 btrfs_release_path(root, path);
6911 return 0;
6912 }
6913
Zheng Yan1a40e232008-09-26 10:09:34 -04006914 mutex_lock(&root->fs_info->tree_reloc_mutex);
6915 ret = init_reloc_tree(trans, root);
6916 BUG_ON(ret);
6917 reloc_root = root->reloc_root;
6918
Yan Zhengf82d02d2008-10-29 14:49:05 -04006919 shared_level = ref_path->shared_level;
6920 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006921
Yan Zhengf82d02d2008-10-29 14:49:05 -04006922 keys = ref_path->node_keys;
6923 nodes = ref_path->new_nodes;
6924 memset(&keys[shared_level + 1], 0,
6925 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6926 memset(&nodes[shared_level + 1], 0,
6927 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006928
Yan Zhengf82d02d2008-10-29 14:49:05 -04006929 if (nodes[lowest_level] == 0) {
6930 path->lowest_level = lowest_level;
6931 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6932 0, 1);
6933 BUG_ON(ret);
6934 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6935 eb = path->nodes[level];
6936 if (!eb || eb == reloc_root->node)
6937 break;
6938 nodes[level] = eb->start;
6939 if (level == 0)
6940 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6941 else
6942 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6943 }
Yan Zheng2b820322008-11-17 21:11:30 -05006944 if (nodes[0] &&
6945 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006946 eb = path->nodes[0];
6947 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6948 group, reloc_inode);
6949 BUG_ON(ret);
6950 }
6951 btrfs_release_path(reloc_root, path);
6952 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006953 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006954 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006955 BUG_ON(ret);
6956 }
6957
Zheng Yan1a40e232008-09-26 10:09:34 -04006958 /*
6959 * replace tree blocks in the fs tree with tree blocks in
6960 * the reloc tree.
6961 */
6962 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6963 BUG_ON(ret < 0);
6964
6965 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006966 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6967 0, 0);
6968 BUG_ON(ret);
6969 extent_buffer_get(path->nodes[0]);
6970 eb = path->nodes[0];
6971 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006972 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6973 BUG_ON(ret);
6974 free_extent_buffer(eb);
6975 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006976
Yan Zhengf82d02d2008-10-29 14:49:05 -04006977 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006978 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006979 return 0;
6980}
6981
Chris Masond3977122009-01-05 21:25:51 -05006982static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006983 struct btrfs_root *root,
6984 struct btrfs_path *path,
6985 struct btrfs_key *first_key,
6986 struct btrfs_ref_path *ref_path)
6987{
6988 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006989
6990 ret = relocate_one_path(trans, root, path, first_key,
6991 ref_path, NULL, NULL);
6992 BUG_ON(ret);
6993
Zheng Yan1a40e232008-09-26 10:09:34 -04006994 return 0;
6995}
6996
Chris Masond3977122009-01-05 21:25:51 -05006997static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006998 struct btrfs_root *extent_root,
6999 struct btrfs_path *path,
7000 struct btrfs_key *extent_key)
7001{
7002 int ret;
7003
Zheng Yan1a40e232008-09-26 10:09:34 -04007004 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7005 if (ret)
7006 goto out;
7007 ret = btrfs_del_item(trans, extent_root, path);
7008out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007009 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007010 return ret;
7011}
7012
Chris Masond3977122009-01-05 21:25:51 -05007013static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007014 struct btrfs_ref_path *ref_path)
7015{
7016 struct btrfs_key root_key;
7017
7018 root_key.objectid = ref_path->root_objectid;
7019 root_key.type = BTRFS_ROOT_ITEM_KEY;
7020 if (is_cowonly_root(ref_path->root_objectid))
7021 root_key.offset = 0;
7022 else
7023 root_key.offset = (u64)-1;
7024
7025 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7026}
7027
Chris Masond3977122009-01-05 21:25:51 -05007028static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007029 struct btrfs_path *path,
7030 struct btrfs_key *extent_key,
7031 struct btrfs_block_group_cache *group,
7032 struct inode *reloc_inode, int pass)
7033{
7034 struct btrfs_trans_handle *trans;
7035 struct btrfs_root *found_root;
7036 struct btrfs_ref_path *ref_path = NULL;
7037 struct disk_extent *new_extents = NULL;
7038 int nr_extents = 0;
7039 int loops;
7040 int ret;
7041 int level;
7042 struct btrfs_key first_key;
7043 u64 prev_block = 0;
7044
Zheng Yan1a40e232008-09-26 10:09:34 -04007045
7046 trans = btrfs_start_transaction(extent_root, 1);
7047 BUG_ON(!trans);
7048
7049 if (extent_key->objectid == 0) {
7050 ret = del_extent_zero(trans, extent_root, path, extent_key);
7051 goto out;
7052 }
7053
7054 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7055 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007056 ret = -ENOMEM;
7057 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007058 }
7059
7060 for (loops = 0; ; loops++) {
7061 if (loops == 0) {
7062 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7063 extent_key->objectid);
7064 } else {
7065 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7066 }
7067 if (ret < 0)
7068 goto out;
7069 if (ret > 0)
7070 break;
7071
7072 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7073 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7074 continue;
7075
7076 found_root = read_ref_root(extent_root->fs_info, ref_path);
7077 BUG_ON(!found_root);
7078 /*
7079 * for reference counted tree, only process reference paths
7080 * rooted at the latest committed root.
7081 */
7082 if (found_root->ref_cows &&
7083 ref_path->root_generation != found_root->root_key.offset)
7084 continue;
7085
7086 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7087 if (pass == 0) {
7088 /*
7089 * copy data extents to new locations
7090 */
7091 u64 group_start = group->key.objectid;
7092 ret = relocate_data_extent(reloc_inode,
7093 extent_key,
7094 group_start);
7095 if (ret < 0)
7096 goto out;
7097 break;
7098 }
7099 level = 0;
7100 } else {
7101 level = ref_path->owner_objectid;
7102 }
7103
7104 if (prev_block != ref_path->nodes[level]) {
7105 struct extent_buffer *eb;
7106 u64 block_start = ref_path->nodes[level];
7107 u64 block_size = btrfs_level_size(found_root, level);
7108
7109 eb = read_tree_block(found_root, block_start,
7110 block_size, 0);
7111 btrfs_tree_lock(eb);
7112 BUG_ON(level != btrfs_header_level(eb));
7113
7114 if (level == 0)
7115 btrfs_item_key_to_cpu(eb, &first_key, 0);
7116 else
7117 btrfs_node_key_to_cpu(eb, &first_key, 0);
7118
7119 btrfs_tree_unlock(eb);
7120 free_extent_buffer(eb);
7121 prev_block = block_start;
7122 }
7123
Yan Zheng24562422009-02-12 14:14:53 -05007124 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007125 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05007126 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007127 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7128 /*
7129 * try to update data extent references while
7130 * keeping metadata shared between snapshots.
7131 */
7132 if (pass == 1) {
7133 ret = relocate_one_path(trans, found_root,
7134 path, &first_key, ref_path,
7135 group, reloc_inode);
7136 if (ret < 0)
7137 goto out;
7138 continue;
7139 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007140 /*
7141 * use fallback method to process the remaining
7142 * references.
7143 */
7144 if (!new_extents) {
7145 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04007146 new_extents = kmalloc(sizeof(*new_extents),
7147 GFP_NOFS);
7148 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007149 ret = get_new_locations(reloc_inode,
7150 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04007151 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04007152 &new_extents,
7153 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007154 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04007155 goto out;
7156 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007157 ret = replace_one_extent(trans, found_root,
7158 path, extent_key,
7159 &first_key, ref_path,
7160 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05007161 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007162 ret = relocate_tree_block(trans, found_root, path,
7163 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007164 }
7165 if (ret < 0)
7166 goto out;
7167 }
7168 ret = 0;
7169out:
7170 btrfs_end_transaction(trans, extent_root);
7171 kfree(new_extents);
7172 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007173 return ret;
7174}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007175#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007176
Chris Masonec44a352008-04-28 15:29:52 -04007177static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7178{
7179 u64 num_devices;
7180 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7181 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7182
Yan Zheng2b820322008-11-17 21:11:30 -05007183 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04007184 if (num_devices == 1) {
7185 stripped |= BTRFS_BLOCK_GROUP_DUP;
7186 stripped = flags & ~stripped;
7187
7188 /* turn raid0 into single device chunks */
7189 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7190 return stripped;
7191
7192 /* turn mirroring into duplication */
7193 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7194 BTRFS_BLOCK_GROUP_RAID10))
7195 return stripped | BTRFS_BLOCK_GROUP_DUP;
7196 return flags;
7197 } else {
7198 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007199 if (flags & stripped)
7200 return flags;
7201
7202 stripped |= BTRFS_BLOCK_GROUP_DUP;
7203 stripped = flags & ~stripped;
7204
7205 /* switch duplicated blocks with raid1 */
7206 if (flags & BTRFS_BLOCK_GROUP_DUP)
7207 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7208
7209 /* turn single device chunks into raid0 */
7210 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7211 }
7212 return flags;
7213}
7214
Christoph Hellwigb2950862008-12-02 09:54:17 -05007215static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04007216 struct btrfs_block_group_cache *shrink_block_group,
7217 int force)
7218{
7219 struct btrfs_trans_handle *trans;
7220 u64 new_alloc_flags;
7221 u64 calc;
7222
Chris Masonc286ac42008-07-22 23:06:41 -04007223 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007224 if (btrfs_block_group_used(&shrink_block_group->item) +
7225 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04007226 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04007227
Chris Mason0ef3e662008-05-24 14:04:53 -04007228 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007229 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04007230
Chris Mason0ef3e662008-05-24 14:04:53 -04007231 new_alloc_flags = update_block_group_flags(root,
7232 shrink_block_group->flags);
7233 if (new_alloc_flags != shrink_block_group->flags) {
7234 calc =
7235 btrfs_block_group_used(&shrink_block_group->item);
7236 } else {
7237 calc = shrink_block_group->key.offset;
7238 }
Chris Masonc286ac42008-07-22 23:06:41 -04007239 spin_unlock(&shrink_block_group->lock);
7240
Chris Mason0ef3e662008-05-24 14:04:53 -04007241 do_chunk_alloc(trans, root->fs_info->extent_root,
7242 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04007243
Chris Mason0ef3e662008-05-24 14:04:53 -04007244 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04007245 } else
7246 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04007247 return 0;
7248}
7249
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007250
7251int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
7252 struct btrfs_block_group_cache *group)
7253
7254{
7255 __alloc_chunk_for_shrink(root, group, 1);
7256 set_block_group_readonly(group);
7257 return 0;
7258}
7259
Josef Bacikba1bf482009-09-11 16:11:19 -04007260/*
7261 * checks to see if its even possible to relocate this block group.
7262 *
7263 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7264 * ok to go ahead and try.
7265 */
7266int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007267{
Zheng Yan1a40e232008-09-26 10:09:34 -04007268 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007269 struct btrfs_space_info *space_info;
7270 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7271 struct btrfs_device *device;
7272 int full = 0;
7273 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007274
Josef Bacikba1bf482009-09-11 16:11:19 -04007275 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007276
Josef Bacikba1bf482009-09-11 16:11:19 -04007277 /* odd, couldn't find the block group, leave it alone */
7278 if (!block_group)
7279 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007280
Josef Bacikba1bf482009-09-11 16:11:19 -04007281 /* no bytes used, we're good */
7282 if (!btrfs_block_group_used(&block_group->item))
7283 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007284
Josef Bacikba1bf482009-09-11 16:11:19 -04007285 space_info = block_group->space_info;
7286 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007287
Josef Bacikba1bf482009-09-11 16:11:19 -04007288 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007289
Josef Bacikba1bf482009-09-11 16:11:19 -04007290 /*
7291 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007292 * relocate it unless we're able to allocate a new chunk below.
7293 *
7294 * Otherwise, we need to make sure we have room in the space to handle
7295 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007296 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007297 if ((space_info->total_bytes != block_group->key.offset) &&
7298 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04007299 space_info->bytes_pinned + space_info->bytes_readonly +
7300 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04007301 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007302 spin_unlock(&space_info->lock);
7303 goto out;
7304 }
7305 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007306
Josef Bacikba1bf482009-09-11 16:11:19 -04007307 /*
7308 * ok we don't have enough space, but maybe we have free space on our
7309 * devices to allocate new chunks for relocation, so loop through our
7310 * alloc devices and guess if we have enough space. However, if we
7311 * were marked as full, then we know there aren't enough chunks, and we
7312 * can just return.
7313 */
7314 ret = -1;
7315 if (full)
7316 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05007317
Josef Bacikba1bf482009-09-11 16:11:19 -04007318 mutex_lock(&root->fs_info->chunk_mutex);
7319 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7320 u64 min_free = btrfs_block_group_used(&block_group->item);
7321 u64 dev_offset, max_avail;
Chris Masonea8c2812008-08-04 23:17:27 -04007322
Josef Bacikba1bf482009-09-11 16:11:19 -04007323 /*
7324 * check to make sure we can actually find a chunk with enough
7325 * space to fit our block group in.
7326 */
7327 if (device->total_bytes > device->bytes_used + min_free) {
7328 ret = find_free_dev_extent(NULL, device, min_free,
7329 &dev_offset, &max_avail);
7330 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05007331 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04007332 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007333 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007334 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007335 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007336out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007337 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007338 return ret;
7339}
7340
Christoph Hellwigb2950862008-12-02 09:54:17 -05007341static int find_first_block_group(struct btrfs_root *root,
7342 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007343{
Chris Mason925baed2008-06-25 16:01:30 -04007344 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007345 struct btrfs_key found_key;
7346 struct extent_buffer *leaf;
7347 int slot;
7348
7349 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7350 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007351 goto out;
7352
Chris Masond3977122009-01-05 21:25:51 -05007353 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007354 slot = path->slots[0];
7355 leaf = path->nodes[0];
7356 if (slot >= btrfs_header_nritems(leaf)) {
7357 ret = btrfs_next_leaf(root, path);
7358 if (ret == 0)
7359 continue;
7360 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007361 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007362 break;
7363 }
7364 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7365
7366 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007367 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7368 ret = 0;
7369 goto out;
7370 }
Chris Mason0b86a832008-03-24 15:01:56 -04007371 path->slots[0]++;
7372 }
7373 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04007374out:
Chris Mason0b86a832008-03-24 15:01:56 -04007375 return ret;
7376}
7377
Zheng Yan1a40e232008-09-26 10:09:34 -04007378int btrfs_free_block_groups(struct btrfs_fs_info *info)
7379{
7380 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007381 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007382 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007383 struct rb_node *n;
7384
Yan Zheng11833d62009-09-11 16:11:19 -04007385 down_write(&info->extent_commit_sem);
7386 while (!list_empty(&info->caching_block_groups)) {
7387 caching_ctl = list_entry(info->caching_block_groups.next,
7388 struct btrfs_caching_control, list);
7389 list_del(&caching_ctl->list);
7390 put_caching_control(caching_ctl);
7391 }
7392 up_write(&info->extent_commit_sem);
7393
Zheng Yan1a40e232008-09-26 10:09:34 -04007394 spin_lock(&info->block_group_cache_lock);
7395 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7396 block_group = rb_entry(n, struct btrfs_block_group_cache,
7397 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007398 rb_erase(&block_group->cache_node,
7399 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007400 spin_unlock(&info->block_group_cache_lock);
7401
Josef Bacik80eb2342008-10-29 14:49:05 -04007402 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007403 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007404 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007405
Josef Bacik817d52f2009-07-13 21:29:25 -04007406 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007407 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007408
7409 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007410 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007411
7412 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007413 }
7414 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007415
7416 /* now that all the block groups are freed, go through and
7417 * free all the space_info structs. This is only called during
7418 * the final stages of unmount, and so we know nobody is
7419 * using them. We call synchronize_rcu() once before we start,
7420 * just to be on the safe side.
7421 */
7422 synchronize_rcu();
7423
7424 while(!list_empty(&info->space_info)) {
7425 space_info = list_entry(info->space_info.next,
7426 struct btrfs_space_info,
7427 list);
7428
7429 list_del(&space_info->list);
7430 kfree(space_info);
7431 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007432 return 0;
7433}
7434
Chris Mason9078a3e2007-04-26 16:46:15 -04007435int btrfs_read_block_groups(struct btrfs_root *root)
7436{
7437 struct btrfs_path *path;
7438 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007439 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007440 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007441 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007442 struct btrfs_key key;
7443 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007444 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007445
Chris Masonbe744172007-05-06 10:15:01 -04007446 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007447 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007448 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007449 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007450 path = btrfs_alloc_path();
7451 if (!path)
7452 return -ENOMEM;
7453
Chris Masond3977122009-01-05 21:25:51 -05007454 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007455 ret = find_first_block_group(root, path, &key);
7456 if (ret > 0) {
7457 ret = 0;
7458 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007459 }
Chris Mason0b86a832008-03-24 15:01:56 -04007460 if (ret != 0)
7461 goto error;
7462
Chris Mason5f39d392007-10-15 16:14:19 -04007463 leaf = path->nodes[0];
7464 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007465 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007466 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007467 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04007468 break;
7469 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007470
Yan Zhengd2fb3432008-12-11 16:30:39 -05007471 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007472 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04007473 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007474 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007475 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007476 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007477
7478 /*
7479 * we only want to have 32k of ram per block group for keeping
7480 * track of free space, and if we pass 1/2 of that we want to
7481 * start converting things over to using bitmaps
7482 */
7483 cache->extents_thresh = ((1024 * 32) / 2) /
7484 sizeof(struct btrfs_free_space);
7485
Chris Mason5f39d392007-10-15 16:14:19 -04007486 read_extent_buffer(leaf, &cache->item,
7487 btrfs_item_ptr_offset(leaf, path->slots[0]),
7488 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007489 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007490
Chris Mason9078a3e2007-04-26 16:46:15 -04007491 key.objectid = found_key.objectid + found_key.offset;
7492 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007493 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007494 cache->sectorsize = root->sectorsize;
7495
Josef Bacik817d52f2009-07-13 21:29:25 -04007496 /*
7497 * check for two cases, either we are full, and therefore
7498 * don't need to bother with the caching work since we won't
7499 * find any space, or we are empty, and we can just add all
7500 * the space in and be done with it. This saves us _alot_ of
7501 * time, particularly in the full case.
7502 */
7503 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04007504 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04007505 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007506 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007507 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007508 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007509 exclude_super_stripes(root, cache);
7510 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007511 cache->cached = BTRFS_CACHE_FINISHED;
7512 add_new_free_space(cache, root->fs_info,
7513 found_key.objectid,
7514 found_key.objectid +
7515 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007516 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007517 }
Chris Mason96b51792007-10-15 16:15:19 -04007518
Chris Mason6324fbf2008-03-24 15:01:59 -04007519 ret = update_space_info(info, cache->flags, found_key.offset,
7520 btrfs_block_group_used(&cache->item),
7521 &space_info);
7522 BUG_ON(ret);
7523 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007524 spin_lock(&cache->space_info->lock);
7525 cache->space_info->bytes_super += cache->bytes_super;
7526 spin_unlock(&cache->space_info->lock);
7527
Josef Bacik80eb2342008-10-29 14:49:05 -04007528 down_write(&space_info->groups_sem);
7529 list_add_tail(&cache->list, &space_info->block_groups);
7530 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007531
Josef Bacik0f9dd462008-09-23 13:14:11 -04007532 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7533 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007534
7535 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007536 if (btrfs_chunk_readonly(root, cache->key.objectid))
7537 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007538 }
Chris Mason0b86a832008-03-24 15:01:56 -04007539 ret = 0;
7540error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007541 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007542 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007543}
Chris Mason6324fbf2008-03-24 15:01:59 -04007544
7545int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7546 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007547 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007548 u64 size)
7549{
7550 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007551 struct btrfs_root *extent_root;
7552 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007553
7554 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007555
Chris Mason12fcfd22009-03-24 10:24:20 -04007556 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007557
Chris Mason8f18cf12008-04-25 16:53:30 -04007558 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007559 if (!cache)
7560 return -ENOMEM;
7561
Chris Masone17cade2008-04-15 15:41:47 -04007562 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007563 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007564 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007565 cache->sectorsize = root->sectorsize;
7566
7567 /*
7568 * we only want to have 32k of ram per block group for keeping track
7569 * of free space, and if we pass 1/2 of that we want to start
7570 * converting things over to using bitmaps
7571 */
7572 cache->extents_thresh = ((1024 * 32) / 2) /
7573 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007574 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007575 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04007576 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007577 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007578 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007579
Chris Mason6324fbf2008-03-24 15:01:59 -04007580 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007581 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7582 cache->flags = type;
7583 btrfs_set_block_group_flags(&cache->item, type);
7584
Yan Zheng11833d62009-09-11 16:11:19 -04007585 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007586 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007587 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007588
Josef Bacik817d52f2009-07-13 21:29:25 -04007589 add_new_free_space(cache, root->fs_info, chunk_offset,
7590 chunk_offset + size);
7591
Yan Zheng11833d62009-09-11 16:11:19 -04007592 free_excluded_extents(root, cache);
7593
Chris Mason6324fbf2008-03-24 15:01:59 -04007594 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7595 &cache->space_info);
7596 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007597
7598 spin_lock(&cache->space_info->lock);
7599 cache->space_info->bytes_super += cache->bytes_super;
7600 spin_unlock(&cache->space_info->lock);
7601
Josef Bacik80eb2342008-10-29 14:49:05 -04007602 down_write(&cache->space_info->groups_sem);
7603 list_add_tail(&cache->list, &cache->space_info->block_groups);
7604 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007605
Josef Bacik0f9dd462008-09-23 13:14:11 -04007606 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7607 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007608
Chris Mason6324fbf2008-03-24 15:01:59 -04007609 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7610 sizeof(cache->item));
7611 BUG_ON(ret);
7612
Chris Masond18a2c42008-04-04 15:40:00 -04007613 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007614
Chris Mason6324fbf2008-03-24 15:01:59 -04007615 return 0;
7616}
Zheng Yan1a40e232008-09-26 10:09:34 -04007617
7618int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7619 struct btrfs_root *root, u64 group_start)
7620{
7621 struct btrfs_path *path;
7622 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007623 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007624 struct btrfs_key key;
7625 int ret;
7626
Zheng Yan1a40e232008-09-26 10:09:34 -04007627 root = root->fs_info->extent_root;
7628
7629 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7630 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007631 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007632
7633 memcpy(&key, &block_group->key, sizeof(key));
7634
Chris Mason44fb5512009-06-04 15:34:51 -04007635 /* make sure this block group isn't part of an allocation cluster */
7636 cluster = &root->fs_info->data_alloc_cluster;
7637 spin_lock(&cluster->refill_lock);
7638 btrfs_return_cluster_to_free_space(block_group, cluster);
7639 spin_unlock(&cluster->refill_lock);
7640
7641 /*
7642 * make sure this block group isn't part of a metadata
7643 * allocation cluster
7644 */
7645 cluster = &root->fs_info->meta_alloc_cluster;
7646 spin_lock(&cluster->refill_lock);
7647 btrfs_return_cluster_to_free_space(block_group, cluster);
7648 spin_unlock(&cluster->refill_lock);
7649
Zheng Yan1a40e232008-09-26 10:09:34 -04007650 path = btrfs_alloc_path();
7651 BUG_ON(!path);
7652
Yan Zheng3dfdb932009-01-21 10:49:16 -05007653 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007654 rb_erase(&block_group->cache_node,
7655 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007656 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007657
Josef Bacik80eb2342008-10-29 14:49:05 -04007658 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007659 /*
7660 * we must use list_del_init so people can check to see if they
7661 * are still on the list after taking the semaphore
7662 */
7663 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007664 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007665
Josef Bacik817d52f2009-07-13 21:29:25 -04007666 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007667 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007668
7669 btrfs_remove_free_space_cache(block_group);
7670
Yan Zhengc146afa2008-11-12 14:34:12 -05007671 spin_lock(&block_group->space_info->lock);
7672 block_group->space_info->total_bytes -= block_group->key.offset;
7673 block_group->space_info->bytes_readonly -= block_group->key.offset;
7674 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007675
7676 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007677
Chris Masonfa9c0d792009-04-03 09:47:43 -04007678 btrfs_put_block_group(block_group);
7679 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007680
7681 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7682 if (ret > 0)
7683 ret = -EIO;
7684 if (ret < 0)
7685 goto out;
7686
7687 ret = btrfs_del_item(trans, root, path);
7688out:
7689 btrfs_free_path(path);
7690 return ret;
7691}