blob: f32b1618ee6ddc4da960234a06941120b92cdf39 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050027#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040031#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040033#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040034#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Josef Bacikf3465ca2008-11-12 14:19:50 -050036static int update_block_group(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 u64 bytenr, u64 num_bytes, int alloc,
39 int mark_free);
Yan Zheng11833d62009-09-11 16:11:19 -040040static int update_reserved_extents(struct btrfs_block_group_cache *cache,
41 u64 num_bytes, int reserve);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040042static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
43 struct btrfs_root *root,
44 u64 bytenr, u64 num_bytes, u64 parent,
45 u64 root_objectid, u64 owner_objectid,
46 u64 owner_offset, int refs_to_drop,
47 struct btrfs_delayed_extent_op *extra_op);
48static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
49 struct extent_buffer *leaf,
50 struct btrfs_extent_item *ei);
51static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
52 struct btrfs_root *root,
53 u64 parent, u64 root_objectid,
54 u64 flags, u64 owner, u64 offset,
55 struct btrfs_key *ins, int ref_mod);
56static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
57 struct btrfs_root *root,
58 u64 parent, u64 root_objectid,
59 u64 flags, struct btrfs_disk_key *key,
60 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050061static int do_chunk_alloc(struct btrfs_trans_handle *trans,
62 struct btrfs_root *extent_root, u64 alloc_bytes,
63 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040064static int pin_down_bytes(struct btrfs_trans_handle *trans,
65 struct btrfs_root *root,
66 struct btrfs_path *path,
67 u64 bytenr, u64 num_bytes,
68 int is_data, int reserved,
69 struct extent_buffer **must_clean);
70static int find_next_key(struct btrfs_path *path, int level,
71 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040072static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
73 int dump_block_groups);
Yan, Zheng424499d2010-05-16 10:46:25 -040074static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
75 struct btrfs_root *root,
76 struct btrfs_space_info *sinfo, u64 num_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -040077static int shrink_delalloc(struct btrfs_trans_handle *trans,
78 struct btrfs_root *root,
79 struct btrfs_space_info *sinfo, u64 to_reclaim);
Josef Bacik6a632092009-02-20 11:00:09 -050080
Josef Bacik817d52f2009-07-13 21:29:25 -040081static noinline int
82block_group_cache_done(struct btrfs_block_group_cache *cache)
83{
84 smp_mb();
85 return cache->cached == BTRFS_CACHE_FINISHED;
86}
87
Josef Bacik0f9dd462008-09-23 13:14:11 -040088static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
89{
90 return (cache->flags & bits) == bits;
91}
92
Josef Bacik11dfe352009-11-13 20:12:59 +000093void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
94{
95 atomic_inc(&cache->count);
96}
97
98void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
99{
100 if (atomic_dec_and_test(&cache->count))
101 kfree(cache);
102}
103
Josef Bacik0f9dd462008-09-23 13:14:11 -0400104/*
105 * this adds the block group to the fs_info rb tree for the block group
106 * cache
107 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500108static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400109 struct btrfs_block_group_cache *block_group)
110{
111 struct rb_node **p;
112 struct rb_node *parent = NULL;
113 struct btrfs_block_group_cache *cache;
114
115 spin_lock(&info->block_group_cache_lock);
116 p = &info->block_group_cache_tree.rb_node;
117
118 while (*p) {
119 parent = *p;
120 cache = rb_entry(parent, struct btrfs_block_group_cache,
121 cache_node);
122 if (block_group->key.objectid < cache->key.objectid) {
123 p = &(*p)->rb_left;
124 } else if (block_group->key.objectid > cache->key.objectid) {
125 p = &(*p)->rb_right;
126 } else {
127 spin_unlock(&info->block_group_cache_lock);
128 return -EEXIST;
129 }
130 }
131
132 rb_link_node(&block_group->cache_node, parent, p);
133 rb_insert_color(&block_group->cache_node,
134 &info->block_group_cache_tree);
135 spin_unlock(&info->block_group_cache_lock);
136
137 return 0;
138}
139
140/*
141 * This will return the block group at or after bytenr if contains is 0, else
142 * it will return the block group that contains the bytenr
143 */
144static struct btrfs_block_group_cache *
145block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
146 int contains)
147{
148 struct btrfs_block_group_cache *cache, *ret = NULL;
149 struct rb_node *n;
150 u64 end, start;
151
152 spin_lock(&info->block_group_cache_lock);
153 n = info->block_group_cache_tree.rb_node;
154
155 while (n) {
156 cache = rb_entry(n, struct btrfs_block_group_cache,
157 cache_node);
158 end = cache->key.objectid + cache->key.offset - 1;
159 start = cache->key.objectid;
160
161 if (bytenr < start) {
162 if (!contains && (!ret || start < ret->key.objectid))
163 ret = cache;
164 n = n->rb_left;
165 } else if (bytenr > start) {
166 if (contains && bytenr <= end) {
167 ret = cache;
168 break;
169 }
170 n = n->rb_right;
171 } else {
172 ret = cache;
173 break;
174 }
175 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500176 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000177 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400178 spin_unlock(&info->block_group_cache_lock);
179
180 return ret;
181}
182
Yan Zheng11833d62009-09-11 16:11:19 -0400183static int add_excluded_extent(struct btrfs_root *root,
184 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400185{
Yan Zheng11833d62009-09-11 16:11:19 -0400186 u64 end = start + num_bytes - 1;
187 set_extent_bits(&root->fs_info->freed_extents[0],
188 start, end, EXTENT_UPTODATE, GFP_NOFS);
189 set_extent_bits(&root->fs_info->freed_extents[1],
190 start, end, EXTENT_UPTODATE, GFP_NOFS);
191 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400192}
193
Yan Zheng11833d62009-09-11 16:11:19 -0400194static void free_excluded_extents(struct btrfs_root *root,
195 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400196{
Yan Zheng11833d62009-09-11 16:11:19 -0400197 u64 start, end;
198
199 start = cache->key.objectid;
200 end = start + cache->key.offset - 1;
201
202 clear_extent_bits(&root->fs_info->freed_extents[0],
203 start, end, EXTENT_UPTODATE, GFP_NOFS);
204 clear_extent_bits(&root->fs_info->freed_extents[1],
205 start, end, EXTENT_UPTODATE, GFP_NOFS);
206}
207
208static int exclude_super_stripes(struct btrfs_root *root,
209 struct btrfs_block_group_cache *cache)
210{
Josef Bacik817d52f2009-07-13 21:29:25 -0400211 u64 bytenr;
212 u64 *logical;
213 int stripe_len;
214 int i, nr, ret;
215
Yan, Zheng06b23312009-11-26 09:31:11 +0000216 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
217 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
218 cache->bytes_super += stripe_len;
219 ret = add_excluded_extent(root, cache->key.objectid,
220 stripe_len);
221 BUG_ON(ret);
222 }
223
Josef Bacik817d52f2009-07-13 21:29:25 -0400224 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
225 bytenr = btrfs_sb_offset(i);
226 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
227 cache->key.objectid, bytenr,
228 0, &logical, &nr, &stripe_len);
229 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400230
Josef Bacik817d52f2009-07-13 21:29:25 -0400231 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400232 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400233 ret = add_excluded_extent(root, logical[nr],
234 stripe_len);
235 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400236 }
Yan Zheng11833d62009-09-11 16:11:19 -0400237
Josef Bacik817d52f2009-07-13 21:29:25 -0400238 kfree(logical);
239 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400240 return 0;
241}
242
Yan Zheng11833d62009-09-11 16:11:19 -0400243static struct btrfs_caching_control *
244get_caching_control(struct btrfs_block_group_cache *cache)
245{
246 struct btrfs_caching_control *ctl;
247
248 spin_lock(&cache->lock);
249 if (cache->cached != BTRFS_CACHE_STARTED) {
250 spin_unlock(&cache->lock);
251 return NULL;
252 }
253
254 ctl = cache->caching_ctl;
255 atomic_inc(&ctl->count);
256 spin_unlock(&cache->lock);
257 return ctl;
258}
259
260static void put_caching_control(struct btrfs_caching_control *ctl)
261{
262 if (atomic_dec_and_test(&ctl->count))
263 kfree(ctl);
264}
265
Josef Bacik0f9dd462008-09-23 13:14:11 -0400266/*
267 * this is only called by cache_block_group, since we could have freed extents
268 * we need to check the pinned_extents for any extents that can't be used yet
269 * since their free space will be released as soon as the transaction commits.
270 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400271static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400272 struct btrfs_fs_info *info, u64 start, u64 end)
273{
Josef Bacik817d52f2009-07-13 21:29:25 -0400274 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400275 int ret;
276
277 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400278 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400279 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400280 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400281 if (ret)
282 break;
283
Yan, Zheng06b23312009-11-26 09:31:11 +0000284 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400285 start = extent_end + 1;
286 } else if (extent_start > start && extent_start < end) {
287 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400288 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500289 ret = btrfs_add_free_space(block_group, start,
290 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400291 BUG_ON(ret);
292 start = extent_end + 1;
293 } else {
294 break;
295 }
296 }
297
298 if (start < end) {
299 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400300 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500301 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400302 BUG_ON(ret);
303 }
304
Josef Bacik817d52f2009-07-13 21:29:25 -0400305 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400306}
307
Josef Bacik817d52f2009-07-13 21:29:25 -0400308static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400309{
Josef Bacik817d52f2009-07-13 21:29:25 -0400310 struct btrfs_block_group_cache *block_group = data;
311 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400312 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
313 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400314 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400315 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400316 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400317 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400318 u64 last = 0;
319 u32 nritems;
320 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400321
Chris Masone37c9e62007-05-09 20:13:14 -0400322 path = btrfs_alloc_path();
323 if (!path)
324 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400325
Yan Zheng11833d62009-09-11 16:11:19 -0400326 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400327 spin_lock(&block_group->space_info->lock);
328 block_group->space_info->bytes_super += block_group->bytes_super;
329 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400330
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400332
Chris Mason5cd57b22008-06-25 16:01:30 -0400333 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400334 * We don't want to deadlock with somebody trying to allocate a new
335 * extent for the extent root while also trying to search the extent
336 * root to add free space. So we skip locking and search the commit
337 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400338 */
339 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400340 path->search_commit_root = 1;
341 path->reada = 2;
342
Yan Zhenge4404d62008-12-12 10:03:26 -0500343 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400344 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400345 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400346again:
Yan Zheng11833d62009-09-11 16:11:19 -0400347 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400348 /* need to make sure the commit_root doesn't disappear */
349 down_read(&fs_info->extent_commit_sem);
350
Yan Zheng11833d62009-09-11 16:11:19 -0400351 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400352 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400353 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500354
Yan Zheng11833d62009-09-11 16:11:19 -0400355 leaf = path->nodes[0];
356 nritems = btrfs_header_nritems(leaf);
357
Chris Masond3977122009-01-05 21:25:51 -0500358 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400359 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400360 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400361 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400362 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400363 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400364
Yan Zheng11833d62009-09-11 16:11:19 -0400365 if (path->slots[0] < nritems) {
366 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
367 } else {
368 ret = find_next_key(path, 0, &key);
369 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400370 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400371
Yan Zheng11833d62009-09-11 16:11:19 -0400372 caching_ctl->progress = last;
373 btrfs_release_path(extent_root, path);
374 up_read(&fs_info->extent_commit_sem);
375 mutex_unlock(&caching_ctl->mutex);
376 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400377 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400378 else
379 cond_resched();
380 goto again;
381 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400382
Yan Zheng11833d62009-09-11 16:11:19 -0400383 if (key.objectid < block_group->key.objectid) {
384 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400385 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400386 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400387
Chris Masone37c9e62007-05-09 20:13:14 -0400388 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400389 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400390 break;
Yan7d7d6062007-09-14 16:15:28 -0400391
Yan Zheng11833d62009-09-11 16:11:19 -0400392 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400393 total_found += add_new_free_space(block_group,
394 fs_info, last,
395 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400396 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400397
Yan Zheng11833d62009-09-11 16:11:19 -0400398 if (total_found > (1024 * 1024 * 2)) {
399 total_found = 0;
400 wake_up(&caching_ctl->wait);
401 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400402 }
Chris Masone37c9e62007-05-09 20:13:14 -0400403 path->slots[0]++;
404 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400405 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400406
407 total_found += add_new_free_space(block_group, fs_info, last,
408 block_group->key.objectid +
409 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400410 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400411
412 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400413 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400414 block_group->cached = BTRFS_CACHE_FINISHED;
415 spin_unlock(&block_group->lock);
416
Chris Mason54aa1f42007-06-22 14:16:25 -0400417err:
Chris Masone37c9e62007-05-09 20:13:14 -0400418 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400419 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400420
Yan Zheng11833d62009-09-11 16:11:19 -0400421 free_excluded_extents(extent_root, block_group);
422
423 mutex_unlock(&caching_ctl->mutex);
424 wake_up(&caching_ctl->wait);
425
426 put_caching_control(caching_ctl);
427 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000428 btrfs_put_block_group(block_group);
429
Josef Bacik817d52f2009-07-13 21:29:25 -0400430 return 0;
431}
432
433static int cache_block_group(struct btrfs_block_group_cache *cache)
434{
Yan Zheng11833d62009-09-11 16:11:19 -0400435 struct btrfs_fs_info *fs_info = cache->fs_info;
436 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400437 struct task_struct *tsk;
438 int ret = 0;
439
Yan Zheng11833d62009-09-11 16:11:19 -0400440 smp_mb();
441 if (cache->cached != BTRFS_CACHE_NO)
442 return 0;
443
444 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
445 BUG_ON(!caching_ctl);
446
447 INIT_LIST_HEAD(&caching_ctl->list);
448 mutex_init(&caching_ctl->mutex);
449 init_waitqueue_head(&caching_ctl->wait);
450 caching_ctl->block_group = cache;
451 caching_ctl->progress = cache->key.objectid;
452 /* one for caching kthread, one for caching block group list */
453 atomic_set(&caching_ctl->count, 2);
454
Josef Bacik817d52f2009-07-13 21:29:25 -0400455 spin_lock(&cache->lock);
456 if (cache->cached != BTRFS_CACHE_NO) {
457 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400458 kfree(caching_ctl);
459 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400460 }
Yan Zheng11833d62009-09-11 16:11:19 -0400461 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400462 cache->cached = BTRFS_CACHE_STARTED;
463 spin_unlock(&cache->lock);
464
Yan Zheng11833d62009-09-11 16:11:19 -0400465 down_write(&fs_info->extent_commit_sem);
466 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
467 up_write(&fs_info->extent_commit_sem);
468
469 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000470 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400471
Josef Bacik817d52f2009-07-13 21:29:25 -0400472 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
473 cache->key.objectid);
474 if (IS_ERR(tsk)) {
475 ret = PTR_ERR(tsk);
476 printk(KERN_ERR "error running thread %d\n", ret);
477 BUG();
478 }
479
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400480 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400481}
482
Josef Bacik0f9dd462008-09-23 13:14:11 -0400483/*
484 * return the block group that starts at or after bytenr
485 */
Chris Masond3977122009-01-05 21:25:51 -0500486static struct btrfs_block_group_cache *
487btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400488{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400489 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400490
Josef Bacik0f9dd462008-09-23 13:14:11 -0400491 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400492
Josef Bacik0f9dd462008-09-23 13:14:11 -0400493 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400494}
495
Josef Bacik0f9dd462008-09-23 13:14:11 -0400496/*
Sankar P9f556842009-05-14 13:52:22 -0400497 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400498 */
Chris Masond3977122009-01-05 21:25:51 -0500499struct btrfs_block_group_cache *btrfs_lookup_block_group(
500 struct btrfs_fs_info *info,
501 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400502{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400503 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400504
Josef Bacik0f9dd462008-09-23 13:14:11 -0400505 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400506
Josef Bacik0f9dd462008-09-23 13:14:11 -0400507 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400508}
Chris Mason0b86a832008-03-24 15:01:56 -0400509
Josef Bacik0f9dd462008-09-23 13:14:11 -0400510static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
511 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400512{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400513 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400514 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400515
Yan, Zhengb742bb82010-05-16 10:46:24 -0400516 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
517 BTRFS_BLOCK_GROUP_METADATA;
518
Chris Mason4184ea72009-03-10 12:39:20 -0400519 rcu_read_lock();
520 list_for_each_entry_rcu(found, head, list) {
521 if (found->flags == flags) {
522 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400523 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400524 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400525 }
Chris Mason4184ea72009-03-10 12:39:20 -0400526 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400527 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400528}
529
Chris Mason4184ea72009-03-10 12:39:20 -0400530/*
531 * after adding space to the filesystem, we need to clear the full flags
532 * on all the space infos.
533 */
534void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
535{
536 struct list_head *head = &info->space_info;
537 struct btrfs_space_info *found;
538
539 rcu_read_lock();
540 list_for_each_entry_rcu(found, head, list)
541 found->full = 0;
542 rcu_read_unlock();
543}
544
Josef Bacik80eb2342008-10-29 14:49:05 -0400545static u64 div_factor(u64 num, int factor)
546{
547 if (factor == 10)
548 return num;
549 num *= factor;
550 do_div(num, 10);
551 return num;
552}
553
Yan Zhengd2fb3432008-12-11 16:30:39 -0500554u64 btrfs_find_block_group(struct btrfs_root *root,
555 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400556{
Chris Mason96b51792007-10-15 16:15:19 -0400557 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400558 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500559 u64 last = max(search_hint, search_start);
560 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400561 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500562 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400563 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400564again:
Zheng Yane8569812008-09-26 10:05:48 -0400565 while (1) {
566 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400567 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400568 break;
Chris Mason96b51792007-10-15 16:15:19 -0400569
Chris Masonc286ac42008-07-22 23:06:41 -0400570 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400571 last = cache->key.objectid + cache->key.offset;
572 used = btrfs_block_group_used(&cache->item);
573
Yan Zhengd2fb3432008-12-11 16:30:39 -0500574 if ((full_search || !cache->ro) &&
575 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400576 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500577 div_factor(cache->key.offset, factor)) {
578 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400579 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400580 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400581 goto found;
582 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400583 }
Chris Masonc286ac42008-07-22 23:06:41 -0400584 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400585 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400586 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400587 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400588 if (!wrapped) {
589 last = search_start;
590 wrapped = 1;
591 goto again;
592 }
593 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400594 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400595 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400596 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400597 goto again;
598 }
Chris Masonbe744172007-05-06 10:15:01 -0400599found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500600 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400601}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400602
Chris Masone02119d2008-09-05 16:13:11 -0400603/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400604int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400605{
606 int ret;
607 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400608 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400609
Zheng Yan31840ae2008-09-23 13:14:14 -0400610 path = btrfs_alloc_path();
611 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400612 key.objectid = start;
613 key.offset = len;
614 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
615 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
616 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400617 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500618 return ret;
619}
620
Chris Masond8d5f3e2007-12-11 12:42:00 -0500621/*
622 * Back reference rules. Back refs have three main goals:
623 *
624 * 1) differentiate between all holders of references to an extent so that
625 * when a reference is dropped we can make sure it was a valid reference
626 * before freeing the extent.
627 *
628 * 2) Provide enough information to quickly find the holders of an extent
629 * if we notice a given block is corrupted or bad.
630 *
631 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
632 * maintenance. This is actually the same as #2, but with a slightly
633 * different use case.
634 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400635 * There are two kinds of back refs. The implicit back refs is optimized
636 * for pointers in non-shared tree blocks. For a given pointer in a block,
637 * back refs of this kind provide information about the block's owner tree
638 * and the pointer's key. These information allow us to find the block by
639 * b-tree searching. The full back refs is for pointers in tree blocks not
640 * referenced by their owner trees. The location of tree block is recorded
641 * in the back refs. Actually the full back refs is generic, and can be
642 * used in all cases the implicit back refs is used. The major shortcoming
643 * of the full back refs is its overhead. Every time a tree block gets
644 * COWed, we have to update back refs entry for all pointers in it.
645 *
646 * For a newly allocated tree block, we use implicit back refs for
647 * pointers in it. This means most tree related operations only involve
648 * implicit back refs. For a tree block created in old transaction, the
649 * only way to drop a reference to it is COW it. So we can detect the
650 * event that tree block loses its owner tree's reference and do the
651 * back refs conversion.
652 *
653 * When a tree block is COW'd through a tree, there are four cases:
654 *
655 * The reference count of the block is one and the tree is the block's
656 * owner tree. Nothing to do in this case.
657 *
658 * The reference count of the block is one and the tree is not the
659 * block's owner tree. In this case, full back refs is used for pointers
660 * in the block. Remove these full back refs, add implicit back refs for
661 * every pointers in the new block.
662 *
663 * The reference count of the block is greater than one and the tree is
664 * the block's owner tree. In this case, implicit back refs is used for
665 * pointers in the block. Add full back refs for every pointers in the
666 * block, increase lower level extents' reference counts. The original
667 * implicit back refs are entailed to the new block.
668 *
669 * The reference count of the block is greater than one and the tree is
670 * not the block's owner tree. Add implicit back refs for every pointer in
671 * the new block, increase lower level extents' reference count.
672 *
673 * Back Reference Key composing:
674 *
675 * The key objectid corresponds to the first byte in the extent,
676 * The key type is used to differentiate between types of back refs.
677 * There are different meanings of the key offset for different types
678 * of back refs.
679 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500680 * File extents can be referenced by:
681 *
682 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400683 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500684 * - different offsets inside a file (bookend extents in file.c)
685 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400686 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500687 *
688 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500689 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400690 * - original offset in the file
691 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400692 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400693 * The key offset for the implicit back refs is hash of the first
694 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500695 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400696 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500697 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400698 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500699 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400700 * The key offset for the implicit back refs is the first byte of
701 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500702 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400703 * When a file extent is allocated, The implicit back refs is used.
704 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500705 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400706 * (root_key.objectid, inode objectid, offset in file, 1)
707 *
708 * When a file extent is removed file truncation, we find the
709 * corresponding implicit back refs and check the following fields:
710 *
711 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500712 *
713 * Btree extents can be referenced by:
714 *
715 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500716 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400717 * Both the implicit back refs and the full back refs for tree blocks
718 * only consist of key. The key offset for the implicit back refs is
719 * objectid of block's owner tree. The key offset for the full back refs
720 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500721 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400722 * When implicit back refs is used, information about the lowest key and
723 * level of the tree block are required. These information are stored in
724 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500725 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400726
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400727#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
728static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
729 struct btrfs_root *root,
730 struct btrfs_path *path,
731 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500732{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400733 struct btrfs_extent_item *item;
734 struct btrfs_extent_item_v0 *ei0;
735 struct btrfs_extent_ref_v0 *ref0;
736 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400737 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400738 struct btrfs_key key;
739 struct btrfs_key found_key;
740 u32 new_size = sizeof(*item);
741 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500742 int ret;
743
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400744 leaf = path->nodes[0];
745 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500746
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400747 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
748 ei0 = btrfs_item_ptr(leaf, path->slots[0],
749 struct btrfs_extent_item_v0);
750 refs = btrfs_extent_refs_v0(leaf, ei0);
751
752 if (owner == (u64)-1) {
753 while (1) {
754 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
755 ret = btrfs_next_leaf(root, path);
756 if (ret < 0)
757 return ret;
758 BUG_ON(ret > 0);
759 leaf = path->nodes[0];
760 }
761 btrfs_item_key_to_cpu(leaf, &found_key,
762 path->slots[0]);
763 BUG_ON(key.objectid != found_key.objectid);
764 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
765 path->slots[0]++;
766 continue;
767 }
768 ref0 = btrfs_item_ptr(leaf, path->slots[0],
769 struct btrfs_extent_ref_v0);
770 owner = btrfs_ref_objectid_v0(leaf, ref0);
771 break;
772 }
773 }
774 btrfs_release_path(root, path);
775
776 if (owner < BTRFS_FIRST_FREE_OBJECTID)
777 new_size += sizeof(*bi);
778
779 new_size -= sizeof(*ei0);
780 ret = btrfs_search_slot(trans, root, &key, path,
781 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400782 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400783 return ret;
784 BUG_ON(ret);
785
786 ret = btrfs_extend_item(trans, root, path, new_size);
787 BUG_ON(ret);
788
789 leaf = path->nodes[0];
790 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
791 btrfs_set_extent_refs(leaf, item, refs);
792 /* FIXME: get real generation */
793 btrfs_set_extent_generation(leaf, item, 0);
794 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
795 btrfs_set_extent_flags(leaf, item,
796 BTRFS_EXTENT_FLAG_TREE_BLOCK |
797 BTRFS_BLOCK_FLAG_FULL_BACKREF);
798 bi = (struct btrfs_tree_block_info *)(item + 1);
799 /* FIXME: get first key of the block */
800 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
801 btrfs_set_tree_block_level(leaf, bi, (int)owner);
802 } else {
803 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
804 }
805 btrfs_mark_buffer_dirty(leaf);
806 return 0;
807}
808#endif
809
810static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
811{
812 u32 high_crc = ~(u32)0;
813 u32 low_crc = ~(u32)0;
814 __le64 lenum;
815
816 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100817 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400818 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100819 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400820 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100821 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400822
823 return ((u64)high_crc << 31) ^ (u64)low_crc;
824}
825
826static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
827 struct btrfs_extent_data_ref *ref)
828{
829 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
830 btrfs_extent_data_ref_objectid(leaf, ref),
831 btrfs_extent_data_ref_offset(leaf, ref));
832}
833
834static int match_extent_data_ref(struct extent_buffer *leaf,
835 struct btrfs_extent_data_ref *ref,
836 u64 root_objectid, u64 owner, u64 offset)
837{
838 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
839 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
840 btrfs_extent_data_ref_offset(leaf, ref) != offset)
841 return 0;
842 return 1;
843}
844
845static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
846 struct btrfs_root *root,
847 struct btrfs_path *path,
848 u64 bytenr, u64 parent,
849 u64 root_objectid,
850 u64 owner, u64 offset)
851{
852 struct btrfs_key key;
853 struct btrfs_extent_data_ref *ref;
854 struct extent_buffer *leaf;
855 u32 nritems;
856 int ret;
857 int recow;
858 int err = -ENOENT;
859
860 key.objectid = bytenr;
861 if (parent) {
862 key.type = BTRFS_SHARED_DATA_REF_KEY;
863 key.offset = parent;
864 } else {
865 key.type = BTRFS_EXTENT_DATA_REF_KEY;
866 key.offset = hash_extent_data_ref(root_objectid,
867 owner, offset);
868 }
869again:
870 recow = 0;
871 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
872 if (ret < 0) {
873 err = ret;
874 goto fail;
875 }
876
877 if (parent) {
878 if (!ret)
879 return 0;
880#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
881 key.type = BTRFS_EXTENT_REF_V0_KEY;
882 btrfs_release_path(root, path);
883 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
884 if (ret < 0) {
885 err = ret;
886 goto fail;
887 }
888 if (!ret)
889 return 0;
890#endif
891 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400892 }
893
894 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400895 nritems = btrfs_header_nritems(leaf);
896 while (1) {
897 if (path->slots[0] >= nritems) {
898 ret = btrfs_next_leaf(root, path);
899 if (ret < 0)
900 err = ret;
901 if (ret)
902 goto fail;
903
904 leaf = path->nodes[0];
905 nritems = btrfs_header_nritems(leaf);
906 recow = 1;
907 }
908
909 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
910 if (key.objectid != bytenr ||
911 key.type != BTRFS_EXTENT_DATA_REF_KEY)
912 goto fail;
913
914 ref = btrfs_item_ptr(leaf, path->slots[0],
915 struct btrfs_extent_data_ref);
916
917 if (match_extent_data_ref(leaf, ref, root_objectid,
918 owner, offset)) {
919 if (recow) {
920 btrfs_release_path(root, path);
921 goto again;
922 }
923 err = 0;
924 break;
925 }
926 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400927 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400928fail:
929 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400930}
931
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400932static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
933 struct btrfs_root *root,
934 struct btrfs_path *path,
935 u64 bytenr, u64 parent,
936 u64 root_objectid, u64 owner,
937 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400938{
939 struct btrfs_key key;
940 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400941 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400942 u32 num_refs;
943 int ret;
944
945 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946 if (parent) {
947 key.type = BTRFS_SHARED_DATA_REF_KEY;
948 key.offset = parent;
949 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400950 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400951 key.type = BTRFS_EXTENT_DATA_REF_KEY;
952 key.offset = hash_extent_data_ref(root_objectid,
953 owner, offset);
954 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400955 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956
957 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
958 if (ret && ret != -EEXIST)
959 goto fail;
960
961 leaf = path->nodes[0];
962 if (parent) {
963 struct btrfs_shared_data_ref *ref;
964 ref = btrfs_item_ptr(leaf, path->slots[0],
965 struct btrfs_shared_data_ref);
966 if (ret == 0) {
967 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
968 } else {
969 num_refs = btrfs_shared_data_ref_count(leaf, ref);
970 num_refs += refs_to_add;
971 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
972 }
973 } else {
974 struct btrfs_extent_data_ref *ref;
975 while (ret == -EEXIST) {
976 ref = btrfs_item_ptr(leaf, path->slots[0],
977 struct btrfs_extent_data_ref);
978 if (match_extent_data_ref(leaf, ref, root_objectid,
979 owner, offset))
980 break;
981 btrfs_release_path(root, path);
982 key.offset++;
983 ret = btrfs_insert_empty_item(trans, root, path, &key,
984 size);
985 if (ret && ret != -EEXIST)
986 goto fail;
987
988 leaf = path->nodes[0];
989 }
990 ref = btrfs_item_ptr(leaf, path->slots[0],
991 struct btrfs_extent_data_ref);
992 if (ret == 0) {
993 btrfs_set_extent_data_ref_root(leaf, ref,
994 root_objectid);
995 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
996 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
997 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
998 } else {
999 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1000 num_refs += refs_to_add;
1001 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1002 }
1003 }
1004 btrfs_mark_buffer_dirty(leaf);
1005 ret = 0;
1006fail:
Chris Mason7bb86312007-12-11 09:25:06 -05001007 btrfs_release_path(root, path);
1008 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001009}
1010
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001011static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1012 struct btrfs_root *root,
1013 struct btrfs_path *path,
1014 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001015{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001016 struct btrfs_key key;
1017 struct btrfs_extent_data_ref *ref1 = NULL;
1018 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001019 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001020 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001021 int ret = 0;
1022
1023 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001024 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1025
1026 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1027 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1028 struct btrfs_extent_data_ref);
1029 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1030 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1031 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1032 struct btrfs_shared_data_ref);
1033 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1034#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1035 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1036 struct btrfs_extent_ref_v0 *ref0;
1037 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1038 struct btrfs_extent_ref_v0);
1039 num_refs = btrfs_ref_count_v0(leaf, ref0);
1040#endif
1041 } else {
1042 BUG();
1043 }
1044
Chris Mason56bec292009-03-13 10:10:06 -04001045 BUG_ON(num_refs < refs_to_drop);
1046 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001047
Zheng Yan31840ae2008-09-23 13:14:14 -04001048 if (num_refs == 0) {
1049 ret = btrfs_del_item(trans, root, path);
1050 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001051 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1052 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1053 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1054 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1055#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1056 else {
1057 struct btrfs_extent_ref_v0 *ref0;
1058 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1059 struct btrfs_extent_ref_v0);
1060 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1061 }
1062#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001063 btrfs_mark_buffer_dirty(leaf);
1064 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001065 return ret;
1066}
1067
1068static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1069 struct btrfs_path *path,
1070 struct btrfs_extent_inline_ref *iref)
1071{
1072 struct btrfs_key key;
1073 struct extent_buffer *leaf;
1074 struct btrfs_extent_data_ref *ref1;
1075 struct btrfs_shared_data_ref *ref2;
1076 u32 num_refs = 0;
1077
1078 leaf = path->nodes[0];
1079 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1080 if (iref) {
1081 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1082 BTRFS_EXTENT_DATA_REF_KEY) {
1083 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1084 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1085 } else {
1086 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1087 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1088 }
1089 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1090 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1091 struct btrfs_extent_data_ref);
1092 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1093 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1094 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1095 struct btrfs_shared_data_ref);
1096 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1097#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1098 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1099 struct btrfs_extent_ref_v0 *ref0;
1100 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1101 struct btrfs_extent_ref_v0);
1102 num_refs = btrfs_ref_count_v0(leaf, ref0);
1103#endif
1104 } else {
1105 WARN_ON(1);
1106 }
1107 return num_refs;
1108}
1109
1110static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1111 struct btrfs_root *root,
1112 struct btrfs_path *path,
1113 u64 bytenr, u64 parent,
1114 u64 root_objectid)
1115{
1116 struct btrfs_key key;
1117 int ret;
1118
1119 key.objectid = bytenr;
1120 if (parent) {
1121 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1122 key.offset = parent;
1123 } else {
1124 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1125 key.offset = root_objectid;
1126 }
1127
1128 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1129 if (ret > 0)
1130 ret = -ENOENT;
1131#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1132 if (ret == -ENOENT && parent) {
1133 btrfs_release_path(root, path);
1134 key.type = BTRFS_EXTENT_REF_V0_KEY;
1135 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1136 if (ret > 0)
1137 ret = -ENOENT;
1138 }
1139#endif
1140 return ret;
1141}
1142
1143static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1144 struct btrfs_root *root,
1145 struct btrfs_path *path,
1146 u64 bytenr, u64 parent,
1147 u64 root_objectid)
1148{
1149 struct btrfs_key key;
1150 int ret;
1151
1152 key.objectid = bytenr;
1153 if (parent) {
1154 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1155 key.offset = parent;
1156 } else {
1157 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1158 key.offset = root_objectid;
1159 }
1160
1161 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001162 btrfs_release_path(root, path);
1163 return ret;
1164}
1165
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001166static inline int extent_ref_type(u64 parent, u64 owner)
1167{
1168 int type;
1169 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1170 if (parent > 0)
1171 type = BTRFS_SHARED_BLOCK_REF_KEY;
1172 else
1173 type = BTRFS_TREE_BLOCK_REF_KEY;
1174 } else {
1175 if (parent > 0)
1176 type = BTRFS_SHARED_DATA_REF_KEY;
1177 else
1178 type = BTRFS_EXTENT_DATA_REF_KEY;
1179 }
1180 return type;
1181}
1182
Yan Zheng2c47e6052009-06-27 21:07:35 -04001183static int find_next_key(struct btrfs_path *path, int level,
1184 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001185
1186{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001187 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001188 if (!path->nodes[level])
1189 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001190 if (path->slots[level] + 1 >=
1191 btrfs_header_nritems(path->nodes[level]))
1192 continue;
1193 if (level == 0)
1194 btrfs_item_key_to_cpu(path->nodes[level], key,
1195 path->slots[level] + 1);
1196 else
1197 btrfs_node_key_to_cpu(path->nodes[level], key,
1198 path->slots[level] + 1);
1199 return 0;
1200 }
1201 return 1;
1202}
1203
1204/*
1205 * look for inline back ref. if back ref is found, *ref_ret is set
1206 * to the address of inline back ref, and 0 is returned.
1207 *
1208 * if back ref isn't found, *ref_ret is set to the address where it
1209 * should be inserted, and -ENOENT is returned.
1210 *
1211 * if insert is true and there are too many inline back refs, the path
1212 * points to the extent item, and -EAGAIN is returned.
1213 *
1214 * NOTE: inline back refs are ordered in the same way that back ref
1215 * items in the tree are ordered.
1216 */
1217static noinline_for_stack
1218int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1219 struct btrfs_root *root,
1220 struct btrfs_path *path,
1221 struct btrfs_extent_inline_ref **ref_ret,
1222 u64 bytenr, u64 num_bytes,
1223 u64 parent, u64 root_objectid,
1224 u64 owner, u64 offset, int insert)
1225{
1226 struct btrfs_key key;
1227 struct extent_buffer *leaf;
1228 struct btrfs_extent_item *ei;
1229 struct btrfs_extent_inline_ref *iref;
1230 u64 flags;
1231 u64 item_size;
1232 unsigned long ptr;
1233 unsigned long end;
1234 int extra_size;
1235 int type;
1236 int want;
1237 int ret;
1238 int err = 0;
1239
1240 key.objectid = bytenr;
1241 key.type = BTRFS_EXTENT_ITEM_KEY;
1242 key.offset = num_bytes;
1243
1244 want = extent_ref_type(parent, owner);
1245 if (insert) {
1246 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001247 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001248 } else
1249 extra_size = -1;
1250 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1251 if (ret < 0) {
1252 err = ret;
1253 goto out;
1254 }
1255 BUG_ON(ret);
1256
1257 leaf = path->nodes[0];
1258 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1259#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1260 if (item_size < sizeof(*ei)) {
1261 if (!insert) {
1262 err = -ENOENT;
1263 goto out;
1264 }
1265 ret = convert_extent_item_v0(trans, root, path, owner,
1266 extra_size);
1267 if (ret < 0) {
1268 err = ret;
1269 goto out;
1270 }
1271 leaf = path->nodes[0];
1272 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1273 }
1274#endif
1275 BUG_ON(item_size < sizeof(*ei));
1276
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001277 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1278 flags = btrfs_extent_flags(leaf, ei);
1279
1280 ptr = (unsigned long)(ei + 1);
1281 end = (unsigned long)ei + item_size;
1282
1283 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1284 ptr += sizeof(struct btrfs_tree_block_info);
1285 BUG_ON(ptr > end);
1286 } else {
1287 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1288 }
1289
1290 err = -ENOENT;
1291 while (1) {
1292 if (ptr >= end) {
1293 WARN_ON(ptr > end);
1294 break;
1295 }
1296 iref = (struct btrfs_extent_inline_ref *)ptr;
1297 type = btrfs_extent_inline_ref_type(leaf, iref);
1298 if (want < type)
1299 break;
1300 if (want > type) {
1301 ptr += btrfs_extent_inline_ref_size(type);
1302 continue;
1303 }
1304
1305 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1306 struct btrfs_extent_data_ref *dref;
1307 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1308 if (match_extent_data_ref(leaf, dref, root_objectid,
1309 owner, offset)) {
1310 err = 0;
1311 break;
1312 }
1313 if (hash_extent_data_ref_item(leaf, dref) <
1314 hash_extent_data_ref(root_objectid, owner, offset))
1315 break;
1316 } else {
1317 u64 ref_offset;
1318 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1319 if (parent > 0) {
1320 if (parent == ref_offset) {
1321 err = 0;
1322 break;
1323 }
1324 if (ref_offset < parent)
1325 break;
1326 } else {
1327 if (root_objectid == ref_offset) {
1328 err = 0;
1329 break;
1330 }
1331 if (ref_offset < root_objectid)
1332 break;
1333 }
1334 }
1335 ptr += btrfs_extent_inline_ref_size(type);
1336 }
1337 if (err == -ENOENT && insert) {
1338 if (item_size + extra_size >=
1339 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1340 err = -EAGAIN;
1341 goto out;
1342 }
1343 /*
1344 * To add new inline back ref, we have to make sure
1345 * there is no corresponding back ref item.
1346 * For simplicity, we just do not add new inline back
1347 * ref if there is any kind of item for this block
1348 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001349 if (find_next_key(path, 0, &key) == 0 &&
1350 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001351 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001352 err = -EAGAIN;
1353 goto out;
1354 }
1355 }
1356 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1357out:
Yan Zheng85d41982009-06-11 08:51:10 -04001358 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001359 path->keep_locks = 0;
1360 btrfs_unlock_up_safe(path, 1);
1361 }
1362 return err;
1363}
1364
1365/*
1366 * helper to add new inline back ref
1367 */
1368static noinline_for_stack
1369int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1370 struct btrfs_root *root,
1371 struct btrfs_path *path,
1372 struct btrfs_extent_inline_ref *iref,
1373 u64 parent, u64 root_objectid,
1374 u64 owner, u64 offset, int refs_to_add,
1375 struct btrfs_delayed_extent_op *extent_op)
1376{
1377 struct extent_buffer *leaf;
1378 struct btrfs_extent_item *ei;
1379 unsigned long ptr;
1380 unsigned long end;
1381 unsigned long item_offset;
1382 u64 refs;
1383 int size;
1384 int type;
1385 int ret;
1386
1387 leaf = path->nodes[0];
1388 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1389 item_offset = (unsigned long)iref - (unsigned long)ei;
1390
1391 type = extent_ref_type(parent, owner);
1392 size = btrfs_extent_inline_ref_size(type);
1393
1394 ret = btrfs_extend_item(trans, root, path, size);
1395 BUG_ON(ret);
1396
1397 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1398 refs = btrfs_extent_refs(leaf, ei);
1399 refs += refs_to_add;
1400 btrfs_set_extent_refs(leaf, ei, refs);
1401 if (extent_op)
1402 __run_delayed_extent_op(extent_op, leaf, ei);
1403
1404 ptr = (unsigned long)ei + item_offset;
1405 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1406 if (ptr < end - size)
1407 memmove_extent_buffer(leaf, ptr + size, ptr,
1408 end - size - ptr);
1409
1410 iref = (struct btrfs_extent_inline_ref *)ptr;
1411 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1412 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1413 struct btrfs_extent_data_ref *dref;
1414 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1415 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1416 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1417 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1418 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1419 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1420 struct btrfs_shared_data_ref *sref;
1421 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1422 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1423 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1424 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1425 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1426 } else {
1427 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1428 }
1429 btrfs_mark_buffer_dirty(leaf);
1430 return 0;
1431}
1432
1433static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1434 struct btrfs_root *root,
1435 struct btrfs_path *path,
1436 struct btrfs_extent_inline_ref **ref_ret,
1437 u64 bytenr, u64 num_bytes, u64 parent,
1438 u64 root_objectid, u64 owner, u64 offset)
1439{
1440 int ret;
1441
1442 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1443 bytenr, num_bytes, parent,
1444 root_objectid, owner, offset, 0);
1445 if (ret != -ENOENT)
1446 return ret;
1447
1448 btrfs_release_path(root, path);
1449 *ref_ret = NULL;
1450
1451 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1452 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1453 root_objectid);
1454 } else {
1455 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1456 root_objectid, owner, offset);
1457 }
1458 return ret;
1459}
1460
1461/*
1462 * helper to update/remove inline back ref
1463 */
1464static noinline_for_stack
1465int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1466 struct btrfs_root *root,
1467 struct btrfs_path *path,
1468 struct btrfs_extent_inline_ref *iref,
1469 int refs_to_mod,
1470 struct btrfs_delayed_extent_op *extent_op)
1471{
1472 struct extent_buffer *leaf;
1473 struct btrfs_extent_item *ei;
1474 struct btrfs_extent_data_ref *dref = NULL;
1475 struct btrfs_shared_data_ref *sref = NULL;
1476 unsigned long ptr;
1477 unsigned long end;
1478 u32 item_size;
1479 int size;
1480 int type;
1481 int ret;
1482 u64 refs;
1483
1484 leaf = path->nodes[0];
1485 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1486 refs = btrfs_extent_refs(leaf, ei);
1487 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1488 refs += refs_to_mod;
1489 btrfs_set_extent_refs(leaf, ei, refs);
1490 if (extent_op)
1491 __run_delayed_extent_op(extent_op, leaf, ei);
1492
1493 type = btrfs_extent_inline_ref_type(leaf, iref);
1494
1495 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1496 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1497 refs = btrfs_extent_data_ref_count(leaf, dref);
1498 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1499 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1500 refs = btrfs_shared_data_ref_count(leaf, sref);
1501 } else {
1502 refs = 1;
1503 BUG_ON(refs_to_mod != -1);
1504 }
1505
1506 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1507 refs += refs_to_mod;
1508
1509 if (refs > 0) {
1510 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1511 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1512 else
1513 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1514 } else {
1515 size = btrfs_extent_inline_ref_size(type);
1516 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1517 ptr = (unsigned long)iref;
1518 end = (unsigned long)ei + item_size;
1519 if (ptr + size < end)
1520 memmove_extent_buffer(leaf, ptr, ptr + size,
1521 end - ptr - size);
1522 item_size -= size;
1523 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1524 BUG_ON(ret);
1525 }
1526 btrfs_mark_buffer_dirty(leaf);
1527 return 0;
1528}
1529
1530static noinline_for_stack
1531int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1532 struct btrfs_root *root,
1533 struct btrfs_path *path,
1534 u64 bytenr, u64 num_bytes, u64 parent,
1535 u64 root_objectid, u64 owner,
1536 u64 offset, int refs_to_add,
1537 struct btrfs_delayed_extent_op *extent_op)
1538{
1539 struct btrfs_extent_inline_ref *iref;
1540 int ret;
1541
1542 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1543 bytenr, num_bytes, parent,
1544 root_objectid, owner, offset, 1);
1545 if (ret == 0) {
1546 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1547 ret = update_inline_extent_backref(trans, root, path, iref,
1548 refs_to_add, extent_op);
1549 } else if (ret == -ENOENT) {
1550 ret = setup_inline_extent_backref(trans, root, path, iref,
1551 parent, root_objectid,
1552 owner, offset, refs_to_add,
1553 extent_op);
1554 }
1555 return ret;
1556}
1557
1558static int insert_extent_backref(struct btrfs_trans_handle *trans,
1559 struct btrfs_root *root,
1560 struct btrfs_path *path,
1561 u64 bytenr, u64 parent, u64 root_objectid,
1562 u64 owner, u64 offset, int refs_to_add)
1563{
1564 int ret;
1565 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1566 BUG_ON(refs_to_add != 1);
1567 ret = insert_tree_block_ref(trans, root, path, bytenr,
1568 parent, root_objectid);
1569 } else {
1570 ret = insert_extent_data_ref(trans, root, path, bytenr,
1571 parent, root_objectid,
1572 owner, offset, refs_to_add);
1573 }
1574 return ret;
1575}
1576
1577static int remove_extent_backref(struct btrfs_trans_handle *trans,
1578 struct btrfs_root *root,
1579 struct btrfs_path *path,
1580 struct btrfs_extent_inline_ref *iref,
1581 int refs_to_drop, int is_data)
1582{
1583 int ret;
1584
1585 BUG_ON(!is_data && refs_to_drop != 1);
1586 if (iref) {
1587 ret = update_inline_extent_backref(trans, root, path, iref,
1588 -refs_to_drop, NULL);
1589 } else if (is_data) {
1590 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1591 } else {
1592 ret = btrfs_del_item(trans, root, path);
1593 }
1594 return ret;
1595}
1596
Chris Mason15916de2008-11-19 21:17:22 -05001597static void btrfs_issue_discard(struct block_device *bdev,
1598 u64 start, u64 len)
1599{
Christoph Hellwig746cd1e2009-09-12 07:35:43 +02001600 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
1601 DISCARD_FL_BARRIER);
Chris Mason15916de2008-11-19 21:17:22 -05001602}
Chris Mason15916de2008-11-19 21:17:22 -05001603
Liu Hui1f3c79a2009-01-05 15:57:51 -05001604static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1605 u64 num_bytes)
1606{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001607 int ret;
1608 u64 map_length = num_bytes;
1609 struct btrfs_multi_bio *multi = NULL;
1610
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001611 if (!btrfs_test_opt(root, DISCARD))
1612 return 0;
1613
Liu Hui1f3c79a2009-01-05 15:57:51 -05001614 /* Tell the block device(s) that the sectors can be discarded */
1615 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1616 bytenr, &map_length, &multi, 0);
1617 if (!ret) {
1618 struct btrfs_bio_stripe *stripe = multi->stripes;
1619 int i;
1620
1621 if (map_length > num_bytes)
1622 map_length = num_bytes;
1623
1624 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1625 btrfs_issue_discard(stripe->dev->bdev,
1626 stripe->physical,
1627 map_length);
1628 }
1629 kfree(multi);
1630 }
1631
1632 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001633}
1634
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001635int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1636 struct btrfs_root *root,
1637 u64 bytenr, u64 num_bytes, u64 parent,
1638 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001639{
1640 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001641 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1642 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001643
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001644 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1645 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1646 parent, root_objectid, (int)owner,
1647 BTRFS_ADD_DELAYED_REF, NULL);
1648 } else {
1649 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1650 parent, root_objectid, owner, offset,
1651 BTRFS_ADD_DELAYED_REF, NULL);
1652 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001653 return ret;
1654}
1655
Chris Mason925baed2008-06-25 16:01:30 -04001656static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001657 struct btrfs_root *root,
1658 u64 bytenr, u64 num_bytes,
1659 u64 parent, u64 root_objectid,
1660 u64 owner, u64 offset, int refs_to_add,
1661 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001662{
Chris Mason5caf2a02007-04-02 11:20:42 -04001663 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001664 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001665 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001666 u64 refs;
1667 int ret;
1668 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001669
Chris Mason5caf2a02007-04-02 11:20:42 -04001670 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001671 if (!path)
1672 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001673
Chris Mason3c12ac72008-04-21 12:01:38 -04001674 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001675 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001676 /* this will setup the path even if it fails to insert the back ref */
1677 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1678 path, bytenr, num_bytes, parent,
1679 root_objectid, owner, offset,
1680 refs_to_add, extent_op);
1681 if (ret == 0)
1682 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001683
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001684 if (ret != -EAGAIN) {
1685 err = ret;
1686 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001687 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001688
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001689 leaf = path->nodes[0];
1690 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1691 refs = btrfs_extent_refs(leaf, item);
1692 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1693 if (extent_op)
1694 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001695
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001696 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001697 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001698
Chris Mason3c12ac72008-04-21 12:01:38 -04001699 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001700 path->leave_spinning = 1;
1701
Chris Mason56bec292009-03-13 10:10:06 -04001702 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001703 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001704 path, bytenr, parent, root_objectid,
1705 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001706 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001707out:
Chris Mason74493f72007-12-11 09:25:06 -05001708 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001709 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001710}
1711
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001712static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1713 struct btrfs_root *root,
1714 struct btrfs_delayed_ref_node *node,
1715 struct btrfs_delayed_extent_op *extent_op,
1716 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001717{
Chris Mason56bec292009-03-13 10:10:06 -04001718 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001719 struct btrfs_delayed_data_ref *ref;
1720 struct btrfs_key ins;
1721 u64 parent = 0;
1722 u64 ref_root = 0;
1723 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001724
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001725 ins.objectid = node->bytenr;
1726 ins.offset = node->num_bytes;
1727 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001728
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001729 ref = btrfs_delayed_node_to_data_ref(node);
1730 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1731 parent = ref->parent;
1732 else
1733 ref_root = ref->root;
1734
1735 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1736 if (extent_op) {
1737 BUG_ON(extent_op->update_key);
1738 flags |= extent_op->flags_to_set;
1739 }
1740 ret = alloc_reserved_file_extent(trans, root,
1741 parent, ref_root, flags,
1742 ref->objectid, ref->offset,
1743 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001744 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1745 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1746 node->num_bytes, parent,
1747 ref_root, ref->objectid,
1748 ref->offset, node->ref_mod,
1749 extent_op);
1750 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1751 ret = __btrfs_free_extent(trans, root, node->bytenr,
1752 node->num_bytes, parent,
1753 ref_root, ref->objectid,
1754 ref->offset, node->ref_mod,
1755 extent_op);
1756 } else {
1757 BUG();
1758 }
Chris Mason56bec292009-03-13 10:10:06 -04001759 return ret;
1760}
1761
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001762static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1763 struct extent_buffer *leaf,
1764 struct btrfs_extent_item *ei)
1765{
1766 u64 flags = btrfs_extent_flags(leaf, ei);
1767 if (extent_op->update_flags) {
1768 flags |= extent_op->flags_to_set;
1769 btrfs_set_extent_flags(leaf, ei, flags);
1770 }
1771
1772 if (extent_op->update_key) {
1773 struct btrfs_tree_block_info *bi;
1774 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1775 bi = (struct btrfs_tree_block_info *)(ei + 1);
1776 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1777 }
1778}
1779
1780static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1781 struct btrfs_root *root,
1782 struct btrfs_delayed_ref_node *node,
1783 struct btrfs_delayed_extent_op *extent_op)
1784{
1785 struct btrfs_key key;
1786 struct btrfs_path *path;
1787 struct btrfs_extent_item *ei;
1788 struct extent_buffer *leaf;
1789 u32 item_size;
1790 int ret;
1791 int err = 0;
1792
1793 path = btrfs_alloc_path();
1794 if (!path)
1795 return -ENOMEM;
1796
1797 key.objectid = node->bytenr;
1798 key.type = BTRFS_EXTENT_ITEM_KEY;
1799 key.offset = node->num_bytes;
1800
1801 path->reada = 1;
1802 path->leave_spinning = 1;
1803 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1804 path, 0, 1);
1805 if (ret < 0) {
1806 err = ret;
1807 goto out;
1808 }
1809 if (ret > 0) {
1810 err = -EIO;
1811 goto out;
1812 }
1813
1814 leaf = path->nodes[0];
1815 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1816#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1817 if (item_size < sizeof(*ei)) {
1818 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1819 path, (u64)-1, 0);
1820 if (ret < 0) {
1821 err = ret;
1822 goto out;
1823 }
1824 leaf = path->nodes[0];
1825 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1826 }
1827#endif
1828 BUG_ON(item_size < sizeof(*ei));
1829 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1830 __run_delayed_extent_op(extent_op, leaf, ei);
1831
1832 btrfs_mark_buffer_dirty(leaf);
1833out:
1834 btrfs_free_path(path);
1835 return err;
1836}
1837
1838static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1839 struct btrfs_root *root,
1840 struct btrfs_delayed_ref_node *node,
1841 struct btrfs_delayed_extent_op *extent_op,
1842 int insert_reserved)
1843{
1844 int ret = 0;
1845 struct btrfs_delayed_tree_ref *ref;
1846 struct btrfs_key ins;
1847 u64 parent = 0;
1848 u64 ref_root = 0;
1849
1850 ins.objectid = node->bytenr;
1851 ins.offset = node->num_bytes;
1852 ins.type = BTRFS_EXTENT_ITEM_KEY;
1853
1854 ref = btrfs_delayed_node_to_tree_ref(node);
1855 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1856 parent = ref->parent;
1857 else
1858 ref_root = ref->root;
1859
1860 BUG_ON(node->ref_mod != 1);
1861 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1862 BUG_ON(!extent_op || !extent_op->update_flags ||
1863 !extent_op->update_key);
1864 ret = alloc_reserved_tree_block(trans, root,
1865 parent, ref_root,
1866 extent_op->flags_to_set,
1867 &extent_op->key,
1868 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001869 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1870 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1871 node->num_bytes, parent, ref_root,
1872 ref->level, 0, 1, extent_op);
1873 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1874 ret = __btrfs_free_extent(trans, root, node->bytenr,
1875 node->num_bytes, parent, ref_root,
1876 ref->level, 0, 1, extent_op);
1877 } else {
1878 BUG();
1879 }
1880 return ret;
1881}
1882
1883
Chris Mason56bec292009-03-13 10:10:06 -04001884/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001885static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *root,
1887 struct btrfs_delayed_ref_node *node,
1888 struct btrfs_delayed_extent_op *extent_op,
1889 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001890{
Josef Bacikeb099672009-02-12 09:27:38 -05001891 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001892 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001893 struct btrfs_delayed_ref_head *head;
1894 /*
1895 * we've hit the end of the chain and we were supposed
1896 * to insert this extent into the tree. But, it got
1897 * deleted before we ever needed to insert it, so all
1898 * we have to do is clean up the accounting
1899 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001900 BUG_ON(extent_op);
1901 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001902 if (insert_reserved) {
Yan Zheng11833d62009-09-11 16:11:19 -04001903 int mark_free = 0;
1904 struct extent_buffer *must_clean = NULL;
1905
1906 ret = pin_down_bytes(trans, root, NULL,
1907 node->bytenr, node->num_bytes,
1908 head->is_data, 1, &must_clean);
1909 if (ret > 0)
1910 mark_free = 1;
1911
1912 if (must_clean) {
1913 clean_tree_block(NULL, root, must_clean);
1914 btrfs_tree_unlock(must_clean);
1915 free_extent_buffer(must_clean);
1916 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001917 if (head->is_data) {
1918 ret = btrfs_del_csums(trans, root,
1919 node->bytenr,
1920 node->num_bytes);
1921 BUG_ON(ret);
1922 }
Yan Zheng11833d62009-09-11 16:11:19 -04001923 if (mark_free) {
1924 ret = btrfs_free_reserved_extent(root,
1925 node->bytenr,
1926 node->num_bytes);
1927 BUG_ON(ret);
1928 }
Chris Mason56bec292009-03-13 10:10:06 -04001929 }
Chris Mason56bec292009-03-13 10:10:06 -04001930 mutex_unlock(&head->mutex);
1931 return 0;
1932 }
Josef Bacikeb099672009-02-12 09:27:38 -05001933
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001934 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1935 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1936 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1937 insert_reserved);
1938 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1939 node->type == BTRFS_SHARED_DATA_REF_KEY)
1940 ret = run_delayed_data_ref(trans, root, node, extent_op,
1941 insert_reserved);
1942 else
1943 BUG();
1944 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001945}
1946
Chris Mason56bec292009-03-13 10:10:06 -04001947static noinline struct btrfs_delayed_ref_node *
1948select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001949{
Chris Mason56bec292009-03-13 10:10:06 -04001950 struct rb_node *node;
1951 struct btrfs_delayed_ref_node *ref;
1952 int action = BTRFS_ADD_DELAYED_REF;
1953again:
1954 /*
1955 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1956 * this prevents ref count from going down to zero when
1957 * there still are pending delayed ref.
1958 */
1959 node = rb_prev(&head->node.rb_node);
1960 while (1) {
1961 if (!node)
1962 break;
1963 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1964 rb_node);
1965 if (ref->bytenr != head->node.bytenr)
1966 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001967 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001968 return ref;
1969 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001970 }
Chris Mason56bec292009-03-13 10:10:06 -04001971 if (action == BTRFS_ADD_DELAYED_REF) {
1972 action = BTRFS_DROP_DELAYED_REF;
1973 goto again;
1974 }
1975 return NULL;
1976}
1977
Chris Masonc3e69d52009-03-13 10:17:05 -04001978static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1979 struct btrfs_root *root,
1980 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001981{
Chris Mason56bec292009-03-13 10:10:06 -04001982 struct btrfs_delayed_ref_root *delayed_refs;
1983 struct btrfs_delayed_ref_node *ref;
1984 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001985 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001986 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001987 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001988 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001989
1990 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001991 while (1) {
1992 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001993 /* pick a new head ref from the cluster list */
1994 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001995 break;
Chris Mason56bec292009-03-13 10:10:06 -04001996
Chris Masonc3e69d52009-03-13 10:17:05 -04001997 locked_ref = list_entry(cluster->next,
1998 struct btrfs_delayed_ref_head, cluster);
1999
2000 /* grab the lock that says we are going to process
2001 * all the refs for this head */
2002 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2003
2004 /*
2005 * we may have dropped the spin lock to get the head
2006 * mutex lock, and that might have given someone else
2007 * time to free the head. If that's true, it has been
2008 * removed from our list and we can move on.
2009 */
2010 if (ret == -EAGAIN) {
2011 locked_ref = NULL;
2012 count++;
2013 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002014 }
2015 }
2016
2017 /*
2018 * record the must insert reserved flag before we
2019 * drop the spin lock.
2020 */
2021 must_insert_reserved = locked_ref->must_insert_reserved;
2022 locked_ref->must_insert_reserved = 0;
2023
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002024 extent_op = locked_ref->extent_op;
2025 locked_ref->extent_op = NULL;
2026
Chris Mason56bec292009-03-13 10:10:06 -04002027 /*
2028 * locked_ref is the head node, so we have to go one
2029 * node back for any delayed ref updates
2030 */
Chris Mason56bec292009-03-13 10:10:06 -04002031 ref = select_delayed_ref(locked_ref);
2032 if (!ref) {
2033 /* All delayed refs have been processed, Go ahead
2034 * and send the head node to run_one_delayed_ref,
2035 * so that any accounting fixes can happen
2036 */
2037 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002038
2039 if (extent_op && must_insert_reserved) {
2040 kfree(extent_op);
2041 extent_op = NULL;
2042 }
2043
2044 if (extent_op) {
2045 spin_unlock(&delayed_refs->lock);
2046
2047 ret = run_delayed_extent_op(trans, root,
2048 ref, extent_op);
2049 BUG_ON(ret);
2050 kfree(extent_op);
2051
2052 cond_resched();
2053 spin_lock(&delayed_refs->lock);
2054 continue;
2055 }
2056
Chris Masonc3e69d52009-03-13 10:17:05 -04002057 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002058 locked_ref = NULL;
2059 }
2060
2061 ref->in_tree = 0;
2062 rb_erase(&ref->rb_node, &delayed_refs->root);
2063 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064
Chris Mason56bec292009-03-13 10:10:06 -04002065 spin_unlock(&delayed_refs->lock);
2066
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002067 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002068 must_insert_reserved);
2069 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002070
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002071 btrfs_put_delayed_ref(ref);
2072 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002073 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002074
Chris Mason1887be62009-03-13 10:11:24 -04002075 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002076 spin_lock(&delayed_refs->lock);
2077 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002078 return count;
2079}
2080
2081/*
2082 * this starts processing the delayed reference count updates and
2083 * extent insertions we have queued up so far. count can be
2084 * 0, which means to process everything in the tree at the start
2085 * of the run (but not newly added entries), or it can be some target
2086 * number you'd like to process.
2087 */
2088int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2089 struct btrfs_root *root, unsigned long count)
2090{
2091 struct rb_node *node;
2092 struct btrfs_delayed_ref_root *delayed_refs;
2093 struct btrfs_delayed_ref_node *ref;
2094 struct list_head cluster;
2095 int ret;
2096 int run_all = count == (unsigned long)-1;
2097 int run_most = 0;
2098
2099 if (root == root->fs_info->extent_root)
2100 root = root->fs_info->tree_root;
2101
2102 delayed_refs = &trans->transaction->delayed_refs;
2103 INIT_LIST_HEAD(&cluster);
2104again:
2105 spin_lock(&delayed_refs->lock);
2106 if (count == 0) {
2107 count = delayed_refs->num_entries * 2;
2108 run_most = 1;
2109 }
2110 while (1) {
2111 if (!(run_all || run_most) &&
2112 delayed_refs->num_heads_ready < 64)
2113 break;
2114
2115 /*
2116 * go find something we can process in the rbtree. We start at
2117 * the beginning of the tree, and then build a cluster
2118 * of refs to process starting at the first one we are able to
2119 * lock
2120 */
2121 ret = btrfs_find_ref_cluster(trans, &cluster,
2122 delayed_refs->run_delayed_start);
2123 if (ret)
2124 break;
2125
2126 ret = run_clustered_refs(trans, root, &cluster);
2127 BUG_ON(ret < 0);
2128
2129 count -= min_t(unsigned long, ret, count);
2130
2131 if (count == 0)
2132 break;
2133 }
2134
Chris Mason56bec292009-03-13 10:10:06 -04002135 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002136 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002137 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002138 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002139 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002140
2141 while (node) {
2142 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2143 rb_node);
2144 if (btrfs_delayed_ref_is_head(ref)) {
2145 struct btrfs_delayed_ref_head *head;
2146
2147 head = btrfs_delayed_node_to_head(ref);
2148 atomic_inc(&ref->refs);
2149
2150 spin_unlock(&delayed_refs->lock);
2151 mutex_lock(&head->mutex);
2152 mutex_unlock(&head->mutex);
2153
2154 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002155 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002156 goto again;
2157 }
2158 node = rb_next(node);
2159 }
2160 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002161 schedule_timeout(1);
2162 goto again;
2163 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002164out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002165 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002166 return 0;
2167}
2168
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002169int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2170 struct btrfs_root *root,
2171 u64 bytenr, u64 num_bytes, u64 flags,
2172 int is_data)
2173{
2174 struct btrfs_delayed_extent_op *extent_op;
2175 int ret;
2176
2177 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2178 if (!extent_op)
2179 return -ENOMEM;
2180
2181 extent_op->flags_to_set = flags;
2182 extent_op->update_flags = 1;
2183 extent_op->update_key = 0;
2184 extent_op->is_data = is_data ? 1 : 0;
2185
2186 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2187 if (ret)
2188 kfree(extent_op);
2189 return ret;
2190}
2191
2192static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2193 struct btrfs_root *root,
2194 struct btrfs_path *path,
2195 u64 objectid, u64 offset, u64 bytenr)
2196{
2197 struct btrfs_delayed_ref_head *head;
2198 struct btrfs_delayed_ref_node *ref;
2199 struct btrfs_delayed_data_ref *data_ref;
2200 struct btrfs_delayed_ref_root *delayed_refs;
2201 struct rb_node *node;
2202 int ret = 0;
2203
2204 ret = -ENOENT;
2205 delayed_refs = &trans->transaction->delayed_refs;
2206 spin_lock(&delayed_refs->lock);
2207 head = btrfs_find_delayed_ref_head(trans, bytenr);
2208 if (!head)
2209 goto out;
2210
2211 if (!mutex_trylock(&head->mutex)) {
2212 atomic_inc(&head->node.refs);
2213 spin_unlock(&delayed_refs->lock);
2214
2215 btrfs_release_path(root->fs_info->extent_root, path);
2216
2217 mutex_lock(&head->mutex);
2218 mutex_unlock(&head->mutex);
2219 btrfs_put_delayed_ref(&head->node);
2220 return -EAGAIN;
2221 }
2222
2223 node = rb_prev(&head->node.rb_node);
2224 if (!node)
2225 goto out_unlock;
2226
2227 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2228
2229 if (ref->bytenr != bytenr)
2230 goto out_unlock;
2231
2232 ret = 1;
2233 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2234 goto out_unlock;
2235
2236 data_ref = btrfs_delayed_node_to_data_ref(ref);
2237
2238 node = rb_prev(node);
2239 if (node) {
2240 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2241 if (ref->bytenr == bytenr)
2242 goto out_unlock;
2243 }
2244
2245 if (data_ref->root != root->root_key.objectid ||
2246 data_ref->objectid != objectid || data_ref->offset != offset)
2247 goto out_unlock;
2248
2249 ret = 0;
2250out_unlock:
2251 mutex_unlock(&head->mutex);
2252out:
2253 spin_unlock(&delayed_refs->lock);
2254 return ret;
2255}
2256
2257static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2258 struct btrfs_root *root,
2259 struct btrfs_path *path,
2260 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002261{
2262 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002263 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002264 struct btrfs_extent_data_ref *ref;
2265 struct btrfs_extent_inline_ref *iref;
2266 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002267 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002268 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002269 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002270
Chris Masonbe20aa92007-12-17 20:14:01 -05002271 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002272 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002273 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002274
Chris Masonbe20aa92007-12-17 20:14:01 -05002275 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2276 if (ret < 0)
2277 goto out;
2278 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002279
2280 ret = -ENOENT;
2281 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002282 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002283
Zheng Yan31840ae2008-09-23 13:14:14 -04002284 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002285 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002286 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002287
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002289 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002290
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002291 ret = 1;
2292 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2293#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2294 if (item_size < sizeof(*ei)) {
2295 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2296 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002297 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002298#endif
2299 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2300
2301 if (item_size != sizeof(*ei) +
2302 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2303 goto out;
2304
2305 if (btrfs_extent_generation(leaf, ei) <=
2306 btrfs_root_last_snapshot(&root->root_item))
2307 goto out;
2308
2309 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2310 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2311 BTRFS_EXTENT_DATA_REF_KEY)
2312 goto out;
2313
2314 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2315 if (btrfs_extent_refs(leaf, ei) !=
2316 btrfs_extent_data_ref_count(leaf, ref) ||
2317 btrfs_extent_data_ref_root(leaf, ref) !=
2318 root->root_key.objectid ||
2319 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2320 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2321 goto out;
2322
Yan Zhengf321e492008-07-30 09:26:11 -04002323 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002324out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002325 return ret;
2326}
2327
2328int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2329 struct btrfs_root *root,
2330 u64 objectid, u64 offset, u64 bytenr)
2331{
2332 struct btrfs_path *path;
2333 int ret;
2334 int ret2;
2335
2336 path = btrfs_alloc_path();
2337 if (!path)
2338 return -ENOENT;
2339
2340 do {
2341 ret = check_committed_ref(trans, root, path, objectid,
2342 offset, bytenr);
2343 if (ret && ret != -ENOENT)
2344 goto out;
2345
2346 ret2 = check_delayed_ref(trans, root, path, objectid,
2347 offset, bytenr);
2348 } while (ret2 == -EAGAIN);
2349
2350 if (ret2 && ret2 != -ENOENT) {
2351 ret = ret2;
2352 goto out;
2353 }
2354
2355 if (ret != -ENOENT || ret2 != -ENOENT)
2356 ret = 0;
2357out:
Yan Zhengf321e492008-07-30 09:26:11 -04002358 btrfs_free_path(path);
2359 return ret;
2360}
2361
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002362#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002363int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2364 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002365{
Chris Mason5f39d392007-10-15 16:14:19 -04002366 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002367 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002368 u64 root_gen;
2369 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002370 int i;
Chris Masondb945352007-10-15 16:15:53 -04002371 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002372 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002373 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002374
Chris Mason3768f362007-03-13 16:47:54 -04002375 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002376 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002377
Zheng Yane4657682008-09-26 10:04:53 -04002378 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2379 shared = 0;
2380 root_gen = root->root_key.offset;
2381 } else {
2382 shared = 1;
2383 root_gen = trans->transid - 1;
2384 }
2385
Chris Masondb945352007-10-15 16:15:53 -04002386 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002387 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002388
Zheng Yan31840ae2008-09-23 13:14:14 -04002389 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002390 struct btrfs_leaf_ref *ref;
2391 struct btrfs_extent_info *info;
2392
Zheng Yan31840ae2008-09-23 13:14:14 -04002393 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002394 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002395 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002396 goto out;
2397 }
2398
Zheng Yane4657682008-09-26 10:04:53 -04002399 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002400 ref->bytenr = buf->start;
2401 ref->owner = btrfs_header_owner(buf);
2402 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002403 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002404 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002405
Zheng Yan31840ae2008-09-23 13:14:14 -04002406 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002407 u64 disk_bytenr;
2408 btrfs_item_key_to_cpu(buf, &key, i);
2409 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2410 continue;
2411 fi = btrfs_item_ptr(buf, i,
2412 struct btrfs_file_extent_item);
2413 if (btrfs_file_extent_type(buf, fi) ==
2414 BTRFS_FILE_EXTENT_INLINE)
2415 continue;
2416 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2417 if (disk_bytenr == 0)
2418 continue;
2419
2420 info->bytenr = disk_bytenr;
2421 info->num_bytes =
2422 btrfs_file_extent_disk_num_bytes(buf, fi);
2423 info->objectid = key.objectid;
2424 info->offset = key.offset;
2425 info++;
2426 }
2427
Zheng Yane4657682008-09-26 10:04:53 -04002428 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002429 if (ret == -EEXIST && shared) {
2430 struct btrfs_leaf_ref *old;
2431 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2432 BUG_ON(!old);
2433 btrfs_remove_leaf_ref(root, old);
2434 btrfs_free_leaf_ref(root, old);
2435 ret = btrfs_add_leaf_ref(root, ref, shared);
2436 }
Yan Zheng31153d82008-07-28 15:32:19 -04002437 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002438 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002439 }
2440out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002441 return ret;
2442}
2443
Chris Masonb7a9f292009-02-04 09:23:45 -05002444/* when a block goes through cow, we update the reference counts of
2445 * everything that block points to. The internal pointers of the block
2446 * can be in just about any order, and it is likely to have clusters of
2447 * things that are close together and clusters of things that are not.
2448 *
2449 * To help reduce the seeks that come with updating all of these reference
2450 * counts, sort them by byte number before actual updates are done.
2451 *
2452 * struct refsort is used to match byte number to slot in the btree block.
2453 * we sort based on the byte number and then use the slot to actually
2454 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002455 *
2456 * struct refsort is smaller than strcut btrfs_item and smaller than
2457 * struct btrfs_key_ptr. Since we're currently limited to the page size
2458 * for a btree block, there's no way for a kmalloc of refsorts for a
2459 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002460 */
2461struct refsort {
2462 u64 bytenr;
2463 u32 slot;
2464};
2465
2466/*
2467 * for passing into sort()
2468 */
2469static int refsort_cmp(const void *a_void, const void *b_void)
2470{
2471 const struct refsort *a = a_void;
2472 const struct refsort *b = b_void;
2473
2474 if (a->bytenr < b->bytenr)
2475 return -1;
2476 if (a->bytenr > b->bytenr)
2477 return 1;
2478 return 0;
2479}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002480#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002481
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002482static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002483 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002484 struct extent_buffer *buf,
2485 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002486{
2487 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002488 u64 num_bytes;
2489 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002490 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002491 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002492 struct btrfs_key key;
2493 struct btrfs_file_extent_item *fi;
2494 int i;
2495 int level;
2496 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002497 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002498 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002499
2500 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002501 nritems = btrfs_header_nritems(buf);
2502 level = btrfs_header_level(buf);
2503
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002504 if (!root->ref_cows && level == 0)
2505 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002506
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002507 if (inc)
2508 process_func = btrfs_inc_extent_ref;
2509 else
2510 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002511
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002512 if (full_backref)
2513 parent = buf->start;
2514 else
2515 parent = 0;
2516
Zheng Yan31840ae2008-09-23 13:14:14 -04002517 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002518 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002519 btrfs_item_key_to_cpu(buf, &key, i);
2520 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002521 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002522 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002523 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002524 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002525 BTRFS_FILE_EXTENT_INLINE)
2526 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002527 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2528 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002529 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002530
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002531 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2532 key.offset -= btrfs_file_extent_offset(buf, fi);
2533 ret = process_func(trans, root, bytenr, num_bytes,
2534 parent, ref_root, key.objectid,
2535 key.offset);
2536 if (ret)
2537 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002538 } else {
2539 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002540 num_bytes = btrfs_level_size(root, level - 1);
2541 ret = process_func(trans, root, bytenr, num_bytes,
2542 parent, ref_root, level - 1, 0);
2543 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002544 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002545 }
2546 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002547 return 0;
2548fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002549 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002550 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002551}
2552
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002553int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2554 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002555{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002556 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2557}
Zheng Yan31840ae2008-09-23 13:14:14 -04002558
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002559int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2560 struct extent_buffer *buf, int full_backref)
2561{
2562 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002563}
2564
Chris Mason9078a3e2007-04-26 16:46:15 -04002565static int write_one_cache_group(struct btrfs_trans_handle *trans,
2566 struct btrfs_root *root,
2567 struct btrfs_path *path,
2568 struct btrfs_block_group_cache *cache)
2569{
2570 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002571 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002572 unsigned long bi;
2573 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002574
Chris Mason9078a3e2007-04-26 16:46:15 -04002575 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002576 if (ret < 0)
2577 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002578 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002579
2580 leaf = path->nodes[0];
2581 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2582 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2583 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002584 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002585fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002586 if (ret)
2587 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002588 return 0;
2589
2590}
2591
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002592static struct btrfs_block_group_cache *
2593next_block_group(struct btrfs_root *root,
2594 struct btrfs_block_group_cache *cache)
2595{
2596 struct rb_node *node;
2597 spin_lock(&root->fs_info->block_group_cache_lock);
2598 node = rb_next(&cache->cache_node);
2599 btrfs_put_block_group(cache);
2600 if (node) {
2601 cache = rb_entry(node, struct btrfs_block_group_cache,
2602 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002603 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002604 } else
2605 cache = NULL;
2606 spin_unlock(&root->fs_info->block_group_cache_lock);
2607 return cache;
2608}
2609
Chris Mason96b51792007-10-15 16:15:19 -04002610int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2611 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002612{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002613 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002614 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002615 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002616 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002617
2618 path = btrfs_alloc_path();
2619 if (!path)
2620 return -ENOMEM;
2621
Chris Masond3977122009-01-05 21:25:51 -05002622 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002623 if (last == 0) {
2624 err = btrfs_run_delayed_refs(trans, root,
2625 (unsigned long)-1);
2626 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002627 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002628
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002629 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2630 while (cache) {
2631 if (cache->dirty)
2632 break;
2633 cache = next_block_group(root, cache);
2634 }
2635 if (!cache) {
2636 if (last == 0)
2637 break;
2638 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002639 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002640 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002641
2642 cache->dirty = 0;
2643 last = cache->key.objectid + cache->key.offset;
2644
2645 err = write_one_cache_group(trans, root, path, cache);
2646 BUG_ON(err);
2647 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002648 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002649
Chris Mason9078a3e2007-04-26 16:46:15 -04002650 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002651 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002652}
2653
Yan Zhengd2fb3432008-12-11 16:30:39 -05002654int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2655{
2656 struct btrfs_block_group_cache *block_group;
2657 int readonly = 0;
2658
2659 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2660 if (!block_group || block_group->ro)
2661 readonly = 1;
2662 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002663 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002664 return readonly;
2665}
2666
Chris Mason593060d2008-03-25 16:50:33 -04002667static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2668 u64 total_bytes, u64 bytes_used,
2669 struct btrfs_space_info **space_info)
2670{
2671 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002672 int i;
2673 int factor;
2674
2675 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2676 BTRFS_BLOCK_GROUP_RAID10))
2677 factor = 2;
2678 else
2679 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002680
2681 found = __find_space_info(info, flags);
2682 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002683 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002684 found->total_bytes += total_bytes;
2685 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002686 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002687 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002688 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002689 *space_info = found;
2690 return 0;
2691 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002692 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002693 if (!found)
2694 return -ENOMEM;
2695
Yan, Zhengb742bb82010-05-16 10:46:24 -04002696 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2697 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04002698 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002699 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04002700 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2701 BTRFS_BLOCK_GROUP_SYSTEM |
2702 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04002703 found->total_bytes = total_bytes;
2704 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002705 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002706 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002707 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002708 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002709 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002710 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002711 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002712 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002713 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002714 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002715 return 0;
2716}
2717
Chris Mason8790d502008-04-03 16:29:03 -04002718static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2719{
2720 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002721 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002722 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002723 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002724 if (extra_flags) {
2725 if (flags & BTRFS_BLOCK_GROUP_DATA)
2726 fs_info->avail_data_alloc_bits |= extra_flags;
2727 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2728 fs_info->avail_metadata_alloc_bits |= extra_flags;
2729 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2730 fs_info->avail_system_alloc_bits |= extra_flags;
2731 }
2732}
Chris Mason593060d2008-03-25 16:50:33 -04002733
Yan Zhengc146afa2008-11-12 14:34:12 -05002734static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2735{
2736 spin_lock(&cache->space_info->lock);
2737 spin_lock(&cache->lock);
2738 if (!cache->ro) {
2739 cache->space_info->bytes_readonly += cache->key.offset -
2740 btrfs_block_group_used(&cache->item);
2741 cache->ro = 1;
2742 }
2743 spin_unlock(&cache->lock);
2744 spin_unlock(&cache->space_info->lock);
2745}
2746
Yan Zheng2b820322008-11-17 21:11:30 -05002747u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002748{
Yan Zheng2b820322008-11-17 21:11:30 -05002749 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002750
2751 if (num_devices == 1)
2752 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2753 if (num_devices < 4)
2754 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2755
Chris Masonec44a352008-04-28 15:29:52 -04002756 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2757 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002758 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002759 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002760 }
Chris Masonec44a352008-04-28 15:29:52 -04002761
2762 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002763 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002764 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002765 }
Chris Masonec44a352008-04-28 15:29:52 -04002766
2767 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2768 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2769 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2770 (flags & BTRFS_BLOCK_GROUP_DUP)))
2771 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2772 return flags;
2773}
2774
Yan, Zhengb742bb82010-05-16 10:46:24 -04002775static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05002776{
Yan, Zhengb742bb82010-05-16 10:46:24 -04002777 if (flags & BTRFS_BLOCK_GROUP_DATA)
2778 flags |= root->fs_info->avail_data_alloc_bits &
2779 root->fs_info->data_alloc_profile;
2780 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2781 flags |= root->fs_info->avail_system_alloc_bits &
2782 root->fs_info->system_alloc_profile;
2783 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
2784 flags |= root->fs_info->avail_metadata_alloc_bits &
2785 root->fs_info->metadata_alloc_profile;
2786 return btrfs_reduce_alloc_profile(root, flags);
2787}
Josef Bacik6a632092009-02-20 11:00:09 -05002788
Yan, Zhengb742bb82010-05-16 10:46:24 -04002789static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
2790{
2791 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05002792
Yan, Zhengb742bb82010-05-16 10:46:24 -04002793 if (data)
2794 flags = BTRFS_BLOCK_GROUP_DATA;
2795 else if (root == root->fs_info->chunk_root)
2796 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2797 else
2798 flags = BTRFS_BLOCK_GROUP_METADATA;
2799
2800 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05002801}
2802
2803void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2804{
2805 u64 alloc_target;
2806
2807 alloc_target = btrfs_get_alloc_profile(root, 1);
2808 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2809 alloc_target);
2810}
2811
Josef Bacik9ed74f22009-09-11 16:12:44 -04002812static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2813{
2814 u64 num_bytes;
2815 int level;
2816
2817 level = BTRFS_MAX_LEVEL - 2;
2818 /*
2819 * NOTE: these calculations are absolutely the worst possible case.
2820 * This assumes that _every_ item we insert will require a new leaf, and
2821 * that the tree has grown to its maximum level size.
2822 */
2823
2824 /*
2825 * for every item we insert we could insert both an extent item and a
2826 * extent ref item. Then for ever item we insert, we will need to cow
2827 * both the original leaf, plus the leaf to the left and right of it.
2828 *
2829 * Unless we are talking about the extent root, then we just want the
2830 * number of items * 2, since we just need the extent item plus its ref.
2831 */
2832 if (root == root->fs_info->extent_root)
2833 num_bytes = num_items * 2;
2834 else
2835 num_bytes = (num_items + (2 * num_items)) * 3;
2836
2837 /*
2838 * num_bytes is total number of leaves we could need times the leaf
2839 * size, and then for every leaf we could end up cow'ing 2 nodes per
2840 * level, down to the leaf level.
2841 */
2842 num_bytes = (num_bytes * root->leafsize) +
2843 (num_bytes * (level * 2)) * root->nodesize;
2844
2845 return num_bytes;
2846}
2847
Josef Bacik6a632092009-02-20 11:00:09 -05002848/*
Josef Bacik9ed74f22009-09-11 16:12:44 -04002849 * Unreserve metadata space for delalloc. If we have less reserved credits than
2850 * we have extents, this function does nothing.
Josef Bacik6a632092009-02-20 11:00:09 -05002851 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04002852int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2853 struct inode *inode, int num_items)
Josef Bacik6a632092009-02-20 11:00:09 -05002854{
2855 struct btrfs_fs_info *info = root->fs_info;
2856 struct btrfs_space_info *meta_sinfo;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002857 u64 num_bytes;
2858 u64 alloc_target;
2859 bool bug = false;
Josef Bacik6a632092009-02-20 11:00:09 -05002860
2861 /* get the space info for where the metadata will live */
2862 alloc_target = btrfs_get_alloc_profile(root, 0);
2863 meta_sinfo = __find_space_info(info, alloc_target);
2864
Josef Bacik9ed74f22009-09-11 16:12:44 -04002865 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2866 num_items);
2867
2868 spin_lock(&meta_sinfo->lock);
Josef Bacik32c00af2009-10-08 13:34:05 -04002869 spin_lock(&BTRFS_I(inode)->accounting_lock);
2870 if (BTRFS_I(inode)->reserved_extents <=
2871 BTRFS_I(inode)->outstanding_extents) {
2872 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002873 spin_unlock(&meta_sinfo->lock);
2874 return 0;
2875 }
Josef Bacik32c00af2009-10-08 13:34:05 -04002876 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002877
Josef Bacik287a0ab2010-03-19 18:07:23 +00002878 BTRFS_I(inode)->reserved_extents -= num_items;
Josef Bacik32c00af2009-10-08 13:34:05 -04002879 BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002880
2881 if (meta_sinfo->bytes_delalloc < num_bytes) {
2882 bug = true;
2883 meta_sinfo->bytes_delalloc = 0;
2884 } else {
2885 meta_sinfo->bytes_delalloc -= num_bytes;
2886 }
2887 spin_unlock(&meta_sinfo->lock);
2888
2889 BUG_ON(bug);
2890
2891 return 0;
2892}
2893
2894static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2895{
2896 u64 thresh;
2897
2898 thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2899 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2900 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2901 meta_sinfo->bytes_may_use;
2902
2903 thresh = meta_sinfo->total_bytes - thresh;
2904 thresh *= 80;
2905 do_div(thresh, 100);
2906 if (thresh <= meta_sinfo->bytes_delalloc)
2907 meta_sinfo->force_delalloc = 1;
2908 else
2909 meta_sinfo->force_delalloc = 0;
2910}
2911
Josef Bacik9ed74f22009-09-11 16:12:44 -04002912/*
2913 * Reserve metadata space for delalloc.
2914 */
2915int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
2916 struct inode *inode, int num_items)
2917{
2918 struct btrfs_fs_info *info = root->fs_info;
2919 struct btrfs_space_info *meta_sinfo;
2920 u64 num_bytes;
2921 u64 used;
2922 u64 alloc_target;
2923 int flushed = 0;
2924 int force_delalloc;
2925
2926 /* get the space info for where the metadata will live */
2927 alloc_target = btrfs_get_alloc_profile(root, 0);
2928 meta_sinfo = __find_space_info(info, alloc_target);
2929
2930 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2931 num_items);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002932again:
Josef Bacik6a632092009-02-20 11:00:09 -05002933 spin_lock(&meta_sinfo->lock);
Josef Bacik6a632092009-02-20 11:00:09 -05002934
Josef Bacik9ed74f22009-09-11 16:12:44 -04002935 force_delalloc = meta_sinfo->force_delalloc;
Josef Bacik6a632092009-02-20 11:00:09 -05002936
Josef Bacik9ed74f22009-09-11 16:12:44 -04002937 if (unlikely(!meta_sinfo->bytes_root))
2938 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
2939
2940 if (!flushed)
2941 meta_sinfo->bytes_delalloc += num_bytes;
2942
2943 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2944 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2945 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2946 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
2947
2948 if (used > meta_sinfo->total_bytes) {
2949 flushed++;
2950
2951 if (flushed == 1) {
Yan, Zheng424499d2010-05-16 10:46:25 -04002952 if (maybe_allocate_chunk(NULL, root, meta_sinfo,
2953 num_bytes))
Josef Bacik9ed74f22009-09-11 16:12:44 -04002954 goto again;
2955 flushed++;
2956 } else {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002957 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002958 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002959
Josef Bacik9ed74f22009-09-11 16:12:44 -04002960 if (flushed == 2) {
2961 filemap_flush(inode->i_mapping);
2962 goto again;
2963 } else if (flushed == 3) {
Yan, Zheng5da9d012010-05-16 10:46:25 -04002964 shrink_delalloc(NULL, root, meta_sinfo, num_bytes);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002965 goto again;
2966 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04002967 spin_lock(&meta_sinfo->lock);
2968 meta_sinfo->bytes_delalloc -= num_bytes;
Josef Bacik6a632092009-02-20 11:00:09 -05002969 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002970 printk(KERN_ERR "enospc, has %d, reserved %d\n",
Josef Bacik32c00af2009-10-08 13:34:05 -04002971 BTRFS_I(inode)->outstanding_extents,
2972 BTRFS_I(inode)->reserved_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002973 dump_space_info(meta_sinfo, 0, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05002974 return -ENOSPC;
2975 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04002976
Josef Bacik287a0ab2010-03-19 18:07:23 +00002977 BTRFS_I(inode)->reserved_extents += num_items;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002978 check_force_delalloc(meta_sinfo);
2979 spin_unlock(&meta_sinfo->lock);
2980
2981 if (!flushed && force_delalloc)
2982 filemap_flush(inode->i_mapping);
2983
2984 return 0;
2985}
2986
2987/*
2988 * unreserve num_items number of items worth of metadata space. This needs to
2989 * be paired with btrfs_reserve_metadata_space.
2990 *
2991 * NOTE: if you have the option, run this _AFTER_ you do a
2992 * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
2993 * oprations which will result in more used metadata, so we want to make sure we
2994 * can do that without issue.
2995 */
2996int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
2997{
2998 struct btrfs_fs_info *info = root->fs_info;
2999 struct btrfs_space_info *meta_sinfo;
3000 u64 num_bytes;
3001 u64 alloc_target;
3002 bool bug = false;
3003
3004 /* get the space info for where the metadata will live */
3005 alloc_target = btrfs_get_alloc_profile(root, 0);
3006 meta_sinfo = __find_space_info(info, alloc_target);
3007
3008 num_bytes = calculate_bytes_needed(root, num_items);
3009
3010 spin_lock(&meta_sinfo->lock);
3011 if (meta_sinfo->bytes_may_use < num_bytes) {
3012 bug = true;
3013 meta_sinfo->bytes_may_use = 0;
3014 } else {
3015 meta_sinfo->bytes_may_use -= num_bytes;
3016 }
3017 spin_unlock(&meta_sinfo->lock);
3018
3019 BUG_ON(bug);
3020
3021 return 0;
3022}
3023
3024/*
3025 * Reserve some metadata space for use. We'll calculate the worste case number
3026 * of bytes that would be needed to modify num_items number of items. If we
3027 * have space, fantastic, if not, you get -ENOSPC. Please call
3028 * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
3029 * items you reserved, since whatever metadata you needed should have already
3030 * been allocated.
3031 *
3032 * This will commit the transaction to make more space if we don't have enough
3033 * metadata space. THe only time we don't do this is if we're reserving space
3034 * inside of a transaction, then we will just return -ENOSPC and it is the
3035 * callers responsibility to handle it properly.
3036 */
3037int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3038{
3039 struct btrfs_fs_info *info = root->fs_info;
3040 struct btrfs_space_info *meta_sinfo;
3041 u64 num_bytes;
3042 u64 used;
3043 u64 alloc_target;
3044 int retries = 0;
3045
3046 /* get the space info for where the metadata will live */
3047 alloc_target = btrfs_get_alloc_profile(root, 0);
3048 meta_sinfo = __find_space_info(info, alloc_target);
3049
3050 num_bytes = calculate_bytes_needed(root, num_items);
3051again:
3052 spin_lock(&meta_sinfo->lock);
3053
3054 if (unlikely(!meta_sinfo->bytes_root))
3055 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3056
3057 if (!retries)
3058 meta_sinfo->bytes_may_use += num_bytes;
3059
3060 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3061 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3062 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3063 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3064
3065 if (used > meta_sinfo->total_bytes) {
3066 retries++;
3067 if (retries == 1) {
Yan, Zheng424499d2010-05-16 10:46:25 -04003068 if (maybe_allocate_chunk(NULL, root, meta_sinfo,
3069 num_bytes))
Josef Bacik9ed74f22009-09-11 16:12:44 -04003070 goto again;
3071 retries++;
3072 } else {
3073 spin_unlock(&meta_sinfo->lock);
3074 }
3075
3076 if (retries == 2) {
Yan, Zheng5da9d012010-05-16 10:46:25 -04003077 shrink_delalloc(NULL, root, meta_sinfo, num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003078 goto again;
3079 }
3080 spin_lock(&meta_sinfo->lock);
3081 meta_sinfo->bytes_may_use -= num_bytes;
3082 spin_unlock(&meta_sinfo->lock);
3083
3084 dump_space_info(meta_sinfo, 0, 0);
3085 return -ENOSPC;
3086 }
3087
3088 check_force_delalloc(meta_sinfo);
Josef Bacik6a632092009-02-20 11:00:09 -05003089 spin_unlock(&meta_sinfo->lock);
3090
3091 return 0;
3092}
3093
3094/*
3095 * This will check the space that the inode allocates from to make sure we have
3096 * enough space for bytes.
3097 */
3098int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3099 u64 bytes)
3100{
3101 struct btrfs_space_info *data_sinfo;
Josef Bacikab6e2412010-03-19 14:38:13 +00003102 u64 used;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003103 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003104
3105 /* make sure bytes are sectorsize aligned */
3106 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3107
3108 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003109 if (!data_sinfo)
3110 goto alloc;
3111
Josef Bacik6a632092009-02-20 11:00:09 -05003112again:
3113 /* make sure we have enough space to handle the data first */
3114 spin_lock(&data_sinfo->lock);
Josef Bacikab6e2412010-03-19 14:38:13 +00003115 used = data_sinfo->bytes_used + data_sinfo->bytes_delalloc +
3116 data_sinfo->bytes_reserved + data_sinfo->bytes_pinned +
3117 data_sinfo->bytes_readonly + data_sinfo->bytes_may_use +
3118 data_sinfo->bytes_super;
3119
3120 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003121 struct btrfs_trans_handle *trans;
3122
Josef Bacik6a632092009-02-20 11:00:09 -05003123 /*
3124 * if we don't have enough free bytes in this space then we need
3125 * to alloc a new chunk.
3126 */
3127 if (!data_sinfo->full) {
3128 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003129
3130 data_sinfo->force_alloc = 1;
3131 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003132alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003133 alloc_target = btrfs_get_alloc_profile(root, 1);
3134 trans = btrfs_start_transaction(root, 1);
3135 if (!trans)
3136 return -ENOMEM;
3137
3138 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3139 bytes + 2 * 1024 * 1024,
3140 alloc_target, 0);
3141 btrfs_end_transaction(trans, root);
3142 if (ret)
3143 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04003144
3145 if (!data_sinfo) {
3146 btrfs_set_inode_space_info(root, inode);
3147 data_sinfo = BTRFS_I(inode)->space_info;
3148 }
Josef Bacik6a632092009-02-20 11:00:09 -05003149 goto again;
3150 }
3151 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003152
3153 /* commit the current transaction and try again */
Sage Weildd7e0b72009-09-29 18:38:44 -04003154 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003155 committed = 1;
3156 trans = btrfs_join_transaction(root, 1);
3157 if (!trans)
3158 return -ENOMEM;
3159 ret = btrfs_commit_transaction(trans, root);
3160 if (ret)
3161 return ret;
3162 goto again;
3163 }
3164
Josef Bacik6a632092009-02-20 11:00:09 -05003165 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3166 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04003167 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07003168 "%llu total\n", (unsigned long long)bytes,
3169 (unsigned long long)data_sinfo->bytes_delalloc,
3170 (unsigned long long)data_sinfo->bytes_used,
3171 (unsigned long long)data_sinfo->bytes_reserved,
3172 (unsigned long long)data_sinfo->bytes_pinned,
3173 (unsigned long long)data_sinfo->bytes_readonly,
3174 (unsigned long long)data_sinfo->bytes_may_use,
3175 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003176 return -ENOSPC;
3177 }
3178 data_sinfo->bytes_may_use += bytes;
3179 BTRFS_I(inode)->reserved_bytes += bytes;
3180 spin_unlock(&data_sinfo->lock);
3181
Josef Bacik9ed74f22009-09-11 16:12:44 -04003182 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003183}
3184
3185/*
3186 * if there was an error for whatever reason after calling
3187 * btrfs_check_data_free_space, call this so we can cleanup the counters.
3188 */
3189void btrfs_free_reserved_data_space(struct btrfs_root *root,
3190 struct inode *inode, u64 bytes)
3191{
3192 struct btrfs_space_info *data_sinfo;
3193
3194 /* make sure bytes are sectorsize aligned */
3195 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3196
3197 data_sinfo = BTRFS_I(inode)->space_info;
3198 spin_lock(&data_sinfo->lock);
3199 data_sinfo->bytes_may_use -= bytes;
3200 BTRFS_I(inode)->reserved_bytes -= bytes;
3201 spin_unlock(&data_sinfo->lock);
3202}
3203
3204/* called when we are adding a delalloc extent to the inode's io_tree */
3205void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3206 u64 bytes)
3207{
3208 struct btrfs_space_info *data_sinfo;
3209
3210 /* get the space info for where this inode will be storing its data */
3211 data_sinfo = BTRFS_I(inode)->space_info;
3212
3213 /* make sure we have enough space to handle the data first */
3214 spin_lock(&data_sinfo->lock);
3215 data_sinfo->bytes_delalloc += bytes;
3216
3217 /*
3218 * we are adding a delalloc extent without calling
3219 * btrfs_check_data_free_space first. This happens on a weird
3220 * writepage condition, but shouldn't hurt our accounting
3221 */
3222 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3223 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3224 BTRFS_I(inode)->reserved_bytes = 0;
3225 } else {
3226 data_sinfo->bytes_may_use -= bytes;
3227 BTRFS_I(inode)->reserved_bytes -= bytes;
3228 }
3229
3230 spin_unlock(&data_sinfo->lock);
3231}
3232
3233/* called when we are clearing an delalloc extent from the inode's io_tree */
3234void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3235 u64 bytes)
3236{
3237 struct btrfs_space_info *info;
3238
3239 info = BTRFS_I(inode)->space_info;
3240
3241 spin_lock(&info->lock);
3242 info->bytes_delalloc -= bytes;
3243 spin_unlock(&info->lock);
3244}
3245
Josef Bacik97e728d2009-04-21 17:40:57 -04003246static void force_metadata_allocation(struct btrfs_fs_info *info)
3247{
3248 struct list_head *head = &info->space_info;
3249 struct btrfs_space_info *found;
3250
3251 rcu_read_lock();
3252 list_for_each_entry_rcu(found, head, list) {
3253 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3254 found->force_alloc = 1;
3255 }
3256 rcu_read_unlock();
3257}
3258
Yan, Zheng424499d2010-05-16 10:46:25 -04003259static int should_alloc_chunk(struct btrfs_space_info *sinfo,
3260 u64 alloc_bytes)
3261{
3262 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3263
3264 if (sinfo->bytes_used + sinfo->bytes_reserved +
3265 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3266 return 0;
3267
3268 if (sinfo->bytes_used + sinfo->bytes_reserved +
3269 alloc_bytes < div_factor(num_bytes, 8))
3270 return 0;
3271
3272 return 1;
3273}
3274
Chris Mason6324fbf2008-03-24 15:01:59 -04003275static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3276 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003277 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003278{
3279 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003280 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Yan Zhengc146afa2008-11-12 14:34:12 -05003281 int ret = 0;
3282
Josef Bacik97e728d2009-04-21 17:40:57 -04003283 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003284
Yan Zheng2b820322008-11-17 21:11:30 -05003285 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003286
Chris Mason6324fbf2008-03-24 15:01:59 -04003287 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003288 if (!space_info) {
3289 ret = update_space_info(extent_root->fs_info, flags,
3290 0, 0, &space_info);
3291 BUG_ON(ret);
3292 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003293 BUG_ON(!space_info);
3294
Josef Bacik25179202008-10-29 14:49:05 -04003295 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003296 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003297 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003298 if (space_info->full) {
3299 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003300 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003301 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003302
Yan, Zheng424499d2010-05-16 10:46:25 -04003303 if (!force && !should_alloc_chunk(space_info, alloc_bytes)) {
Josef Bacik25179202008-10-29 14:49:05 -04003304 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003305 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003306 }
Josef Bacik25179202008-10-29 14:49:05 -04003307 spin_unlock(&space_info->lock);
3308
Josef Bacik97e728d2009-04-21 17:40:57 -04003309 /*
3310 * if we're doing a data chunk, go ahead and make sure that
3311 * we keep a reasonable number of metadata chunks allocated in the
3312 * FS as well.
3313 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003314 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003315 fs_info->data_chunk_allocations++;
3316 if (!(fs_info->data_chunk_allocations %
3317 fs_info->metadata_ratio))
3318 force_metadata_allocation(fs_info);
3319 }
3320
Yan Zheng2b820322008-11-17 21:11:30 -05003321 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003322 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003323 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003324 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003325 else
3326 ret = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003327 space_info->force_alloc = 0;
3328 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003329out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003330 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003331 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003332}
3333
Yan, Zheng424499d2010-05-16 10:46:25 -04003334static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
3335 struct btrfs_root *root,
3336 struct btrfs_space_info *sinfo, u64 num_bytes)
3337{
3338 int ret;
3339 int end_trans = 0;
3340
3341 if (sinfo->full)
3342 return 0;
3343
3344 spin_lock(&sinfo->lock);
3345 ret = should_alloc_chunk(sinfo, num_bytes + 2 * 1024 * 1024);
3346 spin_unlock(&sinfo->lock);
3347 if (!ret)
3348 return 0;
3349
3350 if (!trans) {
3351 trans = btrfs_join_transaction(root, 1);
3352 BUG_ON(IS_ERR(trans));
3353 end_trans = 1;
3354 }
3355
3356 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3357 num_bytes + 2 * 1024 * 1024,
3358 get_alloc_profile(root, sinfo->flags), 0);
3359
3360 if (end_trans)
3361 btrfs_end_transaction(trans, root);
3362
3363 return ret == 1 ? 1 : 0;
3364}
3365
Yan, Zheng5da9d012010-05-16 10:46:25 -04003366/*
3367 * shrink metadata reservation for delalloc
3368 */
3369static int shrink_delalloc(struct btrfs_trans_handle *trans,
3370 struct btrfs_root *root,
3371 struct btrfs_space_info *sinfo, u64 to_reclaim)
3372{
3373 u64 reserved;
3374 u64 max_reclaim;
3375 u64 reclaimed = 0;
3376 int pause = 1;
3377 int ret;
3378
3379 spin_lock(&sinfo->lock);
3380 reserved = sinfo->bytes_delalloc;
3381 spin_unlock(&sinfo->lock);
3382
3383 if (reserved == 0)
3384 return 0;
3385
3386 max_reclaim = min(reserved, to_reclaim);
3387
3388 while (1) {
3389 ret = btrfs_start_one_delalloc_inode(root, trans ? 1 : 0);
3390 if (!ret) {
3391 __set_current_state(TASK_INTERRUPTIBLE);
3392 schedule_timeout(pause);
3393 pause <<= 1;
3394 if (pause > HZ / 10)
3395 pause = HZ / 10;
3396 } else {
3397 pause = 1;
3398 }
3399
3400 spin_lock(&sinfo->lock);
3401 if (reserved > sinfo->bytes_delalloc)
3402 reclaimed = reserved - sinfo->bytes_delalloc;
3403 reserved = sinfo->bytes_delalloc;
3404 spin_unlock(&sinfo->lock);
3405
3406 if (reserved == 0 || reclaimed >= max_reclaim)
3407 break;
3408
3409 if (trans && trans->transaction->blocked)
3410 return -EAGAIN;
3411 }
3412 return reclaimed >= to_reclaim;
3413}
3414
Chris Mason9078a3e2007-04-26 16:46:15 -04003415static int update_block_group(struct btrfs_trans_handle *trans,
3416 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04003417 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04003418 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04003419{
3420 struct btrfs_block_group_cache *cache;
3421 struct btrfs_fs_info *info = root->fs_info;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003422 int factor;
Chris Masondb945352007-10-15 16:15:53 -04003423 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003424 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003425 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04003426
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003427 /* block accounting for super block */
3428 spin_lock(&info->delalloc_lock);
3429 old_val = btrfs_super_bytes_used(&info->super_copy);
3430 if (alloc)
3431 old_val += num_bytes;
3432 else
3433 old_val -= num_bytes;
3434 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003435 spin_unlock(&info->delalloc_lock);
3436
Chris Masond3977122009-01-05 21:25:51 -05003437 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003438 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003439 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04003440 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003441 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
3442 BTRFS_BLOCK_GROUP_RAID1 |
3443 BTRFS_BLOCK_GROUP_RAID10))
3444 factor = 2;
3445 else
3446 factor = 1;
Chris Masondb945352007-10-15 16:15:53 -04003447 byte_in_group = bytenr - cache->key.objectid;
3448 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003449
Josef Bacik25179202008-10-29 14:49:05 -04003450 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003451 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003452 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04003453 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04003454 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04003455 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04003456 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003457 btrfs_set_block_group_used(&cache->item, old_val);
3458 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003459 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003460 cache->space_info->bytes_used += num_bytes;
3461 cache->space_info->disk_used += num_bytes * factor;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003462 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05003463 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003464 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003465 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003466 } else {
Chris Masondb945352007-10-15 16:15:53 -04003467 old_val -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003468 btrfs_set_block_group_used(&cache->item, old_val);
Chris Mason6324fbf2008-03-24 15:01:59 -04003469 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003470 cache->space_info->disk_used -= num_bytes * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05003471 if (cache->ro)
3472 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003473 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003474 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04003475 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003476 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003477
3478 ret = btrfs_discard_extent(root, bytenr,
3479 num_bytes);
3480 WARN_ON(ret);
3481
Josef Bacik0f9dd462008-09-23 13:14:11 -04003482 ret = btrfs_add_free_space(cache, bytenr,
3483 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003484 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04003485 }
Chris Masoncd1bc462007-04-27 10:08:34 -04003486 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04003487 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003488 total -= num_bytes;
3489 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003490 }
3491 return 0;
3492}
Chris Mason6324fbf2008-03-24 15:01:59 -04003493
Chris Masona061fc82008-05-07 11:43:44 -04003494static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3495{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003496 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003497 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003498
3499 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3500 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003501 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003502
Yan Zhengd2fb3432008-12-11 16:30:39 -05003503 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04003504 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003505
3506 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003507}
3508
Yan Zheng11833d62009-09-11 16:11:19 -04003509/*
3510 * this function must be called within transaction
3511 */
3512int btrfs_pin_extent(struct btrfs_root *root,
3513 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05003514{
Yan324ae4d2007-11-16 14:57:08 -05003515 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -04003516 struct btrfs_block_group_cache *cache;
Yan324ae4d2007-11-16 14:57:08 -05003517
Yan Zheng11833d62009-09-11 16:11:19 -04003518 cache = btrfs_lookup_block_group(fs_info, bytenr);
3519 BUG_ON(!cache);
Chris Masonb9473432009-03-13 11:00:37 -04003520
Yan Zheng11833d62009-09-11 16:11:19 -04003521 spin_lock(&cache->space_info->lock);
3522 spin_lock(&cache->lock);
3523 cache->pinned += num_bytes;
3524 cache->space_info->bytes_pinned += num_bytes;
3525 if (reserved) {
3526 cache->reserved -= num_bytes;
3527 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05003528 }
Yan Zheng11833d62009-09-11 16:11:19 -04003529 spin_unlock(&cache->lock);
3530 spin_unlock(&cache->space_info->lock);
3531
3532 btrfs_put_block_group(cache);
3533
3534 set_extent_dirty(fs_info->pinned_extents,
3535 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Yan324ae4d2007-11-16 14:57:08 -05003536 return 0;
3537}
Chris Mason9078a3e2007-04-26 16:46:15 -04003538
Yan Zheng11833d62009-09-11 16:11:19 -04003539static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3540 u64 num_bytes, int reserve)
Zheng Yane8569812008-09-26 10:05:48 -04003541{
Yan Zheng11833d62009-09-11 16:11:19 -04003542 spin_lock(&cache->space_info->lock);
3543 spin_lock(&cache->lock);
3544 if (reserve) {
3545 cache->reserved += num_bytes;
3546 cache->space_info->bytes_reserved += num_bytes;
3547 } else {
3548 cache->reserved -= num_bytes;
3549 cache->space_info->bytes_reserved -= num_bytes;
3550 }
3551 spin_unlock(&cache->lock);
3552 spin_unlock(&cache->space_info->lock);
3553 return 0;
3554}
Zheng Yane8569812008-09-26 10:05:48 -04003555
Yan Zheng11833d62009-09-11 16:11:19 -04003556int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3557 struct btrfs_root *root)
3558{
3559 struct btrfs_fs_info *fs_info = root->fs_info;
3560 struct btrfs_caching_control *next;
3561 struct btrfs_caching_control *caching_ctl;
3562 struct btrfs_block_group_cache *cache;
3563
3564 down_write(&fs_info->extent_commit_sem);
3565
3566 list_for_each_entry_safe(caching_ctl, next,
3567 &fs_info->caching_block_groups, list) {
3568 cache = caching_ctl->block_group;
3569 if (block_group_cache_done(cache)) {
3570 cache->last_byte_to_unpin = (u64)-1;
3571 list_del_init(&caching_ctl->list);
3572 put_caching_control(caching_ctl);
3573 } else {
3574 cache->last_byte_to_unpin = caching_ctl->progress;
3575 }
3576 }
3577
3578 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3579 fs_info->pinned_extents = &fs_info->freed_extents[1];
3580 else
3581 fs_info->pinned_extents = &fs_info->freed_extents[0];
3582
3583 up_write(&fs_info->extent_commit_sem);
3584 return 0;
3585}
3586
3587static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3588{
3589 struct btrfs_fs_info *fs_info = root->fs_info;
3590 struct btrfs_block_group_cache *cache = NULL;
3591 u64 len;
3592
3593 while (start <= end) {
3594 if (!cache ||
3595 start >= cache->key.objectid + cache->key.offset) {
3596 if (cache)
3597 btrfs_put_block_group(cache);
3598 cache = btrfs_lookup_block_group(fs_info, start);
3599 BUG_ON(!cache);
3600 }
3601
3602 len = cache->key.objectid + cache->key.offset - start;
3603 len = min(len, end + 1 - start);
3604
3605 if (start < cache->last_byte_to_unpin) {
3606 len = min(len, cache->last_byte_to_unpin - start);
3607 btrfs_add_free_space(cache, start, len);
3608 }
Josef Bacik25179202008-10-29 14:49:05 -04003609
3610 spin_lock(&cache->space_info->lock);
3611 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003612 cache->pinned -= len;
3613 cache->space_info->bytes_pinned -= len;
Josef Bacik25179202008-10-29 14:49:05 -04003614 spin_unlock(&cache->lock);
3615 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003616
3617 start += len;
3618 }
3619
3620 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003621 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04003622 return 0;
3623}
3624
3625int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04003626 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05003627{
Yan Zheng11833d62009-09-11 16:11:19 -04003628 struct btrfs_fs_info *fs_info = root->fs_info;
3629 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04003630 u64 start;
3631 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003632 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003633
Yan Zheng11833d62009-09-11 16:11:19 -04003634 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3635 unpin = &fs_info->freed_extents[1];
3636 else
3637 unpin = &fs_info->freed_extents[0];
3638
Chris Masond3977122009-01-05 21:25:51 -05003639 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003640 ret = find_first_extent_bit(unpin, 0, &start, &end,
3641 EXTENT_DIRTY);
3642 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003643 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003644
3645 ret = btrfs_discard_extent(root, start, end + 1 - start);
3646
Chris Mason1a5bc162007-10-15 16:15:26 -04003647 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04003648 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04003649 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003650 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003651
Liu Hui1f3c79a2009-01-05 15:57:51 -05003652 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003653}
3654
Zheng Yan31840ae2008-09-23 13:14:14 -04003655static int pin_down_bytes(struct btrfs_trans_handle *trans,
3656 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003657 struct btrfs_path *path,
Yan Zheng11833d62009-09-11 16:11:19 -04003658 u64 bytenr, u64 num_bytes,
3659 int is_data, int reserved,
Chris Masonb9473432009-03-13 11:00:37 -04003660 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003661{
Chris Mason1a5bc162007-10-15 16:15:26 -04003662 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003663 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003664
Zheng Yan31840ae2008-09-23 13:14:14 -04003665 if (is_data)
3666 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003667
Chris Mason444528b2009-10-14 09:38:28 -04003668 /*
3669 * discard is sloooow, and so triggering discards on
3670 * individual btree blocks isn't a good plan. Just
3671 * pin everything in discard mode.
3672 */
3673 if (btrfs_test_opt(root, DISCARD))
3674 goto pinit;
3675
Zheng Yan31840ae2008-09-23 13:14:14 -04003676 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3677 if (!buf)
3678 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003679
Zheng Yan31840ae2008-09-23 13:14:14 -04003680 /* we can reuse a block if it hasn't been written
3681 * and it is from this transaction. We can't
3682 * reuse anything from the tree log root because
3683 * it has tiny sub-transactions.
3684 */
3685 if (btrfs_buffer_uptodate(buf, 0) &&
3686 btrfs_try_tree_lock(buf)) {
3687 u64 header_owner = btrfs_header_owner(buf);
3688 u64 header_transid = btrfs_header_generation(buf);
3689 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3690 header_transid == trans->transid &&
3691 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003692 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003693 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003694 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003695 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003696 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003697 free_extent_buffer(buf);
3698pinit:
Yan Zheng11833d62009-09-11 16:11:19 -04003699 if (path)
3700 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003701 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003702 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
Zheng Yan31840ae2008-09-23 13:14:14 -04003703
Chris Masonbe744172007-05-06 10:15:01 -04003704 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003705 return 0;
3706}
3707
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003708static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3709 struct btrfs_root *root,
3710 u64 bytenr, u64 num_bytes, u64 parent,
3711 u64 root_objectid, u64 owner_objectid,
3712 u64 owner_offset, int refs_to_drop,
3713 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003714{
Chris Masone2fa7222007-03-12 16:22:34 -04003715 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003716 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003717 struct btrfs_fs_info *info = root->fs_info;
3718 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003719 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003720 struct btrfs_extent_item *ei;
3721 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003722 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003723 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003724 int extent_slot = 0;
3725 int found_extent = 0;
3726 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003727 u32 item_size;
3728 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003729
Chris Mason5caf2a02007-04-02 11:20:42 -04003730 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003731 if (!path)
3732 return -ENOMEM;
3733
Chris Mason3c12ac72008-04-21 12:01:38 -04003734 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003735 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003736
3737 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3738 BUG_ON(!is_data && refs_to_drop != 1);
3739
3740 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3741 bytenr, num_bytes, parent,
3742 root_objectid, owner_objectid,
3743 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003744 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003745 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003746 while (extent_slot >= 0) {
3747 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003748 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003749 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003750 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003751 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3752 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003753 found_extent = 1;
3754 break;
3755 }
3756 if (path->slots[0] - extent_slot > 5)
3757 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003758 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003759 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003760#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3761 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3762 if (found_extent && item_size < sizeof(*ei))
3763 found_extent = 0;
3764#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003765 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003766 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003767 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003768 NULL, refs_to_drop,
3769 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003770 BUG_ON(ret);
3771 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003772 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003773
3774 key.objectid = bytenr;
3775 key.type = BTRFS_EXTENT_ITEM_KEY;
3776 key.offset = num_bytes;
3777
Zheng Yan31840ae2008-09-23 13:14:14 -04003778 ret = btrfs_search_slot(trans, extent_root,
3779 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003780 if (ret) {
3781 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003782 ", was looking for %llu\n", ret,
3783 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003784 btrfs_print_leaf(extent_root, path->nodes[0]);
3785 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003786 BUG_ON(ret);
3787 extent_slot = path->slots[0];
3788 }
Chris Mason7bb86312007-12-11 09:25:06 -05003789 } else {
3790 btrfs_print_leaf(extent_root, path->nodes[0]);
3791 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003792 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003793 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003794 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003795 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003796 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003797 (unsigned long long)owner_objectid,
3798 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003799 }
Chris Mason5f39d392007-10-15 16:14:19 -04003800
3801 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003802 item_size = btrfs_item_size_nr(leaf, extent_slot);
3803#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3804 if (item_size < sizeof(*ei)) {
3805 BUG_ON(found_extent || extent_slot != path->slots[0]);
3806 ret = convert_extent_item_v0(trans, extent_root, path,
3807 owner_objectid, 0);
3808 BUG_ON(ret < 0);
3809
3810 btrfs_release_path(extent_root, path);
3811 path->leave_spinning = 1;
3812
3813 key.objectid = bytenr;
3814 key.type = BTRFS_EXTENT_ITEM_KEY;
3815 key.offset = num_bytes;
3816
3817 ret = btrfs_search_slot(trans, extent_root, &key, path,
3818 -1, 1);
3819 if (ret) {
3820 printk(KERN_ERR "umm, got %d back from search"
3821 ", was looking for %llu\n", ret,
3822 (unsigned long long)bytenr);
3823 btrfs_print_leaf(extent_root, path->nodes[0]);
3824 }
3825 BUG_ON(ret);
3826 extent_slot = path->slots[0];
3827 leaf = path->nodes[0];
3828 item_size = btrfs_item_size_nr(leaf, extent_slot);
3829 }
3830#endif
3831 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003832 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003833 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003834 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3835 struct btrfs_tree_block_info *bi;
3836 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3837 bi = (struct btrfs_tree_block_info *)(ei + 1);
3838 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003839 }
3840
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003841 refs = btrfs_extent_refs(leaf, ei);
3842 BUG_ON(refs < refs_to_drop);
3843 refs -= refs_to_drop;
3844
3845 if (refs > 0) {
3846 if (extent_op)
3847 __run_delayed_extent_op(extent_op, leaf, ei);
3848 /*
3849 * In the case of inline back ref, reference count will
3850 * be updated by remove_extent_backref
3851 */
3852 if (iref) {
3853 BUG_ON(!found_extent);
3854 } else {
3855 btrfs_set_extent_refs(leaf, ei, refs);
3856 btrfs_mark_buffer_dirty(leaf);
3857 }
3858 if (found_extent) {
3859 ret = remove_extent_backref(trans, extent_root, path,
3860 iref, refs_to_drop,
3861 is_data);
3862 BUG_ON(ret);
3863 }
3864 } else {
3865 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003866 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003868 if (found_extent) {
3869 BUG_ON(is_data && refs_to_drop !=
3870 extent_data_ref_count(root, path, iref));
3871 if (iref) {
3872 BUG_ON(path->slots[0] != extent_slot);
3873 } else {
3874 BUG_ON(path->slots[0] != extent_slot + 1);
3875 path->slots[0] = extent_slot;
3876 num_to_del = 2;
3877 }
Chris Mason78fae272007-03-25 11:35:08 -04003878 }
Chris Masonb9473432009-03-13 11:00:37 -04003879
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003880 ret = pin_down_bytes(trans, root, path, bytenr,
Yan Zheng11833d62009-09-11 16:11:19 -04003881 num_bytes, is_data, 0, &must_clean);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003882 if (ret > 0)
3883 mark_free = 1;
3884 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003885 /*
3886 * it is going to be very rare for someone to be waiting
3887 * on the block we're freeing. del_items might need to
3888 * schedule, so rather than get fancy, just force it
3889 * to blocking here
3890 */
3891 if (must_clean)
3892 btrfs_set_lock_blocking(must_clean);
3893
Chris Mason952fcca2008-02-18 16:33:44 -05003894 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3895 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003896 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003897 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003898
Chris Masonb9473432009-03-13 11:00:37 -04003899 if (must_clean) {
3900 clean_tree_block(NULL, root, must_clean);
3901 btrfs_tree_unlock(must_clean);
3902 free_extent_buffer(must_clean);
3903 }
3904
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003905 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003906 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3907 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003908 } else {
3909 invalidate_mapping_pages(info->btree_inode->i_mapping,
3910 bytenr >> PAGE_CACHE_SHIFT,
3911 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003912 }
3913
Chris Masondcbdd4d2008-12-16 13:51:01 -05003914 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3915 mark_free);
3916 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003917 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003918 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003919 return ret;
3920}
3921
3922/*
Chris Mason1887be62009-03-13 10:11:24 -04003923 * when we free an extent, it is possible (and likely) that we free the last
3924 * delayed ref for that extent as well. This searches the delayed ref tree for
3925 * a given extent, and if there are no other delayed refs to be processed, it
3926 * removes it from the tree.
3927 */
3928static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3929 struct btrfs_root *root, u64 bytenr)
3930{
3931 struct btrfs_delayed_ref_head *head;
3932 struct btrfs_delayed_ref_root *delayed_refs;
3933 struct btrfs_delayed_ref_node *ref;
3934 struct rb_node *node;
3935 int ret;
3936
3937 delayed_refs = &trans->transaction->delayed_refs;
3938 spin_lock(&delayed_refs->lock);
3939 head = btrfs_find_delayed_ref_head(trans, bytenr);
3940 if (!head)
3941 goto out;
3942
3943 node = rb_prev(&head->node.rb_node);
3944 if (!node)
3945 goto out;
3946
3947 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3948
3949 /* there are still entries for this ref, we can't drop it */
3950 if (ref->bytenr == bytenr)
3951 goto out;
3952
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003953 if (head->extent_op) {
3954 if (!head->must_insert_reserved)
3955 goto out;
3956 kfree(head->extent_op);
3957 head->extent_op = NULL;
3958 }
3959
Chris Mason1887be62009-03-13 10:11:24 -04003960 /*
3961 * waiting for the lock here would deadlock. If someone else has it
3962 * locked they are already in the process of dropping it anyway
3963 */
3964 if (!mutex_trylock(&head->mutex))
3965 goto out;
3966
3967 /*
3968 * at this point we have a head with no other entries. Go
3969 * ahead and process it.
3970 */
3971 head->node.in_tree = 0;
3972 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04003973
Chris Mason1887be62009-03-13 10:11:24 -04003974 delayed_refs->num_entries--;
3975
3976 /*
3977 * we don't take a ref on the node because we're removing it from the
3978 * tree, so we just steal the ref the tree was holding.
3979 */
Chris Masonc3e69d52009-03-13 10:17:05 -04003980 delayed_refs->num_heads--;
3981 if (list_empty(&head->cluster))
3982 delayed_refs->num_heads_ready--;
3983
3984 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04003985 spin_unlock(&delayed_refs->lock);
3986
3987 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003988 &head->node, head->extent_op,
3989 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04003990 BUG_ON(ret);
3991 btrfs_put_delayed_ref(&head->node);
3992 return 0;
3993out:
3994 spin_unlock(&delayed_refs->lock);
3995 return 0;
3996}
3997
Chris Mason925baed2008-06-25 16:01:30 -04003998int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003999 struct btrfs_root *root,
4000 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004001 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004002{
4003 int ret;
4004
Chris Mason56bec292009-03-13 10:10:06 -04004005 /*
4006 * tree log blocks never actually go into the extent allocation
4007 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004008 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004009 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4010 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004011 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004012 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004013 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004014 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4015 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4016 parent, root_objectid, (int)owner,
4017 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004018 BUG_ON(ret);
4019 ret = check_ref_cleanup(trans, root, bytenr);
4020 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004021 } else {
4022 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4023 parent, root_objectid, owner,
4024 offset, BTRFS_DROP_DELAYED_REF, NULL);
4025 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004026 }
Chris Mason925baed2008-06-25 16:01:30 -04004027 return ret;
4028}
4029
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00004030int btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4031 struct btrfs_root *root,
4032 u64 bytenr, u32 blocksize,
4033 u64 parent, u64 root_objectid, int level)
4034{
4035 u64 used;
4036 spin_lock(&root->node_lock);
4037 used = btrfs_root_used(&root->root_item) - blocksize;
4038 btrfs_set_root_used(&root->root_item, used);
4039 spin_unlock(&root->node_lock);
4040
4041 return btrfs_free_extent(trans, root, bytenr, blocksize,
4042 parent, root_objectid, level, 0);
4043}
4044
Chris Mason87ee04e2007-11-30 11:30:34 -05004045static u64 stripe_align(struct btrfs_root *root, u64 val)
4046{
4047 u64 mask = ((u64)root->stripesize - 1);
4048 u64 ret = (val + mask) & ~mask;
4049 return ret;
4050}
4051
Chris Masonfec577f2007-02-26 10:40:21 -05004052/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004053 * when we wait for progress in the block group caching, its because
4054 * our allocation attempt failed at least once. So, we must sleep
4055 * and let some progress happen before we try again.
4056 *
4057 * This function will sleep at least once waiting for new free space to
4058 * show up, and then it will check the block group free space numbers
4059 * for our min num_bytes. Another option is to have it go ahead
4060 * and look in the rbtree for a free extent of a given size, but this
4061 * is a good start.
4062 */
4063static noinline int
4064wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4065 u64 num_bytes)
4066{
Yan Zheng11833d62009-09-11 16:11:19 -04004067 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004068 DEFINE_WAIT(wait);
4069
Yan Zheng11833d62009-09-11 16:11:19 -04004070 caching_ctl = get_caching_control(cache);
4071 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004072 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004073
Yan Zheng11833d62009-09-11 16:11:19 -04004074 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004075 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004076
4077 put_caching_control(caching_ctl);
4078 return 0;
4079}
4080
4081static noinline int
4082wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4083{
4084 struct btrfs_caching_control *caching_ctl;
4085 DEFINE_WAIT(wait);
4086
4087 caching_ctl = get_caching_control(cache);
4088 if (!caching_ctl)
4089 return 0;
4090
4091 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4092
4093 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004094 return 0;
4095}
4096
Yan, Zhengb742bb82010-05-16 10:46:24 -04004097static int get_block_group_index(struct btrfs_block_group_cache *cache)
4098{
4099 int index;
4100 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4101 index = 0;
4102 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4103 index = 1;
4104 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4105 index = 2;
4106 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4107 index = 3;
4108 else
4109 index = 4;
4110 return index;
4111}
4112
Josef Bacik817d52f2009-07-13 21:29:25 -04004113enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004114 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004115 LOOP_CACHING_NOWAIT = 1,
4116 LOOP_CACHING_WAIT = 2,
4117 LOOP_ALLOC_CHUNK = 3,
4118 LOOP_NO_EMPTY_SIZE = 4,
4119};
4120
4121/*
Chris Masonfec577f2007-02-26 10:40:21 -05004122 * walks the btree of allocated extents and find a hole of a given size.
4123 * The key ins is changed to record the hole:
4124 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004125 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004126 * ins->offset == number of blocks
4127 * Any available blocks before search_start are skipped.
4128 */
Chris Masond3977122009-01-05 21:25:51 -05004129static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004130 struct btrfs_root *orig_root,
4131 u64 num_bytes, u64 empty_size,
4132 u64 search_start, u64 search_end,
4133 u64 hint_byte, struct btrfs_key *ins,
4134 u64 exclude_start, u64 exclude_nr,
4135 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004136{
Josef Bacik80eb2342008-10-29 14:49:05 -04004137 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004138 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004139 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004140 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004141 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004142 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004143 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004144 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004145 int last_ptr_loop = 0;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004146 int index = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004147 int loop = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004148 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004149 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004150 bool failed_alloc = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004151 u64 ideal_cache_percent = 0;
4152 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004153
Chris Masondb945352007-10-15 16:15:53 -04004154 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004155 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004156 ins->objectid = 0;
4157 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004158
Josef Bacik2552d172009-04-03 10:14:19 -04004159 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004160 if (!space_info) {
4161 printk(KERN_ERR "No space info for %d\n", data);
4162 return -ENOSPC;
4163 }
Josef Bacik2552d172009-04-03 10:14:19 -04004164
Chris Mason0ef3e662008-05-24 14:04:53 -04004165 if (orig_root->ref_cows || empty_size)
4166 allowed_chunk_alloc = 1;
4167
Chris Mason239b14b2008-03-24 15:02:07 -04004168 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004169 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004170 if (!btrfs_test_opt(root, SSD))
4171 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004172 }
4173
Chris Masonfa9c0d72009-04-03 09:47:43 -04004174 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4175 last_ptr = &root->fs_info->data_alloc_cluster;
4176 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004177
Chris Mason239b14b2008-03-24 15:02:07 -04004178 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004179 spin_lock(&last_ptr->lock);
4180 if (last_ptr->block_group)
4181 hint_byte = last_ptr->window_start;
4182 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004183 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004184
Chris Masona061fc82008-05-07 11:43:44 -04004185 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004186 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004187
Josef Bacik817d52f2009-07-13 21:29:25 -04004188 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004189 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004190
Josef Bacik2552d172009-04-03 10:14:19 -04004191 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004192ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004193 block_group = btrfs_lookup_block_group(root->fs_info,
4194 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004195 /*
4196 * we don't want to use the block group if it doesn't match our
4197 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004198 *
4199 * However if we are re-searching with an ideal block group
4200 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004201 */
4202 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004203 (block_group->cached != BTRFS_CACHE_NO ||
4204 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004205 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004206 if (list_empty(&block_group->list) ||
4207 block_group->ro) {
4208 /*
4209 * someone is removing this block group,
4210 * we can't jump into the have_block_group
4211 * target because our list pointers are not
4212 * valid
4213 */
4214 btrfs_put_block_group(block_group);
4215 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004216 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004217 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004218 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004219 }
Josef Bacik2552d172009-04-03 10:14:19 -04004220 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004221 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004222 }
Chris Mason42e70e72008-11-07 18:17:11 -05004223 }
Josef Bacik2552d172009-04-03 10:14:19 -04004224search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004225 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004226 list_for_each_entry(block_group, &space_info->block_groups[index],
4227 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004228 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004229 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004230
Josef Bacik11dfe352009-11-13 20:12:59 +00004231 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004232 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004233
Josef Bacik2552d172009-04-03 10:14:19 -04004234have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004235 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004236 u64 free_percent;
4237
4238 free_percent = btrfs_block_group_used(&block_group->item);
4239 free_percent *= 100;
4240 free_percent = div64_u64(free_percent,
4241 block_group->key.offset);
4242 free_percent = 100 - free_percent;
4243 if (free_percent > ideal_cache_percent &&
4244 likely(!block_group->ro)) {
4245 ideal_cache_offset = block_group->key.objectid;
4246 ideal_cache_percent = free_percent;
4247 }
4248
Josef Bacik817d52f2009-07-13 21:29:25 -04004249 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05004250 * We only want to start kthread caching if we are at
4251 * the point where we will wait for caching to make
4252 * progress, or if our ideal search is over and we've
4253 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04004254 */
4255 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05004256 (loop > LOOP_FIND_IDEAL &&
4257 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004258 ret = cache_block_group(block_group);
4259 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004260 }
Josef Bacikccf0e722009-11-10 21:23:48 -05004261 found_uncached_bg = true;
4262
4263 /*
4264 * If loop is set for cached only, try the next block
4265 * group.
4266 */
4267 if (loop == LOOP_FIND_IDEAL)
4268 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05004269 }
4270
Josef Bacik817d52f2009-07-13 21:29:25 -04004271 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05004272 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04004273 found_uncached_bg = true;
4274
Josef Bacikea6a4782008-11-20 12:16:16 -05004275 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004276 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004277
Josef Bacik0a243252009-09-11 16:11:20 -04004278 /*
4279 * Ok we want to try and use the cluster allocator, so lets look
4280 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4281 * have tried the cluster allocator plenty of times at this
4282 * point and not have found anything, so we are likely way too
4283 * fragmented for the clustering stuff to find anything, so lets
4284 * just skip it and let the allocator find whatever block it can
4285 * find
4286 */
4287 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004288 /*
4289 * the refill lock keeps out other
4290 * people trying to start a new cluster
4291 */
4292 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004293 if (last_ptr->block_group &&
4294 (last_ptr->block_group->ro ||
4295 !block_group_bits(last_ptr->block_group, data))) {
4296 offset = 0;
4297 goto refill_cluster;
4298 }
4299
Chris Masonfa9c0d72009-04-03 09:47:43 -04004300 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4301 num_bytes, search_start);
4302 if (offset) {
4303 /* we have a block, we're done */
4304 spin_unlock(&last_ptr->refill_lock);
4305 goto checks;
4306 }
4307
4308 spin_lock(&last_ptr->lock);
4309 /*
4310 * whoops, this cluster doesn't actually point to
4311 * this block group. Get a ref on the block
4312 * group is does point to and try again
4313 */
4314 if (!last_ptr_loop && last_ptr->block_group &&
4315 last_ptr->block_group != block_group) {
4316
4317 btrfs_put_block_group(block_group);
4318 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00004319 btrfs_get_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004320 spin_unlock(&last_ptr->lock);
4321 spin_unlock(&last_ptr->refill_lock);
4322
4323 last_ptr_loop = 1;
4324 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04004325 /*
4326 * we know this block group is properly
4327 * in the list because
4328 * btrfs_remove_block_group, drops the
4329 * cluster before it removes the block
4330 * group from the list
4331 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04004332 goto have_block_group;
4333 }
4334 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004335refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04004336 /*
4337 * this cluster didn't work out, free it and
4338 * start over
4339 */
4340 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4341
4342 last_ptr_loop = 0;
4343
4344 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04004345 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04004346 block_group, last_ptr,
4347 offset, num_bytes,
4348 empty_cluster + empty_size);
4349 if (ret == 0) {
4350 /*
4351 * now pull our allocation out of this
4352 * cluster
4353 */
4354 offset = btrfs_alloc_from_cluster(block_group,
4355 last_ptr, num_bytes,
4356 search_start);
4357 if (offset) {
4358 /* we found one, proceed */
4359 spin_unlock(&last_ptr->refill_lock);
4360 goto checks;
4361 }
Josef Bacik0a243252009-09-11 16:11:20 -04004362 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4363 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004364 spin_unlock(&last_ptr->refill_lock);
4365
Josef Bacik0a243252009-09-11 16:11:20 -04004366 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004367 wait_block_group_cache_progress(block_group,
4368 num_bytes + empty_cluster + empty_size);
4369 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004370 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004371
Chris Masonfa9c0d72009-04-03 09:47:43 -04004372 /*
4373 * at this point we either didn't find a cluster
4374 * or we weren't able to allocate a block from our
4375 * cluster. Free the cluster we've been trying
4376 * to use, and go to the next block group
4377 */
Josef Bacik0a243252009-09-11 16:11:20 -04004378 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004379 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04004380 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004381 }
4382
Josef Bacik6226cb02009-04-03 10:14:18 -04004383 offset = btrfs_find_space_for_alloc(block_group, search_start,
4384 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004385 /*
4386 * If we didn't find a chunk, and we haven't failed on this
4387 * block group before, and this block group is in the middle of
4388 * caching and we are ok with waiting, then go ahead and wait
4389 * for progress to be made, and set failed_alloc to true.
4390 *
4391 * If failed_alloc is true then we've already waited on this
4392 * block group once and should move on to the next block group.
4393 */
4394 if (!offset && !failed_alloc && !cached &&
4395 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004396 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004397 num_bytes + empty_size);
4398 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004399 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004400 } else if (!offset) {
4401 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04004402 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004403checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04004404 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04004405 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04004406 if (search_start + num_bytes >= search_end) {
4407 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004408 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004409 }
Chris Masone37c9e62007-05-09 20:13:14 -04004410
Josef Bacik2552d172009-04-03 10:14:19 -04004411 /* move on to the next group */
4412 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04004413 block_group->key.objectid + block_group->key.offset) {
4414 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004415 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004416 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004417
Josef Bacik2552d172009-04-03 10:14:19 -04004418 if (exclude_nr > 0 &&
4419 (search_start + num_bytes > exclude_start &&
4420 search_start < exclude_start + exclude_nr)) {
4421 search_start = exclude_start + exclude_nr;
4422
Josef Bacik6226cb02009-04-03 10:14:18 -04004423 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004424 /*
4425 * if search_start is still in this block group
4426 * then we just re-search this block group
4427 */
4428 if (search_start >= block_group->key.objectid &&
4429 search_start < (block_group->key.objectid +
Josef Bacik6226cb02009-04-03 10:14:18 -04004430 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04004431 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04004432 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004433 }
Josef Bacik2552d172009-04-03 10:14:19 -04004434
4435 ins->objectid = search_start;
4436 ins->offset = num_bytes;
4437
Josef Bacik6226cb02009-04-03 10:14:18 -04004438 if (offset < search_start)
4439 btrfs_add_free_space(block_group, offset,
4440 search_start - offset);
4441 BUG_ON(offset > search_start);
4442
Yan Zheng11833d62009-09-11 16:11:19 -04004443 update_reserved_extents(block_group, num_bytes, 1);
4444
Josef Bacik2552d172009-04-03 10:14:19 -04004445 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04004446 break;
4447loop:
Josef Bacik0a243252009-09-11 16:11:20 -04004448 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004449 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004450 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d72009-04-03 09:47:43 -04004451 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004452 }
4453 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004454
Yan, Zhengb742bb82010-05-16 10:46:24 -04004455 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
4456 goto search;
4457
Josef Bacikccf0e722009-11-10 21:23:48 -05004458 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
4459 * for them to make caching progress. Also
4460 * determine the best possible bg to cache
4461 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
4462 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04004463 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4464 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4465 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4466 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04004467 */
Josef Bacik817d52f2009-07-13 21:29:25 -04004468 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4469 (found_uncached_bg || empty_size || empty_cluster ||
4470 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004471 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004472 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004473 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004474 loop++;
4475 if (!ideal_cache_percent &&
4476 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04004477 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05004478
4479 /*
4480 * 1 of the following 2 things have happened so far
4481 *
4482 * 1) We found an ideal block group for caching that
4483 * is mostly full and will cache quickly, so we might
4484 * as well wait for it.
4485 *
4486 * 2) We searched for cached only and we didn't find
4487 * anything, and we didn't start any caching kthreads
4488 * either, so chances are we will loop through and
4489 * start a couple caching kthreads, and then come back
4490 * around and just wait for them. This will be slower
4491 * because we will have 2 caching kthreads reading at
4492 * the same time when we could have just started one
4493 * and waited for it to get far enough to give us an
4494 * allocation, so go ahead and go to the wait caching
4495 * loop.
4496 */
4497 loop = LOOP_CACHING_WAIT;
4498 search_start = ideal_cache_offset;
4499 ideal_cache_percent = 0;
4500 goto ideal_cache;
4501 } else if (loop == LOOP_FIND_IDEAL) {
4502 /*
4503 * Didn't find a uncached bg, wait on anything we find
4504 * next.
4505 */
4506 loop = LOOP_CACHING_WAIT;
4507 goto search;
4508 }
4509
4510 if (loop < LOOP_CACHING_WAIT) {
4511 loop++;
4512 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04004513 }
4514
4515 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004516 empty_size = 0;
4517 empty_cluster = 0;
4518 }
Chris Mason42e70e72008-11-07 18:17:11 -05004519
Josef Bacik2552d172009-04-03 10:14:19 -04004520 if (allowed_chunk_alloc) {
4521 ret = do_chunk_alloc(trans, root, num_bytes +
4522 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04004523 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004524 done_chunk_alloc = 1;
4525 } else if (!done_chunk_alloc) {
Josef Bacik2552d172009-04-03 10:14:19 -04004526 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004527 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004528
Josef Bacik817d52f2009-07-13 21:29:25 -04004529 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004530 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04004531 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004532 }
Josef Bacik2552d172009-04-03 10:14:19 -04004533 ret = -ENOSPC;
4534 } else if (!ins->objectid) {
4535 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04004536 }
Chris Mason0b86a832008-03-24 15:01:56 -04004537
Josef Bacik80eb2342008-10-29 14:49:05 -04004538 /* we found what we needed */
4539 if (ins->objectid) {
4540 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05004541 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04004542
Chris Masonfa9c0d72009-04-03 09:47:43 -04004543 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004544 ret = 0;
4545 }
Chris Mason0b86a832008-03-24 15:01:56 -04004546
Chris Mason0f70abe2007-02-28 16:46:22 -05004547 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004548}
Chris Masonec44a352008-04-28 15:29:52 -04004549
Josef Bacik9ed74f22009-09-11 16:12:44 -04004550static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4551 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04004552{
4553 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004554 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004555
Josef Bacik9ed74f22009-09-11 16:12:44 -04004556 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05004557 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4558 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04004559 info->bytes_pinned - info->bytes_reserved -
4560 info->bytes_super),
Chris Masond3977122009-01-05 21:25:51 -05004561 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05004562 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Josef Bacik9ed74f22009-09-11 16:12:44 -04004563 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4564 "\n",
Joel Becker21380932009-04-21 12:38:29 -07004565 (unsigned long long)info->total_bytes,
4566 (unsigned long long)info->bytes_pinned,
4567 (unsigned long long)info->bytes_delalloc,
4568 (unsigned long long)info->bytes_may_use,
Josef Bacik9ed74f22009-09-11 16:12:44 -04004569 (unsigned long long)info->bytes_used,
4570 (unsigned long long)info->bytes_root,
4571 (unsigned long long)info->bytes_super,
4572 (unsigned long long)info->bytes_reserved);
4573 spin_unlock(&info->lock);
4574
4575 if (!dump_block_groups)
4576 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004577
Josef Bacik80eb2342008-10-29 14:49:05 -04004578 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004579again:
4580 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004581 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05004582 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4583 "%llu pinned %llu reserved\n",
4584 (unsigned long long)cache->key.objectid,
4585 (unsigned long long)cache->key.offset,
4586 (unsigned long long)btrfs_block_group_used(&cache->item),
4587 (unsigned long long)cache->pinned,
4588 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004589 btrfs_dump_free_space(cache, bytes);
4590 spin_unlock(&cache->lock);
4591 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04004592 if (++index < BTRFS_NR_RAID_TYPES)
4593 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04004594 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004595}
Zheng Yane8569812008-09-26 10:05:48 -04004596
Yan Zheng11833d62009-09-11 16:11:19 -04004597int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4598 struct btrfs_root *root,
4599 u64 num_bytes, u64 min_alloc_size,
4600 u64 empty_size, u64 hint_byte,
4601 u64 search_end, struct btrfs_key *ins,
4602 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004603{
4604 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04004605 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004606
Josef Bacik6a632092009-02-20 11:00:09 -05004607 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04004608again:
Chris Mason0ef3e662008-05-24 14:04:53 -04004609 /*
4610 * the only place that sets empty_size is btrfs_realloc_node, which
4611 * is not called recursively on allocations
4612 */
Josef Bacik83d3c962009-12-07 21:45:59 +00004613 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04004614 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004615 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04004616
Chris Masondb945352007-10-15 16:15:53 -04004617 WARN_ON(num_bytes < root->sectorsize);
4618 ret = find_free_extent(trans, root, num_bytes, empty_size,
4619 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04004620 trans->alloc_exclude_start,
4621 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04004622
Chris Mason98d20f62008-04-14 09:46:10 -04004623 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4624 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004625 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004626 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04004627 do_chunk_alloc(trans, root->fs_info->extent_root,
4628 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004629 goto again;
4630 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004631 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004632 struct btrfs_space_info *sinfo;
4633
4634 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004635 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4636 "wanted %llu\n", (unsigned long long)data,
4637 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004638 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04004639 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004640
4641 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004642}
4643
Chris Mason65b51a02008-08-01 15:11:20 -04004644int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4645{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004646 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004647 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004648
Josef Bacik0f9dd462008-09-23 13:14:11 -04004649 cache = btrfs_lookup_block_group(root->fs_info, start);
4650 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004651 printk(KERN_ERR "Unable to find block group for %llu\n",
4652 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004653 return -ENOSPC;
4654 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004655
4656 ret = btrfs_discard_extent(root, start, len);
4657
Josef Bacik0f9dd462008-09-23 13:14:11 -04004658 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04004659 update_reserved_extents(cache, len, 0);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004660 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04004661
Chris Masone6dcd2d2008-07-17 12:53:50 -04004662 return ret;
4663}
4664
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004665static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4666 struct btrfs_root *root,
4667 u64 parent, u64 root_objectid,
4668 u64 flags, u64 owner, u64 offset,
4669 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004670{
4671 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004672 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004673 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004674 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004675 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004676 struct extent_buffer *leaf;
4677 int type;
4678 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004679
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004680 if (parent > 0)
4681 type = BTRFS_SHARED_DATA_REF_KEY;
4682 else
4683 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004684
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004685 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004686
4687 path = btrfs_alloc_path();
4688 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004689
Chris Masonb9473432009-03-13 11:00:37 -04004690 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004691 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4692 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004693 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004694
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004695 leaf = path->nodes[0];
4696 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004697 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004698 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4699 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4700 btrfs_set_extent_flags(leaf, extent_item,
4701 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004702
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004703 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4704 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4705 if (parent > 0) {
4706 struct btrfs_shared_data_ref *ref;
4707 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4708 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4709 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4710 } else {
4711 struct btrfs_extent_data_ref *ref;
4712 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4713 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4714 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4715 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4716 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4717 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004718
4719 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004720 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004721
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004722 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4723 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05004724 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004725 printk(KERN_ERR "btrfs update block group failed for %llu "
4726 "%llu\n", (unsigned long long)ins->objectid,
4727 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004728 BUG();
4729 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004730 return ret;
4731}
4732
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004733static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4734 struct btrfs_root *root,
4735 u64 parent, u64 root_objectid,
4736 u64 flags, struct btrfs_disk_key *key,
4737 int level, struct btrfs_key *ins)
4738{
4739 int ret;
4740 struct btrfs_fs_info *fs_info = root->fs_info;
4741 struct btrfs_extent_item *extent_item;
4742 struct btrfs_tree_block_info *block_info;
4743 struct btrfs_extent_inline_ref *iref;
4744 struct btrfs_path *path;
4745 struct extent_buffer *leaf;
4746 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4747
4748 path = btrfs_alloc_path();
4749 BUG_ON(!path);
4750
4751 path->leave_spinning = 1;
4752 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4753 ins, size);
4754 BUG_ON(ret);
4755
4756 leaf = path->nodes[0];
4757 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4758 struct btrfs_extent_item);
4759 btrfs_set_extent_refs(leaf, extent_item, 1);
4760 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4761 btrfs_set_extent_flags(leaf, extent_item,
4762 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4763 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4764
4765 btrfs_set_tree_block_key(leaf, block_info, key);
4766 btrfs_set_tree_block_level(leaf, block_info, level);
4767
4768 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4769 if (parent > 0) {
4770 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4771 btrfs_set_extent_inline_ref_type(leaf, iref,
4772 BTRFS_SHARED_BLOCK_REF_KEY);
4773 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4774 } else {
4775 btrfs_set_extent_inline_ref_type(leaf, iref,
4776 BTRFS_TREE_BLOCK_REF_KEY);
4777 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4778 }
4779
4780 btrfs_mark_buffer_dirty(leaf);
4781 btrfs_free_path(path);
4782
4783 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4784 1, 0);
4785 if (ret) {
4786 printk(KERN_ERR "btrfs update block group failed for %llu "
4787 "%llu\n", (unsigned long long)ins->objectid,
4788 (unsigned long long)ins->offset);
4789 BUG();
4790 }
4791 return ret;
4792}
4793
4794int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4795 struct btrfs_root *root,
4796 u64 root_objectid, u64 owner,
4797 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004798{
4799 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04004800
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004801 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004802
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004803 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4804 0, root_objectid, owner, offset,
4805 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004806 return ret;
4807}
Chris Masone02119d2008-09-05 16:13:11 -04004808
4809/*
4810 * this is used by the tree logging recovery code. It records that
4811 * an extent has been allocated and makes sure to clear the free
4812 * space cache bits as well
4813 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004814int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4815 struct btrfs_root *root,
4816 u64 root_objectid, u64 owner, u64 offset,
4817 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004818{
4819 int ret;
4820 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04004821 struct btrfs_caching_control *caching_ctl;
4822 u64 start = ins->objectid;
4823 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04004824
Chris Masone02119d2008-09-05 16:13:11 -04004825 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04004826 cache_block_group(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004827 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004828
Yan Zheng11833d62009-09-11 16:11:19 -04004829 if (!caching_ctl) {
4830 BUG_ON(!block_group_cache_done(block_group));
4831 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4832 BUG_ON(ret);
4833 } else {
4834 mutex_lock(&caching_ctl->mutex);
4835
4836 if (start >= caching_ctl->progress) {
4837 ret = add_excluded_extent(root, start, num_bytes);
4838 BUG_ON(ret);
4839 } else if (start + num_bytes <= caching_ctl->progress) {
4840 ret = btrfs_remove_free_space(block_group,
4841 start, num_bytes);
4842 BUG_ON(ret);
4843 } else {
4844 num_bytes = caching_ctl->progress - start;
4845 ret = btrfs_remove_free_space(block_group,
4846 start, num_bytes);
4847 BUG_ON(ret);
4848
4849 start = caching_ctl->progress;
4850 num_bytes = ins->objectid + ins->offset -
4851 caching_ctl->progress;
4852 ret = add_excluded_extent(root, start, num_bytes);
4853 BUG_ON(ret);
4854 }
4855
4856 mutex_unlock(&caching_ctl->mutex);
4857 put_caching_control(caching_ctl);
4858 }
4859
4860 update_reserved_extents(block_group, ins->offset, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004861 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004862 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4863 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004864 return ret;
4865}
4866
Chris Masone6dcd2d2008-07-17 12:53:50 -04004867/*
4868 * finds a free extent and does all the dirty work required for allocation
4869 * returns the key for the extent through ins, and a tree buffer for
4870 * the first block of the extent through buf.
4871 *
4872 * returns 0 if everything worked, non-zero otherwise.
4873 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004874static int alloc_tree_block(struct btrfs_trans_handle *trans,
4875 struct btrfs_root *root,
4876 u64 num_bytes, u64 parent, u64 root_objectid,
4877 struct btrfs_disk_key *key, int level,
4878 u64 empty_size, u64 hint_byte, u64 search_end,
4879 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004880{
4881 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004882 u64 flags = 0;
4883
Yan Zheng11833d62009-09-11 16:11:19 -04004884 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4885 empty_size, hint_byte, search_end,
4886 ins, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004887 if (ret)
4888 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004889
4890 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4891 if (parent == 0)
4892 parent = ins->objectid;
4893 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4894 } else
4895 BUG_ON(parent > 0);
4896
Chris Masond00aff02008-09-11 15:54:42 -04004897 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004898 struct btrfs_delayed_extent_op *extent_op;
4899 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4900 BUG_ON(!extent_op);
4901 if (key)
4902 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4903 else
4904 memset(&extent_op->key, 0, sizeof(extent_op->key));
4905 extent_op->flags_to_set = flags;
4906 extent_op->update_key = 1;
4907 extent_op->update_flags = 1;
4908 extent_op->is_data = 0;
4909
4910 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4911 ins->offset, parent, root_objectid,
4912 level, BTRFS_ADD_DELAYED_EXTENT,
4913 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004914 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004915 }
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00004916
4917 if (root_objectid == root->root_key.objectid) {
4918 u64 used;
4919 spin_lock(&root->node_lock);
4920 used = btrfs_root_used(&root->root_item) + num_bytes;
4921 btrfs_set_root_used(&root->root_item, used);
4922 spin_unlock(&root->node_lock);
4923 }
Chris Mason925baed2008-06-25 16:01:30 -04004924 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004925}
Chris Mason65b51a02008-08-01 15:11:20 -04004926
4927struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4928 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004929 u64 bytenr, u32 blocksize,
4930 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004931{
4932 struct extent_buffer *buf;
4933
4934 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4935 if (!buf)
4936 return ERR_PTR(-ENOMEM);
4937 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004938 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004939 btrfs_tree_lock(buf);
4940 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004941
4942 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004943 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004944
Chris Masond0c803c2008-09-11 16:17:57 -04004945 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004946 /*
4947 * we allow two log transactions at a time, use different
4948 * EXENT bit to differentiate dirty pages.
4949 */
4950 if (root->log_transid % 2 == 0)
4951 set_extent_dirty(&root->dirty_log_pages, buf->start,
4952 buf->start + buf->len - 1, GFP_NOFS);
4953 else
4954 set_extent_new(&root->dirty_log_pages, buf->start,
4955 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004956 } else {
4957 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4958 buf->start + buf->len - 1, GFP_NOFS);
4959 }
Chris Mason65b51a02008-08-01 15:11:20 -04004960 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004961 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004962 return buf;
4963}
4964
Chris Masonfec577f2007-02-26 10:40:21 -05004965/*
4966 * helper function to allocate a block for a given tree
4967 * returns the tree buffer or NULL.
4968 */
Chris Mason5f39d392007-10-15 16:14:19 -04004969struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004970 struct btrfs_root *root, u32 blocksize,
4971 u64 parent, u64 root_objectid,
4972 struct btrfs_disk_key *key, int level,
4973 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004974{
Chris Masone2fa7222007-03-12 16:22:34 -04004975 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004976 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004977 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004978
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004979 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4980 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004981 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004982 BUG_ON(ret > 0);
4983 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004984 }
Chris Mason55c69072008-01-09 15:55:33 -05004985
Chris Mason4008c042009-02-12 14:09:45 -05004986 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4987 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004988 return buf;
4989}
Chris Masona28ec192007-03-06 20:08:01 -05004990
Yan Zheng2c47e6052009-06-27 21:07:35 -04004991struct walk_control {
4992 u64 refs[BTRFS_MAX_LEVEL];
4993 u64 flags[BTRFS_MAX_LEVEL];
4994 struct btrfs_key update_progress;
4995 int stage;
4996 int level;
4997 int shared_level;
4998 int update_ref;
4999 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005000 int reada_slot;
5001 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005002};
5003
5004#define DROP_REFERENCE 1
5005#define UPDATE_BACKREF 2
5006
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005007static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5008 struct btrfs_root *root,
5009 struct walk_control *wc,
5010 struct btrfs_path *path)
5011{
5012 u64 bytenr;
5013 u64 generation;
5014 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005015 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005016 u64 last = 0;
5017 u32 nritems;
5018 u32 blocksize;
5019 struct btrfs_key key;
5020 struct extent_buffer *eb;
5021 int ret;
5022 int slot;
5023 int nread = 0;
5024
5025 if (path->slots[wc->level] < wc->reada_slot) {
5026 wc->reada_count = wc->reada_count * 2 / 3;
5027 wc->reada_count = max(wc->reada_count, 2);
5028 } else {
5029 wc->reada_count = wc->reada_count * 3 / 2;
5030 wc->reada_count = min_t(int, wc->reada_count,
5031 BTRFS_NODEPTRS_PER_BLOCK(root));
5032 }
5033
5034 eb = path->nodes[wc->level];
5035 nritems = btrfs_header_nritems(eb);
5036 blocksize = btrfs_level_size(root, wc->level - 1);
5037
5038 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5039 if (nread >= wc->reada_count)
5040 break;
5041
5042 cond_resched();
5043 bytenr = btrfs_node_blockptr(eb, slot);
5044 generation = btrfs_node_ptr_generation(eb, slot);
5045
5046 if (slot == path->slots[wc->level])
5047 goto reada;
5048
5049 if (wc->stage == UPDATE_BACKREF &&
5050 generation <= root->root_key.offset)
5051 continue;
5052
Yan, Zheng94fcca92009-10-09 09:25:16 -04005053 /* We don't lock the tree block, it's OK to be racy here */
5054 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5055 &refs, &flags);
5056 BUG_ON(ret);
5057 BUG_ON(refs == 0);
5058
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005059 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005060 if (refs == 1)
5061 goto reada;
5062
Yan, Zheng94fcca92009-10-09 09:25:16 -04005063 if (wc->level == 1 &&
5064 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5065 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005066 if (!wc->update_ref ||
5067 generation <= root->root_key.offset)
5068 continue;
5069 btrfs_node_key_to_cpu(eb, &key, slot);
5070 ret = btrfs_comp_cpu_keys(&key,
5071 &wc->update_progress);
5072 if (ret < 0)
5073 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005074 } else {
5075 if (wc->level == 1 &&
5076 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5077 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005078 }
5079reada:
5080 ret = readahead_tree_block(root, bytenr, blocksize,
5081 generation);
5082 if (ret)
5083 break;
5084 last = bytenr + blocksize;
5085 nread++;
5086 }
5087 wc->reada_slot = slot;
5088}
5089
Chris Mason9aca1d52007-03-13 11:09:37 -04005090/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005091 * hepler to process tree block while walking down the tree.
5092 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005093 * when wc->stage == UPDATE_BACKREF, this function updates
5094 * back refs for pointers in the block.
5095 *
5096 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005097 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005098static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5099 struct btrfs_root *root,
5100 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005101 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005102{
5103 int level = wc->level;
5104 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005105 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5106 int ret;
5107
5108 if (wc->stage == UPDATE_BACKREF &&
5109 btrfs_header_owner(eb) != root->root_key.objectid)
5110 return 1;
5111
5112 /*
5113 * when reference count of tree block is 1, it won't increase
5114 * again. once full backref flag is set, we never clear it.
5115 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005116 if (lookup_info &&
5117 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5118 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005119 BUG_ON(!path->locks[level]);
5120 ret = btrfs_lookup_extent_info(trans, root,
5121 eb->start, eb->len,
5122 &wc->refs[level],
5123 &wc->flags[level]);
5124 BUG_ON(ret);
5125 BUG_ON(wc->refs[level] == 0);
5126 }
5127
Yan Zheng2c47e6052009-06-27 21:07:35 -04005128 if (wc->stage == DROP_REFERENCE) {
5129 if (wc->refs[level] > 1)
5130 return 1;
5131
5132 if (path->locks[level] && !wc->keep_locks) {
5133 btrfs_tree_unlock(eb);
5134 path->locks[level] = 0;
5135 }
5136 return 0;
5137 }
5138
5139 /* wc->stage == UPDATE_BACKREF */
5140 if (!(wc->flags[level] & flag)) {
5141 BUG_ON(!path->locks[level]);
5142 ret = btrfs_inc_ref(trans, root, eb, 1);
5143 BUG_ON(ret);
5144 ret = btrfs_dec_ref(trans, root, eb, 0);
5145 BUG_ON(ret);
5146 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5147 eb->len, flag, 0);
5148 BUG_ON(ret);
5149 wc->flags[level] |= flag;
5150 }
5151
5152 /*
5153 * the block is shared by multiple trees, so it's not good to
5154 * keep the tree lock
5155 */
5156 if (path->locks[level] && level > 0) {
5157 btrfs_tree_unlock(eb);
5158 path->locks[level] = 0;
5159 }
5160 return 0;
5161}
5162
5163/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005164 * hepler to process tree block pointer.
5165 *
5166 * when wc->stage == DROP_REFERENCE, this function checks
5167 * reference count of the block pointed to. if the block
5168 * is shared and we need update back refs for the subtree
5169 * rooted at the block, this function changes wc->stage to
5170 * UPDATE_BACKREF. if the block is shared and there is no
5171 * need to update back, this function drops the reference
5172 * to the block.
5173 *
5174 * NOTE: return value 1 means we should stop walking down.
5175 */
5176static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5177 struct btrfs_root *root,
5178 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005179 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005180{
5181 u64 bytenr;
5182 u64 generation;
5183 u64 parent;
5184 u32 blocksize;
5185 struct btrfs_key key;
5186 struct extent_buffer *next;
5187 int level = wc->level;
5188 int reada = 0;
5189 int ret = 0;
5190
5191 generation = btrfs_node_ptr_generation(path->nodes[level],
5192 path->slots[level]);
5193 /*
5194 * if the lower level block was created before the snapshot
5195 * was created, we know there is no need to update back refs
5196 * for the subtree
5197 */
5198 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005199 generation <= root->root_key.offset) {
5200 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005201 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005202 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005203
5204 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5205 blocksize = btrfs_level_size(root, level - 1);
5206
5207 next = btrfs_find_tree_block(root, bytenr, blocksize);
5208 if (!next) {
5209 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00005210 if (!next)
5211 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005212 reada = 1;
5213 }
5214 btrfs_tree_lock(next);
5215 btrfs_set_lock_blocking(next);
5216
Yan, Zheng94fcca92009-10-09 09:25:16 -04005217 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5218 &wc->refs[level - 1],
5219 &wc->flags[level - 1]);
5220 BUG_ON(ret);
5221 BUG_ON(wc->refs[level - 1] == 0);
5222 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005223
Yan, Zheng94fcca92009-10-09 09:25:16 -04005224 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005225 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005226 if (level == 1 &&
5227 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5228 goto skip;
5229
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005230 if (!wc->update_ref ||
5231 generation <= root->root_key.offset)
5232 goto skip;
5233
5234 btrfs_node_key_to_cpu(path->nodes[level], &key,
5235 path->slots[level]);
5236 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5237 if (ret < 0)
5238 goto skip;
5239
5240 wc->stage = UPDATE_BACKREF;
5241 wc->shared_level = level - 1;
5242 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005243 } else {
5244 if (level == 1 &&
5245 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5246 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005247 }
5248
5249 if (!btrfs_buffer_uptodate(next, generation)) {
5250 btrfs_tree_unlock(next);
5251 free_extent_buffer(next);
5252 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005253 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005254 }
5255
5256 if (!next) {
5257 if (reada && level == 1)
5258 reada_walk_down(trans, root, wc, path);
5259 next = read_tree_block(root, bytenr, blocksize, generation);
5260 btrfs_tree_lock(next);
5261 btrfs_set_lock_blocking(next);
5262 }
5263
5264 level--;
5265 BUG_ON(level != btrfs_header_level(next));
5266 path->nodes[level] = next;
5267 path->slots[level] = 0;
5268 path->locks[level] = 1;
5269 wc->level = level;
5270 if (wc->level == 1)
5271 wc->reada_slot = 0;
5272 return 0;
5273skip:
5274 wc->refs[level - 1] = 0;
5275 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005276 if (wc->stage == DROP_REFERENCE) {
5277 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5278 parent = path->nodes[level]->start;
5279 } else {
5280 BUG_ON(root->root_key.objectid !=
5281 btrfs_header_owner(path->nodes[level]));
5282 parent = 0;
5283 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005284
Yan, Zheng94fcca92009-10-09 09:25:16 -04005285 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5286 root->root_key.objectid, level - 1, 0);
5287 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005288 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005289 btrfs_tree_unlock(next);
5290 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04005291 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005292 return 1;
5293}
5294
5295/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005296 * hepler to process tree block while walking up the tree.
5297 *
5298 * when wc->stage == DROP_REFERENCE, this function drops
5299 * reference count on the block.
5300 *
5301 * when wc->stage == UPDATE_BACKREF, this function changes
5302 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5303 * to UPDATE_BACKREF previously while processing the block.
5304 *
5305 * NOTE: return value 1 means we should stop walking up.
5306 */
5307static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5308 struct btrfs_root *root,
5309 struct btrfs_path *path,
5310 struct walk_control *wc)
5311{
5312 int ret = 0;
5313 int level = wc->level;
5314 struct extent_buffer *eb = path->nodes[level];
5315 u64 parent = 0;
5316
5317 if (wc->stage == UPDATE_BACKREF) {
5318 BUG_ON(wc->shared_level < level);
5319 if (level < wc->shared_level)
5320 goto out;
5321
Yan Zheng2c47e6052009-06-27 21:07:35 -04005322 ret = find_next_key(path, level + 1, &wc->update_progress);
5323 if (ret > 0)
5324 wc->update_ref = 0;
5325
5326 wc->stage = DROP_REFERENCE;
5327 wc->shared_level = -1;
5328 path->slots[level] = 0;
5329
5330 /*
5331 * check reference count again if the block isn't locked.
5332 * we should start walking down the tree again if reference
5333 * count is one.
5334 */
5335 if (!path->locks[level]) {
5336 BUG_ON(level == 0);
5337 btrfs_tree_lock(eb);
5338 btrfs_set_lock_blocking(eb);
5339 path->locks[level] = 1;
5340
5341 ret = btrfs_lookup_extent_info(trans, root,
5342 eb->start, eb->len,
5343 &wc->refs[level],
5344 &wc->flags[level]);
5345 BUG_ON(ret);
5346 BUG_ON(wc->refs[level] == 0);
5347 if (wc->refs[level] == 1) {
5348 btrfs_tree_unlock(eb);
5349 path->locks[level] = 0;
5350 return 1;
5351 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005352 }
5353 }
5354
5355 /* wc->stage == DROP_REFERENCE */
5356 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5357
5358 if (wc->refs[level] == 1) {
5359 if (level == 0) {
5360 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5361 ret = btrfs_dec_ref(trans, root, eb, 1);
5362 else
5363 ret = btrfs_dec_ref(trans, root, eb, 0);
5364 BUG_ON(ret);
5365 }
5366 /* make block locked assertion in clean_tree_block happy */
5367 if (!path->locks[level] &&
5368 btrfs_header_generation(eb) == trans->transid) {
5369 btrfs_tree_lock(eb);
5370 btrfs_set_lock_blocking(eb);
5371 path->locks[level] = 1;
5372 }
5373 clean_tree_block(trans, root, eb);
5374 }
5375
5376 if (eb == root->node) {
5377 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5378 parent = eb->start;
5379 else
5380 BUG_ON(root->root_key.objectid !=
5381 btrfs_header_owner(eb));
5382 } else {
5383 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5384 parent = path->nodes[level + 1]->start;
5385 else
5386 BUG_ON(root->root_key.objectid !=
5387 btrfs_header_owner(path->nodes[level + 1]));
5388 }
5389
5390 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5391 root->root_key.objectid, level, 0);
5392 BUG_ON(ret);
5393out:
5394 wc->refs[level] = 0;
5395 wc->flags[level] = 0;
5396 return ret;
5397}
5398
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005399static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5400 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005401 struct btrfs_path *path,
5402 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005403{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005404 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005405 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005406 int ret;
5407
Yan Zheng2c47e6052009-06-27 21:07:35 -04005408 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005409 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005410 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005411 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005412
Yan Zheng2c47e6052009-06-27 21:07:35 -04005413 if (level == 0)
5414 break;
5415
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005416 if (path->slots[level] >=
5417 btrfs_header_nritems(path->nodes[level]))
5418 break;
5419
Yan, Zheng94fcca92009-10-09 09:25:16 -04005420 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005421 if (ret > 0) {
5422 path->slots[level]++;
5423 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00005424 } else if (ret < 0)
5425 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005426 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005427 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005428 return 0;
5429}
5430
Chris Masond3977122009-01-05 21:25:51 -05005431static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005432 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005433 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005434 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005435{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005436 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005437 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005438
Yan Zheng2c47e6052009-06-27 21:07:35 -04005439 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5440 while (level < max_level && path->nodes[level]) {
5441 wc->level = level;
5442 if (path->slots[level] + 1 <
5443 btrfs_header_nritems(path->nodes[level])) {
5444 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005445 return 0;
5446 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005447 ret = walk_up_proc(trans, root, path, wc);
5448 if (ret > 0)
5449 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05005450
Yan Zheng2c47e6052009-06-27 21:07:35 -04005451 if (path->locks[level]) {
5452 btrfs_tree_unlock(path->nodes[level]);
5453 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005454 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005455 free_extent_buffer(path->nodes[level]);
5456 path->nodes[level] = NULL;
5457 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005458 }
5459 }
5460 return 1;
5461}
5462
Chris Mason9aca1d52007-03-13 11:09:37 -04005463/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005464 * drop a subvolume tree.
5465 *
5466 * this function traverses the tree freeing any blocks that only
5467 * referenced by the tree.
5468 *
5469 * when a shared tree block is found. this function decreases its
5470 * reference count by one. if update_ref is true, this function
5471 * also make sure backrefs for the shared block and all lower level
5472 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04005473 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005474int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05005475{
Chris Mason5caf2a02007-04-02 11:20:42 -04005476 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005477 struct btrfs_trans_handle *trans;
5478 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005479 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005480 struct walk_control *wc;
5481 struct btrfs_key key;
5482 int err = 0;
5483 int ret;
5484 int level;
Chris Mason20524f02007-03-10 06:35:47 -05005485
Chris Mason5caf2a02007-04-02 11:20:42 -04005486 path = btrfs_alloc_path();
5487 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05005488
Yan Zheng2c47e6052009-06-27 21:07:35 -04005489 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5490 BUG_ON(!wc);
5491
5492 trans = btrfs_start_transaction(tree_root, 1);
5493
Chris Mason9f3a7422007-08-07 15:52:19 -04005494 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005495 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005496 path->nodes[level] = btrfs_lock_root_node(root);
5497 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005498 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005499 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005500 memset(&wc->update_progress, 0,
5501 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005502 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005503 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005504 memcpy(&wc->update_progress, &key,
5505 sizeof(wc->update_progress));
5506
Chris Mason6702ed42007-08-07 16:15:09 -04005507 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005508 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005509 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005510 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5511 path->lowest_level = 0;
5512 if (ret < 0) {
5513 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005514 goto out;
5515 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005516 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005517
Chris Mason7d9eb122008-07-08 14:19:17 -04005518 /*
5519 * unlock our path, this is safe because only this
5520 * function is allowed to delete this snapshot
5521 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005522 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005523
Yan Zheng2c47e6052009-06-27 21:07:35 -04005524 level = btrfs_header_level(root->node);
5525 while (1) {
5526 btrfs_tree_lock(path->nodes[level]);
5527 btrfs_set_lock_blocking(path->nodes[level]);
5528
5529 ret = btrfs_lookup_extent_info(trans, root,
5530 path->nodes[level]->start,
5531 path->nodes[level]->len,
5532 &wc->refs[level],
5533 &wc->flags[level]);
5534 BUG_ON(ret);
5535 BUG_ON(wc->refs[level] == 0);
5536
5537 if (level == root_item->drop_level)
5538 break;
5539
5540 btrfs_tree_unlock(path->nodes[level]);
5541 WARN_ON(wc->refs[level] != 1);
5542 level--;
5543 }
5544 }
5545
5546 wc->level = level;
5547 wc->shared_level = -1;
5548 wc->stage = DROP_REFERENCE;
5549 wc->update_ref = update_ref;
5550 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005551 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005552
5553 while (1) {
5554 ret = walk_down_tree(trans, root, path, wc);
5555 if (ret < 0) {
5556 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005557 break;
5558 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005559
5560 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5561 if (ret < 0) {
5562 err = ret;
5563 break;
5564 }
5565
5566 if (ret > 0) {
5567 BUG_ON(wc->stage != DROP_REFERENCE);
5568 break;
5569 }
5570
5571 if (wc->stage == DROP_REFERENCE) {
5572 level = wc->level;
5573 btrfs_node_key(path->nodes[level],
5574 &root_item->drop_progress,
5575 path->slots[level]);
5576 root_item->drop_level = level;
5577 }
5578
5579 BUG_ON(wc->level == 0);
5580 if (trans->transaction->in_commit ||
5581 trans->transaction->delayed_refs.flushing) {
5582 ret = btrfs_update_root(trans, tree_root,
5583 &root->root_key,
5584 root_item);
5585 BUG_ON(ret);
5586
5587 btrfs_end_transaction(trans, tree_root);
5588 trans = btrfs_start_transaction(tree_root, 1);
5589 } else {
5590 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005591 update = trans->delayed_ref_updates;
5592 trans->delayed_ref_updates = 0;
5593 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005594 btrfs_run_delayed_refs(trans, tree_root,
5595 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005596 }
Chris Mason20524f02007-03-10 06:35:47 -05005597 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005598 btrfs_release_path(root, path);
5599 BUG_ON(err);
5600
5601 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5602 BUG_ON(ret);
5603
Yan, Zheng76dda932009-09-21 16:00:26 -04005604 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5605 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5606 NULL, NULL);
5607 BUG_ON(ret < 0);
5608 if (ret > 0) {
5609 ret = btrfs_del_orphan_item(trans, tree_root,
5610 root->root_key.objectid);
5611 BUG_ON(ret);
5612 }
5613 }
5614
5615 if (root->in_radix) {
5616 btrfs_free_fs_root(tree_root->fs_info, root);
5617 } else {
5618 free_extent_buffer(root->node);
5619 free_extent_buffer(root->commit_root);
5620 kfree(root);
5621 }
Chris Mason9f3a7422007-08-07 15:52:19 -04005622out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005623 btrfs_end_transaction(trans, tree_root);
5624 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005625 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005626 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005627}
Chris Mason9078a3e2007-04-26 16:46:15 -04005628
Yan Zheng2c47e6052009-06-27 21:07:35 -04005629/*
5630 * drop subtree rooted at tree block 'node'.
5631 *
5632 * NOTE: this function will unlock and release tree block 'node'
5633 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005634int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5635 struct btrfs_root *root,
5636 struct extent_buffer *node,
5637 struct extent_buffer *parent)
5638{
5639 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005640 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005641 int level;
5642 int parent_level;
5643 int ret = 0;
5644 int wret;
5645
Yan Zheng2c47e6052009-06-27 21:07:35 -04005646 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5647
Yan Zhengf82d02d2008-10-29 14:49:05 -04005648 path = btrfs_alloc_path();
5649 BUG_ON(!path);
5650
Yan Zheng2c47e6052009-06-27 21:07:35 -04005651 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5652 BUG_ON(!wc);
5653
Chris Masonb9447ef2009-03-09 11:45:38 -04005654 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005655 parent_level = btrfs_header_level(parent);
5656 extent_buffer_get(parent);
5657 path->nodes[parent_level] = parent;
5658 path->slots[parent_level] = btrfs_header_nritems(parent);
5659
Chris Masonb9447ef2009-03-09 11:45:38 -04005660 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005661 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005662 path->nodes[level] = node;
5663 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005664 path->locks[level] = 1;
5665
5666 wc->refs[parent_level] = 1;
5667 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5668 wc->level = level;
5669 wc->shared_level = -1;
5670 wc->stage = DROP_REFERENCE;
5671 wc->update_ref = 0;
5672 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005673 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005674
5675 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005676 wret = walk_down_tree(trans, root, path, wc);
5677 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005678 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005679 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005680 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005681
Yan Zheng2c47e6052009-06-27 21:07:35 -04005682 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005683 if (wret < 0)
5684 ret = wret;
5685 if (wret != 0)
5686 break;
5687 }
5688
Yan Zheng2c47e6052009-06-27 21:07:35 -04005689 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005690 btrfs_free_path(path);
5691 return ret;
5692}
5693
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005694#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005695static unsigned long calc_ra(unsigned long start, unsigned long last,
5696 unsigned long nr)
5697{
5698 return min(last, start + nr - 1);
5699}
5700
Chris Masond3977122009-01-05 21:25:51 -05005701static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005702 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005703{
5704 u64 page_start;
5705 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005706 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005707 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005708 unsigned long i;
5709 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005710 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005711 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005712 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005713 unsigned int total_read = 0;
5714 unsigned int total_dirty = 0;
5715 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005716
5717 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005718
5719 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005720 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005721 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5722
Zheng Yan1a40e232008-09-26 10:09:34 -04005723 /* make sure the dirty trick played by the caller work */
5724 ret = invalidate_inode_pages2_range(inode->i_mapping,
5725 first_index, last_index);
5726 if (ret)
5727 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005728
Chris Mason4313b392008-01-03 09:08:48 -05005729 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005730
Zheng Yan1a40e232008-09-26 10:09:34 -04005731 for (i = first_index ; i <= last_index; i++) {
5732 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005733 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005734 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005735 }
5736 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005737again:
5738 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005739 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005740 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005741 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005742 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005743 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005744 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005745 if (!PageUptodate(page)) {
5746 btrfs_readpage(NULL, page);
5747 lock_page(page);
5748 if (!PageUptodate(page)) {
5749 unlock_page(page);
5750 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005751 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005752 goto out_unlock;
5753 }
5754 }
Chris Masonec44a352008-04-28 15:29:52 -04005755 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005756
Chris Masonedbd8d42007-12-21 16:27:24 -05005757 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5758 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005759 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005760
Chris Mason3eaa2882008-07-24 11:57:52 -04005761 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5762 if (ordered) {
5763 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5764 unlock_page(page);
5765 page_cache_release(page);
5766 btrfs_start_ordered_extent(inode, ordered, 1);
5767 btrfs_put_ordered_extent(ordered);
5768 goto again;
5769 }
5770 set_page_extent_mapped(page);
5771
Zheng Yan1a40e232008-09-26 10:09:34 -04005772 if (i == first_index)
5773 set_extent_bits(io_tree, page_start, page_end,
5774 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005775 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005776
Chris Masona061fc82008-05-07 11:43:44 -04005777 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005778 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005779
Chris Masond1310b22008-01-24 16:13:08 -05005780 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005781 unlock_page(page);
5782 page_cache_release(page);
5783 }
5784
5785out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005786 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005787 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005788 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005789 return ret;
5790}
5791
Chris Masond3977122009-01-05 21:25:51 -05005792static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005793 struct btrfs_key *extent_key,
5794 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005795{
Zheng Yan1a40e232008-09-26 10:09:34 -04005796 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5797 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5798 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005799 u64 start = extent_key->objectid - offset;
5800 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005801
5802 em = alloc_extent_map(GFP_NOFS);
5803 BUG_ON(!em || IS_ERR(em));
5804
Yan Zheng66435582008-10-30 14:19:50 -04005805 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005806 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005807 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005808 em->block_start = extent_key->objectid;
5809 em->bdev = root->fs_info->fs_devices->latest_bdev;
5810 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5811
5812 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005813 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005814 while (1) {
5815 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04005816 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005817 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005818 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005819 if (ret != -EEXIST) {
5820 free_extent_map(em);
5821 break;
5822 }
Yan Zheng66435582008-10-30 14:19:50 -04005823 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005824 }
Yan Zheng66435582008-10-30 14:19:50 -04005825 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005826
Yan Zheng66435582008-10-30 14:19:50 -04005827 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005828}
5829
5830struct btrfs_ref_path {
5831 u64 extent_start;
5832 u64 nodes[BTRFS_MAX_LEVEL];
5833 u64 root_objectid;
5834 u64 root_generation;
5835 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005836 u32 num_refs;
5837 int lowest_level;
5838 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005839 int shared_level;
5840
5841 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5842 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005843};
5844
5845struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005846 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005847 u64 disk_bytenr;
5848 u64 disk_num_bytes;
5849 u64 offset;
5850 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005851 u8 compression;
5852 u8 encryption;
5853 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005854};
5855
5856static int is_cowonly_root(u64 root_objectid)
5857{
5858 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5859 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5860 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5861 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005862 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5863 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005864 return 1;
5865 return 0;
5866}
5867
Chris Masond3977122009-01-05 21:25:51 -05005868static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005869 struct btrfs_root *extent_root,
5870 struct btrfs_ref_path *ref_path,
5871 int first_time)
5872{
5873 struct extent_buffer *leaf;
5874 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005875 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005876 struct btrfs_key key;
5877 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005878 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005879 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005880 int level;
5881 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005882
Zheng Yan1a40e232008-09-26 10:09:34 -04005883 path = btrfs_alloc_path();
5884 if (!path)
5885 return -ENOMEM;
5886
Zheng Yan1a40e232008-09-26 10:09:34 -04005887 if (first_time) {
5888 ref_path->lowest_level = -1;
5889 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005890 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005891 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005892 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005893walk_down:
5894 level = ref_path->current_level - 1;
5895 while (level >= -1) {
5896 u64 parent;
5897 if (level < ref_path->lowest_level)
5898 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005899
Chris Masond3977122009-01-05 21:25:51 -05005900 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005901 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005902 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005903 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005904 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005905
Zheng Yan1a40e232008-09-26 10:09:34 -04005906 parent = ref_path->nodes[level + 1];
5907 ref_path->nodes[level + 1] = 0;
5908 ref_path->current_level = level;
5909 BUG_ON(parent == 0);
5910
5911 key.objectid = bytenr;
5912 key.offset = parent + 1;
5913 key.type = BTRFS_EXTENT_REF_KEY;
5914
5915 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005916 if (ret < 0)
5917 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005918 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005919
Chris Masonedbd8d42007-12-21 16:27:24 -05005920 leaf = path->nodes[0];
5921 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005922 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005923 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005924 if (ret < 0)
5925 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005926 if (ret > 0)
5927 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005928 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005929 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005930
5931 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005932 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005933 found_key.type == BTRFS_EXTENT_REF_KEY) {
5934 if (level < ref_path->shared_level)
5935 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005936 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005937 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005938next:
5939 level--;
5940 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005941 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005942 }
5943 /* reached lowest level */
5944 ret = 1;
5945 goto out;
5946walk_up:
5947 level = ref_path->current_level;
5948 while (level < BTRFS_MAX_LEVEL - 1) {
5949 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005950
5951 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005952 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005953 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005954 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005955
Zheng Yan1a40e232008-09-26 10:09:34 -04005956 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005957
Zheng Yan1a40e232008-09-26 10:09:34 -04005958 key.objectid = bytenr;
5959 key.offset = 0;
5960 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005961
Zheng Yan1a40e232008-09-26 10:09:34 -04005962 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5963 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005964 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005965
5966 leaf = path->nodes[0];
5967 nritems = btrfs_header_nritems(leaf);
5968 if (path->slots[0] >= nritems) {
5969 ret = btrfs_next_leaf(extent_root, path);
5970 if (ret < 0)
5971 goto out;
5972 if (ret > 0) {
5973 /* the extent was freed by someone */
5974 if (ref_path->lowest_level == level)
5975 goto out;
5976 btrfs_release_path(extent_root, path);
5977 goto walk_down;
5978 }
5979 leaf = path->nodes[0];
5980 }
5981
5982 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5983 if (found_key.objectid != bytenr ||
5984 found_key.type != BTRFS_EXTENT_REF_KEY) {
5985 /* the extent was freed by someone */
5986 if (ref_path->lowest_level == level) {
5987 ret = 1;
5988 goto out;
5989 }
5990 btrfs_release_path(extent_root, path);
5991 goto walk_down;
5992 }
5993found:
5994 ref = btrfs_item_ptr(leaf, path->slots[0],
5995 struct btrfs_extent_ref);
5996 ref_objectid = btrfs_ref_objectid(leaf, ref);
5997 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5998 if (first_time) {
5999 level = (int)ref_objectid;
6000 BUG_ON(level >= BTRFS_MAX_LEVEL);
6001 ref_path->lowest_level = level;
6002 ref_path->current_level = level;
6003 ref_path->nodes[level] = bytenr;
6004 } else {
6005 WARN_ON(ref_objectid != level);
6006 }
6007 } else {
6008 WARN_ON(level != -1);
6009 }
6010 first_time = 0;
6011
6012 if (ref_path->lowest_level == level) {
6013 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006014 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6015 }
6016
6017 /*
6018 * the block is tree root or the block isn't in reference
6019 * counted tree.
6020 */
6021 if (found_key.objectid == found_key.offset ||
6022 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6023 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6024 ref_path->root_generation =
6025 btrfs_ref_generation(leaf, ref);
6026 if (level < 0) {
6027 /* special reference from the tree log */
6028 ref_path->nodes[0] = found_key.offset;
6029 ref_path->current_level = 0;
6030 }
6031 ret = 0;
6032 goto out;
6033 }
6034
6035 level++;
6036 BUG_ON(ref_path->nodes[level] != 0);
6037 ref_path->nodes[level] = found_key.offset;
6038 ref_path->current_level = level;
6039
6040 /*
6041 * the reference was created in the running transaction,
6042 * no need to continue walking up.
6043 */
6044 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6045 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6046 ref_path->root_generation =
6047 btrfs_ref_generation(leaf, ref);
6048 ret = 0;
6049 goto out;
6050 }
6051
6052 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006053 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006054 }
6055 /* reached max tree level, but no tree root found. */
6056 BUG();
6057out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006058 btrfs_free_path(path);
6059 return ret;
6060}
6061
6062static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6063 struct btrfs_root *extent_root,
6064 struct btrfs_ref_path *ref_path,
6065 u64 extent_start)
6066{
6067 memset(ref_path, 0, sizeof(*ref_path));
6068 ref_path->extent_start = extent_start;
6069
6070 return __next_ref_path(trans, extent_root, ref_path, 1);
6071}
6072
6073static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6074 struct btrfs_root *extent_root,
6075 struct btrfs_ref_path *ref_path)
6076{
6077 return __next_ref_path(trans, extent_root, ref_path, 0);
6078}
6079
Chris Masond3977122009-01-05 21:25:51 -05006080static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006081 struct btrfs_key *extent_key,
6082 u64 offset, int no_fragment,
6083 struct disk_extent **extents,
6084 int *nr_extents)
6085{
6086 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6087 struct btrfs_path *path;
6088 struct btrfs_file_extent_item *fi;
6089 struct extent_buffer *leaf;
6090 struct disk_extent *exts = *extents;
6091 struct btrfs_key found_key;
6092 u64 cur_pos;
6093 u64 last_byte;
6094 u32 nritems;
6095 int nr = 0;
6096 int max = *nr_extents;
6097 int ret;
6098
6099 WARN_ON(!no_fragment && *extents);
6100 if (!exts) {
6101 max = 1;
6102 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6103 if (!exts)
6104 return -ENOMEM;
6105 }
6106
6107 path = btrfs_alloc_path();
6108 BUG_ON(!path);
6109
6110 cur_pos = extent_key->objectid - offset;
6111 last_byte = extent_key->objectid + extent_key->offset;
6112 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6113 cur_pos, 0);
6114 if (ret < 0)
6115 goto out;
6116 if (ret > 0) {
6117 ret = -ENOENT;
6118 goto out;
6119 }
6120
6121 while (1) {
6122 leaf = path->nodes[0];
6123 nritems = btrfs_header_nritems(leaf);
6124 if (path->slots[0] >= nritems) {
6125 ret = btrfs_next_leaf(root, path);
6126 if (ret < 0)
6127 goto out;
6128 if (ret > 0)
6129 break;
6130 leaf = path->nodes[0];
6131 }
6132
6133 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6134 if (found_key.offset != cur_pos ||
6135 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6136 found_key.objectid != reloc_inode->i_ino)
6137 break;
6138
6139 fi = btrfs_item_ptr(leaf, path->slots[0],
6140 struct btrfs_file_extent_item);
6141 if (btrfs_file_extent_type(leaf, fi) !=
6142 BTRFS_FILE_EXTENT_REG ||
6143 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6144 break;
6145
6146 if (nr == max) {
6147 struct disk_extent *old = exts;
6148 max *= 2;
6149 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6150 memcpy(exts, old, sizeof(*exts) * nr);
6151 if (old != *extents)
6152 kfree(old);
6153 }
6154
6155 exts[nr].disk_bytenr =
6156 btrfs_file_extent_disk_bytenr(leaf, fi);
6157 exts[nr].disk_num_bytes =
6158 btrfs_file_extent_disk_num_bytes(leaf, fi);
6159 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6160 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04006161 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6162 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6163 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6164 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6165 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04006166 BUG_ON(exts[nr].offset > 0);
6167 BUG_ON(exts[nr].compression || exts[nr].encryption);
6168 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006169
6170 cur_pos += exts[nr].num_bytes;
6171 nr++;
6172
6173 if (cur_pos + offset >= last_byte)
6174 break;
6175
6176 if (no_fragment) {
6177 ret = 1;
6178 goto out;
6179 }
6180 path->slots[0]++;
6181 }
6182
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006183 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04006184 if (cur_pos + offset < last_byte) {
6185 ret = -ENOENT;
6186 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05006187 }
6188 ret = 0;
6189out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006190 btrfs_free_path(path);
6191 if (ret) {
6192 if (exts != *extents)
6193 kfree(exts);
6194 } else {
6195 *extents = exts;
6196 *nr_extents = nr;
6197 }
6198 return ret;
6199}
6200
Chris Masond3977122009-01-05 21:25:51 -05006201static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006202 struct btrfs_root *root,
6203 struct btrfs_path *path,
6204 struct btrfs_key *extent_key,
6205 struct btrfs_key *leaf_key,
6206 struct btrfs_ref_path *ref_path,
6207 struct disk_extent *new_extents,
6208 int nr_extents)
6209{
6210 struct extent_buffer *leaf;
6211 struct btrfs_file_extent_item *fi;
6212 struct inode *inode = NULL;
6213 struct btrfs_key key;
6214 u64 lock_start = 0;
6215 u64 lock_end = 0;
6216 u64 num_bytes;
6217 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05006218 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006219 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006220 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006221 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04006222 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04006223 int ret;
6224
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006225 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04006226 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006227 if (key.objectid < ref_path->owner_objectid ||
6228 (key.objectid == ref_path->owner_objectid &&
6229 key.type < BTRFS_EXTENT_DATA_KEY)) {
6230 key.objectid = ref_path->owner_objectid;
6231 key.type = BTRFS_EXTENT_DATA_KEY;
6232 key.offset = 0;
6233 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006234 }
6235
6236 while (1) {
6237 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6238 if (ret < 0)
6239 goto out;
6240
6241 leaf = path->nodes[0];
6242 nritems = btrfs_header_nritems(leaf);
6243next:
6244 if (extent_locked && ret > 0) {
6245 /*
6246 * the file extent item was modified by someone
6247 * before the extent got locked.
6248 */
Zheng Yan1a40e232008-09-26 10:09:34 -04006249 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6250 lock_end, GFP_NOFS);
6251 extent_locked = 0;
6252 }
6253
6254 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006255 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04006256 break;
6257
6258 BUG_ON(extent_locked);
6259 ret = btrfs_next_leaf(root, path);
6260 if (ret < 0)
6261 goto out;
6262 if (ret > 0)
6263 break;
6264 leaf = path->nodes[0];
6265 nritems = btrfs_header_nritems(leaf);
6266 }
6267
6268 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6269
6270 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6271 if ((key.objectid > ref_path->owner_objectid) ||
6272 (key.objectid == ref_path->owner_objectid &&
6273 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05006274 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006275 break;
6276 }
6277
6278 if (inode && key.objectid != inode->i_ino) {
6279 BUG_ON(extent_locked);
6280 btrfs_release_path(root, path);
6281 mutex_unlock(&inode->i_mutex);
6282 iput(inode);
6283 inode = NULL;
6284 continue;
6285 }
6286
6287 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6288 path->slots[0]++;
6289 ret = 1;
6290 goto next;
6291 }
6292 fi = btrfs_item_ptr(leaf, path->slots[0],
6293 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04006294 extent_type = btrfs_file_extent_type(leaf, fi);
6295 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6296 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04006297 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6298 extent_key->objectid)) {
6299 path->slots[0]++;
6300 ret = 1;
6301 goto next;
6302 }
6303
6304 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6305 ext_offset = btrfs_file_extent_offset(leaf, fi);
6306
Yan Zheng86288a12009-01-21 10:49:16 -05006307 if (search_end == (u64)-1) {
6308 search_end = key.offset - ext_offset +
6309 btrfs_file_extent_ram_bytes(leaf, fi);
6310 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006311
6312 if (!extent_locked) {
6313 lock_start = key.offset;
6314 lock_end = lock_start + num_bytes - 1;
6315 } else {
Yan Zheng66435582008-10-30 14:19:50 -04006316 if (lock_start > key.offset ||
6317 lock_end + 1 < key.offset + num_bytes) {
6318 unlock_extent(&BTRFS_I(inode)->io_tree,
6319 lock_start, lock_end, GFP_NOFS);
6320 extent_locked = 0;
6321 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006322 }
6323
6324 if (!inode) {
6325 btrfs_release_path(root, path);
6326
6327 inode = btrfs_iget_locked(root->fs_info->sb,
6328 key.objectid, root);
6329 if (inode->i_state & I_NEW) {
6330 BTRFS_I(inode)->root = root;
6331 BTRFS_I(inode)->location.objectid =
6332 key.objectid;
6333 BTRFS_I(inode)->location.type =
6334 BTRFS_INODE_ITEM_KEY;
6335 BTRFS_I(inode)->location.offset = 0;
6336 btrfs_read_locked_inode(inode);
6337 unlock_new_inode(inode);
6338 }
6339 /*
6340 * some code call btrfs_commit_transaction while
6341 * holding the i_mutex, so we can't use mutex_lock
6342 * here.
6343 */
6344 if (is_bad_inode(inode) ||
6345 !mutex_trylock(&inode->i_mutex)) {
6346 iput(inode);
6347 inode = NULL;
6348 key.offset = (u64)-1;
6349 goto skip;
6350 }
6351 }
6352
6353 if (!extent_locked) {
6354 struct btrfs_ordered_extent *ordered;
6355
6356 btrfs_release_path(root, path);
6357
6358 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6359 lock_end, GFP_NOFS);
6360 ordered = btrfs_lookup_first_ordered_extent(inode,
6361 lock_end);
6362 if (ordered &&
6363 ordered->file_offset <= lock_end &&
6364 ordered->file_offset + ordered->len > lock_start) {
6365 unlock_extent(&BTRFS_I(inode)->io_tree,
6366 lock_start, lock_end, GFP_NOFS);
6367 btrfs_start_ordered_extent(inode, ordered, 1);
6368 btrfs_put_ordered_extent(ordered);
6369 key.offset += num_bytes;
6370 goto skip;
6371 }
6372 if (ordered)
6373 btrfs_put_ordered_extent(ordered);
6374
Zheng Yan1a40e232008-09-26 10:09:34 -04006375 extent_locked = 1;
6376 continue;
6377 }
6378
6379 if (nr_extents == 1) {
6380 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04006381 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6382 new_extents[0].disk_bytenr);
6383 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6384 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006385 btrfs_mark_buffer_dirty(leaf);
6386
6387 btrfs_drop_extent_cache(inode, key.offset,
6388 key.offset + num_bytes - 1, 0);
6389
6390 ret = btrfs_inc_extent_ref(trans, root,
6391 new_extents[0].disk_bytenr,
6392 new_extents[0].disk_num_bytes,
6393 leaf->start,
6394 root->root_key.objectid,
6395 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006396 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006397 BUG_ON(ret);
6398
6399 ret = btrfs_free_extent(trans, root,
6400 extent_key->objectid,
6401 extent_key->offset,
6402 leaf->start,
6403 btrfs_header_owner(leaf),
6404 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006405 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006406 BUG_ON(ret);
6407
6408 btrfs_release_path(root, path);
6409 key.offset += num_bytes;
6410 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04006411 BUG_ON(1);
6412#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006413 u64 alloc_hint;
6414 u64 extent_len;
6415 int i;
6416 /*
6417 * drop old extent pointer at first, then insert the
6418 * new pointers one bye one
6419 */
6420 btrfs_release_path(root, path);
6421 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6422 key.offset + num_bytes,
6423 key.offset, &alloc_hint);
6424 BUG_ON(ret);
6425
6426 for (i = 0; i < nr_extents; i++) {
6427 if (ext_offset >= new_extents[i].num_bytes) {
6428 ext_offset -= new_extents[i].num_bytes;
6429 continue;
6430 }
6431 extent_len = min(new_extents[i].num_bytes -
6432 ext_offset, num_bytes);
6433
6434 ret = btrfs_insert_empty_item(trans, root,
6435 path, &key,
6436 sizeof(*fi));
6437 BUG_ON(ret);
6438
6439 leaf = path->nodes[0];
6440 fi = btrfs_item_ptr(leaf, path->slots[0],
6441 struct btrfs_file_extent_item);
6442 btrfs_set_file_extent_generation(leaf, fi,
6443 trans->transid);
6444 btrfs_set_file_extent_type(leaf, fi,
6445 BTRFS_FILE_EXTENT_REG);
6446 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6447 new_extents[i].disk_bytenr);
6448 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6449 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04006450 btrfs_set_file_extent_ram_bytes(leaf, fi,
6451 new_extents[i].ram_bytes);
6452
6453 btrfs_set_file_extent_compression(leaf, fi,
6454 new_extents[i].compression);
6455 btrfs_set_file_extent_encryption(leaf, fi,
6456 new_extents[i].encryption);
6457 btrfs_set_file_extent_other_encoding(leaf, fi,
6458 new_extents[i].other_encoding);
6459
Zheng Yan1a40e232008-09-26 10:09:34 -04006460 btrfs_set_file_extent_num_bytes(leaf, fi,
6461 extent_len);
6462 ext_offset += new_extents[i].offset;
6463 btrfs_set_file_extent_offset(leaf, fi,
6464 ext_offset);
6465 btrfs_mark_buffer_dirty(leaf);
6466
6467 btrfs_drop_extent_cache(inode, key.offset,
6468 key.offset + extent_len - 1, 0);
6469
6470 ret = btrfs_inc_extent_ref(trans, root,
6471 new_extents[i].disk_bytenr,
6472 new_extents[i].disk_num_bytes,
6473 leaf->start,
6474 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006475 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006476 BUG_ON(ret);
6477 btrfs_release_path(root, path);
6478
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006479 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04006480
6481 ext_offset = 0;
6482 num_bytes -= extent_len;
6483 key.offset += extent_len;
6484
6485 if (num_bytes == 0)
6486 break;
6487 }
6488 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006489#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04006490 }
6491
6492 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006493 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6494 lock_end, GFP_NOFS);
6495 extent_locked = 0;
6496 }
6497skip:
6498 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05006499 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006500 break;
6501
6502 cond_resched();
6503 }
6504 ret = 0;
6505out:
6506 btrfs_release_path(root, path);
6507 if (inode) {
6508 mutex_unlock(&inode->i_mutex);
6509 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006510 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6511 lock_end, GFP_NOFS);
6512 }
6513 iput(inode);
6514 }
6515 return ret;
6516}
6517
Zheng Yan1a40e232008-09-26 10:09:34 -04006518int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6519 struct btrfs_root *root,
6520 struct extent_buffer *buf, u64 orig_start)
6521{
6522 int level;
6523 int ret;
6524
6525 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6526 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6527
6528 level = btrfs_header_level(buf);
6529 if (level == 0) {
6530 struct btrfs_leaf_ref *ref;
6531 struct btrfs_leaf_ref *orig_ref;
6532
6533 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6534 if (!orig_ref)
6535 return -ENOENT;
6536
6537 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6538 if (!ref) {
6539 btrfs_free_leaf_ref(root, orig_ref);
6540 return -ENOMEM;
6541 }
6542
6543 ref->nritems = orig_ref->nritems;
6544 memcpy(ref->extents, orig_ref->extents,
6545 sizeof(ref->extents[0]) * ref->nritems);
6546
6547 btrfs_free_leaf_ref(root, orig_ref);
6548
6549 ref->root_gen = trans->transid;
6550 ref->bytenr = buf->start;
6551 ref->owner = btrfs_header_owner(buf);
6552 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006553
Zheng Yan1a40e232008-09-26 10:09:34 -04006554 ret = btrfs_add_leaf_ref(root, ref, 0);
6555 WARN_ON(ret);
6556 btrfs_free_leaf_ref(root, ref);
6557 }
6558 return 0;
6559}
6560
Chris Masond3977122009-01-05 21:25:51 -05006561static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006562 struct extent_buffer *leaf,
6563 struct btrfs_block_group_cache *group,
6564 struct btrfs_root *target_root)
6565{
6566 struct btrfs_key key;
6567 struct inode *inode = NULL;
6568 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006569 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04006570 u64 num_bytes;
6571 u64 skip_objectid = 0;
6572 u32 nritems;
6573 u32 i;
6574
6575 nritems = btrfs_header_nritems(leaf);
6576 for (i = 0; i < nritems; i++) {
6577 btrfs_item_key_to_cpu(leaf, &key, i);
6578 if (key.objectid == skip_objectid ||
6579 key.type != BTRFS_EXTENT_DATA_KEY)
6580 continue;
6581 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6582 if (btrfs_file_extent_type(leaf, fi) ==
6583 BTRFS_FILE_EXTENT_INLINE)
6584 continue;
6585 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6586 continue;
6587 if (!inode || inode->i_ino != key.objectid) {
6588 iput(inode);
6589 inode = btrfs_ilookup(target_root->fs_info->sb,
6590 key.objectid, target_root, 1);
6591 }
6592 if (!inode) {
6593 skip_objectid = key.objectid;
6594 continue;
6595 }
6596 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6597
Josef Bacik2ac55d42010-02-03 19:33:23 +00006598 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
6599 key.offset + num_bytes - 1, 0, &cached_state,
6600 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006601 btrfs_drop_extent_cache(inode, key.offset,
6602 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006603 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
6604 key.offset + num_bytes - 1, &cached_state,
6605 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006606 cond_resched();
6607 }
6608 iput(inode);
6609 return 0;
6610}
6611
Chris Masond3977122009-01-05 21:25:51 -05006612static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006613 struct btrfs_root *root,
6614 struct extent_buffer *leaf,
6615 struct btrfs_block_group_cache *group,
6616 struct inode *reloc_inode)
6617{
6618 struct btrfs_key key;
6619 struct btrfs_key extent_key;
6620 struct btrfs_file_extent_item *fi;
6621 struct btrfs_leaf_ref *ref;
6622 struct disk_extent *new_extent;
6623 u64 bytenr;
6624 u64 num_bytes;
6625 u32 nritems;
6626 u32 i;
6627 int ext_index;
6628 int nr_extent;
6629 int ret;
6630
6631 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6632 BUG_ON(!new_extent);
6633
6634 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6635 BUG_ON(!ref);
6636
6637 ext_index = -1;
6638 nritems = btrfs_header_nritems(leaf);
6639 for (i = 0; i < nritems; i++) {
6640 btrfs_item_key_to_cpu(leaf, &key, i);
6641 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6642 continue;
6643 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6644 if (btrfs_file_extent_type(leaf, fi) ==
6645 BTRFS_FILE_EXTENT_INLINE)
6646 continue;
6647 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6648 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6649 if (bytenr == 0)
6650 continue;
6651
6652 ext_index++;
6653 if (bytenr >= group->key.objectid + group->key.offset ||
6654 bytenr + num_bytes <= group->key.objectid)
6655 continue;
6656
6657 extent_key.objectid = bytenr;
6658 extent_key.offset = num_bytes;
6659 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6660 nr_extent = 1;
6661 ret = get_new_locations(reloc_inode, &extent_key,
6662 group->key.objectid, 1,
6663 &new_extent, &nr_extent);
6664 if (ret > 0)
6665 continue;
6666 BUG_ON(ret < 0);
6667
6668 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6669 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6670 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6671 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6672
Zheng Yan1a40e232008-09-26 10:09:34 -04006673 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6674 new_extent->disk_bytenr);
6675 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6676 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006677 btrfs_mark_buffer_dirty(leaf);
6678
6679 ret = btrfs_inc_extent_ref(trans, root,
6680 new_extent->disk_bytenr,
6681 new_extent->disk_num_bytes,
6682 leaf->start,
6683 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006684 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006685 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006686
Zheng Yan1a40e232008-09-26 10:09:34 -04006687 ret = btrfs_free_extent(trans, root,
6688 bytenr, num_bytes, leaf->start,
6689 btrfs_header_owner(leaf),
6690 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006691 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006692 BUG_ON(ret);
6693 cond_resched();
6694 }
6695 kfree(new_extent);
6696 BUG_ON(ext_index + 1 != ref->nritems);
6697 btrfs_free_leaf_ref(root, ref);
6698 return 0;
6699}
6700
Yan Zhengf82d02d2008-10-29 14:49:05 -04006701int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6702 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006703{
6704 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006705 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006706
6707 if (root->reloc_root) {
6708 reloc_root = root->reloc_root;
6709 root->reloc_root = NULL;
6710 list_add(&reloc_root->dead_list,
6711 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006712
6713 btrfs_set_root_bytenr(&reloc_root->root_item,
6714 reloc_root->node->start);
6715 btrfs_set_root_level(&root->root_item,
6716 btrfs_header_level(reloc_root->node));
6717 memset(&reloc_root->root_item.drop_progress, 0,
6718 sizeof(struct btrfs_disk_key));
6719 reloc_root->root_item.drop_level = 0;
6720
6721 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6722 &reloc_root->root_key,
6723 &reloc_root->root_item);
6724 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006725 }
6726 return 0;
6727}
6728
6729int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6730{
6731 struct btrfs_trans_handle *trans;
6732 struct btrfs_root *reloc_root;
6733 struct btrfs_root *prev_root = NULL;
6734 struct list_head dead_roots;
6735 int ret;
6736 unsigned long nr;
6737
6738 INIT_LIST_HEAD(&dead_roots);
6739 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6740
6741 while (!list_empty(&dead_roots)) {
6742 reloc_root = list_entry(dead_roots.prev,
6743 struct btrfs_root, dead_list);
6744 list_del_init(&reloc_root->dead_list);
6745
6746 BUG_ON(reloc_root->commit_root != NULL);
6747 while (1) {
6748 trans = btrfs_join_transaction(root, 1);
6749 BUG_ON(!trans);
6750
6751 mutex_lock(&root->fs_info->drop_mutex);
6752 ret = btrfs_drop_snapshot(trans, reloc_root);
6753 if (ret != -EAGAIN)
6754 break;
6755 mutex_unlock(&root->fs_info->drop_mutex);
6756
6757 nr = trans->blocks_used;
6758 ret = btrfs_end_transaction(trans, root);
6759 BUG_ON(ret);
6760 btrfs_btree_balance_dirty(root, nr);
6761 }
6762
6763 free_extent_buffer(reloc_root->node);
6764
6765 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6766 &reloc_root->root_key);
6767 BUG_ON(ret);
6768 mutex_unlock(&root->fs_info->drop_mutex);
6769
6770 nr = trans->blocks_used;
6771 ret = btrfs_end_transaction(trans, root);
6772 BUG_ON(ret);
6773 btrfs_btree_balance_dirty(root, nr);
6774
6775 kfree(prev_root);
6776 prev_root = reloc_root;
6777 }
6778 if (prev_root) {
6779 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6780 kfree(prev_root);
6781 }
6782 return 0;
6783}
6784
6785int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6786{
6787 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6788 return 0;
6789}
6790
6791int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6792{
6793 struct btrfs_root *reloc_root;
6794 struct btrfs_trans_handle *trans;
6795 struct btrfs_key location;
6796 int found;
6797 int ret;
6798
6799 mutex_lock(&root->fs_info->tree_reloc_mutex);
6800 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6801 BUG_ON(ret);
6802 found = !list_empty(&root->fs_info->dead_reloc_roots);
6803 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6804
6805 if (found) {
6806 trans = btrfs_start_transaction(root, 1);
6807 BUG_ON(!trans);
6808 ret = btrfs_commit_transaction(trans, root);
6809 BUG_ON(ret);
6810 }
6811
6812 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6813 location.offset = (u64)-1;
6814 location.type = BTRFS_ROOT_ITEM_KEY;
6815
6816 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6817 BUG_ON(!reloc_root);
6818 btrfs_orphan_cleanup(reloc_root);
6819 return 0;
6820}
6821
Chris Masond3977122009-01-05 21:25:51 -05006822static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006823 struct btrfs_root *root)
6824{
6825 struct btrfs_root *reloc_root;
6826 struct extent_buffer *eb;
6827 struct btrfs_root_item *root_item;
6828 struct btrfs_key root_key;
6829 int ret;
6830
6831 BUG_ON(!root->ref_cows);
6832 if (root->reloc_root)
6833 return 0;
6834
6835 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6836 BUG_ON(!root_item);
6837
6838 ret = btrfs_copy_root(trans, root, root->commit_root,
6839 &eb, BTRFS_TREE_RELOC_OBJECTID);
6840 BUG_ON(ret);
6841
6842 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6843 root_key.offset = root->root_key.objectid;
6844 root_key.type = BTRFS_ROOT_ITEM_KEY;
6845
6846 memcpy(root_item, &root->root_item, sizeof(root_item));
6847 btrfs_set_root_refs(root_item, 0);
6848 btrfs_set_root_bytenr(root_item, eb->start);
6849 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006850 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006851
6852 btrfs_tree_unlock(eb);
6853 free_extent_buffer(eb);
6854
6855 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6856 &root_key, root_item);
6857 BUG_ON(ret);
6858 kfree(root_item);
6859
6860 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6861 &root_key);
6862 BUG_ON(!reloc_root);
6863 reloc_root->last_trans = trans->transid;
6864 reloc_root->commit_root = NULL;
6865 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6866
6867 root->reloc_root = reloc_root;
6868 return 0;
6869}
6870
6871/*
6872 * Core function of space balance.
6873 *
6874 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006875 * counted roots. There is one reloc tree for each subvol, and all
6876 * reloc trees share same root key objectid. Reloc trees are snapshots
6877 * of the latest committed roots of subvols (root->commit_root).
6878 *
6879 * To relocate a tree block referenced by a subvol, there are two steps.
6880 * COW the block through subvol's reloc tree, then update block pointer
6881 * in the subvol to point to the new block. Since all reloc trees share
6882 * same root key objectid, doing special handing for tree blocks owned
6883 * by them is easy. Once a tree block has been COWed in one reloc tree,
6884 * we can use the resulting new block directly when the same block is
6885 * required to COW again through other reloc trees. By this way, relocated
6886 * tree blocks are shared between reloc trees, so they are also shared
6887 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006888 */
Chris Masond3977122009-01-05 21:25:51 -05006889static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006890 struct btrfs_root *root,
6891 struct btrfs_path *path,
6892 struct btrfs_key *first_key,
6893 struct btrfs_ref_path *ref_path,
6894 struct btrfs_block_group_cache *group,
6895 struct inode *reloc_inode)
6896{
6897 struct btrfs_root *reloc_root;
6898 struct extent_buffer *eb = NULL;
6899 struct btrfs_key *keys;
6900 u64 *nodes;
6901 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006902 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006903 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006904 int ret;
6905
6906 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6907 lowest_level = ref_path->owner_objectid;
6908
Yan Zhengf82d02d2008-10-29 14:49:05 -04006909 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006910 path->lowest_level = lowest_level;
6911 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6912 BUG_ON(ret < 0);
6913 path->lowest_level = 0;
6914 btrfs_release_path(root, path);
6915 return 0;
6916 }
6917
Zheng Yan1a40e232008-09-26 10:09:34 -04006918 mutex_lock(&root->fs_info->tree_reloc_mutex);
6919 ret = init_reloc_tree(trans, root);
6920 BUG_ON(ret);
6921 reloc_root = root->reloc_root;
6922
Yan Zhengf82d02d2008-10-29 14:49:05 -04006923 shared_level = ref_path->shared_level;
6924 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006925
Yan Zhengf82d02d2008-10-29 14:49:05 -04006926 keys = ref_path->node_keys;
6927 nodes = ref_path->new_nodes;
6928 memset(&keys[shared_level + 1], 0,
6929 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6930 memset(&nodes[shared_level + 1], 0,
6931 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006932
Yan Zhengf82d02d2008-10-29 14:49:05 -04006933 if (nodes[lowest_level] == 0) {
6934 path->lowest_level = lowest_level;
6935 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6936 0, 1);
6937 BUG_ON(ret);
6938 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6939 eb = path->nodes[level];
6940 if (!eb || eb == reloc_root->node)
6941 break;
6942 nodes[level] = eb->start;
6943 if (level == 0)
6944 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6945 else
6946 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6947 }
Yan Zheng2b820322008-11-17 21:11:30 -05006948 if (nodes[0] &&
6949 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006950 eb = path->nodes[0];
6951 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6952 group, reloc_inode);
6953 BUG_ON(ret);
6954 }
6955 btrfs_release_path(reloc_root, path);
6956 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006957 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006958 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006959 BUG_ON(ret);
6960 }
6961
Zheng Yan1a40e232008-09-26 10:09:34 -04006962 /*
6963 * replace tree blocks in the fs tree with tree blocks in
6964 * the reloc tree.
6965 */
6966 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6967 BUG_ON(ret < 0);
6968
6969 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006970 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6971 0, 0);
6972 BUG_ON(ret);
6973 extent_buffer_get(path->nodes[0]);
6974 eb = path->nodes[0];
6975 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006976 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6977 BUG_ON(ret);
6978 free_extent_buffer(eb);
6979 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006980
Yan Zhengf82d02d2008-10-29 14:49:05 -04006981 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006982 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006983 return 0;
6984}
6985
Chris Masond3977122009-01-05 21:25:51 -05006986static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006987 struct btrfs_root *root,
6988 struct btrfs_path *path,
6989 struct btrfs_key *first_key,
6990 struct btrfs_ref_path *ref_path)
6991{
6992 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006993
6994 ret = relocate_one_path(trans, root, path, first_key,
6995 ref_path, NULL, NULL);
6996 BUG_ON(ret);
6997
Zheng Yan1a40e232008-09-26 10:09:34 -04006998 return 0;
6999}
7000
Chris Masond3977122009-01-05 21:25:51 -05007001static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007002 struct btrfs_root *extent_root,
7003 struct btrfs_path *path,
7004 struct btrfs_key *extent_key)
7005{
7006 int ret;
7007
Zheng Yan1a40e232008-09-26 10:09:34 -04007008 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7009 if (ret)
7010 goto out;
7011 ret = btrfs_del_item(trans, extent_root, path);
7012out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007013 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007014 return ret;
7015}
7016
Chris Masond3977122009-01-05 21:25:51 -05007017static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007018 struct btrfs_ref_path *ref_path)
7019{
7020 struct btrfs_key root_key;
7021
7022 root_key.objectid = ref_path->root_objectid;
7023 root_key.type = BTRFS_ROOT_ITEM_KEY;
7024 if (is_cowonly_root(ref_path->root_objectid))
7025 root_key.offset = 0;
7026 else
7027 root_key.offset = (u64)-1;
7028
7029 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7030}
7031
Chris Masond3977122009-01-05 21:25:51 -05007032static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007033 struct btrfs_path *path,
7034 struct btrfs_key *extent_key,
7035 struct btrfs_block_group_cache *group,
7036 struct inode *reloc_inode, int pass)
7037{
7038 struct btrfs_trans_handle *trans;
7039 struct btrfs_root *found_root;
7040 struct btrfs_ref_path *ref_path = NULL;
7041 struct disk_extent *new_extents = NULL;
7042 int nr_extents = 0;
7043 int loops;
7044 int ret;
7045 int level;
7046 struct btrfs_key first_key;
7047 u64 prev_block = 0;
7048
Zheng Yan1a40e232008-09-26 10:09:34 -04007049
7050 trans = btrfs_start_transaction(extent_root, 1);
7051 BUG_ON(!trans);
7052
7053 if (extent_key->objectid == 0) {
7054 ret = del_extent_zero(trans, extent_root, path, extent_key);
7055 goto out;
7056 }
7057
7058 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7059 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007060 ret = -ENOMEM;
7061 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007062 }
7063
7064 for (loops = 0; ; loops++) {
7065 if (loops == 0) {
7066 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7067 extent_key->objectid);
7068 } else {
7069 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7070 }
7071 if (ret < 0)
7072 goto out;
7073 if (ret > 0)
7074 break;
7075
7076 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7077 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7078 continue;
7079
7080 found_root = read_ref_root(extent_root->fs_info, ref_path);
7081 BUG_ON(!found_root);
7082 /*
7083 * for reference counted tree, only process reference paths
7084 * rooted at the latest committed root.
7085 */
7086 if (found_root->ref_cows &&
7087 ref_path->root_generation != found_root->root_key.offset)
7088 continue;
7089
7090 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7091 if (pass == 0) {
7092 /*
7093 * copy data extents to new locations
7094 */
7095 u64 group_start = group->key.objectid;
7096 ret = relocate_data_extent(reloc_inode,
7097 extent_key,
7098 group_start);
7099 if (ret < 0)
7100 goto out;
7101 break;
7102 }
7103 level = 0;
7104 } else {
7105 level = ref_path->owner_objectid;
7106 }
7107
7108 if (prev_block != ref_path->nodes[level]) {
7109 struct extent_buffer *eb;
7110 u64 block_start = ref_path->nodes[level];
7111 u64 block_size = btrfs_level_size(found_root, level);
7112
7113 eb = read_tree_block(found_root, block_start,
7114 block_size, 0);
7115 btrfs_tree_lock(eb);
7116 BUG_ON(level != btrfs_header_level(eb));
7117
7118 if (level == 0)
7119 btrfs_item_key_to_cpu(eb, &first_key, 0);
7120 else
7121 btrfs_node_key_to_cpu(eb, &first_key, 0);
7122
7123 btrfs_tree_unlock(eb);
7124 free_extent_buffer(eb);
7125 prev_block = block_start;
7126 }
7127
Yan Zheng24562422009-02-12 14:14:53 -05007128 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007129 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05007130 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007131 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7132 /*
7133 * try to update data extent references while
7134 * keeping metadata shared between snapshots.
7135 */
7136 if (pass == 1) {
7137 ret = relocate_one_path(trans, found_root,
7138 path, &first_key, ref_path,
7139 group, reloc_inode);
7140 if (ret < 0)
7141 goto out;
7142 continue;
7143 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007144 /*
7145 * use fallback method to process the remaining
7146 * references.
7147 */
7148 if (!new_extents) {
7149 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04007150 new_extents = kmalloc(sizeof(*new_extents),
7151 GFP_NOFS);
7152 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007153 ret = get_new_locations(reloc_inode,
7154 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04007155 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04007156 &new_extents,
7157 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007158 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04007159 goto out;
7160 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007161 ret = replace_one_extent(trans, found_root,
7162 path, extent_key,
7163 &first_key, ref_path,
7164 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05007165 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007166 ret = relocate_tree_block(trans, found_root, path,
7167 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007168 }
7169 if (ret < 0)
7170 goto out;
7171 }
7172 ret = 0;
7173out:
7174 btrfs_end_transaction(trans, extent_root);
7175 kfree(new_extents);
7176 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007177 return ret;
7178}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007179#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007180
Chris Masonec44a352008-04-28 15:29:52 -04007181static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7182{
7183 u64 num_devices;
7184 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7185 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7186
Yan Zheng2b820322008-11-17 21:11:30 -05007187 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04007188 if (num_devices == 1) {
7189 stripped |= BTRFS_BLOCK_GROUP_DUP;
7190 stripped = flags & ~stripped;
7191
7192 /* turn raid0 into single device chunks */
7193 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7194 return stripped;
7195
7196 /* turn mirroring into duplication */
7197 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7198 BTRFS_BLOCK_GROUP_RAID10))
7199 return stripped | BTRFS_BLOCK_GROUP_DUP;
7200 return flags;
7201 } else {
7202 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007203 if (flags & stripped)
7204 return flags;
7205
7206 stripped |= BTRFS_BLOCK_GROUP_DUP;
7207 stripped = flags & ~stripped;
7208
7209 /* switch duplicated blocks with raid1 */
7210 if (flags & BTRFS_BLOCK_GROUP_DUP)
7211 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7212
7213 /* turn single device chunks into raid0 */
7214 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7215 }
7216 return flags;
7217}
7218
Christoph Hellwigb2950862008-12-02 09:54:17 -05007219static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04007220 struct btrfs_block_group_cache *shrink_block_group,
7221 int force)
7222{
7223 struct btrfs_trans_handle *trans;
7224 u64 new_alloc_flags;
7225 u64 calc;
7226
Chris Masonc286ac42008-07-22 23:06:41 -04007227 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007228 if (btrfs_block_group_used(&shrink_block_group->item) +
7229 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04007230 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04007231
Chris Mason0ef3e662008-05-24 14:04:53 -04007232 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007233 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04007234
Chris Mason0ef3e662008-05-24 14:04:53 -04007235 new_alloc_flags = update_block_group_flags(root,
7236 shrink_block_group->flags);
7237 if (new_alloc_flags != shrink_block_group->flags) {
7238 calc =
7239 btrfs_block_group_used(&shrink_block_group->item);
7240 } else {
7241 calc = shrink_block_group->key.offset;
7242 }
Chris Masonc286ac42008-07-22 23:06:41 -04007243 spin_unlock(&shrink_block_group->lock);
7244
Chris Mason0ef3e662008-05-24 14:04:53 -04007245 do_chunk_alloc(trans, root->fs_info->extent_root,
7246 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04007247
Chris Mason0ef3e662008-05-24 14:04:53 -04007248 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04007249 } else
7250 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04007251 return 0;
7252}
7253
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007254
7255int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
7256 struct btrfs_block_group_cache *group)
7257
7258{
7259 __alloc_chunk_for_shrink(root, group, 1);
7260 set_block_group_readonly(group);
7261 return 0;
7262}
7263
Josef Bacikba1bf482009-09-11 16:11:19 -04007264/*
7265 * checks to see if its even possible to relocate this block group.
7266 *
7267 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7268 * ok to go ahead and try.
7269 */
7270int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007271{
Zheng Yan1a40e232008-09-26 10:09:34 -04007272 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007273 struct btrfs_space_info *space_info;
7274 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7275 struct btrfs_device *device;
7276 int full = 0;
7277 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007278
Josef Bacikba1bf482009-09-11 16:11:19 -04007279 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007280
Josef Bacikba1bf482009-09-11 16:11:19 -04007281 /* odd, couldn't find the block group, leave it alone */
7282 if (!block_group)
7283 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007284
Josef Bacikba1bf482009-09-11 16:11:19 -04007285 /* no bytes used, we're good */
7286 if (!btrfs_block_group_used(&block_group->item))
7287 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007288
Josef Bacikba1bf482009-09-11 16:11:19 -04007289 space_info = block_group->space_info;
7290 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007291
Josef Bacikba1bf482009-09-11 16:11:19 -04007292 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007293
Josef Bacikba1bf482009-09-11 16:11:19 -04007294 /*
7295 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007296 * relocate it unless we're able to allocate a new chunk below.
7297 *
7298 * Otherwise, we need to make sure we have room in the space to handle
7299 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007300 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007301 if ((space_info->total_bytes != block_group->key.offset) &&
7302 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04007303 space_info->bytes_pinned + space_info->bytes_readonly +
7304 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04007305 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007306 spin_unlock(&space_info->lock);
7307 goto out;
7308 }
7309 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007310
Josef Bacikba1bf482009-09-11 16:11:19 -04007311 /*
7312 * ok we don't have enough space, but maybe we have free space on our
7313 * devices to allocate new chunks for relocation, so loop through our
7314 * alloc devices and guess if we have enough space. However, if we
7315 * were marked as full, then we know there aren't enough chunks, and we
7316 * can just return.
7317 */
7318 ret = -1;
7319 if (full)
7320 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05007321
Josef Bacikba1bf482009-09-11 16:11:19 -04007322 mutex_lock(&root->fs_info->chunk_mutex);
7323 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7324 u64 min_free = btrfs_block_group_used(&block_group->item);
7325 u64 dev_offset, max_avail;
Chris Masonea8c2812008-08-04 23:17:27 -04007326
Josef Bacikba1bf482009-09-11 16:11:19 -04007327 /*
7328 * check to make sure we can actually find a chunk with enough
7329 * space to fit our block group in.
7330 */
7331 if (device->total_bytes > device->bytes_used + min_free) {
7332 ret = find_free_dev_extent(NULL, device, min_free,
7333 &dev_offset, &max_avail);
7334 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05007335 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04007336 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007337 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007338 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007339 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007340out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007341 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007342 return ret;
7343}
7344
Christoph Hellwigb2950862008-12-02 09:54:17 -05007345static int find_first_block_group(struct btrfs_root *root,
7346 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007347{
Chris Mason925baed2008-06-25 16:01:30 -04007348 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007349 struct btrfs_key found_key;
7350 struct extent_buffer *leaf;
7351 int slot;
7352
7353 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7354 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007355 goto out;
7356
Chris Masond3977122009-01-05 21:25:51 -05007357 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007358 slot = path->slots[0];
7359 leaf = path->nodes[0];
7360 if (slot >= btrfs_header_nritems(leaf)) {
7361 ret = btrfs_next_leaf(root, path);
7362 if (ret == 0)
7363 continue;
7364 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007365 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007366 break;
7367 }
7368 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7369
7370 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007371 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7372 ret = 0;
7373 goto out;
7374 }
Chris Mason0b86a832008-03-24 15:01:56 -04007375 path->slots[0]++;
7376 }
Chris Mason925baed2008-06-25 16:01:30 -04007377out:
Chris Mason0b86a832008-03-24 15:01:56 -04007378 return ret;
7379}
7380
Zheng Yan1a40e232008-09-26 10:09:34 -04007381int btrfs_free_block_groups(struct btrfs_fs_info *info)
7382{
7383 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007384 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007385 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007386 struct rb_node *n;
7387
Yan Zheng11833d62009-09-11 16:11:19 -04007388 down_write(&info->extent_commit_sem);
7389 while (!list_empty(&info->caching_block_groups)) {
7390 caching_ctl = list_entry(info->caching_block_groups.next,
7391 struct btrfs_caching_control, list);
7392 list_del(&caching_ctl->list);
7393 put_caching_control(caching_ctl);
7394 }
7395 up_write(&info->extent_commit_sem);
7396
Zheng Yan1a40e232008-09-26 10:09:34 -04007397 spin_lock(&info->block_group_cache_lock);
7398 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7399 block_group = rb_entry(n, struct btrfs_block_group_cache,
7400 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007401 rb_erase(&block_group->cache_node,
7402 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007403 spin_unlock(&info->block_group_cache_lock);
7404
Josef Bacik80eb2342008-10-29 14:49:05 -04007405 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007406 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007407 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007408
Josef Bacik817d52f2009-07-13 21:29:25 -04007409 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007410 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007411
7412 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007413 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007414
7415 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007416 }
7417 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007418
7419 /* now that all the block groups are freed, go through and
7420 * free all the space_info structs. This is only called during
7421 * the final stages of unmount, and so we know nobody is
7422 * using them. We call synchronize_rcu() once before we start,
7423 * just to be on the safe side.
7424 */
7425 synchronize_rcu();
7426
7427 while(!list_empty(&info->space_info)) {
7428 space_info = list_entry(info->space_info.next,
7429 struct btrfs_space_info,
7430 list);
7431
7432 list_del(&space_info->list);
7433 kfree(space_info);
7434 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007435 return 0;
7436}
7437
Yan, Zhengb742bb82010-05-16 10:46:24 -04007438static void __link_block_group(struct btrfs_space_info *space_info,
7439 struct btrfs_block_group_cache *cache)
7440{
7441 int index = get_block_group_index(cache);
7442
7443 down_write(&space_info->groups_sem);
7444 list_add_tail(&cache->list, &space_info->block_groups[index]);
7445 up_write(&space_info->groups_sem);
7446}
7447
Chris Mason9078a3e2007-04-26 16:46:15 -04007448int btrfs_read_block_groups(struct btrfs_root *root)
7449{
7450 struct btrfs_path *path;
7451 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007452 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007453 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007454 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007455 struct btrfs_key key;
7456 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007457 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007458
Chris Masonbe744172007-05-06 10:15:01 -04007459 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007460 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007461 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007462 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007463 path = btrfs_alloc_path();
7464 if (!path)
7465 return -ENOMEM;
7466
Chris Masond3977122009-01-05 21:25:51 -05007467 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007468 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007469 if (ret > 0)
7470 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007471 if (ret != 0)
7472 goto error;
7473
Chris Mason5f39d392007-10-15 16:14:19 -04007474 leaf = path->nodes[0];
7475 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007476 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007477 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007478 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04007479 break;
7480 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007481
Yan Zhengd2fb3432008-12-11 16:30:39 -05007482 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007483 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007484 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007485 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007486 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007487 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007488
7489 /*
7490 * we only want to have 32k of ram per block group for keeping
7491 * track of free space, and if we pass 1/2 of that we want to
7492 * start converting things over to using bitmaps
7493 */
7494 cache->extents_thresh = ((1024 * 32) / 2) /
7495 sizeof(struct btrfs_free_space);
7496
Chris Mason5f39d392007-10-15 16:14:19 -04007497 read_extent_buffer(leaf, &cache->item,
7498 btrfs_item_ptr_offset(leaf, path->slots[0]),
7499 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007500 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007501
Chris Mason9078a3e2007-04-26 16:46:15 -04007502 key.objectid = found_key.objectid + found_key.offset;
7503 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007504 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007505 cache->sectorsize = root->sectorsize;
7506
Josef Bacik817d52f2009-07-13 21:29:25 -04007507 /*
7508 * check for two cases, either we are full, and therefore
7509 * don't need to bother with the caching work since we won't
7510 * find any space, or we are empty, and we can just add all
7511 * the space in and be done with it. This saves us _alot_ of
7512 * time, particularly in the full case.
7513 */
7514 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04007515 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04007516 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007517 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007518 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007519 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007520 exclude_super_stripes(root, cache);
7521 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007522 cache->cached = BTRFS_CACHE_FINISHED;
7523 add_new_free_space(cache, root->fs_info,
7524 found_key.objectid,
7525 found_key.objectid +
7526 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007527 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007528 }
Chris Mason96b51792007-10-15 16:15:19 -04007529
Chris Mason6324fbf2008-03-24 15:01:59 -04007530 ret = update_space_info(info, cache->flags, found_key.offset,
7531 btrfs_block_group_used(&cache->item),
7532 &space_info);
7533 BUG_ON(ret);
7534 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007535 spin_lock(&cache->space_info->lock);
7536 cache->space_info->bytes_super += cache->bytes_super;
7537 spin_unlock(&cache->space_info->lock);
7538
Yan, Zhengb742bb82010-05-16 10:46:24 -04007539 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007540
Josef Bacik0f9dd462008-09-23 13:14:11 -04007541 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7542 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007543
7544 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007545 if (btrfs_chunk_readonly(root, cache->key.objectid))
7546 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007547 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007548
7549 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7550 if (!(get_alloc_profile(root, space_info->flags) &
7551 (BTRFS_BLOCK_GROUP_RAID10 |
7552 BTRFS_BLOCK_GROUP_RAID1 |
7553 BTRFS_BLOCK_GROUP_DUP)))
7554 continue;
7555 /*
7556 * avoid allocating from un-mirrored block group if there are
7557 * mirrored block groups.
7558 */
7559 list_for_each_entry(cache, &space_info->block_groups[3], list)
7560 set_block_group_readonly(cache);
7561 list_for_each_entry(cache, &space_info->block_groups[4], list)
7562 set_block_group_readonly(cache);
7563 }
Chris Mason0b86a832008-03-24 15:01:56 -04007564 ret = 0;
7565error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007566 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007567 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007568}
Chris Mason6324fbf2008-03-24 15:01:59 -04007569
7570int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7571 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007572 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007573 u64 size)
7574{
7575 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007576 struct btrfs_root *extent_root;
7577 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007578
7579 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007580
Chris Mason12fcfd22009-03-24 10:24:20 -04007581 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007582
Chris Mason8f18cf12008-04-25 16:53:30 -04007583 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007584 if (!cache)
7585 return -ENOMEM;
7586
Chris Masone17cade2008-04-15 15:41:47 -04007587 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007588 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007589 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007590 cache->sectorsize = root->sectorsize;
7591
7592 /*
7593 * we only want to have 32k of ram per block group for keeping track
7594 * of free space, and if we pass 1/2 of that we want to start
7595 * converting things over to using bitmaps
7596 */
7597 cache->extents_thresh = ((1024 * 32) / 2) /
7598 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007599 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007600 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007601 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007602 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007603 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007604
Chris Mason6324fbf2008-03-24 15:01:59 -04007605 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007606 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7607 cache->flags = type;
7608 btrfs_set_block_group_flags(&cache->item, type);
7609
Yan Zheng11833d62009-09-11 16:11:19 -04007610 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007611 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007612 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007613
Josef Bacik817d52f2009-07-13 21:29:25 -04007614 add_new_free_space(cache, root->fs_info, chunk_offset,
7615 chunk_offset + size);
7616
Yan Zheng11833d62009-09-11 16:11:19 -04007617 free_excluded_extents(root, cache);
7618
Chris Mason6324fbf2008-03-24 15:01:59 -04007619 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7620 &cache->space_info);
7621 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007622
7623 spin_lock(&cache->space_info->lock);
7624 cache->space_info->bytes_super += cache->bytes_super;
7625 spin_unlock(&cache->space_info->lock);
7626
Yan, Zhengb742bb82010-05-16 10:46:24 -04007627 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007628
Josef Bacik0f9dd462008-09-23 13:14:11 -04007629 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7630 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007631
Chris Mason6324fbf2008-03-24 15:01:59 -04007632 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7633 sizeof(cache->item));
7634 BUG_ON(ret);
7635
Chris Masond18a2c42008-04-04 15:40:00 -04007636 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007637
Chris Mason6324fbf2008-03-24 15:01:59 -04007638 return 0;
7639}
Zheng Yan1a40e232008-09-26 10:09:34 -04007640
7641int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7642 struct btrfs_root *root, u64 group_start)
7643{
7644 struct btrfs_path *path;
7645 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007646 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007647 struct btrfs_key key;
7648 int ret;
7649
Zheng Yan1a40e232008-09-26 10:09:34 -04007650 root = root->fs_info->extent_root;
7651
7652 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7653 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007654 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007655
7656 memcpy(&key, &block_group->key, sizeof(key));
7657
Chris Mason44fb5512009-06-04 15:34:51 -04007658 /* make sure this block group isn't part of an allocation cluster */
7659 cluster = &root->fs_info->data_alloc_cluster;
7660 spin_lock(&cluster->refill_lock);
7661 btrfs_return_cluster_to_free_space(block_group, cluster);
7662 spin_unlock(&cluster->refill_lock);
7663
7664 /*
7665 * make sure this block group isn't part of a metadata
7666 * allocation cluster
7667 */
7668 cluster = &root->fs_info->meta_alloc_cluster;
7669 spin_lock(&cluster->refill_lock);
7670 btrfs_return_cluster_to_free_space(block_group, cluster);
7671 spin_unlock(&cluster->refill_lock);
7672
Zheng Yan1a40e232008-09-26 10:09:34 -04007673 path = btrfs_alloc_path();
7674 BUG_ON(!path);
7675
Yan Zheng3dfdb932009-01-21 10:49:16 -05007676 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007677 rb_erase(&block_group->cache_node,
7678 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007679 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007680
Josef Bacik80eb2342008-10-29 14:49:05 -04007681 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007682 /*
7683 * we must use list_del_init so people can check to see if they
7684 * are still on the list after taking the semaphore
7685 */
7686 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007687 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007688
Josef Bacik817d52f2009-07-13 21:29:25 -04007689 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007690 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007691
7692 btrfs_remove_free_space_cache(block_group);
7693
Yan Zhengc146afa2008-11-12 14:34:12 -05007694 spin_lock(&block_group->space_info->lock);
7695 block_group->space_info->total_bytes -= block_group->key.offset;
7696 block_group->space_info->bytes_readonly -= block_group->key.offset;
7697 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007698
7699 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007700
Chris Masonfa9c0d72009-04-03 09:47:43 -04007701 btrfs_put_block_group(block_group);
7702 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007703
7704 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7705 if (ret > 0)
7706 ret = -EIO;
7707 if (ret < 0)
7708 goto out;
7709
7710 ret = btrfs_del_item(trans, root, path);
7711out:
7712 btrfs_free_path(path);
7713 return ret;
7714}