blob: dc84daee6bc472dbc480efd7ad179933277c5eb4 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050025#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050026#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027#include "ctree.h"
28#include "disk-io.h"
29#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040030#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040032#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040033#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Chris Mason56bec292009-03-13 10:10:06 -040035static int update_reserved_extents(struct btrfs_root *root,
36 u64 bytenr, u64 num, int reserve);
Josef Bacikf3465ca2008-11-12 14:19:50 -050037static int update_block_group(struct btrfs_trans_handle *trans,
38 struct btrfs_root *root,
39 u64 bytenr, u64 num_bytes, int alloc,
40 int mark_free);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040041static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root,
43 u64 bytenr, u64 num_bytes, u64 parent,
44 u64 root_objectid, u64 owner_objectid,
45 u64 owner_offset, int refs_to_drop,
46 struct btrfs_delayed_extent_op *extra_op);
47static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
48 struct extent_buffer *leaf,
49 struct btrfs_extent_item *ei);
50static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root,
52 u64 parent, u64 root_objectid,
53 u64 flags, u64 owner, u64 offset,
54 struct btrfs_key *ins, int ref_mod);
55static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 parent, u64 root_objectid,
58 u64 flags, struct btrfs_disk_key *key,
59 int level, struct btrfs_key *ins);
Yand548ee52008-01-03 13:56:30 -050060
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);
64
Josef Bacik817d52f2009-07-13 21:29:25 -040065static noinline int
66block_group_cache_done(struct btrfs_block_group_cache *cache)
67{
68 smp_mb();
69 return cache->cached == BTRFS_CACHE_FINISHED;
70}
71
Josef Bacik0f9dd462008-09-23 13:14:11 -040072static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
73{
74 return (cache->flags & bits) == bits;
75}
76
77/*
78 * this adds the block group to the fs_info rb tree for the block group
79 * cache
80 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050081static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040082 struct btrfs_block_group_cache *block_group)
83{
84 struct rb_node **p;
85 struct rb_node *parent = NULL;
86 struct btrfs_block_group_cache *cache;
87
88 spin_lock(&info->block_group_cache_lock);
89 p = &info->block_group_cache_tree.rb_node;
90
91 while (*p) {
92 parent = *p;
93 cache = rb_entry(parent, struct btrfs_block_group_cache,
94 cache_node);
95 if (block_group->key.objectid < cache->key.objectid) {
96 p = &(*p)->rb_left;
97 } else if (block_group->key.objectid > cache->key.objectid) {
98 p = &(*p)->rb_right;
99 } else {
100 spin_unlock(&info->block_group_cache_lock);
101 return -EEXIST;
102 }
103 }
104
105 rb_link_node(&block_group->cache_node, parent, p);
106 rb_insert_color(&block_group->cache_node,
107 &info->block_group_cache_tree);
108 spin_unlock(&info->block_group_cache_lock);
109
110 return 0;
111}
112
113/*
114 * This will return the block group at or after bytenr if contains is 0, else
115 * it will return the block group that contains the bytenr
116 */
117static struct btrfs_block_group_cache *
118block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
119 int contains)
120{
121 struct btrfs_block_group_cache *cache, *ret = NULL;
122 struct rb_node *n;
123 u64 end, start;
124
125 spin_lock(&info->block_group_cache_lock);
126 n = info->block_group_cache_tree.rb_node;
127
128 while (n) {
129 cache = rb_entry(n, struct btrfs_block_group_cache,
130 cache_node);
131 end = cache->key.objectid + cache->key.offset - 1;
132 start = cache->key.objectid;
133
134 if (bytenr < start) {
135 if (!contains && (!ret || start < ret->key.objectid))
136 ret = cache;
137 n = n->rb_left;
138 } else if (bytenr > start) {
139 if (contains && bytenr <= end) {
140 ret = cache;
141 break;
142 }
143 n = n->rb_right;
144 } else {
145 ret = cache;
146 break;
147 }
148 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500149 if (ret)
150 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400151 spin_unlock(&info->block_group_cache_lock);
152
153 return ret;
154}
155
Josef Bacik68b38552009-07-27 13:57:01 -0400156/*
157 * We always set EXTENT_LOCKED for the super mirror extents so we don't
158 * overwrite them, so those bits need to be unset. Also, if we are unmounting
159 * with pinned extents still sitting there because we had a block group caching,
160 * we need to clear those now, since we are done.
161 */
162void btrfs_free_pinned_extents(struct btrfs_fs_info *info)
Josef Bacik817d52f2009-07-13 21:29:25 -0400163{
164 u64 start, end, last = 0;
165 int ret;
166
167 while (1) {
168 ret = find_first_extent_bit(&info->pinned_extents, last,
Josef Bacik68b38552009-07-27 13:57:01 -0400169 &start, &end,
170 EXTENT_LOCKED|EXTENT_DIRTY);
Josef Bacik817d52f2009-07-13 21:29:25 -0400171 if (ret)
172 break;
173
Josef Bacik68b38552009-07-27 13:57:01 -0400174 clear_extent_bits(&info->pinned_extents, start, end,
175 EXTENT_LOCKED|EXTENT_DIRTY, GFP_NOFS);
Josef Bacik817d52f2009-07-13 21:29:25 -0400176 last = end+1;
177 }
178}
179
180static int remove_sb_from_cache(struct btrfs_root *root,
181 struct btrfs_block_group_cache *cache)
182{
183 struct btrfs_fs_info *fs_info = root->fs_info;
184 u64 bytenr;
185 u64 *logical;
186 int stripe_len;
187 int i, nr, ret;
188
189 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
190 bytenr = btrfs_sb_offset(i);
191 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
192 cache->key.objectid, bytenr,
193 0, &logical, &nr, &stripe_len);
194 BUG_ON(ret);
195 while (nr--) {
196 try_lock_extent(&fs_info->pinned_extents,
197 logical[nr],
198 logical[nr] + stripe_len - 1, GFP_NOFS);
199 }
200 kfree(logical);
201 }
202
203 return 0;
204}
205
Josef Bacik0f9dd462008-09-23 13:14:11 -0400206/*
207 * this is only called by cache_block_group, since we could have freed extents
208 * we need to check the pinned_extents for any extents that can't be used yet
209 * since their free space will be released as soon as the transaction commits.
210 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400211static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400212 struct btrfs_fs_info *info, u64 start, u64 end)
213{
Josef Bacik817d52f2009-07-13 21:29:25 -0400214 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400215 int ret;
216
217 while (start < end) {
218 ret = find_first_extent_bit(&info->pinned_extents, start,
219 &extent_start, &extent_end,
Josef Bacik68b38552009-07-27 13:57:01 -0400220 EXTENT_DIRTY|EXTENT_LOCKED);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400221 if (ret)
222 break;
223
224 if (extent_start == start) {
225 start = extent_end + 1;
226 } else if (extent_start > start && extent_start < end) {
227 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400228 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500229 ret = btrfs_add_free_space(block_group, start,
230 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400231 BUG_ON(ret);
232 start = extent_end + 1;
233 } else {
234 break;
235 }
236 }
237
238 if (start < end) {
239 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400240 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500241 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400242 BUG_ON(ret);
243 }
244
Josef Bacik817d52f2009-07-13 21:29:25 -0400245 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400246}
247
Josef Bacik817d52f2009-07-13 21:29:25 -0400248static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400249{
Josef Bacik817d52f2009-07-13 21:29:25 -0400250 struct btrfs_block_group_cache *block_group = data;
251 struct btrfs_fs_info *fs_info = block_group->fs_info;
252 u64 last = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400253 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400254 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400255 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400256 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400257 int slot;
Josef Bacik817d52f2009-07-13 21:29:25 -0400258 u64 total_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400259
Josef Bacik817d52f2009-07-13 21:29:25 -0400260 BUG_ON(!fs_info);
Chris Masonf510cfe2007-10-15 16:14:48 -0400261
Chris Masone37c9e62007-05-09 20:13:14 -0400262 path = btrfs_alloc_path();
263 if (!path)
264 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400265
Josef Bacik817d52f2009-07-13 21:29:25 -0400266 atomic_inc(&block_group->space_info->caching_threads);
267 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
268again:
269 /* need to make sure the commit_root doesn't disappear */
Yan Zheng276e6802009-07-30 09:40:40 -0400270 down_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400271
Chris Mason5cd57b22008-06-25 16:01:30 -0400272 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400273 * We don't want to deadlock with somebody trying to allocate a new
274 * extent for the extent root while also trying to search the extent
275 * root to add free space. So we skip locking and search the commit
276 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400277 */
278 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400279 path->search_commit_root = 1;
280 path->reada = 2;
281
Yan Zhenge4404d62008-12-12 10:03:26 -0500282 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400283 key.offset = 0;
284 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik817d52f2009-07-13 21:29:25 -0400285 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400286 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400287 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500288
Chris Masond3977122009-01-05 21:25:51 -0500289 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400290 smp_mb();
Yan Zhengf25784b2009-07-28 08:41:57 -0400291 if (block_group->fs_info->closing > 1) {
292 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400293 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400294 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400295
Chris Mason5f39d392007-10-15 16:14:19 -0400296 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400297 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400298 if (slot >= btrfs_header_nritems(leaf)) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400299 ret = btrfs_next_leaf(fs_info->extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400300 if (ret < 0)
301 goto err;
Josef Bacik817d52f2009-07-13 21:29:25 -0400302 else if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400303 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400304
Chris Masonf36f3042009-07-30 10:04:48 -0400305 if (need_resched() ||
306 btrfs_transaction_in_commit(fs_info)) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400307 btrfs_release_path(fs_info->extent_root, path);
Yan Zheng276e6802009-07-30 09:40:40 -0400308 up_read(&fs_info->extent_commit_sem);
Chris Masonf36f3042009-07-30 10:04:48 -0400309 schedule_timeout(1);
Josef Bacik817d52f2009-07-13 21:29:25 -0400310 goto again;
311 }
312
313 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400314 }
Chris Mason5f39d392007-10-15 16:14:19 -0400315 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400317 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400318
Chris Masone37c9e62007-05-09 20:13:14 -0400319 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400320 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400321 break;
Yan7d7d6062007-09-14 16:15:28 -0400322
323 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400324 total_found += add_new_free_space(block_group,
325 fs_info, last,
326 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400327 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400328 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400329
330 if (total_found > (1024 * 1024 * 2)) {
331 total_found = 0;
332 wake_up(&block_group->caching_q);
333 }
Yan7d7d6062007-09-14 16:15:28 -0400334next:
Chris Masone37c9e62007-05-09 20:13:14 -0400335 path->slots[0]++;
336 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400337 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400338
339 total_found += add_new_free_space(block_group, fs_info, last,
340 block_group->key.objectid +
341 block_group->key.offset);
342
343 spin_lock(&block_group->lock);
344 block_group->cached = BTRFS_CACHE_FINISHED;
345 spin_unlock(&block_group->lock);
346
Chris Mason54aa1f42007-06-22 14:16:25 -0400347err:
Chris Masone37c9e62007-05-09 20:13:14 -0400348 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400349 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400350 atomic_dec(&block_group->space_info->caching_threads);
351 wake_up(&block_group->caching_q);
352
Josef Bacik817d52f2009-07-13 21:29:25 -0400353 return 0;
354}
355
356static int cache_block_group(struct btrfs_block_group_cache *cache)
357{
358 struct task_struct *tsk;
359 int ret = 0;
360
361 spin_lock(&cache->lock);
362 if (cache->cached != BTRFS_CACHE_NO) {
363 spin_unlock(&cache->lock);
364 return ret;
365 }
366 cache->cached = BTRFS_CACHE_STARTED;
367 spin_unlock(&cache->lock);
368
369 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
370 cache->key.objectid);
371 if (IS_ERR(tsk)) {
372 ret = PTR_ERR(tsk);
373 printk(KERN_ERR "error running thread %d\n", ret);
374 BUG();
375 }
376
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400377 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400378}
379
Josef Bacik0f9dd462008-09-23 13:14:11 -0400380/*
381 * return the block group that starts at or after bytenr
382 */
Chris Masond3977122009-01-05 21:25:51 -0500383static struct btrfs_block_group_cache *
384btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400385{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400386 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400387
Josef Bacik0f9dd462008-09-23 13:14:11 -0400388 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400389
Josef Bacik0f9dd462008-09-23 13:14:11 -0400390 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400391}
392
Josef Bacik0f9dd462008-09-23 13:14:11 -0400393/*
Sankar P9f556842009-05-14 13:52:22 -0400394 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400395 */
Chris Masond3977122009-01-05 21:25:51 -0500396struct btrfs_block_group_cache *btrfs_lookup_block_group(
397 struct btrfs_fs_info *info,
398 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400399{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400400 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400401
Josef Bacik0f9dd462008-09-23 13:14:11 -0400402 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400403
Josef Bacik0f9dd462008-09-23 13:14:11 -0400404 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400405}
Chris Mason0b86a832008-03-24 15:01:56 -0400406
Chris Masonfa9c0d792009-04-03 09:47:43 -0400407void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
Yan Zhengd2fb3432008-12-11 16:30:39 -0500408{
409 if (atomic_dec_and_test(&cache->count))
410 kfree(cache);
411}
412
Josef Bacik0f9dd462008-09-23 13:14:11 -0400413static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
414 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400415{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400416 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400417 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400418
419 rcu_read_lock();
420 list_for_each_entry_rcu(found, head, list) {
421 if (found->flags == flags) {
422 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400423 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400424 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400425 }
Chris Mason4184ea72009-03-10 12:39:20 -0400426 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400427 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400428}
429
Chris Mason4184ea72009-03-10 12:39:20 -0400430/*
431 * after adding space to the filesystem, we need to clear the full flags
432 * on all the space infos.
433 */
434void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
435{
436 struct list_head *head = &info->space_info;
437 struct btrfs_space_info *found;
438
439 rcu_read_lock();
440 list_for_each_entry_rcu(found, head, list)
441 found->full = 0;
442 rcu_read_unlock();
443}
444
Josef Bacik80eb2342008-10-29 14:49:05 -0400445static u64 div_factor(u64 num, int factor)
446{
447 if (factor == 10)
448 return num;
449 num *= factor;
450 do_div(num, 10);
451 return num;
452}
453
Yan Zhengd2fb3432008-12-11 16:30:39 -0500454u64 btrfs_find_block_group(struct btrfs_root *root,
455 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400456{
Chris Mason96b51792007-10-15 16:15:19 -0400457 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400458 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500459 u64 last = max(search_hint, search_start);
460 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400461 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500462 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400463 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400464again:
Zheng Yane8569812008-09-26 10:05:48 -0400465 while (1) {
466 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400467 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400468 break;
Chris Mason96b51792007-10-15 16:15:19 -0400469
Chris Masonc286ac42008-07-22 23:06:41 -0400470 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400471 last = cache->key.objectid + cache->key.offset;
472 used = btrfs_block_group_used(&cache->item);
473
Yan Zhengd2fb3432008-12-11 16:30:39 -0500474 if ((full_search || !cache->ro) &&
475 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400476 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500477 div_factor(cache->key.offset, factor)) {
478 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400479 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400480 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400481 goto found;
482 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400483 }
Chris Masonc286ac42008-07-22 23:06:41 -0400484 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400485 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400486 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400487 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400488 if (!wrapped) {
489 last = search_start;
490 wrapped = 1;
491 goto again;
492 }
493 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400494 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400495 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400496 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400497 goto again;
498 }
Chris Masonbe744172007-05-06 10:15:01 -0400499found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500500 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400501}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400502
Chris Masone02119d2008-09-05 16:13:11 -0400503/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400504int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400505{
506 int ret;
507 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400508 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400509
Zheng Yan31840ae2008-09-23 13:14:14 -0400510 path = btrfs_alloc_path();
511 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400512 key.objectid = start;
513 key.offset = len;
514 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
515 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
516 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400517 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500518 return ret;
519}
520
Chris Masond8d5f3e2007-12-11 12:42:00 -0500521/*
522 * Back reference rules. Back refs have three main goals:
523 *
524 * 1) differentiate between all holders of references to an extent so that
525 * when a reference is dropped we can make sure it was a valid reference
526 * before freeing the extent.
527 *
528 * 2) Provide enough information to quickly find the holders of an extent
529 * if we notice a given block is corrupted or bad.
530 *
531 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
532 * maintenance. This is actually the same as #2, but with a slightly
533 * different use case.
534 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400535 * There are two kinds of back refs. The implicit back refs is optimized
536 * for pointers in non-shared tree blocks. For a given pointer in a block,
537 * back refs of this kind provide information about the block's owner tree
538 * and the pointer's key. These information allow us to find the block by
539 * b-tree searching. The full back refs is for pointers in tree blocks not
540 * referenced by their owner trees. The location of tree block is recorded
541 * in the back refs. Actually the full back refs is generic, and can be
542 * used in all cases the implicit back refs is used. The major shortcoming
543 * of the full back refs is its overhead. Every time a tree block gets
544 * COWed, we have to update back refs entry for all pointers in it.
545 *
546 * For a newly allocated tree block, we use implicit back refs for
547 * pointers in it. This means most tree related operations only involve
548 * implicit back refs. For a tree block created in old transaction, the
549 * only way to drop a reference to it is COW it. So we can detect the
550 * event that tree block loses its owner tree's reference and do the
551 * back refs conversion.
552 *
553 * When a tree block is COW'd through a tree, there are four cases:
554 *
555 * The reference count of the block is one and the tree is the block's
556 * owner tree. Nothing to do in this case.
557 *
558 * The reference count of the block is one and the tree is not the
559 * block's owner tree. In this case, full back refs is used for pointers
560 * in the block. Remove these full back refs, add implicit back refs for
561 * every pointers in the new block.
562 *
563 * The reference count of the block is greater than one and the tree is
564 * the block's owner tree. In this case, implicit back refs is used for
565 * pointers in the block. Add full back refs for every pointers in the
566 * block, increase lower level extents' reference counts. The original
567 * implicit back refs are entailed to the new block.
568 *
569 * The reference count of the block is greater than one and the tree is
570 * not the block's owner tree. Add implicit back refs for every pointer in
571 * the new block, increase lower level extents' reference count.
572 *
573 * Back Reference Key composing:
574 *
575 * The key objectid corresponds to the first byte in the extent,
576 * The key type is used to differentiate between types of back refs.
577 * There are different meanings of the key offset for different types
578 * of back refs.
579 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500580 * File extents can be referenced by:
581 *
582 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400583 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500584 * - different offsets inside a file (bookend extents in file.c)
585 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400586 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500587 *
588 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500589 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400590 * - original offset in the file
591 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400592 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400593 * The key offset for the implicit back refs is hash of the first
594 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500595 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400596 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500597 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400598 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500599 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400600 * The key offset for the implicit back refs is the first byte of
601 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500602 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400603 * When a file extent is allocated, The implicit back refs is used.
604 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500605 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400606 * (root_key.objectid, inode objectid, offset in file, 1)
607 *
608 * When a file extent is removed file truncation, we find the
609 * corresponding implicit back refs and check the following fields:
610 *
611 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500612 *
613 * Btree extents can be referenced by:
614 *
615 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500616 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400617 * Both the implicit back refs and the full back refs for tree blocks
618 * only consist of key. The key offset for the implicit back refs is
619 * objectid of block's owner tree. The key offset for the full back refs
620 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500621 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400622 * When implicit back refs is used, information about the lowest key and
623 * level of the tree block are required. These information are stored in
624 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500625 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400626
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400627#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
628static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
629 struct btrfs_root *root,
630 struct btrfs_path *path,
631 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500632{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400633 struct btrfs_extent_item *item;
634 struct btrfs_extent_item_v0 *ei0;
635 struct btrfs_extent_ref_v0 *ref0;
636 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400637 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400638 struct btrfs_key key;
639 struct btrfs_key found_key;
640 u32 new_size = sizeof(*item);
641 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500642 int ret;
643
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400644 leaf = path->nodes[0];
645 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500646
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400647 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
648 ei0 = btrfs_item_ptr(leaf, path->slots[0],
649 struct btrfs_extent_item_v0);
650 refs = btrfs_extent_refs_v0(leaf, ei0);
651
652 if (owner == (u64)-1) {
653 while (1) {
654 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
655 ret = btrfs_next_leaf(root, path);
656 if (ret < 0)
657 return ret;
658 BUG_ON(ret > 0);
659 leaf = path->nodes[0];
660 }
661 btrfs_item_key_to_cpu(leaf, &found_key,
662 path->slots[0]);
663 BUG_ON(key.objectid != found_key.objectid);
664 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
665 path->slots[0]++;
666 continue;
667 }
668 ref0 = btrfs_item_ptr(leaf, path->slots[0],
669 struct btrfs_extent_ref_v0);
670 owner = btrfs_ref_objectid_v0(leaf, ref0);
671 break;
672 }
673 }
674 btrfs_release_path(root, path);
675
676 if (owner < BTRFS_FIRST_FREE_OBJECTID)
677 new_size += sizeof(*bi);
678
679 new_size -= sizeof(*ei0);
680 ret = btrfs_search_slot(trans, root, &key, path,
681 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400682 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400683 return ret;
684 BUG_ON(ret);
685
686 ret = btrfs_extend_item(trans, root, path, new_size);
687 BUG_ON(ret);
688
689 leaf = path->nodes[0];
690 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
691 btrfs_set_extent_refs(leaf, item, refs);
692 /* FIXME: get real generation */
693 btrfs_set_extent_generation(leaf, item, 0);
694 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
695 btrfs_set_extent_flags(leaf, item,
696 BTRFS_EXTENT_FLAG_TREE_BLOCK |
697 BTRFS_BLOCK_FLAG_FULL_BACKREF);
698 bi = (struct btrfs_tree_block_info *)(item + 1);
699 /* FIXME: get first key of the block */
700 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
701 btrfs_set_tree_block_level(leaf, bi, (int)owner);
702 } else {
703 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
704 }
705 btrfs_mark_buffer_dirty(leaf);
706 return 0;
707}
708#endif
709
710static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
711{
712 u32 high_crc = ~(u32)0;
713 u32 low_crc = ~(u32)0;
714 __le64 lenum;
715
716 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100717 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400718 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100719 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400720 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100721 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400722
723 return ((u64)high_crc << 31) ^ (u64)low_crc;
724}
725
726static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
727 struct btrfs_extent_data_ref *ref)
728{
729 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
730 btrfs_extent_data_ref_objectid(leaf, ref),
731 btrfs_extent_data_ref_offset(leaf, ref));
732}
733
734static int match_extent_data_ref(struct extent_buffer *leaf,
735 struct btrfs_extent_data_ref *ref,
736 u64 root_objectid, u64 owner, u64 offset)
737{
738 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
739 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
740 btrfs_extent_data_ref_offset(leaf, ref) != offset)
741 return 0;
742 return 1;
743}
744
745static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
746 struct btrfs_root *root,
747 struct btrfs_path *path,
748 u64 bytenr, u64 parent,
749 u64 root_objectid,
750 u64 owner, u64 offset)
751{
752 struct btrfs_key key;
753 struct btrfs_extent_data_ref *ref;
754 struct extent_buffer *leaf;
755 u32 nritems;
756 int ret;
757 int recow;
758 int err = -ENOENT;
759
760 key.objectid = bytenr;
761 if (parent) {
762 key.type = BTRFS_SHARED_DATA_REF_KEY;
763 key.offset = parent;
764 } else {
765 key.type = BTRFS_EXTENT_DATA_REF_KEY;
766 key.offset = hash_extent_data_ref(root_objectid,
767 owner, offset);
768 }
769again:
770 recow = 0;
771 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
772 if (ret < 0) {
773 err = ret;
774 goto fail;
775 }
776
777 if (parent) {
778 if (!ret)
779 return 0;
780#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
781 key.type = BTRFS_EXTENT_REF_V0_KEY;
782 btrfs_release_path(root, path);
783 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
784 if (ret < 0) {
785 err = ret;
786 goto fail;
787 }
788 if (!ret)
789 return 0;
790#endif
791 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400792 }
793
794 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400795 nritems = btrfs_header_nritems(leaf);
796 while (1) {
797 if (path->slots[0] >= nritems) {
798 ret = btrfs_next_leaf(root, path);
799 if (ret < 0)
800 err = ret;
801 if (ret)
802 goto fail;
803
804 leaf = path->nodes[0];
805 nritems = btrfs_header_nritems(leaf);
806 recow = 1;
807 }
808
809 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
810 if (key.objectid != bytenr ||
811 key.type != BTRFS_EXTENT_DATA_REF_KEY)
812 goto fail;
813
814 ref = btrfs_item_ptr(leaf, path->slots[0],
815 struct btrfs_extent_data_ref);
816
817 if (match_extent_data_ref(leaf, ref, root_objectid,
818 owner, offset)) {
819 if (recow) {
820 btrfs_release_path(root, path);
821 goto again;
822 }
823 err = 0;
824 break;
825 }
826 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400827 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400828fail:
829 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400830}
831
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400832static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
833 struct btrfs_root *root,
834 struct btrfs_path *path,
835 u64 bytenr, u64 parent,
836 u64 root_objectid, u64 owner,
837 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400838{
839 struct btrfs_key key;
840 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400841 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400842 u32 num_refs;
843 int ret;
844
845 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400846 if (parent) {
847 key.type = BTRFS_SHARED_DATA_REF_KEY;
848 key.offset = parent;
849 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400850 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400851 key.type = BTRFS_EXTENT_DATA_REF_KEY;
852 key.offset = hash_extent_data_ref(root_objectid,
853 owner, offset);
854 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400855 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400856
857 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
858 if (ret && ret != -EEXIST)
859 goto fail;
860
861 leaf = path->nodes[0];
862 if (parent) {
863 struct btrfs_shared_data_ref *ref;
864 ref = btrfs_item_ptr(leaf, path->slots[0],
865 struct btrfs_shared_data_ref);
866 if (ret == 0) {
867 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
868 } else {
869 num_refs = btrfs_shared_data_ref_count(leaf, ref);
870 num_refs += refs_to_add;
871 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
872 }
873 } else {
874 struct btrfs_extent_data_ref *ref;
875 while (ret == -EEXIST) {
876 ref = btrfs_item_ptr(leaf, path->slots[0],
877 struct btrfs_extent_data_ref);
878 if (match_extent_data_ref(leaf, ref, root_objectid,
879 owner, offset))
880 break;
881 btrfs_release_path(root, path);
882 key.offset++;
883 ret = btrfs_insert_empty_item(trans, root, path, &key,
884 size);
885 if (ret && ret != -EEXIST)
886 goto fail;
887
888 leaf = path->nodes[0];
889 }
890 ref = btrfs_item_ptr(leaf, path->slots[0],
891 struct btrfs_extent_data_ref);
892 if (ret == 0) {
893 btrfs_set_extent_data_ref_root(leaf, ref,
894 root_objectid);
895 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
896 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
897 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
898 } else {
899 num_refs = btrfs_extent_data_ref_count(leaf, ref);
900 num_refs += refs_to_add;
901 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
902 }
903 }
904 btrfs_mark_buffer_dirty(leaf);
905 ret = 0;
906fail:
Chris Mason7bb86312007-12-11 09:25:06 -0500907 btrfs_release_path(root, path);
908 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500909}
910
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
912 struct btrfs_root *root,
913 struct btrfs_path *path,
914 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -0400915{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400916 struct btrfs_key key;
917 struct btrfs_extent_data_ref *ref1 = NULL;
918 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400919 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400920 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400921 int ret = 0;
922
923 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400924 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
925
926 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
927 ref1 = btrfs_item_ptr(leaf, path->slots[0],
928 struct btrfs_extent_data_ref);
929 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
930 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
931 ref2 = btrfs_item_ptr(leaf, path->slots[0],
932 struct btrfs_shared_data_ref);
933 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
934#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
935 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
936 struct btrfs_extent_ref_v0 *ref0;
937 ref0 = btrfs_item_ptr(leaf, path->slots[0],
938 struct btrfs_extent_ref_v0);
939 num_refs = btrfs_ref_count_v0(leaf, ref0);
940#endif
941 } else {
942 BUG();
943 }
944
Chris Mason56bec292009-03-13 10:10:06 -0400945 BUG_ON(num_refs < refs_to_drop);
946 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400947
Zheng Yan31840ae2008-09-23 13:14:14 -0400948 if (num_refs == 0) {
949 ret = btrfs_del_item(trans, root, path);
950 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400951 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
952 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
953 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
954 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
955#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
956 else {
957 struct btrfs_extent_ref_v0 *ref0;
958 ref0 = btrfs_item_ptr(leaf, path->slots[0],
959 struct btrfs_extent_ref_v0);
960 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
961 }
962#endif
Zheng Yan31840ae2008-09-23 13:14:14 -0400963 btrfs_mark_buffer_dirty(leaf);
964 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400965 return ret;
966}
967
968static noinline u32 extent_data_ref_count(struct btrfs_root *root,
969 struct btrfs_path *path,
970 struct btrfs_extent_inline_ref *iref)
971{
972 struct btrfs_key key;
973 struct extent_buffer *leaf;
974 struct btrfs_extent_data_ref *ref1;
975 struct btrfs_shared_data_ref *ref2;
976 u32 num_refs = 0;
977
978 leaf = path->nodes[0];
979 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
980 if (iref) {
981 if (btrfs_extent_inline_ref_type(leaf, iref) ==
982 BTRFS_EXTENT_DATA_REF_KEY) {
983 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
984 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
985 } else {
986 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
987 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
988 }
989 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
990 ref1 = btrfs_item_ptr(leaf, path->slots[0],
991 struct btrfs_extent_data_ref);
992 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
993 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
994 ref2 = btrfs_item_ptr(leaf, path->slots[0],
995 struct btrfs_shared_data_ref);
996 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
997#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
998 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
999 struct btrfs_extent_ref_v0 *ref0;
1000 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1001 struct btrfs_extent_ref_v0);
1002 num_refs = btrfs_ref_count_v0(leaf, ref0);
1003#endif
1004 } else {
1005 WARN_ON(1);
1006 }
1007 return num_refs;
1008}
1009
1010static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1011 struct btrfs_root *root,
1012 struct btrfs_path *path,
1013 u64 bytenr, u64 parent,
1014 u64 root_objectid)
1015{
1016 struct btrfs_key key;
1017 int ret;
1018
1019 key.objectid = bytenr;
1020 if (parent) {
1021 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1022 key.offset = parent;
1023 } else {
1024 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1025 key.offset = root_objectid;
1026 }
1027
1028 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1029 if (ret > 0)
1030 ret = -ENOENT;
1031#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1032 if (ret == -ENOENT && parent) {
1033 btrfs_release_path(root, path);
1034 key.type = BTRFS_EXTENT_REF_V0_KEY;
1035 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1036 if (ret > 0)
1037 ret = -ENOENT;
1038 }
1039#endif
1040 return ret;
1041}
1042
1043static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1044 struct btrfs_root *root,
1045 struct btrfs_path *path,
1046 u64 bytenr, u64 parent,
1047 u64 root_objectid)
1048{
1049 struct btrfs_key key;
1050 int ret;
1051
1052 key.objectid = bytenr;
1053 if (parent) {
1054 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1055 key.offset = parent;
1056 } else {
1057 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1058 key.offset = root_objectid;
1059 }
1060
1061 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001062 btrfs_release_path(root, path);
1063 return ret;
1064}
1065
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001066static inline int extent_ref_type(u64 parent, u64 owner)
1067{
1068 int type;
1069 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1070 if (parent > 0)
1071 type = BTRFS_SHARED_BLOCK_REF_KEY;
1072 else
1073 type = BTRFS_TREE_BLOCK_REF_KEY;
1074 } else {
1075 if (parent > 0)
1076 type = BTRFS_SHARED_DATA_REF_KEY;
1077 else
1078 type = BTRFS_EXTENT_DATA_REF_KEY;
1079 }
1080 return type;
1081}
1082
Yan Zheng2c47e6052009-06-27 21:07:35 -04001083static int find_next_key(struct btrfs_path *path, int level,
1084 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001085
1086{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001087 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001088 if (!path->nodes[level])
1089 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090 if (path->slots[level] + 1 >=
1091 btrfs_header_nritems(path->nodes[level]))
1092 continue;
1093 if (level == 0)
1094 btrfs_item_key_to_cpu(path->nodes[level], key,
1095 path->slots[level] + 1);
1096 else
1097 btrfs_node_key_to_cpu(path->nodes[level], key,
1098 path->slots[level] + 1);
1099 return 0;
1100 }
1101 return 1;
1102}
1103
1104/*
1105 * look for inline back ref. if back ref is found, *ref_ret is set
1106 * to the address of inline back ref, and 0 is returned.
1107 *
1108 * if back ref isn't found, *ref_ret is set to the address where it
1109 * should be inserted, and -ENOENT is returned.
1110 *
1111 * if insert is true and there are too many inline back refs, the path
1112 * points to the extent item, and -EAGAIN is returned.
1113 *
1114 * NOTE: inline back refs are ordered in the same way that back ref
1115 * items in the tree are ordered.
1116 */
1117static noinline_for_stack
1118int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1119 struct btrfs_root *root,
1120 struct btrfs_path *path,
1121 struct btrfs_extent_inline_ref **ref_ret,
1122 u64 bytenr, u64 num_bytes,
1123 u64 parent, u64 root_objectid,
1124 u64 owner, u64 offset, int insert)
1125{
1126 struct btrfs_key key;
1127 struct extent_buffer *leaf;
1128 struct btrfs_extent_item *ei;
1129 struct btrfs_extent_inline_ref *iref;
1130 u64 flags;
1131 u64 item_size;
1132 unsigned long ptr;
1133 unsigned long end;
1134 int extra_size;
1135 int type;
1136 int want;
1137 int ret;
1138 int err = 0;
1139
1140 key.objectid = bytenr;
1141 key.type = BTRFS_EXTENT_ITEM_KEY;
1142 key.offset = num_bytes;
1143
1144 want = extent_ref_type(parent, owner);
1145 if (insert) {
1146 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001147 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001148 } else
1149 extra_size = -1;
1150 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1151 if (ret < 0) {
1152 err = ret;
1153 goto out;
1154 }
1155 BUG_ON(ret);
1156
1157 leaf = path->nodes[0];
1158 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1159#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1160 if (item_size < sizeof(*ei)) {
1161 if (!insert) {
1162 err = -ENOENT;
1163 goto out;
1164 }
1165 ret = convert_extent_item_v0(trans, root, path, owner,
1166 extra_size);
1167 if (ret < 0) {
1168 err = ret;
1169 goto out;
1170 }
1171 leaf = path->nodes[0];
1172 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1173 }
1174#endif
1175 BUG_ON(item_size < sizeof(*ei));
1176
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001177 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1178 flags = btrfs_extent_flags(leaf, ei);
1179
1180 ptr = (unsigned long)(ei + 1);
1181 end = (unsigned long)ei + item_size;
1182
1183 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1184 ptr += sizeof(struct btrfs_tree_block_info);
1185 BUG_ON(ptr > end);
1186 } else {
1187 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1188 }
1189
1190 err = -ENOENT;
1191 while (1) {
1192 if (ptr >= end) {
1193 WARN_ON(ptr > end);
1194 break;
1195 }
1196 iref = (struct btrfs_extent_inline_ref *)ptr;
1197 type = btrfs_extent_inline_ref_type(leaf, iref);
1198 if (want < type)
1199 break;
1200 if (want > type) {
1201 ptr += btrfs_extent_inline_ref_size(type);
1202 continue;
1203 }
1204
1205 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1206 struct btrfs_extent_data_ref *dref;
1207 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1208 if (match_extent_data_ref(leaf, dref, root_objectid,
1209 owner, offset)) {
1210 err = 0;
1211 break;
1212 }
1213 if (hash_extent_data_ref_item(leaf, dref) <
1214 hash_extent_data_ref(root_objectid, owner, offset))
1215 break;
1216 } else {
1217 u64 ref_offset;
1218 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1219 if (parent > 0) {
1220 if (parent == ref_offset) {
1221 err = 0;
1222 break;
1223 }
1224 if (ref_offset < parent)
1225 break;
1226 } else {
1227 if (root_objectid == ref_offset) {
1228 err = 0;
1229 break;
1230 }
1231 if (ref_offset < root_objectid)
1232 break;
1233 }
1234 }
1235 ptr += btrfs_extent_inline_ref_size(type);
1236 }
1237 if (err == -ENOENT && insert) {
1238 if (item_size + extra_size >=
1239 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1240 err = -EAGAIN;
1241 goto out;
1242 }
1243 /*
1244 * To add new inline back ref, we have to make sure
1245 * there is no corresponding back ref item.
1246 * For simplicity, we just do not add new inline back
1247 * ref if there is any kind of item for this block
1248 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001249 if (find_next_key(path, 0, &key) == 0 &&
1250 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001251 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001252 err = -EAGAIN;
1253 goto out;
1254 }
1255 }
1256 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1257out:
Yan Zheng85d41982009-06-11 08:51:10 -04001258 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001259 path->keep_locks = 0;
1260 btrfs_unlock_up_safe(path, 1);
1261 }
1262 return err;
1263}
1264
1265/*
1266 * helper to add new inline back ref
1267 */
1268static noinline_for_stack
1269int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1270 struct btrfs_root *root,
1271 struct btrfs_path *path,
1272 struct btrfs_extent_inline_ref *iref,
1273 u64 parent, u64 root_objectid,
1274 u64 owner, u64 offset, int refs_to_add,
1275 struct btrfs_delayed_extent_op *extent_op)
1276{
1277 struct extent_buffer *leaf;
1278 struct btrfs_extent_item *ei;
1279 unsigned long ptr;
1280 unsigned long end;
1281 unsigned long item_offset;
1282 u64 refs;
1283 int size;
1284 int type;
1285 int ret;
1286
1287 leaf = path->nodes[0];
1288 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1289 item_offset = (unsigned long)iref - (unsigned long)ei;
1290
1291 type = extent_ref_type(parent, owner);
1292 size = btrfs_extent_inline_ref_size(type);
1293
1294 ret = btrfs_extend_item(trans, root, path, size);
1295 BUG_ON(ret);
1296
1297 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1298 refs = btrfs_extent_refs(leaf, ei);
1299 refs += refs_to_add;
1300 btrfs_set_extent_refs(leaf, ei, refs);
1301 if (extent_op)
1302 __run_delayed_extent_op(extent_op, leaf, ei);
1303
1304 ptr = (unsigned long)ei + item_offset;
1305 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1306 if (ptr < end - size)
1307 memmove_extent_buffer(leaf, ptr + size, ptr,
1308 end - size - ptr);
1309
1310 iref = (struct btrfs_extent_inline_ref *)ptr;
1311 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1312 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1313 struct btrfs_extent_data_ref *dref;
1314 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1315 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1316 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1317 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1318 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1319 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1320 struct btrfs_shared_data_ref *sref;
1321 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1322 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1323 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1324 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1325 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1326 } else {
1327 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1328 }
1329 btrfs_mark_buffer_dirty(leaf);
1330 return 0;
1331}
1332
1333static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1334 struct btrfs_root *root,
1335 struct btrfs_path *path,
1336 struct btrfs_extent_inline_ref **ref_ret,
1337 u64 bytenr, u64 num_bytes, u64 parent,
1338 u64 root_objectid, u64 owner, u64 offset)
1339{
1340 int ret;
1341
1342 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1343 bytenr, num_bytes, parent,
1344 root_objectid, owner, offset, 0);
1345 if (ret != -ENOENT)
1346 return ret;
1347
1348 btrfs_release_path(root, path);
1349 *ref_ret = NULL;
1350
1351 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1352 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1353 root_objectid);
1354 } else {
1355 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1356 root_objectid, owner, offset);
1357 }
1358 return ret;
1359}
1360
1361/*
1362 * helper to update/remove inline back ref
1363 */
1364static noinline_for_stack
1365int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1366 struct btrfs_root *root,
1367 struct btrfs_path *path,
1368 struct btrfs_extent_inline_ref *iref,
1369 int refs_to_mod,
1370 struct btrfs_delayed_extent_op *extent_op)
1371{
1372 struct extent_buffer *leaf;
1373 struct btrfs_extent_item *ei;
1374 struct btrfs_extent_data_ref *dref = NULL;
1375 struct btrfs_shared_data_ref *sref = NULL;
1376 unsigned long ptr;
1377 unsigned long end;
1378 u32 item_size;
1379 int size;
1380 int type;
1381 int ret;
1382 u64 refs;
1383
1384 leaf = path->nodes[0];
1385 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1386 refs = btrfs_extent_refs(leaf, ei);
1387 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1388 refs += refs_to_mod;
1389 btrfs_set_extent_refs(leaf, ei, refs);
1390 if (extent_op)
1391 __run_delayed_extent_op(extent_op, leaf, ei);
1392
1393 type = btrfs_extent_inline_ref_type(leaf, iref);
1394
1395 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1396 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1397 refs = btrfs_extent_data_ref_count(leaf, dref);
1398 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1399 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1400 refs = btrfs_shared_data_ref_count(leaf, sref);
1401 } else {
1402 refs = 1;
1403 BUG_ON(refs_to_mod != -1);
1404 }
1405
1406 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1407 refs += refs_to_mod;
1408
1409 if (refs > 0) {
1410 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1411 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1412 else
1413 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1414 } else {
1415 size = btrfs_extent_inline_ref_size(type);
1416 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1417 ptr = (unsigned long)iref;
1418 end = (unsigned long)ei + item_size;
1419 if (ptr + size < end)
1420 memmove_extent_buffer(leaf, ptr, ptr + size,
1421 end - ptr - size);
1422 item_size -= size;
1423 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1424 BUG_ON(ret);
1425 }
1426 btrfs_mark_buffer_dirty(leaf);
1427 return 0;
1428}
1429
1430static noinline_for_stack
1431int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1432 struct btrfs_root *root,
1433 struct btrfs_path *path,
1434 u64 bytenr, u64 num_bytes, u64 parent,
1435 u64 root_objectid, u64 owner,
1436 u64 offset, int refs_to_add,
1437 struct btrfs_delayed_extent_op *extent_op)
1438{
1439 struct btrfs_extent_inline_ref *iref;
1440 int ret;
1441
1442 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1443 bytenr, num_bytes, parent,
1444 root_objectid, owner, offset, 1);
1445 if (ret == 0) {
1446 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1447 ret = update_inline_extent_backref(trans, root, path, iref,
1448 refs_to_add, extent_op);
1449 } else if (ret == -ENOENT) {
1450 ret = setup_inline_extent_backref(trans, root, path, iref,
1451 parent, root_objectid,
1452 owner, offset, refs_to_add,
1453 extent_op);
1454 }
1455 return ret;
1456}
1457
1458static int insert_extent_backref(struct btrfs_trans_handle *trans,
1459 struct btrfs_root *root,
1460 struct btrfs_path *path,
1461 u64 bytenr, u64 parent, u64 root_objectid,
1462 u64 owner, u64 offset, int refs_to_add)
1463{
1464 int ret;
1465 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1466 BUG_ON(refs_to_add != 1);
1467 ret = insert_tree_block_ref(trans, root, path, bytenr,
1468 parent, root_objectid);
1469 } else {
1470 ret = insert_extent_data_ref(trans, root, path, bytenr,
1471 parent, root_objectid,
1472 owner, offset, refs_to_add);
1473 }
1474 return ret;
1475}
1476
1477static int remove_extent_backref(struct btrfs_trans_handle *trans,
1478 struct btrfs_root *root,
1479 struct btrfs_path *path,
1480 struct btrfs_extent_inline_ref *iref,
1481 int refs_to_drop, int is_data)
1482{
1483 int ret;
1484
1485 BUG_ON(!is_data && refs_to_drop != 1);
1486 if (iref) {
1487 ret = update_inline_extent_backref(trans, root, path, iref,
1488 -refs_to_drop, NULL);
1489 } else if (is_data) {
1490 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1491 } else {
1492 ret = btrfs_del_item(trans, root, path);
1493 }
1494 return ret;
1495}
1496
Chris Mason4b4e25f2008-11-20 10:22:27 -05001497#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001498static void btrfs_issue_discard(struct block_device *bdev,
1499 u64 start, u64 len)
1500{
Chris Mason15916de2008-11-19 21:17:22 -05001501 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -05001502}
Chris Mason4b4e25f2008-11-20 10:22:27 -05001503#endif
Chris Mason15916de2008-11-19 21:17:22 -05001504
Liu Hui1f3c79a2009-01-05 15:57:51 -05001505static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1506 u64 num_bytes)
1507{
1508#ifdef BIO_RW_DISCARD
1509 int ret;
1510 u64 map_length = num_bytes;
1511 struct btrfs_multi_bio *multi = NULL;
1512
1513 /* Tell the block device(s) that the sectors can be discarded */
1514 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1515 bytenr, &map_length, &multi, 0);
1516 if (!ret) {
1517 struct btrfs_bio_stripe *stripe = multi->stripes;
1518 int i;
1519
1520 if (map_length > num_bytes)
1521 map_length = num_bytes;
1522
1523 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1524 btrfs_issue_discard(stripe->dev->bdev,
1525 stripe->physical,
1526 map_length);
1527 }
1528 kfree(multi);
1529 }
1530
1531 return ret;
1532#else
1533 return 0;
1534#endif
1535}
1536
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001537int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1538 struct btrfs_root *root,
1539 u64 bytenr, u64 num_bytes, u64 parent,
1540 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001541{
1542 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001543 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1544 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001545
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001546 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1547 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1548 parent, root_objectid, (int)owner,
1549 BTRFS_ADD_DELAYED_REF, NULL);
1550 } else {
1551 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1552 parent, root_objectid, owner, offset,
1553 BTRFS_ADD_DELAYED_REF, NULL);
1554 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001555 return ret;
1556}
1557
Chris Mason925baed2008-06-25 16:01:30 -04001558static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001559 struct btrfs_root *root,
1560 u64 bytenr, u64 num_bytes,
1561 u64 parent, u64 root_objectid,
1562 u64 owner, u64 offset, int refs_to_add,
1563 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001564{
Chris Mason5caf2a02007-04-02 11:20:42 -04001565 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001566 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001567 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001568 u64 refs;
1569 int ret;
1570 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001571
Chris Mason5caf2a02007-04-02 11:20:42 -04001572 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001573 if (!path)
1574 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001575
Chris Mason3c12ac72008-04-21 12:01:38 -04001576 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001577 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001578 /* this will setup the path even if it fails to insert the back ref */
1579 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1580 path, bytenr, num_bytes, parent,
1581 root_objectid, owner, offset,
1582 refs_to_add, extent_op);
1583 if (ret == 0)
1584 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001585
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001586 if (ret != -EAGAIN) {
1587 err = ret;
1588 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001589 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001590
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001591 leaf = path->nodes[0];
1592 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1593 refs = btrfs_extent_refs(leaf, item);
1594 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1595 if (extent_op)
1596 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001597
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001598 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001599 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001600
Chris Mason3c12ac72008-04-21 12:01:38 -04001601 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001602 path->leave_spinning = 1;
1603
Chris Mason56bec292009-03-13 10:10:06 -04001604 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001605 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001606 path, bytenr, parent, root_objectid,
1607 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001608 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001609out:
Chris Mason74493f72007-12-11 09:25:06 -05001610 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001611 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001612}
1613
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001614static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1615 struct btrfs_root *root,
1616 struct btrfs_delayed_ref_node *node,
1617 struct btrfs_delayed_extent_op *extent_op,
1618 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001619{
Chris Mason56bec292009-03-13 10:10:06 -04001620 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001621 struct btrfs_delayed_data_ref *ref;
1622 struct btrfs_key ins;
1623 u64 parent = 0;
1624 u64 ref_root = 0;
1625 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001626
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001627 ins.objectid = node->bytenr;
1628 ins.offset = node->num_bytes;
1629 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001630
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001631 ref = btrfs_delayed_node_to_data_ref(node);
1632 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1633 parent = ref->parent;
1634 else
1635 ref_root = ref->root;
1636
1637 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1638 if (extent_op) {
1639 BUG_ON(extent_op->update_key);
1640 flags |= extent_op->flags_to_set;
1641 }
1642 ret = alloc_reserved_file_extent(trans, root,
1643 parent, ref_root, flags,
1644 ref->objectid, ref->offset,
1645 &ins, node->ref_mod);
1646 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1647 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1648 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1649 node->num_bytes, parent,
1650 ref_root, ref->objectid,
1651 ref->offset, node->ref_mod,
1652 extent_op);
1653 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1654 ret = __btrfs_free_extent(trans, root, node->bytenr,
1655 node->num_bytes, parent,
1656 ref_root, ref->objectid,
1657 ref->offset, node->ref_mod,
1658 extent_op);
1659 } else {
1660 BUG();
1661 }
Chris Mason56bec292009-03-13 10:10:06 -04001662 return ret;
1663}
1664
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001665static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1666 struct extent_buffer *leaf,
1667 struct btrfs_extent_item *ei)
1668{
1669 u64 flags = btrfs_extent_flags(leaf, ei);
1670 if (extent_op->update_flags) {
1671 flags |= extent_op->flags_to_set;
1672 btrfs_set_extent_flags(leaf, ei, flags);
1673 }
1674
1675 if (extent_op->update_key) {
1676 struct btrfs_tree_block_info *bi;
1677 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1678 bi = (struct btrfs_tree_block_info *)(ei + 1);
1679 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1680 }
1681}
1682
1683static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1684 struct btrfs_root *root,
1685 struct btrfs_delayed_ref_node *node,
1686 struct btrfs_delayed_extent_op *extent_op)
1687{
1688 struct btrfs_key key;
1689 struct btrfs_path *path;
1690 struct btrfs_extent_item *ei;
1691 struct extent_buffer *leaf;
1692 u32 item_size;
1693 int ret;
1694 int err = 0;
1695
1696 path = btrfs_alloc_path();
1697 if (!path)
1698 return -ENOMEM;
1699
1700 key.objectid = node->bytenr;
1701 key.type = BTRFS_EXTENT_ITEM_KEY;
1702 key.offset = node->num_bytes;
1703
1704 path->reada = 1;
1705 path->leave_spinning = 1;
1706 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1707 path, 0, 1);
1708 if (ret < 0) {
1709 err = ret;
1710 goto out;
1711 }
1712 if (ret > 0) {
1713 err = -EIO;
1714 goto out;
1715 }
1716
1717 leaf = path->nodes[0];
1718 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1719#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1720 if (item_size < sizeof(*ei)) {
1721 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1722 path, (u64)-1, 0);
1723 if (ret < 0) {
1724 err = ret;
1725 goto out;
1726 }
1727 leaf = path->nodes[0];
1728 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1729 }
1730#endif
1731 BUG_ON(item_size < sizeof(*ei));
1732 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1733 __run_delayed_extent_op(extent_op, leaf, ei);
1734
1735 btrfs_mark_buffer_dirty(leaf);
1736out:
1737 btrfs_free_path(path);
1738 return err;
1739}
1740
1741static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1742 struct btrfs_root *root,
1743 struct btrfs_delayed_ref_node *node,
1744 struct btrfs_delayed_extent_op *extent_op,
1745 int insert_reserved)
1746{
1747 int ret = 0;
1748 struct btrfs_delayed_tree_ref *ref;
1749 struct btrfs_key ins;
1750 u64 parent = 0;
1751 u64 ref_root = 0;
1752
1753 ins.objectid = node->bytenr;
1754 ins.offset = node->num_bytes;
1755 ins.type = BTRFS_EXTENT_ITEM_KEY;
1756
1757 ref = btrfs_delayed_node_to_tree_ref(node);
1758 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1759 parent = ref->parent;
1760 else
1761 ref_root = ref->root;
1762
1763 BUG_ON(node->ref_mod != 1);
1764 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1765 BUG_ON(!extent_op || !extent_op->update_flags ||
1766 !extent_op->update_key);
1767 ret = alloc_reserved_tree_block(trans, root,
1768 parent, ref_root,
1769 extent_op->flags_to_set,
1770 &extent_op->key,
1771 ref->level, &ins);
1772 update_reserved_extents(root, ins.objectid, ins.offset, 0);
1773 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1774 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1775 node->num_bytes, parent, ref_root,
1776 ref->level, 0, 1, extent_op);
1777 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1778 ret = __btrfs_free_extent(trans, root, node->bytenr,
1779 node->num_bytes, parent, ref_root,
1780 ref->level, 0, 1, extent_op);
1781 } else {
1782 BUG();
1783 }
1784 return ret;
1785}
1786
1787
Chris Mason56bec292009-03-13 10:10:06 -04001788/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001789static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1790 struct btrfs_root *root,
1791 struct btrfs_delayed_ref_node *node,
1792 struct btrfs_delayed_extent_op *extent_op,
1793 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001794{
Josef Bacikeb099672009-02-12 09:27:38 -05001795 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001796 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001797 struct btrfs_delayed_ref_head *head;
1798 /*
1799 * we've hit the end of the chain and we were supposed
1800 * to insert this extent into the tree. But, it got
1801 * deleted before we ever needed to insert it, so all
1802 * we have to do is clean up the accounting
1803 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001804 BUG_ON(extent_op);
1805 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001806 if (insert_reserved) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001807 if (head->is_data) {
1808 ret = btrfs_del_csums(trans, root,
1809 node->bytenr,
1810 node->num_bytes);
1811 BUG_ON(ret);
1812 }
1813 btrfs_update_pinned_extents(root, node->bytenr,
Josef Bacik68b38552009-07-27 13:57:01 -04001814 node->num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04001815 update_reserved_extents(root, node->bytenr,
1816 node->num_bytes, 0);
1817 }
Chris Mason56bec292009-03-13 10:10:06 -04001818 mutex_unlock(&head->mutex);
1819 return 0;
1820 }
Josef Bacikeb099672009-02-12 09:27:38 -05001821
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001822 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1823 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1824 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1825 insert_reserved);
1826 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1827 node->type == BTRFS_SHARED_DATA_REF_KEY)
1828 ret = run_delayed_data_ref(trans, root, node, extent_op,
1829 insert_reserved);
1830 else
1831 BUG();
1832 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001833}
1834
Chris Mason56bec292009-03-13 10:10:06 -04001835static noinline struct btrfs_delayed_ref_node *
1836select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001837{
Chris Mason56bec292009-03-13 10:10:06 -04001838 struct rb_node *node;
1839 struct btrfs_delayed_ref_node *ref;
1840 int action = BTRFS_ADD_DELAYED_REF;
1841again:
1842 /*
1843 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1844 * this prevents ref count from going down to zero when
1845 * there still are pending delayed ref.
1846 */
1847 node = rb_prev(&head->node.rb_node);
1848 while (1) {
1849 if (!node)
1850 break;
1851 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1852 rb_node);
1853 if (ref->bytenr != head->node.bytenr)
1854 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001855 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001856 return ref;
1857 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001858 }
Chris Mason56bec292009-03-13 10:10:06 -04001859 if (action == BTRFS_ADD_DELAYED_REF) {
1860 action = BTRFS_DROP_DELAYED_REF;
1861 goto again;
1862 }
1863 return NULL;
1864}
1865
Chris Masonc3e69d52009-03-13 10:17:05 -04001866static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1867 struct btrfs_root *root,
1868 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001869{
Chris Mason56bec292009-03-13 10:10:06 -04001870 struct btrfs_delayed_ref_root *delayed_refs;
1871 struct btrfs_delayed_ref_node *ref;
1872 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001873 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001874 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001875 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001876 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001877
1878 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001879 while (1) {
1880 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001881 /* pick a new head ref from the cluster list */
1882 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001883 break;
Chris Mason56bec292009-03-13 10:10:06 -04001884
Chris Masonc3e69d52009-03-13 10:17:05 -04001885 locked_ref = list_entry(cluster->next,
1886 struct btrfs_delayed_ref_head, cluster);
1887
1888 /* grab the lock that says we are going to process
1889 * all the refs for this head */
1890 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1891
1892 /*
1893 * we may have dropped the spin lock to get the head
1894 * mutex lock, and that might have given someone else
1895 * time to free the head. If that's true, it has been
1896 * removed from our list and we can move on.
1897 */
1898 if (ret == -EAGAIN) {
1899 locked_ref = NULL;
1900 count++;
1901 continue;
Chris Mason56bec292009-03-13 10:10:06 -04001902 }
1903 }
1904
1905 /*
1906 * record the must insert reserved flag before we
1907 * drop the spin lock.
1908 */
1909 must_insert_reserved = locked_ref->must_insert_reserved;
1910 locked_ref->must_insert_reserved = 0;
1911
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001912 extent_op = locked_ref->extent_op;
1913 locked_ref->extent_op = NULL;
1914
Chris Mason56bec292009-03-13 10:10:06 -04001915 /*
1916 * locked_ref is the head node, so we have to go one
1917 * node back for any delayed ref updates
1918 */
Chris Mason56bec292009-03-13 10:10:06 -04001919 ref = select_delayed_ref(locked_ref);
1920 if (!ref) {
1921 /* All delayed refs have been processed, Go ahead
1922 * and send the head node to run_one_delayed_ref,
1923 * so that any accounting fixes can happen
1924 */
1925 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001926
1927 if (extent_op && must_insert_reserved) {
1928 kfree(extent_op);
1929 extent_op = NULL;
1930 }
1931
1932 if (extent_op) {
1933 spin_unlock(&delayed_refs->lock);
1934
1935 ret = run_delayed_extent_op(trans, root,
1936 ref, extent_op);
1937 BUG_ON(ret);
1938 kfree(extent_op);
1939
1940 cond_resched();
1941 spin_lock(&delayed_refs->lock);
1942 continue;
1943 }
1944
Chris Masonc3e69d52009-03-13 10:17:05 -04001945 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04001946 locked_ref = NULL;
1947 }
1948
1949 ref->in_tree = 0;
1950 rb_erase(&ref->rb_node, &delayed_refs->root);
1951 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001952
Chris Mason56bec292009-03-13 10:10:06 -04001953 spin_unlock(&delayed_refs->lock);
1954
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001955 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04001956 must_insert_reserved);
1957 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04001958
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001959 btrfs_put_delayed_ref(ref);
1960 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04001961 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001962
Chris Mason1887be62009-03-13 10:11:24 -04001963 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001964 spin_lock(&delayed_refs->lock);
1965 }
Chris Masonc3e69d52009-03-13 10:17:05 -04001966 return count;
1967}
1968
1969/*
1970 * this starts processing the delayed reference count updates and
1971 * extent insertions we have queued up so far. count can be
1972 * 0, which means to process everything in the tree at the start
1973 * of the run (but not newly added entries), or it can be some target
1974 * number you'd like to process.
1975 */
1976int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1977 struct btrfs_root *root, unsigned long count)
1978{
1979 struct rb_node *node;
1980 struct btrfs_delayed_ref_root *delayed_refs;
1981 struct btrfs_delayed_ref_node *ref;
1982 struct list_head cluster;
1983 int ret;
1984 int run_all = count == (unsigned long)-1;
1985 int run_most = 0;
1986
1987 if (root == root->fs_info->extent_root)
1988 root = root->fs_info->tree_root;
1989
1990 delayed_refs = &trans->transaction->delayed_refs;
1991 INIT_LIST_HEAD(&cluster);
1992again:
1993 spin_lock(&delayed_refs->lock);
1994 if (count == 0) {
1995 count = delayed_refs->num_entries * 2;
1996 run_most = 1;
1997 }
1998 while (1) {
1999 if (!(run_all || run_most) &&
2000 delayed_refs->num_heads_ready < 64)
2001 break;
2002
2003 /*
2004 * go find something we can process in the rbtree. We start at
2005 * the beginning of the tree, and then build a cluster
2006 * of refs to process starting at the first one we are able to
2007 * lock
2008 */
2009 ret = btrfs_find_ref_cluster(trans, &cluster,
2010 delayed_refs->run_delayed_start);
2011 if (ret)
2012 break;
2013
2014 ret = run_clustered_refs(trans, root, &cluster);
2015 BUG_ON(ret < 0);
2016
2017 count -= min_t(unsigned long, ret, count);
2018
2019 if (count == 0)
2020 break;
2021 }
2022
Chris Mason56bec292009-03-13 10:10:06 -04002023 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002024 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002025 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002026 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002027 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002028
2029 while (node) {
2030 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2031 rb_node);
2032 if (btrfs_delayed_ref_is_head(ref)) {
2033 struct btrfs_delayed_ref_head *head;
2034
2035 head = btrfs_delayed_node_to_head(ref);
2036 atomic_inc(&ref->refs);
2037
2038 spin_unlock(&delayed_refs->lock);
2039 mutex_lock(&head->mutex);
2040 mutex_unlock(&head->mutex);
2041
2042 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002043 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002044 goto again;
2045 }
2046 node = rb_next(node);
2047 }
2048 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002049 schedule_timeout(1);
2050 goto again;
2051 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002052out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002053 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002054 return 0;
2055}
2056
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002057int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2058 struct btrfs_root *root,
2059 u64 bytenr, u64 num_bytes, u64 flags,
2060 int is_data)
2061{
2062 struct btrfs_delayed_extent_op *extent_op;
2063 int ret;
2064
2065 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2066 if (!extent_op)
2067 return -ENOMEM;
2068
2069 extent_op->flags_to_set = flags;
2070 extent_op->update_flags = 1;
2071 extent_op->update_key = 0;
2072 extent_op->is_data = is_data ? 1 : 0;
2073
2074 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2075 if (ret)
2076 kfree(extent_op);
2077 return ret;
2078}
2079
2080static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2081 struct btrfs_root *root,
2082 struct btrfs_path *path,
2083 u64 objectid, u64 offset, u64 bytenr)
2084{
2085 struct btrfs_delayed_ref_head *head;
2086 struct btrfs_delayed_ref_node *ref;
2087 struct btrfs_delayed_data_ref *data_ref;
2088 struct btrfs_delayed_ref_root *delayed_refs;
2089 struct rb_node *node;
2090 int ret = 0;
2091
2092 ret = -ENOENT;
2093 delayed_refs = &trans->transaction->delayed_refs;
2094 spin_lock(&delayed_refs->lock);
2095 head = btrfs_find_delayed_ref_head(trans, bytenr);
2096 if (!head)
2097 goto out;
2098
2099 if (!mutex_trylock(&head->mutex)) {
2100 atomic_inc(&head->node.refs);
2101 spin_unlock(&delayed_refs->lock);
2102
2103 btrfs_release_path(root->fs_info->extent_root, path);
2104
2105 mutex_lock(&head->mutex);
2106 mutex_unlock(&head->mutex);
2107 btrfs_put_delayed_ref(&head->node);
2108 return -EAGAIN;
2109 }
2110
2111 node = rb_prev(&head->node.rb_node);
2112 if (!node)
2113 goto out_unlock;
2114
2115 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2116
2117 if (ref->bytenr != bytenr)
2118 goto out_unlock;
2119
2120 ret = 1;
2121 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2122 goto out_unlock;
2123
2124 data_ref = btrfs_delayed_node_to_data_ref(ref);
2125
2126 node = rb_prev(node);
2127 if (node) {
2128 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2129 if (ref->bytenr == bytenr)
2130 goto out_unlock;
2131 }
2132
2133 if (data_ref->root != root->root_key.objectid ||
2134 data_ref->objectid != objectid || data_ref->offset != offset)
2135 goto out_unlock;
2136
2137 ret = 0;
2138out_unlock:
2139 mutex_unlock(&head->mutex);
2140out:
2141 spin_unlock(&delayed_refs->lock);
2142 return ret;
2143}
2144
2145static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2146 struct btrfs_root *root,
2147 struct btrfs_path *path,
2148 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002149{
2150 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002151 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002152 struct btrfs_extent_data_ref *ref;
2153 struct btrfs_extent_inline_ref *iref;
2154 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002155 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002156 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002157 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002158
Chris Masonbe20aa92007-12-17 20:14:01 -05002159 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002160 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002161 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002162
Chris Masonbe20aa92007-12-17 20:14:01 -05002163 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2164 if (ret < 0)
2165 goto out;
2166 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002167
2168 ret = -ENOENT;
2169 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002170 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002171
Zheng Yan31840ae2008-09-23 13:14:14 -04002172 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002173 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002174 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002175
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002176 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002177 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002178
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002179 ret = 1;
2180 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2181#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2182 if (item_size < sizeof(*ei)) {
2183 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2184 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002185 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002186#endif
2187 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2188
2189 if (item_size != sizeof(*ei) +
2190 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2191 goto out;
2192
2193 if (btrfs_extent_generation(leaf, ei) <=
2194 btrfs_root_last_snapshot(&root->root_item))
2195 goto out;
2196
2197 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2198 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2199 BTRFS_EXTENT_DATA_REF_KEY)
2200 goto out;
2201
2202 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2203 if (btrfs_extent_refs(leaf, ei) !=
2204 btrfs_extent_data_ref_count(leaf, ref) ||
2205 btrfs_extent_data_ref_root(leaf, ref) !=
2206 root->root_key.objectid ||
2207 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2208 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2209 goto out;
2210
Yan Zhengf321e492008-07-30 09:26:11 -04002211 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002212out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002213 return ret;
2214}
2215
2216int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2217 struct btrfs_root *root,
2218 u64 objectid, u64 offset, u64 bytenr)
2219{
2220 struct btrfs_path *path;
2221 int ret;
2222 int ret2;
2223
2224 path = btrfs_alloc_path();
2225 if (!path)
2226 return -ENOENT;
2227
2228 do {
2229 ret = check_committed_ref(trans, root, path, objectid,
2230 offset, bytenr);
2231 if (ret && ret != -ENOENT)
2232 goto out;
2233
2234 ret2 = check_delayed_ref(trans, root, path, objectid,
2235 offset, bytenr);
2236 } while (ret2 == -EAGAIN);
2237
2238 if (ret2 && ret2 != -ENOENT) {
2239 ret = ret2;
2240 goto out;
2241 }
2242
2243 if (ret != -ENOENT || ret2 != -ENOENT)
2244 ret = 0;
2245out:
Yan Zhengf321e492008-07-30 09:26:11 -04002246 btrfs_free_path(path);
2247 return ret;
2248}
2249
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002250#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002251int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2252 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002253{
Chris Mason5f39d392007-10-15 16:14:19 -04002254 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002255 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002256 u64 root_gen;
2257 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002258 int i;
Chris Masondb945352007-10-15 16:15:53 -04002259 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002260 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002261 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002262
Chris Mason3768f362007-03-13 16:47:54 -04002263 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002264 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002265
Zheng Yane4657682008-09-26 10:04:53 -04002266 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2267 shared = 0;
2268 root_gen = root->root_key.offset;
2269 } else {
2270 shared = 1;
2271 root_gen = trans->transid - 1;
2272 }
2273
Chris Masondb945352007-10-15 16:15:53 -04002274 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002275 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002276
Zheng Yan31840ae2008-09-23 13:14:14 -04002277 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002278 struct btrfs_leaf_ref *ref;
2279 struct btrfs_extent_info *info;
2280
Zheng Yan31840ae2008-09-23 13:14:14 -04002281 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002282 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002283 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002284 goto out;
2285 }
2286
Zheng Yane4657682008-09-26 10:04:53 -04002287 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002288 ref->bytenr = buf->start;
2289 ref->owner = btrfs_header_owner(buf);
2290 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002291 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002292 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002293
Zheng Yan31840ae2008-09-23 13:14:14 -04002294 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002295 u64 disk_bytenr;
2296 btrfs_item_key_to_cpu(buf, &key, i);
2297 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2298 continue;
2299 fi = btrfs_item_ptr(buf, i,
2300 struct btrfs_file_extent_item);
2301 if (btrfs_file_extent_type(buf, fi) ==
2302 BTRFS_FILE_EXTENT_INLINE)
2303 continue;
2304 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2305 if (disk_bytenr == 0)
2306 continue;
2307
2308 info->bytenr = disk_bytenr;
2309 info->num_bytes =
2310 btrfs_file_extent_disk_num_bytes(buf, fi);
2311 info->objectid = key.objectid;
2312 info->offset = key.offset;
2313 info++;
2314 }
2315
Zheng Yane4657682008-09-26 10:04:53 -04002316 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002317 if (ret == -EEXIST && shared) {
2318 struct btrfs_leaf_ref *old;
2319 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2320 BUG_ON(!old);
2321 btrfs_remove_leaf_ref(root, old);
2322 btrfs_free_leaf_ref(root, old);
2323 ret = btrfs_add_leaf_ref(root, ref, shared);
2324 }
Yan Zheng31153d82008-07-28 15:32:19 -04002325 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002326 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002327 }
2328out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002329 return ret;
2330}
2331
Chris Masonb7a9f292009-02-04 09:23:45 -05002332/* when a block goes through cow, we update the reference counts of
2333 * everything that block points to. The internal pointers of the block
2334 * can be in just about any order, and it is likely to have clusters of
2335 * things that are close together and clusters of things that are not.
2336 *
2337 * To help reduce the seeks that come with updating all of these reference
2338 * counts, sort them by byte number before actual updates are done.
2339 *
2340 * struct refsort is used to match byte number to slot in the btree block.
2341 * we sort based on the byte number and then use the slot to actually
2342 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002343 *
2344 * struct refsort is smaller than strcut btrfs_item and smaller than
2345 * struct btrfs_key_ptr. Since we're currently limited to the page size
2346 * for a btree block, there's no way for a kmalloc of refsorts for a
2347 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002348 */
2349struct refsort {
2350 u64 bytenr;
2351 u32 slot;
2352};
2353
2354/*
2355 * for passing into sort()
2356 */
2357static int refsort_cmp(const void *a_void, const void *b_void)
2358{
2359 const struct refsort *a = a_void;
2360 const struct refsort *b = b_void;
2361
2362 if (a->bytenr < b->bytenr)
2363 return -1;
2364 if (a->bytenr > b->bytenr)
2365 return 1;
2366 return 0;
2367}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002368#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002369
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002370static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002371 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002372 struct extent_buffer *buf,
2373 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002374{
2375 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002376 u64 num_bytes;
2377 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002378 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002379 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002380 struct btrfs_key key;
2381 struct btrfs_file_extent_item *fi;
2382 int i;
2383 int level;
2384 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002385 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002386 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002387
2388 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002389 nritems = btrfs_header_nritems(buf);
2390 level = btrfs_header_level(buf);
2391
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002392 if (!root->ref_cows && level == 0)
2393 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002394
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002395 if (inc)
2396 process_func = btrfs_inc_extent_ref;
2397 else
2398 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002399
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002400 if (full_backref)
2401 parent = buf->start;
2402 else
2403 parent = 0;
2404
Zheng Yan31840ae2008-09-23 13:14:14 -04002405 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002406 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002407 btrfs_item_key_to_cpu(buf, &key, i);
2408 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002409 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002410 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002411 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002412 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002413 BTRFS_FILE_EXTENT_INLINE)
2414 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002415 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2416 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002417 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002418
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002419 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2420 key.offset -= btrfs_file_extent_offset(buf, fi);
2421 ret = process_func(trans, root, bytenr, num_bytes,
2422 parent, ref_root, key.objectid,
2423 key.offset);
2424 if (ret)
2425 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002426 } else {
2427 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002428 num_bytes = btrfs_level_size(root, level - 1);
2429 ret = process_func(trans, root, bytenr, num_bytes,
2430 parent, ref_root, level - 1, 0);
2431 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002432 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002433 }
2434 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002435 return 0;
2436fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002437 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002438 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002439}
2440
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002441int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2442 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002443{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002444 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2445}
Zheng Yan31840ae2008-09-23 13:14:14 -04002446
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002447int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2448 struct extent_buffer *buf, int full_backref)
2449{
2450 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002451}
2452
Chris Mason9078a3e2007-04-26 16:46:15 -04002453static int write_one_cache_group(struct btrfs_trans_handle *trans,
2454 struct btrfs_root *root,
2455 struct btrfs_path *path,
2456 struct btrfs_block_group_cache *cache)
2457{
2458 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002459 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002460 unsigned long bi;
2461 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002462
Chris Mason9078a3e2007-04-26 16:46:15 -04002463 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002464 if (ret < 0)
2465 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002466 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002467
2468 leaf = path->nodes[0];
2469 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2470 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2471 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002472 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002473fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002474 if (ret)
2475 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002476 return 0;
2477
2478}
2479
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002480static struct btrfs_block_group_cache *
2481next_block_group(struct btrfs_root *root,
2482 struct btrfs_block_group_cache *cache)
2483{
2484 struct rb_node *node;
2485 spin_lock(&root->fs_info->block_group_cache_lock);
2486 node = rb_next(&cache->cache_node);
2487 btrfs_put_block_group(cache);
2488 if (node) {
2489 cache = rb_entry(node, struct btrfs_block_group_cache,
2490 cache_node);
2491 atomic_inc(&cache->count);
2492 } else
2493 cache = NULL;
2494 spin_unlock(&root->fs_info->block_group_cache_lock);
2495 return cache;
2496}
2497
Chris Mason96b51792007-10-15 16:15:19 -04002498int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2499 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002500{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002501 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002502 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002503 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002504 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002505
2506 path = btrfs_alloc_path();
2507 if (!path)
2508 return -ENOMEM;
2509
Chris Masond3977122009-01-05 21:25:51 -05002510 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002511 if (last == 0) {
2512 err = btrfs_run_delayed_refs(trans, root,
2513 (unsigned long)-1);
2514 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002515 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002516
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002517 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2518 while (cache) {
2519 if (cache->dirty)
2520 break;
2521 cache = next_block_group(root, cache);
2522 }
2523 if (!cache) {
2524 if (last == 0)
2525 break;
2526 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002527 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002528 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002529
2530 cache->dirty = 0;
2531 last = cache->key.objectid + cache->key.offset;
2532
2533 err = write_one_cache_group(trans, root, path, cache);
2534 BUG_ON(err);
2535 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002536 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002537
Chris Mason9078a3e2007-04-26 16:46:15 -04002538 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002539 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002540}
2541
Yan Zhengd2fb3432008-12-11 16:30:39 -05002542int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2543{
2544 struct btrfs_block_group_cache *block_group;
2545 int readonly = 0;
2546
2547 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2548 if (!block_group || block_group->ro)
2549 readonly = 1;
2550 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002551 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002552 return readonly;
2553}
2554
Chris Mason593060d2008-03-25 16:50:33 -04002555static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2556 u64 total_bytes, u64 bytes_used,
2557 struct btrfs_space_info **space_info)
2558{
2559 struct btrfs_space_info *found;
2560
2561 found = __find_space_info(info, flags);
2562 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002563 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002564 found->total_bytes += total_bytes;
2565 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002566 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002567 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002568 *space_info = found;
2569 return 0;
2570 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002571 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002572 if (!found)
2573 return -ENOMEM;
2574
Josef Bacik0f9dd462008-09-23 13:14:11 -04002575 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002576 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002577 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002578 found->flags = flags;
2579 found->total_bytes = total_bytes;
2580 found->bytes_used = bytes_used;
2581 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002582 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002583 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002584 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002585 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002586 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002587 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002588 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002589 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002590 return 0;
2591}
2592
Chris Mason8790d502008-04-03 16:29:03 -04002593static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2594{
2595 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002596 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002597 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002598 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002599 if (extra_flags) {
2600 if (flags & BTRFS_BLOCK_GROUP_DATA)
2601 fs_info->avail_data_alloc_bits |= extra_flags;
2602 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2603 fs_info->avail_metadata_alloc_bits |= extra_flags;
2604 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2605 fs_info->avail_system_alloc_bits |= extra_flags;
2606 }
2607}
Chris Mason593060d2008-03-25 16:50:33 -04002608
Yan Zhengc146afa2008-11-12 14:34:12 -05002609static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2610{
2611 spin_lock(&cache->space_info->lock);
2612 spin_lock(&cache->lock);
2613 if (!cache->ro) {
2614 cache->space_info->bytes_readonly += cache->key.offset -
2615 btrfs_block_group_used(&cache->item);
2616 cache->ro = 1;
2617 }
2618 spin_unlock(&cache->lock);
2619 spin_unlock(&cache->space_info->lock);
2620}
2621
Yan Zheng2b820322008-11-17 21:11:30 -05002622u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002623{
Yan Zheng2b820322008-11-17 21:11:30 -05002624 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002625
2626 if (num_devices == 1)
2627 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2628 if (num_devices < 4)
2629 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2630
Chris Masonec44a352008-04-28 15:29:52 -04002631 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2632 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002633 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002634 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002635 }
Chris Masonec44a352008-04-28 15:29:52 -04002636
2637 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002638 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002639 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002640 }
Chris Masonec44a352008-04-28 15:29:52 -04002641
2642 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2643 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2644 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2645 (flags & BTRFS_BLOCK_GROUP_DUP)))
2646 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2647 return flags;
2648}
2649
Josef Bacik6a632092009-02-20 11:00:09 -05002650static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2651{
2652 struct btrfs_fs_info *info = root->fs_info;
2653 u64 alloc_profile;
2654
2655 if (data) {
2656 alloc_profile = info->avail_data_alloc_bits &
2657 info->data_alloc_profile;
2658 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2659 } else if (root == root->fs_info->chunk_root) {
2660 alloc_profile = info->avail_system_alloc_bits &
2661 info->system_alloc_profile;
2662 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2663 } else {
2664 alloc_profile = info->avail_metadata_alloc_bits &
2665 info->metadata_alloc_profile;
2666 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2667 }
2668
2669 return btrfs_reduce_alloc_profile(root, data);
2670}
2671
2672void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2673{
2674 u64 alloc_target;
2675
2676 alloc_target = btrfs_get_alloc_profile(root, 1);
2677 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2678 alloc_target);
2679}
2680
2681/*
2682 * for now this just makes sure we have at least 5% of our metadata space free
2683 * for use.
2684 */
2685int btrfs_check_metadata_free_space(struct btrfs_root *root)
2686{
2687 struct btrfs_fs_info *info = root->fs_info;
2688 struct btrfs_space_info *meta_sinfo;
2689 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002690 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002691
2692 /* get the space info for where the metadata will live */
2693 alloc_target = btrfs_get_alloc_profile(root, 0);
2694 meta_sinfo = __find_space_info(info, alloc_target);
2695
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002696again:
Josef Bacik6a632092009-02-20 11:00:09 -05002697 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002698 if (!meta_sinfo->full)
2699 thresh = meta_sinfo->total_bytes * 80;
2700 else
2701 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05002702
2703 do_div(thresh, 100);
2704
2705 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2706 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002707 struct btrfs_trans_handle *trans;
2708 if (!meta_sinfo->full) {
2709 meta_sinfo->force_alloc = 1;
2710 spin_unlock(&meta_sinfo->lock);
2711
2712 trans = btrfs_start_transaction(root, 1);
2713 if (!trans)
2714 return -ENOMEM;
2715
2716 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2717 2 * 1024 * 1024, alloc_target, 0);
2718 btrfs_end_transaction(trans, root);
2719 goto again;
2720 }
Josef Bacik6a632092009-02-20 11:00:09 -05002721 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002722
2723 if (!committed) {
2724 committed = 1;
2725 trans = btrfs_join_transaction(root, 1);
2726 if (!trans)
2727 return -ENOMEM;
2728 ret = btrfs_commit_transaction(trans, root);
2729 if (ret)
2730 return ret;
2731 goto again;
2732 }
Josef Bacik6a632092009-02-20 11:00:09 -05002733 return -ENOSPC;
2734 }
2735 spin_unlock(&meta_sinfo->lock);
2736
2737 return 0;
2738}
2739
2740/*
2741 * This will check the space that the inode allocates from to make sure we have
2742 * enough space for bytes.
2743 */
2744int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
2745 u64 bytes)
2746{
2747 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002748 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002749
2750 /* make sure bytes are sectorsize aligned */
2751 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2752
2753 data_sinfo = BTRFS_I(inode)->space_info;
2754again:
2755 /* make sure we have enough space to handle the data first */
2756 spin_lock(&data_sinfo->lock);
2757 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
2758 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
2759 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
2760 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002761 struct btrfs_trans_handle *trans;
2762
Josef Bacik6a632092009-02-20 11:00:09 -05002763 /*
2764 * if we don't have enough free bytes in this space then we need
2765 * to alloc a new chunk.
2766 */
2767 if (!data_sinfo->full) {
2768 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05002769
2770 data_sinfo->force_alloc = 1;
2771 spin_unlock(&data_sinfo->lock);
2772
2773 alloc_target = btrfs_get_alloc_profile(root, 1);
2774 trans = btrfs_start_transaction(root, 1);
2775 if (!trans)
2776 return -ENOMEM;
2777
2778 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2779 bytes + 2 * 1024 * 1024,
2780 alloc_target, 0);
2781 btrfs_end_transaction(trans, root);
2782 if (ret)
2783 return ret;
2784 goto again;
2785 }
2786 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002787
2788 /* commit the current transaction and try again */
2789 if (!committed) {
2790 committed = 1;
2791 trans = btrfs_join_transaction(root, 1);
2792 if (!trans)
2793 return -ENOMEM;
2794 ret = btrfs_commit_transaction(trans, root);
2795 if (ret)
2796 return ret;
2797 goto again;
2798 }
2799
Josef Bacik6a632092009-02-20 11:00:09 -05002800 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2801 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04002802 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07002803 "%llu total\n", (unsigned long long)bytes,
2804 (unsigned long long)data_sinfo->bytes_delalloc,
2805 (unsigned long long)data_sinfo->bytes_used,
2806 (unsigned long long)data_sinfo->bytes_reserved,
2807 (unsigned long long)data_sinfo->bytes_pinned,
2808 (unsigned long long)data_sinfo->bytes_readonly,
2809 (unsigned long long)data_sinfo->bytes_may_use,
2810 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05002811 return -ENOSPC;
2812 }
2813 data_sinfo->bytes_may_use += bytes;
2814 BTRFS_I(inode)->reserved_bytes += bytes;
2815 spin_unlock(&data_sinfo->lock);
2816
2817 return btrfs_check_metadata_free_space(root);
2818}
2819
2820/*
2821 * if there was an error for whatever reason after calling
2822 * btrfs_check_data_free_space, call this so we can cleanup the counters.
2823 */
2824void btrfs_free_reserved_data_space(struct btrfs_root *root,
2825 struct inode *inode, u64 bytes)
2826{
2827 struct btrfs_space_info *data_sinfo;
2828
2829 /* make sure bytes are sectorsize aligned */
2830 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2831
2832 data_sinfo = BTRFS_I(inode)->space_info;
2833 spin_lock(&data_sinfo->lock);
2834 data_sinfo->bytes_may_use -= bytes;
2835 BTRFS_I(inode)->reserved_bytes -= bytes;
2836 spin_unlock(&data_sinfo->lock);
2837}
2838
2839/* called when we are adding a delalloc extent to the inode's io_tree */
2840void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
2841 u64 bytes)
2842{
2843 struct btrfs_space_info *data_sinfo;
2844
2845 /* get the space info for where this inode will be storing its data */
2846 data_sinfo = BTRFS_I(inode)->space_info;
2847
2848 /* make sure we have enough space to handle the data first */
2849 spin_lock(&data_sinfo->lock);
2850 data_sinfo->bytes_delalloc += bytes;
2851
2852 /*
2853 * we are adding a delalloc extent without calling
2854 * btrfs_check_data_free_space first. This happens on a weird
2855 * writepage condition, but shouldn't hurt our accounting
2856 */
2857 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
2858 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
2859 BTRFS_I(inode)->reserved_bytes = 0;
2860 } else {
2861 data_sinfo->bytes_may_use -= bytes;
2862 BTRFS_I(inode)->reserved_bytes -= bytes;
2863 }
2864
2865 spin_unlock(&data_sinfo->lock);
2866}
2867
2868/* called when we are clearing an delalloc extent from the inode's io_tree */
2869void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2870 u64 bytes)
2871{
2872 struct btrfs_space_info *info;
2873
2874 info = BTRFS_I(inode)->space_info;
2875
2876 spin_lock(&info->lock);
2877 info->bytes_delalloc -= bytes;
2878 spin_unlock(&info->lock);
2879}
2880
Josef Bacik97e728d2009-04-21 17:40:57 -04002881static void force_metadata_allocation(struct btrfs_fs_info *info)
2882{
2883 struct list_head *head = &info->space_info;
2884 struct btrfs_space_info *found;
2885
2886 rcu_read_lock();
2887 list_for_each_entry_rcu(found, head, list) {
2888 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
2889 found->force_alloc = 1;
2890 }
2891 rcu_read_unlock();
2892}
2893
Chris Mason6324fbf2008-03-24 15:01:59 -04002894static int do_chunk_alloc(struct btrfs_trans_handle *trans,
2895 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04002896 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04002897{
2898 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04002899 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04002900 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05002901 int ret = 0;
2902
Josef Bacik97e728d2009-04-21 17:40:57 -04002903 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04002904
Yan Zheng2b820322008-11-17 21:11:30 -05002905 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04002906
Chris Mason6324fbf2008-03-24 15:01:59 -04002907 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04002908 if (!space_info) {
2909 ret = update_space_info(extent_root->fs_info, flags,
2910 0, 0, &space_info);
2911 BUG_ON(ret);
2912 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002913 BUG_ON(!space_info);
2914
Josef Bacik25179202008-10-29 14:49:05 -04002915 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04002916 if (space_info->force_alloc) {
2917 force = 1;
2918 space_info->force_alloc = 0;
2919 }
Josef Bacik25179202008-10-29 14:49:05 -04002920 if (space_info->full) {
2921 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002922 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002923 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002924
Yan Zhengc146afa2008-11-12 14:34:12 -05002925 thresh = space_info->total_bytes - space_info->bytes_readonly;
2926 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04002927 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04002928 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04002929 space_info->bytes_reserved + alloc_bytes) < thresh) {
2930 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002931 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002932 }
Josef Bacik25179202008-10-29 14:49:05 -04002933 spin_unlock(&space_info->lock);
2934
Josef Bacik97e728d2009-04-21 17:40:57 -04002935 /*
2936 * if we're doing a data chunk, go ahead and make sure that
2937 * we keep a reasonable number of metadata chunks allocated in the
2938 * FS as well.
2939 */
2940 if (flags & BTRFS_BLOCK_GROUP_DATA) {
2941 fs_info->data_chunk_allocations++;
2942 if (!(fs_info->data_chunk_allocations %
2943 fs_info->metadata_ratio))
2944 force_metadata_allocation(fs_info);
2945 }
2946
Yan Zheng2b820322008-11-17 21:11:30 -05002947 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05002948 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04002949 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04002950out:
Yan Zhengc146afa2008-11-12 14:34:12 -05002951 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002952 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04002953}
2954
Chris Mason9078a3e2007-04-26 16:46:15 -04002955static int update_block_group(struct btrfs_trans_handle *trans,
2956 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04002957 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04002958 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04002959{
2960 struct btrfs_block_group_cache *cache;
2961 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04002962 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002963 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04002964 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04002965
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002966 /* block accounting for super block */
2967 spin_lock(&info->delalloc_lock);
2968 old_val = btrfs_super_bytes_used(&info->super_copy);
2969 if (alloc)
2970 old_val += num_bytes;
2971 else
2972 old_val -= num_bytes;
2973 btrfs_set_super_bytes_used(&info->super_copy, old_val);
2974
2975 /* block accounting for root item */
2976 old_val = btrfs_root_used(&root->root_item);
2977 if (alloc)
2978 old_val += num_bytes;
2979 else
2980 old_val -= num_bytes;
2981 btrfs_set_root_used(&root->root_item, old_val);
2982 spin_unlock(&info->delalloc_lock);
2983
Chris Masond3977122009-01-05 21:25:51 -05002984 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04002985 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002986 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04002987 return -1;
Chris Masondb945352007-10-15 16:15:53 -04002988 byte_in_group = bytenr - cache->key.objectid;
2989 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04002990
Josef Bacik25179202008-10-29 14:49:05 -04002991 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04002992 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002993 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04002994 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04002995 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04002996 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04002997 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002998 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05002999 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05003000 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003001 btrfs_set_block_group_used(&cache->item, old_val);
3002 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003003 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003004 } else {
Chris Masondb945352007-10-15 16:15:53 -04003005 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003006 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05003007 if (cache->ro)
3008 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003009 btrfs_set_block_group_used(&cache->item, old_val);
3010 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003011 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04003012 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003013 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003014
3015 ret = btrfs_discard_extent(root, bytenr,
3016 num_bytes);
3017 WARN_ON(ret);
3018
Josef Bacik0f9dd462008-09-23 13:14:11 -04003019 ret = btrfs_add_free_space(cache, bytenr,
3020 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003021 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04003022 }
Chris Masoncd1bc462007-04-27 10:08:34 -04003023 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003024 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003025 total -= num_bytes;
3026 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003027 }
3028 return 0;
3029}
Chris Mason6324fbf2008-03-24 15:01:59 -04003030
Chris Masona061fc82008-05-07 11:43:44 -04003031static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3032{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003033 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003034 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003035
3036 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3037 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003038 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003039
Yan Zhengd2fb3432008-12-11 16:30:39 -05003040 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003041 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003042
3043 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003044}
3045
Chris Masone02119d2008-09-05 16:13:11 -04003046int btrfs_update_pinned_extents(struct btrfs_root *root,
Josef Bacik68b38552009-07-27 13:57:01 -04003047 u64 bytenr, u64 num, int pin)
Yan324ae4d2007-11-16 14:57:08 -05003048{
3049 u64 len;
3050 struct btrfs_block_group_cache *cache;
3051 struct btrfs_fs_info *fs_info = root->fs_info;
3052
Josef Bacik68b38552009-07-27 13:57:01 -04003053 if (pin)
Yan324ae4d2007-11-16 14:57:08 -05003054 set_extent_dirty(&fs_info->pinned_extents,
3055 bytenr, bytenr + num - 1, GFP_NOFS);
Chris Masonb9473432009-03-13 11:00:37 -04003056
Yan324ae4d2007-11-16 14:57:08 -05003057 while (num > 0) {
3058 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04003059 BUG_ON(!cache);
3060 len = min(num, cache->key.offset -
3061 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05003062 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04003063 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04003064 spin_lock(&cache->lock);
3065 cache->pinned += len;
3066 cache->space_info->bytes_pinned += len;
3067 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003068 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05003069 fs_info->total_pinned += len;
3070 } else {
Josef Bacik68b38552009-07-27 13:57:01 -04003071 int unpin = 0;
3072
3073 /*
3074 * in order to not race with the block group caching, we
3075 * only want to unpin the extent if we are cached. If
3076 * we aren't cached, we want to start async caching this
3077 * block group so we can free the extent the next time
3078 * around.
3079 */
Josef Bacik25179202008-10-29 14:49:05 -04003080 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04003081 spin_lock(&cache->lock);
Josef Bacik68b38552009-07-27 13:57:01 -04003082 unpin = (cache->cached == BTRFS_CACHE_FINISHED);
3083 if (likely(unpin)) {
3084 cache->pinned -= len;
3085 cache->space_info->bytes_pinned -= len;
3086 fs_info->total_pinned -= len;
3087 }
Zheng Yane8569812008-09-26 10:05:48 -04003088 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003089 spin_unlock(&cache->space_info->lock);
Josef Bacik68b38552009-07-27 13:57:01 -04003090
3091 if (likely(unpin))
3092 clear_extent_dirty(&fs_info->pinned_extents,
3093 bytenr, bytenr + len -1,
3094 GFP_NOFS);
3095 else
3096 cache_block_group(cache);
3097
3098 if (unpin)
Josef Bacik07103a32008-11-19 15:17:55 -05003099 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05003100 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003101 btrfs_put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05003102 bytenr += len;
3103 num -= len;
3104 }
3105 return 0;
3106}
Chris Mason9078a3e2007-04-26 16:46:15 -04003107
Zheng Yane8569812008-09-26 10:05:48 -04003108static int update_reserved_extents(struct btrfs_root *root,
3109 u64 bytenr, u64 num, int reserve)
3110{
3111 u64 len;
3112 struct btrfs_block_group_cache *cache;
3113 struct btrfs_fs_info *fs_info = root->fs_info;
3114
Zheng Yane8569812008-09-26 10:05:48 -04003115 while (num > 0) {
3116 cache = btrfs_lookup_block_group(fs_info, bytenr);
3117 BUG_ON(!cache);
3118 len = min(num, cache->key.offset -
3119 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04003120
3121 spin_lock(&cache->space_info->lock);
3122 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04003123 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04003124 cache->reserved += len;
3125 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04003126 } else {
Zheng Yane8569812008-09-26 10:05:48 -04003127 cache->reserved -= len;
3128 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04003129 }
Josef Bacik25179202008-10-29 14:49:05 -04003130 spin_unlock(&cache->lock);
3131 spin_unlock(&cache->space_info->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003132 btrfs_put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04003133 bytenr += len;
3134 num -= len;
3135 }
3136 return 0;
3137}
3138
Chris Masond1310b22008-01-24 16:13:08 -05003139int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04003140{
Chris Masonccd467d2007-06-28 15:57:36 -04003141 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04003142 u64 start;
3143 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05003144 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04003145 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04003146
Chris Masond3977122009-01-05 21:25:51 -05003147 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003148 ret = find_first_extent_bit(pinned_extents, last,
3149 &start, &end, EXTENT_DIRTY);
3150 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04003151 break;
Josef Bacik817d52f2009-07-13 21:29:25 -04003152
Chris Mason1a5bc162007-10-15 16:15:26 -04003153 set_extent_dirty(copy, start, end, GFP_NOFS);
3154 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04003155 }
3156 return 0;
3157}
3158
3159int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
3160 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05003161 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05003162{
Chris Mason1a5bc162007-10-15 16:15:26 -04003163 u64 start;
3164 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003165 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003166
Chris Masond3977122009-01-05 21:25:51 -05003167 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003168 ret = find_first_extent_bit(unpin, 0, &start, &end,
3169 EXTENT_DIRTY);
3170 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003171 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003172
3173 ret = btrfs_discard_extent(root, start, end + 1 - start);
3174
Chris Masonb9473432009-03-13 11:00:37 -04003175 /* unlocks the pinned mutex */
Josef Bacik68b38552009-07-27 13:57:01 -04003176 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04003177 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003178
Chris Masonb9473432009-03-13 11:00:37 -04003179 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003180 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003181
Liu Hui1f3c79a2009-01-05 15:57:51 -05003182 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003183}
3184
Zheng Yan31840ae2008-09-23 13:14:14 -04003185static int pin_down_bytes(struct btrfs_trans_handle *trans,
3186 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003187 struct btrfs_path *path,
3188 u64 bytenr, u64 num_bytes, int is_data,
3189 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003190{
Chris Mason1a5bc162007-10-15 16:15:26 -04003191 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003192 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003193
Zheng Yan31840ae2008-09-23 13:14:14 -04003194 if (is_data)
3195 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003196
Zheng Yan31840ae2008-09-23 13:14:14 -04003197 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3198 if (!buf)
3199 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003200
Zheng Yan31840ae2008-09-23 13:14:14 -04003201 /* we can reuse a block if it hasn't been written
3202 * and it is from this transaction. We can't
3203 * reuse anything from the tree log root because
3204 * it has tiny sub-transactions.
3205 */
3206 if (btrfs_buffer_uptodate(buf, 0) &&
3207 btrfs_try_tree_lock(buf)) {
3208 u64 header_owner = btrfs_header_owner(buf);
3209 u64 header_transid = btrfs_header_generation(buf);
3210 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3211 header_transid == trans->transid &&
3212 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003213 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003214 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003215 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003216 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003217 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003218 free_extent_buffer(buf);
3219pinit:
Chris Masonb9473432009-03-13 11:00:37 -04003220 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003221 /* unlocks the pinned mutex */
Josef Bacik68b38552009-07-27 13:57:01 -04003222 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003223
Chris Masonbe744172007-05-06 10:15:01 -04003224 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003225 return 0;
3226}
3227
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003228
3229static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3230 struct btrfs_root *root,
3231 u64 bytenr, u64 num_bytes, u64 parent,
3232 u64 root_objectid, u64 owner_objectid,
3233 u64 owner_offset, int refs_to_drop,
3234 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003235{
Chris Masone2fa7222007-03-12 16:22:34 -04003236 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003237 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003238 struct btrfs_fs_info *info = root->fs_info;
3239 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003240 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003241 struct btrfs_extent_item *ei;
3242 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003243 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003244 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003245 int extent_slot = 0;
3246 int found_extent = 0;
3247 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003248 u32 item_size;
3249 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003250
Chris Mason5caf2a02007-04-02 11:20:42 -04003251 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003252 if (!path)
3253 return -ENOMEM;
3254
Chris Mason3c12ac72008-04-21 12:01:38 -04003255 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003256 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003257
3258 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3259 BUG_ON(!is_data && refs_to_drop != 1);
3260
3261 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3262 bytenr, num_bytes, parent,
3263 root_objectid, owner_objectid,
3264 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003265 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003266 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003267 while (extent_slot >= 0) {
3268 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003269 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003270 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003271 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003272 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3273 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003274 found_extent = 1;
3275 break;
3276 }
3277 if (path->slots[0] - extent_slot > 5)
3278 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003279 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003280 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003281#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3282 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3283 if (found_extent && item_size < sizeof(*ei))
3284 found_extent = 0;
3285#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003286 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003287 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003288 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003289 NULL, refs_to_drop,
3290 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003291 BUG_ON(ret);
3292 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003293 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003294
3295 key.objectid = bytenr;
3296 key.type = BTRFS_EXTENT_ITEM_KEY;
3297 key.offset = num_bytes;
3298
Zheng Yan31840ae2008-09-23 13:14:14 -04003299 ret = btrfs_search_slot(trans, extent_root,
3300 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003301 if (ret) {
3302 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003303 ", was looking for %llu\n", ret,
3304 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003305 btrfs_print_leaf(extent_root, path->nodes[0]);
3306 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003307 BUG_ON(ret);
3308 extent_slot = path->slots[0];
3309 }
Chris Mason7bb86312007-12-11 09:25:06 -05003310 } else {
3311 btrfs_print_leaf(extent_root, path->nodes[0]);
3312 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003313 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003314 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003315 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003316 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003317 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003318 (unsigned long long)owner_objectid,
3319 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003320 }
Chris Mason5f39d392007-10-15 16:14:19 -04003321
3322 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003323 item_size = btrfs_item_size_nr(leaf, extent_slot);
3324#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3325 if (item_size < sizeof(*ei)) {
3326 BUG_ON(found_extent || extent_slot != path->slots[0]);
3327 ret = convert_extent_item_v0(trans, extent_root, path,
3328 owner_objectid, 0);
3329 BUG_ON(ret < 0);
3330
3331 btrfs_release_path(extent_root, path);
3332 path->leave_spinning = 1;
3333
3334 key.objectid = bytenr;
3335 key.type = BTRFS_EXTENT_ITEM_KEY;
3336 key.offset = num_bytes;
3337
3338 ret = btrfs_search_slot(trans, extent_root, &key, path,
3339 -1, 1);
3340 if (ret) {
3341 printk(KERN_ERR "umm, got %d back from search"
3342 ", was looking for %llu\n", ret,
3343 (unsigned long long)bytenr);
3344 btrfs_print_leaf(extent_root, path->nodes[0]);
3345 }
3346 BUG_ON(ret);
3347 extent_slot = path->slots[0];
3348 leaf = path->nodes[0];
3349 item_size = btrfs_item_size_nr(leaf, extent_slot);
3350 }
3351#endif
3352 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003353 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003354 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003355 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3356 struct btrfs_tree_block_info *bi;
3357 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3358 bi = (struct btrfs_tree_block_info *)(ei + 1);
3359 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003360 }
3361
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003362 refs = btrfs_extent_refs(leaf, ei);
3363 BUG_ON(refs < refs_to_drop);
3364 refs -= refs_to_drop;
3365
3366 if (refs > 0) {
3367 if (extent_op)
3368 __run_delayed_extent_op(extent_op, leaf, ei);
3369 /*
3370 * In the case of inline back ref, reference count will
3371 * be updated by remove_extent_backref
3372 */
3373 if (iref) {
3374 BUG_ON(!found_extent);
3375 } else {
3376 btrfs_set_extent_refs(leaf, ei, refs);
3377 btrfs_mark_buffer_dirty(leaf);
3378 }
3379 if (found_extent) {
3380 ret = remove_extent_backref(trans, extent_root, path,
3381 iref, refs_to_drop,
3382 is_data);
3383 BUG_ON(ret);
3384 }
3385 } else {
3386 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003387 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003388
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003389 if (found_extent) {
3390 BUG_ON(is_data && refs_to_drop !=
3391 extent_data_ref_count(root, path, iref));
3392 if (iref) {
3393 BUG_ON(path->slots[0] != extent_slot);
3394 } else {
3395 BUG_ON(path->slots[0] != extent_slot + 1);
3396 path->slots[0] = extent_slot;
3397 num_to_del = 2;
3398 }
Chris Mason78fae272007-03-25 11:35:08 -04003399 }
Chris Masonb9473432009-03-13 11:00:37 -04003400
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003401 ret = pin_down_bytes(trans, root, path, bytenr,
3402 num_bytes, is_data, &must_clean);
3403 if (ret > 0)
3404 mark_free = 1;
3405 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003406 /*
3407 * it is going to be very rare for someone to be waiting
3408 * on the block we're freeing. del_items might need to
3409 * schedule, so rather than get fancy, just force it
3410 * to blocking here
3411 */
3412 if (must_clean)
3413 btrfs_set_lock_blocking(must_clean);
3414
Chris Mason952fcca2008-02-18 16:33:44 -05003415 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3416 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003417 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003418 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003419
Chris Masonb9473432009-03-13 11:00:37 -04003420 if (must_clean) {
3421 clean_tree_block(NULL, root, must_clean);
3422 btrfs_tree_unlock(must_clean);
3423 free_extent_buffer(must_clean);
3424 }
3425
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003426 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003427 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3428 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003429 } else {
3430 invalidate_mapping_pages(info->btree_inode->i_mapping,
3431 bytenr >> PAGE_CACHE_SHIFT,
3432 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003433 }
3434
Chris Masondcbdd4d2008-12-16 13:51:01 -05003435 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3436 mark_free);
3437 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003438 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003439 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003440 return ret;
3441}
3442
3443/*
Chris Mason1887be62009-03-13 10:11:24 -04003444 * when we free an extent, it is possible (and likely) that we free the last
3445 * delayed ref for that extent as well. This searches the delayed ref tree for
3446 * a given extent, and if there are no other delayed refs to be processed, it
3447 * removes it from the tree.
3448 */
3449static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3450 struct btrfs_root *root, u64 bytenr)
3451{
3452 struct btrfs_delayed_ref_head *head;
3453 struct btrfs_delayed_ref_root *delayed_refs;
3454 struct btrfs_delayed_ref_node *ref;
3455 struct rb_node *node;
3456 int ret;
3457
3458 delayed_refs = &trans->transaction->delayed_refs;
3459 spin_lock(&delayed_refs->lock);
3460 head = btrfs_find_delayed_ref_head(trans, bytenr);
3461 if (!head)
3462 goto out;
3463
3464 node = rb_prev(&head->node.rb_node);
3465 if (!node)
3466 goto out;
3467
3468 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3469
3470 /* there are still entries for this ref, we can't drop it */
3471 if (ref->bytenr == bytenr)
3472 goto out;
3473
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003474 if (head->extent_op) {
3475 if (!head->must_insert_reserved)
3476 goto out;
3477 kfree(head->extent_op);
3478 head->extent_op = NULL;
3479 }
3480
Chris Mason1887be62009-03-13 10:11:24 -04003481 /*
3482 * waiting for the lock here would deadlock. If someone else has it
3483 * locked they are already in the process of dropping it anyway
3484 */
3485 if (!mutex_trylock(&head->mutex))
3486 goto out;
3487
3488 /*
3489 * at this point we have a head with no other entries. Go
3490 * ahead and process it.
3491 */
3492 head->node.in_tree = 0;
3493 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04003494
Chris Mason1887be62009-03-13 10:11:24 -04003495 delayed_refs->num_entries--;
3496
3497 /*
3498 * we don't take a ref on the node because we're removing it from the
3499 * tree, so we just steal the ref the tree was holding.
3500 */
Chris Masonc3e69d52009-03-13 10:17:05 -04003501 delayed_refs->num_heads--;
3502 if (list_empty(&head->cluster))
3503 delayed_refs->num_heads_ready--;
3504
3505 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04003506 spin_unlock(&delayed_refs->lock);
3507
3508 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003509 &head->node, head->extent_op,
3510 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04003511 BUG_ON(ret);
3512 btrfs_put_delayed_ref(&head->node);
3513 return 0;
3514out:
3515 spin_unlock(&delayed_refs->lock);
3516 return 0;
3517}
3518
Chris Mason925baed2008-06-25 16:01:30 -04003519int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003520 struct btrfs_root *root,
3521 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003522 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04003523{
3524 int ret;
3525
Chris Mason56bec292009-03-13 10:10:06 -04003526 /*
3527 * tree log blocks never actually go into the extent allocation
3528 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003529 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003530 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
3531 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04003532 /* unlocks the pinned mutex */
Josef Bacik68b38552009-07-27 13:57:01 -04003533 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04003534 update_reserved_extents(root, bytenr, num_bytes, 0);
3535 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003536 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
3537 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
3538 parent, root_objectid, (int)owner,
3539 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04003540 BUG_ON(ret);
3541 ret = check_ref_cleanup(trans, root, bytenr);
3542 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003543 } else {
3544 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
3545 parent, root_objectid, owner,
3546 offset, BTRFS_DROP_DELAYED_REF, NULL);
3547 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04003548 }
Chris Mason925baed2008-06-25 16:01:30 -04003549 return ret;
3550}
3551
Chris Mason87ee04e2007-11-30 11:30:34 -05003552static u64 stripe_align(struct btrfs_root *root, u64 val)
3553{
3554 u64 mask = ((u64)root->stripesize - 1);
3555 u64 ret = (val + mask) & ~mask;
3556 return ret;
3557}
3558
Chris Masonfec577f2007-02-26 10:40:21 -05003559/*
Josef Bacik817d52f2009-07-13 21:29:25 -04003560 * when we wait for progress in the block group caching, its because
3561 * our allocation attempt failed at least once. So, we must sleep
3562 * and let some progress happen before we try again.
3563 *
3564 * This function will sleep at least once waiting for new free space to
3565 * show up, and then it will check the block group free space numbers
3566 * for our min num_bytes. Another option is to have it go ahead
3567 * and look in the rbtree for a free extent of a given size, but this
3568 * is a good start.
3569 */
3570static noinline int
3571wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
3572 u64 num_bytes)
3573{
3574 DEFINE_WAIT(wait);
3575
3576 prepare_to_wait(&cache->caching_q, &wait, TASK_UNINTERRUPTIBLE);
3577
3578 if (block_group_cache_done(cache)) {
3579 finish_wait(&cache->caching_q, &wait);
3580 return 0;
3581 }
3582 schedule();
3583 finish_wait(&cache->caching_q, &wait);
3584
3585 wait_event(cache->caching_q, block_group_cache_done(cache) ||
3586 (cache->free_space >= num_bytes));
3587 return 0;
3588}
3589
3590enum btrfs_loop_type {
3591 LOOP_CACHED_ONLY = 0,
3592 LOOP_CACHING_NOWAIT = 1,
3593 LOOP_CACHING_WAIT = 2,
3594 LOOP_ALLOC_CHUNK = 3,
3595 LOOP_NO_EMPTY_SIZE = 4,
3596};
3597
3598/*
Chris Masonfec577f2007-02-26 10:40:21 -05003599 * walks the btree of allocated extents and find a hole of a given size.
3600 * The key ins is changed to record the hole:
3601 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04003602 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05003603 * ins->offset == number of blocks
3604 * Any available blocks before search_start are skipped.
3605 */
Chris Masond3977122009-01-05 21:25:51 -05003606static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003607 struct btrfs_root *orig_root,
3608 u64 num_bytes, u64 empty_size,
3609 u64 search_start, u64 search_end,
3610 u64 hint_byte, struct btrfs_key *ins,
3611 u64 exclude_start, u64 exclude_nr,
3612 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05003613{
Josef Bacik80eb2342008-10-29 14:49:05 -04003614 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003615 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003616 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04003617 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04003618 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04003619 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003620 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003621 int last_ptr_loop = 0;
3622 int loop = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04003623 bool found_uncached_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05003624
Chris Masondb945352007-10-15 16:15:53 -04003625 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04003626 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04003627 ins->objectid = 0;
3628 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003629
Josef Bacik2552d172009-04-03 10:14:19 -04003630 space_info = __find_space_info(root->fs_info, data);
3631
Chris Mason0ef3e662008-05-24 14:04:53 -04003632 if (orig_root->ref_cows || empty_size)
3633 allowed_chunk_alloc = 1;
3634
Chris Mason239b14b2008-03-24 15:02:07 -04003635 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003636 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05003637 if (!btrfs_test_opt(root, SSD))
3638 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04003639 }
3640
Chris Masonfa9c0d792009-04-03 09:47:43 -04003641 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
3642 last_ptr = &root->fs_info->data_alloc_cluster;
3643 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003644
Chris Mason239b14b2008-03-24 15:02:07 -04003645 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003646 spin_lock(&last_ptr->lock);
3647 if (last_ptr->block_group)
3648 hint_byte = last_ptr->window_start;
3649 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003650 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003651
Chris Masona061fc82008-05-07 11:43:44 -04003652 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04003653 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04003654
Josef Bacik817d52f2009-07-13 21:29:25 -04003655 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003656 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003657
Josef Bacik2552d172009-04-03 10:14:19 -04003658 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04003659 block_group = btrfs_lookup_block_group(root->fs_info,
3660 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04003661 /*
3662 * we don't want to use the block group if it doesn't match our
3663 * allocation bits, or if its not cached.
3664 */
3665 if (block_group && block_group_bits(block_group, data) &&
3666 block_group_cache_done(block_group)) {
Josef Bacik2552d172009-04-03 10:14:19 -04003667 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003668 if (list_empty(&block_group->list) ||
3669 block_group->ro) {
3670 /*
3671 * someone is removing this block group,
3672 * we can't jump into the have_block_group
3673 * target because our list pointers are not
3674 * valid
3675 */
3676 btrfs_put_block_group(block_group);
3677 up_read(&space_info->groups_sem);
3678 } else
3679 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003680 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003681 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003682 }
Chris Mason42e70e72008-11-07 18:17:11 -05003683 }
Chris Mason43662112008-11-07 09:06:11 -05003684
Josef Bacik2552d172009-04-03 10:14:19 -04003685search:
Josef Bacik80eb2342008-10-29 14:49:05 -04003686 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04003687 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04003688 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04003689 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05003690
Josef Bacik2552d172009-04-03 10:14:19 -04003691 atomic_inc(&block_group->count);
3692 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05003693
Josef Bacik2552d172009-04-03 10:14:19 -04003694have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04003695 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
3696 /*
3697 * we want to start caching kthreads, but not too many
3698 * right off the bat so we don't overwhelm the system,
3699 * so only start them if there are less than 2 and we're
3700 * in the initial allocation phase.
3701 */
3702 if (loop > LOOP_CACHING_NOWAIT ||
3703 atomic_read(&space_info->caching_threads) < 2) {
3704 ret = cache_block_group(block_group);
3705 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04003706 }
Josef Bacikea6a4782008-11-20 12:16:16 -05003707 }
3708
Josef Bacik817d52f2009-07-13 21:29:25 -04003709 cached = block_group_cache_done(block_group);
3710 if (unlikely(!cached)) {
3711 found_uncached_bg = true;
3712
3713 /* if we only want cached bgs, loop */
3714 if (loop == LOOP_CACHED_ONLY)
3715 goto loop;
3716 }
3717
Josef Bacikea6a4782008-11-20 12:16:16 -05003718 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04003719 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003720
Chris Masonfa9c0d792009-04-03 09:47:43 -04003721 if (last_ptr) {
3722 /*
3723 * the refill lock keeps out other
3724 * people trying to start a new cluster
3725 */
3726 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003727 if (last_ptr->block_group &&
3728 (last_ptr->block_group->ro ||
3729 !block_group_bits(last_ptr->block_group, data))) {
3730 offset = 0;
3731 goto refill_cluster;
3732 }
3733
Chris Masonfa9c0d792009-04-03 09:47:43 -04003734 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
3735 num_bytes, search_start);
3736 if (offset) {
3737 /* we have a block, we're done */
3738 spin_unlock(&last_ptr->refill_lock);
3739 goto checks;
3740 }
3741
3742 spin_lock(&last_ptr->lock);
3743 /*
3744 * whoops, this cluster doesn't actually point to
3745 * this block group. Get a ref on the block
3746 * group is does point to and try again
3747 */
3748 if (!last_ptr_loop && last_ptr->block_group &&
3749 last_ptr->block_group != block_group) {
3750
3751 btrfs_put_block_group(block_group);
3752 block_group = last_ptr->block_group;
3753 atomic_inc(&block_group->count);
3754 spin_unlock(&last_ptr->lock);
3755 spin_unlock(&last_ptr->refill_lock);
3756
3757 last_ptr_loop = 1;
3758 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04003759 /*
3760 * we know this block group is properly
3761 * in the list because
3762 * btrfs_remove_block_group, drops the
3763 * cluster before it removes the block
3764 * group from the list
3765 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04003766 goto have_block_group;
3767 }
3768 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04003769refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04003770 /*
3771 * this cluster didn't work out, free it and
3772 * start over
3773 */
3774 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3775
3776 last_ptr_loop = 0;
3777
3778 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04003779 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003780 block_group, last_ptr,
3781 offset, num_bytes,
3782 empty_cluster + empty_size);
3783 if (ret == 0) {
3784 /*
3785 * now pull our allocation out of this
3786 * cluster
3787 */
3788 offset = btrfs_alloc_from_cluster(block_group,
3789 last_ptr, num_bytes,
3790 search_start);
3791 if (offset) {
3792 /* we found one, proceed */
3793 spin_unlock(&last_ptr->refill_lock);
3794 goto checks;
3795 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003796 } else if (!cached && loop > LOOP_CACHING_NOWAIT) {
3797 spin_unlock(&last_ptr->refill_lock);
3798
3799 wait_block_group_cache_progress(block_group,
3800 num_bytes + empty_cluster + empty_size);
3801 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003802 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003803
Chris Masonfa9c0d792009-04-03 09:47:43 -04003804 /*
3805 * at this point we either didn't find a cluster
3806 * or we weren't able to allocate a block from our
3807 * cluster. Free the cluster we've been trying
3808 * to use, and go to the next block group
3809 */
Josef Bacik817d52f2009-07-13 21:29:25 -04003810 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003811 btrfs_return_cluster_to_free_space(NULL,
3812 last_ptr);
3813 spin_unlock(&last_ptr->refill_lock);
3814 goto loop;
3815 }
3816 spin_unlock(&last_ptr->refill_lock);
3817 }
3818
Josef Bacik6226cb02009-04-03 10:14:18 -04003819 offset = btrfs_find_space_for_alloc(block_group, search_start,
3820 num_bytes, empty_size);
Josef Bacik817d52f2009-07-13 21:29:25 -04003821 if (!offset && (cached || (!cached &&
3822 loop == LOOP_CACHING_NOWAIT))) {
Josef Bacik2552d172009-04-03 10:14:19 -04003823 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04003824 } else if (!offset && (!cached &&
3825 loop > LOOP_CACHING_NOWAIT)) {
3826 wait_block_group_cache_progress(block_group,
3827 num_bytes + empty_size);
3828 goto have_block_group;
3829 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003830checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04003831 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04003832 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04003833 if (search_start + num_bytes >= search_end) {
3834 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003835 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04003836 }
Chris Masone37c9e62007-05-09 20:13:14 -04003837
Josef Bacik2552d172009-04-03 10:14:19 -04003838 /* move on to the next group */
3839 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04003840 block_group->key.objectid + block_group->key.offset) {
3841 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003842 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04003843 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003844
Josef Bacik2552d172009-04-03 10:14:19 -04003845 if (exclude_nr > 0 &&
3846 (search_start + num_bytes > exclude_start &&
3847 search_start < exclude_start + exclude_nr)) {
3848 search_start = exclude_start + exclude_nr;
3849
Josef Bacik6226cb02009-04-03 10:14:18 -04003850 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003851 /*
3852 * if search_start is still in this block group
3853 * then we just re-search this block group
3854 */
3855 if (search_start >= block_group->key.objectid &&
3856 search_start < (block_group->key.objectid +
Josef Bacik6226cb02009-04-03 10:14:18 -04003857 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04003858 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04003859 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003860 }
Josef Bacik2552d172009-04-03 10:14:19 -04003861
3862 ins->objectid = search_start;
3863 ins->offset = num_bytes;
3864
Josef Bacik6226cb02009-04-03 10:14:18 -04003865 if (offset < search_start)
3866 btrfs_add_free_space(block_group, offset,
3867 search_start - offset);
3868 BUG_ON(offset > search_start);
3869
Josef Bacik2552d172009-04-03 10:14:19 -04003870 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04003871 break;
3872loop:
Chris Masonfa9c0d792009-04-03 09:47:43 -04003873 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003874 }
3875 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05003876
Josef Bacik817d52f2009-07-13 21:29:25 -04003877 /* LOOP_CACHED_ONLY, only search fully cached block groups
3878 * LOOP_CACHING_NOWAIT, search partially cached block groups, but
3879 * dont wait foR them to finish caching
3880 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3881 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3882 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3883 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04003884 */
Josef Bacik817d52f2009-07-13 21:29:25 -04003885 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
3886 (found_uncached_bg || empty_size || empty_cluster ||
3887 allowed_chunk_alloc)) {
3888 if (found_uncached_bg) {
3889 found_uncached_bg = false;
3890 if (loop < LOOP_CACHING_WAIT) {
3891 loop++;
3892 goto search;
3893 }
3894 }
3895
3896 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003897 empty_size = 0;
3898 empty_cluster = 0;
3899 }
Chris Mason42e70e72008-11-07 18:17:11 -05003900
Josef Bacik2552d172009-04-03 10:14:19 -04003901 if (allowed_chunk_alloc) {
3902 ret = do_chunk_alloc(trans, root, num_bytes +
3903 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04003904 allowed_chunk_alloc = 0;
3905 } else {
3906 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003907 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003908
Josef Bacik817d52f2009-07-13 21:29:25 -04003909 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003910 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04003911 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003912 }
Josef Bacik2552d172009-04-03 10:14:19 -04003913 ret = -ENOSPC;
3914 } else if (!ins->objectid) {
3915 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04003916 }
Chris Mason0b86a832008-03-24 15:01:56 -04003917
Josef Bacik80eb2342008-10-29 14:49:05 -04003918 /* we found what we needed */
3919 if (ins->objectid) {
3920 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05003921 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04003922
Chris Masonfa9c0d792009-04-03 09:47:43 -04003923 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003924 ret = 0;
3925 }
Chris Mason0b86a832008-03-24 15:01:56 -04003926
Chris Mason0f70abe2007-02-28 16:46:22 -05003927 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003928}
Chris Masonec44a352008-04-28 15:29:52 -04003929
Josef Bacik0f9dd462008-09-23 13:14:11 -04003930static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3931{
3932 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003933
Chris Masond3977122009-01-05 21:25:51 -05003934 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3935 (unsigned long long)(info->total_bytes - info->bytes_used -
3936 info->bytes_pinned - info->bytes_reserved),
3937 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05003938 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Joel Becker21380932009-04-21 12:38:29 -07003939 " may_use=%llu, used=%llu\n",
3940 (unsigned long long)info->total_bytes,
3941 (unsigned long long)info->bytes_pinned,
3942 (unsigned long long)info->bytes_delalloc,
3943 (unsigned long long)info->bytes_may_use,
3944 (unsigned long long)info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003945
Josef Bacik80eb2342008-10-29 14:49:05 -04003946 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003947 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003948 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05003949 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3950 "%llu pinned %llu reserved\n",
3951 (unsigned long long)cache->key.objectid,
3952 (unsigned long long)cache->key.offset,
3953 (unsigned long long)btrfs_block_group_used(&cache->item),
3954 (unsigned long long)cache->pinned,
3955 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003956 btrfs_dump_free_space(cache, bytes);
3957 spin_unlock(&cache->lock);
3958 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003959 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003960}
Zheng Yane8569812008-09-26 10:05:48 -04003961
Chris Masone6dcd2d2008-07-17 12:53:50 -04003962static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3963 struct btrfs_root *root,
3964 u64 num_bytes, u64 min_alloc_size,
3965 u64 empty_size, u64 hint_byte,
3966 u64 search_end, struct btrfs_key *ins,
3967 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003968{
3969 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003970 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04003971 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003972
Josef Bacik6a632092009-02-20 11:00:09 -05003973 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04003974again:
Chris Mason0ef3e662008-05-24 14:04:53 -04003975 /*
3976 * the only place that sets empty_size is btrfs_realloc_node, which
3977 * is not called recursively on allocations
3978 */
3979 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003980 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003981 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003982 2 * 1024 * 1024,
3983 BTRFS_BLOCK_GROUP_METADATA |
3984 (info->metadata_alloc_profile &
3985 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003986 }
3987 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003988 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003989 }
Chris Mason0b86a832008-03-24 15:01:56 -04003990
Chris Masondb945352007-10-15 16:15:53 -04003991 WARN_ON(num_bytes < root->sectorsize);
3992 ret = find_free_extent(trans, root, num_bytes, empty_size,
3993 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003994 trans->alloc_exclude_start,
3995 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003996
Chris Mason98d20f62008-04-14 09:46:10 -04003997 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3998 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003999 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004000 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04004001 do_chunk_alloc(trans, root->fs_info->extent_root,
4002 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004003 goto again;
4004 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004005 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004006 struct btrfs_space_info *sinfo;
4007
4008 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004009 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4010 "wanted %llu\n", (unsigned long long)data,
4011 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004012 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04004013 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004014
4015 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004016}
4017
Chris Mason65b51a02008-08-01 15:11:20 -04004018int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4019{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004020 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004021 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004022
Josef Bacik0f9dd462008-09-23 13:14:11 -04004023 cache = btrfs_lookup_block_group(root->fs_info, start);
4024 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004025 printk(KERN_ERR "Unable to find block group for %llu\n",
4026 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004027 return -ENOSPC;
4028 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004029
4030 ret = btrfs_discard_extent(root, start, len);
4031
Josef Bacik0f9dd462008-09-23 13:14:11 -04004032 btrfs_add_free_space(cache, start, len);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004033 btrfs_put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04004034 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004035
4036 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04004037}
4038
Chris Masone6dcd2d2008-07-17 12:53:50 -04004039int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4040 struct btrfs_root *root,
4041 u64 num_bytes, u64 min_alloc_size,
4042 u64 empty_size, u64 hint_byte,
4043 u64 search_end, struct btrfs_key *ins,
4044 u64 data)
4045{
4046 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004047 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
4048 empty_size, hint_byte, search_end, ins,
4049 data);
Josef Bacik817d52f2009-07-13 21:29:25 -04004050 if (!ret)
4051 update_reserved_extents(root, ins->objectid, ins->offset, 1);
4052
Chris Masone6dcd2d2008-07-17 12:53:50 -04004053 return ret;
4054}
4055
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004056static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4057 struct btrfs_root *root,
4058 u64 parent, u64 root_objectid,
4059 u64 flags, u64 owner, u64 offset,
4060 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004061{
4062 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004063 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004064 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004065 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004066 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004067 struct extent_buffer *leaf;
4068 int type;
4069 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004070
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004071 if (parent > 0)
4072 type = BTRFS_SHARED_DATA_REF_KEY;
4073 else
4074 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004075
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004076 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004077
4078 path = btrfs_alloc_path();
4079 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004080
Chris Masonb9473432009-03-13 11:00:37 -04004081 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004082 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4083 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004084 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004085
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004086 leaf = path->nodes[0];
4087 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004088 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004089 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4090 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4091 btrfs_set_extent_flags(leaf, extent_item,
4092 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004093
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004094 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4095 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4096 if (parent > 0) {
4097 struct btrfs_shared_data_ref *ref;
4098 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4099 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4100 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4101 } else {
4102 struct btrfs_extent_data_ref *ref;
4103 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4104 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4105 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4106 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4107 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4108 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004109
4110 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004111 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004112
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004113 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4114 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05004115 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004116 printk(KERN_ERR "btrfs update block group failed for %llu "
4117 "%llu\n", (unsigned long long)ins->objectid,
4118 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004119 BUG();
4120 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004121 return ret;
4122}
4123
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004124static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4125 struct btrfs_root *root,
4126 u64 parent, u64 root_objectid,
4127 u64 flags, struct btrfs_disk_key *key,
4128 int level, struct btrfs_key *ins)
4129{
4130 int ret;
4131 struct btrfs_fs_info *fs_info = root->fs_info;
4132 struct btrfs_extent_item *extent_item;
4133 struct btrfs_tree_block_info *block_info;
4134 struct btrfs_extent_inline_ref *iref;
4135 struct btrfs_path *path;
4136 struct extent_buffer *leaf;
4137 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4138
4139 path = btrfs_alloc_path();
4140 BUG_ON(!path);
4141
4142 path->leave_spinning = 1;
4143 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4144 ins, size);
4145 BUG_ON(ret);
4146
4147 leaf = path->nodes[0];
4148 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4149 struct btrfs_extent_item);
4150 btrfs_set_extent_refs(leaf, extent_item, 1);
4151 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4152 btrfs_set_extent_flags(leaf, extent_item,
4153 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4154 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4155
4156 btrfs_set_tree_block_key(leaf, block_info, key);
4157 btrfs_set_tree_block_level(leaf, block_info, level);
4158
4159 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4160 if (parent > 0) {
4161 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4162 btrfs_set_extent_inline_ref_type(leaf, iref,
4163 BTRFS_SHARED_BLOCK_REF_KEY);
4164 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4165 } else {
4166 btrfs_set_extent_inline_ref_type(leaf, iref,
4167 BTRFS_TREE_BLOCK_REF_KEY);
4168 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4169 }
4170
4171 btrfs_mark_buffer_dirty(leaf);
4172 btrfs_free_path(path);
4173
4174 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4175 1, 0);
4176 if (ret) {
4177 printk(KERN_ERR "btrfs update block group failed for %llu "
4178 "%llu\n", (unsigned long long)ins->objectid,
4179 (unsigned long long)ins->offset);
4180 BUG();
4181 }
4182 return ret;
4183}
4184
4185int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4186 struct btrfs_root *root,
4187 u64 root_objectid, u64 owner,
4188 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004189{
4190 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04004191
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004192 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004193
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004194 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4195 0, root_objectid, owner, offset,
4196 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004197 return ret;
4198}
Chris Masone02119d2008-09-05 16:13:11 -04004199
4200/*
4201 * this is used by the tree logging recovery code. It records that
4202 * an extent has been allocated and makes sure to clear the free
4203 * space cache bits as well
4204 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004205int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4206 struct btrfs_root *root,
4207 u64 root_objectid, u64 owner, u64 offset,
4208 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004209{
4210 int ret;
4211 struct btrfs_block_group_cache *block_group;
4212
Chris Masone02119d2008-09-05 16:13:11 -04004213 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04004214 cache_block_group(block_group);
4215 wait_event(block_group->caching_q,
4216 block_group_cache_done(block_group));
Chris Masone02119d2008-09-05 16:13:11 -04004217
Josef Bacikea6a4782008-11-20 12:16:16 -05004218 ret = btrfs_remove_free_space(block_group, ins->objectid,
4219 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004220 BUG_ON(ret);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004221 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004222 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4223 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004224 return ret;
4225}
4226
Chris Masone6dcd2d2008-07-17 12:53:50 -04004227/*
4228 * finds a free extent and does all the dirty work required for allocation
4229 * returns the key for the extent through ins, and a tree buffer for
4230 * the first block of the extent through buf.
4231 *
4232 * returns 0 if everything worked, non-zero otherwise.
4233 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004234static int alloc_tree_block(struct btrfs_trans_handle *trans,
4235 struct btrfs_root *root,
4236 u64 num_bytes, u64 parent, u64 root_objectid,
4237 struct btrfs_disk_key *key, int level,
4238 u64 empty_size, u64 hint_byte, u64 search_end,
4239 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004240{
4241 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004242 u64 flags = 0;
4243
4244 ret = __btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4245 empty_size, hint_byte, search_end,
4246 ins, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004247 if (ret)
4248 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004249
4250 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4251 if (parent == 0)
4252 parent = ins->objectid;
4253 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4254 } else
4255 BUG_ON(parent > 0);
4256
4257 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04004258 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004259 struct btrfs_delayed_extent_op *extent_op;
4260 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4261 BUG_ON(!extent_op);
4262 if (key)
4263 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4264 else
4265 memset(&extent_op->key, 0, sizeof(extent_op->key));
4266 extent_op->flags_to_set = flags;
4267 extent_op->update_key = 1;
4268 extent_op->update_flags = 1;
4269 extent_op->is_data = 0;
4270
4271 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4272 ins->offset, parent, root_objectid,
4273 level, BTRFS_ADD_DELAYED_EXTENT,
4274 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004275 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004276 }
Chris Mason925baed2008-06-25 16:01:30 -04004277 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004278}
Chris Mason65b51a02008-08-01 15:11:20 -04004279
4280struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4281 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004282 u64 bytenr, u32 blocksize,
4283 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004284{
4285 struct extent_buffer *buf;
4286
4287 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4288 if (!buf)
4289 return ERR_PTR(-ENOMEM);
4290 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004291 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004292 btrfs_tree_lock(buf);
4293 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004294
4295 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004296 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004297
Chris Masond0c803c2008-09-11 16:17:57 -04004298 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4299 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04004300 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004301 } else {
4302 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4303 buf->start + buf->len - 1, GFP_NOFS);
4304 }
Chris Mason65b51a02008-08-01 15:11:20 -04004305 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004306 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004307 return buf;
4308}
4309
Chris Masonfec577f2007-02-26 10:40:21 -05004310/*
4311 * helper function to allocate a block for a given tree
4312 * returns the tree buffer or NULL.
4313 */
Chris Mason5f39d392007-10-15 16:14:19 -04004314struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004315 struct btrfs_root *root, u32 blocksize,
4316 u64 parent, u64 root_objectid,
4317 struct btrfs_disk_key *key, int level,
4318 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004319{
Chris Masone2fa7222007-03-12 16:22:34 -04004320 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004321 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004322 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004323
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004324 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4325 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004326 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004327 BUG_ON(ret > 0);
4328 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004329 }
Chris Mason55c69072008-01-09 15:55:33 -05004330
Chris Mason4008c042009-02-12 14:09:45 -05004331 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4332 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004333 return buf;
4334}
Chris Masona28ec192007-03-06 20:08:01 -05004335
Yan Zheng2c47e6052009-06-27 21:07:35 -04004336#if 0
Chris Masone02119d2008-09-05 16:13:11 -04004337int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
4338 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04004339{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004340 u64 disk_bytenr;
4341 u64 num_bytes;
Chris Mason5f39d392007-10-15 16:14:19 -04004342 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04004343 struct btrfs_file_extent_item *fi;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004344 u32 nritems;
Chris Mason6407bf62007-03-27 06:33:00 -04004345 int i;
Chris Mason6407bf62007-03-27 06:33:00 -04004346 int ret;
4347
Chris Mason5f39d392007-10-15 16:14:19 -04004348 BUG_ON(!btrfs_is_leaf(leaf));
4349 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05004350
Chris Mason6407bf62007-03-27 06:33:00 -04004351 for (i = 0; i < nritems; i++) {
Chris Masone34a5b42008-07-22 12:08:37 -04004352 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04004353 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05004354
4355 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04004356 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04004357 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004358
Chris Mason6407bf62007-03-27 06:33:00 -04004359 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05004360
4361 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04004362 if (btrfs_file_extent_type(leaf, fi) ==
4363 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04004364 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05004365
Chris Masondb945352007-10-15 16:15:53 -04004366 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05004367
4368 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04004369 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04004370 continue;
Chris Mason4a096752008-07-21 10:29:44 -04004371
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004372 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4373 ret = btrfs_free_extent(trans, root, disk_bytenr, num_bytes,
4374 leaf->start, 0, key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04004375 BUG_ON(ret);
Chris Mason6407bf62007-03-27 06:33:00 -04004376 }
4377 return 0;
4378}
4379
Chris Masond3977122009-01-05 21:25:51 -05004380static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04004381 struct btrfs_root *root,
4382 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04004383{
4384 int i;
4385 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05004386 struct btrfs_extent_info *info;
4387 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04004388
Chris Masonbd56b302009-02-04 09:27:02 -05004389 if (ref->nritems == 0)
4390 return 0;
4391
4392 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04004393 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05004394 sorted[i].bytenr = ref->extents[i].bytenr;
4395 sorted[i].slot = i;
4396 }
4397 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
4398
4399 /*
4400 * the items in the ref were sorted when the ref was inserted
4401 * into the ref cache, so this is already in order
4402 */
4403 for (i = 0; i < ref->nritems; i++) {
4404 info = ref->extents + sorted[i].slot;
Chris Mason56bec292009-03-13 10:10:06 -04004405 ret = btrfs_free_extent(trans, root, info->bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004406 info->num_bytes, ref->bytenr,
4407 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004408 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04004409
4410 atomic_inc(&root->fs_info->throttle_gen);
4411 wake_up(&root->fs_info->transaction_throttle);
4412 cond_resched();
4413
Yan Zheng31153d82008-07-28 15:32:19 -04004414 BUG_ON(ret);
4415 info++;
4416 }
Yan Zheng31153d82008-07-28 15:32:19 -04004417
Chris Mason806638b2009-02-05 09:08:14 -05004418 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04004419 return 0;
4420}
4421
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004422
Chris Mason56bec292009-03-13 10:10:06 -04004423static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
4424 struct btrfs_root *root, u64 start,
Chris Masond3977122009-01-05 21:25:51 -05004425 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04004426{
Chris Mason017e5362008-07-28 15:32:51 -04004427 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04004428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004429 ret = btrfs_lookup_extent_refs(trans, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04004430 BUG_ON(ret);
4431
Chris Masond3977122009-01-05 21:25:51 -05004432#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04004433 /* if the refs count is one, it won't get increased again. But
4434 * if the ref count is > 1, someone may be decreasing it at
4435 * the same time we are.
4436 */
4437 if (*refs != 1) {
4438 struct extent_buffer *eb = NULL;
4439 eb = btrfs_find_create_tree_block(root, start, len);
4440 if (eb)
4441 btrfs_tree_lock(eb);
4442
4443 mutex_lock(&root->fs_info->alloc_mutex);
4444 ret = lookup_extent_ref(NULL, root, start, len, refs);
4445 BUG_ON(ret);
4446 mutex_unlock(&root->fs_info->alloc_mutex);
4447
4448 if (eb) {
4449 btrfs_tree_unlock(eb);
4450 free_extent_buffer(eb);
4451 }
4452 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05004453 printk(KERN_ERR "btrfs block %llu went down to one "
4454 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04004455 }
4456
4457 }
4458#endif
4459
Chris Masone7a84562008-06-25 16:01:31 -04004460 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04004461 return ret;
Chris Mason333db942008-06-25 16:01:30 -04004462}
4463
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004464
Chris Mason333db942008-06-25 16:01:30 -04004465/*
Chris Masonbd56b302009-02-04 09:27:02 -05004466 * this is used while deleting old snapshots, and it drops the refs
4467 * on a whole subtree starting from a level 1 node.
4468 *
4469 * The idea is to sort all the leaf pointers, and then drop the
4470 * ref on all the leaves in order. Most of the time the leaves
4471 * will have ref cache entries, so no leaf IOs will be required to
4472 * find the extents they have references on.
4473 *
4474 * For each leaf, any references it has are also dropped in order
4475 *
4476 * This ends up dropping the references in something close to optimal
4477 * order for reading and modifying the extent allocation tree.
4478 */
4479static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
4480 struct btrfs_root *root,
4481 struct btrfs_path *path)
4482{
4483 u64 bytenr;
4484 u64 root_owner;
4485 u64 root_gen;
4486 struct extent_buffer *eb = path->nodes[1];
4487 struct extent_buffer *leaf;
4488 struct btrfs_leaf_ref *ref;
4489 struct refsort *sorted = NULL;
4490 int nritems = btrfs_header_nritems(eb);
4491 int ret;
4492 int i;
4493 int refi = 0;
4494 int slot = path->slots[1];
4495 u32 blocksize = btrfs_level_size(root, 0);
4496 u32 refs;
4497
4498 if (nritems == 0)
4499 goto out;
4500
4501 root_owner = btrfs_header_owner(eb);
4502 root_gen = btrfs_header_generation(eb);
4503 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
4504
4505 /*
4506 * step one, sort all the leaf pointers so we don't scribble
4507 * randomly into the extent allocation tree
4508 */
4509 for (i = slot; i < nritems; i++) {
4510 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
4511 sorted[refi].slot = i;
4512 refi++;
4513 }
4514
4515 /*
4516 * nritems won't be zero, but if we're picking up drop_snapshot
4517 * after a crash, slot might be > 0, so double check things
4518 * just in case.
4519 */
4520 if (refi == 0)
4521 goto out;
4522
4523 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
4524
4525 /*
4526 * the first loop frees everything the leaves point to
4527 */
4528 for (i = 0; i < refi; i++) {
4529 u64 ptr_gen;
4530
4531 bytenr = sorted[i].bytenr;
4532
4533 /*
4534 * check the reference count on this leaf. If it is > 1
4535 * we just decrement it below and don't update any
4536 * of the refs the leaf points to.
4537 */
Chris Mason56bec292009-03-13 10:10:06 -04004538 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4539 blocksize, &refs);
Chris Masonbd56b302009-02-04 09:27:02 -05004540 BUG_ON(ret);
4541 if (refs != 1)
4542 continue;
4543
4544 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
4545
4546 /*
4547 * the leaf only had one reference, which means the
4548 * only thing pointing to this leaf is the snapshot
4549 * we're deleting. It isn't possible for the reference
4550 * count to increase again later
4551 *
4552 * The reference cache is checked for the leaf,
4553 * and if found we'll be able to drop any refs held by
4554 * the leaf without needing to read it in.
4555 */
4556 ref = btrfs_lookup_leaf_ref(root, bytenr);
4557 if (ref && ref->generation != ptr_gen) {
4558 btrfs_free_leaf_ref(root, ref);
4559 ref = NULL;
4560 }
4561 if (ref) {
4562 ret = cache_drop_leaf_ref(trans, root, ref);
4563 BUG_ON(ret);
4564 btrfs_remove_leaf_ref(root, ref);
4565 btrfs_free_leaf_ref(root, ref);
4566 } else {
4567 /*
4568 * the leaf wasn't in the reference cache, so
4569 * we have to read it.
4570 */
4571 leaf = read_tree_block(root, bytenr, blocksize,
4572 ptr_gen);
4573 ret = btrfs_drop_leaf_ref(trans, root, leaf);
4574 BUG_ON(ret);
4575 free_extent_buffer(leaf);
4576 }
4577 atomic_inc(&root->fs_info->throttle_gen);
4578 wake_up(&root->fs_info->transaction_throttle);
4579 cond_resched();
4580 }
4581
4582 /*
4583 * run through the loop again to free the refs on the leaves.
4584 * This is faster than doing it in the loop above because
4585 * the leaves are likely to be clustered together. We end up
4586 * working in nice chunks on the extent allocation tree.
4587 */
4588 for (i = 0; i < refi; i++) {
4589 bytenr = sorted[i].bytenr;
Chris Mason56bec292009-03-13 10:10:06 -04004590 ret = btrfs_free_extent(trans, root, bytenr,
Chris Masonbd56b302009-02-04 09:27:02 -05004591 blocksize, eb->start,
4592 root_owner, root_gen, 0, 1);
4593 BUG_ON(ret);
4594
4595 atomic_inc(&root->fs_info->throttle_gen);
4596 wake_up(&root->fs_info->transaction_throttle);
4597 cond_resched();
4598 }
4599out:
4600 kfree(sorted);
4601
4602 /*
4603 * update the path to show we've processed the entire level 1
4604 * node. This will get saved into the root's drop_snapshot_progress
4605 * field so these drops are not repeated again if this transaction
4606 * commits.
4607 */
4608 path->slots[1] = nritems;
4609 return 0;
4610}
4611
4612/*
Chris Mason9aca1d52007-03-13 11:09:37 -04004613 * helper function for drop_snapshot, this walks down the tree dropping ref
4614 * counts as it goes.
4615 */
Chris Masond3977122009-01-05 21:25:51 -05004616static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004617 struct btrfs_root *root,
4618 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05004619{
Chris Mason7bb86312007-12-11 09:25:06 -05004620 u64 root_owner;
4621 u64 root_gen;
4622 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04004623 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04004624 struct extent_buffer *next;
4625 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05004626 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04004627 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05004628 int ret;
4629 u32 refs;
4630
Chris Mason5caf2a02007-04-02 11:20:42 -04004631 WARN_ON(*level < 0);
4632 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason56bec292009-03-13 10:10:06 -04004633 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04004634 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05004635 BUG_ON(ret);
4636 if (refs > 1)
4637 goto out;
Chris Masone0115992007-06-19 16:23:05 -04004638
Chris Mason9aca1d52007-03-13 11:09:37 -04004639 /*
4640 * walk down to the last node level and free all the leaves
4641 */
Chris Masond3977122009-01-05 21:25:51 -05004642 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04004643 WARN_ON(*level < 0);
4644 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05004645 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04004646
Chris Mason5f39d392007-10-15 16:14:19 -04004647 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04004648 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04004649
Chris Mason7518a232007-03-12 12:01:18 -04004650 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04004651 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05004652 break;
Chris Masonbd56b302009-02-04 09:27:02 -05004653
4654 /* the new code goes down to level 1 and does all the
4655 * leaves pointed to that node in bulk. So, this check
4656 * for level 0 will always be false.
4657 *
4658 * But, the disk format allows the drop_snapshot_progress
4659 * field in the root to leave things in a state where
4660 * a leaf will need cleaning up here. If someone crashes
4661 * with the old code and then boots with the new code,
4662 * we might find a leaf here.
4663 */
Chris Mason6407bf62007-03-27 06:33:00 -04004664 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004665 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04004666 BUG_ON(ret);
4667 break;
4668 }
Chris Masonbd56b302009-02-04 09:27:02 -05004669
4670 /*
4671 * once we get to level one, process the whole node
4672 * at once, including everything below it.
4673 */
4674 if (*level == 1) {
4675 ret = drop_level_one_refs(trans, root, path);
4676 BUG_ON(ret);
4677 break;
4678 }
4679
Chris Masondb945352007-10-15 16:15:53 -04004680 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04004681 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04004682 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04004683
Chris Mason56bec292009-03-13 10:10:06 -04004684 ret = drop_snap_lookup_refcount(trans, root, bytenr,
4685 blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04004686 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05004687
4688 /*
4689 * if there is more than one reference, we don't need
4690 * to read that node to drop any references it has. We
4691 * just drop the ref we hold on that node and move on to the
4692 * next slot in this level.
4693 */
Chris Mason6407bf62007-03-27 06:33:00 -04004694 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05004695 parent = path->nodes[*level];
4696 root_owner = btrfs_header_owner(parent);
4697 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05004698 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04004699
Chris Mason56bec292009-03-13 10:10:06 -04004700 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004701 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004702 root_owner, root_gen,
4703 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05004704 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04004705
4706 atomic_inc(&root->fs_info->throttle_gen);
4707 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04004708 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04004709
Chris Mason20524f02007-03-10 06:35:47 -05004710 continue;
4711 }
Chris Mason333db942008-06-25 16:01:30 -04004712
Chris Masonbd56b302009-02-04 09:27:02 -05004713 /*
4714 * we need to keep freeing things in the next level down.
4715 * read the block and loop around to process it
4716 */
4717 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04004718 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04004719 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04004720 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05004721 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04004722 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05004723 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04004724 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004725 }
4726out:
Chris Mason5caf2a02007-04-02 11:20:42 -04004727 WARN_ON(*level < 0);
4728 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05004729
4730 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05004731 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04004732 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05004733 } else {
4734 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04004735 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05004736 }
4737
Yan Zheng31153d82008-07-28 15:32:19 -04004738 blocksize = btrfs_level_size(root, *level);
4739 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05004740 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04004741
Chris Masonbd56b302009-02-04 09:27:02 -05004742 /*
4743 * cleanup and free the reference on the last node
4744 * we processed
4745 */
Chris Mason56bec292009-03-13 10:10:06 -04004746 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04004747 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004748 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004749 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05004750 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05004751
Chris Mason20524f02007-03-10 06:35:47 -05004752 *level += 1;
4753 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04004754
Chris Masone7a84562008-06-25 16:01:31 -04004755 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004756 return 0;
4757}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004758#endif
Chris Mason20524f02007-03-10 06:35:47 -05004759
Yan Zheng2c47e6052009-06-27 21:07:35 -04004760struct walk_control {
4761 u64 refs[BTRFS_MAX_LEVEL];
4762 u64 flags[BTRFS_MAX_LEVEL];
4763 struct btrfs_key update_progress;
4764 int stage;
4765 int level;
4766 int shared_level;
4767 int update_ref;
4768 int keep_locks;
4769};
4770
4771#define DROP_REFERENCE 1
4772#define UPDATE_BACKREF 2
4773
Chris Mason9aca1d52007-03-13 11:09:37 -04004774/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04004775 * hepler to process tree block while walking down the tree.
4776 *
4777 * when wc->stage == DROP_REFERENCE, this function checks
4778 * reference count of the block. if the block is shared and
4779 * we need update back refs for the subtree rooted at the
4780 * block, this function changes wc->stage to UPDATE_BACKREF
4781 *
4782 * when wc->stage == UPDATE_BACKREF, this function updates
4783 * back refs for pointers in the block.
4784 *
4785 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004786 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004787static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4788 struct btrfs_root *root,
4789 struct btrfs_path *path,
4790 struct walk_control *wc)
4791{
4792 int level = wc->level;
4793 struct extent_buffer *eb = path->nodes[level];
4794 struct btrfs_key key;
4795 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4796 int ret;
4797
4798 if (wc->stage == UPDATE_BACKREF &&
4799 btrfs_header_owner(eb) != root->root_key.objectid)
4800 return 1;
4801
4802 /*
4803 * when reference count of tree block is 1, it won't increase
4804 * again. once full backref flag is set, we never clear it.
4805 */
4806 if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4807 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) {
4808 BUG_ON(!path->locks[level]);
4809 ret = btrfs_lookup_extent_info(trans, root,
4810 eb->start, eb->len,
4811 &wc->refs[level],
4812 &wc->flags[level]);
4813 BUG_ON(ret);
4814 BUG_ON(wc->refs[level] == 0);
4815 }
4816
4817 if (wc->stage == DROP_REFERENCE &&
4818 wc->update_ref && wc->refs[level] > 1) {
4819 BUG_ON(eb == root->node);
4820 BUG_ON(path->slots[level] > 0);
4821 if (level == 0)
4822 btrfs_item_key_to_cpu(eb, &key, path->slots[level]);
4823 else
4824 btrfs_node_key_to_cpu(eb, &key, path->slots[level]);
4825 if (btrfs_header_owner(eb) == root->root_key.objectid &&
4826 btrfs_comp_cpu_keys(&key, &wc->update_progress) >= 0) {
4827 wc->stage = UPDATE_BACKREF;
4828 wc->shared_level = level;
4829 }
4830 }
4831
4832 if (wc->stage == DROP_REFERENCE) {
4833 if (wc->refs[level] > 1)
4834 return 1;
4835
4836 if (path->locks[level] && !wc->keep_locks) {
4837 btrfs_tree_unlock(eb);
4838 path->locks[level] = 0;
4839 }
4840 return 0;
4841 }
4842
4843 /* wc->stage == UPDATE_BACKREF */
4844 if (!(wc->flags[level] & flag)) {
4845 BUG_ON(!path->locks[level]);
4846 ret = btrfs_inc_ref(trans, root, eb, 1);
4847 BUG_ON(ret);
4848 ret = btrfs_dec_ref(trans, root, eb, 0);
4849 BUG_ON(ret);
4850 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
4851 eb->len, flag, 0);
4852 BUG_ON(ret);
4853 wc->flags[level] |= flag;
4854 }
4855
4856 /*
4857 * the block is shared by multiple trees, so it's not good to
4858 * keep the tree lock
4859 */
4860 if (path->locks[level] && level > 0) {
4861 btrfs_tree_unlock(eb);
4862 path->locks[level] = 0;
4863 }
4864 return 0;
4865}
4866
4867/*
4868 * hepler to process tree block while walking up the tree.
4869 *
4870 * when wc->stage == DROP_REFERENCE, this function drops
4871 * reference count on the block.
4872 *
4873 * when wc->stage == UPDATE_BACKREF, this function changes
4874 * wc->stage back to DROP_REFERENCE if we changed wc->stage
4875 * to UPDATE_BACKREF previously while processing the block.
4876 *
4877 * NOTE: return value 1 means we should stop walking up.
4878 */
4879static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
4880 struct btrfs_root *root,
4881 struct btrfs_path *path,
4882 struct walk_control *wc)
4883{
4884 int ret = 0;
4885 int level = wc->level;
4886 struct extent_buffer *eb = path->nodes[level];
4887 u64 parent = 0;
4888
4889 if (wc->stage == UPDATE_BACKREF) {
4890 BUG_ON(wc->shared_level < level);
4891 if (level < wc->shared_level)
4892 goto out;
4893
4894 BUG_ON(wc->refs[level] <= 1);
4895 ret = find_next_key(path, level + 1, &wc->update_progress);
4896 if (ret > 0)
4897 wc->update_ref = 0;
4898
4899 wc->stage = DROP_REFERENCE;
4900 wc->shared_level = -1;
4901 path->slots[level] = 0;
4902
4903 /*
4904 * check reference count again if the block isn't locked.
4905 * we should start walking down the tree again if reference
4906 * count is one.
4907 */
4908 if (!path->locks[level]) {
4909 BUG_ON(level == 0);
4910 btrfs_tree_lock(eb);
4911 btrfs_set_lock_blocking(eb);
4912 path->locks[level] = 1;
4913
4914 ret = btrfs_lookup_extent_info(trans, root,
4915 eb->start, eb->len,
4916 &wc->refs[level],
4917 &wc->flags[level]);
4918 BUG_ON(ret);
4919 BUG_ON(wc->refs[level] == 0);
4920 if (wc->refs[level] == 1) {
4921 btrfs_tree_unlock(eb);
4922 path->locks[level] = 0;
4923 return 1;
4924 }
4925 } else {
4926 BUG_ON(level != 0);
4927 }
4928 }
4929
4930 /* wc->stage == DROP_REFERENCE */
4931 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
4932
4933 if (wc->refs[level] == 1) {
4934 if (level == 0) {
4935 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4936 ret = btrfs_dec_ref(trans, root, eb, 1);
4937 else
4938 ret = btrfs_dec_ref(trans, root, eb, 0);
4939 BUG_ON(ret);
4940 }
4941 /* make block locked assertion in clean_tree_block happy */
4942 if (!path->locks[level] &&
4943 btrfs_header_generation(eb) == trans->transid) {
4944 btrfs_tree_lock(eb);
4945 btrfs_set_lock_blocking(eb);
4946 path->locks[level] = 1;
4947 }
4948 clean_tree_block(trans, root, eb);
4949 }
4950
4951 if (eb == root->node) {
4952 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4953 parent = eb->start;
4954 else
4955 BUG_ON(root->root_key.objectid !=
4956 btrfs_header_owner(eb));
4957 } else {
4958 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
4959 parent = path->nodes[level + 1]->start;
4960 else
4961 BUG_ON(root->root_key.objectid !=
4962 btrfs_header_owner(path->nodes[level + 1]));
4963 }
4964
4965 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
4966 root->root_key.objectid, level, 0);
4967 BUG_ON(ret);
4968out:
4969 wc->refs[level] = 0;
4970 wc->flags[level] = 0;
4971 return ret;
4972}
4973
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004974static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4975 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004976 struct btrfs_path *path,
4977 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04004978{
4979 struct extent_buffer *next;
4980 struct extent_buffer *cur;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004981 u64 bytenr;
4982 u64 ptr_gen;
4983 u32 blocksize;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004984 int level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004985 int ret;
4986
Yan Zheng2c47e6052009-06-27 21:07:35 -04004987 while (level >= 0) {
4988 cur = path->nodes[level];
4989 BUG_ON(path->slots[level] >= btrfs_header_nritems(cur));
Yan Zhengf82d02d2008-10-29 14:49:05 -04004990
Yan Zheng2c47e6052009-06-27 21:07:35 -04004991 ret = walk_down_proc(trans, root, path, wc);
4992 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04004993 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004994
Yan Zheng2c47e6052009-06-27 21:07:35 -04004995 if (level == 0)
4996 break;
4997
4998 bytenr = btrfs_node_blockptr(cur, path->slots[level]);
4999 blocksize = btrfs_level_size(root, level - 1);
5000 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[level]);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005001
5002 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
5003 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005004 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005005
Yan Zheng2c47e6052009-06-27 21:07:35 -04005006 level--;
5007 BUG_ON(level != btrfs_header_level(next));
5008 path->nodes[level] = next;
5009 path->slots[level] = 0;
5010 path->locks[level] = 1;
5011 wc->level = level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005012 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005013 return 0;
5014}
5015
Chris Masond3977122009-01-05 21:25:51 -05005016static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005017 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005018 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005019 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005020{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005021 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005022 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005023
Yan Zheng2c47e6052009-06-27 21:07:35 -04005024 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5025 while (level < max_level && path->nodes[level]) {
5026 wc->level = level;
5027 if (path->slots[level] + 1 <
5028 btrfs_header_nritems(path->nodes[level])) {
5029 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005030 return 0;
5031 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005032 ret = walk_up_proc(trans, root, path, wc);
5033 if (ret > 0)
5034 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05005035
Yan Zheng2c47e6052009-06-27 21:07:35 -04005036 if (path->locks[level]) {
5037 btrfs_tree_unlock(path->nodes[level]);
5038 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005039 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005040 free_extent_buffer(path->nodes[level]);
5041 path->nodes[level] = NULL;
5042 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005043 }
5044 }
5045 return 1;
5046}
5047
Chris Mason9aca1d52007-03-13 11:09:37 -04005048/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005049 * drop a subvolume tree.
5050 *
5051 * this function traverses the tree freeing any blocks that only
5052 * referenced by the tree.
5053 *
5054 * when a shared tree block is found. this function decreases its
5055 * reference count by one. if update_ref is true, this function
5056 * also make sure backrefs for the shared block and all lower level
5057 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04005058 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005059int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05005060{
Chris Mason5caf2a02007-04-02 11:20:42 -04005061 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005062 struct btrfs_trans_handle *trans;
5063 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005064 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005065 struct walk_control *wc;
5066 struct btrfs_key key;
5067 int err = 0;
5068 int ret;
5069 int level;
Chris Mason20524f02007-03-10 06:35:47 -05005070
Chris Mason5caf2a02007-04-02 11:20:42 -04005071 path = btrfs_alloc_path();
5072 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05005073
Yan Zheng2c47e6052009-06-27 21:07:35 -04005074 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5075 BUG_ON(!wc);
5076
5077 trans = btrfs_start_transaction(tree_root, 1);
5078
Chris Mason9f3a7422007-08-07 15:52:19 -04005079 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005080 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005081 path->nodes[level] = btrfs_lock_root_node(root);
5082 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005083 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005084 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005085 memset(&wc->update_progress, 0,
5086 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005087 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005088 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005089 memcpy(&wc->update_progress, &key,
5090 sizeof(wc->update_progress));
5091
Chris Mason6702ed42007-08-07 16:15:09 -04005092 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005093 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005094 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005095 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5096 path->lowest_level = 0;
5097 if (ret < 0) {
5098 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005099 goto out;
5100 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005101 btrfs_node_key_to_cpu(path->nodes[level], &key,
5102 path->slots[level]);
5103 WARN_ON(memcmp(&key, &wc->update_progress, sizeof(key)));
5104
Chris Mason7d9eb122008-07-08 14:19:17 -04005105 /*
5106 * unlock our path, this is safe because only this
5107 * function is allowed to delete this snapshot
5108 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005109 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005110
Yan Zheng2c47e6052009-06-27 21:07:35 -04005111 level = btrfs_header_level(root->node);
5112 while (1) {
5113 btrfs_tree_lock(path->nodes[level]);
5114 btrfs_set_lock_blocking(path->nodes[level]);
5115
5116 ret = btrfs_lookup_extent_info(trans, root,
5117 path->nodes[level]->start,
5118 path->nodes[level]->len,
5119 &wc->refs[level],
5120 &wc->flags[level]);
5121 BUG_ON(ret);
5122 BUG_ON(wc->refs[level] == 0);
5123
5124 if (level == root_item->drop_level)
5125 break;
5126
5127 btrfs_tree_unlock(path->nodes[level]);
5128 WARN_ON(wc->refs[level] != 1);
5129 level--;
5130 }
5131 }
5132
5133 wc->level = level;
5134 wc->shared_level = -1;
5135 wc->stage = DROP_REFERENCE;
5136 wc->update_ref = update_ref;
5137 wc->keep_locks = 0;
5138
5139 while (1) {
5140 ret = walk_down_tree(trans, root, path, wc);
5141 if (ret < 0) {
5142 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005143 break;
5144 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005145
5146 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5147 if (ret < 0) {
5148 err = ret;
5149 break;
5150 }
5151
5152 if (ret > 0) {
5153 BUG_ON(wc->stage != DROP_REFERENCE);
5154 break;
5155 }
5156
5157 if (wc->stage == DROP_REFERENCE) {
5158 level = wc->level;
5159 btrfs_node_key(path->nodes[level],
5160 &root_item->drop_progress,
5161 path->slots[level]);
5162 root_item->drop_level = level;
5163 }
5164
5165 BUG_ON(wc->level == 0);
5166 if (trans->transaction->in_commit ||
5167 trans->transaction->delayed_refs.flushing) {
5168 ret = btrfs_update_root(trans, tree_root,
5169 &root->root_key,
5170 root_item);
5171 BUG_ON(ret);
5172
5173 btrfs_end_transaction(trans, tree_root);
5174 trans = btrfs_start_transaction(tree_root, 1);
5175 } else {
5176 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005177 update = trans->delayed_ref_updates;
5178 trans->delayed_ref_updates = 0;
5179 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005180 btrfs_run_delayed_refs(trans, tree_root,
5181 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005182 }
Chris Mason20524f02007-03-10 06:35:47 -05005183 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005184 btrfs_release_path(root, path);
5185 BUG_ON(err);
5186
5187 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5188 BUG_ON(ret);
5189
5190 free_extent_buffer(root->node);
5191 free_extent_buffer(root->commit_root);
5192 kfree(root);
Chris Mason9f3a7422007-08-07 15:52:19 -04005193out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005194 btrfs_end_transaction(trans, tree_root);
5195 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005196 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005197 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005198}
Chris Mason9078a3e2007-04-26 16:46:15 -04005199
Yan Zheng2c47e6052009-06-27 21:07:35 -04005200/*
5201 * drop subtree rooted at tree block 'node'.
5202 *
5203 * NOTE: this function will unlock and release tree block 'node'
5204 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005205int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5206 struct btrfs_root *root,
5207 struct extent_buffer *node,
5208 struct extent_buffer *parent)
5209{
5210 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005211 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005212 int level;
5213 int parent_level;
5214 int ret = 0;
5215 int wret;
5216
Yan Zheng2c47e6052009-06-27 21:07:35 -04005217 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5218
Yan Zhengf82d02d2008-10-29 14:49:05 -04005219 path = btrfs_alloc_path();
5220 BUG_ON(!path);
5221
Yan Zheng2c47e6052009-06-27 21:07:35 -04005222 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5223 BUG_ON(!wc);
5224
Chris Masonb9447ef2009-03-09 11:45:38 -04005225 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005226 parent_level = btrfs_header_level(parent);
5227 extent_buffer_get(parent);
5228 path->nodes[parent_level] = parent;
5229 path->slots[parent_level] = btrfs_header_nritems(parent);
5230
Chris Masonb9447ef2009-03-09 11:45:38 -04005231 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005232 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005233 path->nodes[level] = node;
5234 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005235 path->locks[level] = 1;
5236
5237 wc->refs[parent_level] = 1;
5238 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5239 wc->level = level;
5240 wc->shared_level = -1;
5241 wc->stage = DROP_REFERENCE;
5242 wc->update_ref = 0;
5243 wc->keep_locks = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005244
5245 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005246 wret = walk_down_tree(trans, root, path, wc);
5247 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005248 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005249 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005250 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005251
Yan Zheng2c47e6052009-06-27 21:07:35 -04005252 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005253 if (wret < 0)
5254 ret = wret;
5255 if (wret != 0)
5256 break;
5257 }
5258
Yan Zheng2c47e6052009-06-27 21:07:35 -04005259 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005260 btrfs_free_path(path);
5261 return ret;
5262}
5263
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005264#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005265static unsigned long calc_ra(unsigned long start, unsigned long last,
5266 unsigned long nr)
5267{
5268 return min(last, start + nr - 1);
5269}
5270
Chris Masond3977122009-01-05 21:25:51 -05005271static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005272 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005273{
5274 u64 page_start;
5275 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005276 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005277 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005278 unsigned long i;
5279 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005280 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005281 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005282 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005283 unsigned int total_read = 0;
5284 unsigned int total_dirty = 0;
5285 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005286
5287 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005288
5289 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005290 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005291 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5292
Zheng Yan1a40e232008-09-26 10:09:34 -04005293 /* make sure the dirty trick played by the caller work */
5294 ret = invalidate_inode_pages2_range(inode->i_mapping,
5295 first_index, last_index);
5296 if (ret)
5297 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005298
Chris Mason4313b392008-01-03 09:08:48 -05005299 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005300
Zheng Yan1a40e232008-09-26 10:09:34 -04005301 for (i = first_index ; i <= last_index; i++) {
5302 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005303 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005304 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005305 }
5306 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005307again:
5308 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005309 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005310 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005311 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005312 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005313 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005314 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005315 if (!PageUptodate(page)) {
5316 btrfs_readpage(NULL, page);
5317 lock_page(page);
5318 if (!PageUptodate(page)) {
5319 unlock_page(page);
5320 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005321 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005322 goto out_unlock;
5323 }
5324 }
Chris Masonec44a352008-04-28 15:29:52 -04005325 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005326
Chris Masonedbd8d42007-12-21 16:27:24 -05005327 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5328 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005329 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005330
Chris Mason3eaa2882008-07-24 11:57:52 -04005331 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5332 if (ordered) {
5333 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5334 unlock_page(page);
5335 page_cache_release(page);
5336 btrfs_start_ordered_extent(inode, ordered, 1);
5337 btrfs_put_ordered_extent(ordered);
5338 goto again;
5339 }
5340 set_page_extent_mapped(page);
5341
Zheng Yan1a40e232008-09-26 10:09:34 -04005342 if (i == first_index)
5343 set_extent_bits(io_tree, page_start, page_end,
5344 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005345 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005346
Chris Masona061fc82008-05-07 11:43:44 -04005347 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005348 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005349
Chris Masond1310b22008-01-24 16:13:08 -05005350 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005351 unlock_page(page);
5352 page_cache_release(page);
5353 }
5354
5355out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005356 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005357 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005358 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005359 return ret;
5360}
5361
Chris Masond3977122009-01-05 21:25:51 -05005362static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005363 struct btrfs_key *extent_key,
5364 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005365{
Zheng Yan1a40e232008-09-26 10:09:34 -04005366 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5367 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5368 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005369 u64 start = extent_key->objectid - offset;
5370 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005371
5372 em = alloc_extent_map(GFP_NOFS);
5373 BUG_ON(!em || IS_ERR(em));
5374
Yan Zheng66435582008-10-30 14:19:50 -04005375 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005376 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005377 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005378 em->block_start = extent_key->objectid;
5379 em->bdev = root->fs_info->fs_devices->latest_bdev;
5380 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5381
5382 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005383 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005384 while (1) {
5385 int ret;
5386 spin_lock(&em_tree->lock);
5387 ret = add_extent_mapping(em_tree, em);
5388 spin_unlock(&em_tree->lock);
5389 if (ret != -EEXIST) {
5390 free_extent_map(em);
5391 break;
5392 }
Yan Zheng66435582008-10-30 14:19:50 -04005393 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005394 }
Yan Zheng66435582008-10-30 14:19:50 -04005395 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005396
Yan Zheng66435582008-10-30 14:19:50 -04005397 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005398}
5399
5400struct btrfs_ref_path {
5401 u64 extent_start;
5402 u64 nodes[BTRFS_MAX_LEVEL];
5403 u64 root_objectid;
5404 u64 root_generation;
5405 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005406 u32 num_refs;
5407 int lowest_level;
5408 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005409 int shared_level;
5410
5411 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5412 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005413};
5414
5415struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005416 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005417 u64 disk_bytenr;
5418 u64 disk_num_bytes;
5419 u64 offset;
5420 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005421 u8 compression;
5422 u8 encryption;
5423 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005424};
5425
5426static int is_cowonly_root(u64 root_objectid)
5427{
5428 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5429 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5430 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5431 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005432 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5433 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005434 return 1;
5435 return 0;
5436}
5437
Chris Masond3977122009-01-05 21:25:51 -05005438static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005439 struct btrfs_root *extent_root,
5440 struct btrfs_ref_path *ref_path,
5441 int first_time)
5442{
5443 struct extent_buffer *leaf;
5444 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005445 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005446 struct btrfs_key key;
5447 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005448 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005449 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005450 int level;
5451 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005452
Zheng Yan1a40e232008-09-26 10:09:34 -04005453 path = btrfs_alloc_path();
5454 if (!path)
5455 return -ENOMEM;
5456
Zheng Yan1a40e232008-09-26 10:09:34 -04005457 if (first_time) {
5458 ref_path->lowest_level = -1;
5459 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005460 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005461 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005462 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005463walk_down:
5464 level = ref_path->current_level - 1;
5465 while (level >= -1) {
5466 u64 parent;
5467 if (level < ref_path->lowest_level)
5468 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005469
Chris Masond3977122009-01-05 21:25:51 -05005470 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005471 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005472 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005473 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005474 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005475
Zheng Yan1a40e232008-09-26 10:09:34 -04005476 parent = ref_path->nodes[level + 1];
5477 ref_path->nodes[level + 1] = 0;
5478 ref_path->current_level = level;
5479 BUG_ON(parent == 0);
5480
5481 key.objectid = bytenr;
5482 key.offset = parent + 1;
5483 key.type = BTRFS_EXTENT_REF_KEY;
5484
5485 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005486 if (ret < 0)
5487 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005488 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005489
Chris Masonedbd8d42007-12-21 16:27:24 -05005490 leaf = path->nodes[0];
5491 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005492 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005493 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005494 if (ret < 0)
5495 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005496 if (ret > 0)
5497 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005498 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005499 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005500
5501 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005502 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005503 found_key.type == BTRFS_EXTENT_REF_KEY) {
5504 if (level < ref_path->shared_level)
5505 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005506 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005507 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005508next:
5509 level--;
5510 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005511 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005512 }
5513 /* reached lowest level */
5514 ret = 1;
5515 goto out;
5516walk_up:
5517 level = ref_path->current_level;
5518 while (level < BTRFS_MAX_LEVEL - 1) {
5519 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005520
5521 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005522 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005523 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005524 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005525
Zheng Yan1a40e232008-09-26 10:09:34 -04005526 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005527
Zheng Yan1a40e232008-09-26 10:09:34 -04005528 key.objectid = bytenr;
5529 key.offset = 0;
5530 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005531
Zheng Yan1a40e232008-09-26 10:09:34 -04005532 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5533 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005534 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005535
5536 leaf = path->nodes[0];
5537 nritems = btrfs_header_nritems(leaf);
5538 if (path->slots[0] >= nritems) {
5539 ret = btrfs_next_leaf(extent_root, path);
5540 if (ret < 0)
5541 goto out;
5542 if (ret > 0) {
5543 /* the extent was freed by someone */
5544 if (ref_path->lowest_level == level)
5545 goto out;
5546 btrfs_release_path(extent_root, path);
5547 goto walk_down;
5548 }
5549 leaf = path->nodes[0];
5550 }
5551
5552 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5553 if (found_key.objectid != bytenr ||
5554 found_key.type != BTRFS_EXTENT_REF_KEY) {
5555 /* the extent was freed by someone */
5556 if (ref_path->lowest_level == level) {
5557 ret = 1;
5558 goto out;
5559 }
5560 btrfs_release_path(extent_root, path);
5561 goto walk_down;
5562 }
5563found:
5564 ref = btrfs_item_ptr(leaf, path->slots[0],
5565 struct btrfs_extent_ref);
5566 ref_objectid = btrfs_ref_objectid(leaf, ref);
5567 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5568 if (first_time) {
5569 level = (int)ref_objectid;
5570 BUG_ON(level >= BTRFS_MAX_LEVEL);
5571 ref_path->lowest_level = level;
5572 ref_path->current_level = level;
5573 ref_path->nodes[level] = bytenr;
5574 } else {
5575 WARN_ON(ref_objectid != level);
5576 }
5577 } else {
5578 WARN_ON(level != -1);
5579 }
5580 first_time = 0;
5581
5582 if (ref_path->lowest_level == level) {
5583 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005584 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5585 }
5586
5587 /*
5588 * the block is tree root or the block isn't in reference
5589 * counted tree.
5590 */
5591 if (found_key.objectid == found_key.offset ||
5592 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5593 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5594 ref_path->root_generation =
5595 btrfs_ref_generation(leaf, ref);
5596 if (level < 0) {
5597 /* special reference from the tree log */
5598 ref_path->nodes[0] = found_key.offset;
5599 ref_path->current_level = 0;
5600 }
5601 ret = 0;
5602 goto out;
5603 }
5604
5605 level++;
5606 BUG_ON(ref_path->nodes[level] != 0);
5607 ref_path->nodes[level] = found_key.offset;
5608 ref_path->current_level = level;
5609
5610 /*
5611 * the reference was created in the running transaction,
5612 * no need to continue walking up.
5613 */
5614 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5615 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5616 ref_path->root_generation =
5617 btrfs_ref_generation(leaf, ref);
5618 ret = 0;
5619 goto out;
5620 }
5621
5622 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005623 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005624 }
5625 /* reached max tree level, but no tree root found. */
5626 BUG();
5627out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005628 btrfs_free_path(path);
5629 return ret;
5630}
5631
5632static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5633 struct btrfs_root *extent_root,
5634 struct btrfs_ref_path *ref_path,
5635 u64 extent_start)
5636{
5637 memset(ref_path, 0, sizeof(*ref_path));
5638 ref_path->extent_start = extent_start;
5639
5640 return __next_ref_path(trans, extent_root, ref_path, 1);
5641}
5642
5643static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5644 struct btrfs_root *extent_root,
5645 struct btrfs_ref_path *ref_path)
5646{
5647 return __next_ref_path(trans, extent_root, ref_path, 0);
5648}
5649
Chris Masond3977122009-01-05 21:25:51 -05005650static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005651 struct btrfs_key *extent_key,
5652 u64 offset, int no_fragment,
5653 struct disk_extent **extents,
5654 int *nr_extents)
5655{
5656 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5657 struct btrfs_path *path;
5658 struct btrfs_file_extent_item *fi;
5659 struct extent_buffer *leaf;
5660 struct disk_extent *exts = *extents;
5661 struct btrfs_key found_key;
5662 u64 cur_pos;
5663 u64 last_byte;
5664 u32 nritems;
5665 int nr = 0;
5666 int max = *nr_extents;
5667 int ret;
5668
5669 WARN_ON(!no_fragment && *extents);
5670 if (!exts) {
5671 max = 1;
5672 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
5673 if (!exts)
5674 return -ENOMEM;
5675 }
5676
5677 path = btrfs_alloc_path();
5678 BUG_ON(!path);
5679
5680 cur_pos = extent_key->objectid - offset;
5681 last_byte = extent_key->objectid + extent_key->offset;
5682 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
5683 cur_pos, 0);
5684 if (ret < 0)
5685 goto out;
5686 if (ret > 0) {
5687 ret = -ENOENT;
5688 goto out;
5689 }
5690
5691 while (1) {
5692 leaf = path->nodes[0];
5693 nritems = btrfs_header_nritems(leaf);
5694 if (path->slots[0] >= nritems) {
5695 ret = btrfs_next_leaf(root, path);
5696 if (ret < 0)
5697 goto out;
5698 if (ret > 0)
5699 break;
5700 leaf = path->nodes[0];
5701 }
5702
5703 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5704 if (found_key.offset != cur_pos ||
5705 found_key.type != BTRFS_EXTENT_DATA_KEY ||
5706 found_key.objectid != reloc_inode->i_ino)
5707 break;
5708
5709 fi = btrfs_item_ptr(leaf, path->slots[0],
5710 struct btrfs_file_extent_item);
5711 if (btrfs_file_extent_type(leaf, fi) !=
5712 BTRFS_FILE_EXTENT_REG ||
5713 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5714 break;
5715
5716 if (nr == max) {
5717 struct disk_extent *old = exts;
5718 max *= 2;
5719 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
5720 memcpy(exts, old, sizeof(*exts) * nr);
5721 if (old != *extents)
5722 kfree(old);
5723 }
5724
5725 exts[nr].disk_bytenr =
5726 btrfs_file_extent_disk_bytenr(leaf, fi);
5727 exts[nr].disk_num_bytes =
5728 btrfs_file_extent_disk_num_bytes(leaf, fi);
5729 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
5730 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04005731 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
5732 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
5733 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
5734 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
5735 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04005736 BUG_ON(exts[nr].offset > 0);
5737 BUG_ON(exts[nr].compression || exts[nr].encryption);
5738 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005739
5740 cur_pos += exts[nr].num_bytes;
5741 nr++;
5742
5743 if (cur_pos + offset >= last_byte)
5744 break;
5745
5746 if (no_fragment) {
5747 ret = 1;
5748 goto out;
5749 }
5750 path->slots[0]++;
5751 }
5752
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005753 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04005754 if (cur_pos + offset < last_byte) {
5755 ret = -ENOENT;
5756 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05005757 }
5758 ret = 0;
5759out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005760 btrfs_free_path(path);
5761 if (ret) {
5762 if (exts != *extents)
5763 kfree(exts);
5764 } else {
5765 *extents = exts;
5766 *nr_extents = nr;
5767 }
5768 return ret;
5769}
5770
Chris Masond3977122009-01-05 21:25:51 -05005771static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005772 struct btrfs_root *root,
5773 struct btrfs_path *path,
5774 struct btrfs_key *extent_key,
5775 struct btrfs_key *leaf_key,
5776 struct btrfs_ref_path *ref_path,
5777 struct disk_extent *new_extents,
5778 int nr_extents)
5779{
5780 struct extent_buffer *leaf;
5781 struct btrfs_file_extent_item *fi;
5782 struct inode *inode = NULL;
5783 struct btrfs_key key;
5784 u64 lock_start = 0;
5785 u64 lock_end = 0;
5786 u64 num_bytes;
5787 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05005788 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005789 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005790 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005791 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04005792 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04005793 int ret;
5794
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005795 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04005796 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005797 if (key.objectid < ref_path->owner_objectid ||
5798 (key.objectid == ref_path->owner_objectid &&
5799 key.type < BTRFS_EXTENT_DATA_KEY)) {
5800 key.objectid = ref_path->owner_objectid;
5801 key.type = BTRFS_EXTENT_DATA_KEY;
5802 key.offset = 0;
5803 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005804 }
5805
5806 while (1) {
5807 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5808 if (ret < 0)
5809 goto out;
5810
5811 leaf = path->nodes[0];
5812 nritems = btrfs_header_nritems(leaf);
5813next:
5814 if (extent_locked && ret > 0) {
5815 /*
5816 * the file extent item was modified by someone
5817 * before the extent got locked.
5818 */
Zheng Yan1a40e232008-09-26 10:09:34 -04005819 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5820 lock_end, GFP_NOFS);
5821 extent_locked = 0;
5822 }
5823
5824 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005825 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04005826 break;
5827
5828 BUG_ON(extent_locked);
5829 ret = btrfs_next_leaf(root, path);
5830 if (ret < 0)
5831 goto out;
5832 if (ret > 0)
5833 break;
5834 leaf = path->nodes[0];
5835 nritems = btrfs_header_nritems(leaf);
5836 }
5837
5838 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5839
5840 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
5841 if ((key.objectid > ref_path->owner_objectid) ||
5842 (key.objectid == ref_path->owner_objectid &&
5843 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05005844 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005845 break;
5846 }
5847
5848 if (inode && key.objectid != inode->i_ino) {
5849 BUG_ON(extent_locked);
5850 btrfs_release_path(root, path);
5851 mutex_unlock(&inode->i_mutex);
5852 iput(inode);
5853 inode = NULL;
5854 continue;
5855 }
5856
5857 if (key.type != BTRFS_EXTENT_DATA_KEY) {
5858 path->slots[0]++;
5859 ret = 1;
5860 goto next;
5861 }
5862 fi = btrfs_item_ptr(leaf, path->slots[0],
5863 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04005864 extent_type = btrfs_file_extent_type(leaf, fi);
5865 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
5866 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04005867 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
5868 extent_key->objectid)) {
5869 path->slots[0]++;
5870 ret = 1;
5871 goto next;
5872 }
5873
5874 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5875 ext_offset = btrfs_file_extent_offset(leaf, fi);
5876
Yan Zheng86288a12009-01-21 10:49:16 -05005877 if (search_end == (u64)-1) {
5878 search_end = key.offset - ext_offset +
5879 btrfs_file_extent_ram_bytes(leaf, fi);
5880 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005881
5882 if (!extent_locked) {
5883 lock_start = key.offset;
5884 lock_end = lock_start + num_bytes - 1;
5885 } else {
Yan Zheng66435582008-10-30 14:19:50 -04005886 if (lock_start > key.offset ||
5887 lock_end + 1 < key.offset + num_bytes) {
5888 unlock_extent(&BTRFS_I(inode)->io_tree,
5889 lock_start, lock_end, GFP_NOFS);
5890 extent_locked = 0;
5891 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005892 }
5893
5894 if (!inode) {
5895 btrfs_release_path(root, path);
5896
5897 inode = btrfs_iget_locked(root->fs_info->sb,
5898 key.objectid, root);
5899 if (inode->i_state & I_NEW) {
5900 BTRFS_I(inode)->root = root;
5901 BTRFS_I(inode)->location.objectid =
5902 key.objectid;
5903 BTRFS_I(inode)->location.type =
5904 BTRFS_INODE_ITEM_KEY;
5905 BTRFS_I(inode)->location.offset = 0;
5906 btrfs_read_locked_inode(inode);
5907 unlock_new_inode(inode);
5908 }
5909 /*
5910 * some code call btrfs_commit_transaction while
5911 * holding the i_mutex, so we can't use mutex_lock
5912 * here.
5913 */
5914 if (is_bad_inode(inode) ||
5915 !mutex_trylock(&inode->i_mutex)) {
5916 iput(inode);
5917 inode = NULL;
5918 key.offset = (u64)-1;
5919 goto skip;
5920 }
5921 }
5922
5923 if (!extent_locked) {
5924 struct btrfs_ordered_extent *ordered;
5925
5926 btrfs_release_path(root, path);
5927
5928 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5929 lock_end, GFP_NOFS);
5930 ordered = btrfs_lookup_first_ordered_extent(inode,
5931 lock_end);
5932 if (ordered &&
5933 ordered->file_offset <= lock_end &&
5934 ordered->file_offset + ordered->len > lock_start) {
5935 unlock_extent(&BTRFS_I(inode)->io_tree,
5936 lock_start, lock_end, GFP_NOFS);
5937 btrfs_start_ordered_extent(inode, ordered, 1);
5938 btrfs_put_ordered_extent(ordered);
5939 key.offset += num_bytes;
5940 goto skip;
5941 }
5942 if (ordered)
5943 btrfs_put_ordered_extent(ordered);
5944
Zheng Yan1a40e232008-09-26 10:09:34 -04005945 extent_locked = 1;
5946 continue;
5947 }
5948
5949 if (nr_extents == 1) {
5950 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04005951 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5952 new_extents[0].disk_bytenr);
5953 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5954 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005955 btrfs_mark_buffer_dirty(leaf);
5956
5957 btrfs_drop_extent_cache(inode, key.offset,
5958 key.offset + num_bytes - 1, 0);
5959
5960 ret = btrfs_inc_extent_ref(trans, root,
5961 new_extents[0].disk_bytenr,
5962 new_extents[0].disk_num_bytes,
5963 leaf->start,
5964 root->root_key.objectid,
5965 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005966 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005967 BUG_ON(ret);
5968
5969 ret = btrfs_free_extent(trans, root,
5970 extent_key->objectid,
5971 extent_key->offset,
5972 leaf->start,
5973 btrfs_header_owner(leaf),
5974 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005975 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005976 BUG_ON(ret);
5977
5978 btrfs_release_path(root, path);
5979 key.offset += num_bytes;
5980 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04005981 BUG_ON(1);
5982#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04005983 u64 alloc_hint;
5984 u64 extent_len;
5985 int i;
5986 /*
5987 * drop old extent pointer at first, then insert the
5988 * new pointers one bye one
5989 */
5990 btrfs_release_path(root, path);
5991 ret = btrfs_drop_extents(trans, root, inode, key.offset,
5992 key.offset + num_bytes,
5993 key.offset, &alloc_hint);
5994 BUG_ON(ret);
5995
5996 for (i = 0; i < nr_extents; i++) {
5997 if (ext_offset >= new_extents[i].num_bytes) {
5998 ext_offset -= new_extents[i].num_bytes;
5999 continue;
6000 }
6001 extent_len = min(new_extents[i].num_bytes -
6002 ext_offset, num_bytes);
6003
6004 ret = btrfs_insert_empty_item(trans, root,
6005 path, &key,
6006 sizeof(*fi));
6007 BUG_ON(ret);
6008
6009 leaf = path->nodes[0];
6010 fi = btrfs_item_ptr(leaf, path->slots[0],
6011 struct btrfs_file_extent_item);
6012 btrfs_set_file_extent_generation(leaf, fi,
6013 trans->transid);
6014 btrfs_set_file_extent_type(leaf, fi,
6015 BTRFS_FILE_EXTENT_REG);
6016 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6017 new_extents[i].disk_bytenr);
6018 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6019 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04006020 btrfs_set_file_extent_ram_bytes(leaf, fi,
6021 new_extents[i].ram_bytes);
6022
6023 btrfs_set_file_extent_compression(leaf, fi,
6024 new_extents[i].compression);
6025 btrfs_set_file_extent_encryption(leaf, fi,
6026 new_extents[i].encryption);
6027 btrfs_set_file_extent_other_encoding(leaf, fi,
6028 new_extents[i].other_encoding);
6029
Zheng Yan1a40e232008-09-26 10:09:34 -04006030 btrfs_set_file_extent_num_bytes(leaf, fi,
6031 extent_len);
6032 ext_offset += new_extents[i].offset;
6033 btrfs_set_file_extent_offset(leaf, fi,
6034 ext_offset);
6035 btrfs_mark_buffer_dirty(leaf);
6036
6037 btrfs_drop_extent_cache(inode, key.offset,
6038 key.offset + extent_len - 1, 0);
6039
6040 ret = btrfs_inc_extent_ref(trans, root,
6041 new_extents[i].disk_bytenr,
6042 new_extents[i].disk_num_bytes,
6043 leaf->start,
6044 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006045 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006046 BUG_ON(ret);
6047 btrfs_release_path(root, path);
6048
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006049 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04006050
6051 ext_offset = 0;
6052 num_bytes -= extent_len;
6053 key.offset += extent_len;
6054
6055 if (num_bytes == 0)
6056 break;
6057 }
6058 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006059#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04006060 }
6061
6062 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006063 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6064 lock_end, GFP_NOFS);
6065 extent_locked = 0;
6066 }
6067skip:
6068 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05006069 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006070 break;
6071
6072 cond_resched();
6073 }
6074 ret = 0;
6075out:
6076 btrfs_release_path(root, path);
6077 if (inode) {
6078 mutex_unlock(&inode->i_mutex);
6079 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006080 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6081 lock_end, GFP_NOFS);
6082 }
6083 iput(inode);
6084 }
6085 return ret;
6086}
6087
Zheng Yan1a40e232008-09-26 10:09:34 -04006088int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6089 struct btrfs_root *root,
6090 struct extent_buffer *buf, u64 orig_start)
6091{
6092 int level;
6093 int ret;
6094
6095 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6096 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6097
6098 level = btrfs_header_level(buf);
6099 if (level == 0) {
6100 struct btrfs_leaf_ref *ref;
6101 struct btrfs_leaf_ref *orig_ref;
6102
6103 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6104 if (!orig_ref)
6105 return -ENOENT;
6106
6107 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6108 if (!ref) {
6109 btrfs_free_leaf_ref(root, orig_ref);
6110 return -ENOMEM;
6111 }
6112
6113 ref->nritems = orig_ref->nritems;
6114 memcpy(ref->extents, orig_ref->extents,
6115 sizeof(ref->extents[0]) * ref->nritems);
6116
6117 btrfs_free_leaf_ref(root, orig_ref);
6118
6119 ref->root_gen = trans->transid;
6120 ref->bytenr = buf->start;
6121 ref->owner = btrfs_header_owner(buf);
6122 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006123
Zheng Yan1a40e232008-09-26 10:09:34 -04006124 ret = btrfs_add_leaf_ref(root, ref, 0);
6125 WARN_ON(ret);
6126 btrfs_free_leaf_ref(root, ref);
6127 }
6128 return 0;
6129}
6130
Chris Masond3977122009-01-05 21:25:51 -05006131static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006132 struct extent_buffer *leaf,
6133 struct btrfs_block_group_cache *group,
6134 struct btrfs_root *target_root)
6135{
6136 struct btrfs_key key;
6137 struct inode *inode = NULL;
6138 struct btrfs_file_extent_item *fi;
6139 u64 num_bytes;
6140 u64 skip_objectid = 0;
6141 u32 nritems;
6142 u32 i;
6143
6144 nritems = btrfs_header_nritems(leaf);
6145 for (i = 0; i < nritems; i++) {
6146 btrfs_item_key_to_cpu(leaf, &key, i);
6147 if (key.objectid == skip_objectid ||
6148 key.type != BTRFS_EXTENT_DATA_KEY)
6149 continue;
6150 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6151 if (btrfs_file_extent_type(leaf, fi) ==
6152 BTRFS_FILE_EXTENT_INLINE)
6153 continue;
6154 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6155 continue;
6156 if (!inode || inode->i_ino != key.objectid) {
6157 iput(inode);
6158 inode = btrfs_ilookup(target_root->fs_info->sb,
6159 key.objectid, target_root, 1);
6160 }
6161 if (!inode) {
6162 skip_objectid = key.objectid;
6163 continue;
6164 }
6165 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6166
6167 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6168 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006169 btrfs_drop_extent_cache(inode, key.offset,
6170 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006171 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6172 key.offset + num_bytes - 1, GFP_NOFS);
6173 cond_resched();
6174 }
6175 iput(inode);
6176 return 0;
6177}
6178
Chris Masond3977122009-01-05 21:25:51 -05006179static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006180 struct btrfs_root *root,
6181 struct extent_buffer *leaf,
6182 struct btrfs_block_group_cache *group,
6183 struct inode *reloc_inode)
6184{
6185 struct btrfs_key key;
6186 struct btrfs_key extent_key;
6187 struct btrfs_file_extent_item *fi;
6188 struct btrfs_leaf_ref *ref;
6189 struct disk_extent *new_extent;
6190 u64 bytenr;
6191 u64 num_bytes;
6192 u32 nritems;
6193 u32 i;
6194 int ext_index;
6195 int nr_extent;
6196 int ret;
6197
6198 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6199 BUG_ON(!new_extent);
6200
6201 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6202 BUG_ON(!ref);
6203
6204 ext_index = -1;
6205 nritems = btrfs_header_nritems(leaf);
6206 for (i = 0; i < nritems; i++) {
6207 btrfs_item_key_to_cpu(leaf, &key, i);
6208 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6209 continue;
6210 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6211 if (btrfs_file_extent_type(leaf, fi) ==
6212 BTRFS_FILE_EXTENT_INLINE)
6213 continue;
6214 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6215 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6216 if (bytenr == 0)
6217 continue;
6218
6219 ext_index++;
6220 if (bytenr >= group->key.objectid + group->key.offset ||
6221 bytenr + num_bytes <= group->key.objectid)
6222 continue;
6223
6224 extent_key.objectid = bytenr;
6225 extent_key.offset = num_bytes;
6226 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6227 nr_extent = 1;
6228 ret = get_new_locations(reloc_inode, &extent_key,
6229 group->key.objectid, 1,
6230 &new_extent, &nr_extent);
6231 if (ret > 0)
6232 continue;
6233 BUG_ON(ret < 0);
6234
6235 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6236 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6237 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6238 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6239
Zheng Yan1a40e232008-09-26 10:09:34 -04006240 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6241 new_extent->disk_bytenr);
6242 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6243 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006244 btrfs_mark_buffer_dirty(leaf);
6245
6246 ret = btrfs_inc_extent_ref(trans, root,
6247 new_extent->disk_bytenr,
6248 new_extent->disk_num_bytes,
6249 leaf->start,
6250 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006251 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006252 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006253
Zheng Yan1a40e232008-09-26 10:09:34 -04006254 ret = btrfs_free_extent(trans, root,
6255 bytenr, num_bytes, leaf->start,
6256 btrfs_header_owner(leaf),
6257 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006258 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006259 BUG_ON(ret);
6260 cond_resched();
6261 }
6262 kfree(new_extent);
6263 BUG_ON(ext_index + 1 != ref->nritems);
6264 btrfs_free_leaf_ref(root, ref);
6265 return 0;
6266}
6267
Yan Zhengf82d02d2008-10-29 14:49:05 -04006268int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6269 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006270{
6271 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006272 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006273
6274 if (root->reloc_root) {
6275 reloc_root = root->reloc_root;
6276 root->reloc_root = NULL;
6277 list_add(&reloc_root->dead_list,
6278 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006279
6280 btrfs_set_root_bytenr(&reloc_root->root_item,
6281 reloc_root->node->start);
6282 btrfs_set_root_level(&root->root_item,
6283 btrfs_header_level(reloc_root->node));
6284 memset(&reloc_root->root_item.drop_progress, 0,
6285 sizeof(struct btrfs_disk_key));
6286 reloc_root->root_item.drop_level = 0;
6287
6288 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6289 &reloc_root->root_key,
6290 &reloc_root->root_item);
6291 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006292 }
6293 return 0;
6294}
6295
6296int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6297{
6298 struct btrfs_trans_handle *trans;
6299 struct btrfs_root *reloc_root;
6300 struct btrfs_root *prev_root = NULL;
6301 struct list_head dead_roots;
6302 int ret;
6303 unsigned long nr;
6304
6305 INIT_LIST_HEAD(&dead_roots);
6306 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6307
6308 while (!list_empty(&dead_roots)) {
6309 reloc_root = list_entry(dead_roots.prev,
6310 struct btrfs_root, dead_list);
6311 list_del_init(&reloc_root->dead_list);
6312
6313 BUG_ON(reloc_root->commit_root != NULL);
6314 while (1) {
6315 trans = btrfs_join_transaction(root, 1);
6316 BUG_ON(!trans);
6317
6318 mutex_lock(&root->fs_info->drop_mutex);
6319 ret = btrfs_drop_snapshot(trans, reloc_root);
6320 if (ret != -EAGAIN)
6321 break;
6322 mutex_unlock(&root->fs_info->drop_mutex);
6323
6324 nr = trans->blocks_used;
6325 ret = btrfs_end_transaction(trans, root);
6326 BUG_ON(ret);
6327 btrfs_btree_balance_dirty(root, nr);
6328 }
6329
6330 free_extent_buffer(reloc_root->node);
6331
6332 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6333 &reloc_root->root_key);
6334 BUG_ON(ret);
6335 mutex_unlock(&root->fs_info->drop_mutex);
6336
6337 nr = trans->blocks_used;
6338 ret = btrfs_end_transaction(trans, root);
6339 BUG_ON(ret);
6340 btrfs_btree_balance_dirty(root, nr);
6341
6342 kfree(prev_root);
6343 prev_root = reloc_root;
6344 }
6345 if (prev_root) {
6346 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6347 kfree(prev_root);
6348 }
6349 return 0;
6350}
6351
6352int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6353{
6354 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6355 return 0;
6356}
6357
6358int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6359{
6360 struct btrfs_root *reloc_root;
6361 struct btrfs_trans_handle *trans;
6362 struct btrfs_key location;
6363 int found;
6364 int ret;
6365
6366 mutex_lock(&root->fs_info->tree_reloc_mutex);
6367 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6368 BUG_ON(ret);
6369 found = !list_empty(&root->fs_info->dead_reloc_roots);
6370 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6371
6372 if (found) {
6373 trans = btrfs_start_transaction(root, 1);
6374 BUG_ON(!trans);
6375 ret = btrfs_commit_transaction(trans, root);
6376 BUG_ON(ret);
6377 }
6378
6379 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6380 location.offset = (u64)-1;
6381 location.type = BTRFS_ROOT_ITEM_KEY;
6382
6383 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6384 BUG_ON(!reloc_root);
6385 btrfs_orphan_cleanup(reloc_root);
6386 return 0;
6387}
6388
Chris Masond3977122009-01-05 21:25:51 -05006389static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006390 struct btrfs_root *root)
6391{
6392 struct btrfs_root *reloc_root;
6393 struct extent_buffer *eb;
6394 struct btrfs_root_item *root_item;
6395 struct btrfs_key root_key;
6396 int ret;
6397
6398 BUG_ON(!root->ref_cows);
6399 if (root->reloc_root)
6400 return 0;
6401
6402 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6403 BUG_ON(!root_item);
6404
6405 ret = btrfs_copy_root(trans, root, root->commit_root,
6406 &eb, BTRFS_TREE_RELOC_OBJECTID);
6407 BUG_ON(ret);
6408
6409 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6410 root_key.offset = root->root_key.objectid;
6411 root_key.type = BTRFS_ROOT_ITEM_KEY;
6412
6413 memcpy(root_item, &root->root_item, sizeof(root_item));
6414 btrfs_set_root_refs(root_item, 0);
6415 btrfs_set_root_bytenr(root_item, eb->start);
6416 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006417 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006418
6419 btrfs_tree_unlock(eb);
6420 free_extent_buffer(eb);
6421
6422 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6423 &root_key, root_item);
6424 BUG_ON(ret);
6425 kfree(root_item);
6426
6427 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6428 &root_key);
6429 BUG_ON(!reloc_root);
6430 reloc_root->last_trans = trans->transid;
6431 reloc_root->commit_root = NULL;
6432 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6433
6434 root->reloc_root = reloc_root;
6435 return 0;
6436}
6437
6438/*
6439 * Core function of space balance.
6440 *
6441 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006442 * counted roots. There is one reloc tree for each subvol, and all
6443 * reloc trees share same root key objectid. Reloc trees are snapshots
6444 * of the latest committed roots of subvols (root->commit_root).
6445 *
6446 * To relocate a tree block referenced by a subvol, there are two steps.
6447 * COW the block through subvol's reloc tree, then update block pointer
6448 * in the subvol to point to the new block. Since all reloc trees share
6449 * same root key objectid, doing special handing for tree blocks owned
6450 * by them is easy. Once a tree block has been COWed in one reloc tree,
6451 * we can use the resulting new block directly when the same block is
6452 * required to COW again through other reloc trees. By this way, relocated
6453 * tree blocks are shared between reloc trees, so they are also shared
6454 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006455 */
Chris Masond3977122009-01-05 21:25:51 -05006456static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006457 struct btrfs_root *root,
6458 struct btrfs_path *path,
6459 struct btrfs_key *first_key,
6460 struct btrfs_ref_path *ref_path,
6461 struct btrfs_block_group_cache *group,
6462 struct inode *reloc_inode)
6463{
6464 struct btrfs_root *reloc_root;
6465 struct extent_buffer *eb = NULL;
6466 struct btrfs_key *keys;
6467 u64 *nodes;
6468 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006469 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006470 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006471 int ret;
6472
6473 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6474 lowest_level = ref_path->owner_objectid;
6475
Yan Zhengf82d02d2008-10-29 14:49:05 -04006476 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006477 path->lowest_level = lowest_level;
6478 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6479 BUG_ON(ret < 0);
6480 path->lowest_level = 0;
6481 btrfs_release_path(root, path);
6482 return 0;
6483 }
6484
Zheng Yan1a40e232008-09-26 10:09:34 -04006485 mutex_lock(&root->fs_info->tree_reloc_mutex);
6486 ret = init_reloc_tree(trans, root);
6487 BUG_ON(ret);
6488 reloc_root = root->reloc_root;
6489
Yan Zhengf82d02d2008-10-29 14:49:05 -04006490 shared_level = ref_path->shared_level;
6491 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006492
Yan Zhengf82d02d2008-10-29 14:49:05 -04006493 keys = ref_path->node_keys;
6494 nodes = ref_path->new_nodes;
6495 memset(&keys[shared_level + 1], 0,
6496 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6497 memset(&nodes[shared_level + 1], 0,
6498 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006499
Yan Zhengf82d02d2008-10-29 14:49:05 -04006500 if (nodes[lowest_level] == 0) {
6501 path->lowest_level = lowest_level;
6502 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6503 0, 1);
6504 BUG_ON(ret);
6505 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6506 eb = path->nodes[level];
6507 if (!eb || eb == reloc_root->node)
6508 break;
6509 nodes[level] = eb->start;
6510 if (level == 0)
6511 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6512 else
6513 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6514 }
Yan Zheng2b820322008-11-17 21:11:30 -05006515 if (nodes[0] &&
6516 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006517 eb = path->nodes[0];
6518 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6519 group, reloc_inode);
6520 BUG_ON(ret);
6521 }
6522 btrfs_release_path(reloc_root, path);
6523 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006524 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006525 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006526 BUG_ON(ret);
6527 }
6528
Zheng Yan1a40e232008-09-26 10:09:34 -04006529 /*
6530 * replace tree blocks in the fs tree with tree blocks in
6531 * the reloc tree.
6532 */
6533 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6534 BUG_ON(ret < 0);
6535
6536 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006537 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6538 0, 0);
6539 BUG_ON(ret);
6540 extent_buffer_get(path->nodes[0]);
6541 eb = path->nodes[0];
6542 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006543 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6544 BUG_ON(ret);
6545 free_extent_buffer(eb);
6546 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006547
Yan Zhengf82d02d2008-10-29 14:49:05 -04006548 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006549 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006550 return 0;
6551}
6552
Chris Masond3977122009-01-05 21:25:51 -05006553static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006554 struct btrfs_root *root,
6555 struct btrfs_path *path,
6556 struct btrfs_key *first_key,
6557 struct btrfs_ref_path *ref_path)
6558{
6559 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006560
6561 ret = relocate_one_path(trans, root, path, first_key,
6562 ref_path, NULL, NULL);
6563 BUG_ON(ret);
6564
Zheng Yan1a40e232008-09-26 10:09:34 -04006565 return 0;
6566}
6567
Chris Masond3977122009-01-05 21:25:51 -05006568static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006569 struct btrfs_root *extent_root,
6570 struct btrfs_path *path,
6571 struct btrfs_key *extent_key)
6572{
6573 int ret;
6574
Zheng Yan1a40e232008-09-26 10:09:34 -04006575 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6576 if (ret)
6577 goto out;
6578 ret = btrfs_del_item(trans, extent_root, path);
6579out:
Chris Masonedbd8d42007-12-21 16:27:24 -05006580 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006581 return ret;
6582}
6583
Chris Masond3977122009-01-05 21:25:51 -05006584static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006585 struct btrfs_ref_path *ref_path)
6586{
6587 struct btrfs_key root_key;
6588
6589 root_key.objectid = ref_path->root_objectid;
6590 root_key.type = BTRFS_ROOT_ITEM_KEY;
6591 if (is_cowonly_root(ref_path->root_objectid))
6592 root_key.offset = 0;
6593 else
6594 root_key.offset = (u64)-1;
6595
6596 return btrfs_read_fs_root_no_name(fs_info, &root_key);
6597}
6598
Chris Masond3977122009-01-05 21:25:51 -05006599static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006600 struct btrfs_path *path,
6601 struct btrfs_key *extent_key,
6602 struct btrfs_block_group_cache *group,
6603 struct inode *reloc_inode, int pass)
6604{
6605 struct btrfs_trans_handle *trans;
6606 struct btrfs_root *found_root;
6607 struct btrfs_ref_path *ref_path = NULL;
6608 struct disk_extent *new_extents = NULL;
6609 int nr_extents = 0;
6610 int loops;
6611 int ret;
6612 int level;
6613 struct btrfs_key first_key;
6614 u64 prev_block = 0;
6615
Zheng Yan1a40e232008-09-26 10:09:34 -04006616
6617 trans = btrfs_start_transaction(extent_root, 1);
6618 BUG_ON(!trans);
6619
6620 if (extent_key->objectid == 0) {
6621 ret = del_extent_zero(trans, extent_root, path, extent_key);
6622 goto out;
6623 }
6624
6625 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6626 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05006627 ret = -ENOMEM;
6628 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006629 }
6630
6631 for (loops = 0; ; loops++) {
6632 if (loops == 0) {
6633 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6634 extent_key->objectid);
6635 } else {
6636 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6637 }
6638 if (ret < 0)
6639 goto out;
6640 if (ret > 0)
6641 break;
6642
6643 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6644 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6645 continue;
6646
6647 found_root = read_ref_root(extent_root->fs_info, ref_path);
6648 BUG_ON(!found_root);
6649 /*
6650 * for reference counted tree, only process reference paths
6651 * rooted at the latest committed root.
6652 */
6653 if (found_root->ref_cows &&
6654 ref_path->root_generation != found_root->root_key.offset)
6655 continue;
6656
6657 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6658 if (pass == 0) {
6659 /*
6660 * copy data extents to new locations
6661 */
6662 u64 group_start = group->key.objectid;
6663 ret = relocate_data_extent(reloc_inode,
6664 extent_key,
6665 group_start);
6666 if (ret < 0)
6667 goto out;
6668 break;
6669 }
6670 level = 0;
6671 } else {
6672 level = ref_path->owner_objectid;
6673 }
6674
6675 if (prev_block != ref_path->nodes[level]) {
6676 struct extent_buffer *eb;
6677 u64 block_start = ref_path->nodes[level];
6678 u64 block_size = btrfs_level_size(found_root, level);
6679
6680 eb = read_tree_block(found_root, block_start,
6681 block_size, 0);
6682 btrfs_tree_lock(eb);
6683 BUG_ON(level != btrfs_header_level(eb));
6684
6685 if (level == 0)
6686 btrfs_item_key_to_cpu(eb, &first_key, 0);
6687 else
6688 btrfs_node_key_to_cpu(eb, &first_key, 0);
6689
6690 btrfs_tree_unlock(eb);
6691 free_extent_buffer(eb);
6692 prev_block = block_start;
6693 }
6694
Yan Zheng24562422009-02-12 14:14:53 -05006695 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006696 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05006697 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05006698 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6699 /*
6700 * try to update data extent references while
6701 * keeping metadata shared between snapshots.
6702 */
6703 if (pass == 1) {
6704 ret = relocate_one_path(trans, found_root,
6705 path, &first_key, ref_path,
6706 group, reloc_inode);
6707 if (ret < 0)
6708 goto out;
6709 continue;
6710 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006711 /*
6712 * use fallback method to process the remaining
6713 * references.
6714 */
6715 if (!new_extents) {
6716 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04006717 new_extents = kmalloc(sizeof(*new_extents),
6718 GFP_NOFS);
6719 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006720 ret = get_new_locations(reloc_inode,
6721 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04006722 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04006723 &new_extents,
6724 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006725 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04006726 goto out;
6727 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006728 ret = replace_one_extent(trans, found_root,
6729 path, extent_key,
6730 &first_key, ref_path,
6731 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05006732 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006733 ret = relocate_tree_block(trans, found_root, path,
6734 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006735 }
6736 if (ret < 0)
6737 goto out;
6738 }
6739 ret = 0;
6740out:
6741 btrfs_end_transaction(trans, extent_root);
6742 kfree(new_extents);
6743 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006744 return ret;
6745}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006746#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05006747
Chris Masonec44a352008-04-28 15:29:52 -04006748static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6749{
6750 u64 num_devices;
6751 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6752 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6753
Yan Zheng2b820322008-11-17 21:11:30 -05006754 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04006755 if (num_devices == 1) {
6756 stripped |= BTRFS_BLOCK_GROUP_DUP;
6757 stripped = flags & ~stripped;
6758
6759 /* turn raid0 into single device chunks */
6760 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6761 return stripped;
6762
6763 /* turn mirroring into duplication */
6764 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6765 BTRFS_BLOCK_GROUP_RAID10))
6766 return stripped | BTRFS_BLOCK_GROUP_DUP;
6767 return flags;
6768 } else {
6769 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006770 if (flags & stripped)
6771 return flags;
6772
6773 stripped |= BTRFS_BLOCK_GROUP_DUP;
6774 stripped = flags & ~stripped;
6775
6776 /* switch duplicated blocks with raid1 */
6777 if (flags & BTRFS_BLOCK_GROUP_DUP)
6778 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6779
6780 /* turn single device chunks into raid0 */
6781 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6782 }
6783 return flags;
6784}
6785
Christoph Hellwigb2950862008-12-02 09:54:17 -05006786static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04006787 struct btrfs_block_group_cache *shrink_block_group,
6788 int force)
6789{
6790 struct btrfs_trans_handle *trans;
6791 u64 new_alloc_flags;
6792 u64 calc;
6793
Chris Masonc286ac42008-07-22 23:06:41 -04006794 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006795 if (btrfs_block_group_used(&shrink_block_group->item) +
6796 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04006797 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006798
Chris Mason0ef3e662008-05-24 14:04:53 -04006799 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006800 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04006801
Chris Mason0ef3e662008-05-24 14:04:53 -04006802 new_alloc_flags = update_block_group_flags(root,
6803 shrink_block_group->flags);
6804 if (new_alloc_flags != shrink_block_group->flags) {
6805 calc =
6806 btrfs_block_group_used(&shrink_block_group->item);
6807 } else {
6808 calc = shrink_block_group->key.offset;
6809 }
Chris Masonc286ac42008-07-22 23:06:41 -04006810 spin_unlock(&shrink_block_group->lock);
6811
Chris Mason0ef3e662008-05-24 14:04:53 -04006812 do_chunk_alloc(trans, root->fs_info->extent_root,
6813 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04006814
Chris Mason0ef3e662008-05-24 14:04:53 -04006815 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04006816 } else
6817 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04006818 return 0;
6819}
6820
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006821
6822int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
6823 struct btrfs_block_group_cache *group)
6824
6825{
6826 __alloc_chunk_for_shrink(root, group, 1);
6827 set_block_group_readonly(group);
6828 return 0;
6829}
6830
6831#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006832static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
6833 struct btrfs_root *root,
6834 u64 objectid, u64 size)
6835{
6836 struct btrfs_path *path;
6837 struct btrfs_inode_item *item;
6838 struct extent_buffer *leaf;
6839 int ret;
6840
6841 path = btrfs_alloc_path();
6842 if (!path)
6843 return -ENOMEM;
6844
Chris Masonb9473432009-03-13 11:00:37 -04006845 path->leave_spinning = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006846 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
6847 if (ret)
6848 goto out;
6849
6850 leaf = path->nodes[0];
6851 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
6852 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
6853 btrfs_set_inode_generation(leaf, item, 1);
6854 btrfs_set_inode_size(leaf, item, size);
6855 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05006856 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006857 btrfs_mark_buffer_dirty(leaf);
6858 btrfs_release_path(root, path);
6859out:
6860 btrfs_free_path(path);
6861 return ret;
6862}
6863
Chris Masond3977122009-01-05 21:25:51 -05006864static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006865 struct btrfs_block_group_cache *group)
6866{
6867 struct inode *inode = NULL;
6868 struct btrfs_trans_handle *trans;
6869 struct btrfs_root *root;
6870 struct btrfs_key root_key;
6871 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
6872 int err = 0;
6873
6874 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6875 root_key.type = BTRFS_ROOT_ITEM_KEY;
6876 root_key.offset = (u64)-1;
6877 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
6878 if (IS_ERR(root))
6879 return ERR_CAST(root);
6880
6881 trans = btrfs_start_transaction(root, 1);
6882 BUG_ON(!trans);
6883
6884 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
6885 if (err)
6886 goto out;
6887
6888 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
6889 BUG_ON(err);
6890
6891 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04006892 group->key.offset, 0, group->key.offset,
6893 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006894 BUG_ON(err);
6895
6896 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
6897 if (inode->i_state & I_NEW) {
6898 BTRFS_I(inode)->root = root;
6899 BTRFS_I(inode)->location.objectid = objectid;
6900 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
6901 BTRFS_I(inode)->location.offset = 0;
6902 btrfs_read_locked_inode(inode);
6903 unlock_new_inode(inode);
6904 BUG_ON(is_bad_inode(inode));
6905 } else {
6906 BUG_ON(1);
6907 }
Yan Zheng17d217f2008-12-12 10:03:38 -05006908 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006909
6910 err = btrfs_orphan_add(trans, inode);
6911out:
6912 btrfs_end_transaction(trans, root);
6913 if (err) {
6914 if (inode)
6915 iput(inode);
6916 inode = ERR_PTR(err);
6917 }
6918 return inode;
6919}
6920
Yan Zheng17d217f2008-12-12 10:03:38 -05006921int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
6922{
6923
6924 struct btrfs_ordered_sum *sums;
6925 struct btrfs_sector_sum *sector_sum;
6926 struct btrfs_ordered_extent *ordered;
6927 struct btrfs_root *root = BTRFS_I(inode)->root;
6928 struct list_head list;
6929 size_t offset;
6930 int ret;
6931 u64 disk_bytenr;
6932
6933 INIT_LIST_HEAD(&list);
6934
6935 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
6936 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
6937
6938 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05006939 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05006940 disk_bytenr + len - 1, &list);
6941
6942 while (!list_empty(&list)) {
6943 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
6944 list_del_init(&sums->list);
6945
6946 sector_sum = sums->sums;
6947 sums->bytenr = ordered->start;
6948
6949 offset = 0;
6950 while (offset < sums->len) {
6951 sector_sum->bytenr += ordered->start - disk_bytenr;
6952 sector_sum++;
6953 offset += root->sectorsize;
6954 }
6955
6956 btrfs_add_ordered_sum(inode, ordered, sums);
6957 }
6958 btrfs_put_ordered_extent(ordered);
6959 return 0;
6960}
6961
Zheng Yan1a40e232008-09-26 10:09:34 -04006962int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05006963{
6964 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05006965 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04006966 struct btrfs_fs_info *info = root->fs_info;
6967 struct extent_buffer *leaf;
6968 struct inode *reloc_inode;
6969 struct btrfs_block_group_cache *block_group;
6970 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04006971 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05006972 u64 cur_byte;
6973 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05006974 u32 nritems;
6975 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04006976 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04006977 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006978
Chris Masonedbd8d42007-12-21 16:27:24 -05006979 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04006980
6981 block_group = btrfs_lookup_block_group(info, group_start);
6982 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006983
Chris Masond3977122009-01-05 21:25:51 -05006984 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04006985 (unsigned long long)block_group->key.objectid,
6986 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04006987
Zheng Yan1a40e232008-09-26 10:09:34 -04006988 path = btrfs_alloc_path();
6989 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04006990
Zheng Yan1a40e232008-09-26 10:09:34 -04006991 reloc_inode = create_reloc_inode(info, block_group);
6992 BUG_ON(IS_ERR(reloc_inode));
6993
Zheng Yan1a40e232008-09-26 10:09:34 -04006994 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05006995 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006996
Zheng Yan1a40e232008-09-26 10:09:34 -04006997 btrfs_start_delalloc_inodes(info->tree_root);
6998 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04006999again:
Yan Zhengd899e052008-10-30 14:25:28 -04007000 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007001 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04007002 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007003 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05007004 key.offset = 0;
7005 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05007006 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05007007
Zheng Yan1a40e232008-09-26 10:09:34 -04007008 trans = btrfs_start_transaction(info->tree_root, 1);
7009 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04007010
Zheng Yan1a40e232008-09-26 10:09:34 -04007011 mutex_lock(&root->fs_info->cleaner_mutex);
7012 btrfs_clean_old_snapshots(info->tree_root);
7013 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
7014 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04007015
Chris Mason56bec292009-03-13 10:10:06 -04007016 trans = btrfs_start_transaction(info->tree_root, 1);
7017 btrfs_commit_transaction(trans, info->tree_root);
7018
Chris Masond3977122009-01-05 21:25:51 -05007019 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05007020 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7021 if (ret < 0)
7022 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04007023next:
Chris Masonedbd8d42007-12-21 16:27:24 -05007024 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05007025 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05007026 if (path->slots[0] >= nritems) {
7027 ret = btrfs_next_leaf(root, path);
7028 if (ret < 0)
7029 goto out;
7030 if (ret == 1) {
7031 ret = 0;
7032 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05007033 }
Yan73e48b22008-01-03 14:14:39 -05007034 leaf = path->nodes[0];
7035 nritems = btrfs_header_nritems(leaf);
7036 }
7037
Zheng Yan1a40e232008-09-26 10:09:34 -04007038 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05007039
Zheng Yan1a40e232008-09-26 10:09:34 -04007040 if (key.objectid >= block_group->key.objectid +
7041 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04007042 break;
7043
Chris Mason725c8462008-01-04 16:47:16 -05007044 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05007045 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007046 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05007047 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007048 continue;
Chris Mason725c8462008-01-04 16:47:16 -05007049 }
7050 progress = 1;
7051
Zheng Yan1a40e232008-09-26 10:09:34 -04007052 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
7053 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05007054 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05007055 goto next;
7056 }
Yan73e48b22008-01-03 14:14:39 -05007057
Chris Masonedbd8d42007-12-21 16:27:24 -05007058 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04007059 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05007060 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007061
7062 __alloc_chunk_for_shrink(root, block_group, 0);
7063 ret = relocate_one_extent(root, path, &key, block_group,
7064 reloc_inode, pass);
7065 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04007066 if (ret > 0)
7067 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04007068
7069 key.objectid = cur_byte;
7070 key.type = 0;
7071 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007072 }
7073
7074 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007075
7076 if (pass == 0) {
7077 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
7078 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04007079 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007080
7081 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05007082 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04007083 (unsigned long long)total_found, pass);
7084 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04007085 if (total_found == skipped && pass > 2) {
7086 iput(reloc_inode);
7087 reloc_inode = create_reloc_inode(info, block_group);
7088 pass = 0;
7089 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007090 goto again;
7091 }
7092
Zheng Yan1a40e232008-09-26 10:09:34 -04007093 /* delete reloc_inode */
7094 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04007095
Zheng Yan1a40e232008-09-26 10:09:34 -04007096 /* unpin extents in this range */
7097 trans = btrfs_start_transaction(info->tree_root, 1);
7098 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04007099
Zheng Yan1a40e232008-09-26 10:09:34 -04007100 spin_lock(&block_group->lock);
7101 WARN_ON(block_group->pinned > 0);
7102 WARN_ON(block_group->reserved > 0);
7103 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
7104 spin_unlock(&block_group->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007105 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007106 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007107out:
Zheng Yan1a40e232008-09-26 10:09:34 -04007108 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007109 return ret;
7110}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007111#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007112
Christoph Hellwigb2950862008-12-02 09:54:17 -05007113static int find_first_block_group(struct btrfs_root *root,
7114 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007115{
Chris Mason925baed2008-06-25 16:01:30 -04007116 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007117 struct btrfs_key found_key;
7118 struct extent_buffer *leaf;
7119 int slot;
7120
7121 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7122 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007123 goto out;
7124
Chris Masond3977122009-01-05 21:25:51 -05007125 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007126 slot = path->slots[0];
7127 leaf = path->nodes[0];
7128 if (slot >= btrfs_header_nritems(leaf)) {
7129 ret = btrfs_next_leaf(root, path);
7130 if (ret == 0)
7131 continue;
7132 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007133 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007134 break;
7135 }
7136 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7137
7138 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007139 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7140 ret = 0;
7141 goto out;
7142 }
Chris Mason0b86a832008-03-24 15:01:56 -04007143 path->slots[0]++;
7144 }
7145 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04007146out:
Chris Mason0b86a832008-03-24 15:01:56 -04007147 return ret;
7148}
7149
Zheng Yan1a40e232008-09-26 10:09:34 -04007150int btrfs_free_block_groups(struct btrfs_fs_info *info)
7151{
7152 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007153 struct btrfs_space_info *space_info;
Zheng Yan1a40e232008-09-26 10:09:34 -04007154 struct rb_node *n;
7155
Zheng Yan1a40e232008-09-26 10:09:34 -04007156 spin_lock(&info->block_group_cache_lock);
7157 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7158 block_group = rb_entry(n, struct btrfs_block_group_cache,
7159 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007160 rb_erase(&block_group->cache_node,
7161 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007162 spin_unlock(&info->block_group_cache_lock);
7163
Josef Bacik80eb2342008-10-29 14:49:05 -04007164 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007165 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007166 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007167
Josef Bacik817d52f2009-07-13 21:29:25 -04007168 if (block_group->cached == BTRFS_CACHE_STARTED)
7169 wait_event(block_group->caching_q,
7170 block_group_cache_done(block_group));
7171
7172 btrfs_remove_free_space_cache(block_group);
7173
Yan Zhengd2fb3432008-12-11 16:30:39 -05007174 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04007175 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007176
7177 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007178 }
7179 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007180
7181 /* now that all the block groups are freed, go through and
7182 * free all the space_info structs. This is only called during
7183 * the final stages of unmount, and so we know nobody is
7184 * using them. We call synchronize_rcu() once before we start,
7185 * just to be on the safe side.
7186 */
7187 synchronize_rcu();
7188
7189 while(!list_empty(&info->space_info)) {
7190 space_info = list_entry(info->space_info.next,
7191 struct btrfs_space_info,
7192 list);
7193
7194 list_del(&space_info->list);
7195 kfree(space_info);
7196 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007197 return 0;
7198}
7199
Chris Mason9078a3e2007-04-26 16:46:15 -04007200int btrfs_read_block_groups(struct btrfs_root *root)
7201{
7202 struct btrfs_path *path;
7203 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007204 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007205 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007206 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007207 struct btrfs_key key;
7208 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007209 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007210
Chris Masonbe744172007-05-06 10:15:01 -04007211 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007212 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007213 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007214 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007215 path = btrfs_alloc_path();
7216 if (!path)
7217 return -ENOMEM;
7218
Chris Masond3977122009-01-05 21:25:51 -05007219 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007220 ret = find_first_block_group(root, path, &key);
7221 if (ret > 0) {
7222 ret = 0;
7223 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007224 }
Chris Mason0b86a832008-03-24 15:01:56 -04007225 if (ret != 0)
7226 goto error;
7227
Chris Mason5f39d392007-10-15 16:14:19 -04007228 leaf = path->nodes[0];
7229 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007230 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007231 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007232 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04007233 break;
7234 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007235
Yan Zhengd2fb3432008-12-11 16:30:39 -05007236 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007237 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007238 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007239 cache->fs_info = info;
7240 init_waitqueue_head(&cache->caching_q);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007241 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007242 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007243
7244 /*
7245 * we only want to have 32k of ram per block group for keeping
7246 * track of free space, and if we pass 1/2 of that we want to
7247 * start converting things over to using bitmaps
7248 */
7249 cache->extents_thresh = ((1024 * 32) / 2) /
7250 sizeof(struct btrfs_free_space);
7251
Chris Mason5f39d392007-10-15 16:14:19 -04007252 read_extent_buffer(leaf, &cache->item,
7253 btrfs_item_ptr_offset(leaf, path->slots[0]),
7254 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007255 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007256
Chris Mason9078a3e2007-04-26 16:46:15 -04007257 key.objectid = found_key.objectid + found_key.offset;
7258 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007259 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007260 cache->sectorsize = root->sectorsize;
7261
7262 remove_sb_from_cache(root, cache);
7263
7264 /*
7265 * check for two cases, either we are full, and therefore
7266 * don't need to bother with the caching work since we won't
7267 * find any space, or we are empty, and we can just add all
7268 * the space in and be done with it. This saves us _alot_ of
7269 * time, particularly in the full case.
7270 */
7271 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
7272 cache->cached = BTRFS_CACHE_FINISHED;
7273 } else if (btrfs_block_group_used(&cache->item) == 0) {
7274 cache->cached = BTRFS_CACHE_FINISHED;
7275 add_new_free_space(cache, root->fs_info,
7276 found_key.objectid,
7277 found_key.objectid +
7278 found_key.offset);
7279 }
Chris Mason96b51792007-10-15 16:15:19 -04007280
Chris Mason6324fbf2008-03-24 15:01:59 -04007281 ret = update_space_info(info, cache->flags, found_key.offset,
7282 btrfs_block_group_used(&cache->item),
7283 &space_info);
7284 BUG_ON(ret);
7285 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04007286 down_write(&space_info->groups_sem);
7287 list_add_tail(&cache->list, &space_info->block_groups);
7288 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007289
Josef Bacik0f9dd462008-09-23 13:14:11 -04007290 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7291 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007292
7293 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007294 if (btrfs_chunk_readonly(root, cache->key.objectid))
7295 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007296 }
Chris Mason0b86a832008-03-24 15:01:56 -04007297 ret = 0;
7298error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007299 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007300 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007301}
Chris Mason6324fbf2008-03-24 15:01:59 -04007302
7303int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7304 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007305 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007306 u64 size)
7307{
7308 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007309 struct btrfs_root *extent_root;
7310 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007311
7312 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007313
Chris Mason12fcfd22009-03-24 10:24:20 -04007314 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007315
Chris Mason8f18cf12008-04-25 16:53:30 -04007316 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007317 if (!cache)
7318 return -ENOMEM;
7319
Chris Masone17cade2008-04-15 15:41:47 -04007320 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007321 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007322 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007323 cache->sectorsize = root->sectorsize;
7324
7325 /*
7326 * we only want to have 32k of ram per block group for keeping track
7327 * of free space, and if we pass 1/2 of that we want to start
7328 * converting things over to using bitmaps
7329 */
7330 cache->extents_thresh = ((1024 * 32) / 2) /
7331 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007332 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007333 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007334 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007335 init_waitqueue_head(&cache->caching_q);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007336 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007337 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007338
Chris Mason6324fbf2008-03-24 15:01:59 -04007339 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007340 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7341 cache->flags = type;
7342 btrfs_set_block_group_flags(&cache->item, type);
7343
Josef Bacik817d52f2009-07-13 21:29:25 -04007344 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik96303082009-07-13 21:29:25 -04007345 remove_sb_from_cache(root, cache);
7346
Josef Bacik817d52f2009-07-13 21:29:25 -04007347 add_new_free_space(cache, root->fs_info, chunk_offset,
7348 chunk_offset + size);
7349
Chris Mason6324fbf2008-03-24 15:01:59 -04007350 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7351 &cache->space_info);
7352 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04007353 down_write(&cache->space_info->groups_sem);
7354 list_add_tail(&cache->list, &cache->space_info->block_groups);
7355 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007356
Josef Bacik0f9dd462008-09-23 13:14:11 -04007357 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7358 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007359
Chris Mason6324fbf2008-03-24 15:01:59 -04007360 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7361 sizeof(cache->item));
7362 BUG_ON(ret);
7363
Chris Masond18a2c42008-04-04 15:40:00 -04007364 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007365
Chris Mason6324fbf2008-03-24 15:01:59 -04007366 return 0;
7367}
Zheng Yan1a40e232008-09-26 10:09:34 -04007368
7369int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7370 struct btrfs_root *root, u64 group_start)
7371{
7372 struct btrfs_path *path;
7373 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007374 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007375 struct btrfs_key key;
7376 int ret;
7377
Zheng Yan1a40e232008-09-26 10:09:34 -04007378 root = root->fs_info->extent_root;
7379
7380 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7381 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007382 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007383
7384 memcpy(&key, &block_group->key, sizeof(key));
7385
Chris Mason44fb5512009-06-04 15:34:51 -04007386 /* make sure this block group isn't part of an allocation cluster */
7387 cluster = &root->fs_info->data_alloc_cluster;
7388 spin_lock(&cluster->refill_lock);
7389 btrfs_return_cluster_to_free_space(block_group, cluster);
7390 spin_unlock(&cluster->refill_lock);
7391
7392 /*
7393 * make sure this block group isn't part of a metadata
7394 * allocation cluster
7395 */
7396 cluster = &root->fs_info->meta_alloc_cluster;
7397 spin_lock(&cluster->refill_lock);
7398 btrfs_return_cluster_to_free_space(block_group, cluster);
7399 spin_unlock(&cluster->refill_lock);
7400
Zheng Yan1a40e232008-09-26 10:09:34 -04007401 path = btrfs_alloc_path();
7402 BUG_ON(!path);
7403
Yan Zheng3dfdb932009-01-21 10:49:16 -05007404 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007405 rb_erase(&block_group->cache_node,
7406 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007407 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007408
Josef Bacik80eb2342008-10-29 14:49:05 -04007409 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007410 /*
7411 * we must use list_del_init so people can check to see if they
7412 * are still on the list after taking the semaphore
7413 */
7414 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007415 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007416
Josef Bacik817d52f2009-07-13 21:29:25 -04007417 if (block_group->cached == BTRFS_CACHE_STARTED)
7418 wait_event(block_group->caching_q,
7419 block_group_cache_done(block_group));
7420
7421 btrfs_remove_free_space_cache(block_group);
7422
Yan Zhengc146afa2008-11-12 14:34:12 -05007423 spin_lock(&block_group->space_info->lock);
7424 block_group->space_info->total_bytes -= block_group->key.offset;
7425 block_group->space_info->bytes_readonly -= block_group->key.offset;
7426 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007427
7428 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007429
Chris Masonfa9c0d792009-04-03 09:47:43 -04007430 btrfs_put_block_group(block_group);
7431 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007432
7433 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7434 if (ret > 0)
7435 ret = -EIO;
7436 if (ret < 0)
7437 goto out;
7438
7439 ret = btrfs_del_item(trans, root, path);
7440out:
7441 btrfs_free_path(path);
7442 return ret;
7443}