blob: cec05e10014281886014b1274ffe7e4e1e86a6bd [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050027#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040031#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040033#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040034#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Josef Bacikf3465ca2008-11-12 14:19:50 -050036static int update_block_group(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
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
Josef Bacikdde5abe2010-09-16 16:17:03 -0400245 /* We're loading it the fast way, so we don't have a caching_ctl. */
246 if (!cache->caching_ctl) {
247 spin_unlock(&cache->lock);
248 return NULL;
249 }
250
Yan Zheng11833d62009-09-11 16:11:19 -0400251 ctl = cache->caching_ctl;
252 atomic_inc(&ctl->count);
253 spin_unlock(&cache->lock);
254 return ctl;
255}
256
257static void put_caching_control(struct btrfs_caching_control *ctl)
258{
259 if (atomic_dec_and_test(&ctl->count))
260 kfree(ctl);
261}
262
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263/*
264 * this is only called by cache_block_group, since we could have freed extents
265 * we need to check the pinned_extents for any extents that can't be used yet
266 * since their free space will be released as soon as the transaction commits.
267 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400268static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400269 struct btrfs_fs_info *info, u64 start, u64 end)
270{
Josef Bacik817d52f2009-07-13 21:29:25 -0400271 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400272 int ret;
273
274 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400275 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400276 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400277 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400278 if (ret)
279 break;
280
Yan, Zheng06b23312009-11-26 09:31:11 +0000281 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400282 start = extent_end + 1;
283 } else if (extent_start > start && extent_start < end) {
284 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400285 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500286 ret = btrfs_add_free_space(block_group, start,
287 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400288 BUG_ON(ret);
289 start = extent_end + 1;
290 } else {
291 break;
292 }
293 }
294
295 if (start < end) {
296 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400297 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500298 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400299 BUG_ON(ret);
300 }
301
Josef Bacik817d52f2009-07-13 21:29:25 -0400302 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400303}
304
Josef Bacik817d52f2009-07-13 21:29:25 -0400305static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400306{
Josef Bacik817d52f2009-07-13 21:29:25 -0400307 struct btrfs_block_group_cache *block_group = data;
308 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400309 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
310 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400311 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400312 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400313 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400314 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400315 u64 last = 0;
316 u32 nritems;
317 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400318
Chris Masone37c9e62007-05-09 20:13:14 -0400319 path = btrfs_alloc_path();
320 if (!path)
321 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400322
Yan Zheng11833d62009-09-11 16:11:19 -0400323 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400324 spin_lock(&block_group->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400325 block_group->space_info->bytes_readonly += block_group->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -0400326 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400327
Josef Bacik817d52f2009-07-13 21:29:25 -0400328 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400329
Chris Mason5cd57b22008-06-25 16:01:30 -0400330 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 * We don't want to deadlock with somebody trying to allocate a new
332 * extent for the extent root while also trying to search the extent
333 * root to add free space. So we skip locking and search the commit
334 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400335 */
336 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400337 path->search_commit_root = 1;
338 path->reada = 2;
339
Yan Zhenge4404d62008-12-12 10:03:26 -0500340 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400341 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400342 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400343again:
Yan Zheng11833d62009-09-11 16:11:19 -0400344 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400345 /* need to make sure the commit_root doesn't disappear */
346 down_read(&fs_info->extent_commit_sem);
347
Yan Zheng11833d62009-09-11 16:11:19 -0400348 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400349 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400350 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500351
Yan Zheng11833d62009-09-11 16:11:19 -0400352 leaf = path->nodes[0];
353 nritems = btrfs_header_nritems(leaf);
354
Chris Masond3977122009-01-05 21:25:51 -0500355 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400356 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400357 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400358 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400359 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400360 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400361
Yan Zheng11833d62009-09-11 16:11:19 -0400362 if (path->slots[0] < nritems) {
363 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
364 } else {
365 ret = find_next_key(path, 0, &key);
366 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400367 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400368
Yan Zheng11833d62009-09-11 16:11:19 -0400369 caching_ctl->progress = last;
370 btrfs_release_path(extent_root, path);
371 up_read(&fs_info->extent_commit_sem);
372 mutex_unlock(&caching_ctl->mutex);
373 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400374 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400375 else
376 cond_resched();
377 goto again;
378 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400379
Yan Zheng11833d62009-09-11 16:11:19 -0400380 if (key.objectid < block_group->key.objectid) {
381 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400382 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400383 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400384
Chris Masone37c9e62007-05-09 20:13:14 -0400385 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400386 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400387 break;
Yan7d7d6062007-09-14 16:15:28 -0400388
Yan Zheng11833d62009-09-11 16:11:19 -0400389 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400390 total_found += add_new_free_space(block_group,
391 fs_info, last,
392 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400393 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400394
Yan Zheng11833d62009-09-11 16:11:19 -0400395 if (total_found > (1024 * 1024 * 2)) {
396 total_found = 0;
397 wake_up(&caching_ctl->wait);
398 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400399 }
Chris Masone37c9e62007-05-09 20:13:14 -0400400 path->slots[0]++;
401 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400402 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400403
404 total_found += add_new_free_space(block_group, fs_info, last,
405 block_group->key.objectid +
406 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400407 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400408
409 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400410 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400411 block_group->cached = BTRFS_CACHE_FINISHED;
412 spin_unlock(&block_group->lock);
413
Chris Mason54aa1f42007-06-22 14:16:25 -0400414err:
Chris Masone37c9e62007-05-09 20:13:14 -0400415 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400416 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400417
Yan Zheng11833d62009-09-11 16:11:19 -0400418 free_excluded_extents(extent_root, block_group);
419
420 mutex_unlock(&caching_ctl->mutex);
421 wake_up(&caching_ctl->wait);
422
423 put_caching_control(caching_ctl);
424 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000425 btrfs_put_block_group(block_group);
426
Josef Bacik817d52f2009-07-13 21:29:25 -0400427 return 0;
428}
429
Josef Bacik9d66e232010-08-25 16:54:15 -0400430static int cache_block_group(struct btrfs_block_group_cache *cache,
431 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500432 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400433 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400434{
Yan Zheng11833d62009-09-11 16:11:19 -0400435 struct btrfs_fs_info *fs_info = cache->fs_info;
436 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400437 struct task_struct *tsk;
438 int ret = 0;
439
Yan Zheng11833d62009-09-11 16:11:19 -0400440 smp_mb();
441 if (cache->cached != BTRFS_CACHE_NO)
442 return 0;
443
Josef Bacik9d66e232010-08-25 16:54:15 -0400444 /*
445 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500446 * to have the normal tree locking. Also if we are currently trying to
447 * allocate blocks for the tree root we can't do the fast caching since
448 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400449 */
Josef Bacikb8399de2010-12-08 09:15:11 -0500450 if (!trans->transaction->in_commit &&
451 (root && root != root->fs_info->tree_root)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400452 spin_lock(&cache->lock);
453 if (cache->cached != BTRFS_CACHE_NO) {
454 spin_unlock(&cache->lock);
455 return 0;
456 }
457 cache->cached = BTRFS_CACHE_STARTED;
458 spin_unlock(&cache->lock);
459
460 ret = load_free_space_cache(fs_info, cache);
461
462 spin_lock(&cache->lock);
463 if (ret == 1) {
464 cache->cached = BTRFS_CACHE_FINISHED;
465 cache->last_byte_to_unpin = (u64)-1;
466 } else {
467 cache->cached = BTRFS_CACHE_NO;
468 }
469 spin_unlock(&cache->lock);
470 if (ret == 1)
471 return 0;
472 }
473
474 if (load_cache_only)
475 return 0;
476
Yan Zheng11833d62009-09-11 16:11:19 -0400477 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
478 BUG_ON(!caching_ctl);
479
480 INIT_LIST_HEAD(&caching_ctl->list);
481 mutex_init(&caching_ctl->mutex);
482 init_waitqueue_head(&caching_ctl->wait);
483 caching_ctl->block_group = cache;
484 caching_ctl->progress = cache->key.objectid;
485 /* one for caching kthread, one for caching block group list */
486 atomic_set(&caching_ctl->count, 2);
487
Josef Bacik817d52f2009-07-13 21:29:25 -0400488 spin_lock(&cache->lock);
489 if (cache->cached != BTRFS_CACHE_NO) {
490 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400491 kfree(caching_ctl);
492 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400493 }
Yan Zheng11833d62009-09-11 16:11:19 -0400494 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400495 cache->cached = BTRFS_CACHE_STARTED;
496 spin_unlock(&cache->lock);
497
Yan Zheng11833d62009-09-11 16:11:19 -0400498 down_write(&fs_info->extent_commit_sem);
499 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
500 up_write(&fs_info->extent_commit_sem);
501
502 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000503 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400504
Josef Bacik817d52f2009-07-13 21:29:25 -0400505 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
506 cache->key.objectid);
507 if (IS_ERR(tsk)) {
508 ret = PTR_ERR(tsk);
509 printk(KERN_ERR "error running thread %d\n", ret);
510 BUG();
511 }
512
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400513 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400514}
515
Josef Bacik0f9dd462008-09-23 13:14:11 -0400516/*
517 * return the block group that starts at or after bytenr
518 */
Chris Masond3977122009-01-05 21:25:51 -0500519static struct btrfs_block_group_cache *
520btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400521{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400522 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400523
Josef Bacik0f9dd462008-09-23 13:14:11 -0400524 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400525
Josef Bacik0f9dd462008-09-23 13:14:11 -0400526 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400527}
528
Josef Bacik0f9dd462008-09-23 13:14:11 -0400529/*
Sankar P9f556842009-05-14 13:52:22 -0400530 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400531 */
Chris Masond3977122009-01-05 21:25:51 -0500532struct btrfs_block_group_cache *btrfs_lookup_block_group(
533 struct btrfs_fs_info *info,
534 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400535{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400536 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400537
Josef Bacik0f9dd462008-09-23 13:14:11 -0400538 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400539
Josef Bacik0f9dd462008-09-23 13:14:11 -0400540 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400541}
Chris Mason0b86a832008-03-24 15:01:56 -0400542
Josef Bacik0f9dd462008-09-23 13:14:11 -0400543static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
544 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400545{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400546 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400547 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400548
Yan, Zhengb742bb82010-05-16 10:46:24 -0400549 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
550 BTRFS_BLOCK_GROUP_METADATA;
551
Chris Mason4184ea72009-03-10 12:39:20 -0400552 rcu_read_lock();
553 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400554 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400555 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400556 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400557 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400558 }
Chris Mason4184ea72009-03-10 12:39:20 -0400559 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400560 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400561}
562
Chris Mason4184ea72009-03-10 12:39:20 -0400563/*
564 * after adding space to the filesystem, we need to clear the full flags
565 * on all the space infos.
566 */
567void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
568{
569 struct list_head *head = &info->space_info;
570 struct btrfs_space_info *found;
571
572 rcu_read_lock();
573 list_for_each_entry_rcu(found, head, list)
574 found->full = 0;
575 rcu_read_unlock();
576}
577
Josef Bacik80eb2342008-10-29 14:49:05 -0400578static u64 div_factor(u64 num, int factor)
579{
580 if (factor == 10)
581 return num;
582 num *= factor;
583 do_div(num, 10);
584 return num;
585}
586
Chris Masone5bc2452010-10-26 13:37:56 -0400587static u64 div_factor_fine(u64 num, int factor)
588{
589 if (factor == 100)
590 return num;
591 num *= factor;
592 do_div(num, 100);
593 return num;
594}
595
Yan Zhengd2fb3432008-12-11 16:30:39 -0500596u64 btrfs_find_block_group(struct btrfs_root *root,
597 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400598{
Chris Mason96b51792007-10-15 16:15:19 -0400599 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400600 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500601 u64 last = max(search_hint, search_start);
602 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400603 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500604 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400605 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400606again:
Zheng Yane8569812008-09-26 10:05:48 -0400607 while (1) {
608 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400609 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400610 break;
Chris Mason96b51792007-10-15 16:15:19 -0400611
Chris Masonc286ac42008-07-22 23:06:41 -0400612 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400613 last = cache->key.objectid + cache->key.offset;
614 used = btrfs_block_group_used(&cache->item);
615
Yan Zhengd2fb3432008-12-11 16:30:39 -0500616 if ((full_search || !cache->ro) &&
617 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400618 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500619 div_factor(cache->key.offset, factor)) {
620 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400621 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400622 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400623 goto found;
624 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400625 }
Chris Masonc286ac42008-07-22 23:06:41 -0400626 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400627 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400628 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400629 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400630 if (!wrapped) {
631 last = search_start;
632 wrapped = 1;
633 goto again;
634 }
635 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400636 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400637 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400638 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400639 goto again;
640 }
Chris Masonbe744172007-05-06 10:15:01 -0400641found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500642 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400643}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400644
Chris Masone02119d2008-09-05 16:13:11 -0400645/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400646int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400647{
648 int ret;
649 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400650 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400651
Zheng Yan31840ae2008-09-23 13:14:14 -0400652 path = btrfs_alloc_path();
653 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400654 key.objectid = start;
655 key.offset = len;
656 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
657 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
658 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400659 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500660 return ret;
661}
662
Chris Masond8d5f3e2007-12-11 12:42:00 -0500663/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400664 * helper function to lookup reference count and flags of extent.
665 *
666 * the head node for delayed ref is used to store the sum of all the
667 * reference count modifications queued up in the rbtree. the head
668 * node may also store the extent flags to set. This way you can check
669 * to see what the reference count and extent flags would be if all of
670 * the delayed refs are not processed.
671 */
672int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
673 struct btrfs_root *root, u64 bytenr,
674 u64 num_bytes, u64 *refs, u64 *flags)
675{
676 struct btrfs_delayed_ref_head *head;
677 struct btrfs_delayed_ref_root *delayed_refs;
678 struct btrfs_path *path;
679 struct btrfs_extent_item *ei;
680 struct extent_buffer *leaf;
681 struct btrfs_key key;
682 u32 item_size;
683 u64 num_refs;
684 u64 extent_flags;
685 int ret;
686
687 path = btrfs_alloc_path();
688 if (!path)
689 return -ENOMEM;
690
691 key.objectid = bytenr;
692 key.type = BTRFS_EXTENT_ITEM_KEY;
693 key.offset = num_bytes;
694 if (!trans) {
695 path->skip_locking = 1;
696 path->search_commit_root = 1;
697 }
698again:
699 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
700 &key, path, 0, 0);
701 if (ret < 0)
702 goto out_free;
703
704 if (ret == 0) {
705 leaf = path->nodes[0];
706 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
707 if (item_size >= sizeof(*ei)) {
708 ei = btrfs_item_ptr(leaf, path->slots[0],
709 struct btrfs_extent_item);
710 num_refs = btrfs_extent_refs(leaf, ei);
711 extent_flags = btrfs_extent_flags(leaf, ei);
712 } else {
713#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
714 struct btrfs_extent_item_v0 *ei0;
715 BUG_ON(item_size != sizeof(*ei0));
716 ei0 = btrfs_item_ptr(leaf, path->slots[0],
717 struct btrfs_extent_item_v0);
718 num_refs = btrfs_extent_refs_v0(leaf, ei0);
719 /* FIXME: this isn't correct for data */
720 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
721#else
722 BUG();
723#endif
724 }
725 BUG_ON(num_refs == 0);
726 } else {
727 num_refs = 0;
728 extent_flags = 0;
729 ret = 0;
730 }
731
732 if (!trans)
733 goto out;
734
735 delayed_refs = &trans->transaction->delayed_refs;
736 spin_lock(&delayed_refs->lock);
737 head = btrfs_find_delayed_ref_head(trans, bytenr);
738 if (head) {
739 if (!mutex_trylock(&head->mutex)) {
740 atomic_inc(&head->node.refs);
741 spin_unlock(&delayed_refs->lock);
742
743 btrfs_release_path(root->fs_info->extent_root, path);
744
745 mutex_lock(&head->mutex);
746 mutex_unlock(&head->mutex);
747 btrfs_put_delayed_ref(&head->node);
748 goto again;
749 }
750 if (head->extent_op && head->extent_op->update_flags)
751 extent_flags |= head->extent_op->flags_to_set;
752 else
753 BUG_ON(num_refs == 0);
754
755 num_refs += head->node.ref_mod;
756 mutex_unlock(&head->mutex);
757 }
758 spin_unlock(&delayed_refs->lock);
759out:
760 WARN_ON(num_refs == 0);
761 if (refs)
762 *refs = num_refs;
763 if (flags)
764 *flags = extent_flags;
765out_free:
766 btrfs_free_path(path);
767 return ret;
768}
769
770/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500771 * Back reference rules. Back refs have three main goals:
772 *
773 * 1) differentiate between all holders of references to an extent so that
774 * when a reference is dropped we can make sure it was a valid reference
775 * before freeing the extent.
776 *
777 * 2) Provide enough information to quickly find the holders of an extent
778 * if we notice a given block is corrupted or bad.
779 *
780 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
781 * maintenance. This is actually the same as #2, but with a slightly
782 * different use case.
783 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400784 * There are two kinds of back refs. The implicit back refs is optimized
785 * for pointers in non-shared tree blocks. For a given pointer in a block,
786 * back refs of this kind provide information about the block's owner tree
787 * and the pointer's key. These information allow us to find the block by
788 * b-tree searching. The full back refs is for pointers in tree blocks not
789 * referenced by their owner trees. The location of tree block is recorded
790 * in the back refs. Actually the full back refs is generic, and can be
791 * used in all cases the implicit back refs is used. The major shortcoming
792 * of the full back refs is its overhead. Every time a tree block gets
793 * COWed, we have to update back refs entry for all pointers in it.
794 *
795 * For a newly allocated tree block, we use implicit back refs for
796 * pointers in it. This means most tree related operations only involve
797 * implicit back refs. For a tree block created in old transaction, the
798 * only way to drop a reference to it is COW it. So we can detect the
799 * event that tree block loses its owner tree's reference and do the
800 * back refs conversion.
801 *
802 * When a tree block is COW'd through a tree, there are four cases:
803 *
804 * The reference count of the block is one and the tree is the block's
805 * owner tree. Nothing to do in this case.
806 *
807 * The reference count of the block is one and the tree is not the
808 * block's owner tree. In this case, full back refs is used for pointers
809 * in the block. Remove these full back refs, add implicit back refs for
810 * every pointers in the new block.
811 *
812 * The reference count of the block is greater than one and the tree is
813 * the block's owner tree. In this case, implicit back refs is used for
814 * pointers in the block. Add full back refs for every pointers in the
815 * block, increase lower level extents' reference counts. The original
816 * implicit back refs are entailed to the new block.
817 *
818 * The reference count of the block is greater than one and the tree is
819 * not the block's owner tree. Add implicit back refs for every pointer in
820 * the new block, increase lower level extents' reference count.
821 *
822 * Back Reference Key composing:
823 *
824 * The key objectid corresponds to the first byte in the extent,
825 * The key type is used to differentiate between types of back refs.
826 * There are different meanings of the key offset for different types
827 * of back refs.
828 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500829 * File extents can be referenced by:
830 *
831 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400832 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500833 * - different offsets inside a file (bookend extents in file.c)
834 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400835 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500836 *
837 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500838 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400839 * - original offset in the file
840 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400841 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400842 * The key offset for the implicit back refs is hash of the first
843 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500844 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400845 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500846 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400847 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500848 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400849 * The key offset for the implicit back refs is the first byte of
850 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500851 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400852 * When a file extent is allocated, The implicit back refs is used.
853 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500854 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400855 * (root_key.objectid, inode objectid, offset in file, 1)
856 *
857 * When a file extent is removed file truncation, we find the
858 * corresponding implicit back refs and check the following fields:
859 *
860 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500861 *
862 * Btree extents can be referenced by:
863 *
864 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500865 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 * Both the implicit back refs and the full back refs for tree blocks
867 * only consist of key. The key offset for the implicit back refs is
868 * objectid of block's owner tree. The key offset for the full back refs
869 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500870 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871 * When implicit back refs is used, information about the lowest key and
872 * level of the tree block are required. These information are stored in
873 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500874 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400875
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400876#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
877static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
878 struct btrfs_root *root,
879 struct btrfs_path *path,
880 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500881{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400882 struct btrfs_extent_item *item;
883 struct btrfs_extent_item_v0 *ei0;
884 struct btrfs_extent_ref_v0 *ref0;
885 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400886 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400887 struct btrfs_key key;
888 struct btrfs_key found_key;
889 u32 new_size = sizeof(*item);
890 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500891 int ret;
892
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400893 leaf = path->nodes[0];
894 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500895
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400896 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
897 ei0 = btrfs_item_ptr(leaf, path->slots[0],
898 struct btrfs_extent_item_v0);
899 refs = btrfs_extent_refs_v0(leaf, ei0);
900
901 if (owner == (u64)-1) {
902 while (1) {
903 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
904 ret = btrfs_next_leaf(root, path);
905 if (ret < 0)
906 return ret;
907 BUG_ON(ret > 0);
908 leaf = path->nodes[0];
909 }
910 btrfs_item_key_to_cpu(leaf, &found_key,
911 path->slots[0]);
912 BUG_ON(key.objectid != found_key.objectid);
913 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
914 path->slots[0]++;
915 continue;
916 }
917 ref0 = btrfs_item_ptr(leaf, path->slots[0],
918 struct btrfs_extent_ref_v0);
919 owner = btrfs_ref_objectid_v0(leaf, ref0);
920 break;
921 }
922 }
923 btrfs_release_path(root, path);
924
925 if (owner < BTRFS_FIRST_FREE_OBJECTID)
926 new_size += sizeof(*bi);
927
928 new_size -= sizeof(*ei0);
929 ret = btrfs_search_slot(trans, root, &key, path,
930 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400931 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400932 return ret;
933 BUG_ON(ret);
934
935 ret = btrfs_extend_item(trans, root, path, new_size);
936 BUG_ON(ret);
937
938 leaf = path->nodes[0];
939 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
940 btrfs_set_extent_refs(leaf, item, refs);
941 /* FIXME: get real generation */
942 btrfs_set_extent_generation(leaf, item, 0);
943 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
944 btrfs_set_extent_flags(leaf, item,
945 BTRFS_EXTENT_FLAG_TREE_BLOCK |
946 BTRFS_BLOCK_FLAG_FULL_BACKREF);
947 bi = (struct btrfs_tree_block_info *)(item + 1);
948 /* FIXME: get first key of the block */
949 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
950 btrfs_set_tree_block_level(leaf, bi, (int)owner);
951 } else {
952 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
953 }
954 btrfs_mark_buffer_dirty(leaf);
955 return 0;
956}
957#endif
958
959static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
960{
961 u32 high_crc = ~(u32)0;
962 u32 low_crc = ~(u32)0;
963 __le64 lenum;
964
965 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100966 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400967 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100968 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400969 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100970 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400971
972 return ((u64)high_crc << 31) ^ (u64)low_crc;
973}
974
975static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
976 struct btrfs_extent_data_ref *ref)
977{
978 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
979 btrfs_extent_data_ref_objectid(leaf, ref),
980 btrfs_extent_data_ref_offset(leaf, ref));
981}
982
983static int match_extent_data_ref(struct extent_buffer *leaf,
984 struct btrfs_extent_data_ref *ref,
985 u64 root_objectid, u64 owner, u64 offset)
986{
987 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
988 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
989 btrfs_extent_data_ref_offset(leaf, ref) != offset)
990 return 0;
991 return 1;
992}
993
994static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
995 struct btrfs_root *root,
996 struct btrfs_path *path,
997 u64 bytenr, u64 parent,
998 u64 root_objectid,
999 u64 owner, u64 offset)
1000{
1001 struct btrfs_key key;
1002 struct btrfs_extent_data_ref *ref;
1003 struct extent_buffer *leaf;
1004 u32 nritems;
1005 int ret;
1006 int recow;
1007 int err = -ENOENT;
1008
1009 key.objectid = bytenr;
1010 if (parent) {
1011 key.type = BTRFS_SHARED_DATA_REF_KEY;
1012 key.offset = parent;
1013 } else {
1014 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1015 key.offset = hash_extent_data_ref(root_objectid,
1016 owner, offset);
1017 }
1018again:
1019 recow = 0;
1020 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1021 if (ret < 0) {
1022 err = ret;
1023 goto fail;
1024 }
1025
1026 if (parent) {
1027 if (!ret)
1028 return 0;
1029#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1030 key.type = BTRFS_EXTENT_REF_V0_KEY;
1031 btrfs_release_path(root, path);
1032 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1033 if (ret < 0) {
1034 err = ret;
1035 goto fail;
1036 }
1037 if (!ret)
1038 return 0;
1039#endif
1040 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001041 }
1042
1043 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044 nritems = btrfs_header_nritems(leaf);
1045 while (1) {
1046 if (path->slots[0] >= nritems) {
1047 ret = btrfs_next_leaf(root, path);
1048 if (ret < 0)
1049 err = ret;
1050 if (ret)
1051 goto fail;
1052
1053 leaf = path->nodes[0];
1054 nritems = btrfs_header_nritems(leaf);
1055 recow = 1;
1056 }
1057
1058 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1059 if (key.objectid != bytenr ||
1060 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1061 goto fail;
1062
1063 ref = btrfs_item_ptr(leaf, path->slots[0],
1064 struct btrfs_extent_data_ref);
1065
1066 if (match_extent_data_ref(leaf, ref, root_objectid,
1067 owner, offset)) {
1068 if (recow) {
1069 btrfs_release_path(root, path);
1070 goto again;
1071 }
1072 err = 0;
1073 break;
1074 }
1075 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001076 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001077fail:
1078 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001079}
1080
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001081static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1082 struct btrfs_root *root,
1083 struct btrfs_path *path,
1084 u64 bytenr, u64 parent,
1085 u64 root_objectid, u64 owner,
1086 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001087{
1088 struct btrfs_key key;
1089 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001091 u32 num_refs;
1092 int ret;
1093
1094 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095 if (parent) {
1096 key.type = BTRFS_SHARED_DATA_REF_KEY;
1097 key.offset = parent;
1098 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001099 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001100 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1101 key.offset = hash_extent_data_ref(root_objectid,
1102 owner, offset);
1103 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001104 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001105
1106 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1107 if (ret && ret != -EEXIST)
1108 goto fail;
1109
1110 leaf = path->nodes[0];
1111 if (parent) {
1112 struct btrfs_shared_data_ref *ref;
1113 ref = btrfs_item_ptr(leaf, path->slots[0],
1114 struct btrfs_shared_data_ref);
1115 if (ret == 0) {
1116 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1117 } else {
1118 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1119 num_refs += refs_to_add;
1120 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1121 }
1122 } else {
1123 struct btrfs_extent_data_ref *ref;
1124 while (ret == -EEXIST) {
1125 ref = btrfs_item_ptr(leaf, path->slots[0],
1126 struct btrfs_extent_data_ref);
1127 if (match_extent_data_ref(leaf, ref, root_objectid,
1128 owner, offset))
1129 break;
1130 btrfs_release_path(root, path);
1131 key.offset++;
1132 ret = btrfs_insert_empty_item(trans, root, path, &key,
1133 size);
1134 if (ret && ret != -EEXIST)
1135 goto fail;
1136
1137 leaf = path->nodes[0];
1138 }
1139 ref = btrfs_item_ptr(leaf, path->slots[0],
1140 struct btrfs_extent_data_ref);
1141 if (ret == 0) {
1142 btrfs_set_extent_data_ref_root(leaf, ref,
1143 root_objectid);
1144 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1145 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1146 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1147 } else {
1148 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1149 num_refs += refs_to_add;
1150 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1151 }
1152 }
1153 btrfs_mark_buffer_dirty(leaf);
1154 ret = 0;
1155fail:
Chris Mason7bb86312007-12-11 09:25:06 -05001156 btrfs_release_path(root, path);
1157 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001158}
1159
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001160static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root,
1162 struct btrfs_path *path,
1163 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001164{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001165 struct btrfs_key key;
1166 struct btrfs_extent_data_ref *ref1 = NULL;
1167 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001168 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001169 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001170 int ret = 0;
1171
1172 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001173 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1174
1175 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1176 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1177 struct btrfs_extent_data_ref);
1178 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1179 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1180 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1181 struct btrfs_shared_data_ref);
1182 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1183#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1184 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1185 struct btrfs_extent_ref_v0 *ref0;
1186 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1187 struct btrfs_extent_ref_v0);
1188 num_refs = btrfs_ref_count_v0(leaf, ref0);
1189#endif
1190 } else {
1191 BUG();
1192 }
1193
Chris Mason56bec292009-03-13 10:10:06 -04001194 BUG_ON(num_refs < refs_to_drop);
1195 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001196
Zheng Yan31840ae2008-09-23 13:14:14 -04001197 if (num_refs == 0) {
1198 ret = btrfs_del_item(trans, root, path);
1199 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001200 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1201 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1202 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1203 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1204#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1205 else {
1206 struct btrfs_extent_ref_v0 *ref0;
1207 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1208 struct btrfs_extent_ref_v0);
1209 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1210 }
1211#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001212 btrfs_mark_buffer_dirty(leaf);
1213 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001214 return ret;
1215}
1216
1217static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1218 struct btrfs_path *path,
1219 struct btrfs_extent_inline_ref *iref)
1220{
1221 struct btrfs_key key;
1222 struct extent_buffer *leaf;
1223 struct btrfs_extent_data_ref *ref1;
1224 struct btrfs_shared_data_ref *ref2;
1225 u32 num_refs = 0;
1226
1227 leaf = path->nodes[0];
1228 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1229 if (iref) {
1230 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1231 BTRFS_EXTENT_DATA_REF_KEY) {
1232 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1233 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1234 } else {
1235 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1236 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1237 }
1238 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1239 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1240 struct btrfs_extent_data_ref);
1241 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1242 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1243 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1244 struct btrfs_shared_data_ref);
1245 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1246#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1247 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1248 struct btrfs_extent_ref_v0 *ref0;
1249 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1250 struct btrfs_extent_ref_v0);
1251 num_refs = btrfs_ref_count_v0(leaf, ref0);
1252#endif
1253 } else {
1254 WARN_ON(1);
1255 }
1256 return num_refs;
1257}
1258
1259static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1260 struct btrfs_root *root,
1261 struct btrfs_path *path,
1262 u64 bytenr, u64 parent,
1263 u64 root_objectid)
1264{
1265 struct btrfs_key key;
1266 int ret;
1267
1268 key.objectid = bytenr;
1269 if (parent) {
1270 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1271 key.offset = parent;
1272 } else {
1273 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1274 key.offset = root_objectid;
1275 }
1276
1277 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1278 if (ret > 0)
1279 ret = -ENOENT;
1280#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1281 if (ret == -ENOENT && parent) {
1282 btrfs_release_path(root, path);
1283 key.type = BTRFS_EXTENT_REF_V0_KEY;
1284 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1285 if (ret > 0)
1286 ret = -ENOENT;
1287 }
1288#endif
1289 return ret;
1290}
1291
1292static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1293 struct btrfs_root *root,
1294 struct btrfs_path *path,
1295 u64 bytenr, u64 parent,
1296 u64 root_objectid)
1297{
1298 struct btrfs_key key;
1299 int ret;
1300
1301 key.objectid = bytenr;
1302 if (parent) {
1303 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1304 key.offset = parent;
1305 } else {
1306 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1307 key.offset = root_objectid;
1308 }
1309
1310 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001311 btrfs_release_path(root, path);
1312 return ret;
1313}
1314
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001315static inline int extent_ref_type(u64 parent, u64 owner)
1316{
1317 int type;
1318 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1319 if (parent > 0)
1320 type = BTRFS_SHARED_BLOCK_REF_KEY;
1321 else
1322 type = BTRFS_TREE_BLOCK_REF_KEY;
1323 } else {
1324 if (parent > 0)
1325 type = BTRFS_SHARED_DATA_REF_KEY;
1326 else
1327 type = BTRFS_EXTENT_DATA_REF_KEY;
1328 }
1329 return type;
1330}
1331
Yan Zheng2c47e6052009-06-27 21:07:35 -04001332static int find_next_key(struct btrfs_path *path, int level,
1333 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001334
1335{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001336 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001337 if (!path->nodes[level])
1338 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001339 if (path->slots[level] + 1 >=
1340 btrfs_header_nritems(path->nodes[level]))
1341 continue;
1342 if (level == 0)
1343 btrfs_item_key_to_cpu(path->nodes[level], key,
1344 path->slots[level] + 1);
1345 else
1346 btrfs_node_key_to_cpu(path->nodes[level], key,
1347 path->slots[level] + 1);
1348 return 0;
1349 }
1350 return 1;
1351}
1352
1353/*
1354 * look for inline back ref. if back ref is found, *ref_ret is set
1355 * to the address of inline back ref, and 0 is returned.
1356 *
1357 * if back ref isn't found, *ref_ret is set to the address where it
1358 * should be inserted, and -ENOENT is returned.
1359 *
1360 * if insert is true and there are too many inline back refs, the path
1361 * points to the extent item, and -EAGAIN is returned.
1362 *
1363 * NOTE: inline back refs are ordered in the same way that back ref
1364 * items in the tree are ordered.
1365 */
1366static noinline_for_stack
1367int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1368 struct btrfs_root *root,
1369 struct btrfs_path *path,
1370 struct btrfs_extent_inline_ref **ref_ret,
1371 u64 bytenr, u64 num_bytes,
1372 u64 parent, u64 root_objectid,
1373 u64 owner, u64 offset, int insert)
1374{
1375 struct btrfs_key key;
1376 struct extent_buffer *leaf;
1377 struct btrfs_extent_item *ei;
1378 struct btrfs_extent_inline_ref *iref;
1379 u64 flags;
1380 u64 item_size;
1381 unsigned long ptr;
1382 unsigned long end;
1383 int extra_size;
1384 int type;
1385 int want;
1386 int ret;
1387 int err = 0;
1388
1389 key.objectid = bytenr;
1390 key.type = BTRFS_EXTENT_ITEM_KEY;
1391 key.offset = num_bytes;
1392
1393 want = extent_ref_type(parent, owner);
1394 if (insert) {
1395 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001396 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 } else
1398 extra_size = -1;
1399 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1400 if (ret < 0) {
1401 err = ret;
1402 goto out;
1403 }
1404 BUG_ON(ret);
1405
1406 leaf = path->nodes[0];
1407 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1408#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1409 if (item_size < sizeof(*ei)) {
1410 if (!insert) {
1411 err = -ENOENT;
1412 goto out;
1413 }
1414 ret = convert_extent_item_v0(trans, root, path, owner,
1415 extra_size);
1416 if (ret < 0) {
1417 err = ret;
1418 goto out;
1419 }
1420 leaf = path->nodes[0];
1421 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1422 }
1423#endif
1424 BUG_ON(item_size < sizeof(*ei));
1425
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001426 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1427 flags = btrfs_extent_flags(leaf, ei);
1428
1429 ptr = (unsigned long)(ei + 1);
1430 end = (unsigned long)ei + item_size;
1431
1432 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1433 ptr += sizeof(struct btrfs_tree_block_info);
1434 BUG_ON(ptr > end);
1435 } else {
1436 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1437 }
1438
1439 err = -ENOENT;
1440 while (1) {
1441 if (ptr >= end) {
1442 WARN_ON(ptr > end);
1443 break;
1444 }
1445 iref = (struct btrfs_extent_inline_ref *)ptr;
1446 type = btrfs_extent_inline_ref_type(leaf, iref);
1447 if (want < type)
1448 break;
1449 if (want > type) {
1450 ptr += btrfs_extent_inline_ref_size(type);
1451 continue;
1452 }
1453
1454 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1455 struct btrfs_extent_data_ref *dref;
1456 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1457 if (match_extent_data_ref(leaf, dref, root_objectid,
1458 owner, offset)) {
1459 err = 0;
1460 break;
1461 }
1462 if (hash_extent_data_ref_item(leaf, dref) <
1463 hash_extent_data_ref(root_objectid, owner, offset))
1464 break;
1465 } else {
1466 u64 ref_offset;
1467 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1468 if (parent > 0) {
1469 if (parent == ref_offset) {
1470 err = 0;
1471 break;
1472 }
1473 if (ref_offset < parent)
1474 break;
1475 } else {
1476 if (root_objectid == ref_offset) {
1477 err = 0;
1478 break;
1479 }
1480 if (ref_offset < root_objectid)
1481 break;
1482 }
1483 }
1484 ptr += btrfs_extent_inline_ref_size(type);
1485 }
1486 if (err == -ENOENT && insert) {
1487 if (item_size + extra_size >=
1488 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1489 err = -EAGAIN;
1490 goto out;
1491 }
1492 /*
1493 * To add new inline back ref, we have to make sure
1494 * there is no corresponding back ref item.
1495 * For simplicity, we just do not add new inline back
1496 * ref if there is any kind of item for this block
1497 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001498 if (find_next_key(path, 0, &key) == 0 &&
1499 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001500 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001501 err = -EAGAIN;
1502 goto out;
1503 }
1504 }
1505 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1506out:
Yan Zheng85d41982009-06-11 08:51:10 -04001507 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001508 path->keep_locks = 0;
1509 btrfs_unlock_up_safe(path, 1);
1510 }
1511 return err;
1512}
1513
1514/*
1515 * helper to add new inline back ref
1516 */
1517static noinline_for_stack
1518int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1519 struct btrfs_root *root,
1520 struct btrfs_path *path,
1521 struct btrfs_extent_inline_ref *iref,
1522 u64 parent, u64 root_objectid,
1523 u64 owner, u64 offset, int refs_to_add,
1524 struct btrfs_delayed_extent_op *extent_op)
1525{
1526 struct extent_buffer *leaf;
1527 struct btrfs_extent_item *ei;
1528 unsigned long ptr;
1529 unsigned long end;
1530 unsigned long item_offset;
1531 u64 refs;
1532 int size;
1533 int type;
1534 int ret;
1535
1536 leaf = path->nodes[0];
1537 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1538 item_offset = (unsigned long)iref - (unsigned long)ei;
1539
1540 type = extent_ref_type(parent, owner);
1541 size = btrfs_extent_inline_ref_size(type);
1542
1543 ret = btrfs_extend_item(trans, root, path, size);
1544 BUG_ON(ret);
1545
1546 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1547 refs = btrfs_extent_refs(leaf, ei);
1548 refs += refs_to_add;
1549 btrfs_set_extent_refs(leaf, ei, refs);
1550 if (extent_op)
1551 __run_delayed_extent_op(extent_op, leaf, ei);
1552
1553 ptr = (unsigned long)ei + item_offset;
1554 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1555 if (ptr < end - size)
1556 memmove_extent_buffer(leaf, ptr + size, ptr,
1557 end - size - ptr);
1558
1559 iref = (struct btrfs_extent_inline_ref *)ptr;
1560 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1561 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1562 struct btrfs_extent_data_ref *dref;
1563 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1564 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1565 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1566 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1567 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1568 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1569 struct btrfs_shared_data_ref *sref;
1570 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1571 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1572 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1573 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1574 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1575 } else {
1576 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1577 }
1578 btrfs_mark_buffer_dirty(leaf);
1579 return 0;
1580}
1581
1582static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1583 struct btrfs_root *root,
1584 struct btrfs_path *path,
1585 struct btrfs_extent_inline_ref **ref_ret,
1586 u64 bytenr, u64 num_bytes, u64 parent,
1587 u64 root_objectid, u64 owner, u64 offset)
1588{
1589 int ret;
1590
1591 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1592 bytenr, num_bytes, parent,
1593 root_objectid, owner, offset, 0);
1594 if (ret != -ENOENT)
1595 return ret;
1596
1597 btrfs_release_path(root, path);
1598 *ref_ret = NULL;
1599
1600 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1601 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1602 root_objectid);
1603 } else {
1604 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1605 root_objectid, owner, offset);
1606 }
1607 return ret;
1608}
1609
1610/*
1611 * helper to update/remove inline back ref
1612 */
1613static noinline_for_stack
1614int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1615 struct btrfs_root *root,
1616 struct btrfs_path *path,
1617 struct btrfs_extent_inline_ref *iref,
1618 int refs_to_mod,
1619 struct btrfs_delayed_extent_op *extent_op)
1620{
1621 struct extent_buffer *leaf;
1622 struct btrfs_extent_item *ei;
1623 struct btrfs_extent_data_ref *dref = NULL;
1624 struct btrfs_shared_data_ref *sref = NULL;
1625 unsigned long ptr;
1626 unsigned long end;
1627 u32 item_size;
1628 int size;
1629 int type;
1630 int ret;
1631 u64 refs;
1632
1633 leaf = path->nodes[0];
1634 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1635 refs = btrfs_extent_refs(leaf, ei);
1636 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1637 refs += refs_to_mod;
1638 btrfs_set_extent_refs(leaf, ei, refs);
1639 if (extent_op)
1640 __run_delayed_extent_op(extent_op, leaf, ei);
1641
1642 type = btrfs_extent_inline_ref_type(leaf, iref);
1643
1644 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1645 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1646 refs = btrfs_extent_data_ref_count(leaf, dref);
1647 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1648 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1649 refs = btrfs_shared_data_ref_count(leaf, sref);
1650 } else {
1651 refs = 1;
1652 BUG_ON(refs_to_mod != -1);
1653 }
1654
1655 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1656 refs += refs_to_mod;
1657
1658 if (refs > 0) {
1659 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1660 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1661 else
1662 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1663 } else {
1664 size = btrfs_extent_inline_ref_size(type);
1665 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1666 ptr = (unsigned long)iref;
1667 end = (unsigned long)ei + item_size;
1668 if (ptr + size < end)
1669 memmove_extent_buffer(leaf, ptr, ptr + size,
1670 end - ptr - size);
1671 item_size -= size;
1672 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1673 BUG_ON(ret);
1674 }
1675 btrfs_mark_buffer_dirty(leaf);
1676 return 0;
1677}
1678
1679static noinline_for_stack
1680int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1681 struct btrfs_root *root,
1682 struct btrfs_path *path,
1683 u64 bytenr, u64 num_bytes, u64 parent,
1684 u64 root_objectid, u64 owner,
1685 u64 offset, int refs_to_add,
1686 struct btrfs_delayed_extent_op *extent_op)
1687{
1688 struct btrfs_extent_inline_ref *iref;
1689 int ret;
1690
1691 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1692 bytenr, num_bytes, parent,
1693 root_objectid, owner, offset, 1);
1694 if (ret == 0) {
1695 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1696 ret = update_inline_extent_backref(trans, root, path, iref,
1697 refs_to_add, extent_op);
1698 } else if (ret == -ENOENT) {
1699 ret = setup_inline_extent_backref(trans, root, path, iref,
1700 parent, root_objectid,
1701 owner, offset, refs_to_add,
1702 extent_op);
1703 }
1704 return ret;
1705}
1706
1707static int insert_extent_backref(struct btrfs_trans_handle *trans,
1708 struct btrfs_root *root,
1709 struct btrfs_path *path,
1710 u64 bytenr, u64 parent, u64 root_objectid,
1711 u64 owner, u64 offset, int refs_to_add)
1712{
1713 int ret;
1714 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1715 BUG_ON(refs_to_add != 1);
1716 ret = insert_tree_block_ref(trans, root, path, bytenr,
1717 parent, root_objectid);
1718 } else {
1719 ret = insert_extent_data_ref(trans, root, path, bytenr,
1720 parent, root_objectid,
1721 owner, offset, refs_to_add);
1722 }
1723 return ret;
1724}
1725
1726static int remove_extent_backref(struct btrfs_trans_handle *trans,
1727 struct btrfs_root *root,
1728 struct btrfs_path *path,
1729 struct btrfs_extent_inline_ref *iref,
1730 int refs_to_drop, int is_data)
1731{
1732 int ret;
1733
1734 BUG_ON(!is_data && refs_to_drop != 1);
1735 if (iref) {
1736 ret = update_inline_extent_backref(trans, root, path, iref,
1737 -refs_to_drop, NULL);
1738 } else if (is_data) {
1739 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1740 } else {
1741 ret = btrfs_del_item(trans, root, path);
1742 }
1743 return ret;
1744}
1745
Chris Mason15916de2008-11-19 21:17:22 -05001746static void btrfs_issue_discard(struct block_device *bdev,
1747 u64 start, u64 len)
1748{
Christoph Hellwig746cd1e2009-09-12 07:35:43 +02001749 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +04001750 BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
Chris Mason15916de2008-11-19 21:17:22 -05001751}
Chris Mason15916de2008-11-19 21:17:22 -05001752
Liu Hui1f3c79a2009-01-05 15:57:51 -05001753static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1754 u64 num_bytes)
1755{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001756 int ret;
1757 u64 map_length = num_bytes;
1758 struct btrfs_multi_bio *multi = NULL;
1759
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001760 if (!btrfs_test_opt(root, DISCARD))
1761 return 0;
1762
Liu Hui1f3c79a2009-01-05 15:57:51 -05001763 /* Tell the block device(s) that the sectors can be discarded */
1764 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1765 bytenr, &map_length, &multi, 0);
1766 if (!ret) {
1767 struct btrfs_bio_stripe *stripe = multi->stripes;
1768 int i;
1769
1770 if (map_length > num_bytes)
1771 map_length = num_bytes;
1772
1773 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1774 btrfs_issue_discard(stripe->dev->bdev,
1775 stripe->physical,
1776 map_length);
1777 }
1778 kfree(multi);
1779 }
1780
1781 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001782}
1783
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001784int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1785 struct btrfs_root *root,
1786 u64 bytenr, u64 num_bytes, u64 parent,
1787 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001788{
1789 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001790 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1791 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001792
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001793 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1794 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1795 parent, root_objectid, (int)owner,
1796 BTRFS_ADD_DELAYED_REF, NULL);
1797 } else {
1798 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1799 parent, root_objectid, owner, offset,
1800 BTRFS_ADD_DELAYED_REF, NULL);
1801 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001802 return ret;
1803}
1804
Chris Mason925baed2008-06-25 16:01:30 -04001805static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001806 struct btrfs_root *root,
1807 u64 bytenr, u64 num_bytes,
1808 u64 parent, u64 root_objectid,
1809 u64 owner, u64 offset, int refs_to_add,
1810 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001811{
Chris Mason5caf2a02007-04-02 11:20:42 -04001812 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001813 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001814 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001815 u64 refs;
1816 int ret;
1817 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001818
Chris Mason5caf2a02007-04-02 11:20:42 -04001819 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001820 if (!path)
1821 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001822
Chris Mason3c12ac72008-04-21 12:01:38 -04001823 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001824 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001825 /* this will setup the path even if it fails to insert the back ref */
1826 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1827 path, bytenr, num_bytes, parent,
1828 root_objectid, owner, offset,
1829 refs_to_add, extent_op);
1830 if (ret == 0)
1831 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001832
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001833 if (ret != -EAGAIN) {
1834 err = ret;
1835 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001836 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001837
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001838 leaf = path->nodes[0];
1839 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1840 refs = btrfs_extent_refs(leaf, item);
1841 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1842 if (extent_op)
1843 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001844
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001845 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001846 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001847
Chris Mason3c12ac72008-04-21 12:01:38 -04001848 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001849 path->leave_spinning = 1;
1850
Chris Mason56bec292009-03-13 10:10:06 -04001851 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001852 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001853 path, bytenr, parent, root_objectid,
1854 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001855 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001856out:
Chris Mason74493f72007-12-11 09:25:06 -05001857 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001858 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001859}
1860
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001861static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1862 struct btrfs_root *root,
1863 struct btrfs_delayed_ref_node *node,
1864 struct btrfs_delayed_extent_op *extent_op,
1865 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001866{
Chris Mason56bec292009-03-13 10:10:06 -04001867 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001868 struct btrfs_delayed_data_ref *ref;
1869 struct btrfs_key ins;
1870 u64 parent = 0;
1871 u64 ref_root = 0;
1872 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001873
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001874 ins.objectid = node->bytenr;
1875 ins.offset = node->num_bytes;
1876 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001877
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 ref = btrfs_delayed_node_to_data_ref(node);
1879 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1880 parent = ref->parent;
1881 else
1882 ref_root = ref->root;
1883
1884 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1885 if (extent_op) {
1886 BUG_ON(extent_op->update_key);
1887 flags |= extent_op->flags_to_set;
1888 }
1889 ret = alloc_reserved_file_extent(trans, root,
1890 parent, ref_root, flags,
1891 ref->objectid, ref->offset,
1892 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001893 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1894 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1895 node->num_bytes, parent,
1896 ref_root, ref->objectid,
1897 ref->offset, node->ref_mod,
1898 extent_op);
1899 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1900 ret = __btrfs_free_extent(trans, root, node->bytenr,
1901 node->num_bytes, parent,
1902 ref_root, ref->objectid,
1903 ref->offset, node->ref_mod,
1904 extent_op);
1905 } else {
1906 BUG();
1907 }
Chris Mason56bec292009-03-13 10:10:06 -04001908 return ret;
1909}
1910
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001911static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1912 struct extent_buffer *leaf,
1913 struct btrfs_extent_item *ei)
1914{
1915 u64 flags = btrfs_extent_flags(leaf, ei);
1916 if (extent_op->update_flags) {
1917 flags |= extent_op->flags_to_set;
1918 btrfs_set_extent_flags(leaf, ei, flags);
1919 }
1920
1921 if (extent_op->update_key) {
1922 struct btrfs_tree_block_info *bi;
1923 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1924 bi = (struct btrfs_tree_block_info *)(ei + 1);
1925 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1926 }
1927}
1928
1929static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1930 struct btrfs_root *root,
1931 struct btrfs_delayed_ref_node *node,
1932 struct btrfs_delayed_extent_op *extent_op)
1933{
1934 struct btrfs_key key;
1935 struct btrfs_path *path;
1936 struct btrfs_extent_item *ei;
1937 struct extent_buffer *leaf;
1938 u32 item_size;
1939 int ret;
1940 int err = 0;
1941
1942 path = btrfs_alloc_path();
1943 if (!path)
1944 return -ENOMEM;
1945
1946 key.objectid = node->bytenr;
1947 key.type = BTRFS_EXTENT_ITEM_KEY;
1948 key.offset = node->num_bytes;
1949
1950 path->reada = 1;
1951 path->leave_spinning = 1;
1952 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1953 path, 0, 1);
1954 if (ret < 0) {
1955 err = ret;
1956 goto out;
1957 }
1958 if (ret > 0) {
1959 err = -EIO;
1960 goto out;
1961 }
1962
1963 leaf = path->nodes[0];
1964 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1965#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1966 if (item_size < sizeof(*ei)) {
1967 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1968 path, (u64)-1, 0);
1969 if (ret < 0) {
1970 err = ret;
1971 goto out;
1972 }
1973 leaf = path->nodes[0];
1974 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1975 }
1976#endif
1977 BUG_ON(item_size < sizeof(*ei));
1978 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1979 __run_delayed_extent_op(extent_op, leaf, ei);
1980
1981 btrfs_mark_buffer_dirty(leaf);
1982out:
1983 btrfs_free_path(path);
1984 return err;
1985}
1986
1987static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1988 struct btrfs_root *root,
1989 struct btrfs_delayed_ref_node *node,
1990 struct btrfs_delayed_extent_op *extent_op,
1991 int insert_reserved)
1992{
1993 int ret = 0;
1994 struct btrfs_delayed_tree_ref *ref;
1995 struct btrfs_key ins;
1996 u64 parent = 0;
1997 u64 ref_root = 0;
1998
1999 ins.objectid = node->bytenr;
2000 ins.offset = node->num_bytes;
2001 ins.type = BTRFS_EXTENT_ITEM_KEY;
2002
2003 ref = btrfs_delayed_node_to_tree_ref(node);
2004 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2005 parent = ref->parent;
2006 else
2007 ref_root = ref->root;
2008
2009 BUG_ON(node->ref_mod != 1);
2010 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2011 BUG_ON(!extent_op || !extent_op->update_flags ||
2012 !extent_op->update_key);
2013 ret = alloc_reserved_tree_block(trans, root,
2014 parent, ref_root,
2015 extent_op->flags_to_set,
2016 &extent_op->key,
2017 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002018 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2019 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2020 node->num_bytes, parent, ref_root,
2021 ref->level, 0, 1, extent_op);
2022 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2023 ret = __btrfs_free_extent(trans, root, node->bytenr,
2024 node->num_bytes, parent, ref_root,
2025 ref->level, 0, 1, extent_op);
2026 } else {
2027 BUG();
2028 }
2029 return ret;
2030}
2031
Chris Mason56bec292009-03-13 10:10:06 -04002032/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002033static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2034 struct btrfs_root *root,
2035 struct btrfs_delayed_ref_node *node,
2036 struct btrfs_delayed_extent_op *extent_op,
2037 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002038{
Josef Bacikeb099672009-02-12 09:27:38 -05002039 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002040 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002041 struct btrfs_delayed_ref_head *head;
2042 /*
2043 * we've hit the end of the chain and we were supposed
2044 * to insert this extent into the tree. But, it got
2045 * deleted before we ever needed to insert it, so all
2046 * we have to do is clean up the accounting
2047 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002048 BUG_ON(extent_op);
2049 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002050 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002051 btrfs_pin_extent(root, node->bytenr,
2052 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002053 if (head->is_data) {
2054 ret = btrfs_del_csums(trans, root,
2055 node->bytenr,
2056 node->num_bytes);
2057 BUG_ON(ret);
2058 }
Chris Mason56bec292009-03-13 10:10:06 -04002059 }
Chris Mason56bec292009-03-13 10:10:06 -04002060 mutex_unlock(&head->mutex);
2061 return 0;
2062 }
Josef Bacikeb099672009-02-12 09:27:38 -05002063
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2065 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2066 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2067 insert_reserved);
2068 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2069 node->type == BTRFS_SHARED_DATA_REF_KEY)
2070 ret = run_delayed_data_ref(trans, root, node, extent_op,
2071 insert_reserved);
2072 else
2073 BUG();
2074 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002075}
2076
Chris Mason56bec292009-03-13 10:10:06 -04002077static noinline struct btrfs_delayed_ref_node *
2078select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002079{
Chris Mason56bec292009-03-13 10:10:06 -04002080 struct rb_node *node;
2081 struct btrfs_delayed_ref_node *ref;
2082 int action = BTRFS_ADD_DELAYED_REF;
2083again:
2084 /*
2085 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2086 * this prevents ref count from going down to zero when
2087 * there still are pending delayed ref.
2088 */
2089 node = rb_prev(&head->node.rb_node);
2090 while (1) {
2091 if (!node)
2092 break;
2093 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2094 rb_node);
2095 if (ref->bytenr != head->node.bytenr)
2096 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002097 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002098 return ref;
2099 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002100 }
Chris Mason56bec292009-03-13 10:10:06 -04002101 if (action == BTRFS_ADD_DELAYED_REF) {
2102 action = BTRFS_DROP_DELAYED_REF;
2103 goto again;
2104 }
2105 return NULL;
2106}
2107
Chris Masonc3e69d52009-03-13 10:17:05 -04002108static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2109 struct btrfs_root *root,
2110 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002111{
Chris Mason56bec292009-03-13 10:10:06 -04002112 struct btrfs_delayed_ref_root *delayed_refs;
2113 struct btrfs_delayed_ref_node *ref;
2114 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002115 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002116 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002117 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002118 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002119
2120 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002121 while (1) {
2122 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002123 /* pick a new head ref from the cluster list */
2124 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002125 break;
Chris Mason56bec292009-03-13 10:10:06 -04002126
Chris Masonc3e69d52009-03-13 10:17:05 -04002127 locked_ref = list_entry(cluster->next,
2128 struct btrfs_delayed_ref_head, cluster);
2129
2130 /* grab the lock that says we are going to process
2131 * all the refs for this head */
2132 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2133
2134 /*
2135 * we may have dropped the spin lock to get the head
2136 * mutex lock, and that might have given someone else
2137 * time to free the head. If that's true, it has been
2138 * removed from our list and we can move on.
2139 */
2140 if (ret == -EAGAIN) {
2141 locked_ref = NULL;
2142 count++;
2143 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002144 }
2145 }
2146
2147 /*
2148 * record the must insert reserved flag before we
2149 * drop the spin lock.
2150 */
2151 must_insert_reserved = locked_ref->must_insert_reserved;
2152 locked_ref->must_insert_reserved = 0;
2153
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002154 extent_op = locked_ref->extent_op;
2155 locked_ref->extent_op = NULL;
2156
Chris Mason56bec292009-03-13 10:10:06 -04002157 /*
2158 * locked_ref is the head node, so we have to go one
2159 * node back for any delayed ref updates
2160 */
Chris Mason56bec292009-03-13 10:10:06 -04002161 ref = select_delayed_ref(locked_ref);
2162 if (!ref) {
2163 /* All delayed refs have been processed, Go ahead
2164 * and send the head node to run_one_delayed_ref,
2165 * so that any accounting fixes can happen
2166 */
2167 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002168
2169 if (extent_op && must_insert_reserved) {
2170 kfree(extent_op);
2171 extent_op = NULL;
2172 }
2173
2174 if (extent_op) {
2175 spin_unlock(&delayed_refs->lock);
2176
2177 ret = run_delayed_extent_op(trans, root,
2178 ref, extent_op);
2179 BUG_ON(ret);
2180 kfree(extent_op);
2181
2182 cond_resched();
2183 spin_lock(&delayed_refs->lock);
2184 continue;
2185 }
2186
Chris Masonc3e69d52009-03-13 10:17:05 -04002187 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002188 locked_ref = NULL;
2189 }
2190
2191 ref->in_tree = 0;
2192 rb_erase(&ref->rb_node, &delayed_refs->root);
2193 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002194
Chris Mason56bec292009-03-13 10:10:06 -04002195 spin_unlock(&delayed_refs->lock);
2196
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002197 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002198 must_insert_reserved);
2199 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002200
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002201 btrfs_put_delayed_ref(ref);
2202 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002203 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002204
Chris Mason1887be62009-03-13 10:11:24 -04002205 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002206 spin_lock(&delayed_refs->lock);
2207 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002208 return count;
2209}
2210
2211/*
2212 * this starts processing the delayed reference count updates and
2213 * extent insertions we have queued up so far. count can be
2214 * 0, which means to process everything in the tree at the start
2215 * of the run (but not newly added entries), or it can be some target
2216 * number you'd like to process.
2217 */
2218int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2219 struct btrfs_root *root, unsigned long count)
2220{
2221 struct rb_node *node;
2222 struct btrfs_delayed_ref_root *delayed_refs;
2223 struct btrfs_delayed_ref_node *ref;
2224 struct list_head cluster;
2225 int ret;
2226 int run_all = count == (unsigned long)-1;
2227 int run_most = 0;
2228
2229 if (root == root->fs_info->extent_root)
2230 root = root->fs_info->tree_root;
2231
2232 delayed_refs = &trans->transaction->delayed_refs;
2233 INIT_LIST_HEAD(&cluster);
2234again:
2235 spin_lock(&delayed_refs->lock);
2236 if (count == 0) {
2237 count = delayed_refs->num_entries * 2;
2238 run_most = 1;
2239 }
2240 while (1) {
2241 if (!(run_all || run_most) &&
2242 delayed_refs->num_heads_ready < 64)
2243 break;
2244
2245 /*
2246 * go find something we can process in the rbtree. We start at
2247 * the beginning of the tree, and then build a cluster
2248 * of refs to process starting at the first one we are able to
2249 * lock
2250 */
2251 ret = btrfs_find_ref_cluster(trans, &cluster,
2252 delayed_refs->run_delayed_start);
2253 if (ret)
2254 break;
2255
2256 ret = run_clustered_refs(trans, root, &cluster);
2257 BUG_ON(ret < 0);
2258
2259 count -= min_t(unsigned long, ret, count);
2260
2261 if (count == 0)
2262 break;
2263 }
2264
Chris Mason56bec292009-03-13 10:10:06 -04002265 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002266 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002267 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002268 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002269 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002270
2271 while (node) {
2272 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2273 rb_node);
2274 if (btrfs_delayed_ref_is_head(ref)) {
2275 struct btrfs_delayed_ref_head *head;
2276
2277 head = btrfs_delayed_node_to_head(ref);
2278 atomic_inc(&ref->refs);
2279
2280 spin_unlock(&delayed_refs->lock);
2281 mutex_lock(&head->mutex);
2282 mutex_unlock(&head->mutex);
2283
2284 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002285 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002286 goto again;
2287 }
2288 node = rb_next(node);
2289 }
2290 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002291 schedule_timeout(1);
2292 goto again;
2293 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002294out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002295 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002296 return 0;
2297}
2298
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2300 struct btrfs_root *root,
2301 u64 bytenr, u64 num_bytes, u64 flags,
2302 int is_data)
2303{
2304 struct btrfs_delayed_extent_op *extent_op;
2305 int ret;
2306
2307 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2308 if (!extent_op)
2309 return -ENOMEM;
2310
2311 extent_op->flags_to_set = flags;
2312 extent_op->update_flags = 1;
2313 extent_op->update_key = 0;
2314 extent_op->is_data = is_data ? 1 : 0;
2315
2316 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2317 if (ret)
2318 kfree(extent_op);
2319 return ret;
2320}
2321
2322static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2323 struct btrfs_root *root,
2324 struct btrfs_path *path,
2325 u64 objectid, u64 offset, u64 bytenr)
2326{
2327 struct btrfs_delayed_ref_head *head;
2328 struct btrfs_delayed_ref_node *ref;
2329 struct btrfs_delayed_data_ref *data_ref;
2330 struct btrfs_delayed_ref_root *delayed_refs;
2331 struct rb_node *node;
2332 int ret = 0;
2333
2334 ret = -ENOENT;
2335 delayed_refs = &trans->transaction->delayed_refs;
2336 spin_lock(&delayed_refs->lock);
2337 head = btrfs_find_delayed_ref_head(trans, bytenr);
2338 if (!head)
2339 goto out;
2340
2341 if (!mutex_trylock(&head->mutex)) {
2342 atomic_inc(&head->node.refs);
2343 spin_unlock(&delayed_refs->lock);
2344
2345 btrfs_release_path(root->fs_info->extent_root, path);
2346
2347 mutex_lock(&head->mutex);
2348 mutex_unlock(&head->mutex);
2349 btrfs_put_delayed_ref(&head->node);
2350 return -EAGAIN;
2351 }
2352
2353 node = rb_prev(&head->node.rb_node);
2354 if (!node)
2355 goto out_unlock;
2356
2357 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2358
2359 if (ref->bytenr != bytenr)
2360 goto out_unlock;
2361
2362 ret = 1;
2363 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2364 goto out_unlock;
2365
2366 data_ref = btrfs_delayed_node_to_data_ref(ref);
2367
2368 node = rb_prev(node);
2369 if (node) {
2370 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2371 if (ref->bytenr == bytenr)
2372 goto out_unlock;
2373 }
2374
2375 if (data_ref->root != root->root_key.objectid ||
2376 data_ref->objectid != objectid || data_ref->offset != offset)
2377 goto out_unlock;
2378
2379 ret = 0;
2380out_unlock:
2381 mutex_unlock(&head->mutex);
2382out:
2383 spin_unlock(&delayed_refs->lock);
2384 return ret;
2385}
2386
2387static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2388 struct btrfs_root *root,
2389 struct btrfs_path *path,
2390 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002391{
2392 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002393 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002394 struct btrfs_extent_data_ref *ref;
2395 struct btrfs_extent_inline_ref *iref;
2396 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002397 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002398 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002399 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002400
Chris Masonbe20aa92007-12-17 20:14:01 -05002401 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002402 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002403 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002404
Chris Masonbe20aa92007-12-17 20:14:01 -05002405 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2406 if (ret < 0)
2407 goto out;
2408 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002409
2410 ret = -ENOENT;
2411 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002412 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002413
Zheng Yan31840ae2008-09-23 13:14:14 -04002414 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002415 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002416 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002417
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002418 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002419 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002420
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002421 ret = 1;
2422 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2423#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2424 if (item_size < sizeof(*ei)) {
2425 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2426 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002427 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002428#endif
2429 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2430
2431 if (item_size != sizeof(*ei) +
2432 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2433 goto out;
2434
2435 if (btrfs_extent_generation(leaf, ei) <=
2436 btrfs_root_last_snapshot(&root->root_item))
2437 goto out;
2438
2439 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2440 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2441 BTRFS_EXTENT_DATA_REF_KEY)
2442 goto out;
2443
2444 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2445 if (btrfs_extent_refs(leaf, ei) !=
2446 btrfs_extent_data_ref_count(leaf, ref) ||
2447 btrfs_extent_data_ref_root(leaf, ref) !=
2448 root->root_key.objectid ||
2449 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2450 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2451 goto out;
2452
Yan Zhengf321e492008-07-30 09:26:11 -04002453 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002454out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002455 return ret;
2456}
2457
2458int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2459 struct btrfs_root *root,
2460 u64 objectid, u64 offset, u64 bytenr)
2461{
2462 struct btrfs_path *path;
2463 int ret;
2464 int ret2;
2465
2466 path = btrfs_alloc_path();
2467 if (!path)
2468 return -ENOENT;
2469
2470 do {
2471 ret = check_committed_ref(trans, root, path, objectid,
2472 offset, bytenr);
2473 if (ret && ret != -ENOENT)
2474 goto out;
2475
2476 ret2 = check_delayed_ref(trans, root, path, objectid,
2477 offset, bytenr);
2478 } while (ret2 == -EAGAIN);
2479
2480 if (ret2 && ret2 != -ENOENT) {
2481 ret = ret2;
2482 goto out;
2483 }
2484
2485 if (ret != -ENOENT || ret2 != -ENOENT)
2486 ret = 0;
2487out:
Yan Zhengf321e492008-07-30 09:26:11 -04002488 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002489 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2490 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002491 return ret;
2492}
2493
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002494#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002495int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2496 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002497{
Chris Mason5f39d392007-10-15 16:14:19 -04002498 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002499 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002500 u64 root_gen;
2501 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002502 int i;
Chris Masondb945352007-10-15 16:15:53 -04002503 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002504 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002505 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002506
Chris Mason3768f362007-03-13 16:47:54 -04002507 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002508 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002509
Zheng Yane4657682008-09-26 10:04:53 -04002510 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2511 shared = 0;
2512 root_gen = root->root_key.offset;
2513 } else {
2514 shared = 1;
2515 root_gen = trans->transid - 1;
2516 }
2517
Chris Masondb945352007-10-15 16:15:53 -04002518 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002519 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002520
Zheng Yan31840ae2008-09-23 13:14:14 -04002521 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002522 struct btrfs_leaf_ref *ref;
2523 struct btrfs_extent_info *info;
2524
Zheng Yan31840ae2008-09-23 13:14:14 -04002525 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002526 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002527 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002528 goto out;
2529 }
2530
Zheng Yane4657682008-09-26 10:04:53 -04002531 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002532 ref->bytenr = buf->start;
2533 ref->owner = btrfs_header_owner(buf);
2534 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002535 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002536 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002537
Zheng Yan31840ae2008-09-23 13:14:14 -04002538 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002539 u64 disk_bytenr;
2540 btrfs_item_key_to_cpu(buf, &key, i);
2541 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2542 continue;
2543 fi = btrfs_item_ptr(buf, i,
2544 struct btrfs_file_extent_item);
2545 if (btrfs_file_extent_type(buf, fi) ==
2546 BTRFS_FILE_EXTENT_INLINE)
2547 continue;
2548 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2549 if (disk_bytenr == 0)
2550 continue;
2551
2552 info->bytenr = disk_bytenr;
2553 info->num_bytes =
2554 btrfs_file_extent_disk_num_bytes(buf, fi);
2555 info->objectid = key.objectid;
2556 info->offset = key.offset;
2557 info++;
2558 }
2559
Zheng Yane4657682008-09-26 10:04:53 -04002560 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002561 if (ret == -EEXIST && shared) {
2562 struct btrfs_leaf_ref *old;
2563 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2564 BUG_ON(!old);
2565 btrfs_remove_leaf_ref(root, old);
2566 btrfs_free_leaf_ref(root, old);
2567 ret = btrfs_add_leaf_ref(root, ref, shared);
2568 }
Yan Zheng31153d82008-07-28 15:32:19 -04002569 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002570 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002571 }
2572out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002573 return ret;
2574}
2575
Chris Masonb7a9f292009-02-04 09:23:45 -05002576/* when a block goes through cow, we update the reference counts of
2577 * everything that block points to. The internal pointers of the block
2578 * can be in just about any order, and it is likely to have clusters of
2579 * things that are close together and clusters of things that are not.
2580 *
2581 * To help reduce the seeks that come with updating all of these reference
2582 * counts, sort them by byte number before actual updates are done.
2583 *
2584 * struct refsort is used to match byte number to slot in the btree block.
2585 * we sort based on the byte number and then use the slot to actually
2586 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002587 *
2588 * struct refsort is smaller than strcut btrfs_item and smaller than
2589 * struct btrfs_key_ptr. Since we're currently limited to the page size
2590 * for a btree block, there's no way for a kmalloc of refsorts for a
2591 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002592 */
2593struct refsort {
2594 u64 bytenr;
2595 u32 slot;
2596};
2597
2598/*
2599 * for passing into sort()
2600 */
2601static int refsort_cmp(const void *a_void, const void *b_void)
2602{
2603 const struct refsort *a = a_void;
2604 const struct refsort *b = b_void;
2605
2606 if (a->bytenr < b->bytenr)
2607 return -1;
2608 if (a->bytenr > b->bytenr)
2609 return 1;
2610 return 0;
2611}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002612#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002613
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002614static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002615 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002616 struct extent_buffer *buf,
2617 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002618{
2619 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002620 u64 num_bytes;
2621 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002622 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002623 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002624 struct btrfs_key key;
2625 struct btrfs_file_extent_item *fi;
2626 int i;
2627 int level;
2628 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002629 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002630 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002631
2632 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002633 nritems = btrfs_header_nritems(buf);
2634 level = btrfs_header_level(buf);
2635
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002636 if (!root->ref_cows && level == 0)
2637 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002638
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002639 if (inc)
2640 process_func = btrfs_inc_extent_ref;
2641 else
2642 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002643
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002644 if (full_backref)
2645 parent = buf->start;
2646 else
2647 parent = 0;
2648
Zheng Yan31840ae2008-09-23 13:14:14 -04002649 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002650 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002651 btrfs_item_key_to_cpu(buf, &key, i);
2652 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002653 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002654 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002655 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002656 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002657 BTRFS_FILE_EXTENT_INLINE)
2658 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002659 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2660 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002661 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002662
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002663 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2664 key.offset -= btrfs_file_extent_offset(buf, fi);
2665 ret = process_func(trans, root, bytenr, num_bytes,
2666 parent, ref_root, key.objectid,
2667 key.offset);
2668 if (ret)
2669 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002670 } else {
2671 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002672 num_bytes = btrfs_level_size(root, level - 1);
2673 ret = process_func(trans, root, bytenr, num_bytes,
2674 parent, ref_root, level - 1, 0);
2675 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002676 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002677 }
2678 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002679 return 0;
2680fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002681 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002682 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002683}
2684
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002685int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2686 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002687{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002688 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2689}
Zheng Yan31840ae2008-09-23 13:14:14 -04002690
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002691int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2692 struct extent_buffer *buf, int full_backref)
2693{
2694 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002695}
2696
Chris Mason9078a3e2007-04-26 16:46:15 -04002697static int write_one_cache_group(struct btrfs_trans_handle *trans,
2698 struct btrfs_root *root,
2699 struct btrfs_path *path,
2700 struct btrfs_block_group_cache *cache)
2701{
2702 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002703 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002704 unsigned long bi;
2705 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002706
Chris Mason9078a3e2007-04-26 16:46:15 -04002707 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002708 if (ret < 0)
2709 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002710 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002711
2712 leaf = path->nodes[0];
2713 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2714 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2715 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002716 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002717fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002718 if (ret)
2719 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002720 return 0;
2721
2722}
2723
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002724static struct btrfs_block_group_cache *
2725next_block_group(struct btrfs_root *root,
2726 struct btrfs_block_group_cache *cache)
2727{
2728 struct rb_node *node;
2729 spin_lock(&root->fs_info->block_group_cache_lock);
2730 node = rb_next(&cache->cache_node);
2731 btrfs_put_block_group(cache);
2732 if (node) {
2733 cache = rb_entry(node, struct btrfs_block_group_cache,
2734 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002735 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002736 } else
2737 cache = NULL;
2738 spin_unlock(&root->fs_info->block_group_cache_lock);
2739 return cache;
2740}
2741
Josef Bacik0af3d002010-06-21 14:48:16 -04002742static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2743 struct btrfs_trans_handle *trans,
2744 struct btrfs_path *path)
2745{
2746 struct btrfs_root *root = block_group->fs_info->tree_root;
2747 struct inode *inode = NULL;
2748 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002749 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002750 int num_pages = 0;
2751 int retries = 0;
2752 int ret = 0;
2753
2754 /*
2755 * If this block group is smaller than 100 megs don't bother caching the
2756 * block group.
2757 */
2758 if (block_group->key.offset < (100 * 1024 * 1024)) {
2759 spin_lock(&block_group->lock);
2760 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2761 spin_unlock(&block_group->lock);
2762 return 0;
2763 }
2764
2765again:
2766 inode = lookup_free_space_inode(root, block_group, path);
2767 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2768 ret = PTR_ERR(inode);
2769 btrfs_release_path(root, path);
2770 goto out;
2771 }
2772
2773 if (IS_ERR(inode)) {
2774 BUG_ON(retries);
2775 retries++;
2776
2777 if (block_group->ro)
2778 goto out_free;
2779
2780 ret = create_free_space_inode(root, trans, block_group, path);
2781 if (ret)
2782 goto out_free;
2783 goto again;
2784 }
2785
2786 /*
2787 * We want to set the generation to 0, that way if anything goes wrong
2788 * from here on out we know not to trust this cache when we load up next
2789 * time.
2790 */
2791 BTRFS_I(inode)->generation = 0;
2792 ret = btrfs_update_inode(trans, root, inode);
2793 WARN_ON(ret);
2794
2795 if (i_size_read(inode) > 0) {
2796 ret = btrfs_truncate_free_space_cache(root, trans, path,
2797 inode);
2798 if (ret)
2799 goto out_put;
2800 }
2801
2802 spin_lock(&block_group->lock);
2803 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002804 /* We're not cached, don't bother trying to write stuff out */
2805 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002806 spin_unlock(&block_group->lock);
2807 goto out_put;
2808 }
2809 spin_unlock(&block_group->lock);
2810
2811 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2812 if (!num_pages)
2813 num_pages = 1;
2814
2815 /*
2816 * Just to make absolutely sure we have enough space, we're going to
2817 * preallocate 12 pages worth of space for each block group. In
2818 * practice we ought to use at most 8, but we need extra space so we can
2819 * add our header and have a terminator between the extents and the
2820 * bitmaps.
2821 */
2822 num_pages *= 16;
2823 num_pages *= PAGE_CACHE_SIZE;
2824
2825 ret = btrfs_check_data_free_space(inode, num_pages);
2826 if (ret)
2827 goto out_put;
2828
2829 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2830 num_pages, num_pages,
2831 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002832 if (!ret)
2833 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002834 btrfs_free_reserved_data_space(inode, num_pages);
2835out_put:
2836 iput(inode);
2837out_free:
2838 btrfs_release_path(root, path);
2839out:
2840 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002841 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002842 spin_unlock(&block_group->lock);
2843
2844 return ret;
2845}
2846
Chris Mason96b51792007-10-15 16:15:19 -04002847int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2848 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002849{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002850 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002851 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002852 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002853 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002854
2855 path = btrfs_alloc_path();
2856 if (!path)
2857 return -ENOMEM;
2858
Josef Bacik0af3d002010-06-21 14:48:16 -04002859again:
2860 while (1) {
2861 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2862 while (cache) {
2863 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2864 break;
2865 cache = next_block_group(root, cache);
2866 }
2867 if (!cache) {
2868 if (last == 0)
2869 break;
2870 last = 0;
2871 continue;
2872 }
2873 err = cache_save_setup(cache, trans, path);
2874 last = cache->key.objectid + cache->key.offset;
2875 btrfs_put_block_group(cache);
2876 }
2877
Chris Masond3977122009-01-05 21:25:51 -05002878 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002879 if (last == 0) {
2880 err = btrfs_run_delayed_refs(trans, root,
2881 (unsigned long)-1);
2882 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002883 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002884
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002885 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2886 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002887 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2888 btrfs_put_block_group(cache);
2889 goto again;
2890 }
2891
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002892 if (cache->dirty)
2893 break;
2894 cache = next_block_group(root, cache);
2895 }
2896 if (!cache) {
2897 if (last == 0)
2898 break;
2899 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002900 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002901 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002902
Josef Bacik0cb59c92010-07-02 12:14:14 -04002903 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2904 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002905 cache->dirty = 0;
2906 last = cache->key.objectid + cache->key.offset;
2907
2908 err = write_one_cache_group(trans, root, path, cache);
2909 BUG_ON(err);
2910 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002911 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002912
Josef Bacik0cb59c92010-07-02 12:14:14 -04002913 while (1) {
2914 /*
2915 * I don't think this is needed since we're just marking our
2916 * preallocated extent as written, but just in case it can't
2917 * hurt.
2918 */
2919 if (last == 0) {
2920 err = btrfs_run_delayed_refs(trans, root,
2921 (unsigned long)-1);
2922 BUG_ON(err);
2923 }
2924
2925 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2926 while (cache) {
2927 /*
2928 * Really this shouldn't happen, but it could if we
2929 * couldn't write the entire preallocated extent and
2930 * splitting the extent resulted in a new block.
2931 */
2932 if (cache->dirty) {
2933 btrfs_put_block_group(cache);
2934 goto again;
2935 }
2936 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2937 break;
2938 cache = next_block_group(root, cache);
2939 }
2940 if (!cache) {
2941 if (last == 0)
2942 break;
2943 last = 0;
2944 continue;
2945 }
2946
2947 btrfs_write_out_cache(root, trans, cache, path);
2948
2949 /*
2950 * If we didn't have an error then the cache state is still
2951 * NEED_WRITE, so we can set it to WRITTEN.
2952 */
2953 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2954 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2955 last = cache->key.objectid + cache->key.offset;
2956 btrfs_put_block_group(cache);
2957 }
2958
Chris Mason9078a3e2007-04-26 16:46:15 -04002959 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002960 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002961}
2962
Yan Zhengd2fb3432008-12-11 16:30:39 -05002963int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2964{
2965 struct btrfs_block_group_cache *block_group;
2966 int readonly = 0;
2967
2968 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2969 if (!block_group || block_group->ro)
2970 readonly = 1;
2971 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002972 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002973 return readonly;
2974}
2975
Chris Mason593060d2008-03-25 16:50:33 -04002976static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2977 u64 total_bytes, u64 bytes_used,
2978 struct btrfs_space_info **space_info)
2979{
2980 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002981 int i;
2982 int factor;
2983
2984 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2985 BTRFS_BLOCK_GROUP_RAID10))
2986 factor = 2;
2987 else
2988 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002989
2990 found = __find_space_info(info, flags);
2991 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002992 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002993 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002994 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002995 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002996 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002997 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002998 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002999 *space_info = found;
3000 return 0;
3001 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003002 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003003 if (!found)
3004 return -ENOMEM;
3005
Yan, Zhengb742bb82010-05-16 10:46:24 -04003006 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3007 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003008 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003009 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04003010 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
3011 BTRFS_BLOCK_GROUP_SYSTEM |
3012 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04003013 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003014 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003015 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003016 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003017 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003018 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003019 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003020 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003021 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04003022 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003023 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003024 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04003025 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04003026 return 0;
3027}
3028
Chris Mason8790d502008-04-03 16:29:03 -04003029static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3030{
3031 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04003032 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04003033 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04003034 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04003035 if (extra_flags) {
3036 if (flags & BTRFS_BLOCK_GROUP_DATA)
3037 fs_info->avail_data_alloc_bits |= extra_flags;
3038 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3039 fs_info->avail_metadata_alloc_bits |= extra_flags;
3040 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3041 fs_info->avail_system_alloc_bits |= extra_flags;
3042 }
3043}
Chris Mason593060d2008-03-25 16:50:33 -04003044
Yan Zheng2b820322008-11-17 21:11:30 -05003045u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003046{
Yan Zheng2b820322008-11-17 21:11:30 -05003047 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04003048
3049 if (num_devices == 1)
3050 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3051 if (num_devices < 4)
3052 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3053
Chris Masonec44a352008-04-28 15:29:52 -04003054 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3055 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003056 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003057 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003058 }
Chris Masonec44a352008-04-28 15:29:52 -04003059
3060 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003061 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003062 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003063 }
Chris Masonec44a352008-04-28 15:29:52 -04003064
3065 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3066 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3067 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3068 (flags & BTRFS_BLOCK_GROUP_DUP)))
3069 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3070 return flags;
3071}
3072
Yan, Zhengb742bb82010-05-16 10:46:24 -04003073static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003074{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003075 if (flags & BTRFS_BLOCK_GROUP_DATA)
3076 flags |= root->fs_info->avail_data_alloc_bits &
3077 root->fs_info->data_alloc_profile;
3078 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3079 flags |= root->fs_info->avail_system_alloc_bits &
3080 root->fs_info->system_alloc_profile;
3081 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3082 flags |= root->fs_info->avail_metadata_alloc_bits &
3083 root->fs_info->metadata_alloc_profile;
3084 return btrfs_reduce_alloc_profile(root, flags);
3085}
Josef Bacik6a632092009-02-20 11:00:09 -05003086
Yan, Zhengb742bb82010-05-16 10:46:24 -04003087static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
3088{
3089 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003090
Yan, Zhengb742bb82010-05-16 10:46:24 -04003091 if (data)
3092 flags = BTRFS_BLOCK_GROUP_DATA;
3093 else if (root == root->fs_info->chunk_root)
3094 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3095 else
3096 flags = BTRFS_BLOCK_GROUP_METADATA;
3097
3098 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003099}
3100
3101void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3102{
Josef Bacik6a632092009-02-20 11:00:09 -05003103 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003104 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003105}
3106
3107/*
3108 * This will check the space that the inode allocates from to make sure we have
3109 * enough space for bytes.
3110 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003111int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003112{
3113 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003114 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003115 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003116 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003117
3118 /* make sure bytes are sectorsize aligned */
3119 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3120
Josef Bacik0af3d002010-06-21 14:48:16 -04003121 if (root == root->fs_info->tree_root) {
3122 alloc_chunk = 0;
3123 committed = 1;
3124 }
3125
Josef Bacik6a632092009-02-20 11:00:09 -05003126 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003127 if (!data_sinfo)
3128 goto alloc;
3129
Josef Bacik6a632092009-02-20 11:00:09 -05003130again:
3131 /* make sure we have enough space to handle the data first */
3132 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003133 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3134 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3135 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003136
3137 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003138 struct btrfs_trans_handle *trans;
3139
Josef Bacik6a632092009-02-20 11:00:09 -05003140 /*
3141 * if we don't have enough free bytes in this space then we need
3142 * to alloc a new chunk.
3143 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003144 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003145 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003146
3147 data_sinfo->force_alloc = 1;
3148 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003149alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003150 alloc_target = btrfs_get_alloc_profile(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003151 trans = btrfs_join_transaction(root, 1);
3152 if (IS_ERR(trans))
3153 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003154
3155 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3156 bytes + 2 * 1024 * 1024,
3157 alloc_target, 0);
3158 btrfs_end_transaction(trans, root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003159 if (ret < 0)
Josef Bacik6a632092009-02-20 11:00:09 -05003160 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04003161
3162 if (!data_sinfo) {
3163 btrfs_set_inode_space_info(root, inode);
3164 data_sinfo = BTRFS_I(inode)->space_info;
3165 }
Josef Bacik6a632092009-02-20 11:00:09 -05003166 goto again;
3167 }
3168 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003169
3170 /* commit the current transaction and try again */
Sage Weildd7e0b72009-09-29 18:38:44 -04003171 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003172 committed = 1;
3173 trans = btrfs_join_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003174 if (IS_ERR(trans))
3175 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003176 ret = btrfs_commit_transaction(trans, root);
3177 if (ret)
3178 return ret;
3179 goto again;
3180 }
3181
Chris Mason933b5852010-05-26 11:31:00 -04003182#if 0 /* I hope we never need this code again, just in case */
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003183 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3184 "%llu bytes_reserved, " "%llu bytes_pinned, "
3185 "%llu bytes_readonly, %llu may use %llu total\n",
3186 (unsigned long long)bytes,
Joel Becker21380932009-04-21 12:38:29 -07003187 (unsigned long long)data_sinfo->bytes_used,
3188 (unsigned long long)data_sinfo->bytes_reserved,
3189 (unsigned long long)data_sinfo->bytes_pinned,
3190 (unsigned long long)data_sinfo->bytes_readonly,
3191 (unsigned long long)data_sinfo->bytes_may_use,
3192 (unsigned long long)data_sinfo->total_bytes);
Chris Mason933b5852010-05-26 11:31:00 -04003193#endif
Josef Bacik6a632092009-02-20 11:00:09 -05003194 return -ENOSPC;
3195 }
3196 data_sinfo->bytes_may_use += bytes;
3197 BTRFS_I(inode)->reserved_bytes += bytes;
3198 spin_unlock(&data_sinfo->lock);
3199
Josef Bacik9ed74f22009-09-11 16:12:44 -04003200 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003201}
3202
3203/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003204 * called when we are clearing an delalloc extent from the
3205 * inode's io_tree or there was an error for whatever reason
3206 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003207 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003208void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003209{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003210 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003211 struct btrfs_space_info *data_sinfo;
3212
3213 /* make sure bytes are sectorsize aligned */
3214 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3215
3216 data_sinfo = BTRFS_I(inode)->space_info;
3217 spin_lock(&data_sinfo->lock);
3218 data_sinfo->bytes_may_use -= bytes;
3219 BTRFS_I(inode)->reserved_bytes -= bytes;
3220 spin_unlock(&data_sinfo->lock);
3221}
3222
Josef Bacik97e728d2009-04-21 17:40:57 -04003223static void force_metadata_allocation(struct btrfs_fs_info *info)
3224{
3225 struct list_head *head = &info->space_info;
3226 struct btrfs_space_info *found;
3227
3228 rcu_read_lock();
3229 list_for_each_entry_rcu(found, head, list) {
3230 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3231 found->force_alloc = 1;
3232 }
3233 rcu_read_unlock();
3234}
3235
Chris Masone5bc2452010-10-26 13:37:56 -04003236static int should_alloc_chunk(struct btrfs_root *root,
3237 struct btrfs_space_info *sinfo, u64 alloc_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003238{
3239 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Masone5bc2452010-10-26 13:37:56 -04003240 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003241
3242 if (sinfo->bytes_used + sinfo->bytes_reserved +
3243 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3244 return 0;
3245
3246 if (sinfo->bytes_used + sinfo->bytes_reserved +
3247 alloc_bytes < div_factor(num_bytes, 8))
3248 return 0;
3249
Chris Masone5bc2452010-10-26 13:37:56 -04003250 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3251 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3252
3253 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003254 return 0;
3255
Yan, Zheng424499d2010-05-16 10:46:25 -04003256 return 1;
3257}
3258
Chris Mason6324fbf2008-03-24 15:01:59 -04003259static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3260 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003261 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003262{
3263 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003264 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Yan Zhengc146afa2008-11-12 14:34:12 -05003265 int ret = 0;
3266
Josef Bacik97e728d2009-04-21 17:40:57 -04003267 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003268
Yan Zheng2b820322008-11-17 21:11:30 -05003269 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003270
Chris Mason6324fbf2008-03-24 15:01:59 -04003271 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003272 if (!space_info) {
3273 ret = update_space_info(extent_root->fs_info, flags,
3274 0, 0, &space_info);
3275 BUG_ON(ret);
3276 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003277 BUG_ON(!space_info);
3278
Josef Bacik25179202008-10-29 14:49:05 -04003279 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003280 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003281 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003282 if (space_info->full) {
3283 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003284 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003285 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003286
Chris Masone5bc2452010-10-26 13:37:56 -04003287 if (!force && !should_alloc_chunk(extent_root, space_info,
3288 alloc_bytes)) {
Josef Bacik25179202008-10-29 14:49:05 -04003289 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003290 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003291 }
Josef Bacik25179202008-10-29 14:49:05 -04003292 spin_unlock(&space_info->lock);
3293
Josef Bacik97e728d2009-04-21 17:40:57 -04003294 /*
Josef Bacik67377732010-09-16 16:19:09 -04003295 * If we have mixed data/metadata chunks we want to make sure we keep
3296 * allocating mixed chunks instead of individual chunks.
3297 */
3298 if (btrfs_mixed_space_info(space_info))
3299 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3300
3301 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003302 * if we're doing a data chunk, go ahead and make sure that
3303 * we keep a reasonable number of metadata chunks allocated in the
3304 * FS as well.
3305 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003306 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003307 fs_info->data_chunk_allocations++;
3308 if (!(fs_info->data_chunk_allocations %
3309 fs_info->metadata_ratio))
3310 force_metadata_allocation(fs_info);
3311 }
3312
Yan Zheng2b820322008-11-17 21:11:30 -05003313 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003314 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003315 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003316 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003317 else
3318 ret = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003319 space_info->force_alloc = 0;
3320 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003321out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003322 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003323 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003324}
3325
Yan, Zheng5da9d012010-05-16 10:46:25 -04003326/*
3327 * shrink metadata reservation for delalloc
3328 */
3329static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003330 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003331{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003332 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003333 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003334 u64 reserved;
3335 u64 max_reclaim;
3336 u64 reclaimed = 0;
3337 int pause = 1;
Chris Masonbf9022e2010-10-26 13:40:45 -04003338 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003339
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003340 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003341 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003342
3343 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003344 reserved = space_info->bytes_reserved;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003345
3346 if (reserved == 0)
3347 return 0;
3348
3349 max_reclaim = min(reserved, to_reclaim);
3350
3351 while (1) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003352 /* have the flusher threads jump in and do some IO */
3353 smp_mb();
3354 nr_pages = min_t(unsigned long, nr_pages,
3355 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3356 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003357
Josef Bacik0019f102010-10-15 15:18:40 -04003358 spin_lock(&space_info->lock);
3359 if (reserved > space_info->bytes_reserved)
3360 reclaimed += reserved - space_info->bytes_reserved;
3361 reserved = space_info->bytes_reserved;
3362 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003363
3364 if (reserved == 0 || reclaimed >= max_reclaim)
3365 break;
3366
3367 if (trans && trans->transaction->blocked)
3368 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003369
3370 __set_current_state(TASK_INTERRUPTIBLE);
3371 schedule_timeout(pause);
3372 pause <<= 1;
3373 if (pause > HZ / 10)
3374 pause = HZ / 10;
3375
Yan, Zheng5da9d012010-05-16 10:46:25 -04003376 }
3377 return reclaimed >= to_reclaim;
3378}
3379
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003380/*
3381 * Retries tells us how many times we've called reserve_metadata_bytes. The
3382 * idea is if this is the first call (retries == 0) then we will add to our
3383 * reserved count if we can't make the allocation in order to hold our place
3384 * while we go and try and free up space. That way for retries > 1 we don't try
3385 * and add space, we just check to see if the amount of unused space is >= the
3386 * total space, meaning that our reservation is valid.
3387 *
3388 * However if we don't intend to retry this reservation, pass -1 as retries so
3389 * that it short circuits this logic.
3390 */
3391static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3392 struct btrfs_root *root,
3393 struct btrfs_block_rsv *block_rsv,
3394 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003395{
3396 struct btrfs_space_info *space_info = block_rsv->space_info;
3397 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003398 u64 num_bytes = orig_bytes;
3399 int retries = 0;
3400 int ret = 0;
3401 bool reserved = false;
Josef Bacik38227932010-10-26 12:52:53 -04003402 bool committed = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003403
3404again:
3405 ret = -ENOSPC;
3406 if (reserved)
3407 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003408
3409 spin_lock(&space_info->lock);
3410 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003411 space_info->bytes_pinned + space_info->bytes_readonly +
3412 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003413
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003414 /*
3415 * The idea here is that we've not already over-reserved the block group
3416 * then we can go ahead and save our reservation first and then start
3417 * flushing if we need to. Otherwise if we've already overcommitted
3418 * lets start flushing stuff first and then come back and try to make
3419 * our reservation.
3420 */
3421 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003422 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003423 if (unused >= num_bytes) {
3424 if (!reserved)
3425 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003426 ret = 0;
3427 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003428 /*
3429 * Ok set num_bytes to orig_bytes since we aren't
3430 * overocmmitted, this way we only try and reclaim what
3431 * we need.
3432 */
3433 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003434 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003435 } else {
3436 /*
3437 * Ok we're over committed, set num_bytes to the overcommitted
3438 * amount plus the amount of bytes that we need for this
3439 * reservation.
3440 */
3441 num_bytes = unused - space_info->total_bytes +
3442 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003443 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003444
3445 /*
3446 * Couldn't make our reservation, save our place so while we're trying
3447 * to reclaim space we can actually use it instead of somebody else
3448 * stealing it from us.
3449 */
3450 if (ret && !reserved) {
3451 space_info->bytes_reserved += orig_bytes;
3452 reserved = true;
3453 }
3454
Yan, Zhengf0486c62010-05-16 10:46:25 -04003455 spin_unlock(&space_info->lock);
3456
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003457 if (!ret)
3458 return 0;
3459
3460 if (!flush)
3461 goto out;
3462
3463 /*
3464 * We do synchronous shrinking since we don't actually unreserve
3465 * metadata until after the IO is completed.
3466 */
3467 ret = shrink_delalloc(trans, root, num_bytes, 1);
3468 if (ret > 0)
3469 return 0;
3470 else if (ret < 0)
3471 goto out;
3472
3473 /*
3474 * So if we were overcommitted it's possible that somebody else flushed
3475 * out enough space and we simply didn't have enough space to reclaim,
3476 * so go back around and try again.
3477 */
3478 if (retries < 2) {
3479 retries++;
3480 goto again;
3481 }
3482
3483 spin_lock(&space_info->lock);
3484 /*
3485 * Not enough space to be reclaimed, don't bother committing the
3486 * transaction.
3487 */
3488 if (space_info->bytes_pinned < orig_bytes)
3489 ret = -ENOSPC;
3490 spin_unlock(&space_info->lock);
3491 if (ret)
3492 goto out;
3493
3494 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003495 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003496 goto out;
3497
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003498 ret = -ENOSPC;
3499 trans = btrfs_join_transaction(root, 1);
3500 if (IS_ERR(trans))
3501 goto out;
3502 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003503 if (!ret) {
3504 trans = NULL;
3505 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003506 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003507 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003508
3509out:
3510 if (reserved) {
3511 spin_lock(&space_info->lock);
3512 space_info->bytes_reserved -= orig_bytes;
3513 spin_unlock(&space_info->lock);
3514 }
3515
Yan, Zhengf0486c62010-05-16 10:46:25 -04003516 return ret;
3517}
3518
3519static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3520 struct btrfs_root *root)
3521{
3522 struct btrfs_block_rsv *block_rsv;
3523 if (root->ref_cows)
3524 block_rsv = trans->block_rsv;
3525 else
3526 block_rsv = root->block_rsv;
3527
3528 if (!block_rsv)
3529 block_rsv = &root->fs_info->empty_block_rsv;
3530
3531 return block_rsv;
3532}
3533
3534static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3535 u64 num_bytes)
3536{
3537 int ret = -ENOSPC;
3538 spin_lock(&block_rsv->lock);
3539 if (block_rsv->reserved >= num_bytes) {
3540 block_rsv->reserved -= num_bytes;
3541 if (block_rsv->reserved < block_rsv->size)
3542 block_rsv->full = 0;
3543 ret = 0;
3544 }
3545 spin_unlock(&block_rsv->lock);
3546 return ret;
3547}
3548
3549static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3550 u64 num_bytes, int update_size)
3551{
3552 spin_lock(&block_rsv->lock);
3553 block_rsv->reserved += num_bytes;
3554 if (update_size)
3555 block_rsv->size += num_bytes;
3556 else if (block_rsv->reserved >= block_rsv->size)
3557 block_rsv->full = 1;
3558 spin_unlock(&block_rsv->lock);
3559}
3560
3561void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3562 struct btrfs_block_rsv *dest, u64 num_bytes)
3563{
3564 struct btrfs_space_info *space_info = block_rsv->space_info;
3565
3566 spin_lock(&block_rsv->lock);
3567 if (num_bytes == (u64)-1)
3568 num_bytes = block_rsv->size;
3569 block_rsv->size -= num_bytes;
3570 if (block_rsv->reserved >= block_rsv->size) {
3571 num_bytes = block_rsv->reserved - block_rsv->size;
3572 block_rsv->reserved = block_rsv->size;
3573 block_rsv->full = 1;
3574 } else {
3575 num_bytes = 0;
3576 }
3577 spin_unlock(&block_rsv->lock);
3578
3579 if (num_bytes > 0) {
3580 if (dest) {
3581 block_rsv_add_bytes(dest, num_bytes, 0);
3582 } else {
3583 spin_lock(&space_info->lock);
3584 space_info->bytes_reserved -= num_bytes;
3585 spin_unlock(&space_info->lock);
3586 }
3587 }
3588}
3589
3590static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3591 struct btrfs_block_rsv *dst, u64 num_bytes)
3592{
3593 int ret;
3594
3595 ret = block_rsv_use_bytes(src, num_bytes);
3596 if (ret)
3597 return ret;
3598
3599 block_rsv_add_bytes(dst, num_bytes, 1);
3600 return 0;
3601}
3602
3603void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3604{
3605 memset(rsv, 0, sizeof(*rsv));
3606 spin_lock_init(&rsv->lock);
3607 atomic_set(&rsv->usage, 1);
3608 rsv->priority = 6;
3609 INIT_LIST_HEAD(&rsv->list);
3610}
3611
3612struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3613{
3614 struct btrfs_block_rsv *block_rsv;
3615 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003616
3617 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3618 if (!block_rsv)
3619 return NULL;
3620
3621 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003622 block_rsv->space_info = __find_space_info(fs_info,
3623 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003624 return block_rsv;
3625}
3626
3627void btrfs_free_block_rsv(struct btrfs_root *root,
3628 struct btrfs_block_rsv *rsv)
3629{
3630 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3631 btrfs_block_rsv_release(root, rsv, (u64)-1);
3632 if (!rsv->durable)
3633 kfree(rsv);
3634 }
3635}
3636
3637/*
3638 * make the block_rsv struct be able to capture freed space.
3639 * the captured space will re-add to the the block_rsv struct
3640 * after transaction commit
3641 */
3642void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3643 struct btrfs_block_rsv *block_rsv)
3644{
3645 block_rsv->durable = 1;
3646 mutex_lock(&fs_info->durable_block_rsv_mutex);
3647 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3648 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3649}
3650
3651int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3652 struct btrfs_root *root,
3653 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003654 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003655{
3656 int ret;
3657
3658 if (num_bytes == 0)
3659 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003660
3661 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003662 if (!ret) {
3663 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3664 return 0;
3665 }
3666
Yan, Zhengf0486c62010-05-16 10:46:25 -04003667 return ret;
3668}
3669
3670int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3671 struct btrfs_root *root,
3672 struct btrfs_block_rsv *block_rsv,
3673 u64 min_reserved, int min_factor)
3674{
3675 u64 num_bytes = 0;
3676 int commit_trans = 0;
3677 int ret = -ENOSPC;
3678
3679 if (!block_rsv)
3680 return 0;
3681
3682 spin_lock(&block_rsv->lock);
3683 if (min_factor > 0)
3684 num_bytes = div_factor(block_rsv->size, min_factor);
3685 if (min_reserved > num_bytes)
3686 num_bytes = min_reserved;
3687
3688 if (block_rsv->reserved >= num_bytes) {
3689 ret = 0;
3690 } else {
3691 num_bytes -= block_rsv->reserved;
3692 if (block_rsv->durable &&
3693 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3694 commit_trans = 1;
3695 }
3696 spin_unlock(&block_rsv->lock);
3697 if (!ret)
3698 return 0;
3699
3700 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003701 ret = reserve_metadata_bytes(trans, root, block_rsv,
3702 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003703 if (!ret) {
3704 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3705 return 0;
3706 }
3707 }
3708
3709 if (commit_trans) {
3710 if (trans)
3711 return -EAGAIN;
3712
3713 trans = btrfs_join_transaction(root, 1);
3714 BUG_ON(IS_ERR(trans));
3715 ret = btrfs_commit_transaction(trans, root);
3716 return 0;
3717 }
3718
3719 WARN_ON(1);
3720 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
3721 block_rsv->size, block_rsv->reserved,
3722 block_rsv->freed[0], block_rsv->freed[1]);
3723
3724 return -ENOSPC;
3725}
3726
3727int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3728 struct btrfs_block_rsv *dst_rsv,
3729 u64 num_bytes)
3730{
3731 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3732}
3733
3734void btrfs_block_rsv_release(struct btrfs_root *root,
3735 struct btrfs_block_rsv *block_rsv,
3736 u64 num_bytes)
3737{
3738 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3739 if (global_rsv->full || global_rsv == block_rsv ||
3740 block_rsv->space_info != global_rsv->space_info)
3741 global_rsv = NULL;
3742 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3743}
3744
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003745/*
3746 * helper to calculate size of global block reservation.
3747 * the desired value is sum of space used by extent tree,
3748 * checksum tree and root tree
3749 */
3750static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3751{
3752 struct btrfs_space_info *sinfo;
3753 u64 num_bytes;
3754 u64 meta_used;
3755 u64 data_used;
3756 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3757#if 0
3758 /*
3759 * per tree used space accounting can be inaccuracy, so we
3760 * can't rely on it.
3761 */
3762 spin_lock(&fs_info->extent_root->accounting_lock);
3763 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3764 spin_unlock(&fs_info->extent_root->accounting_lock);
3765
3766 spin_lock(&fs_info->csum_root->accounting_lock);
3767 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3768 spin_unlock(&fs_info->csum_root->accounting_lock);
3769
3770 spin_lock(&fs_info->tree_root->accounting_lock);
3771 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3772 spin_unlock(&fs_info->tree_root->accounting_lock);
3773#endif
3774 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3775 spin_lock(&sinfo->lock);
3776 data_used = sinfo->bytes_used;
3777 spin_unlock(&sinfo->lock);
3778
3779 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3780 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003781 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3782 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003783 meta_used = sinfo->bytes_used;
3784 spin_unlock(&sinfo->lock);
3785
3786 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3787 csum_size * 2;
3788 num_bytes += div64_u64(data_used + meta_used, 50);
3789
3790 if (num_bytes * 3 > meta_used)
3791 num_bytes = div64_u64(meta_used, 3);
3792
3793 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3794}
3795
3796static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3797{
3798 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3799 struct btrfs_space_info *sinfo = block_rsv->space_info;
3800 u64 num_bytes;
3801
3802 num_bytes = calc_global_metadata_size(fs_info);
3803
3804 spin_lock(&block_rsv->lock);
3805 spin_lock(&sinfo->lock);
3806
3807 block_rsv->size = num_bytes;
3808
3809 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003810 sinfo->bytes_reserved + sinfo->bytes_readonly +
3811 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003812
3813 if (sinfo->total_bytes > num_bytes) {
3814 num_bytes = sinfo->total_bytes - num_bytes;
3815 block_rsv->reserved += num_bytes;
3816 sinfo->bytes_reserved += num_bytes;
3817 }
3818
3819 if (block_rsv->reserved >= block_rsv->size) {
3820 num_bytes = block_rsv->reserved - block_rsv->size;
3821 sinfo->bytes_reserved -= num_bytes;
3822 block_rsv->reserved = block_rsv->size;
3823 block_rsv->full = 1;
3824 }
3825#if 0
3826 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3827 block_rsv->size, block_rsv->reserved);
3828#endif
3829 spin_unlock(&sinfo->lock);
3830 spin_unlock(&block_rsv->lock);
3831}
3832
Yan, Zhengf0486c62010-05-16 10:46:25 -04003833static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3834{
3835 struct btrfs_space_info *space_info;
3836
3837 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3838 fs_info->chunk_block_rsv.space_info = space_info;
3839 fs_info->chunk_block_rsv.priority = 10;
3840
3841 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003842 fs_info->global_block_rsv.space_info = space_info;
3843 fs_info->global_block_rsv.priority = 10;
3844 fs_info->global_block_rsv.refill_used = 1;
3845 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003846 fs_info->trans_block_rsv.space_info = space_info;
3847 fs_info->empty_block_rsv.space_info = space_info;
3848 fs_info->empty_block_rsv.priority = 10;
3849
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003850 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3851 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3852 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3853 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003854 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003855
3856 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3857
3858 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3859
3860 update_global_block_rsv(fs_info);
3861}
3862
3863static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3864{
3865 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3866 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3867 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3868 WARN_ON(fs_info->trans_block_rsv.size > 0);
3869 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3870 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3871 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003872}
3873
Yan, Zhenga22285a2010-05-16 10:48:46 -04003874static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3875{
3876 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3877 3 * num_items;
3878}
3879
3880int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3881 struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003882 int num_items)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003883{
3884 u64 num_bytes;
3885 int ret;
3886
3887 if (num_items == 0 || root->fs_info->chunk_root == root)
3888 return 0;
3889
3890 num_bytes = calc_trans_metadata_size(root, num_items);
3891 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003892 num_bytes);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003893 if (!ret) {
3894 trans->bytes_reserved += num_bytes;
3895 trans->block_rsv = &root->fs_info->trans_block_rsv;
3896 }
3897 return ret;
3898}
3899
3900void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3901 struct btrfs_root *root)
3902{
3903 if (!trans->bytes_reserved)
3904 return;
3905
3906 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3907 btrfs_block_rsv_release(root, trans->block_rsv,
3908 trans->bytes_reserved);
3909 trans->bytes_reserved = 0;
3910}
3911
Yan, Zhengd68fc572010-05-16 10:49:58 -04003912int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3913 struct inode *inode)
3914{
3915 struct btrfs_root *root = BTRFS_I(inode)->root;
3916 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3917 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3918
3919 /*
3920 * one for deleting orphan item, one for updating inode and
3921 * two for calling btrfs_truncate_inode_items.
3922 *
3923 * btrfs_truncate_inode_items is a delete operation, it frees
3924 * more space than it uses in most cases. So two units of
3925 * metadata space should be enough for calling it many times.
3926 * If all of the metadata space is used, we can commit
3927 * transaction and use space it freed.
3928 */
3929 u64 num_bytes = calc_trans_metadata_size(root, 4);
3930 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3931}
3932
3933void btrfs_orphan_release_metadata(struct inode *inode)
3934{
3935 struct btrfs_root *root = BTRFS_I(inode)->root;
3936 u64 num_bytes = calc_trans_metadata_size(root, 4);
3937 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3938}
3939
Yan, Zhenga22285a2010-05-16 10:48:46 -04003940int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3941 struct btrfs_pending_snapshot *pending)
3942{
3943 struct btrfs_root *root = pending->root;
3944 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3945 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3946 /*
3947 * two for root back/forward refs, two for directory entries
3948 * and one for root of the snapshot.
3949 */
3950 u64 num_bytes = calc_trans_metadata_size(root, 5);
3951 dst_rsv->space_info = src_rsv->space_info;
3952 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3953}
3954
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003955static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3956{
3957 return num_bytes >>= 3;
3958}
3959
3960int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3961{
3962 struct btrfs_root *root = BTRFS_I(inode)->root;
3963 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3964 u64 to_reserve;
3965 int nr_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003966 int ret;
3967
3968 if (btrfs_transaction_in_commit(root->fs_info))
3969 schedule_timeout(1);
3970
3971 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003972
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003973 spin_lock(&BTRFS_I(inode)->accounting_lock);
3974 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
3975 if (nr_extents > BTRFS_I(inode)->reserved_extents) {
3976 nr_extents -= BTRFS_I(inode)->reserved_extents;
3977 to_reserve = calc_trans_metadata_size(root, nr_extents);
3978 } else {
3979 nr_extents = 0;
3980 to_reserve = 0;
3981 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003982 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003983
3984 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003985 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
3986 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003987 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003988
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003989 spin_lock(&BTRFS_I(inode)->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003990 BTRFS_I(inode)->reserved_extents += nr_extents;
3991 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
3992 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3993
3994 block_rsv_add_bytes(block_rsv, to_reserve, 1);
3995
3996 if (block_rsv->size > 512 * 1024 * 1024)
Josef Bacik0019f102010-10-15 15:18:40 -04003997 shrink_delalloc(NULL, root, to_reserve, 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003998
3999 return 0;
4000}
4001
4002void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4003{
4004 struct btrfs_root *root = BTRFS_I(inode)->root;
4005 u64 to_free;
4006 int nr_extents;
4007
4008 num_bytes = ALIGN(num_bytes, root->sectorsize);
4009 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
4010
4011 spin_lock(&BTRFS_I(inode)->accounting_lock);
4012 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4013 if (nr_extents < BTRFS_I(inode)->reserved_extents) {
4014 nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents;
4015 BTRFS_I(inode)->reserved_extents -= nr_extents;
4016 } else {
4017 nr_extents = 0;
4018 }
4019 spin_unlock(&BTRFS_I(inode)->accounting_lock);
4020
4021 to_free = calc_csum_metadata_size(inode, num_bytes);
4022 if (nr_extents > 0)
4023 to_free += calc_trans_metadata_size(root, nr_extents);
4024
4025 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4026 to_free);
4027}
4028
4029int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4030{
4031 int ret;
4032
4033 ret = btrfs_check_data_free_space(inode, num_bytes);
4034 if (ret)
4035 return ret;
4036
4037 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4038 if (ret) {
4039 btrfs_free_reserved_data_space(inode, num_bytes);
4040 return ret;
4041 }
4042
4043 return 0;
4044}
4045
4046void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4047{
4048 btrfs_delalloc_release_metadata(inode, num_bytes);
4049 btrfs_free_reserved_data_space(inode, num_bytes);
4050}
4051
Chris Mason9078a3e2007-04-26 16:46:15 -04004052static int update_block_group(struct btrfs_trans_handle *trans,
4053 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004054 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004055{
Josef Bacik0af3d002010-06-21 14:48:16 -04004056 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004057 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004058 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004059 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004060 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004061 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004062
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004063 /* block accounting for super block */
4064 spin_lock(&info->delalloc_lock);
4065 old_val = btrfs_super_bytes_used(&info->super_copy);
4066 if (alloc)
4067 old_val += num_bytes;
4068 else
4069 old_val -= num_bytes;
4070 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004071 spin_unlock(&info->delalloc_lock);
4072
Chris Masond3977122009-01-05 21:25:51 -05004073 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004074 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004075 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004076 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004077 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4078 BTRFS_BLOCK_GROUP_RAID1 |
4079 BTRFS_BLOCK_GROUP_RAID10))
4080 factor = 2;
4081 else
4082 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004083 /*
4084 * If this block group has free space cache written out, we
4085 * need to make sure to load it if we are removing space. This
4086 * is because we need the unpinning stage to actually add the
4087 * space back to the block group, otherwise we will leak space.
4088 */
4089 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004090 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004091
Chris Masondb945352007-10-15 16:15:53 -04004092 byte_in_group = bytenr - cache->key.objectid;
4093 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004094
Josef Bacik25179202008-10-29 14:49:05 -04004095 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004096 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004097
4098 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4099 cache->disk_cache_state < BTRFS_DC_CLEAR)
4100 cache->disk_cache_state = BTRFS_DC_CLEAR;
4101
Josef Bacik0f9dd462008-09-23 13:14:11 -04004102 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004103 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004104 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004105 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004106 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004107 btrfs_set_block_group_used(&cache->item, old_val);
4108 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004109 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004110 cache->space_info->bytes_used += num_bytes;
4111 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004112 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004113 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004114 } else {
Chris Masondb945352007-10-15 16:15:53 -04004115 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004116 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004117 cache->pinned += num_bytes;
4118 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004119 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004120 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004121 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004122 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004123
Yan, Zhengf0486c62010-05-16 10:46:25 -04004124 set_extent_dirty(info->pinned_extents,
4125 bytenr, bytenr + num_bytes - 1,
4126 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004127 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004128 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004129 total -= num_bytes;
4130 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004131 }
4132 return 0;
4133}
Chris Mason6324fbf2008-03-24 15:01:59 -04004134
Chris Masona061fc82008-05-07 11:43:44 -04004135static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4136{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004137 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004138 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004139
4140 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4141 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004142 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004143
Yan Zhengd2fb3432008-12-11 16:30:39 -05004144 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004145 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004146
4147 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004148}
4149
Yan, Zhengf0486c62010-05-16 10:46:25 -04004150static int pin_down_extent(struct btrfs_root *root,
4151 struct btrfs_block_group_cache *cache,
4152 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004153{
Yan Zheng11833d62009-09-11 16:11:19 -04004154 spin_lock(&cache->space_info->lock);
4155 spin_lock(&cache->lock);
4156 cache->pinned += num_bytes;
4157 cache->space_info->bytes_pinned += num_bytes;
4158 if (reserved) {
4159 cache->reserved -= num_bytes;
4160 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004161 }
Yan Zheng11833d62009-09-11 16:11:19 -04004162 spin_unlock(&cache->lock);
4163 spin_unlock(&cache->space_info->lock);
4164
Yan, Zhengf0486c62010-05-16 10:46:25 -04004165 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4166 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004167 return 0;
4168}
Chris Mason9078a3e2007-04-26 16:46:15 -04004169
Yan, Zhengf0486c62010-05-16 10:46:25 -04004170/*
4171 * this function must be called within transaction
4172 */
4173int btrfs_pin_extent(struct btrfs_root *root,
4174 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004175{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004176 struct btrfs_block_group_cache *cache;
4177
4178 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4179 BUG_ON(!cache);
4180
4181 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4182
4183 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004184 return 0;
4185}
Zheng Yane8569812008-09-26 10:05:48 -04004186
Yan, Zhengf0486c62010-05-16 10:46:25 -04004187/*
4188 * update size of reserved extents. this function may return -EAGAIN
4189 * if 'reserve' is true or 'sinfo' is false.
4190 */
4191static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
4192 u64 num_bytes, int reserve, int sinfo)
4193{
4194 int ret = 0;
4195 if (sinfo) {
4196 struct btrfs_space_info *space_info = cache->space_info;
4197 spin_lock(&space_info->lock);
4198 spin_lock(&cache->lock);
4199 if (reserve) {
4200 if (cache->ro) {
4201 ret = -EAGAIN;
4202 } else {
4203 cache->reserved += num_bytes;
4204 space_info->bytes_reserved += num_bytes;
4205 }
4206 } else {
4207 if (cache->ro)
4208 space_info->bytes_readonly += num_bytes;
4209 cache->reserved -= num_bytes;
4210 space_info->bytes_reserved -= num_bytes;
4211 }
4212 spin_unlock(&cache->lock);
4213 spin_unlock(&space_info->lock);
4214 } else {
4215 spin_lock(&cache->lock);
4216 if (cache->ro) {
4217 ret = -EAGAIN;
4218 } else {
4219 if (reserve)
4220 cache->reserved += num_bytes;
4221 else
4222 cache->reserved -= num_bytes;
4223 }
4224 spin_unlock(&cache->lock);
4225 }
4226 return ret;
4227}
4228
Yan Zheng11833d62009-09-11 16:11:19 -04004229int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4230 struct btrfs_root *root)
4231{
4232 struct btrfs_fs_info *fs_info = root->fs_info;
4233 struct btrfs_caching_control *next;
4234 struct btrfs_caching_control *caching_ctl;
4235 struct btrfs_block_group_cache *cache;
4236
4237 down_write(&fs_info->extent_commit_sem);
4238
4239 list_for_each_entry_safe(caching_ctl, next,
4240 &fs_info->caching_block_groups, list) {
4241 cache = caching_ctl->block_group;
4242 if (block_group_cache_done(cache)) {
4243 cache->last_byte_to_unpin = (u64)-1;
4244 list_del_init(&caching_ctl->list);
4245 put_caching_control(caching_ctl);
4246 } else {
4247 cache->last_byte_to_unpin = caching_ctl->progress;
4248 }
4249 }
4250
4251 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4252 fs_info->pinned_extents = &fs_info->freed_extents[1];
4253 else
4254 fs_info->pinned_extents = &fs_info->freed_extents[0];
4255
4256 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004257
4258 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004259 return 0;
4260}
4261
4262static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4263{
4264 struct btrfs_fs_info *fs_info = root->fs_info;
4265 struct btrfs_block_group_cache *cache = NULL;
4266 u64 len;
4267
4268 while (start <= end) {
4269 if (!cache ||
4270 start >= cache->key.objectid + cache->key.offset) {
4271 if (cache)
4272 btrfs_put_block_group(cache);
4273 cache = btrfs_lookup_block_group(fs_info, start);
4274 BUG_ON(!cache);
4275 }
4276
4277 len = cache->key.objectid + cache->key.offset - start;
4278 len = min(len, end + 1 - start);
4279
4280 if (start < cache->last_byte_to_unpin) {
4281 len = min(len, cache->last_byte_to_unpin - start);
4282 btrfs_add_free_space(cache, start, len);
4283 }
Josef Bacik25179202008-10-29 14:49:05 -04004284
Yan, Zhengf0486c62010-05-16 10:46:25 -04004285 start += len;
4286
Josef Bacik25179202008-10-29 14:49:05 -04004287 spin_lock(&cache->space_info->lock);
4288 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004289 cache->pinned -= len;
4290 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004291 if (cache->ro) {
4292 cache->space_info->bytes_readonly += len;
4293 } else if (cache->reserved_pinned > 0) {
4294 len = min(len, cache->reserved_pinned);
4295 cache->reserved_pinned -= len;
4296 cache->space_info->bytes_reserved += len;
4297 }
Josef Bacik25179202008-10-29 14:49:05 -04004298 spin_unlock(&cache->lock);
4299 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004300 }
4301
4302 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004303 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004304 return 0;
4305}
4306
4307int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004308 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004309{
Yan Zheng11833d62009-09-11 16:11:19 -04004310 struct btrfs_fs_info *fs_info = root->fs_info;
4311 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004312 struct btrfs_block_rsv *block_rsv;
4313 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004314 u64 start;
4315 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004316 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004317 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004318
Yan Zheng11833d62009-09-11 16:11:19 -04004319 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4320 unpin = &fs_info->freed_extents[1];
4321 else
4322 unpin = &fs_info->freed_extents[0];
4323
Chris Masond3977122009-01-05 21:25:51 -05004324 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004325 ret = find_first_extent_bit(unpin, 0, &start, &end,
4326 EXTENT_DIRTY);
4327 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004328 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004329
4330 ret = btrfs_discard_extent(root, start, end + 1 - start);
4331
Chris Mason1a5bc162007-10-15 16:15:26 -04004332 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004333 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004334 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004335 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004336
Yan, Zhengf0486c62010-05-16 10:46:25 -04004337 mutex_lock(&fs_info->durable_block_rsv_mutex);
4338 list_for_each_entry_safe(block_rsv, next_rsv,
4339 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004340
Yan, Zhengf0486c62010-05-16 10:46:25 -04004341 idx = trans->transid & 0x1;
4342 if (block_rsv->freed[idx] > 0) {
4343 block_rsv_add_bytes(block_rsv,
4344 block_rsv->freed[idx], 0);
4345 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004346 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004347 if (atomic_read(&block_rsv->usage) == 0) {
4348 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004349
Yan, Zhengf0486c62010-05-16 10:46:25 -04004350 if (block_rsv->freed[0] == 0 &&
4351 block_rsv->freed[1] == 0) {
4352 list_del_init(&block_rsv->list);
4353 kfree(block_rsv);
4354 }
4355 } else {
4356 btrfs_block_rsv_release(root, block_rsv, 0);
4357 }
4358 }
4359 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4360
Chris Masone20d96d2007-03-22 12:13:20 -04004361 return 0;
4362}
4363
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004364static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4365 struct btrfs_root *root,
4366 u64 bytenr, u64 num_bytes, u64 parent,
4367 u64 root_objectid, u64 owner_objectid,
4368 u64 owner_offset, int refs_to_drop,
4369 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004370{
Chris Masone2fa7222007-03-12 16:22:34 -04004371 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004372 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004373 struct btrfs_fs_info *info = root->fs_info;
4374 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004375 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004376 struct btrfs_extent_item *ei;
4377 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004378 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004379 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004380 int extent_slot = 0;
4381 int found_extent = 0;
4382 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004383 u32 item_size;
4384 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004385
Chris Mason5caf2a02007-04-02 11:20:42 -04004386 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004387 if (!path)
4388 return -ENOMEM;
4389
Chris Mason3c12ac72008-04-21 12:01:38 -04004390 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004391 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004392
4393 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4394 BUG_ON(!is_data && refs_to_drop != 1);
4395
4396 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4397 bytenr, num_bytes, parent,
4398 root_objectid, owner_objectid,
4399 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004400 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004401 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004402 while (extent_slot >= 0) {
4403 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004404 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004405 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004406 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004407 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4408 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004409 found_extent = 1;
4410 break;
4411 }
4412 if (path->slots[0] - extent_slot > 5)
4413 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004414 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004415 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004416#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4417 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4418 if (found_extent && item_size < sizeof(*ei))
4419 found_extent = 0;
4420#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004421 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004422 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004423 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004424 NULL, refs_to_drop,
4425 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004426 BUG_ON(ret);
4427 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04004428 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004429
4430 key.objectid = bytenr;
4431 key.type = BTRFS_EXTENT_ITEM_KEY;
4432 key.offset = num_bytes;
4433
Zheng Yan31840ae2008-09-23 13:14:14 -04004434 ret = btrfs_search_slot(trans, extent_root,
4435 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004436 if (ret) {
4437 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004438 ", was looking for %llu\n", ret,
4439 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004440 btrfs_print_leaf(extent_root, path->nodes[0]);
4441 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004442 BUG_ON(ret);
4443 extent_slot = path->slots[0];
4444 }
Chris Mason7bb86312007-12-11 09:25:06 -05004445 } else {
4446 btrfs_print_leaf(extent_root, path->nodes[0]);
4447 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004448 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004449 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004450 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004451 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004452 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004453 (unsigned long long)owner_objectid,
4454 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004455 }
Chris Mason5f39d392007-10-15 16:14:19 -04004456
4457 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004458 item_size = btrfs_item_size_nr(leaf, extent_slot);
4459#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4460 if (item_size < sizeof(*ei)) {
4461 BUG_ON(found_extent || extent_slot != path->slots[0]);
4462 ret = convert_extent_item_v0(trans, extent_root, path,
4463 owner_objectid, 0);
4464 BUG_ON(ret < 0);
4465
4466 btrfs_release_path(extent_root, path);
4467 path->leave_spinning = 1;
4468
4469 key.objectid = bytenr;
4470 key.type = BTRFS_EXTENT_ITEM_KEY;
4471 key.offset = num_bytes;
4472
4473 ret = btrfs_search_slot(trans, extent_root, &key, path,
4474 -1, 1);
4475 if (ret) {
4476 printk(KERN_ERR "umm, got %d back from search"
4477 ", was looking for %llu\n", ret,
4478 (unsigned long long)bytenr);
4479 btrfs_print_leaf(extent_root, path->nodes[0]);
4480 }
4481 BUG_ON(ret);
4482 extent_slot = path->slots[0];
4483 leaf = path->nodes[0];
4484 item_size = btrfs_item_size_nr(leaf, extent_slot);
4485 }
4486#endif
4487 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004488 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004489 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004490 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4491 struct btrfs_tree_block_info *bi;
4492 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4493 bi = (struct btrfs_tree_block_info *)(ei + 1);
4494 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004495 }
4496
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004497 refs = btrfs_extent_refs(leaf, ei);
4498 BUG_ON(refs < refs_to_drop);
4499 refs -= refs_to_drop;
4500
4501 if (refs > 0) {
4502 if (extent_op)
4503 __run_delayed_extent_op(extent_op, leaf, ei);
4504 /*
4505 * In the case of inline back ref, reference count will
4506 * be updated by remove_extent_backref
4507 */
4508 if (iref) {
4509 BUG_ON(!found_extent);
4510 } else {
4511 btrfs_set_extent_refs(leaf, ei, refs);
4512 btrfs_mark_buffer_dirty(leaf);
4513 }
4514 if (found_extent) {
4515 ret = remove_extent_backref(trans, extent_root, path,
4516 iref, refs_to_drop,
4517 is_data);
4518 BUG_ON(ret);
4519 }
4520 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004521 if (found_extent) {
4522 BUG_ON(is_data && refs_to_drop !=
4523 extent_data_ref_count(root, path, iref));
4524 if (iref) {
4525 BUG_ON(path->slots[0] != extent_slot);
4526 } else {
4527 BUG_ON(path->slots[0] != extent_slot + 1);
4528 path->slots[0] = extent_slot;
4529 num_to_del = 2;
4530 }
Chris Mason78fae272007-03-25 11:35:08 -04004531 }
Chris Masonb9473432009-03-13 11:00:37 -04004532
Chris Mason952fcca2008-02-18 16:33:44 -05004533 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4534 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004535 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04004536 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01004537
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004538 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004539 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4540 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004541 } else {
4542 invalidate_mapping_pages(info->btree_inode->i_mapping,
4543 bytenr >> PAGE_CACHE_SHIFT,
4544 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004545 }
4546
Yan, Zhengf0486c62010-05-16 10:46:25 -04004547 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004548 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004549 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004550 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004551 return ret;
4552}
4553
4554/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004555 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004556 * delayed ref for that extent as well. This searches the delayed ref tree for
4557 * a given extent, and if there are no other delayed refs to be processed, it
4558 * removes it from the tree.
4559 */
4560static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4561 struct btrfs_root *root, u64 bytenr)
4562{
4563 struct btrfs_delayed_ref_head *head;
4564 struct btrfs_delayed_ref_root *delayed_refs;
4565 struct btrfs_delayed_ref_node *ref;
4566 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004567 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004568
4569 delayed_refs = &trans->transaction->delayed_refs;
4570 spin_lock(&delayed_refs->lock);
4571 head = btrfs_find_delayed_ref_head(trans, bytenr);
4572 if (!head)
4573 goto out;
4574
4575 node = rb_prev(&head->node.rb_node);
4576 if (!node)
4577 goto out;
4578
4579 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4580
4581 /* there are still entries for this ref, we can't drop it */
4582 if (ref->bytenr == bytenr)
4583 goto out;
4584
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004585 if (head->extent_op) {
4586 if (!head->must_insert_reserved)
4587 goto out;
4588 kfree(head->extent_op);
4589 head->extent_op = NULL;
4590 }
4591
Chris Mason1887be62009-03-13 10:11:24 -04004592 /*
4593 * waiting for the lock here would deadlock. If someone else has it
4594 * locked they are already in the process of dropping it anyway
4595 */
4596 if (!mutex_trylock(&head->mutex))
4597 goto out;
4598
4599 /*
4600 * at this point we have a head with no other entries. Go
4601 * ahead and process it.
4602 */
4603 head->node.in_tree = 0;
4604 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004605
Chris Mason1887be62009-03-13 10:11:24 -04004606 delayed_refs->num_entries--;
4607
4608 /*
4609 * we don't take a ref on the node because we're removing it from the
4610 * tree, so we just steal the ref the tree was holding.
4611 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004612 delayed_refs->num_heads--;
4613 if (list_empty(&head->cluster))
4614 delayed_refs->num_heads_ready--;
4615
4616 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004617 spin_unlock(&delayed_refs->lock);
4618
Yan, Zhengf0486c62010-05-16 10:46:25 -04004619 BUG_ON(head->extent_op);
4620 if (head->must_insert_reserved)
4621 ret = 1;
4622
4623 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004624 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004625 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004626out:
4627 spin_unlock(&delayed_refs->lock);
4628 return 0;
4629}
4630
Yan, Zhengf0486c62010-05-16 10:46:25 -04004631void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4632 struct btrfs_root *root,
4633 struct extent_buffer *buf,
4634 u64 parent, int last_ref)
4635{
4636 struct btrfs_block_rsv *block_rsv;
4637 struct btrfs_block_group_cache *cache = NULL;
4638 int ret;
4639
4640 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4641 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4642 parent, root->root_key.objectid,
4643 btrfs_header_level(buf),
4644 BTRFS_DROP_DELAYED_REF, NULL);
4645 BUG_ON(ret);
4646 }
4647
4648 if (!last_ref)
4649 return;
4650
4651 block_rsv = get_block_rsv(trans, root);
4652 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004653 if (block_rsv->space_info != cache->space_info)
4654 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004655
4656 if (btrfs_header_generation(buf) == trans->transid) {
4657 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4658 ret = check_ref_cleanup(trans, root, buf->start);
4659 if (!ret)
4660 goto pin;
4661 }
4662
4663 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4664 pin_down_extent(root, cache, buf->start, buf->len, 1);
4665 goto pin;
4666 }
4667
4668 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4669
4670 btrfs_add_free_space(cache, buf->start, buf->len);
4671 ret = update_reserved_bytes(cache, buf->len, 0, 0);
4672 if (ret == -EAGAIN) {
4673 /* block group became read-only */
4674 update_reserved_bytes(cache, buf->len, 0, 1);
4675 goto out;
4676 }
4677
4678 ret = 1;
4679 spin_lock(&block_rsv->lock);
4680 if (block_rsv->reserved < block_rsv->size) {
4681 block_rsv->reserved += buf->len;
4682 ret = 0;
4683 }
4684 spin_unlock(&block_rsv->lock);
4685
4686 if (ret) {
4687 spin_lock(&cache->space_info->lock);
4688 cache->space_info->bytes_reserved -= buf->len;
4689 spin_unlock(&cache->space_info->lock);
4690 }
4691 goto out;
4692 }
4693pin:
4694 if (block_rsv->durable && !cache->ro) {
4695 ret = 0;
4696 spin_lock(&cache->lock);
4697 if (!cache->ro) {
4698 cache->reserved_pinned += buf->len;
4699 ret = 1;
4700 }
4701 spin_unlock(&cache->lock);
4702
4703 if (ret) {
4704 spin_lock(&block_rsv->lock);
4705 block_rsv->freed[trans->transid & 0x1] += buf->len;
4706 spin_unlock(&block_rsv->lock);
4707 }
4708 }
4709out:
4710 btrfs_put_block_group(cache);
4711}
4712
Chris Mason925baed2008-06-25 16:01:30 -04004713int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004714 struct btrfs_root *root,
4715 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004716 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004717{
4718 int ret;
4719
Chris Mason56bec292009-03-13 10:10:06 -04004720 /*
4721 * tree log blocks never actually go into the extent allocation
4722 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004723 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004724 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4725 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004726 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004727 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004728 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004729 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4730 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4731 parent, root_objectid, (int)owner,
4732 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004733 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004734 } else {
4735 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4736 parent, root_objectid, owner,
4737 offset, BTRFS_DROP_DELAYED_REF, NULL);
4738 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004739 }
Chris Mason925baed2008-06-25 16:01:30 -04004740 return ret;
4741}
4742
Chris Mason87ee04e2007-11-30 11:30:34 -05004743static u64 stripe_align(struct btrfs_root *root, u64 val)
4744{
4745 u64 mask = ((u64)root->stripesize - 1);
4746 u64 ret = (val + mask) & ~mask;
4747 return ret;
4748}
4749
Chris Masonfec577f2007-02-26 10:40:21 -05004750/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004751 * when we wait for progress in the block group caching, its because
4752 * our allocation attempt failed at least once. So, we must sleep
4753 * and let some progress happen before we try again.
4754 *
4755 * This function will sleep at least once waiting for new free space to
4756 * show up, and then it will check the block group free space numbers
4757 * for our min num_bytes. Another option is to have it go ahead
4758 * and look in the rbtree for a free extent of a given size, but this
4759 * is a good start.
4760 */
4761static noinline int
4762wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4763 u64 num_bytes)
4764{
Yan Zheng11833d62009-09-11 16:11:19 -04004765 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004766 DEFINE_WAIT(wait);
4767
Yan Zheng11833d62009-09-11 16:11:19 -04004768 caching_ctl = get_caching_control(cache);
4769 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004770 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004771
Yan Zheng11833d62009-09-11 16:11:19 -04004772 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004773 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004774
4775 put_caching_control(caching_ctl);
4776 return 0;
4777}
4778
4779static noinline int
4780wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4781{
4782 struct btrfs_caching_control *caching_ctl;
4783 DEFINE_WAIT(wait);
4784
4785 caching_ctl = get_caching_control(cache);
4786 if (!caching_ctl)
4787 return 0;
4788
4789 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4790
4791 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004792 return 0;
4793}
4794
Yan, Zhengb742bb82010-05-16 10:46:24 -04004795static int get_block_group_index(struct btrfs_block_group_cache *cache)
4796{
4797 int index;
4798 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4799 index = 0;
4800 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4801 index = 1;
4802 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4803 index = 2;
4804 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4805 index = 3;
4806 else
4807 index = 4;
4808 return index;
4809}
4810
Josef Bacik817d52f2009-07-13 21:29:25 -04004811enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004812 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004813 LOOP_CACHING_NOWAIT = 1,
4814 LOOP_CACHING_WAIT = 2,
4815 LOOP_ALLOC_CHUNK = 3,
4816 LOOP_NO_EMPTY_SIZE = 4,
4817};
4818
4819/*
Chris Masonfec577f2007-02-26 10:40:21 -05004820 * walks the btree of allocated extents and find a hole of a given size.
4821 * The key ins is changed to record the hole:
4822 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004823 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004824 * ins->offset == number of blocks
4825 * Any available blocks before search_start are skipped.
4826 */
Chris Masond3977122009-01-05 21:25:51 -05004827static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004828 struct btrfs_root *orig_root,
4829 u64 num_bytes, u64 empty_size,
4830 u64 search_start, u64 search_end,
4831 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004832 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004833{
Josef Bacik80eb2342008-10-29 14:49:05 -04004834 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004835 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004836 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004837 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004838 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004839 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004840 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004841 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004842 int last_ptr_loop = 0;
4843 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004844 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004845 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004846 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004847 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004848 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004849 u64 ideal_cache_percent = 0;
4850 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004851
Chris Masondb945352007-10-15 16:15:53 -04004852 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004853 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004854 ins->objectid = 0;
4855 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004856
Josef Bacik2552d172009-04-03 10:14:19 -04004857 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004858 if (!space_info) {
4859 printk(KERN_ERR "No space info for %d\n", data);
4860 return -ENOSPC;
4861 }
Josef Bacik2552d172009-04-03 10:14:19 -04004862
Josef Bacik67377732010-09-16 16:19:09 -04004863 /*
4864 * If the space info is for both data and metadata it means we have a
4865 * small filesystem and we can't use the clustering stuff.
4866 */
4867 if (btrfs_mixed_space_info(space_info))
4868 use_cluster = false;
4869
Chris Mason0ef3e662008-05-24 14:04:53 -04004870 if (orig_root->ref_cows || empty_size)
4871 allowed_chunk_alloc = 1;
4872
Josef Bacik67377732010-09-16 16:19:09 -04004873 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004874 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004875 if (!btrfs_test_opt(root, SSD))
4876 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004877 }
4878
Josef Bacik67377732010-09-16 16:19:09 -04004879 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4880 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004881 last_ptr = &root->fs_info->data_alloc_cluster;
4882 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004883
Chris Mason239b14b2008-03-24 15:02:07 -04004884 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004885 spin_lock(&last_ptr->lock);
4886 if (last_ptr->block_group)
4887 hint_byte = last_ptr->window_start;
4888 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004889 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004890
Chris Masona061fc82008-05-07 11:43:44 -04004891 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004892 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004893
Josef Bacik817d52f2009-07-13 21:29:25 -04004894 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004895 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004896
Josef Bacik2552d172009-04-03 10:14:19 -04004897 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004898ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004899 block_group = btrfs_lookup_block_group(root->fs_info,
4900 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004901 /*
4902 * we don't want to use the block group if it doesn't match our
4903 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004904 *
4905 * However if we are re-searching with an ideal block group
4906 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004907 */
4908 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004909 (block_group->cached != BTRFS_CACHE_NO ||
4910 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004911 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004912 if (list_empty(&block_group->list) ||
4913 block_group->ro) {
4914 /*
4915 * someone is removing this block group,
4916 * we can't jump into the have_block_group
4917 * target because our list pointers are not
4918 * valid
4919 */
4920 btrfs_put_block_group(block_group);
4921 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004922 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004923 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004924 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004925 }
Josef Bacik2552d172009-04-03 10:14:19 -04004926 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004927 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004928 }
Chris Mason42e70e72008-11-07 18:17:11 -05004929 }
Josef Bacik2552d172009-04-03 10:14:19 -04004930search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004931 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004932 list_for_each_entry(block_group, &space_info->block_groups[index],
4933 list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004934 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004935 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004936
Josef Bacik11dfe352009-11-13 20:12:59 +00004937 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004938 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004939
Josef Bacik2552d172009-04-03 10:14:19 -04004940have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004941 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004942 u64 free_percent;
4943
Josef Bacikb8399de2010-12-08 09:15:11 -05004944 ret = cache_block_group(block_group, trans,
4945 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04004946 if (block_group->cached == BTRFS_CACHE_FINISHED)
4947 goto have_block_group;
4948
Josef Bacikccf0e722009-11-10 21:23:48 -05004949 free_percent = btrfs_block_group_used(&block_group->item);
4950 free_percent *= 100;
4951 free_percent = div64_u64(free_percent,
4952 block_group->key.offset);
4953 free_percent = 100 - free_percent;
4954 if (free_percent > ideal_cache_percent &&
4955 likely(!block_group->ro)) {
4956 ideal_cache_offset = block_group->key.objectid;
4957 ideal_cache_percent = free_percent;
4958 }
4959
Josef Bacik817d52f2009-07-13 21:29:25 -04004960 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05004961 * We only want to start kthread caching if we are at
4962 * the point where we will wait for caching to make
4963 * progress, or if our ideal search is over and we've
4964 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04004965 */
4966 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05004967 (loop > LOOP_FIND_IDEAL &&
4968 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacikb8399de2010-12-08 09:15:11 -05004969 ret = cache_block_group(block_group, trans,
4970 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004971 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004972 }
Josef Bacikccf0e722009-11-10 21:23:48 -05004973 found_uncached_bg = true;
4974
4975 /*
4976 * If loop is set for cached only, try the next block
4977 * group.
4978 */
4979 if (loop == LOOP_FIND_IDEAL)
4980 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05004981 }
4982
Josef Bacik817d52f2009-07-13 21:29:25 -04004983 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05004984 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04004985 found_uncached_bg = true;
4986
Josef Bacikea6a4782008-11-20 12:16:16 -05004987 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004988 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004989
Josef Bacik0a243252009-09-11 16:11:20 -04004990 /*
4991 * Ok we want to try and use the cluster allocator, so lets look
4992 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4993 * have tried the cluster allocator plenty of times at this
4994 * point and not have found anything, so we are likely way too
4995 * fragmented for the clustering stuff to find anything, so lets
4996 * just skip it and let the allocator find whatever block it can
4997 * find
4998 */
4999 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005000 /*
5001 * the refill lock keeps out other
5002 * people trying to start a new cluster
5003 */
5004 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005005 if (last_ptr->block_group &&
5006 (last_ptr->block_group->ro ||
5007 !block_group_bits(last_ptr->block_group, data))) {
5008 offset = 0;
5009 goto refill_cluster;
5010 }
5011
Chris Masonfa9c0d792009-04-03 09:47:43 -04005012 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5013 num_bytes, search_start);
5014 if (offset) {
5015 /* we have a block, we're done */
5016 spin_unlock(&last_ptr->refill_lock);
5017 goto checks;
5018 }
5019
5020 spin_lock(&last_ptr->lock);
5021 /*
5022 * whoops, this cluster doesn't actually point to
5023 * this block group. Get a ref on the block
5024 * group is does point to and try again
5025 */
5026 if (!last_ptr_loop && last_ptr->block_group &&
5027 last_ptr->block_group != block_group) {
5028
5029 btrfs_put_block_group(block_group);
5030 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005031 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005032 spin_unlock(&last_ptr->lock);
5033 spin_unlock(&last_ptr->refill_lock);
5034
5035 last_ptr_loop = 1;
5036 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005037 /*
5038 * we know this block group is properly
5039 * in the list because
5040 * btrfs_remove_block_group, drops the
5041 * cluster before it removes the block
5042 * group from the list
5043 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04005044 goto have_block_group;
5045 }
5046 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005047refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04005048 /*
5049 * this cluster didn't work out, free it and
5050 * start over
5051 */
5052 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5053
5054 last_ptr_loop = 0;
5055
5056 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005057 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005058 block_group, last_ptr,
5059 offset, num_bytes,
5060 empty_cluster + empty_size);
5061 if (ret == 0) {
5062 /*
5063 * now pull our allocation out of this
5064 * cluster
5065 */
5066 offset = btrfs_alloc_from_cluster(block_group,
5067 last_ptr, num_bytes,
5068 search_start);
5069 if (offset) {
5070 /* we found one, proceed */
5071 spin_unlock(&last_ptr->refill_lock);
5072 goto checks;
5073 }
Josef Bacik0a243252009-09-11 16:11:20 -04005074 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5075 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005076 spin_unlock(&last_ptr->refill_lock);
5077
Josef Bacik0a243252009-09-11 16:11:20 -04005078 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005079 wait_block_group_cache_progress(block_group,
5080 num_bytes + empty_cluster + empty_size);
5081 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005082 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005083
Chris Masonfa9c0d792009-04-03 09:47:43 -04005084 /*
5085 * at this point we either didn't find a cluster
5086 * or we weren't able to allocate a block from our
5087 * cluster. Free the cluster we've been trying
5088 * to use, and go to the next block group
5089 */
Josef Bacik0a243252009-09-11 16:11:20 -04005090 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005091 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005092 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005093 }
5094
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005095 offset = btrfs_find_space_for_alloc(block_group, search_start,
5096 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005097 /*
5098 * If we didn't find a chunk, and we haven't failed on this
5099 * block group before, and this block group is in the middle of
5100 * caching and we are ok with waiting, then go ahead and wait
5101 * for progress to be made, and set failed_alloc to true.
5102 *
5103 * If failed_alloc is true then we've already waited on this
5104 * block group once and should move on to the next block group.
5105 */
5106 if (!offset && !failed_alloc && !cached &&
5107 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005108 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005109 num_bytes + empty_size);
5110 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005111 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005112 } else if (!offset) {
5113 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005114 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005115checks:
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005116 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005117 /* move on to the next group */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005118 if (search_start + num_bytes >= search_end) {
5119 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005120 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005121 }
Chris Masone37c9e62007-05-09 20:13:14 -04005122
Josef Bacik2552d172009-04-03 10:14:19 -04005123 /* move on to the next group */
5124 if (search_start + num_bytes >
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005125 block_group->key.objectid + block_group->key.offset) {
5126 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005127 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005128 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005129
Josef Bacik2552d172009-04-03 10:14:19 -04005130 ins->objectid = search_start;
5131 ins->offset = num_bytes;
5132
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005133 if (offset < search_start)
5134 btrfs_add_free_space(block_group, offset,
5135 search_start - offset);
5136 BUG_ON(offset > search_start);
5137
Yan, Zhengf0486c62010-05-16 10:46:25 -04005138 ret = update_reserved_bytes(block_group, num_bytes, 1,
5139 (data & BTRFS_BLOCK_GROUP_DATA));
5140 if (ret == -EAGAIN) {
5141 btrfs_add_free_space(block_group, offset, num_bytes);
5142 goto loop;
5143 }
Yan Zheng11833d62009-09-11 16:11:19 -04005144
Josef Bacik2552d172009-04-03 10:14:19 -04005145 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005146 ins->objectid = search_start;
5147 ins->offset = num_bytes;
5148
5149 if (offset < search_start)
5150 btrfs_add_free_space(block_group, offset,
5151 search_start - offset);
5152 BUG_ON(offset > search_start);
Josef Bacik2552d172009-04-03 10:14:19 -04005153 break;
5154loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005155 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005156 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005157 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d792009-04-03 09:47:43 -04005158 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005159 }
5160 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005161
Yan, Zhengb742bb82010-05-16 10:46:24 -04005162 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5163 goto search;
5164
Josef Bacikccf0e722009-11-10 21:23:48 -05005165 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5166 * for them to make caching progress. Also
5167 * determine the best possible bg to cache
5168 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5169 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005170 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5171 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5172 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5173 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04005174 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005175 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5176 (found_uncached_bg || empty_size || empty_cluster ||
5177 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005178 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005179 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005180 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005181 loop++;
5182 if (!ideal_cache_percent &&
5183 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005184 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005185
5186 /*
5187 * 1 of the following 2 things have happened so far
5188 *
5189 * 1) We found an ideal block group for caching that
5190 * is mostly full and will cache quickly, so we might
5191 * as well wait for it.
5192 *
5193 * 2) We searched for cached only and we didn't find
5194 * anything, and we didn't start any caching kthreads
5195 * either, so chances are we will loop through and
5196 * start a couple caching kthreads, and then come back
5197 * around and just wait for them. This will be slower
5198 * because we will have 2 caching kthreads reading at
5199 * the same time when we could have just started one
5200 * and waited for it to get far enough to give us an
5201 * allocation, so go ahead and go to the wait caching
5202 * loop.
5203 */
5204 loop = LOOP_CACHING_WAIT;
5205 search_start = ideal_cache_offset;
5206 ideal_cache_percent = 0;
5207 goto ideal_cache;
5208 } else if (loop == LOOP_FIND_IDEAL) {
5209 /*
5210 * Didn't find a uncached bg, wait on anything we find
5211 * next.
5212 */
5213 loop = LOOP_CACHING_WAIT;
5214 goto search;
5215 }
5216
5217 if (loop < LOOP_CACHING_WAIT) {
5218 loop++;
5219 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005220 }
5221
5222 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005223 empty_size = 0;
5224 empty_cluster = 0;
5225 }
Chris Mason42e70e72008-11-07 18:17:11 -05005226
Josef Bacik2552d172009-04-03 10:14:19 -04005227 if (allowed_chunk_alloc) {
5228 ret = do_chunk_alloc(trans, root, num_bytes +
5229 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04005230 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005231 done_chunk_alloc = 1;
5232 } else if (!done_chunk_alloc) {
Josef Bacik2552d172009-04-03 10:14:19 -04005233 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005234 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005235
Josef Bacik817d52f2009-07-13 21:29:25 -04005236 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005237 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005238 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005239 }
Josef Bacik2552d172009-04-03 10:14:19 -04005240 ret = -ENOSPC;
5241 } else if (!ins->objectid) {
5242 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04005243 }
Chris Mason0b86a832008-03-24 15:01:56 -04005244
Josef Bacik80eb2342008-10-29 14:49:05 -04005245 /* we found what we needed */
5246 if (ins->objectid) {
5247 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05005248 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04005249
Chris Masonfa9c0d792009-04-03 09:47:43 -04005250 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005251 ret = 0;
5252 }
Chris Mason0b86a832008-03-24 15:01:56 -04005253
Chris Mason0f70abe2007-02-28 16:46:22 -05005254 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005255}
Chris Masonec44a352008-04-28 15:29:52 -04005256
Josef Bacik9ed74f22009-09-11 16:12:44 -04005257static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5258 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005259{
5260 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005261 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005262
Josef Bacik9ed74f22009-09-11 16:12:44 -04005263 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005264 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5265 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005266 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005267 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005268 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005269 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5270 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005271 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005272 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005273 (unsigned long long)info->bytes_pinned,
5274 (unsigned long long)info->bytes_reserved,
5275 (unsigned long long)info->bytes_may_use,
5276 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005277 spin_unlock(&info->lock);
5278
5279 if (!dump_block_groups)
5280 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005281
Josef Bacik80eb2342008-10-29 14:49:05 -04005282 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005283again:
5284 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005285 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005286 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5287 "%llu pinned %llu reserved\n",
5288 (unsigned long long)cache->key.objectid,
5289 (unsigned long long)cache->key.offset,
5290 (unsigned long long)btrfs_block_group_used(&cache->item),
5291 (unsigned long long)cache->pinned,
5292 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005293 btrfs_dump_free_space(cache, bytes);
5294 spin_unlock(&cache->lock);
5295 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005296 if (++index < BTRFS_NR_RAID_TYPES)
5297 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005298 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005299}
Zheng Yane8569812008-09-26 10:05:48 -04005300
Yan Zheng11833d62009-09-11 16:11:19 -04005301int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5302 struct btrfs_root *root,
5303 u64 num_bytes, u64 min_alloc_size,
5304 u64 empty_size, u64 hint_byte,
5305 u64 search_end, struct btrfs_key *ins,
5306 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005307{
5308 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005309 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005310
Josef Bacik6a632092009-02-20 11:00:09 -05005311 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005312again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005313 /*
5314 * the only place that sets empty_size is btrfs_realloc_node, which
5315 * is not called recursively on allocations
5316 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005317 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005318 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005319 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04005320
Chris Masondb945352007-10-15 16:15:53 -04005321 WARN_ON(num_bytes < root->sectorsize);
5322 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005323 search_start, search_end, hint_byte,
5324 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005325
Chris Mason98d20f62008-04-14 09:46:10 -04005326 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5327 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005328 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005329 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005330 do_chunk_alloc(trans, root->fs_info->extent_root,
5331 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005332 goto again;
5333 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005334 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005335 struct btrfs_space_info *sinfo;
5336
5337 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005338 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5339 "wanted %llu\n", (unsigned long long)data,
5340 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005341 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005342 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005343
5344 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005345}
5346
Chris Mason65b51a02008-08-01 15:11:20 -04005347int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5348{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005349 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005350 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005351
Josef Bacik0f9dd462008-09-23 13:14:11 -04005352 cache = btrfs_lookup_block_group(root->fs_info, start);
5353 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005354 printk(KERN_ERR "Unable to find block group for %llu\n",
5355 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005356 return -ENOSPC;
5357 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005358
5359 ret = btrfs_discard_extent(root, start, len);
5360
Josef Bacik0f9dd462008-09-23 13:14:11 -04005361 btrfs_add_free_space(cache, start, len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005362 update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005363 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005364
Chris Masone6dcd2d2008-07-17 12:53:50 -04005365 return ret;
5366}
5367
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005368static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5369 struct btrfs_root *root,
5370 u64 parent, u64 root_objectid,
5371 u64 flags, u64 owner, u64 offset,
5372 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005373{
5374 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005375 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005376 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005377 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005378 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005379 struct extent_buffer *leaf;
5380 int type;
5381 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005382
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005383 if (parent > 0)
5384 type = BTRFS_SHARED_DATA_REF_KEY;
5385 else
5386 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005387
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005388 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005389
5390 path = btrfs_alloc_path();
5391 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05005392
Chris Masonb9473432009-03-13 11:00:37 -04005393 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005394 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5395 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005396 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005397
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005398 leaf = path->nodes[0];
5399 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005400 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005401 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5402 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5403 btrfs_set_extent_flags(leaf, extent_item,
5404 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005405
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005406 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5407 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5408 if (parent > 0) {
5409 struct btrfs_shared_data_ref *ref;
5410 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5411 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5412 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5413 } else {
5414 struct btrfs_extent_data_ref *ref;
5415 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5416 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5417 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5418 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5419 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5420 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005421
5422 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005423 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005424
Yan, Zhengf0486c62010-05-16 10:46:25 -04005425 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005426 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005427 printk(KERN_ERR "btrfs update block group failed for %llu "
5428 "%llu\n", (unsigned long long)ins->objectid,
5429 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005430 BUG();
5431 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005432 return ret;
5433}
5434
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005435static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5436 struct btrfs_root *root,
5437 u64 parent, u64 root_objectid,
5438 u64 flags, struct btrfs_disk_key *key,
5439 int level, struct btrfs_key *ins)
5440{
5441 int ret;
5442 struct btrfs_fs_info *fs_info = root->fs_info;
5443 struct btrfs_extent_item *extent_item;
5444 struct btrfs_tree_block_info *block_info;
5445 struct btrfs_extent_inline_ref *iref;
5446 struct btrfs_path *path;
5447 struct extent_buffer *leaf;
5448 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5449
5450 path = btrfs_alloc_path();
5451 BUG_ON(!path);
5452
5453 path->leave_spinning = 1;
5454 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5455 ins, size);
5456 BUG_ON(ret);
5457
5458 leaf = path->nodes[0];
5459 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5460 struct btrfs_extent_item);
5461 btrfs_set_extent_refs(leaf, extent_item, 1);
5462 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5463 btrfs_set_extent_flags(leaf, extent_item,
5464 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5465 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5466
5467 btrfs_set_tree_block_key(leaf, block_info, key);
5468 btrfs_set_tree_block_level(leaf, block_info, level);
5469
5470 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5471 if (parent > 0) {
5472 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5473 btrfs_set_extent_inline_ref_type(leaf, iref,
5474 BTRFS_SHARED_BLOCK_REF_KEY);
5475 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5476 } else {
5477 btrfs_set_extent_inline_ref_type(leaf, iref,
5478 BTRFS_TREE_BLOCK_REF_KEY);
5479 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5480 }
5481
5482 btrfs_mark_buffer_dirty(leaf);
5483 btrfs_free_path(path);
5484
Yan, Zhengf0486c62010-05-16 10:46:25 -04005485 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005486 if (ret) {
5487 printk(KERN_ERR "btrfs update block group failed for %llu "
5488 "%llu\n", (unsigned long long)ins->objectid,
5489 (unsigned long long)ins->offset);
5490 BUG();
5491 }
5492 return ret;
5493}
5494
5495int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5496 struct btrfs_root *root,
5497 u64 root_objectid, u64 owner,
5498 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005499{
5500 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005501
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005502 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005503
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005504 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5505 0, root_objectid, owner, offset,
5506 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005507 return ret;
5508}
Chris Masone02119d2008-09-05 16:13:11 -04005509
5510/*
5511 * this is used by the tree logging recovery code. It records that
5512 * an extent has been allocated and makes sure to clear the free
5513 * space cache bits as well
5514 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005515int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5516 struct btrfs_root *root,
5517 u64 root_objectid, u64 owner, u64 offset,
5518 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005519{
5520 int ret;
5521 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005522 struct btrfs_caching_control *caching_ctl;
5523 u64 start = ins->objectid;
5524 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005525
Chris Masone02119d2008-09-05 16:13:11 -04005526 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005527 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005528 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005529
Yan Zheng11833d62009-09-11 16:11:19 -04005530 if (!caching_ctl) {
5531 BUG_ON(!block_group_cache_done(block_group));
5532 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5533 BUG_ON(ret);
5534 } else {
5535 mutex_lock(&caching_ctl->mutex);
5536
5537 if (start >= caching_ctl->progress) {
5538 ret = add_excluded_extent(root, start, num_bytes);
5539 BUG_ON(ret);
5540 } else if (start + num_bytes <= caching_ctl->progress) {
5541 ret = btrfs_remove_free_space(block_group,
5542 start, num_bytes);
5543 BUG_ON(ret);
5544 } else {
5545 num_bytes = caching_ctl->progress - start;
5546 ret = btrfs_remove_free_space(block_group,
5547 start, num_bytes);
5548 BUG_ON(ret);
5549
5550 start = caching_ctl->progress;
5551 num_bytes = ins->objectid + ins->offset -
5552 caching_ctl->progress;
5553 ret = add_excluded_extent(root, start, num_bytes);
5554 BUG_ON(ret);
5555 }
5556
5557 mutex_unlock(&caching_ctl->mutex);
5558 put_caching_control(caching_ctl);
5559 }
5560
Yan, Zhengf0486c62010-05-16 10:46:25 -04005561 ret = update_reserved_bytes(block_group, ins->offset, 1, 1);
5562 BUG_ON(ret);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005563 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005564 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5565 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005566 return ret;
5567}
5568
Chris Mason65b51a02008-08-01 15:11:20 -04005569struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5570 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005571 u64 bytenr, u32 blocksize,
5572 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005573{
5574 struct extent_buffer *buf;
5575
5576 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5577 if (!buf)
5578 return ERR_PTR(-ENOMEM);
5579 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005580 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005581 btrfs_tree_lock(buf);
5582 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005583
5584 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005585 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005586
Chris Masond0c803c2008-09-11 16:17:57 -04005587 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005588 /*
5589 * we allow two log transactions at a time, use different
5590 * EXENT bit to differentiate dirty pages.
5591 */
5592 if (root->log_transid % 2 == 0)
5593 set_extent_dirty(&root->dirty_log_pages, buf->start,
5594 buf->start + buf->len - 1, GFP_NOFS);
5595 else
5596 set_extent_new(&root->dirty_log_pages, buf->start,
5597 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005598 } else {
5599 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5600 buf->start + buf->len - 1, GFP_NOFS);
5601 }
Chris Mason65b51a02008-08-01 15:11:20 -04005602 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005603 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005604 return buf;
5605}
5606
Yan, Zhengf0486c62010-05-16 10:46:25 -04005607static struct btrfs_block_rsv *
5608use_block_rsv(struct btrfs_trans_handle *trans,
5609 struct btrfs_root *root, u32 blocksize)
5610{
5611 struct btrfs_block_rsv *block_rsv;
5612 int ret;
5613
5614 block_rsv = get_block_rsv(trans, root);
5615
5616 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005617 ret = reserve_metadata_bytes(trans, root, block_rsv,
5618 blocksize, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005619 if (ret)
5620 return ERR_PTR(ret);
5621 return block_rsv;
5622 }
5623
5624 ret = block_rsv_use_bytes(block_rsv, blocksize);
5625 if (!ret)
5626 return block_rsv;
5627
Yan, Zhengf0486c62010-05-16 10:46:25 -04005628 return ERR_PTR(-ENOSPC);
5629}
5630
5631static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5632{
5633 block_rsv_add_bytes(block_rsv, blocksize, 0);
5634 block_rsv_release_bytes(block_rsv, NULL, 0);
5635}
5636
Chris Masonfec577f2007-02-26 10:40:21 -05005637/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005638 * finds a free extent and does all the dirty work required for allocation
5639 * returns the key for the extent through ins, and a tree buffer for
5640 * the first block of the extent through buf.
5641 *
Chris Masonfec577f2007-02-26 10:40:21 -05005642 * returns the tree buffer or NULL.
5643 */
Chris Mason5f39d392007-10-15 16:14:19 -04005644struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005645 struct btrfs_root *root, u32 blocksize,
5646 u64 parent, u64 root_objectid,
5647 struct btrfs_disk_key *key, int level,
5648 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005649{
Chris Masone2fa7222007-03-12 16:22:34 -04005650 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005651 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005652 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005653 u64 flags = 0;
5654 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005655
Yan, Zhengf0486c62010-05-16 10:46:25 -04005656
5657 block_rsv = use_block_rsv(trans, root, blocksize);
5658 if (IS_ERR(block_rsv))
5659 return ERR_CAST(block_rsv);
5660
5661 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5662 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005663 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005664 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005665 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005666 }
Chris Mason55c69072008-01-09 15:55:33 -05005667
Chris Mason4008c042009-02-12 14:09:45 -05005668 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5669 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005670 BUG_ON(IS_ERR(buf));
5671
5672 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5673 if (parent == 0)
5674 parent = ins.objectid;
5675 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5676 } else
5677 BUG_ON(parent > 0);
5678
5679 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5680 struct btrfs_delayed_extent_op *extent_op;
5681 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5682 BUG_ON(!extent_op);
5683 if (key)
5684 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5685 else
5686 memset(&extent_op->key, 0, sizeof(extent_op->key));
5687 extent_op->flags_to_set = flags;
5688 extent_op->update_key = 1;
5689 extent_op->update_flags = 1;
5690 extent_op->is_data = 0;
5691
5692 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5693 ins.offset, parent, root_objectid,
5694 level, BTRFS_ADD_DELAYED_EXTENT,
5695 extent_op);
5696 BUG_ON(ret);
5697 }
Chris Masonfec577f2007-02-26 10:40:21 -05005698 return buf;
5699}
Chris Masona28ec192007-03-06 20:08:01 -05005700
Yan Zheng2c47e6052009-06-27 21:07:35 -04005701struct walk_control {
5702 u64 refs[BTRFS_MAX_LEVEL];
5703 u64 flags[BTRFS_MAX_LEVEL];
5704 struct btrfs_key update_progress;
5705 int stage;
5706 int level;
5707 int shared_level;
5708 int update_ref;
5709 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005710 int reada_slot;
5711 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005712};
5713
5714#define DROP_REFERENCE 1
5715#define UPDATE_BACKREF 2
5716
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005717static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5718 struct btrfs_root *root,
5719 struct walk_control *wc,
5720 struct btrfs_path *path)
5721{
5722 u64 bytenr;
5723 u64 generation;
5724 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005725 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005726 u32 nritems;
5727 u32 blocksize;
5728 struct btrfs_key key;
5729 struct extent_buffer *eb;
5730 int ret;
5731 int slot;
5732 int nread = 0;
5733
5734 if (path->slots[wc->level] < wc->reada_slot) {
5735 wc->reada_count = wc->reada_count * 2 / 3;
5736 wc->reada_count = max(wc->reada_count, 2);
5737 } else {
5738 wc->reada_count = wc->reada_count * 3 / 2;
5739 wc->reada_count = min_t(int, wc->reada_count,
5740 BTRFS_NODEPTRS_PER_BLOCK(root));
5741 }
5742
5743 eb = path->nodes[wc->level];
5744 nritems = btrfs_header_nritems(eb);
5745 blocksize = btrfs_level_size(root, wc->level - 1);
5746
5747 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5748 if (nread >= wc->reada_count)
5749 break;
5750
5751 cond_resched();
5752 bytenr = btrfs_node_blockptr(eb, slot);
5753 generation = btrfs_node_ptr_generation(eb, slot);
5754
5755 if (slot == path->slots[wc->level])
5756 goto reada;
5757
5758 if (wc->stage == UPDATE_BACKREF &&
5759 generation <= root->root_key.offset)
5760 continue;
5761
Yan, Zheng94fcca92009-10-09 09:25:16 -04005762 /* We don't lock the tree block, it's OK to be racy here */
5763 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5764 &refs, &flags);
5765 BUG_ON(ret);
5766 BUG_ON(refs == 0);
5767
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005768 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005769 if (refs == 1)
5770 goto reada;
5771
Yan, Zheng94fcca92009-10-09 09:25:16 -04005772 if (wc->level == 1 &&
5773 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5774 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005775 if (!wc->update_ref ||
5776 generation <= root->root_key.offset)
5777 continue;
5778 btrfs_node_key_to_cpu(eb, &key, slot);
5779 ret = btrfs_comp_cpu_keys(&key,
5780 &wc->update_progress);
5781 if (ret < 0)
5782 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005783 } else {
5784 if (wc->level == 1 &&
5785 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5786 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005787 }
5788reada:
5789 ret = readahead_tree_block(root, bytenr, blocksize,
5790 generation);
5791 if (ret)
5792 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005793 nread++;
5794 }
5795 wc->reada_slot = slot;
5796}
5797
Chris Mason9aca1d52007-03-13 11:09:37 -04005798/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005799 * hepler to process tree block while walking down the tree.
5800 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005801 * when wc->stage == UPDATE_BACKREF, this function updates
5802 * back refs for pointers in the block.
5803 *
5804 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005805 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005806static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5807 struct btrfs_root *root,
5808 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005809 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005810{
5811 int level = wc->level;
5812 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005813 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5814 int ret;
5815
5816 if (wc->stage == UPDATE_BACKREF &&
5817 btrfs_header_owner(eb) != root->root_key.objectid)
5818 return 1;
5819
5820 /*
5821 * when reference count of tree block is 1, it won't increase
5822 * again. once full backref flag is set, we never clear it.
5823 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005824 if (lookup_info &&
5825 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5826 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005827 BUG_ON(!path->locks[level]);
5828 ret = btrfs_lookup_extent_info(trans, root,
5829 eb->start, eb->len,
5830 &wc->refs[level],
5831 &wc->flags[level]);
5832 BUG_ON(ret);
5833 BUG_ON(wc->refs[level] == 0);
5834 }
5835
Yan Zheng2c47e6052009-06-27 21:07:35 -04005836 if (wc->stage == DROP_REFERENCE) {
5837 if (wc->refs[level] > 1)
5838 return 1;
5839
5840 if (path->locks[level] && !wc->keep_locks) {
5841 btrfs_tree_unlock(eb);
5842 path->locks[level] = 0;
5843 }
5844 return 0;
5845 }
5846
5847 /* wc->stage == UPDATE_BACKREF */
5848 if (!(wc->flags[level] & flag)) {
5849 BUG_ON(!path->locks[level]);
5850 ret = btrfs_inc_ref(trans, root, eb, 1);
5851 BUG_ON(ret);
5852 ret = btrfs_dec_ref(trans, root, eb, 0);
5853 BUG_ON(ret);
5854 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5855 eb->len, flag, 0);
5856 BUG_ON(ret);
5857 wc->flags[level] |= flag;
5858 }
5859
5860 /*
5861 * the block is shared by multiple trees, so it's not good to
5862 * keep the tree lock
5863 */
5864 if (path->locks[level] && level > 0) {
5865 btrfs_tree_unlock(eb);
5866 path->locks[level] = 0;
5867 }
5868 return 0;
5869}
5870
5871/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005872 * hepler to process tree block pointer.
5873 *
5874 * when wc->stage == DROP_REFERENCE, this function checks
5875 * reference count of the block pointed to. if the block
5876 * is shared and we need update back refs for the subtree
5877 * rooted at the block, this function changes wc->stage to
5878 * UPDATE_BACKREF. if the block is shared and there is no
5879 * need to update back, this function drops the reference
5880 * to the block.
5881 *
5882 * NOTE: return value 1 means we should stop walking down.
5883 */
5884static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5885 struct btrfs_root *root,
5886 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005887 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005888{
5889 u64 bytenr;
5890 u64 generation;
5891 u64 parent;
5892 u32 blocksize;
5893 struct btrfs_key key;
5894 struct extent_buffer *next;
5895 int level = wc->level;
5896 int reada = 0;
5897 int ret = 0;
5898
5899 generation = btrfs_node_ptr_generation(path->nodes[level],
5900 path->slots[level]);
5901 /*
5902 * if the lower level block was created before the snapshot
5903 * was created, we know there is no need to update back refs
5904 * for the subtree
5905 */
5906 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005907 generation <= root->root_key.offset) {
5908 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005909 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005910 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005911
5912 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5913 blocksize = btrfs_level_size(root, level - 1);
5914
5915 next = btrfs_find_tree_block(root, bytenr, blocksize);
5916 if (!next) {
5917 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00005918 if (!next)
5919 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005920 reada = 1;
5921 }
5922 btrfs_tree_lock(next);
5923 btrfs_set_lock_blocking(next);
5924
Yan, Zheng94fcca92009-10-09 09:25:16 -04005925 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5926 &wc->refs[level - 1],
5927 &wc->flags[level - 1]);
5928 BUG_ON(ret);
5929 BUG_ON(wc->refs[level - 1] == 0);
5930 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005931
Yan, Zheng94fcca92009-10-09 09:25:16 -04005932 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005933 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005934 if (level == 1 &&
5935 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5936 goto skip;
5937
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005938 if (!wc->update_ref ||
5939 generation <= root->root_key.offset)
5940 goto skip;
5941
5942 btrfs_node_key_to_cpu(path->nodes[level], &key,
5943 path->slots[level]);
5944 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5945 if (ret < 0)
5946 goto skip;
5947
5948 wc->stage = UPDATE_BACKREF;
5949 wc->shared_level = level - 1;
5950 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005951 } else {
5952 if (level == 1 &&
5953 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5954 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005955 }
5956
5957 if (!btrfs_buffer_uptodate(next, generation)) {
5958 btrfs_tree_unlock(next);
5959 free_extent_buffer(next);
5960 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005961 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005962 }
5963
5964 if (!next) {
5965 if (reada && level == 1)
5966 reada_walk_down(trans, root, wc, path);
5967 next = read_tree_block(root, bytenr, blocksize, generation);
5968 btrfs_tree_lock(next);
5969 btrfs_set_lock_blocking(next);
5970 }
5971
5972 level--;
5973 BUG_ON(level != btrfs_header_level(next));
5974 path->nodes[level] = next;
5975 path->slots[level] = 0;
5976 path->locks[level] = 1;
5977 wc->level = level;
5978 if (wc->level == 1)
5979 wc->reada_slot = 0;
5980 return 0;
5981skip:
5982 wc->refs[level - 1] = 0;
5983 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005984 if (wc->stage == DROP_REFERENCE) {
5985 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5986 parent = path->nodes[level]->start;
5987 } else {
5988 BUG_ON(root->root_key.objectid !=
5989 btrfs_header_owner(path->nodes[level]));
5990 parent = 0;
5991 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005992
Yan, Zheng94fcca92009-10-09 09:25:16 -04005993 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5994 root->root_key.objectid, level - 1, 0);
5995 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005996 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005997 btrfs_tree_unlock(next);
5998 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04005999 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006000 return 1;
6001}
6002
6003/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006004 * hepler to process tree block while walking up the tree.
6005 *
6006 * when wc->stage == DROP_REFERENCE, this function drops
6007 * reference count on the block.
6008 *
6009 * when wc->stage == UPDATE_BACKREF, this function changes
6010 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6011 * to UPDATE_BACKREF previously while processing the block.
6012 *
6013 * NOTE: return value 1 means we should stop walking up.
6014 */
6015static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6016 struct btrfs_root *root,
6017 struct btrfs_path *path,
6018 struct walk_control *wc)
6019{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006020 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006021 int level = wc->level;
6022 struct extent_buffer *eb = path->nodes[level];
6023 u64 parent = 0;
6024
6025 if (wc->stage == UPDATE_BACKREF) {
6026 BUG_ON(wc->shared_level < level);
6027 if (level < wc->shared_level)
6028 goto out;
6029
Yan Zheng2c47e6052009-06-27 21:07:35 -04006030 ret = find_next_key(path, level + 1, &wc->update_progress);
6031 if (ret > 0)
6032 wc->update_ref = 0;
6033
6034 wc->stage = DROP_REFERENCE;
6035 wc->shared_level = -1;
6036 path->slots[level] = 0;
6037
6038 /*
6039 * check reference count again if the block isn't locked.
6040 * we should start walking down the tree again if reference
6041 * count is one.
6042 */
6043 if (!path->locks[level]) {
6044 BUG_ON(level == 0);
6045 btrfs_tree_lock(eb);
6046 btrfs_set_lock_blocking(eb);
6047 path->locks[level] = 1;
6048
6049 ret = btrfs_lookup_extent_info(trans, root,
6050 eb->start, eb->len,
6051 &wc->refs[level],
6052 &wc->flags[level]);
6053 BUG_ON(ret);
6054 BUG_ON(wc->refs[level] == 0);
6055 if (wc->refs[level] == 1) {
6056 btrfs_tree_unlock(eb);
6057 path->locks[level] = 0;
6058 return 1;
6059 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006060 }
6061 }
6062
6063 /* wc->stage == DROP_REFERENCE */
6064 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6065
6066 if (wc->refs[level] == 1) {
6067 if (level == 0) {
6068 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6069 ret = btrfs_dec_ref(trans, root, eb, 1);
6070 else
6071 ret = btrfs_dec_ref(trans, root, eb, 0);
6072 BUG_ON(ret);
6073 }
6074 /* make block locked assertion in clean_tree_block happy */
6075 if (!path->locks[level] &&
6076 btrfs_header_generation(eb) == trans->transid) {
6077 btrfs_tree_lock(eb);
6078 btrfs_set_lock_blocking(eb);
6079 path->locks[level] = 1;
6080 }
6081 clean_tree_block(trans, root, eb);
6082 }
6083
6084 if (eb == root->node) {
6085 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6086 parent = eb->start;
6087 else
6088 BUG_ON(root->root_key.objectid !=
6089 btrfs_header_owner(eb));
6090 } else {
6091 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6092 parent = path->nodes[level + 1]->start;
6093 else
6094 BUG_ON(root->root_key.objectid !=
6095 btrfs_header_owner(path->nodes[level + 1]));
6096 }
6097
Yan, Zhengf0486c62010-05-16 10:46:25 -04006098 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006099out:
6100 wc->refs[level] = 0;
6101 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006102 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006103}
6104
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006105static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6106 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006107 struct btrfs_path *path,
6108 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006109{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006110 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006111 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006112 int ret;
6113
Yan Zheng2c47e6052009-06-27 21:07:35 -04006114 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006115 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006116 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006117 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006118
Yan Zheng2c47e6052009-06-27 21:07:35 -04006119 if (level == 0)
6120 break;
6121
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006122 if (path->slots[level] >=
6123 btrfs_header_nritems(path->nodes[level]))
6124 break;
6125
Yan, Zheng94fcca92009-10-09 09:25:16 -04006126 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006127 if (ret > 0) {
6128 path->slots[level]++;
6129 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006130 } else if (ret < 0)
6131 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006132 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006133 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006134 return 0;
6135}
6136
Chris Masond3977122009-01-05 21:25:51 -05006137static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006138 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006139 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006140 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006141{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006142 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006143 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006144
Yan Zheng2c47e6052009-06-27 21:07:35 -04006145 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6146 while (level < max_level && path->nodes[level]) {
6147 wc->level = level;
6148 if (path->slots[level] + 1 <
6149 btrfs_header_nritems(path->nodes[level])) {
6150 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006151 return 0;
6152 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006153 ret = walk_up_proc(trans, root, path, wc);
6154 if (ret > 0)
6155 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006156
Yan Zheng2c47e6052009-06-27 21:07:35 -04006157 if (path->locks[level]) {
6158 btrfs_tree_unlock(path->nodes[level]);
6159 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006160 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006161 free_extent_buffer(path->nodes[level]);
6162 path->nodes[level] = NULL;
6163 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006164 }
6165 }
6166 return 1;
6167}
6168
Chris Mason9aca1d52007-03-13 11:09:37 -04006169/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006170 * drop a subvolume tree.
6171 *
6172 * this function traverses the tree freeing any blocks that only
6173 * referenced by the tree.
6174 *
6175 * when a shared tree block is found. this function decreases its
6176 * reference count by one. if update_ref is true, this function
6177 * also make sure backrefs for the shared block and all lower level
6178 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006179 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006180int btrfs_drop_snapshot(struct btrfs_root *root,
6181 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006182{
Chris Mason5caf2a02007-04-02 11:20:42 -04006183 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006184 struct btrfs_trans_handle *trans;
6185 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006186 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006187 struct walk_control *wc;
6188 struct btrfs_key key;
6189 int err = 0;
6190 int ret;
6191 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006192
Chris Mason5caf2a02007-04-02 11:20:42 -04006193 path = btrfs_alloc_path();
6194 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006195
Yan Zheng2c47e6052009-06-27 21:07:35 -04006196 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6197 BUG_ON(!wc);
6198
Yan, Zhenga22285a2010-05-16 10:48:46 -04006199 trans = btrfs_start_transaction(tree_root, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006200 if (block_rsv)
6201 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006202
Chris Mason9f3a7422007-08-07 15:52:19 -04006203 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006204 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006205 path->nodes[level] = btrfs_lock_root_node(root);
6206 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006207 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006208 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006209 memset(&wc->update_progress, 0,
6210 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006211 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006212 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006213 memcpy(&wc->update_progress, &key,
6214 sizeof(wc->update_progress));
6215
Chris Mason6702ed42007-08-07 16:15:09 -04006216 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006217 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006218 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006219 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6220 path->lowest_level = 0;
6221 if (ret < 0) {
6222 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006223 goto out;
6224 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006225 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006226
Chris Mason7d9eb122008-07-08 14:19:17 -04006227 /*
6228 * unlock our path, this is safe because only this
6229 * function is allowed to delete this snapshot
6230 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006231 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006232
Yan Zheng2c47e6052009-06-27 21:07:35 -04006233 level = btrfs_header_level(root->node);
6234 while (1) {
6235 btrfs_tree_lock(path->nodes[level]);
6236 btrfs_set_lock_blocking(path->nodes[level]);
6237
6238 ret = btrfs_lookup_extent_info(trans, root,
6239 path->nodes[level]->start,
6240 path->nodes[level]->len,
6241 &wc->refs[level],
6242 &wc->flags[level]);
6243 BUG_ON(ret);
6244 BUG_ON(wc->refs[level] == 0);
6245
6246 if (level == root_item->drop_level)
6247 break;
6248
6249 btrfs_tree_unlock(path->nodes[level]);
6250 WARN_ON(wc->refs[level] != 1);
6251 level--;
6252 }
6253 }
6254
6255 wc->level = level;
6256 wc->shared_level = -1;
6257 wc->stage = DROP_REFERENCE;
6258 wc->update_ref = update_ref;
6259 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006260 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006261
6262 while (1) {
6263 ret = walk_down_tree(trans, root, path, wc);
6264 if (ret < 0) {
6265 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006266 break;
6267 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006268
6269 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6270 if (ret < 0) {
6271 err = ret;
6272 break;
6273 }
6274
6275 if (ret > 0) {
6276 BUG_ON(wc->stage != DROP_REFERENCE);
6277 break;
6278 }
6279
6280 if (wc->stage == DROP_REFERENCE) {
6281 level = wc->level;
6282 btrfs_node_key(path->nodes[level],
6283 &root_item->drop_progress,
6284 path->slots[level]);
6285 root_item->drop_level = level;
6286 }
6287
6288 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006289 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006290 ret = btrfs_update_root(trans, tree_root,
6291 &root->root_key,
6292 root_item);
6293 BUG_ON(ret);
6294
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006295 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006296 trans = btrfs_start_transaction(tree_root, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006297 if (block_rsv)
6298 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006299 }
Chris Mason20524f02007-03-10 06:35:47 -05006300 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006301 btrfs_release_path(root, path);
6302 BUG_ON(err);
6303
6304 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6305 BUG_ON(ret);
6306
Yan, Zheng76dda932009-09-21 16:00:26 -04006307 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6308 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6309 NULL, NULL);
6310 BUG_ON(ret < 0);
6311 if (ret > 0) {
6312 ret = btrfs_del_orphan_item(trans, tree_root,
6313 root->root_key.objectid);
6314 BUG_ON(ret);
6315 }
6316 }
6317
6318 if (root->in_radix) {
6319 btrfs_free_fs_root(tree_root->fs_info, root);
6320 } else {
6321 free_extent_buffer(root->node);
6322 free_extent_buffer(root->commit_root);
6323 kfree(root);
6324 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006325out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006326 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006327 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006328 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006329 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006330}
Chris Mason9078a3e2007-04-26 16:46:15 -04006331
Yan Zheng2c47e6052009-06-27 21:07:35 -04006332/*
6333 * drop subtree rooted at tree block 'node'.
6334 *
6335 * NOTE: this function will unlock and release tree block 'node'
6336 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006337int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6338 struct btrfs_root *root,
6339 struct extent_buffer *node,
6340 struct extent_buffer *parent)
6341{
6342 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006343 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006344 int level;
6345 int parent_level;
6346 int ret = 0;
6347 int wret;
6348
Yan Zheng2c47e6052009-06-27 21:07:35 -04006349 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6350
Yan Zhengf82d02d2008-10-29 14:49:05 -04006351 path = btrfs_alloc_path();
6352 BUG_ON(!path);
6353
Yan Zheng2c47e6052009-06-27 21:07:35 -04006354 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6355 BUG_ON(!wc);
6356
Chris Masonb9447ef2009-03-09 11:45:38 -04006357 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006358 parent_level = btrfs_header_level(parent);
6359 extent_buffer_get(parent);
6360 path->nodes[parent_level] = parent;
6361 path->slots[parent_level] = btrfs_header_nritems(parent);
6362
Chris Masonb9447ef2009-03-09 11:45:38 -04006363 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006364 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006365 path->nodes[level] = node;
6366 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006367 path->locks[level] = 1;
6368
6369 wc->refs[parent_level] = 1;
6370 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6371 wc->level = level;
6372 wc->shared_level = -1;
6373 wc->stage = DROP_REFERENCE;
6374 wc->update_ref = 0;
6375 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006376 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006377
6378 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006379 wret = walk_down_tree(trans, root, path, wc);
6380 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006381 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006382 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006383 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006384
Yan Zheng2c47e6052009-06-27 21:07:35 -04006385 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006386 if (wret < 0)
6387 ret = wret;
6388 if (wret != 0)
6389 break;
6390 }
6391
Yan Zheng2c47e6052009-06-27 21:07:35 -04006392 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006393 btrfs_free_path(path);
6394 return ret;
6395}
6396
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006397#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04006398static unsigned long calc_ra(unsigned long start, unsigned long last,
6399 unsigned long nr)
6400{
6401 return min(last, start + nr - 1);
6402}
6403
Chris Masond3977122009-01-05 21:25:51 -05006404static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05006405 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05006406{
6407 u64 page_start;
6408 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04006409 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006410 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006411 unsigned long i;
6412 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05006413 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05006414 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04006415 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04006416 unsigned int total_read = 0;
6417 unsigned int total_dirty = 0;
6418 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05006419
6420 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006421
6422 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006423 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05006424 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6425
Zheng Yan1a40e232008-09-26 10:09:34 -04006426 /* make sure the dirty trick played by the caller work */
6427 ret = invalidate_inode_pages2_range(inode->i_mapping,
6428 first_index, last_index);
6429 if (ret)
6430 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04006431
Chris Mason4313b392008-01-03 09:08:48 -05006432 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05006433
Zheng Yan1a40e232008-09-26 10:09:34 -04006434 for (i = first_index ; i <= last_index; i++) {
6435 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04006436 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04006437 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04006438 }
6439 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04006440again:
6441 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04006442 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05006443 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04006444 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006445 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006446 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04006447 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006448 if (!PageUptodate(page)) {
6449 btrfs_readpage(NULL, page);
6450 lock_page(page);
6451 if (!PageUptodate(page)) {
6452 unlock_page(page);
6453 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006454 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05006455 goto out_unlock;
6456 }
6457 }
Chris Masonec44a352008-04-28 15:29:52 -04006458 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04006459
Chris Masonedbd8d42007-12-21 16:27:24 -05006460 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6461 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006462 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006463
Chris Mason3eaa2882008-07-24 11:57:52 -04006464 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6465 if (ordered) {
6466 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6467 unlock_page(page);
6468 page_cache_release(page);
6469 btrfs_start_ordered_extent(inode, ordered, 1);
6470 btrfs_put_ordered_extent(ordered);
6471 goto again;
6472 }
6473 set_page_extent_mapped(page);
6474
Zheng Yan1a40e232008-09-26 10:09:34 -04006475 if (i == first_index)
6476 set_extent_bits(io_tree, page_start, page_end,
6477 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006478 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04006479
Chris Masona061fc82008-05-07 11:43:44 -04006480 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006481 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006482
Chris Masond1310b22008-01-24 16:13:08 -05006483 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006484 unlock_page(page);
6485 page_cache_release(page);
6486 }
6487
6488out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04006489 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05006490 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006491 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04006492 return ret;
6493}
6494
Chris Masond3977122009-01-05 21:25:51 -05006495static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006496 struct btrfs_key *extent_key,
6497 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05006498{
Zheng Yan1a40e232008-09-26 10:09:34 -04006499 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6500 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6501 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04006502 u64 start = extent_key->objectid - offset;
6503 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006504
6505 em = alloc_extent_map(GFP_NOFS);
6506 BUG_ON(!em || IS_ERR(em));
6507
Yan Zheng66435582008-10-30 14:19:50 -04006508 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006509 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04006510 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04006511 em->block_start = extent_key->objectid;
6512 em->bdev = root->fs_info->fs_devices->latest_bdev;
6513 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6514
6515 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04006516 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006517 while (1) {
6518 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04006519 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006520 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04006521 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006522 if (ret != -EEXIST) {
6523 free_extent_map(em);
6524 break;
6525 }
Yan Zheng66435582008-10-30 14:19:50 -04006526 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006527 }
Yan Zheng66435582008-10-30 14:19:50 -04006528 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006529
Yan Zheng66435582008-10-30 14:19:50 -04006530 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04006531}
6532
6533struct btrfs_ref_path {
6534 u64 extent_start;
6535 u64 nodes[BTRFS_MAX_LEVEL];
6536 u64 root_objectid;
6537 u64 root_generation;
6538 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006539 u32 num_refs;
6540 int lowest_level;
6541 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006542 int shared_level;
6543
6544 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6545 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04006546};
6547
6548struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04006549 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04006550 u64 disk_bytenr;
6551 u64 disk_num_bytes;
6552 u64 offset;
6553 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04006554 u8 compression;
6555 u8 encryption;
6556 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04006557};
6558
6559static int is_cowonly_root(u64 root_objectid)
6560{
6561 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6562 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6563 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6564 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05006565 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6566 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04006567 return 1;
6568 return 0;
6569}
6570
Chris Masond3977122009-01-05 21:25:51 -05006571static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006572 struct btrfs_root *extent_root,
6573 struct btrfs_ref_path *ref_path,
6574 int first_time)
6575{
6576 struct extent_buffer *leaf;
6577 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05006578 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05006579 struct btrfs_key key;
6580 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04006581 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05006582 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04006583 int level;
6584 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006585
Zheng Yan1a40e232008-09-26 10:09:34 -04006586 path = btrfs_alloc_path();
6587 if (!path)
6588 return -ENOMEM;
6589
Zheng Yan1a40e232008-09-26 10:09:34 -04006590 if (first_time) {
6591 ref_path->lowest_level = -1;
6592 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006593 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006594 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04006595 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006596walk_down:
6597 level = ref_path->current_level - 1;
6598 while (level >= -1) {
6599 u64 parent;
6600 if (level < ref_path->lowest_level)
6601 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006602
Chris Masond3977122009-01-05 21:25:51 -05006603 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006604 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006605 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006606 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006607 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006608
Zheng Yan1a40e232008-09-26 10:09:34 -04006609 parent = ref_path->nodes[level + 1];
6610 ref_path->nodes[level + 1] = 0;
6611 ref_path->current_level = level;
6612 BUG_ON(parent == 0);
6613
6614 key.objectid = bytenr;
6615 key.offset = parent + 1;
6616 key.type = BTRFS_EXTENT_REF_KEY;
6617
6618 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006619 if (ret < 0)
6620 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006621 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006622
Chris Masonedbd8d42007-12-21 16:27:24 -05006623 leaf = path->nodes[0];
6624 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04006625 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04006626 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04006627 if (ret < 0)
6628 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006629 if (ret > 0)
6630 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04006631 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04006632 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006633
6634 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04006635 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04006636 found_key.type == BTRFS_EXTENT_REF_KEY) {
6637 if (level < ref_path->shared_level)
6638 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006639 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006640 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006641next:
6642 level--;
6643 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006644 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006645 }
6646 /* reached lowest level */
6647 ret = 1;
6648 goto out;
6649walk_up:
6650 level = ref_path->current_level;
6651 while (level < BTRFS_MAX_LEVEL - 1) {
6652 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006653
6654 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006655 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006656 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006657 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006658
Zheng Yan1a40e232008-09-26 10:09:34 -04006659 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006660
Zheng Yan1a40e232008-09-26 10:09:34 -04006661 key.objectid = bytenr;
6662 key.offset = 0;
6663 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006664
Zheng Yan1a40e232008-09-26 10:09:34 -04006665 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6666 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006667 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006668
6669 leaf = path->nodes[0];
6670 nritems = btrfs_header_nritems(leaf);
6671 if (path->slots[0] >= nritems) {
6672 ret = btrfs_next_leaf(extent_root, path);
6673 if (ret < 0)
6674 goto out;
6675 if (ret > 0) {
6676 /* the extent was freed by someone */
6677 if (ref_path->lowest_level == level)
6678 goto out;
6679 btrfs_release_path(extent_root, path);
6680 goto walk_down;
6681 }
6682 leaf = path->nodes[0];
6683 }
6684
6685 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6686 if (found_key.objectid != bytenr ||
6687 found_key.type != BTRFS_EXTENT_REF_KEY) {
6688 /* the extent was freed by someone */
6689 if (ref_path->lowest_level == level) {
6690 ret = 1;
6691 goto out;
6692 }
6693 btrfs_release_path(extent_root, path);
6694 goto walk_down;
6695 }
6696found:
6697 ref = btrfs_item_ptr(leaf, path->slots[0],
6698 struct btrfs_extent_ref);
6699 ref_objectid = btrfs_ref_objectid(leaf, ref);
6700 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6701 if (first_time) {
6702 level = (int)ref_objectid;
6703 BUG_ON(level >= BTRFS_MAX_LEVEL);
6704 ref_path->lowest_level = level;
6705 ref_path->current_level = level;
6706 ref_path->nodes[level] = bytenr;
6707 } else {
6708 WARN_ON(ref_objectid != level);
6709 }
6710 } else {
6711 WARN_ON(level != -1);
6712 }
6713 first_time = 0;
6714
6715 if (ref_path->lowest_level == level) {
6716 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006717 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6718 }
6719
6720 /*
6721 * the block is tree root or the block isn't in reference
6722 * counted tree.
6723 */
6724 if (found_key.objectid == found_key.offset ||
6725 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6726 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6727 ref_path->root_generation =
6728 btrfs_ref_generation(leaf, ref);
6729 if (level < 0) {
6730 /* special reference from the tree log */
6731 ref_path->nodes[0] = found_key.offset;
6732 ref_path->current_level = 0;
6733 }
6734 ret = 0;
6735 goto out;
6736 }
6737
6738 level++;
6739 BUG_ON(ref_path->nodes[level] != 0);
6740 ref_path->nodes[level] = found_key.offset;
6741 ref_path->current_level = level;
6742
6743 /*
6744 * the reference was created in the running transaction,
6745 * no need to continue walking up.
6746 */
6747 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6748 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6749 ref_path->root_generation =
6750 btrfs_ref_generation(leaf, ref);
6751 ret = 0;
6752 goto out;
6753 }
6754
6755 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006756 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006757 }
6758 /* reached max tree level, but no tree root found. */
6759 BUG();
6760out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006761 btrfs_free_path(path);
6762 return ret;
6763}
6764
6765static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6766 struct btrfs_root *extent_root,
6767 struct btrfs_ref_path *ref_path,
6768 u64 extent_start)
6769{
6770 memset(ref_path, 0, sizeof(*ref_path));
6771 ref_path->extent_start = extent_start;
6772
6773 return __next_ref_path(trans, extent_root, ref_path, 1);
6774}
6775
6776static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6777 struct btrfs_root *extent_root,
6778 struct btrfs_ref_path *ref_path)
6779{
6780 return __next_ref_path(trans, extent_root, ref_path, 0);
6781}
6782
Chris Masond3977122009-01-05 21:25:51 -05006783static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006784 struct btrfs_key *extent_key,
6785 u64 offset, int no_fragment,
6786 struct disk_extent **extents,
6787 int *nr_extents)
6788{
6789 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6790 struct btrfs_path *path;
6791 struct btrfs_file_extent_item *fi;
6792 struct extent_buffer *leaf;
6793 struct disk_extent *exts = *extents;
6794 struct btrfs_key found_key;
6795 u64 cur_pos;
6796 u64 last_byte;
6797 u32 nritems;
6798 int nr = 0;
6799 int max = *nr_extents;
6800 int ret;
6801
6802 WARN_ON(!no_fragment && *extents);
6803 if (!exts) {
6804 max = 1;
6805 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6806 if (!exts)
6807 return -ENOMEM;
6808 }
6809
6810 path = btrfs_alloc_path();
6811 BUG_ON(!path);
6812
6813 cur_pos = extent_key->objectid - offset;
6814 last_byte = extent_key->objectid + extent_key->offset;
6815 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6816 cur_pos, 0);
6817 if (ret < 0)
6818 goto out;
6819 if (ret > 0) {
6820 ret = -ENOENT;
6821 goto out;
6822 }
6823
6824 while (1) {
6825 leaf = path->nodes[0];
6826 nritems = btrfs_header_nritems(leaf);
6827 if (path->slots[0] >= nritems) {
6828 ret = btrfs_next_leaf(root, path);
6829 if (ret < 0)
6830 goto out;
6831 if (ret > 0)
6832 break;
6833 leaf = path->nodes[0];
6834 }
6835
6836 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6837 if (found_key.offset != cur_pos ||
6838 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6839 found_key.objectid != reloc_inode->i_ino)
6840 break;
6841
6842 fi = btrfs_item_ptr(leaf, path->slots[0],
6843 struct btrfs_file_extent_item);
6844 if (btrfs_file_extent_type(leaf, fi) !=
6845 BTRFS_FILE_EXTENT_REG ||
6846 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6847 break;
6848
6849 if (nr == max) {
6850 struct disk_extent *old = exts;
6851 max *= 2;
6852 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6853 memcpy(exts, old, sizeof(*exts) * nr);
6854 if (old != *extents)
6855 kfree(old);
6856 }
6857
6858 exts[nr].disk_bytenr =
6859 btrfs_file_extent_disk_bytenr(leaf, fi);
6860 exts[nr].disk_num_bytes =
6861 btrfs_file_extent_disk_num_bytes(leaf, fi);
6862 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6863 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04006864 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6865 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6866 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6867 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6868 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04006869 BUG_ON(exts[nr].offset > 0);
6870 BUG_ON(exts[nr].compression || exts[nr].encryption);
6871 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006872
6873 cur_pos += exts[nr].num_bytes;
6874 nr++;
6875
6876 if (cur_pos + offset >= last_byte)
6877 break;
6878
6879 if (no_fragment) {
6880 ret = 1;
6881 goto out;
6882 }
6883 path->slots[0]++;
6884 }
6885
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006886 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04006887 if (cur_pos + offset < last_byte) {
6888 ret = -ENOENT;
6889 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05006890 }
6891 ret = 0;
6892out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006893 btrfs_free_path(path);
6894 if (ret) {
6895 if (exts != *extents)
6896 kfree(exts);
6897 } else {
6898 *extents = exts;
6899 *nr_extents = nr;
6900 }
6901 return ret;
6902}
6903
Chris Masond3977122009-01-05 21:25:51 -05006904static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006905 struct btrfs_root *root,
6906 struct btrfs_path *path,
6907 struct btrfs_key *extent_key,
6908 struct btrfs_key *leaf_key,
6909 struct btrfs_ref_path *ref_path,
6910 struct disk_extent *new_extents,
6911 int nr_extents)
6912{
6913 struct extent_buffer *leaf;
6914 struct btrfs_file_extent_item *fi;
6915 struct inode *inode = NULL;
6916 struct btrfs_key key;
6917 u64 lock_start = 0;
6918 u64 lock_end = 0;
6919 u64 num_bytes;
6920 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05006921 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006922 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006923 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006924 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04006925 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04006926 int ret;
6927
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006928 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04006929 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006930 if (key.objectid < ref_path->owner_objectid ||
6931 (key.objectid == ref_path->owner_objectid &&
6932 key.type < BTRFS_EXTENT_DATA_KEY)) {
6933 key.objectid = ref_path->owner_objectid;
6934 key.type = BTRFS_EXTENT_DATA_KEY;
6935 key.offset = 0;
6936 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006937 }
6938
6939 while (1) {
6940 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6941 if (ret < 0)
6942 goto out;
6943
6944 leaf = path->nodes[0];
6945 nritems = btrfs_header_nritems(leaf);
6946next:
6947 if (extent_locked && ret > 0) {
6948 /*
6949 * the file extent item was modified by someone
6950 * before the extent got locked.
6951 */
Zheng Yan1a40e232008-09-26 10:09:34 -04006952 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6953 lock_end, GFP_NOFS);
6954 extent_locked = 0;
6955 }
6956
6957 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006958 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04006959 break;
6960
6961 BUG_ON(extent_locked);
6962 ret = btrfs_next_leaf(root, path);
6963 if (ret < 0)
6964 goto out;
6965 if (ret > 0)
6966 break;
6967 leaf = path->nodes[0];
6968 nritems = btrfs_header_nritems(leaf);
6969 }
6970
6971 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6972
6973 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6974 if ((key.objectid > ref_path->owner_objectid) ||
6975 (key.objectid == ref_path->owner_objectid &&
6976 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05006977 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006978 break;
6979 }
6980
6981 if (inode && key.objectid != inode->i_ino) {
6982 BUG_ON(extent_locked);
6983 btrfs_release_path(root, path);
6984 mutex_unlock(&inode->i_mutex);
6985 iput(inode);
6986 inode = NULL;
6987 continue;
6988 }
6989
6990 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6991 path->slots[0]++;
6992 ret = 1;
6993 goto next;
6994 }
6995 fi = btrfs_item_ptr(leaf, path->slots[0],
6996 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04006997 extent_type = btrfs_file_extent_type(leaf, fi);
6998 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6999 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04007000 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
7001 extent_key->objectid)) {
7002 path->slots[0]++;
7003 ret = 1;
7004 goto next;
7005 }
7006
7007 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7008 ext_offset = btrfs_file_extent_offset(leaf, fi);
7009
Yan Zheng86288a12009-01-21 10:49:16 -05007010 if (search_end == (u64)-1) {
7011 search_end = key.offset - ext_offset +
7012 btrfs_file_extent_ram_bytes(leaf, fi);
7013 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007014
7015 if (!extent_locked) {
7016 lock_start = key.offset;
7017 lock_end = lock_start + num_bytes - 1;
7018 } else {
Yan Zheng66435582008-10-30 14:19:50 -04007019 if (lock_start > key.offset ||
7020 lock_end + 1 < key.offset + num_bytes) {
7021 unlock_extent(&BTRFS_I(inode)->io_tree,
7022 lock_start, lock_end, GFP_NOFS);
7023 extent_locked = 0;
7024 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007025 }
7026
7027 if (!inode) {
7028 btrfs_release_path(root, path);
7029
7030 inode = btrfs_iget_locked(root->fs_info->sb,
7031 key.objectid, root);
7032 if (inode->i_state & I_NEW) {
7033 BTRFS_I(inode)->root = root;
7034 BTRFS_I(inode)->location.objectid =
7035 key.objectid;
7036 BTRFS_I(inode)->location.type =
7037 BTRFS_INODE_ITEM_KEY;
7038 BTRFS_I(inode)->location.offset = 0;
7039 btrfs_read_locked_inode(inode);
7040 unlock_new_inode(inode);
7041 }
7042 /*
7043 * some code call btrfs_commit_transaction while
7044 * holding the i_mutex, so we can't use mutex_lock
7045 * here.
7046 */
7047 if (is_bad_inode(inode) ||
7048 !mutex_trylock(&inode->i_mutex)) {
7049 iput(inode);
7050 inode = NULL;
7051 key.offset = (u64)-1;
7052 goto skip;
7053 }
7054 }
7055
7056 if (!extent_locked) {
7057 struct btrfs_ordered_extent *ordered;
7058
7059 btrfs_release_path(root, path);
7060
7061 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7062 lock_end, GFP_NOFS);
7063 ordered = btrfs_lookup_first_ordered_extent(inode,
7064 lock_end);
7065 if (ordered &&
7066 ordered->file_offset <= lock_end &&
7067 ordered->file_offset + ordered->len > lock_start) {
7068 unlock_extent(&BTRFS_I(inode)->io_tree,
7069 lock_start, lock_end, GFP_NOFS);
7070 btrfs_start_ordered_extent(inode, ordered, 1);
7071 btrfs_put_ordered_extent(ordered);
7072 key.offset += num_bytes;
7073 goto skip;
7074 }
7075 if (ordered)
7076 btrfs_put_ordered_extent(ordered);
7077
Zheng Yan1a40e232008-09-26 10:09:34 -04007078 extent_locked = 1;
7079 continue;
7080 }
7081
7082 if (nr_extents == 1) {
7083 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04007084 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7085 new_extents[0].disk_bytenr);
7086 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7087 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007088 btrfs_mark_buffer_dirty(leaf);
7089
7090 btrfs_drop_extent_cache(inode, key.offset,
7091 key.offset + num_bytes - 1, 0);
7092
7093 ret = btrfs_inc_extent_ref(trans, root,
7094 new_extents[0].disk_bytenr,
7095 new_extents[0].disk_num_bytes,
7096 leaf->start,
7097 root->root_key.objectid,
7098 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007099 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007100 BUG_ON(ret);
7101
7102 ret = btrfs_free_extent(trans, root,
7103 extent_key->objectid,
7104 extent_key->offset,
7105 leaf->start,
7106 btrfs_header_owner(leaf),
7107 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007108 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007109 BUG_ON(ret);
7110
7111 btrfs_release_path(root, path);
7112 key.offset += num_bytes;
7113 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04007114 BUG_ON(1);
7115#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04007116 u64 alloc_hint;
7117 u64 extent_len;
7118 int i;
7119 /*
7120 * drop old extent pointer at first, then insert the
7121 * new pointers one bye one
7122 */
7123 btrfs_release_path(root, path);
7124 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7125 key.offset + num_bytes,
7126 key.offset, &alloc_hint);
7127 BUG_ON(ret);
7128
7129 for (i = 0; i < nr_extents; i++) {
7130 if (ext_offset >= new_extents[i].num_bytes) {
7131 ext_offset -= new_extents[i].num_bytes;
7132 continue;
7133 }
7134 extent_len = min(new_extents[i].num_bytes -
7135 ext_offset, num_bytes);
7136
7137 ret = btrfs_insert_empty_item(trans, root,
7138 path, &key,
7139 sizeof(*fi));
7140 BUG_ON(ret);
7141
7142 leaf = path->nodes[0];
7143 fi = btrfs_item_ptr(leaf, path->slots[0],
7144 struct btrfs_file_extent_item);
7145 btrfs_set_file_extent_generation(leaf, fi,
7146 trans->transid);
7147 btrfs_set_file_extent_type(leaf, fi,
7148 BTRFS_FILE_EXTENT_REG);
7149 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7150 new_extents[i].disk_bytenr);
7151 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7152 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04007153 btrfs_set_file_extent_ram_bytes(leaf, fi,
7154 new_extents[i].ram_bytes);
7155
7156 btrfs_set_file_extent_compression(leaf, fi,
7157 new_extents[i].compression);
7158 btrfs_set_file_extent_encryption(leaf, fi,
7159 new_extents[i].encryption);
7160 btrfs_set_file_extent_other_encoding(leaf, fi,
7161 new_extents[i].other_encoding);
7162
Zheng Yan1a40e232008-09-26 10:09:34 -04007163 btrfs_set_file_extent_num_bytes(leaf, fi,
7164 extent_len);
7165 ext_offset += new_extents[i].offset;
7166 btrfs_set_file_extent_offset(leaf, fi,
7167 ext_offset);
7168 btrfs_mark_buffer_dirty(leaf);
7169
7170 btrfs_drop_extent_cache(inode, key.offset,
7171 key.offset + extent_len - 1, 0);
7172
7173 ret = btrfs_inc_extent_ref(trans, root,
7174 new_extents[i].disk_bytenr,
7175 new_extents[i].disk_num_bytes,
7176 leaf->start,
7177 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007178 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007179 BUG_ON(ret);
7180 btrfs_release_path(root, path);
7181
Yan Zhenga76a3cd2008-10-09 11:46:29 -04007182 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04007183
7184 ext_offset = 0;
7185 num_bytes -= extent_len;
7186 key.offset += extent_len;
7187
7188 if (num_bytes == 0)
7189 break;
7190 }
7191 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007192#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04007193 }
7194
7195 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007196 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7197 lock_end, GFP_NOFS);
7198 extent_locked = 0;
7199 }
7200skip:
7201 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05007202 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007203 break;
7204
7205 cond_resched();
7206 }
7207 ret = 0;
7208out:
7209 btrfs_release_path(root, path);
7210 if (inode) {
7211 mutex_unlock(&inode->i_mutex);
7212 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007213 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7214 lock_end, GFP_NOFS);
7215 }
7216 iput(inode);
7217 }
7218 return ret;
7219}
7220
Zheng Yan1a40e232008-09-26 10:09:34 -04007221int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7222 struct btrfs_root *root,
7223 struct extent_buffer *buf, u64 orig_start)
7224{
7225 int level;
7226 int ret;
7227
7228 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7229 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7230
7231 level = btrfs_header_level(buf);
7232 if (level == 0) {
7233 struct btrfs_leaf_ref *ref;
7234 struct btrfs_leaf_ref *orig_ref;
7235
7236 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7237 if (!orig_ref)
7238 return -ENOENT;
7239
7240 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7241 if (!ref) {
7242 btrfs_free_leaf_ref(root, orig_ref);
7243 return -ENOMEM;
7244 }
7245
7246 ref->nritems = orig_ref->nritems;
7247 memcpy(ref->extents, orig_ref->extents,
7248 sizeof(ref->extents[0]) * ref->nritems);
7249
7250 btrfs_free_leaf_ref(root, orig_ref);
7251
7252 ref->root_gen = trans->transid;
7253 ref->bytenr = buf->start;
7254 ref->owner = btrfs_header_owner(buf);
7255 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05007256
Zheng Yan1a40e232008-09-26 10:09:34 -04007257 ret = btrfs_add_leaf_ref(root, ref, 0);
7258 WARN_ON(ret);
7259 btrfs_free_leaf_ref(root, ref);
7260 }
7261 return 0;
7262}
7263
Chris Masond3977122009-01-05 21:25:51 -05007264static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007265 struct extent_buffer *leaf,
7266 struct btrfs_block_group_cache *group,
7267 struct btrfs_root *target_root)
7268{
7269 struct btrfs_key key;
7270 struct inode *inode = NULL;
7271 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007272 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04007273 u64 num_bytes;
7274 u64 skip_objectid = 0;
7275 u32 nritems;
7276 u32 i;
7277
7278 nritems = btrfs_header_nritems(leaf);
7279 for (i = 0; i < nritems; i++) {
7280 btrfs_item_key_to_cpu(leaf, &key, i);
7281 if (key.objectid == skip_objectid ||
7282 key.type != BTRFS_EXTENT_DATA_KEY)
7283 continue;
7284 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7285 if (btrfs_file_extent_type(leaf, fi) ==
7286 BTRFS_FILE_EXTENT_INLINE)
7287 continue;
7288 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7289 continue;
7290 if (!inode || inode->i_ino != key.objectid) {
7291 iput(inode);
7292 inode = btrfs_ilookup(target_root->fs_info->sb,
7293 key.objectid, target_root, 1);
7294 }
7295 if (!inode) {
7296 skip_objectid = key.objectid;
7297 continue;
7298 }
7299 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7300
Josef Bacik2ac55d42010-02-03 19:33:23 +00007301 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7302 key.offset + num_bytes - 1, 0, &cached_state,
7303 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007304 btrfs_drop_extent_cache(inode, key.offset,
7305 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007306 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7307 key.offset + num_bytes - 1, &cached_state,
7308 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007309 cond_resched();
7310 }
7311 iput(inode);
7312 return 0;
7313}
7314
Chris Masond3977122009-01-05 21:25:51 -05007315static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007316 struct btrfs_root *root,
7317 struct extent_buffer *leaf,
7318 struct btrfs_block_group_cache *group,
7319 struct inode *reloc_inode)
7320{
7321 struct btrfs_key key;
7322 struct btrfs_key extent_key;
7323 struct btrfs_file_extent_item *fi;
7324 struct btrfs_leaf_ref *ref;
7325 struct disk_extent *new_extent;
7326 u64 bytenr;
7327 u64 num_bytes;
7328 u32 nritems;
7329 u32 i;
7330 int ext_index;
7331 int nr_extent;
7332 int ret;
7333
7334 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
7335 BUG_ON(!new_extent);
7336
7337 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7338 BUG_ON(!ref);
7339
7340 ext_index = -1;
7341 nritems = btrfs_header_nritems(leaf);
7342 for (i = 0; i < nritems; i++) {
7343 btrfs_item_key_to_cpu(leaf, &key, i);
7344 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7345 continue;
7346 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7347 if (btrfs_file_extent_type(leaf, fi) ==
7348 BTRFS_FILE_EXTENT_INLINE)
7349 continue;
7350 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7351 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7352 if (bytenr == 0)
7353 continue;
7354
7355 ext_index++;
7356 if (bytenr >= group->key.objectid + group->key.offset ||
7357 bytenr + num_bytes <= group->key.objectid)
7358 continue;
7359
7360 extent_key.objectid = bytenr;
7361 extent_key.offset = num_bytes;
7362 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7363 nr_extent = 1;
7364 ret = get_new_locations(reloc_inode, &extent_key,
7365 group->key.objectid, 1,
7366 &new_extent, &nr_extent);
7367 if (ret > 0)
7368 continue;
7369 BUG_ON(ret < 0);
7370
7371 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7372 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7373 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7374 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7375
Zheng Yan1a40e232008-09-26 10:09:34 -04007376 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7377 new_extent->disk_bytenr);
7378 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7379 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007380 btrfs_mark_buffer_dirty(leaf);
7381
7382 ret = btrfs_inc_extent_ref(trans, root,
7383 new_extent->disk_bytenr,
7384 new_extent->disk_num_bytes,
7385 leaf->start,
7386 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007387 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007388 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04007389
Zheng Yan1a40e232008-09-26 10:09:34 -04007390 ret = btrfs_free_extent(trans, root,
7391 bytenr, num_bytes, leaf->start,
7392 btrfs_header_owner(leaf),
7393 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007394 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007395 BUG_ON(ret);
7396 cond_resched();
7397 }
7398 kfree(new_extent);
7399 BUG_ON(ext_index + 1 != ref->nritems);
7400 btrfs_free_leaf_ref(root, ref);
7401 return 0;
7402}
7403
Yan Zhengf82d02d2008-10-29 14:49:05 -04007404int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7405 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04007406{
7407 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007408 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007409
7410 if (root->reloc_root) {
7411 reloc_root = root->reloc_root;
7412 root->reloc_root = NULL;
7413 list_add(&reloc_root->dead_list,
7414 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007415
7416 btrfs_set_root_bytenr(&reloc_root->root_item,
7417 reloc_root->node->start);
7418 btrfs_set_root_level(&root->root_item,
7419 btrfs_header_level(reloc_root->node));
7420 memset(&reloc_root->root_item.drop_progress, 0,
7421 sizeof(struct btrfs_disk_key));
7422 reloc_root->root_item.drop_level = 0;
7423
7424 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7425 &reloc_root->root_key,
7426 &reloc_root->root_item);
7427 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007428 }
7429 return 0;
7430}
7431
7432int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7433{
7434 struct btrfs_trans_handle *trans;
7435 struct btrfs_root *reloc_root;
7436 struct btrfs_root *prev_root = NULL;
7437 struct list_head dead_roots;
7438 int ret;
7439 unsigned long nr;
7440
7441 INIT_LIST_HEAD(&dead_roots);
7442 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7443
7444 while (!list_empty(&dead_roots)) {
7445 reloc_root = list_entry(dead_roots.prev,
7446 struct btrfs_root, dead_list);
7447 list_del_init(&reloc_root->dead_list);
7448
7449 BUG_ON(reloc_root->commit_root != NULL);
7450 while (1) {
7451 trans = btrfs_join_transaction(root, 1);
7452 BUG_ON(!trans);
7453
7454 mutex_lock(&root->fs_info->drop_mutex);
7455 ret = btrfs_drop_snapshot(trans, reloc_root);
7456 if (ret != -EAGAIN)
7457 break;
7458 mutex_unlock(&root->fs_info->drop_mutex);
7459
7460 nr = trans->blocks_used;
7461 ret = btrfs_end_transaction(trans, root);
7462 BUG_ON(ret);
7463 btrfs_btree_balance_dirty(root, nr);
7464 }
7465
7466 free_extent_buffer(reloc_root->node);
7467
7468 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7469 &reloc_root->root_key);
7470 BUG_ON(ret);
7471 mutex_unlock(&root->fs_info->drop_mutex);
7472
7473 nr = trans->blocks_used;
7474 ret = btrfs_end_transaction(trans, root);
7475 BUG_ON(ret);
7476 btrfs_btree_balance_dirty(root, nr);
7477
7478 kfree(prev_root);
7479 prev_root = reloc_root;
7480 }
7481 if (prev_root) {
7482 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7483 kfree(prev_root);
7484 }
7485 return 0;
7486}
7487
7488int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7489{
7490 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7491 return 0;
7492}
7493
7494int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7495{
7496 struct btrfs_root *reloc_root;
7497 struct btrfs_trans_handle *trans;
7498 struct btrfs_key location;
7499 int found;
7500 int ret;
7501
7502 mutex_lock(&root->fs_info->tree_reloc_mutex);
7503 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7504 BUG_ON(ret);
7505 found = !list_empty(&root->fs_info->dead_reloc_roots);
7506 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7507
7508 if (found) {
7509 trans = btrfs_start_transaction(root, 1);
7510 BUG_ON(!trans);
7511 ret = btrfs_commit_transaction(trans, root);
7512 BUG_ON(ret);
7513 }
7514
7515 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7516 location.offset = (u64)-1;
7517 location.type = BTRFS_ROOT_ITEM_KEY;
7518
7519 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7520 BUG_ON(!reloc_root);
7521 btrfs_orphan_cleanup(reloc_root);
7522 return 0;
7523}
7524
Chris Masond3977122009-01-05 21:25:51 -05007525static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007526 struct btrfs_root *root)
7527{
7528 struct btrfs_root *reloc_root;
7529 struct extent_buffer *eb;
7530 struct btrfs_root_item *root_item;
7531 struct btrfs_key root_key;
7532 int ret;
7533
7534 BUG_ON(!root->ref_cows);
7535 if (root->reloc_root)
7536 return 0;
7537
7538 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7539 BUG_ON(!root_item);
7540
7541 ret = btrfs_copy_root(trans, root, root->commit_root,
7542 &eb, BTRFS_TREE_RELOC_OBJECTID);
7543 BUG_ON(ret);
7544
7545 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7546 root_key.offset = root->root_key.objectid;
7547 root_key.type = BTRFS_ROOT_ITEM_KEY;
7548
7549 memcpy(root_item, &root->root_item, sizeof(root_item));
7550 btrfs_set_root_refs(root_item, 0);
7551 btrfs_set_root_bytenr(root_item, eb->start);
7552 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04007553 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007554
7555 btrfs_tree_unlock(eb);
7556 free_extent_buffer(eb);
7557
7558 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7559 &root_key, root_item);
7560 BUG_ON(ret);
7561 kfree(root_item);
7562
7563 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7564 &root_key);
7565 BUG_ON(!reloc_root);
7566 reloc_root->last_trans = trans->transid;
7567 reloc_root->commit_root = NULL;
7568 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7569
7570 root->reloc_root = reloc_root;
7571 return 0;
7572}
7573
7574/*
7575 * Core function of space balance.
7576 *
7577 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04007578 * counted roots. There is one reloc tree for each subvol, and all
7579 * reloc trees share same root key objectid. Reloc trees are snapshots
7580 * of the latest committed roots of subvols (root->commit_root).
7581 *
7582 * To relocate a tree block referenced by a subvol, there are two steps.
7583 * COW the block through subvol's reloc tree, then update block pointer
7584 * in the subvol to point to the new block. Since all reloc trees share
7585 * same root key objectid, doing special handing for tree blocks owned
7586 * by them is easy. Once a tree block has been COWed in one reloc tree,
7587 * we can use the resulting new block directly when the same block is
7588 * required to COW again through other reloc trees. By this way, relocated
7589 * tree blocks are shared between reloc trees, so they are also shared
7590 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04007591 */
Chris Masond3977122009-01-05 21:25:51 -05007592static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007593 struct btrfs_root *root,
7594 struct btrfs_path *path,
7595 struct btrfs_key *first_key,
7596 struct btrfs_ref_path *ref_path,
7597 struct btrfs_block_group_cache *group,
7598 struct inode *reloc_inode)
7599{
7600 struct btrfs_root *reloc_root;
7601 struct extent_buffer *eb = NULL;
7602 struct btrfs_key *keys;
7603 u64 *nodes;
7604 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007605 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04007606 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007607 int ret;
7608
7609 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7610 lowest_level = ref_path->owner_objectid;
7611
Yan Zhengf82d02d2008-10-29 14:49:05 -04007612 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007613 path->lowest_level = lowest_level;
7614 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7615 BUG_ON(ret < 0);
7616 path->lowest_level = 0;
7617 btrfs_release_path(root, path);
7618 return 0;
7619 }
7620
Zheng Yan1a40e232008-09-26 10:09:34 -04007621 mutex_lock(&root->fs_info->tree_reloc_mutex);
7622 ret = init_reloc_tree(trans, root);
7623 BUG_ON(ret);
7624 reloc_root = root->reloc_root;
7625
Yan Zhengf82d02d2008-10-29 14:49:05 -04007626 shared_level = ref_path->shared_level;
7627 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007628
Yan Zhengf82d02d2008-10-29 14:49:05 -04007629 keys = ref_path->node_keys;
7630 nodes = ref_path->new_nodes;
7631 memset(&keys[shared_level + 1], 0,
7632 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7633 memset(&nodes[shared_level + 1], 0,
7634 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04007635
Yan Zhengf82d02d2008-10-29 14:49:05 -04007636 if (nodes[lowest_level] == 0) {
7637 path->lowest_level = lowest_level;
7638 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7639 0, 1);
7640 BUG_ON(ret);
7641 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7642 eb = path->nodes[level];
7643 if (!eb || eb == reloc_root->node)
7644 break;
7645 nodes[level] = eb->start;
7646 if (level == 0)
7647 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7648 else
7649 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7650 }
Yan Zheng2b820322008-11-17 21:11:30 -05007651 if (nodes[0] &&
7652 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007653 eb = path->nodes[0];
7654 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7655 group, reloc_inode);
7656 BUG_ON(ret);
7657 }
7658 btrfs_release_path(reloc_root, path);
7659 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007660 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007661 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007662 BUG_ON(ret);
7663 }
7664
Zheng Yan1a40e232008-09-26 10:09:34 -04007665 /*
7666 * replace tree blocks in the fs tree with tree blocks in
7667 * the reloc tree.
7668 */
7669 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7670 BUG_ON(ret < 0);
7671
7672 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007673 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7674 0, 0);
7675 BUG_ON(ret);
7676 extent_buffer_get(path->nodes[0]);
7677 eb = path->nodes[0];
7678 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007679 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7680 BUG_ON(ret);
7681 free_extent_buffer(eb);
7682 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007683
Yan Zhengf82d02d2008-10-29 14:49:05 -04007684 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007685 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007686 return 0;
7687}
7688
Chris Masond3977122009-01-05 21:25:51 -05007689static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007690 struct btrfs_root *root,
7691 struct btrfs_path *path,
7692 struct btrfs_key *first_key,
7693 struct btrfs_ref_path *ref_path)
7694{
7695 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007696
7697 ret = relocate_one_path(trans, root, path, first_key,
7698 ref_path, NULL, NULL);
7699 BUG_ON(ret);
7700
Zheng Yan1a40e232008-09-26 10:09:34 -04007701 return 0;
7702}
7703
Chris Masond3977122009-01-05 21:25:51 -05007704static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007705 struct btrfs_root *extent_root,
7706 struct btrfs_path *path,
7707 struct btrfs_key *extent_key)
7708{
7709 int ret;
7710
Zheng Yan1a40e232008-09-26 10:09:34 -04007711 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7712 if (ret)
7713 goto out;
7714 ret = btrfs_del_item(trans, extent_root, path);
7715out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007716 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007717 return ret;
7718}
7719
Chris Masond3977122009-01-05 21:25:51 -05007720static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007721 struct btrfs_ref_path *ref_path)
7722{
7723 struct btrfs_key root_key;
7724
7725 root_key.objectid = ref_path->root_objectid;
7726 root_key.type = BTRFS_ROOT_ITEM_KEY;
7727 if (is_cowonly_root(ref_path->root_objectid))
7728 root_key.offset = 0;
7729 else
7730 root_key.offset = (u64)-1;
7731
7732 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7733}
7734
Chris Masond3977122009-01-05 21:25:51 -05007735static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007736 struct btrfs_path *path,
7737 struct btrfs_key *extent_key,
7738 struct btrfs_block_group_cache *group,
7739 struct inode *reloc_inode, int pass)
7740{
7741 struct btrfs_trans_handle *trans;
7742 struct btrfs_root *found_root;
7743 struct btrfs_ref_path *ref_path = NULL;
7744 struct disk_extent *new_extents = NULL;
7745 int nr_extents = 0;
7746 int loops;
7747 int ret;
7748 int level;
7749 struct btrfs_key first_key;
7750 u64 prev_block = 0;
7751
Zheng Yan1a40e232008-09-26 10:09:34 -04007752
7753 trans = btrfs_start_transaction(extent_root, 1);
7754 BUG_ON(!trans);
7755
7756 if (extent_key->objectid == 0) {
7757 ret = del_extent_zero(trans, extent_root, path, extent_key);
7758 goto out;
7759 }
7760
7761 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7762 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007763 ret = -ENOMEM;
7764 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007765 }
7766
7767 for (loops = 0; ; loops++) {
7768 if (loops == 0) {
7769 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7770 extent_key->objectid);
7771 } else {
7772 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7773 }
7774 if (ret < 0)
7775 goto out;
7776 if (ret > 0)
7777 break;
7778
7779 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7780 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7781 continue;
7782
7783 found_root = read_ref_root(extent_root->fs_info, ref_path);
7784 BUG_ON(!found_root);
7785 /*
7786 * for reference counted tree, only process reference paths
7787 * rooted at the latest committed root.
7788 */
7789 if (found_root->ref_cows &&
7790 ref_path->root_generation != found_root->root_key.offset)
7791 continue;
7792
7793 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7794 if (pass == 0) {
7795 /*
7796 * copy data extents to new locations
7797 */
7798 u64 group_start = group->key.objectid;
7799 ret = relocate_data_extent(reloc_inode,
7800 extent_key,
7801 group_start);
7802 if (ret < 0)
7803 goto out;
7804 break;
7805 }
7806 level = 0;
7807 } else {
7808 level = ref_path->owner_objectid;
7809 }
7810
7811 if (prev_block != ref_path->nodes[level]) {
7812 struct extent_buffer *eb;
7813 u64 block_start = ref_path->nodes[level];
7814 u64 block_size = btrfs_level_size(found_root, level);
7815
7816 eb = read_tree_block(found_root, block_start,
7817 block_size, 0);
7818 btrfs_tree_lock(eb);
7819 BUG_ON(level != btrfs_header_level(eb));
7820
7821 if (level == 0)
7822 btrfs_item_key_to_cpu(eb, &first_key, 0);
7823 else
7824 btrfs_node_key_to_cpu(eb, &first_key, 0);
7825
7826 btrfs_tree_unlock(eb);
7827 free_extent_buffer(eb);
7828 prev_block = block_start;
7829 }
7830
Yan Zheng24562422009-02-12 14:14:53 -05007831 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007832 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05007833 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007834 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7835 /*
7836 * try to update data extent references while
7837 * keeping metadata shared between snapshots.
7838 */
7839 if (pass == 1) {
7840 ret = relocate_one_path(trans, found_root,
7841 path, &first_key, ref_path,
7842 group, reloc_inode);
7843 if (ret < 0)
7844 goto out;
7845 continue;
7846 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007847 /*
7848 * use fallback method to process the remaining
7849 * references.
7850 */
7851 if (!new_extents) {
7852 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04007853 new_extents = kmalloc(sizeof(*new_extents),
7854 GFP_NOFS);
7855 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007856 ret = get_new_locations(reloc_inode,
7857 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04007858 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04007859 &new_extents,
7860 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007861 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04007862 goto out;
7863 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007864 ret = replace_one_extent(trans, found_root,
7865 path, extent_key,
7866 &first_key, ref_path,
7867 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05007868 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007869 ret = relocate_tree_block(trans, found_root, path,
7870 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007871 }
7872 if (ret < 0)
7873 goto out;
7874 }
7875 ret = 0;
7876out:
7877 btrfs_end_transaction(trans, extent_root);
7878 kfree(new_extents);
7879 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007880 return ret;
7881}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007882#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007883
Chris Masonec44a352008-04-28 15:29:52 -04007884static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7885{
7886 u64 num_devices;
7887 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7888 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7889
Yan Zheng2b820322008-11-17 21:11:30 -05007890 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04007891 if (num_devices == 1) {
7892 stripped |= BTRFS_BLOCK_GROUP_DUP;
7893 stripped = flags & ~stripped;
7894
7895 /* turn raid0 into single device chunks */
7896 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7897 return stripped;
7898
7899 /* turn mirroring into duplication */
7900 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7901 BTRFS_BLOCK_GROUP_RAID10))
7902 return stripped | BTRFS_BLOCK_GROUP_DUP;
7903 return flags;
7904 } else {
7905 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007906 if (flags & stripped)
7907 return flags;
7908
7909 stripped |= BTRFS_BLOCK_GROUP_DUP;
7910 stripped = flags & ~stripped;
7911
7912 /* switch duplicated blocks with raid1 */
7913 if (flags & BTRFS_BLOCK_GROUP_DUP)
7914 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7915
7916 /* turn single device chunks into raid0 */
7917 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7918 }
7919 return flags;
7920}
7921
Yan, Zhengf0486c62010-05-16 10:46:25 -04007922static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04007923{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007924 struct btrfs_space_info *sinfo = cache->space_info;
7925 u64 num_bytes;
7926 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007927
Yan, Zhengf0486c62010-05-16 10:46:25 -04007928 if (cache->ro)
7929 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04007930
Yan, Zhengf0486c62010-05-16 10:46:25 -04007931 spin_lock(&sinfo->lock);
7932 spin_lock(&cache->lock);
7933 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7934 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007935
Yan, Zhengf0486c62010-05-16 10:46:25 -04007936 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
7937 sinfo->bytes_may_use + sinfo->bytes_readonly +
7938 cache->reserved_pinned + num_bytes < sinfo->total_bytes) {
7939 sinfo->bytes_readonly += num_bytes;
7940 sinfo->bytes_reserved += cache->reserved_pinned;
7941 cache->reserved_pinned = 0;
7942 cache->ro = 1;
7943 ret = 0;
7944 }
7945 spin_unlock(&cache->lock);
7946 spin_unlock(&sinfo->lock);
7947 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007948}
7949
Yan, Zhengf0486c62010-05-16 10:46:25 -04007950int btrfs_set_block_group_ro(struct btrfs_root *root,
7951 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007952
7953{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007954 struct btrfs_trans_handle *trans;
7955 u64 alloc_flags;
7956 int ret;
7957
7958 BUG_ON(cache->ro);
7959
7960 trans = btrfs_join_transaction(root, 1);
7961 BUG_ON(IS_ERR(trans));
7962
7963 alloc_flags = update_block_group_flags(root, cache->flags);
7964 if (alloc_flags != cache->flags)
7965 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7966
7967 ret = set_block_group_ro(cache);
7968 if (!ret)
7969 goto out;
7970 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
7971 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7972 if (ret < 0)
7973 goto out;
7974 ret = set_block_group_ro(cache);
7975out:
7976 btrfs_end_transaction(trans, root);
7977 return ret;
7978}
7979
7980int btrfs_set_block_group_rw(struct btrfs_root *root,
7981 struct btrfs_block_group_cache *cache)
7982{
7983 struct btrfs_space_info *sinfo = cache->space_info;
7984 u64 num_bytes;
7985
7986 BUG_ON(!cache->ro);
7987
7988 spin_lock(&sinfo->lock);
7989 spin_lock(&cache->lock);
7990 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7991 cache->bytes_super - btrfs_block_group_used(&cache->item);
7992 sinfo->bytes_readonly -= num_bytes;
7993 cache->ro = 0;
7994 spin_unlock(&cache->lock);
7995 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007996 return 0;
7997}
7998
Josef Bacikba1bf482009-09-11 16:11:19 -04007999/*
8000 * checks to see if its even possible to relocate this block group.
8001 *
8002 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8003 * ok to go ahead and try.
8004 */
8005int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008006{
Zheng Yan1a40e232008-09-26 10:09:34 -04008007 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008008 struct btrfs_space_info *space_info;
8009 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8010 struct btrfs_device *device;
8011 int full = 0;
8012 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008013
Josef Bacikba1bf482009-09-11 16:11:19 -04008014 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008015
Josef Bacikba1bf482009-09-11 16:11:19 -04008016 /* odd, couldn't find the block group, leave it alone */
8017 if (!block_group)
8018 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008019
Josef Bacikba1bf482009-09-11 16:11:19 -04008020 /* no bytes used, we're good */
8021 if (!btrfs_block_group_used(&block_group->item))
8022 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008023
Josef Bacikba1bf482009-09-11 16:11:19 -04008024 space_info = block_group->space_info;
8025 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008026
Josef Bacikba1bf482009-09-11 16:11:19 -04008027 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008028
Josef Bacikba1bf482009-09-11 16:11:19 -04008029 /*
8030 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008031 * relocate it unless we're able to allocate a new chunk below.
8032 *
8033 * Otherwise, we need to make sure we have room in the space to handle
8034 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008035 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008036 if ((space_info->total_bytes != block_group->key.offset) &&
8037 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04008038 space_info->bytes_pinned + space_info->bytes_readonly +
8039 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04008040 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008041 spin_unlock(&space_info->lock);
8042 goto out;
8043 }
8044 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008045
Josef Bacikba1bf482009-09-11 16:11:19 -04008046 /*
8047 * ok we don't have enough space, but maybe we have free space on our
8048 * devices to allocate new chunks for relocation, so loop through our
8049 * alloc devices and guess if we have enough space. However, if we
8050 * were marked as full, then we know there aren't enough chunks, and we
8051 * can just return.
8052 */
8053 ret = -1;
8054 if (full)
8055 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05008056
Josef Bacikba1bf482009-09-11 16:11:19 -04008057 mutex_lock(&root->fs_info->chunk_mutex);
8058 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8059 u64 min_free = btrfs_block_group_used(&block_group->item);
8060 u64 dev_offset, max_avail;
Chris Masonea8c2812008-08-04 23:17:27 -04008061
Josef Bacikba1bf482009-09-11 16:11:19 -04008062 /*
8063 * check to make sure we can actually find a chunk with enough
8064 * space to fit our block group in.
8065 */
8066 if (device->total_bytes > device->bytes_used + min_free) {
8067 ret = find_free_dev_extent(NULL, device, min_free,
8068 &dev_offset, &max_avail);
8069 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05008070 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04008071 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008072 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008073 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008074 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05008075out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008076 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008077 return ret;
8078}
8079
Christoph Hellwigb2950862008-12-02 09:54:17 -05008080static int find_first_block_group(struct btrfs_root *root,
8081 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008082{
Chris Mason925baed2008-06-25 16:01:30 -04008083 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008084 struct btrfs_key found_key;
8085 struct extent_buffer *leaf;
8086 int slot;
8087
8088 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8089 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008090 goto out;
8091
Chris Masond3977122009-01-05 21:25:51 -05008092 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008093 slot = path->slots[0];
8094 leaf = path->nodes[0];
8095 if (slot >= btrfs_header_nritems(leaf)) {
8096 ret = btrfs_next_leaf(root, path);
8097 if (ret == 0)
8098 continue;
8099 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008100 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008101 break;
8102 }
8103 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8104
8105 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008106 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8107 ret = 0;
8108 goto out;
8109 }
Chris Mason0b86a832008-03-24 15:01:56 -04008110 path->slots[0]++;
8111 }
Chris Mason925baed2008-06-25 16:01:30 -04008112out:
Chris Mason0b86a832008-03-24 15:01:56 -04008113 return ret;
8114}
8115
Josef Bacik0af3d002010-06-21 14:48:16 -04008116void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8117{
8118 struct btrfs_block_group_cache *block_group;
8119 u64 last = 0;
8120
8121 while (1) {
8122 struct inode *inode;
8123
8124 block_group = btrfs_lookup_first_block_group(info, last);
8125 while (block_group) {
8126 spin_lock(&block_group->lock);
8127 if (block_group->iref)
8128 break;
8129 spin_unlock(&block_group->lock);
8130 block_group = next_block_group(info->tree_root,
8131 block_group);
8132 }
8133 if (!block_group) {
8134 if (last == 0)
8135 break;
8136 last = 0;
8137 continue;
8138 }
8139
8140 inode = block_group->inode;
8141 block_group->iref = 0;
8142 block_group->inode = NULL;
8143 spin_unlock(&block_group->lock);
8144 iput(inode);
8145 last = block_group->key.objectid + block_group->key.offset;
8146 btrfs_put_block_group(block_group);
8147 }
8148}
8149
Zheng Yan1a40e232008-09-26 10:09:34 -04008150int btrfs_free_block_groups(struct btrfs_fs_info *info)
8151{
8152 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008153 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008154 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008155 struct rb_node *n;
8156
Yan Zheng11833d62009-09-11 16:11:19 -04008157 down_write(&info->extent_commit_sem);
8158 while (!list_empty(&info->caching_block_groups)) {
8159 caching_ctl = list_entry(info->caching_block_groups.next,
8160 struct btrfs_caching_control, list);
8161 list_del(&caching_ctl->list);
8162 put_caching_control(caching_ctl);
8163 }
8164 up_write(&info->extent_commit_sem);
8165
Zheng Yan1a40e232008-09-26 10:09:34 -04008166 spin_lock(&info->block_group_cache_lock);
8167 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8168 block_group = rb_entry(n, struct btrfs_block_group_cache,
8169 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008170 rb_erase(&block_group->cache_node,
8171 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008172 spin_unlock(&info->block_group_cache_lock);
8173
Josef Bacik80eb2342008-10-29 14:49:05 -04008174 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008175 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008176 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008177
Josef Bacik817d52f2009-07-13 21:29:25 -04008178 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008179 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008180
8181 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008182 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008183
8184 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008185 }
8186 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008187
8188 /* now that all the block groups are freed, go through and
8189 * free all the space_info structs. This is only called during
8190 * the final stages of unmount, and so we know nobody is
8191 * using them. We call synchronize_rcu() once before we start,
8192 * just to be on the safe side.
8193 */
8194 synchronize_rcu();
8195
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008196 release_global_block_rsv(info);
8197
Chris Mason4184ea72009-03-10 12:39:20 -04008198 while(!list_empty(&info->space_info)) {
8199 space_info = list_entry(info->space_info.next,
8200 struct btrfs_space_info,
8201 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008202 if (space_info->bytes_pinned > 0 ||
8203 space_info->bytes_reserved > 0) {
8204 WARN_ON(1);
8205 dump_space_info(space_info, 0, 0);
8206 }
Chris Mason4184ea72009-03-10 12:39:20 -04008207 list_del(&space_info->list);
8208 kfree(space_info);
8209 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008210 return 0;
8211}
8212
Yan, Zhengb742bb82010-05-16 10:46:24 -04008213static void __link_block_group(struct btrfs_space_info *space_info,
8214 struct btrfs_block_group_cache *cache)
8215{
8216 int index = get_block_group_index(cache);
8217
8218 down_write(&space_info->groups_sem);
8219 list_add_tail(&cache->list, &space_info->block_groups[index]);
8220 up_write(&space_info->groups_sem);
8221}
8222
Chris Mason9078a3e2007-04-26 16:46:15 -04008223int btrfs_read_block_groups(struct btrfs_root *root)
8224{
8225 struct btrfs_path *path;
8226 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008227 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008228 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008229 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008230 struct btrfs_key key;
8231 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008232 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008233 int need_clear = 0;
8234 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008235
Chris Masonbe744172007-05-06 10:15:01 -04008236 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008237 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008238 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008239 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008240 path = btrfs_alloc_path();
8241 if (!path)
8242 return -ENOMEM;
8243
Josef Bacik0af3d002010-06-21 14:48:16 -04008244 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8245 if (cache_gen != 0 &&
8246 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8247 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008248 if (btrfs_test_opt(root, CLEAR_CACHE))
8249 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04008250 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
8251 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04008252
Chris Masond3977122009-01-05 21:25:51 -05008253 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008254 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008255 if (ret > 0)
8256 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008257 if (ret != 0)
8258 goto error;
8259
Chris Mason5f39d392007-10-15 16:14:19 -04008260 leaf = path->nodes[0];
8261 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008262 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008263 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008264 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008265 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008266 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008267
Yan Zhengd2fb3432008-12-11 16:30:39 -05008268 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008269 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04008270 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008271 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008272 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008273 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008274
Josef Bacik0af3d002010-06-21 14:48:16 -04008275 if (need_clear)
8276 cache->disk_cache_state = BTRFS_DC_CLEAR;
8277
Josef Bacik96303082009-07-13 21:29:25 -04008278 /*
8279 * we only want to have 32k of ram per block group for keeping
8280 * track of free space, and if we pass 1/2 of that we want to
8281 * start converting things over to using bitmaps
8282 */
8283 cache->extents_thresh = ((1024 * 32) / 2) /
8284 sizeof(struct btrfs_free_space);
8285
Chris Mason5f39d392007-10-15 16:14:19 -04008286 read_extent_buffer(leaf, &cache->item,
8287 btrfs_item_ptr_offset(leaf, path->slots[0]),
8288 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008289 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008290
Chris Mason9078a3e2007-04-26 16:46:15 -04008291 key.objectid = found_key.objectid + found_key.offset;
8292 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04008293 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008294 cache->sectorsize = root->sectorsize;
8295
Josef Bacik817d52f2009-07-13 21:29:25 -04008296 /*
8297 * check for two cases, either we are full, and therefore
8298 * don't need to bother with the caching work since we won't
8299 * find any space, or we are empty, and we can just add all
8300 * the space in and be done with it. This saves us _alot_ of
8301 * time, particularly in the full case.
8302 */
8303 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04008304 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04008305 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008306 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008307 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008308 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008309 exclude_super_stripes(root, cache);
8310 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008311 cache->cached = BTRFS_CACHE_FINISHED;
8312 add_new_free_space(cache, root->fs_info,
8313 found_key.objectid,
8314 found_key.objectid +
8315 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008316 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008317 }
Chris Mason96b51792007-10-15 16:15:19 -04008318
Chris Mason6324fbf2008-03-24 15:01:59 -04008319 ret = update_space_info(info, cache->flags, found_key.offset,
8320 btrfs_block_group_used(&cache->item),
8321 &space_info);
8322 BUG_ON(ret);
8323 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008324 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008325 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008326 spin_unlock(&cache->space_info->lock);
8327
Yan, Zhengb742bb82010-05-16 10:46:24 -04008328 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008329
Josef Bacik0f9dd462008-09-23 13:14:11 -04008330 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8331 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04008332
8333 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008334 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04008335 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04008336 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008337
8338 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8339 if (!(get_alloc_profile(root, space_info->flags) &
8340 (BTRFS_BLOCK_GROUP_RAID10 |
8341 BTRFS_BLOCK_GROUP_RAID1 |
8342 BTRFS_BLOCK_GROUP_DUP)))
8343 continue;
8344 /*
8345 * avoid allocating from un-mirrored block group if there are
8346 * mirrored block groups.
8347 */
8348 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008349 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008350 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008351 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008352 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008353
8354 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008355 ret = 0;
8356error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008357 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008358 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008359}
Chris Mason6324fbf2008-03-24 15:01:59 -04008360
8361int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8362 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008363 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008364 u64 size)
8365{
8366 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008367 struct btrfs_root *extent_root;
8368 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008369
8370 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008371
Chris Mason12fcfd22009-03-24 10:24:20 -04008372 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008373
Chris Mason8f18cf12008-04-25 16:53:30 -04008374 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008375 if (!cache)
8376 return -ENOMEM;
8377
Chris Masone17cade2008-04-15 15:41:47 -04008378 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008379 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008380 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008381 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008382 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04008383
8384 /*
8385 * we only want to have 32k of ram per block group for keeping track
8386 * of free space, and if we pass 1/2 of that we want to start
8387 * converting things over to using bitmaps
8388 */
8389 cache->extents_thresh = ((1024 * 32) / 2) /
8390 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008391 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008392 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04008393 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008394 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008395 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008396
Chris Mason6324fbf2008-03-24 15:01:59 -04008397 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008398 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8399 cache->flags = type;
8400 btrfs_set_block_group_flags(&cache->item, type);
8401
Yan Zheng11833d62009-09-11 16:11:19 -04008402 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008403 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04008404 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04008405
Josef Bacik817d52f2009-07-13 21:29:25 -04008406 add_new_free_space(cache, root->fs_info, chunk_offset,
8407 chunk_offset + size);
8408
Yan Zheng11833d62009-09-11 16:11:19 -04008409 free_excluded_extents(root, cache);
8410
Chris Mason6324fbf2008-03-24 15:01:59 -04008411 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8412 &cache->space_info);
8413 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04008414
8415 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008416 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008417 spin_unlock(&cache->space_info->lock);
8418
Yan, Zhengb742bb82010-05-16 10:46:24 -04008419 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008420
Josef Bacik0f9dd462008-09-23 13:14:11 -04008421 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8422 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04008423
Chris Mason6324fbf2008-03-24 15:01:59 -04008424 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8425 sizeof(cache->item));
8426 BUG_ON(ret);
8427
Chris Masond18a2c42008-04-04 15:40:00 -04008428 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008429
Chris Mason6324fbf2008-03-24 15:01:59 -04008430 return 0;
8431}
Zheng Yan1a40e232008-09-26 10:09:34 -04008432
8433int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8434 struct btrfs_root *root, u64 group_start)
8435{
8436 struct btrfs_path *path;
8437 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008438 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008439 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008440 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008441 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008442 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04008443 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008444
Zheng Yan1a40e232008-09-26 10:09:34 -04008445 root = root->fs_info->extent_root;
8446
8447 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8448 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008449 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008450
8451 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04008452 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8453 BTRFS_BLOCK_GROUP_RAID1 |
8454 BTRFS_BLOCK_GROUP_RAID10))
8455 factor = 2;
8456 else
8457 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008458
Chris Mason44fb5512009-06-04 15:34:51 -04008459 /* make sure this block group isn't part of an allocation cluster */
8460 cluster = &root->fs_info->data_alloc_cluster;
8461 spin_lock(&cluster->refill_lock);
8462 btrfs_return_cluster_to_free_space(block_group, cluster);
8463 spin_unlock(&cluster->refill_lock);
8464
8465 /*
8466 * make sure this block group isn't part of a metadata
8467 * allocation cluster
8468 */
8469 cluster = &root->fs_info->meta_alloc_cluster;
8470 spin_lock(&cluster->refill_lock);
8471 btrfs_return_cluster_to_free_space(block_group, cluster);
8472 spin_unlock(&cluster->refill_lock);
8473
Zheng Yan1a40e232008-09-26 10:09:34 -04008474 path = btrfs_alloc_path();
8475 BUG_ON(!path);
8476
Josef Bacik0af3d002010-06-21 14:48:16 -04008477 inode = lookup_free_space_inode(root, block_group, path);
8478 if (!IS_ERR(inode)) {
8479 btrfs_orphan_add(trans, inode);
8480 clear_nlink(inode);
8481 /* One for the block groups ref */
8482 spin_lock(&block_group->lock);
8483 if (block_group->iref) {
8484 block_group->iref = 0;
8485 block_group->inode = NULL;
8486 spin_unlock(&block_group->lock);
8487 iput(inode);
8488 } else {
8489 spin_unlock(&block_group->lock);
8490 }
8491 /* One for our lookup ref */
8492 iput(inode);
8493 }
8494
8495 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8496 key.offset = block_group->key.objectid;
8497 key.type = 0;
8498
8499 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8500 if (ret < 0)
8501 goto out;
8502 if (ret > 0)
8503 btrfs_release_path(tree_root, path);
8504 if (ret == 0) {
8505 ret = btrfs_del_item(trans, tree_root, path);
8506 if (ret)
8507 goto out;
8508 btrfs_release_path(tree_root, path);
8509 }
8510
Yan Zheng3dfdb932009-01-21 10:49:16 -05008511 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008512 rb_erase(&block_group->cache_node,
8513 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008514 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008515
Josef Bacik80eb2342008-10-29 14:49:05 -04008516 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008517 /*
8518 * we must use list_del_init so people can check to see if they
8519 * are still on the list after taking the semaphore
8520 */
8521 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008522 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008523
Josef Bacik817d52f2009-07-13 21:29:25 -04008524 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008525 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008526
8527 btrfs_remove_free_space_cache(block_group);
8528
Yan Zhengc146afa2008-11-12 14:34:12 -05008529 spin_lock(&block_group->space_info->lock);
8530 block_group->space_info->total_bytes -= block_group->key.offset;
8531 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008532 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008533 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008534
Josef Bacik0af3d002010-06-21 14:48:16 -04008535 memcpy(&key, &block_group->key, sizeof(key));
8536
Chris Mason283bb192009-07-24 16:30:55 -04008537 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008538
Chris Masonfa9c0d792009-04-03 09:47:43 -04008539 btrfs_put_block_group(block_group);
8540 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008541
8542 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8543 if (ret > 0)
8544 ret = -EIO;
8545 if (ret < 0)
8546 goto out;
8547
8548 ret = btrfs_del_item(trans, root, path);
8549out:
8550 btrfs_free_path(path);
8551 return ret;
8552}