blob: 9a325e465ad9be7c850a56719a1e821b58697489 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050027#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040031#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040033#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040034#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Josef Bacikf3465ca2008-11-12 14:19:50 -050036static int update_block_group(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040038 u64 bytenr, u64 num_bytes, int alloc);
39static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve, int sinfo);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040041static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root,
43 u64 bytenr, u64 num_bytes, u64 parent,
44 u64 root_objectid, u64 owner_objectid,
45 u64 owner_offset, int refs_to_drop,
46 struct btrfs_delayed_extent_op *extra_op);
47static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
48 struct extent_buffer *leaf,
49 struct btrfs_extent_item *ei);
50static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root,
52 u64 parent, u64 root_objectid,
53 u64 flags, u64 owner, u64 offset,
54 struct btrfs_key *ins, int ref_mod);
55static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 parent, u64 root_objectid,
58 u64 flags, struct btrfs_disk_key *key,
59 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050060static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61 struct btrfs_root *extent_root, u64 alloc_bytes,
62 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040063static int find_next_key(struct btrfs_path *path, int level,
64 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040065static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
66 int dump_block_groups);
Josef Bacik6a632092009-02-20 11:00:09 -050067
Josef Bacik817d52f2009-07-13 21:29:25 -040068static noinline int
69block_group_cache_done(struct btrfs_block_group_cache *cache)
70{
71 smp_mb();
72 return cache->cached == BTRFS_CACHE_FINISHED;
73}
74
Josef Bacik0f9dd462008-09-23 13:14:11 -040075static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
76{
77 return (cache->flags & bits) == bits;
78}
79
Josef Bacik11dfe352009-11-13 20:12:59 +000080void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
81{
82 atomic_inc(&cache->count);
83}
84
85void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
86{
Yan, Zhengf0486c62010-05-16 10:46:25 -040087 if (atomic_dec_and_test(&cache->count)) {
88 WARN_ON(cache->pinned > 0);
89 WARN_ON(cache->reserved > 0);
90 WARN_ON(cache->reserved_pinned > 0);
Josef Bacik11dfe352009-11-13 20:12:59 +000091 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -040092 }
Josef Bacik11dfe352009-11-13 20:12:59 +000093}
94
Josef Bacik0f9dd462008-09-23 13:14:11 -040095/*
96 * this adds the block group to the fs_info rb tree for the block group
97 * cache
98 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050099static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400100 struct btrfs_block_group_cache *block_group)
101{
102 struct rb_node **p;
103 struct rb_node *parent = NULL;
104 struct btrfs_block_group_cache *cache;
105
106 spin_lock(&info->block_group_cache_lock);
107 p = &info->block_group_cache_tree.rb_node;
108
109 while (*p) {
110 parent = *p;
111 cache = rb_entry(parent, struct btrfs_block_group_cache,
112 cache_node);
113 if (block_group->key.objectid < cache->key.objectid) {
114 p = &(*p)->rb_left;
115 } else if (block_group->key.objectid > cache->key.objectid) {
116 p = &(*p)->rb_right;
117 } else {
118 spin_unlock(&info->block_group_cache_lock);
119 return -EEXIST;
120 }
121 }
122
123 rb_link_node(&block_group->cache_node, parent, p);
124 rb_insert_color(&block_group->cache_node,
125 &info->block_group_cache_tree);
126 spin_unlock(&info->block_group_cache_lock);
127
128 return 0;
129}
130
131/*
132 * This will return the block group at or after bytenr if contains is 0, else
133 * it will return the block group that contains the bytenr
134 */
135static struct btrfs_block_group_cache *
136block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
137 int contains)
138{
139 struct btrfs_block_group_cache *cache, *ret = NULL;
140 struct rb_node *n;
141 u64 end, start;
142
143 spin_lock(&info->block_group_cache_lock);
144 n = info->block_group_cache_tree.rb_node;
145
146 while (n) {
147 cache = rb_entry(n, struct btrfs_block_group_cache,
148 cache_node);
149 end = cache->key.objectid + cache->key.offset - 1;
150 start = cache->key.objectid;
151
152 if (bytenr < start) {
153 if (!contains && (!ret || start < ret->key.objectid))
154 ret = cache;
155 n = n->rb_left;
156 } else if (bytenr > start) {
157 if (contains && bytenr <= end) {
158 ret = cache;
159 break;
160 }
161 n = n->rb_right;
162 } else {
163 ret = cache;
164 break;
165 }
166 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500167 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000168 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400169 spin_unlock(&info->block_group_cache_lock);
170
171 return ret;
172}
173
Yan Zheng11833d62009-09-11 16:11:19 -0400174static int add_excluded_extent(struct btrfs_root *root,
175 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400176{
Yan Zheng11833d62009-09-11 16:11:19 -0400177 u64 end = start + num_bytes - 1;
178 set_extent_bits(&root->fs_info->freed_extents[0],
179 start, end, EXTENT_UPTODATE, GFP_NOFS);
180 set_extent_bits(&root->fs_info->freed_extents[1],
181 start, end, EXTENT_UPTODATE, GFP_NOFS);
182 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400183}
184
Yan Zheng11833d62009-09-11 16:11:19 -0400185static void free_excluded_extents(struct btrfs_root *root,
186 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400187{
Yan Zheng11833d62009-09-11 16:11:19 -0400188 u64 start, end;
189
190 start = cache->key.objectid;
191 end = start + cache->key.offset - 1;
192
193 clear_extent_bits(&root->fs_info->freed_extents[0],
194 start, end, EXTENT_UPTODATE, GFP_NOFS);
195 clear_extent_bits(&root->fs_info->freed_extents[1],
196 start, end, EXTENT_UPTODATE, GFP_NOFS);
197}
198
199static int exclude_super_stripes(struct btrfs_root *root,
200 struct btrfs_block_group_cache *cache)
201{
Josef Bacik817d52f2009-07-13 21:29:25 -0400202 u64 bytenr;
203 u64 *logical;
204 int stripe_len;
205 int i, nr, ret;
206
Yan, Zheng06b23312009-11-26 09:31:11 +0000207 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
208 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
209 cache->bytes_super += stripe_len;
210 ret = add_excluded_extent(root, cache->key.objectid,
211 stripe_len);
212 BUG_ON(ret);
213 }
214
Josef Bacik817d52f2009-07-13 21:29:25 -0400215 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
216 bytenr = btrfs_sb_offset(i);
217 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
218 cache->key.objectid, bytenr,
219 0, &logical, &nr, &stripe_len);
220 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400221
Josef Bacik817d52f2009-07-13 21:29:25 -0400222 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400223 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400224 ret = add_excluded_extent(root, logical[nr],
225 stripe_len);
226 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400227 }
Yan Zheng11833d62009-09-11 16:11:19 -0400228
Josef Bacik817d52f2009-07-13 21:29:25 -0400229 kfree(logical);
230 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400231 return 0;
232}
233
Yan Zheng11833d62009-09-11 16:11:19 -0400234static struct btrfs_caching_control *
235get_caching_control(struct btrfs_block_group_cache *cache)
236{
237 struct btrfs_caching_control *ctl;
238
239 spin_lock(&cache->lock);
240 if (cache->cached != BTRFS_CACHE_STARTED) {
241 spin_unlock(&cache->lock);
242 return NULL;
243 }
244
245 ctl = cache->caching_ctl;
246 atomic_inc(&ctl->count);
247 spin_unlock(&cache->lock);
248 return ctl;
249}
250
251static void put_caching_control(struct btrfs_caching_control *ctl)
252{
253 if (atomic_dec_and_test(&ctl->count))
254 kfree(ctl);
255}
256
Josef Bacik0f9dd462008-09-23 13:14:11 -0400257/*
258 * this is only called by cache_block_group, since we could have freed extents
259 * we need to check the pinned_extents for any extents that can't be used yet
260 * since their free space will be released as soon as the transaction commits.
261 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400262static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263 struct btrfs_fs_info *info, u64 start, u64 end)
264{
Josef Bacik817d52f2009-07-13 21:29:25 -0400265 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400266 int ret;
267
268 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400269 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400270 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400271 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400272 if (ret)
273 break;
274
Yan, Zheng06b23312009-11-26 09:31:11 +0000275 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400276 start = extent_end + 1;
277 } else if (extent_start > start && extent_start < end) {
278 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400279 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500280 ret = btrfs_add_free_space(block_group, start,
281 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400282 BUG_ON(ret);
283 start = extent_end + 1;
284 } else {
285 break;
286 }
287 }
288
289 if (start < end) {
290 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400291 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500292 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400293 BUG_ON(ret);
294 }
295
Josef Bacik817d52f2009-07-13 21:29:25 -0400296 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400297}
298
Josef Bacik817d52f2009-07-13 21:29:25 -0400299static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400300{
Josef Bacik817d52f2009-07-13 21:29:25 -0400301 struct btrfs_block_group_cache *block_group = data;
302 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400303 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
304 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400305 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400306 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400307 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400308 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400309 u64 last = 0;
310 u32 nritems;
311 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400312
Chris Masone37c9e62007-05-09 20:13:14 -0400313 path = btrfs_alloc_path();
314 if (!path)
315 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400316
Yan Zheng11833d62009-09-11 16:11:19 -0400317 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400318 spin_lock(&block_group->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400319 block_group->space_info->bytes_readonly += block_group->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -0400320 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400321
Josef Bacik817d52f2009-07-13 21:29:25 -0400322 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400323
Chris Mason5cd57b22008-06-25 16:01:30 -0400324 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400325 * We don't want to deadlock with somebody trying to allocate a new
326 * extent for the extent root while also trying to search the extent
327 * root to add free space. So we skip locking and search the commit
328 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400329 */
330 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 path->search_commit_root = 1;
332 path->reada = 2;
333
Yan Zhenge4404d62008-12-12 10:03:26 -0500334 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400335 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400336 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400337again:
Yan Zheng11833d62009-09-11 16:11:19 -0400338 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400339 /* need to make sure the commit_root doesn't disappear */
340 down_read(&fs_info->extent_commit_sem);
341
Yan Zheng11833d62009-09-11 16:11:19 -0400342 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400343 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400344 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500345
Yan Zheng11833d62009-09-11 16:11:19 -0400346 leaf = path->nodes[0];
347 nritems = btrfs_header_nritems(leaf);
348
Chris Masond3977122009-01-05 21:25:51 -0500349 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400350 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400351 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400352 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400353 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400354 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400355
Yan Zheng11833d62009-09-11 16:11:19 -0400356 if (path->slots[0] < nritems) {
357 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
358 } else {
359 ret = find_next_key(path, 0, &key);
360 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400361 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400362
Yan Zheng11833d62009-09-11 16:11:19 -0400363 caching_ctl->progress = last;
364 btrfs_release_path(extent_root, path);
365 up_read(&fs_info->extent_commit_sem);
366 mutex_unlock(&caching_ctl->mutex);
367 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400368 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400369 else
370 cond_resched();
371 goto again;
372 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400373
Yan Zheng11833d62009-09-11 16:11:19 -0400374 if (key.objectid < block_group->key.objectid) {
375 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400376 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400377 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400378
Chris Masone37c9e62007-05-09 20:13:14 -0400379 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400380 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400381 break;
Yan7d7d6062007-09-14 16:15:28 -0400382
Yan Zheng11833d62009-09-11 16:11:19 -0400383 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400384 total_found += add_new_free_space(block_group,
385 fs_info, last,
386 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400387 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400388
Yan Zheng11833d62009-09-11 16:11:19 -0400389 if (total_found > (1024 * 1024 * 2)) {
390 total_found = 0;
391 wake_up(&caching_ctl->wait);
392 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400393 }
Chris Masone37c9e62007-05-09 20:13:14 -0400394 path->slots[0]++;
395 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400396 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400397
398 total_found += add_new_free_space(block_group, fs_info, last,
399 block_group->key.objectid +
400 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400401 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400402
403 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400404 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400405 block_group->cached = BTRFS_CACHE_FINISHED;
406 spin_unlock(&block_group->lock);
407
Chris Mason54aa1f42007-06-22 14:16:25 -0400408err:
Chris Masone37c9e62007-05-09 20:13:14 -0400409 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400410 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400411
Yan Zheng11833d62009-09-11 16:11:19 -0400412 free_excluded_extents(extent_root, block_group);
413
414 mutex_unlock(&caching_ctl->mutex);
415 wake_up(&caching_ctl->wait);
416
417 put_caching_control(caching_ctl);
418 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000419 btrfs_put_block_group(block_group);
420
Josef Bacik817d52f2009-07-13 21:29:25 -0400421 return 0;
422}
423
Josef Bacik9d66e232010-08-25 16:54:15 -0400424static int cache_block_group(struct btrfs_block_group_cache *cache,
425 struct btrfs_trans_handle *trans,
426 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400427{
Yan Zheng11833d62009-09-11 16:11:19 -0400428 struct btrfs_fs_info *fs_info = cache->fs_info;
429 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400430 struct task_struct *tsk;
431 int ret = 0;
432
Yan Zheng11833d62009-09-11 16:11:19 -0400433 smp_mb();
434 if (cache->cached != BTRFS_CACHE_NO)
435 return 0;
436
Josef Bacik9d66e232010-08-25 16:54:15 -0400437 /*
438 * We can't do the read from on-disk cache during a commit since we need
439 * to have the normal tree locking.
440 */
441 if (!trans->transaction->in_commit) {
442 spin_lock(&cache->lock);
443 if (cache->cached != BTRFS_CACHE_NO) {
444 spin_unlock(&cache->lock);
445 return 0;
446 }
447 cache->cached = BTRFS_CACHE_STARTED;
448 spin_unlock(&cache->lock);
449
450 ret = load_free_space_cache(fs_info, cache);
451
452 spin_lock(&cache->lock);
453 if (ret == 1) {
454 cache->cached = BTRFS_CACHE_FINISHED;
455 cache->last_byte_to_unpin = (u64)-1;
456 } else {
457 cache->cached = BTRFS_CACHE_NO;
458 }
459 spin_unlock(&cache->lock);
460 if (ret == 1)
461 return 0;
462 }
463
464 if (load_cache_only)
465 return 0;
466
Yan Zheng11833d62009-09-11 16:11:19 -0400467 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
468 BUG_ON(!caching_ctl);
469
470 INIT_LIST_HEAD(&caching_ctl->list);
471 mutex_init(&caching_ctl->mutex);
472 init_waitqueue_head(&caching_ctl->wait);
473 caching_ctl->block_group = cache;
474 caching_ctl->progress = cache->key.objectid;
475 /* one for caching kthread, one for caching block group list */
476 atomic_set(&caching_ctl->count, 2);
477
Josef Bacik817d52f2009-07-13 21:29:25 -0400478 spin_lock(&cache->lock);
479 if (cache->cached != BTRFS_CACHE_NO) {
480 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400481 kfree(caching_ctl);
482 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400483 }
Yan Zheng11833d62009-09-11 16:11:19 -0400484 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400485 cache->cached = BTRFS_CACHE_STARTED;
486 spin_unlock(&cache->lock);
487
Yan Zheng11833d62009-09-11 16:11:19 -0400488 down_write(&fs_info->extent_commit_sem);
489 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
490 up_write(&fs_info->extent_commit_sem);
491
492 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000493 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400494
Josef Bacik817d52f2009-07-13 21:29:25 -0400495 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
496 cache->key.objectid);
497 if (IS_ERR(tsk)) {
498 ret = PTR_ERR(tsk);
499 printk(KERN_ERR "error running thread %d\n", ret);
500 BUG();
501 }
502
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400503 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400504}
505
Josef Bacik0f9dd462008-09-23 13:14:11 -0400506/*
507 * return the block group that starts at or after bytenr
508 */
Chris Masond3977122009-01-05 21:25:51 -0500509static struct btrfs_block_group_cache *
510btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400511{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400512 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400513
Josef Bacik0f9dd462008-09-23 13:14:11 -0400514 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400515
Josef Bacik0f9dd462008-09-23 13:14:11 -0400516 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400517}
518
Josef Bacik0f9dd462008-09-23 13:14:11 -0400519/*
Sankar P9f556842009-05-14 13:52:22 -0400520 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400521 */
Chris Masond3977122009-01-05 21:25:51 -0500522struct btrfs_block_group_cache *btrfs_lookup_block_group(
523 struct btrfs_fs_info *info,
524 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400525{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400526 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400527
Josef Bacik0f9dd462008-09-23 13:14:11 -0400528 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400529
Josef Bacik0f9dd462008-09-23 13:14:11 -0400530 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400531}
Chris Mason0b86a832008-03-24 15:01:56 -0400532
Josef Bacik0f9dd462008-09-23 13:14:11 -0400533static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
534 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400535{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400536 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400537 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400538
Yan, Zhengb742bb82010-05-16 10:46:24 -0400539 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
540 BTRFS_BLOCK_GROUP_METADATA;
541
Chris Mason4184ea72009-03-10 12:39:20 -0400542 rcu_read_lock();
543 list_for_each_entry_rcu(found, head, list) {
544 if (found->flags == flags) {
545 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400546 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400547 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400548 }
Chris Mason4184ea72009-03-10 12:39:20 -0400549 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400550 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400551}
552
Chris Mason4184ea72009-03-10 12:39:20 -0400553/*
554 * after adding space to the filesystem, we need to clear the full flags
555 * on all the space infos.
556 */
557void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
558{
559 struct list_head *head = &info->space_info;
560 struct btrfs_space_info *found;
561
562 rcu_read_lock();
563 list_for_each_entry_rcu(found, head, list)
564 found->full = 0;
565 rcu_read_unlock();
566}
567
Josef Bacik80eb2342008-10-29 14:49:05 -0400568static u64 div_factor(u64 num, int factor)
569{
570 if (factor == 10)
571 return num;
572 num *= factor;
573 do_div(num, 10);
574 return num;
575}
576
Yan Zhengd2fb3432008-12-11 16:30:39 -0500577u64 btrfs_find_block_group(struct btrfs_root *root,
578 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400579{
Chris Mason96b51792007-10-15 16:15:19 -0400580 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400581 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500582 u64 last = max(search_hint, search_start);
583 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400584 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500585 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400586 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400587again:
Zheng Yane8569812008-09-26 10:05:48 -0400588 while (1) {
589 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400590 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400591 break;
Chris Mason96b51792007-10-15 16:15:19 -0400592
Chris Masonc286ac42008-07-22 23:06:41 -0400593 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400594 last = cache->key.objectid + cache->key.offset;
595 used = btrfs_block_group_used(&cache->item);
596
Yan Zhengd2fb3432008-12-11 16:30:39 -0500597 if ((full_search || !cache->ro) &&
598 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400599 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500600 div_factor(cache->key.offset, factor)) {
601 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400602 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400603 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400604 goto found;
605 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400606 }
Chris Masonc286ac42008-07-22 23:06:41 -0400607 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400608 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400609 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400610 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400611 if (!wrapped) {
612 last = search_start;
613 wrapped = 1;
614 goto again;
615 }
616 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400617 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400618 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400619 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400620 goto again;
621 }
Chris Masonbe744172007-05-06 10:15:01 -0400622found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500623 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400624}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400625
Chris Masone02119d2008-09-05 16:13:11 -0400626/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400627int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400628{
629 int ret;
630 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400631 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400632
Zheng Yan31840ae2008-09-23 13:14:14 -0400633 path = btrfs_alloc_path();
634 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400635 key.objectid = start;
636 key.offset = len;
637 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
638 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
639 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400640 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500641 return ret;
642}
643
Chris Masond8d5f3e2007-12-11 12:42:00 -0500644/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400645 * helper function to lookup reference count and flags of extent.
646 *
647 * the head node for delayed ref is used to store the sum of all the
648 * reference count modifications queued up in the rbtree. the head
649 * node may also store the extent flags to set. This way you can check
650 * to see what the reference count and extent flags would be if all of
651 * the delayed refs are not processed.
652 */
653int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
654 struct btrfs_root *root, u64 bytenr,
655 u64 num_bytes, u64 *refs, u64 *flags)
656{
657 struct btrfs_delayed_ref_head *head;
658 struct btrfs_delayed_ref_root *delayed_refs;
659 struct btrfs_path *path;
660 struct btrfs_extent_item *ei;
661 struct extent_buffer *leaf;
662 struct btrfs_key key;
663 u32 item_size;
664 u64 num_refs;
665 u64 extent_flags;
666 int ret;
667
668 path = btrfs_alloc_path();
669 if (!path)
670 return -ENOMEM;
671
672 key.objectid = bytenr;
673 key.type = BTRFS_EXTENT_ITEM_KEY;
674 key.offset = num_bytes;
675 if (!trans) {
676 path->skip_locking = 1;
677 path->search_commit_root = 1;
678 }
679again:
680 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
681 &key, path, 0, 0);
682 if (ret < 0)
683 goto out_free;
684
685 if (ret == 0) {
686 leaf = path->nodes[0];
687 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
688 if (item_size >= sizeof(*ei)) {
689 ei = btrfs_item_ptr(leaf, path->slots[0],
690 struct btrfs_extent_item);
691 num_refs = btrfs_extent_refs(leaf, ei);
692 extent_flags = btrfs_extent_flags(leaf, ei);
693 } else {
694#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
695 struct btrfs_extent_item_v0 *ei0;
696 BUG_ON(item_size != sizeof(*ei0));
697 ei0 = btrfs_item_ptr(leaf, path->slots[0],
698 struct btrfs_extent_item_v0);
699 num_refs = btrfs_extent_refs_v0(leaf, ei0);
700 /* FIXME: this isn't correct for data */
701 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
702#else
703 BUG();
704#endif
705 }
706 BUG_ON(num_refs == 0);
707 } else {
708 num_refs = 0;
709 extent_flags = 0;
710 ret = 0;
711 }
712
713 if (!trans)
714 goto out;
715
716 delayed_refs = &trans->transaction->delayed_refs;
717 spin_lock(&delayed_refs->lock);
718 head = btrfs_find_delayed_ref_head(trans, bytenr);
719 if (head) {
720 if (!mutex_trylock(&head->mutex)) {
721 atomic_inc(&head->node.refs);
722 spin_unlock(&delayed_refs->lock);
723
724 btrfs_release_path(root->fs_info->extent_root, path);
725
726 mutex_lock(&head->mutex);
727 mutex_unlock(&head->mutex);
728 btrfs_put_delayed_ref(&head->node);
729 goto again;
730 }
731 if (head->extent_op && head->extent_op->update_flags)
732 extent_flags |= head->extent_op->flags_to_set;
733 else
734 BUG_ON(num_refs == 0);
735
736 num_refs += head->node.ref_mod;
737 mutex_unlock(&head->mutex);
738 }
739 spin_unlock(&delayed_refs->lock);
740out:
741 WARN_ON(num_refs == 0);
742 if (refs)
743 *refs = num_refs;
744 if (flags)
745 *flags = extent_flags;
746out_free:
747 btrfs_free_path(path);
748 return ret;
749}
750
751/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500752 * Back reference rules. Back refs have three main goals:
753 *
754 * 1) differentiate between all holders of references to an extent so that
755 * when a reference is dropped we can make sure it was a valid reference
756 * before freeing the extent.
757 *
758 * 2) Provide enough information to quickly find the holders of an extent
759 * if we notice a given block is corrupted or bad.
760 *
761 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
762 * maintenance. This is actually the same as #2, but with a slightly
763 * different use case.
764 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400765 * There are two kinds of back refs. The implicit back refs is optimized
766 * for pointers in non-shared tree blocks. For a given pointer in a block,
767 * back refs of this kind provide information about the block's owner tree
768 * and the pointer's key. These information allow us to find the block by
769 * b-tree searching. The full back refs is for pointers in tree blocks not
770 * referenced by their owner trees. The location of tree block is recorded
771 * in the back refs. Actually the full back refs is generic, and can be
772 * used in all cases the implicit back refs is used. The major shortcoming
773 * of the full back refs is its overhead. Every time a tree block gets
774 * COWed, we have to update back refs entry for all pointers in it.
775 *
776 * For a newly allocated tree block, we use implicit back refs for
777 * pointers in it. This means most tree related operations only involve
778 * implicit back refs. For a tree block created in old transaction, the
779 * only way to drop a reference to it is COW it. So we can detect the
780 * event that tree block loses its owner tree's reference and do the
781 * back refs conversion.
782 *
783 * When a tree block is COW'd through a tree, there are four cases:
784 *
785 * The reference count of the block is one and the tree is the block's
786 * owner tree. Nothing to do in this case.
787 *
788 * The reference count of the block is one and the tree is not the
789 * block's owner tree. In this case, full back refs is used for pointers
790 * in the block. Remove these full back refs, add implicit back refs for
791 * every pointers in the new block.
792 *
793 * The reference count of the block is greater than one and the tree is
794 * the block's owner tree. In this case, implicit back refs is used for
795 * pointers in the block. Add full back refs for every pointers in the
796 * block, increase lower level extents' reference counts. The original
797 * implicit back refs are entailed to the new block.
798 *
799 * The reference count of the block is greater than one and the tree is
800 * not the block's owner tree. Add implicit back refs for every pointer in
801 * the new block, increase lower level extents' reference count.
802 *
803 * Back Reference Key composing:
804 *
805 * The key objectid corresponds to the first byte in the extent,
806 * The key type is used to differentiate between types of back refs.
807 * There are different meanings of the key offset for different types
808 * of back refs.
809 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500810 * File extents can be referenced by:
811 *
812 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400813 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500814 * - different offsets inside a file (bookend extents in file.c)
815 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400816 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500817 *
818 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500819 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400820 * - original offset in the file
821 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400822 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400823 * The key offset for the implicit back refs is hash of the first
824 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500825 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400826 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500827 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400828 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500829 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400830 * The key offset for the implicit back refs is the first byte of
831 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500832 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400833 * When a file extent is allocated, The implicit back refs is used.
834 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500835 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400836 * (root_key.objectid, inode objectid, offset in file, 1)
837 *
838 * When a file extent is removed file truncation, we find the
839 * corresponding implicit back refs and check the following fields:
840 *
841 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500842 *
843 * Btree extents can be referenced by:
844 *
845 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500846 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400847 * Both the implicit back refs and the full back refs for tree blocks
848 * only consist of key. The key offset for the implicit back refs is
849 * objectid of block's owner tree. The key offset for the full back refs
850 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500851 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400852 * When implicit back refs is used, information about the lowest key and
853 * level of the tree block are required. These information are stored in
854 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500855 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400856
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
858static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
859 struct btrfs_root *root,
860 struct btrfs_path *path,
861 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500862{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400863 struct btrfs_extent_item *item;
864 struct btrfs_extent_item_v0 *ei0;
865 struct btrfs_extent_ref_v0 *ref0;
866 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400867 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400868 struct btrfs_key key;
869 struct btrfs_key found_key;
870 u32 new_size = sizeof(*item);
871 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500872 int ret;
873
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400874 leaf = path->nodes[0];
875 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500876
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400877 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
878 ei0 = btrfs_item_ptr(leaf, path->slots[0],
879 struct btrfs_extent_item_v0);
880 refs = btrfs_extent_refs_v0(leaf, ei0);
881
882 if (owner == (u64)-1) {
883 while (1) {
884 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
885 ret = btrfs_next_leaf(root, path);
886 if (ret < 0)
887 return ret;
888 BUG_ON(ret > 0);
889 leaf = path->nodes[0];
890 }
891 btrfs_item_key_to_cpu(leaf, &found_key,
892 path->slots[0]);
893 BUG_ON(key.objectid != found_key.objectid);
894 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
895 path->slots[0]++;
896 continue;
897 }
898 ref0 = btrfs_item_ptr(leaf, path->slots[0],
899 struct btrfs_extent_ref_v0);
900 owner = btrfs_ref_objectid_v0(leaf, ref0);
901 break;
902 }
903 }
904 btrfs_release_path(root, path);
905
906 if (owner < BTRFS_FIRST_FREE_OBJECTID)
907 new_size += sizeof(*bi);
908
909 new_size -= sizeof(*ei0);
910 ret = btrfs_search_slot(trans, root, &key, path,
911 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400912 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400913 return ret;
914 BUG_ON(ret);
915
916 ret = btrfs_extend_item(trans, root, path, new_size);
917 BUG_ON(ret);
918
919 leaf = path->nodes[0];
920 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
921 btrfs_set_extent_refs(leaf, item, refs);
922 /* FIXME: get real generation */
923 btrfs_set_extent_generation(leaf, item, 0);
924 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
925 btrfs_set_extent_flags(leaf, item,
926 BTRFS_EXTENT_FLAG_TREE_BLOCK |
927 BTRFS_BLOCK_FLAG_FULL_BACKREF);
928 bi = (struct btrfs_tree_block_info *)(item + 1);
929 /* FIXME: get first key of the block */
930 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
931 btrfs_set_tree_block_level(leaf, bi, (int)owner);
932 } else {
933 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
934 }
935 btrfs_mark_buffer_dirty(leaf);
936 return 0;
937}
938#endif
939
940static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
941{
942 u32 high_crc = ~(u32)0;
943 u32 low_crc = ~(u32)0;
944 __le64 lenum;
945
946 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100947 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400948 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100949 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100951 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400952
953 return ((u64)high_crc << 31) ^ (u64)low_crc;
954}
955
956static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
957 struct btrfs_extent_data_ref *ref)
958{
959 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
960 btrfs_extent_data_ref_objectid(leaf, ref),
961 btrfs_extent_data_ref_offset(leaf, ref));
962}
963
964static int match_extent_data_ref(struct extent_buffer *leaf,
965 struct btrfs_extent_data_ref *ref,
966 u64 root_objectid, u64 owner, u64 offset)
967{
968 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
969 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
970 btrfs_extent_data_ref_offset(leaf, ref) != offset)
971 return 0;
972 return 1;
973}
974
975static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
976 struct btrfs_root *root,
977 struct btrfs_path *path,
978 u64 bytenr, u64 parent,
979 u64 root_objectid,
980 u64 owner, u64 offset)
981{
982 struct btrfs_key key;
983 struct btrfs_extent_data_ref *ref;
984 struct extent_buffer *leaf;
985 u32 nritems;
986 int ret;
987 int recow;
988 int err = -ENOENT;
989
990 key.objectid = bytenr;
991 if (parent) {
992 key.type = BTRFS_SHARED_DATA_REF_KEY;
993 key.offset = parent;
994 } else {
995 key.type = BTRFS_EXTENT_DATA_REF_KEY;
996 key.offset = hash_extent_data_ref(root_objectid,
997 owner, offset);
998 }
999again:
1000 recow = 0;
1001 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1002 if (ret < 0) {
1003 err = ret;
1004 goto fail;
1005 }
1006
1007 if (parent) {
1008 if (!ret)
1009 return 0;
1010#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1011 key.type = BTRFS_EXTENT_REF_V0_KEY;
1012 btrfs_release_path(root, path);
1013 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1014 if (ret < 0) {
1015 err = ret;
1016 goto fail;
1017 }
1018 if (!ret)
1019 return 0;
1020#endif
1021 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001022 }
1023
1024 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001025 nritems = btrfs_header_nritems(leaf);
1026 while (1) {
1027 if (path->slots[0] >= nritems) {
1028 ret = btrfs_next_leaf(root, path);
1029 if (ret < 0)
1030 err = ret;
1031 if (ret)
1032 goto fail;
1033
1034 leaf = path->nodes[0];
1035 nritems = btrfs_header_nritems(leaf);
1036 recow = 1;
1037 }
1038
1039 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1040 if (key.objectid != bytenr ||
1041 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1042 goto fail;
1043
1044 ref = btrfs_item_ptr(leaf, path->slots[0],
1045 struct btrfs_extent_data_ref);
1046
1047 if (match_extent_data_ref(leaf, ref, root_objectid,
1048 owner, offset)) {
1049 if (recow) {
1050 btrfs_release_path(root, path);
1051 goto again;
1052 }
1053 err = 0;
1054 break;
1055 }
1056 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001057 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001058fail:
1059 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001060}
1061
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001062static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1063 struct btrfs_root *root,
1064 struct btrfs_path *path,
1065 u64 bytenr, u64 parent,
1066 u64 root_objectid, u64 owner,
1067 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001068{
1069 struct btrfs_key key;
1070 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001071 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001072 u32 num_refs;
1073 int ret;
1074
1075 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001076 if (parent) {
1077 key.type = BTRFS_SHARED_DATA_REF_KEY;
1078 key.offset = parent;
1079 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001080 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001081 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1082 key.offset = hash_extent_data_ref(root_objectid,
1083 owner, offset);
1084 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001085 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001086
1087 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1088 if (ret && ret != -EEXIST)
1089 goto fail;
1090
1091 leaf = path->nodes[0];
1092 if (parent) {
1093 struct btrfs_shared_data_ref *ref;
1094 ref = btrfs_item_ptr(leaf, path->slots[0],
1095 struct btrfs_shared_data_ref);
1096 if (ret == 0) {
1097 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1098 } else {
1099 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1100 num_refs += refs_to_add;
1101 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1102 }
1103 } else {
1104 struct btrfs_extent_data_ref *ref;
1105 while (ret == -EEXIST) {
1106 ref = btrfs_item_ptr(leaf, path->slots[0],
1107 struct btrfs_extent_data_ref);
1108 if (match_extent_data_ref(leaf, ref, root_objectid,
1109 owner, offset))
1110 break;
1111 btrfs_release_path(root, path);
1112 key.offset++;
1113 ret = btrfs_insert_empty_item(trans, root, path, &key,
1114 size);
1115 if (ret && ret != -EEXIST)
1116 goto fail;
1117
1118 leaf = path->nodes[0];
1119 }
1120 ref = btrfs_item_ptr(leaf, path->slots[0],
1121 struct btrfs_extent_data_ref);
1122 if (ret == 0) {
1123 btrfs_set_extent_data_ref_root(leaf, ref,
1124 root_objectid);
1125 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1126 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1127 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1128 } else {
1129 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1130 num_refs += refs_to_add;
1131 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1132 }
1133 }
1134 btrfs_mark_buffer_dirty(leaf);
1135 ret = 0;
1136fail:
Chris Mason7bb86312007-12-11 09:25:06 -05001137 btrfs_release_path(root, path);
1138 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001139}
1140
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001141static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1142 struct btrfs_root *root,
1143 struct btrfs_path *path,
1144 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001145{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001146 struct btrfs_key key;
1147 struct btrfs_extent_data_ref *ref1 = NULL;
1148 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001149 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001150 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001151 int ret = 0;
1152
1153 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001154 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1155
1156 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1157 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1158 struct btrfs_extent_data_ref);
1159 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1160 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1161 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1162 struct btrfs_shared_data_ref);
1163 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1164#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1165 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1166 struct btrfs_extent_ref_v0 *ref0;
1167 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1168 struct btrfs_extent_ref_v0);
1169 num_refs = btrfs_ref_count_v0(leaf, ref0);
1170#endif
1171 } else {
1172 BUG();
1173 }
1174
Chris Mason56bec292009-03-13 10:10:06 -04001175 BUG_ON(num_refs < refs_to_drop);
1176 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001177
Zheng Yan31840ae2008-09-23 13:14:14 -04001178 if (num_refs == 0) {
1179 ret = btrfs_del_item(trans, root, path);
1180 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001181 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1182 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1183 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1184 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1185#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1186 else {
1187 struct btrfs_extent_ref_v0 *ref0;
1188 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1189 struct btrfs_extent_ref_v0);
1190 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1191 }
1192#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001193 btrfs_mark_buffer_dirty(leaf);
1194 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001195 return ret;
1196}
1197
1198static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1199 struct btrfs_path *path,
1200 struct btrfs_extent_inline_ref *iref)
1201{
1202 struct btrfs_key key;
1203 struct extent_buffer *leaf;
1204 struct btrfs_extent_data_ref *ref1;
1205 struct btrfs_shared_data_ref *ref2;
1206 u32 num_refs = 0;
1207
1208 leaf = path->nodes[0];
1209 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1210 if (iref) {
1211 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1212 BTRFS_EXTENT_DATA_REF_KEY) {
1213 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1214 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1215 } else {
1216 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1217 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1218 }
1219 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1220 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1221 struct btrfs_extent_data_ref);
1222 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1223 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1224 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1225 struct btrfs_shared_data_ref);
1226 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1227#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1228 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1229 struct btrfs_extent_ref_v0 *ref0;
1230 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1231 struct btrfs_extent_ref_v0);
1232 num_refs = btrfs_ref_count_v0(leaf, ref0);
1233#endif
1234 } else {
1235 WARN_ON(1);
1236 }
1237 return num_refs;
1238}
1239
1240static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1241 struct btrfs_root *root,
1242 struct btrfs_path *path,
1243 u64 bytenr, u64 parent,
1244 u64 root_objectid)
1245{
1246 struct btrfs_key key;
1247 int ret;
1248
1249 key.objectid = bytenr;
1250 if (parent) {
1251 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1252 key.offset = parent;
1253 } else {
1254 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1255 key.offset = root_objectid;
1256 }
1257
1258 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1259 if (ret > 0)
1260 ret = -ENOENT;
1261#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1262 if (ret == -ENOENT && parent) {
1263 btrfs_release_path(root, path);
1264 key.type = BTRFS_EXTENT_REF_V0_KEY;
1265 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1266 if (ret > 0)
1267 ret = -ENOENT;
1268 }
1269#endif
1270 return ret;
1271}
1272
1273static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1274 struct btrfs_root *root,
1275 struct btrfs_path *path,
1276 u64 bytenr, u64 parent,
1277 u64 root_objectid)
1278{
1279 struct btrfs_key key;
1280 int ret;
1281
1282 key.objectid = bytenr;
1283 if (parent) {
1284 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1285 key.offset = parent;
1286 } else {
1287 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1288 key.offset = root_objectid;
1289 }
1290
1291 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001292 btrfs_release_path(root, path);
1293 return ret;
1294}
1295
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001296static inline int extent_ref_type(u64 parent, u64 owner)
1297{
1298 int type;
1299 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1300 if (parent > 0)
1301 type = BTRFS_SHARED_BLOCK_REF_KEY;
1302 else
1303 type = BTRFS_TREE_BLOCK_REF_KEY;
1304 } else {
1305 if (parent > 0)
1306 type = BTRFS_SHARED_DATA_REF_KEY;
1307 else
1308 type = BTRFS_EXTENT_DATA_REF_KEY;
1309 }
1310 return type;
1311}
1312
Yan Zheng2c47e6052009-06-27 21:07:35 -04001313static int find_next_key(struct btrfs_path *path, int level,
1314 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001315
1316{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001317 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001318 if (!path->nodes[level])
1319 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001320 if (path->slots[level] + 1 >=
1321 btrfs_header_nritems(path->nodes[level]))
1322 continue;
1323 if (level == 0)
1324 btrfs_item_key_to_cpu(path->nodes[level], key,
1325 path->slots[level] + 1);
1326 else
1327 btrfs_node_key_to_cpu(path->nodes[level], key,
1328 path->slots[level] + 1);
1329 return 0;
1330 }
1331 return 1;
1332}
1333
1334/*
1335 * look for inline back ref. if back ref is found, *ref_ret is set
1336 * to the address of inline back ref, and 0 is returned.
1337 *
1338 * if back ref isn't found, *ref_ret is set to the address where it
1339 * should be inserted, and -ENOENT is returned.
1340 *
1341 * if insert is true and there are too many inline back refs, the path
1342 * points to the extent item, and -EAGAIN is returned.
1343 *
1344 * NOTE: inline back refs are ordered in the same way that back ref
1345 * items in the tree are ordered.
1346 */
1347static noinline_for_stack
1348int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1349 struct btrfs_root *root,
1350 struct btrfs_path *path,
1351 struct btrfs_extent_inline_ref **ref_ret,
1352 u64 bytenr, u64 num_bytes,
1353 u64 parent, u64 root_objectid,
1354 u64 owner, u64 offset, int insert)
1355{
1356 struct btrfs_key key;
1357 struct extent_buffer *leaf;
1358 struct btrfs_extent_item *ei;
1359 struct btrfs_extent_inline_ref *iref;
1360 u64 flags;
1361 u64 item_size;
1362 unsigned long ptr;
1363 unsigned long end;
1364 int extra_size;
1365 int type;
1366 int want;
1367 int ret;
1368 int err = 0;
1369
1370 key.objectid = bytenr;
1371 key.type = BTRFS_EXTENT_ITEM_KEY;
1372 key.offset = num_bytes;
1373
1374 want = extent_ref_type(parent, owner);
1375 if (insert) {
1376 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001377 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001378 } else
1379 extra_size = -1;
1380 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1381 if (ret < 0) {
1382 err = ret;
1383 goto out;
1384 }
1385 BUG_ON(ret);
1386
1387 leaf = path->nodes[0];
1388 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1389#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1390 if (item_size < sizeof(*ei)) {
1391 if (!insert) {
1392 err = -ENOENT;
1393 goto out;
1394 }
1395 ret = convert_extent_item_v0(trans, root, path, owner,
1396 extra_size);
1397 if (ret < 0) {
1398 err = ret;
1399 goto out;
1400 }
1401 leaf = path->nodes[0];
1402 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1403 }
1404#endif
1405 BUG_ON(item_size < sizeof(*ei));
1406
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001407 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1408 flags = btrfs_extent_flags(leaf, ei);
1409
1410 ptr = (unsigned long)(ei + 1);
1411 end = (unsigned long)ei + item_size;
1412
1413 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1414 ptr += sizeof(struct btrfs_tree_block_info);
1415 BUG_ON(ptr > end);
1416 } else {
1417 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1418 }
1419
1420 err = -ENOENT;
1421 while (1) {
1422 if (ptr >= end) {
1423 WARN_ON(ptr > end);
1424 break;
1425 }
1426 iref = (struct btrfs_extent_inline_ref *)ptr;
1427 type = btrfs_extent_inline_ref_type(leaf, iref);
1428 if (want < type)
1429 break;
1430 if (want > type) {
1431 ptr += btrfs_extent_inline_ref_size(type);
1432 continue;
1433 }
1434
1435 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1436 struct btrfs_extent_data_ref *dref;
1437 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1438 if (match_extent_data_ref(leaf, dref, root_objectid,
1439 owner, offset)) {
1440 err = 0;
1441 break;
1442 }
1443 if (hash_extent_data_ref_item(leaf, dref) <
1444 hash_extent_data_ref(root_objectid, owner, offset))
1445 break;
1446 } else {
1447 u64 ref_offset;
1448 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1449 if (parent > 0) {
1450 if (parent == ref_offset) {
1451 err = 0;
1452 break;
1453 }
1454 if (ref_offset < parent)
1455 break;
1456 } else {
1457 if (root_objectid == ref_offset) {
1458 err = 0;
1459 break;
1460 }
1461 if (ref_offset < root_objectid)
1462 break;
1463 }
1464 }
1465 ptr += btrfs_extent_inline_ref_size(type);
1466 }
1467 if (err == -ENOENT && insert) {
1468 if (item_size + extra_size >=
1469 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1470 err = -EAGAIN;
1471 goto out;
1472 }
1473 /*
1474 * To add new inline back ref, we have to make sure
1475 * there is no corresponding back ref item.
1476 * For simplicity, we just do not add new inline back
1477 * ref if there is any kind of item for this block
1478 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001479 if (find_next_key(path, 0, &key) == 0 &&
1480 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001481 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001482 err = -EAGAIN;
1483 goto out;
1484 }
1485 }
1486 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1487out:
Yan Zheng85d41982009-06-11 08:51:10 -04001488 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001489 path->keep_locks = 0;
1490 btrfs_unlock_up_safe(path, 1);
1491 }
1492 return err;
1493}
1494
1495/*
1496 * helper to add new inline back ref
1497 */
1498static noinline_for_stack
1499int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1500 struct btrfs_root *root,
1501 struct btrfs_path *path,
1502 struct btrfs_extent_inline_ref *iref,
1503 u64 parent, u64 root_objectid,
1504 u64 owner, u64 offset, int refs_to_add,
1505 struct btrfs_delayed_extent_op *extent_op)
1506{
1507 struct extent_buffer *leaf;
1508 struct btrfs_extent_item *ei;
1509 unsigned long ptr;
1510 unsigned long end;
1511 unsigned long item_offset;
1512 u64 refs;
1513 int size;
1514 int type;
1515 int ret;
1516
1517 leaf = path->nodes[0];
1518 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1519 item_offset = (unsigned long)iref - (unsigned long)ei;
1520
1521 type = extent_ref_type(parent, owner);
1522 size = btrfs_extent_inline_ref_size(type);
1523
1524 ret = btrfs_extend_item(trans, root, path, size);
1525 BUG_ON(ret);
1526
1527 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1528 refs = btrfs_extent_refs(leaf, ei);
1529 refs += refs_to_add;
1530 btrfs_set_extent_refs(leaf, ei, refs);
1531 if (extent_op)
1532 __run_delayed_extent_op(extent_op, leaf, ei);
1533
1534 ptr = (unsigned long)ei + item_offset;
1535 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1536 if (ptr < end - size)
1537 memmove_extent_buffer(leaf, ptr + size, ptr,
1538 end - size - ptr);
1539
1540 iref = (struct btrfs_extent_inline_ref *)ptr;
1541 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1542 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1543 struct btrfs_extent_data_ref *dref;
1544 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1545 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1546 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1547 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1548 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1549 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1550 struct btrfs_shared_data_ref *sref;
1551 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1552 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1553 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1554 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1555 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1556 } else {
1557 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1558 }
1559 btrfs_mark_buffer_dirty(leaf);
1560 return 0;
1561}
1562
1563static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1564 struct btrfs_root *root,
1565 struct btrfs_path *path,
1566 struct btrfs_extent_inline_ref **ref_ret,
1567 u64 bytenr, u64 num_bytes, u64 parent,
1568 u64 root_objectid, u64 owner, u64 offset)
1569{
1570 int ret;
1571
1572 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1573 bytenr, num_bytes, parent,
1574 root_objectid, owner, offset, 0);
1575 if (ret != -ENOENT)
1576 return ret;
1577
1578 btrfs_release_path(root, path);
1579 *ref_ret = NULL;
1580
1581 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1582 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1583 root_objectid);
1584 } else {
1585 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1586 root_objectid, owner, offset);
1587 }
1588 return ret;
1589}
1590
1591/*
1592 * helper to update/remove inline back ref
1593 */
1594static noinline_for_stack
1595int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1596 struct btrfs_root *root,
1597 struct btrfs_path *path,
1598 struct btrfs_extent_inline_ref *iref,
1599 int refs_to_mod,
1600 struct btrfs_delayed_extent_op *extent_op)
1601{
1602 struct extent_buffer *leaf;
1603 struct btrfs_extent_item *ei;
1604 struct btrfs_extent_data_ref *dref = NULL;
1605 struct btrfs_shared_data_ref *sref = NULL;
1606 unsigned long ptr;
1607 unsigned long end;
1608 u32 item_size;
1609 int size;
1610 int type;
1611 int ret;
1612 u64 refs;
1613
1614 leaf = path->nodes[0];
1615 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1616 refs = btrfs_extent_refs(leaf, ei);
1617 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1618 refs += refs_to_mod;
1619 btrfs_set_extent_refs(leaf, ei, refs);
1620 if (extent_op)
1621 __run_delayed_extent_op(extent_op, leaf, ei);
1622
1623 type = btrfs_extent_inline_ref_type(leaf, iref);
1624
1625 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1626 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1627 refs = btrfs_extent_data_ref_count(leaf, dref);
1628 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1629 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1630 refs = btrfs_shared_data_ref_count(leaf, sref);
1631 } else {
1632 refs = 1;
1633 BUG_ON(refs_to_mod != -1);
1634 }
1635
1636 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1637 refs += refs_to_mod;
1638
1639 if (refs > 0) {
1640 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1641 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1642 else
1643 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1644 } else {
1645 size = btrfs_extent_inline_ref_size(type);
1646 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1647 ptr = (unsigned long)iref;
1648 end = (unsigned long)ei + item_size;
1649 if (ptr + size < end)
1650 memmove_extent_buffer(leaf, ptr, ptr + size,
1651 end - ptr - size);
1652 item_size -= size;
1653 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1654 BUG_ON(ret);
1655 }
1656 btrfs_mark_buffer_dirty(leaf);
1657 return 0;
1658}
1659
1660static noinline_for_stack
1661int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1662 struct btrfs_root *root,
1663 struct btrfs_path *path,
1664 u64 bytenr, u64 num_bytes, u64 parent,
1665 u64 root_objectid, u64 owner,
1666 u64 offset, int refs_to_add,
1667 struct btrfs_delayed_extent_op *extent_op)
1668{
1669 struct btrfs_extent_inline_ref *iref;
1670 int ret;
1671
1672 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1673 bytenr, num_bytes, parent,
1674 root_objectid, owner, offset, 1);
1675 if (ret == 0) {
1676 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1677 ret = update_inline_extent_backref(trans, root, path, iref,
1678 refs_to_add, extent_op);
1679 } else if (ret == -ENOENT) {
1680 ret = setup_inline_extent_backref(trans, root, path, iref,
1681 parent, root_objectid,
1682 owner, offset, refs_to_add,
1683 extent_op);
1684 }
1685 return ret;
1686}
1687
1688static int insert_extent_backref(struct btrfs_trans_handle *trans,
1689 struct btrfs_root *root,
1690 struct btrfs_path *path,
1691 u64 bytenr, u64 parent, u64 root_objectid,
1692 u64 owner, u64 offset, int refs_to_add)
1693{
1694 int ret;
1695 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1696 BUG_ON(refs_to_add != 1);
1697 ret = insert_tree_block_ref(trans, root, path, bytenr,
1698 parent, root_objectid);
1699 } else {
1700 ret = insert_extent_data_ref(trans, root, path, bytenr,
1701 parent, root_objectid,
1702 owner, offset, refs_to_add);
1703 }
1704 return ret;
1705}
1706
1707static int remove_extent_backref(struct btrfs_trans_handle *trans,
1708 struct btrfs_root *root,
1709 struct btrfs_path *path,
1710 struct btrfs_extent_inline_ref *iref,
1711 int refs_to_drop, int is_data)
1712{
1713 int ret;
1714
1715 BUG_ON(!is_data && refs_to_drop != 1);
1716 if (iref) {
1717 ret = update_inline_extent_backref(trans, root, path, iref,
1718 -refs_to_drop, NULL);
1719 } else if (is_data) {
1720 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1721 } else {
1722 ret = btrfs_del_item(trans, root, path);
1723 }
1724 return ret;
1725}
1726
Chris Mason15916de2008-11-19 21:17:22 -05001727static void btrfs_issue_discard(struct block_device *bdev,
1728 u64 start, u64 len)
1729{
Christoph Hellwig746cd1e2009-09-12 07:35:43 +02001730 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +04001731 BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
Chris Mason15916de2008-11-19 21:17:22 -05001732}
Chris Mason15916de2008-11-19 21:17:22 -05001733
Liu Hui1f3c79a2009-01-05 15:57:51 -05001734static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1735 u64 num_bytes)
1736{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001737 int ret;
1738 u64 map_length = num_bytes;
1739 struct btrfs_multi_bio *multi = NULL;
1740
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001741 if (!btrfs_test_opt(root, DISCARD))
1742 return 0;
1743
Liu Hui1f3c79a2009-01-05 15:57:51 -05001744 /* Tell the block device(s) that the sectors can be discarded */
1745 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1746 bytenr, &map_length, &multi, 0);
1747 if (!ret) {
1748 struct btrfs_bio_stripe *stripe = multi->stripes;
1749 int i;
1750
1751 if (map_length > num_bytes)
1752 map_length = num_bytes;
1753
1754 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1755 btrfs_issue_discard(stripe->dev->bdev,
1756 stripe->physical,
1757 map_length);
1758 }
1759 kfree(multi);
1760 }
1761
1762 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001763}
1764
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001765int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1766 struct btrfs_root *root,
1767 u64 bytenr, u64 num_bytes, u64 parent,
1768 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001769{
1770 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001771 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1772 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001773
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001774 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1775 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1776 parent, root_objectid, (int)owner,
1777 BTRFS_ADD_DELAYED_REF, NULL);
1778 } else {
1779 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1780 parent, root_objectid, owner, offset,
1781 BTRFS_ADD_DELAYED_REF, NULL);
1782 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001783 return ret;
1784}
1785
Chris Mason925baed2008-06-25 16:01:30 -04001786static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001787 struct btrfs_root *root,
1788 u64 bytenr, u64 num_bytes,
1789 u64 parent, u64 root_objectid,
1790 u64 owner, u64 offset, int refs_to_add,
1791 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001792{
Chris Mason5caf2a02007-04-02 11:20:42 -04001793 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001794 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001795 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001796 u64 refs;
1797 int ret;
1798 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001799
Chris Mason5caf2a02007-04-02 11:20:42 -04001800 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001801 if (!path)
1802 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001803
Chris Mason3c12ac72008-04-21 12:01:38 -04001804 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001805 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001806 /* this will setup the path even if it fails to insert the back ref */
1807 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1808 path, bytenr, num_bytes, parent,
1809 root_objectid, owner, offset,
1810 refs_to_add, extent_op);
1811 if (ret == 0)
1812 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001813
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001814 if (ret != -EAGAIN) {
1815 err = ret;
1816 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001817 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001818
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001819 leaf = path->nodes[0];
1820 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1821 refs = btrfs_extent_refs(leaf, item);
1822 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1823 if (extent_op)
1824 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001825
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001826 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001827 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001828
Chris Mason3c12ac72008-04-21 12:01:38 -04001829 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001830 path->leave_spinning = 1;
1831
Chris Mason56bec292009-03-13 10:10:06 -04001832 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001833 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001834 path, bytenr, parent, root_objectid,
1835 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001836 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001837out:
Chris Mason74493f72007-12-11 09:25:06 -05001838 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001839 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001840}
1841
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001842static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1843 struct btrfs_root *root,
1844 struct btrfs_delayed_ref_node *node,
1845 struct btrfs_delayed_extent_op *extent_op,
1846 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001847{
Chris Mason56bec292009-03-13 10:10:06 -04001848 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001849 struct btrfs_delayed_data_ref *ref;
1850 struct btrfs_key ins;
1851 u64 parent = 0;
1852 u64 ref_root = 0;
1853 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001854
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001855 ins.objectid = node->bytenr;
1856 ins.offset = node->num_bytes;
1857 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001858
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001859 ref = btrfs_delayed_node_to_data_ref(node);
1860 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1861 parent = ref->parent;
1862 else
1863 ref_root = ref->root;
1864
1865 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1866 if (extent_op) {
1867 BUG_ON(extent_op->update_key);
1868 flags |= extent_op->flags_to_set;
1869 }
1870 ret = alloc_reserved_file_extent(trans, root,
1871 parent, ref_root, flags,
1872 ref->objectid, ref->offset,
1873 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001874 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1875 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1876 node->num_bytes, parent,
1877 ref_root, ref->objectid,
1878 ref->offset, node->ref_mod,
1879 extent_op);
1880 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1881 ret = __btrfs_free_extent(trans, root, node->bytenr,
1882 node->num_bytes, parent,
1883 ref_root, ref->objectid,
1884 ref->offset, node->ref_mod,
1885 extent_op);
1886 } else {
1887 BUG();
1888 }
Chris Mason56bec292009-03-13 10:10:06 -04001889 return ret;
1890}
1891
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001892static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1893 struct extent_buffer *leaf,
1894 struct btrfs_extent_item *ei)
1895{
1896 u64 flags = btrfs_extent_flags(leaf, ei);
1897 if (extent_op->update_flags) {
1898 flags |= extent_op->flags_to_set;
1899 btrfs_set_extent_flags(leaf, ei, flags);
1900 }
1901
1902 if (extent_op->update_key) {
1903 struct btrfs_tree_block_info *bi;
1904 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1905 bi = (struct btrfs_tree_block_info *)(ei + 1);
1906 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1907 }
1908}
1909
1910static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1911 struct btrfs_root *root,
1912 struct btrfs_delayed_ref_node *node,
1913 struct btrfs_delayed_extent_op *extent_op)
1914{
1915 struct btrfs_key key;
1916 struct btrfs_path *path;
1917 struct btrfs_extent_item *ei;
1918 struct extent_buffer *leaf;
1919 u32 item_size;
1920 int ret;
1921 int err = 0;
1922
1923 path = btrfs_alloc_path();
1924 if (!path)
1925 return -ENOMEM;
1926
1927 key.objectid = node->bytenr;
1928 key.type = BTRFS_EXTENT_ITEM_KEY;
1929 key.offset = node->num_bytes;
1930
1931 path->reada = 1;
1932 path->leave_spinning = 1;
1933 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1934 path, 0, 1);
1935 if (ret < 0) {
1936 err = ret;
1937 goto out;
1938 }
1939 if (ret > 0) {
1940 err = -EIO;
1941 goto out;
1942 }
1943
1944 leaf = path->nodes[0];
1945 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1946#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1947 if (item_size < sizeof(*ei)) {
1948 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1949 path, (u64)-1, 0);
1950 if (ret < 0) {
1951 err = ret;
1952 goto out;
1953 }
1954 leaf = path->nodes[0];
1955 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1956 }
1957#endif
1958 BUG_ON(item_size < sizeof(*ei));
1959 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1960 __run_delayed_extent_op(extent_op, leaf, ei);
1961
1962 btrfs_mark_buffer_dirty(leaf);
1963out:
1964 btrfs_free_path(path);
1965 return err;
1966}
1967
1968static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1969 struct btrfs_root *root,
1970 struct btrfs_delayed_ref_node *node,
1971 struct btrfs_delayed_extent_op *extent_op,
1972 int insert_reserved)
1973{
1974 int ret = 0;
1975 struct btrfs_delayed_tree_ref *ref;
1976 struct btrfs_key ins;
1977 u64 parent = 0;
1978 u64 ref_root = 0;
1979
1980 ins.objectid = node->bytenr;
1981 ins.offset = node->num_bytes;
1982 ins.type = BTRFS_EXTENT_ITEM_KEY;
1983
1984 ref = btrfs_delayed_node_to_tree_ref(node);
1985 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1986 parent = ref->parent;
1987 else
1988 ref_root = ref->root;
1989
1990 BUG_ON(node->ref_mod != 1);
1991 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1992 BUG_ON(!extent_op || !extent_op->update_flags ||
1993 !extent_op->update_key);
1994 ret = alloc_reserved_tree_block(trans, root,
1995 parent, ref_root,
1996 extent_op->flags_to_set,
1997 &extent_op->key,
1998 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001999 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2000 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2001 node->num_bytes, parent, ref_root,
2002 ref->level, 0, 1, extent_op);
2003 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2004 ret = __btrfs_free_extent(trans, root, node->bytenr,
2005 node->num_bytes, parent, ref_root,
2006 ref->level, 0, 1, extent_op);
2007 } else {
2008 BUG();
2009 }
2010 return ret;
2011}
2012
Chris Mason56bec292009-03-13 10:10:06 -04002013/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002014static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2015 struct btrfs_root *root,
2016 struct btrfs_delayed_ref_node *node,
2017 struct btrfs_delayed_extent_op *extent_op,
2018 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002019{
Josef Bacikeb099672009-02-12 09:27:38 -05002020 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002021 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002022 struct btrfs_delayed_ref_head *head;
2023 /*
2024 * we've hit the end of the chain and we were supposed
2025 * to insert this extent into the tree. But, it got
2026 * deleted before we ever needed to insert it, so all
2027 * we have to do is clean up the accounting
2028 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002029 BUG_ON(extent_op);
2030 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002031 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002032 btrfs_pin_extent(root, node->bytenr,
2033 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002034 if (head->is_data) {
2035 ret = btrfs_del_csums(trans, root,
2036 node->bytenr,
2037 node->num_bytes);
2038 BUG_ON(ret);
2039 }
Chris Mason56bec292009-03-13 10:10:06 -04002040 }
Chris Mason56bec292009-03-13 10:10:06 -04002041 mutex_unlock(&head->mutex);
2042 return 0;
2043 }
Josef Bacikeb099672009-02-12 09:27:38 -05002044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002045 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2046 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2047 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2048 insert_reserved);
2049 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2050 node->type == BTRFS_SHARED_DATA_REF_KEY)
2051 ret = run_delayed_data_ref(trans, root, node, extent_op,
2052 insert_reserved);
2053 else
2054 BUG();
2055 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002056}
2057
Chris Mason56bec292009-03-13 10:10:06 -04002058static noinline struct btrfs_delayed_ref_node *
2059select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002060{
Chris Mason56bec292009-03-13 10:10:06 -04002061 struct rb_node *node;
2062 struct btrfs_delayed_ref_node *ref;
2063 int action = BTRFS_ADD_DELAYED_REF;
2064again:
2065 /*
2066 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2067 * this prevents ref count from going down to zero when
2068 * there still are pending delayed ref.
2069 */
2070 node = rb_prev(&head->node.rb_node);
2071 while (1) {
2072 if (!node)
2073 break;
2074 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2075 rb_node);
2076 if (ref->bytenr != head->node.bytenr)
2077 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002078 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002079 return ref;
2080 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002081 }
Chris Mason56bec292009-03-13 10:10:06 -04002082 if (action == BTRFS_ADD_DELAYED_REF) {
2083 action = BTRFS_DROP_DELAYED_REF;
2084 goto again;
2085 }
2086 return NULL;
2087}
2088
Chris Masonc3e69d52009-03-13 10:17:05 -04002089static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2090 struct btrfs_root *root,
2091 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002092{
Chris Mason56bec292009-03-13 10:10:06 -04002093 struct btrfs_delayed_ref_root *delayed_refs;
2094 struct btrfs_delayed_ref_node *ref;
2095 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002096 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002097 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002098 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002099 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002100
2101 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002102 while (1) {
2103 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002104 /* pick a new head ref from the cluster list */
2105 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002106 break;
Chris Mason56bec292009-03-13 10:10:06 -04002107
Chris Masonc3e69d52009-03-13 10:17:05 -04002108 locked_ref = list_entry(cluster->next,
2109 struct btrfs_delayed_ref_head, cluster);
2110
2111 /* grab the lock that says we are going to process
2112 * all the refs for this head */
2113 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2114
2115 /*
2116 * we may have dropped the spin lock to get the head
2117 * mutex lock, and that might have given someone else
2118 * time to free the head. If that's true, it has been
2119 * removed from our list and we can move on.
2120 */
2121 if (ret == -EAGAIN) {
2122 locked_ref = NULL;
2123 count++;
2124 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002125 }
2126 }
2127
2128 /*
2129 * record the must insert reserved flag before we
2130 * drop the spin lock.
2131 */
2132 must_insert_reserved = locked_ref->must_insert_reserved;
2133 locked_ref->must_insert_reserved = 0;
2134
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002135 extent_op = locked_ref->extent_op;
2136 locked_ref->extent_op = NULL;
2137
Chris Mason56bec292009-03-13 10:10:06 -04002138 /*
2139 * locked_ref is the head node, so we have to go one
2140 * node back for any delayed ref updates
2141 */
Chris Mason56bec292009-03-13 10:10:06 -04002142 ref = select_delayed_ref(locked_ref);
2143 if (!ref) {
2144 /* All delayed refs have been processed, Go ahead
2145 * and send the head node to run_one_delayed_ref,
2146 * so that any accounting fixes can happen
2147 */
2148 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002149
2150 if (extent_op && must_insert_reserved) {
2151 kfree(extent_op);
2152 extent_op = NULL;
2153 }
2154
2155 if (extent_op) {
2156 spin_unlock(&delayed_refs->lock);
2157
2158 ret = run_delayed_extent_op(trans, root,
2159 ref, extent_op);
2160 BUG_ON(ret);
2161 kfree(extent_op);
2162
2163 cond_resched();
2164 spin_lock(&delayed_refs->lock);
2165 continue;
2166 }
2167
Chris Masonc3e69d52009-03-13 10:17:05 -04002168 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002169 locked_ref = NULL;
2170 }
2171
2172 ref->in_tree = 0;
2173 rb_erase(&ref->rb_node, &delayed_refs->root);
2174 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002175
Chris Mason56bec292009-03-13 10:10:06 -04002176 spin_unlock(&delayed_refs->lock);
2177
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002178 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002179 must_insert_reserved);
2180 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002181
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002182 btrfs_put_delayed_ref(ref);
2183 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002184 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002185
Chris Mason1887be62009-03-13 10:11:24 -04002186 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002187 spin_lock(&delayed_refs->lock);
2188 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002189 return count;
2190}
2191
2192/*
2193 * this starts processing the delayed reference count updates and
2194 * extent insertions we have queued up so far. count can be
2195 * 0, which means to process everything in the tree at the start
2196 * of the run (but not newly added entries), or it can be some target
2197 * number you'd like to process.
2198 */
2199int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2200 struct btrfs_root *root, unsigned long count)
2201{
2202 struct rb_node *node;
2203 struct btrfs_delayed_ref_root *delayed_refs;
2204 struct btrfs_delayed_ref_node *ref;
2205 struct list_head cluster;
2206 int ret;
2207 int run_all = count == (unsigned long)-1;
2208 int run_most = 0;
2209
2210 if (root == root->fs_info->extent_root)
2211 root = root->fs_info->tree_root;
2212
2213 delayed_refs = &trans->transaction->delayed_refs;
2214 INIT_LIST_HEAD(&cluster);
2215again:
2216 spin_lock(&delayed_refs->lock);
2217 if (count == 0) {
2218 count = delayed_refs->num_entries * 2;
2219 run_most = 1;
2220 }
2221 while (1) {
2222 if (!(run_all || run_most) &&
2223 delayed_refs->num_heads_ready < 64)
2224 break;
2225
2226 /*
2227 * go find something we can process in the rbtree. We start at
2228 * the beginning of the tree, and then build a cluster
2229 * of refs to process starting at the first one we are able to
2230 * lock
2231 */
2232 ret = btrfs_find_ref_cluster(trans, &cluster,
2233 delayed_refs->run_delayed_start);
2234 if (ret)
2235 break;
2236
2237 ret = run_clustered_refs(trans, root, &cluster);
2238 BUG_ON(ret < 0);
2239
2240 count -= min_t(unsigned long, ret, count);
2241
2242 if (count == 0)
2243 break;
2244 }
2245
Chris Mason56bec292009-03-13 10:10:06 -04002246 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002247 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002248 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002249 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002250 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002251
2252 while (node) {
2253 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2254 rb_node);
2255 if (btrfs_delayed_ref_is_head(ref)) {
2256 struct btrfs_delayed_ref_head *head;
2257
2258 head = btrfs_delayed_node_to_head(ref);
2259 atomic_inc(&ref->refs);
2260
2261 spin_unlock(&delayed_refs->lock);
2262 mutex_lock(&head->mutex);
2263 mutex_unlock(&head->mutex);
2264
2265 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002266 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002267 goto again;
2268 }
2269 node = rb_next(node);
2270 }
2271 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002272 schedule_timeout(1);
2273 goto again;
2274 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002275out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002276 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002277 return 0;
2278}
2279
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002280int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2281 struct btrfs_root *root,
2282 u64 bytenr, u64 num_bytes, u64 flags,
2283 int is_data)
2284{
2285 struct btrfs_delayed_extent_op *extent_op;
2286 int ret;
2287
2288 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2289 if (!extent_op)
2290 return -ENOMEM;
2291
2292 extent_op->flags_to_set = flags;
2293 extent_op->update_flags = 1;
2294 extent_op->update_key = 0;
2295 extent_op->is_data = is_data ? 1 : 0;
2296
2297 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2298 if (ret)
2299 kfree(extent_op);
2300 return ret;
2301}
2302
2303static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2304 struct btrfs_root *root,
2305 struct btrfs_path *path,
2306 u64 objectid, u64 offset, u64 bytenr)
2307{
2308 struct btrfs_delayed_ref_head *head;
2309 struct btrfs_delayed_ref_node *ref;
2310 struct btrfs_delayed_data_ref *data_ref;
2311 struct btrfs_delayed_ref_root *delayed_refs;
2312 struct rb_node *node;
2313 int ret = 0;
2314
2315 ret = -ENOENT;
2316 delayed_refs = &trans->transaction->delayed_refs;
2317 spin_lock(&delayed_refs->lock);
2318 head = btrfs_find_delayed_ref_head(trans, bytenr);
2319 if (!head)
2320 goto out;
2321
2322 if (!mutex_trylock(&head->mutex)) {
2323 atomic_inc(&head->node.refs);
2324 spin_unlock(&delayed_refs->lock);
2325
2326 btrfs_release_path(root->fs_info->extent_root, path);
2327
2328 mutex_lock(&head->mutex);
2329 mutex_unlock(&head->mutex);
2330 btrfs_put_delayed_ref(&head->node);
2331 return -EAGAIN;
2332 }
2333
2334 node = rb_prev(&head->node.rb_node);
2335 if (!node)
2336 goto out_unlock;
2337
2338 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2339
2340 if (ref->bytenr != bytenr)
2341 goto out_unlock;
2342
2343 ret = 1;
2344 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2345 goto out_unlock;
2346
2347 data_ref = btrfs_delayed_node_to_data_ref(ref);
2348
2349 node = rb_prev(node);
2350 if (node) {
2351 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2352 if (ref->bytenr == bytenr)
2353 goto out_unlock;
2354 }
2355
2356 if (data_ref->root != root->root_key.objectid ||
2357 data_ref->objectid != objectid || data_ref->offset != offset)
2358 goto out_unlock;
2359
2360 ret = 0;
2361out_unlock:
2362 mutex_unlock(&head->mutex);
2363out:
2364 spin_unlock(&delayed_refs->lock);
2365 return ret;
2366}
2367
2368static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2369 struct btrfs_root *root,
2370 struct btrfs_path *path,
2371 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002372{
2373 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002374 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002375 struct btrfs_extent_data_ref *ref;
2376 struct btrfs_extent_inline_ref *iref;
2377 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002378 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002379 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002380 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002381
Chris Masonbe20aa92007-12-17 20:14:01 -05002382 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002383 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002384 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002385
Chris Masonbe20aa92007-12-17 20:14:01 -05002386 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2387 if (ret < 0)
2388 goto out;
2389 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002390
2391 ret = -ENOENT;
2392 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002393 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002394
Zheng Yan31840ae2008-09-23 13:14:14 -04002395 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002396 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002397 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002398
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002399 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002400 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002401
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002402 ret = 1;
2403 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2404#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2405 if (item_size < sizeof(*ei)) {
2406 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2407 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002408 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002409#endif
2410 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2411
2412 if (item_size != sizeof(*ei) +
2413 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2414 goto out;
2415
2416 if (btrfs_extent_generation(leaf, ei) <=
2417 btrfs_root_last_snapshot(&root->root_item))
2418 goto out;
2419
2420 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2421 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2422 BTRFS_EXTENT_DATA_REF_KEY)
2423 goto out;
2424
2425 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2426 if (btrfs_extent_refs(leaf, ei) !=
2427 btrfs_extent_data_ref_count(leaf, ref) ||
2428 btrfs_extent_data_ref_root(leaf, ref) !=
2429 root->root_key.objectid ||
2430 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2431 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2432 goto out;
2433
Yan Zhengf321e492008-07-30 09:26:11 -04002434 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002435out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002436 return ret;
2437}
2438
2439int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2440 struct btrfs_root *root,
2441 u64 objectid, u64 offset, u64 bytenr)
2442{
2443 struct btrfs_path *path;
2444 int ret;
2445 int ret2;
2446
2447 path = btrfs_alloc_path();
2448 if (!path)
2449 return -ENOENT;
2450
2451 do {
2452 ret = check_committed_ref(trans, root, path, objectid,
2453 offset, bytenr);
2454 if (ret && ret != -ENOENT)
2455 goto out;
2456
2457 ret2 = check_delayed_ref(trans, root, path, objectid,
2458 offset, bytenr);
2459 } while (ret2 == -EAGAIN);
2460
2461 if (ret2 && ret2 != -ENOENT) {
2462 ret = ret2;
2463 goto out;
2464 }
2465
2466 if (ret != -ENOENT || ret2 != -ENOENT)
2467 ret = 0;
2468out:
Yan Zhengf321e492008-07-30 09:26:11 -04002469 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002470 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2471 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002472 return ret;
2473}
2474
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002475#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002476int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2477 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002478{
Chris Mason5f39d392007-10-15 16:14:19 -04002479 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002480 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002481 u64 root_gen;
2482 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002483 int i;
Chris Masondb945352007-10-15 16:15:53 -04002484 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002485 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002486 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002487
Chris Mason3768f362007-03-13 16:47:54 -04002488 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002489 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002490
Zheng Yane4657682008-09-26 10:04:53 -04002491 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2492 shared = 0;
2493 root_gen = root->root_key.offset;
2494 } else {
2495 shared = 1;
2496 root_gen = trans->transid - 1;
2497 }
2498
Chris Masondb945352007-10-15 16:15:53 -04002499 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002500 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002501
Zheng Yan31840ae2008-09-23 13:14:14 -04002502 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002503 struct btrfs_leaf_ref *ref;
2504 struct btrfs_extent_info *info;
2505
Zheng Yan31840ae2008-09-23 13:14:14 -04002506 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002507 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002508 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002509 goto out;
2510 }
2511
Zheng Yane4657682008-09-26 10:04:53 -04002512 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002513 ref->bytenr = buf->start;
2514 ref->owner = btrfs_header_owner(buf);
2515 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002516 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002517 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002518
Zheng Yan31840ae2008-09-23 13:14:14 -04002519 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002520 u64 disk_bytenr;
2521 btrfs_item_key_to_cpu(buf, &key, i);
2522 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2523 continue;
2524 fi = btrfs_item_ptr(buf, i,
2525 struct btrfs_file_extent_item);
2526 if (btrfs_file_extent_type(buf, fi) ==
2527 BTRFS_FILE_EXTENT_INLINE)
2528 continue;
2529 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2530 if (disk_bytenr == 0)
2531 continue;
2532
2533 info->bytenr = disk_bytenr;
2534 info->num_bytes =
2535 btrfs_file_extent_disk_num_bytes(buf, fi);
2536 info->objectid = key.objectid;
2537 info->offset = key.offset;
2538 info++;
2539 }
2540
Zheng Yane4657682008-09-26 10:04:53 -04002541 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002542 if (ret == -EEXIST && shared) {
2543 struct btrfs_leaf_ref *old;
2544 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2545 BUG_ON(!old);
2546 btrfs_remove_leaf_ref(root, old);
2547 btrfs_free_leaf_ref(root, old);
2548 ret = btrfs_add_leaf_ref(root, ref, shared);
2549 }
Yan Zheng31153d82008-07-28 15:32:19 -04002550 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002551 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002552 }
2553out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002554 return ret;
2555}
2556
Chris Masonb7a9f292009-02-04 09:23:45 -05002557/* when a block goes through cow, we update the reference counts of
2558 * everything that block points to. The internal pointers of the block
2559 * can be in just about any order, and it is likely to have clusters of
2560 * things that are close together and clusters of things that are not.
2561 *
2562 * To help reduce the seeks that come with updating all of these reference
2563 * counts, sort them by byte number before actual updates are done.
2564 *
2565 * struct refsort is used to match byte number to slot in the btree block.
2566 * we sort based on the byte number and then use the slot to actually
2567 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002568 *
2569 * struct refsort is smaller than strcut btrfs_item and smaller than
2570 * struct btrfs_key_ptr. Since we're currently limited to the page size
2571 * for a btree block, there's no way for a kmalloc of refsorts for a
2572 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002573 */
2574struct refsort {
2575 u64 bytenr;
2576 u32 slot;
2577};
2578
2579/*
2580 * for passing into sort()
2581 */
2582static int refsort_cmp(const void *a_void, const void *b_void)
2583{
2584 const struct refsort *a = a_void;
2585 const struct refsort *b = b_void;
2586
2587 if (a->bytenr < b->bytenr)
2588 return -1;
2589 if (a->bytenr > b->bytenr)
2590 return 1;
2591 return 0;
2592}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002593#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002594
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002595static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002596 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002597 struct extent_buffer *buf,
2598 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002599{
2600 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002601 u64 num_bytes;
2602 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002603 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002604 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002605 struct btrfs_key key;
2606 struct btrfs_file_extent_item *fi;
2607 int i;
2608 int level;
2609 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002610 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002611 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002612
2613 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002614 nritems = btrfs_header_nritems(buf);
2615 level = btrfs_header_level(buf);
2616
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002617 if (!root->ref_cows && level == 0)
2618 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002619
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002620 if (inc)
2621 process_func = btrfs_inc_extent_ref;
2622 else
2623 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002624
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002625 if (full_backref)
2626 parent = buf->start;
2627 else
2628 parent = 0;
2629
Zheng Yan31840ae2008-09-23 13:14:14 -04002630 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002631 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002632 btrfs_item_key_to_cpu(buf, &key, i);
2633 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002634 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002635 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002636 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002637 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002638 BTRFS_FILE_EXTENT_INLINE)
2639 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002640 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2641 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002642 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002643
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002644 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2645 key.offset -= btrfs_file_extent_offset(buf, fi);
2646 ret = process_func(trans, root, bytenr, num_bytes,
2647 parent, ref_root, key.objectid,
2648 key.offset);
2649 if (ret)
2650 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002651 } else {
2652 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002653 num_bytes = btrfs_level_size(root, level - 1);
2654 ret = process_func(trans, root, bytenr, num_bytes,
2655 parent, ref_root, level - 1, 0);
2656 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002657 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002658 }
2659 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002660 return 0;
2661fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002662 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002663 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002664}
2665
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002666int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2667 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002668{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002669 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2670}
Zheng Yan31840ae2008-09-23 13:14:14 -04002671
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002672int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2673 struct extent_buffer *buf, int full_backref)
2674{
2675 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002676}
2677
Chris Mason9078a3e2007-04-26 16:46:15 -04002678static int write_one_cache_group(struct btrfs_trans_handle *trans,
2679 struct btrfs_root *root,
2680 struct btrfs_path *path,
2681 struct btrfs_block_group_cache *cache)
2682{
2683 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002684 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002685 unsigned long bi;
2686 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002687
Chris Mason9078a3e2007-04-26 16:46:15 -04002688 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002689 if (ret < 0)
2690 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002691 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002692
2693 leaf = path->nodes[0];
2694 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2695 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2696 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002697 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002698fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002699 if (ret)
2700 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002701 return 0;
2702
2703}
2704
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002705static struct btrfs_block_group_cache *
2706next_block_group(struct btrfs_root *root,
2707 struct btrfs_block_group_cache *cache)
2708{
2709 struct rb_node *node;
2710 spin_lock(&root->fs_info->block_group_cache_lock);
2711 node = rb_next(&cache->cache_node);
2712 btrfs_put_block_group(cache);
2713 if (node) {
2714 cache = rb_entry(node, struct btrfs_block_group_cache,
2715 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002716 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002717 } else
2718 cache = NULL;
2719 spin_unlock(&root->fs_info->block_group_cache_lock);
2720 return cache;
2721}
2722
Josef Bacik0af3d002010-06-21 14:48:16 -04002723static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2724 struct btrfs_trans_handle *trans,
2725 struct btrfs_path *path)
2726{
2727 struct btrfs_root *root = block_group->fs_info->tree_root;
2728 struct inode *inode = NULL;
2729 u64 alloc_hint = 0;
2730 int num_pages = 0;
2731 int retries = 0;
2732 int ret = 0;
2733
2734 /*
2735 * If this block group is smaller than 100 megs don't bother caching the
2736 * block group.
2737 */
2738 if (block_group->key.offset < (100 * 1024 * 1024)) {
2739 spin_lock(&block_group->lock);
2740 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2741 spin_unlock(&block_group->lock);
2742 return 0;
2743 }
2744
2745again:
2746 inode = lookup_free_space_inode(root, block_group, path);
2747 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2748 ret = PTR_ERR(inode);
2749 btrfs_release_path(root, path);
2750 goto out;
2751 }
2752
2753 if (IS_ERR(inode)) {
2754 BUG_ON(retries);
2755 retries++;
2756
2757 if (block_group->ro)
2758 goto out_free;
2759
2760 ret = create_free_space_inode(root, trans, block_group, path);
2761 if (ret)
2762 goto out_free;
2763 goto again;
2764 }
2765
2766 /*
2767 * We want to set the generation to 0, that way if anything goes wrong
2768 * from here on out we know not to trust this cache when we load up next
2769 * time.
2770 */
2771 BTRFS_I(inode)->generation = 0;
2772 ret = btrfs_update_inode(trans, root, inode);
2773 WARN_ON(ret);
2774
2775 if (i_size_read(inode) > 0) {
2776 ret = btrfs_truncate_free_space_cache(root, trans, path,
2777 inode);
2778 if (ret)
2779 goto out_put;
2780 }
2781
2782 spin_lock(&block_group->lock);
2783 if (block_group->cached != BTRFS_CACHE_FINISHED) {
2784 spin_unlock(&block_group->lock);
2785 goto out_put;
2786 }
2787 spin_unlock(&block_group->lock);
2788
2789 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2790 if (!num_pages)
2791 num_pages = 1;
2792
2793 /*
2794 * Just to make absolutely sure we have enough space, we're going to
2795 * preallocate 12 pages worth of space for each block group. In
2796 * practice we ought to use at most 8, but we need extra space so we can
2797 * add our header and have a terminator between the extents and the
2798 * bitmaps.
2799 */
2800 num_pages *= 16;
2801 num_pages *= PAGE_CACHE_SIZE;
2802
2803 ret = btrfs_check_data_free_space(inode, num_pages);
2804 if (ret)
2805 goto out_put;
2806
2807 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2808 num_pages, num_pages,
2809 &alloc_hint);
2810 btrfs_free_reserved_data_space(inode, num_pages);
2811out_put:
2812 iput(inode);
2813out_free:
2814 btrfs_release_path(root, path);
2815out:
2816 spin_lock(&block_group->lock);
2817 if (ret)
2818 block_group->disk_cache_state = BTRFS_DC_ERROR;
2819 else
2820 block_group->disk_cache_state = BTRFS_DC_SETUP;
2821 spin_unlock(&block_group->lock);
2822
2823 return ret;
2824}
2825
Chris Mason96b51792007-10-15 16:15:19 -04002826int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2827 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002828{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002829 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002830 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002831 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002832 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002833
2834 path = btrfs_alloc_path();
2835 if (!path)
2836 return -ENOMEM;
2837
Josef Bacik0af3d002010-06-21 14:48:16 -04002838again:
2839 while (1) {
2840 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2841 while (cache) {
2842 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2843 break;
2844 cache = next_block_group(root, cache);
2845 }
2846 if (!cache) {
2847 if (last == 0)
2848 break;
2849 last = 0;
2850 continue;
2851 }
2852 err = cache_save_setup(cache, trans, path);
2853 last = cache->key.objectid + cache->key.offset;
2854 btrfs_put_block_group(cache);
2855 }
2856
Chris Masond3977122009-01-05 21:25:51 -05002857 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002858 if (last == 0) {
2859 err = btrfs_run_delayed_refs(trans, root,
2860 (unsigned long)-1);
2861 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002862 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002863
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002864 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2865 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002866 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2867 btrfs_put_block_group(cache);
2868 goto again;
2869 }
2870
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002871 if (cache->dirty)
2872 break;
2873 cache = next_block_group(root, cache);
2874 }
2875 if (!cache) {
2876 if (last == 0)
2877 break;
2878 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002879 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002880 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002881
Josef Bacik0cb59c92010-07-02 12:14:14 -04002882 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2883 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002884 cache->dirty = 0;
2885 last = cache->key.objectid + cache->key.offset;
2886
2887 err = write_one_cache_group(trans, root, path, cache);
2888 BUG_ON(err);
2889 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002890 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002891
Josef Bacik0cb59c92010-07-02 12:14:14 -04002892 while (1) {
2893 /*
2894 * I don't think this is needed since we're just marking our
2895 * preallocated extent as written, but just in case it can't
2896 * hurt.
2897 */
2898 if (last == 0) {
2899 err = btrfs_run_delayed_refs(trans, root,
2900 (unsigned long)-1);
2901 BUG_ON(err);
2902 }
2903
2904 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2905 while (cache) {
2906 /*
2907 * Really this shouldn't happen, but it could if we
2908 * couldn't write the entire preallocated extent and
2909 * splitting the extent resulted in a new block.
2910 */
2911 if (cache->dirty) {
2912 btrfs_put_block_group(cache);
2913 goto again;
2914 }
2915 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2916 break;
2917 cache = next_block_group(root, cache);
2918 }
2919 if (!cache) {
2920 if (last == 0)
2921 break;
2922 last = 0;
2923 continue;
2924 }
2925
2926 btrfs_write_out_cache(root, trans, cache, path);
2927
2928 /*
2929 * If we didn't have an error then the cache state is still
2930 * NEED_WRITE, so we can set it to WRITTEN.
2931 */
2932 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2933 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2934 last = cache->key.objectid + cache->key.offset;
2935 btrfs_put_block_group(cache);
2936 }
2937
Chris Mason9078a3e2007-04-26 16:46:15 -04002938 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002939 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002940}
2941
Yan Zhengd2fb3432008-12-11 16:30:39 -05002942int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2943{
2944 struct btrfs_block_group_cache *block_group;
2945 int readonly = 0;
2946
2947 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2948 if (!block_group || block_group->ro)
2949 readonly = 1;
2950 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002951 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002952 return readonly;
2953}
2954
Chris Mason593060d2008-03-25 16:50:33 -04002955static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2956 u64 total_bytes, u64 bytes_used,
2957 struct btrfs_space_info **space_info)
2958{
2959 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002960 int i;
2961 int factor;
2962
2963 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2964 BTRFS_BLOCK_GROUP_RAID10))
2965 factor = 2;
2966 else
2967 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002968
2969 found = __find_space_info(info, flags);
2970 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002971 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002972 found->total_bytes += total_bytes;
2973 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002974 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002975 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002976 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002977 *space_info = found;
2978 return 0;
2979 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002980 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002981 if (!found)
2982 return -ENOMEM;
2983
Yan, Zhengb742bb82010-05-16 10:46:24 -04002984 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2985 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04002986 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002987 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04002988 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2989 BTRFS_BLOCK_GROUP_SYSTEM |
2990 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04002991 found->total_bytes = total_bytes;
2992 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002993 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002994 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002995 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002996 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002997 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002998 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002999 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003000 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003001 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04003002 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04003003 return 0;
3004}
3005
Chris Mason8790d502008-04-03 16:29:03 -04003006static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3007{
3008 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04003009 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04003010 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04003011 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04003012 if (extra_flags) {
3013 if (flags & BTRFS_BLOCK_GROUP_DATA)
3014 fs_info->avail_data_alloc_bits |= extra_flags;
3015 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3016 fs_info->avail_metadata_alloc_bits |= extra_flags;
3017 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3018 fs_info->avail_system_alloc_bits |= extra_flags;
3019 }
3020}
Chris Mason593060d2008-03-25 16:50:33 -04003021
Yan Zheng2b820322008-11-17 21:11:30 -05003022u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003023{
Yan Zheng2b820322008-11-17 21:11:30 -05003024 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04003025
3026 if (num_devices == 1)
3027 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3028 if (num_devices < 4)
3029 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3030
Chris Masonec44a352008-04-28 15:29:52 -04003031 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3032 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003033 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003034 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003035 }
Chris Masonec44a352008-04-28 15:29:52 -04003036
3037 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003038 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003039 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003040 }
Chris Masonec44a352008-04-28 15:29:52 -04003041
3042 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3043 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3044 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3045 (flags & BTRFS_BLOCK_GROUP_DUP)))
3046 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3047 return flags;
3048}
3049
Yan, Zhengb742bb82010-05-16 10:46:24 -04003050static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003051{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003052 if (flags & BTRFS_BLOCK_GROUP_DATA)
3053 flags |= root->fs_info->avail_data_alloc_bits &
3054 root->fs_info->data_alloc_profile;
3055 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3056 flags |= root->fs_info->avail_system_alloc_bits &
3057 root->fs_info->system_alloc_profile;
3058 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3059 flags |= root->fs_info->avail_metadata_alloc_bits &
3060 root->fs_info->metadata_alloc_profile;
3061 return btrfs_reduce_alloc_profile(root, flags);
3062}
Josef Bacik6a632092009-02-20 11:00:09 -05003063
Yan, Zhengb742bb82010-05-16 10:46:24 -04003064static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
3065{
3066 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003067
Yan, Zhengb742bb82010-05-16 10:46:24 -04003068 if (data)
3069 flags = BTRFS_BLOCK_GROUP_DATA;
3070 else if (root == root->fs_info->chunk_root)
3071 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3072 else
3073 flags = BTRFS_BLOCK_GROUP_METADATA;
3074
3075 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003076}
3077
3078void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3079{
Josef Bacik6a632092009-02-20 11:00:09 -05003080 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003081 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003082}
3083
3084/*
3085 * This will check the space that the inode allocates from to make sure we have
3086 * enough space for bytes.
3087 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003088int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003089{
3090 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003091 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003092 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003093 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003094
3095 /* make sure bytes are sectorsize aligned */
3096 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3097
Josef Bacik0af3d002010-06-21 14:48:16 -04003098 if (root == root->fs_info->tree_root) {
3099 alloc_chunk = 0;
3100 committed = 1;
3101 }
3102
Josef Bacik6a632092009-02-20 11:00:09 -05003103 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003104 if (!data_sinfo)
3105 goto alloc;
3106
Josef Bacik6a632092009-02-20 11:00:09 -05003107again:
3108 /* make sure we have enough space to handle the data first */
3109 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003110 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3111 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3112 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003113
3114 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003115 struct btrfs_trans_handle *trans;
3116
Josef Bacik6a632092009-02-20 11:00:09 -05003117 /*
3118 * if we don't have enough free bytes in this space then we need
3119 * to alloc a new chunk.
3120 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003121 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003122 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003123
3124 data_sinfo->force_alloc = 1;
3125 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003126alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003127 alloc_target = btrfs_get_alloc_profile(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003128 trans = btrfs_join_transaction(root, 1);
3129 if (IS_ERR(trans))
3130 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003131
3132 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3133 bytes + 2 * 1024 * 1024,
3134 alloc_target, 0);
3135 btrfs_end_transaction(trans, root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003136 if (ret < 0)
Josef Bacik6a632092009-02-20 11:00:09 -05003137 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04003138
3139 if (!data_sinfo) {
3140 btrfs_set_inode_space_info(root, inode);
3141 data_sinfo = BTRFS_I(inode)->space_info;
3142 }
Josef Bacik6a632092009-02-20 11:00:09 -05003143 goto again;
3144 }
3145 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003146
3147 /* commit the current transaction and try again */
Sage Weildd7e0b72009-09-29 18:38:44 -04003148 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003149 committed = 1;
3150 trans = btrfs_join_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003151 if (IS_ERR(trans))
3152 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003153 ret = btrfs_commit_transaction(trans, root);
3154 if (ret)
3155 return ret;
3156 goto again;
3157 }
3158
Chris Mason933b5852010-05-26 11:31:00 -04003159#if 0 /* I hope we never need this code again, just in case */
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003160 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3161 "%llu bytes_reserved, " "%llu bytes_pinned, "
3162 "%llu bytes_readonly, %llu may use %llu total\n",
3163 (unsigned long long)bytes,
Joel Becker21380932009-04-21 12:38:29 -07003164 (unsigned long long)data_sinfo->bytes_used,
3165 (unsigned long long)data_sinfo->bytes_reserved,
3166 (unsigned long long)data_sinfo->bytes_pinned,
3167 (unsigned long long)data_sinfo->bytes_readonly,
3168 (unsigned long long)data_sinfo->bytes_may_use,
3169 (unsigned long long)data_sinfo->total_bytes);
Chris Mason933b5852010-05-26 11:31:00 -04003170#endif
Josef Bacik6a632092009-02-20 11:00:09 -05003171 return -ENOSPC;
3172 }
3173 data_sinfo->bytes_may_use += bytes;
3174 BTRFS_I(inode)->reserved_bytes += bytes;
3175 spin_unlock(&data_sinfo->lock);
3176
Josef Bacik9ed74f22009-09-11 16:12:44 -04003177 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003178}
3179
3180/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003181 * called when we are clearing an delalloc extent from the
3182 * inode's io_tree or there was an error for whatever reason
3183 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003184 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003185void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003186{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003187 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003188 struct btrfs_space_info *data_sinfo;
3189
3190 /* make sure bytes are sectorsize aligned */
3191 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3192
3193 data_sinfo = BTRFS_I(inode)->space_info;
3194 spin_lock(&data_sinfo->lock);
3195 data_sinfo->bytes_may_use -= bytes;
3196 BTRFS_I(inode)->reserved_bytes -= bytes;
3197 spin_unlock(&data_sinfo->lock);
3198}
3199
Josef Bacik97e728d2009-04-21 17:40:57 -04003200static void force_metadata_allocation(struct btrfs_fs_info *info)
3201{
3202 struct list_head *head = &info->space_info;
3203 struct btrfs_space_info *found;
3204
3205 rcu_read_lock();
3206 list_for_each_entry_rcu(found, head, list) {
3207 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3208 found->force_alloc = 1;
3209 }
3210 rcu_read_unlock();
3211}
3212
Yan, Zheng424499d2010-05-16 10:46:25 -04003213static int should_alloc_chunk(struct btrfs_space_info *sinfo,
3214 u64 alloc_bytes)
3215{
3216 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3217
3218 if (sinfo->bytes_used + sinfo->bytes_reserved +
3219 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3220 return 0;
3221
3222 if (sinfo->bytes_used + sinfo->bytes_reserved +
3223 alloc_bytes < div_factor(num_bytes, 8))
3224 return 0;
3225
3226 return 1;
3227}
3228
Chris Mason6324fbf2008-03-24 15:01:59 -04003229static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3230 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003231 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003232{
3233 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003234 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Yan Zhengc146afa2008-11-12 14:34:12 -05003235 int ret = 0;
3236
Josef Bacik97e728d2009-04-21 17:40:57 -04003237 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003238
Yan Zheng2b820322008-11-17 21:11:30 -05003239 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003240
Chris Mason6324fbf2008-03-24 15:01:59 -04003241 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003242 if (!space_info) {
3243 ret = update_space_info(extent_root->fs_info, flags,
3244 0, 0, &space_info);
3245 BUG_ON(ret);
3246 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003247 BUG_ON(!space_info);
3248
Josef Bacik25179202008-10-29 14:49:05 -04003249 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003250 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003251 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003252 if (space_info->full) {
3253 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003254 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003255 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003256
Yan, Zheng424499d2010-05-16 10:46:25 -04003257 if (!force && !should_alloc_chunk(space_info, alloc_bytes)) {
Josef Bacik25179202008-10-29 14:49:05 -04003258 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003259 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003260 }
Josef Bacik25179202008-10-29 14:49:05 -04003261 spin_unlock(&space_info->lock);
3262
Josef Bacik97e728d2009-04-21 17:40:57 -04003263 /*
3264 * if we're doing a data chunk, go ahead and make sure that
3265 * we keep a reasonable number of metadata chunks allocated in the
3266 * FS as well.
3267 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003268 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003269 fs_info->data_chunk_allocations++;
3270 if (!(fs_info->data_chunk_allocations %
3271 fs_info->metadata_ratio))
3272 force_metadata_allocation(fs_info);
3273 }
3274
Yan Zheng2b820322008-11-17 21:11:30 -05003275 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003276 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003277 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003278 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003279 else
3280 ret = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003281 space_info->force_alloc = 0;
3282 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003283out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003284 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003285 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003286}
3287
Yan, Zheng424499d2010-05-16 10:46:25 -04003288static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
3289 struct btrfs_root *root,
3290 struct btrfs_space_info *sinfo, u64 num_bytes)
3291{
3292 int ret;
3293 int end_trans = 0;
3294
3295 if (sinfo->full)
3296 return 0;
3297
3298 spin_lock(&sinfo->lock);
3299 ret = should_alloc_chunk(sinfo, num_bytes + 2 * 1024 * 1024);
3300 spin_unlock(&sinfo->lock);
3301 if (!ret)
3302 return 0;
3303
3304 if (!trans) {
3305 trans = btrfs_join_transaction(root, 1);
3306 BUG_ON(IS_ERR(trans));
3307 end_trans = 1;
3308 }
3309
3310 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3311 num_bytes + 2 * 1024 * 1024,
3312 get_alloc_profile(root, sinfo->flags), 0);
3313
3314 if (end_trans)
3315 btrfs_end_transaction(trans, root);
3316
3317 return ret == 1 ? 1 : 0;
3318}
3319
Yan, Zheng5da9d012010-05-16 10:46:25 -04003320/*
3321 * shrink metadata reservation for delalloc
3322 */
3323static int shrink_delalloc(struct btrfs_trans_handle *trans,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003324 struct btrfs_root *root, u64 to_reclaim)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003325{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003326 struct btrfs_block_rsv *block_rsv;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003327 u64 reserved;
3328 u64 max_reclaim;
3329 u64 reclaimed = 0;
3330 int pause = 1;
3331 int ret;
3332
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003333 block_rsv = &root->fs_info->delalloc_block_rsv;
3334 spin_lock(&block_rsv->lock);
3335 reserved = block_rsv->reserved;
3336 spin_unlock(&block_rsv->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003337
3338 if (reserved == 0)
3339 return 0;
3340
3341 max_reclaim = min(reserved, to_reclaim);
3342
3343 while (1) {
3344 ret = btrfs_start_one_delalloc_inode(root, trans ? 1 : 0);
3345 if (!ret) {
3346 __set_current_state(TASK_INTERRUPTIBLE);
3347 schedule_timeout(pause);
3348 pause <<= 1;
3349 if (pause > HZ / 10)
3350 pause = HZ / 10;
3351 } else {
3352 pause = 1;
3353 }
3354
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003355 spin_lock(&block_rsv->lock);
3356 if (reserved > block_rsv->reserved)
3357 reclaimed = reserved - block_rsv->reserved;
3358 reserved = block_rsv->reserved;
3359 spin_unlock(&block_rsv->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003360
3361 if (reserved == 0 || reclaimed >= max_reclaim)
3362 break;
3363
3364 if (trans && trans->transaction->blocked)
3365 return -EAGAIN;
3366 }
3367 return reclaimed >= to_reclaim;
3368}
3369
Yan, Zhengf0486c62010-05-16 10:46:25 -04003370static int should_retry_reserve(struct btrfs_trans_handle *trans,
3371 struct btrfs_root *root,
3372 struct btrfs_block_rsv *block_rsv,
3373 u64 num_bytes, int *retries)
3374{
3375 struct btrfs_space_info *space_info = block_rsv->space_info;
3376 int ret;
3377
3378 if ((*retries) > 2)
3379 return -ENOSPC;
3380
3381 ret = maybe_allocate_chunk(trans, root, space_info, num_bytes);
3382 if (ret)
3383 return 1;
3384
3385 if (trans && trans->transaction->in_commit)
3386 return -ENOSPC;
3387
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003388 ret = shrink_delalloc(trans, root, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003389 if (ret)
3390 return ret;
3391
3392 spin_lock(&space_info->lock);
3393 if (space_info->bytes_pinned < num_bytes)
3394 ret = 1;
3395 spin_unlock(&space_info->lock);
3396 if (ret)
3397 return -ENOSPC;
3398
3399 (*retries)++;
3400
3401 if (trans)
3402 return -EAGAIN;
3403
3404 trans = btrfs_join_transaction(root, 1);
3405 BUG_ON(IS_ERR(trans));
3406 ret = btrfs_commit_transaction(trans, root);
3407 BUG_ON(ret);
3408
3409 return 1;
3410}
3411
3412static int reserve_metadata_bytes(struct btrfs_block_rsv *block_rsv,
3413 u64 num_bytes)
3414{
3415 struct btrfs_space_info *space_info = block_rsv->space_info;
3416 u64 unused;
3417 int ret = -ENOSPC;
3418
3419 spin_lock(&space_info->lock);
3420 unused = space_info->bytes_used + space_info->bytes_reserved +
3421 space_info->bytes_pinned + space_info->bytes_readonly;
3422
3423 if (unused < space_info->total_bytes)
3424 unused = space_info->total_bytes - unused;
3425 else
3426 unused = 0;
3427
3428 if (unused >= num_bytes) {
3429 if (block_rsv->priority >= 10) {
3430 space_info->bytes_reserved += num_bytes;
3431 ret = 0;
3432 } else {
3433 if ((unused + block_rsv->reserved) *
3434 block_rsv->priority >=
3435 (num_bytes + block_rsv->reserved) * 10) {
3436 space_info->bytes_reserved += num_bytes;
3437 ret = 0;
3438 }
3439 }
3440 }
3441 spin_unlock(&space_info->lock);
3442
3443 return ret;
3444}
3445
3446static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3447 struct btrfs_root *root)
3448{
3449 struct btrfs_block_rsv *block_rsv;
3450 if (root->ref_cows)
3451 block_rsv = trans->block_rsv;
3452 else
3453 block_rsv = root->block_rsv;
3454
3455 if (!block_rsv)
3456 block_rsv = &root->fs_info->empty_block_rsv;
3457
3458 return block_rsv;
3459}
3460
3461static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3462 u64 num_bytes)
3463{
3464 int ret = -ENOSPC;
3465 spin_lock(&block_rsv->lock);
3466 if (block_rsv->reserved >= num_bytes) {
3467 block_rsv->reserved -= num_bytes;
3468 if (block_rsv->reserved < block_rsv->size)
3469 block_rsv->full = 0;
3470 ret = 0;
3471 }
3472 spin_unlock(&block_rsv->lock);
3473 return ret;
3474}
3475
3476static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3477 u64 num_bytes, int update_size)
3478{
3479 spin_lock(&block_rsv->lock);
3480 block_rsv->reserved += num_bytes;
3481 if (update_size)
3482 block_rsv->size += num_bytes;
3483 else if (block_rsv->reserved >= block_rsv->size)
3484 block_rsv->full = 1;
3485 spin_unlock(&block_rsv->lock);
3486}
3487
3488void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3489 struct btrfs_block_rsv *dest, u64 num_bytes)
3490{
3491 struct btrfs_space_info *space_info = block_rsv->space_info;
3492
3493 spin_lock(&block_rsv->lock);
3494 if (num_bytes == (u64)-1)
3495 num_bytes = block_rsv->size;
3496 block_rsv->size -= num_bytes;
3497 if (block_rsv->reserved >= block_rsv->size) {
3498 num_bytes = block_rsv->reserved - block_rsv->size;
3499 block_rsv->reserved = block_rsv->size;
3500 block_rsv->full = 1;
3501 } else {
3502 num_bytes = 0;
3503 }
3504 spin_unlock(&block_rsv->lock);
3505
3506 if (num_bytes > 0) {
3507 if (dest) {
3508 block_rsv_add_bytes(dest, num_bytes, 0);
3509 } else {
3510 spin_lock(&space_info->lock);
3511 space_info->bytes_reserved -= num_bytes;
3512 spin_unlock(&space_info->lock);
3513 }
3514 }
3515}
3516
3517static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3518 struct btrfs_block_rsv *dst, u64 num_bytes)
3519{
3520 int ret;
3521
3522 ret = block_rsv_use_bytes(src, num_bytes);
3523 if (ret)
3524 return ret;
3525
3526 block_rsv_add_bytes(dst, num_bytes, 1);
3527 return 0;
3528}
3529
3530void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3531{
3532 memset(rsv, 0, sizeof(*rsv));
3533 spin_lock_init(&rsv->lock);
3534 atomic_set(&rsv->usage, 1);
3535 rsv->priority = 6;
3536 INIT_LIST_HEAD(&rsv->list);
3537}
3538
3539struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3540{
3541 struct btrfs_block_rsv *block_rsv;
3542 struct btrfs_fs_info *fs_info = root->fs_info;
3543 u64 alloc_target;
3544
3545 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3546 if (!block_rsv)
3547 return NULL;
3548
3549 btrfs_init_block_rsv(block_rsv);
3550
3551 alloc_target = btrfs_get_alloc_profile(root, 0);
3552 block_rsv->space_info = __find_space_info(fs_info,
3553 BTRFS_BLOCK_GROUP_METADATA);
3554
3555 return block_rsv;
3556}
3557
3558void btrfs_free_block_rsv(struct btrfs_root *root,
3559 struct btrfs_block_rsv *rsv)
3560{
3561 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3562 btrfs_block_rsv_release(root, rsv, (u64)-1);
3563 if (!rsv->durable)
3564 kfree(rsv);
3565 }
3566}
3567
3568/*
3569 * make the block_rsv struct be able to capture freed space.
3570 * the captured space will re-add to the the block_rsv struct
3571 * after transaction commit
3572 */
3573void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3574 struct btrfs_block_rsv *block_rsv)
3575{
3576 block_rsv->durable = 1;
3577 mutex_lock(&fs_info->durable_block_rsv_mutex);
3578 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3579 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3580}
3581
3582int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3583 struct btrfs_root *root,
3584 struct btrfs_block_rsv *block_rsv,
3585 u64 num_bytes, int *retries)
3586{
3587 int ret;
3588
3589 if (num_bytes == 0)
3590 return 0;
3591again:
3592 ret = reserve_metadata_bytes(block_rsv, num_bytes);
3593 if (!ret) {
3594 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3595 return 0;
3596 }
3597
3598 ret = should_retry_reserve(trans, root, block_rsv, num_bytes, retries);
3599 if (ret > 0)
3600 goto again;
3601
3602 return ret;
3603}
3604
3605int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3606 struct btrfs_root *root,
3607 struct btrfs_block_rsv *block_rsv,
3608 u64 min_reserved, int min_factor)
3609{
3610 u64 num_bytes = 0;
3611 int commit_trans = 0;
3612 int ret = -ENOSPC;
3613
3614 if (!block_rsv)
3615 return 0;
3616
3617 spin_lock(&block_rsv->lock);
3618 if (min_factor > 0)
3619 num_bytes = div_factor(block_rsv->size, min_factor);
3620 if (min_reserved > num_bytes)
3621 num_bytes = min_reserved;
3622
3623 if (block_rsv->reserved >= num_bytes) {
3624 ret = 0;
3625 } else {
3626 num_bytes -= block_rsv->reserved;
3627 if (block_rsv->durable &&
3628 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3629 commit_trans = 1;
3630 }
3631 spin_unlock(&block_rsv->lock);
3632 if (!ret)
3633 return 0;
3634
3635 if (block_rsv->refill_used) {
3636 ret = reserve_metadata_bytes(block_rsv, num_bytes);
3637 if (!ret) {
3638 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3639 return 0;
3640 }
3641 }
3642
3643 if (commit_trans) {
3644 if (trans)
3645 return -EAGAIN;
3646
3647 trans = btrfs_join_transaction(root, 1);
3648 BUG_ON(IS_ERR(trans));
3649 ret = btrfs_commit_transaction(trans, root);
3650 return 0;
3651 }
3652
3653 WARN_ON(1);
3654 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
3655 block_rsv->size, block_rsv->reserved,
3656 block_rsv->freed[0], block_rsv->freed[1]);
3657
3658 return -ENOSPC;
3659}
3660
3661int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3662 struct btrfs_block_rsv *dst_rsv,
3663 u64 num_bytes)
3664{
3665 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3666}
3667
3668void btrfs_block_rsv_release(struct btrfs_root *root,
3669 struct btrfs_block_rsv *block_rsv,
3670 u64 num_bytes)
3671{
3672 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3673 if (global_rsv->full || global_rsv == block_rsv ||
3674 block_rsv->space_info != global_rsv->space_info)
3675 global_rsv = NULL;
3676 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3677}
3678
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003679/*
3680 * helper to calculate size of global block reservation.
3681 * the desired value is sum of space used by extent tree,
3682 * checksum tree and root tree
3683 */
3684static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3685{
3686 struct btrfs_space_info *sinfo;
3687 u64 num_bytes;
3688 u64 meta_used;
3689 u64 data_used;
3690 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3691#if 0
3692 /*
3693 * per tree used space accounting can be inaccuracy, so we
3694 * can't rely on it.
3695 */
3696 spin_lock(&fs_info->extent_root->accounting_lock);
3697 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3698 spin_unlock(&fs_info->extent_root->accounting_lock);
3699
3700 spin_lock(&fs_info->csum_root->accounting_lock);
3701 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3702 spin_unlock(&fs_info->csum_root->accounting_lock);
3703
3704 spin_lock(&fs_info->tree_root->accounting_lock);
3705 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3706 spin_unlock(&fs_info->tree_root->accounting_lock);
3707#endif
3708 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3709 spin_lock(&sinfo->lock);
3710 data_used = sinfo->bytes_used;
3711 spin_unlock(&sinfo->lock);
3712
3713 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3714 spin_lock(&sinfo->lock);
3715 meta_used = sinfo->bytes_used;
3716 spin_unlock(&sinfo->lock);
3717
3718 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3719 csum_size * 2;
3720 num_bytes += div64_u64(data_used + meta_used, 50);
3721
3722 if (num_bytes * 3 > meta_used)
3723 num_bytes = div64_u64(meta_used, 3);
3724
3725 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3726}
3727
3728static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3729{
3730 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3731 struct btrfs_space_info *sinfo = block_rsv->space_info;
3732 u64 num_bytes;
3733
3734 num_bytes = calc_global_metadata_size(fs_info);
3735
3736 spin_lock(&block_rsv->lock);
3737 spin_lock(&sinfo->lock);
3738
3739 block_rsv->size = num_bytes;
3740
3741 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
3742 sinfo->bytes_reserved + sinfo->bytes_readonly;
3743
3744 if (sinfo->total_bytes > num_bytes) {
3745 num_bytes = sinfo->total_bytes - num_bytes;
3746 block_rsv->reserved += num_bytes;
3747 sinfo->bytes_reserved += num_bytes;
3748 }
3749
3750 if (block_rsv->reserved >= block_rsv->size) {
3751 num_bytes = block_rsv->reserved - block_rsv->size;
3752 sinfo->bytes_reserved -= num_bytes;
3753 block_rsv->reserved = block_rsv->size;
3754 block_rsv->full = 1;
3755 }
3756#if 0
3757 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3758 block_rsv->size, block_rsv->reserved);
3759#endif
3760 spin_unlock(&sinfo->lock);
3761 spin_unlock(&block_rsv->lock);
3762}
3763
Yan, Zhengf0486c62010-05-16 10:46:25 -04003764static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3765{
3766 struct btrfs_space_info *space_info;
3767
3768 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3769 fs_info->chunk_block_rsv.space_info = space_info;
3770 fs_info->chunk_block_rsv.priority = 10;
3771
3772 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003773 fs_info->global_block_rsv.space_info = space_info;
3774 fs_info->global_block_rsv.priority = 10;
3775 fs_info->global_block_rsv.refill_used = 1;
3776 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003777 fs_info->trans_block_rsv.space_info = space_info;
3778 fs_info->empty_block_rsv.space_info = space_info;
3779 fs_info->empty_block_rsv.priority = 10;
3780
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003781 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3782 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3783 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3784 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003785 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003786
3787 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3788
3789 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3790
3791 update_global_block_rsv(fs_info);
3792}
3793
3794static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3795{
3796 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3797 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3798 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3799 WARN_ON(fs_info->trans_block_rsv.size > 0);
3800 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3801 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3802 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003803}
3804
Yan, Zhenga22285a2010-05-16 10:48:46 -04003805static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3806{
3807 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3808 3 * num_items;
3809}
3810
3811int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3812 struct btrfs_root *root,
3813 int num_items, int *retries)
3814{
3815 u64 num_bytes;
3816 int ret;
3817
3818 if (num_items == 0 || root->fs_info->chunk_root == root)
3819 return 0;
3820
3821 num_bytes = calc_trans_metadata_size(root, num_items);
3822 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
3823 num_bytes, retries);
3824 if (!ret) {
3825 trans->bytes_reserved += num_bytes;
3826 trans->block_rsv = &root->fs_info->trans_block_rsv;
3827 }
3828 return ret;
3829}
3830
3831void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3832 struct btrfs_root *root)
3833{
3834 if (!trans->bytes_reserved)
3835 return;
3836
3837 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3838 btrfs_block_rsv_release(root, trans->block_rsv,
3839 trans->bytes_reserved);
3840 trans->bytes_reserved = 0;
3841}
3842
Yan, Zhengd68fc572010-05-16 10:49:58 -04003843int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3844 struct inode *inode)
3845{
3846 struct btrfs_root *root = BTRFS_I(inode)->root;
3847 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3848 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3849
3850 /*
3851 * one for deleting orphan item, one for updating inode and
3852 * two for calling btrfs_truncate_inode_items.
3853 *
3854 * btrfs_truncate_inode_items is a delete operation, it frees
3855 * more space than it uses in most cases. So two units of
3856 * metadata space should be enough for calling it many times.
3857 * If all of the metadata space is used, we can commit
3858 * transaction and use space it freed.
3859 */
3860 u64 num_bytes = calc_trans_metadata_size(root, 4);
3861 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3862}
3863
3864void btrfs_orphan_release_metadata(struct inode *inode)
3865{
3866 struct btrfs_root *root = BTRFS_I(inode)->root;
3867 u64 num_bytes = calc_trans_metadata_size(root, 4);
3868 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3869}
3870
Yan, Zhenga22285a2010-05-16 10:48:46 -04003871int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3872 struct btrfs_pending_snapshot *pending)
3873{
3874 struct btrfs_root *root = pending->root;
3875 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3876 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3877 /*
3878 * two for root back/forward refs, two for directory entries
3879 * and one for root of the snapshot.
3880 */
3881 u64 num_bytes = calc_trans_metadata_size(root, 5);
3882 dst_rsv->space_info = src_rsv->space_info;
3883 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3884}
3885
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003886static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3887{
3888 return num_bytes >>= 3;
3889}
3890
3891int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3892{
3893 struct btrfs_root *root = BTRFS_I(inode)->root;
3894 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3895 u64 to_reserve;
3896 int nr_extents;
3897 int retries = 0;
3898 int ret;
3899
3900 if (btrfs_transaction_in_commit(root->fs_info))
3901 schedule_timeout(1);
3902
3903 num_bytes = ALIGN(num_bytes, root->sectorsize);
3904again:
3905 spin_lock(&BTRFS_I(inode)->accounting_lock);
3906 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
3907 if (nr_extents > BTRFS_I(inode)->reserved_extents) {
3908 nr_extents -= BTRFS_I(inode)->reserved_extents;
3909 to_reserve = calc_trans_metadata_size(root, nr_extents);
3910 } else {
3911 nr_extents = 0;
3912 to_reserve = 0;
3913 }
3914
3915 to_reserve += calc_csum_metadata_size(inode, num_bytes);
3916 ret = reserve_metadata_bytes(block_rsv, to_reserve);
3917 if (ret) {
3918 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3919 ret = should_retry_reserve(NULL, root, block_rsv, to_reserve,
3920 &retries);
3921 if (ret > 0)
3922 goto again;
3923 return ret;
3924 }
3925
3926 BTRFS_I(inode)->reserved_extents += nr_extents;
3927 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
3928 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3929
3930 block_rsv_add_bytes(block_rsv, to_reserve, 1);
3931
3932 if (block_rsv->size > 512 * 1024 * 1024)
3933 shrink_delalloc(NULL, root, to_reserve);
3934
3935 return 0;
3936}
3937
3938void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
3939{
3940 struct btrfs_root *root = BTRFS_I(inode)->root;
3941 u64 to_free;
3942 int nr_extents;
3943
3944 num_bytes = ALIGN(num_bytes, root->sectorsize);
3945 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
3946
3947 spin_lock(&BTRFS_I(inode)->accounting_lock);
3948 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
3949 if (nr_extents < BTRFS_I(inode)->reserved_extents) {
3950 nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents;
3951 BTRFS_I(inode)->reserved_extents -= nr_extents;
3952 } else {
3953 nr_extents = 0;
3954 }
3955 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3956
3957 to_free = calc_csum_metadata_size(inode, num_bytes);
3958 if (nr_extents > 0)
3959 to_free += calc_trans_metadata_size(root, nr_extents);
3960
3961 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
3962 to_free);
3963}
3964
3965int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
3966{
3967 int ret;
3968
3969 ret = btrfs_check_data_free_space(inode, num_bytes);
3970 if (ret)
3971 return ret;
3972
3973 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
3974 if (ret) {
3975 btrfs_free_reserved_data_space(inode, num_bytes);
3976 return ret;
3977 }
3978
3979 return 0;
3980}
3981
3982void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
3983{
3984 btrfs_delalloc_release_metadata(inode, num_bytes);
3985 btrfs_free_reserved_data_space(inode, num_bytes);
3986}
3987
Chris Mason9078a3e2007-04-26 16:46:15 -04003988static int update_block_group(struct btrfs_trans_handle *trans,
3989 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003990 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04003991{
Josef Bacik0af3d002010-06-21 14:48:16 -04003992 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04003993 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04003994 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003995 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003996 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04003997 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04003998
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003999 /* block accounting for super block */
4000 spin_lock(&info->delalloc_lock);
4001 old_val = btrfs_super_bytes_used(&info->super_copy);
4002 if (alloc)
4003 old_val += num_bytes;
4004 else
4005 old_val -= num_bytes;
4006 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004007 spin_unlock(&info->delalloc_lock);
4008
Chris Masond3977122009-01-05 21:25:51 -05004009 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004010 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004011 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004012 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004013 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4014 BTRFS_BLOCK_GROUP_RAID1 |
4015 BTRFS_BLOCK_GROUP_RAID10))
4016 factor = 2;
4017 else
4018 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004019 /*
4020 * If this block group has free space cache written out, we
4021 * need to make sure to load it if we are removing space. This
4022 * is because we need the unpinning stage to actually add the
4023 * space back to the block group, otherwise we will leak space.
4024 */
4025 if (!alloc && cache->cached == BTRFS_CACHE_NO)
4026 cache_block_group(cache, trans, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004027
Chris Masondb945352007-10-15 16:15:53 -04004028 byte_in_group = bytenr - cache->key.objectid;
4029 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004030
Josef Bacik25179202008-10-29 14:49:05 -04004031 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004032 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004033
4034 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4035 cache->disk_cache_state < BTRFS_DC_CLEAR)
4036 cache->disk_cache_state = BTRFS_DC_CLEAR;
4037
Josef Bacik0f9dd462008-09-23 13:14:11 -04004038 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004039 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004040 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004041 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004042 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004043 btrfs_set_block_group_used(&cache->item, old_val);
4044 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004045 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004046 cache->space_info->bytes_used += num_bytes;
4047 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004048 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004049 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004050 } else {
Chris Masondb945352007-10-15 16:15:53 -04004051 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004052 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004053 cache->pinned += num_bytes;
4054 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004055 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004056 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004057 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004058 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004059
Yan, Zhengf0486c62010-05-16 10:46:25 -04004060 set_extent_dirty(info->pinned_extents,
4061 bytenr, bytenr + num_bytes - 1,
4062 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004063 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004064 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004065 total -= num_bytes;
4066 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004067 }
4068 return 0;
4069}
Chris Mason6324fbf2008-03-24 15:01:59 -04004070
Chris Masona061fc82008-05-07 11:43:44 -04004071static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4072{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004073 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004074 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004075
4076 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4077 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004078 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004079
Yan Zhengd2fb3432008-12-11 16:30:39 -05004080 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004081 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004082
4083 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004084}
4085
Yan, Zhengf0486c62010-05-16 10:46:25 -04004086static int pin_down_extent(struct btrfs_root *root,
4087 struct btrfs_block_group_cache *cache,
4088 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004089{
Yan Zheng11833d62009-09-11 16:11:19 -04004090 spin_lock(&cache->space_info->lock);
4091 spin_lock(&cache->lock);
4092 cache->pinned += num_bytes;
4093 cache->space_info->bytes_pinned += num_bytes;
4094 if (reserved) {
4095 cache->reserved -= num_bytes;
4096 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004097 }
Yan Zheng11833d62009-09-11 16:11:19 -04004098 spin_unlock(&cache->lock);
4099 spin_unlock(&cache->space_info->lock);
4100
Yan, Zhengf0486c62010-05-16 10:46:25 -04004101 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4102 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004103 return 0;
4104}
Chris Mason9078a3e2007-04-26 16:46:15 -04004105
Yan, Zhengf0486c62010-05-16 10:46:25 -04004106/*
4107 * this function must be called within transaction
4108 */
4109int btrfs_pin_extent(struct btrfs_root *root,
4110 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004111{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004112 struct btrfs_block_group_cache *cache;
4113
4114 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4115 BUG_ON(!cache);
4116
4117 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4118
4119 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004120 return 0;
4121}
Zheng Yane8569812008-09-26 10:05:48 -04004122
Yan, Zhengf0486c62010-05-16 10:46:25 -04004123/*
4124 * update size of reserved extents. this function may return -EAGAIN
4125 * if 'reserve' is true or 'sinfo' is false.
4126 */
4127static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
4128 u64 num_bytes, int reserve, int sinfo)
4129{
4130 int ret = 0;
4131 if (sinfo) {
4132 struct btrfs_space_info *space_info = cache->space_info;
4133 spin_lock(&space_info->lock);
4134 spin_lock(&cache->lock);
4135 if (reserve) {
4136 if (cache->ro) {
4137 ret = -EAGAIN;
4138 } else {
4139 cache->reserved += num_bytes;
4140 space_info->bytes_reserved += num_bytes;
4141 }
4142 } else {
4143 if (cache->ro)
4144 space_info->bytes_readonly += num_bytes;
4145 cache->reserved -= num_bytes;
4146 space_info->bytes_reserved -= num_bytes;
4147 }
4148 spin_unlock(&cache->lock);
4149 spin_unlock(&space_info->lock);
4150 } else {
4151 spin_lock(&cache->lock);
4152 if (cache->ro) {
4153 ret = -EAGAIN;
4154 } else {
4155 if (reserve)
4156 cache->reserved += num_bytes;
4157 else
4158 cache->reserved -= num_bytes;
4159 }
4160 spin_unlock(&cache->lock);
4161 }
4162 return ret;
4163}
4164
Yan Zheng11833d62009-09-11 16:11:19 -04004165int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4166 struct btrfs_root *root)
4167{
4168 struct btrfs_fs_info *fs_info = root->fs_info;
4169 struct btrfs_caching_control *next;
4170 struct btrfs_caching_control *caching_ctl;
4171 struct btrfs_block_group_cache *cache;
4172
4173 down_write(&fs_info->extent_commit_sem);
4174
4175 list_for_each_entry_safe(caching_ctl, next,
4176 &fs_info->caching_block_groups, list) {
4177 cache = caching_ctl->block_group;
4178 if (block_group_cache_done(cache)) {
4179 cache->last_byte_to_unpin = (u64)-1;
4180 list_del_init(&caching_ctl->list);
4181 put_caching_control(caching_ctl);
4182 } else {
4183 cache->last_byte_to_unpin = caching_ctl->progress;
4184 }
4185 }
4186
4187 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4188 fs_info->pinned_extents = &fs_info->freed_extents[1];
4189 else
4190 fs_info->pinned_extents = &fs_info->freed_extents[0];
4191
4192 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004193
4194 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004195 return 0;
4196}
4197
4198static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4199{
4200 struct btrfs_fs_info *fs_info = root->fs_info;
4201 struct btrfs_block_group_cache *cache = NULL;
4202 u64 len;
4203
4204 while (start <= end) {
4205 if (!cache ||
4206 start >= cache->key.objectid + cache->key.offset) {
4207 if (cache)
4208 btrfs_put_block_group(cache);
4209 cache = btrfs_lookup_block_group(fs_info, start);
4210 BUG_ON(!cache);
4211 }
4212
4213 len = cache->key.objectid + cache->key.offset - start;
4214 len = min(len, end + 1 - start);
4215
4216 if (start < cache->last_byte_to_unpin) {
4217 len = min(len, cache->last_byte_to_unpin - start);
4218 btrfs_add_free_space(cache, start, len);
4219 }
Josef Bacik25179202008-10-29 14:49:05 -04004220
Yan, Zhengf0486c62010-05-16 10:46:25 -04004221 start += len;
4222
Josef Bacik25179202008-10-29 14:49:05 -04004223 spin_lock(&cache->space_info->lock);
4224 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004225 cache->pinned -= len;
4226 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004227 if (cache->ro) {
4228 cache->space_info->bytes_readonly += len;
4229 } else if (cache->reserved_pinned > 0) {
4230 len = min(len, cache->reserved_pinned);
4231 cache->reserved_pinned -= len;
4232 cache->space_info->bytes_reserved += len;
4233 }
Josef Bacik25179202008-10-29 14:49:05 -04004234 spin_unlock(&cache->lock);
4235 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004236 }
4237
4238 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004239 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004240 return 0;
4241}
4242
4243int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004244 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004245{
Yan Zheng11833d62009-09-11 16:11:19 -04004246 struct btrfs_fs_info *fs_info = root->fs_info;
4247 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004248 struct btrfs_block_rsv *block_rsv;
4249 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004250 u64 start;
4251 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004252 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004253 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004254
Yan Zheng11833d62009-09-11 16:11:19 -04004255 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4256 unpin = &fs_info->freed_extents[1];
4257 else
4258 unpin = &fs_info->freed_extents[0];
4259
Chris Masond3977122009-01-05 21:25:51 -05004260 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004261 ret = find_first_extent_bit(unpin, 0, &start, &end,
4262 EXTENT_DIRTY);
4263 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004264 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004265
4266 ret = btrfs_discard_extent(root, start, end + 1 - start);
4267
Chris Mason1a5bc162007-10-15 16:15:26 -04004268 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004269 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004270 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004271 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004272
Yan, Zhengf0486c62010-05-16 10:46:25 -04004273 mutex_lock(&fs_info->durable_block_rsv_mutex);
4274 list_for_each_entry_safe(block_rsv, next_rsv,
4275 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004276
Yan, Zhengf0486c62010-05-16 10:46:25 -04004277 idx = trans->transid & 0x1;
4278 if (block_rsv->freed[idx] > 0) {
4279 block_rsv_add_bytes(block_rsv,
4280 block_rsv->freed[idx], 0);
4281 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004282 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004283 if (atomic_read(&block_rsv->usage) == 0) {
4284 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004285
Yan, Zhengf0486c62010-05-16 10:46:25 -04004286 if (block_rsv->freed[0] == 0 &&
4287 block_rsv->freed[1] == 0) {
4288 list_del_init(&block_rsv->list);
4289 kfree(block_rsv);
4290 }
4291 } else {
4292 btrfs_block_rsv_release(root, block_rsv, 0);
4293 }
4294 }
4295 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4296
Chris Masone20d96d2007-03-22 12:13:20 -04004297 return 0;
4298}
4299
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004300static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4301 struct btrfs_root *root,
4302 u64 bytenr, u64 num_bytes, u64 parent,
4303 u64 root_objectid, u64 owner_objectid,
4304 u64 owner_offset, int refs_to_drop,
4305 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004306{
Chris Masone2fa7222007-03-12 16:22:34 -04004307 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004308 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004309 struct btrfs_fs_info *info = root->fs_info;
4310 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004311 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004312 struct btrfs_extent_item *ei;
4313 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004314 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004315 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004316 int extent_slot = 0;
4317 int found_extent = 0;
4318 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004319 u32 item_size;
4320 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004321
Chris Mason5caf2a02007-04-02 11:20:42 -04004322 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004323 if (!path)
4324 return -ENOMEM;
4325
Chris Mason3c12ac72008-04-21 12:01:38 -04004326 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004327 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004328
4329 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4330 BUG_ON(!is_data && refs_to_drop != 1);
4331
4332 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4333 bytenr, num_bytes, parent,
4334 root_objectid, owner_objectid,
4335 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004336 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004337 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004338 while (extent_slot >= 0) {
4339 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004340 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004341 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004342 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004343 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4344 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004345 found_extent = 1;
4346 break;
4347 }
4348 if (path->slots[0] - extent_slot > 5)
4349 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004350 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004351 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004352#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4353 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4354 if (found_extent && item_size < sizeof(*ei))
4355 found_extent = 0;
4356#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004357 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004358 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004359 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004360 NULL, refs_to_drop,
4361 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004362 BUG_ON(ret);
4363 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04004364 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004365
4366 key.objectid = bytenr;
4367 key.type = BTRFS_EXTENT_ITEM_KEY;
4368 key.offset = num_bytes;
4369
Zheng Yan31840ae2008-09-23 13:14:14 -04004370 ret = btrfs_search_slot(trans, extent_root,
4371 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004372 if (ret) {
4373 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004374 ", was looking for %llu\n", ret,
4375 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004376 btrfs_print_leaf(extent_root, path->nodes[0]);
4377 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004378 BUG_ON(ret);
4379 extent_slot = path->slots[0];
4380 }
Chris Mason7bb86312007-12-11 09:25:06 -05004381 } else {
4382 btrfs_print_leaf(extent_root, path->nodes[0]);
4383 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004384 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004385 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004386 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004387 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004388 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004389 (unsigned long long)owner_objectid,
4390 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004391 }
Chris Mason5f39d392007-10-15 16:14:19 -04004392
4393 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004394 item_size = btrfs_item_size_nr(leaf, extent_slot);
4395#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4396 if (item_size < sizeof(*ei)) {
4397 BUG_ON(found_extent || extent_slot != path->slots[0]);
4398 ret = convert_extent_item_v0(trans, extent_root, path,
4399 owner_objectid, 0);
4400 BUG_ON(ret < 0);
4401
4402 btrfs_release_path(extent_root, path);
4403 path->leave_spinning = 1;
4404
4405 key.objectid = bytenr;
4406 key.type = BTRFS_EXTENT_ITEM_KEY;
4407 key.offset = num_bytes;
4408
4409 ret = btrfs_search_slot(trans, extent_root, &key, path,
4410 -1, 1);
4411 if (ret) {
4412 printk(KERN_ERR "umm, got %d back from search"
4413 ", was looking for %llu\n", ret,
4414 (unsigned long long)bytenr);
4415 btrfs_print_leaf(extent_root, path->nodes[0]);
4416 }
4417 BUG_ON(ret);
4418 extent_slot = path->slots[0];
4419 leaf = path->nodes[0];
4420 item_size = btrfs_item_size_nr(leaf, extent_slot);
4421 }
4422#endif
4423 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004424 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004425 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004426 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4427 struct btrfs_tree_block_info *bi;
4428 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4429 bi = (struct btrfs_tree_block_info *)(ei + 1);
4430 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004431 }
4432
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004433 refs = btrfs_extent_refs(leaf, ei);
4434 BUG_ON(refs < refs_to_drop);
4435 refs -= refs_to_drop;
4436
4437 if (refs > 0) {
4438 if (extent_op)
4439 __run_delayed_extent_op(extent_op, leaf, ei);
4440 /*
4441 * In the case of inline back ref, reference count will
4442 * be updated by remove_extent_backref
4443 */
4444 if (iref) {
4445 BUG_ON(!found_extent);
4446 } else {
4447 btrfs_set_extent_refs(leaf, ei, refs);
4448 btrfs_mark_buffer_dirty(leaf);
4449 }
4450 if (found_extent) {
4451 ret = remove_extent_backref(trans, extent_root, path,
4452 iref, refs_to_drop,
4453 is_data);
4454 BUG_ON(ret);
4455 }
4456 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004457 if (found_extent) {
4458 BUG_ON(is_data && refs_to_drop !=
4459 extent_data_ref_count(root, path, iref));
4460 if (iref) {
4461 BUG_ON(path->slots[0] != extent_slot);
4462 } else {
4463 BUG_ON(path->slots[0] != extent_slot + 1);
4464 path->slots[0] = extent_slot;
4465 num_to_del = 2;
4466 }
Chris Mason78fae272007-03-25 11:35:08 -04004467 }
Chris Masonb9473432009-03-13 11:00:37 -04004468
Chris Mason952fcca2008-02-18 16:33:44 -05004469 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4470 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004471 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04004472 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01004473
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004474 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004475 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4476 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004477 } else {
4478 invalidate_mapping_pages(info->btree_inode->i_mapping,
4479 bytenr >> PAGE_CACHE_SHIFT,
4480 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004481 }
4482
Yan, Zhengf0486c62010-05-16 10:46:25 -04004483 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004484 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004485 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004486 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004487 return ret;
4488}
4489
4490/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004491 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004492 * delayed ref for that extent as well. This searches the delayed ref tree for
4493 * a given extent, and if there are no other delayed refs to be processed, it
4494 * removes it from the tree.
4495 */
4496static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4497 struct btrfs_root *root, u64 bytenr)
4498{
4499 struct btrfs_delayed_ref_head *head;
4500 struct btrfs_delayed_ref_root *delayed_refs;
4501 struct btrfs_delayed_ref_node *ref;
4502 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004503 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004504
4505 delayed_refs = &trans->transaction->delayed_refs;
4506 spin_lock(&delayed_refs->lock);
4507 head = btrfs_find_delayed_ref_head(trans, bytenr);
4508 if (!head)
4509 goto out;
4510
4511 node = rb_prev(&head->node.rb_node);
4512 if (!node)
4513 goto out;
4514
4515 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4516
4517 /* there are still entries for this ref, we can't drop it */
4518 if (ref->bytenr == bytenr)
4519 goto out;
4520
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004521 if (head->extent_op) {
4522 if (!head->must_insert_reserved)
4523 goto out;
4524 kfree(head->extent_op);
4525 head->extent_op = NULL;
4526 }
4527
Chris Mason1887be62009-03-13 10:11:24 -04004528 /*
4529 * waiting for the lock here would deadlock. If someone else has it
4530 * locked they are already in the process of dropping it anyway
4531 */
4532 if (!mutex_trylock(&head->mutex))
4533 goto out;
4534
4535 /*
4536 * at this point we have a head with no other entries. Go
4537 * ahead and process it.
4538 */
4539 head->node.in_tree = 0;
4540 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004541
Chris Mason1887be62009-03-13 10:11:24 -04004542 delayed_refs->num_entries--;
4543
4544 /*
4545 * we don't take a ref on the node because we're removing it from the
4546 * tree, so we just steal the ref the tree was holding.
4547 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004548 delayed_refs->num_heads--;
4549 if (list_empty(&head->cluster))
4550 delayed_refs->num_heads_ready--;
4551
4552 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004553 spin_unlock(&delayed_refs->lock);
4554
Yan, Zhengf0486c62010-05-16 10:46:25 -04004555 BUG_ON(head->extent_op);
4556 if (head->must_insert_reserved)
4557 ret = 1;
4558
4559 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004560 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004561 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004562out:
4563 spin_unlock(&delayed_refs->lock);
4564 return 0;
4565}
4566
Yan, Zhengf0486c62010-05-16 10:46:25 -04004567void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4568 struct btrfs_root *root,
4569 struct extent_buffer *buf,
4570 u64 parent, int last_ref)
4571{
4572 struct btrfs_block_rsv *block_rsv;
4573 struct btrfs_block_group_cache *cache = NULL;
4574 int ret;
4575
4576 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4577 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4578 parent, root->root_key.objectid,
4579 btrfs_header_level(buf),
4580 BTRFS_DROP_DELAYED_REF, NULL);
4581 BUG_ON(ret);
4582 }
4583
4584 if (!last_ref)
4585 return;
4586
4587 block_rsv = get_block_rsv(trans, root);
4588 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004589 if (block_rsv->space_info != cache->space_info)
4590 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004591
4592 if (btrfs_header_generation(buf) == trans->transid) {
4593 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4594 ret = check_ref_cleanup(trans, root, buf->start);
4595 if (!ret)
4596 goto pin;
4597 }
4598
4599 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4600 pin_down_extent(root, cache, buf->start, buf->len, 1);
4601 goto pin;
4602 }
4603
4604 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4605
4606 btrfs_add_free_space(cache, buf->start, buf->len);
4607 ret = update_reserved_bytes(cache, buf->len, 0, 0);
4608 if (ret == -EAGAIN) {
4609 /* block group became read-only */
4610 update_reserved_bytes(cache, buf->len, 0, 1);
4611 goto out;
4612 }
4613
4614 ret = 1;
4615 spin_lock(&block_rsv->lock);
4616 if (block_rsv->reserved < block_rsv->size) {
4617 block_rsv->reserved += buf->len;
4618 ret = 0;
4619 }
4620 spin_unlock(&block_rsv->lock);
4621
4622 if (ret) {
4623 spin_lock(&cache->space_info->lock);
4624 cache->space_info->bytes_reserved -= buf->len;
4625 spin_unlock(&cache->space_info->lock);
4626 }
4627 goto out;
4628 }
4629pin:
4630 if (block_rsv->durable && !cache->ro) {
4631 ret = 0;
4632 spin_lock(&cache->lock);
4633 if (!cache->ro) {
4634 cache->reserved_pinned += buf->len;
4635 ret = 1;
4636 }
4637 spin_unlock(&cache->lock);
4638
4639 if (ret) {
4640 spin_lock(&block_rsv->lock);
4641 block_rsv->freed[trans->transid & 0x1] += buf->len;
4642 spin_unlock(&block_rsv->lock);
4643 }
4644 }
4645out:
4646 btrfs_put_block_group(cache);
4647}
4648
Chris Mason925baed2008-06-25 16:01:30 -04004649int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004650 struct btrfs_root *root,
4651 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004652 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004653{
4654 int ret;
4655
Chris Mason56bec292009-03-13 10:10:06 -04004656 /*
4657 * tree log blocks never actually go into the extent allocation
4658 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004659 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004660 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4661 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004662 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004663 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004664 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004665 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4666 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4667 parent, root_objectid, (int)owner,
4668 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004669 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004670 } else {
4671 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4672 parent, root_objectid, owner,
4673 offset, BTRFS_DROP_DELAYED_REF, NULL);
4674 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004675 }
Chris Mason925baed2008-06-25 16:01:30 -04004676 return ret;
4677}
4678
Chris Mason87ee04e2007-11-30 11:30:34 -05004679static u64 stripe_align(struct btrfs_root *root, u64 val)
4680{
4681 u64 mask = ((u64)root->stripesize - 1);
4682 u64 ret = (val + mask) & ~mask;
4683 return ret;
4684}
4685
Chris Masonfec577f2007-02-26 10:40:21 -05004686/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004687 * when we wait for progress in the block group caching, its because
4688 * our allocation attempt failed at least once. So, we must sleep
4689 * and let some progress happen before we try again.
4690 *
4691 * This function will sleep at least once waiting for new free space to
4692 * show up, and then it will check the block group free space numbers
4693 * for our min num_bytes. Another option is to have it go ahead
4694 * and look in the rbtree for a free extent of a given size, but this
4695 * is a good start.
4696 */
4697static noinline int
4698wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4699 u64 num_bytes)
4700{
Yan Zheng11833d62009-09-11 16:11:19 -04004701 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004702 DEFINE_WAIT(wait);
4703
Yan Zheng11833d62009-09-11 16:11:19 -04004704 caching_ctl = get_caching_control(cache);
4705 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004706 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004707
Yan Zheng11833d62009-09-11 16:11:19 -04004708 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004709 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004710
4711 put_caching_control(caching_ctl);
4712 return 0;
4713}
4714
4715static noinline int
4716wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4717{
4718 struct btrfs_caching_control *caching_ctl;
4719 DEFINE_WAIT(wait);
4720
4721 caching_ctl = get_caching_control(cache);
4722 if (!caching_ctl)
4723 return 0;
4724
4725 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4726
4727 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004728 return 0;
4729}
4730
Yan, Zhengb742bb82010-05-16 10:46:24 -04004731static int get_block_group_index(struct btrfs_block_group_cache *cache)
4732{
4733 int index;
4734 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4735 index = 0;
4736 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4737 index = 1;
4738 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4739 index = 2;
4740 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4741 index = 3;
4742 else
4743 index = 4;
4744 return index;
4745}
4746
Josef Bacik817d52f2009-07-13 21:29:25 -04004747enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004748 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004749 LOOP_CACHING_NOWAIT = 1,
4750 LOOP_CACHING_WAIT = 2,
4751 LOOP_ALLOC_CHUNK = 3,
4752 LOOP_NO_EMPTY_SIZE = 4,
4753};
4754
4755/*
Chris Masonfec577f2007-02-26 10:40:21 -05004756 * walks the btree of allocated extents and find a hole of a given size.
4757 * The key ins is changed to record the hole:
4758 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004759 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004760 * ins->offset == number of blocks
4761 * Any available blocks before search_start are skipped.
4762 */
Chris Masond3977122009-01-05 21:25:51 -05004763static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004764 struct btrfs_root *orig_root,
4765 u64 num_bytes, u64 empty_size,
4766 u64 search_start, u64 search_end,
4767 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004768 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004769{
Josef Bacik80eb2342008-10-29 14:49:05 -04004770 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004771 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004772 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004773 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004774 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004775 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004776 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004777 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004778 int last_ptr_loop = 0;
4779 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004780 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004781 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004782 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004783 bool failed_alloc = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004784 u64 ideal_cache_percent = 0;
4785 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004786
Chris Masondb945352007-10-15 16:15:53 -04004787 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004788 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004789 ins->objectid = 0;
4790 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004791
Josef Bacik2552d172009-04-03 10:14:19 -04004792 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004793 if (!space_info) {
4794 printk(KERN_ERR "No space info for %d\n", data);
4795 return -ENOSPC;
4796 }
Josef Bacik2552d172009-04-03 10:14:19 -04004797
Chris Mason0ef3e662008-05-24 14:04:53 -04004798 if (orig_root->ref_cows || empty_size)
4799 allowed_chunk_alloc = 1;
4800
Chris Mason239b14b2008-03-24 15:02:07 -04004801 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004802 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004803 if (!btrfs_test_opt(root, SSD))
4804 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004805 }
4806
Chris Masonfa9c0d72009-04-03 09:47:43 -04004807 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4808 last_ptr = &root->fs_info->data_alloc_cluster;
4809 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004810
Chris Mason239b14b2008-03-24 15:02:07 -04004811 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004812 spin_lock(&last_ptr->lock);
4813 if (last_ptr->block_group)
4814 hint_byte = last_ptr->window_start;
4815 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004816 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004817
Chris Masona061fc82008-05-07 11:43:44 -04004818 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004819 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004820
Josef Bacik817d52f2009-07-13 21:29:25 -04004821 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004822 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004823
Josef Bacik2552d172009-04-03 10:14:19 -04004824 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004825ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004826 block_group = btrfs_lookup_block_group(root->fs_info,
4827 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004828 /*
4829 * we don't want to use the block group if it doesn't match our
4830 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004831 *
4832 * However if we are re-searching with an ideal block group
4833 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004834 */
4835 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004836 (block_group->cached != BTRFS_CACHE_NO ||
4837 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004838 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004839 if (list_empty(&block_group->list) ||
4840 block_group->ro) {
4841 /*
4842 * someone is removing this block group,
4843 * we can't jump into the have_block_group
4844 * target because our list pointers are not
4845 * valid
4846 */
4847 btrfs_put_block_group(block_group);
4848 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004849 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004850 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004851 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004852 }
Josef Bacik2552d172009-04-03 10:14:19 -04004853 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004854 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004855 }
Chris Mason42e70e72008-11-07 18:17:11 -05004856 }
Josef Bacik2552d172009-04-03 10:14:19 -04004857search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004858 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004859 list_for_each_entry(block_group, &space_info->block_groups[index],
4860 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004861 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004862 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004863
Josef Bacik11dfe352009-11-13 20:12:59 +00004864 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004865 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004866
Josef Bacik2552d172009-04-03 10:14:19 -04004867have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004868 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004869 u64 free_percent;
4870
Josef Bacik9d66e232010-08-25 16:54:15 -04004871 ret = cache_block_group(block_group, trans, 1);
4872 if (block_group->cached == BTRFS_CACHE_FINISHED)
4873 goto have_block_group;
4874
Josef Bacikccf0e722009-11-10 21:23:48 -05004875 free_percent = btrfs_block_group_used(&block_group->item);
4876 free_percent *= 100;
4877 free_percent = div64_u64(free_percent,
4878 block_group->key.offset);
4879 free_percent = 100 - free_percent;
4880 if (free_percent > ideal_cache_percent &&
4881 likely(!block_group->ro)) {
4882 ideal_cache_offset = block_group->key.objectid;
4883 ideal_cache_percent = free_percent;
4884 }
4885
Josef Bacik817d52f2009-07-13 21:29:25 -04004886 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05004887 * We only want to start kthread caching if we are at
4888 * the point where we will wait for caching to make
4889 * progress, or if our ideal search is over and we've
4890 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04004891 */
4892 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05004893 (loop > LOOP_FIND_IDEAL &&
4894 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacik9d66e232010-08-25 16:54:15 -04004895 ret = cache_block_group(block_group, trans, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004896 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004897 }
Josef Bacikccf0e722009-11-10 21:23:48 -05004898 found_uncached_bg = true;
4899
4900 /*
4901 * If loop is set for cached only, try the next block
4902 * group.
4903 */
4904 if (loop == LOOP_FIND_IDEAL)
4905 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05004906 }
4907
Josef Bacik817d52f2009-07-13 21:29:25 -04004908 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05004909 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04004910 found_uncached_bg = true;
4911
Josef Bacikea6a4782008-11-20 12:16:16 -05004912 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004913 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004914
Josef Bacik0a243252009-09-11 16:11:20 -04004915 /*
4916 * Ok we want to try and use the cluster allocator, so lets look
4917 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4918 * have tried the cluster allocator plenty of times at this
4919 * point and not have found anything, so we are likely way too
4920 * fragmented for the clustering stuff to find anything, so lets
4921 * just skip it and let the allocator find whatever block it can
4922 * find
4923 */
4924 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004925 /*
4926 * the refill lock keeps out other
4927 * people trying to start a new cluster
4928 */
4929 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004930 if (last_ptr->block_group &&
4931 (last_ptr->block_group->ro ||
4932 !block_group_bits(last_ptr->block_group, data))) {
4933 offset = 0;
4934 goto refill_cluster;
4935 }
4936
Chris Masonfa9c0d72009-04-03 09:47:43 -04004937 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4938 num_bytes, search_start);
4939 if (offset) {
4940 /* we have a block, we're done */
4941 spin_unlock(&last_ptr->refill_lock);
4942 goto checks;
4943 }
4944
4945 spin_lock(&last_ptr->lock);
4946 /*
4947 * whoops, this cluster doesn't actually point to
4948 * this block group. Get a ref on the block
4949 * group is does point to and try again
4950 */
4951 if (!last_ptr_loop && last_ptr->block_group &&
4952 last_ptr->block_group != block_group) {
4953
4954 btrfs_put_block_group(block_group);
4955 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00004956 btrfs_get_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04004957 spin_unlock(&last_ptr->lock);
4958 spin_unlock(&last_ptr->refill_lock);
4959
4960 last_ptr_loop = 1;
4961 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04004962 /*
4963 * we know this block group is properly
4964 * in the list because
4965 * btrfs_remove_block_group, drops the
4966 * cluster before it removes the block
4967 * group from the list
4968 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04004969 goto have_block_group;
4970 }
4971 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004972refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04004973 /*
4974 * this cluster didn't work out, free it and
4975 * start over
4976 */
4977 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4978
4979 last_ptr_loop = 0;
4980
4981 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04004982 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04004983 block_group, last_ptr,
4984 offset, num_bytes,
4985 empty_cluster + empty_size);
4986 if (ret == 0) {
4987 /*
4988 * now pull our allocation out of this
4989 * cluster
4990 */
4991 offset = btrfs_alloc_from_cluster(block_group,
4992 last_ptr, num_bytes,
4993 search_start);
4994 if (offset) {
4995 /* we found one, proceed */
4996 spin_unlock(&last_ptr->refill_lock);
4997 goto checks;
4998 }
Josef Bacik0a243252009-09-11 16:11:20 -04004999 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5000 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005001 spin_unlock(&last_ptr->refill_lock);
5002
Josef Bacik0a243252009-09-11 16:11:20 -04005003 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005004 wait_block_group_cache_progress(block_group,
5005 num_bytes + empty_cluster + empty_size);
5006 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005007 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005008
Chris Masonfa9c0d72009-04-03 09:47:43 -04005009 /*
5010 * at this point we either didn't find a cluster
5011 * or we weren't able to allocate a block from our
5012 * cluster. Free the cluster we've been trying
5013 * to use, and go to the next block group
5014 */
Josef Bacik0a243252009-09-11 16:11:20 -04005015 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005016 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005017 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005018 }
5019
Josef Bacik6226cb02009-04-03 10:14:18 -04005020 offset = btrfs_find_space_for_alloc(block_group, search_start,
5021 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005022 /*
5023 * If we didn't find a chunk, and we haven't failed on this
5024 * block group before, and this block group is in the middle of
5025 * caching and we are ok with waiting, then go ahead and wait
5026 * for progress to be made, and set failed_alloc to true.
5027 *
5028 * If failed_alloc is true then we've already waited on this
5029 * block group once and should move on to the next block group.
5030 */
5031 if (!offset && !failed_alloc && !cached &&
5032 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005033 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005034 num_bytes + empty_size);
5035 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005036 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005037 } else if (!offset) {
5038 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005039 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005040checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005041 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005042 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005043 if (search_start + num_bytes >= search_end) {
5044 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005045 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005046 }
Chris Masone37c9e62007-05-09 20:13:14 -04005047
Josef Bacik2552d172009-04-03 10:14:19 -04005048 /* move on to the next group */
5049 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005050 block_group->key.objectid + block_group->key.offset) {
5051 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005052 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005053 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005054
Josef Bacik2552d172009-04-03 10:14:19 -04005055 ins->objectid = search_start;
5056 ins->offset = num_bytes;
5057
Josef Bacik6226cb02009-04-03 10:14:18 -04005058 if (offset < search_start)
5059 btrfs_add_free_space(block_group, offset,
5060 search_start - offset);
5061 BUG_ON(offset > search_start);
5062
Yan, Zhengf0486c62010-05-16 10:46:25 -04005063 ret = update_reserved_bytes(block_group, num_bytes, 1,
5064 (data & BTRFS_BLOCK_GROUP_DATA));
5065 if (ret == -EAGAIN) {
5066 btrfs_add_free_space(block_group, offset, num_bytes);
5067 goto loop;
5068 }
Yan Zheng11833d62009-09-11 16:11:19 -04005069
Josef Bacik2552d172009-04-03 10:14:19 -04005070 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005071 ins->objectid = search_start;
5072 ins->offset = num_bytes;
5073
5074 if (offset < search_start)
5075 btrfs_add_free_space(block_group, offset,
5076 search_start - offset);
5077 BUG_ON(offset > search_start);
Josef Bacik2552d172009-04-03 10:14:19 -04005078 break;
5079loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005080 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005081 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005082 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d72009-04-03 09:47:43 -04005083 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005084 }
5085 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005086
Yan, Zhengb742bb82010-05-16 10:46:24 -04005087 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5088 goto search;
5089
Josef Bacikccf0e722009-11-10 21:23:48 -05005090 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5091 * for them to make caching progress. Also
5092 * determine the best possible bg to cache
5093 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5094 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005095 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5096 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5097 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5098 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04005099 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005100 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5101 (found_uncached_bg || empty_size || empty_cluster ||
5102 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005103 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005104 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005105 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005106 loop++;
5107 if (!ideal_cache_percent &&
5108 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005109 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005110
5111 /*
5112 * 1 of the following 2 things have happened so far
5113 *
5114 * 1) We found an ideal block group for caching that
5115 * is mostly full and will cache quickly, so we might
5116 * as well wait for it.
5117 *
5118 * 2) We searched for cached only and we didn't find
5119 * anything, and we didn't start any caching kthreads
5120 * either, so chances are we will loop through and
5121 * start a couple caching kthreads, and then come back
5122 * around and just wait for them. This will be slower
5123 * because we will have 2 caching kthreads reading at
5124 * the same time when we could have just started one
5125 * and waited for it to get far enough to give us an
5126 * allocation, so go ahead and go to the wait caching
5127 * loop.
5128 */
5129 loop = LOOP_CACHING_WAIT;
5130 search_start = ideal_cache_offset;
5131 ideal_cache_percent = 0;
5132 goto ideal_cache;
5133 } else if (loop == LOOP_FIND_IDEAL) {
5134 /*
5135 * Didn't find a uncached bg, wait on anything we find
5136 * next.
5137 */
5138 loop = LOOP_CACHING_WAIT;
5139 goto search;
5140 }
5141
5142 if (loop < LOOP_CACHING_WAIT) {
5143 loop++;
5144 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005145 }
5146
5147 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005148 empty_size = 0;
5149 empty_cluster = 0;
5150 }
Chris Mason42e70e72008-11-07 18:17:11 -05005151
Josef Bacik2552d172009-04-03 10:14:19 -04005152 if (allowed_chunk_alloc) {
5153 ret = do_chunk_alloc(trans, root, num_bytes +
5154 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04005155 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005156 done_chunk_alloc = 1;
5157 } else if (!done_chunk_alloc) {
Josef Bacik2552d172009-04-03 10:14:19 -04005158 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005159 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005160
Josef Bacik817d52f2009-07-13 21:29:25 -04005161 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005162 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005163 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005164 }
Josef Bacik2552d172009-04-03 10:14:19 -04005165 ret = -ENOSPC;
5166 } else if (!ins->objectid) {
5167 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04005168 }
Chris Mason0b86a832008-03-24 15:01:56 -04005169
Josef Bacik80eb2342008-10-29 14:49:05 -04005170 /* we found what we needed */
5171 if (ins->objectid) {
5172 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05005173 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04005174
Chris Masonfa9c0d72009-04-03 09:47:43 -04005175 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005176 ret = 0;
5177 }
Chris Mason0b86a832008-03-24 15:01:56 -04005178
Chris Mason0f70abe2007-02-28 16:46:22 -05005179 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005180}
Chris Masonec44a352008-04-28 15:29:52 -04005181
Josef Bacik9ed74f22009-09-11 16:12:44 -04005182static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5183 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005184{
5185 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005186 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005187
Josef Bacik9ed74f22009-09-11 16:12:44 -04005188 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005189 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5190 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005191 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005192 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005193 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005194 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5195 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005196 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005197 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005198 (unsigned long long)info->bytes_pinned,
5199 (unsigned long long)info->bytes_reserved,
5200 (unsigned long long)info->bytes_may_use,
5201 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005202 spin_unlock(&info->lock);
5203
5204 if (!dump_block_groups)
5205 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005206
Josef Bacik80eb2342008-10-29 14:49:05 -04005207 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005208again:
5209 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005210 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005211 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5212 "%llu pinned %llu reserved\n",
5213 (unsigned long long)cache->key.objectid,
5214 (unsigned long long)cache->key.offset,
5215 (unsigned long long)btrfs_block_group_used(&cache->item),
5216 (unsigned long long)cache->pinned,
5217 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005218 btrfs_dump_free_space(cache, bytes);
5219 spin_unlock(&cache->lock);
5220 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005221 if (++index < BTRFS_NR_RAID_TYPES)
5222 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005223 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005224}
Zheng Yane8569812008-09-26 10:05:48 -04005225
Yan Zheng11833d62009-09-11 16:11:19 -04005226int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5227 struct btrfs_root *root,
5228 u64 num_bytes, u64 min_alloc_size,
5229 u64 empty_size, u64 hint_byte,
5230 u64 search_end, struct btrfs_key *ins,
5231 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005232{
5233 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005234 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005235
Josef Bacik6a632092009-02-20 11:00:09 -05005236 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005237again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005238 /*
5239 * the only place that sets empty_size is btrfs_realloc_node, which
5240 * is not called recursively on allocations
5241 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005242 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005243 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005244 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04005245
Chris Masondb945352007-10-15 16:15:53 -04005246 WARN_ON(num_bytes < root->sectorsize);
5247 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005248 search_start, search_end, hint_byte,
5249 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005250
Chris Mason98d20f62008-04-14 09:46:10 -04005251 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5252 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005253 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005254 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005255 do_chunk_alloc(trans, root->fs_info->extent_root,
5256 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005257 goto again;
5258 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005259 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005260 struct btrfs_space_info *sinfo;
5261
5262 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005263 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5264 "wanted %llu\n", (unsigned long long)data,
5265 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005266 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005267 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005268
5269 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005270}
5271
Chris Mason65b51a02008-08-01 15:11:20 -04005272int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5273{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005274 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005275 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005276
Josef Bacik0f9dd462008-09-23 13:14:11 -04005277 cache = btrfs_lookup_block_group(root->fs_info, start);
5278 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005279 printk(KERN_ERR "Unable to find block group for %llu\n",
5280 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005281 return -ENOSPC;
5282 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005283
5284 ret = btrfs_discard_extent(root, start, len);
5285
Josef Bacik0f9dd462008-09-23 13:14:11 -04005286 btrfs_add_free_space(cache, start, len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005287 update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005288 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005289
Chris Masone6dcd2d2008-07-17 12:53:50 -04005290 return ret;
5291}
5292
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005293static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5294 struct btrfs_root *root,
5295 u64 parent, u64 root_objectid,
5296 u64 flags, u64 owner, u64 offset,
5297 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005298{
5299 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005300 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005301 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005302 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005303 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005304 struct extent_buffer *leaf;
5305 int type;
5306 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005307
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005308 if (parent > 0)
5309 type = BTRFS_SHARED_DATA_REF_KEY;
5310 else
5311 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005312
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005313 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005314
5315 path = btrfs_alloc_path();
5316 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05005317
Chris Masonb9473432009-03-13 11:00:37 -04005318 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005319 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5320 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005321 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005322
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005323 leaf = path->nodes[0];
5324 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005325 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005326 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5327 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5328 btrfs_set_extent_flags(leaf, extent_item,
5329 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005330
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005331 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5332 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5333 if (parent > 0) {
5334 struct btrfs_shared_data_ref *ref;
5335 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5336 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5337 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5338 } else {
5339 struct btrfs_extent_data_ref *ref;
5340 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5341 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5342 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5343 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5344 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5345 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005346
5347 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005348 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005349
Yan, Zhengf0486c62010-05-16 10:46:25 -04005350 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005351 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005352 printk(KERN_ERR "btrfs update block group failed for %llu "
5353 "%llu\n", (unsigned long long)ins->objectid,
5354 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005355 BUG();
5356 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005357 return ret;
5358}
5359
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005360static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5361 struct btrfs_root *root,
5362 u64 parent, u64 root_objectid,
5363 u64 flags, struct btrfs_disk_key *key,
5364 int level, struct btrfs_key *ins)
5365{
5366 int ret;
5367 struct btrfs_fs_info *fs_info = root->fs_info;
5368 struct btrfs_extent_item *extent_item;
5369 struct btrfs_tree_block_info *block_info;
5370 struct btrfs_extent_inline_ref *iref;
5371 struct btrfs_path *path;
5372 struct extent_buffer *leaf;
5373 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5374
5375 path = btrfs_alloc_path();
5376 BUG_ON(!path);
5377
5378 path->leave_spinning = 1;
5379 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5380 ins, size);
5381 BUG_ON(ret);
5382
5383 leaf = path->nodes[0];
5384 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5385 struct btrfs_extent_item);
5386 btrfs_set_extent_refs(leaf, extent_item, 1);
5387 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5388 btrfs_set_extent_flags(leaf, extent_item,
5389 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5390 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5391
5392 btrfs_set_tree_block_key(leaf, block_info, key);
5393 btrfs_set_tree_block_level(leaf, block_info, level);
5394
5395 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5396 if (parent > 0) {
5397 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5398 btrfs_set_extent_inline_ref_type(leaf, iref,
5399 BTRFS_SHARED_BLOCK_REF_KEY);
5400 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5401 } else {
5402 btrfs_set_extent_inline_ref_type(leaf, iref,
5403 BTRFS_TREE_BLOCK_REF_KEY);
5404 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5405 }
5406
5407 btrfs_mark_buffer_dirty(leaf);
5408 btrfs_free_path(path);
5409
Yan, Zhengf0486c62010-05-16 10:46:25 -04005410 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005411 if (ret) {
5412 printk(KERN_ERR "btrfs update block group failed for %llu "
5413 "%llu\n", (unsigned long long)ins->objectid,
5414 (unsigned long long)ins->offset);
5415 BUG();
5416 }
5417 return ret;
5418}
5419
5420int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5421 struct btrfs_root *root,
5422 u64 root_objectid, u64 owner,
5423 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005424{
5425 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005426
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005427 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005429 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5430 0, root_objectid, owner, offset,
5431 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005432 return ret;
5433}
Chris Masone02119d2008-09-05 16:13:11 -04005434
5435/*
5436 * this is used by the tree logging recovery code. It records that
5437 * an extent has been allocated and makes sure to clear the free
5438 * space cache bits as well
5439 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005440int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5441 struct btrfs_root *root,
5442 u64 root_objectid, u64 owner, u64 offset,
5443 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005444{
5445 int ret;
5446 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005447 struct btrfs_caching_control *caching_ctl;
5448 u64 start = ins->objectid;
5449 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005450
Chris Masone02119d2008-09-05 16:13:11 -04005451 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik9d66e232010-08-25 16:54:15 -04005452 cache_block_group(block_group, trans, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005453 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005454
Yan Zheng11833d62009-09-11 16:11:19 -04005455 if (!caching_ctl) {
5456 BUG_ON(!block_group_cache_done(block_group));
5457 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5458 BUG_ON(ret);
5459 } else {
5460 mutex_lock(&caching_ctl->mutex);
5461
5462 if (start >= caching_ctl->progress) {
5463 ret = add_excluded_extent(root, start, num_bytes);
5464 BUG_ON(ret);
5465 } else if (start + num_bytes <= caching_ctl->progress) {
5466 ret = btrfs_remove_free_space(block_group,
5467 start, num_bytes);
5468 BUG_ON(ret);
5469 } else {
5470 num_bytes = caching_ctl->progress - start;
5471 ret = btrfs_remove_free_space(block_group,
5472 start, num_bytes);
5473 BUG_ON(ret);
5474
5475 start = caching_ctl->progress;
5476 num_bytes = ins->objectid + ins->offset -
5477 caching_ctl->progress;
5478 ret = add_excluded_extent(root, start, num_bytes);
5479 BUG_ON(ret);
5480 }
5481
5482 mutex_unlock(&caching_ctl->mutex);
5483 put_caching_control(caching_ctl);
5484 }
5485
Yan, Zhengf0486c62010-05-16 10:46:25 -04005486 ret = update_reserved_bytes(block_group, ins->offset, 1, 1);
5487 BUG_ON(ret);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005488 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005489 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5490 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005491 return ret;
5492}
5493
Chris Mason65b51a02008-08-01 15:11:20 -04005494struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5495 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005496 u64 bytenr, u32 blocksize,
5497 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005498{
5499 struct extent_buffer *buf;
5500
5501 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5502 if (!buf)
5503 return ERR_PTR(-ENOMEM);
5504 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005505 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005506 btrfs_tree_lock(buf);
5507 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005508
5509 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005510 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005511
Chris Masond0c803c2008-09-11 16:17:57 -04005512 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005513 /*
5514 * we allow two log transactions at a time, use different
5515 * EXENT bit to differentiate dirty pages.
5516 */
5517 if (root->log_transid % 2 == 0)
5518 set_extent_dirty(&root->dirty_log_pages, buf->start,
5519 buf->start + buf->len - 1, GFP_NOFS);
5520 else
5521 set_extent_new(&root->dirty_log_pages, buf->start,
5522 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005523 } else {
5524 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5525 buf->start + buf->len - 1, GFP_NOFS);
5526 }
Chris Mason65b51a02008-08-01 15:11:20 -04005527 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005528 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005529 return buf;
5530}
5531
Yan, Zhengf0486c62010-05-16 10:46:25 -04005532static struct btrfs_block_rsv *
5533use_block_rsv(struct btrfs_trans_handle *trans,
5534 struct btrfs_root *root, u32 blocksize)
5535{
5536 struct btrfs_block_rsv *block_rsv;
5537 int ret;
5538
5539 block_rsv = get_block_rsv(trans, root);
5540
5541 if (block_rsv->size == 0) {
5542 ret = reserve_metadata_bytes(block_rsv, blocksize);
5543 if (ret)
5544 return ERR_PTR(ret);
5545 return block_rsv;
5546 }
5547
5548 ret = block_rsv_use_bytes(block_rsv, blocksize);
5549 if (!ret)
5550 return block_rsv;
5551
5552 WARN_ON(1);
5553 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
5554 block_rsv->size, block_rsv->reserved,
5555 block_rsv->freed[0], block_rsv->freed[1]);
5556
5557 return ERR_PTR(-ENOSPC);
5558}
5559
5560static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5561{
5562 block_rsv_add_bytes(block_rsv, blocksize, 0);
5563 block_rsv_release_bytes(block_rsv, NULL, 0);
5564}
5565
Chris Masonfec577f2007-02-26 10:40:21 -05005566/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005567 * finds a free extent and does all the dirty work required for allocation
5568 * returns the key for the extent through ins, and a tree buffer for
5569 * the first block of the extent through buf.
5570 *
Chris Masonfec577f2007-02-26 10:40:21 -05005571 * returns the tree buffer or NULL.
5572 */
Chris Mason5f39d392007-10-15 16:14:19 -04005573struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005574 struct btrfs_root *root, u32 blocksize,
5575 u64 parent, u64 root_objectid,
5576 struct btrfs_disk_key *key, int level,
5577 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005578{
Chris Masone2fa7222007-03-12 16:22:34 -04005579 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005580 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005581 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005582 u64 flags = 0;
5583 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005584
Yan, Zhengf0486c62010-05-16 10:46:25 -04005585
5586 block_rsv = use_block_rsv(trans, root, blocksize);
5587 if (IS_ERR(block_rsv))
5588 return ERR_CAST(block_rsv);
5589
5590 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5591 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005592 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005593 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005594 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005595 }
Chris Mason55c69072008-01-09 15:55:33 -05005596
Chris Mason4008c042009-02-12 14:09:45 -05005597 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5598 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005599 BUG_ON(IS_ERR(buf));
5600
5601 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5602 if (parent == 0)
5603 parent = ins.objectid;
5604 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5605 } else
5606 BUG_ON(parent > 0);
5607
5608 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5609 struct btrfs_delayed_extent_op *extent_op;
5610 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5611 BUG_ON(!extent_op);
5612 if (key)
5613 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5614 else
5615 memset(&extent_op->key, 0, sizeof(extent_op->key));
5616 extent_op->flags_to_set = flags;
5617 extent_op->update_key = 1;
5618 extent_op->update_flags = 1;
5619 extent_op->is_data = 0;
5620
5621 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5622 ins.offset, parent, root_objectid,
5623 level, BTRFS_ADD_DELAYED_EXTENT,
5624 extent_op);
5625 BUG_ON(ret);
5626 }
Chris Masonfec577f2007-02-26 10:40:21 -05005627 return buf;
5628}
Chris Masona28ec192007-03-06 20:08:01 -05005629
Yan Zheng2c47e6052009-06-27 21:07:35 -04005630struct walk_control {
5631 u64 refs[BTRFS_MAX_LEVEL];
5632 u64 flags[BTRFS_MAX_LEVEL];
5633 struct btrfs_key update_progress;
5634 int stage;
5635 int level;
5636 int shared_level;
5637 int update_ref;
5638 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005639 int reada_slot;
5640 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005641};
5642
5643#define DROP_REFERENCE 1
5644#define UPDATE_BACKREF 2
5645
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005646static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5647 struct btrfs_root *root,
5648 struct walk_control *wc,
5649 struct btrfs_path *path)
5650{
5651 u64 bytenr;
5652 u64 generation;
5653 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005654 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005655 u64 last = 0;
5656 u32 nritems;
5657 u32 blocksize;
5658 struct btrfs_key key;
5659 struct extent_buffer *eb;
5660 int ret;
5661 int slot;
5662 int nread = 0;
5663
5664 if (path->slots[wc->level] < wc->reada_slot) {
5665 wc->reada_count = wc->reada_count * 2 / 3;
5666 wc->reada_count = max(wc->reada_count, 2);
5667 } else {
5668 wc->reada_count = wc->reada_count * 3 / 2;
5669 wc->reada_count = min_t(int, wc->reada_count,
5670 BTRFS_NODEPTRS_PER_BLOCK(root));
5671 }
5672
5673 eb = path->nodes[wc->level];
5674 nritems = btrfs_header_nritems(eb);
5675 blocksize = btrfs_level_size(root, wc->level - 1);
5676
5677 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5678 if (nread >= wc->reada_count)
5679 break;
5680
5681 cond_resched();
5682 bytenr = btrfs_node_blockptr(eb, slot);
5683 generation = btrfs_node_ptr_generation(eb, slot);
5684
5685 if (slot == path->slots[wc->level])
5686 goto reada;
5687
5688 if (wc->stage == UPDATE_BACKREF &&
5689 generation <= root->root_key.offset)
5690 continue;
5691
Yan, Zheng94fcca92009-10-09 09:25:16 -04005692 /* We don't lock the tree block, it's OK to be racy here */
5693 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5694 &refs, &flags);
5695 BUG_ON(ret);
5696 BUG_ON(refs == 0);
5697
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005698 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005699 if (refs == 1)
5700 goto reada;
5701
Yan, Zheng94fcca92009-10-09 09:25:16 -04005702 if (wc->level == 1 &&
5703 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5704 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005705 if (!wc->update_ref ||
5706 generation <= root->root_key.offset)
5707 continue;
5708 btrfs_node_key_to_cpu(eb, &key, slot);
5709 ret = btrfs_comp_cpu_keys(&key,
5710 &wc->update_progress);
5711 if (ret < 0)
5712 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005713 } else {
5714 if (wc->level == 1 &&
5715 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5716 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005717 }
5718reada:
5719 ret = readahead_tree_block(root, bytenr, blocksize,
5720 generation);
5721 if (ret)
5722 break;
5723 last = bytenr + blocksize;
5724 nread++;
5725 }
5726 wc->reada_slot = slot;
5727}
5728
Chris Mason9aca1d52007-03-13 11:09:37 -04005729/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005730 * hepler to process tree block while walking down the tree.
5731 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005732 * when wc->stage == UPDATE_BACKREF, this function updates
5733 * back refs for pointers in the block.
5734 *
5735 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005736 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005737static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5738 struct btrfs_root *root,
5739 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005740 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005741{
5742 int level = wc->level;
5743 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005744 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5745 int ret;
5746
5747 if (wc->stage == UPDATE_BACKREF &&
5748 btrfs_header_owner(eb) != root->root_key.objectid)
5749 return 1;
5750
5751 /*
5752 * when reference count of tree block is 1, it won't increase
5753 * again. once full backref flag is set, we never clear it.
5754 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005755 if (lookup_info &&
5756 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5757 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005758 BUG_ON(!path->locks[level]);
5759 ret = btrfs_lookup_extent_info(trans, root,
5760 eb->start, eb->len,
5761 &wc->refs[level],
5762 &wc->flags[level]);
5763 BUG_ON(ret);
5764 BUG_ON(wc->refs[level] == 0);
5765 }
5766
Yan Zheng2c47e6052009-06-27 21:07:35 -04005767 if (wc->stage == DROP_REFERENCE) {
5768 if (wc->refs[level] > 1)
5769 return 1;
5770
5771 if (path->locks[level] && !wc->keep_locks) {
5772 btrfs_tree_unlock(eb);
5773 path->locks[level] = 0;
5774 }
5775 return 0;
5776 }
5777
5778 /* wc->stage == UPDATE_BACKREF */
5779 if (!(wc->flags[level] & flag)) {
5780 BUG_ON(!path->locks[level]);
5781 ret = btrfs_inc_ref(trans, root, eb, 1);
5782 BUG_ON(ret);
5783 ret = btrfs_dec_ref(trans, root, eb, 0);
5784 BUG_ON(ret);
5785 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5786 eb->len, flag, 0);
5787 BUG_ON(ret);
5788 wc->flags[level] |= flag;
5789 }
5790
5791 /*
5792 * the block is shared by multiple trees, so it's not good to
5793 * keep the tree lock
5794 */
5795 if (path->locks[level] && level > 0) {
5796 btrfs_tree_unlock(eb);
5797 path->locks[level] = 0;
5798 }
5799 return 0;
5800}
5801
5802/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005803 * hepler to process tree block pointer.
5804 *
5805 * when wc->stage == DROP_REFERENCE, this function checks
5806 * reference count of the block pointed to. if the block
5807 * is shared and we need update back refs for the subtree
5808 * rooted at the block, this function changes wc->stage to
5809 * UPDATE_BACKREF. if the block is shared and there is no
5810 * need to update back, this function drops the reference
5811 * to the block.
5812 *
5813 * NOTE: return value 1 means we should stop walking down.
5814 */
5815static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5816 struct btrfs_root *root,
5817 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005818 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005819{
5820 u64 bytenr;
5821 u64 generation;
5822 u64 parent;
5823 u32 blocksize;
5824 struct btrfs_key key;
5825 struct extent_buffer *next;
5826 int level = wc->level;
5827 int reada = 0;
5828 int ret = 0;
5829
5830 generation = btrfs_node_ptr_generation(path->nodes[level],
5831 path->slots[level]);
5832 /*
5833 * if the lower level block was created before the snapshot
5834 * was created, we know there is no need to update back refs
5835 * for the subtree
5836 */
5837 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005838 generation <= root->root_key.offset) {
5839 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005840 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005841 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005842
5843 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5844 blocksize = btrfs_level_size(root, level - 1);
5845
5846 next = btrfs_find_tree_block(root, bytenr, blocksize);
5847 if (!next) {
5848 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00005849 if (!next)
5850 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005851 reada = 1;
5852 }
5853 btrfs_tree_lock(next);
5854 btrfs_set_lock_blocking(next);
5855
Yan, Zheng94fcca92009-10-09 09:25:16 -04005856 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5857 &wc->refs[level - 1],
5858 &wc->flags[level - 1]);
5859 BUG_ON(ret);
5860 BUG_ON(wc->refs[level - 1] == 0);
5861 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005862
Yan, Zheng94fcca92009-10-09 09:25:16 -04005863 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005864 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005865 if (level == 1 &&
5866 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5867 goto skip;
5868
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005869 if (!wc->update_ref ||
5870 generation <= root->root_key.offset)
5871 goto skip;
5872
5873 btrfs_node_key_to_cpu(path->nodes[level], &key,
5874 path->slots[level]);
5875 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5876 if (ret < 0)
5877 goto skip;
5878
5879 wc->stage = UPDATE_BACKREF;
5880 wc->shared_level = level - 1;
5881 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005882 } else {
5883 if (level == 1 &&
5884 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5885 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005886 }
5887
5888 if (!btrfs_buffer_uptodate(next, generation)) {
5889 btrfs_tree_unlock(next);
5890 free_extent_buffer(next);
5891 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005892 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005893 }
5894
5895 if (!next) {
5896 if (reada && level == 1)
5897 reada_walk_down(trans, root, wc, path);
5898 next = read_tree_block(root, bytenr, blocksize, generation);
5899 btrfs_tree_lock(next);
5900 btrfs_set_lock_blocking(next);
5901 }
5902
5903 level--;
5904 BUG_ON(level != btrfs_header_level(next));
5905 path->nodes[level] = next;
5906 path->slots[level] = 0;
5907 path->locks[level] = 1;
5908 wc->level = level;
5909 if (wc->level == 1)
5910 wc->reada_slot = 0;
5911 return 0;
5912skip:
5913 wc->refs[level - 1] = 0;
5914 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005915 if (wc->stage == DROP_REFERENCE) {
5916 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5917 parent = path->nodes[level]->start;
5918 } else {
5919 BUG_ON(root->root_key.objectid !=
5920 btrfs_header_owner(path->nodes[level]));
5921 parent = 0;
5922 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005923
Yan, Zheng94fcca92009-10-09 09:25:16 -04005924 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5925 root->root_key.objectid, level - 1, 0);
5926 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005927 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005928 btrfs_tree_unlock(next);
5929 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04005930 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005931 return 1;
5932}
5933
5934/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005935 * hepler to process tree block while walking up the tree.
5936 *
5937 * when wc->stage == DROP_REFERENCE, this function drops
5938 * reference count on the block.
5939 *
5940 * when wc->stage == UPDATE_BACKREF, this function changes
5941 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5942 * to UPDATE_BACKREF previously while processing the block.
5943 *
5944 * NOTE: return value 1 means we should stop walking up.
5945 */
5946static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5947 struct btrfs_root *root,
5948 struct btrfs_path *path,
5949 struct walk_control *wc)
5950{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005951 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005952 int level = wc->level;
5953 struct extent_buffer *eb = path->nodes[level];
5954 u64 parent = 0;
5955
5956 if (wc->stage == UPDATE_BACKREF) {
5957 BUG_ON(wc->shared_level < level);
5958 if (level < wc->shared_level)
5959 goto out;
5960
Yan Zheng2c47e6052009-06-27 21:07:35 -04005961 ret = find_next_key(path, level + 1, &wc->update_progress);
5962 if (ret > 0)
5963 wc->update_ref = 0;
5964
5965 wc->stage = DROP_REFERENCE;
5966 wc->shared_level = -1;
5967 path->slots[level] = 0;
5968
5969 /*
5970 * check reference count again if the block isn't locked.
5971 * we should start walking down the tree again if reference
5972 * count is one.
5973 */
5974 if (!path->locks[level]) {
5975 BUG_ON(level == 0);
5976 btrfs_tree_lock(eb);
5977 btrfs_set_lock_blocking(eb);
5978 path->locks[level] = 1;
5979
5980 ret = btrfs_lookup_extent_info(trans, root,
5981 eb->start, eb->len,
5982 &wc->refs[level],
5983 &wc->flags[level]);
5984 BUG_ON(ret);
5985 BUG_ON(wc->refs[level] == 0);
5986 if (wc->refs[level] == 1) {
5987 btrfs_tree_unlock(eb);
5988 path->locks[level] = 0;
5989 return 1;
5990 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005991 }
5992 }
5993
5994 /* wc->stage == DROP_REFERENCE */
5995 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5996
5997 if (wc->refs[level] == 1) {
5998 if (level == 0) {
5999 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6000 ret = btrfs_dec_ref(trans, root, eb, 1);
6001 else
6002 ret = btrfs_dec_ref(trans, root, eb, 0);
6003 BUG_ON(ret);
6004 }
6005 /* make block locked assertion in clean_tree_block happy */
6006 if (!path->locks[level] &&
6007 btrfs_header_generation(eb) == trans->transid) {
6008 btrfs_tree_lock(eb);
6009 btrfs_set_lock_blocking(eb);
6010 path->locks[level] = 1;
6011 }
6012 clean_tree_block(trans, root, eb);
6013 }
6014
6015 if (eb == root->node) {
6016 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6017 parent = eb->start;
6018 else
6019 BUG_ON(root->root_key.objectid !=
6020 btrfs_header_owner(eb));
6021 } else {
6022 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6023 parent = path->nodes[level + 1]->start;
6024 else
6025 BUG_ON(root->root_key.objectid !=
6026 btrfs_header_owner(path->nodes[level + 1]));
6027 }
6028
Yan, Zhengf0486c62010-05-16 10:46:25 -04006029 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006030out:
6031 wc->refs[level] = 0;
6032 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006033 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006034}
6035
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006036static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6037 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006038 struct btrfs_path *path,
6039 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006040{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006041 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006042 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006043 int ret;
6044
Yan Zheng2c47e6052009-06-27 21:07:35 -04006045 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006046 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006047 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006048 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006049
Yan Zheng2c47e6052009-06-27 21:07:35 -04006050 if (level == 0)
6051 break;
6052
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006053 if (path->slots[level] >=
6054 btrfs_header_nritems(path->nodes[level]))
6055 break;
6056
Yan, Zheng94fcca92009-10-09 09:25:16 -04006057 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006058 if (ret > 0) {
6059 path->slots[level]++;
6060 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00006061 } else if (ret < 0)
6062 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006063 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006064 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006065 return 0;
6066}
6067
Chris Masond3977122009-01-05 21:25:51 -05006068static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006069 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006070 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006071 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006072{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006073 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006074 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006075
Yan Zheng2c47e6052009-06-27 21:07:35 -04006076 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6077 while (level < max_level && path->nodes[level]) {
6078 wc->level = level;
6079 if (path->slots[level] + 1 <
6080 btrfs_header_nritems(path->nodes[level])) {
6081 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006082 return 0;
6083 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006084 ret = walk_up_proc(trans, root, path, wc);
6085 if (ret > 0)
6086 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006087
Yan Zheng2c47e6052009-06-27 21:07:35 -04006088 if (path->locks[level]) {
6089 btrfs_tree_unlock(path->nodes[level]);
6090 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006091 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006092 free_extent_buffer(path->nodes[level]);
6093 path->nodes[level] = NULL;
6094 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006095 }
6096 }
6097 return 1;
6098}
6099
Chris Mason9aca1d52007-03-13 11:09:37 -04006100/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006101 * drop a subvolume tree.
6102 *
6103 * this function traverses the tree freeing any blocks that only
6104 * referenced by the tree.
6105 *
6106 * when a shared tree block is found. this function decreases its
6107 * reference count by one. if update_ref is true, this function
6108 * also make sure backrefs for the shared block and all lower level
6109 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006110 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006111int btrfs_drop_snapshot(struct btrfs_root *root,
6112 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006113{
Chris Mason5caf2a02007-04-02 11:20:42 -04006114 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006115 struct btrfs_trans_handle *trans;
6116 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006117 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006118 struct walk_control *wc;
6119 struct btrfs_key key;
6120 int err = 0;
6121 int ret;
6122 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006123
Chris Mason5caf2a02007-04-02 11:20:42 -04006124 path = btrfs_alloc_path();
6125 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006126
Yan Zheng2c47e6052009-06-27 21:07:35 -04006127 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6128 BUG_ON(!wc);
6129
Yan, Zhenga22285a2010-05-16 10:48:46 -04006130 trans = btrfs_start_transaction(tree_root, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006131 if (block_rsv)
6132 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006133
Chris Mason9f3a7422007-08-07 15:52:19 -04006134 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006135 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006136 path->nodes[level] = btrfs_lock_root_node(root);
6137 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006138 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006139 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006140 memset(&wc->update_progress, 0,
6141 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006142 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006143 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006144 memcpy(&wc->update_progress, &key,
6145 sizeof(wc->update_progress));
6146
Chris Mason6702ed42007-08-07 16:15:09 -04006147 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006148 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006149 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006150 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6151 path->lowest_level = 0;
6152 if (ret < 0) {
6153 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006154 goto out;
6155 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006156 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006157
Chris Mason7d9eb122008-07-08 14:19:17 -04006158 /*
6159 * unlock our path, this is safe because only this
6160 * function is allowed to delete this snapshot
6161 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006162 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006163
Yan Zheng2c47e6052009-06-27 21:07:35 -04006164 level = btrfs_header_level(root->node);
6165 while (1) {
6166 btrfs_tree_lock(path->nodes[level]);
6167 btrfs_set_lock_blocking(path->nodes[level]);
6168
6169 ret = btrfs_lookup_extent_info(trans, root,
6170 path->nodes[level]->start,
6171 path->nodes[level]->len,
6172 &wc->refs[level],
6173 &wc->flags[level]);
6174 BUG_ON(ret);
6175 BUG_ON(wc->refs[level] == 0);
6176
6177 if (level == root_item->drop_level)
6178 break;
6179
6180 btrfs_tree_unlock(path->nodes[level]);
6181 WARN_ON(wc->refs[level] != 1);
6182 level--;
6183 }
6184 }
6185
6186 wc->level = level;
6187 wc->shared_level = -1;
6188 wc->stage = DROP_REFERENCE;
6189 wc->update_ref = update_ref;
6190 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006191 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006192
6193 while (1) {
6194 ret = walk_down_tree(trans, root, path, wc);
6195 if (ret < 0) {
6196 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006197 break;
6198 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006199
6200 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6201 if (ret < 0) {
6202 err = ret;
6203 break;
6204 }
6205
6206 if (ret > 0) {
6207 BUG_ON(wc->stage != DROP_REFERENCE);
6208 break;
6209 }
6210
6211 if (wc->stage == DROP_REFERENCE) {
6212 level = wc->level;
6213 btrfs_node_key(path->nodes[level],
6214 &root_item->drop_progress,
6215 path->slots[level]);
6216 root_item->drop_level = level;
6217 }
6218
6219 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006220 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006221 ret = btrfs_update_root(trans, tree_root,
6222 &root->root_key,
6223 root_item);
6224 BUG_ON(ret);
6225
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006226 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006227 trans = btrfs_start_transaction(tree_root, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006228 if (block_rsv)
6229 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006230 }
Chris Mason20524f02007-03-10 06:35:47 -05006231 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006232 btrfs_release_path(root, path);
6233 BUG_ON(err);
6234
6235 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6236 BUG_ON(ret);
6237
Yan, Zheng76dda932009-09-21 16:00:26 -04006238 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6239 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6240 NULL, NULL);
6241 BUG_ON(ret < 0);
6242 if (ret > 0) {
6243 ret = btrfs_del_orphan_item(trans, tree_root,
6244 root->root_key.objectid);
6245 BUG_ON(ret);
6246 }
6247 }
6248
6249 if (root->in_radix) {
6250 btrfs_free_fs_root(tree_root->fs_info, root);
6251 } else {
6252 free_extent_buffer(root->node);
6253 free_extent_buffer(root->commit_root);
6254 kfree(root);
6255 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006256out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006257 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006258 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006259 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006260 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006261}
Chris Mason9078a3e2007-04-26 16:46:15 -04006262
Yan Zheng2c47e6052009-06-27 21:07:35 -04006263/*
6264 * drop subtree rooted at tree block 'node'.
6265 *
6266 * NOTE: this function will unlock and release tree block 'node'
6267 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006268int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6269 struct btrfs_root *root,
6270 struct extent_buffer *node,
6271 struct extent_buffer *parent)
6272{
6273 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006274 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006275 int level;
6276 int parent_level;
6277 int ret = 0;
6278 int wret;
6279
Yan Zheng2c47e6052009-06-27 21:07:35 -04006280 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6281
Yan Zhengf82d02d2008-10-29 14:49:05 -04006282 path = btrfs_alloc_path();
6283 BUG_ON(!path);
6284
Yan Zheng2c47e6052009-06-27 21:07:35 -04006285 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6286 BUG_ON(!wc);
6287
Chris Masonb9447ef2009-03-09 11:45:38 -04006288 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006289 parent_level = btrfs_header_level(parent);
6290 extent_buffer_get(parent);
6291 path->nodes[parent_level] = parent;
6292 path->slots[parent_level] = btrfs_header_nritems(parent);
6293
Chris Masonb9447ef2009-03-09 11:45:38 -04006294 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006295 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006296 path->nodes[level] = node;
6297 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006298 path->locks[level] = 1;
6299
6300 wc->refs[parent_level] = 1;
6301 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6302 wc->level = level;
6303 wc->shared_level = -1;
6304 wc->stage = DROP_REFERENCE;
6305 wc->update_ref = 0;
6306 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006307 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006308
6309 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006310 wret = walk_down_tree(trans, root, path, wc);
6311 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006312 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006313 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006314 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006315
Yan Zheng2c47e6052009-06-27 21:07:35 -04006316 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006317 if (wret < 0)
6318 ret = wret;
6319 if (wret != 0)
6320 break;
6321 }
6322
Yan Zheng2c47e6052009-06-27 21:07:35 -04006323 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006324 btrfs_free_path(path);
6325 return ret;
6326}
6327
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006328#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04006329static unsigned long calc_ra(unsigned long start, unsigned long last,
6330 unsigned long nr)
6331{
6332 return min(last, start + nr - 1);
6333}
6334
Chris Masond3977122009-01-05 21:25:51 -05006335static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05006336 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05006337{
6338 u64 page_start;
6339 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04006340 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006341 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006342 unsigned long i;
6343 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05006344 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05006345 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04006346 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04006347 unsigned int total_read = 0;
6348 unsigned int total_dirty = 0;
6349 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05006350
6351 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006352
6353 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006354 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05006355 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6356
Zheng Yan1a40e232008-09-26 10:09:34 -04006357 /* make sure the dirty trick played by the caller work */
6358 ret = invalidate_inode_pages2_range(inode->i_mapping,
6359 first_index, last_index);
6360 if (ret)
6361 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04006362
Chris Mason4313b392008-01-03 09:08:48 -05006363 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05006364
Zheng Yan1a40e232008-09-26 10:09:34 -04006365 for (i = first_index ; i <= last_index; i++) {
6366 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04006367 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04006368 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04006369 }
6370 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04006371again:
6372 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04006373 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05006374 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04006375 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006376 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006377 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04006378 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006379 if (!PageUptodate(page)) {
6380 btrfs_readpage(NULL, page);
6381 lock_page(page);
6382 if (!PageUptodate(page)) {
6383 unlock_page(page);
6384 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006385 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05006386 goto out_unlock;
6387 }
6388 }
Chris Masonec44a352008-04-28 15:29:52 -04006389 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04006390
Chris Masonedbd8d42007-12-21 16:27:24 -05006391 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6392 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006393 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006394
Chris Mason3eaa2882008-07-24 11:57:52 -04006395 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6396 if (ordered) {
6397 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6398 unlock_page(page);
6399 page_cache_release(page);
6400 btrfs_start_ordered_extent(inode, ordered, 1);
6401 btrfs_put_ordered_extent(ordered);
6402 goto again;
6403 }
6404 set_page_extent_mapped(page);
6405
Zheng Yan1a40e232008-09-26 10:09:34 -04006406 if (i == first_index)
6407 set_extent_bits(io_tree, page_start, page_end,
6408 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006409 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04006410
Chris Masona061fc82008-05-07 11:43:44 -04006411 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006412 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006413
Chris Masond1310b22008-01-24 16:13:08 -05006414 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006415 unlock_page(page);
6416 page_cache_release(page);
6417 }
6418
6419out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04006420 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05006421 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006422 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04006423 return ret;
6424}
6425
Chris Masond3977122009-01-05 21:25:51 -05006426static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006427 struct btrfs_key *extent_key,
6428 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05006429{
Zheng Yan1a40e232008-09-26 10:09:34 -04006430 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6431 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6432 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04006433 u64 start = extent_key->objectid - offset;
6434 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006435
6436 em = alloc_extent_map(GFP_NOFS);
6437 BUG_ON(!em || IS_ERR(em));
6438
Yan Zheng66435582008-10-30 14:19:50 -04006439 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006440 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04006441 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04006442 em->block_start = extent_key->objectid;
6443 em->bdev = root->fs_info->fs_devices->latest_bdev;
6444 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6445
6446 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04006447 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006448 while (1) {
6449 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04006450 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006451 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04006452 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006453 if (ret != -EEXIST) {
6454 free_extent_map(em);
6455 break;
6456 }
Yan Zheng66435582008-10-30 14:19:50 -04006457 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006458 }
Yan Zheng66435582008-10-30 14:19:50 -04006459 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006460
Yan Zheng66435582008-10-30 14:19:50 -04006461 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04006462}
6463
6464struct btrfs_ref_path {
6465 u64 extent_start;
6466 u64 nodes[BTRFS_MAX_LEVEL];
6467 u64 root_objectid;
6468 u64 root_generation;
6469 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006470 u32 num_refs;
6471 int lowest_level;
6472 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006473 int shared_level;
6474
6475 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6476 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04006477};
6478
6479struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04006480 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04006481 u64 disk_bytenr;
6482 u64 disk_num_bytes;
6483 u64 offset;
6484 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04006485 u8 compression;
6486 u8 encryption;
6487 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04006488};
6489
6490static int is_cowonly_root(u64 root_objectid)
6491{
6492 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6493 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6494 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6495 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05006496 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6497 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04006498 return 1;
6499 return 0;
6500}
6501
Chris Masond3977122009-01-05 21:25:51 -05006502static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006503 struct btrfs_root *extent_root,
6504 struct btrfs_ref_path *ref_path,
6505 int first_time)
6506{
6507 struct extent_buffer *leaf;
6508 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05006509 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05006510 struct btrfs_key key;
6511 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04006512 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05006513 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04006514 int level;
6515 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006516
Zheng Yan1a40e232008-09-26 10:09:34 -04006517 path = btrfs_alloc_path();
6518 if (!path)
6519 return -ENOMEM;
6520
Zheng Yan1a40e232008-09-26 10:09:34 -04006521 if (first_time) {
6522 ref_path->lowest_level = -1;
6523 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006524 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006525 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04006526 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006527walk_down:
6528 level = ref_path->current_level - 1;
6529 while (level >= -1) {
6530 u64 parent;
6531 if (level < ref_path->lowest_level)
6532 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006533
Chris Masond3977122009-01-05 21:25:51 -05006534 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006535 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006536 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006537 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006538 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006539
Zheng Yan1a40e232008-09-26 10:09:34 -04006540 parent = ref_path->nodes[level + 1];
6541 ref_path->nodes[level + 1] = 0;
6542 ref_path->current_level = level;
6543 BUG_ON(parent == 0);
6544
6545 key.objectid = bytenr;
6546 key.offset = parent + 1;
6547 key.type = BTRFS_EXTENT_REF_KEY;
6548
6549 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006550 if (ret < 0)
6551 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006552 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006553
Chris Masonedbd8d42007-12-21 16:27:24 -05006554 leaf = path->nodes[0];
6555 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04006556 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04006557 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04006558 if (ret < 0)
6559 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006560 if (ret > 0)
6561 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04006562 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04006563 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006564
6565 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04006566 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04006567 found_key.type == BTRFS_EXTENT_REF_KEY) {
6568 if (level < ref_path->shared_level)
6569 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006570 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006571 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006572next:
6573 level--;
6574 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006575 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006576 }
6577 /* reached lowest level */
6578 ret = 1;
6579 goto out;
6580walk_up:
6581 level = ref_path->current_level;
6582 while (level < BTRFS_MAX_LEVEL - 1) {
6583 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006584
6585 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006586 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006587 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006588 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006589
Zheng Yan1a40e232008-09-26 10:09:34 -04006590 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006591
Zheng Yan1a40e232008-09-26 10:09:34 -04006592 key.objectid = bytenr;
6593 key.offset = 0;
6594 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006595
Zheng Yan1a40e232008-09-26 10:09:34 -04006596 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6597 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006598 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006599
6600 leaf = path->nodes[0];
6601 nritems = btrfs_header_nritems(leaf);
6602 if (path->slots[0] >= nritems) {
6603 ret = btrfs_next_leaf(extent_root, path);
6604 if (ret < 0)
6605 goto out;
6606 if (ret > 0) {
6607 /* the extent was freed by someone */
6608 if (ref_path->lowest_level == level)
6609 goto out;
6610 btrfs_release_path(extent_root, path);
6611 goto walk_down;
6612 }
6613 leaf = path->nodes[0];
6614 }
6615
6616 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6617 if (found_key.objectid != bytenr ||
6618 found_key.type != BTRFS_EXTENT_REF_KEY) {
6619 /* the extent was freed by someone */
6620 if (ref_path->lowest_level == level) {
6621 ret = 1;
6622 goto out;
6623 }
6624 btrfs_release_path(extent_root, path);
6625 goto walk_down;
6626 }
6627found:
6628 ref = btrfs_item_ptr(leaf, path->slots[0],
6629 struct btrfs_extent_ref);
6630 ref_objectid = btrfs_ref_objectid(leaf, ref);
6631 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6632 if (first_time) {
6633 level = (int)ref_objectid;
6634 BUG_ON(level >= BTRFS_MAX_LEVEL);
6635 ref_path->lowest_level = level;
6636 ref_path->current_level = level;
6637 ref_path->nodes[level] = bytenr;
6638 } else {
6639 WARN_ON(ref_objectid != level);
6640 }
6641 } else {
6642 WARN_ON(level != -1);
6643 }
6644 first_time = 0;
6645
6646 if (ref_path->lowest_level == level) {
6647 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006648 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6649 }
6650
6651 /*
6652 * the block is tree root or the block isn't in reference
6653 * counted tree.
6654 */
6655 if (found_key.objectid == found_key.offset ||
6656 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6657 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6658 ref_path->root_generation =
6659 btrfs_ref_generation(leaf, ref);
6660 if (level < 0) {
6661 /* special reference from the tree log */
6662 ref_path->nodes[0] = found_key.offset;
6663 ref_path->current_level = 0;
6664 }
6665 ret = 0;
6666 goto out;
6667 }
6668
6669 level++;
6670 BUG_ON(ref_path->nodes[level] != 0);
6671 ref_path->nodes[level] = found_key.offset;
6672 ref_path->current_level = level;
6673
6674 /*
6675 * the reference was created in the running transaction,
6676 * no need to continue walking up.
6677 */
6678 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6679 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6680 ref_path->root_generation =
6681 btrfs_ref_generation(leaf, ref);
6682 ret = 0;
6683 goto out;
6684 }
6685
6686 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006687 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006688 }
6689 /* reached max tree level, but no tree root found. */
6690 BUG();
6691out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006692 btrfs_free_path(path);
6693 return ret;
6694}
6695
6696static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6697 struct btrfs_root *extent_root,
6698 struct btrfs_ref_path *ref_path,
6699 u64 extent_start)
6700{
6701 memset(ref_path, 0, sizeof(*ref_path));
6702 ref_path->extent_start = extent_start;
6703
6704 return __next_ref_path(trans, extent_root, ref_path, 1);
6705}
6706
6707static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6708 struct btrfs_root *extent_root,
6709 struct btrfs_ref_path *ref_path)
6710{
6711 return __next_ref_path(trans, extent_root, ref_path, 0);
6712}
6713
Chris Masond3977122009-01-05 21:25:51 -05006714static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006715 struct btrfs_key *extent_key,
6716 u64 offset, int no_fragment,
6717 struct disk_extent **extents,
6718 int *nr_extents)
6719{
6720 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6721 struct btrfs_path *path;
6722 struct btrfs_file_extent_item *fi;
6723 struct extent_buffer *leaf;
6724 struct disk_extent *exts = *extents;
6725 struct btrfs_key found_key;
6726 u64 cur_pos;
6727 u64 last_byte;
6728 u32 nritems;
6729 int nr = 0;
6730 int max = *nr_extents;
6731 int ret;
6732
6733 WARN_ON(!no_fragment && *extents);
6734 if (!exts) {
6735 max = 1;
6736 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6737 if (!exts)
6738 return -ENOMEM;
6739 }
6740
6741 path = btrfs_alloc_path();
6742 BUG_ON(!path);
6743
6744 cur_pos = extent_key->objectid - offset;
6745 last_byte = extent_key->objectid + extent_key->offset;
6746 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6747 cur_pos, 0);
6748 if (ret < 0)
6749 goto out;
6750 if (ret > 0) {
6751 ret = -ENOENT;
6752 goto out;
6753 }
6754
6755 while (1) {
6756 leaf = path->nodes[0];
6757 nritems = btrfs_header_nritems(leaf);
6758 if (path->slots[0] >= nritems) {
6759 ret = btrfs_next_leaf(root, path);
6760 if (ret < 0)
6761 goto out;
6762 if (ret > 0)
6763 break;
6764 leaf = path->nodes[0];
6765 }
6766
6767 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6768 if (found_key.offset != cur_pos ||
6769 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6770 found_key.objectid != reloc_inode->i_ino)
6771 break;
6772
6773 fi = btrfs_item_ptr(leaf, path->slots[0],
6774 struct btrfs_file_extent_item);
6775 if (btrfs_file_extent_type(leaf, fi) !=
6776 BTRFS_FILE_EXTENT_REG ||
6777 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6778 break;
6779
6780 if (nr == max) {
6781 struct disk_extent *old = exts;
6782 max *= 2;
6783 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6784 memcpy(exts, old, sizeof(*exts) * nr);
6785 if (old != *extents)
6786 kfree(old);
6787 }
6788
6789 exts[nr].disk_bytenr =
6790 btrfs_file_extent_disk_bytenr(leaf, fi);
6791 exts[nr].disk_num_bytes =
6792 btrfs_file_extent_disk_num_bytes(leaf, fi);
6793 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6794 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04006795 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6796 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6797 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6798 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6799 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04006800 BUG_ON(exts[nr].offset > 0);
6801 BUG_ON(exts[nr].compression || exts[nr].encryption);
6802 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006803
6804 cur_pos += exts[nr].num_bytes;
6805 nr++;
6806
6807 if (cur_pos + offset >= last_byte)
6808 break;
6809
6810 if (no_fragment) {
6811 ret = 1;
6812 goto out;
6813 }
6814 path->slots[0]++;
6815 }
6816
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006817 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04006818 if (cur_pos + offset < last_byte) {
6819 ret = -ENOENT;
6820 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05006821 }
6822 ret = 0;
6823out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006824 btrfs_free_path(path);
6825 if (ret) {
6826 if (exts != *extents)
6827 kfree(exts);
6828 } else {
6829 *extents = exts;
6830 *nr_extents = nr;
6831 }
6832 return ret;
6833}
6834
Chris Masond3977122009-01-05 21:25:51 -05006835static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006836 struct btrfs_root *root,
6837 struct btrfs_path *path,
6838 struct btrfs_key *extent_key,
6839 struct btrfs_key *leaf_key,
6840 struct btrfs_ref_path *ref_path,
6841 struct disk_extent *new_extents,
6842 int nr_extents)
6843{
6844 struct extent_buffer *leaf;
6845 struct btrfs_file_extent_item *fi;
6846 struct inode *inode = NULL;
6847 struct btrfs_key key;
6848 u64 lock_start = 0;
6849 u64 lock_end = 0;
6850 u64 num_bytes;
6851 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05006852 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006853 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006854 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006855 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04006856 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04006857 int ret;
6858
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006859 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04006860 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006861 if (key.objectid < ref_path->owner_objectid ||
6862 (key.objectid == ref_path->owner_objectid &&
6863 key.type < BTRFS_EXTENT_DATA_KEY)) {
6864 key.objectid = ref_path->owner_objectid;
6865 key.type = BTRFS_EXTENT_DATA_KEY;
6866 key.offset = 0;
6867 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006868 }
6869
6870 while (1) {
6871 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6872 if (ret < 0)
6873 goto out;
6874
6875 leaf = path->nodes[0];
6876 nritems = btrfs_header_nritems(leaf);
6877next:
6878 if (extent_locked && ret > 0) {
6879 /*
6880 * the file extent item was modified by someone
6881 * before the extent got locked.
6882 */
Zheng Yan1a40e232008-09-26 10:09:34 -04006883 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6884 lock_end, GFP_NOFS);
6885 extent_locked = 0;
6886 }
6887
6888 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006889 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04006890 break;
6891
6892 BUG_ON(extent_locked);
6893 ret = btrfs_next_leaf(root, path);
6894 if (ret < 0)
6895 goto out;
6896 if (ret > 0)
6897 break;
6898 leaf = path->nodes[0];
6899 nritems = btrfs_header_nritems(leaf);
6900 }
6901
6902 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6903
6904 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6905 if ((key.objectid > ref_path->owner_objectid) ||
6906 (key.objectid == ref_path->owner_objectid &&
6907 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05006908 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006909 break;
6910 }
6911
6912 if (inode && key.objectid != inode->i_ino) {
6913 BUG_ON(extent_locked);
6914 btrfs_release_path(root, path);
6915 mutex_unlock(&inode->i_mutex);
6916 iput(inode);
6917 inode = NULL;
6918 continue;
6919 }
6920
6921 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6922 path->slots[0]++;
6923 ret = 1;
6924 goto next;
6925 }
6926 fi = btrfs_item_ptr(leaf, path->slots[0],
6927 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04006928 extent_type = btrfs_file_extent_type(leaf, fi);
6929 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6930 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04006931 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6932 extent_key->objectid)) {
6933 path->slots[0]++;
6934 ret = 1;
6935 goto next;
6936 }
6937
6938 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6939 ext_offset = btrfs_file_extent_offset(leaf, fi);
6940
Yan Zheng86288a12009-01-21 10:49:16 -05006941 if (search_end == (u64)-1) {
6942 search_end = key.offset - ext_offset +
6943 btrfs_file_extent_ram_bytes(leaf, fi);
6944 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006945
6946 if (!extent_locked) {
6947 lock_start = key.offset;
6948 lock_end = lock_start + num_bytes - 1;
6949 } else {
Yan Zheng66435582008-10-30 14:19:50 -04006950 if (lock_start > key.offset ||
6951 lock_end + 1 < key.offset + num_bytes) {
6952 unlock_extent(&BTRFS_I(inode)->io_tree,
6953 lock_start, lock_end, GFP_NOFS);
6954 extent_locked = 0;
6955 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006956 }
6957
6958 if (!inode) {
6959 btrfs_release_path(root, path);
6960
6961 inode = btrfs_iget_locked(root->fs_info->sb,
6962 key.objectid, root);
6963 if (inode->i_state & I_NEW) {
6964 BTRFS_I(inode)->root = root;
6965 BTRFS_I(inode)->location.objectid =
6966 key.objectid;
6967 BTRFS_I(inode)->location.type =
6968 BTRFS_INODE_ITEM_KEY;
6969 BTRFS_I(inode)->location.offset = 0;
6970 btrfs_read_locked_inode(inode);
6971 unlock_new_inode(inode);
6972 }
6973 /*
6974 * some code call btrfs_commit_transaction while
6975 * holding the i_mutex, so we can't use mutex_lock
6976 * here.
6977 */
6978 if (is_bad_inode(inode) ||
6979 !mutex_trylock(&inode->i_mutex)) {
6980 iput(inode);
6981 inode = NULL;
6982 key.offset = (u64)-1;
6983 goto skip;
6984 }
6985 }
6986
6987 if (!extent_locked) {
6988 struct btrfs_ordered_extent *ordered;
6989
6990 btrfs_release_path(root, path);
6991
6992 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6993 lock_end, GFP_NOFS);
6994 ordered = btrfs_lookup_first_ordered_extent(inode,
6995 lock_end);
6996 if (ordered &&
6997 ordered->file_offset <= lock_end &&
6998 ordered->file_offset + ordered->len > lock_start) {
6999 unlock_extent(&BTRFS_I(inode)->io_tree,
7000 lock_start, lock_end, GFP_NOFS);
7001 btrfs_start_ordered_extent(inode, ordered, 1);
7002 btrfs_put_ordered_extent(ordered);
7003 key.offset += num_bytes;
7004 goto skip;
7005 }
7006 if (ordered)
7007 btrfs_put_ordered_extent(ordered);
7008
Zheng Yan1a40e232008-09-26 10:09:34 -04007009 extent_locked = 1;
7010 continue;
7011 }
7012
7013 if (nr_extents == 1) {
7014 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04007015 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7016 new_extents[0].disk_bytenr);
7017 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7018 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007019 btrfs_mark_buffer_dirty(leaf);
7020
7021 btrfs_drop_extent_cache(inode, key.offset,
7022 key.offset + num_bytes - 1, 0);
7023
7024 ret = btrfs_inc_extent_ref(trans, root,
7025 new_extents[0].disk_bytenr,
7026 new_extents[0].disk_num_bytes,
7027 leaf->start,
7028 root->root_key.objectid,
7029 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007030 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007031 BUG_ON(ret);
7032
7033 ret = btrfs_free_extent(trans, root,
7034 extent_key->objectid,
7035 extent_key->offset,
7036 leaf->start,
7037 btrfs_header_owner(leaf),
7038 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007039 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007040 BUG_ON(ret);
7041
7042 btrfs_release_path(root, path);
7043 key.offset += num_bytes;
7044 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04007045 BUG_ON(1);
7046#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04007047 u64 alloc_hint;
7048 u64 extent_len;
7049 int i;
7050 /*
7051 * drop old extent pointer at first, then insert the
7052 * new pointers one bye one
7053 */
7054 btrfs_release_path(root, path);
7055 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7056 key.offset + num_bytes,
7057 key.offset, &alloc_hint);
7058 BUG_ON(ret);
7059
7060 for (i = 0; i < nr_extents; i++) {
7061 if (ext_offset >= new_extents[i].num_bytes) {
7062 ext_offset -= new_extents[i].num_bytes;
7063 continue;
7064 }
7065 extent_len = min(new_extents[i].num_bytes -
7066 ext_offset, num_bytes);
7067
7068 ret = btrfs_insert_empty_item(trans, root,
7069 path, &key,
7070 sizeof(*fi));
7071 BUG_ON(ret);
7072
7073 leaf = path->nodes[0];
7074 fi = btrfs_item_ptr(leaf, path->slots[0],
7075 struct btrfs_file_extent_item);
7076 btrfs_set_file_extent_generation(leaf, fi,
7077 trans->transid);
7078 btrfs_set_file_extent_type(leaf, fi,
7079 BTRFS_FILE_EXTENT_REG);
7080 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7081 new_extents[i].disk_bytenr);
7082 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7083 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04007084 btrfs_set_file_extent_ram_bytes(leaf, fi,
7085 new_extents[i].ram_bytes);
7086
7087 btrfs_set_file_extent_compression(leaf, fi,
7088 new_extents[i].compression);
7089 btrfs_set_file_extent_encryption(leaf, fi,
7090 new_extents[i].encryption);
7091 btrfs_set_file_extent_other_encoding(leaf, fi,
7092 new_extents[i].other_encoding);
7093
Zheng Yan1a40e232008-09-26 10:09:34 -04007094 btrfs_set_file_extent_num_bytes(leaf, fi,
7095 extent_len);
7096 ext_offset += new_extents[i].offset;
7097 btrfs_set_file_extent_offset(leaf, fi,
7098 ext_offset);
7099 btrfs_mark_buffer_dirty(leaf);
7100
7101 btrfs_drop_extent_cache(inode, key.offset,
7102 key.offset + extent_len - 1, 0);
7103
7104 ret = btrfs_inc_extent_ref(trans, root,
7105 new_extents[i].disk_bytenr,
7106 new_extents[i].disk_num_bytes,
7107 leaf->start,
7108 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007109 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007110 BUG_ON(ret);
7111 btrfs_release_path(root, path);
7112
Yan Zhenga76a3cd2008-10-09 11:46:29 -04007113 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04007114
7115 ext_offset = 0;
7116 num_bytes -= extent_len;
7117 key.offset += extent_len;
7118
7119 if (num_bytes == 0)
7120 break;
7121 }
7122 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007123#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04007124 }
7125
7126 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007127 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7128 lock_end, GFP_NOFS);
7129 extent_locked = 0;
7130 }
7131skip:
7132 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05007133 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007134 break;
7135
7136 cond_resched();
7137 }
7138 ret = 0;
7139out:
7140 btrfs_release_path(root, path);
7141 if (inode) {
7142 mutex_unlock(&inode->i_mutex);
7143 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007144 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7145 lock_end, GFP_NOFS);
7146 }
7147 iput(inode);
7148 }
7149 return ret;
7150}
7151
Zheng Yan1a40e232008-09-26 10:09:34 -04007152int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7153 struct btrfs_root *root,
7154 struct extent_buffer *buf, u64 orig_start)
7155{
7156 int level;
7157 int ret;
7158
7159 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7160 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7161
7162 level = btrfs_header_level(buf);
7163 if (level == 0) {
7164 struct btrfs_leaf_ref *ref;
7165 struct btrfs_leaf_ref *orig_ref;
7166
7167 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7168 if (!orig_ref)
7169 return -ENOENT;
7170
7171 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7172 if (!ref) {
7173 btrfs_free_leaf_ref(root, orig_ref);
7174 return -ENOMEM;
7175 }
7176
7177 ref->nritems = orig_ref->nritems;
7178 memcpy(ref->extents, orig_ref->extents,
7179 sizeof(ref->extents[0]) * ref->nritems);
7180
7181 btrfs_free_leaf_ref(root, orig_ref);
7182
7183 ref->root_gen = trans->transid;
7184 ref->bytenr = buf->start;
7185 ref->owner = btrfs_header_owner(buf);
7186 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05007187
Zheng Yan1a40e232008-09-26 10:09:34 -04007188 ret = btrfs_add_leaf_ref(root, ref, 0);
7189 WARN_ON(ret);
7190 btrfs_free_leaf_ref(root, ref);
7191 }
7192 return 0;
7193}
7194
Chris Masond3977122009-01-05 21:25:51 -05007195static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007196 struct extent_buffer *leaf,
7197 struct btrfs_block_group_cache *group,
7198 struct btrfs_root *target_root)
7199{
7200 struct btrfs_key key;
7201 struct inode *inode = NULL;
7202 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007203 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04007204 u64 num_bytes;
7205 u64 skip_objectid = 0;
7206 u32 nritems;
7207 u32 i;
7208
7209 nritems = btrfs_header_nritems(leaf);
7210 for (i = 0; i < nritems; i++) {
7211 btrfs_item_key_to_cpu(leaf, &key, i);
7212 if (key.objectid == skip_objectid ||
7213 key.type != BTRFS_EXTENT_DATA_KEY)
7214 continue;
7215 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7216 if (btrfs_file_extent_type(leaf, fi) ==
7217 BTRFS_FILE_EXTENT_INLINE)
7218 continue;
7219 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7220 continue;
7221 if (!inode || inode->i_ino != key.objectid) {
7222 iput(inode);
7223 inode = btrfs_ilookup(target_root->fs_info->sb,
7224 key.objectid, target_root, 1);
7225 }
7226 if (!inode) {
7227 skip_objectid = key.objectid;
7228 continue;
7229 }
7230 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7231
Josef Bacik2ac55d42010-02-03 19:33:23 +00007232 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7233 key.offset + num_bytes - 1, 0, &cached_state,
7234 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007235 btrfs_drop_extent_cache(inode, key.offset,
7236 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007237 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7238 key.offset + num_bytes - 1, &cached_state,
7239 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007240 cond_resched();
7241 }
7242 iput(inode);
7243 return 0;
7244}
7245
Chris Masond3977122009-01-05 21:25:51 -05007246static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007247 struct btrfs_root *root,
7248 struct extent_buffer *leaf,
7249 struct btrfs_block_group_cache *group,
7250 struct inode *reloc_inode)
7251{
7252 struct btrfs_key key;
7253 struct btrfs_key extent_key;
7254 struct btrfs_file_extent_item *fi;
7255 struct btrfs_leaf_ref *ref;
7256 struct disk_extent *new_extent;
7257 u64 bytenr;
7258 u64 num_bytes;
7259 u32 nritems;
7260 u32 i;
7261 int ext_index;
7262 int nr_extent;
7263 int ret;
7264
7265 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
7266 BUG_ON(!new_extent);
7267
7268 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7269 BUG_ON(!ref);
7270
7271 ext_index = -1;
7272 nritems = btrfs_header_nritems(leaf);
7273 for (i = 0; i < nritems; i++) {
7274 btrfs_item_key_to_cpu(leaf, &key, i);
7275 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7276 continue;
7277 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7278 if (btrfs_file_extent_type(leaf, fi) ==
7279 BTRFS_FILE_EXTENT_INLINE)
7280 continue;
7281 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7282 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7283 if (bytenr == 0)
7284 continue;
7285
7286 ext_index++;
7287 if (bytenr >= group->key.objectid + group->key.offset ||
7288 bytenr + num_bytes <= group->key.objectid)
7289 continue;
7290
7291 extent_key.objectid = bytenr;
7292 extent_key.offset = num_bytes;
7293 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7294 nr_extent = 1;
7295 ret = get_new_locations(reloc_inode, &extent_key,
7296 group->key.objectid, 1,
7297 &new_extent, &nr_extent);
7298 if (ret > 0)
7299 continue;
7300 BUG_ON(ret < 0);
7301
7302 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7303 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7304 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7305 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7306
Zheng Yan1a40e232008-09-26 10:09:34 -04007307 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7308 new_extent->disk_bytenr);
7309 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7310 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007311 btrfs_mark_buffer_dirty(leaf);
7312
7313 ret = btrfs_inc_extent_ref(trans, root,
7314 new_extent->disk_bytenr,
7315 new_extent->disk_num_bytes,
7316 leaf->start,
7317 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007318 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007319 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04007320
Zheng Yan1a40e232008-09-26 10:09:34 -04007321 ret = btrfs_free_extent(trans, root,
7322 bytenr, num_bytes, leaf->start,
7323 btrfs_header_owner(leaf),
7324 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007325 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007326 BUG_ON(ret);
7327 cond_resched();
7328 }
7329 kfree(new_extent);
7330 BUG_ON(ext_index + 1 != ref->nritems);
7331 btrfs_free_leaf_ref(root, ref);
7332 return 0;
7333}
7334
Yan Zhengf82d02d2008-10-29 14:49:05 -04007335int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7336 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04007337{
7338 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007339 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007340
7341 if (root->reloc_root) {
7342 reloc_root = root->reloc_root;
7343 root->reloc_root = NULL;
7344 list_add(&reloc_root->dead_list,
7345 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007346
7347 btrfs_set_root_bytenr(&reloc_root->root_item,
7348 reloc_root->node->start);
7349 btrfs_set_root_level(&root->root_item,
7350 btrfs_header_level(reloc_root->node));
7351 memset(&reloc_root->root_item.drop_progress, 0,
7352 sizeof(struct btrfs_disk_key));
7353 reloc_root->root_item.drop_level = 0;
7354
7355 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7356 &reloc_root->root_key,
7357 &reloc_root->root_item);
7358 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007359 }
7360 return 0;
7361}
7362
7363int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7364{
7365 struct btrfs_trans_handle *trans;
7366 struct btrfs_root *reloc_root;
7367 struct btrfs_root *prev_root = NULL;
7368 struct list_head dead_roots;
7369 int ret;
7370 unsigned long nr;
7371
7372 INIT_LIST_HEAD(&dead_roots);
7373 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7374
7375 while (!list_empty(&dead_roots)) {
7376 reloc_root = list_entry(dead_roots.prev,
7377 struct btrfs_root, dead_list);
7378 list_del_init(&reloc_root->dead_list);
7379
7380 BUG_ON(reloc_root->commit_root != NULL);
7381 while (1) {
7382 trans = btrfs_join_transaction(root, 1);
7383 BUG_ON(!trans);
7384
7385 mutex_lock(&root->fs_info->drop_mutex);
7386 ret = btrfs_drop_snapshot(trans, reloc_root);
7387 if (ret != -EAGAIN)
7388 break;
7389 mutex_unlock(&root->fs_info->drop_mutex);
7390
7391 nr = trans->blocks_used;
7392 ret = btrfs_end_transaction(trans, root);
7393 BUG_ON(ret);
7394 btrfs_btree_balance_dirty(root, nr);
7395 }
7396
7397 free_extent_buffer(reloc_root->node);
7398
7399 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7400 &reloc_root->root_key);
7401 BUG_ON(ret);
7402 mutex_unlock(&root->fs_info->drop_mutex);
7403
7404 nr = trans->blocks_used;
7405 ret = btrfs_end_transaction(trans, root);
7406 BUG_ON(ret);
7407 btrfs_btree_balance_dirty(root, nr);
7408
7409 kfree(prev_root);
7410 prev_root = reloc_root;
7411 }
7412 if (prev_root) {
7413 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7414 kfree(prev_root);
7415 }
7416 return 0;
7417}
7418
7419int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7420{
7421 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7422 return 0;
7423}
7424
7425int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7426{
7427 struct btrfs_root *reloc_root;
7428 struct btrfs_trans_handle *trans;
7429 struct btrfs_key location;
7430 int found;
7431 int ret;
7432
7433 mutex_lock(&root->fs_info->tree_reloc_mutex);
7434 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7435 BUG_ON(ret);
7436 found = !list_empty(&root->fs_info->dead_reloc_roots);
7437 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7438
7439 if (found) {
7440 trans = btrfs_start_transaction(root, 1);
7441 BUG_ON(!trans);
7442 ret = btrfs_commit_transaction(trans, root);
7443 BUG_ON(ret);
7444 }
7445
7446 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7447 location.offset = (u64)-1;
7448 location.type = BTRFS_ROOT_ITEM_KEY;
7449
7450 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7451 BUG_ON(!reloc_root);
7452 btrfs_orphan_cleanup(reloc_root);
7453 return 0;
7454}
7455
Chris Masond3977122009-01-05 21:25:51 -05007456static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007457 struct btrfs_root *root)
7458{
7459 struct btrfs_root *reloc_root;
7460 struct extent_buffer *eb;
7461 struct btrfs_root_item *root_item;
7462 struct btrfs_key root_key;
7463 int ret;
7464
7465 BUG_ON(!root->ref_cows);
7466 if (root->reloc_root)
7467 return 0;
7468
7469 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7470 BUG_ON(!root_item);
7471
7472 ret = btrfs_copy_root(trans, root, root->commit_root,
7473 &eb, BTRFS_TREE_RELOC_OBJECTID);
7474 BUG_ON(ret);
7475
7476 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7477 root_key.offset = root->root_key.objectid;
7478 root_key.type = BTRFS_ROOT_ITEM_KEY;
7479
7480 memcpy(root_item, &root->root_item, sizeof(root_item));
7481 btrfs_set_root_refs(root_item, 0);
7482 btrfs_set_root_bytenr(root_item, eb->start);
7483 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04007484 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007485
7486 btrfs_tree_unlock(eb);
7487 free_extent_buffer(eb);
7488
7489 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7490 &root_key, root_item);
7491 BUG_ON(ret);
7492 kfree(root_item);
7493
7494 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7495 &root_key);
7496 BUG_ON(!reloc_root);
7497 reloc_root->last_trans = trans->transid;
7498 reloc_root->commit_root = NULL;
7499 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7500
7501 root->reloc_root = reloc_root;
7502 return 0;
7503}
7504
7505/*
7506 * Core function of space balance.
7507 *
7508 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04007509 * counted roots. There is one reloc tree for each subvol, and all
7510 * reloc trees share same root key objectid. Reloc trees are snapshots
7511 * of the latest committed roots of subvols (root->commit_root).
7512 *
7513 * To relocate a tree block referenced by a subvol, there are two steps.
7514 * COW the block through subvol's reloc tree, then update block pointer
7515 * in the subvol to point to the new block. Since all reloc trees share
7516 * same root key objectid, doing special handing for tree blocks owned
7517 * by them is easy. Once a tree block has been COWed in one reloc tree,
7518 * we can use the resulting new block directly when the same block is
7519 * required to COW again through other reloc trees. By this way, relocated
7520 * tree blocks are shared between reloc trees, so they are also shared
7521 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04007522 */
Chris Masond3977122009-01-05 21:25:51 -05007523static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007524 struct btrfs_root *root,
7525 struct btrfs_path *path,
7526 struct btrfs_key *first_key,
7527 struct btrfs_ref_path *ref_path,
7528 struct btrfs_block_group_cache *group,
7529 struct inode *reloc_inode)
7530{
7531 struct btrfs_root *reloc_root;
7532 struct extent_buffer *eb = NULL;
7533 struct btrfs_key *keys;
7534 u64 *nodes;
7535 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007536 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04007537 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007538 int ret;
7539
7540 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7541 lowest_level = ref_path->owner_objectid;
7542
Yan Zhengf82d02d2008-10-29 14:49:05 -04007543 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007544 path->lowest_level = lowest_level;
7545 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7546 BUG_ON(ret < 0);
7547 path->lowest_level = 0;
7548 btrfs_release_path(root, path);
7549 return 0;
7550 }
7551
Zheng Yan1a40e232008-09-26 10:09:34 -04007552 mutex_lock(&root->fs_info->tree_reloc_mutex);
7553 ret = init_reloc_tree(trans, root);
7554 BUG_ON(ret);
7555 reloc_root = root->reloc_root;
7556
Yan Zhengf82d02d2008-10-29 14:49:05 -04007557 shared_level = ref_path->shared_level;
7558 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007559
Yan Zhengf82d02d2008-10-29 14:49:05 -04007560 keys = ref_path->node_keys;
7561 nodes = ref_path->new_nodes;
7562 memset(&keys[shared_level + 1], 0,
7563 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7564 memset(&nodes[shared_level + 1], 0,
7565 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04007566
Yan Zhengf82d02d2008-10-29 14:49:05 -04007567 if (nodes[lowest_level] == 0) {
7568 path->lowest_level = lowest_level;
7569 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7570 0, 1);
7571 BUG_ON(ret);
7572 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7573 eb = path->nodes[level];
7574 if (!eb || eb == reloc_root->node)
7575 break;
7576 nodes[level] = eb->start;
7577 if (level == 0)
7578 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7579 else
7580 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7581 }
Yan Zheng2b820322008-11-17 21:11:30 -05007582 if (nodes[0] &&
7583 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007584 eb = path->nodes[0];
7585 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7586 group, reloc_inode);
7587 BUG_ON(ret);
7588 }
7589 btrfs_release_path(reloc_root, path);
7590 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007591 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007592 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007593 BUG_ON(ret);
7594 }
7595
Zheng Yan1a40e232008-09-26 10:09:34 -04007596 /*
7597 * replace tree blocks in the fs tree with tree blocks in
7598 * the reloc tree.
7599 */
7600 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7601 BUG_ON(ret < 0);
7602
7603 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007604 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7605 0, 0);
7606 BUG_ON(ret);
7607 extent_buffer_get(path->nodes[0]);
7608 eb = path->nodes[0];
7609 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007610 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7611 BUG_ON(ret);
7612 free_extent_buffer(eb);
7613 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007614
Yan Zhengf82d02d2008-10-29 14:49:05 -04007615 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007616 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007617 return 0;
7618}
7619
Chris Masond3977122009-01-05 21:25:51 -05007620static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007621 struct btrfs_root *root,
7622 struct btrfs_path *path,
7623 struct btrfs_key *first_key,
7624 struct btrfs_ref_path *ref_path)
7625{
7626 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007627
7628 ret = relocate_one_path(trans, root, path, first_key,
7629 ref_path, NULL, NULL);
7630 BUG_ON(ret);
7631
Zheng Yan1a40e232008-09-26 10:09:34 -04007632 return 0;
7633}
7634
Chris Masond3977122009-01-05 21:25:51 -05007635static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007636 struct btrfs_root *extent_root,
7637 struct btrfs_path *path,
7638 struct btrfs_key *extent_key)
7639{
7640 int ret;
7641
Zheng Yan1a40e232008-09-26 10:09:34 -04007642 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7643 if (ret)
7644 goto out;
7645 ret = btrfs_del_item(trans, extent_root, path);
7646out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007647 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007648 return ret;
7649}
7650
Chris Masond3977122009-01-05 21:25:51 -05007651static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007652 struct btrfs_ref_path *ref_path)
7653{
7654 struct btrfs_key root_key;
7655
7656 root_key.objectid = ref_path->root_objectid;
7657 root_key.type = BTRFS_ROOT_ITEM_KEY;
7658 if (is_cowonly_root(ref_path->root_objectid))
7659 root_key.offset = 0;
7660 else
7661 root_key.offset = (u64)-1;
7662
7663 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7664}
7665
Chris Masond3977122009-01-05 21:25:51 -05007666static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007667 struct btrfs_path *path,
7668 struct btrfs_key *extent_key,
7669 struct btrfs_block_group_cache *group,
7670 struct inode *reloc_inode, int pass)
7671{
7672 struct btrfs_trans_handle *trans;
7673 struct btrfs_root *found_root;
7674 struct btrfs_ref_path *ref_path = NULL;
7675 struct disk_extent *new_extents = NULL;
7676 int nr_extents = 0;
7677 int loops;
7678 int ret;
7679 int level;
7680 struct btrfs_key first_key;
7681 u64 prev_block = 0;
7682
Zheng Yan1a40e232008-09-26 10:09:34 -04007683
7684 trans = btrfs_start_transaction(extent_root, 1);
7685 BUG_ON(!trans);
7686
7687 if (extent_key->objectid == 0) {
7688 ret = del_extent_zero(trans, extent_root, path, extent_key);
7689 goto out;
7690 }
7691
7692 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7693 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007694 ret = -ENOMEM;
7695 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007696 }
7697
7698 for (loops = 0; ; loops++) {
7699 if (loops == 0) {
7700 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7701 extent_key->objectid);
7702 } else {
7703 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7704 }
7705 if (ret < 0)
7706 goto out;
7707 if (ret > 0)
7708 break;
7709
7710 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7711 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7712 continue;
7713
7714 found_root = read_ref_root(extent_root->fs_info, ref_path);
7715 BUG_ON(!found_root);
7716 /*
7717 * for reference counted tree, only process reference paths
7718 * rooted at the latest committed root.
7719 */
7720 if (found_root->ref_cows &&
7721 ref_path->root_generation != found_root->root_key.offset)
7722 continue;
7723
7724 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7725 if (pass == 0) {
7726 /*
7727 * copy data extents to new locations
7728 */
7729 u64 group_start = group->key.objectid;
7730 ret = relocate_data_extent(reloc_inode,
7731 extent_key,
7732 group_start);
7733 if (ret < 0)
7734 goto out;
7735 break;
7736 }
7737 level = 0;
7738 } else {
7739 level = ref_path->owner_objectid;
7740 }
7741
7742 if (prev_block != ref_path->nodes[level]) {
7743 struct extent_buffer *eb;
7744 u64 block_start = ref_path->nodes[level];
7745 u64 block_size = btrfs_level_size(found_root, level);
7746
7747 eb = read_tree_block(found_root, block_start,
7748 block_size, 0);
7749 btrfs_tree_lock(eb);
7750 BUG_ON(level != btrfs_header_level(eb));
7751
7752 if (level == 0)
7753 btrfs_item_key_to_cpu(eb, &first_key, 0);
7754 else
7755 btrfs_node_key_to_cpu(eb, &first_key, 0);
7756
7757 btrfs_tree_unlock(eb);
7758 free_extent_buffer(eb);
7759 prev_block = block_start;
7760 }
7761
Yan Zheng24562422009-02-12 14:14:53 -05007762 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007763 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05007764 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007765 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7766 /*
7767 * try to update data extent references while
7768 * keeping metadata shared between snapshots.
7769 */
7770 if (pass == 1) {
7771 ret = relocate_one_path(trans, found_root,
7772 path, &first_key, ref_path,
7773 group, reloc_inode);
7774 if (ret < 0)
7775 goto out;
7776 continue;
7777 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007778 /*
7779 * use fallback method to process the remaining
7780 * references.
7781 */
7782 if (!new_extents) {
7783 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04007784 new_extents = kmalloc(sizeof(*new_extents),
7785 GFP_NOFS);
7786 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007787 ret = get_new_locations(reloc_inode,
7788 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04007789 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04007790 &new_extents,
7791 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007792 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04007793 goto out;
7794 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007795 ret = replace_one_extent(trans, found_root,
7796 path, extent_key,
7797 &first_key, ref_path,
7798 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05007799 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007800 ret = relocate_tree_block(trans, found_root, path,
7801 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007802 }
7803 if (ret < 0)
7804 goto out;
7805 }
7806 ret = 0;
7807out:
7808 btrfs_end_transaction(trans, extent_root);
7809 kfree(new_extents);
7810 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007811 return ret;
7812}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007813#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007814
Chris Masonec44a352008-04-28 15:29:52 -04007815static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7816{
7817 u64 num_devices;
7818 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7819 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7820
Yan Zheng2b820322008-11-17 21:11:30 -05007821 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04007822 if (num_devices == 1) {
7823 stripped |= BTRFS_BLOCK_GROUP_DUP;
7824 stripped = flags & ~stripped;
7825
7826 /* turn raid0 into single device chunks */
7827 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7828 return stripped;
7829
7830 /* turn mirroring into duplication */
7831 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7832 BTRFS_BLOCK_GROUP_RAID10))
7833 return stripped | BTRFS_BLOCK_GROUP_DUP;
7834 return flags;
7835 } else {
7836 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007837 if (flags & stripped)
7838 return flags;
7839
7840 stripped |= BTRFS_BLOCK_GROUP_DUP;
7841 stripped = flags & ~stripped;
7842
7843 /* switch duplicated blocks with raid1 */
7844 if (flags & BTRFS_BLOCK_GROUP_DUP)
7845 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7846
7847 /* turn single device chunks into raid0 */
7848 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7849 }
7850 return flags;
7851}
7852
Yan, Zhengf0486c62010-05-16 10:46:25 -04007853static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04007854{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007855 struct btrfs_space_info *sinfo = cache->space_info;
7856 u64 num_bytes;
7857 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007858
Yan, Zhengf0486c62010-05-16 10:46:25 -04007859 if (cache->ro)
7860 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04007861
Yan, Zhengf0486c62010-05-16 10:46:25 -04007862 spin_lock(&sinfo->lock);
7863 spin_lock(&cache->lock);
7864 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7865 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007866
Yan, Zhengf0486c62010-05-16 10:46:25 -04007867 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
7868 sinfo->bytes_may_use + sinfo->bytes_readonly +
7869 cache->reserved_pinned + num_bytes < sinfo->total_bytes) {
7870 sinfo->bytes_readonly += num_bytes;
7871 sinfo->bytes_reserved += cache->reserved_pinned;
7872 cache->reserved_pinned = 0;
7873 cache->ro = 1;
7874 ret = 0;
7875 }
7876 spin_unlock(&cache->lock);
7877 spin_unlock(&sinfo->lock);
7878 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007879}
7880
Yan, Zhengf0486c62010-05-16 10:46:25 -04007881int btrfs_set_block_group_ro(struct btrfs_root *root,
7882 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007883
7884{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007885 struct btrfs_trans_handle *trans;
7886 u64 alloc_flags;
7887 int ret;
7888
7889 BUG_ON(cache->ro);
7890
7891 trans = btrfs_join_transaction(root, 1);
7892 BUG_ON(IS_ERR(trans));
7893
7894 alloc_flags = update_block_group_flags(root, cache->flags);
7895 if (alloc_flags != cache->flags)
7896 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7897
7898 ret = set_block_group_ro(cache);
7899 if (!ret)
7900 goto out;
7901 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
7902 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7903 if (ret < 0)
7904 goto out;
7905 ret = set_block_group_ro(cache);
7906out:
7907 btrfs_end_transaction(trans, root);
7908 return ret;
7909}
7910
7911int btrfs_set_block_group_rw(struct btrfs_root *root,
7912 struct btrfs_block_group_cache *cache)
7913{
7914 struct btrfs_space_info *sinfo = cache->space_info;
7915 u64 num_bytes;
7916
7917 BUG_ON(!cache->ro);
7918
7919 spin_lock(&sinfo->lock);
7920 spin_lock(&cache->lock);
7921 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7922 cache->bytes_super - btrfs_block_group_used(&cache->item);
7923 sinfo->bytes_readonly -= num_bytes;
7924 cache->ro = 0;
7925 spin_unlock(&cache->lock);
7926 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007927 return 0;
7928}
7929
Josef Bacikba1bf482009-09-11 16:11:19 -04007930/*
7931 * checks to see if its even possible to relocate this block group.
7932 *
7933 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7934 * ok to go ahead and try.
7935 */
7936int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007937{
Zheng Yan1a40e232008-09-26 10:09:34 -04007938 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007939 struct btrfs_space_info *space_info;
7940 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7941 struct btrfs_device *device;
7942 int full = 0;
7943 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007944
Josef Bacikba1bf482009-09-11 16:11:19 -04007945 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007946
Josef Bacikba1bf482009-09-11 16:11:19 -04007947 /* odd, couldn't find the block group, leave it alone */
7948 if (!block_group)
7949 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007950
Josef Bacikba1bf482009-09-11 16:11:19 -04007951 /* no bytes used, we're good */
7952 if (!btrfs_block_group_used(&block_group->item))
7953 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007954
Josef Bacikba1bf482009-09-11 16:11:19 -04007955 space_info = block_group->space_info;
7956 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007957
Josef Bacikba1bf482009-09-11 16:11:19 -04007958 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007959
Josef Bacikba1bf482009-09-11 16:11:19 -04007960 /*
7961 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007962 * relocate it unless we're able to allocate a new chunk below.
7963 *
7964 * Otherwise, we need to make sure we have room in the space to handle
7965 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007966 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007967 if ((space_info->total_bytes != block_group->key.offset) &&
7968 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04007969 space_info->bytes_pinned + space_info->bytes_readonly +
7970 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04007971 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007972 spin_unlock(&space_info->lock);
7973 goto out;
7974 }
7975 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007976
Josef Bacikba1bf482009-09-11 16:11:19 -04007977 /*
7978 * ok we don't have enough space, but maybe we have free space on our
7979 * devices to allocate new chunks for relocation, so loop through our
7980 * alloc devices and guess if we have enough space. However, if we
7981 * were marked as full, then we know there aren't enough chunks, and we
7982 * can just return.
7983 */
7984 ret = -1;
7985 if (full)
7986 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05007987
Josef Bacikba1bf482009-09-11 16:11:19 -04007988 mutex_lock(&root->fs_info->chunk_mutex);
7989 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7990 u64 min_free = btrfs_block_group_used(&block_group->item);
7991 u64 dev_offset, max_avail;
Chris Masonea8c2812008-08-04 23:17:27 -04007992
Josef Bacikba1bf482009-09-11 16:11:19 -04007993 /*
7994 * check to make sure we can actually find a chunk with enough
7995 * space to fit our block group in.
7996 */
7997 if (device->total_bytes > device->bytes_used + min_free) {
7998 ret = find_free_dev_extent(NULL, device, min_free,
7999 &dev_offset, &max_avail);
8000 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05008001 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04008002 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008003 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008004 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008005 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05008006out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008007 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008008 return ret;
8009}
8010
Christoph Hellwigb2950862008-12-02 09:54:17 -05008011static int find_first_block_group(struct btrfs_root *root,
8012 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008013{
Chris Mason925baed2008-06-25 16:01:30 -04008014 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008015 struct btrfs_key found_key;
8016 struct extent_buffer *leaf;
8017 int slot;
8018
8019 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8020 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008021 goto out;
8022
Chris Masond3977122009-01-05 21:25:51 -05008023 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008024 slot = path->slots[0];
8025 leaf = path->nodes[0];
8026 if (slot >= btrfs_header_nritems(leaf)) {
8027 ret = btrfs_next_leaf(root, path);
8028 if (ret == 0)
8029 continue;
8030 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008031 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008032 break;
8033 }
8034 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8035
8036 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008037 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8038 ret = 0;
8039 goto out;
8040 }
Chris Mason0b86a832008-03-24 15:01:56 -04008041 path->slots[0]++;
8042 }
Chris Mason925baed2008-06-25 16:01:30 -04008043out:
Chris Mason0b86a832008-03-24 15:01:56 -04008044 return ret;
8045}
8046
Josef Bacik0af3d002010-06-21 14:48:16 -04008047void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8048{
8049 struct btrfs_block_group_cache *block_group;
8050 u64 last = 0;
8051
8052 while (1) {
8053 struct inode *inode;
8054
8055 block_group = btrfs_lookup_first_block_group(info, last);
8056 while (block_group) {
8057 spin_lock(&block_group->lock);
8058 if (block_group->iref)
8059 break;
8060 spin_unlock(&block_group->lock);
8061 block_group = next_block_group(info->tree_root,
8062 block_group);
8063 }
8064 if (!block_group) {
8065 if (last == 0)
8066 break;
8067 last = 0;
8068 continue;
8069 }
8070
8071 inode = block_group->inode;
8072 block_group->iref = 0;
8073 block_group->inode = NULL;
8074 spin_unlock(&block_group->lock);
8075 iput(inode);
8076 last = block_group->key.objectid + block_group->key.offset;
8077 btrfs_put_block_group(block_group);
8078 }
8079}
8080
Zheng Yan1a40e232008-09-26 10:09:34 -04008081int btrfs_free_block_groups(struct btrfs_fs_info *info)
8082{
8083 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008084 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008085 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008086 struct rb_node *n;
8087
Yan Zheng11833d62009-09-11 16:11:19 -04008088 down_write(&info->extent_commit_sem);
8089 while (!list_empty(&info->caching_block_groups)) {
8090 caching_ctl = list_entry(info->caching_block_groups.next,
8091 struct btrfs_caching_control, list);
8092 list_del(&caching_ctl->list);
8093 put_caching_control(caching_ctl);
8094 }
8095 up_write(&info->extent_commit_sem);
8096
Zheng Yan1a40e232008-09-26 10:09:34 -04008097 spin_lock(&info->block_group_cache_lock);
8098 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8099 block_group = rb_entry(n, struct btrfs_block_group_cache,
8100 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008101 rb_erase(&block_group->cache_node,
8102 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008103 spin_unlock(&info->block_group_cache_lock);
8104
Josef Bacik80eb2342008-10-29 14:49:05 -04008105 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008106 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008107 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008108
Josef Bacik817d52f2009-07-13 21:29:25 -04008109 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008110 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008111
8112 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008113 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008114
8115 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008116 }
8117 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008118
8119 /* now that all the block groups are freed, go through and
8120 * free all the space_info structs. This is only called during
8121 * the final stages of unmount, and so we know nobody is
8122 * using them. We call synchronize_rcu() once before we start,
8123 * just to be on the safe side.
8124 */
8125 synchronize_rcu();
8126
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008127 release_global_block_rsv(info);
8128
Chris Mason4184ea72009-03-10 12:39:20 -04008129 while(!list_empty(&info->space_info)) {
8130 space_info = list_entry(info->space_info.next,
8131 struct btrfs_space_info,
8132 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008133 if (space_info->bytes_pinned > 0 ||
8134 space_info->bytes_reserved > 0) {
8135 WARN_ON(1);
8136 dump_space_info(space_info, 0, 0);
8137 }
Chris Mason4184ea72009-03-10 12:39:20 -04008138 list_del(&space_info->list);
8139 kfree(space_info);
8140 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008141 return 0;
8142}
8143
Yan, Zhengb742bb82010-05-16 10:46:24 -04008144static void __link_block_group(struct btrfs_space_info *space_info,
8145 struct btrfs_block_group_cache *cache)
8146{
8147 int index = get_block_group_index(cache);
8148
8149 down_write(&space_info->groups_sem);
8150 list_add_tail(&cache->list, &space_info->block_groups[index]);
8151 up_write(&space_info->groups_sem);
8152}
8153
Chris Mason9078a3e2007-04-26 16:46:15 -04008154int btrfs_read_block_groups(struct btrfs_root *root)
8155{
8156 struct btrfs_path *path;
8157 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008158 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008159 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008160 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008161 struct btrfs_key key;
8162 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008163 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008164 int need_clear = 0;
8165 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008166
Chris Masonbe744172007-05-06 10:15:01 -04008167 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008168 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008169 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008170 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008171 path = btrfs_alloc_path();
8172 if (!path)
8173 return -ENOMEM;
8174
Josef Bacik0af3d002010-06-21 14:48:16 -04008175 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8176 if (cache_gen != 0 &&
8177 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8178 need_clear = 1;
8179
Chris Masond3977122009-01-05 21:25:51 -05008180 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008181 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008182 if (ret > 0)
8183 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008184 if (ret != 0)
8185 goto error;
8186
Chris Mason5f39d392007-10-15 16:14:19 -04008187 leaf = path->nodes[0];
8188 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008189 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008190 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008191 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008192 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008193 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008194
Yan Zhengd2fb3432008-12-11 16:30:39 -05008195 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008196 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008197 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008198 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008199 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008200 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008201
Josef Bacik0af3d002010-06-21 14:48:16 -04008202 if (need_clear)
8203 cache->disk_cache_state = BTRFS_DC_CLEAR;
8204
Josef Bacik96303082009-07-13 21:29:25 -04008205 /*
8206 * we only want to have 32k of ram per block group for keeping
8207 * track of free space, and if we pass 1/2 of that we want to
8208 * start converting things over to using bitmaps
8209 */
8210 cache->extents_thresh = ((1024 * 32) / 2) /
8211 sizeof(struct btrfs_free_space);
8212
Chris Mason5f39d392007-10-15 16:14:19 -04008213 read_extent_buffer(leaf, &cache->item,
8214 btrfs_item_ptr_offset(leaf, path->slots[0]),
8215 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008216 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008217
Chris Mason9078a3e2007-04-26 16:46:15 -04008218 key.objectid = found_key.objectid + found_key.offset;
8219 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04008220 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008221 cache->sectorsize = root->sectorsize;
8222
Josef Bacik817d52f2009-07-13 21:29:25 -04008223 /*
8224 * check for two cases, either we are full, and therefore
8225 * don't need to bother with the caching work since we won't
8226 * find any space, or we are empty, and we can just add all
8227 * the space in and be done with it. This saves us _alot_ of
8228 * time, particularly in the full case.
8229 */
8230 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04008231 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04008232 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008233 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008234 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008235 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008236 exclude_super_stripes(root, cache);
8237 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008238 cache->cached = BTRFS_CACHE_FINISHED;
8239 add_new_free_space(cache, root->fs_info,
8240 found_key.objectid,
8241 found_key.objectid +
8242 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008243 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008244 }
Chris Mason96b51792007-10-15 16:15:19 -04008245
Chris Mason6324fbf2008-03-24 15:01:59 -04008246 ret = update_space_info(info, cache->flags, found_key.offset,
8247 btrfs_block_group_used(&cache->item),
8248 &space_info);
8249 BUG_ON(ret);
8250 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008251 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008252 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008253 spin_unlock(&cache->space_info->lock);
8254
Yan, Zhengb742bb82010-05-16 10:46:24 -04008255 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008256
Josef Bacik0f9dd462008-09-23 13:14:11 -04008257 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8258 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04008259
8260 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008261 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04008262 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04008263 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008264
8265 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8266 if (!(get_alloc_profile(root, space_info->flags) &
8267 (BTRFS_BLOCK_GROUP_RAID10 |
8268 BTRFS_BLOCK_GROUP_RAID1 |
8269 BTRFS_BLOCK_GROUP_DUP)))
8270 continue;
8271 /*
8272 * avoid allocating from un-mirrored block group if there are
8273 * mirrored block groups.
8274 */
8275 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008276 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008277 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008278 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008279 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008280
8281 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008282 ret = 0;
8283error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008284 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008285 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008286}
Chris Mason6324fbf2008-03-24 15:01:59 -04008287
8288int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8289 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008290 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008291 u64 size)
8292{
8293 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008294 struct btrfs_root *extent_root;
8295 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008296
8297 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008298
Chris Mason12fcfd22009-03-24 10:24:20 -04008299 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008300
Chris Mason8f18cf12008-04-25 16:53:30 -04008301 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008302 if (!cache)
8303 return -ENOMEM;
8304
Chris Masone17cade2008-04-15 15:41:47 -04008305 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008306 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008307 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008308 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008309 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04008310
8311 /*
8312 * we only want to have 32k of ram per block group for keeping track
8313 * of free space, and if we pass 1/2 of that we want to start
8314 * converting things over to using bitmaps
8315 */
8316 cache->extents_thresh = ((1024 * 32) / 2) /
8317 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008318 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008319 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008320 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008321 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008322 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008323
Chris Mason6324fbf2008-03-24 15:01:59 -04008324 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008325 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8326 cache->flags = type;
8327 btrfs_set_block_group_flags(&cache->item, type);
8328
Yan Zheng11833d62009-09-11 16:11:19 -04008329 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008330 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04008331 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04008332
Josef Bacik817d52f2009-07-13 21:29:25 -04008333 add_new_free_space(cache, root->fs_info, chunk_offset,
8334 chunk_offset + size);
8335
Yan Zheng11833d62009-09-11 16:11:19 -04008336 free_excluded_extents(root, cache);
8337
Chris Mason6324fbf2008-03-24 15:01:59 -04008338 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8339 &cache->space_info);
8340 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04008341
8342 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008343 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008344 spin_unlock(&cache->space_info->lock);
8345
Yan, Zhengb742bb82010-05-16 10:46:24 -04008346 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008347
Josef Bacik0f9dd462008-09-23 13:14:11 -04008348 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8349 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04008350
Chris Mason6324fbf2008-03-24 15:01:59 -04008351 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8352 sizeof(cache->item));
8353 BUG_ON(ret);
8354
Chris Masond18a2c42008-04-04 15:40:00 -04008355 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008356
Chris Mason6324fbf2008-03-24 15:01:59 -04008357 return 0;
8358}
Zheng Yan1a40e232008-09-26 10:09:34 -04008359
8360int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8361 struct btrfs_root *root, u64 group_start)
8362{
8363 struct btrfs_path *path;
8364 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008365 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008366 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008367 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008368 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008369 int ret;
8370
Zheng Yan1a40e232008-09-26 10:09:34 -04008371 root = root->fs_info->extent_root;
8372
8373 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8374 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008375 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008376
Chris Mason44fb5512009-06-04 15:34:51 -04008377 /* make sure this block group isn't part of an allocation cluster */
8378 cluster = &root->fs_info->data_alloc_cluster;
8379 spin_lock(&cluster->refill_lock);
8380 btrfs_return_cluster_to_free_space(block_group, cluster);
8381 spin_unlock(&cluster->refill_lock);
8382
8383 /*
8384 * make sure this block group isn't part of a metadata
8385 * allocation cluster
8386 */
8387 cluster = &root->fs_info->meta_alloc_cluster;
8388 spin_lock(&cluster->refill_lock);
8389 btrfs_return_cluster_to_free_space(block_group, cluster);
8390 spin_unlock(&cluster->refill_lock);
8391
Zheng Yan1a40e232008-09-26 10:09:34 -04008392 path = btrfs_alloc_path();
8393 BUG_ON(!path);
8394
Josef Bacik0af3d002010-06-21 14:48:16 -04008395 inode = lookup_free_space_inode(root, block_group, path);
8396 if (!IS_ERR(inode)) {
8397 btrfs_orphan_add(trans, inode);
8398 clear_nlink(inode);
8399 /* One for the block groups ref */
8400 spin_lock(&block_group->lock);
8401 if (block_group->iref) {
8402 block_group->iref = 0;
8403 block_group->inode = NULL;
8404 spin_unlock(&block_group->lock);
8405 iput(inode);
8406 } else {
8407 spin_unlock(&block_group->lock);
8408 }
8409 /* One for our lookup ref */
8410 iput(inode);
8411 }
8412
8413 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8414 key.offset = block_group->key.objectid;
8415 key.type = 0;
8416
8417 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8418 if (ret < 0)
8419 goto out;
8420 if (ret > 0)
8421 btrfs_release_path(tree_root, path);
8422 if (ret == 0) {
8423 ret = btrfs_del_item(trans, tree_root, path);
8424 if (ret)
8425 goto out;
8426 btrfs_release_path(tree_root, path);
8427 }
8428
Yan Zheng3dfdb932009-01-21 10:49:16 -05008429 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008430 rb_erase(&block_group->cache_node,
8431 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008432 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008433
Josef Bacik80eb2342008-10-29 14:49:05 -04008434 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008435 /*
8436 * we must use list_del_init so people can check to see if they
8437 * are still on the list after taking the semaphore
8438 */
8439 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008440 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008441
Josef Bacik817d52f2009-07-13 21:29:25 -04008442 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008443 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008444
8445 btrfs_remove_free_space_cache(block_group);
8446
Yan Zhengc146afa2008-11-12 14:34:12 -05008447 spin_lock(&block_group->space_info->lock);
8448 block_group->space_info->total_bytes -= block_group->key.offset;
8449 block_group->space_info->bytes_readonly -= block_group->key.offset;
8450 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008451
Josef Bacik0af3d002010-06-21 14:48:16 -04008452 memcpy(&key, &block_group->key, sizeof(key));
8453
Chris Mason283bb192009-07-24 16:30:55 -04008454 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008455
Chris Masonfa9c0d72009-04-03 09:47:43 -04008456 btrfs_put_block_group(block_group);
8457 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008458
8459 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8460 if (ret > 0)
8461 ret = -EIO;
8462 if (ret < 0)
8463 goto out;
8464
8465 ret = btrfs_del_item(trans, root, path);
8466out:
8467 btrfs_free_path(path);
8468 return ret;
8469}