blob: c8c318494deea99a5b2a46a1c0db648f94079095 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050027#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040031#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040033#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040034#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Chris Mason0e4f8f82011-04-15 16:05:44 -040036/* control flags for do_chunk_alloc's force field
37 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
38 * if we really need one.
39 *
40 * CHUNK_ALLOC_FORCE means it must try to allocate one
41 *
42 * CHUNK_ALLOC_LIMITED means to only try and allocate one
43 * if we have very few chunks already allocated. This is
44 * used as part of the clustering code to help make sure
45 * we have a good pool of storage to cluster in, without
46 * filling the FS with empty chunks
47 *
48 */
49enum {
50 CHUNK_ALLOC_NO_FORCE = 0,
51 CHUNK_ALLOC_FORCE = 1,
52 CHUNK_ALLOC_LIMITED = 2,
53};
54
Josef Bacikf3465ca2008-11-12 14:19:50 -050055static int update_block_group(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040057 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040058static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
59 struct btrfs_root *root,
60 u64 bytenr, u64 num_bytes, u64 parent,
61 u64 root_objectid, u64 owner_objectid,
62 u64 owner_offset, int refs_to_drop,
63 struct btrfs_delayed_extent_op *extra_op);
64static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
65 struct extent_buffer *leaf,
66 struct btrfs_extent_item *ei);
67static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
68 struct btrfs_root *root,
69 u64 parent, u64 root_objectid,
70 u64 flags, u64 owner, u64 offset,
71 struct btrfs_key *ins, int ref_mod);
72static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
73 struct btrfs_root *root,
74 u64 parent, u64 root_objectid,
75 u64 flags, struct btrfs_disk_key *key,
76 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050077static int do_chunk_alloc(struct btrfs_trans_handle *trans,
78 struct btrfs_root *extent_root, u64 alloc_bytes,
79 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040080static int find_next_key(struct btrfs_path *path, int level,
81 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040082static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
83 int dump_block_groups);
Josef Bacik6a632092009-02-20 11:00:09 -050084
Josef Bacik817d52f2009-07-13 21:29:25 -040085static noinline int
86block_group_cache_done(struct btrfs_block_group_cache *cache)
87{
88 smp_mb();
89 return cache->cached == BTRFS_CACHE_FINISHED;
90}
91
Josef Bacik0f9dd462008-09-23 13:14:11 -040092static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
93{
94 return (cache->flags & bits) == bits;
95}
96
Josef Bacik11dfe352009-11-13 20:12:59 +000097void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
98{
99 atomic_inc(&cache->count);
100}
101
102void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
103{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400104 if (atomic_dec_and_test(&cache->count)) {
105 WARN_ON(cache->pinned > 0);
106 WARN_ON(cache->reserved > 0);
107 WARN_ON(cache->reserved_pinned > 0);
Josef Bacik11dfe352009-11-13 20:12:59 +0000108 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400109 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000110}
111
Josef Bacik0f9dd462008-09-23 13:14:11 -0400112/*
113 * this adds the block group to the fs_info rb tree for the block group
114 * cache
115 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500116static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400117 struct btrfs_block_group_cache *block_group)
118{
119 struct rb_node **p;
120 struct rb_node *parent = NULL;
121 struct btrfs_block_group_cache *cache;
122
123 spin_lock(&info->block_group_cache_lock);
124 p = &info->block_group_cache_tree.rb_node;
125
126 while (*p) {
127 parent = *p;
128 cache = rb_entry(parent, struct btrfs_block_group_cache,
129 cache_node);
130 if (block_group->key.objectid < cache->key.objectid) {
131 p = &(*p)->rb_left;
132 } else if (block_group->key.objectid > cache->key.objectid) {
133 p = &(*p)->rb_right;
134 } else {
135 spin_unlock(&info->block_group_cache_lock);
136 return -EEXIST;
137 }
138 }
139
140 rb_link_node(&block_group->cache_node, parent, p);
141 rb_insert_color(&block_group->cache_node,
142 &info->block_group_cache_tree);
143 spin_unlock(&info->block_group_cache_lock);
144
145 return 0;
146}
147
148/*
149 * This will return the block group at or after bytenr if contains is 0, else
150 * it will return the block group that contains the bytenr
151 */
152static struct btrfs_block_group_cache *
153block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
154 int contains)
155{
156 struct btrfs_block_group_cache *cache, *ret = NULL;
157 struct rb_node *n;
158 u64 end, start;
159
160 spin_lock(&info->block_group_cache_lock);
161 n = info->block_group_cache_tree.rb_node;
162
163 while (n) {
164 cache = rb_entry(n, struct btrfs_block_group_cache,
165 cache_node);
166 end = cache->key.objectid + cache->key.offset - 1;
167 start = cache->key.objectid;
168
169 if (bytenr < start) {
170 if (!contains && (!ret || start < ret->key.objectid))
171 ret = cache;
172 n = n->rb_left;
173 } else if (bytenr > start) {
174 if (contains && bytenr <= end) {
175 ret = cache;
176 break;
177 }
178 n = n->rb_right;
179 } else {
180 ret = cache;
181 break;
182 }
183 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500184 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000185 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400186 spin_unlock(&info->block_group_cache_lock);
187
188 return ret;
189}
190
Yan Zheng11833d62009-09-11 16:11:19 -0400191static int add_excluded_extent(struct btrfs_root *root,
192 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400193{
Yan Zheng11833d62009-09-11 16:11:19 -0400194 u64 end = start + num_bytes - 1;
195 set_extent_bits(&root->fs_info->freed_extents[0],
196 start, end, EXTENT_UPTODATE, GFP_NOFS);
197 set_extent_bits(&root->fs_info->freed_extents[1],
198 start, end, EXTENT_UPTODATE, GFP_NOFS);
199 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400200}
201
Yan Zheng11833d62009-09-11 16:11:19 -0400202static void free_excluded_extents(struct btrfs_root *root,
203 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400204{
Yan Zheng11833d62009-09-11 16:11:19 -0400205 u64 start, end;
206
207 start = cache->key.objectid;
208 end = start + cache->key.offset - 1;
209
210 clear_extent_bits(&root->fs_info->freed_extents[0],
211 start, end, EXTENT_UPTODATE, GFP_NOFS);
212 clear_extent_bits(&root->fs_info->freed_extents[1],
213 start, end, EXTENT_UPTODATE, GFP_NOFS);
214}
215
216static int exclude_super_stripes(struct btrfs_root *root,
217 struct btrfs_block_group_cache *cache)
218{
Josef Bacik817d52f2009-07-13 21:29:25 -0400219 u64 bytenr;
220 u64 *logical;
221 int stripe_len;
222 int i, nr, ret;
223
Yan, Zheng06b23312009-11-26 09:31:11 +0000224 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
225 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
226 cache->bytes_super += stripe_len;
227 ret = add_excluded_extent(root, cache->key.objectid,
228 stripe_len);
229 BUG_ON(ret);
230 }
231
Josef Bacik817d52f2009-07-13 21:29:25 -0400232 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
233 bytenr = btrfs_sb_offset(i);
234 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
235 cache->key.objectid, bytenr,
236 0, &logical, &nr, &stripe_len);
237 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400238
Josef Bacik817d52f2009-07-13 21:29:25 -0400239 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400240 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400241 ret = add_excluded_extent(root, logical[nr],
242 stripe_len);
243 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400244 }
Yan Zheng11833d62009-09-11 16:11:19 -0400245
Josef Bacik817d52f2009-07-13 21:29:25 -0400246 kfree(logical);
247 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400248 return 0;
249}
250
Yan Zheng11833d62009-09-11 16:11:19 -0400251static struct btrfs_caching_control *
252get_caching_control(struct btrfs_block_group_cache *cache)
253{
254 struct btrfs_caching_control *ctl;
255
256 spin_lock(&cache->lock);
257 if (cache->cached != BTRFS_CACHE_STARTED) {
258 spin_unlock(&cache->lock);
259 return NULL;
260 }
261
Josef Bacikdde5abe2010-09-16 16:17:03 -0400262 /* We're loading it the fast way, so we don't have a caching_ctl. */
263 if (!cache->caching_ctl) {
264 spin_unlock(&cache->lock);
265 return NULL;
266 }
267
Yan Zheng11833d62009-09-11 16:11:19 -0400268 ctl = cache->caching_ctl;
269 atomic_inc(&ctl->count);
270 spin_unlock(&cache->lock);
271 return ctl;
272}
273
274static void put_caching_control(struct btrfs_caching_control *ctl)
275{
276 if (atomic_dec_and_test(&ctl->count))
277 kfree(ctl);
278}
279
Josef Bacik0f9dd462008-09-23 13:14:11 -0400280/*
281 * this is only called by cache_block_group, since we could have freed extents
282 * we need to check the pinned_extents for any extents that can't be used yet
283 * since their free space will be released as soon as the transaction commits.
284 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400285static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400286 struct btrfs_fs_info *info, u64 start, u64 end)
287{
Josef Bacik817d52f2009-07-13 21:29:25 -0400288 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400289 int ret;
290
291 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400292 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400293 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400294 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400295 if (ret)
296 break;
297
Yan, Zheng06b23312009-11-26 09:31:11 +0000298 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400299 start = extent_end + 1;
300 } else if (extent_start > start && extent_start < end) {
301 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400302 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500303 ret = btrfs_add_free_space(block_group, start,
304 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400305 BUG_ON(ret);
306 start = extent_end + 1;
307 } else {
308 break;
309 }
310 }
311
312 if (start < end) {
313 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400314 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500315 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316 BUG_ON(ret);
317 }
318
Josef Bacik817d52f2009-07-13 21:29:25 -0400319 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400320}
321
Josef Bacik817d52f2009-07-13 21:29:25 -0400322static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400323{
Josef Bacik817d52f2009-07-13 21:29:25 -0400324 struct btrfs_block_group_cache *block_group = data;
325 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400326 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
327 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400328 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400329 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400330 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400332 u64 last = 0;
333 u32 nritems;
334 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400335
Chris Masone37c9e62007-05-09 20:13:14 -0400336 path = btrfs_alloc_path();
337 if (!path)
338 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400339
Josef Bacik817d52f2009-07-13 21:29:25 -0400340 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400341
Chris Mason5cd57b22008-06-25 16:01:30 -0400342 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400343 * We don't want to deadlock with somebody trying to allocate a new
344 * extent for the extent root while also trying to search the extent
345 * root to add free space. So we skip locking and search the commit
346 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400347 */
348 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400349 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400350 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400351
Yan Zhenge4404d62008-12-12 10:03:26 -0500352 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400353 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400354 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400355again:
Yan Zheng11833d62009-09-11 16:11:19 -0400356 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400357 /* need to make sure the commit_root doesn't disappear */
358 down_read(&fs_info->extent_commit_sem);
359
Yan Zheng11833d62009-09-11 16:11:19 -0400360 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400361 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400362 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500363
Yan Zheng11833d62009-09-11 16:11:19 -0400364 leaf = path->nodes[0];
365 nritems = btrfs_header_nritems(leaf);
366
Chris Masond3977122009-01-05 21:25:51 -0500367 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400368 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400369 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400370 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400371 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400372 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400373
Yan Zheng11833d62009-09-11 16:11:19 -0400374 if (path->slots[0] < nritems) {
375 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
376 } else {
377 ret = find_next_key(path, 0, &key);
378 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400379 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400380
Josef Bacik589d8ad2011-05-11 17:30:53 -0400381 if (need_resched() ||
382 btrfs_next_leaf(extent_root, path)) {
383 caching_ctl->progress = last;
384 btrfs_release_path(extent_root, path);
385 up_read(&fs_info->extent_commit_sem);
386 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400387 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400388 goto again;
389 }
390 leaf = path->nodes[0];
391 nritems = btrfs_header_nritems(leaf);
392 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400393 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400394
Yan Zheng11833d62009-09-11 16:11:19 -0400395 if (key.objectid < block_group->key.objectid) {
396 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400397 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400398 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400399
Chris Masone37c9e62007-05-09 20:13:14 -0400400 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400401 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400402 break;
Yan7d7d6062007-09-14 16:15:28 -0400403
Yan Zheng11833d62009-09-11 16:11:19 -0400404 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400405 total_found += add_new_free_space(block_group,
406 fs_info, last,
407 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400408 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400409
Yan Zheng11833d62009-09-11 16:11:19 -0400410 if (total_found > (1024 * 1024 * 2)) {
411 total_found = 0;
412 wake_up(&caching_ctl->wait);
413 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400414 }
Chris Masone37c9e62007-05-09 20:13:14 -0400415 path->slots[0]++;
416 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400417 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400418
419 total_found += add_new_free_space(block_group, fs_info, last,
420 block_group->key.objectid +
421 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400422 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400423
424 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400425 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400426 block_group->cached = BTRFS_CACHE_FINISHED;
427 spin_unlock(&block_group->lock);
428
Chris Mason54aa1f42007-06-22 14:16:25 -0400429err:
Chris Masone37c9e62007-05-09 20:13:14 -0400430 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400431 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400432
Yan Zheng11833d62009-09-11 16:11:19 -0400433 free_excluded_extents(extent_root, block_group);
434
435 mutex_unlock(&caching_ctl->mutex);
436 wake_up(&caching_ctl->wait);
437
438 put_caching_control(caching_ctl);
439 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000440 btrfs_put_block_group(block_group);
441
Josef Bacik817d52f2009-07-13 21:29:25 -0400442 return 0;
443}
444
Josef Bacik9d66e232010-08-25 16:54:15 -0400445static int cache_block_group(struct btrfs_block_group_cache *cache,
446 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500447 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400448 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400449{
Yan Zheng11833d62009-09-11 16:11:19 -0400450 struct btrfs_fs_info *fs_info = cache->fs_info;
451 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400452 struct task_struct *tsk;
453 int ret = 0;
454
Yan Zheng11833d62009-09-11 16:11:19 -0400455 smp_mb();
456 if (cache->cached != BTRFS_CACHE_NO)
457 return 0;
458
Josef Bacik9d66e232010-08-25 16:54:15 -0400459 /*
460 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500461 * to have the normal tree locking. Also if we are currently trying to
462 * allocate blocks for the tree root we can't do the fast caching since
463 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400464 */
Li Dongyangf7039b12011-03-24 10:24:28 +0000465 if (trans && (!trans->transaction->in_commit) &&
Josef Bacikb8399de2010-12-08 09:15:11 -0500466 (root && root != root->fs_info->tree_root)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400467 spin_lock(&cache->lock);
468 if (cache->cached != BTRFS_CACHE_NO) {
469 spin_unlock(&cache->lock);
470 return 0;
471 }
472 cache->cached = BTRFS_CACHE_STARTED;
473 spin_unlock(&cache->lock);
474
475 ret = load_free_space_cache(fs_info, cache);
476
477 spin_lock(&cache->lock);
478 if (ret == 1) {
479 cache->cached = BTRFS_CACHE_FINISHED;
480 cache->last_byte_to_unpin = (u64)-1;
481 } else {
482 cache->cached = BTRFS_CACHE_NO;
483 }
484 spin_unlock(&cache->lock);
Josef Bacik3c148742011-02-02 15:53:47 +0000485 if (ret == 1) {
486 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400487 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000488 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400489 }
490
491 if (load_cache_only)
492 return 0;
493
Miao Xiefc0e4a32011-03-24 11:41:21 +0000494 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -0400495 BUG_ON(!caching_ctl);
496
497 INIT_LIST_HEAD(&caching_ctl->list);
498 mutex_init(&caching_ctl->mutex);
499 init_waitqueue_head(&caching_ctl->wait);
500 caching_ctl->block_group = cache;
501 caching_ctl->progress = cache->key.objectid;
502 /* one for caching kthread, one for caching block group list */
503 atomic_set(&caching_ctl->count, 2);
504
Josef Bacik817d52f2009-07-13 21:29:25 -0400505 spin_lock(&cache->lock);
506 if (cache->cached != BTRFS_CACHE_NO) {
507 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400508 kfree(caching_ctl);
509 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400510 }
Yan Zheng11833d62009-09-11 16:11:19 -0400511 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400512 cache->cached = BTRFS_CACHE_STARTED;
513 spin_unlock(&cache->lock);
514
Yan Zheng11833d62009-09-11 16:11:19 -0400515 down_write(&fs_info->extent_commit_sem);
516 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
517 up_write(&fs_info->extent_commit_sem);
518
519 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000520 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400521
Josef Bacik817d52f2009-07-13 21:29:25 -0400522 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
523 cache->key.objectid);
524 if (IS_ERR(tsk)) {
525 ret = PTR_ERR(tsk);
526 printk(KERN_ERR "error running thread %d\n", ret);
527 BUG();
528 }
529
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400530 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400531}
532
Josef Bacik0f9dd462008-09-23 13:14:11 -0400533/*
534 * return the block group that starts at or after bytenr
535 */
Chris Masond3977122009-01-05 21:25:51 -0500536static struct btrfs_block_group_cache *
537btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400538{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400539 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400540
Josef Bacik0f9dd462008-09-23 13:14:11 -0400541 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400542
Josef Bacik0f9dd462008-09-23 13:14:11 -0400543 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400544}
545
Josef Bacik0f9dd462008-09-23 13:14:11 -0400546/*
Sankar P9f556842009-05-14 13:52:22 -0400547 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400548 */
Chris Masond3977122009-01-05 21:25:51 -0500549struct btrfs_block_group_cache *btrfs_lookup_block_group(
550 struct btrfs_fs_info *info,
551 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400552{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400553 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400554
Josef Bacik0f9dd462008-09-23 13:14:11 -0400555 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400556
Josef Bacik0f9dd462008-09-23 13:14:11 -0400557 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400558}
Chris Mason0b86a832008-03-24 15:01:56 -0400559
Josef Bacik0f9dd462008-09-23 13:14:11 -0400560static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
561 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400562{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400563 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400564 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400565
Yan, Zhengb742bb82010-05-16 10:46:24 -0400566 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
567 BTRFS_BLOCK_GROUP_METADATA;
568
Chris Mason4184ea72009-03-10 12:39:20 -0400569 rcu_read_lock();
570 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400571 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400572 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400573 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400574 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400575 }
Chris Mason4184ea72009-03-10 12:39:20 -0400576 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400577 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400578}
579
Chris Mason4184ea72009-03-10 12:39:20 -0400580/*
581 * after adding space to the filesystem, we need to clear the full flags
582 * on all the space infos.
583 */
584void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
585{
586 struct list_head *head = &info->space_info;
587 struct btrfs_space_info *found;
588
589 rcu_read_lock();
590 list_for_each_entry_rcu(found, head, list)
591 found->full = 0;
592 rcu_read_unlock();
593}
594
Josef Bacik80eb2342008-10-29 14:49:05 -0400595static u64 div_factor(u64 num, int factor)
596{
597 if (factor == 10)
598 return num;
599 num *= factor;
600 do_div(num, 10);
601 return num;
602}
603
Chris Masone5bc2452010-10-26 13:37:56 -0400604static u64 div_factor_fine(u64 num, int factor)
605{
606 if (factor == 100)
607 return num;
608 num *= factor;
609 do_div(num, 100);
610 return num;
611}
612
Yan Zhengd2fb3432008-12-11 16:30:39 -0500613u64 btrfs_find_block_group(struct btrfs_root *root,
614 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400615{
Chris Mason96b51792007-10-15 16:15:19 -0400616 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400617 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500618 u64 last = max(search_hint, search_start);
619 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400620 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500621 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400622 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400623again:
Zheng Yane8569812008-09-26 10:05:48 -0400624 while (1) {
625 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400626 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400627 break;
Chris Mason96b51792007-10-15 16:15:19 -0400628
Chris Masonc286ac42008-07-22 23:06:41 -0400629 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400630 last = cache->key.objectid + cache->key.offset;
631 used = btrfs_block_group_used(&cache->item);
632
Yan Zhengd2fb3432008-12-11 16:30:39 -0500633 if ((full_search || !cache->ro) &&
634 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400635 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500636 div_factor(cache->key.offset, factor)) {
637 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400638 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400639 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400640 goto found;
641 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400642 }
Chris Masonc286ac42008-07-22 23:06:41 -0400643 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400644 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400645 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400646 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400647 if (!wrapped) {
648 last = search_start;
649 wrapped = 1;
650 goto again;
651 }
652 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400653 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400654 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400655 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400656 goto again;
657 }
Chris Masonbe744172007-05-06 10:15:01 -0400658found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500659 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400660}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400661
Chris Masone02119d2008-09-05 16:13:11 -0400662/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400663int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400664{
665 int ret;
666 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400667 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400668
Zheng Yan31840ae2008-09-23 13:14:14 -0400669 path = btrfs_alloc_path();
670 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400671 key.objectid = start;
672 key.offset = len;
673 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
674 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
675 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400676 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500677 return ret;
678}
679
Chris Masond8d5f3e2007-12-11 12:42:00 -0500680/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400681 * helper function to lookup reference count and flags of extent.
682 *
683 * the head node for delayed ref is used to store the sum of all the
684 * reference count modifications queued up in the rbtree. the head
685 * node may also store the extent flags to set. This way you can check
686 * to see what the reference count and extent flags would be if all of
687 * the delayed refs are not processed.
688 */
689int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
690 struct btrfs_root *root, u64 bytenr,
691 u64 num_bytes, u64 *refs, u64 *flags)
692{
693 struct btrfs_delayed_ref_head *head;
694 struct btrfs_delayed_ref_root *delayed_refs;
695 struct btrfs_path *path;
696 struct btrfs_extent_item *ei;
697 struct extent_buffer *leaf;
698 struct btrfs_key key;
699 u32 item_size;
700 u64 num_refs;
701 u64 extent_flags;
702 int ret;
703
704 path = btrfs_alloc_path();
705 if (!path)
706 return -ENOMEM;
707
708 key.objectid = bytenr;
709 key.type = BTRFS_EXTENT_ITEM_KEY;
710 key.offset = num_bytes;
711 if (!trans) {
712 path->skip_locking = 1;
713 path->search_commit_root = 1;
714 }
715again:
716 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
717 &key, path, 0, 0);
718 if (ret < 0)
719 goto out_free;
720
721 if (ret == 0) {
722 leaf = path->nodes[0];
723 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
724 if (item_size >= sizeof(*ei)) {
725 ei = btrfs_item_ptr(leaf, path->slots[0],
726 struct btrfs_extent_item);
727 num_refs = btrfs_extent_refs(leaf, ei);
728 extent_flags = btrfs_extent_flags(leaf, ei);
729 } else {
730#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
731 struct btrfs_extent_item_v0 *ei0;
732 BUG_ON(item_size != sizeof(*ei0));
733 ei0 = btrfs_item_ptr(leaf, path->slots[0],
734 struct btrfs_extent_item_v0);
735 num_refs = btrfs_extent_refs_v0(leaf, ei0);
736 /* FIXME: this isn't correct for data */
737 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
738#else
739 BUG();
740#endif
741 }
742 BUG_ON(num_refs == 0);
743 } else {
744 num_refs = 0;
745 extent_flags = 0;
746 ret = 0;
747 }
748
749 if (!trans)
750 goto out;
751
752 delayed_refs = &trans->transaction->delayed_refs;
753 spin_lock(&delayed_refs->lock);
754 head = btrfs_find_delayed_ref_head(trans, bytenr);
755 if (head) {
756 if (!mutex_trylock(&head->mutex)) {
757 atomic_inc(&head->node.refs);
758 spin_unlock(&delayed_refs->lock);
759
760 btrfs_release_path(root->fs_info->extent_root, path);
761
762 mutex_lock(&head->mutex);
763 mutex_unlock(&head->mutex);
764 btrfs_put_delayed_ref(&head->node);
765 goto again;
766 }
767 if (head->extent_op && head->extent_op->update_flags)
768 extent_flags |= head->extent_op->flags_to_set;
769 else
770 BUG_ON(num_refs == 0);
771
772 num_refs += head->node.ref_mod;
773 mutex_unlock(&head->mutex);
774 }
775 spin_unlock(&delayed_refs->lock);
776out:
777 WARN_ON(num_refs == 0);
778 if (refs)
779 *refs = num_refs;
780 if (flags)
781 *flags = extent_flags;
782out_free:
783 btrfs_free_path(path);
784 return ret;
785}
786
787/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500788 * Back reference rules. Back refs have three main goals:
789 *
790 * 1) differentiate between all holders of references to an extent so that
791 * when a reference is dropped we can make sure it was a valid reference
792 * before freeing the extent.
793 *
794 * 2) Provide enough information to quickly find the holders of an extent
795 * if we notice a given block is corrupted or bad.
796 *
797 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
798 * maintenance. This is actually the same as #2, but with a slightly
799 * different use case.
800 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400801 * There are two kinds of back refs. The implicit back refs is optimized
802 * for pointers in non-shared tree blocks. For a given pointer in a block,
803 * back refs of this kind provide information about the block's owner tree
804 * and the pointer's key. These information allow us to find the block by
805 * b-tree searching. The full back refs is for pointers in tree blocks not
806 * referenced by their owner trees. The location of tree block is recorded
807 * in the back refs. Actually the full back refs is generic, and can be
808 * used in all cases the implicit back refs is used. The major shortcoming
809 * of the full back refs is its overhead. Every time a tree block gets
810 * COWed, we have to update back refs entry for all pointers in it.
811 *
812 * For a newly allocated tree block, we use implicit back refs for
813 * pointers in it. This means most tree related operations only involve
814 * implicit back refs. For a tree block created in old transaction, the
815 * only way to drop a reference to it is COW it. So we can detect the
816 * event that tree block loses its owner tree's reference and do the
817 * back refs conversion.
818 *
819 * When a tree block is COW'd through a tree, there are four cases:
820 *
821 * The reference count of the block is one and the tree is the block's
822 * owner tree. Nothing to do in this case.
823 *
824 * The reference count of the block is one and the tree is not the
825 * block's owner tree. In this case, full back refs is used for pointers
826 * in the block. Remove these full back refs, add implicit back refs for
827 * every pointers in the new block.
828 *
829 * The reference count of the block is greater than one and the tree is
830 * the block's owner tree. In this case, implicit back refs is used for
831 * pointers in the block. Add full back refs for every pointers in the
832 * block, increase lower level extents' reference counts. The original
833 * implicit back refs are entailed to the new block.
834 *
835 * The reference count of the block is greater than one and the tree is
836 * not the block's owner tree. Add implicit back refs for every pointer in
837 * the new block, increase lower level extents' reference count.
838 *
839 * Back Reference Key composing:
840 *
841 * The key objectid corresponds to the first byte in the extent,
842 * The key type is used to differentiate between types of back refs.
843 * There are different meanings of the key offset for different types
844 * of back refs.
845 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500846 * File extents can be referenced by:
847 *
848 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400849 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500850 * - different offsets inside a file (bookend extents in file.c)
851 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400852 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500853 *
854 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500855 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400856 * - original offset in the file
857 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400858 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400859 * The key offset for the implicit back refs is hash of the first
860 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500861 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400862 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500863 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400864 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500865 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 * The key offset for the implicit back refs is the first byte of
867 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500868 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400869 * When a file extent is allocated, The implicit back refs is used.
870 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500871 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400872 * (root_key.objectid, inode objectid, offset in file, 1)
873 *
874 * When a file extent is removed file truncation, we find the
875 * corresponding implicit back refs and check the following fields:
876 *
877 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500878 *
879 * Btree extents can be referenced by:
880 *
881 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500882 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400883 * Both the implicit back refs and the full back refs for tree blocks
884 * only consist of key. The key offset for the implicit back refs is
885 * objectid of block's owner tree. The key offset for the full back refs
886 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500887 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400888 * When implicit back refs is used, information about the lowest key and
889 * level of the tree block are required. These information are stored in
890 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500891 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400892
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400893#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
894static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
895 struct btrfs_root *root,
896 struct btrfs_path *path,
897 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500898{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 struct btrfs_extent_item *item;
900 struct btrfs_extent_item_v0 *ei0;
901 struct btrfs_extent_ref_v0 *ref0;
902 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400903 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400904 struct btrfs_key key;
905 struct btrfs_key found_key;
906 u32 new_size = sizeof(*item);
907 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500908 int ret;
909
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400910 leaf = path->nodes[0];
911 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500912
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400913 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
914 ei0 = btrfs_item_ptr(leaf, path->slots[0],
915 struct btrfs_extent_item_v0);
916 refs = btrfs_extent_refs_v0(leaf, ei0);
917
918 if (owner == (u64)-1) {
919 while (1) {
920 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
921 ret = btrfs_next_leaf(root, path);
922 if (ret < 0)
923 return ret;
924 BUG_ON(ret > 0);
925 leaf = path->nodes[0];
926 }
927 btrfs_item_key_to_cpu(leaf, &found_key,
928 path->slots[0]);
929 BUG_ON(key.objectid != found_key.objectid);
930 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
931 path->slots[0]++;
932 continue;
933 }
934 ref0 = btrfs_item_ptr(leaf, path->slots[0],
935 struct btrfs_extent_ref_v0);
936 owner = btrfs_ref_objectid_v0(leaf, ref0);
937 break;
938 }
939 }
940 btrfs_release_path(root, path);
941
942 if (owner < BTRFS_FIRST_FREE_OBJECTID)
943 new_size += sizeof(*bi);
944
945 new_size -= sizeof(*ei0);
946 ret = btrfs_search_slot(trans, root, &key, path,
947 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400948 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 return ret;
950 BUG_ON(ret);
951
952 ret = btrfs_extend_item(trans, root, path, new_size);
953 BUG_ON(ret);
954
955 leaf = path->nodes[0];
956 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
957 btrfs_set_extent_refs(leaf, item, refs);
958 /* FIXME: get real generation */
959 btrfs_set_extent_generation(leaf, item, 0);
960 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
961 btrfs_set_extent_flags(leaf, item,
962 BTRFS_EXTENT_FLAG_TREE_BLOCK |
963 BTRFS_BLOCK_FLAG_FULL_BACKREF);
964 bi = (struct btrfs_tree_block_info *)(item + 1);
965 /* FIXME: get first key of the block */
966 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
967 btrfs_set_tree_block_level(leaf, bi, (int)owner);
968 } else {
969 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
970 }
971 btrfs_mark_buffer_dirty(leaf);
972 return 0;
973}
974#endif
975
976static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
977{
978 u32 high_crc = ~(u32)0;
979 u32 low_crc = ~(u32)0;
980 __le64 lenum;
981
982 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100983 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400984 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100985 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100987 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988
989 return ((u64)high_crc << 31) ^ (u64)low_crc;
990}
991
992static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
993 struct btrfs_extent_data_ref *ref)
994{
995 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
996 btrfs_extent_data_ref_objectid(leaf, ref),
997 btrfs_extent_data_ref_offset(leaf, ref));
998}
999
1000static int match_extent_data_ref(struct extent_buffer *leaf,
1001 struct btrfs_extent_data_ref *ref,
1002 u64 root_objectid, u64 owner, u64 offset)
1003{
1004 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1005 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1006 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1007 return 0;
1008 return 1;
1009}
1010
1011static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1012 struct btrfs_root *root,
1013 struct btrfs_path *path,
1014 u64 bytenr, u64 parent,
1015 u64 root_objectid,
1016 u64 owner, u64 offset)
1017{
1018 struct btrfs_key key;
1019 struct btrfs_extent_data_ref *ref;
1020 struct extent_buffer *leaf;
1021 u32 nritems;
1022 int ret;
1023 int recow;
1024 int err = -ENOENT;
1025
1026 key.objectid = bytenr;
1027 if (parent) {
1028 key.type = BTRFS_SHARED_DATA_REF_KEY;
1029 key.offset = parent;
1030 } else {
1031 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1032 key.offset = hash_extent_data_ref(root_objectid,
1033 owner, offset);
1034 }
1035again:
1036 recow = 0;
1037 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1038 if (ret < 0) {
1039 err = ret;
1040 goto fail;
1041 }
1042
1043 if (parent) {
1044 if (!ret)
1045 return 0;
1046#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1047 key.type = BTRFS_EXTENT_REF_V0_KEY;
1048 btrfs_release_path(root, path);
1049 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1050 if (ret < 0) {
1051 err = ret;
1052 goto fail;
1053 }
1054 if (!ret)
1055 return 0;
1056#endif
1057 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001058 }
1059
1060 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001061 nritems = btrfs_header_nritems(leaf);
1062 while (1) {
1063 if (path->slots[0] >= nritems) {
1064 ret = btrfs_next_leaf(root, path);
1065 if (ret < 0)
1066 err = ret;
1067 if (ret)
1068 goto fail;
1069
1070 leaf = path->nodes[0];
1071 nritems = btrfs_header_nritems(leaf);
1072 recow = 1;
1073 }
1074
1075 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1076 if (key.objectid != bytenr ||
1077 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1078 goto fail;
1079
1080 ref = btrfs_item_ptr(leaf, path->slots[0],
1081 struct btrfs_extent_data_ref);
1082
1083 if (match_extent_data_ref(leaf, ref, root_objectid,
1084 owner, offset)) {
1085 if (recow) {
1086 btrfs_release_path(root, path);
1087 goto again;
1088 }
1089 err = 0;
1090 break;
1091 }
1092 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001093 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001094fail:
1095 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001096}
1097
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001098static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1099 struct btrfs_root *root,
1100 struct btrfs_path *path,
1101 u64 bytenr, u64 parent,
1102 u64 root_objectid, u64 owner,
1103 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001104{
1105 struct btrfs_key key;
1106 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001107 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001108 u32 num_refs;
1109 int ret;
1110
1111 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001112 if (parent) {
1113 key.type = BTRFS_SHARED_DATA_REF_KEY;
1114 key.offset = parent;
1115 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001116 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001117 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1118 key.offset = hash_extent_data_ref(root_objectid,
1119 owner, offset);
1120 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001121 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001122
1123 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1124 if (ret && ret != -EEXIST)
1125 goto fail;
1126
1127 leaf = path->nodes[0];
1128 if (parent) {
1129 struct btrfs_shared_data_ref *ref;
1130 ref = btrfs_item_ptr(leaf, path->slots[0],
1131 struct btrfs_shared_data_ref);
1132 if (ret == 0) {
1133 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1134 } else {
1135 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1136 num_refs += refs_to_add;
1137 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1138 }
1139 } else {
1140 struct btrfs_extent_data_ref *ref;
1141 while (ret == -EEXIST) {
1142 ref = btrfs_item_ptr(leaf, path->slots[0],
1143 struct btrfs_extent_data_ref);
1144 if (match_extent_data_ref(leaf, ref, root_objectid,
1145 owner, offset))
1146 break;
1147 btrfs_release_path(root, path);
1148 key.offset++;
1149 ret = btrfs_insert_empty_item(trans, root, path, &key,
1150 size);
1151 if (ret && ret != -EEXIST)
1152 goto fail;
1153
1154 leaf = path->nodes[0];
1155 }
1156 ref = btrfs_item_ptr(leaf, path->slots[0],
1157 struct btrfs_extent_data_ref);
1158 if (ret == 0) {
1159 btrfs_set_extent_data_ref_root(leaf, ref,
1160 root_objectid);
1161 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1162 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1163 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1164 } else {
1165 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1166 num_refs += refs_to_add;
1167 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1168 }
1169 }
1170 btrfs_mark_buffer_dirty(leaf);
1171 ret = 0;
1172fail:
Chris Mason7bb86312007-12-11 09:25:06 -05001173 btrfs_release_path(root, path);
1174 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001175}
1176
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001177static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1178 struct btrfs_root *root,
1179 struct btrfs_path *path,
1180 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001181{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001182 struct btrfs_key key;
1183 struct btrfs_extent_data_ref *ref1 = NULL;
1184 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001185 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001186 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001187 int ret = 0;
1188
1189 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001190 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1191
1192 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1193 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1194 struct btrfs_extent_data_ref);
1195 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1196 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1197 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1198 struct btrfs_shared_data_ref);
1199 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1200#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1201 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1202 struct btrfs_extent_ref_v0 *ref0;
1203 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1204 struct btrfs_extent_ref_v0);
1205 num_refs = btrfs_ref_count_v0(leaf, ref0);
1206#endif
1207 } else {
1208 BUG();
1209 }
1210
Chris Mason56bec292009-03-13 10:10:06 -04001211 BUG_ON(num_refs < refs_to_drop);
1212 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001213
Zheng Yan31840ae2008-09-23 13:14:14 -04001214 if (num_refs == 0) {
1215 ret = btrfs_del_item(trans, root, path);
1216 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001217 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1218 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1219 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1220 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1221#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1222 else {
1223 struct btrfs_extent_ref_v0 *ref0;
1224 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1225 struct btrfs_extent_ref_v0);
1226 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1227 }
1228#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001229 btrfs_mark_buffer_dirty(leaf);
1230 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001231 return ret;
1232}
1233
1234static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1235 struct btrfs_path *path,
1236 struct btrfs_extent_inline_ref *iref)
1237{
1238 struct btrfs_key key;
1239 struct extent_buffer *leaf;
1240 struct btrfs_extent_data_ref *ref1;
1241 struct btrfs_shared_data_ref *ref2;
1242 u32 num_refs = 0;
1243
1244 leaf = path->nodes[0];
1245 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1246 if (iref) {
1247 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1248 BTRFS_EXTENT_DATA_REF_KEY) {
1249 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1250 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1251 } else {
1252 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1253 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1254 }
1255 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1256 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1257 struct btrfs_extent_data_ref);
1258 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1259 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1260 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1261 struct btrfs_shared_data_ref);
1262 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1263#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1264 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1265 struct btrfs_extent_ref_v0 *ref0;
1266 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1267 struct btrfs_extent_ref_v0);
1268 num_refs = btrfs_ref_count_v0(leaf, ref0);
1269#endif
1270 } else {
1271 WARN_ON(1);
1272 }
1273 return num_refs;
1274}
1275
1276static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1277 struct btrfs_root *root,
1278 struct btrfs_path *path,
1279 u64 bytenr, u64 parent,
1280 u64 root_objectid)
1281{
1282 struct btrfs_key key;
1283 int ret;
1284
1285 key.objectid = bytenr;
1286 if (parent) {
1287 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1288 key.offset = parent;
1289 } else {
1290 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1291 key.offset = root_objectid;
1292 }
1293
1294 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1295 if (ret > 0)
1296 ret = -ENOENT;
1297#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1298 if (ret == -ENOENT && parent) {
1299 btrfs_release_path(root, path);
1300 key.type = BTRFS_EXTENT_REF_V0_KEY;
1301 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1302 if (ret > 0)
1303 ret = -ENOENT;
1304 }
1305#endif
1306 return ret;
1307}
1308
1309static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1310 struct btrfs_root *root,
1311 struct btrfs_path *path,
1312 u64 bytenr, u64 parent,
1313 u64 root_objectid)
1314{
1315 struct btrfs_key key;
1316 int ret;
1317
1318 key.objectid = bytenr;
1319 if (parent) {
1320 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1321 key.offset = parent;
1322 } else {
1323 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1324 key.offset = root_objectid;
1325 }
1326
1327 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001328 btrfs_release_path(root, path);
1329 return ret;
1330}
1331
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001332static inline int extent_ref_type(u64 parent, u64 owner)
1333{
1334 int type;
1335 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1336 if (parent > 0)
1337 type = BTRFS_SHARED_BLOCK_REF_KEY;
1338 else
1339 type = BTRFS_TREE_BLOCK_REF_KEY;
1340 } else {
1341 if (parent > 0)
1342 type = BTRFS_SHARED_DATA_REF_KEY;
1343 else
1344 type = BTRFS_EXTENT_DATA_REF_KEY;
1345 }
1346 return type;
1347}
1348
Yan Zheng2c47e6052009-06-27 21:07:35 -04001349static int find_next_key(struct btrfs_path *path, int level,
1350 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001351
1352{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001353 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001354 if (!path->nodes[level])
1355 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001356 if (path->slots[level] + 1 >=
1357 btrfs_header_nritems(path->nodes[level]))
1358 continue;
1359 if (level == 0)
1360 btrfs_item_key_to_cpu(path->nodes[level], key,
1361 path->slots[level] + 1);
1362 else
1363 btrfs_node_key_to_cpu(path->nodes[level], key,
1364 path->slots[level] + 1);
1365 return 0;
1366 }
1367 return 1;
1368}
1369
1370/*
1371 * look for inline back ref. if back ref is found, *ref_ret is set
1372 * to the address of inline back ref, and 0 is returned.
1373 *
1374 * if back ref isn't found, *ref_ret is set to the address where it
1375 * should be inserted, and -ENOENT is returned.
1376 *
1377 * if insert is true and there are too many inline back refs, the path
1378 * points to the extent item, and -EAGAIN is returned.
1379 *
1380 * NOTE: inline back refs are ordered in the same way that back ref
1381 * items in the tree are ordered.
1382 */
1383static noinline_for_stack
1384int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1385 struct btrfs_root *root,
1386 struct btrfs_path *path,
1387 struct btrfs_extent_inline_ref **ref_ret,
1388 u64 bytenr, u64 num_bytes,
1389 u64 parent, u64 root_objectid,
1390 u64 owner, u64 offset, int insert)
1391{
1392 struct btrfs_key key;
1393 struct extent_buffer *leaf;
1394 struct btrfs_extent_item *ei;
1395 struct btrfs_extent_inline_ref *iref;
1396 u64 flags;
1397 u64 item_size;
1398 unsigned long ptr;
1399 unsigned long end;
1400 int extra_size;
1401 int type;
1402 int want;
1403 int ret;
1404 int err = 0;
1405
1406 key.objectid = bytenr;
1407 key.type = BTRFS_EXTENT_ITEM_KEY;
1408 key.offset = num_bytes;
1409
1410 want = extent_ref_type(parent, owner);
1411 if (insert) {
1412 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001413 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001414 } else
1415 extra_size = -1;
1416 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1417 if (ret < 0) {
1418 err = ret;
1419 goto out;
1420 }
1421 BUG_ON(ret);
1422
1423 leaf = path->nodes[0];
1424 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1425#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1426 if (item_size < sizeof(*ei)) {
1427 if (!insert) {
1428 err = -ENOENT;
1429 goto out;
1430 }
1431 ret = convert_extent_item_v0(trans, root, path, owner,
1432 extra_size);
1433 if (ret < 0) {
1434 err = ret;
1435 goto out;
1436 }
1437 leaf = path->nodes[0];
1438 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1439 }
1440#endif
1441 BUG_ON(item_size < sizeof(*ei));
1442
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001443 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1444 flags = btrfs_extent_flags(leaf, ei);
1445
1446 ptr = (unsigned long)(ei + 1);
1447 end = (unsigned long)ei + item_size;
1448
1449 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1450 ptr += sizeof(struct btrfs_tree_block_info);
1451 BUG_ON(ptr > end);
1452 } else {
1453 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1454 }
1455
1456 err = -ENOENT;
1457 while (1) {
1458 if (ptr >= end) {
1459 WARN_ON(ptr > end);
1460 break;
1461 }
1462 iref = (struct btrfs_extent_inline_ref *)ptr;
1463 type = btrfs_extent_inline_ref_type(leaf, iref);
1464 if (want < type)
1465 break;
1466 if (want > type) {
1467 ptr += btrfs_extent_inline_ref_size(type);
1468 continue;
1469 }
1470
1471 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1472 struct btrfs_extent_data_ref *dref;
1473 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1474 if (match_extent_data_ref(leaf, dref, root_objectid,
1475 owner, offset)) {
1476 err = 0;
1477 break;
1478 }
1479 if (hash_extent_data_ref_item(leaf, dref) <
1480 hash_extent_data_ref(root_objectid, owner, offset))
1481 break;
1482 } else {
1483 u64 ref_offset;
1484 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1485 if (parent > 0) {
1486 if (parent == ref_offset) {
1487 err = 0;
1488 break;
1489 }
1490 if (ref_offset < parent)
1491 break;
1492 } else {
1493 if (root_objectid == ref_offset) {
1494 err = 0;
1495 break;
1496 }
1497 if (ref_offset < root_objectid)
1498 break;
1499 }
1500 }
1501 ptr += btrfs_extent_inline_ref_size(type);
1502 }
1503 if (err == -ENOENT && insert) {
1504 if (item_size + extra_size >=
1505 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1506 err = -EAGAIN;
1507 goto out;
1508 }
1509 /*
1510 * To add new inline back ref, we have to make sure
1511 * there is no corresponding back ref item.
1512 * For simplicity, we just do not add new inline back
1513 * ref if there is any kind of item for this block
1514 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001515 if (find_next_key(path, 0, &key) == 0 &&
1516 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001517 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001518 err = -EAGAIN;
1519 goto out;
1520 }
1521 }
1522 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1523out:
Yan Zheng85d41982009-06-11 08:51:10 -04001524 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001525 path->keep_locks = 0;
1526 btrfs_unlock_up_safe(path, 1);
1527 }
1528 return err;
1529}
1530
1531/*
1532 * helper to add new inline back ref
1533 */
1534static noinline_for_stack
1535int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1536 struct btrfs_root *root,
1537 struct btrfs_path *path,
1538 struct btrfs_extent_inline_ref *iref,
1539 u64 parent, u64 root_objectid,
1540 u64 owner, u64 offset, int refs_to_add,
1541 struct btrfs_delayed_extent_op *extent_op)
1542{
1543 struct extent_buffer *leaf;
1544 struct btrfs_extent_item *ei;
1545 unsigned long ptr;
1546 unsigned long end;
1547 unsigned long item_offset;
1548 u64 refs;
1549 int size;
1550 int type;
1551 int ret;
1552
1553 leaf = path->nodes[0];
1554 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1555 item_offset = (unsigned long)iref - (unsigned long)ei;
1556
1557 type = extent_ref_type(parent, owner);
1558 size = btrfs_extent_inline_ref_size(type);
1559
1560 ret = btrfs_extend_item(trans, root, path, size);
1561 BUG_ON(ret);
1562
1563 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1564 refs = btrfs_extent_refs(leaf, ei);
1565 refs += refs_to_add;
1566 btrfs_set_extent_refs(leaf, ei, refs);
1567 if (extent_op)
1568 __run_delayed_extent_op(extent_op, leaf, ei);
1569
1570 ptr = (unsigned long)ei + item_offset;
1571 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1572 if (ptr < end - size)
1573 memmove_extent_buffer(leaf, ptr + size, ptr,
1574 end - size - ptr);
1575
1576 iref = (struct btrfs_extent_inline_ref *)ptr;
1577 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1578 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1579 struct btrfs_extent_data_ref *dref;
1580 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1581 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1582 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1583 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1584 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1585 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1586 struct btrfs_shared_data_ref *sref;
1587 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1588 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1589 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1590 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1591 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1592 } else {
1593 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1594 }
1595 btrfs_mark_buffer_dirty(leaf);
1596 return 0;
1597}
1598
1599static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1600 struct btrfs_root *root,
1601 struct btrfs_path *path,
1602 struct btrfs_extent_inline_ref **ref_ret,
1603 u64 bytenr, u64 num_bytes, u64 parent,
1604 u64 root_objectid, u64 owner, u64 offset)
1605{
1606 int ret;
1607
1608 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1609 bytenr, num_bytes, parent,
1610 root_objectid, owner, offset, 0);
1611 if (ret != -ENOENT)
1612 return ret;
1613
1614 btrfs_release_path(root, path);
1615 *ref_ret = NULL;
1616
1617 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1618 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1619 root_objectid);
1620 } else {
1621 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1622 root_objectid, owner, offset);
1623 }
1624 return ret;
1625}
1626
1627/*
1628 * helper to update/remove inline back ref
1629 */
1630static noinline_for_stack
1631int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1632 struct btrfs_root *root,
1633 struct btrfs_path *path,
1634 struct btrfs_extent_inline_ref *iref,
1635 int refs_to_mod,
1636 struct btrfs_delayed_extent_op *extent_op)
1637{
1638 struct extent_buffer *leaf;
1639 struct btrfs_extent_item *ei;
1640 struct btrfs_extent_data_ref *dref = NULL;
1641 struct btrfs_shared_data_ref *sref = NULL;
1642 unsigned long ptr;
1643 unsigned long end;
1644 u32 item_size;
1645 int size;
1646 int type;
1647 int ret;
1648 u64 refs;
1649
1650 leaf = path->nodes[0];
1651 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1652 refs = btrfs_extent_refs(leaf, ei);
1653 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1654 refs += refs_to_mod;
1655 btrfs_set_extent_refs(leaf, ei, refs);
1656 if (extent_op)
1657 __run_delayed_extent_op(extent_op, leaf, ei);
1658
1659 type = btrfs_extent_inline_ref_type(leaf, iref);
1660
1661 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1662 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1663 refs = btrfs_extent_data_ref_count(leaf, dref);
1664 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1665 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1666 refs = btrfs_shared_data_ref_count(leaf, sref);
1667 } else {
1668 refs = 1;
1669 BUG_ON(refs_to_mod != -1);
1670 }
1671
1672 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1673 refs += refs_to_mod;
1674
1675 if (refs > 0) {
1676 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1677 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1678 else
1679 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1680 } else {
1681 size = btrfs_extent_inline_ref_size(type);
1682 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1683 ptr = (unsigned long)iref;
1684 end = (unsigned long)ei + item_size;
1685 if (ptr + size < end)
1686 memmove_extent_buffer(leaf, ptr, ptr + size,
1687 end - ptr - size);
1688 item_size -= size;
1689 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1690 BUG_ON(ret);
1691 }
1692 btrfs_mark_buffer_dirty(leaf);
1693 return 0;
1694}
1695
1696static noinline_for_stack
1697int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1698 struct btrfs_root *root,
1699 struct btrfs_path *path,
1700 u64 bytenr, u64 num_bytes, u64 parent,
1701 u64 root_objectid, u64 owner,
1702 u64 offset, int refs_to_add,
1703 struct btrfs_delayed_extent_op *extent_op)
1704{
1705 struct btrfs_extent_inline_ref *iref;
1706 int ret;
1707
1708 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1709 bytenr, num_bytes, parent,
1710 root_objectid, owner, offset, 1);
1711 if (ret == 0) {
1712 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1713 ret = update_inline_extent_backref(trans, root, path, iref,
1714 refs_to_add, extent_op);
1715 } else if (ret == -ENOENT) {
1716 ret = setup_inline_extent_backref(trans, root, path, iref,
1717 parent, root_objectid,
1718 owner, offset, refs_to_add,
1719 extent_op);
1720 }
1721 return ret;
1722}
1723
1724static int insert_extent_backref(struct btrfs_trans_handle *trans,
1725 struct btrfs_root *root,
1726 struct btrfs_path *path,
1727 u64 bytenr, u64 parent, u64 root_objectid,
1728 u64 owner, u64 offset, int refs_to_add)
1729{
1730 int ret;
1731 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1732 BUG_ON(refs_to_add != 1);
1733 ret = insert_tree_block_ref(trans, root, path, bytenr,
1734 parent, root_objectid);
1735 } else {
1736 ret = insert_extent_data_ref(trans, root, path, bytenr,
1737 parent, root_objectid,
1738 owner, offset, refs_to_add);
1739 }
1740 return ret;
1741}
1742
1743static int remove_extent_backref(struct btrfs_trans_handle *trans,
1744 struct btrfs_root *root,
1745 struct btrfs_path *path,
1746 struct btrfs_extent_inline_ref *iref,
1747 int refs_to_drop, int is_data)
1748{
1749 int ret;
1750
1751 BUG_ON(!is_data && refs_to_drop != 1);
1752 if (iref) {
1753 ret = update_inline_extent_backref(trans, root, path, iref,
1754 -refs_to_drop, NULL);
1755 } else if (is_data) {
1756 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1757 } else {
1758 ret = btrfs_del_item(trans, root, path);
1759 }
1760 return ret;
1761}
1762
Li Dongyang5378e602011-03-24 10:24:27 +00001763static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001764 u64 start, u64 len)
1765{
Li Dongyang5378e602011-03-24 10:24:27 +00001766 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001767}
Chris Mason15916de2008-11-19 21:17:22 -05001768
Liu Hui1f3c79a2009-01-05 15:57:51 -05001769static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001770 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001771{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001772 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001773 u64 discarded_bytes = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001774 struct btrfs_multi_bio *multi = NULL;
1775
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001776
Liu Hui1f3c79a2009-01-05 15:57:51 -05001777 /* Tell the block device(s) that the sectors can be discarded */
Li Dongyang5378e602011-03-24 10:24:27 +00001778 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1779 bytenr, &num_bytes, &multi, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001780 if (!ret) {
1781 struct btrfs_bio_stripe *stripe = multi->stripes;
1782 int i;
1783
Liu Hui1f3c79a2009-01-05 15:57:51 -05001784
1785 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Li Dongyang5378e602011-03-24 10:24:27 +00001786 ret = btrfs_issue_discard(stripe->dev->bdev,
1787 stripe->physical,
1788 stripe->length);
1789 if (!ret)
1790 discarded_bytes += stripe->length;
1791 else if (ret != -EOPNOTSUPP)
1792 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001793 }
1794 kfree(multi);
1795 }
Li Dongyang5378e602011-03-24 10:24:27 +00001796 if (discarded_bytes && ret == -EOPNOTSUPP)
1797 ret = 0;
1798
1799 if (actual_bytes)
1800 *actual_bytes = discarded_bytes;
1801
Liu Hui1f3c79a2009-01-05 15:57:51 -05001802
1803 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001804}
1805
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001806int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1807 struct btrfs_root *root,
1808 u64 bytenr, u64 num_bytes, u64 parent,
1809 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001810{
1811 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001812 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1813 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001814
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001815 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1816 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1817 parent, root_objectid, (int)owner,
1818 BTRFS_ADD_DELAYED_REF, NULL);
1819 } else {
1820 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1821 parent, root_objectid, owner, offset,
1822 BTRFS_ADD_DELAYED_REF, NULL);
1823 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001824 return ret;
1825}
1826
Chris Mason925baed2008-06-25 16:01:30 -04001827static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001828 struct btrfs_root *root,
1829 u64 bytenr, u64 num_bytes,
1830 u64 parent, u64 root_objectid,
1831 u64 owner, u64 offset, int refs_to_add,
1832 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001833{
Chris Mason5caf2a02007-04-02 11:20:42 -04001834 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001835 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001836 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001837 u64 refs;
1838 int ret;
1839 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001840
Chris Mason5caf2a02007-04-02 11:20:42 -04001841 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001842 if (!path)
1843 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001844
Chris Mason3c12ac72008-04-21 12:01:38 -04001845 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001846 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001847 /* this will setup the path even if it fails to insert the back ref */
1848 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1849 path, bytenr, num_bytes, parent,
1850 root_objectid, owner, offset,
1851 refs_to_add, extent_op);
1852 if (ret == 0)
1853 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001854
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001855 if (ret != -EAGAIN) {
1856 err = ret;
1857 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001858 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001859
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001860 leaf = path->nodes[0];
1861 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1862 refs = btrfs_extent_refs(leaf, item);
1863 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1864 if (extent_op)
1865 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001866
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001867 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001868 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001869
Chris Mason3c12ac72008-04-21 12:01:38 -04001870 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001871 path->leave_spinning = 1;
1872
Chris Mason56bec292009-03-13 10:10:06 -04001873 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001874 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001875 path, bytenr, parent, root_objectid,
1876 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001877 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878out:
Chris Mason74493f72007-12-11 09:25:06 -05001879 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001880 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001881}
1882
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001883static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1884 struct btrfs_root *root,
1885 struct btrfs_delayed_ref_node *node,
1886 struct btrfs_delayed_extent_op *extent_op,
1887 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001888{
Chris Mason56bec292009-03-13 10:10:06 -04001889 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001890 struct btrfs_delayed_data_ref *ref;
1891 struct btrfs_key ins;
1892 u64 parent = 0;
1893 u64 ref_root = 0;
1894 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001895
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001896 ins.objectid = node->bytenr;
1897 ins.offset = node->num_bytes;
1898 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001899
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001900 ref = btrfs_delayed_node_to_data_ref(node);
1901 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1902 parent = ref->parent;
1903 else
1904 ref_root = ref->root;
1905
1906 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1907 if (extent_op) {
1908 BUG_ON(extent_op->update_key);
1909 flags |= extent_op->flags_to_set;
1910 }
1911 ret = alloc_reserved_file_extent(trans, root,
1912 parent, ref_root, flags,
1913 ref->objectid, ref->offset,
1914 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001915 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1916 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1917 node->num_bytes, parent,
1918 ref_root, ref->objectid,
1919 ref->offset, node->ref_mod,
1920 extent_op);
1921 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1922 ret = __btrfs_free_extent(trans, root, node->bytenr,
1923 node->num_bytes, parent,
1924 ref_root, ref->objectid,
1925 ref->offset, node->ref_mod,
1926 extent_op);
1927 } else {
1928 BUG();
1929 }
Chris Mason56bec292009-03-13 10:10:06 -04001930 return ret;
1931}
1932
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001933static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1934 struct extent_buffer *leaf,
1935 struct btrfs_extent_item *ei)
1936{
1937 u64 flags = btrfs_extent_flags(leaf, ei);
1938 if (extent_op->update_flags) {
1939 flags |= extent_op->flags_to_set;
1940 btrfs_set_extent_flags(leaf, ei, flags);
1941 }
1942
1943 if (extent_op->update_key) {
1944 struct btrfs_tree_block_info *bi;
1945 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1946 bi = (struct btrfs_tree_block_info *)(ei + 1);
1947 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1948 }
1949}
1950
1951static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1952 struct btrfs_root *root,
1953 struct btrfs_delayed_ref_node *node,
1954 struct btrfs_delayed_extent_op *extent_op)
1955{
1956 struct btrfs_key key;
1957 struct btrfs_path *path;
1958 struct btrfs_extent_item *ei;
1959 struct extent_buffer *leaf;
1960 u32 item_size;
1961 int ret;
1962 int err = 0;
1963
1964 path = btrfs_alloc_path();
1965 if (!path)
1966 return -ENOMEM;
1967
1968 key.objectid = node->bytenr;
1969 key.type = BTRFS_EXTENT_ITEM_KEY;
1970 key.offset = node->num_bytes;
1971
1972 path->reada = 1;
1973 path->leave_spinning = 1;
1974 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1975 path, 0, 1);
1976 if (ret < 0) {
1977 err = ret;
1978 goto out;
1979 }
1980 if (ret > 0) {
1981 err = -EIO;
1982 goto out;
1983 }
1984
1985 leaf = path->nodes[0];
1986 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1987#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1988 if (item_size < sizeof(*ei)) {
1989 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1990 path, (u64)-1, 0);
1991 if (ret < 0) {
1992 err = ret;
1993 goto out;
1994 }
1995 leaf = path->nodes[0];
1996 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1997 }
1998#endif
1999 BUG_ON(item_size < sizeof(*ei));
2000 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2001 __run_delayed_extent_op(extent_op, leaf, ei);
2002
2003 btrfs_mark_buffer_dirty(leaf);
2004out:
2005 btrfs_free_path(path);
2006 return err;
2007}
2008
2009static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2010 struct btrfs_root *root,
2011 struct btrfs_delayed_ref_node *node,
2012 struct btrfs_delayed_extent_op *extent_op,
2013 int insert_reserved)
2014{
2015 int ret = 0;
2016 struct btrfs_delayed_tree_ref *ref;
2017 struct btrfs_key ins;
2018 u64 parent = 0;
2019 u64 ref_root = 0;
2020
2021 ins.objectid = node->bytenr;
2022 ins.offset = node->num_bytes;
2023 ins.type = BTRFS_EXTENT_ITEM_KEY;
2024
2025 ref = btrfs_delayed_node_to_tree_ref(node);
2026 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2027 parent = ref->parent;
2028 else
2029 ref_root = ref->root;
2030
2031 BUG_ON(node->ref_mod != 1);
2032 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2033 BUG_ON(!extent_op || !extent_op->update_flags ||
2034 !extent_op->update_key);
2035 ret = alloc_reserved_tree_block(trans, root,
2036 parent, ref_root,
2037 extent_op->flags_to_set,
2038 &extent_op->key,
2039 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002040 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2041 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2042 node->num_bytes, parent, ref_root,
2043 ref->level, 0, 1, extent_op);
2044 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2045 ret = __btrfs_free_extent(trans, root, node->bytenr,
2046 node->num_bytes, parent, ref_root,
2047 ref->level, 0, 1, extent_op);
2048 } else {
2049 BUG();
2050 }
2051 return ret;
2052}
2053
Chris Mason56bec292009-03-13 10:10:06 -04002054/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002055static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2056 struct btrfs_root *root,
2057 struct btrfs_delayed_ref_node *node,
2058 struct btrfs_delayed_extent_op *extent_op,
2059 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002060{
Josef Bacikeb099672009-02-12 09:27:38 -05002061 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002062 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002063 struct btrfs_delayed_ref_head *head;
2064 /*
2065 * we've hit the end of the chain and we were supposed
2066 * to insert this extent into the tree. But, it got
2067 * deleted before we ever needed to insert it, so all
2068 * we have to do is clean up the accounting
2069 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002070 BUG_ON(extent_op);
2071 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002072 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002073 btrfs_pin_extent(root, node->bytenr,
2074 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002075 if (head->is_data) {
2076 ret = btrfs_del_csums(trans, root,
2077 node->bytenr,
2078 node->num_bytes);
2079 BUG_ON(ret);
2080 }
Chris Mason56bec292009-03-13 10:10:06 -04002081 }
Chris Mason56bec292009-03-13 10:10:06 -04002082 mutex_unlock(&head->mutex);
2083 return 0;
2084 }
Josef Bacikeb099672009-02-12 09:27:38 -05002085
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2087 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2088 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2089 insert_reserved);
2090 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2091 node->type == BTRFS_SHARED_DATA_REF_KEY)
2092 ret = run_delayed_data_ref(trans, root, node, extent_op,
2093 insert_reserved);
2094 else
2095 BUG();
2096 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002097}
2098
Chris Mason56bec292009-03-13 10:10:06 -04002099static noinline struct btrfs_delayed_ref_node *
2100select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002101{
Chris Mason56bec292009-03-13 10:10:06 -04002102 struct rb_node *node;
2103 struct btrfs_delayed_ref_node *ref;
2104 int action = BTRFS_ADD_DELAYED_REF;
2105again:
2106 /*
2107 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2108 * this prevents ref count from going down to zero when
2109 * there still are pending delayed ref.
2110 */
2111 node = rb_prev(&head->node.rb_node);
2112 while (1) {
2113 if (!node)
2114 break;
2115 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2116 rb_node);
2117 if (ref->bytenr != head->node.bytenr)
2118 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002119 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002120 return ref;
2121 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002122 }
Chris Mason56bec292009-03-13 10:10:06 -04002123 if (action == BTRFS_ADD_DELAYED_REF) {
2124 action = BTRFS_DROP_DELAYED_REF;
2125 goto again;
2126 }
2127 return NULL;
2128}
2129
Chris Masonc3e69d52009-03-13 10:17:05 -04002130static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2131 struct btrfs_root *root,
2132 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002133{
Chris Mason56bec292009-03-13 10:10:06 -04002134 struct btrfs_delayed_ref_root *delayed_refs;
2135 struct btrfs_delayed_ref_node *ref;
2136 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002137 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002138 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002139 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002140 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002141
2142 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002143 while (1) {
2144 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002145 /* pick a new head ref from the cluster list */
2146 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002147 break;
Chris Mason56bec292009-03-13 10:10:06 -04002148
Chris Masonc3e69d52009-03-13 10:17:05 -04002149 locked_ref = list_entry(cluster->next,
2150 struct btrfs_delayed_ref_head, cluster);
2151
2152 /* grab the lock that says we are going to process
2153 * all the refs for this head */
2154 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2155
2156 /*
2157 * we may have dropped the spin lock to get the head
2158 * mutex lock, and that might have given someone else
2159 * time to free the head. If that's true, it has been
2160 * removed from our list and we can move on.
2161 */
2162 if (ret == -EAGAIN) {
2163 locked_ref = NULL;
2164 count++;
2165 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002166 }
2167 }
2168
2169 /*
2170 * record the must insert reserved flag before we
2171 * drop the spin lock.
2172 */
2173 must_insert_reserved = locked_ref->must_insert_reserved;
2174 locked_ref->must_insert_reserved = 0;
2175
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002176 extent_op = locked_ref->extent_op;
2177 locked_ref->extent_op = NULL;
2178
Chris Mason56bec292009-03-13 10:10:06 -04002179 /*
2180 * locked_ref is the head node, so we have to go one
2181 * node back for any delayed ref updates
2182 */
Chris Mason56bec292009-03-13 10:10:06 -04002183 ref = select_delayed_ref(locked_ref);
2184 if (!ref) {
2185 /* All delayed refs have been processed, Go ahead
2186 * and send the head node to run_one_delayed_ref,
2187 * so that any accounting fixes can happen
2188 */
2189 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002190
2191 if (extent_op && must_insert_reserved) {
2192 kfree(extent_op);
2193 extent_op = NULL;
2194 }
2195
2196 if (extent_op) {
2197 spin_unlock(&delayed_refs->lock);
2198
2199 ret = run_delayed_extent_op(trans, root,
2200 ref, extent_op);
2201 BUG_ON(ret);
2202 kfree(extent_op);
2203
2204 cond_resched();
2205 spin_lock(&delayed_refs->lock);
2206 continue;
2207 }
2208
Chris Masonc3e69d52009-03-13 10:17:05 -04002209 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002210 locked_ref = NULL;
2211 }
2212
2213 ref->in_tree = 0;
2214 rb_erase(&ref->rb_node, &delayed_refs->root);
2215 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002216
Chris Mason56bec292009-03-13 10:10:06 -04002217 spin_unlock(&delayed_refs->lock);
2218
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002219 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002220 must_insert_reserved);
2221 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002222
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002223 btrfs_put_delayed_ref(ref);
2224 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002225 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002226
Chris Mason1887be62009-03-13 10:11:24 -04002227 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002228 spin_lock(&delayed_refs->lock);
2229 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002230 return count;
2231}
2232
2233/*
2234 * this starts processing the delayed reference count updates and
2235 * extent insertions we have queued up so far. count can be
2236 * 0, which means to process everything in the tree at the start
2237 * of the run (but not newly added entries), or it can be some target
2238 * number you'd like to process.
2239 */
2240int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2241 struct btrfs_root *root, unsigned long count)
2242{
2243 struct rb_node *node;
2244 struct btrfs_delayed_ref_root *delayed_refs;
2245 struct btrfs_delayed_ref_node *ref;
2246 struct list_head cluster;
2247 int ret;
2248 int run_all = count == (unsigned long)-1;
2249 int run_most = 0;
2250
2251 if (root == root->fs_info->extent_root)
2252 root = root->fs_info->tree_root;
2253
2254 delayed_refs = &trans->transaction->delayed_refs;
2255 INIT_LIST_HEAD(&cluster);
2256again:
2257 spin_lock(&delayed_refs->lock);
2258 if (count == 0) {
2259 count = delayed_refs->num_entries * 2;
2260 run_most = 1;
2261 }
2262 while (1) {
2263 if (!(run_all || run_most) &&
2264 delayed_refs->num_heads_ready < 64)
2265 break;
2266
2267 /*
2268 * go find something we can process in the rbtree. We start at
2269 * the beginning of the tree, and then build a cluster
2270 * of refs to process starting at the first one we are able to
2271 * lock
2272 */
2273 ret = btrfs_find_ref_cluster(trans, &cluster,
2274 delayed_refs->run_delayed_start);
2275 if (ret)
2276 break;
2277
2278 ret = run_clustered_refs(trans, root, &cluster);
2279 BUG_ON(ret < 0);
2280
2281 count -= min_t(unsigned long, ret, count);
2282
2283 if (count == 0)
2284 break;
2285 }
2286
Chris Mason56bec292009-03-13 10:10:06 -04002287 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002288 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002289 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002290 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002291 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002292
2293 while (node) {
2294 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2295 rb_node);
2296 if (btrfs_delayed_ref_is_head(ref)) {
2297 struct btrfs_delayed_ref_head *head;
2298
2299 head = btrfs_delayed_node_to_head(ref);
2300 atomic_inc(&ref->refs);
2301
2302 spin_unlock(&delayed_refs->lock);
2303 mutex_lock(&head->mutex);
2304 mutex_unlock(&head->mutex);
2305
2306 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002307 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002308 goto again;
2309 }
2310 node = rb_next(node);
2311 }
2312 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002313 schedule_timeout(1);
2314 goto again;
2315 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002316out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002317 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002318 return 0;
2319}
2320
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002321int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2322 struct btrfs_root *root,
2323 u64 bytenr, u64 num_bytes, u64 flags,
2324 int is_data)
2325{
2326 struct btrfs_delayed_extent_op *extent_op;
2327 int ret;
2328
2329 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2330 if (!extent_op)
2331 return -ENOMEM;
2332
2333 extent_op->flags_to_set = flags;
2334 extent_op->update_flags = 1;
2335 extent_op->update_key = 0;
2336 extent_op->is_data = is_data ? 1 : 0;
2337
2338 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2339 if (ret)
2340 kfree(extent_op);
2341 return ret;
2342}
2343
2344static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2345 struct btrfs_root *root,
2346 struct btrfs_path *path,
2347 u64 objectid, u64 offset, u64 bytenr)
2348{
2349 struct btrfs_delayed_ref_head *head;
2350 struct btrfs_delayed_ref_node *ref;
2351 struct btrfs_delayed_data_ref *data_ref;
2352 struct btrfs_delayed_ref_root *delayed_refs;
2353 struct rb_node *node;
2354 int ret = 0;
2355
2356 ret = -ENOENT;
2357 delayed_refs = &trans->transaction->delayed_refs;
2358 spin_lock(&delayed_refs->lock);
2359 head = btrfs_find_delayed_ref_head(trans, bytenr);
2360 if (!head)
2361 goto out;
2362
2363 if (!mutex_trylock(&head->mutex)) {
2364 atomic_inc(&head->node.refs);
2365 spin_unlock(&delayed_refs->lock);
2366
2367 btrfs_release_path(root->fs_info->extent_root, path);
2368
2369 mutex_lock(&head->mutex);
2370 mutex_unlock(&head->mutex);
2371 btrfs_put_delayed_ref(&head->node);
2372 return -EAGAIN;
2373 }
2374
2375 node = rb_prev(&head->node.rb_node);
2376 if (!node)
2377 goto out_unlock;
2378
2379 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2380
2381 if (ref->bytenr != bytenr)
2382 goto out_unlock;
2383
2384 ret = 1;
2385 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2386 goto out_unlock;
2387
2388 data_ref = btrfs_delayed_node_to_data_ref(ref);
2389
2390 node = rb_prev(node);
2391 if (node) {
2392 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2393 if (ref->bytenr == bytenr)
2394 goto out_unlock;
2395 }
2396
2397 if (data_ref->root != root->root_key.objectid ||
2398 data_ref->objectid != objectid || data_ref->offset != offset)
2399 goto out_unlock;
2400
2401 ret = 0;
2402out_unlock:
2403 mutex_unlock(&head->mutex);
2404out:
2405 spin_unlock(&delayed_refs->lock);
2406 return ret;
2407}
2408
2409static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2410 struct btrfs_root *root,
2411 struct btrfs_path *path,
2412 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002413{
2414 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002415 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002416 struct btrfs_extent_data_ref *ref;
2417 struct btrfs_extent_inline_ref *iref;
2418 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002419 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002420 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002421 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002422
Chris Masonbe20aa92007-12-17 20:14:01 -05002423 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002424 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002425 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002426
Chris Masonbe20aa92007-12-17 20:14:01 -05002427 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2428 if (ret < 0)
2429 goto out;
2430 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002431
2432 ret = -ENOENT;
2433 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002434 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002435
Zheng Yan31840ae2008-09-23 13:14:14 -04002436 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002437 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002438 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002439
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002440 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002441 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002442
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002443 ret = 1;
2444 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2445#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2446 if (item_size < sizeof(*ei)) {
2447 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2448 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002449 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002450#endif
2451 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2452
2453 if (item_size != sizeof(*ei) +
2454 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2455 goto out;
2456
2457 if (btrfs_extent_generation(leaf, ei) <=
2458 btrfs_root_last_snapshot(&root->root_item))
2459 goto out;
2460
2461 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2462 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2463 BTRFS_EXTENT_DATA_REF_KEY)
2464 goto out;
2465
2466 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2467 if (btrfs_extent_refs(leaf, ei) !=
2468 btrfs_extent_data_ref_count(leaf, ref) ||
2469 btrfs_extent_data_ref_root(leaf, ref) !=
2470 root->root_key.objectid ||
2471 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2472 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2473 goto out;
2474
Yan Zhengf321e492008-07-30 09:26:11 -04002475 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002476out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002477 return ret;
2478}
2479
2480int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2481 struct btrfs_root *root,
2482 u64 objectid, u64 offset, u64 bytenr)
2483{
2484 struct btrfs_path *path;
2485 int ret;
2486 int ret2;
2487
2488 path = btrfs_alloc_path();
2489 if (!path)
2490 return -ENOENT;
2491
2492 do {
2493 ret = check_committed_ref(trans, root, path, objectid,
2494 offset, bytenr);
2495 if (ret && ret != -ENOENT)
2496 goto out;
2497
2498 ret2 = check_delayed_ref(trans, root, path, objectid,
2499 offset, bytenr);
2500 } while (ret2 == -EAGAIN);
2501
2502 if (ret2 && ret2 != -ENOENT) {
2503 ret = ret2;
2504 goto out;
2505 }
2506
2507 if (ret != -ENOENT || ret2 != -ENOENT)
2508 ret = 0;
2509out:
Yan Zhengf321e492008-07-30 09:26:11 -04002510 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002511 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2512 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002513 return ret;
2514}
2515
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002516#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002517int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2518 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002519{
Chris Mason5f39d392007-10-15 16:14:19 -04002520 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002521 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002522 u64 root_gen;
2523 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002524 int i;
Chris Masondb945352007-10-15 16:15:53 -04002525 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002526 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002527 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002528
Chris Mason3768f362007-03-13 16:47:54 -04002529 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002530 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002531
Zheng Yane4657682008-09-26 10:04:53 -04002532 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2533 shared = 0;
2534 root_gen = root->root_key.offset;
2535 } else {
2536 shared = 1;
2537 root_gen = trans->transid - 1;
2538 }
2539
Chris Masondb945352007-10-15 16:15:53 -04002540 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002541 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002542
Zheng Yan31840ae2008-09-23 13:14:14 -04002543 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002544 struct btrfs_leaf_ref *ref;
2545 struct btrfs_extent_info *info;
2546
Zheng Yan31840ae2008-09-23 13:14:14 -04002547 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002548 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002549 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002550 goto out;
2551 }
2552
Zheng Yane4657682008-09-26 10:04:53 -04002553 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002554 ref->bytenr = buf->start;
2555 ref->owner = btrfs_header_owner(buf);
2556 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002557 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002558 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002559
Zheng Yan31840ae2008-09-23 13:14:14 -04002560 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002561 u64 disk_bytenr;
2562 btrfs_item_key_to_cpu(buf, &key, i);
2563 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2564 continue;
2565 fi = btrfs_item_ptr(buf, i,
2566 struct btrfs_file_extent_item);
2567 if (btrfs_file_extent_type(buf, fi) ==
2568 BTRFS_FILE_EXTENT_INLINE)
2569 continue;
2570 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2571 if (disk_bytenr == 0)
2572 continue;
2573
2574 info->bytenr = disk_bytenr;
2575 info->num_bytes =
2576 btrfs_file_extent_disk_num_bytes(buf, fi);
2577 info->objectid = key.objectid;
2578 info->offset = key.offset;
2579 info++;
2580 }
2581
Zheng Yane4657682008-09-26 10:04:53 -04002582 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002583 if (ret == -EEXIST && shared) {
2584 struct btrfs_leaf_ref *old;
2585 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2586 BUG_ON(!old);
2587 btrfs_remove_leaf_ref(root, old);
2588 btrfs_free_leaf_ref(root, old);
2589 ret = btrfs_add_leaf_ref(root, ref, shared);
2590 }
Yan Zheng31153d82008-07-28 15:32:19 -04002591 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002592 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002593 }
2594out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002595 return ret;
2596}
2597
Chris Masonb7a9f292009-02-04 09:23:45 -05002598/* when a block goes through cow, we update the reference counts of
2599 * everything that block points to. The internal pointers of the block
2600 * can be in just about any order, and it is likely to have clusters of
2601 * things that are close together and clusters of things that are not.
2602 *
2603 * To help reduce the seeks that come with updating all of these reference
2604 * counts, sort them by byte number before actual updates are done.
2605 *
2606 * struct refsort is used to match byte number to slot in the btree block.
2607 * we sort based on the byte number and then use the slot to actually
2608 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002609 *
2610 * struct refsort is smaller than strcut btrfs_item and smaller than
2611 * struct btrfs_key_ptr. Since we're currently limited to the page size
2612 * for a btree block, there's no way for a kmalloc of refsorts for a
2613 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002614 */
2615struct refsort {
2616 u64 bytenr;
2617 u32 slot;
2618};
2619
2620/*
2621 * for passing into sort()
2622 */
2623static int refsort_cmp(const void *a_void, const void *b_void)
2624{
2625 const struct refsort *a = a_void;
2626 const struct refsort *b = b_void;
2627
2628 if (a->bytenr < b->bytenr)
2629 return -1;
2630 if (a->bytenr > b->bytenr)
2631 return 1;
2632 return 0;
2633}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002634#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002635
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002636static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002637 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002638 struct extent_buffer *buf,
2639 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002640{
2641 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002642 u64 num_bytes;
2643 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002644 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002645 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002646 struct btrfs_key key;
2647 struct btrfs_file_extent_item *fi;
2648 int i;
2649 int level;
2650 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002651 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002652 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002653
2654 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002655 nritems = btrfs_header_nritems(buf);
2656 level = btrfs_header_level(buf);
2657
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002658 if (!root->ref_cows && level == 0)
2659 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002660
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002661 if (inc)
2662 process_func = btrfs_inc_extent_ref;
2663 else
2664 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002665
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002666 if (full_backref)
2667 parent = buf->start;
2668 else
2669 parent = 0;
2670
Zheng Yan31840ae2008-09-23 13:14:14 -04002671 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002672 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002673 btrfs_item_key_to_cpu(buf, &key, i);
2674 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002675 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002676 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002677 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002678 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002679 BTRFS_FILE_EXTENT_INLINE)
2680 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002681 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2682 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002683 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002684
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002685 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2686 key.offset -= btrfs_file_extent_offset(buf, fi);
2687 ret = process_func(trans, root, bytenr, num_bytes,
2688 parent, ref_root, key.objectid,
2689 key.offset);
2690 if (ret)
2691 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002692 } else {
2693 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002694 num_bytes = btrfs_level_size(root, level - 1);
2695 ret = process_func(trans, root, bytenr, num_bytes,
2696 parent, ref_root, level - 1, 0);
2697 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002698 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002699 }
2700 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002701 return 0;
2702fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002703 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002704 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002705}
2706
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002707int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2708 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002709{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002710 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2711}
Zheng Yan31840ae2008-09-23 13:14:14 -04002712
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002713int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2714 struct extent_buffer *buf, int full_backref)
2715{
2716 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002717}
2718
Chris Mason9078a3e2007-04-26 16:46:15 -04002719static int write_one_cache_group(struct btrfs_trans_handle *trans,
2720 struct btrfs_root *root,
2721 struct btrfs_path *path,
2722 struct btrfs_block_group_cache *cache)
2723{
2724 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002725 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002726 unsigned long bi;
2727 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002728
Chris Mason9078a3e2007-04-26 16:46:15 -04002729 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002730 if (ret < 0)
2731 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002732 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002733
2734 leaf = path->nodes[0];
2735 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2736 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2737 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002738 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002739fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002740 if (ret)
2741 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002742 return 0;
2743
2744}
2745
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002746static struct btrfs_block_group_cache *
2747next_block_group(struct btrfs_root *root,
2748 struct btrfs_block_group_cache *cache)
2749{
2750 struct rb_node *node;
2751 spin_lock(&root->fs_info->block_group_cache_lock);
2752 node = rb_next(&cache->cache_node);
2753 btrfs_put_block_group(cache);
2754 if (node) {
2755 cache = rb_entry(node, struct btrfs_block_group_cache,
2756 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002757 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002758 } else
2759 cache = NULL;
2760 spin_unlock(&root->fs_info->block_group_cache_lock);
2761 return cache;
2762}
2763
Josef Bacik0af3d002010-06-21 14:48:16 -04002764static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2765 struct btrfs_trans_handle *trans,
2766 struct btrfs_path *path)
2767{
2768 struct btrfs_root *root = block_group->fs_info->tree_root;
2769 struct inode *inode = NULL;
2770 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002771 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002772 int num_pages = 0;
2773 int retries = 0;
2774 int ret = 0;
2775
2776 /*
2777 * If this block group is smaller than 100 megs don't bother caching the
2778 * block group.
2779 */
2780 if (block_group->key.offset < (100 * 1024 * 1024)) {
2781 spin_lock(&block_group->lock);
2782 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2783 spin_unlock(&block_group->lock);
2784 return 0;
2785 }
2786
2787again:
2788 inode = lookup_free_space_inode(root, block_group, path);
2789 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2790 ret = PTR_ERR(inode);
2791 btrfs_release_path(root, path);
2792 goto out;
2793 }
2794
2795 if (IS_ERR(inode)) {
2796 BUG_ON(retries);
2797 retries++;
2798
2799 if (block_group->ro)
2800 goto out_free;
2801
2802 ret = create_free_space_inode(root, trans, block_group, path);
2803 if (ret)
2804 goto out_free;
2805 goto again;
2806 }
2807
2808 /*
2809 * We want to set the generation to 0, that way if anything goes wrong
2810 * from here on out we know not to trust this cache when we load up next
2811 * time.
2812 */
2813 BTRFS_I(inode)->generation = 0;
2814 ret = btrfs_update_inode(trans, root, inode);
2815 WARN_ON(ret);
2816
2817 if (i_size_read(inode) > 0) {
2818 ret = btrfs_truncate_free_space_cache(root, trans, path,
2819 inode);
2820 if (ret)
2821 goto out_put;
2822 }
2823
2824 spin_lock(&block_group->lock);
2825 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002826 /* We're not cached, don't bother trying to write stuff out */
2827 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002828 spin_unlock(&block_group->lock);
2829 goto out_put;
2830 }
2831 spin_unlock(&block_group->lock);
2832
2833 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2834 if (!num_pages)
2835 num_pages = 1;
2836
2837 /*
2838 * Just to make absolutely sure we have enough space, we're going to
2839 * preallocate 12 pages worth of space for each block group. In
2840 * practice we ought to use at most 8, but we need extra space so we can
2841 * add our header and have a terminator between the extents and the
2842 * bitmaps.
2843 */
2844 num_pages *= 16;
2845 num_pages *= PAGE_CACHE_SIZE;
2846
2847 ret = btrfs_check_data_free_space(inode, num_pages);
2848 if (ret)
2849 goto out_put;
2850
2851 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2852 num_pages, num_pages,
2853 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002854 if (!ret)
2855 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002856 btrfs_free_reserved_data_space(inode, num_pages);
2857out_put:
2858 iput(inode);
2859out_free:
2860 btrfs_release_path(root, path);
2861out:
2862 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002863 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002864 spin_unlock(&block_group->lock);
2865
2866 return ret;
2867}
2868
Chris Mason96b51792007-10-15 16:15:19 -04002869int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2870 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002871{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002872 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002873 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002874 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002875 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002876
2877 path = btrfs_alloc_path();
2878 if (!path)
2879 return -ENOMEM;
2880
Josef Bacik0af3d002010-06-21 14:48:16 -04002881again:
2882 while (1) {
2883 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2884 while (cache) {
2885 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2886 break;
2887 cache = next_block_group(root, cache);
2888 }
2889 if (!cache) {
2890 if (last == 0)
2891 break;
2892 last = 0;
2893 continue;
2894 }
2895 err = cache_save_setup(cache, trans, path);
2896 last = cache->key.objectid + cache->key.offset;
2897 btrfs_put_block_group(cache);
2898 }
2899
Chris Masond3977122009-01-05 21:25:51 -05002900 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002901 if (last == 0) {
2902 err = btrfs_run_delayed_refs(trans, root,
2903 (unsigned long)-1);
2904 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002905 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002906
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002907 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2908 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002909 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2910 btrfs_put_block_group(cache);
2911 goto again;
2912 }
2913
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002914 if (cache->dirty)
2915 break;
2916 cache = next_block_group(root, cache);
2917 }
2918 if (!cache) {
2919 if (last == 0)
2920 break;
2921 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002922 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002923 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002924
Josef Bacik0cb59c92010-07-02 12:14:14 -04002925 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2926 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002927 cache->dirty = 0;
2928 last = cache->key.objectid + cache->key.offset;
2929
2930 err = write_one_cache_group(trans, root, path, cache);
2931 BUG_ON(err);
2932 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002933 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002934
Josef Bacik0cb59c92010-07-02 12:14:14 -04002935 while (1) {
2936 /*
2937 * I don't think this is needed since we're just marking our
2938 * preallocated extent as written, but just in case it can't
2939 * hurt.
2940 */
2941 if (last == 0) {
2942 err = btrfs_run_delayed_refs(trans, root,
2943 (unsigned long)-1);
2944 BUG_ON(err);
2945 }
2946
2947 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2948 while (cache) {
2949 /*
2950 * Really this shouldn't happen, but it could if we
2951 * couldn't write the entire preallocated extent and
2952 * splitting the extent resulted in a new block.
2953 */
2954 if (cache->dirty) {
2955 btrfs_put_block_group(cache);
2956 goto again;
2957 }
2958 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2959 break;
2960 cache = next_block_group(root, cache);
2961 }
2962 if (!cache) {
2963 if (last == 0)
2964 break;
2965 last = 0;
2966 continue;
2967 }
2968
2969 btrfs_write_out_cache(root, trans, cache, path);
2970
2971 /*
2972 * If we didn't have an error then the cache state is still
2973 * NEED_WRITE, so we can set it to WRITTEN.
2974 */
2975 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2976 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2977 last = cache->key.objectid + cache->key.offset;
2978 btrfs_put_block_group(cache);
2979 }
2980
Chris Mason9078a3e2007-04-26 16:46:15 -04002981 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002982 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002983}
2984
Yan Zhengd2fb3432008-12-11 16:30:39 -05002985int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2986{
2987 struct btrfs_block_group_cache *block_group;
2988 int readonly = 0;
2989
2990 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2991 if (!block_group || block_group->ro)
2992 readonly = 1;
2993 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002994 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002995 return readonly;
2996}
2997
Chris Mason593060d2008-03-25 16:50:33 -04002998static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2999 u64 total_bytes, u64 bytes_used,
3000 struct btrfs_space_info **space_info)
3001{
3002 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003003 int i;
3004 int factor;
3005
3006 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3007 BTRFS_BLOCK_GROUP_RAID10))
3008 factor = 2;
3009 else
3010 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003011
3012 found = __find_space_info(info, flags);
3013 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003014 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003015 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003016 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003017 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003018 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003019 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003020 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003021 *space_info = found;
3022 return 0;
3023 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003024 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003025 if (!found)
3026 return -ENOMEM;
3027
Yan, Zhengb742bb82010-05-16 10:46:24 -04003028 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3029 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003030 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003031 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04003032 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
3033 BTRFS_BLOCK_GROUP_SYSTEM |
3034 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04003035 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003036 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003037 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003038 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003039 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003040 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003041 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003042 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003043 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003044 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003045 found->chunk_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003046 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003047 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04003048 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04003049 return 0;
3050}
3051
Chris Mason8790d502008-04-03 16:29:03 -04003052static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3053{
3054 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04003055 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04003056 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04003057 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04003058 if (extra_flags) {
3059 if (flags & BTRFS_BLOCK_GROUP_DATA)
3060 fs_info->avail_data_alloc_bits |= extra_flags;
3061 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3062 fs_info->avail_metadata_alloc_bits |= extra_flags;
3063 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3064 fs_info->avail_system_alloc_bits |= extra_flags;
3065 }
3066}
Chris Mason593060d2008-03-25 16:50:33 -04003067
Yan Zheng2b820322008-11-17 21:11:30 -05003068u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003069{
Chris Masoncd02dca2010-12-13 14:56:23 -05003070 /*
3071 * we add in the count of missing devices because we want
3072 * to make sure that any RAID levels on a degraded FS
3073 * continue to be honored.
3074 */
3075 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3076 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04003077
3078 if (num_devices == 1)
3079 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3080 if (num_devices < 4)
3081 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3082
Chris Masonec44a352008-04-28 15:29:52 -04003083 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3084 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003085 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003086 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003087 }
Chris Masonec44a352008-04-28 15:29:52 -04003088
3089 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003090 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003091 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003092 }
Chris Masonec44a352008-04-28 15:29:52 -04003093
3094 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3095 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3096 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3097 (flags & BTRFS_BLOCK_GROUP_DUP)))
3098 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3099 return flags;
3100}
3101
Yan, Zhengb742bb82010-05-16 10:46:24 -04003102static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003103{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003104 if (flags & BTRFS_BLOCK_GROUP_DATA)
3105 flags |= root->fs_info->avail_data_alloc_bits &
3106 root->fs_info->data_alloc_profile;
3107 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3108 flags |= root->fs_info->avail_system_alloc_bits &
3109 root->fs_info->system_alloc_profile;
3110 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3111 flags |= root->fs_info->avail_metadata_alloc_bits &
3112 root->fs_info->metadata_alloc_profile;
3113 return btrfs_reduce_alloc_profile(root, flags);
3114}
Josef Bacik6a632092009-02-20 11:00:09 -05003115
Miao Xie6d07bce2011-01-05 10:07:31 +00003116u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003117{
3118 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003119
Yan, Zhengb742bb82010-05-16 10:46:24 -04003120 if (data)
3121 flags = BTRFS_BLOCK_GROUP_DATA;
3122 else if (root == root->fs_info->chunk_root)
3123 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3124 else
3125 flags = BTRFS_BLOCK_GROUP_METADATA;
3126
3127 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003128}
3129
3130void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3131{
Josef Bacik6a632092009-02-20 11:00:09 -05003132 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003133 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003134}
3135
3136/*
3137 * This will check the space that the inode allocates from to make sure we have
3138 * enough space for bytes.
3139 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003140int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003141{
3142 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003143 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003144 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003145 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003146
3147 /* make sure bytes are sectorsize aligned */
3148 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3149
Josef Bacik0af3d002010-06-21 14:48:16 -04003150 if (root == root->fs_info->tree_root) {
3151 alloc_chunk = 0;
3152 committed = 1;
3153 }
3154
Josef Bacik6a632092009-02-20 11:00:09 -05003155 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003156 if (!data_sinfo)
3157 goto alloc;
3158
Josef Bacik6a632092009-02-20 11:00:09 -05003159again:
3160 /* make sure we have enough space to handle the data first */
3161 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003162 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3163 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3164 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003165
3166 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003167 struct btrfs_trans_handle *trans;
3168
Josef Bacik6a632092009-02-20 11:00:09 -05003169 /*
3170 * if we don't have enough free bytes in this space then we need
3171 * to alloc a new chunk.
3172 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003173 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003174 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003175
Chris Mason0e4f8f82011-04-15 16:05:44 -04003176 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003177 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003178alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003179 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003180 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003181 if (IS_ERR(trans))
3182 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003183
3184 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3185 bytes + 2 * 1024 * 1024,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003186 alloc_target,
3187 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003188 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003189 if (ret < 0) {
3190 if (ret != -ENOSPC)
3191 return ret;
3192 else
3193 goto commit_trans;
3194 }
Chris Mason33b4d472009-09-22 14:45:50 -04003195
3196 if (!data_sinfo) {
3197 btrfs_set_inode_space_info(root, inode);
3198 data_sinfo = BTRFS_I(inode)->space_info;
3199 }
Josef Bacik6a632092009-02-20 11:00:09 -05003200 goto again;
3201 }
3202 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003203
3204 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003205commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003206 if (!committed &&
3207 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003208 committed = 1;
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003209 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003210 if (IS_ERR(trans))
3211 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003212 ret = btrfs_commit_transaction(trans, root);
3213 if (ret)
3214 return ret;
3215 goto again;
3216 }
3217
Chris Mason933b5852010-05-26 11:31:00 -04003218#if 0 /* I hope we never need this code again, just in case */
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003219 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3220 "%llu bytes_reserved, " "%llu bytes_pinned, "
3221 "%llu bytes_readonly, %llu may use %llu total\n",
3222 (unsigned long long)bytes,
Joel Becker21380932009-04-21 12:38:29 -07003223 (unsigned long long)data_sinfo->bytes_used,
3224 (unsigned long long)data_sinfo->bytes_reserved,
3225 (unsigned long long)data_sinfo->bytes_pinned,
3226 (unsigned long long)data_sinfo->bytes_readonly,
3227 (unsigned long long)data_sinfo->bytes_may_use,
3228 (unsigned long long)data_sinfo->total_bytes);
Chris Mason933b5852010-05-26 11:31:00 -04003229#endif
Josef Bacik6a632092009-02-20 11:00:09 -05003230 return -ENOSPC;
3231 }
3232 data_sinfo->bytes_may_use += bytes;
3233 BTRFS_I(inode)->reserved_bytes += bytes;
3234 spin_unlock(&data_sinfo->lock);
3235
Josef Bacik9ed74f22009-09-11 16:12:44 -04003236 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003237}
3238
3239/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003240 * called when we are clearing an delalloc extent from the
3241 * inode's io_tree or there was an error for whatever reason
3242 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003243 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003244void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003245{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003246 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003247 struct btrfs_space_info *data_sinfo;
3248
3249 /* make sure bytes are sectorsize aligned */
3250 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3251
3252 data_sinfo = BTRFS_I(inode)->space_info;
3253 spin_lock(&data_sinfo->lock);
3254 data_sinfo->bytes_may_use -= bytes;
3255 BTRFS_I(inode)->reserved_bytes -= bytes;
3256 spin_unlock(&data_sinfo->lock);
3257}
3258
Josef Bacik97e728d2009-04-21 17:40:57 -04003259static void force_metadata_allocation(struct btrfs_fs_info *info)
3260{
3261 struct list_head *head = &info->space_info;
3262 struct btrfs_space_info *found;
3263
3264 rcu_read_lock();
3265 list_for_each_entry_rcu(found, head, list) {
3266 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003267 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003268 }
3269 rcu_read_unlock();
3270}
3271
Chris Masone5bc2452010-10-26 13:37:56 -04003272static int should_alloc_chunk(struct btrfs_root *root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003273 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3274 int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003275{
3276 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003277 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003278 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003279
Chris Mason0e4f8f82011-04-15 16:05:44 -04003280 if (force == CHUNK_ALLOC_FORCE)
3281 return 1;
3282
3283 /*
3284 * in limited mode, we want to have some free space up to
3285 * about 1% of the FS size.
3286 */
3287 if (force == CHUNK_ALLOC_LIMITED) {
3288 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3289 thresh = max_t(u64, 64 * 1024 * 1024,
3290 div_factor_fine(thresh, 1));
3291
3292 if (num_bytes - num_allocated < thresh)
3293 return 1;
3294 }
3295
3296 /*
3297 * we have two similar checks here, one based on percentage
3298 * and once based on a hard number of 256MB. The idea
3299 * is that if we have a good amount of free
3300 * room, don't allocate a chunk. A good mount is
3301 * less than 80% utilized of the chunks we have allocated,
3302 * or more than 256MB free
3303 */
3304 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003305 return 0;
3306
Chris Mason0e4f8f82011-04-15 16:05:44 -04003307 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
Yan, Zheng424499d2010-05-16 10:46:25 -04003308 return 0;
3309
Chris Masone5bc2452010-10-26 13:37:56 -04003310 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003311
3312 /* 256MB or 5% of the FS */
Chris Masone5bc2452010-10-26 13:37:56 -04003313 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3314
3315 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003316 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003317 return 1;
3318}
3319
Chris Mason6324fbf2008-03-24 15:01:59 -04003320static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3321 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003322 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003323{
3324 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003325 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003326 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003327 int ret = 0;
3328
Yan Zheng2b820322008-11-17 21:11:30 -05003329 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003330
Chris Mason6324fbf2008-03-24 15:01:59 -04003331 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003332 if (!space_info) {
3333 ret = update_space_info(extent_root->fs_info, flags,
3334 0, 0, &space_info);
3335 BUG_ON(ret);
3336 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003337 BUG_ON(!space_info);
3338
Josef Bacik6d741192011-04-11 20:20:11 -04003339again:
Josef Bacik25179202008-10-29 14:49:05 -04003340 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003341 if (space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003342 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003343 if (space_info->full) {
3344 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003345 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003346 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003347
Chris Mason0e4f8f82011-04-15 16:05:44 -04003348 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003349 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003350 return 0;
3351 } else if (space_info->chunk_alloc) {
3352 wait_for_alloc = 1;
3353 } else {
3354 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003355 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003356
Josef Bacik25179202008-10-29 14:49:05 -04003357 spin_unlock(&space_info->lock);
3358
Josef Bacik6d741192011-04-11 20:20:11 -04003359 mutex_lock(&fs_info->chunk_mutex);
3360
3361 /*
3362 * The chunk_mutex is held throughout the entirety of a chunk
3363 * allocation, so once we've acquired the chunk_mutex we know that the
3364 * other guy is done and we need to recheck and see if we should
3365 * allocate.
3366 */
3367 if (wait_for_alloc) {
3368 mutex_unlock(&fs_info->chunk_mutex);
3369 wait_for_alloc = 0;
3370 goto again;
3371 }
3372
Josef Bacik97e728d2009-04-21 17:40:57 -04003373 /*
Josef Bacik67377732010-09-16 16:19:09 -04003374 * If we have mixed data/metadata chunks we want to make sure we keep
3375 * allocating mixed chunks instead of individual chunks.
3376 */
3377 if (btrfs_mixed_space_info(space_info))
3378 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3379
3380 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003381 * if we're doing a data chunk, go ahead and make sure that
3382 * we keep a reasonable number of metadata chunks allocated in the
3383 * FS as well.
3384 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003385 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003386 fs_info->data_chunk_allocations++;
3387 if (!(fs_info->data_chunk_allocations %
3388 fs_info->metadata_ratio))
3389 force_metadata_allocation(fs_info);
3390 }
3391
Yan Zheng2b820322008-11-17 21:11:30 -05003392 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003393 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003394 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003395 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003396 else
3397 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003398
Chris Mason0e4f8f82011-04-15 16:05:44 -04003399 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003400 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003401 spin_unlock(&space_info->lock);
Yan Zhengc146afa2008-11-12 14:34:12 -05003402 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003403 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003404}
3405
Yan, Zheng5da9d012010-05-16 10:46:25 -04003406/*
3407 * shrink metadata reservation for delalloc
3408 */
3409static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003410 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003411{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003412 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003413 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003414 u64 reserved;
3415 u64 max_reclaim;
3416 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003417 long time_left;
Chris Masonbf9022e2010-10-26 13:40:45 -04003418 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003419 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003420 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003421
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003422 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003423 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003424
3425 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003426 reserved = space_info->bytes_reserved;
Chris Mason36e39c42011-03-12 07:08:42 -05003427 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003428
3429 if (reserved == 0)
3430 return 0;
3431
3432 max_reclaim = min(reserved, to_reclaim);
3433
Josef Bacikb1953bc2011-01-21 21:10:01 +00003434 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003435 /* have the flusher threads jump in and do some IO */
3436 smp_mb();
3437 nr_pages = min_t(unsigned long, nr_pages,
3438 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3439 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003440
Josef Bacik0019f102010-10-15 15:18:40 -04003441 spin_lock(&space_info->lock);
Chris Mason36e39c42011-03-12 07:08:42 -05003442 if (reserved > space_info->bytes_reserved)
Josef Bacik0019f102010-10-15 15:18:40 -04003443 reclaimed += reserved - space_info->bytes_reserved;
3444 reserved = space_info->bytes_reserved;
3445 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003446
Chris Mason36e39c42011-03-12 07:08:42 -05003447 loops++;
3448
Yan, Zheng5da9d012010-05-16 10:46:25 -04003449 if (reserved == 0 || reclaimed >= max_reclaim)
3450 break;
3451
3452 if (trans && trans->transaction->blocked)
3453 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003454
Chris Mason36e39c42011-03-12 07:08:42 -05003455 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003456
3457 /* We were interrupted, exit */
3458 if (time_left)
3459 break;
3460
Chris Mason36e39c42011-03-12 07:08:42 -05003461 /* we've kicked the IO a few times, if anything has been freed,
3462 * exit. There is no sense in looping here for a long time
3463 * when we really need to commit the transaction, or there are
3464 * just too many writers without enough free space
3465 */
3466
3467 if (loops > 3) {
3468 smp_mb();
3469 if (progress != space_info->reservation_progress)
3470 break;
3471 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003472
Yan, Zheng5da9d012010-05-16 10:46:25 -04003473 }
3474 return reclaimed >= to_reclaim;
3475}
3476
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003477/*
3478 * Retries tells us how many times we've called reserve_metadata_bytes. The
3479 * idea is if this is the first call (retries == 0) then we will add to our
3480 * reserved count if we can't make the allocation in order to hold our place
3481 * while we go and try and free up space. That way for retries > 1 we don't try
3482 * and add space, we just check to see if the amount of unused space is >= the
3483 * total space, meaning that our reservation is valid.
3484 *
3485 * However if we don't intend to retry this reservation, pass -1 as retries so
3486 * that it short circuits this logic.
3487 */
3488static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3489 struct btrfs_root *root,
3490 struct btrfs_block_rsv *block_rsv,
3491 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003492{
3493 struct btrfs_space_info *space_info = block_rsv->space_info;
3494 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003495 u64 num_bytes = orig_bytes;
3496 int retries = 0;
3497 int ret = 0;
3498 bool reserved = false;
Josef Bacik38227932010-10-26 12:52:53 -04003499 bool committed = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003500
3501again:
3502 ret = -ENOSPC;
3503 if (reserved)
3504 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003505
3506 spin_lock(&space_info->lock);
3507 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003508 space_info->bytes_pinned + space_info->bytes_readonly +
3509 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003510
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003511 /*
3512 * The idea here is that we've not already over-reserved the block group
3513 * then we can go ahead and save our reservation first and then start
3514 * flushing if we need to. Otherwise if we've already overcommitted
3515 * lets start flushing stuff first and then come back and try to make
3516 * our reservation.
3517 */
3518 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003519 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003520 if (unused >= num_bytes) {
3521 if (!reserved)
3522 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003523 ret = 0;
3524 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003525 /*
3526 * Ok set num_bytes to orig_bytes since we aren't
3527 * overocmmitted, this way we only try and reclaim what
3528 * we need.
3529 */
3530 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003531 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003532 } else {
3533 /*
3534 * Ok we're over committed, set num_bytes to the overcommitted
3535 * amount plus the amount of bytes that we need for this
3536 * reservation.
3537 */
3538 num_bytes = unused - space_info->total_bytes +
3539 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003540 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003541
3542 /*
3543 * Couldn't make our reservation, save our place so while we're trying
3544 * to reclaim space we can actually use it instead of somebody else
3545 * stealing it from us.
3546 */
3547 if (ret && !reserved) {
3548 space_info->bytes_reserved += orig_bytes;
3549 reserved = true;
3550 }
3551
Yan, Zhengf0486c62010-05-16 10:46:25 -04003552 spin_unlock(&space_info->lock);
3553
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003554 if (!ret)
3555 return 0;
3556
3557 if (!flush)
3558 goto out;
3559
3560 /*
3561 * We do synchronous shrinking since we don't actually unreserve
3562 * metadata until after the IO is completed.
3563 */
3564 ret = shrink_delalloc(trans, root, num_bytes, 1);
3565 if (ret > 0)
3566 return 0;
3567 else if (ret < 0)
3568 goto out;
3569
3570 /*
3571 * So if we were overcommitted it's possible that somebody else flushed
3572 * out enough space and we simply didn't have enough space to reclaim,
3573 * so go back around and try again.
3574 */
3575 if (retries < 2) {
3576 retries++;
3577 goto again;
3578 }
3579
3580 spin_lock(&space_info->lock);
3581 /*
3582 * Not enough space to be reclaimed, don't bother committing the
3583 * transaction.
3584 */
3585 if (space_info->bytes_pinned < orig_bytes)
3586 ret = -ENOSPC;
3587 spin_unlock(&space_info->lock);
3588 if (ret)
3589 goto out;
3590
3591 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003592 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003593 goto out;
3594
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003595 ret = -ENOSPC;
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003596 trans = btrfs_join_transaction(root);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003597 if (IS_ERR(trans))
3598 goto out;
3599 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003600 if (!ret) {
3601 trans = NULL;
3602 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003603 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003604 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003605
3606out:
3607 if (reserved) {
3608 spin_lock(&space_info->lock);
3609 space_info->bytes_reserved -= orig_bytes;
3610 spin_unlock(&space_info->lock);
3611 }
3612
Yan, Zhengf0486c62010-05-16 10:46:25 -04003613 return ret;
3614}
3615
3616static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3617 struct btrfs_root *root)
3618{
3619 struct btrfs_block_rsv *block_rsv;
3620 if (root->ref_cows)
3621 block_rsv = trans->block_rsv;
3622 else
3623 block_rsv = root->block_rsv;
3624
3625 if (!block_rsv)
3626 block_rsv = &root->fs_info->empty_block_rsv;
3627
3628 return block_rsv;
3629}
3630
3631static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3632 u64 num_bytes)
3633{
3634 int ret = -ENOSPC;
3635 spin_lock(&block_rsv->lock);
3636 if (block_rsv->reserved >= num_bytes) {
3637 block_rsv->reserved -= num_bytes;
3638 if (block_rsv->reserved < block_rsv->size)
3639 block_rsv->full = 0;
3640 ret = 0;
3641 }
3642 spin_unlock(&block_rsv->lock);
3643 return ret;
3644}
3645
3646static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3647 u64 num_bytes, int update_size)
3648{
3649 spin_lock(&block_rsv->lock);
3650 block_rsv->reserved += num_bytes;
3651 if (update_size)
3652 block_rsv->size += num_bytes;
3653 else if (block_rsv->reserved >= block_rsv->size)
3654 block_rsv->full = 1;
3655 spin_unlock(&block_rsv->lock);
3656}
3657
3658void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3659 struct btrfs_block_rsv *dest, u64 num_bytes)
3660{
3661 struct btrfs_space_info *space_info = block_rsv->space_info;
3662
3663 spin_lock(&block_rsv->lock);
3664 if (num_bytes == (u64)-1)
3665 num_bytes = block_rsv->size;
3666 block_rsv->size -= num_bytes;
3667 if (block_rsv->reserved >= block_rsv->size) {
3668 num_bytes = block_rsv->reserved - block_rsv->size;
3669 block_rsv->reserved = block_rsv->size;
3670 block_rsv->full = 1;
3671 } else {
3672 num_bytes = 0;
3673 }
3674 spin_unlock(&block_rsv->lock);
3675
3676 if (num_bytes > 0) {
3677 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003678 spin_lock(&dest->lock);
3679 if (!dest->full) {
3680 u64 bytes_to_add;
3681
3682 bytes_to_add = dest->size - dest->reserved;
3683 bytes_to_add = min(num_bytes, bytes_to_add);
3684 dest->reserved += bytes_to_add;
3685 if (dest->reserved >= dest->size)
3686 dest->full = 1;
3687 num_bytes -= bytes_to_add;
3688 }
3689 spin_unlock(&dest->lock);
3690 }
3691 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003692 spin_lock(&space_info->lock);
3693 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003694 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003695 spin_unlock(&space_info->lock);
3696 }
3697 }
3698}
3699
3700static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3701 struct btrfs_block_rsv *dst, u64 num_bytes)
3702{
3703 int ret;
3704
3705 ret = block_rsv_use_bytes(src, num_bytes);
3706 if (ret)
3707 return ret;
3708
3709 block_rsv_add_bytes(dst, num_bytes, 1);
3710 return 0;
3711}
3712
3713void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3714{
3715 memset(rsv, 0, sizeof(*rsv));
3716 spin_lock_init(&rsv->lock);
3717 atomic_set(&rsv->usage, 1);
3718 rsv->priority = 6;
3719 INIT_LIST_HEAD(&rsv->list);
3720}
3721
3722struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3723{
3724 struct btrfs_block_rsv *block_rsv;
3725 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003726
3727 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3728 if (!block_rsv)
3729 return NULL;
3730
3731 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003732 block_rsv->space_info = __find_space_info(fs_info,
3733 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003734 return block_rsv;
3735}
3736
3737void btrfs_free_block_rsv(struct btrfs_root *root,
3738 struct btrfs_block_rsv *rsv)
3739{
3740 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3741 btrfs_block_rsv_release(root, rsv, (u64)-1);
3742 if (!rsv->durable)
3743 kfree(rsv);
3744 }
3745}
3746
3747/*
3748 * make the block_rsv struct be able to capture freed space.
3749 * the captured space will re-add to the the block_rsv struct
3750 * after transaction commit
3751 */
3752void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3753 struct btrfs_block_rsv *block_rsv)
3754{
3755 block_rsv->durable = 1;
3756 mutex_lock(&fs_info->durable_block_rsv_mutex);
3757 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3758 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3759}
3760
3761int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3762 struct btrfs_root *root,
3763 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003764 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003765{
3766 int ret;
3767
3768 if (num_bytes == 0)
3769 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003770
3771 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003772 if (!ret) {
3773 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3774 return 0;
3775 }
3776
Yan, Zhengf0486c62010-05-16 10:46:25 -04003777 return ret;
3778}
3779
3780int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3781 struct btrfs_root *root,
3782 struct btrfs_block_rsv *block_rsv,
3783 u64 min_reserved, int min_factor)
3784{
3785 u64 num_bytes = 0;
3786 int commit_trans = 0;
3787 int ret = -ENOSPC;
3788
3789 if (!block_rsv)
3790 return 0;
3791
3792 spin_lock(&block_rsv->lock);
3793 if (min_factor > 0)
3794 num_bytes = div_factor(block_rsv->size, min_factor);
3795 if (min_reserved > num_bytes)
3796 num_bytes = min_reserved;
3797
3798 if (block_rsv->reserved >= num_bytes) {
3799 ret = 0;
3800 } else {
3801 num_bytes -= block_rsv->reserved;
3802 if (block_rsv->durable &&
3803 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3804 commit_trans = 1;
3805 }
3806 spin_unlock(&block_rsv->lock);
3807 if (!ret)
3808 return 0;
3809
3810 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003811 ret = reserve_metadata_bytes(trans, root, block_rsv,
3812 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003813 if (!ret) {
3814 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3815 return 0;
3816 }
3817 }
3818
3819 if (commit_trans) {
3820 if (trans)
3821 return -EAGAIN;
3822
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003823 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003824 BUG_ON(IS_ERR(trans));
3825 ret = btrfs_commit_transaction(trans, root);
3826 return 0;
3827 }
3828
Yan, Zhengf0486c62010-05-16 10:46:25 -04003829 return -ENOSPC;
3830}
3831
3832int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3833 struct btrfs_block_rsv *dst_rsv,
3834 u64 num_bytes)
3835{
3836 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3837}
3838
3839void btrfs_block_rsv_release(struct btrfs_root *root,
3840 struct btrfs_block_rsv *block_rsv,
3841 u64 num_bytes)
3842{
3843 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3844 if (global_rsv->full || global_rsv == block_rsv ||
3845 block_rsv->space_info != global_rsv->space_info)
3846 global_rsv = NULL;
3847 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3848}
3849
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003850/*
3851 * helper to calculate size of global block reservation.
3852 * the desired value is sum of space used by extent tree,
3853 * checksum tree and root tree
3854 */
3855static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3856{
3857 struct btrfs_space_info *sinfo;
3858 u64 num_bytes;
3859 u64 meta_used;
3860 u64 data_used;
3861 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3862#if 0
3863 /*
3864 * per tree used space accounting can be inaccuracy, so we
3865 * can't rely on it.
3866 */
3867 spin_lock(&fs_info->extent_root->accounting_lock);
3868 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3869 spin_unlock(&fs_info->extent_root->accounting_lock);
3870
3871 spin_lock(&fs_info->csum_root->accounting_lock);
3872 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3873 spin_unlock(&fs_info->csum_root->accounting_lock);
3874
3875 spin_lock(&fs_info->tree_root->accounting_lock);
3876 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3877 spin_unlock(&fs_info->tree_root->accounting_lock);
3878#endif
3879 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3880 spin_lock(&sinfo->lock);
3881 data_used = sinfo->bytes_used;
3882 spin_unlock(&sinfo->lock);
3883
3884 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3885 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003886 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3887 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003888 meta_used = sinfo->bytes_used;
3889 spin_unlock(&sinfo->lock);
3890
3891 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3892 csum_size * 2;
3893 num_bytes += div64_u64(data_used + meta_used, 50);
3894
3895 if (num_bytes * 3 > meta_used)
3896 num_bytes = div64_u64(meta_used, 3);
3897
3898 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3899}
3900
3901static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3902{
3903 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3904 struct btrfs_space_info *sinfo = block_rsv->space_info;
3905 u64 num_bytes;
3906
3907 num_bytes = calc_global_metadata_size(fs_info);
3908
3909 spin_lock(&block_rsv->lock);
3910 spin_lock(&sinfo->lock);
3911
3912 block_rsv->size = num_bytes;
3913
3914 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003915 sinfo->bytes_reserved + sinfo->bytes_readonly +
3916 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003917
3918 if (sinfo->total_bytes > num_bytes) {
3919 num_bytes = sinfo->total_bytes - num_bytes;
3920 block_rsv->reserved += num_bytes;
3921 sinfo->bytes_reserved += num_bytes;
3922 }
3923
3924 if (block_rsv->reserved >= block_rsv->size) {
3925 num_bytes = block_rsv->reserved - block_rsv->size;
3926 sinfo->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003927 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003928 block_rsv->reserved = block_rsv->size;
3929 block_rsv->full = 1;
3930 }
3931#if 0
3932 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3933 block_rsv->size, block_rsv->reserved);
3934#endif
3935 spin_unlock(&sinfo->lock);
3936 spin_unlock(&block_rsv->lock);
3937}
3938
Yan, Zhengf0486c62010-05-16 10:46:25 -04003939static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3940{
3941 struct btrfs_space_info *space_info;
3942
3943 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3944 fs_info->chunk_block_rsv.space_info = space_info;
3945 fs_info->chunk_block_rsv.priority = 10;
3946
3947 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003948 fs_info->global_block_rsv.space_info = space_info;
3949 fs_info->global_block_rsv.priority = 10;
3950 fs_info->global_block_rsv.refill_used = 1;
3951 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003952 fs_info->trans_block_rsv.space_info = space_info;
3953 fs_info->empty_block_rsv.space_info = space_info;
3954 fs_info->empty_block_rsv.priority = 10;
3955
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003956 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3957 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3958 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3959 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003960 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003961
3962 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3963
3964 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3965
3966 update_global_block_rsv(fs_info);
3967}
3968
3969static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3970{
3971 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3972 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3973 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3974 WARN_ON(fs_info->trans_block_rsv.size > 0);
3975 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3976 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3977 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003978}
3979
Yan, Zhenga22285a2010-05-16 10:48:46 -04003980static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3981{
3982 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3983 3 * num_items;
3984}
3985
Josef Bacikfcb80c22011-05-03 10:40:22 -04003986int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans,
3987 struct btrfs_root *root,
3988 struct btrfs_block_rsv *rsv)
3989{
3990 struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv;
3991 u64 num_bytes;
3992 int ret;
3993
3994 /*
3995 * Truncate should be freeing data, but give us 2 items just in case it
3996 * needs to use some space. We may want to be smarter about this in the
3997 * future.
3998 */
3999 num_bytes = calc_trans_metadata_size(root, 2);
4000
4001 /* We already have enough bytes, just return */
4002 if (rsv->reserved >= num_bytes)
4003 return 0;
4004
4005 num_bytes -= rsv->reserved;
4006
4007 /*
4008 * You should have reserved enough space before hand to do this, so this
4009 * should not fail.
4010 */
4011 ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes);
4012 BUG_ON(ret);
4013
4014 return 0;
4015}
4016
Yan, Zhenga22285a2010-05-16 10:48:46 -04004017int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
4018 struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004019 int num_items)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020{
4021 u64 num_bytes;
4022 int ret;
4023
4024 if (num_items == 0 || root->fs_info->chunk_root == root)
4025 return 0;
4026
4027 num_bytes = calc_trans_metadata_size(root, num_items);
4028 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004029 num_bytes);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004030 if (!ret) {
4031 trans->bytes_reserved += num_bytes;
4032 trans->block_rsv = &root->fs_info->trans_block_rsv;
4033 }
4034 return ret;
4035}
4036
4037void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4038 struct btrfs_root *root)
4039{
4040 if (!trans->bytes_reserved)
4041 return;
4042
4043 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
4044 btrfs_block_rsv_release(root, trans->block_rsv,
4045 trans->bytes_reserved);
4046 trans->bytes_reserved = 0;
4047}
4048
Yan, Zhengd68fc572010-05-16 10:49:58 -04004049int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4050 struct inode *inode)
4051{
4052 struct btrfs_root *root = BTRFS_I(inode)->root;
4053 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4054 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4055
4056 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004057 * We need to hold space in order to delete our orphan item once we've
4058 * added it, so this takes the reservation so we can release it later
4059 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004060 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04004061 u64 num_bytes = calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004062 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4063}
4064
4065void btrfs_orphan_release_metadata(struct inode *inode)
4066{
4067 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04004068 u64 num_bytes = calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004069 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4070}
4071
Yan, Zhenga22285a2010-05-16 10:48:46 -04004072int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4073 struct btrfs_pending_snapshot *pending)
4074{
4075 struct btrfs_root *root = pending->root;
4076 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4077 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4078 /*
4079 * two for root back/forward refs, two for directory entries
4080 * and one for root of the snapshot.
4081 */
4082 u64 num_bytes = calc_trans_metadata_size(root, 5);
4083 dst_rsv->space_info = src_rsv->space_info;
4084 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4085}
4086
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004087static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
4088{
4089 return num_bytes >>= 3;
4090}
4091
4092int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4093{
4094 struct btrfs_root *root = BTRFS_I(inode)->root;
4095 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
4096 u64 to_reserve;
4097 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004098 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004099 int ret;
4100
4101 if (btrfs_transaction_in_commit(root->fs_info))
4102 schedule_timeout(1);
4103
4104 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004105
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004106 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004107 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4108
4109 if (nr_extents > reserved_extents) {
4110 nr_extents -= reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004111 to_reserve = calc_trans_metadata_size(root, nr_extents);
4112 } else {
4113 nr_extents = 0;
4114 to_reserve = 0;
4115 }
Josef Bacik57a45ced2011-01-25 16:30:38 -05004116
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004117 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004118 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
4119 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004120 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004121
Josef Bacik57a45ced2011-01-25 16:30:38 -05004122 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004123 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004124
4125 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4126
4127 if (block_rsv->size > 512 * 1024 * 1024)
Josef Bacik0019f102010-10-15 15:18:40 -04004128 shrink_delalloc(NULL, root, to_reserve, 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004129
4130 return 0;
4131}
4132
4133void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4134{
4135 struct btrfs_root *root = BTRFS_I(inode)->root;
4136 u64 to_free;
4137 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004138 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004139
4140 num_bytes = ALIGN(num_bytes, root->sectorsize);
4141 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik3c148742011-02-02 15:53:47 +00004142 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004143
Josef Bacik57a45ced2011-01-25 16:30:38 -05004144 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4145 do {
4146 int old, new;
4147
4148 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4149 if (nr_extents >= reserved_extents) {
4150 nr_extents = 0;
4151 break;
4152 }
4153 old = reserved_extents;
4154 nr_extents = reserved_extents - nr_extents;
4155 new = reserved_extents - nr_extents;
4156 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
4157 reserved_extents, new);
4158 if (likely(old == reserved_extents))
4159 break;
4160 reserved_extents = old;
4161 } while (1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004162
4163 to_free = calc_csum_metadata_size(inode, num_bytes);
4164 if (nr_extents > 0)
4165 to_free += calc_trans_metadata_size(root, nr_extents);
4166
4167 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4168 to_free);
4169}
4170
4171int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4172{
4173 int ret;
4174
4175 ret = btrfs_check_data_free_space(inode, num_bytes);
4176 if (ret)
4177 return ret;
4178
4179 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4180 if (ret) {
4181 btrfs_free_reserved_data_space(inode, num_bytes);
4182 return ret;
4183 }
4184
4185 return 0;
4186}
4187
4188void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4189{
4190 btrfs_delalloc_release_metadata(inode, num_bytes);
4191 btrfs_free_reserved_data_space(inode, num_bytes);
4192}
4193
Chris Mason9078a3e2007-04-26 16:46:15 -04004194static int update_block_group(struct btrfs_trans_handle *trans,
4195 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004196 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004197{
Josef Bacik0af3d002010-06-21 14:48:16 -04004198 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004199 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004200 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004201 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004202 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004203 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004204
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004205 /* block accounting for super block */
4206 spin_lock(&info->delalloc_lock);
4207 old_val = btrfs_super_bytes_used(&info->super_copy);
4208 if (alloc)
4209 old_val += num_bytes;
4210 else
4211 old_val -= num_bytes;
4212 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004213 spin_unlock(&info->delalloc_lock);
4214
Chris Masond3977122009-01-05 21:25:51 -05004215 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004216 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004217 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004218 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004219 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4220 BTRFS_BLOCK_GROUP_RAID1 |
4221 BTRFS_BLOCK_GROUP_RAID10))
4222 factor = 2;
4223 else
4224 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004225 /*
4226 * If this block group has free space cache written out, we
4227 * need to make sure to load it if we are removing space. This
4228 * is because we need the unpinning stage to actually add the
4229 * space back to the block group, otherwise we will leak space.
4230 */
4231 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004232 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004233
Chris Masondb945352007-10-15 16:15:53 -04004234 byte_in_group = bytenr - cache->key.objectid;
4235 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004236
Josef Bacik25179202008-10-29 14:49:05 -04004237 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004238 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004239
4240 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4241 cache->disk_cache_state < BTRFS_DC_CLEAR)
4242 cache->disk_cache_state = BTRFS_DC_CLEAR;
4243
Josef Bacik0f9dd462008-09-23 13:14:11 -04004244 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004245 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004246 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004247 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004248 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004249 btrfs_set_block_group_used(&cache->item, old_val);
4250 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004251 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004252 cache->space_info->reservation_progress++;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004253 cache->space_info->bytes_used += num_bytes;
4254 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004255 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004256 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004257 } else {
Chris Masondb945352007-10-15 16:15:53 -04004258 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004259 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004260 cache->pinned += num_bytes;
4261 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004262 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004263 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004264 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004265 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004266
Yan, Zhengf0486c62010-05-16 10:46:25 -04004267 set_extent_dirty(info->pinned_extents,
4268 bytenr, bytenr + num_bytes - 1,
4269 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004270 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004271 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004272 total -= num_bytes;
4273 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004274 }
4275 return 0;
4276}
Chris Mason6324fbf2008-03-24 15:01:59 -04004277
Chris Masona061fc82008-05-07 11:43:44 -04004278static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4279{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004280 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004281 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004282
4283 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4284 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004285 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004286
Yan Zhengd2fb3432008-12-11 16:30:39 -05004287 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004288 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004289
4290 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004291}
4292
Yan, Zhengf0486c62010-05-16 10:46:25 -04004293static int pin_down_extent(struct btrfs_root *root,
4294 struct btrfs_block_group_cache *cache,
4295 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004296{
Yan Zheng11833d62009-09-11 16:11:19 -04004297 spin_lock(&cache->space_info->lock);
4298 spin_lock(&cache->lock);
4299 cache->pinned += num_bytes;
4300 cache->space_info->bytes_pinned += num_bytes;
4301 if (reserved) {
4302 cache->reserved -= num_bytes;
4303 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004304 cache->space_info->reservation_progress++;
Yan324ae4d2007-11-16 14:57:08 -05004305 }
Yan Zheng11833d62009-09-11 16:11:19 -04004306 spin_unlock(&cache->lock);
4307 spin_unlock(&cache->space_info->lock);
4308
Yan, Zhengf0486c62010-05-16 10:46:25 -04004309 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4310 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004311 return 0;
4312}
Chris Mason9078a3e2007-04-26 16:46:15 -04004313
Yan, Zhengf0486c62010-05-16 10:46:25 -04004314/*
4315 * this function must be called within transaction
4316 */
4317int btrfs_pin_extent(struct btrfs_root *root,
4318 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004319{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004320 struct btrfs_block_group_cache *cache;
4321
4322 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4323 BUG_ON(!cache);
4324
4325 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4326
4327 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004328 return 0;
4329}
Zheng Yane8569812008-09-26 10:05:48 -04004330
Yan, Zhengf0486c62010-05-16 10:46:25 -04004331/*
4332 * update size of reserved extents. this function may return -EAGAIN
4333 * if 'reserve' is true or 'sinfo' is false.
4334 */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004335int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4336 u64 num_bytes, int reserve, int sinfo)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004337{
4338 int ret = 0;
4339 if (sinfo) {
4340 struct btrfs_space_info *space_info = cache->space_info;
4341 spin_lock(&space_info->lock);
4342 spin_lock(&cache->lock);
4343 if (reserve) {
4344 if (cache->ro) {
4345 ret = -EAGAIN;
4346 } else {
4347 cache->reserved += num_bytes;
4348 space_info->bytes_reserved += num_bytes;
4349 }
4350 } else {
4351 if (cache->ro)
4352 space_info->bytes_readonly += num_bytes;
4353 cache->reserved -= num_bytes;
4354 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004355 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004356 }
4357 spin_unlock(&cache->lock);
4358 spin_unlock(&space_info->lock);
4359 } else {
4360 spin_lock(&cache->lock);
4361 if (cache->ro) {
4362 ret = -EAGAIN;
4363 } else {
4364 if (reserve)
4365 cache->reserved += num_bytes;
4366 else
4367 cache->reserved -= num_bytes;
4368 }
4369 spin_unlock(&cache->lock);
4370 }
4371 return ret;
4372}
4373
Yan Zheng11833d62009-09-11 16:11:19 -04004374int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4375 struct btrfs_root *root)
4376{
4377 struct btrfs_fs_info *fs_info = root->fs_info;
4378 struct btrfs_caching_control *next;
4379 struct btrfs_caching_control *caching_ctl;
4380 struct btrfs_block_group_cache *cache;
4381
4382 down_write(&fs_info->extent_commit_sem);
4383
4384 list_for_each_entry_safe(caching_ctl, next,
4385 &fs_info->caching_block_groups, list) {
4386 cache = caching_ctl->block_group;
4387 if (block_group_cache_done(cache)) {
4388 cache->last_byte_to_unpin = (u64)-1;
4389 list_del_init(&caching_ctl->list);
4390 put_caching_control(caching_ctl);
4391 } else {
4392 cache->last_byte_to_unpin = caching_ctl->progress;
4393 }
4394 }
4395
4396 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4397 fs_info->pinned_extents = &fs_info->freed_extents[1];
4398 else
4399 fs_info->pinned_extents = &fs_info->freed_extents[0];
4400
4401 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004402
4403 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004404 return 0;
4405}
4406
4407static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4408{
4409 struct btrfs_fs_info *fs_info = root->fs_info;
4410 struct btrfs_block_group_cache *cache = NULL;
4411 u64 len;
4412
4413 while (start <= end) {
4414 if (!cache ||
4415 start >= cache->key.objectid + cache->key.offset) {
4416 if (cache)
4417 btrfs_put_block_group(cache);
4418 cache = btrfs_lookup_block_group(fs_info, start);
4419 BUG_ON(!cache);
4420 }
4421
4422 len = cache->key.objectid + cache->key.offset - start;
4423 len = min(len, end + 1 - start);
4424
4425 if (start < cache->last_byte_to_unpin) {
4426 len = min(len, cache->last_byte_to_unpin - start);
4427 btrfs_add_free_space(cache, start, len);
4428 }
Josef Bacik25179202008-10-29 14:49:05 -04004429
Yan, Zhengf0486c62010-05-16 10:46:25 -04004430 start += len;
4431
Josef Bacik25179202008-10-29 14:49:05 -04004432 spin_lock(&cache->space_info->lock);
4433 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004434 cache->pinned -= len;
4435 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004436 if (cache->ro) {
4437 cache->space_info->bytes_readonly += len;
4438 } else if (cache->reserved_pinned > 0) {
4439 len = min(len, cache->reserved_pinned);
4440 cache->reserved_pinned -= len;
4441 cache->space_info->bytes_reserved += len;
4442 }
Josef Bacik25179202008-10-29 14:49:05 -04004443 spin_unlock(&cache->lock);
4444 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004445 }
4446
4447 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004448 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004449 return 0;
4450}
4451
4452int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004453 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004454{
Yan Zheng11833d62009-09-11 16:11:19 -04004455 struct btrfs_fs_info *fs_info = root->fs_info;
4456 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004457 struct btrfs_block_rsv *block_rsv;
4458 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004459 u64 start;
4460 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004461 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004462 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004463
Yan Zheng11833d62009-09-11 16:11:19 -04004464 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4465 unpin = &fs_info->freed_extents[1];
4466 else
4467 unpin = &fs_info->freed_extents[0];
4468
Chris Masond3977122009-01-05 21:25:51 -05004469 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004470 ret = find_first_extent_bit(unpin, 0, &start, &end,
4471 EXTENT_DIRTY);
4472 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004473 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004474
Li Dongyang5378e602011-03-24 10:24:27 +00004475 if (btrfs_test_opt(root, DISCARD))
4476 ret = btrfs_discard_extent(root, start,
4477 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004478
Chris Mason1a5bc162007-10-15 16:15:26 -04004479 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004480 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004481 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004482 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004483
Yan, Zhengf0486c62010-05-16 10:46:25 -04004484 mutex_lock(&fs_info->durable_block_rsv_mutex);
4485 list_for_each_entry_safe(block_rsv, next_rsv,
4486 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004487
Yan, Zhengf0486c62010-05-16 10:46:25 -04004488 idx = trans->transid & 0x1;
4489 if (block_rsv->freed[idx] > 0) {
4490 block_rsv_add_bytes(block_rsv,
4491 block_rsv->freed[idx], 0);
4492 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004493 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004494 if (atomic_read(&block_rsv->usage) == 0) {
4495 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004496
Yan, Zhengf0486c62010-05-16 10:46:25 -04004497 if (block_rsv->freed[0] == 0 &&
4498 block_rsv->freed[1] == 0) {
4499 list_del_init(&block_rsv->list);
4500 kfree(block_rsv);
4501 }
4502 } else {
4503 btrfs_block_rsv_release(root, block_rsv, 0);
4504 }
4505 }
4506 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4507
Chris Masone20d96d2007-03-22 12:13:20 -04004508 return 0;
4509}
4510
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004511static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4512 struct btrfs_root *root,
4513 u64 bytenr, u64 num_bytes, u64 parent,
4514 u64 root_objectid, u64 owner_objectid,
4515 u64 owner_offset, int refs_to_drop,
4516 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004517{
Chris Masone2fa7222007-03-12 16:22:34 -04004518 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004519 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004520 struct btrfs_fs_info *info = root->fs_info;
4521 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004522 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004523 struct btrfs_extent_item *ei;
4524 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004525 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004526 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004527 int extent_slot = 0;
4528 int found_extent = 0;
4529 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004530 u32 item_size;
4531 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004532
Chris Mason5caf2a02007-04-02 11:20:42 -04004533 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004534 if (!path)
4535 return -ENOMEM;
4536
Chris Mason3c12ac72008-04-21 12:01:38 -04004537 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004538 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004539
4540 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4541 BUG_ON(!is_data && refs_to_drop != 1);
4542
4543 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4544 bytenr, num_bytes, parent,
4545 root_objectid, owner_objectid,
4546 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004547 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004548 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004549 while (extent_slot >= 0) {
4550 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004551 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004552 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004553 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004554 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4555 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004556 found_extent = 1;
4557 break;
4558 }
4559 if (path->slots[0] - extent_slot > 5)
4560 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004561 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004562 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004563#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4564 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4565 if (found_extent && item_size < sizeof(*ei))
4566 found_extent = 0;
4567#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004568 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004569 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004570 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004571 NULL, refs_to_drop,
4572 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004573 BUG_ON(ret);
4574 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04004575 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004576
4577 key.objectid = bytenr;
4578 key.type = BTRFS_EXTENT_ITEM_KEY;
4579 key.offset = num_bytes;
4580
Zheng Yan31840ae2008-09-23 13:14:14 -04004581 ret = btrfs_search_slot(trans, extent_root,
4582 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004583 if (ret) {
4584 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004585 ", was looking for %llu\n", ret,
4586 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004587 btrfs_print_leaf(extent_root, path->nodes[0]);
4588 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004589 BUG_ON(ret);
4590 extent_slot = path->slots[0];
4591 }
Chris Mason7bb86312007-12-11 09:25:06 -05004592 } else {
4593 btrfs_print_leaf(extent_root, path->nodes[0]);
4594 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004595 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004596 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004597 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004598 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004599 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004600 (unsigned long long)owner_objectid,
4601 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004602 }
Chris Mason5f39d392007-10-15 16:14:19 -04004603
4604 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004605 item_size = btrfs_item_size_nr(leaf, extent_slot);
4606#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4607 if (item_size < sizeof(*ei)) {
4608 BUG_ON(found_extent || extent_slot != path->slots[0]);
4609 ret = convert_extent_item_v0(trans, extent_root, path,
4610 owner_objectid, 0);
4611 BUG_ON(ret < 0);
4612
4613 btrfs_release_path(extent_root, path);
4614 path->leave_spinning = 1;
4615
4616 key.objectid = bytenr;
4617 key.type = BTRFS_EXTENT_ITEM_KEY;
4618 key.offset = num_bytes;
4619
4620 ret = btrfs_search_slot(trans, extent_root, &key, path,
4621 -1, 1);
4622 if (ret) {
4623 printk(KERN_ERR "umm, got %d back from search"
4624 ", was looking for %llu\n", ret,
4625 (unsigned long long)bytenr);
4626 btrfs_print_leaf(extent_root, path->nodes[0]);
4627 }
4628 BUG_ON(ret);
4629 extent_slot = path->slots[0];
4630 leaf = path->nodes[0];
4631 item_size = btrfs_item_size_nr(leaf, extent_slot);
4632 }
4633#endif
4634 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004635 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004636 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004637 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4638 struct btrfs_tree_block_info *bi;
4639 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4640 bi = (struct btrfs_tree_block_info *)(ei + 1);
4641 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004642 }
4643
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004644 refs = btrfs_extent_refs(leaf, ei);
4645 BUG_ON(refs < refs_to_drop);
4646 refs -= refs_to_drop;
4647
4648 if (refs > 0) {
4649 if (extent_op)
4650 __run_delayed_extent_op(extent_op, leaf, ei);
4651 /*
4652 * In the case of inline back ref, reference count will
4653 * be updated by remove_extent_backref
4654 */
4655 if (iref) {
4656 BUG_ON(!found_extent);
4657 } else {
4658 btrfs_set_extent_refs(leaf, ei, refs);
4659 btrfs_mark_buffer_dirty(leaf);
4660 }
4661 if (found_extent) {
4662 ret = remove_extent_backref(trans, extent_root, path,
4663 iref, refs_to_drop,
4664 is_data);
4665 BUG_ON(ret);
4666 }
4667 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004668 if (found_extent) {
4669 BUG_ON(is_data && refs_to_drop !=
4670 extent_data_ref_count(root, path, iref));
4671 if (iref) {
4672 BUG_ON(path->slots[0] != extent_slot);
4673 } else {
4674 BUG_ON(path->slots[0] != extent_slot + 1);
4675 path->slots[0] = extent_slot;
4676 num_to_del = 2;
4677 }
Chris Mason78fae272007-03-25 11:35:08 -04004678 }
Chris Masonb9473432009-03-13 11:00:37 -04004679
Chris Mason952fcca2008-02-18 16:33:44 -05004680 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4681 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004682 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04004683 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01004684
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004685 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004686 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4687 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004688 } else {
4689 invalidate_mapping_pages(info->btree_inode->i_mapping,
4690 bytenr >> PAGE_CACHE_SHIFT,
4691 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004692 }
4693
Yan, Zhengf0486c62010-05-16 10:46:25 -04004694 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004695 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004696 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004697 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004698 return ret;
4699}
4700
4701/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004702 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004703 * delayed ref for that extent as well. This searches the delayed ref tree for
4704 * a given extent, and if there are no other delayed refs to be processed, it
4705 * removes it from the tree.
4706 */
4707static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4708 struct btrfs_root *root, u64 bytenr)
4709{
4710 struct btrfs_delayed_ref_head *head;
4711 struct btrfs_delayed_ref_root *delayed_refs;
4712 struct btrfs_delayed_ref_node *ref;
4713 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004714 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004715
4716 delayed_refs = &trans->transaction->delayed_refs;
4717 spin_lock(&delayed_refs->lock);
4718 head = btrfs_find_delayed_ref_head(trans, bytenr);
4719 if (!head)
4720 goto out;
4721
4722 node = rb_prev(&head->node.rb_node);
4723 if (!node)
4724 goto out;
4725
4726 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4727
4728 /* there are still entries for this ref, we can't drop it */
4729 if (ref->bytenr == bytenr)
4730 goto out;
4731
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004732 if (head->extent_op) {
4733 if (!head->must_insert_reserved)
4734 goto out;
4735 kfree(head->extent_op);
4736 head->extent_op = NULL;
4737 }
4738
Chris Mason1887be62009-03-13 10:11:24 -04004739 /*
4740 * waiting for the lock here would deadlock. If someone else has it
4741 * locked they are already in the process of dropping it anyway
4742 */
4743 if (!mutex_trylock(&head->mutex))
4744 goto out;
4745
4746 /*
4747 * at this point we have a head with no other entries. Go
4748 * ahead and process it.
4749 */
4750 head->node.in_tree = 0;
4751 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004752
Chris Mason1887be62009-03-13 10:11:24 -04004753 delayed_refs->num_entries--;
4754
4755 /*
4756 * we don't take a ref on the node because we're removing it from the
4757 * tree, so we just steal the ref the tree was holding.
4758 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004759 delayed_refs->num_heads--;
4760 if (list_empty(&head->cluster))
4761 delayed_refs->num_heads_ready--;
4762
4763 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004764 spin_unlock(&delayed_refs->lock);
4765
Yan, Zhengf0486c62010-05-16 10:46:25 -04004766 BUG_ON(head->extent_op);
4767 if (head->must_insert_reserved)
4768 ret = 1;
4769
4770 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004771 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004772 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004773out:
4774 spin_unlock(&delayed_refs->lock);
4775 return 0;
4776}
4777
Yan, Zhengf0486c62010-05-16 10:46:25 -04004778void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4779 struct btrfs_root *root,
4780 struct extent_buffer *buf,
4781 u64 parent, int last_ref)
4782{
4783 struct btrfs_block_rsv *block_rsv;
4784 struct btrfs_block_group_cache *cache = NULL;
4785 int ret;
4786
4787 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4788 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4789 parent, root->root_key.objectid,
4790 btrfs_header_level(buf),
4791 BTRFS_DROP_DELAYED_REF, NULL);
4792 BUG_ON(ret);
4793 }
4794
4795 if (!last_ref)
4796 return;
4797
4798 block_rsv = get_block_rsv(trans, root);
4799 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004800 if (block_rsv->space_info != cache->space_info)
4801 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004802
4803 if (btrfs_header_generation(buf) == trans->transid) {
4804 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4805 ret = check_ref_cleanup(trans, root, buf->start);
4806 if (!ret)
4807 goto pin;
4808 }
4809
4810 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4811 pin_down_extent(root, cache, buf->start, buf->len, 1);
4812 goto pin;
4813 }
4814
4815 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4816
4817 btrfs_add_free_space(cache, buf->start, buf->len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00004818 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004819 if (ret == -EAGAIN) {
4820 /* block group became read-only */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004821 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004822 goto out;
4823 }
4824
4825 ret = 1;
4826 spin_lock(&block_rsv->lock);
4827 if (block_rsv->reserved < block_rsv->size) {
4828 block_rsv->reserved += buf->len;
4829 ret = 0;
4830 }
4831 spin_unlock(&block_rsv->lock);
4832
4833 if (ret) {
4834 spin_lock(&cache->space_info->lock);
4835 cache->space_info->bytes_reserved -= buf->len;
Chris Mason36e39c42011-03-12 07:08:42 -05004836 cache->space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004837 spin_unlock(&cache->space_info->lock);
4838 }
4839 goto out;
4840 }
4841pin:
4842 if (block_rsv->durable && !cache->ro) {
4843 ret = 0;
4844 spin_lock(&cache->lock);
4845 if (!cache->ro) {
4846 cache->reserved_pinned += buf->len;
4847 ret = 1;
4848 }
4849 spin_unlock(&cache->lock);
4850
4851 if (ret) {
4852 spin_lock(&block_rsv->lock);
4853 block_rsv->freed[trans->transid & 0x1] += buf->len;
4854 spin_unlock(&block_rsv->lock);
4855 }
4856 }
4857out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004858 /*
4859 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4860 * anymore.
4861 */
4862 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004863 btrfs_put_block_group(cache);
4864}
4865
Chris Mason925baed2008-06-25 16:01:30 -04004866int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004867 struct btrfs_root *root,
4868 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004869 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004870{
4871 int ret;
4872
Chris Mason56bec292009-03-13 10:10:06 -04004873 /*
4874 * tree log blocks never actually go into the extent allocation
4875 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004876 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004877 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4878 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004879 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004880 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004881 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004882 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4883 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4884 parent, root_objectid, (int)owner,
4885 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004886 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004887 } else {
4888 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4889 parent, root_objectid, owner,
4890 offset, BTRFS_DROP_DELAYED_REF, NULL);
4891 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004892 }
Chris Mason925baed2008-06-25 16:01:30 -04004893 return ret;
4894}
4895
Chris Mason87ee04e2007-11-30 11:30:34 -05004896static u64 stripe_align(struct btrfs_root *root, u64 val)
4897{
4898 u64 mask = ((u64)root->stripesize - 1);
4899 u64 ret = (val + mask) & ~mask;
4900 return ret;
4901}
4902
Chris Masonfec577f2007-02-26 10:40:21 -05004903/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004904 * when we wait for progress in the block group caching, its because
4905 * our allocation attempt failed at least once. So, we must sleep
4906 * and let some progress happen before we try again.
4907 *
4908 * This function will sleep at least once waiting for new free space to
4909 * show up, and then it will check the block group free space numbers
4910 * for our min num_bytes. Another option is to have it go ahead
4911 * and look in the rbtree for a free extent of a given size, but this
4912 * is a good start.
4913 */
4914static noinline int
4915wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4916 u64 num_bytes)
4917{
Yan Zheng11833d62009-09-11 16:11:19 -04004918 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004919 DEFINE_WAIT(wait);
4920
Yan Zheng11833d62009-09-11 16:11:19 -04004921 caching_ctl = get_caching_control(cache);
4922 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004923 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004924
Yan Zheng11833d62009-09-11 16:11:19 -04004925 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004926 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004927
4928 put_caching_control(caching_ctl);
4929 return 0;
4930}
4931
4932static noinline int
4933wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4934{
4935 struct btrfs_caching_control *caching_ctl;
4936 DEFINE_WAIT(wait);
4937
4938 caching_ctl = get_caching_control(cache);
4939 if (!caching_ctl)
4940 return 0;
4941
4942 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4943
4944 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004945 return 0;
4946}
4947
Yan, Zhengb742bb82010-05-16 10:46:24 -04004948static int get_block_group_index(struct btrfs_block_group_cache *cache)
4949{
4950 int index;
4951 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4952 index = 0;
4953 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4954 index = 1;
4955 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4956 index = 2;
4957 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4958 index = 3;
4959 else
4960 index = 4;
4961 return index;
4962}
4963
Josef Bacik817d52f2009-07-13 21:29:25 -04004964enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004965 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004966 LOOP_CACHING_NOWAIT = 1,
4967 LOOP_CACHING_WAIT = 2,
4968 LOOP_ALLOC_CHUNK = 3,
4969 LOOP_NO_EMPTY_SIZE = 4,
4970};
4971
4972/*
Chris Masonfec577f2007-02-26 10:40:21 -05004973 * walks the btree of allocated extents and find a hole of a given size.
4974 * The key ins is changed to record the hole:
4975 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004976 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004977 * ins->offset == number of blocks
4978 * Any available blocks before search_start are skipped.
4979 */
Chris Masond3977122009-01-05 21:25:51 -05004980static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004981 struct btrfs_root *orig_root,
4982 u64 num_bytes, u64 empty_size,
4983 u64 search_start, u64 search_end,
4984 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004985 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004986{
Josef Bacik80eb2342008-10-29 14:49:05 -04004987 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004988 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004989 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004990 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004991 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004992 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004993 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004994 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004995 int last_ptr_loop = 0;
4996 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004997 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004998 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004999 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005000 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005001 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05005002 u64 ideal_cache_percent = 0;
5003 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05005004
Chris Masondb945352007-10-15 16:15:53 -04005005 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005006 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005007 ins->objectid = 0;
5008 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005009
Josef Bacik2552d172009-04-03 10:14:19 -04005010 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005011 if (!space_info) {
5012 printk(KERN_ERR "No space info for %d\n", data);
5013 return -ENOSPC;
5014 }
Josef Bacik2552d172009-04-03 10:14:19 -04005015
Josef Bacik67377732010-09-16 16:19:09 -04005016 /*
5017 * If the space info is for both data and metadata it means we have a
5018 * small filesystem and we can't use the clustering stuff.
5019 */
5020 if (btrfs_mixed_space_info(space_info))
5021 use_cluster = false;
5022
Chris Mason0ef3e662008-05-24 14:04:53 -04005023 if (orig_root->ref_cows || empty_size)
5024 allowed_chunk_alloc = 1;
5025
Josef Bacik67377732010-09-16 16:19:09 -04005026 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005027 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005028 if (!btrfs_test_opt(root, SSD))
5029 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005030 }
5031
Josef Bacik67377732010-09-16 16:19:09 -04005032 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5033 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005034 last_ptr = &root->fs_info->data_alloc_cluster;
5035 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005036
Chris Mason239b14b2008-03-24 15:02:07 -04005037 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005038 spin_lock(&last_ptr->lock);
5039 if (last_ptr->block_group)
5040 hint_byte = last_ptr->window_start;
5041 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005042 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005043
Chris Masona061fc82008-05-07 11:43:44 -04005044 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005045 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005046
Josef Bacik817d52f2009-07-13 21:29:25 -04005047 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005048 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005049
Josef Bacik2552d172009-04-03 10:14:19 -04005050 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005051ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04005052 block_group = btrfs_lookup_block_group(root->fs_info,
5053 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04005054 /*
5055 * we don't want to use the block group if it doesn't match our
5056 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005057 *
5058 * However if we are re-searching with an ideal block group
5059 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005060 */
5061 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05005062 (block_group->cached != BTRFS_CACHE_NO ||
5063 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04005064 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005065 if (list_empty(&block_group->list) ||
5066 block_group->ro) {
5067 /*
5068 * someone is removing this block group,
5069 * we can't jump into the have_block_group
5070 * target because our list pointers are not
5071 * valid
5072 */
5073 btrfs_put_block_group(block_group);
5074 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005075 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005076 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005077 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005078 }
Josef Bacik2552d172009-04-03 10:14:19 -04005079 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005080 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005081 }
Chris Mason42e70e72008-11-07 18:17:11 -05005082 }
Josef Bacik2552d172009-04-03 10:14:19 -04005083search:
Josef Bacik80eb2342008-10-29 14:49:05 -04005084 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005085 list_for_each_entry(block_group, &space_info->block_groups[index],
5086 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005087 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005088 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005089
Josef Bacik11dfe352009-11-13 20:12:59 +00005090 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005091 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005092
Chris Mason83a50de2010-12-13 15:06:46 -05005093 /*
5094 * this can happen if we end up cycling through all the
5095 * raid types, but we want to make sure we only allocate
5096 * for the proper type.
5097 */
5098 if (!block_group_bits(block_group, data)) {
5099 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5100 BTRFS_BLOCK_GROUP_RAID1 |
5101 BTRFS_BLOCK_GROUP_RAID10;
5102
5103 /*
5104 * if they asked for extra copies and this block group
5105 * doesn't provide them, bail. This does allow us to
5106 * fill raid0 from raid1.
5107 */
5108 if ((data & extra) && !(block_group->flags & extra))
5109 goto loop;
5110 }
5111
Josef Bacik2552d172009-04-03 10:14:19 -04005112have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04005113 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005114 u64 free_percent;
5115
Josef Bacikb8399de2010-12-08 09:15:11 -05005116 ret = cache_block_group(block_group, trans,
5117 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04005118 if (block_group->cached == BTRFS_CACHE_FINISHED)
5119 goto have_block_group;
5120
Josef Bacikccf0e722009-11-10 21:23:48 -05005121 free_percent = btrfs_block_group_used(&block_group->item);
5122 free_percent *= 100;
5123 free_percent = div64_u64(free_percent,
5124 block_group->key.offset);
5125 free_percent = 100 - free_percent;
5126 if (free_percent > ideal_cache_percent &&
5127 likely(!block_group->ro)) {
5128 ideal_cache_offset = block_group->key.objectid;
5129 ideal_cache_percent = free_percent;
5130 }
5131
Josef Bacik817d52f2009-07-13 21:29:25 -04005132 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005133 * We only want to start kthread caching if we are at
5134 * the point where we will wait for caching to make
5135 * progress, or if our ideal search is over and we've
5136 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04005137 */
5138 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05005139 (loop > LOOP_FIND_IDEAL &&
5140 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacikb8399de2010-12-08 09:15:11 -05005141 ret = cache_block_group(block_group, trans,
5142 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005143 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005144 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005145 found_uncached_bg = true;
5146
5147 /*
5148 * If loop is set for cached only, try the next block
5149 * group.
5150 */
5151 if (loop == LOOP_FIND_IDEAL)
5152 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005153 }
5154
Josef Bacik817d52f2009-07-13 21:29:25 -04005155 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005156 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005157 found_uncached_bg = true;
5158
Josef Bacikea6a4782008-11-20 12:16:16 -05005159 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005160 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005161
Josef Bacikcca1c812011-05-13 11:07:12 -04005162 spin_lock(&block_group->tree_lock);
5163 if (cached &&
5164 block_group->free_space < num_bytes + empty_size) {
5165 spin_unlock(&block_group->tree_lock);
5166 goto loop;
5167 }
5168 spin_unlock(&block_group->tree_lock);
5169
Josef Bacik0a243252009-09-11 16:11:20 -04005170 /*
5171 * Ok we want to try and use the cluster allocator, so lets look
5172 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5173 * have tried the cluster allocator plenty of times at this
5174 * point and not have found anything, so we are likely way too
5175 * fragmented for the clustering stuff to find anything, so lets
5176 * just skip it and let the allocator find whatever block it can
5177 * find
5178 */
5179 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005180 /*
5181 * the refill lock keeps out other
5182 * people trying to start a new cluster
5183 */
5184 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005185 if (last_ptr->block_group &&
5186 (last_ptr->block_group->ro ||
5187 !block_group_bits(last_ptr->block_group, data))) {
5188 offset = 0;
5189 goto refill_cluster;
5190 }
5191
Chris Masonfa9c0d72009-04-03 09:47:43 -04005192 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5193 num_bytes, search_start);
5194 if (offset) {
5195 /* we have a block, we're done */
5196 spin_unlock(&last_ptr->refill_lock);
5197 goto checks;
5198 }
5199
5200 spin_lock(&last_ptr->lock);
5201 /*
5202 * whoops, this cluster doesn't actually point to
5203 * this block group. Get a ref on the block
5204 * group is does point to and try again
5205 */
5206 if (!last_ptr_loop && last_ptr->block_group &&
5207 last_ptr->block_group != block_group) {
5208
5209 btrfs_put_block_group(block_group);
5210 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005211 btrfs_get_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005212 spin_unlock(&last_ptr->lock);
5213 spin_unlock(&last_ptr->refill_lock);
5214
5215 last_ptr_loop = 1;
5216 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005217 /*
5218 * we know this block group is properly
5219 * in the list because
5220 * btrfs_remove_block_group, drops the
5221 * cluster before it removes the block
5222 * group from the list
5223 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04005224 goto have_block_group;
5225 }
5226 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005227refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04005228 /*
5229 * this cluster didn't work out, free it and
5230 * start over
5231 */
5232 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5233
5234 last_ptr_loop = 0;
5235
5236 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005237 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005238 block_group, last_ptr,
5239 offset, num_bytes,
5240 empty_cluster + empty_size);
5241 if (ret == 0) {
5242 /*
5243 * now pull our allocation out of this
5244 * cluster
5245 */
5246 offset = btrfs_alloc_from_cluster(block_group,
5247 last_ptr, num_bytes,
5248 search_start);
5249 if (offset) {
5250 /* we found one, proceed */
5251 spin_unlock(&last_ptr->refill_lock);
5252 goto checks;
5253 }
Josef Bacik0a243252009-09-11 16:11:20 -04005254 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5255 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005256 spin_unlock(&last_ptr->refill_lock);
5257
Josef Bacik0a243252009-09-11 16:11:20 -04005258 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005259 wait_block_group_cache_progress(block_group,
5260 num_bytes + empty_cluster + empty_size);
5261 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005262 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005263
Chris Masonfa9c0d72009-04-03 09:47:43 -04005264 /*
5265 * at this point we either didn't find a cluster
5266 * or we weren't able to allocate a block from our
5267 * cluster. Free the cluster we've been trying
5268 * to use, and go to the next block group
5269 */
Josef Bacik0a243252009-09-11 16:11:20 -04005270 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005271 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005272 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005273 }
5274
Josef Bacik6226cb02009-04-03 10:14:18 -04005275 offset = btrfs_find_space_for_alloc(block_group, search_start,
5276 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005277 /*
5278 * If we didn't find a chunk, and we haven't failed on this
5279 * block group before, and this block group is in the middle of
5280 * caching and we are ok with waiting, then go ahead and wait
5281 * for progress to be made, and set failed_alloc to true.
5282 *
5283 * If failed_alloc is true then we've already waited on this
5284 * block group once and should move on to the next block group.
5285 */
5286 if (!offset && !failed_alloc && !cached &&
5287 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005288 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005289 num_bytes + empty_size);
5290 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005291 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005292 } else if (!offset) {
5293 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005294 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005295checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005296 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005297 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005298 if (search_start + num_bytes >= search_end) {
5299 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005300 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005301 }
Chris Masone37c9e62007-05-09 20:13:14 -04005302
Josef Bacik2552d172009-04-03 10:14:19 -04005303 /* move on to the next group */
5304 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005305 block_group->key.objectid + block_group->key.offset) {
5306 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005307 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005308 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005309
Josef Bacik2552d172009-04-03 10:14:19 -04005310 ins->objectid = search_start;
5311 ins->offset = num_bytes;
5312
Josef Bacik6226cb02009-04-03 10:14:18 -04005313 if (offset < search_start)
5314 btrfs_add_free_space(block_group, offset,
5315 search_start - offset);
5316 BUG_ON(offset > search_start);
5317
Li Dongyangb4d00d52011-03-24 10:24:25 +00005318 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005319 (data & BTRFS_BLOCK_GROUP_DATA));
5320 if (ret == -EAGAIN) {
5321 btrfs_add_free_space(block_group, offset, num_bytes);
5322 goto loop;
5323 }
Yan Zheng11833d62009-09-11 16:11:19 -04005324
Josef Bacik2552d172009-04-03 10:14:19 -04005325 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005326 ins->objectid = search_start;
5327 ins->offset = num_bytes;
5328
5329 if (offset < search_start)
5330 btrfs_add_free_space(block_group, offset,
5331 search_start - offset);
5332 BUG_ON(offset > search_start);
Josef Bacikd82a6f12011-05-11 15:26:06 -04005333 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005334 break;
5335loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005336 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005337 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005338 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d72009-04-03 09:47:43 -04005339 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005340 }
5341 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005342
Yan, Zhengb742bb82010-05-16 10:46:24 -04005343 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5344 goto search;
5345
Josef Bacikccf0e722009-11-10 21:23:48 -05005346 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5347 * for them to make caching progress. Also
5348 * determine the best possible bg to cache
5349 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5350 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005351 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5352 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5353 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5354 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04005355 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005356 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5357 (found_uncached_bg || empty_size || empty_cluster ||
5358 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005359 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005360 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005361 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005362 loop++;
5363 if (!ideal_cache_percent &&
5364 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005365 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005366
5367 /*
5368 * 1 of the following 2 things have happened so far
5369 *
5370 * 1) We found an ideal block group for caching that
5371 * is mostly full and will cache quickly, so we might
5372 * as well wait for it.
5373 *
5374 * 2) We searched for cached only and we didn't find
5375 * anything, and we didn't start any caching kthreads
5376 * either, so chances are we will loop through and
5377 * start a couple caching kthreads, and then come back
5378 * around and just wait for them. This will be slower
5379 * because we will have 2 caching kthreads reading at
5380 * the same time when we could have just started one
5381 * and waited for it to get far enough to give us an
5382 * allocation, so go ahead and go to the wait caching
5383 * loop.
5384 */
5385 loop = LOOP_CACHING_WAIT;
5386 search_start = ideal_cache_offset;
5387 ideal_cache_percent = 0;
5388 goto ideal_cache;
5389 } else if (loop == LOOP_FIND_IDEAL) {
5390 /*
5391 * Didn't find a uncached bg, wait on anything we find
5392 * next.
5393 */
5394 loop = LOOP_CACHING_WAIT;
5395 goto search;
5396 }
5397
5398 if (loop < LOOP_CACHING_WAIT) {
5399 loop++;
5400 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005401 }
5402
5403 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005404 empty_size = 0;
5405 empty_cluster = 0;
5406 }
Chris Mason42e70e72008-11-07 18:17:11 -05005407
Josef Bacik2552d172009-04-03 10:14:19 -04005408 if (allowed_chunk_alloc) {
5409 ret = do_chunk_alloc(trans, root, num_bytes +
Chris Mason0e4f8f82011-04-15 16:05:44 -04005410 2 * 1024 * 1024, data,
5411 CHUNK_ALLOC_LIMITED);
Josef Bacik2552d172009-04-03 10:14:19 -04005412 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005413 done_chunk_alloc = 1;
Chris Mason0e4f8f82011-04-15 16:05:44 -04005414 } else if (!done_chunk_alloc &&
5415 space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) {
5416 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005417 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005418
Josef Bacik817d52f2009-07-13 21:29:25 -04005419 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005420 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005421 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005422 }
Josef Bacik2552d172009-04-03 10:14:19 -04005423 ret = -ENOSPC;
5424 } else if (!ins->objectid) {
5425 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04005426 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005427 ret = 0;
5428 }
Chris Mason0b86a832008-03-24 15:01:56 -04005429
Chris Mason0f70abe2007-02-28 16:46:22 -05005430 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005431}
Chris Masonec44a352008-04-28 15:29:52 -04005432
Josef Bacik9ed74f22009-09-11 16:12:44 -04005433static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5434 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005435{
5436 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005437 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005438
Josef Bacik9ed74f22009-09-11 16:12:44 -04005439 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005440 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5441 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005442 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005443 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005444 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005445 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5446 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005447 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005448 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005449 (unsigned long long)info->bytes_pinned,
5450 (unsigned long long)info->bytes_reserved,
5451 (unsigned long long)info->bytes_may_use,
5452 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005453 spin_unlock(&info->lock);
5454
5455 if (!dump_block_groups)
5456 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005457
Josef Bacik80eb2342008-10-29 14:49:05 -04005458 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005459again:
5460 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005461 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005462 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5463 "%llu pinned %llu reserved\n",
5464 (unsigned long long)cache->key.objectid,
5465 (unsigned long long)cache->key.offset,
5466 (unsigned long long)btrfs_block_group_used(&cache->item),
5467 (unsigned long long)cache->pinned,
5468 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005469 btrfs_dump_free_space(cache, bytes);
5470 spin_unlock(&cache->lock);
5471 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005472 if (++index < BTRFS_NR_RAID_TYPES)
5473 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005474 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005475}
Zheng Yane8569812008-09-26 10:05:48 -04005476
Yan Zheng11833d62009-09-11 16:11:19 -04005477int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5478 struct btrfs_root *root,
5479 u64 num_bytes, u64 min_alloc_size,
5480 u64 empty_size, u64 hint_byte,
5481 u64 search_end, struct btrfs_key *ins,
5482 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005483{
5484 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005485 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005486
Josef Bacik6a632092009-02-20 11:00:09 -05005487 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005488again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005489 /*
5490 * the only place that sets empty_size is btrfs_realloc_node, which
5491 * is not called recursively on allocations
5492 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005493 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005494 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005495 num_bytes + 2 * 1024 * 1024, data,
5496 CHUNK_ALLOC_NO_FORCE);
Chris Mason0b86a832008-03-24 15:01:56 -04005497
Chris Masondb945352007-10-15 16:15:53 -04005498 WARN_ON(num_bytes < root->sectorsize);
5499 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005500 search_start, search_end, hint_byte,
5501 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005502
Chris Mason98d20f62008-04-14 09:46:10 -04005503 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5504 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005505 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005506 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005507 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005508 num_bytes, data, CHUNK_ALLOC_FORCE);
Chris Mason98d20f62008-04-14 09:46:10 -04005509 goto again;
5510 }
Chris Mason91435652011-02-16 13:10:41 -05005511 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005512 struct btrfs_space_info *sinfo;
5513
5514 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005515 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5516 "wanted %llu\n", (unsigned long long)data,
5517 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005518 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005519 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005520
liubo1abe9b82011-03-24 11:18:59 +00005521 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5522
Josef Bacik0f9dd462008-09-23 13:14:11 -04005523 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005524}
5525
Chris Mason65b51a02008-08-01 15:11:20 -04005526int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5527{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005528 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005529 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005530
Josef Bacik0f9dd462008-09-23 13:14:11 -04005531 cache = btrfs_lookup_block_group(root->fs_info, start);
5532 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005533 printk(KERN_ERR "Unable to find block group for %llu\n",
5534 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005535 return -ENOSPC;
5536 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005537
Li Dongyang5378e602011-03-24 10:24:27 +00005538 if (btrfs_test_opt(root, DISCARD))
5539 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005540
Josef Bacik0f9dd462008-09-23 13:14:11 -04005541 btrfs_add_free_space(cache, start, len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00005542 btrfs_update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005543 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005544
liubo1abe9b82011-03-24 11:18:59 +00005545 trace_btrfs_reserved_extent_free(root, start, len);
5546
Chris Masone6dcd2d2008-07-17 12:53:50 -04005547 return ret;
5548}
5549
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005550static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5551 struct btrfs_root *root,
5552 u64 parent, u64 root_objectid,
5553 u64 flags, u64 owner, u64 offset,
5554 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005555{
5556 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005557 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005558 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005560 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561 struct extent_buffer *leaf;
5562 int type;
5563 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005564
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005565 if (parent > 0)
5566 type = BTRFS_SHARED_DATA_REF_KEY;
5567 else
5568 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005569
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005571
5572 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005573 if (!path)
5574 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005575
Chris Masonb9473432009-03-13 11:00:37 -04005576 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005577 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5578 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005579 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005580
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005581 leaf = path->nodes[0];
5582 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005583 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005584 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5585 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5586 btrfs_set_extent_flags(leaf, extent_item,
5587 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005588
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5590 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5591 if (parent > 0) {
5592 struct btrfs_shared_data_ref *ref;
5593 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5594 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5595 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5596 } else {
5597 struct btrfs_extent_data_ref *ref;
5598 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5599 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5600 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5601 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5602 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5603 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005604
5605 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005606 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005607
Yan, Zhengf0486c62010-05-16 10:46:25 -04005608 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005609 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005610 printk(KERN_ERR "btrfs update block group failed for %llu "
5611 "%llu\n", (unsigned long long)ins->objectid,
5612 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005613 BUG();
5614 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005615 return ret;
5616}
5617
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005618static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5619 struct btrfs_root *root,
5620 u64 parent, u64 root_objectid,
5621 u64 flags, struct btrfs_disk_key *key,
5622 int level, struct btrfs_key *ins)
5623{
5624 int ret;
5625 struct btrfs_fs_info *fs_info = root->fs_info;
5626 struct btrfs_extent_item *extent_item;
5627 struct btrfs_tree_block_info *block_info;
5628 struct btrfs_extent_inline_ref *iref;
5629 struct btrfs_path *path;
5630 struct extent_buffer *leaf;
5631 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5632
5633 path = btrfs_alloc_path();
5634 BUG_ON(!path);
5635
5636 path->leave_spinning = 1;
5637 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5638 ins, size);
5639 BUG_ON(ret);
5640
5641 leaf = path->nodes[0];
5642 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5643 struct btrfs_extent_item);
5644 btrfs_set_extent_refs(leaf, extent_item, 1);
5645 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5646 btrfs_set_extent_flags(leaf, extent_item,
5647 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5648 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5649
5650 btrfs_set_tree_block_key(leaf, block_info, key);
5651 btrfs_set_tree_block_level(leaf, block_info, level);
5652
5653 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5654 if (parent > 0) {
5655 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5656 btrfs_set_extent_inline_ref_type(leaf, iref,
5657 BTRFS_SHARED_BLOCK_REF_KEY);
5658 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5659 } else {
5660 btrfs_set_extent_inline_ref_type(leaf, iref,
5661 BTRFS_TREE_BLOCK_REF_KEY);
5662 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5663 }
5664
5665 btrfs_mark_buffer_dirty(leaf);
5666 btrfs_free_path(path);
5667
Yan, Zhengf0486c62010-05-16 10:46:25 -04005668 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005669 if (ret) {
5670 printk(KERN_ERR "btrfs update block group failed for %llu "
5671 "%llu\n", (unsigned long long)ins->objectid,
5672 (unsigned long long)ins->offset);
5673 BUG();
5674 }
5675 return ret;
5676}
5677
5678int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5679 struct btrfs_root *root,
5680 u64 root_objectid, u64 owner,
5681 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005682{
5683 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005684
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005685 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005686
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005687 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5688 0, root_objectid, owner, offset,
5689 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005690 return ret;
5691}
Chris Masone02119d2008-09-05 16:13:11 -04005692
5693/*
5694 * this is used by the tree logging recovery code. It records that
5695 * an extent has been allocated and makes sure to clear the free
5696 * space cache bits as well
5697 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005698int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5699 struct btrfs_root *root,
5700 u64 root_objectid, u64 owner, u64 offset,
5701 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005702{
5703 int ret;
5704 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005705 struct btrfs_caching_control *caching_ctl;
5706 u64 start = ins->objectid;
5707 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005708
Chris Masone02119d2008-09-05 16:13:11 -04005709 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005710 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005711 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005712
Yan Zheng11833d62009-09-11 16:11:19 -04005713 if (!caching_ctl) {
5714 BUG_ON(!block_group_cache_done(block_group));
5715 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5716 BUG_ON(ret);
5717 } else {
5718 mutex_lock(&caching_ctl->mutex);
5719
5720 if (start >= caching_ctl->progress) {
5721 ret = add_excluded_extent(root, start, num_bytes);
5722 BUG_ON(ret);
5723 } else if (start + num_bytes <= caching_ctl->progress) {
5724 ret = btrfs_remove_free_space(block_group,
5725 start, num_bytes);
5726 BUG_ON(ret);
5727 } else {
5728 num_bytes = caching_ctl->progress - start;
5729 ret = btrfs_remove_free_space(block_group,
5730 start, num_bytes);
5731 BUG_ON(ret);
5732
5733 start = caching_ctl->progress;
5734 num_bytes = ins->objectid + ins->offset -
5735 caching_ctl->progress;
5736 ret = add_excluded_extent(root, start, num_bytes);
5737 BUG_ON(ret);
5738 }
5739
5740 mutex_unlock(&caching_ctl->mutex);
5741 put_caching_control(caching_ctl);
5742 }
5743
Li Dongyangb4d00d52011-03-24 10:24:25 +00005744 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005745 BUG_ON(ret);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005746 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005747 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5748 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005749 return ret;
5750}
5751
Chris Mason65b51a02008-08-01 15:11:20 -04005752struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5753 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005754 u64 bytenr, u32 blocksize,
5755 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005756{
5757 struct extent_buffer *buf;
5758
5759 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5760 if (!buf)
5761 return ERR_PTR(-ENOMEM);
5762 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005763 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005764 btrfs_tree_lock(buf);
5765 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005766
5767 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005768 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005769
Chris Masond0c803c2008-09-11 16:17:57 -04005770 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005771 /*
5772 * we allow two log transactions at a time, use different
5773 * EXENT bit to differentiate dirty pages.
5774 */
5775 if (root->log_transid % 2 == 0)
5776 set_extent_dirty(&root->dirty_log_pages, buf->start,
5777 buf->start + buf->len - 1, GFP_NOFS);
5778 else
5779 set_extent_new(&root->dirty_log_pages, buf->start,
5780 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005781 } else {
5782 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5783 buf->start + buf->len - 1, GFP_NOFS);
5784 }
Chris Mason65b51a02008-08-01 15:11:20 -04005785 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005786 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005787 return buf;
5788}
5789
Yan, Zhengf0486c62010-05-16 10:46:25 -04005790static struct btrfs_block_rsv *
5791use_block_rsv(struct btrfs_trans_handle *trans,
5792 struct btrfs_root *root, u32 blocksize)
5793{
5794 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005795 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005796 int ret;
5797
5798 block_rsv = get_block_rsv(trans, root);
5799
5800 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005801 ret = reserve_metadata_bytes(trans, root, block_rsv,
5802 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005803 /*
5804 * If we couldn't reserve metadata bytes try and use some from
5805 * the global reserve.
5806 */
5807 if (ret && block_rsv != global_rsv) {
5808 ret = block_rsv_use_bytes(global_rsv, blocksize);
5809 if (!ret)
5810 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005811 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005812 } else if (ret) {
5813 return ERR_PTR(ret);
5814 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005815 return block_rsv;
5816 }
5817
5818 ret = block_rsv_use_bytes(block_rsv, blocksize);
5819 if (!ret)
5820 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005821 if (ret) {
5822 WARN_ON(1);
5823 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5824 0);
5825 if (!ret) {
5826 spin_lock(&block_rsv->lock);
5827 block_rsv->size += blocksize;
5828 spin_unlock(&block_rsv->lock);
5829 return block_rsv;
5830 } else if (ret && block_rsv != global_rsv) {
5831 ret = block_rsv_use_bytes(global_rsv, blocksize);
5832 if (!ret)
5833 return global_rsv;
5834 }
5835 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005836
Yan, Zhengf0486c62010-05-16 10:46:25 -04005837 return ERR_PTR(-ENOSPC);
5838}
5839
5840static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5841{
5842 block_rsv_add_bytes(block_rsv, blocksize, 0);
5843 block_rsv_release_bytes(block_rsv, NULL, 0);
5844}
5845
Chris Masonfec577f2007-02-26 10:40:21 -05005846/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005847 * finds a free extent and does all the dirty work required for allocation
5848 * returns the key for the extent through ins, and a tree buffer for
5849 * the first block of the extent through buf.
5850 *
Chris Masonfec577f2007-02-26 10:40:21 -05005851 * returns the tree buffer or NULL.
5852 */
Chris Mason5f39d392007-10-15 16:14:19 -04005853struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005854 struct btrfs_root *root, u32 blocksize,
5855 u64 parent, u64 root_objectid,
5856 struct btrfs_disk_key *key, int level,
5857 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005858{
Chris Masone2fa7222007-03-12 16:22:34 -04005859 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005860 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005861 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005862 u64 flags = 0;
5863 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005864
Yan, Zhengf0486c62010-05-16 10:46:25 -04005865
5866 block_rsv = use_block_rsv(trans, root, blocksize);
5867 if (IS_ERR(block_rsv))
5868 return ERR_CAST(block_rsv);
5869
5870 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5871 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005872 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005873 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005874 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005875 }
Chris Mason55c69072008-01-09 15:55:33 -05005876
Chris Mason4008c042009-02-12 14:09:45 -05005877 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5878 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005879 BUG_ON(IS_ERR(buf));
5880
5881 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5882 if (parent == 0)
5883 parent = ins.objectid;
5884 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5885 } else
5886 BUG_ON(parent > 0);
5887
5888 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5889 struct btrfs_delayed_extent_op *extent_op;
5890 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5891 BUG_ON(!extent_op);
5892 if (key)
5893 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5894 else
5895 memset(&extent_op->key, 0, sizeof(extent_op->key));
5896 extent_op->flags_to_set = flags;
5897 extent_op->update_key = 1;
5898 extent_op->update_flags = 1;
5899 extent_op->is_data = 0;
5900
5901 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5902 ins.offset, parent, root_objectid,
5903 level, BTRFS_ADD_DELAYED_EXTENT,
5904 extent_op);
5905 BUG_ON(ret);
5906 }
Chris Masonfec577f2007-02-26 10:40:21 -05005907 return buf;
5908}
Chris Masona28ec192007-03-06 20:08:01 -05005909
Yan Zheng2c47e6052009-06-27 21:07:35 -04005910struct walk_control {
5911 u64 refs[BTRFS_MAX_LEVEL];
5912 u64 flags[BTRFS_MAX_LEVEL];
5913 struct btrfs_key update_progress;
5914 int stage;
5915 int level;
5916 int shared_level;
5917 int update_ref;
5918 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005919 int reada_slot;
5920 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005921};
5922
5923#define DROP_REFERENCE 1
5924#define UPDATE_BACKREF 2
5925
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005926static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5927 struct btrfs_root *root,
5928 struct walk_control *wc,
5929 struct btrfs_path *path)
5930{
5931 u64 bytenr;
5932 u64 generation;
5933 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005934 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005935 u32 nritems;
5936 u32 blocksize;
5937 struct btrfs_key key;
5938 struct extent_buffer *eb;
5939 int ret;
5940 int slot;
5941 int nread = 0;
5942
5943 if (path->slots[wc->level] < wc->reada_slot) {
5944 wc->reada_count = wc->reada_count * 2 / 3;
5945 wc->reada_count = max(wc->reada_count, 2);
5946 } else {
5947 wc->reada_count = wc->reada_count * 3 / 2;
5948 wc->reada_count = min_t(int, wc->reada_count,
5949 BTRFS_NODEPTRS_PER_BLOCK(root));
5950 }
5951
5952 eb = path->nodes[wc->level];
5953 nritems = btrfs_header_nritems(eb);
5954 blocksize = btrfs_level_size(root, wc->level - 1);
5955
5956 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5957 if (nread >= wc->reada_count)
5958 break;
5959
5960 cond_resched();
5961 bytenr = btrfs_node_blockptr(eb, slot);
5962 generation = btrfs_node_ptr_generation(eb, slot);
5963
5964 if (slot == path->slots[wc->level])
5965 goto reada;
5966
5967 if (wc->stage == UPDATE_BACKREF &&
5968 generation <= root->root_key.offset)
5969 continue;
5970
Yan, Zheng94fcca92009-10-09 09:25:16 -04005971 /* We don't lock the tree block, it's OK to be racy here */
5972 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5973 &refs, &flags);
5974 BUG_ON(ret);
5975 BUG_ON(refs == 0);
5976
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005977 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005978 if (refs == 1)
5979 goto reada;
5980
Yan, Zheng94fcca92009-10-09 09:25:16 -04005981 if (wc->level == 1 &&
5982 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5983 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005984 if (!wc->update_ref ||
5985 generation <= root->root_key.offset)
5986 continue;
5987 btrfs_node_key_to_cpu(eb, &key, slot);
5988 ret = btrfs_comp_cpu_keys(&key,
5989 &wc->update_progress);
5990 if (ret < 0)
5991 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005992 } else {
5993 if (wc->level == 1 &&
5994 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5995 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005996 }
5997reada:
5998 ret = readahead_tree_block(root, bytenr, blocksize,
5999 generation);
6000 if (ret)
6001 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006002 nread++;
6003 }
6004 wc->reada_slot = slot;
6005}
6006
Chris Mason9aca1d52007-03-13 11:09:37 -04006007/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006008 * hepler to process tree block while walking down the tree.
6009 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006010 * when wc->stage == UPDATE_BACKREF, this function updates
6011 * back refs for pointers in the block.
6012 *
6013 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006014 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006015static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6016 struct btrfs_root *root,
6017 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006018 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006019{
6020 int level = wc->level;
6021 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006022 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6023 int ret;
6024
6025 if (wc->stage == UPDATE_BACKREF &&
6026 btrfs_header_owner(eb) != root->root_key.objectid)
6027 return 1;
6028
6029 /*
6030 * when reference count of tree block is 1, it won't increase
6031 * again. once full backref flag is set, we never clear it.
6032 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006033 if (lookup_info &&
6034 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6035 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006036 BUG_ON(!path->locks[level]);
6037 ret = btrfs_lookup_extent_info(trans, root,
6038 eb->start, eb->len,
6039 &wc->refs[level],
6040 &wc->flags[level]);
6041 BUG_ON(ret);
6042 BUG_ON(wc->refs[level] == 0);
6043 }
6044
Yan Zheng2c47e6052009-06-27 21:07:35 -04006045 if (wc->stage == DROP_REFERENCE) {
6046 if (wc->refs[level] > 1)
6047 return 1;
6048
6049 if (path->locks[level] && !wc->keep_locks) {
6050 btrfs_tree_unlock(eb);
6051 path->locks[level] = 0;
6052 }
6053 return 0;
6054 }
6055
6056 /* wc->stage == UPDATE_BACKREF */
6057 if (!(wc->flags[level] & flag)) {
6058 BUG_ON(!path->locks[level]);
6059 ret = btrfs_inc_ref(trans, root, eb, 1);
6060 BUG_ON(ret);
6061 ret = btrfs_dec_ref(trans, root, eb, 0);
6062 BUG_ON(ret);
6063 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6064 eb->len, flag, 0);
6065 BUG_ON(ret);
6066 wc->flags[level] |= flag;
6067 }
6068
6069 /*
6070 * the block is shared by multiple trees, so it's not good to
6071 * keep the tree lock
6072 */
6073 if (path->locks[level] && level > 0) {
6074 btrfs_tree_unlock(eb);
6075 path->locks[level] = 0;
6076 }
6077 return 0;
6078}
6079
6080/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006081 * hepler to process tree block pointer.
6082 *
6083 * when wc->stage == DROP_REFERENCE, this function checks
6084 * reference count of the block pointed to. if the block
6085 * is shared and we need update back refs for the subtree
6086 * rooted at the block, this function changes wc->stage to
6087 * UPDATE_BACKREF. if the block is shared and there is no
6088 * need to update back, this function drops the reference
6089 * to the block.
6090 *
6091 * NOTE: return value 1 means we should stop walking down.
6092 */
6093static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6094 struct btrfs_root *root,
6095 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006096 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006097{
6098 u64 bytenr;
6099 u64 generation;
6100 u64 parent;
6101 u32 blocksize;
6102 struct btrfs_key key;
6103 struct extent_buffer *next;
6104 int level = wc->level;
6105 int reada = 0;
6106 int ret = 0;
6107
6108 generation = btrfs_node_ptr_generation(path->nodes[level],
6109 path->slots[level]);
6110 /*
6111 * if the lower level block was created before the snapshot
6112 * was created, we know there is no need to update back refs
6113 * for the subtree
6114 */
6115 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006116 generation <= root->root_key.offset) {
6117 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006118 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006119 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006120
6121 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6122 blocksize = btrfs_level_size(root, level - 1);
6123
6124 next = btrfs_find_tree_block(root, bytenr, blocksize);
6125 if (!next) {
6126 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00006127 if (!next)
6128 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006129 reada = 1;
6130 }
6131 btrfs_tree_lock(next);
6132 btrfs_set_lock_blocking(next);
6133
Yan, Zheng94fcca92009-10-09 09:25:16 -04006134 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6135 &wc->refs[level - 1],
6136 &wc->flags[level - 1]);
6137 BUG_ON(ret);
6138 BUG_ON(wc->refs[level - 1] == 0);
6139 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006140
Yan, Zheng94fcca92009-10-09 09:25:16 -04006141 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006142 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006143 if (level == 1 &&
6144 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6145 goto skip;
6146
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006147 if (!wc->update_ref ||
6148 generation <= root->root_key.offset)
6149 goto skip;
6150
6151 btrfs_node_key_to_cpu(path->nodes[level], &key,
6152 path->slots[level]);
6153 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6154 if (ret < 0)
6155 goto skip;
6156
6157 wc->stage = UPDATE_BACKREF;
6158 wc->shared_level = level - 1;
6159 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006160 } else {
6161 if (level == 1 &&
6162 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6163 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006164 }
6165
6166 if (!btrfs_buffer_uptodate(next, generation)) {
6167 btrfs_tree_unlock(next);
6168 free_extent_buffer(next);
6169 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006170 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006171 }
6172
6173 if (!next) {
6174 if (reada && level == 1)
6175 reada_walk_down(trans, root, wc, path);
6176 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006177 if (!next)
6178 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006179 btrfs_tree_lock(next);
6180 btrfs_set_lock_blocking(next);
6181 }
6182
6183 level--;
6184 BUG_ON(level != btrfs_header_level(next));
6185 path->nodes[level] = next;
6186 path->slots[level] = 0;
6187 path->locks[level] = 1;
6188 wc->level = level;
6189 if (wc->level == 1)
6190 wc->reada_slot = 0;
6191 return 0;
6192skip:
6193 wc->refs[level - 1] = 0;
6194 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006195 if (wc->stage == DROP_REFERENCE) {
6196 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6197 parent = path->nodes[level]->start;
6198 } else {
6199 BUG_ON(root->root_key.objectid !=
6200 btrfs_header_owner(path->nodes[level]));
6201 parent = 0;
6202 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006203
Yan, Zheng94fcca92009-10-09 09:25:16 -04006204 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6205 root->root_key.objectid, level - 1, 0);
6206 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006207 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006208 btrfs_tree_unlock(next);
6209 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006210 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006211 return 1;
6212}
6213
6214/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006215 * hepler to process tree block while walking up the tree.
6216 *
6217 * when wc->stage == DROP_REFERENCE, this function drops
6218 * reference count on the block.
6219 *
6220 * when wc->stage == UPDATE_BACKREF, this function changes
6221 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6222 * to UPDATE_BACKREF previously while processing the block.
6223 *
6224 * NOTE: return value 1 means we should stop walking up.
6225 */
6226static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6227 struct btrfs_root *root,
6228 struct btrfs_path *path,
6229 struct walk_control *wc)
6230{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006231 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006232 int level = wc->level;
6233 struct extent_buffer *eb = path->nodes[level];
6234 u64 parent = 0;
6235
6236 if (wc->stage == UPDATE_BACKREF) {
6237 BUG_ON(wc->shared_level < level);
6238 if (level < wc->shared_level)
6239 goto out;
6240
Yan Zheng2c47e6052009-06-27 21:07:35 -04006241 ret = find_next_key(path, level + 1, &wc->update_progress);
6242 if (ret > 0)
6243 wc->update_ref = 0;
6244
6245 wc->stage = DROP_REFERENCE;
6246 wc->shared_level = -1;
6247 path->slots[level] = 0;
6248
6249 /*
6250 * check reference count again if the block isn't locked.
6251 * we should start walking down the tree again if reference
6252 * count is one.
6253 */
6254 if (!path->locks[level]) {
6255 BUG_ON(level == 0);
6256 btrfs_tree_lock(eb);
6257 btrfs_set_lock_blocking(eb);
6258 path->locks[level] = 1;
6259
6260 ret = btrfs_lookup_extent_info(trans, root,
6261 eb->start, eb->len,
6262 &wc->refs[level],
6263 &wc->flags[level]);
6264 BUG_ON(ret);
6265 BUG_ON(wc->refs[level] == 0);
6266 if (wc->refs[level] == 1) {
6267 btrfs_tree_unlock(eb);
6268 path->locks[level] = 0;
6269 return 1;
6270 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006271 }
6272 }
6273
6274 /* wc->stage == DROP_REFERENCE */
6275 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6276
6277 if (wc->refs[level] == 1) {
6278 if (level == 0) {
6279 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6280 ret = btrfs_dec_ref(trans, root, eb, 1);
6281 else
6282 ret = btrfs_dec_ref(trans, root, eb, 0);
6283 BUG_ON(ret);
6284 }
6285 /* make block locked assertion in clean_tree_block happy */
6286 if (!path->locks[level] &&
6287 btrfs_header_generation(eb) == trans->transid) {
6288 btrfs_tree_lock(eb);
6289 btrfs_set_lock_blocking(eb);
6290 path->locks[level] = 1;
6291 }
6292 clean_tree_block(trans, root, eb);
6293 }
6294
6295 if (eb == root->node) {
6296 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6297 parent = eb->start;
6298 else
6299 BUG_ON(root->root_key.objectid !=
6300 btrfs_header_owner(eb));
6301 } else {
6302 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6303 parent = path->nodes[level + 1]->start;
6304 else
6305 BUG_ON(root->root_key.objectid !=
6306 btrfs_header_owner(path->nodes[level + 1]));
6307 }
6308
Yan, Zhengf0486c62010-05-16 10:46:25 -04006309 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006310out:
6311 wc->refs[level] = 0;
6312 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006313 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006314}
6315
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006316static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6317 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006318 struct btrfs_path *path,
6319 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006320{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006321 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006322 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006323 int ret;
6324
Yan Zheng2c47e6052009-06-27 21:07:35 -04006325 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006326 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006327 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006328 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006329
Yan Zheng2c47e6052009-06-27 21:07:35 -04006330 if (level == 0)
6331 break;
6332
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006333 if (path->slots[level] >=
6334 btrfs_header_nritems(path->nodes[level]))
6335 break;
6336
Yan, Zheng94fcca92009-10-09 09:25:16 -04006337 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006338 if (ret > 0) {
6339 path->slots[level]++;
6340 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00006341 } else if (ret < 0)
6342 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006343 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006344 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006345 return 0;
6346}
6347
Chris Masond3977122009-01-05 21:25:51 -05006348static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006349 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006350 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006351 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006352{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006353 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006354 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006355
Yan Zheng2c47e6052009-06-27 21:07:35 -04006356 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6357 while (level < max_level && path->nodes[level]) {
6358 wc->level = level;
6359 if (path->slots[level] + 1 <
6360 btrfs_header_nritems(path->nodes[level])) {
6361 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006362 return 0;
6363 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006364 ret = walk_up_proc(trans, root, path, wc);
6365 if (ret > 0)
6366 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006367
Yan Zheng2c47e6052009-06-27 21:07:35 -04006368 if (path->locks[level]) {
6369 btrfs_tree_unlock(path->nodes[level]);
6370 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006371 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006372 free_extent_buffer(path->nodes[level]);
6373 path->nodes[level] = NULL;
6374 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006375 }
6376 }
6377 return 1;
6378}
6379
Chris Mason9aca1d52007-03-13 11:09:37 -04006380/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006381 * drop a subvolume tree.
6382 *
6383 * this function traverses the tree freeing any blocks that only
6384 * referenced by the tree.
6385 *
6386 * when a shared tree block is found. this function decreases its
6387 * reference count by one. if update_ref is true, this function
6388 * also make sure backrefs for the shared block and all lower level
6389 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006390 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006391int btrfs_drop_snapshot(struct btrfs_root *root,
6392 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006393{
Chris Mason5caf2a02007-04-02 11:20:42 -04006394 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006395 struct btrfs_trans_handle *trans;
6396 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006397 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006398 struct walk_control *wc;
6399 struct btrfs_key key;
6400 int err = 0;
6401 int ret;
6402 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006403
Chris Mason5caf2a02007-04-02 11:20:42 -04006404 path = btrfs_alloc_path();
6405 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006406
Yan Zheng2c47e6052009-06-27 21:07:35 -04006407 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6408 BUG_ON(!wc);
6409
Yan, Zhenga22285a2010-05-16 10:48:46 -04006410 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006411 BUG_ON(IS_ERR(trans));
6412
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006413 if (block_rsv)
6414 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006415
Chris Mason9f3a7422007-08-07 15:52:19 -04006416 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006417 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006418 path->nodes[level] = btrfs_lock_root_node(root);
6419 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006420 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006421 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006422 memset(&wc->update_progress, 0,
6423 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006424 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006425 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006426 memcpy(&wc->update_progress, &key,
6427 sizeof(wc->update_progress));
6428
Chris Mason6702ed42007-08-07 16:15:09 -04006429 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006430 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006431 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006432 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6433 path->lowest_level = 0;
6434 if (ret < 0) {
6435 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006436 goto out;
6437 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006438 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006439
Chris Mason7d9eb122008-07-08 14:19:17 -04006440 /*
6441 * unlock our path, this is safe because only this
6442 * function is allowed to delete this snapshot
6443 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006444 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006445
Yan Zheng2c47e6052009-06-27 21:07:35 -04006446 level = btrfs_header_level(root->node);
6447 while (1) {
6448 btrfs_tree_lock(path->nodes[level]);
6449 btrfs_set_lock_blocking(path->nodes[level]);
6450
6451 ret = btrfs_lookup_extent_info(trans, root,
6452 path->nodes[level]->start,
6453 path->nodes[level]->len,
6454 &wc->refs[level],
6455 &wc->flags[level]);
6456 BUG_ON(ret);
6457 BUG_ON(wc->refs[level] == 0);
6458
6459 if (level == root_item->drop_level)
6460 break;
6461
6462 btrfs_tree_unlock(path->nodes[level]);
6463 WARN_ON(wc->refs[level] != 1);
6464 level--;
6465 }
6466 }
6467
6468 wc->level = level;
6469 wc->shared_level = -1;
6470 wc->stage = DROP_REFERENCE;
6471 wc->update_ref = update_ref;
6472 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006473 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006474
6475 while (1) {
6476 ret = walk_down_tree(trans, root, path, wc);
6477 if (ret < 0) {
6478 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006479 break;
6480 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006481
6482 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6483 if (ret < 0) {
6484 err = ret;
6485 break;
6486 }
6487
6488 if (ret > 0) {
6489 BUG_ON(wc->stage != DROP_REFERENCE);
6490 break;
6491 }
6492
6493 if (wc->stage == DROP_REFERENCE) {
6494 level = wc->level;
6495 btrfs_node_key(path->nodes[level],
6496 &root_item->drop_progress,
6497 path->slots[level]);
6498 root_item->drop_level = level;
6499 }
6500
6501 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006502 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006503 ret = btrfs_update_root(trans, tree_root,
6504 &root->root_key,
6505 root_item);
6506 BUG_ON(ret);
6507
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006508 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006509 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006510 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006511 if (block_rsv)
6512 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006513 }
Chris Mason20524f02007-03-10 06:35:47 -05006514 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006515 btrfs_release_path(root, path);
6516 BUG_ON(err);
6517
6518 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6519 BUG_ON(ret);
6520
Yan, Zheng76dda932009-09-21 16:00:26 -04006521 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6522 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6523 NULL, NULL);
6524 BUG_ON(ret < 0);
6525 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006526 /* if we fail to delete the orphan item this time
6527 * around, it'll get picked up the next time.
6528 *
6529 * The most common failure here is just -ENOENT.
6530 */
6531 btrfs_del_orphan_item(trans, tree_root,
6532 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006533 }
6534 }
6535
6536 if (root->in_radix) {
6537 btrfs_free_fs_root(tree_root->fs_info, root);
6538 } else {
6539 free_extent_buffer(root->node);
6540 free_extent_buffer(root->commit_root);
6541 kfree(root);
6542 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006543out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006544 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006545 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006546 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006547 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006548}
Chris Mason9078a3e2007-04-26 16:46:15 -04006549
Yan Zheng2c47e6052009-06-27 21:07:35 -04006550/*
6551 * drop subtree rooted at tree block 'node'.
6552 *
6553 * NOTE: this function will unlock and release tree block 'node'
6554 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006555int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6556 struct btrfs_root *root,
6557 struct extent_buffer *node,
6558 struct extent_buffer *parent)
6559{
6560 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006561 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006562 int level;
6563 int parent_level;
6564 int ret = 0;
6565 int wret;
6566
Yan Zheng2c47e6052009-06-27 21:07:35 -04006567 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6568
Yan Zhengf82d02d2008-10-29 14:49:05 -04006569 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006570 if (!path)
6571 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006572
Yan Zheng2c47e6052009-06-27 21:07:35 -04006573 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006574 if (!wc) {
6575 btrfs_free_path(path);
6576 return -ENOMEM;
6577 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006578
Chris Masonb9447ef2009-03-09 11:45:38 -04006579 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006580 parent_level = btrfs_header_level(parent);
6581 extent_buffer_get(parent);
6582 path->nodes[parent_level] = parent;
6583 path->slots[parent_level] = btrfs_header_nritems(parent);
6584
Chris Masonb9447ef2009-03-09 11:45:38 -04006585 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006586 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006587 path->nodes[level] = node;
6588 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006589 path->locks[level] = 1;
6590
6591 wc->refs[parent_level] = 1;
6592 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6593 wc->level = level;
6594 wc->shared_level = -1;
6595 wc->stage = DROP_REFERENCE;
6596 wc->update_ref = 0;
6597 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006598 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006599
6600 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006601 wret = walk_down_tree(trans, root, path, wc);
6602 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006603 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006604 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006605 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006606
Yan Zheng2c47e6052009-06-27 21:07:35 -04006607 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006608 if (wret < 0)
6609 ret = wret;
6610 if (wret != 0)
6611 break;
6612 }
6613
Yan Zheng2c47e6052009-06-27 21:07:35 -04006614 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006615 btrfs_free_path(path);
6616 return ret;
6617}
6618
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006619#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04006620static unsigned long calc_ra(unsigned long start, unsigned long last,
6621 unsigned long nr)
6622{
6623 return min(last, start + nr - 1);
6624}
6625
Chris Masond3977122009-01-05 21:25:51 -05006626static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05006627 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05006628{
6629 u64 page_start;
6630 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04006631 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006632 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006633 unsigned long i;
6634 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05006635 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05006636 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04006637 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04006638 unsigned int total_read = 0;
6639 unsigned int total_dirty = 0;
6640 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05006641
6642 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00006643 if (!ra)
6644 return -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006645
6646 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006647 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05006648 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6649
Zheng Yan1a40e232008-09-26 10:09:34 -04006650 /* make sure the dirty trick played by the caller work */
6651 ret = invalidate_inode_pages2_range(inode->i_mapping,
6652 first_index, last_index);
6653 if (ret)
6654 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04006655
Chris Mason4313b392008-01-03 09:08:48 -05006656 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05006657
Zheng Yan1a40e232008-09-26 10:09:34 -04006658 for (i = first_index ; i <= last_index; i++) {
6659 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04006660 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04006661 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04006662 }
6663 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04006664again:
6665 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04006666 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05006667 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04006668 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006669 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006670 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04006671 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006672 if (!PageUptodate(page)) {
6673 btrfs_readpage(NULL, page);
6674 lock_page(page);
6675 if (!PageUptodate(page)) {
6676 unlock_page(page);
6677 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006678 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05006679 goto out_unlock;
6680 }
6681 }
Chris Masonec44a352008-04-28 15:29:52 -04006682 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04006683
Chris Masonedbd8d42007-12-21 16:27:24 -05006684 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6685 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006686 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006687
Chris Mason3eaa2882008-07-24 11:57:52 -04006688 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6689 if (ordered) {
6690 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6691 unlock_page(page);
6692 page_cache_release(page);
6693 btrfs_start_ordered_extent(inode, ordered, 1);
6694 btrfs_put_ordered_extent(ordered);
6695 goto again;
6696 }
6697 set_page_extent_mapped(page);
6698
Zheng Yan1a40e232008-09-26 10:09:34 -04006699 if (i == first_index)
6700 set_extent_bits(io_tree, page_start, page_end,
6701 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006702 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04006703
Chris Masona061fc82008-05-07 11:43:44 -04006704 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006705 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006706
Chris Masond1310b22008-01-24 16:13:08 -05006707 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006708 unlock_page(page);
6709 page_cache_release(page);
6710 }
6711
6712out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04006713 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05006714 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006715 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04006716 return ret;
6717}
6718
Chris Masond3977122009-01-05 21:25:51 -05006719static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006720 struct btrfs_key *extent_key,
6721 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05006722{
Zheng Yan1a40e232008-09-26 10:09:34 -04006723 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6724 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6725 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04006726 u64 start = extent_key->objectid - offset;
6727 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006728
6729 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +00006730 BUG_ON(!em);
Zheng Yan1a40e232008-09-26 10:09:34 -04006731
Yan Zheng66435582008-10-30 14:19:50 -04006732 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006733 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04006734 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04006735 em->block_start = extent_key->objectid;
6736 em->bdev = root->fs_info->fs_devices->latest_bdev;
6737 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6738
6739 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04006740 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006741 while (1) {
6742 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04006743 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006744 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04006745 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006746 if (ret != -EEXIST) {
6747 free_extent_map(em);
6748 break;
6749 }
Yan Zheng66435582008-10-30 14:19:50 -04006750 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006751 }
Yan Zheng66435582008-10-30 14:19:50 -04006752 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006753
Yan Zheng66435582008-10-30 14:19:50 -04006754 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04006755}
6756
6757struct btrfs_ref_path {
6758 u64 extent_start;
6759 u64 nodes[BTRFS_MAX_LEVEL];
6760 u64 root_objectid;
6761 u64 root_generation;
6762 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006763 u32 num_refs;
6764 int lowest_level;
6765 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006766 int shared_level;
6767
6768 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6769 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04006770};
6771
6772struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04006773 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04006774 u64 disk_bytenr;
6775 u64 disk_num_bytes;
6776 u64 offset;
6777 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04006778 u8 compression;
6779 u8 encryption;
6780 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04006781};
6782
6783static int is_cowonly_root(u64 root_objectid)
6784{
6785 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6786 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6787 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6788 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05006789 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6790 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04006791 return 1;
6792 return 0;
6793}
6794
Chris Masond3977122009-01-05 21:25:51 -05006795static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006796 struct btrfs_root *extent_root,
6797 struct btrfs_ref_path *ref_path,
6798 int first_time)
6799{
6800 struct extent_buffer *leaf;
6801 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05006802 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05006803 struct btrfs_key key;
6804 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04006805 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05006806 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04006807 int level;
6808 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006809
Zheng Yan1a40e232008-09-26 10:09:34 -04006810 path = btrfs_alloc_path();
6811 if (!path)
6812 return -ENOMEM;
6813
Zheng Yan1a40e232008-09-26 10:09:34 -04006814 if (first_time) {
6815 ref_path->lowest_level = -1;
6816 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006817 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006818 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04006819 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006820walk_down:
6821 level = ref_path->current_level - 1;
6822 while (level >= -1) {
6823 u64 parent;
6824 if (level < ref_path->lowest_level)
6825 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006826
Chris Masond3977122009-01-05 21:25:51 -05006827 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006828 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006829 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006830 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006831 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006832
Zheng Yan1a40e232008-09-26 10:09:34 -04006833 parent = ref_path->nodes[level + 1];
6834 ref_path->nodes[level + 1] = 0;
6835 ref_path->current_level = level;
6836 BUG_ON(parent == 0);
6837
6838 key.objectid = bytenr;
6839 key.offset = parent + 1;
6840 key.type = BTRFS_EXTENT_REF_KEY;
6841
6842 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006843 if (ret < 0)
6844 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006845 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006846
Chris Masonedbd8d42007-12-21 16:27:24 -05006847 leaf = path->nodes[0];
6848 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04006849 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04006850 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04006851 if (ret < 0)
6852 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006853 if (ret > 0)
6854 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04006855 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04006856 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006857
6858 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04006859 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04006860 found_key.type == BTRFS_EXTENT_REF_KEY) {
6861 if (level < ref_path->shared_level)
6862 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006863 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006864 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006865next:
6866 level--;
6867 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006868 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006869 }
6870 /* reached lowest level */
6871 ret = 1;
6872 goto out;
6873walk_up:
6874 level = ref_path->current_level;
6875 while (level < BTRFS_MAX_LEVEL - 1) {
6876 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006877
6878 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006879 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006880 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006881 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006882
Zheng Yan1a40e232008-09-26 10:09:34 -04006883 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006884
Zheng Yan1a40e232008-09-26 10:09:34 -04006885 key.objectid = bytenr;
6886 key.offset = 0;
6887 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006888
Zheng Yan1a40e232008-09-26 10:09:34 -04006889 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6890 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006891 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006892
6893 leaf = path->nodes[0];
6894 nritems = btrfs_header_nritems(leaf);
6895 if (path->slots[0] >= nritems) {
6896 ret = btrfs_next_leaf(extent_root, path);
6897 if (ret < 0)
6898 goto out;
6899 if (ret > 0) {
6900 /* the extent was freed by someone */
6901 if (ref_path->lowest_level == level)
6902 goto out;
6903 btrfs_release_path(extent_root, path);
6904 goto walk_down;
6905 }
6906 leaf = path->nodes[0];
6907 }
6908
6909 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6910 if (found_key.objectid != bytenr ||
6911 found_key.type != BTRFS_EXTENT_REF_KEY) {
6912 /* the extent was freed by someone */
6913 if (ref_path->lowest_level == level) {
6914 ret = 1;
6915 goto out;
6916 }
6917 btrfs_release_path(extent_root, path);
6918 goto walk_down;
6919 }
6920found:
6921 ref = btrfs_item_ptr(leaf, path->slots[0],
6922 struct btrfs_extent_ref);
6923 ref_objectid = btrfs_ref_objectid(leaf, ref);
6924 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6925 if (first_time) {
6926 level = (int)ref_objectid;
6927 BUG_ON(level >= BTRFS_MAX_LEVEL);
6928 ref_path->lowest_level = level;
6929 ref_path->current_level = level;
6930 ref_path->nodes[level] = bytenr;
6931 } else {
6932 WARN_ON(ref_objectid != level);
6933 }
6934 } else {
6935 WARN_ON(level != -1);
6936 }
6937 first_time = 0;
6938
6939 if (ref_path->lowest_level == level) {
6940 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006941 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6942 }
6943
6944 /*
6945 * the block is tree root or the block isn't in reference
6946 * counted tree.
6947 */
6948 if (found_key.objectid == found_key.offset ||
6949 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6950 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6951 ref_path->root_generation =
6952 btrfs_ref_generation(leaf, ref);
6953 if (level < 0) {
6954 /* special reference from the tree log */
6955 ref_path->nodes[0] = found_key.offset;
6956 ref_path->current_level = 0;
6957 }
6958 ret = 0;
6959 goto out;
6960 }
6961
6962 level++;
6963 BUG_ON(ref_path->nodes[level] != 0);
6964 ref_path->nodes[level] = found_key.offset;
6965 ref_path->current_level = level;
6966
6967 /*
6968 * the reference was created in the running transaction,
6969 * no need to continue walking up.
6970 */
6971 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6972 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6973 ref_path->root_generation =
6974 btrfs_ref_generation(leaf, ref);
6975 ret = 0;
6976 goto out;
6977 }
6978
6979 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006980 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006981 }
6982 /* reached max tree level, but no tree root found. */
6983 BUG();
6984out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006985 btrfs_free_path(path);
6986 return ret;
6987}
6988
6989static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6990 struct btrfs_root *extent_root,
6991 struct btrfs_ref_path *ref_path,
6992 u64 extent_start)
6993{
6994 memset(ref_path, 0, sizeof(*ref_path));
6995 ref_path->extent_start = extent_start;
6996
6997 return __next_ref_path(trans, extent_root, ref_path, 1);
6998}
6999
7000static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
7001 struct btrfs_root *extent_root,
7002 struct btrfs_ref_path *ref_path)
7003{
7004 return __next_ref_path(trans, extent_root, ref_path, 0);
7005}
7006
Chris Masond3977122009-01-05 21:25:51 -05007007static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04007008 struct btrfs_key *extent_key,
7009 u64 offset, int no_fragment,
7010 struct disk_extent **extents,
7011 int *nr_extents)
7012{
7013 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
7014 struct btrfs_path *path;
7015 struct btrfs_file_extent_item *fi;
7016 struct extent_buffer *leaf;
7017 struct disk_extent *exts = *extents;
7018 struct btrfs_key found_key;
7019 u64 cur_pos;
7020 u64 last_byte;
7021 u32 nritems;
7022 int nr = 0;
7023 int max = *nr_extents;
7024 int ret;
7025
7026 WARN_ON(!no_fragment && *extents);
7027 if (!exts) {
7028 max = 1;
7029 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
7030 if (!exts)
7031 return -ENOMEM;
7032 }
7033
7034 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007035 if (!path) {
7036 if (exts != *extents)
7037 kfree(exts);
7038 return -ENOMEM;
7039 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007040
7041 cur_pos = extent_key->objectid - offset;
7042 last_byte = extent_key->objectid + extent_key->offset;
7043 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
7044 cur_pos, 0);
7045 if (ret < 0)
7046 goto out;
7047 if (ret > 0) {
7048 ret = -ENOENT;
7049 goto out;
7050 }
7051
7052 while (1) {
7053 leaf = path->nodes[0];
7054 nritems = btrfs_header_nritems(leaf);
7055 if (path->slots[0] >= nritems) {
7056 ret = btrfs_next_leaf(root, path);
7057 if (ret < 0)
7058 goto out;
7059 if (ret > 0)
7060 break;
7061 leaf = path->nodes[0];
7062 }
7063
7064 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7065 if (found_key.offset != cur_pos ||
7066 found_key.type != BTRFS_EXTENT_DATA_KEY ||
7067 found_key.objectid != reloc_inode->i_ino)
7068 break;
7069
7070 fi = btrfs_item_ptr(leaf, path->slots[0],
7071 struct btrfs_file_extent_item);
7072 if (btrfs_file_extent_type(leaf, fi) !=
7073 BTRFS_FILE_EXTENT_REG ||
7074 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7075 break;
7076
7077 if (nr == max) {
7078 struct disk_extent *old = exts;
7079 max *= 2;
7080 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
Yoshinori Sanodac97e52011-02-15 12:01:42 +00007081 if (!exts) {
7082 ret = -ENOMEM;
7083 goto out;
7084 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007085 memcpy(exts, old, sizeof(*exts) * nr);
7086 if (old != *extents)
7087 kfree(old);
7088 }
7089
7090 exts[nr].disk_bytenr =
7091 btrfs_file_extent_disk_bytenr(leaf, fi);
7092 exts[nr].disk_num_bytes =
7093 btrfs_file_extent_disk_num_bytes(leaf, fi);
7094 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
7095 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04007096 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7097 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
7098 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
7099 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
7100 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04007101 BUG_ON(exts[nr].offset > 0);
7102 BUG_ON(exts[nr].compression || exts[nr].encryption);
7103 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007104
7105 cur_pos += exts[nr].num_bytes;
7106 nr++;
7107
7108 if (cur_pos + offset >= last_byte)
7109 break;
7110
7111 if (no_fragment) {
7112 ret = 1;
7113 goto out;
7114 }
7115 path->slots[0]++;
7116 }
7117
Yan Zheng1f80e4d2008-12-19 10:59:04 -05007118 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04007119 if (cur_pos + offset < last_byte) {
7120 ret = -ENOENT;
7121 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05007122 }
7123 ret = 0;
7124out:
Zheng Yan1a40e232008-09-26 10:09:34 -04007125 btrfs_free_path(path);
7126 if (ret) {
7127 if (exts != *extents)
7128 kfree(exts);
7129 } else {
7130 *extents = exts;
7131 *nr_extents = nr;
7132 }
7133 return ret;
7134}
7135
Chris Masond3977122009-01-05 21:25:51 -05007136static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007137 struct btrfs_root *root,
7138 struct btrfs_path *path,
7139 struct btrfs_key *extent_key,
7140 struct btrfs_key *leaf_key,
7141 struct btrfs_ref_path *ref_path,
7142 struct disk_extent *new_extents,
7143 int nr_extents)
7144{
7145 struct extent_buffer *leaf;
7146 struct btrfs_file_extent_item *fi;
7147 struct inode *inode = NULL;
7148 struct btrfs_key key;
7149 u64 lock_start = 0;
7150 u64 lock_end = 0;
7151 u64 num_bytes;
7152 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05007153 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007154 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007155 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007156 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04007157 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04007158 int ret;
7159
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007160 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04007161 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007162 if (key.objectid < ref_path->owner_objectid ||
7163 (key.objectid == ref_path->owner_objectid &&
7164 key.type < BTRFS_EXTENT_DATA_KEY)) {
7165 key.objectid = ref_path->owner_objectid;
7166 key.type = BTRFS_EXTENT_DATA_KEY;
7167 key.offset = 0;
7168 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007169 }
7170
7171 while (1) {
7172 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7173 if (ret < 0)
7174 goto out;
7175
7176 leaf = path->nodes[0];
7177 nritems = btrfs_header_nritems(leaf);
7178next:
7179 if (extent_locked && ret > 0) {
7180 /*
7181 * the file extent item was modified by someone
7182 * before the extent got locked.
7183 */
Zheng Yan1a40e232008-09-26 10:09:34 -04007184 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7185 lock_end, GFP_NOFS);
7186 extent_locked = 0;
7187 }
7188
7189 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007190 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04007191 break;
7192
7193 BUG_ON(extent_locked);
7194 ret = btrfs_next_leaf(root, path);
7195 if (ret < 0)
7196 goto out;
7197 if (ret > 0)
7198 break;
7199 leaf = path->nodes[0];
7200 nritems = btrfs_header_nritems(leaf);
7201 }
7202
7203 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7204
7205 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7206 if ((key.objectid > ref_path->owner_objectid) ||
7207 (key.objectid == ref_path->owner_objectid &&
7208 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05007209 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007210 break;
7211 }
7212
7213 if (inode && key.objectid != inode->i_ino) {
7214 BUG_ON(extent_locked);
7215 btrfs_release_path(root, path);
7216 mutex_unlock(&inode->i_mutex);
7217 iput(inode);
7218 inode = NULL;
7219 continue;
7220 }
7221
7222 if (key.type != BTRFS_EXTENT_DATA_KEY) {
7223 path->slots[0]++;
7224 ret = 1;
7225 goto next;
7226 }
7227 fi = btrfs_item_ptr(leaf, path->slots[0],
7228 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04007229 extent_type = btrfs_file_extent_type(leaf, fi);
7230 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
7231 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04007232 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
7233 extent_key->objectid)) {
7234 path->slots[0]++;
7235 ret = 1;
7236 goto next;
7237 }
7238
7239 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7240 ext_offset = btrfs_file_extent_offset(leaf, fi);
7241
Yan Zheng86288a12009-01-21 10:49:16 -05007242 if (search_end == (u64)-1) {
7243 search_end = key.offset - ext_offset +
7244 btrfs_file_extent_ram_bytes(leaf, fi);
7245 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007246
7247 if (!extent_locked) {
7248 lock_start = key.offset;
7249 lock_end = lock_start + num_bytes - 1;
7250 } else {
Yan Zheng66435582008-10-30 14:19:50 -04007251 if (lock_start > key.offset ||
7252 lock_end + 1 < key.offset + num_bytes) {
7253 unlock_extent(&BTRFS_I(inode)->io_tree,
7254 lock_start, lock_end, GFP_NOFS);
7255 extent_locked = 0;
7256 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007257 }
7258
7259 if (!inode) {
7260 btrfs_release_path(root, path);
7261
7262 inode = btrfs_iget_locked(root->fs_info->sb,
7263 key.objectid, root);
7264 if (inode->i_state & I_NEW) {
7265 BTRFS_I(inode)->root = root;
7266 BTRFS_I(inode)->location.objectid =
7267 key.objectid;
7268 BTRFS_I(inode)->location.type =
7269 BTRFS_INODE_ITEM_KEY;
7270 BTRFS_I(inode)->location.offset = 0;
7271 btrfs_read_locked_inode(inode);
7272 unlock_new_inode(inode);
7273 }
7274 /*
7275 * some code call btrfs_commit_transaction while
7276 * holding the i_mutex, so we can't use mutex_lock
7277 * here.
7278 */
7279 if (is_bad_inode(inode) ||
7280 !mutex_trylock(&inode->i_mutex)) {
7281 iput(inode);
7282 inode = NULL;
7283 key.offset = (u64)-1;
7284 goto skip;
7285 }
7286 }
7287
7288 if (!extent_locked) {
7289 struct btrfs_ordered_extent *ordered;
7290
7291 btrfs_release_path(root, path);
7292
7293 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7294 lock_end, GFP_NOFS);
7295 ordered = btrfs_lookup_first_ordered_extent(inode,
7296 lock_end);
7297 if (ordered &&
7298 ordered->file_offset <= lock_end &&
7299 ordered->file_offset + ordered->len > lock_start) {
7300 unlock_extent(&BTRFS_I(inode)->io_tree,
7301 lock_start, lock_end, GFP_NOFS);
7302 btrfs_start_ordered_extent(inode, ordered, 1);
7303 btrfs_put_ordered_extent(ordered);
7304 key.offset += num_bytes;
7305 goto skip;
7306 }
7307 if (ordered)
7308 btrfs_put_ordered_extent(ordered);
7309
Zheng Yan1a40e232008-09-26 10:09:34 -04007310 extent_locked = 1;
7311 continue;
7312 }
7313
7314 if (nr_extents == 1) {
7315 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04007316 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7317 new_extents[0].disk_bytenr);
7318 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7319 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007320 btrfs_mark_buffer_dirty(leaf);
7321
7322 btrfs_drop_extent_cache(inode, key.offset,
7323 key.offset + num_bytes - 1, 0);
7324
7325 ret = btrfs_inc_extent_ref(trans, root,
7326 new_extents[0].disk_bytenr,
7327 new_extents[0].disk_num_bytes,
7328 leaf->start,
7329 root->root_key.objectid,
7330 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007331 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007332 BUG_ON(ret);
7333
7334 ret = btrfs_free_extent(trans, root,
7335 extent_key->objectid,
7336 extent_key->offset,
7337 leaf->start,
7338 btrfs_header_owner(leaf),
7339 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007340 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007341 BUG_ON(ret);
7342
7343 btrfs_release_path(root, path);
7344 key.offset += num_bytes;
7345 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04007346 BUG_ON(1);
7347#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04007348 u64 alloc_hint;
7349 u64 extent_len;
7350 int i;
7351 /*
7352 * drop old extent pointer at first, then insert the
7353 * new pointers one bye one
7354 */
7355 btrfs_release_path(root, path);
7356 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7357 key.offset + num_bytes,
7358 key.offset, &alloc_hint);
7359 BUG_ON(ret);
7360
7361 for (i = 0; i < nr_extents; i++) {
7362 if (ext_offset >= new_extents[i].num_bytes) {
7363 ext_offset -= new_extents[i].num_bytes;
7364 continue;
7365 }
7366 extent_len = min(new_extents[i].num_bytes -
7367 ext_offset, num_bytes);
7368
7369 ret = btrfs_insert_empty_item(trans, root,
7370 path, &key,
7371 sizeof(*fi));
7372 BUG_ON(ret);
7373
7374 leaf = path->nodes[0];
7375 fi = btrfs_item_ptr(leaf, path->slots[0],
7376 struct btrfs_file_extent_item);
7377 btrfs_set_file_extent_generation(leaf, fi,
7378 trans->transid);
7379 btrfs_set_file_extent_type(leaf, fi,
7380 BTRFS_FILE_EXTENT_REG);
7381 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7382 new_extents[i].disk_bytenr);
7383 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7384 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04007385 btrfs_set_file_extent_ram_bytes(leaf, fi,
7386 new_extents[i].ram_bytes);
7387
7388 btrfs_set_file_extent_compression(leaf, fi,
7389 new_extents[i].compression);
7390 btrfs_set_file_extent_encryption(leaf, fi,
7391 new_extents[i].encryption);
7392 btrfs_set_file_extent_other_encoding(leaf, fi,
7393 new_extents[i].other_encoding);
7394
Zheng Yan1a40e232008-09-26 10:09:34 -04007395 btrfs_set_file_extent_num_bytes(leaf, fi,
7396 extent_len);
7397 ext_offset += new_extents[i].offset;
7398 btrfs_set_file_extent_offset(leaf, fi,
7399 ext_offset);
7400 btrfs_mark_buffer_dirty(leaf);
7401
7402 btrfs_drop_extent_cache(inode, key.offset,
7403 key.offset + extent_len - 1, 0);
7404
7405 ret = btrfs_inc_extent_ref(trans, root,
7406 new_extents[i].disk_bytenr,
7407 new_extents[i].disk_num_bytes,
7408 leaf->start,
7409 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007410 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007411 BUG_ON(ret);
7412 btrfs_release_path(root, path);
7413
Yan Zhenga76a3cd2008-10-09 11:46:29 -04007414 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04007415
7416 ext_offset = 0;
7417 num_bytes -= extent_len;
7418 key.offset += extent_len;
7419
7420 if (num_bytes == 0)
7421 break;
7422 }
7423 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007424#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04007425 }
7426
7427 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007428 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7429 lock_end, GFP_NOFS);
7430 extent_locked = 0;
7431 }
7432skip:
7433 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05007434 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007435 break;
7436
7437 cond_resched();
7438 }
7439 ret = 0;
7440out:
7441 btrfs_release_path(root, path);
7442 if (inode) {
7443 mutex_unlock(&inode->i_mutex);
7444 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007445 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7446 lock_end, GFP_NOFS);
7447 }
7448 iput(inode);
7449 }
7450 return ret;
7451}
7452
Zheng Yan1a40e232008-09-26 10:09:34 -04007453int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7454 struct btrfs_root *root,
7455 struct extent_buffer *buf, u64 orig_start)
7456{
7457 int level;
7458 int ret;
7459
7460 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7461 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7462
7463 level = btrfs_header_level(buf);
7464 if (level == 0) {
7465 struct btrfs_leaf_ref *ref;
7466 struct btrfs_leaf_ref *orig_ref;
7467
7468 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7469 if (!orig_ref)
7470 return -ENOENT;
7471
7472 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7473 if (!ref) {
7474 btrfs_free_leaf_ref(root, orig_ref);
7475 return -ENOMEM;
7476 }
7477
7478 ref->nritems = orig_ref->nritems;
7479 memcpy(ref->extents, orig_ref->extents,
7480 sizeof(ref->extents[0]) * ref->nritems);
7481
7482 btrfs_free_leaf_ref(root, orig_ref);
7483
7484 ref->root_gen = trans->transid;
7485 ref->bytenr = buf->start;
7486 ref->owner = btrfs_header_owner(buf);
7487 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05007488
Zheng Yan1a40e232008-09-26 10:09:34 -04007489 ret = btrfs_add_leaf_ref(root, ref, 0);
7490 WARN_ON(ret);
7491 btrfs_free_leaf_ref(root, ref);
7492 }
7493 return 0;
7494}
7495
Chris Masond3977122009-01-05 21:25:51 -05007496static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007497 struct extent_buffer *leaf,
7498 struct btrfs_block_group_cache *group,
7499 struct btrfs_root *target_root)
7500{
7501 struct btrfs_key key;
7502 struct inode *inode = NULL;
7503 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007504 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04007505 u64 num_bytes;
7506 u64 skip_objectid = 0;
7507 u32 nritems;
7508 u32 i;
7509
7510 nritems = btrfs_header_nritems(leaf);
7511 for (i = 0; i < nritems; i++) {
7512 btrfs_item_key_to_cpu(leaf, &key, i);
7513 if (key.objectid == skip_objectid ||
7514 key.type != BTRFS_EXTENT_DATA_KEY)
7515 continue;
7516 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7517 if (btrfs_file_extent_type(leaf, fi) ==
7518 BTRFS_FILE_EXTENT_INLINE)
7519 continue;
7520 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7521 continue;
7522 if (!inode || inode->i_ino != key.objectid) {
7523 iput(inode);
7524 inode = btrfs_ilookup(target_root->fs_info->sb,
7525 key.objectid, target_root, 1);
7526 }
7527 if (!inode) {
7528 skip_objectid = key.objectid;
7529 continue;
7530 }
7531 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7532
Josef Bacik2ac55d42010-02-03 19:33:23 +00007533 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7534 key.offset + num_bytes - 1, 0, &cached_state,
7535 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007536 btrfs_drop_extent_cache(inode, key.offset,
7537 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007538 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7539 key.offset + num_bytes - 1, &cached_state,
7540 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007541 cond_resched();
7542 }
7543 iput(inode);
7544 return 0;
7545}
7546
Chris Masond3977122009-01-05 21:25:51 -05007547static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007548 struct btrfs_root *root,
7549 struct extent_buffer *leaf,
7550 struct btrfs_block_group_cache *group,
7551 struct inode *reloc_inode)
7552{
7553 struct btrfs_key key;
7554 struct btrfs_key extent_key;
7555 struct btrfs_file_extent_item *fi;
7556 struct btrfs_leaf_ref *ref;
7557 struct disk_extent *new_extent;
7558 u64 bytenr;
7559 u64 num_bytes;
7560 u32 nritems;
7561 u32 i;
7562 int ext_index;
7563 int nr_extent;
7564 int ret;
7565
7566 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007567 if (!new_extent)
7568 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007569
7570 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7571 BUG_ON(!ref);
7572
7573 ext_index = -1;
7574 nritems = btrfs_header_nritems(leaf);
7575 for (i = 0; i < nritems; i++) {
7576 btrfs_item_key_to_cpu(leaf, &key, i);
7577 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7578 continue;
7579 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7580 if (btrfs_file_extent_type(leaf, fi) ==
7581 BTRFS_FILE_EXTENT_INLINE)
7582 continue;
7583 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7584 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7585 if (bytenr == 0)
7586 continue;
7587
7588 ext_index++;
7589 if (bytenr >= group->key.objectid + group->key.offset ||
7590 bytenr + num_bytes <= group->key.objectid)
7591 continue;
7592
7593 extent_key.objectid = bytenr;
7594 extent_key.offset = num_bytes;
7595 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7596 nr_extent = 1;
7597 ret = get_new_locations(reloc_inode, &extent_key,
7598 group->key.objectid, 1,
7599 &new_extent, &nr_extent);
7600 if (ret > 0)
7601 continue;
7602 BUG_ON(ret < 0);
7603
7604 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7605 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7606 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7607 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7608
Zheng Yan1a40e232008-09-26 10:09:34 -04007609 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7610 new_extent->disk_bytenr);
7611 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7612 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007613 btrfs_mark_buffer_dirty(leaf);
7614
7615 ret = btrfs_inc_extent_ref(trans, root,
7616 new_extent->disk_bytenr,
7617 new_extent->disk_num_bytes,
7618 leaf->start,
7619 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007620 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007621 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04007622
Zheng Yan1a40e232008-09-26 10:09:34 -04007623 ret = btrfs_free_extent(trans, root,
7624 bytenr, num_bytes, leaf->start,
7625 btrfs_header_owner(leaf),
7626 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007627 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007628 BUG_ON(ret);
7629 cond_resched();
7630 }
7631 kfree(new_extent);
7632 BUG_ON(ext_index + 1 != ref->nritems);
7633 btrfs_free_leaf_ref(root, ref);
7634 return 0;
7635}
7636
Yan Zhengf82d02d2008-10-29 14:49:05 -04007637int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7638 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04007639{
7640 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007641 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007642
7643 if (root->reloc_root) {
7644 reloc_root = root->reloc_root;
7645 root->reloc_root = NULL;
7646 list_add(&reloc_root->dead_list,
7647 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007648
7649 btrfs_set_root_bytenr(&reloc_root->root_item,
7650 reloc_root->node->start);
7651 btrfs_set_root_level(&root->root_item,
7652 btrfs_header_level(reloc_root->node));
7653 memset(&reloc_root->root_item.drop_progress, 0,
7654 sizeof(struct btrfs_disk_key));
7655 reloc_root->root_item.drop_level = 0;
7656
7657 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7658 &reloc_root->root_key,
7659 &reloc_root->root_item);
7660 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007661 }
7662 return 0;
7663}
7664
7665int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7666{
7667 struct btrfs_trans_handle *trans;
7668 struct btrfs_root *reloc_root;
7669 struct btrfs_root *prev_root = NULL;
7670 struct list_head dead_roots;
7671 int ret;
7672 unsigned long nr;
7673
7674 INIT_LIST_HEAD(&dead_roots);
7675 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7676
7677 while (!list_empty(&dead_roots)) {
7678 reloc_root = list_entry(dead_roots.prev,
7679 struct btrfs_root, dead_list);
7680 list_del_init(&reloc_root->dead_list);
7681
7682 BUG_ON(reloc_root->commit_root != NULL);
7683 while (1) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04007684 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007685 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007686
7687 mutex_lock(&root->fs_info->drop_mutex);
7688 ret = btrfs_drop_snapshot(trans, reloc_root);
7689 if (ret != -EAGAIN)
7690 break;
7691 mutex_unlock(&root->fs_info->drop_mutex);
7692
7693 nr = trans->blocks_used;
7694 ret = btrfs_end_transaction(trans, root);
7695 BUG_ON(ret);
7696 btrfs_btree_balance_dirty(root, nr);
7697 }
7698
7699 free_extent_buffer(reloc_root->node);
7700
7701 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7702 &reloc_root->root_key);
7703 BUG_ON(ret);
7704 mutex_unlock(&root->fs_info->drop_mutex);
7705
7706 nr = trans->blocks_used;
7707 ret = btrfs_end_transaction(trans, root);
7708 BUG_ON(ret);
7709 btrfs_btree_balance_dirty(root, nr);
7710
7711 kfree(prev_root);
7712 prev_root = reloc_root;
7713 }
7714 if (prev_root) {
7715 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7716 kfree(prev_root);
7717 }
7718 return 0;
7719}
7720
7721int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7722{
7723 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7724 return 0;
7725}
7726
7727int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7728{
7729 struct btrfs_root *reloc_root;
7730 struct btrfs_trans_handle *trans;
7731 struct btrfs_key location;
7732 int found;
7733 int ret;
7734
7735 mutex_lock(&root->fs_info->tree_reloc_mutex);
7736 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7737 BUG_ON(ret);
7738 found = !list_empty(&root->fs_info->dead_reloc_roots);
7739 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7740
7741 if (found) {
7742 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007743 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007744 ret = btrfs_commit_transaction(trans, root);
7745 BUG_ON(ret);
7746 }
7747
7748 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7749 location.offset = (u64)-1;
7750 location.type = BTRFS_ROOT_ITEM_KEY;
7751
7752 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7753 BUG_ON(!reloc_root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05007754 ret = btrfs_orphan_cleanup(reloc_root);
7755 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007756 return 0;
7757}
7758
Chris Masond3977122009-01-05 21:25:51 -05007759static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007760 struct btrfs_root *root)
7761{
7762 struct btrfs_root *reloc_root;
7763 struct extent_buffer *eb;
7764 struct btrfs_root_item *root_item;
7765 struct btrfs_key root_key;
7766 int ret;
7767
7768 BUG_ON(!root->ref_cows);
7769 if (root->reloc_root)
7770 return 0;
7771
7772 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007773 if (!root_item)
7774 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007775
7776 ret = btrfs_copy_root(trans, root, root->commit_root,
7777 &eb, BTRFS_TREE_RELOC_OBJECTID);
7778 BUG_ON(ret);
7779
7780 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7781 root_key.offset = root->root_key.objectid;
7782 root_key.type = BTRFS_ROOT_ITEM_KEY;
7783
7784 memcpy(root_item, &root->root_item, sizeof(root_item));
7785 btrfs_set_root_refs(root_item, 0);
7786 btrfs_set_root_bytenr(root_item, eb->start);
7787 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04007788 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007789
7790 btrfs_tree_unlock(eb);
7791 free_extent_buffer(eb);
7792
7793 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7794 &root_key, root_item);
7795 BUG_ON(ret);
7796 kfree(root_item);
7797
7798 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7799 &root_key);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007800 BUG_ON(IS_ERR(reloc_root));
Zheng Yan1a40e232008-09-26 10:09:34 -04007801 reloc_root->last_trans = trans->transid;
7802 reloc_root->commit_root = NULL;
7803 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7804
7805 root->reloc_root = reloc_root;
7806 return 0;
7807}
7808
7809/*
7810 * Core function of space balance.
7811 *
7812 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04007813 * counted roots. There is one reloc tree for each subvol, and all
7814 * reloc trees share same root key objectid. Reloc trees are snapshots
7815 * of the latest committed roots of subvols (root->commit_root).
7816 *
7817 * To relocate a tree block referenced by a subvol, there are two steps.
7818 * COW the block through subvol's reloc tree, then update block pointer
7819 * in the subvol to point to the new block. Since all reloc trees share
7820 * same root key objectid, doing special handing for tree blocks owned
7821 * by them is easy. Once a tree block has been COWed in one reloc tree,
7822 * we can use the resulting new block directly when the same block is
7823 * required to COW again through other reloc trees. By this way, relocated
7824 * tree blocks are shared between reloc trees, so they are also shared
7825 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04007826 */
Chris Masond3977122009-01-05 21:25:51 -05007827static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007828 struct btrfs_root *root,
7829 struct btrfs_path *path,
7830 struct btrfs_key *first_key,
7831 struct btrfs_ref_path *ref_path,
7832 struct btrfs_block_group_cache *group,
7833 struct inode *reloc_inode)
7834{
7835 struct btrfs_root *reloc_root;
7836 struct extent_buffer *eb = NULL;
7837 struct btrfs_key *keys;
7838 u64 *nodes;
7839 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007840 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04007841 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007842 int ret;
7843
7844 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7845 lowest_level = ref_path->owner_objectid;
7846
Yan Zhengf82d02d2008-10-29 14:49:05 -04007847 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007848 path->lowest_level = lowest_level;
7849 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7850 BUG_ON(ret < 0);
7851 path->lowest_level = 0;
7852 btrfs_release_path(root, path);
7853 return 0;
7854 }
7855
Zheng Yan1a40e232008-09-26 10:09:34 -04007856 mutex_lock(&root->fs_info->tree_reloc_mutex);
7857 ret = init_reloc_tree(trans, root);
7858 BUG_ON(ret);
7859 reloc_root = root->reloc_root;
7860
Yan Zhengf82d02d2008-10-29 14:49:05 -04007861 shared_level = ref_path->shared_level;
7862 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007863
Yan Zhengf82d02d2008-10-29 14:49:05 -04007864 keys = ref_path->node_keys;
7865 nodes = ref_path->new_nodes;
7866 memset(&keys[shared_level + 1], 0,
7867 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7868 memset(&nodes[shared_level + 1], 0,
7869 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04007870
Yan Zhengf82d02d2008-10-29 14:49:05 -04007871 if (nodes[lowest_level] == 0) {
7872 path->lowest_level = lowest_level;
7873 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7874 0, 1);
7875 BUG_ON(ret);
7876 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7877 eb = path->nodes[level];
7878 if (!eb || eb == reloc_root->node)
7879 break;
7880 nodes[level] = eb->start;
7881 if (level == 0)
7882 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7883 else
7884 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7885 }
Yan Zheng2b820322008-11-17 21:11:30 -05007886 if (nodes[0] &&
7887 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007888 eb = path->nodes[0];
7889 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7890 group, reloc_inode);
7891 BUG_ON(ret);
7892 }
7893 btrfs_release_path(reloc_root, path);
7894 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007895 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007896 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007897 BUG_ON(ret);
7898 }
7899
Zheng Yan1a40e232008-09-26 10:09:34 -04007900 /*
7901 * replace tree blocks in the fs tree with tree blocks in
7902 * the reloc tree.
7903 */
7904 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7905 BUG_ON(ret < 0);
7906
7907 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007908 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7909 0, 0);
7910 BUG_ON(ret);
7911 extent_buffer_get(path->nodes[0]);
7912 eb = path->nodes[0];
7913 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007914 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7915 BUG_ON(ret);
7916 free_extent_buffer(eb);
7917 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007918
Yan Zhengf82d02d2008-10-29 14:49:05 -04007919 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007920 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007921 return 0;
7922}
7923
Chris Masond3977122009-01-05 21:25:51 -05007924static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007925 struct btrfs_root *root,
7926 struct btrfs_path *path,
7927 struct btrfs_key *first_key,
7928 struct btrfs_ref_path *ref_path)
7929{
7930 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007931
7932 ret = relocate_one_path(trans, root, path, first_key,
7933 ref_path, NULL, NULL);
7934 BUG_ON(ret);
7935
Zheng Yan1a40e232008-09-26 10:09:34 -04007936 return 0;
7937}
7938
Chris Masond3977122009-01-05 21:25:51 -05007939static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007940 struct btrfs_root *extent_root,
7941 struct btrfs_path *path,
7942 struct btrfs_key *extent_key)
7943{
7944 int ret;
7945
Zheng Yan1a40e232008-09-26 10:09:34 -04007946 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7947 if (ret)
7948 goto out;
7949 ret = btrfs_del_item(trans, extent_root, path);
7950out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007951 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007952 return ret;
7953}
7954
Chris Masond3977122009-01-05 21:25:51 -05007955static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007956 struct btrfs_ref_path *ref_path)
7957{
7958 struct btrfs_key root_key;
7959
7960 root_key.objectid = ref_path->root_objectid;
7961 root_key.type = BTRFS_ROOT_ITEM_KEY;
7962 if (is_cowonly_root(ref_path->root_objectid))
7963 root_key.offset = 0;
7964 else
7965 root_key.offset = (u64)-1;
7966
7967 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7968}
7969
Chris Masond3977122009-01-05 21:25:51 -05007970static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007971 struct btrfs_path *path,
7972 struct btrfs_key *extent_key,
7973 struct btrfs_block_group_cache *group,
7974 struct inode *reloc_inode, int pass)
7975{
7976 struct btrfs_trans_handle *trans;
7977 struct btrfs_root *found_root;
7978 struct btrfs_ref_path *ref_path = NULL;
7979 struct disk_extent *new_extents = NULL;
7980 int nr_extents = 0;
7981 int loops;
7982 int ret;
7983 int level;
7984 struct btrfs_key first_key;
7985 u64 prev_block = 0;
7986
Zheng Yan1a40e232008-09-26 10:09:34 -04007987
7988 trans = btrfs_start_transaction(extent_root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007989 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007990
7991 if (extent_key->objectid == 0) {
7992 ret = del_extent_zero(trans, extent_root, path, extent_key);
7993 goto out;
7994 }
7995
7996 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7997 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007998 ret = -ENOMEM;
7999 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04008000 }
8001
8002 for (loops = 0; ; loops++) {
8003 if (loops == 0) {
8004 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
8005 extent_key->objectid);
8006 } else {
8007 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
8008 }
8009 if (ret < 0)
8010 goto out;
8011 if (ret > 0)
8012 break;
8013
8014 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
8015 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
8016 continue;
8017
8018 found_root = read_ref_root(extent_root->fs_info, ref_path);
8019 BUG_ON(!found_root);
8020 /*
8021 * for reference counted tree, only process reference paths
8022 * rooted at the latest committed root.
8023 */
8024 if (found_root->ref_cows &&
8025 ref_path->root_generation != found_root->root_key.offset)
8026 continue;
8027
8028 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8029 if (pass == 0) {
8030 /*
8031 * copy data extents to new locations
8032 */
8033 u64 group_start = group->key.objectid;
8034 ret = relocate_data_extent(reloc_inode,
8035 extent_key,
8036 group_start);
8037 if (ret < 0)
8038 goto out;
8039 break;
8040 }
8041 level = 0;
8042 } else {
8043 level = ref_path->owner_objectid;
8044 }
8045
8046 if (prev_block != ref_path->nodes[level]) {
8047 struct extent_buffer *eb;
8048 u64 block_start = ref_path->nodes[level];
8049 u64 block_size = btrfs_level_size(found_root, level);
8050
8051 eb = read_tree_block(found_root, block_start,
8052 block_size, 0);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008053 if (!eb) {
8054 ret = -EIO;
8055 goto out;
8056 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008057 btrfs_tree_lock(eb);
8058 BUG_ON(level != btrfs_header_level(eb));
8059
8060 if (level == 0)
8061 btrfs_item_key_to_cpu(eb, &first_key, 0);
8062 else
8063 btrfs_node_key_to_cpu(eb, &first_key, 0);
8064
8065 btrfs_tree_unlock(eb);
8066 free_extent_buffer(eb);
8067 prev_block = block_start;
8068 }
8069
Yan Zheng24562422009-02-12 14:14:53 -05008070 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05008071 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05008072 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05008073 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8074 /*
8075 * try to update data extent references while
8076 * keeping metadata shared between snapshots.
8077 */
8078 if (pass == 1) {
8079 ret = relocate_one_path(trans, found_root,
8080 path, &first_key, ref_path,
8081 group, reloc_inode);
8082 if (ret < 0)
8083 goto out;
8084 continue;
8085 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008086 /*
8087 * use fallback method to process the remaining
8088 * references.
8089 */
8090 if (!new_extents) {
8091 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04008092 new_extents = kmalloc(sizeof(*new_extents),
8093 GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04008094 if (!new_extents) {
8095 ret = -ENOMEM;
8096 goto out;
8097 }
Yan Zhengd899e052008-10-30 14:25:28 -04008098 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008099 ret = get_new_locations(reloc_inode,
8100 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04008101 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04008102 &new_extents,
8103 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04008104 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04008105 goto out;
8106 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008107 ret = replace_one_extent(trans, found_root,
8108 path, extent_key,
8109 &first_key, ref_path,
8110 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05008111 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04008112 ret = relocate_tree_block(trans, found_root, path,
8113 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04008114 }
8115 if (ret < 0)
8116 goto out;
8117 }
8118 ret = 0;
8119out:
8120 btrfs_end_transaction(trans, extent_root);
8121 kfree(new_extents);
8122 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05008123 return ret;
8124}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008125#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05008126
Chris Masonec44a352008-04-28 15:29:52 -04008127static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8128{
8129 u64 num_devices;
8130 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
8131 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8132
Chris Masoncd02dca2010-12-13 14:56:23 -05008133 /*
8134 * we add in the count of missing devices because we want
8135 * to make sure that any RAID levels on a degraded FS
8136 * continue to be honored.
8137 */
8138 num_devices = root->fs_info->fs_devices->rw_devices +
8139 root->fs_info->fs_devices->missing_devices;
8140
Chris Masonec44a352008-04-28 15:29:52 -04008141 if (num_devices == 1) {
8142 stripped |= BTRFS_BLOCK_GROUP_DUP;
8143 stripped = flags & ~stripped;
8144
8145 /* turn raid0 into single device chunks */
8146 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8147 return stripped;
8148
8149 /* turn mirroring into duplication */
8150 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8151 BTRFS_BLOCK_GROUP_RAID10))
8152 return stripped | BTRFS_BLOCK_GROUP_DUP;
8153 return flags;
8154 } else {
8155 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04008156 if (flags & stripped)
8157 return flags;
8158
8159 stripped |= BTRFS_BLOCK_GROUP_DUP;
8160 stripped = flags & ~stripped;
8161
8162 /* switch duplicated blocks with raid1 */
8163 if (flags & BTRFS_BLOCK_GROUP_DUP)
8164 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8165
8166 /* turn single device chunks into raid0 */
8167 return stripped | BTRFS_BLOCK_GROUP_RAID0;
8168 }
8169 return flags;
8170}
8171
Yan, Zhengf0486c62010-05-16 10:46:25 -04008172static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04008173{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008174 struct btrfs_space_info *sinfo = cache->space_info;
8175 u64 num_bytes;
8176 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04008177
Yan, Zhengf0486c62010-05-16 10:46:25 -04008178 if (cache->ro)
8179 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04008180
Yan, Zhengf0486c62010-05-16 10:46:25 -04008181 spin_lock(&sinfo->lock);
8182 spin_lock(&cache->lock);
8183 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8184 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04008185
Yan, Zhengf0486c62010-05-16 10:46:25 -04008186 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
8187 sinfo->bytes_may_use + sinfo->bytes_readonly +
Chris Mason65e53412010-12-24 06:41:52 -05008188 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04008189 sinfo->bytes_readonly += num_bytes;
8190 sinfo->bytes_reserved += cache->reserved_pinned;
8191 cache->reserved_pinned = 0;
8192 cache->ro = 1;
8193 ret = 0;
8194 }
Chris Mason65e53412010-12-24 06:41:52 -05008195
Yan, Zhengf0486c62010-05-16 10:46:25 -04008196 spin_unlock(&cache->lock);
8197 spin_unlock(&sinfo->lock);
8198 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04008199}
8200
Yan, Zhengf0486c62010-05-16 10:46:25 -04008201int btrfs_set_block_group_ro(struct btrfs_root *root,
8202 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008203
8204{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008205 struct btrfs_trans_handle *trans;
8206 u64 alloc_flags;
8207 int ret;
8208
8209 BUG_ON(cache->ro);
8210
Josef Bacik7a7eaa402011-04-13 12:54:33 -04008211 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008212 BUG_ON(IS_ERR(trans));
8213
8214 alloc_flags = update_block_group_flags(root, cache->flags);
8215 if (alloc_flags != cache->flags)
Chris Mason0e4f8f82011-04-15 16:05:44 -04008216 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8217 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008218
8219 ret = set_block_group_ro(cache);
8220 if (!ret)
8221 goto out;
8222 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Chris Mason0e4f8f82011-04-15 16:05:44 -04008223 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8224 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008225 if (ret < 0)
8226 goto out;
8227 ret = set_block_group_ro(cache);
8228out:
8229 btrfs_end_transaction(trans, root);
8230 return ret;
8231}
8232
Chris Masonc87f08c2011-02-16 13:57:04 -05008233int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8234 struct btrfs_root *root, u64 type)
8235{
8236 u64 alloc_flags = get_alloc_profile(root, type);
Chris Mason0e4f8f82011-04-15 16:05:44 -04008237 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8238 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05008239}
8240
Miao Xie6d07bce2011-01-05 10:07:31 +00008241/*
8242 * helper to account the unused space of all the readonly block group in the
8243 * list. takes mirrors into account.
8244 */
8245static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8246{
8247 struct btrfs_block_group_cache *block_group;
8248 u64 free_bytes = 0;
8249 int factor;
8250
8251 list_for_each_entry(block_group, groups_list, list) {
8252 spin_lock(&block_group->lock);
8253
8254 if (!block_group->ro) {
8255 spin_unlock(&block_group->lock);
8256 continue;
8257 }
8258
8259 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8260 BTRFS_BLOCK_GROUP_RAID10 |
8261 BTRFS_BLOCK_GROUP_DUP))
8262 factor = 2;
8263 else
8264 factor = 1;
8265
8266 free_bytes += (block_group->key.offset -
8267 btrfs_block_group_used(&block_group->item)) *
8268 factor;
8269
8270 spin_unlock(&block_group->lock);
8271 }
8272
8273 return free_bytes;
8274}
8275
8276/*
8277 * helper to account the unused space of all the readonly block group in the
8278 * space_info. takes mirrors into account.
8279 */
8280u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8281{
8282 int i;
8283 u64 free_bytes = 0;
8284
8285 spin_lock(&sinfo->lock);
8286
8287 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
8288 if (!list_empty(&sinfo->block_groups[i]))
8289 free_bytes += __btrfs_get_ro_block_group_free_space(
8290 &sinfo->block_groups[i]);
8291
8292 spin_unlock(&sinfo->lock);
8293
8294 return free_bytes;
8295}
8296
Yan, Zhengf0486c62010-05-16 10:46:25 -04008297int btrfs_set_block_group_rw(struct btrfs_root *root,
8298 struct btrfs_block_group_cache *cache)
8299{
8300 struct btrfs_space_info *sinfo = cache->space_info;
8301 u64 num_bytes;
8302
8303 BUG_ON(!cache->ro);
8304
8305 spin_lock(&sinfo->lock);
8306 spin_lock(&cache->lock);
8307 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8308 cache->bytes_super - btrfs_block_group_used(&cache->item);
8309 sinfo->bytes_readonly -= num_bytes;
8310 cache->ro = 0;
8311 spin_unlock(&cache->lock);
8312 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008313 return 0;
8314}
8315
Josef Bacikba1bf482009-09-11 16:11:19 -04008316/*
8317 * checks to see if its even possible to relocate this block group.
8318 *
8319 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8320 * ok to go ahead and try.
8321 */
8322int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008323{
Zheng Yan1a40e232008-09-26 10:09:34 -04008324 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008325 struct btrfs_space_info *space_info;
8326 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8327 struct btrfs_device *device;
8328 int full = 0;
8329 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008330
Josef Bacikba1bf482009-09-11 16:11:19 -04008331 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008332
Josef Bacikba1bf482009-09-11 16:11:19 -04008333 /* odd, couldn't find the block group, leave it alone */
8334 if (!block_group)
8335 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008336
Josef Bacikba1bf482009-09-11 16:11:19 -04008337 /* no bytes used, we're good */
8338 if (!btrfs_block_group_used(&block_group->item))
8339 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008340
Josef Bacikba1bf482009-09-11 16:11:19 -04008341 space_info = block_group->space_info;
8342 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008343
Josef Bacikba1bf482009-09-11 16:11:19 -04008344 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008345
Josef Bacikba1bf482009-09-11 16:11:19 -04008346 /*
8347 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008348 * relocate it unless we're able to allocate a new chunk below.
8349 *
8350 * Otherwise, we need to make sure we have room in the space to handle
8351 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008352 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008353 if ((space_info->total_bytes != block_group->key.offset) &&
8354 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04008355 space_info->bytes_pinned + space_info->bytes_readonly +
8356 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04008357 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008358 spin_unlock(&space_info->lock);
8359 goto out;
8360 }
8361 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008362
Josef Bacikba1bf482009-09-11 16:11:19 -04008363 /*
8364 * ok we don't have enough space, but maybe we have free space on our
8365 * devices to allocate new chunks for relocation, so loop through our
8366 * alloc devices and guess if we have enough space. However, if we
8367 * were marked as full, then we know there aren't enough chunks, and we
8368 * can just return.
8369 */
8370 ret = -1;
8371 if (full)
8372 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05008373
Josef Bacikba1bf482009-09-11 16:11:19 -04008374 mutex_lock(&root->fs_info->chunk_mutex);
8375 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8376 u64 min_free = btrfs_block_group_used(&block_group->item);
Miao Xie7bfc8372011-01-05 10:07:26 +00008377 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008378
Josef Bacikba1bf482009-09-11 16:11:19 -04008379 /*
8380 * check to make sure we can actually find a chunk with enough
8381 * space to fit our block group in.
8382 */
8383 if (device->total_bytes > device->bytes_used + min_free) {
8384 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008385 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008386 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05008387 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04008388 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008389 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008390 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008391 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05008392out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008393 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008394 return ret;
8395}
8396
Christoph Hellwigb2950862008-12-02 09:54:17 -05008397static int find_first_block_group(struct btrfs_root *root,
8398 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008399{
Chris Mason925baed2008-06-25 16:01:30 -04008400 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008401 struct btrfs_key found_key;
8402 struct extent_buffer *leaf;
8403 int slot;
8404
8405 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8406 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008407 goto out;
8408
Chris Masond3977122009-01-05 21:25:51 -05008409 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008410 slot = path->slots[0];
8411 leaf = path->nodes[0];
8412 if (slot >= btrfs_header_nritems(leaf)) {
8413 ret = btrfs_next_leaf(root, path);
8414 if (ret == 0)
8415 continue;
8416 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008417 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008418 break;
8419 }
8420 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8421
8422 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008423 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8424 ret = 0;
8425 goto out;
8426 }
Chris Mason0b86a832008-03-24 15:01:56 -04008427 path->slots[0]++;
8428 }
Chris Mason925baed2008-06-25 16:01:30 -04008429out:
Chris Mason0b86a832008-03-24 15:01:56 -04008430 return ret;
8431}
8432
Josef Bacik0af3d002010-06-21 14:48:16 -04008433void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8434{
8435 struct btrfs_block_group_cache *block_group;
8436 u64 last = 0;
8437
8438 while (1) {
8439 struct inode *inode;
8440
8441 block_group = btrfs_lookup_first_block_group(info, last);
8442 while (block_group) {
8443 spin_lock(&block_group->lock);
8444 if (block_group->iref)
8445 break;
8446 spin_unlock(&block_group->lock);
8447 block_group = next_block_group(info->tree_root,
8448 block_group);
8449 }
8450 if (!block_group) {
8451 if (last == 0)
8452 break;
8453 last = 0;
8454 continue;
8455 }
8456
8457 inode = block_group->inode;
8458 block_group->iref = 0;
8459 block_group->inode = NULL;
8460 spin_unlock(&block_group->lock);
8461 iput(inode);
8462 last = block_group->key.objectid + block_group->key.offset;
8463 btrfs_put_block_group(block_group);
8464 }
8465}
8466
Zheng Yan1a40e232008-09-26 10:09:34 -04008467int btrfs_free_block_groups(struct btrfs_fs_info *info)
8468{
8469 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008470 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008471 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008472 struct rb_node *n;
8473
Yan Zheng11833d62009-09-11 16:11:19 -04008474 down_write(&info->extent_commit_sem);
8475 while (!list_empty(&info->caching_block_groups)) {
8476 caching_ctl = list_entry(info->caching_block_groups.next,
8477 struct btrfs_caching_control, list);
8478 list_del(&caching_ctl->list);
8479 put_caching_control(caching_ctl);
8480 }
8481 up_write(&info->extent_commit_sem);
8482
Zheng Yan1a40e232008-09-26 10:09:34 -04008483 spin_lock(&info->block_group_cache_lock);
8484 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8485 block_group = rb_entry(n, struct btrfs_block_group_cache,
8486 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008487 rb_erase(&block_group->cache_node,
8488 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008489 spin_unlock(&info->block_group_cache_lock);
8490
Josef Bacik80eb2342008-10-29 14:49:05 -04008491 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008492 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008493 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008494
Josef Bacik817d52f2009-07-13 21:29:25 -04008495 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008496 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008497
Josef Bacik3c148742011-02-02 15:53:47 +00008498 /*
8499 * We haven't cached this block group, which means we could
8500 * possibly have excluded extents on this block group.
8501 */
8502 if (block_group->cached == BTRFS_CACHE_NO)
8503 free_excluded_extents(info->extent_root, block_group);
8504
Josef Bacik817d52f2009-07-13 21:29:25 -04008505 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008506 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008507
8508 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008509 }
8510 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008511
8512 /* now that all the block groups are freed, go through and
8513 * free all the space_info structs. This is only called during
8514 * the final stages of unmount, and so we know nobody is
8515 * using them. We call synchronize_rcu() once before we start,
8516 * just to be on the safe side.
8517 */
8518 synchronize_rcu();
8519
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008520 release_global_block_rsv(info);
8521
Chris Mason4184ea72009-03-10 12:39:20 -04008522 while(!list_empty(&info->space_info)) {
8523 space_info = list_entry(info->space_info.next,
8524 struct btrfs_space_info,
8525 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008526 if (space_info->bytes_pinned > 0 ||
8527 space_info->bytes_reserved > 0) {
8528 WARN_ON(1);
8529 dump_space_info(space_info, 0, 0);
8530 }
Chris Mason4184ea72009-03-10 12:39:20 -04008531 list_del(&space_info->list);
8532 kfree(space_info);
8533 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008534 return 0;
8535}
8536
Yan, Zhengb742bb82010-05-16 10:46:24 -04008537static void __link_block_group(struct btrfs_space_info *space_info,
8538 struct btrfs_block_group_cache *cache)
8539{
8540 int index = get_block_group_index(cache);
8541
8542 down_write(&space_info->groups_sem);
8543 list_add_tail(&cache->list, &space_info->block_groups[index]);
8544 up_write(&space_info->groups_sem);
8545}
8546
Chris Mason9078a3e2007-04-26 16:46:15 -04008547int btrfs_read_block_groups(struct btrfs_root *root)
8548{
8549 struct btrfs_path *path;
8550 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008551 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008552 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008553 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008554 struct btrfs_key key;
8555 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008556 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008557 int need_clear = 0;
8558 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008559
Chris Masonbe744172007-05-06 10:15:01 -04008560 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008561 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008562 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008563 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008564 path = btrfs_alloc_path();
8565 if (!path)
8566 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04008567 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04008568
Josef Bacik0af3d002010-06-21 14:48:16 -04008569 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8570 if (cache_gen != 0 &&
8571 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8572 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008573 if (btrfs_test_opt(root, CLEAR_CACHE))
8574 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04008575 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
8576 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04008577
Chris Masond3977122009-01-05 21:25:51 -05008578 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008579 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008580 if (ret > 0)
8581 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008582 if (ret != 0)
8583 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008584 leaf = path->nodes[0];
8585 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008586 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008587 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008588 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008589 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008590 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008591
Yan Zhengd2fb3432008-12-11 16:30:39 -05008592 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008593 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008594 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008595 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008596 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008597 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008598
Josef Bacik0af3d002010-06-21 14:48:16 -04008599 if (need_clear)
8600 cache->disk_cache_state = BTRFS_DC_CLEAR;
8601
Josef Bacik96303082009-07-13 21:29:25 -04008602 /*
8603 * we only want to have 32k of ram per block group for keeping
8604 * track of free space, and if we pass 1/2 of that we want to
8605 * start converting things over to using bitmaps
8606 */
8607 cache->extents_thresh = ((1024 * 32) / 2) /
8608 sizeof(struct btrfs_free_space);
8609
Chris Mason5f39d392007-10-15 16:14:19 -04008610 read_extent_buffer(leaf, &cache->item,
8611 btrfs_item_ptr_offset(leaf, path->slots[0]),
8612 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008613 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008614
Chris Mason9078a3e2007-04-26 16:46:15 -04008615 key.objectid = found_key.objectid + found_key.offset;
8616 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04008617 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008618 cache->sectorsize = root->sectorsize;
8619
Josef Bacik817d52f2009-07-13 21:29:25 -04008620 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008621 * We need to exclude the super stripes now so that the space
8622 * info has super bytes accounted for, otherwise we'll think
8623 * we have more space than we actually do.
8624 */
8625 exclude_super_stripes(root, cache);
8626
8627 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008628 * check for two cases, either we are full, and therefore
8629 * don't need to bother with the caching work since we won't
8630 * find any space, or we are empty, and we can just add all
8631 * the space in and be done with it. This saves us _alot_ of
8632 * time, particularly in the full case.
8633 */
8634 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008635 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008636 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008637 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008638 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008639 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008640 cache->cached = BTRFS_CACHE_FINISHED;
8641 add_new_free_space(cache, root->fs_info,
8642 found_key.objectid,
8643 found_key.objectid +
8644 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008645 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008646 }
Chris Mason96b51792007-10-15 16:15:19 -04008647
Chris Mason6324fbf2008-03-24 15:01:59 -04008648 ret = update_space_info(info, cache->flags, found_key.offset,
8649 btrfs_block_group_used(&cache->item),
8650 &space_info);
8651 BUG_ON(ret);
8652 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008653 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008654 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008655 spin_unlock(&cache->space_info->lock);
8656
Yan, Zhengb742bb82010-05-16 10:46:24 -04008657 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008658
Josef Bacik0f9dd462008-09-23 13:14:11 -04008659 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8660 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04008661
8662 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008663 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04008664 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04008665 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008666
8667 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8668 if (!(get_alloc_profile(root, space_info->flags) &
8669 (BTRFS_BLOCK_GROUP_RAID10 |
8670 BTRFS_BLOCK_GROUP_RAID1 |
8671 BTRFS_BLOCK_GROUP_DUP)))
8672 continue;
8673 /*
8674 * avoid allocating from un-mirrored block group if there are
8675 * mirrored block groups.
8676 */
8677 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008678 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008679 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008680 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008681 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008682
8683 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008684 ret = 0;
8685error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008686 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008687 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008688}
Chris Mason6324fbf2008-03-24 15:01:59 -04008689
8690int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8691 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008692 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008693 u64 size)
8694{
8695 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008696 struct btrfs_root *extent_root;
8697 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008698
8699 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008700
Chris Mason12fcfd22009-03-24 10:24:20 -04008701 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008702
Chris Mason8f18cf12008-04-25 16:53:30 -04008703 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008704 if (!cache)
8705 return -ENOMEM;
8706
Chris Masone17cade2008-04-15 15:41:47 -04008707 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008708 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008709 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008710 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008711 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04008712
8713 /*
8714 * we only want to have 32k of ram per block group for keeping track
8715 * of free space, and if we pass 1/2 of that we want to start
8716 * converting things over to using bitmaps
8717 */
8718 cache->extents_thresh = ((1024 * 32) / 2) /
8719 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008720 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008721 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008722 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008723 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008724 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008725
Chris Mason6324fbf2008-03-24 15:01:59 -04008726 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008727 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8728 cache->flags = type;
8729 btrfs_set_block_group_flags(&cache->item, type);
8730
Yan Zheng11833d62009-09-11 16:11:19 -04008731 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008732 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04008733 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04008734
Josef Bacik817d52f2009-07-13 21:29:25 -04008735 add_new_free_space(cache, root->fs_info, chunk_offset,
8736 chunk_offset + size);
8737
Yan Zheng11833d62009-09-11 16:11:19 -04008738 free_excluded_extents(root, cache);
8739
Chris Mason6324fbf2008-03-24 15:01:59 -04008740 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8741 &cache->space_info);
8742 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04008743
8744 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008745 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008746 spin_unlock(&cache->space_info->lock);
8747
Yan, Zhengb742bb82010-05-16 10:46:24 -04008748 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008749
Josef Bacik0f9dd462008-09-23 13:14:11 -04008750 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8751 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04008752
Chris Mason6324fbf2008-03-24 15:01:59 -04008753 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8754 sizeof(cache->item));
8755 BUG_ON(ret);
8756
Chris Masond18a2c42008-04-04 15:40:00 -04008757 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008758
Chris Mason6324fbf2008-03-24 15:01:59 -04008759 return 0;
8760}
Zheng Yan1a40e232008-09-26 10:09:34 -04008761
8762int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8763 struct btrfs_root *root, u64 group_start)
8764{
8765 struct btrfs_path *path;
8766 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008767 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008768 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008769 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008770 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008771 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04008772 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008773
Zheng Yan1a40e232008-09-26 10:09:34 -04008774 root = root->fs_info->extent_root;
8775
8776 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8777 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008778 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008779
liubo9f7c43c2011-03-07 02:13:33 +00008780 /*
8781 * Free the reserved super bytes from this block group before
8782 * remove it.
8783 */
8784 free_excluded_extents(root, block_group);
8785
Zheng Yan1a40e232008-09-26 10:09:34 -04008786 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04008787 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8788 BTRFS_BLOCK_GROUP_RAID1 |
8789 BTRFS_BLOCK_GROUP_RAID10))
8790 factor = 2;
8791 else
8792 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008793
Chris Mason44fb5512009-06-04 15:34:51 -04008794 /* make sure this block group isn't part of an allocation cluster */
8795 cluster = &root->fs_info->data_alloc_cluster;
8796 spin_lock(&cluster->refill_lock);
8797 btrfs_return_cluster_to_free_space(block_group, cluster);
8798 spin_unlock(&cluster->refill_lock);
8799
8800 /*
8801 * make sure this block group isn't part of a metadata
8802 * allocation cluster
8803 */
8804 cluster = &root->fs_info->meta_alloc_cluster;
8805 spin_lock(&cluster->refill_lock);
8806 btrfs_return_cluster_to_free_space(block_group, cluster);
8807 spin_unlock(&cluster->refill_lock);
8808
Zheng Yan1a40e232008-09-26 10:09:34 -04008809 path = btrfs_alloc_path();
8810 BUG_ON(!path);
8811
Josef Bacik0af3d002010-06-21 14:48:16 -04008812 inode = lookup_free_space_inode(root, block_group, path);
8813 if (!IS_ERR(inode)) {
8814 btrfs_orphan_add(trans, inode);
8815 clear_nlink(inode);
8816 /* One for the block groups ref */
8817 spin_lock(&block_group->lock);
8818 if (block_group->iref) {
8819 block_group->iref = 0;
8820 block_group->inode = NULL;
8821 spin_unlock(&block_group->lock);
8822 iput(inode);
8823 } else {
8824 spin_unlock(&block_group->lock);
8825 }
8826 /* One for our lookup ref */
8827 iput(inode);
8828 }
8829
8830 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8831 key.offset = block_group->key.objectid;
8832 key.type = 0;
8833
8834 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8835 if (ret < 0)
8836 goto out;
8837 if (ret > 0)
8838 btrfs_release_path(tree_root, path);
8839 if (ret == 0) {
8840 ret = btrfs_del_item(trans, tree_root, path);
8841 if (ret)
8842 goto out;
8843 btrfs_release_path(tree_root, path);
8844 }
8845
Yan Zheng3dfdb932009-01-21 10:49:16 -05008846 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008847 rb_erase(&block_group->cache_node,
8848 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008849 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008850
Josef Bacik80eb2342008-10-29 14:49:05 -04008851 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008852 /*
8853 * we must use list_del_init so people can check to see if they
8854 * are still on the list after taking the semaphore
8855 */
8856 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008857 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008858
Josef Bacik817d52f2009-07-13 21:29:25 -04008859 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008860 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008861
8862 btrfs_remove_free_space_cache(block_group);
8863
Yan Zhengc146afa2008-11-12 14:34:12 -05008864 spin_lock(&block_group->space_info->lock);
8865 block_group->space_info->total_bytes -= block_group->key.offset;
8866 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008867 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008868 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008869
Josef Bacik0af3d002010-06-21 14:48:16 -04008870 memcpy(&key, &block_group->key, sizeof(key));
8871
Chris Mason283bb192009-07-24 16:30:55 -04008872 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008873
Chris Masonfa9c0d72009-04-03 09:47:43 -04008874 btrfs_put_block_group(block_group);
8875 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008876
8877 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8878 if (ret > 0)
8879 ret = -EIO;
8880 if (ret < 0)
8881 goto out;
8882
8883 ret = btrfs_del_item(trans, root, path);
8884out:
8885 btrfs_free_path(path);
8886 return ret;
8887}
liuboacce9522011-01-06 19:30:25 +08008888
liuboc59021f2011-03-07 02:13:14 +00008889int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8890{
8891 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008892 struct btrfs_super_block *disk_super;
8893 u64 features;
8894 u64 flags;
8895 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008896 int ret;
8897
liubo1aba86d2011-04-08 08:44:37 +00008898 disk_super = &fs_info->super_copy;
8899 if (!btrfs_super_root(disk_super))
8900 return 1;
liuboc59021f2011-03-07 02:13:14 +00008901
liubo1aba86d2011-04-08 08:44:37 +00008902 features = btrfs_super_incompat_flags(disk_super);
8903 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8904 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008905
liubo1aba86d2011-04-08 08:44:37 +00008906 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8907 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008908 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008909 goto out;
liuboc59021f2011-03-07 02:13:14 +00008910
liubo1aba86d2011-04-08 08:44:37 +00008911 if (mixed) {
8912 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8913 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8914 } else {
8915 flags = BTRFS_BLOCK_GROUP_METADATA;
8916 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8917 if (ret)
8918 goto out;
8919
8920 flags = BTRFS_BLOCK_GROUP_DATA;
8921 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8922 }
8923out:
liuboc59021f2011-03-07 02:13:14 +00008924 return ret;
8925}
8926
liuboacce9522011-01-06 19:30:25 +08008927int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8928{
8929 return unpin_extent_range(root, start, end);
8930}
8931
8932int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008933 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008934{
Li Dongyang5378e602011-03-24 10:24:27 +00008935 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008936}
Li Dongyangf7039b12011-03-24 10:24:28 +00008937
8938int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8939{
8940 struct btrfs_fs_info *fs_info = root->fs_info;
8941 struct btrfs_block_group_cache *cache = NULL;
8942 u64 group_trimmed;
8943 u64 start;
8944 u64 end;
8945 u64 trimmed = 0;
8946 int ret = 0;
8947
8948 cache = btrfs_lookup_block_group(fs_info, range->start);
8949
8950 while (cache) {
8951 if (cache->key.objectid >= (range->start + range->len)) {
8952 btrfs_put_block_group(cache);
8953 break;
8954 }
8955
8956 start = max(range->start, cache->key.objectid);
8957 end = min(range->start + range->len,
8958 cache->key.objectid + cache->key.offset);
8959
8960 if (end - start >= range->minlen) {
8961 if (!block_group_cache_done(cache)) {
8962 ret = cache_block_group(cache, NULL, root, 0);
8963 if (!ret)
8964 wait_block_group_cache_done(cache);
8965 }
8966 ret = btrfs_trim_block_group(cache,
8967 &group_trimmed,
8968 start,
8969 end,
8970 range->minlen);
8971
8972 trimmed += group_trimmed;
8973 if (ret) {
8974 btrfs_put_block_group(cache);
8975 break;
8976 }
8977 }
8978
8979 cache = next_block_group(fs_info->tree_root, cache);
8980 }
8981
8982 range->len = trimmed;
8983 return ret;
8984}