blob: 26479484180df9f995f0cd9be52a3828becd6fe6 [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;
350 path->reada = 2;
351
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
Yan Zheng11833d62009-09-11 16:11:19 -0400381 caching_ctl->progress = last;
382 btrfs_release_path(extent_root, path);
383 up_read(&fs_info->extent_commit_sem);
384 mutex_unlock(&caching_ctl->mutex);
385 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400386 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400387 else
388 cond_resched();
389 goto again;
390 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400391
Yan Zheng11833d62009-09-11 16:11:19 -0400392 if (key.objectid < block_group->key.objectid) {
393 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400394 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400395 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400396
Chris Masone37c9e62007-05-09 20:13:14 -0400397 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400398 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400399 break;
Yan7d7d6062007-09-14 16:15:28 -0400400
Yan Zheng11833d62009-09-11 16:11:19 -0400401 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400402 total_found += add_new_free_space(block_group,
403 fs_info, last,
404 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400405 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400406
Yan Zheng11833d62009-09-11 16:11:19 -0400407 if (total_found > (1024 * 1024 * 2)) {
408 total_found = 0;
409 wake_up(&caching_ctl->wait);
410 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400411 }
Chris Masone37c9e62007-05-09 20:13:14 -0400412 path->slots[0]++;
413 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400414 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400415
416 total_found += add_new_free_space(block_group, fs_info, last,
417 block_group->key.objectid +
418 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400419 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400420
421 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400422 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400423 block_group->cached = BTRFS_CACHE_FINISHED;
424 spin_unlock(&block_group->lock);
425
Chris Mason54aa1f42007-06-22 14:16:25 -0400426err:
Chris Masone37c9e62007-05-09 20:13:14 -0400427 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400428 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400429
Yan Zheng11833d62009-09-11 16:11:19 -0400430 free_excluded_extents(extent_root, block_group);
431
432 mutex_unlock(&caching_ctl->mutex);
433 wake_up(&caching_ctl->wait);
434
435 put_caching_control(caching_ctl);
436 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000437 btrfs_put_block_group(block_group);
438
Josef Bacik817d52f2009-07-13 21:29:25 -0400439 return 0;
440}
441
Josef Bacik9d66e232010-08-25 16:54:15 -0400442static int cache_block_group(struct btrfs_block_group_cache *cache,
443 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500444 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400445 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400446{
Yan Zheng11833d62009-09-11 16:11:19 -0400447 struct btrfs_fs_info *fs_info = cache->fs_info;
448 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400449 struct task_struct *tsk;
450 int ret = 0;
451
Yan Zheng11833d62009-09-11 16:11:19 -0400452 smp_mb();
453 if (cache->cached != BTRFS_CACHE_NO)
454 return 0;
455
Josef Bacik9d66e232010-08-25 16:54:15 -0400456 /*
457 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500458 * to have the normal tree locking. Also if we are currently trying to
459 * allocate blocks for the tree root we can't do the fast caching since
460 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400461 */
Li Dongyangf7039b12011-03-24 10:24:28 +0000462 if (trans && (!trans->transaction->in_commit) &&
Josef Bacikb8399de2010-12-08 09:15:11 -0500463 (root && root != root->fs_info->tree_root)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400464 spin_lock(&cache->lock);
465 if (cache->cached != BTRFS_CACHE_NO) {
466 spin_unlock(&cache->lock);
467 return 0;
468 }
469 cache->cached = BTRFS_CACHE_STARTED;
470 spin_unlock(&cache->lock);
471
472 ret = load_free_space_cache(fs_info, cache);
473
474 spin_lock(&cache->lock);
475 if (ret == 1) {
476 cache->cached = BTRFS_CACHE_FINISHED;
477 cache->last_byte_to_unpin = (u64)-1;
478 } else {
479 cache->cached = BTRFS_CACHE_NO;
480 }
481 spin_unlock(&cache->lock);
Josef Bacik3c148742011-02-02 15:53:47 +0000482 if (ret == 1) {
483 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400484 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000485 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400486 }
487
488 if (load_cache_only)
489 return 0;
490
Miao Xiefc0e4a32011-03-24 11:41:21 +0000491 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -0400492 BUG_ON(!caching_ctl);
493
494 INIT_LIST_HEAD(&caching_ctl->list);
495 mutex_init(&caching_ctl->mutex);
496 init_waitqueue_head(&caching_ctl->wait);
497 caching_ctl->block_group = cache;
498 caching_ctl->progress = cache->key.objectid;
499 /* one for caching kthread, one for caching block group list */
500 atomic_set(&caching_ctl->count, 2);
501
Josef Bacik817d52f2009-07-13 21:29:25 -0400502 spin_lock(&cache->lock);
503 if (cache->cached != BTRFS_CACHE_NO) {
504 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400505 kfree(caching_ctl);
506 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400507 }
Yan Zheng11833d62009-09-11 16:11:19 -0400508 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400509 cache->cached = BTRFS_CACHE_STARTED;
510 spin_unlock(&cache->lock);
511
Yan Zheng11833d62009-09-11 16:11:19 -0400512 down_write(&fs_info->extent_commit_sem);
513 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
514 up_write(&fs_info->extent_commit_sem);
515
516 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000517 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400518
Josef Bacik817d52f2009-07-13 21:29:25 -0400519 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
520 cache->key.objectid);
521 if (IS_ERR(tsk)) {
522 ret = PTR_ERR(tsk);
523 printk(KERN_ERR "error running thread %d\n", ret);
524 BUG();
525 }
526
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400527 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400528}
529
Josef Bacik0f9dd462008-09-23 13:14:11 -0400530/*
531 * return the block group that starts at or after bytenr
532 */
Chris Masond3977122009-01-05 21:25:51 -0500533static struct btrfs_block_group_cache *
534btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400535{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400536 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400537
Josef Bacik0f9dd462008-09-23 13:14:11 -0400538 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400539
Josef Bacik0f9dd462008-09-23 13:14:11 -0400540 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400541}
542
Josef Bacik0f9dd462008-09-23 13:14:11 -0400543/*
Sankar P9f556842009-05-14 13:52:22 -0400544 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400545 */
Chris Masond3977122009-01-05 21:25:51 -0500546struct btrfs_block_group_cache *btrfs_lookup_block_group(
547 struct btrfs_fs_info *info,
548 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400549{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400550 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400551
Josef Bacik0f9dd462008-09-23 13:14:11 -0400552 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400553
Josef Bacik0f9dd462008-09-23 13:14:11 -0400554 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400555}
Chris Mason0b86a832008-03-24 15:01:56 -0400556
Josef Bacik0f9dd462008-09-23 13:14:11 -0400557static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
558 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400559{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400560 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400561 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400562
Yan, Zhengb742bb82010-05-16 10:46:24 -0400563 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
564 BTRFS_BLOCK_GROUP_METADATA;
565
Chris Mason4184ea72009-03-10 12:39:20 -0400566 rcu_read_lock();
567 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400568 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400569 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400570 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400571 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400572 }
Chris Mason4184ea72009-03-10 12:39:20 -0400573 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400574 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400575}
576
Chris Mason4184ea72009-03-10 12:39:20 -0400577/*
578 * after adding space to the filesystem, we need to clear the full flags
579 * on all the space infos.
580 */
581void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
582{
583 struct list_head *head = &info->space_info;
584 struct btrfs_space_info *found;
585
586 rcu_read_lock();
587 list_for_each_entry_rcu(found, head, list)
588 found->full = 0;
589 rcu_read_unlock();
590}
591
Josef Bacik80eb2342008-10-29 14:49:05 -0400592static u64 div_factor(u64 num, int factor)
593{
594 if (factor == 10)
595 return num;
596 num *= factor;
597 do_div(num, 10);
598 return num;
599}
600
Chris Masone5bc2452010-10-26 13:37:56 -0400601static u64 div_factor_fine(u64 num, int factor)
602{
603 if (factor == 100)
604 return num;
605 num *= factor;
606 do_div(num, 100);
607 return num;
608}
609
Yan Zhengd2fb3432008-12-11 16:30:39 -0500610u64 btrfs_find_block_group(struct btrfs_root *root,
611 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400612{
Chris Mason96b51792007-10-15 16:15:19 -0400613 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400614 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500615 u64 last = max(search_hint, search_start);
616 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400617 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500618 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400619 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400620again:
Zheng Yane8569812008-09-26 10:05:48 -0400621 while (1) {
622 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400623 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400624 break;
Chris Mason96b51792007-10-15 16:15:19 -0400625
Chris Masonc286ac42008-07-22 23:06:41 -0400626 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400627 last = cache->key.objectid + cache->key.offset;
628 used = btrfs_block_group_used(&cache->item);
629
Yan Zhengd2fb3432008-12-11 16:30:39 -0500630 if ((full_search || !cache->ro) &&
631 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400632 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500633 div_factor(cache->key.offset, factor)) {
634 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400635 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400636 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400637 goto found;
638 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400639 }
Chris Masonc286ac42008-07-22 23:06:41 -0400640 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400641 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400642 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400643 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400644 if (!wrapped) {
645 last = search_start;
646 wrapped = 1;
647 goto again;
648 }
649 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400650 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400651 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400652 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400653 goto again;
654 }
Chris Masonbe744172007-05-06 10:15:01 -0400655found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500656 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400657}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400658
Chris Masone02119d2008-09-05 16:13:11 -0400659/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400660int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400661{
662 int ret;
663 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400664 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400665
Zheng Yan31840ae2008-09-23 13:14:14 -0400666 path = btrfs_alloc_path();
667 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400668 key.objectid = start;
669 key.offset = len;
670 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
671 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
672 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400673 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500674 return ret;
675}
676
Chris Masond8d5f3e2007-12-11 12:42:00 -0500677/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400678 * helper function to lookup reference count and flags of extent.
679 *
680 * the head node for delayed ref is used to store the sum of all the
681 * reference count modifications queued up in the rbtree. the head
682 * node may also store the extent flags to set. This way you can check
683 * to see what the reference count and extent flags would be if all of
684 * the delayed refs are not processed.
685 */
686int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
687 struct btrfs_root *root, u64 bytenr,
688 u64 num_bytes, u64 *refs, u64 *flags)
689{
690 struct btrfs_delayed_ref_head *head;
691 struct btrfs_delayed_ref_root *delayed_refs;
692 struct btrfs_path *path;
693 struct btrfs_extent_item *ei;
694 struct extent_buffer *leaf;
695 struct btrfs_key key;
696 u32 item_size;
697 u64 num_refs;
698 u64 extent_flags;
699 int ret;
700
701 path = btrfs_alloc_path();
702 if (!path)
703 return -ENOMEM;
704
705 key.objectid = bytenr;
706 key.type = BTRFS_EXTENT_ITEM_KEY;
707 key.offset = num_bytes;
708 if (!trans) {
709 path->skip_locking = 1;
710 path->search_commit_root = 1;
711 }
712again:
713 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
714 &key, path, 0, 0);
715 if (ret < 0)
716 goto out_free;
717
718 if (ret == 0) {
719 leaf = path->nodes[0];
720 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
721 if (item_size >= sizeof(*ei)) {
722 ei = btrfs_item_ptr(leaf, path->slots[0],
723 struct btrfs_extent_item);
724 num_refs = btrfs_extent_refs(leaf, ei);
725 extent_flags = btrfs_extent_flags(leaf, ei);
726 } else {
727#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
728 struct btrfs_extent_item_v0 *ei0;
729 BUG_ON(item_size != sizeof(*ei0));
730 ei0 = btrfs_item_ptr(leaf, path->slots[0],
731 struct btrfs_extent_item_v0);
732 num_refs = btrfs_extent_refs_v0(leaf, ei0);
733 /* FIXME: this isn't correct for data */
734 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
735#else
736 BUG();
737#endif
738 }
739 BUG_ON(num_refs == 0);
740 } else {
741 num_refs = 0;
742 extent_flags = 0;
743 ret = 0;
744 }
745
746 if (!trans)
747 goto out;
748
749 delayed_refs = &trans->transaction->delayed_refs;
750 spin_lock(&delayed_refs->lock);
751 head = btrfs_find_delayed_ref_head(trans, bytenr);
752 if (head) {
753 if (!mutex_trylock(&head->mutex)) {
754 atomic_inc(&head->node.refs);
755 spin_unlock(&delayed_refs->lock);
756
757 btrfs_release_path(root->fs_info->extent_root, path);
758
759 mutex_lock(&head->mutex);
760 mutex_unlock(&head->mutex);
761 btrfs_put_delayed_ref(&head->node);
762 goto again;
763 }
764 if (head->extent_op && head->extent_op->update_flags)
765 extent_flags |= head->extent_op->flags_to_set;
766 else
767 BUG_ON(num_refs == 0);
768
769 num_refs += head->node.ref_mod;
770 mutex_unlock(&head->mutex);
771 }
772 spin_unlock(&delayed_refs->lock);
773out:
774 WARN_ON(num_refs == 0);
775 if (refs)
776 *refs = num_refs;
777 if (flags)
778 *flags = extent_flags;
779out_free:
780 btrfs_free_path(path);
781 return ret;
782}
783
784/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500785 * Back reference rules. Back refs have three main goals:
786 *
787 * 1) differentiate between all holders of references to an extent so that
788 * when a reference is dropped we can make sure it was a valid reference
789 * before freeing the extent.
790 *
791 * 2) Provide enough information to quickly find the holders of an extent
792 * if we notice a given block is corrupted or bad.
793 *
794 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
795 * maintenance. This is actually the same as #2, but with a slightly
796 * different use case.
797 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400798 * There are two kinds of back refs. The implicit back refs is optimized
799 * for pointers in non-shared tree blocks. For a given pointer in a block,
800 * back refs of this kind provide information about the block's owner tree
801 * and the pointer's key. These information allow us to find the block by
802 * b-tree searching. The full back refs is for pointers in tree blocks not
803 * referenced by their owner trees. The location of tree block is recorded
804 * in the back refs. Actually the full back refs is generic, and can be
805 * used in all cases the implicit back refs is used. The major shortcoming
806 * of the full back refs is its overhead. Every time a tree block gets
807 * COWed, we have to update back refs entry for all pointers in it.
808 *
809 * For a newly allocated tree block, we use implicit back refs for
810 * pointers in it. This means most tree related operations only involve
811 * implicit back refs. For a tree block created in old transaction, the
812 * only way to drop a reference to it is COW it. So we can detect the
813 * event that tree block loses its owner tree's reference and do the
814 * back refs conversion.
815 *
816 * When a tree block is COW'd through a tree, there are four cases:
817 *
818 * The reference count of the block is one and the tree is the block's
819 * owner tree. Nothing to do in this case.
820 *
821 * The reference count of the block is one and the tree is not the
822 * block's owner tree. In this case, full back refs is used for pointers
823 * in the block. Remove these full back refs, add implicit back refs for
824 * every pointers in the new block.
825 *
826 * The reference count of the block is greater than one and the tree is
827 * the block's owner tree. In this case, implicit back refs is used for
828 * pointers in the block. Add full back refs for every pointers in the
829 * block, increase lower level extents' reference counts. The original
830 * implicit back refs are entailed to the new block.
831 *
832 * The reference count of the block is greater than one and the tree is
833 * not the block's owner tree. Add implicit back refs for every pointer in
834 * the new block, increase lower level extents' reference count.
835 *
836 * Back Reference Key composing:
837 *
838 * The key objectid corresponds to the first byte in the extent,
839 * The key type is used to differentiate between types of back refs.
840 * There are different meanings of the key offset for different types
841 * of back refs.
842 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500843 * File extents can be referenced by:
844 *
845 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400846 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500847 * - different offsets inside a file (bookend extents in file.c)
848 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400849 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500850 *
851 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500852 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400853 * - original offset in the file
854 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400855 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400856 * The key offset for the implicit back refs is hash of the first
857 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500858 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400859 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500860 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500862 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400863 * The key offset for the implicit back refs is the first byte of
864 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500865 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 * When a file extent is allocated, The implicit back refs is used.
867 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500868 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400869 * (root_key.objectid, inode objectid, offset in file, 1)
870 *
871 * When a file extent is removed file truncation, we find the
872 * corresponding implicit back refs and check the following fields:
873 *
874 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500875 *
876 * Btree extents can be referenced by:
877 *
878 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500879 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400880 * Both the implicit back refs and the full back refs for tree blocks
881 * only consist of key. The key offset for the implicit back refs is
882 * objectid of block's owner tree. The key offset for the full back refs
883 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500884 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400885 * When implicit back refs is used, information about the lowest key and
886 * level of the tree block are required. These information are stored in
887 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500888 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400889
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400890#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
891static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
892 struct btrfs_root *root,
893 struct btrfs_path *path,
894 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500895{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400896 struct btrfs_extent_item *item;
897 struct btrfs_extent_item_v0 *ei0;
898 struct btrfs_extent_ref_v0 *ref0;
899 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400900 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400901 struct btrfs_key key;
902 struct btrfs_key found_key;
903 u32 new_size = sizeof(*item);
904 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500905 int ret;
906
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400907 leaf = path->nodes[0];
908 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500909
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400910 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
911 ei0 = btrfs_item_ptr(leaf, path->slots[0],
912 struct btrfs_extent_item_v0);
913 refs = btrfs_extent_refs_v0(leaf, ei0);
914
915 if (owner == (u64)-1) {
916 while (1) {
917 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
918 ret = btrfs_next_leaf(root, path);
919 if (ret < 0)
920 return ret;
921 BUG_ON(ret > 0);
922 leaf = path->nodes[0];
923 }
924 btrfs_item_key_to_cpu(leaf, &found_key,
925 path->slots[0]);
926 BUG_ON(key.objectid != found_key.objectid);
927 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
928 path->slots[0]++;
929 continue;
930 }
931 ref0 = btrfs_item_ptr(leaf, path->slots[0],
932 struct btrfs_extent_ref_v0);
933 owner = btrfs_ref_objectid_v0(leaf, ref0);
934 break;
935 }
936 }
937 btrfs_release_path(root, path);
938
939 if (owner < BTRFS_FIRST_FREE_OBJECTID)
940 new_size += sizeof(*bi);
941
942 new_size -= sizeof(*ei0);
943 ret = btrfs_search_slot(trans, root, &key, path,
944 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400945 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946 return ret;
947 BUG_ON(ret);
948
949 ret = btrfs_extend_item(trans, root, path, new_size);
950 BUG_ON(ret);
951
952 leaf = path->nodes[0];
953 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
954 btrfs_set_extent_refs(leaf, item, refs);
955 /* FIXME: get real generation */
956 btrfs_set_extent_generation(leaf, item, 0);
957 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
958 btrfs_set_extent_flags(leaf, item,
959 BTRFS_EXTENT_FLAG_TREE_BLOCK |
960 BTRFS_BLOCK_FLAG_FULL_BACKREF);
961 bi = (struct btrfs_tree_block_info *)(item + 1);
962 /* FIXME: get first key of the block */
963 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
964 btrfs_set_tree_block_level(leaf, bi, (int)owner);
965 } else {
966 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
967 }
968 btrfs_mark_buffer_dirty(leaf);
969 return 0;
970}
971#endif
972
973static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
974{
975 u32 high_crc = ~(u32)0;
976 u32 low_crc = ~(u32)0;
977 __le64 lenum;
978
979 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100980 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400981 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100982 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400983 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100984 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985
986 return ((u64)high_crc << 31) ^ (u64)low_crc;
987}
988
989static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
990 struct btrfs_extent_data_ref *ref)
991{
992 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
993 btrfs_extent_data_ref_objectid(leaf, ref),
994 btrfs_extent_data_ref_offset(leaf, ref));
995}
996
997static int match_extent_data_ref(struct extent_buffer *leaf,
998 struct btrfs_extent_data_ref *ref,
999 u64 root_objectid, u64 owner, u64 offset)
1000{
1001 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1002 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1003 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1004 return 0;
1005 return 1;
1006}
1007
1008static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1009 struct btrfs_root *root,
1010 struct btrfs_path *path,
1011 u64 bytenr, u64 parent,
1012 u64 root_objectid,
1013 u64 owner, u64 offset)
1014{
1015 struct btrfs_key key;
1016 struct btrfs_extent_data_ref *ref;
1017 struct extent_buffer *leaf;
1018 u32 nritems;
1019 int ret;
1020 int recow;
1021 int err = -ENOENT;
1022
1023 key.objectid = bytenr;
1024 if (parent) {
1025 key.type = BTRFS_SHARED_DATA_REF_KEY;
1026 key.offset = parent;
1027 } else {
1028 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1029 key.offset = hash_extent_data_ref(root_objectid,
1030 owner, offset);
1031 }
1032again:
1033 recow = 0;
1034 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1035 if (ret < 0) {
1036 err = ret;
1037 goto fail;
1038 }
1039
1040 if (parent) {
1041 if (!ret)
1042 return 0;
1043#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1044 key.type = BTRFS_EXTENT_REF_V0_KEY;
1045 btrfs_release_path(root, path);
1046 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1047 if (ret < 0) {
1048 err = ret;
1049 goto fail;
1050 }
1051 if (!ret)
1052 return 0;
1053#endif
1054 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001055 }
1056
1057 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001058 nritems = btrfs_header_nritems(leaf);
1059 while (1) {
1060 if (path->slots[0] >= nritems) {
1061 ret = btrfs_next_leaf(root, path);
1062 if (ret < 0)
1063 err = ret;
1064 if (ret)
1065 goto fail;
1066
1067 leaf = path->nodes[0];
1068 nritems = btrfs_header_nritems(leaf);
1069 recow = 1;
1070 }
1071
1072 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1073 if (key.objectid != bytenr ||
1074 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1075 goto fail;
1076
1077 ref = btrfs_item_ptr(leaf, path->slots[0],
1078 struct btrfs_extent_data_ref);
1079
1080 if (match_extent_data_ref(leaf, ref, root_objectid,
1081 owner, offset)) {
1082 if (recow) {
1083 btrfs_release_path(root, path);
1084 goto again;
1085 }
1086 err = 0;
1087 break;
1088 }
1089 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001090 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001091fail:
1092 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001093}
1094
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1096 struct btrfs_root *root,
1097 struct btrfs_path *path,
1098 u64 bytenr, u64 parent,
1099 u64 root_objectid, u64 owner,
1100 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001101{
1102 struct btrfs_key key;
1103 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001104 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001105 u32 num_refs;
1106 int ret;
1107
1108 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001109 if (parent) {
1110 key.type = BTRFS_SHARED_DATA_REF_KEY;
1111 key.offset = parent;
1112 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001113 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001114 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1115 key.offset = hash_extent_data_ref(root_objectid,
1116 owner, offset);
1117 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001118 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001119
1120 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1121 if (ret && ret != -EEXIST)
1122 goto fail;
1123
1124 leaf = path->nodes[0];
1125 if (parent) {
1126 struct btrfs_shared_data_ref *ref;
1127 ref = btrfs_item_ptr(leaf, path->slots[0],
1128 struct btrfs_shared_data_ref);
1129 if (ret == 0) {
1130 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1131 } else {
1132 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1133 num_refs += refs_to_add;
1134 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1135 }
1136 } else {
1137 struct btrfs_extent_data_ref *ref;
1138 while (ret == -EEXIST) {
1139 ref = btrfs_item_ptr(leaf, path->slots[0],
1140 struct btrfs_extent_data_ref);
1141 if (match_extent_data_ref(leaf, ref, root_objectid,
1142 owner, offset))
1143 break;
1144 btrfs_release_path(root, path);
1145 key.offset++;
1146 ret = btrfs_insert_empty_item(trans, root, path, &key,
1147 size);
1148 if (ret && ret != -EEXIST)
1149 goto fail;
1150
1151 leaf = path->nodes[0];
1152 }
1153 ref = btrfs_item_ptr(leaf, path->slots[0],
1154 struct btrfs_extent_data_ref);
1155 if (ret == 0) {
1156 btrfs_set_extent_data_ref_root(leaf, ref,
1157 root_objectid);
1158 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1159 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1160 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1161 } else {
1162 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1163 num_refs += refs_to_add;
1164 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1165 }
1166 }
1167 btrfs_mark_buffer_dirty(leaf);
1168 ret = 0;
1169fail:
Chris Mason7bb86312007-12-11 09:25:06 -05001170 btrfs_release_path(root, path);
1171 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001172}
1173
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001174static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1175 struct btrfs_root *root,
1176 struct btrfs_path *path,
1177 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001178{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001179 struct btrfs_key key;
1180 struct btrfs_extent_data_ref *ref1 = NULL;
1181 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001182 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001184 int ret = 0;
1185
1186 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001187 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1188
1189 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1190 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1191 struct btrfs_extent_data_ref);
1192 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1193 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1194 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1195 struct btrfs_shared_data_ref);
1196 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1197#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1198 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1199 struct btrfs_extent_ref_v0 *ref0;
1200 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1201 struct btrfs_extent_ref_v0);
1202 num_refs = btrfs_ref_count_v0(leaf, ref0);
1203#endif
1204 } else {
1205 BUG();
1206 }
1207
Chris Mason56bec292009-03-13 10:10:06 -04001208 BUG_ON(num_refs < refs_to_drop);
1209 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001210
Zheng Yan31840ae2008-09-23 13:14:14 -04001211 if (num_refs == 0) {
1212 ret = btrfs_del_item(trans, root, path);
1213 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001214 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1215 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1216 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1217 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1218#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1219 else {
1220 struct btrfs_extent_ref_v0 *ref0;
1221 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1222 struct btrfs_extent_ref_v0);
1223 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1224 }
1225#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001226 btrfs_mark_buffer_dirty(leaf);
1227 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001228 return ret;
1229}
1230
1231static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1232 struct btrfs_path *path,
1233 struct btrfs_extent_inline_ref *iref)
1234{
1235 struct btrfs_key key;
1236 struct extent_buffer *leaf;
1237 struct btrfs_extent_data_ref *ref1;
1238 struct btrfs_shared_data_ref *ref2;
1239 u32 num_refs = 0;
1240
1241 leaf = path->nodes[0];
1242 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1243 if (iref) {
1244 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1245 BTRFS_EXTENT_DATA_REF_KEY) {
1246 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1247 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1248 } else {
1249 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1250 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1251 }
1252 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1253 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1254 struct btrfs_extent_data_ref);
1255 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1256 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1257 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1258 struct btrfs_shared_data_ref);
1259 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1260#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1261 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1262 struct btrfs_extent_ref_v0 *ref0;
1263 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1264 struct btrfs_extent_ref_v0);
1265 num_refs = btrfs_ref_count_v0(leaf, ref0);
1266#endif
1267 } else {
1268 WARN_ON(1);
1269 }
1270 return num_refs;
1271}
1272
1273static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1274 struct btrfs_root *root,
1275 struct btrfs_path *path,
1276 u64 bytenr, u64 parent,
1277 u64 root_objectid)
1278{
1279 struct btrfs_key key;
1280 int ret;
1281
1282 key.objectid = bytenr;
1283 if (parent) {
1284 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1285 key.offset = parent;
1286 } else {
1287 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1288 key.offset = root_objectid;
1289 }
1290
1291 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1292 if (ret > 0)
1293 ret = -ENOENT;
1294#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1295 if (ret == -ENOENT && parent) {
1296 btrfs_release_path(root, path);
1297 key.type = BTRFS_EXTENT_REF_V0_KEY;
1298 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1299 if (ret > 0)
1300 ret = -ENOENT;
1301 }
1302#endif
1303 return ret;
1304}
1305
1306static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1307 struct btrfs_root *root,
1308 struct btrfs_path *path,
1309 u64 bytenr, u64 parent,
1310 u64 root_objectid)
1311{
1312 struct btrfs_key key;
1313 int ret;
1314
1315 key.objectid = bytenr;
1316 if (parent) {
1317 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1318 key.offset = parent;
1319 } else {
1320 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1321 key.offset = root_objectid;
1322 }
1323
1324 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001325 btrfs_release_path(root, path);
1326 return ret;
1327}
1328
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001329static inline int extent_ref_type(u64 parent, u64 owner)
1330{
1331 int type;
1332 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1333 if (parent > 0)
1334 type = BTRFS_SHARED_BLOCK_REF_KEY;
1335 else
1336 type = BTRFS_TREE_BLOCK_REF_KEY;
1337 } else {
1338 if (parent > 0)
1339 type = BTRFS_SHARED_DATA_REF_KEY;
1340 else
1341 type = BTRFS_EXTENT_DATA_REF_KEY;
1342 }
1343 return type;
1344}
1345
Yan Zheng2c47e6052009-06-27 21:07:35 -04001346static int find_next_key(struct btrfs_path *path, int level,
1347 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001348
1349{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001350 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001351 if (!path->nodes[level])
1352 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001353 if (path->slots[level] + 1 >=
1354 btrfs_header_nritems(path->nodes[level]))
1355 continue;
1356 if (level == 0)
1357 btrfs_item_key_to_cpu(path->nodes[level], key,
1358 path->slots[level] + 1);
1359 else
1360 btrfs_node_key_to_cpu(path->nodes[level], key,
1361 path->slots[level] + 1);
1362 return 0;
1363 }
1364 return 1;
1365}
1366
1367/*
1368 * look for inline back ref. if back ref is found, *ref_ret is set
1369 * to the address of inline back ref, and 0 is returned.
1370 *
1371 * if back ref isn't found, *ref_ret is set to the address where it
1372 * should be inserted, and -ENOENT is returned.
1373 *
1374 * if insert is true and there are too many inline back refs, the path
1375 * points to the extent item, and -EAGAIN is returned.
1376 *
1377 * NOTE: inline back refs are ordered in the same way that back ref
1378 * items in the tree are ordered.
1379 */
1380static noinline_for_stack
1381int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1382 struct btrfs_root *root,
1383 struct btrfs_path *path,
1384 struct btrfs_extent_inline_ref **ref_ret,
1385 u64 bytenr, u64 num_bytes,
1386 u64 parent, u64 root_objectid,
1387 u64 owner, u64 offset, int insert)
1388{
1389 struct btrfs_key key;
1390 struct extent_buffer *leaf;
1391 struct btrfs_extent_item *ei;
1392 struct btrfs_extent_inline_ref *iref;
1393 u64 flags;
1394 u64 item_size;
1395 unsigned long ptr;
1396 unsigned long end;
1397 int extra_size;
1398 int type;
1399 int want;
1400 int ret;
1401 int err = 0;
1402
1403 key.objectid = bytenr;
1404 key.type = BTRFS_EXTENT_ITEM_KEY;
1405 key.offset = num_bytes;
1406
1407 want = extent_ref_type(parent, owner);
1408 if (insert) {
1409 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001410 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001411 } else
1412 extra_size = -1;
1413 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1414 if (ret < 0) {
1415 err = ret;
1416 goto out;
1417 }
1418 BUG_ON(ret);
1419
1420 leaf = path->nodes[0];
1421 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1422#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1423 if (item_size < sizeof(*ei)) {
1424 if (!insert) {
1425 err = -ENOENT;
1426 goto out;
1427 }
1428 ret = convert_extent_item_v0(trans, root, path, owner,
1429 extra_size);
1430 if (ret < 0) {
1431 err = ret;
1432 goto out;
1433 }
1434 leaf = path->nodes[0];
1435 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1436 }
1437#endif
1438 BUG_ON(item_size < sizeof(*ei));
1439
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001440 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1441 flags = btrfs_extent_flags(leaf, ei);
1442
1443 ptr = (unsigned long)(ei + 1);
1444 end = (unsigned long)ei + item_size;
1445
1446 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1447 ptr += sizeof(struct btrfs_tree_block_info);
1448 BUG_ON(ptr > end);
1449 } else {
1450 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1451 }
1452
1453 err = -ENOENT;
1454 while (1) {
1455 if (ptr >= end) {
1456 WARN_ON(ptr > end);
1457 break;
1458 }
1459 iref = (struct btrfs_extent_inline_ref *)ptr;
1460 type = btrfs_extent_inline_ref_type(leaf, iref);
1461 if (want < type)
1462 break;
1463 if (want > type) {
1464 ptr += btrfs_extent_inline_ref_size(type);
1465 continue;
1466 }
1467
1468 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1469 struct btrfs_extent_data_ref *dref;
1470 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1471 if (match_extent_data_ref(leaf, dref, root_objectid,
1472 owner, offset)) {
1473 err = 0;
1474 break;
1475 }
1476 if (hash_extent_data_ref_item(leaf, dref) <
1477 hash_extent_data_ref(root_objectid, owner, offset))
1478 break;
1479 } else {
1480 u64 ref_offset;
1481 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1482 if (parent > 0) {
1483 if (parent == ref_offset) {
1484 err = 0;
1485 break;
1486 }
1487 if (ref_offset < parent)
1488 break;
1489 } else {
1490 if (root_objectid == ref_offset) {
1491 err = 0;
1492 break;
1493 }
1494 if (ref_offset < root_objectid)
1495 break;
1496 }
1497 }
1498 ptr += btrfs_extent_inline_ref_size(type);
1499 }
1500 if (err == -ENOENT && insert) {
1501 if (item_size + extra_size >=
1502 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1503 err = -EAGAIN;
1504 goto out;
1505 }
1506 /*
1507 * To add new inline back ref, we have to make sure
1508 * there is no corresponding back ref item.
1509 * For simplicity, we just do not add new inline back
1510 * ref if there is any kind of item for this block
1511 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001512 if (find_next_key(path, 0, &key) == 0 &&
1513 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001514 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001515 err = -EAGAIN;
1516 goto out;
1517 }
1518 }
1519 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1520out:
Yan Zheng85d41982009-06-11 08:51:10 -04001521 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001522 path->keep_locks = 0;
1523 btrfs_unlock_up_safe(path, 1);
1524 }
1525 return err;
1526}
1527
1528/*
1529 * helper to add new inline back ref
1530 */
1531static noinline_for_stack
1532int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1533 struct btrfs_root *root,
1534 struct btrfs_path *path,
1535 struct btrfs_extent_inline_ref *iref,
1536 u64 parent, u64 root_objectid,
1537 u64 owner, u64 offset, int refs_to_add,
1538 struct btrfs_delayed_extent_op *extent_op)
1539{
1540 struct extent_buffer *leaf;
1541 struct btrfs_extent_item *ei;
1542 unsigned long ptr;
1543 unsigned long end;
1544 unsigned long item_offset;
1545 u64 refs;
1546 int size;
1547 int type;
1548 int ret;
1549
1550 leaf = path->nodes[0];
1551 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1552 item_offset = (unsigned long)iref - (unsigned long)ei;
1553
1554 type = extent_ref_type(parent, owner);
1555 size = btrfs_extent_inline_ref_size(type);
1556
1557 ret = btrfs_extend_item(trans, root, path, size);
1558 BUG_ON(ret);
1559
1560 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1561 refs = btrfs_extent_refs(leaf, ei);
1562 refs += refs_to_add;
1563 btrfs_set_extent_refs(leaf, ei, refs);
1564 if (extent_op)
1565 __run_delayed_extent_op(extent_op, leaf, ei);
1566
1567 ptr = (unsigned long)ei + item_offset;
1568 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1569 if (ptr < end - size)
1570 memmove_extent_buffer(leaf, ptr + size, ptr,
1571 end - size - ptr);
1572
1573 iref = (struct btrfs_extent_inline_ref *)ptr;
1574 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1575 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1576 struct btrfs_extent_data_ref *dref;
1577 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1578 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1579 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1580 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1581 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1582 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1583 struct btrfs_shared_data_ref *sref;
1584 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1585 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1586 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1587 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1588 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1589 } else {
1590 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1591 }
1592 btrfs_mark_buffer_dirty(leaf);
1593 return 0;
1594}
1595
1596static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1597 struct btrfs_root *root,
1598 struct btrfs_path *path,
1599 struct btrfs_extent_inline_ref **ref_ret,
1600 u64 bytenr, u64 num_bytes, u64 parent,
1601 u64 root_objectid, u64 owner, u64 offset)
1602{
1603 int ret;
1604
1605 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1606 bytenr, num_bytes, parent,
1607 root_objectid, owner, offset, 0);
1608 if (ret != -ENOENT)
1609 return ret;
1610
1611 btrfs_release_path(root, path);
1612 *ref_ret = NULL;
1613
1614 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1615 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1616 root_objectid);
1617 } else {
1618 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1619 root_objectid, owner, offset);
1620 }
1621 return ret;
1622}
1623
1624/*
1625 * helper to update/remove inline back ref
1626 */
1627static noinline_for_stack
1628int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1629 struct btrfs_root *root,
1630 struct btrfs_path *path,
1631 struct btrfs_extent_inline_ref *iref,
1632 int refs_to_mod,
1633 struct btrfs_delayed_extent_op *extent_op)
1634{
1635 struct extent_buffer *leaf;
1636 struct btrfs_extent_item *ei;
1637 struct btrfs_extent_data_ref *dref = NULL;
1638 struct btrfs_shared_data_ref *sref = NULL;
1639 unsigned long ptr;
1640 unsigned long end;
1641 u32 item_size;
1642 int size;
1643 int type;
1644 int ret;
1645 u64 refs;
1646
1647 leaf = path->nodes[0];
1648 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1649 refs = btrfs_extent_refs(leaf, ei);
1650 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1651 refs += refs_to_mod;
1652 btrfs_set_extent_refs(leaf, ei, refs);
1653 if (extent_op)
1654 __run_delayed_extent_op(extent_op, leaf, ei);
1655
1656 type = btrfs_extent_inline_ref_type(leaf, iref);
1657
1658 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1659 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1660 refs = btrfs_extent_data_ref_count(leaf, dref);
1661 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1662 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1663 refs = btrfs_shared_data_ref_count(leaf, sref);
1664 } else {
1665 refs = 1;
1666 BUG_ON(refs_to_mod != -1);
1667 }
1668
1669 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1670 refs += refs_to_mod;
1671
1672 if (refs > 0) {
1673 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1674 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1675 else
1676 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1677 } else {
1678 size = btrfs_extent_inline_ref_size(type);
1679 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1680 ptr = (unsigned long)iref;
1681 end = (unsigned long)ei + item_size;
1682 if (ptr + size < end)
1683 memmove_extent_buffer(leaf, ptr, ptr + size,
1684 end - ptr - size);
1685 item_size -= size;
1686 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1687 BUG_ON(ret);
1688 }
1689 btrfs_mark_buffer_dirty(leaf);
1690 return 0;
1691}
1692
1693static noinline_for_stack
1694int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1695 struct btrfs_root *root,
1696 struct btrfs_path *path,
1697 u64 bytenr, u64 num_bytes, u64 parent,
1698 u64 root_objectid, u64 owner,
1699 u64 offset, int refs_to_add,
1700 struct btrfs_delayed_extent_op *extent_op)
1701{
1702 struct btrfs_extent_inline_ref *iref;
1703 int ret;
1704
1705 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1706 bytenr, num_bytes, parent,
1707 root_objectid, owner, offset, 1);
1708 if (ret == 0) {
1709 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1710 ret = update_inline_extent_backref(trans, root, path, iref,
1711 refs_to_add, extent_op);
1712 } else if (ret == -ENOENT) {
1713 ret = setup_inline_extent_backref(trans, root, path, iref,
1714 parent, root_objectid,
1715 owner, offset, refs_to_add,
1716 extent_op);
1717 }
1718 return ret;
1719}
1720
1721static int insert_extent_backref(struct btrfs_trans_handle *trans,
1722 struct btrfs_root *root,
1723 struct btrfs_path *path,
1724 u64 bytenr, u64 parent, u64 root_objectid,
1725 u64 owner, u64 offset, int refs_to_add)
1726{
1727 int ret;
1728 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1729 BUG_ON(refs_to_add != 1);
1730 ret = insert_tree_block_ref(trans, root, path, bytenr,
1731 parent, root_objectid);
1732 } else {
1733 ret = insert_extent_data_ref(trans, root, path, bytenr,
1734 parent, root_objectid,
1735 owner, offset, refs_to_add);
1736 }
1737 return ret;
1738}
1739
1740static int remove_extent_backref(struct btrfs_trans_handle *trans,
1741 struct btrfs_root *root,
1742 struct btrfs_path *path,
1743 struct btrfs_extent_inline_ref *iref,
1744 int refs_to_drop, int is_data)
1745{
1746 int ret;
1747
1748 BUG_ON(!is_data && refs_to_drop != 1);
1749 if (iref) {
1750 ret = update_inline_extent_backref(trans, root, path, iref,
1751 -refs_to_drop, NULL);
1752 } else if (is_data) {
1753 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1754 } else {
1755 ret = btrfs_del_item(trans, root, path);
1756 }
1757 return ret;
1758}
1759
Li Dongyang5378e602011-03-24 10:24:27 +00001760static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001761 u64 start, u64 len)
1762{
Li Dongyang5378e602011-03-24 10:24:27 +00001763 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001764}
Chris Mason15916de2008-11-19 21:17:22 -05001765
Liu Hui1f3c79a2009-01-05 15:57:51 -05001766static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001767 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001768{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001769 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001770 u64 discarded_bytes = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001771 struct btrfs_multi_bio *multi = NULL;
1772
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001773
Liu Hui1f3c79a2009-01-05 15:57:51 -05001774 /* Tell the block device(s) that the sectors can be discarded */
Li Dongyang5378e602011-03-24 10:24:27 +00001775 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1776 bytenr, &num_bytes, &multi, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001777 if (!ret) {
1778 struct btrfs_bio_stripe *stripe = multi->stripes;
1779 int i;
1780
Liu Hui1f3c79a2009-01-05 15:57:51 -05001781
1782 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Li Dongyang5378e602011-03-24 10:24:27 +00001783 ret = btrfs_issue_discard(stripe->dev->bdev,
1784 stripe->physical,
1785 stripe->length);
1786 if (!ret)
1787 discarded_bytes += stripe->length;
1788 else if (ret != -EOPNOTSUPP)
1789 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001790 }
1791 kfree(multi);
1792 }
Li Dongyang5378e602011-03-24 10:24:27 +00001793 if (discarded_bytes && ret == -EOPNOTSUPP)
1794 ret = 0;
1795
1796 if (actual_bytes)
1797 *actual_bytes = discarded_bytes;
1798
Liu Hui1f3c79a2009-01-05 15:57:51 -05001799
1800 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001801}
1802
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001803int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1804 struct btrfs_root *root,
1805 u64 bytenr, u64 num_bytes, u64 parent,
1806 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001807{
1808 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001809 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1810 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001811
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001812 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1813 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1814 parent, root_objectid, (int)owner,
1815 BTRFS_ADD_DELAYED_REF, NULL);
1816 } else {
1817 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1818 parent, root_objectid, owner, offset,
1819 BTRFS_ADD_DELAYED_REF, NULL);
1820 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001821 return ret;
1822}
1823
Chris Mason925baed2008-06-25 16:01:30 -04001824static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001825 struct btrfs_root *root,
1826 u64 bytenr, u64 num_bytes,
1827 u64 parent, u64 root_objectid,
1828 u64 owner, u64 offset, int refs_to_add,
1829 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001830{
Chris Mason5caf2a02007-04-02 11:20:42 -04001831 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001832 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001833 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001834 u64 refs;
1835 int ret;
1836 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001837
Chris Mason5caf2a02007-04-02 11:20:42 -04001838 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001839 if (!path)
1840 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001841
Chris Mason3c12ac72008-04-21 12:01:38 -04001842 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001843 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001844 /* this will setup the path even if it fails to insert the back ref */
1845 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1846 path, bytenr, num_bytes, parent,
1847 root_objectid, owner, offset,
1848 refs_to_add, extent_op);
1849 if (ret == 0)
1850 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001851
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001852 if (ret != -EAGAIN) {
1853 err = ret;
1854 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001855 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001856
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001857 leaf = path->nodes[0];
1858 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1859 refs = btrfs_extent_refs(leaf, item);
1860 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1861 if (extent_op)
1862 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001863
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001864 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001865 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001866
Chris Mason3c12ac72008-04-21 12:01:38 -04001867 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001868 path->leave_spinning = 1;
1869
Chris Mason56bec292009-03-13 10:10:06 -04001870 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001871 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001872 path, bytenr, parent, root_objectid,
1873 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001874 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001875out:
Chris Mason74493f72007-12-11 09:25:06 -05001876 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001877 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001878}
1879
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001880static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1881 struct btrfs_root *root,
1882 struct btrfs_delayed_ref_node *node,
1883 struct btrfs_delayed_extent_op *extent_op,
1884 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001885{
Chris Mason56bec292009-03-13 10:10:06 -04001886 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001887 struct btrfs_delayed_data_ref *ref;
1888 struct btrfs_key ins;
1889 u64 parent = 0;
1890 u64 ref_root = 0;
1891 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001892
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001893 ins.objectid = node->bytenr;
1894 ins.offset = node->num_bytes;
1895 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001896
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001897 ref = btrfs_delayed_node_to_data_ref(node);
1898 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1899 parent = ref->parent;
1900 else
1901 ref_root = ref->root;
1902
1903 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1904 if (extent_op) {
1905 BUG_ON(extent_op->update_key);
1906 flags |= extent_op->flags_to_set;
1907 }
1908 ret = alloc_reserved_file_extent(trans, root,
1909 parent, ref_root, flags,
1910 ref->objectid, ref->offset,
1911 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001912 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1913 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1914 node->num_bytes, parent,
1915 ref_root, ref->objectid,
1916 ref->offset, node->ref_mod,
1917 extent_op);
1918 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1919 ret = __btrfs_free_extent(trans, root, node->bytenr,
1920 node->num_bytes, parent,
1921 ref_root, ref->objectid,
1922 ref->offset, node->ref_mod,
1923 extent_op);
1924 } else {
1925 BUG();
1926 }
Chris Mason56bec292009-03-13 10:10:06 -04001927 return ret;
1928}
1929
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001930static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1931 struct extent_buffer *leaf,
1932 struct btrfs_extent_item *ei)
1933{
1934 u64 flags = btrfs_extent_flags(leaf, ei);
1935 if (extent_op->update_flags) {
1936 flags |= extent_op->flags_to_set;
1937 btrfs_set_extent_flags(leaf, ei, flags);
1938 }
1939
1940 if (extent_op->update_key) {
1941 struct btrfs_tree_block_info *bi;
1942 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1943 bi = (struct btrfs_tree_block_info *)(ei + 1);
1944 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1945 }
1946}
1947
1948static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1949 struct btrfs_root *root,
1950 struct btrfs_delayed_ref_node *node,
1951 struct btrfs_delayed_extent_op *extent_op)
1952{
1953 struct btrfs_key key;
1954 struct btrfs_path *path;
1955 struct btrfs_extent_item *ei;
1956 struct extent_buffer *leaf;
1957 u32 item_size;
1958 int ret;
1959 int err = 0;
1960
1961 path = btrfs_alloc_path();
1962 if (!path)
1963 return -ENOMEM;
1964
1965 key.objectid = node->bytenr;
1966 key.type = BTRFS_EXTENT_ITEM_KEY;
1967 key.offset = node->num_bytes;
1968
1969 path->reada = 1;
1970 path->leave_spinning = 1;
1971 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1972 path, 0, 1);
1973 if (ret < 0) {
1974 err = ret;
1975 goto out;
1976 }
1977 if (ret > 0) {
1978 err = -EIO;
1979 goto out;
1980 }
1981
1982 leaf = path->nodes[0];
1983 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1984#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1985 if (item_size < sizeof(*ei)) {
1986 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1987 path, (u64)-1, 0);
1988 if (ret < 0) {
1989 err = ret;
1990 goto out;
1991 }
1992 leaf = path->nodes[0];
1993 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1994 }
1995#endif
1996 BUG_ON(item_size < sizeof(*ei));
1997 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1998 __run_delayed_extent_op(extent_op, leaf, ei);
1999
2000 btrfs_mark_buffer_dirty(leaf);
2001out:
2002 btrfs_free_path(path);
2003 return err;
2004}
2005
2006static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2007 struct btrfs_root *root,
2008 struct btrfs_delayed_ref_node *node,
2009 struct btrfs_delayed_extent_op *extent_op,
2010 int insert_reserved)
2011{
2012 int ret = 0;
2013 struct btrfs_delayed_tree_ref *ref;
2014 struct btrfs_key ins;
2015 u64 parent = 0;
2016 u64 ref_root = 0;
2017
2018 ins.objectid = node->bytenr;
2019 ins.offset = node->num_bytes;
2020 ins.type = BTRFS_EXTENT_ITEM_KEY;
2021
2022 ref = btrfs_delayed_node_to_tree_ref(node);
2023 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2024 parent = ref->parent;
2025 else
2026 ref_root = ref->root;
2027
2028 BUG_ON(node->ref_mod != 1);
2029 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2030 BUG_ON(!extent_op || !extent_op->update_flags ||
2031 !extent_op->update_key);
2032 ret = alloc_reserved_tree_block(trans, root,
2033 parent, ref_root,
2034 extent_op->flags_to_set,
2035 &extent_op->key,
2036 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002037 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2038 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2039 node->num_bytes, parent, ref_root,
2040 ref->level, 0, 1, extent_op);
2041 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2042 ret = __btrfs_free_extent(trans, root, node->bytenr,
2043 node->num_bytes, parent, ref_root,
2044 ref->level, 0, 1, extent_op);
2045 } else {
2046 BUG();
2047 }
2048 return ret;
2049}
2050
Chris Mason56bec292009-03-13 10:10:06 -04002051/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002052static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2053 struct btrfs_root *root,
2054 struct btrfs_delayed_ref_node *node,
2055 struct btrfs_delayed_extent_op *extent_op,
2056 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002057{
Josef Bacikeb099672009-02-12 09:27:38 -05002058 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002059 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002060 struct btrfs_delayed_ref_head *head;
2061 /*
2062 * we've hit the end of the chain and we were supposed
2063 * to insert this extent into the tree. But, it got
2064 * deleted before we ever needed to insert it, so all
2065 * we have to do is clean up the accounting
2066 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002067 BUG_ON(extent_op);
2068 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002069 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002070 btrfs_pin_extent(root, node->bytenr,
2071 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002072 if (head->is_data) {
2073 ret = btrfs_del_csums(trans, root,
2074 node->bytenr,
2075 node->num_bytes);
2076 BUG_ON(ret);
2077 }
Chris Mason56bec292009-03-13 10:10:06 -04002078 }
Chris Mason56bec292009-03-13 10:10:06 -04002079 mutex_unlock(&head->mutex);
2080 return 0;
2081 }
Josef Bacikeb099672009-02-12 09:27:38 -05002082
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002083 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2084 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2085 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2086 insert_reserved);
2087 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2088 node->type == BTRFS_SHARED_DATA_REF_KEY)
2089 ret = run_delayed_data_ref(trans, root, node, extent_op,
2090 insert_reserved);
2091 else
2092 BUG();
2093 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002094}
2095
Chris Mason56bec292009-03-13 10:10:06 -04002096static noinline struct btrfs_delayed_ref_node *
2097select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002098{
Chris Mason56bec292009-03-13 10:10:06 -04002099 struct rb_node *node;
2100 struct btrfs_delayed_ref_node *ref;
2101 int action = BTRFS_ADD_DELAYED_REF;
2102again:
2103 /*
2104 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2105 * this prevents ref count from going down to zero when
2106 * there still are pending delayed ref.
2107 */
2108 node = rb_prev(&head->node.rb_node);
2109 while (1) {
2110 if (!node)
2111 break;
2112 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2113 rb_node);
2114 if (ref->bytenr != head->node.bytenr)
2115 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002116 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002117 return ref;
2118 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002119 }
Chris Mason56bec292009-03-13 10:10:06 -04002120 if (action == BTRFS_ADD_DELAYED_REF) {
2121 action = BTRFS_DROP_DELAYED_REF;
2122 goto again;
2123 }
2124 return NULL;
2125}
2126
Chris Masonc3e69d52009-03-13 10:17:05 -04002127static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2128 struct btrfs_root *root,
2129 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002130{
Chris Mason56bec292009-03-13 10:10:06 -04002131 struct btrfs_delayed_ref_root *delayed_refs;
2132 struct btrfs_delayed_ref_node *ref;
2133 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002134 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002135 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002136 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002137 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002138
2139 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002140 while (1) {
2141 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002142 /* pick a new head ref from the cluster list */
2143 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002144 break;
Chris Mason56bec292009-03-13 10:10:06 -04002145
Chris Masonc3e69d52009-03-13 10:17:05 -04002146 locked_ref = list_entry(cluster->next,
2147 struct btrfs_delayed_ref_head, cluster);
2148
2149 /* grab the lock that says we are going to process
2150 * all the refs for this head */
2151 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2152
2153 /*
2154 * we may have dropped the spin lock to get the head
2155 * mutex lock, and that might have given someone else
2156 * time to free the head. If that's true, it has been
2157 * removed from our list and we can move on.
2158 */
2159 if (ret == -EAGAIN) {
2160 locked_ref = NULL;
2161 count++;
2162 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002163 }
2164 }
2165
2166 /*
2167 * record the must insert reserved flag before we
2168 * drop the spin lock.
2169 */
2170 must_insert_reserved = locked_ref->must_insert_reserved;
2171 locked_ref->must_insert_reserved = 0;
2172
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002173 extent_op = locked_ref->extent_op;
2174 locked_ref->extent_op = NULL;
2175
Chris Mason56bec292009-03-13 10:10:06 -04002176 /*
2177 * locked_ref is the head node, so we have to go one
2178 * node back for any delayed ref updates
2179 */
Chris Mason56bec292009-03-13 10:10:06 -04002180 ref = select_delayed_ref(locked_ref);
2181 if (!ref) {
2182 /* All delayed refs have been processed, Go ahead
2183 * and send the head node to run_one_delayed_ref,
2184 * so that any accounting fixes can happen
2185 */
2186 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002187
2188 if (extent_op && must_insert_reserved) {
2189 kfree(extent_op);
2190 extent_op = NULL;
2191 }
2192
2193 if (extent_op) {
2194 spin_unlock(&delayed_refs->lock);
2195
2196 ret = run_delayed_extent_op(trans, root,
2197 ref, extent_op);
2198 BUG_ON(ret);
2199 kfree(extent_op);
2200
2201 cond_resched();
2202 spin_lock(&delayed_refs->lock);
2203 continue;
2204 }
2205
Chris Masonc3e69d52009-03-13 10:17:05 -04002206 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002207 locked_ref = NULL;
2208 }
2209
2210 ref->in_tree = 0;
2211 rb_erase(&ref->rb_node, &delayed_refs->root);
2212 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002213
Chris Mason56bec292009-03-13 10:10:06 -04002214 spin_unlock(&delayed_refs->lock);
2215
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002216 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002217 must_insert_reserved);
2218 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002219
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002220 btrfs_put_delayed_ref(ref);
2221 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002222 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002223
Chris Mason1887be62009-03-13 10:11:24 -04002224 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002225 spin_lock(&delayed_refs->lock);
2226 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002227 return count;
2228}
2229
2230/*
2231 * this starts processing the delayed reference count updates and
2232 * extent insertions we have queued up so far. count can be
2233 * 0, which means to process everything in the tree at the start
2234 * of the run (but not newly added entries), or it can be some target
2235 * number you'd like to process.
2236 */
2237int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2238 struct btrfs_root *root, unsigned long count)
2239{
2240 struct rb_node *node;
2241 struct btrfs_delayed_ref_root *delayed_refs;
2242 struct btrfs_delayed_ref_node *ref;
2243 struct list_head cluster;
2244 int ret;
2245 int run_all = count == (unsigned long)-1;
2246 int run_most = 0;
2247
2248 if (root == root->fs_info->extent_root)
2249 root = root->fs_info->tree_root;
2250
2251 delayed_refs = &trans->transaction->delayed_refs;
2252 INIT_LIST_HEAD(&cluster);
2253again:
2254 spin_lock(&delayed_refs->lock);
2255 if (count == 0) {
2256 count = delayed_refs->num_entries * 2;
2257 run_most = 1;
2258 }
2259 while (1) {
2260 if (!(run_all || run_most) &&
2261 delayed_refs->num_heads_ready < 64)
2262 break;
2263
2264 /*
2265 * go find something we can process in the rbtree. We start at
2266 * the beginning of the tree, and then build a cluster
2267 * of refs to process starting at the first one we are able to
2268 * lock
2269 */
2270 ret = btrfs_find_ref_cluster(trans, &cluster,
2271 delayed_refs->run_delayed_start);
2272 if (ret)
2273 break;
2274
2275 ret = run_clustered_refs(trans, root, &cluster);
2276 BUG_ON(ret < 0);
2277
2278 count -= min_t(unsigned long, ret, count);
2279
2280 if (count == 0)
2281 break;
2282 }
2283
Chris Mason56bec292009-03-13 10:10:06 -04002284 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002285 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002286 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002287 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002288 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002289
2290 while (node) {
2291 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2292 rb_node);
2293 if (btrfs_delayed_ref_is_head(ref)) {
2294 struct btrfs_delayed_ref_head *head;
2295
2296 head = btrfs_delayed_node_to_head(ref);
2297 atomic_inc(&ref->refs);
2298
2299 spin_unlock(&delayed_refs->lock);
2300 mutex_lock(&head->mutex);
2301 mutex_unlock(&head->mutex);
2302
2303 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002304 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002305 goto again;
2306 }
2307 node = rb_next(node);
2308 }
2309 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002310 schedule_timeout(1);
2311 goto again;
2312 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002313out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002314 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002315 return 0;
2316}
2317
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002318int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2319 struct btrfs_root *root,
2320 u64 bytenr, u64 num_bytes, u64 flags,
2321 int is_data)
2322{
2323 struct btrfs_delayed_extent_op *extent_op;
2324 int ret;
2325
2326 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2327 if (!extent_op)
2328 return -ENOMEM;
2329
2330 extent_op->flags_to_set = flags;
2331 extent_op->update_flags = 1;
2332 extent_op->update_key = 0;
2333 extent_op->is_data = is_data ? 1 : 0;
2334
2335 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2336 if (ret)
2337 kfree(extent_op);
2338 return ret;
2339}
2340
2341static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2342 struct btrfs_root *root,
2343 struct btrfs_path *path,
2344 u64 objectid, u64 offset, u64 bytenr)
2345{
2346 struct btrfs_delayed_ref_head *head;
2347 struct btrfs_delayed_ref_node *ref;
2348 struct btrfs_delayed_data_ref *data_ref;
2349 struct btrfs_delayed_ref_root *delayed_refs;
2350 struct rb_node *node;
2351 int ret = 0;
2352
2353 ret = -ENOENT;
2354 delayed_refs = &trans->transaction->delayed_refs;
2355 spin_lock(&delayed_refs->lock);
2356 head = btrfs_find_delayed_ref_head(trans, bytenr);
2357 if (!head)
2358 goto out;
2359
2360 if (!mutex_trylock(&head->mutex)) {
2361 atomic_inc(&head->node.refs);
2362 spin_unlock(&delayed_refs->lock);
2363
2364 btrfs_release_path(root->fs_info->extent_root, path);
2365
2366 mutex_lock(&head->mutex);
2367 mutex_unlock(&head->mutex);
2368 btrfs_put_delayed_ref(&head->node);
2369 return -EAGAIN;
2370 }
2371
2372 node = rb_prev(&head->node.rb_node);
2373 if (!node)
2374 goto out_unlock;
2375
2376 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2377
2378 if (ref->bytenr != bytenr)
2379 goto out_unlock;
2380
2381 ret = 1;
2382 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2383 goto out_unlock;
2384
2385 data_ref = btrfs_delayed_node_to_data_ref(ref);
2386
2387 node = rb_prev(node);
2388 if (node) {
2389 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2390 if (ref->bytenr == bytenr)
2391 goto out_unlock;
2392 }
2393
2394 if (data_ref->root != root->root_key.objectid ||
2395 data_ref->objectid != objectid || data_ref->offset != offset)
2396 goto out_unlock;
2397
2398 ret = 0;
2399out_unlock:
2400 mutex_unlock(&head->mutex);
2401out:
2402 spin_unlock(&delayed_refs->lock);
2403 return ret;
2404}
2405
2406static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2407 struct btrfs_root *root,
2408 struct btrfs_path *path,
2409 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002410{
2411 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002412 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002413 struct btrfs_extent_data_ref *ref;
2414 struct btrfs_extent_inline_ref *iref;
2415 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002416 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002417 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002418 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002419
Chris Masonbe20aa92007-12-17 20:14:01 -05002420 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002421 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002422 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002423
Chris Masonbe20aa92007-12-17 20:14:01 -05002424 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2425 if (ret < 0)
2426 goto out;
2427 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002428
2429 ret = -ENOENT;
2430 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002431 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002432
Zheng Yan31840ae2008-09-23 13:14:14 -04002433 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002434 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002435 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002436
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002437 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002438 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002439
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002440 ret = 1;
2441 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2442#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2443 if (item_size < sizeof(*ei)) {
2444 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2445 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002446 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002447#endif
2448 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2449
2450 if (item_size != sizeof(*ei) +
2451 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2452 goto out;
2453
2454 if (btrfs_extent_generation(leaf, ei) <=
2455 btrfs_root_last_snapshot(&root->root_item))
2456 goto out;
2457
2458 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2459 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2460 BTRFS_EXTENT_DATA_REF_KEY)
2461 goto out;
2462
2463 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2464 if (btrfs_extent_refs(leaf, ei) !=
2465 btrfs_extent_data_ref_count(leaf, ref) ||
2466 btrfs_extent_data_ref_root(leaf, ref) !=
2467 root->root_key.objectid ||
2468 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2469 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2470 goto out;
2471
Yan Zhengf321e492008-07-30 09:26:11 -04002472 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002473out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002474 return ret;
2475}
2476
2477int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2478 struct btrfs_root *root,
2479 u64 objectid, u64 offset, u64 bytenr)
2480{
2481 struct btrfs_path *path;
2482 int ret;
2483 int ret2;
2484
2485 path = btrfs_alloc_path();
2486 if (!path)
2487 return -ENOENT;
2488
2489 do {
2490 ret = check_committed_ref(trans, root, path, objectid,
2491 offset, bytenr);
2492 if (ret && ret != -ENOENT)
2493 goto out;
2494
2495 ret2 = check_delayed_ref(trans, root, path, objectid,
2496 offset, bytenr);
2497 } while (ret2 == -EAGAIN);
2498
2499 if (ret2 && ret2 != -ENOENT) {
2500 ret = ret2;
2501 goto out;
2502 }
2503
2504 if (ret != -ENOENT || ret2 != -ENOENT)
2505 ret = 0;
2506out:
Yan Zhengf321e492008-07-30 09:26:11 -04002507 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002508 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2509 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002510 return ret;
2511}
2512
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002513#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002514int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2515 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002516{
Chris Mason5f39d392007-10-15 16:14:19 -04002517 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002518 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002519 u64 root_gen;
2520 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002521 int i;
Chris Masondb945352007-10-15 16:15:53 -04002522 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002523 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002524 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002525
Chris Mason3768f362007-03-13 16:47:54 -04002526 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002527 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002528
Zheng Yane4657682008-09-26 10:04:53 -04002529 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2530 shared = 0;
2531 root_gen = root->root_key.offset;
2532 } else {
2533 shared = 1;
2534 root_gen = trans->transid - 1;
2535 }
2536
Chris Masondb945352007-10-15 16:15:53 -04002537 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002538 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002539
Zheng Yan31840ae2008-09-23 13:14:14 -04002540 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002541 struct btrfs_leaf_ref *ref;
2542 struct btrfs_extent_info *info;
2543
Zheng Yan31840ae2008-09-23 13:14:14 -04002544 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002545 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002546 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002547 goto out;
2548 }
2549
Zheng Yane4657682008-09-26 10:04:53 -04002550 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002551 ref->bytenr = buf->start;
2552 ref->owner = btrfs_header_owner(buf);
2553 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002554 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002555 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002556
Zheng Yan31840ae2008-09-23 13:14:14 -04002557 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002558 u64 disk_bytenr;
2559 btrfs_item_key_to_cpu(buf, &key, i);
2560 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2561 continue;
2562 fi = btrfs_item_ptr(buf, i,
2563 struct btrfs_file_extent_item);
2564 if (btrfs_file_extent_type(buf, fi) ==
2565 BTRFS_FILE_EXTENT_INLINE)
2566 continue;
2567 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2568 if (disk_bytenr == 0)
2569 continue;
2570
2571 info->bytenr = disk_bytenr;
2572 info->num_bytes =
2573 btrfs_file_extent_disk_num_bytes(buf, fi);
2574 info->objectid = key.objectid;
2575 info->offset = key.offset;
2576 info++;
2577 }
2578
Zheng Yane4657682008-09-26 10:04:53 -04002579 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002580 if (ret == -EEXIST && shared) {
2581 struct btrfs_leaf_ref *old;
2582 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2583 BUG_ON(!old);
2584 btrfs_remove_leaf_ref(root, old);
2585 btrfs_free_leaf_ref(root, old);
2586 ret = btrfs_add_leaf_ref(root, ref, shared);
2587 }
Yan Zheng31153d82008-07-28 15:32:19 -04002588 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002589 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002590 }
2591out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002592 return ret;
2593}
2594
Chris Masonb7a9f292009-02-04 09:23:45 -05002595/* when a block goes through cow, we update the reference counts of
2596 * everything that block points to. The internal pointers of the block
2597 * can be in just about any order, and it is likely to have clusters of
2598 * things that are close together and clusters of things that are not.
2599 *
2600 * To help reduce the seeks that come with updating all of these reference
2601 * counts, sort them by byte number before actual updates are done.
2602 *
2603 * struct refsort is used to match byte number to slot in the btree block.
2604 * we sort based on the byte number and then use the slot to actually
2605 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002606 *
2607 * struct refsort is smaller than strcut btrfs_item and smaller than
2608 * struct btrfs_key_ptr. Since we're currently limited to the page size
2609 * for a btree block, there's no way for a kmalloc of refsorts for a
2610 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002611 */
2612struct refsort {
2613 u64 bytenr;
2614 u32 slot;
2615};
2616
2617/*
2618 * for passing into sort()
2619 */
2620static int refsort_cmp(const void *a_void, const void *b_void)
2621{
2622 const struct refsort *a = a_void;
2623 const struct refsort *b = b_void;
2624
2625 if (a->bytenr < b->bytenr)
2626 return -1;
2627 if (a->bytenr > b->bytenr)
2628 return 1;
2629 return 0;
2630}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002631#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002632
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002633static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002634 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002635 struct extent_buffer *buf,
2636 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002637{
2638 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002639 u64 num_bytes;
2640 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002641 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002642 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002643 struct btrfs_key key;
2644 struct btrfs_file_extent_item *fi;
2645 int i;
2646 int level;
2647 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002648 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002649 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002650
2651 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002652 nritems = btrfs_header_nritems(buf);
2653 level = btrfs_header_level(buf);
2654
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002655 if (!root->ref_cows && level == 0)
2656 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002657
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002658 if (inc)
2659 process_func = btrfs_inc_extent_ref;
2660 else
2661 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002662
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002663 if (full_backref)
2664 parent = buf->start;
2665 else
2666 parent = 0;
2667
Zheng Yan31840ae2008-09-23 13:14:14 -04002668 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002669 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002670 btrfs_item_key_to_cpu(buf, &key, i);
2671 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002672 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002673 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002674 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002675 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002676 BTRFS_FILE_EXTENT_INLINE)
2677 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002678 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2679 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002680 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002681
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002682 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2683 key.offset -= btrfs_file_extent_offset(buf, fi);
2684 ret = process_func(trans, root, bytenr, num_bytes,
2685 parent, ref_root, key.objectid,
2686 key.offset);
2687 if (ret)
2688 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002689 } else {
2690 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002691 num_bytes = btrfs_level_size(root, level - 1);
2692 ret = process_func(trans, root, bytenr, num_bytes,
2693 parent, ref_root, level - 1, 0);
2694 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002695 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002696 }
2697 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002698 return 0;
2699fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002700 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002701 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002702}
2703
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002704int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2705 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002706{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002707 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2708}
Zheng Yan31840ae2008-09-23 13:14:14 -04002709
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002710int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2711 struct extent_buffer *buf, int full_backref)
2712{
2713 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002714}
2715
Chris Mason9078a3e2007-04-26 16:46:15 -04002716static int write_one_cache_group(struct btrfs_trans_handle *trans,
2717 struct btrfs_root *root,
2718 struct btrfs_path *path,
2719 struct btrfs_block_group_cache *cache)
2720{
2721 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002722 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002723 unsigned long bi;
2724 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002725
Chris Mason9078a3e2007-04-26 16:46:15 -04002726 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002727 if (ret < 0)
2728 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002729 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002730
2731 leaf = path->nodes[0];
2732 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2733 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2734 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002735 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002736fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002737 if (ret)
2738 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002739 return 0;
2740
2741}
2742
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002743static struct btrfs_block_group_cache *
2744next_block_group(struct btrfs_root *root,
2745 struct btrfs_block_group_cache *cache)
2746{
2747 struct rb_node *node;
2748 spin_lock(&root->fs_info->block_group_cache_lock);
2749 node = rb_next(&cache->cache_node);
2750 btrfs_put_block_group(cache);
2751 if (node) {
2752 cache = rb_entry(node, struct btrfs_block_group_cache,
2753 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002754 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002755 } else
2756 cache = NULL;
2757 spin_unlock(&root->fs_info->block_group_cache_lock);
2758 return cache;
2759}
2760
Josef Bacik0af3d002010-06-21 14:48:16 -04002761static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2762 struct btrfs_trans_handle *trans,
2763 struct btrfs_path *path)
2764{
2765 struct btrfs_root *root = block_group->fs_info->tree_root;
2766 struct inode *inode = NULL;
2767 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002768 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002769 int num_pages = 0;
2770 int retries = 0;
2771 int ret = 0;
2772
2773 /*
2774 * If this block group is smaller than 100 megs don't bother caching the
2775 * block group.
2776 */
2777 if (block_group->key.offset < (100 * 1024 * 1024)) {
2778 spin_lock(&block_group->lock);
2779 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2780 spin_unlock(&block_group->lock);
2781 return 0;
2782 }
2783
2784again:
2785 inode = lookup_free_space_inode(root, block_group, path);
2786 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2787 ret = PTR_ERR(inode);
2788 btrfs_release_path(root, path);
2789 goto out;
2790 }
2791
2792 if (IS_ERR(inode)) {
2793 BUG_ON(retries);
2794 retries++;
2795
2796 if (block_group->ro)
2797 goto out_free;
2798
2799 ret = create_free_space_inode(root, trans, block_group, path);
2800 if (ret)
2801 goto out_free;
2802 goto again;
2803 }
2804
2805 /*
2806 * We want to set the generation to 0, that way if anything goes wrong
2807 * from here on out we know not to trust this cache when we load up next
2808 * time.
2809 */
2810 BTRFS_I(inode)->generation = 0;
2811 ret = btrfs_update_inode(trans, root, inode);
2812 WARN_ON(ret);
2813
2814 if (i_size_read(inode) > 0) {
2815 ret = btrfs_truncate_free_space_cache(root, trans, path,
2816 inode);
2817 if (ret)
2818 goto out_put;
2819 }
2820
2821 spin_lock(&block_group->lock);
2822 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002823 /* We're not cached, don't bother trying to write stuff out */
2824 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002825 spin_unlock(&block_group->lock);
2826 goto out_put;
2827 }
2828 spin_unlock(&block_group->lock);
2829
2830 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2831 if (!num_pages)
2832 num_pages = 1;
2833
2834 /*
2835 * Just to make absolutely sure we have enough space, we're going to
2836 * preallocate 12 pages worth of space for each block group. In
2837 * practice we ought to use at most 8, but we need extra space so we can
2838 * add our header and have a terminator between the extents and the
2839 * bitmaps.
2840 */
2841 num_pages *= 16;
2842 num_pages *= PAGE_CACHE_SIZE;
2843
2844 ret = btrfs_check_data_free_space(inode, num_pages);
2845 if (ret)
2846 goto out_put;
2847
2848 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2849 num_pages, num_pages,
2850 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002851 if (!ret)
2852 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002853 btrfs_free_reserved_data_space(inode, num_pages);
2854out_put:
2855 iput(inode);
2856out_free:
2857 btrfs_release_path(root, path);
2858out:
2859 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002860 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002861 spin_unlock(&block_group->lock);
2862
2863 return ret;
2864}
2865
Chris Mason96b51792007-10-15 16:15:19 -04002866int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2867 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002868{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002869 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002870 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002871 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002872 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002873
2874 path = btrfs_alloc_path();
2875 if (!path)
2876 return -ENOMEM;
2877
Josef Bacik0af3d002010-06-21 14:48:16 -04002878again:
2879 while (1) {
2880 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2881 while (cache) {
2882 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2883 break;
2884 cache = next_block_group(root, cache);
2885 }
2886 if (!cache) {
2887 if (last == 0)
2888 break;
2889 last = 0;
2890 continue;
2891 }
2892 err = cache_save_setup(cache, trans, path);
2893 last = cache->key.objectid + cache->key.offset;
2894 btrfs_put_block_group(cache);
2895 }
2896
Chris Masond3977122009-01-05 21:25:51 -05002897 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002898 if (last == 0) {
2899 err = btrfs_run_delayed_refs(trans, root,
2900 (unsigned long)-1);
2901 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002902 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002903
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002904 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2905 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002906 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2907 btrfs_put_block_group(cache);
2908 goto again;
2909 }
2910
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002911 if (cache->dirty)
2912 break;
2913 cache = next_block_group(root, cache);
2914 }
2915 if (!cache) {
2916 if (last == 0)
2917 break;
2918 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002919 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002920 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002921
Josef Bacik0cb59c92010-07-02 12:14:14 -04002922 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2923 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002924 cache->dirty = 0;
2925 last = cache->key.objectid + cache->key.offset;
2926
2927 err = write_one_cache_group(trans, root, path, cache);
2928 BUG_ON(err);
2929 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002930 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002931
Josef Bacik0cb59c92010-07-02 12:14:14 -04002932 while (1) {
2933 /*
2934 * I don't think this is needed since we're just marking our
2935 * preallocated extent as written, but just in case it can't
2936 * hurt.
2937 */
2938 if (last == 0) {
2939 err = btrfs_run_delayed_refs(trans, root,
2940 (unsigned long)-1);
2941 BUG_ON(err);
2942 }
2943
2944 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2945 while (cache) {
2946 /*
2947 * Really this shouldn't happen, but it could if we
2948 * couldn't write the entire preallocated extent and
2949 * splitting the extent resulted in a new block.
2950 */
2951 if (cache->dirty) {
2952 btrfs_put_block_group(cache);
2953 goto again;
2954 }
2955 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2956 break;
2957 cache = next_block_group(root, cache);
2958 }
2959 if (!cache) {
2960 if (last == 0)
2961 break;
2962 last = 0;
2963 continue;
2964 }
2965
2966 btrfs_write_out_cache(root, trans, cache, path);
2967
2968 /*
2969 * If we didn't have an error then the cache state is still
2970 * NEED_WRITE, so we can set it to WRITTEN.
2971 */
2972 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2973 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2974 last = cache->key.objectid + cache->key.offset;
2975 btrfs_put_block_group(cache);
2976 }
2977
Chris Mason9078a3e2007-04-26 16:46:15 -04002978 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002979 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002980}
2981
Yan Zhengd2fb3432008-12-11 16:30:39 -05002982int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2983{
2984 struct btrfs_block_group_cache *block_group;
2985 int readonly = 0;
2986
2987 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2988 if (!block_group || block_group->ro)
2989 readonly = 1;
2990 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002991 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002992 return readonly;
2993}
2994
Chris Mason593060d2008-03-25 16:50:33 -04002995static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2996 u64 total_bytes, u64 bytes_used,
2997 struct btrfs_space_info **space_info)
2998{
2999 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003000 int i;
3001 int factor;
3002
3003 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3004 BTRFS_BLOCK_GROUP_RAID10))
3005 factor = 2;
3006 else
3007 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003008
3009 found = __find_space_info(info, flags);
3010 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003011 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003012 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003013 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003014 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003015 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003016 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003017 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003018 *space_info = found;
3019 return 0;
3020 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003021 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003022 if (!found)
3023 return -ENOMEM;
3024
Yan, Zhengb742bb82010-05-16 10:46:24 -04003025 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3026 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003027 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003028 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04003029 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
3030 BTRFS_BLOCK_GROUP_SYSTEM |
3031 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04003032 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003033 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003034 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003035 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003036 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003037 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003038 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003039 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003040 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003041 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Chris Mason593060d2008-03-25 16:50:33 -04003042 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003043 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04003044 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04003045 return 0;
3046}
3047
Chris Mason8790d502008-04-03 16:29:03 -04003048static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3049{
3050 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04003051 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04003052 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04003053 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04003054 if (extra_flags) {
3055 if (flags & BTRFS_BLOCK_GROUP_DATA)
3056 fs_info->avail_data_alloc_bits |= extra_flags;
3057 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3058 fs_info->avail_metadata_alloc_bits |= extra_flags;
3059 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3060 fs_info->avail_system_alloc_bits |= extra_flags;
3061 }
3062}
Chris Mason593060d2008-03-25 16:50:33 -04003063
Yan Zheng2b820322008-11-17 21:11:30 -05003064u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003065{
Chris Masoncd02dca2010-12-13 14:56:23 -05003066 /*
3067 * we add in the count of missing devices because we want
3068 * to make sure that any RAID levels on a degraded FS
3069 * continue to be honored.
3070 */
3071 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3072 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04003073
3074 if (num_devices == 1)
3075 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3076 if (num_devices < 4)
3077 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3078
Chris Masonec44a352008-04-28 15:29:52 -04003079 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3080 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003081 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003082 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003083 }
Chris Masonec44a352008-04-28 15:29:52 -04003084
3085 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003086 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003087 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003088 }
Chris Masonec44a352008-04-28 15:29:52 -04003089
3090 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3091 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3092 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3093 (flags & BTRFS_BLOCK_GROUP_DUP)))
3094 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3095 return flags;
3096}
3097
Yan, Zhengb742bb82010-05-16 10:46:24 -04003098static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003099{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003100 if (flags & BTRFS_BLOCK_GROUP_DATA)
3101 flags |= root->fs_info->avail_data_alloc_bits &
3102 root->fs_info->data_alloc_profile;
3103 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3104 flags |= root->fs_info->avail_system_alloc_bits &
3105 root->fs_info->system_alloc_profile;
3106 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3107 flags |= root->fs_info->avail_metadata_alloc_bits &
3108 root->fs_info->metadata_alloc_profile;
3109 return btrfs_reduce_alloc_profile(root, flags);
3110}
Josef Bacik6a632092009-02-20 11:00:09 -05003111
Miao Xie6d07bce2011-01-05 10:07:31 +00003112u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003113{
3114 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003115
Yan, Zhengb742bb82010-05-16 10:46:24 -04003116 if (data)
3117 flags = BTRFS_BLOCK_GROUP_DATA;
3118 else if (root == root->fs_info->chunk_root)
3119 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3120 else
3121 flags = BTRFS_BLOCK_GROUP_METADATA;
3122
3123 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003124}
3125
3126void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3127{
Josef Bacik6a632092009-02-20 11:00:09 -05003128 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003129 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003130}
3131
3132/*
3133 * This will check the space that the inode allocates from to make sure we have
3134 * enough space for bytes.
3135 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003136int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003137{
3138 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003139 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003140 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003141 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003142
3143 /* make sure bytes are sectorsize aligned */
3144 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3145
Josef Bacik0af3d002010-06-21 14:48:16 -04003146 if (root == root->fs_info->tree_root) {
3147 alloc_chunk = 0;
3148 committed = 1;
3149 }
3150
Josef Bacik6a632092009-02-20 11:00:09 -05003151 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003152 if (!data_sinfo)
3153 goto alloc;
3154
Josef Bacik6a632092009-02-20 11:00:09 -05003155again:
3156 /* make sure we have enough space to handle the data first */
3157 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003158 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3159 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3160 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003161
3162 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003163 struct btrfs_trans_handle *trans;
3164
Josef Bacik6a632092009-02-20 11:00:09 -05003165 /*
3166 * if we don't have enough free bytes in this space then we need
3167 * to alloc a new chunk.
3168 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003169 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003170 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003171
Chris Mason0e4f8f82011-04-15 16:05:44 -04003172 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003173 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003174alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003175 alloc_target = btrfs_get_alloc_profile(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003176 trans = btrfs_join_transaction(root, 1);
3177 if (IS_ERR(trans))
3178 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003179
3180 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3181 bytes + 2 * 1024 * 1024,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003182 alloc_target,
3183 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003184 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003185 if (ret < 0) {
3186 if (ret != -ENOSPC)
3187 return ret;
3188 else
3189 goto commit_trans;
3190 }
Chris Mason33b4d472009-09-22 14:45:50 -04003191
3192 if (!data_sinfo) {
3193 btrfs_set_inode_space_info(root, inode);
3194 data_sinfo = BTRFS_I(inode)->space_info;
3195 }
Josef Bacik6a632092009-02-20 11:00:09 -05003196 goto again;
3197 }
3198 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003199
3200 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003201commit_trans:
Sage Weildd7e0b72009-09-29 18:38:44 -04003202 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003203 committed = 1;
3204 trans = btrfs_join_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003205 if (IS_ERR(trans))
3206 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003207 ret = btrfs_commit_transaction(trans, root);
3208 if (ret)
3209 return ret;
3210 goto again;
3211 }
3212
Chris Mason933b5852010-05-26 11:31:00 -04003213#if 0 /* I hope we never need this code again, just in case */
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003214 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3215 "%llu bytes_reserved, " "%llu bytes_pinned, "
3216 "%llu bytes_readonly, %llu may use %llu total\n",
3217 (unsigned long long)bytes,
Joel Becker21380932009-04-21 12:38:29 -07003218 (unsigned long long)data_sinfo->bytes_used,
3219 (unsigned long long)data_sinfo->bytes_reserved,
3220 (unsigned long long)data_sinfo->bytes_pinned,
3221 (unsigned long long)data_sinfo->bytes_readonly,
3222 (unsigned long long)data_sinfo->bytes_may_use,
3223 (unsigned long long)data_sinfo->total_bytes);
Chris Mason933b5852010-05-26 11:31:00 -04003224#endif
Josef Bacik6a632092009-02-20 11:00:09 -05003225 return -ENOSPC;
3226 }
3227 data_sinfo->bytes_may_use += bytes;
3228 BTRFS_I(inode)->reserved_bytes += bytes;
3229 spin_unlock(&data_sinfo->lock);
3230
Josef Bacik9ed74f22009-09-11 16:12:44 -04003231 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003232}
3233
3234/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003235 * called when we are clearing an delalloc extent from the
3236 * inode's io_tree or there was an error for whatever reason
3237 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003238 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003239void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003240{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003241 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003242 struct btrfs_space_info *data_sinfo;
3243
3244 /* make sure bytes are sectorsize aligned */
3245 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3246
3247 data_sinfo = BTRFS_I(inode)->space_info;
3248 spin_lock(&data_sinfo->lock);
3249 data_sinfo->bytes_may_use -= bytes;
3250 BTRFS_I(inode)->reserved_bytes -= bytes;
3251 spin_unlock(&data_sinfo->lock);
3252}
3253
Josef Bacik97e728d2009-04-21 17:40:57 -04003254static void force_metadata_allocation(struct btrfs_fs_info *info)
3255{
3256 struct list_head *head = &info->space_info;
3257 struct btrfs_space_info *found;
3258
3259 rcu_read_lock();
3260 list_for_each_entry_rcu(found, head, list) {
3261 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003262 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003263 }
3264 rcu_read_unlock();
3265}
3266
Chris Masone5bc2452010-10-26 13:37:56 -04003267static int should_alloc_chunk(struct btrfs_root *root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003268 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3269 int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003270{
3271 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003272 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003273 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003274
Chris Mason0e4f8f82011-04-15 16:05:44 -04003275 if (force == CHUNK_ALLOC_FORCE)
3276 return 1;
3277
3278 /*
3279 * in limited mode, we want to have some free space up to
3280 * about 1% of the FS size.
3281 */
3282 if (force == CHUNK_ALLOC_LIMITED) {
3283 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3284 thresh = max_t(u64, 64 * 1024 * 1024,
3285 div_factor_fine(thresh, 1));
3286
3287 if (num_bytes - num_allocated < thresh)
3288 return 1;
3289 }
3290
3291 /*
3292 * we have two similar checks here, one based on percentage
3293 * and once based on a hard number of 256MB. The idea
3294 * is that if we have a good amount of free
3295 * room, don't allocate a chunk. A good mount is
3296 * less than 80% utilized of the chunks we have allocated,
3297 * or more than 256MB free
3298 */
3299 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003300 return 0;
3301
Chris Mason0e4f8f82011-04-15 16:05:44 -04003302 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
Yan, Zheng424499d2010-05-16 10:46:25 -04003303 return 0;
3304
Chris Masone5bc2452010-10-26 13:37:56 -04003305 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003306
3307 /* 256MB or 5% of the FS */
Chris Masone5bc2452010-10-26 13:37:56 -04003308 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3309
3310 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003311 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003312 return 1;
3313}
3314
Chris Mason6324fbf2008-03-24 15:01:59 -04003315static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3316 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003317 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003318{
3319 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003320 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Yan Zhengc146afa2008-11-12 14:34:12 -05003321 int ret = 0;
3322
Josef Bacik97e728d2009-04-21 17:40:57 -04003323 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003324
Yan Zheng2b820322008-11-17 21:11:30 -05003325 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003326
Chris Mason6324fbf2008-03-24 15:01:59 -04003327 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003328 if (!space_info) {
3329 ret = update_space_info(extent_root->fs_info, flags,
3330 0, 0, &space_info);
3331 BUG_ON(ret);
3332 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003333 BUG_ON(!space_info);
3334
Josef Bacik25179202008-10-29 14:49:05 -04003335 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003336 if (space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003337 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003338 if (space_info->full) {
3339 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003340 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003341 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003342
Chris Mason0e4f8f82011-04-15 16:05:44 -04003343 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003344 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003345 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003346 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003347
Josef Bacik25179202008-10-29 14:49:05 -04003348 spin_unlock(&space_info->lock);
3349
Josef Bacik97e728d2009-04-21 17:40:57 -04003350 /*
Josef Bacik67377732010-09-16 16:19:09 -04003351 * If we have mixed data/metadata chunks we want to make sure we keep
3352 * allocating mixed chunks instead of individual chunks.
3353 */
3354 if (btrfs_mixed_space_info(space_info))
3355 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3356
3357 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003358 * if we're doing a data chunk, go ahead and make sure that
3359 * we keep a reasonable number of metadata chunks allocated in the
3360 * FS as well.
3361 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003362 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003363 fs_info->data_chunk_allocations++;
3364 if (!(fs_info->data_chunk_allocations %
3365 fs_info->metadata_ratio))
3366 force_metadata_allocation(fs_info);
3367 }
3368
Yan Zheng2b820322008-11-17 21:11:30 -05003369 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003370 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003371 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003372 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003373 else
3374 ret = 1;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003375 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003376 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003377out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003378 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003379 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003380}
3381
Yan, Zheng5da9d012010-05-16 10:46:25 -04003382/*
3383 * shrink metadata reservation for delalloc
3384 */
3385static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003386 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003387{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003388 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003389 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003390 u64 reserved;
3391 u64 max_reclaim;
3392 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003393 long time_left;
Chris Masonbf9022e2010-10-26 13:40:45 -04003394 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003395 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003396 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003397
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003398 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003399 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003400
3401 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003402 reserved = space_info->bytes_reserved;
Chris Mason36e39c42011-03-12 07:08:42 -05003403 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003404
3405 if (reserved == 0)
3406 return 0;
3407
3408 max_reclaim = min(reserved, to_reclaim);
3409
Josef Bacikb1953bc2011-01-21 21:10:01 +00003410 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003411 /* have the flusher threads jump in and do some IO */
3412 smp_mb();
3413 nr_pages = min_t(unsigned long, nr_pages,
3414 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3415 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003416
Josef Bacik0019f102010-10-15 15:18:40 -04003417 spin_lock(&space_info->lock);
Chris Mason36e39c42011-03-12 07:08:42 -05003418 if (reserved > space_info->bytes_reserved)
Josef Bacik0019f102010-10-15 15:18:40 -04003419 reclaimed += reserved - space_info->bytes_reserved;
3420 reserved = space_info->bytes_reserved;
3421 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003422
Chris Mason36e39c42011-03-12 07:08:42 -05003423 loops++;
3424
Yan, Zheng5da9d012010-05-16 10:46:25 -04003425 if (reserved == 0 || reclaimed >= max_reclaim)
3426 break;
3427
3428 if (trans && trans->transaction->blocked)
3429 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003430
Chris Mason36e39c42011-03-12 07:08:42 -05003431 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003432
3433 /* We were interrupted, exit */
3434 if (time_left)
3435 break;
3436
Chris Mason36e39c42011-03-12 07:08:42 -05003437 /* we've kicked the IO a few times, if anything has been freed,
3438 * exit. There is no sense in looping here for a long time
3439 * when we really need to commit the transaction, or there are
3440 * just too many writers without enough free space
3441 */
3442
3443 if (loops > 3) {
3444 smp_mb();
3445 if (progress != space_info->reservation_progress)
3446 break;
3447 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003448
Yan, Zheng5da9d012010-05-16 10:46:25 -04003449 }
3450 return reclaimed >= to_reclaim;
3451}
3452
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003453/*
3454 * Retries tells us how many times we've called reserve_metadata_bytes. The
3455 * idea is if this is the first call (retries == 0) then we will add to our
3456 * reserved count if we can't make the allocation in order to hold our place
3457 * while we go and try and free up space. That way for retries > 1 we don't try
3458 * and add space, we just check to see if the amount of unused space is >= the
3459 * total space, meaning that our reservation is valid.
3460 *
3461 * However if we don't intend to retry this reservation, pass -1 as retries so
3462 * that it short circuits this logic.
3463 */
3464static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3465 struct btrfs_root *root,
3466 struct btrfs_block_rsv *block_rsv,
3467 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003468{
3469 struct btrfs_space_info *space_info = block_rsv->space_info;
3470 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003471 u64 num_bytes = orig_bytes;
3472 int retries = 0;
3473 int ret = 0;
3474 bool reserved = false;
Josef Bacik38227932010-10-26 12:52:53 -04003475 bool committed = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003476
3477again:
3478 ret = -ENOSPC;
3479 if (reserved)
3480 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003481
3482 spin_lock(&space_info->lock);
3483 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003484 space_info->bytes_pinned + space_info->bytes_readonly +
3485 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003486
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003487 /*
3488 * The idea here is that we've not already over-reserved the block group
3489 * then we can go ahead and save our reservation first and then start
3490 * flushing if we need to. Otherwise if we've already overcommitted
3491 * lets start flushing stuff first and then come back and try to make
3492 * our reservation.
3493 */
3494 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003495 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003496 if (unused >= num_bytes) {
3497 if (!reserved)
3498 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003499 ret = 0;
3500 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003501 /*
3502 * Ok set num_bytes to orig_bytes since we aren't
3503 * overocmmitted, this way we only try and reclaim what
3504 * we need.
3505 */
3506 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003507 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003508 } else {
3509 /*
3510 * Ok we're over committed, set num_bytes to the overcommitted
3511 * amount plus the amount of bytes that we need for this
3512 * reservation.
3513 */
3514 num_bytes = unused - space_info->total_bytes +
3515 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003516 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003517
3518 /*
3519 * Couldn't make our reservation, save our place so while we're trying
3520 * to reclaim space we can actually use it instead of somebody else
3521 * stealing it from us.
3522 */
3523 if (ret && !reserved) {
3524 space_info->bytes_reserved += orig_bytes;
3525 reserved = true;
3526 }
3527
Yan, Zhengf0486c62010-05-16 10:46:25 -04003528 spin_unlock(&space_info->lock);
3529
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003530 if (!ret)
3531 return 0;
3532
3533 if (!flush)
3534 goto out;
3535
3536 /*
3537 * We do synchronous shrinking since we don't actually unreserve
3538 * metadata until after the IO is completed.
3539 */
3540 ret = shrink_delalloc(trans, root, num_bytes, 1);
3541 if (ret > 0)
3542 return 0;
3543 else if (ret < 0)
3544 goto out;
3545
3546 /*
3547 * So if we were overcommitted it's possible that somebody else flushed
3548 * out enough space and we simply didn't have enough space to reclaim,
3549 * so go back around and try again.
3550 */
3551 if (retries < 2) {
3552 retries++;
3553 goto again;
3554 }
3555
3556 spin_lock(&space_info->lock);
3557 /*
3558 * Not enough space to be reclaimed, don't bother committing the
3559 * transaction.
3560 */
3561 if (space_info->bytes_pinned < orig_bytes)
3562 ret = -ENOSPC;
3563 spin_unlock(&space_info->lock);
3564 if (ret)
3565 goto out;
3566
3567 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003568 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003569 goto out;
3570
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003571 ret = -ENOSPC;
3572 trans = btrfs_join_transaction(root, 1);
3573 if (IS_ERR(trans))
3574 goto out;
3575 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003576 if (!ret) {
3577 trans = NULL;
3578 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003579 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003580 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003581
3582out:
3583 if (reserved) {
3584 spin_lock(&space_info->lock);
3585 space_info->bytes_reserved -= orig_bytes;
3586 spin_unlock(&space_info->lock);
3587 }
3588
Yan, Zhengf0486c62010-05-16 10:46:25 -04003589 return ret;
3590}
3591
3592static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3593 struct btrfs_root *root)
3594{
3595 struct btrfs_block_rsv *block_rsv;
3596 if (root->ref_cows)
3597 block_rsv = trans->block_rsv;
3598 else
3599 block_rsv = root->block_rsv;
3600
3601 if (!block_rsv)
3602 block_rsv = &root->fs_info->empty_block_rsv;
3603
3604 return block_rsv;
3605}
3606
3607static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3608 u64 num_bytes)
3609{
3610 int ret = -ENOSPC;
3611 spin_lock(&block_rsv->lock);
3612 if (block_rsv->reserved >= num_bytes) {
3613 block_rsv->reserved -= num_bytes;
3614 if (block_rsv->reserved < block_rsv->size)
3615 block_rsv->full = 0;
3616 ret = 0;
3617 }
3618 spin_unlock(&block_rsv->lock);
3619 return ret;
3620}
3621
3622static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3623 u64 num_bytes, int update_size)
3624{
3625 spin_lock(&block_rsv->lock);
3626 block_rsv->reserved += num_bytes;
3627 if (update_size)
3628 block_rsv->size += num_bytes;
3629 else if (block_rsv->reserved >= block_rsv->size)
3630 block_rsv->full = 1;
3631 spin_unlock(&block_rsv->lock);
3632}
3633
3634void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3635 struct btrfs_block_rsv *dest, u64 num_bytes)
3636{
3637 struct btrfs_space_info *space_info = block_rsv->space_info;
3638
3639 spin_lock(&block_rsv->lock);
3640 if (num_bytes == (u64)-1)
3641 num_bytes = block_rsv->size;
3642 block_rsv->size -= num_bytes;
3643 if (block_rsv->reserved >= block_rsv->size) {
3644 num_bytes = block_rsv->reserved - block_rsv->size;
3645 block_rsv->reserved = block_rsv->size;
3646 block_rsv->full = 1;
3647 } else {
3648 num_bytes = 0;
3649 }
3650 spin_unlock(&block_rsv->lock);
3651
3652 if (num_bytes > 0) {
3653 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003654 spin_lock(&dest->lock);
3655 if (!dest->full) {
3656 u64 bytes_to_add;
3657
3658 bytes_to_add = dest->size - dest->reserved;
3659 bytes_to_add = min(num_bytes, bytes_to_add);
3660 dest->reserved += bytes_to_add;
3661 if (dest->reserved >= dest->size)
3662 dest->full = 1;
3663 num_bytes -= bytes_to_add;
3664 }
3665 spin_unlock(&dest->lock);
3666 }
3667 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003668 spin_lock(&space_info->lock);
3669 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003670 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003671 spin_unlock(&space_info->lock);
3672 }
3673 }
3674}
3675
3676static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3677 struct btrfs_block_rsv *dst, u64 num_bytes)
3678{
3679 int ret;
3680
3681 ret = block_rsv_use_bytes(src, num_bytes);
3682 if (ret)
3683 return ret;
3684
3685 block_rsv_add_bytes(dst, num_bytes, 1);
3686 return 0;
3687}
3688
3689void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3690{
3691 memset(rsv, 0, sizeof(*rsv));
3692 spin_lock_init(&rsv->lock);
3693 atomic_set(&rsv->usage, 1);
3694 rsv->priority = 6;
3695 INIT_LIST_HEAD(&rsv->list);
3696}
3697
3698struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3699{
3700 struct btrfs_block_rsv *block_rsv;
3701 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003702
3703 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3704 if (!block_rsv)
3705 return NULL;
3706
3707 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003708 block_rsv->space_info = __find_space_info(fs_info,
3709 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003710 return block_rsv;
3711}
3712
3713void btrfs_free_block_rsv(struct btrfs_root *root,
3714 struct btrfs_block_rsv *rsv)
3715{
3716 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3717 btrfs_block_rsv_release(root, rsv, (u64)-1);
3718 if (!rsv->durable)
3719 kfree(rsv);
3720 }
3721}
3722
3723/*
3724 * make the block_rsv struct be able to capture freed space.
3725 * the captured space will re-add to the the block_rsv struct
3726 * after transaction commit
3727 */
3728void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3729 struct btrfs_block_rsv *block_rsv)
3730{
3731 block_rsv->durable = 1;
3732 mutex_lock(&fs_info->durable_block_rsv_mutex);
3733 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3734 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3735}
3736
3737int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3738 struct btrfs_root *root,
3739 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003740 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003741{
3742 int ret;
3743
3744 if (num_bytes == 0)
3745 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003746
3747 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003748 if (!ret) {
3749 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3750 return 0;
3751 }
3752
Yan, Zhengf0486c62010-05-16 10:46:25 -04003753 return ret;
3754}
3755
3756int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3757 struct btrfs_root *root,
3758 struct btrfs_block_rsv *block_rsv,
3759 u64 min_reserved, int min_factor)
3760{
3761 u64 num_bytes = 0;
3762 int commit_trans = 0;
3763 int ret = -ENOSPC;
3764
3765 if (!block_rsv)
3766 return 0;
3767
3768 spin_lock(&block_rsv->lock);
3769 if (min_factor > 0)
3770 num_bytes = div_factor(block_rsv->size, min_factor);
3771 if (min_reserved > num_bytes)
3772 num_bytes = min_reserved;
3773
3774 if (block_rsv->reserved >= num_bytes) {
3775 ret = 0;
3776 } else {
3777 num_bytes -= block_rsv->reserved;
3778 if (block_rsv->durable &&
3779 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3780 commit_trans = 1;
3781 }
3782 spin_unlock(&block_rsv->lock);
3783 if (!ret)
3784 return 0;
3785
3786 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003787 ret = reserve_metadata_bytes(trans, root, block_rsv,
3788 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003789 if (!ret) {
3790 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3791 return 0;
3792 }
3793 }
3794
3795 if (commit_trans) {
3796 if (trans)
3797 return -EAGAIN;
3798
3799 trans = btrfs_join_transaction(root, 1);
3800 BUG_ON(IS_ERR(trans));
3801 ret = btrfs_commit_transaction(trans, root);
3802 return 0;
3803 }
3804
Yan, Zhengf0486c62010-05-16 10:46:25 -04003805 return -ENOSPC;
3806}
3807
3808int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3809 struct btrfs_block_rsv *dst_rsv,
3810 u64 num_bytes)
3811{
3812 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3813}
3814
3815void btrfs_block_rsv_release(struct btrfs_root *root,
3816 struct btrfs_block_rsv *block_rsv,
3817 u64 num_bytes)
3818{
3819 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3820 if (global_rsv->full || global_rsv == block_rsv ||
3821 block_rsv->space_info != global_rsv->space_info)
3822 global_rsv = NULL;
3823 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3824}
3825
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003826/*
3827 * helper to calculate size of global block reservation.
3828 * the desired value is sum of space used by extent tree,
3829 * checksum tree and root tree
3830 */
3831static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3832{
3833 struct btrfs_space_info *sinfo;
3834 u64 num_bytes;
3835 u64 meta_used;
3836 u64 data_used;
3837 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3838#if 0
3839 /*
3840 * per tree used space accounting can be inaccuracy, so we
3841 * can't rely on it.
3842 */
3843 spin_lock(&fs_info->extent_root->accounting_lock);
3844 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3845 spin_unlock(&fs_info->extent_root->accounting_lock);
3846
3847 spin_lock(&fs_info->csum_root->accounting_lock);
3848 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3849 spin_unlock(&fs_info->csum_root->accounting_lock);
3850
3851 spin_lock(&fs_info->tree_root->accounting_lock);
3852 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3853 spin_unlock(&fs_info->tree_root->accounting_lock);
3854#endif
3855 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3856 spin_lock(&sinfo->lock);
3857 data_used = sinfo->bytes_used;
3858 spin_unlock(&sinfo->lock);
3859
3860 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3861 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003862 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3863 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003864 meta_used = sinfo->bytes_used;
3865 spin_unlock(&sinfo->lock);
3866
3867 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3868 csum_size * 2;
3869 num_bytes += div64_u64(data_used + meta_used, 50);
3870
3871 if (num_bytes * 3 > meta_used)
3872 num_bytes = div64_u64(meta_used, 3);
3873
3874 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3875}
3876
3877static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3878{
3879 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3880 struct btrfs_space_info *sinfo = block_rsv->space_info;
3881 u64 num_bytes;
3882
3883 num_bytes = calc_global_metadata_size(fs_info);
3884
3885 spin_lock(&block_rsv->lock);
3886 spin_lock(&sinfo->lock);
3887
3888 block_rsv->size = num_bytes;
3889
3890 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003891 sinfo->bytes_reserved + sinfo->bytes_readonly +
3892 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003893
3894 if (sinfo->total_bytes > num_bytes) {
3895 num_bytes = sinfo->total_bytes - num_bytes;
3896 block_rsv->reserved += num_bytes;
3897 sinfo->bytes_reserved += num_bytes;
3898 }
3899
3900 if (block_rsv->reserved >= block_rsv->size) {
3901 num_bytes = block_rsv->reserved - block_rsv->size;
3902 sinfo->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003903 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003904 block_rsv->reserved = block_rsv->size;
3905 block_rsv->full = 1;
3906 }
3907#if 0
3908 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3909 block_rsv->size, block_rsv->reserved);
3910#endif
3911 spin_unlock(&sinfo->lock);
3912 spin_unlock(&block_rsv->lock);
3913}
3914
Yan, Zhengf0486c62010-05-16 10:46:25 -04003915static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3916{
3917 struct btrfs_space_info *space_info;
3918
3919 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3920 fs_info->chunk_block_rsv.space_info = space_info;
3921 fs_info->chunk_block_rsv.priority = 10;
3922
3923 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003924 fs_info->global_block_rsv.space_info = space_info;
3925 fs_info->global_block_rsv.priority = 10;
3926 fs_info->global_block_rsv.refill_used = 1;
3927 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003928 fs_info->trans_block_rsv.space_info = space_info;
3929 fs_info->empty_block_rsv.space_info = space_info;
3930 fs_info->empty_block_rsv.priority = 10;
3931
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003932 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3933 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3934 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3935 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003936 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003937
3938 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3939
3940 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3941
3942 update_global_block_rsv(fs_info);
3943}
3944
3945static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3946{
3947 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3948 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3949 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3950 WARN_ON(fs_info->trans_block_rsv.size > 0);
3951 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3952 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3953 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003954}
3955
Yan, Zhenga22285a2010-05-16 10:48:46 -04003956static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3957{
3958 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3959 3 * num_items;
3960}
3961
3962int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3963 struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003964 int num_items)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003965{
3966 u64 num_bytes;
3967 int ret;
3968
3969 if (num_items == 0 || root->fs_info->chunk_root == root)
3970 return 0;
3971
3972 num_bytes = calc_trans_metadata_size(root, num_items);
3973 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003974 num_bytes);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003975 if (!ret) {
3976 trans->bytes_reserved += num_bytes;
3977 trans->block_rsv = &root->fs_info->trans_block_rsv;
3978 }
3979 return ret;
3980}
3981
3982void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3983 struct btrfs_root *root)
3984{
3985 if (!trans->bytes_reserved)
3986 return;
3987
3988 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3989 btrfs_block_rsv_release(root, trans->block_rsv,
3990 trans->bytes_reserved);
3991 trans->bytes_reserved = 0;
3992}
3993
Yan, Zhengd68fc572010-05-16 10:49:58 -04003994int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3995 struct inode *inode)
3996{
3997 struct btrfs_root *root = BTRFS_I(inode)->root;
3998 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3999 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4000
4001 /*
4002 * one for deleting orphan item, one for updating inode and
4003 * two for calling btrfs_truncate_inode_items.
4004 *
4005 * btrfs_truncate_inode_items is a delete operation, it frees
4006 * more space than it uses in most cases. So two units of
4007 * metadata space should be enough for calling it many times.
4008 * If all of the metadata space is used, we can commit
4009 * transaction and use space it freed.
4010 */
4011 u64 num_bytes = calc_trans_metadata_size(root, 4);
4012 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4013}
4014
4015void btrfs_orphan_release_metadata(struct inode *inode)
4016{
4017 struct btrfs_root *root = BTRFS_I(inode)->root;
4018 u64 num_bytes = calc_trans_metadata_size(root, 4);
4019 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4020}
4021
Yan, Zhenga22285a2010-05-16 10:48:46 -04004022int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4023 struct btrfs_pending_snapshot *pending)
4024{
4025 struct btrfs_root *root = pending->root;
4026 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4027 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4028 /*
4029 * two for root back/forward refs, two for directory entries
4030 * and one for root of the snapshot.
4031 */
4032 u64 num_bytes = calc_trans_metadata_size(root, 5);
4033 dst_rsv->space_info = src_rsv->space_info;
4034 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4035}
4036
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004037static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
4038{
4039 return num_bytes >>= 3;
4040}
4041
4042int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4043{
4044 struct btrfs_root *root = BTRFS_I(inode)->root;
4045 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
4046 u64 to_reserve;
4047 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004048 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004049 int ret;
4050
4051 if (btrfs_transaction_in_commit(root->fs_info))
4052 schedule_timeout(1);
4053
4054 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004055
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004056 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004057 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4058
4059 if (nr_extents > reserved_extents) {
4060 nr_extents -= reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004061 to_reserve = calc_trans_metadata_size(root, nr_extents);
4062 } else {
4063 nr_extents = 0;
4064 to_reserve = 0;
4065 }
Josef Bacik57a45ced2011-01-25 16:30:38 -05004066
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004067 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004068 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
4069 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004070 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004071
Josef Bacik57a45ced2011-01-25 16:30:38 -05004072 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004073 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004074
4075 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4076
4077 if (block_rsv->size > 512 * 1024 * 1024)
Josef Bacik0019f102010-10-15 15:18:40 -04004078 shrink_delalloc(NULL, root, to_reserve, 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004079
4080 return 0;
4081}
4082
4083void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4084{
4085 struct btrfs_root *root = BTRFS_I(inode)->root;
4086 u64 to_free;
4087 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004088 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004089
4090 num_bytes = ALIGN(num_bytes, root->sectorsize);
4091 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik3c148742011-02-02 15:53:47 +00004092 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004093
Josef Bacik57a45ced2011-01-25 16:30:38 -05004094 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4095 do {
4096 int old, new;
4097
4098 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4099 if (nr_extents >= reserved_extents) {
4100 nr_extents = 0;
4101 break;
4102 }
4103 old = reserved_extents;
4104 nr_extents = reserved_extents - nr_extents;
4105 new = reserved_extents - nr_extents;
4106 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
4107 reserved_extents, new);
4108 if (likely(old == reserved_extents))
4109 break;
4110 reserved_extents = old;
4111 } while (1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004112
4113 to_free = calc_csum_metadata_size(inode, num_bytes);
4114 if (nr_extents > 0)
4115 to_free += calc_trans_metadata_size(root, nr_extents);
4116
4117 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4118 to_free);
4119}
4120
4121int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4122{
4123 int ret;
4124
4125 ret = btrfs_check_data_free_space(inode, num_bytes);
4126 if (ret)
4127 return ret;
4128
4129 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4130 if (ret) {
4131 btrfs_free_reserved_data_space(inode, num_bytes);
4132 return ret;
4133 }
4134
4135 return 0;
4136}
4137
4138void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4139{
4140 btrfs_delalloc_release_metadata(inode, num_bytes);
4141 btrfs_free_reserved_data_space(inode, num_bytes);
4142}
4143
Chris Mason9078a3e2007-04-26 16:46:15 -04004144static int update_block_group(struct btrfs_trans_handle *trans,
4145 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004146 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004147{
Josef Bacik0af3d002010-06-21 14:48:16 -04004148 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004149 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004150 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004151 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004152 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004153 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004154
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004155 /* block accounting for super block */
4156 spin_lock(&info->delalloc_lock);
4157 old_val = btrfs_super_bytes_used(&info->super_copy);
4158 if (alloc)
4159 old_val += num_bytes;
4160 else
4161 old_val -= num_bytes;
4162 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004163 spin_unlock(&info->delalloc_lock);
4164
Chris Masond3977122009-01-05 21:25:51 -05004165 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004166 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004167 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004168 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004169 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4170 BTRFS_BLOCK_GROUP_RAID1 |
4171 BTRFS_BLOCK_GROUP_RAID10))
4172 factor = 2;
4173 else
4174 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004175 /*
4176 * If this block group has free space cache written out, we
4177 * need to make sure to load it if we are removing space. This
4178 * is because we need the unpinning stage to actually add the
4179 * space back to the block group, otherwise we will leak space.
4180 */
4181 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004182 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004183
Chris Masondb945352007-10-15 16:15:53 -04004184 byte_in_group = bytenr - cache->key.objectid;
4185 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004186
Josef Bacik25179202008-10-29 14:49:05 -04004187 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004188 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004189
4190 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4191 cache->disk_cache_state < BTRFS_DC_CLEAR)
4192 cache->disk_cache_state = BTRFS_DC_CLEAR;
4193
Josef Bacik0f9dd462008-09-23 13:14:11 -04004194 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004195 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004196 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004197 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004198 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004199 btrfs_set_block_group_used(&cache->item, old_val);
4200 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004201 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004202 cache->space_info->reservation_progress++;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004203 cache->space_info->bytes_used += num_bytes;
4204 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004205 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004206 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004207 } else {
Chris Masondb945352007-10-15 16:15:53 -04004208 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004209 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004210 cache->pinned += num_bytes;
4211 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004212 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004213 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004214 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004215 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004216
Yan, Zhengf0486c62010-05-16 10:46:25 -04004217 set_extent_dirty(info->pinned_extents,
4218 bytenr, bytenr + num_bytes - 1,
4219 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004220 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004221 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004222 total -= num_bytes;
4223 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004224 }
4225 return 0;
4226}
Chris Mason6324fbf2008-03-24 15:01:59 -04004227
Chris Masona061fc82008-05-07 11:43:44 -04004228static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4229{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004230 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004231 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004232
4233 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4234 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004235 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004236
Yan Zhengd2fb3432008-12-11 16:30:39 -05004237 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004238 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004239
4240 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004241}
4242
Yan, Zhengf0486c62010-05-16 10:46:25 -04004243static int pin_down_extent(struct btrfs_root *root,
4244 struct btrfs_block_group_cache *cache,
4245 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004246{
Yan Zheng11833d62009-09-11 16:11:19 -04004247 spin_lock(&cache->space_info->lock);
4248 spin_lock(&cache->lock);
4249 cache->pinned += num_bytes;
4250 cache->space_info->bytes_pinned += num_bytes;
4251 if (reserved) {
4252 cache->reserved -= num_bytes;
4253 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004254 cache->space_info->reservation_progress++;
Yan324ae4d2007-11-16 14:57:08 -05004255 }
Yan Zheng11833d62009-09-11 16:11:19 -04004256 spin_unlock(&cache->lock);
4257 spin_unlock(&cache->space_info->lock);
4258
Yan, Zhengf0486c62010-05-16 10:46:25 -04004259 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4260 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004261 return 0;
4262}
Chris Mason9078a3e2007-04-26 16:46:15 -04004263
Yan, Zhengf0486c62010-05-16 10:46:25 -04004264/*
4265 * this function must be called within transaction
4266 */
4267int btrfs_pin_extent(struct btrfs_root *root,
4268 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004269{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004270 struct btrfs_block_group_cache *cache;
4271
4272 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4273 BUG_ON(!cache);
4274
4275 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4276
4277 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004278 return 0;
4279}
Zheng Yane8569812008-09-26 10:05:48 -04004280
Yan, Zhengf0486c62010-05-16 10:46:25 -04004281/*
4282 * update size of reserved extents. this function may return -EAGAIN
4283 * if 'reserve' is true or 'sinfo' is false.
4284 */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004285int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4286 u64 num_bytes, int reserve, int sinfo)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004287{
4288 int ret = 0;
4289 if (sinfo) {
4290 struct btrfs_space_info *space_info = cache->space_info;
4291 spin_lock(&space_info->lock);
4292 spin_lock(&cache->lock);
4293 if (reserve) {
4294 if (cache->ro) {
4295 ret = -EAGAIN;
4296 } else {
4297 cache->reserved += num_bytes;
4298 space_info->bytes_reserved += num_bytes;
4299 }
4300 } else {
4301 if (cache->ro)
4302 space_info->bytes_readonly += num_bytes;
4303 cache->reserved -= num_bytes;
4304 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004305 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004306 }
4307 spin_unlock(&cache->lock);
4308 spin_unlock(&space_info->lock);
4309 } else {
4310 spin_lock(&cache->lock);
4311 if (cache->ro) {
4312 ret = -EAGAIN;
4313 } else {
4314 if (reserve)
4315 cache->reserved += num_bytes;
4316 else
4317 cache->reserved -= num_bytes;
4318 }
4319 spin_unlock(&cache->lock);
4320 }
4321 return ret;
4322}
4323
Yan Zheng11833d62009-09-11 16:11:19 -04004324int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4325 struct btrfs_root *root)
4326{
4327 struct btrfs_fs_info *fs_info = root->fs_info;
4328 struct btrfs_caching_control *next;
4329 struct btrfs_caching_control *caching_ctl;
4330 struct btrfs_block_group_cache *cache;
4331
4332 down_write(&fs_info->extent_commit_sem);
4333
4334 list_for_each_entry_safe(caching_ctl, next,
4335 &fs_info->caching_block_groups, list) {
4336 cache = caching_ctl->block_group;
4337 if (block_group_cache_done(cache)) {
4338 cache->last_byte_to_unpin = (u64)-1;
4339 list_del_init(&caching_ctl->list);
4340 put_caching_control(caching_ctl);
4341 } else {
4342 cache->last_byte_to_unpin = caching_ctl->progress;
4343 }
4344 }
4345
4346 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4347 fs_info->pinned_extents = &fs_info->freed_extents[1];
4348 else
4349 fs_info->pinned_extents = &fs_info->freed_extents[0];
4350
4351 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004352
4353 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004354 return 0;
4355}
4356
4357static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4358{
4359 struct btrfs_fs_info *fs_info = root->fs_info;
4360 struct btrfs_block_group_cache *cache = NULL;
4361 u64 len;
4362
4363 while (start <= end) {
4364 if (!cache ||
4365 start >= cache->key.objectid + cache->key.offset) {
4366 if (cache)
4367 btrfs_put_block_group(cache);
4368 cache = btrfs_lookup_block_group(fs_info, start);
4369 BUG_ON(!cache);
4370 }
4371
4372 len = cache->key.objectid + cache->key.offset - start;
4373 len = min(len, end + 1 - start);
4374
4375 if (start < cache->last_byte_to_unpin) {
4376 len = min(len, cache->last_byte_to_unpin - start);
4377 btrfs_add_free_space(cache, start, len);
4378 }
Josef Bacik25179202008-10-29 14:49:05 -04004379
Yan, Zhengf0486c62010-05-16 10:46:25 -04004380 start += len;
4381
Josef Bacik25179202008-10-29 14:49:05 -04004382 spin_lock(&cache->space_info->lock);
4383 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004384 cache->pinned -= len;
4385 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004386 if (cache->ro) {
4387 cache->space_info->bytes_readonly += len;
4388 } else if (cache->reserved_pinned > 0) {
4389 len = min(len, cache->reserved_pinned);
4390 cache->reserved_pinned -= len;
4391 cache->space_info->bytes_reserved += len;
4392 }
Josef Bacik25179202008-10-29 14:49:05 -04004393 spin_unlock(&cache->lock);
4394 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004395 }
4396
4397 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004398 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004399 return 0;
4400}
4401
4402int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004403 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004404{
Yan Zheng11833d62009-09-11 16:11:19 -04004405 struct btrfs_fs_info *fs_info = root->fs_info;
4406 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004407 struct btrfs_block_rsv *block_rsv;
4408 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004409 u64 start;
4410 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004411 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004412 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004413
Yan Zheng11833d62009-09-11 16:11:19 -04004414 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4415 unpin = &fs_info->freed_extents[1];
4416 else
4417 unpin = &fs_info->freed_extents[0];
4418
Chris Masond3977122009-01-05 21:25:51 -05004419 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004420 ret = find_first_extent_bit(unpin, 0, &start, &end,
4421 EXTENT_DIRTY);
4422 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004423 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004424
Li Dongyang5378e602011-03-24 10:24:27 +00004425 if (btrfs_test_opt(root, DISCARD))
4426 ret = btrfs_discard_extent(root, start,
4427 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004428
Chris Mason1a5bc162007-10-15 16:15:26 -04004429 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004430 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004431 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004432 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004433
Yan, Zhengf0486c62010-05-16 10:46:25 -04004434 mutex_lock(&fs_info->durable_block_rsv_mutex);
4435 list_for_each_entry_safe(block_rsv, next_rsv,
4436 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004437
Yan, Zhengf0486c62010-05-16 10:46:25 -04004438 idx = trans->transid & 0x1;
4439 if (block_rsv->freed[idx] > 0) {
4440 block_rsv_add_bytes(block_rsv,
4441 block_rsv->freed[idx], 0);
4442 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004443 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004444 if (atomic_read(&block_rsv->usage) == 0) {
4445 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004446
Yan, Zhengf0486c62010-05-16 10:46:25 -04004447 if (block_rsv->freed[0] == 0 &&
4448 block_rsv->freed[1] == 0) {
4449 list_del_init(&block_rsv->list);
4450 kfree(block_rsv);
4451 }
4452 } else {
4453 btrfs_block_rsv_release(root, block_rsv, 0);
4454 }
4455 }
4456 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4457
Chris Masone20d96d2007-03-22 12:13:20 -04004458 return 0;
4459}
4460
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004461static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4462 struct btrfs_root *root,
4463 u64 bytenr, u64 num_bytes, u64 parent,
4464 u64 root_objectid, u64 owner_objectid,
4465 u64 owner_offset, int refs_to_drop,
4466 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004467{
Chris Masone2fa7222007-03-12 16:22:34 -04004468 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004469 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004470 struct btrfs_fs_info *info = root->fs_info;
4471 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004472 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004473 struct btrfs_extent_item *ei;
4474 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004475 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004476 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004477 int extent_slot = 0;
4478 int found_extent = 0;
4479 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004480 u32 item_size;
4481 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004482
Chris Mason5caf2a02007-04-02 11:20:42 -04004483 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004484 if (!path)
4485 return -ENOMEM;
4486
Chris Mason3c12ac72008-04-21 12:01:38 -04004487 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004488 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004489
4490 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4491 BUG_ON(!is_data && refs_to_drop != 1);
4492
4493 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4494 bytenr, num_bytes, parent,
4495 root_objectid, owner_objectid,
4496 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004497 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004498 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004499 while (extent_slot >= 0) {
4500 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004501 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004502 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004503 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004504 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4505 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004506 found_extent = 1;
4507 break;
4508 }
4509 if (path->slots[0] - extent_slot > 5)
4510 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004511 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004512 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004513#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4514 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4515 if (found_extent && item_size < sizeof(*ei))
4516 found_extent = 0;
4517#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004518 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004519 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004520 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004521 NULL, refs_to_drop,
4522 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004523 BUG_ON(ret);
4524 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04004525 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004526
4527 key.objectid = bytenr;
4528 key.type = BTRFS_EXTENT_ITEM_KEY;
4529 key.offset = num_bytes;
4530
Zheng Yan31840ae2008-09-23 13:14:14 -04004531 ret = btrfs_search_slot(trans, extent_root,
4532 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004533 if (ret) {
4534 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004535 ", was looking for %llu\n", ret,
4536 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004537 btrfs_print_leaf(extent_root, path->nodes[0]);
4538 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004539 BUG_ON(ret);
4540 extent_slot = path->slots[0];
4541 }
Chris Mason7bb86312007-12-11 09:25:06 -05004542 } else {
4543 btrfs_print_leaf(extent_root, path->nodes[0]);
4544 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004545 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004546 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004547 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004548 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004549 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004550 (unsigned long long)owner_objectid,
4551 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004552 }
Chris Mason5f39d392007-10-15 16:14:19 -04004553
4554 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004555 item_size = btrfs_item_size_nr(leaf, extent_slot);
4556#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4557 if (item_size < sizeof(*ei)) {
4558 BUG_ON(found_extent || extent_slot != path->slots[0]);
4559 ret = convert_extent_item_v0(trans, extent_root, path,
4560 owner_objectid, 0);
4561 BUG_ON(ret < 0);
4562
4563 btrfs_release_path(extent_root, path);
4564 path->leave_spinning = 1;
4565
4566 key.objectid = bytenr;
4567 key.type = BTRFS_EXTENT_ITEM_KEY;
4568 key.offset = num_bytes;
4569
4570 ret = btrfs_search_slot(trans, extent_root, &key, path,
4571 -1, 1);
4572 if (ret) {
4573 printk(KERN_ERR "umm, got %d back from search"
4574 ", was looking for %llu\n", ret,
4575 (unsigned long long)bytenr);
4576 btrfs_print_leaf(extent_root, path->nodes[0]);
4577 }
4578 BUG_ON(ret);
4579 extent_slot = path->slots[0];
4580 leaf = path->nodes[0];
4581 item_size = btrfs_item_size_nr(leaf, extent_slot);
4582 }
4583#endif
4584 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004585 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004586 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004587 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4588 struct btrfs_tree_block_info *bi;
4589 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4590 bi = (struct btrfs_tree_block_info *)(ei + 1);
4591 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004592 }
4593
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004594 refs = btrfs_extent_refs(leaf, ei);
4595 BUG_ON(refs < refs_to_drop);
4596 refs -= refs_to_drop;
4597
4598 if (refs > 0) {
4599 if (extent_op)
4600 __run_delayed_extent_op(extent_op, leaf, ei);
4601 /*
4602 * In the case of inline back ref, reference count will
4603 * be updated by remove_extent_backref
4604 */
4605 if (iref) {
4606 BUG_ON(!found_extent);
4607 } else {
4608 btrfs_set_extent_refs(leaf, ei, refs);
4609 btrfs_mark_buffer_dirty(leaf);
4610 }
4611 if (found_extent) {
4612 ret = remove_extent_backref(trans, extent_root, path,
4613 iref, refs_to_drop,
4614 is_data);
4615 BUG_ON(ret);
4616 }
4617 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004618 if (found_extent) {
4619 BUG_ON(is_data && refs_to_drop !=
4620 extent_data_ref_count(root, path, iref));
4621 if (iref) {
4622 BUG_ON(path->slots[0] != extent_slot);
4623 } else {
4624 BUG_ON(path->slots[0] != extent_slot + 1);
4625 path->slots[0] = extent_slot;
4626 num_to_del = 2;
4627 }
Chris Mason78fae272007-03-25 11:35:08 -04004628 }
Chris Masonb9473432009-03-13 11:00:37 -04004629
Chris Mason952fcca2008-02-18 16:33:44 -05004630 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4631 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004632 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04004633 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01004634
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004635 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004636 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4637 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004638 } else {
4639 invalidate_mapping_pages(info->btree_inode->i_mapping,
4640 bytenr >> PAGE_CACHE_SHIFT,
4641 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004642 }
4643
Yan, Zhengf0486c62010-05-16 10:46:25 -04004644 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004645 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004646 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004647 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004648 return ret;
4649}
4650
4651/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004652 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004653 * delayed ref for that extent as well. This searches the delayed ref tree for
4654 * a given extent, and if there are no other delayed refs to be processed, it
4655 * removes it from the tree.
4656 */
4657static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4658 struct btrfs_root *root, u64 bytenr)
4659{
4660 struct btrfs_delayed_ref_head *head;
4661 struct btrfs_delayed_ref_root *delayed_refs;
4662 struct btrfs_delayed_ref_node *ref;
4663 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004664 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004665
4666 delayed_refs = &trans->transaction->delayed_refs;
4667 spin_lock(&delayed_refs->lock);
4668 head = btrfs_find_delayed_ref_head(trans, bytenr);
4669 if (!head)
4670 goto out;
4671
4672 node = rb_prev(&head->node.rb_node);
4673 if (!node)
4674 goto out;
4675
4676 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4677
4678 /* there are still entries for this ref, we can't drop it */
4679 if (ref->bytenr == bytenr)
4680 goto out;
4681
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004682 if (head->extent_op) {
4683 if (!head->must_insert_reserved)
4684 goto out;
4685 kfree(head->extent_op);
4686 head->extent_op = NULL;
4687 }
4688
Chris Mason1887be62009-03-13 10:11:24 -04004689 /*
4690 * waiting for the lock here would deadlock. If someone else has it
4691 * locked they are already in the process of dropping it anyway
4692 */
4693 if (!mutex_trylock(&head->mutex))
4694 goto out;
4695
4696 /*
4697 * at this point we have a head with no other entries. Go
4698 * ahead and process it.
4699 */
4700 head->node.in_tree = 0;
4701 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004702
Chris Mason1887be62009-03-13 10:11:24 -04004703 delayed_refs->num_entries--;
4704
4705 /*
4706 * we don't take a ref on the node because we're removing it from the
4707 * tree, so we just steal the ref the tree was holding.
4708 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004709 delayed_refs->num_heads--;
4710 if (list_empty(&head->cluster))
4711 delayed_refs->num_heads_ready--;
4712
4713 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004714 spin_unlock(&delayed_refs->lock);
4715
Yan, Zhengf0486c62010-05-16 10:46:25 -04004716 BUG_ON(head->extent_op);
4717 if (head->must_insert_reserved)
4718 ret = 1;
4719
4720 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004721 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004722 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004723out:
4724 spin_unlock(&delayed_refs->lock);
4725 return 0;
4726}
4727
Yan, Zhengf0486c62010-05-16 10:46:25 -04004728void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4729 struct btrfs_root *root,
4730 struct extent_buffer *buf,
4731 u64 parent, int last_ref)
4732{
4733 struct btrfs_block_rsv *block_rsv;
4734 struct btrfs_block_group_cache *cache = NULL;
4735 int ret;
4736
4737 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4738 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4739 parent, root->root_key.objectid,
4740 btrfs_header_level(buf),
4741 BTRFS_DROP_DELAYED_REF, NULL);
4742 BUG_ON(ret);
4743 }
4744
4745 if (!last_ref)
4746 return;
4747
4748 block_rsv = get_block_rsv(trans, root);
4749 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004750 if (block_rsv->space_info != cache->space_info)
4751 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004752
4753 if (btrfs_header_generation(buf) == trans->transid) {
4754 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4755 ret = check_ref_cleanup(trans, root, buf->start);
4756 if (!ret)
4757 goto pin;
4758 }
4759
4760 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4761 pin_down_extent(root, cache, buf->start, buf->len, 1);
4762 goto pin;
4763 }
4764
4765 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4766
4767 btrfs_add_free_space(cache, buf->start, buf->len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00004768 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004769 if (ret == -EAGAIN) {
4770 /* block group became read-only */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004771 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004772 goto out;
4773 }
4774
4775 ret = 1;
4776 spin_lock(&block_rsv->lock);
4777 if (block_rsv->reserved < block_rsv->size) {
4778 block_rsv->reserved += buf->len;
4779 ret = 0;
4780 }
4781 spin_unlock(&block_rsv->lock);
4782
4783 if (ret) {
4784 spin_lock(&cache->space_info->lock);
4785 cache->space_info->bytes_reserved -= buf->len;
Chris Mason36e39c42011-03-12 07:08:42 -05004786 cache->space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004787 spin_unlock(&cache->space_info->lock);
4788 }
4789 goto out;
4790 }
4791pin:
4792 if (block_rsv->durable && !cache->ro) {
4793 ret = 0;
4794 spin_lock(&cache->lock);
4795 if (!cache->ro) {
4796 cache->reserved_pinned += buf->len;
4797 ret = 1;
4798 }
4799 spin_unlock(&cache->lock);
4800
4801 if (ret) {
4802 spin_lock(&block_rsv->lock);
4803 block_rsv->freed[trans->transid & 0x1] += buf->len;
4804 spin_unlock(&block_rsv->lock);
4805 }
4806 }
4807out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004808 /*
4809 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4810 * anymore.
4811 */
4812 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004813 btrfs_put_block_group(cache);
4814}
4815
Chris Mason925baed2008-06-25 16:01:30 -04004816int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004817 struct btrfs_root *root,
4818 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004819 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004820{
4821 int ret;
4822
Chris Mason56bec292009-03-13 10:10:06 -04004823 /*
4824 * tree log blocks never actually go into the extent allocation
4825 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004826 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004827 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4828 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004829 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004830 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004831 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004832 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4833 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4834 parent, root_objectid, (int)owner,
4835 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004836 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004837 } else {
4838 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4839 parent, root_objectid, owner,
4840 offset, BTRFS_DROP_DELAYED_REF, NULL);
4841 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004842 }
Chris Mason925baed2008-06-25 16:01:30 -04004843 return ret;
4844}
4845
Chris Mason87ee04e2007-11-30 11:30:34 -05004846static u64 stripe_align(struct btrfs_root *root, u64 val)
4847{
4848 u64 mask = ((u64)root->stripesize - 1);
4849 u64 ret = (val + mask) & ~mask;
4850 return ret;
4851}
4852
Chris Masonfec577f2007-02-26 10:40:21 -05004853/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004854 * when we wait for progress in the block group caching, its because
4855 * our allocation attempt failed at least once. So, we must sleep
4856 * and let some progress happen before we try again.
4857 *
4858 * This function will sleep at least once waiting for new free space to
4859 * show up, and then it will check the block group free space numbers
4860 * for our min num_bytes. Another option is to have it go ahead
4861 * and look in the rbtree for a free extent of a given size, but this
4862 * is a good start.
4863 */
4864static noinline int
4865wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4866 u64 num_bytes)
4867{
Yan Zheng11833d62009-09-11 16:11:19 -04004868 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004869 DEFINE_WAIT(wait);
4870
Yan Zheng11833d62009-09-11 16:11:19 -04004871 caching_ctl = get_caching_control(cache);
4872 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004873 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004874
Yan Zheng11833d62009-09-11 16:11:19 -04004875 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004876 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004877
4878 put_caching_control(caching_ctl);
4879 return 0;
4880}
4881
4882static noinline int
4883wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4884{
4885 struct btrfs_caching_control *caching_ctl;
4886 DEFINE_WAIT(wait);
4887
4888 caching_ctl = get_caching_control(cache);
4889 if (!caching_ctl)
4890 return 0;
4891
4892 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4893
4894 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004895 return 0;
4896}
4897
Yan, Zhengb742bb82010-05-16 10:46:24 -04004898static int get_block_group_index(struct btrfs_block_group_cache *cache)
4899{
4900 int index;
4901 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4902 index = 0;
4903 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4904 index = 1;
4905 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4906 index = 2;
4907 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4908 index = 3;
4909 else
4910 index = 4;
4911 return index;
4912}
4913
Josef Bacik817d52f2009-07-13 21:29:25 -04004914enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004915 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004916 LOOP_CACHING_NOWAIT = 1,
4917 LOOP_CACHING_WAIT = 2,
4918 LOOP_ALLOC_CHUNK = 3,
4919 LOOP_NO_EMPTY_SIZE = 4,
4920};
4921
4922/*
Chris Masonfec577f2007-02-26 10:40:21 -05004923 * walks the btree of allocated extents and find a hole of a given size.
4924 * The key ins is changed to record the hole:
4925 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004926 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004927 * ins->offset == number of blocks
4928 * Any available blocks before search_start are skipped.
4929 */
Chris Masond3977122009-01-05 21:25:51 -05004930static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004931 struct btrfs_root *orig_root,
4932 u64 num_bytes, u64 empty_size,
4933 u64 search_start, u64 search_end,
4934 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004935 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004936{
Josef Bacik80eb2342008-10-29 14:49:05 -04004937 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004938 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004939 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004940 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004941 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004942 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004943 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004944 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004945 int last_ptr_loop = 0;
4946 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004947 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004948 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004949 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004950 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004951 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004952 u64 ideal_cache_percent = 0;
4953 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004954
Chris Masondb945352007-10-15 16:15:53 -04004955 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004956 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004957 ins->objectid = 0;
4958 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004959
Josef Bacik2552d172009-04-03 10:14:19 -04004960 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004961 if (!space_info) {
4962 printk(KERN_ERR "No space info for %d\n", data);
4963 return -ENOSPC;
4964 }
Josef Bacik2552d172009-04-03 10:14:19 -04004965
Josef Bacik67377732010-09-16 16:19:09 -04004966 /*
4967 * If the space info is for both data and metadata it means we have a
4968 * small filesystem and we can't use the clustering stuff.
4969 */
4970 if (btrfs_mixed_space_info(space_info))
4971 use_cluster = false;
4972
Chris Mason0ef3e662008-05-24 14:04:53 -04004973 if (orig_root->ref_cows || empty_size)
4974 allowed_chunk_alloc = 1;
4975
Josef Bacik67377732010-09-16 16:19:09 -04004976 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004977 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004978 if (!btrfs_test_opt(root, SSD))
4979 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004980 }
4981
Josef Bacik67377732010-09-16 16:19:09 -04004982 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4983 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004984 last_ptr = &root->fs_info->data_alloc_cluster;
4985 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004986
Chris Mason239b14b2008-03-24 15:02:07 -04004987 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004988 spin_lock(&last_ptr->lock);
4989 if (last_ptr->block_group)
4990 hint_byte = last_ptr->window_start;
4991 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004992 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004993
Chris Masona061fc82008-05-07 11:43:44 -04004994 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004995 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004996
Josef Bacik817d52f2009-07-13 21:29:25 -04004997 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004998 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004999
Josef Bacik2552d172009-04-03 10:14:19 -04005000 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005001ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04005002 block_group = btrfs_lookup_block_group(root->fs_info,
5003 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04005004 /*
5005 * we don't want to use the block group if it doesn't match our
5006 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005007 *
5008 * However if we are re-searching with an ideal block group
5009 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005010 */
5011 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05005012 (block_group->cached != BTRFS_CACHE_NO ||
5013 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04005014 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005015 if (list_empty(&block_group->list) ||
5016 block_group->ro) {
5017 /*
5018 * someone is removing this block group,
5019 * we can't jump into the have_block_group
5020 * target because our list pointers are not
5021 * valid
5022 */
5023 btrfs_put_block_group(block_group);
5024 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005025 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005026 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005027 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005028 }
Josef Bacik2552d172009-04-03 10:14:19 -04005029 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005030 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005031 }
Chris Mason42e70e72008-11-07 18:17:11 -05005032 }
Josef Bacik2552d172009-04-03 10:14:19 -04005033search:
Josef Bacik80eb2342008-10-29 14:49:05 -04005034 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005035 list_for_each_entry(block_group, &space_info->block_groups[index],
5036 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005037 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005038 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005039
Josef Bacik11dfe352009-11-13 20:12:59 +00005040 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005041 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005042
Chris Mason83a50de2010-12-13 15:06:46 -05005043 /*
5044 * this can happen if we end up cycling through all the
5045 * raid types, but we want to make sure we only allocate
5046 * for the proper type.
5047 */
5048 if (!block_group_bits(block_group, data)) {
5049 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5050 BTRFS_BLOCK_GROUP_RAID1 |
5051 BTRFS_BLOCK_GROUP_RAID10;
5052
5053 /*
5054 * if they asked for extra copies and this block group
5055 * doesn't provide them, bail. This does allow us to
5056 * fill raid0 from raid1.
5057 */
5058 if ((data & extra) && !(block_group->flags & extra))
5059 goto loop;
5060 }
5061
Josef Bacik2552d172009-04-03 10:14:19 -04005062have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04005063 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005064 u64 free_percent;
5065
Josef Bacikb8399de2010-12-08 09:15:11 -05005066 ret = cache_block_group(block_group, trans,
5067 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04005068 if (block_group->cached == BTRFS_CACHE_FINISHED)
5069 goto have_block_group;
5070
Josef Bacikccf0e722009-11-10 21:23:48 -05005071 free_percent = btrfs_block_group_used(&block_group->item);
5072 free_percent *= 100;
5073 free_percent = div64_u64(free_percent,
5074 block_group->key.offset);
5075 free_percent = 100 - free_percent;
5076 if (free_percent > ideal_cache_percent &&
5077 likely(!block_group->ro)) {
5078 ideal_cache_offset = block_group->key.objectid;
5079 ideal_cache_percent = free_percent;
5080 }
5081
Josef Bacik817d52f2009-07-13 21:29:25 -04005082 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005083 * We only want to start kthread caching if we are at
5084 * the point where we will wait for caching to make
5085 * progress, or if our ideal search is over and we've
5086 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04005087 */
5088 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05005089 (loop > LOOP_FIND_IDEAL &&
5090 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacikb8399de2010-12-08 09:15:11 -05005091 ret = cache_block_group(block_group, trans,
5092 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005093 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005094 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005095 found_uncached_bg = true;
5096
5097 /*
5098 * If loop is set for cached only, try the next block
5099 * group.
5100 */
5101 if (loop == LOOP_FIND_IDEAL)
5102 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005103 }
5104
Josef Bacik817d52f2009-07-13 21:29:25 -04005105 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005106 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005107 found_uncached_bg = true;
5108
Josef Bacikea6a4782008-11-20 12:16:16 -05005109 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005110 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005111
Josef Bacik0a243252009-09-11 16:11:20 -04005112 /*
5113 * Ok we want to try and use the cluster allocator, so lets look
5114 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5115 * have tried the cluster allocator plenty of times at this
5116 * point and not have found anything, so we are likely way too
5117 * fragmented for the clustering stuff to find anything, so lets
5118 * just skip it and let the allocator find whatever block it can
5119 * find
5120 */
5121 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005122 /*
5123 * the refill lock keeps out other
5124 * people trying to start a new cluster
5125 */
5126 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005127 if (last_ptr->block_group &&
5128 (last_ptr->block_group->ro ||
5129 !block_group_bits(last_ptr->block_group, data))) {
5130 offset = 0;
5131 goto refill_cluster;
5132 }
5133
Chris Masonfa9c0d72009-04-03 09:47:43 -04005134 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5135 num_bytes, search_start);
5136 if (offset) {
5137 /* we have a block, we're done */
5138 spin_unlock(&last_ptr->refill_lock);
5139 goto checks;
5140 }
5141
5142 spin_lock(&last_ptr->lock);
5143 /*
5144 * whoops, this cluster doesn't actually point to
5145 * this block group. Get a ref on the block
5146 * group is does point to and try again
5147 */
5148 if (!last_ptr_loop && last_ptr->block_group &&
5149 last_ptr->block_group != block_group) {
5150
5151 btrfs_put_block_group(block_group);
5152 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005153 btrfs_get_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005154 spin_unlock(&last_ptr->lock);
5155 spin_unlock(&last_ptr->refill_lock);
5156
5157 last_ptr_loop = 1;
5158 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005159 /*
5160 * we know this block group is properly
5161 * in the list because
5162 * btrfs_remove_block_group, drops the
5163 * cluster before it removes the block
5164 * group from the list
5165 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04005166 goto have_block_group;
5167 }
5168 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005169refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04005170 /*
5171 * this cluster didn't work out, free it and
5172 * start over
5173 */
5174 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5175
5176 last_ptr_loop = 0;
5177
5178 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005179 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005180 block_group, last_ptr,
5181 offset, num_bytes,
5182 empty_cluster + empty_size);
5183 if (ret == 0) {
5184 /*
5185 * now pull our allocation out of this
5186 * cluster
5187 */
5188 offset = btrfs_alloc_from_cluster(block_group,
5189 last_ptr, num_bytes,
5190 search_start);
5191 if (offset) {
5192 /* we found one, proceed */
5193 spin_unlock(&last_ptr->refill_lock);
5194 goto checks;
5195 }
Josef Bacik0a243252009-09-11 16:11:20 -04005196 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5197 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005198 spin_unlock(&last_ptr->refill_lock);
5199
Josef Bacik0a243252009-09-11 16:11:20 -04005200 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005201 wait_block_group_cache_progress(block_group,
5202 num_bytes + empty_cluster + empty_size);
5203 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005204 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005205
Chris Masonfa9c0d72009-04-03 09:47:43 -04005206 /*
5207 * at this point we either didn't find a cluster
5208 * or we weren't able to allocate a block from our
5209 * cluster. Free the cluster we've been trying
5210 * to use, and go to the next block group
5211 */
Josef Bacik0a243252009-09-11 16:11:20 -04005212 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005213 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005214 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005215 }
5216
Josef Bacik6226cb02009-04-03 10:14:18 -04005217 offset = btrfs_find_space_for_alloc(block_group, search_start,
5218 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005219 /*
5220 * If we didn't find a chunk, and we haven't failed on this
5221 * block group before, and this block group is in the middle of
5222 * caching and we are ok with waiting, then go ahead and wait
5223 * for progress to be made, and set failed_alloc to true.
5224 *
5225 * If failed_alloc is true then we've already waited on this
5226 * block group once and should move on to the next block group.
5227 */
5228 if (!offset && !failed_alloc && !cached &&
5229 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005230 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005231 num_bytes + empty_size);
5232 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005233 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005234 } else if (!offset) {
5235 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005236 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005237checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005238 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005239 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005240 if (search_start + num_bytes >= search_end) {
5241 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005242 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005243 }
Chris Masone37c9e62007-05-09 20:13:14 -04005244
Josef Bacik2552d172009-04-03 10:14:19 -04005245 /* move on to the next group */
5246 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005247 block_group->key.objectid + block_group->key.offset) {
5248 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005249 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005250 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005251
Josef Bacik2552d172009-04-03 10:14:19 -04005252 ins->objectid = search_start;
5253 ins->offset = num_bytes;
5254
Josef Bacik6226cb02009-04-03 10:14:18 -04005255 if (offset < search_start)
5256 btrfs_add_free_space(block_group, offset,
5257 search_start - offset);
5258 BUG_ON(offset > search_start);
5259
Li Dongyangb4d00d52011-03-24 10:24:25 +00005260 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005261 (data & BTRFS_BLOCK_GROUP_DATA));
5262 if (ret == -EAGAIN) {
5263 btrfs_add_free_space(block_group, offset, num_bytes);
5264 goto loop;
5265 }
Yan Zheng11833d62009-09-11 16:11:19 -04005266
Josef Bacik2552d172009-04-03 10:14:19 -04005267 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005268 ins->objectid = search_start;
5269 ins->offset = num_bytes;
5270
5271 if (offset < search_start)
5272 btrfs_add_free_space(block_group, offset,
5273 search_start - offset);
5274 BUG_ON(offset > search_start);
Josef Bacik2552d172009-04-03 10:14:19 -04005275 break;
5276loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005277 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005278 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005279 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d72009-04-03 09:47:43 -04005280 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005281 }
5282 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005283
Yan, Zhengb742bb82010-05-16 10:46:24 -04005284 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5285 goto search;
5286
Josef Bacikccf0e722009-11-10 21:23:48 -05005287 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5288 * for them to make caching progress. Also
5289 * determine the best possible bg to cache
5290 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5291 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005292 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5293 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5294 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5295 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04005296 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005297 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5298 (found_uncached_bg || empty_size || empty_cluster ||
5299 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005300 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005301 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005302 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005303 loop++;
5304 if (!ideal_cache_percent &&
5305 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005306 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005307
5308 /*
5309 * 1 of the following 2 things have happened so far
5310 *
5311 * 1) We found an ideal block group for caching that
5312 * is mostly full and will cache quickly, so we might
5313 * as well wait for it.
5314 *
5315 * 2) We searched for cached only and we didn't find
5316 * anything, and we didn't start any caching kthreads
5317 * either, so chances are we will loop through and
5318 * start a couple caching kthreads, and then come back
5319 * around and just wait for them. This will be slower
5320 * because we will have 2 caching kthreads reading at
5321 * the same time when we could have just started one
5322 * and waited for it to get far enough to give us an
5323 * allocation, so go ahead and go to the wait caching
5324 * loop.
5325 */
5326 loop = LOOP_CACHING_WAIT;
5327 search_start = ideal_cache_offset;
5328 ideal_cache_percent = 0;
5329 goto ideal_cache;
5330 } else if (loop == LOOP_FIND_IDEAL) {
5331 /*
5332 * Didn't find a uncached bg, wait on anything we find
5333 * next.
5334 */
5335 loop = LOOP_CACHING_WAIT;
5336 goto search;
5337 }
5338
5339 if (loop < LOOP_CACHING_WAIT) {
5340 loop++;
5341 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005342 }
5343
5344 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005345 empty_size = 0;
5346 empty_cluster = 0;
5347 }
Chris Mason42e70e72008-11-07 18:17:11 -05005348
Josef Bacik2552d172009-04-03 10:14:19 -04005349 if (allowed_chunk_alloc) {
5350 ret = do_chunk_alloc(trans, root, num_bytes +
Chris Mason0e4f8f82011-04-15 16:05:44 -04005351 2 * 1024 * 1024, data,
5352 CHUNK_ALLOC_LIMITED);
Josef Bacik2552d172009-04-03 10:14:19 -04005353 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005354 done_chunk_alloc = 1;
Chris Mason0e4f8f82011-04-15 16:05:44 -04005355 } else if (!done_chunk_alloc &&
5356 space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) {
5357 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005358 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005359
Josef Bacik817d52f2009-07-13 21:29:25 -04005360 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005361 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005362 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005363 }
Josef Bacik2552d172009-04-03 10:14:19 -04005364 ret = -ENOSPC;
5365 } else if (!ins->objectid) {
5366 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04005367 }
Chris Mason0b86a832008-03-24 15:01:56 -04005368
Josef Bacik80eb2342008-10-29 14:49:05 -04005369 /* we found what we needed */
5370 if (ins->objectid) {
5371 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05005372 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04005373
Chris Masonfa9c0d72009-04-03 09:47:43 -04005374 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005375 ret = 0;
5376 }
Chris Mason0b86a832008-03-24 15:01:56 -04005377
Chris Mason0f70abe2007-02-28 16:46:22 -05005378 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005379}
Chris Masonec44a352008-04-28 15:29:52 -04005380
Josef Bacik9ed74f22009-09-11 16:12:44 -04005381static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5382 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005383{
5384 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005385 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005386
Josef Bacik9ed74f22009-09-11 16:12:44 -04005387 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005388 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5389 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005390 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005391 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005392 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005393 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5394 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005395 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005396 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005397 (unsigned long long)info->bytes_pinned,
5398 (unsigned long long)info->bytes_reserved,
5399 (unsigned long long)info->bytes_may_use,
5400 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005401 spin_unlock(&info->lock);
5402
5403 if (!dump_block_groups)
5404 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005405
Josef Bacik80eb2342008-10-29 14:49:05 -04005406 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005407again:
5408 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005409 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005410 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5411 "%llu pinned %llu reserved\n",
5412 (unsigned long long)cache->key.objectid,
5413 (unsigned long long)cache->key.offset,
5414 (unsigned long long)btrfs_block_group_used(&cache->item),
5415 (unsigned long long)cache->pinned,
5416 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005417 btrfs_dump_free_space(cache, bytes);
5418 spin_unlock(&cache->lock);
5419 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005420 if (++index < BTRFS_NR_RAID_TYPES)
5421 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005422 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005423}
Zheng Yane8569812008-09-26 10:05:48 -04005424
Yan Zheng11833d62009-09-11 16:11:19 -04005425int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5426 struct btrfs_root *root,
5427 u64 num_bytes, u64 min_alloc_size,
5428 u64 empty_size, u64 hint_byte,
5429 u64 search_end, struct btrfs_key *ins,
5430 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005431{
5432 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005433 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005434
Josef Bacik6a632092009-02-20 11:00:09 -05005435 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005436again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005437 /*
5438 * the only place that sets empty_size is btrfs_realloc_node, which
5439 * is not called recursively on allocations
5440 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005441 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005442 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005443 num_bytes + 2 * 1024 * 1024, data,
5444 CHUNK_ALLOC_NO_FORCE);
Chris Mason0b86a832008-03-24 15:01:56 -04005445
Chris Masondb945352007-10-15 16:15:53 -04005446 WARN_ON(num_bytes < root->sectorsize);
5447 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005448 search_start, search_end, hint_byte,
5449 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005450
Chris Mason98d20f62008-04-14 09:46:10 -04005451 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5452 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005453 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005454 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005455 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005456 num_bytes, data, CHUNK_ALLOC_FORCE);
Chris Mason98d20f62008-04-14 09:46:10 -04005457 goto again;
5458 }
Chris Mason91435652011-02-16 13:10:41 -05005459 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005460 struct btrfs_space_info *sinfo;
5461
5462 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005463 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5464 "wanted %llu\n", (unsigned long long)data,
5465 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005466 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005467 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005468
liubo1abe9b82011-03-24 11:18:59 +00005469 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5470
Josef Bacik0f9dd462008-09-23 13:14:11 -04005471 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005472}
5473
Chris Mason65b51a02008-08-01 15:11:20 -04005474int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5475{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005476 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005477 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005478
Josef Bacik0f9dd462008-09-23 13:14:11 -04005479 cache = btrfs_lookup_block_group(root->fs_info, start);
5480 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005481 printk(KERN_ERR "Unable to find block group for %llu\n",
5482 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005483 return -ENOSPC;
5484 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005485
Li Dongyang5378e602011-03-24 10:24:27 +00005486 if (btrfs_test_opt(root, DISCARD))
5487 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005488
Josef Bacik0f9dd462008-09-23 13:14:11 -04005489 btrfs_add_free_space(cache, start, len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00005490 btrfs_update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005491 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005492
liubo1abe9b82011-03-24 11:18:59 +00005493 trace_btrfs_reserved_extent_free(root, start, len);
5494
Chris Masone6dcd2d2008-07-17 12:53:50 -04005495 return ret;
5496}
5497
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005498static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5499 struct btrfs_root *root,
5500 u64 parent, u64 root_objectid,
5501 u64 flags, u64 owner, u64 offset,
5502 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005503{
5504 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005505 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005506 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005507 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005508 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005509 struct extent_buffer *leaf;
5510 int type;
5511 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005512
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005513 if (parent > 0)
5514 type = BTRFS_SHARED_DATA_REF_KEY;
5515 else
5516 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005517
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005518 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005519
5520 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005521 if (!path)
5522 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005523
Chris Masonb9473432009-03-13 11:00:37 -04005524 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005525 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5526 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005527 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005528
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005529 leaf = path->nodes[0];
5530 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005531 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005532 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5533 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5534 btrfs_set_extent_flags(leaf, extent_item,
5535 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005536
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005537 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5538 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5539 if (parent > 0) {
5540 struct btrfs_shared_data_ref *ref;
5541 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5542 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5543 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5544 } else {
5545 struct btrfs_extent_data_ref *ref;
5546 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5547 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5548 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5549 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5550 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5551 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005552
5553 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005554 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005555
Yan, Zhengf0486c62010-05-16 10:46:25 -04005556 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005557 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005558 printk(KERN_ERR "btrfs update block group failed for %llu "
5559 "%llu\n", (unsigned long long)ins->objectid,
5560 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005561 BUG();
5562 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005563 return ret;
5564}
5565
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005566static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5567 struct btrfs_root *root,
5568 u64 parent, u64 root_objectid,
5569 u64 flags, struct btrfs_disk_key *key,
5570 int level, struct btrfs_key *ins)
5571{
5572 int ret;
5573 struct btrfs_fs_info *fs_info = root->fs_info;
5574 struct btrfs_extent_item *extent_item;
5575 struct btrfs_tree_block_info *block_info;
5576 struct btrfs_extent_inline_ref *iref;
5577 struct btrfs_path *path;
5578 struct extent_buffer *leaf;
5579 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5580
5581 path = btrfs_alloc_path();
5582 BUG_ON(!path);
5583
5584 path->leave_spinning = 1;
5585 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5586 ins, size);
5587 BUG_ON(ret);
5588
5589 leaf = path->nodes[0];
5590 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5591 struct btrfs_extent_item);
5592 btrfs_set_extent_refs(leaf, extent_item, 1);
5593 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5594 btrfs_set_extent_flags(leaf, extent_item,
5595 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5596 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5597
5598 btrfs_set_tree_block_key(leaf, block_info, key);
5599 btrfs_set_tree_block_level(leaf, block_info, level);
5600
5601 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5602 if (parent > 0) {
5603 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5604 btrfs_set_extent_inline_ref_type(leaf, iref,
5605 BTRFS_SHARED_BLOCK_REF_KEY);
5606 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5607 } else {
5608 btrfs_set_extent_inline_ref_type(leaf, iref,
5609 BTRFS_TREE_BLOCK_REF_KEY);
5610 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5611 }
5612
5613 btrfs_mark_buffer_dirty(leaf);
5614 btrfs_free_path(path);
5615
Yan, Zhengf0486c62010-05-16 10:46:25 -04005616 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617 if (ret) {
5618 printk(KERN_ERR "btrfs update block group failed for %llu "
5619 "%llu\n", (unsigned long long)ins->objectid,
5620 (unsigned long long)ins->offset);
5621 BUG();
5622 }
5623 return ret;
5624}
5625
5626int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5627 struct btrfs_root *root,
5628 u64 root_objectid, u64 owner,
5629 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005630{
5631 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005632
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005633 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005634
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005635 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5636 0, root_objectid, owner, offset,
5637 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005638 return ret;
5639}
Chris Masone02119d2008-09-05 16:13:11 -04005640
5641/*
5642 * this is used by the tree logging recovery code. It records that
5643 * an extent has been allocated and makes sure to clear the free
5644 * space cache bits as well
5645 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005646int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5647 struct btrfs_root *root,
5648 u64 root_objectid, u64 owner, u64 offset,
5649 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005650{
5651 int ret;
5652 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005653 struct btrfs_caching_control *caching_ctl;
5654 u64 start = ins->objectid;
5655 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005656
Chris Masone02119d2008-09-05 16:13:11 -04005657 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005658 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005659 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005660
Yan Zheng11833d62009-09-11 16:11:19 -04005661 if (!caching_ctl) {
5662 BUG_ON(!block_group_cache_done(block_group));
5663 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5664 BUG_ON(ret);
5665 } else {
5666 mutex_lock(&caching_ctl->mutex);
5667
5668 if (start >= caching_ctl->progress) {
5669 ret = add_excluded_extent(root, start, num_bytes);
5670 BUG_ON(ret);
5671 } else if (start + num_bytes <= caching_ctl->progress) {
5672 ret = btrfs_remove_free_space(block_group,
5673 start, num_bytes);
5674 BUG_ON(ret);
5675 } else {
5676 num_bytes = caching_ctl->progress - start;
5677 ret = btrfs_remove_free_space(block_group,
5678 start, num_bytes);
5679 BUG_ON(ret);
5680
5681 start = caching_ctl->progress;
5682 num_bytes = ins->objectid + ins->offset -
5683 caching_ctl->progress;
5684 ret = add_excluded_extent(root, start, num_bytes);
5685 BUG_ON(ret);
5686 }
5687
5688 mutex_unlock(&caching_ctl->mutex);
5689 put_caching_control(caching_ctl);
5690 }
5691
Li Dongyangb4d00d52011-03-24 10:24:25 +00005692 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005693 BUG_ON(ret);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005694 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005695 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5696 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005697 return ret;
5698}
5699
Chris Mason65b51a02008-08-01 15:11:20 -04005700struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5701 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005702 u64 bytenr, u32 blocksize,
5703 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005704{
5705 struct extent_buffer *buf;
5706
5707 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5708 if (!buf)
5709 return ERR_PTR(-ENOMEM);
5710 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005711 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005712 btrfs_tree_lock(buf);
5713 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005714
5715 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005716 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005717
Chris Masond0c803c2008-09-11 16:17:57 -04005718 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005719 /*
5720 * we allow two log transactions at a time, use different
5721 * EXENT bit to differentiate dirty pages.
5722 */
5723 if (root->log_transid % 2 == 0)
5724 set_extent_dirty(&root->dirty_log_pages, buf->start,
5725 buf->start + buf->len - 1, GFP_NOFS);
5726 else
5727 set_extent_new(&root->dirty_log_pages, buf->start,
5728 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005729 } else {
5730 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5731 buf->start + buf->len - 1, GFP_NOFS);
5732 }
Chris Mason65b51a02008-08-01 15:11:20 -04005733 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005734 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005735 return buf;
5736}
5737
Yan, Zhengf0486c62010-05-16 10:46:25 -04005738static struct btrfs_block_rsv *
5739use_block_rsv(struct btrfs_trans_handle *trans,
5740 struct btrfs_root *root, u32 blocksize)
5741{
5742 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005743 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005744 int ret;
5745
5746 block_rsv = get_block_rsv(trans, root);
5747
5748 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005749 ret = reserve_metadata_bytes(trans, root, block_rsv,
5750 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005751 /*
5752 * If we couldn't reserve metadata bytes try and use some from
5753 * the global reserve.
5754 */
5755 if (ret && block_rsv != global_rsv) {
5756 ret = block_rsv_use_bytes(global_rsv, blocksize);
5757 if (!ret)
5758 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005759 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005760 } else if (ret) {
5761 return ERR_PTR(ret);
5762 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005763 return block_rsv;
5764 }
5765
5766 ret = block_rsv_use_bytes(block_rsv, blocksize);
5767 if (!ret)
5768 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005769 if (ret) {
5770 WARN_ON(1);
5771 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5772 0);
5773 if (!ret) {
5774 spin_lock(&block_rsv->lock);
5775 block_rsv->size += blocksize;
5776 spin_unlock(&block_rsv->lock);
5777 return block_rsv;
5778 } else if (ret && block_rsv != global_rsv) {
5779 ret = block_rsv_use_bytes(global_rsv, blocksize);
5780 if (!ret)
5781 return global_rsv;
5782 }
5783 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005784
Yan, Zhengf0486c62010-05-16 10:46:25 -04005785 return ERR_PTR(-ENOSPC);
5786}
5787
5788static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5789{
5790 block_rsv_add_bytes(block_rsv, blocksize, 0);
5791 block_rsv_release_bytes(block_rsv, NULL, 0);
5792}
5793
Chris Masonfec577f2007-02-26 10:40:21 -05005794/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005795 * finds a free extent and does all the dirty work required for allocation
5796 * returns the key for the extent through ins, and a tree buffer for
5797 * the first block of the extent through buf.
5798 *
Chris Masonfec577f2007-02-26 10:40:21 -05005799 * returns the tree buffer or NULL.
5800 */
Chris Mason5f39d392007-10-15 16:14:19 -04005801struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005802 struct btrfs_root *root, u32 blocksize,
5803 u64 parent, u64 root_objectid,
5804 struct btrfs_disk_key *key, int level,
5805 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005806{
Chris Masone2fa7222007-03-12 16:22:34 -04005807 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005808 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005809 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005810 u64 flags = 0;
5811 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005812
Yan, Zhengf0486c62010-05-16 10:46:25 -04005813
5814 block_rsv = use_block_rsv(trans, root, blocksize);
5815 if (IS_ERR(block_rsv))
5816 return ERR_CAST(block_rsv);
5817
5818 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5819 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005820 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005821 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005822 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005823 }
Chris Mason55c69072008-01-09 15:55:33 -05005824
Chris Mason4008c042009-02-12 14:09:45 -05005825 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5826 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005827 BUG_ON(IS_ERR(buf));
5828
5829 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5830 if (parent == 0)
5831 parent = ins.objectid;
5832 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5833 } else
5834 BUG_ON(parent > 0);
5835
5836 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5837 struct btrfs_delayed_extent_op *extent_op;
5838 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5839 BUG_ON(!extent_op);
5840 if (key)
5841 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5842 else
5843 memset(&extent_op->key, 0, sizeof(extent_op->key));
5844 extent_op->flags_to_set = flags;
5845 extent_op->update_key = 1;
5846 extent_op->update_flags = 1;
5847 extent_op->is_data = 0;
5848
5849 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5850 ins.offset, parent, root_objectid,
5851 level, BTRFS_ADD_DELAYED_EXTENT,
5852 extent_op);
5853 BUG_ON(ret);
5854 }
Chris Masonfec577f2007-02-26 10:40:21 -05005855 return buf;
5856}
Chris Masona28ec192007-03-06 20:08:01 -05005857
Yan Zheng2c47e6052009-06-27 21:07:35 -04005858struct walk_control {
5859 u64 refs[BTRFS_MAX_LEVEL];
5860 u64 flags[BTRFS_MAX_LEVEL];
5861 struct btrfs_key update_progress;
5862 int stage;
5863 int level;
5864 int shared_level;
5865 int update_ref;
5866 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005867 int reada_slot;
5868 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005869};
5870
5871#define DROP_REFERENCE 1
5872#define UPDATE_BACKREF 2
5873
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005874static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5875 struct btrfs_root *root,
5876 struct walk_control *wc,
5877 struct btrfs_path *path)
5878{
5879 u64 bytenr;
5880 u64 generation;
5881 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005882 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005883 u32 nritems;
5884 u32 blocksize;
5885 struct btrfs_key key;
5886 struct extent_buffer *eb;
5887 int ret;
5888 int slot;
5889 int nread = 0;
5890
5891 if (path->slots[wc->level] < wc->reada_slot) {
5892 wc->reada_count = wc->reada_count * 2 / 3;
5893 wc->reada_count = max(wc->reada_count, 2);
5894 } else {
5895 wc->reada_count = wc->reada_count * 3 / 2;
5896 wc->reada_count = min_t(int, wc->reada_count,
5897 BTRFS_NODEPTRS_PER_BLOCK(root));
5898 }
5899
5900 eb = path->nodes[wc->level];
5901 nritems = btrfs_header_nritems(eb);
5902 blocksize = btrfs_level_size(root, wc->level - 1);
5903
5904 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5905 if (nread >= wc->reada_count)
5906 break;
5907
5908 cond_resched();
5909 bytenr = btrfs_node_blockptr(eb, slot);
5910 generation = btrfs_node_ptr_generation(eb, slot);
5911
5912 if (slot == path->slots[wc->level])
5913 goto reada;
5914
5915 if (wc->stage == UPDATE_BACKREF &&
5916 generation <= root->root_key.offset)
5917 continue;
5918
Yan, Zheng94fcca92009-10-09 09:25:16 -04005919 /* We don't lock the tree block, it's OK to be racy here */
5920 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5921 &refs, &flags);
5922 BUG_ON(ret);
5923 BUG_ON(refs == 0);
5924
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005925 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005926 if (refs == 1)
5927 goto reada;
5928
Yan, Zheng94fcca92009-10-09 09:25:16 -04005929 if (wc->level == 1 &&
5930 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5931 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005932 if (!wc->update_ref ||
5933 generation <= root->root_key.offset)
5934 continue;
5935 btrfs_node_key_to_cpu(eb, &key, slot);
5936 ret = btrfs_comp_cpu_keys(&key,
5937 &wc->update_progress);
5938 if (ret < 0)
5939 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005940 } else {
5941 if (wc->level == 1 &&
5942 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5943 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005944 }
5945reada:
5946 ret = readahead_tree_block(root, bytenr, blocksize,
5947 generation);
5948 if (ret)
5949 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005950 nread++;
5951 }
5952 wc->reada_slot = slot;
5953}
5954
Chris Mason9aca1d52007-03-13 11:09:37 -04005955/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005956 * hepler to process tree block while walking down the tree.
5957 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005958 * when wc->stage == UPDATE_BACKREF, this function updates
5959 * back refs for pointers in the block.
5960 *
5961 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005962 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005963static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5964 struct btrfs_root *root,
5965 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005966 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005967{
5968 int level = wc->level;
5969 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005970 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5971 int ret;
5972
5973 if (wc->stage == UPDATE_BACKREF &&
5974 btrfs_header_owner(eb) != root->root_key.objectid)
5975 return 1;
5976
5977 /*
5978 * when reference count of tree block is 1, it won't increase
5979 * again. once full backref flag is set, we never clear it.
5980 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005981 if (lookup_info &&
5982 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5983 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005984 BUG_ON(!path->locks[level]);
5985 ret = btrfs_lookup_extent_info(trans, root,
5986 eb->start, eb->len,
5987 &wc->refs[level],
5988 &wc->flags[level]);
5989 BUG_ON(ret);
5990 BUG_ON(wc->refs[level] == 0);
5991 }
5992
Yan Zheng2c47e6052009-06-27 21:07:35 -04005993 if (wc->stage == DROP_REFERENCE) {
5994 if (wc->refs[level] > 1)
5995 return 1;
5996
5997 if (path->locks[level] && !wc->keep_locks) {
5998 btrfs_tree_unlock(eb);
5999 path->locks[level] = 0;
6000 }
6001 return 0;
6002 }
6003
6004 /* wc->stage == UPDATE_BACKREF */
6005 if (!(wc->flags[level] & flag)) {
6006 BUG_ON(!path->locks[level]);
6007 ret = btrfs_inc_ref(trans, root, eb, 1);
6008 BUG_ON(ret);
6009 ret = btrfs_dec_ref(trans, root, eb, 0);
6010 BUG_ON(ret);
6011 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6012 eb->len, flag, 0);
6013 BUG_ON(ret);
6014 wc->flags[level] |= flag;
6015 }
6016
6017 /*
6018 * the block is shared by multiple trees, so it's not good to
6019 * keep the tree lock
6020 */
6021 if (path->locks[level] && level > 0) {
6022 btrfs_tree_unlock(eb);
6023 path->locks[level] = 0;
6024 }
6025 return 0;
6026}
6027
6028/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006029 * hepler to process tree block pointer.
6030 *
6031 * when wc->stage == DROP_REFERENCE, this function checks
6032 * reference count of the block pointed to. if the block
6033 * is shared and we need update back refs for the subtree
6034 * rooted at the block, this function changes wc->stage to
6035 * UPDATE_BACKREF. if the block is shared and there is no
6036 * need to update back, this function drops the reference
6037 * to the block.
6038 *
6039 * NOTE: return value 1 means we should stop walking down.
6040 */
6041static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6042 struct btrfs_root *root,
6043 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006044 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006045{
6046 u64 bytenr;
6047 u64 generation;
6048 u64 parent;
6049 u32 blocksize;
6050 struct btrfs_key key;
6051 struct extent_buffer *next;
6052 int level = wc->level;
6053 int reada = 0;
6054 int ret = 0;
6055
6056 generation = btrfs_node_ptr_generation(path->nodes[level],
6057 path->slots[level]);
6058 /*
6059 * if the lower level block was created before the snapshot
6060 * was created, we know there is no need to update back refs
6061 * for the subtree
6062 */
6063 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006064 generation <= root->root_key.offset) {
6065 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006066 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006067 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006068
6069 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6070 blocksize = btrfs_level_size(root, level - 1);
6071
6072 next = btrfs_find_tree_block(root, bytenr, blocksize);
6073 if (!next) {
6074 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00006075 if (!next)
6076 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006077 reada = 1;
6078 }
6079 btrfs_tree_lock(next);
6080 btrfs_set_lock_blocking(next);
6081
Yan, Zheng94fcca92009-10-09 09:25:16 -04006082 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6083 &wc->refs[level - 1],
6084 &wc->flags[level - 1]);
6085 BUG_ON(ret);
6086 BUG_ON(wc->refs[level - 1] == 0);
6087 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006088
Yan, Zheng94fcca92009-10-09 09:25:16 -04006089 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006090 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006091 if (level == 1 &&
6092 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6093 goto skip;
6094
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006095 if (!wc->update_ref ||
6096 generation <= root->root_key.offset)
6097 goto skip;
6098
6099 btrfs_node_key_to_cpu(path->nodes[level], &key,
6100 path->slots[level]);
6101 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6102 if (ret < 0)
6103 goto skip;
6104
6105 wc->stage = UPDATE_BACKREF;
6106 wc->shared_level = level - 1;
6107 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006108 } else {
6109 if (level == 1 &&
6110 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6111 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006112 }
6113
6114 if (!btrfs_buffer_uptodate(next, generation)) {
6115 btrfs_tree_unlock(next);
6116 free_extent_buffer(next);
6117 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006118 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006119 }
6120
6121 if (!next) {
6122 if (reada && level == 1)
6123 reada_walk_down(trans, root, wc, path);
6124 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006125 if (!next)
6126 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006127 btrfs_tree_lock(next);
6128 btrfs_set_lock_blocking(next);
6129 }
6130
6131 level--;
6132 BUG_ON(level != btrfs_header_level(next));
6133 path->nodes[level] = next;
6134 path->slots[level] = 0;
6135 path->locks[level] = 1;
6136 wc->level = level;
6137 if (wc->level == 1)
6138 wc->reada_slot = 0;
6139 return 0;
6140skip:
6141 wc->refs[level - 1] = 0;
6142 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006143 if (wc->stage == DROP_REFERENCE) {
6144 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6145 parent = path->nodes[level]->start;
6146 } else {
6147 BUG_ON(root->root_key.objectid !=
6148 btrfs_header_owner(path->nodes[level]));
6149 parent = 0;
6150 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006151
Yan, Zheng94fcca92009-10-09 09:25:16 -04006152 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6153 root->root_key.objectid, level - 1, 0);
6154 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006155 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006156 btrfs_tree_unlock(next);
6157 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006158 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006159 return 1;
6160}
6161
6162/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006163 * hepler to process tree block while walking up the tree.
6164 *
6165 * when wc->stage == DROP_REFERENCE, this function drops
6166 * reference count on the block.
6167 *
6168 * when wc->stage == UPDATE_BACKREF, this function changes
6169 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6170 * to UPDATE_BACKREF previously while processing the block.
6171 *
6172 * NOTE: return value 1 means we should stop walking up.
6173 */
6174static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6175 struct btrfs_root *root,
6176 struct btrfs_path *path,
6177 struct walk_control *wc)
6178{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006179 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006180 int level = wc->level;
6181 struct extent_buffer *eb = path->nodes[level];
6182 u64 parent = 0;
6183
6184 if (wc->stage == UPDATE_BACKREF) {
6185 BUG_ON(wc->shared_level < level);
6186 if (level < wc->shared_level)
6187 goto out;
6188
Yan Zheng2c47e6052009-06-27 21:07:35 -04006189 ret = find_next_key(path, level + 1, &wc->update_progress);
6190 if (ret > 0)
6191 wc->update_ref = 0;
6192
6193 wc->stage = DROP_REFERENCE;
6194 wc->shared_level = -1;
6195 path->slots[level] = 0;
6196
6197 /*
6198 * check reference count again if the block isn't locked.
6199 * we should start walking down the tree again if reference
6200 * count is one.
6201 */
6202 if (!path->locks[level]) {
6203 BUG_ON(level == 0);
6204 btrfs_tree_lock(eb);
6205 btrfs_set_lock_blocking(eb);
6206 path->locks[level] = 1;
6207
6208 ret = btrfs_lookup_extent_info(trans, root,
6209 eb->start, eb->len,
6210 &wc->refs[level],
6211 &wc->flags[level]);
6212 BUG_ON(ret);
6213 BUG_ON(wc->refs[level] == 0);
6214 if (wc->refs[level] == 1) {
6215 btrfs_tree_unlock(eb);
6216 path->locks[level] = 0;
6217 return 1;
6218 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006219 }
6220 }
6221
6222 /* wc->stage == DROP_REFERENCE */
6223 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6224
6225 if (wc->refs[level] == 1) {
6226 if (level == 0) {
6227 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6228 ret = btrfs_dec_ref(trans, root, eb, 1);
6229 else
6230 ret = btrfs_dec_ref(trans, root, eb, 0);
6231 BUG_ON(ret);
6232 }
6233 /* make block locked assertion in clean_tree_block happy */
6234 if (!path->locks[level] &&
6235 btrfs_header_generation(eb) == trans->transid) {
6236 btrfs_tree_lock(eb);
6237 btrfs_set_lock_blocking(eb);
6238 path->locks[level] = 1;
6239 }
6240 clean_tree_block(trans, root, eb);
6241 }
6242
6243 if (eb == root->node) {
6244 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6245 parent = eb->start;
6246 else
6247 BUG_ON(root->root_key.objectid !=
6248 btrfs_header_owner(eb));
6249 } else {
6250 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6251 parent = path->nodes[level + 1]->start;
6252 else
6253 BUG_ON(root->root_key.objectid !=
6254 btrfs_header_owner(path->nodes[level + 1]));
6255 }
6256
Yan, Zhengf0486c62010-05-16 10:46:25 -04006257 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006258out:
6259 wc->refs[level] = 0;
6260 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006261 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006262}
6263
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006264static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6265 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006266 struct btrfs_path *path,
6267 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006268{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006269 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006270 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006271 int ret;
6272
Yan Zheng2c47e6052009-06-27 21:07:35 -04006273 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006274 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006275 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006276 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006277
Yan Zheng2c47e6052009-06-27 21:07:35 -04006278 if (level == 0)
6279 break;
6280
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006281 if (path->slots[level] >=
6282 btrfs_header_nritems(path->nodes[level]))
6283 break;
6284
Yan, Zheng94fcca92009-10-09 09:25:16 -04006285 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006286 if (ret > 0) {
6287 path->slots[level]++;
6288 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00006289 } else if (ret < 0)
6290 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006291 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006292 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006293 return 0;
6294}
6295
Chris Masond3977122009-01-05 21:25:51 -05006296static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006297 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006298 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006299 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006300{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006301 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006302 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006303
Yan Zheng2c47e6052009-06-27 21:07:35 -04006304 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6305 while (level < max_level && path->nodes[level]) {
6306 wc->level = level;
6307 if (path->slots[level] + 1 <
6308 btrfs_header_nritems(path->nodes[level])) {
6309 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006310 return 0;
6311 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006312 ret = walk_up_proc(trans, root, path, wc);
6313 if (ret > 0)
6314 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006315
Yan Zheng2c47e6052009-06-27 21:07:35 -04006316 if (path->locks[level]) {
6317 btrfs_tree_unlock(path->nodes[level]);
6318 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006319 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006320 free_extent_buffer(path->nodes[level]);
6321 path->nodes[level] = NULL;
6322 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006323 }
6324 }
6325 return 1;
6326}
6327
Chris Mason9aca1d52007-03-13 11:09:37 -04006328/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006329 * drop a subvolume tree.
6330 *
6331 * this function traverses the tree freeing any blocks that only
6332 * referenced by the tree.
6333 *
6334 * when a shared tree block is found. this function decreases its
6335 * reference count by one. if update_ref is true, this function
6336 * also make sure backrefs for the shared block and all lower level
6337 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006338 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006339int btrfs_drop_snapshot(struct btrfs_root *root,
6340 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006341{
Chris Mason5caf2a02007-04-02 11:20:42 -04006342 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006343 struct btrfs_trans_handle *trans;
6344 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006345 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006346 struct walk_control *wc;
6347 struct btrfs_key key;
6348 int err = 0;
6349 int ret;
6350 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006351
Chris Mason5caf2a02007-04-02 11:20:42 -04006352 path = btrfs_alloc_path();
6353 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006354
Yan Zheng2c47e6052009-06-27 21:07:35 -04006355 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6356 BUG_ON(!wc);
6357
Yan, Zhenga22285a2010-05-16 10:48:46 -04006358 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006359 BUG_ON(IS_ERR(trans));
6360
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006361 if (block_rsv)
6362 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006363
Chris Mason9f3a7422007-08-07 15:52:19 -04006364 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006365 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006366 path->nodes[level] = btrfs_lock_root_node(root);
6367 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006368 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006369 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006370 memset(&wc->update_progress, 0,
6371 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006372 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006373 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006374 memcpy(&wc->update_progress, &key,
6375 sizeof(wc->update_progress));
6376
Chris Mason6702ed42007-08-07 16:15:09 -04006377 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006378 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006379 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006380 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6381 path->lowest_level = 0;
6382 if (ret < 0) {
6383 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006384 goto out;
6385 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006386 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006387
Chris Mason7d9eb122008-07-08 14:19:17 -04006388 /*
6389 * unlock our path, this is safe because only this
6390 * function is allowed to delete this snapshot
6391 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006392 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006393
Yan Zheng2c47e6052009-06-27 21:07:35 -04006394 level = btrfs_header_level(root->node);
6395 while (1) {
6396 btrfs_tree_lock(path->nodes[level]);
6397 btrfs_set_lock_blocking(path->nodes[level]);
6398
6399 ret = btrfs_lookup_extent_info(trans, root,
6400 path->nodes[level]->start,
6401 path->nodes[level]->len,
6402 &wc->refs[level],
6403 &wc->flags[level]);
6404 BUG_ON(ret);
6405 BUG_ON(wc->refs[level] == 0);
6406
6407 if (level == root_item->drop_level)
6408 break;
6409
6410 btrfs_tree_unlock(path->nodes[level]);
6411 WARN_ON(wc->refs[level] != 1);
6412 level--;
6413 }
6414 }
6415
6416 wc->level = level;
6417 wc->shared_level = -1;
6418 wc->stage = DROP_REFERENCE;
6419 wc->update_ref = update_ref;
6420 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006421 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006422
6423 while (1) {
6424 ret = walk_down_tree(trans, root, path, wc);
6425 if (ret < 0) {
6426 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006427 break;
6428 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006429
6430 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6431 if (ret < 0) {
6432 err = ret;
6433 break;
6434 }
6435
6436 if (ret > 0) {
6437 BUG_ON(wc->stage != DROP_REFERENCE);
6438 break;
6439 }
6440
6441 if (wc->stage == DROP_REFERENCE) {
6442 level = wc->level;
6443 btrfs_node_key(path->nodes[level],
6444 &root_item->drop_progress,
6445 path->slots[level]);
6446 root_item->drop_level = level;
6447 }
6448
6449 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006450 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006451 ret = btrfs_update_root(trans, tree_root,
6452 &root->root_key,
6453 root_item);
6454 BUG_ON(ret);
6455
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006456 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006457 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006458 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006459 if (block_rsv)
6460 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006461 }
Chris Mason20524f02007-03-10 06:35:47 -05006462 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006463 btrfs_release_path(root, path);
6464 BUG_ON(err);
6465
6466 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6467 BUG_ON(ret);
6468
Yan, Zheng76dda932009-09-21 16:00:26 -04006469 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6470 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6471 NULL, NULL);
6472 BUG_ON(ret < 0);
6473 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006474 /* if we fail to delete the orphan item this time
6475 * around, it'll get picked up the next time.
6476 *
6477 * The most common failure here is just -ENOENT.
6478 */
6479 btrfs_del_orphan_item(trans, tree_root,
6480 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006481 }
6482 }
6483
6484 if (root->in_radix) {
6485 btrfs_free_fs_root(tree_root->fs_info, root);
6486 } else {
6487 free_extent_buffer(root->node);
6488 free_extent_buffer(root->commit_root);
6489 kfree(root);
6490 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006491out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006492 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006493 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006494 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006495 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006496}
Chris Mason9078a3e2007-04-26 16:46:15 -04006497
Yan Zheng2c47e6052009-06-27 21:07:35 -04006498/*
6499 * drop subtree rooted at tree block 'node'.
6500 *
6501 * NOTE: this function will unlock and release tree block 'node'
6502 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006503int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6504 struct btrfs_root *root,
6505 struct extent_buffer *node,
6506 struct extent_buffer *parent)
6507{
6508 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006509 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006510 int level;
6511 int parent_level;
6512 int ret = 0;
6513 int wret;
6514
Yan Zheng2c47e6052009-06-27 21:07:35 -04006515 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6516
Yan Zhengf82d02d2008-10-29 14:49:05 -04006517 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006518 if (!path)
6519 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006520
Yan Zheng2c47e6052009-06-27 21:07:35 -04006521 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006522 if (!wc) {
6523 btrfs_free_path(path);
6524 return -ENOMEM;
6525 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006526
Chris Masonb9447ef2009-03-09 11:45:38 -04006527 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006528 parent_level = btrfs_header_level(parent);
6529 extent_buffer_get(parent);
6530 path->nodes[parent_level] = parent;
6531 path->slots[parent_level] = btrfs_header_nritems(parent);
6532
Chris Masonb9447ef2009-03-09 11:45:38 -04006533 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006534 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006535 path->nodes[level] = node;
6536 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006537 path->locks[level] = 1;
6538
6539 wc->refs[parent_level] = 1;
6540 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6541 wc->level = level;
6542 wc->shared_level = -1;
6543 wc->stage = DROP_REFERENCE;
6544 wc->update_ref = 0;
6545 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006546 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006547
6548 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006549 wret = walk_down_tree(trans, root, path, wc);
6550 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006551 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006552 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006553 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006554
Yan Zheng2c47e6052009-06-27 21:07:35 -04006555 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006556 if (wret < 0)
6557 ret = wret;
6558 if (wret != 0)
6559 break;
6560 }
6561
Yan Zheng2c47e6052009-06-27 21:07:35 -04006562 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006563 btrfs_free_path(path);
6564 return ret;
6565}
6566
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006567#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04006568static unsigned long calc_ra(unsigned long start, unsigned long last,
6569 unsigned long nr)
6570{
6571 return min(last, start + nr - 1);
6572}
6573
Chris Masond3977122009-01-05 21:25:51 -05006574static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05006575 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05006576{
6577 u64 page_start;
6578 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04006579 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006580 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006581 unsigned long i;
6582 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05006583 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05006584 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04006585 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04006586 unsigned int total_read = 0;
6587 unsigned int total_dirty = 0;
6588 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05006589
6590 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00006591 if (!ra)
6592 return -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006593
6594 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006595 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05006596 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6597
Zheng Yan1a40e232008-09-26 10:09:34 -04006598 /* make sure the dirty trick played by the caller work */
6599 ret = invalidate_inode_pages2_range(inode->i_mapping,
6600 first_index, last_index);
6601 if (ret)
6602 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04006603
Chris Mason4313b392008-01-03 09:08:48 -05006604 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05006605
Zheng Yan1a40e232008-09-26 10:09:34 -04006606 for (i = first_index ; i <= last_index; i++) {
6607 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04006608 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04006609 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04006610 }
6611 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04006612again:
6613 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04006614 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05006615 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04006616 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006617 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006618 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04006619 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006620 if (!PageUptodate(page)) {
6621 btrfs_readpage(NULL, page);
6622 lock_page(page);
6623 if (!PageUptodate(page)) {
6624 unlock_page(page);
6625 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006626 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05006627 goto out_unlock;
6628 }
6629 }
Chris Masonec44a352008-04-28 15:29:52 -04006630 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04006631
Chris Masonedbd8d42007-12-21 16:27:24 -05006632 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6633 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006634 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006635
Chris Mason3eaa2882008-07-24 11:57:52 -04006636 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6637 if (ordered) {
6638 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6639 unlock_page(page);
6640 page_cache_release(page);
6641 btrfs_start_ordered_extent(inode, ordered, 1);
6642 btrfs_put_ordered_extent(ordered);
6643 goto again;
6644 }
6645 set_page_extent_mapped(page);
6646
Zheng Yan1a40e232008-09-26 10:09:34 -04006647 if (i == first_index)
6648 set_extent_bits(io_tree, page_start, page_end,
6649 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006650 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04006651
Chris Masona061fc82008-05-07 11:43:44 -04006652 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006653 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006654
Chris Masond1310b22008-01-24 16:13:08 -05006655 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006656 unlock_page(page);
6657 page_cache_release(page);
6658 }
6659
6660out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04006661 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05006662 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006663 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04006664 return ret;
6665}
6666
Chris Masond3977122009-01-05 21:25:51 -05006667static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006668 struct btrfs_key *extent_key,
6669 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05006670{
Zheng Yan1a40e232008-09-26 10:09:34 -04006671 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6672 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6673 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04006674 u64 start = extent_key->objectid - offset;
6675 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006676
6677 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +00006678 BUG_ON(!em);
Zheng Yan1a40e232008-09-26 10:09:34 -04006679
Yan Zheng66435582008-10-30 14:19:50 -04006680 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006681 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04006682 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04006683 em->block_start = extent_key->objectid;
6684 em->bdev = root->fs_info->fs_devices->latest_bdev;
6685 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6686
6687 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04006688 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006689 while (1) {
6690 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04006691 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006692 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04006693 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006694 if (ret != -EEXIST) {
6695 free_extent_map(em);
6696 break;
6697 }
Yan Zheng66435582008-10-30 14:19:50 -04006698 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006699 }
Yan Zheng66435582008-10-30 14:19:50 -04006700 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006701
Yan Zheng66435582008-10-30 14:19:50 -04006702 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04006703}
6704
6705struct btrfs_ref_path {
6706 u64 extent_start;
6707 u64 nodes[BTRFS_MAX_LEVEL];
6708 u64 root_objectid;
6709 u64 root_generation;
6710 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006711 u32 num_refs;
6712 int lowest_level;
6713 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006714 int shared_level;
6715
6716 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6717 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04006718};
6719
6720struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04006721 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04006722 u64 disk_bytenr;
6723 u64 disk_num_bytes;
6724 u64 offset;
6725 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04006726 u8 compression;
6727 u8 encryption;
6728 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04006729};
6730
6731static int is_cowonly_root(u64 root_objectid)
6732{
6733 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6734 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6735 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6736 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05006737 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6738 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04006739 return 1;
6740 return 0;
6741}
6742
Chris Masond3977122009-01-05 21:25:51 -05006743static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006744 struct btrfs_root *extent_root,
6745 struct btrfs_ref_path *ref_path,
6746 int first_time)
6747{
6748 struct extent_buffer *leaf;
6749 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05006750 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05006751 struct btrfs_key key;
6752 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04006753 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05006754 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04006755 int level;
6756 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006757
Zheng Yan1a40e232008-09-26 10:09:34 -04006758 path = btrfs_alloc_path();
6759 if (!path)
6760 return -ENOMEM;
6761
Zheng Yan1a40e232008-09-26 10:09:34 -04006762 if (first_time) {
6763 ref_path->lowest_level = -1;
6764 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006765 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006766 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04006767 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006768walk_down:
6769 level = ref_path->current_level - 1;
6770 while (level >= -1) {
6771 u64 parent;
6772 if (level < ref_path->lowest_level)
6773 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006774
Chris Masond3977122009-01-05 21:25:51 -05006775 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006776 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006777 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006778 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006779 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006780
Zheng Yan1a40e232008-09-26 10:09:34 -04006781 parent = ref_path->nodes[level + 1];
6782 ref_path->nodes[level + 1] = 0;
6783 ref_path->current_level = level;
6784 BUG_ON(parent == 0);
6785
6786 key.objectid = bytenr;
6787 key.offset = parent + 1;
6788 key.type = BTRFS_EXTENT_REF_KEY;
6789
6790 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006791 if (ret < 0)
6792 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006793 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006794
Chris Masonedbd8d42007-12-21 16:27:24 -05006795 leaf = path->nodes[0];
6796 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04006797 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04006798 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04006799 if (ret < 0)
6800 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006801 if (ret > 0)
6802 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04006803 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04006804 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006805
6806 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04006807 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04006808 found_key.type == BTRFS_EXTENT_REF_KEY) {
6809 if (level < ref_path->shared_level)
6810 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006811 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006812 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006813next:
6814 level--;
6815 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006816 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006817 }
6818 /* reached lowest level */
6819 ret = 1;
6820 goto out;
6821walk_up:
6822 level = ref_path->current_level;
6823 while (level < BTRFS_MAX_LEVEL - 1) {
6824 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006825
6826 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006827 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006828 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006829 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006830
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 key.objectid = bytenr;
6834 key.offset = 0;
6835 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006836
Zheng Yan1a40e232008-09-26 10:09:34 -04006837 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6838 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006839 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006840
6841 leaf = path->nodes[0];
6842 nritems = btrfs_header_nritems(leaf);
6843 if (path->slots[0] >= nritems) {
6844 ret = btrfs_next_leaf(extent_root, path);
6845 if (ret < 0)
6846 goto out;
6847 if (ret > 0) {
6848 /* the extent was freed by someone */
6849 if (ref_path->lowest_level == level)
6850 goto out;
6851 btrfs_release_path(extent_root, path);
6852 goto walk_down;
6853 }
6854 leaf = path->nodes[0];
6855 }
6856
6857 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6858 if (found_key.objectid != bytenr ||
6859 found_key.type != BTRFS_EXTENT_REF_KEY) {
6860 /* the extent was freed by someone */
6861 if (ref_path->lowest_level == level) {
6862 ret = 1;
6863 goto out;
6864 }
6865 btrfs_release_path(extent_root, path);
6866 goto walk_down;
6867 }
6868found:
6869 ref = btrfs_item_ptr(leaf, path->slots[0],
6870 struct btrfs_extent_ref);
6871 ref_objectid = btrfs_ref_objectid(leaf, ref);
6872 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6873 if (first_time) {
6874 level = (int)ref_objectid;
6875 BUG_ON(level >= BTRFS_MAX_LEVEL);
6876 ref_path->lowest_level = level;
6877 ref_path->current_level = level;
6878 ref_path->nodes[level] = bytenr;
6879 } else {
6880 WARN_ON(ref_objectid != level);
6881 }
6882 } else {
6883 WARN_ON(level != -1);
6884 }
6885 first_time = 0;
6886
6887 if (ref_path->lowest_level == level) {
6888 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006889 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6890 }
6891
6892 /*
6893 * the block is tree root or the block isn't in reference
6894 * counted tree.
6895 */
6896 if (found_key.objectid == found_key.offset ||
6897 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6898 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6899 ref_path->root_generation =
6900 btrfs_ref_generation(leaf, ref);
6901 if (level < 0) {
6902 /* special reference from the tree log */
6903 ref_path->nodes[0] = found_key.offset;
6904 ref_path->current_level = 0;
6905 }
6906 ret = 0;
6907 goto out;
6908 }
6909
6910 level++;
6911 BUG_ON(ref_path->nodes[level] != 0);
6912 ref_path->nodes[level] = found_key.offset;
6913 ref_path->current_level = level;
6914
6915 /*
6916 * the reference was created in the running transaction,
6917 * no need to continue walking up.
6918 */
6919 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6920 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6921 ref_path->root_generation =
6922 btrfs_ref_generation(leaf, ref);
6923 ret = 0;
6924 goto out;
6925 }
6926
6927 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006928 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006929 }
6930 /* reached max tree level, but no tree root found. */
6931 BUG();
6932out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006933 btrfs_free_path(path);
6934 return ret;
6935}
6936
6937static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6938 struct btrfs_root *extent_root,
6939 struct btrfs_ref_path *ref_path,
6940 u64 extent_start)
6941{
6942 memset(ref_path, 0, sizeof(*ref_path));
6943 ref_path->extent_start = extent_start;
6944
6945 return __next_ref_path(trans, extent_root, ref_path, 1);
6946}
6947
6948static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6949 struct btrfs_root *extent_root,
6950 struct btrfs_ref_path *ref_path)
6951{
6952 return __next_ref_path(trans, extent_root, ref_path, 0);
6953}
6954
Chris Masond3977122009-01-05 21:25:51 -05006955static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006956 struct btrfs_key *extent_key,
6957 u64 offset, int no_fragment,
6958 struct disk_extent **extents,
6959 int *nr_extents)
6960{
6961 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6962 struct btrfs_path *path;
6963 struct btrfs_file_extent_item *fi;
6964 struct extent_buffer *leaf;
6965 struct disk_extent *exts = *extents;
6966 struct btrfs_key found_key;
6967 u64 cur_pos;
6968 u64 last_byte;
6969 u32 nritems;
6970 int nr = 0;
6971 int max = *nr_extents;
6972 int ret;
6973
6974 WARN_ON(!no_fragment && *extents);
6975 if (!exts) {
6976 max = 1;
6977 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6978 if (!exts)
6979 return -ENOMEM;
6980 }
6981
6982 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006983 if (!path) {
6984 if (exts != *extents)
6985 kfree(exts);
6986 return -ENOMEM;
6987 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006988
6989 cur_pos = extent_key->objectid - offset;
6990 last_byte = extent_key->objectid + extent_key->offset;
6991 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6992 cur_pos, 0);
6993 if (ret < 0)
6994 goto out;
6995 if (ret > 0) {
6996 ret = -ENOENT;
6997 goto out;
6998 }
6999
7000 while (1) {
7001 leaf = path->nodes[0];
7002 nritems = btrfs_header_nritems(leaf);
7003 if (path->slots[0] >= nritems) {
7004 ret = btrfs_next_leaf(root, path);
7005 if (ret < 0)
7006 goto out;
7007 if (ret > 0)
7008 break;
7009 leaf = path->nodes[0];
7010 }
7011
7012 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7013 if (found_key.offset != cur_pos ||
7014 found_key.type != BTRFS_EXTENT_DATA_KEY ||
7015 found_key.objectid != reloc_inode->i_ino)
7016 break;
7017
7018 fi = btrfs_item_ptr(leaf, path->slots[0],
7019 struct btrfs_file_extent_item);
7020 if (btrfs_file_extent_type(leaf, fi) !=
7021 BTRFS_FILE_EXTENT_REG ||
7022 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7023 break;
7024
7025 if (nr == max) {
7026 struct disk_extent *old = exts;
7027 max *= 2;
7028 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
Yoshinori Sanodac97e52011-02-15 12:01:42 +00007029 if (!exts) {
7030 ret = -ENOMEM;
7031 goto out;
7032 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007033 memcpy(exts, old, sizeof(*exts) * nr);
7034 if (old != *extents)
7035 kfree(old);
7036 }
7037
7038 exts[nr].disk_bytenr =
7039 btrfs_file_extent_disk_bytenr(leaf, fi);
7040 exts[nr].disk_num_bytes =
7041 btrfs_file_extent_disk_num_bytes(leaf, fi);
7042 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
7043 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04007044 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7045 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
7046 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
7047 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
7048 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04007049 BUG_ON(exts[nr].offset > 0);
7050 BUG_ON(exts[nr].compression || exts[nr].encryption);
7051 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007052
7053 cur_pos += exts[nr].num_bytes;
7054 nr++;
7055
7056 if (cur_pos + offset >= last_byte)
7057 break;
7058
7059 if (no_fragment) {
7060 ret = 1;
7061 goto out;
7062 }
7063 path->slots[0]++;
7064 }
7065
Yan Zheng1f80e4d2008-12-19 10:59:04 -05007066 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04007067 if (cur_pos + offset < last_byte) {
7068 ret = -ENOENT;
7069 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05007070 }
7071 ret = 0;
7072out:
Zheng Yan1a40e232008-09-26 10:09:34 -04007073 btrfs_free_path(path);
7074 if (ret) {
7075 if (exts != *extents)
7076 kfree(exts);
7077 } else {
7078 *extents = exts;
7079 *nr_extents = nr;
7080 }
7081 return ret;
7082}
7083
Chris Masond3977122009-01-05 21:25:51 -05007084static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007085 struct btrfs_root *root,
7086 struct btrfs_path *path,
7087 struct btrfs_key *extent_key,
7088 struct btrfs_key *leaf_key,
7089 struct btrfs_ref_path *ref_path,
7090 struct disk_extent *new_extents,
7091 int nr_extents)
7092{
7093 struct extent_buffer *leaf;
7094 struct btrfs_file_extent_item *fi;
7095 struct inode *inode = NULL;
7096 struct btrfs_key key;
7097 u64 lock_start = 0;
7098 u64 lock_end = 0;
7099 u64 num_bytes;
7100 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05007101 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007102 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007103 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007104 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04007105 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04007106 int ret;
7107
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007108 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04007109 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007110 if (key.objectid < ref_path->owner_objectid ||
7111 (key.objectid == ref_path->owner_objectid &&
7112 key.type < BTRFS_EXTENT_DATA_KEY)) {
7113 key.objectid = ref_path->owner_objectid;
7114 key.type = BTRFS_EXTENT_DATA_KEY;
7115 key.offset = 0;
7116 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007117 }
7118
7119 while (1) {
7120 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7121 if (ret < 0)
7122 goto out;
7123
7124 leaf = path->nodes[0];
7125 nritems = btrfs_header_nritems(leaf);
7126next:
7127 if (extent_locked && ret > 0) {
7128 /*
7129 * the file extent item was modified by someone
7130 * before the extent got locked.
7131 */
Zheng Yan1a40e232008-09-26 10:09:34 -04007132 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7133 lock_end, GFP_NOFS);
7134 extent_locked = 0;
7135 }
7136
7137 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007138 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04007139 break;
7140
7141 BUG_ON(extent_locked);
7142 ret = btrfs_next_leaf(root, path);
7143 if (ret < 0)
7144 goto out;
7145 if (ret > 0)
7146 break;
7147 leaf = path->nodes[0];
7148 nritems = btrfs_header_nritems(leaf);
7149 }
7150
7151 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7152
7153 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7154 if ((key.objectid > ref_path->owner_objectid) ||
7155 (key.objectid == ref_path->owner_objectid &&
7156 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05007157 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007158 break;
7159 }
7160
7161 if (inode && key.objectid != inode->i_ino) {
7162 BUG_ON(extent_locked);
7163 btrfs_release_path(root, path);
7164 mutex_unlock(&inode->i_mutex);
7165 iput(inode);
7166 inode = NULL;
7167 continue;
7168 }
7169
7170 if (key.type != BTRFS_EXTENT_DATA_KEY) {
7171 path->slots[0]++;
7172 ret = 1;
7173 goto next;
7174 }
7175 fi = btrfs_item_ptr(leaf, path->slots[0],
7176 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04007177 extent_type = btrfs_file_extent_type(leaf, fi);
7178 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
7179 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04007180 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
7181 extent_key->objectid)) {
7182 path->slots[0]++;
7183 ret = 1;
7184 goto next;
7185 }
7186
7187 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7188 ext_offset = btrfs_file_extent_offset(leaf, fi);
7189
Yan Zheng86288a12009-01-21 10:49:16 -05007190 if (search_end == (u64)-1) {
7191 search_end = key.offset - ext_offset +
7192 btrfs_file_extent_ram_bytes(leaf, fi);
7193 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007194
7195 if (!extent_locked) {
7196 lock_start = key.offset;
7197 lock_end = lock_start + num_bytes - 1;
7198 } else {
Yan Zheng66435582008-10-30 14:19:50 -04007199 if (lock_start > key.offset ||
7200 lock_end + 1 < key.offset + num_bytes) {
7201 unlock_extent(&BTRFS_I(inode)->io_tree,
7202 lock_start, lock_end, GFP_NOFS);
7203 extent_locked = 0;
7204 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007205 }
7206
7207 if (!inode) {
7208 btrfs_release_path(root, path);
7209
7210 inode = btrfs_iget_locked(root->fs_info->sb,
7211 key.objectid, root);
7212 if (inode->i_state & I_NEW) {
7213 BTRFS_I(inode)->root = root;
7214 BTRFS_I(inode)->location.objectid =
7215 key.objectid;
7216 BTRFS_I(inode)->location.type =
7217 BTRFS_INODE_ITEM_KEY;
7218 BTRFS_I(inode)->location.offset = 0;
7219 btrfs_read_locked_inode(inode);
7220 unlock_new_inode(inode);
7221 }
7222 /*
7223 * some code call btrfs_commit_transaction while
7224 * holding the i_mutex, so we can't use mutex_lock
7225 * here.
7226 */
7227 if (is_bad_inode(inode) ||
7228 !mutex_trylock(&inode->i_mutex)) {
7229 iput(inode);
7230 inode = NULL;
7231 key.offset = (u64)-1;
7232 goto skip;
7233 }
7234 }
7235
7236 if (!extent_locked) {
7237 struct btrfs_ordered_extent *ordered;
7238
7239 btrfs_release_path(root, path);
7240
7241 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7242 lock_end, GFP_NOFS);
7243 ordered = btrfs_lookup_first_ordered_extent(inode,
7244 lock_end);
7245 if (ordered &&
7246 ordered->file_offset <= lock_end &&
7247 ordered->file_offset + ordered->len > lock_start) {
7248 unlock_extent(&BTRFS_I(inode)->io_tree,
7249 lock_start, lock_end, GFP_NOFS);
7250 btrfs_start_ordered_extent(inode, ordered, 1);
7251 btrfs_put_ordered_extent(ordered);
7252 key.offset += num_bytes;
7253 goto skip;
7254 }
7255 if (ordered)
7256 btrfs_put_ordered_extent(ordered);
7257
Zheng Yan1a40e232008-09-26 10:09:34 -04007258 extent_locked = 1;
7259 continue;
7260 }
7261
7262 if (nr_extents == 1) {
7263 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04007264 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7265 new_extents[0].disk_bytenr);
7266 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7267 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007268 btrfs_mark_buffer_dirty(leaf);
7269
7270 btrfs_drop_extent_cache(inode, key.offset,
7271 key.offset + num_bytes - 1, 0);
7272
7273 ret = btrfs_inc_extent_ref(trans, root,
7274 new_extents[0].disk_bytenr,
7275 new_extents[0].disk_num_bytes,
7276 leaf->start,
7277 root->root_key.objectid,
7278 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007279 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007280 BUG_ON(ret);
7281
7282 ret = btrfs_free_extent(trans, root,
7283 extent_key->objectid,
7284 extent_key->offset,
7285 leaf->start,
7286 btrfs_header_owner(leaf),
7287 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007288 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007289 BUG_ON(ret);
7290
7291 btrfs_release_path(root, path);
7292 key.offset += num_bytes;
7293 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04007294 BUG_ON(1);
7295#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04007296 u64 alloc_hint;
7297 u64 extent_len;
7298 int i;
7299 /*
7300 * drop old extent pointer at first, then insert the
7301 * new pointers one bye one
7302 */
7303 btrfs_release_path(root, path);
7304 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7305 key.offset + num_bytes,
7306 key.offset, &alloc_hint);
7307 BUG_ON(ret);
7308
7309 for (i = 0; i < nr_extents; i++) {
7310 if (ext_offset >= new_extents[i].num_bytes) {
7311 ext_offset -= new_extents[i].num_bytes;
7312 continue;
7313 }
7314 extent_len = min(new_extents[i].num_bytes -
7315 ext_offset, num_bytes);
7316
7317 ret = btrfs_insert_empty_item(trans, root,
7318 path, &key,
7319 sizeof(*fi));
7320 BUG_ON(ret);
7321
7322 leaf = path->nodes[0];
7323 fi = btrfs_item_ptr(leaf, path->slots[0],
7324 struct btrfs_file_extent_item);
7325 btrfs_set_file_extent_generation(leaf, fi,
7326 trans->transid);
7327 btrfs_set_file_extent_type(leaf, fi,
7328 BTRFS_FILE_EXTENT_REG);
7329 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7330 new_extents[i].disk_bytenr);
7331 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7332 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04007333 btrfs_set_file_extent_ram_bytes(leaf, fi,
7334 new_extents[i].ram_bytes);
7335
7336 btrfs_set_file_extent_compression(leaf, fi,
7337 new_extents[i].compression);
7338 btrfs_set_file_extent_encryption(leaf, fi,
7339 new_extents[i].encryption);
7340 btrfs_set_file_extent_other_encoding(leaf, fi,
7341 new_extents[i].other_encoding);
7342
Zheng Yan1a40e232008-09-26 10:09:34 -04007343 btrfs_set_file_extent_num_bytes(leaf, fi,
7344 extent_len);
7345 ext_offset += new_extents[i].offset;
7346 btrfs_set_file_extent_offset(leaf, fi,
7347 ext_offset);
7348 btrfs_mark_buffer_dirty(leaf);
7349
7350 btrfs_drop_extent_cache(inode, key.offset,
7351 key.offset + extent_len - 1, 0);
7352
7353 ret = btrfs_inc_extent_ref(trans, root,
7354 new_extents[i].disk_bytenr,
7355 new_extents[i].disk_num_bytes,
7356 leaf->start,
7357 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007358 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007359 BUG_ON(ret);
7360 btrfs_release_path(root, path);
7361
Yan Zhenga76a3cd2008-10-09 11:46:29 -04007362 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04007363
7364 ext_offset = 0;
7365 num_bytes -= extent_len;
7366 key.offset += extent_len;
7367
7368 if (num_bytes == 0)
7369 break;
7370 }
7371 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007372#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04007373 }
7374
7375 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007376 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7377 lock_end, GFP_NOFS);
7378 extent_locked = 0;
7379 }
7380skip:
7381 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05007382 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007383 break;
7384
7385 cond_resched();
7386 }
7387 ret = 0;
7388out:
7389 btrfs_release_path(root, path);
7390 if (inode) {
7391 mutex_unlock(&inode->i_mutex);
7392 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007393 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7394 lock_end, GFP_NOFS);
7395 }
7396 iput(inode);
7397 }
7398 return ret;
7399}
7400
Zheng Yan1a40e232008-09-26 10:09:34 -04007401int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7402 struct btrfs_root *root,
7403 struct extent_buffer *buf, u64 orig_start)
7404{
7405 int level;
7406 int ret;
7407
7408 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7409 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7410
7411 level = btrfs_header_level(buf);
7412 if (level == 0) {
7413 struct btrfs_leaf_ref *ref;
7414 struct btrfs_leaf_ref *orig_ref;
7415
7416 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7417 if (!orig_ref)
7418 return -ENOENT;
7419
7420 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7421 if (!ref) {
7422 btrfs_free_leaf_ref(root, orig_ref);
7423 return -ENOMEM;
7424 }
7425
7426 ref->nritems = orig_ref->nritems;
7427 memcpy(ref->extents, orig_ref->extents,
7428 sizeof(ref->extents[0]) * ref->nritems);
7429
7430 btrfs_free_leaf_ref(root, orig_ref);
7431
7432 ref->root_gen = trans->transid;
7433 ref->bytenr = buf->start;
7434 ref->owner = btrfs_header_owner(buf);
7435 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05007436
Zheng Yan1a40e232008-09-26 10:09:34 -04007437 ret = btrfs_add_leaf_ref(root, ref, 0);
7438 WARN_ON(ret);
7439 btrfs_free_leaf_ref(root, ref);
7440 }
7441 return 0;
7442}
7443
Chris Masond3977122009-01-05 21:25:51 -05007444static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007445 struct extent_buffer *leaf,
7446 struct btrfs_block_group_cache *group,
7447 struct btrfs_root *target_root)
7448{
7449 struct btrfs_key key;
7450 struct inode *inode = NULL;
7451 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007452 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04007453 u64 num_bytes;
7454 u64 skip_objectid = 0;
7455 u32 nritems;
7456 u32 i;
7457
7458 nritems = btrfs_header_nritems(leaf);
7459 for (i = 0; i < nritems; i++) {
7460 btrfs_item_key_to_cpu(leaf, &key, i);
7461 if (key.objectid == skip_objectid ||
7462 key.type != BTRFS_EXTENT_DATA_KEY)
7463 continue;
7464 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7465 if (btrfs_file_extent_type(leaf, fi) ==
7466 BTRFS_FILE_EXTENT_INLINE)
7467 continue;
7468 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7469 continue;
7470 if (!inode || inode->i_ino != key.objectid) {
7471 iput(inode);
7472 inode = btrfs_ilookup(target_root->fs_info->sb,
7473 key.objectid, target_root, 1);
7474 }
7475 if (!inode) {
7476 skip_objectid = key.objectid;
7477 continue;
7478 }
7479 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7480
Josef Bacik2ac55d42010-02-03 19:33:23 +00007481 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7482 key.offset + num_bytes - 1, 0, &cached_state,
7483 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007484 btrfs_drop_extent_cache(inode, key.offset,
7485 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007486 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7487 key.offset + num_bytes - 1, &cached_state,
7488 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007489 cond_resched();
7490 }
7491 iput(inode);
7492 return 0;
7493}
7494
Chris Masond3977122009-01-05 21:25:51 -05007495static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007496 struct btrfs_root *root,
7497 struct extent_buffer *leaf,
7498 struct btrfs_block_group_cache *group,
7499 struct inode *reloc_inode)
7500{
7501 struct btrfs_key key;
7502 struct btrfs_key extent_key;
7503 struct btrfs_file_extent_item *fi;
7504 struct btrfs_leaf_ref *ref;
7505 struct disk_extent *new_extent;
7506 u64 bytenr;
7507 u64 num_bytes;
7508 u32 nritems;
7509 u32 i;
7510 int ext_index;
7511 int nr_extent;
7512 int ret;
7513
7514 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007515 if (!new_extent)
7516 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007517
7518 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7519 BUG_ON(!ref);
7520
7521 ext_index = -1;
7522 nritems = btrfs_header_nritems(leaf);
7523 for (i = 0; i < nritems; i++) {
7524 btrfs_item_key_to_cpu(leaf, &key, i);
7525 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7526 continue;
7527 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7528 if (btrfs_file_extent_type(leaf, fi) ==
7529 BTRFS_FILE_EXTENT_INLINE)
7530 continue;
7531 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7532 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7533 if (bytenr == 0)
7534 continue;
7535
7536 ext_index++;
7537 if (bytenr >= group->key.objectid + group->key.offset ||
7538 bytenr + num_bytes <= group->key.objectid)
7539 continue;
7540
7541 extent_key.objectid = bytenr;
7542 extent_key.offset = num_bytes;
7543 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7544 nr_extent = 1;
7545 ret = get_new_locations(reloc_inode, &extent_key,
7546 group->key.objectid, 1,
7547 &new_extent, &nr_extent);
7548 if (ret > 0)
7549 continue;
7550 BUG_ON(ret < 0);
7551
7552 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7553 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7554 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7555 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7556
Zheng Yan1a40e232008-09-26 10:09:34 -04007557 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7558 new_extent->disk_bytenr);
7559 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7560 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007561 btrfs_mark_buffer_dirty(leaf);
7562
7563 ret = btrfs_inc_extent_ref(trans, root,
7564 new_extent->disk_bytenr,
7565 new_extent->disk_num_bytes,
7566 leaf->start,
7567 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007568 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007569 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04007570
Zheng Yan1a40e232008-09-26 10:09:34 -04007571 ret = btrfs_free_extent(trans, root,
7572 bytenr, num_bytes, leaf->start,
7573 btrfs_header_owner(leaf),
7574 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007575 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007576 BUG_ON(ret);
7577 cond_resched();
7578 }
7579 kfree(new_extent);
7580 BUG_ON(ext_index + 1 != ref->nritems);
7581 btrfs_free_leaf_ref(root, ref);
7582 return 0;
7583}
7584
Yan Zhengf82d02d2008-10-29 14:49:05 -04007585int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7586 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04007587{
7588 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007589 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007590
7591 if (root->reloc_root) {
7592 reloc_root = root->reloc_root;
7593 root->reloc_root = NULL;
7594 list_add(&reloc_root->dead_list,
7595 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007596
7597 btrfs_set_root_bytenr(&reloc_root->root_item,
7598 reloc_root->node->start);
7599 btrfs_set_root_level(&root->root_item,
7600 btrfs_header_level(reloc_root->node));
7601 memset(&reloc_root->root_item.drop_progress, 0,
7602 sizeof(struct btrfs_disk_key));
7603 reloc_root->root_item.drop_level = 0;
7604
7605 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7606 &reloc_root->root_key,
7607 &reloc_root->root_item);
7608 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007609 }
7610 return 0;
7611}
7612
7613int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7614{
7615 struct btrfs_trans_handle *trans;
7616 struct btrfs_root *reloc_root;
7617 struct btrfs_root *prev_root = NULL;
7618 struct list_head dead_roots;
7619 int ret;
7620 unsigned long nr;
7621
7622 INIT_LIST_HEAD(&dead_roots);
7623 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7624
7625 while (!list_empty(&dead_roots)) {
7626 reloc_root = list_entry(dead_roots.prev,
7627 struct btrfs_root, dead_list);
7628 list_del_init(&reloc_root->dead_list);
7629
7630 BUG_ON(reloc_root->commit_root != NULL);
7631 while (1) {
7632 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007633 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007634
7635 mutex_lock(&root->fs_info->drop_mutex);
7636 ret = btrfs_drop_snapshot(trans, reloc_root);
7637 if (ret != -EAGAIN)
7638 break;
7639 mutex_unlock(&root->fs_info->drop_mutex);
7640
7641 nr = trans->blocks_used;
7642 ret = btrfs_end_transaction(trans, root);
7643 BUG_ON(ret);
7644 btrfs_btree_balance_dirty(root, nr);
7645 }
7646
7647 free_extent_buffer(reloc_root->node);
7648
7649 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7650 &reloc_root->root_key);
7651 BUG_ON(ret);
7652 mutex_unlock(&root->fs_info->drop_mutex);
7653
7654 nr = trans->blocks_used;
7655 ret = btrfs_end_transaction(trans, root);
7656 BUG_ON(ret);
7657 btrfs_btree_balance_dirty(root, nr);
7658
7659 kfree(prev_root);
7660 prev_root = reloc_root;
7661 }
7662 if (prev_root) {
7663 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7664 kfree(prev_root);
7665 }
7666 return 0;
7667}
7668
7669int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7670{
7671 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7672 return 0;
7673}
7674
7675int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7676{
7677 struct btrfs_root *reloc_root;
7678 struct btrfs_trans_handle *trans;
7679 struct btrfs_key location;
7680 int found;
7681 int ret;
7682
7683 mutex_lock(&root->fs_info->tree_reloc_mutex);
7684 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7685 BUG_ON(ret);
7686 found = !list_empty(&root->fs_info->dead_reloc_roots);
7687 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7688
7689 if (found) {
7690 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007691 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007692 ret = btrfs_commit_transaction(trans, root);
7693 BUG_ON(ret);
7694 }
7695
7696 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7697 location.offset = (u64)-1;
7698 location.type = BTRFS_ROOT_ITEM_KEY;
7699
7700 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7701 BUG_ON(!reloc_root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05007702 ret = btrfs_orphan_cleanup(reloc_root);
7703 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007704 return 0;
7705}
7706
Chris Masond3977122009-01-05 21:25:51 -05007707static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007708 struct btrfs_root *root)
7709{
7710 struct btrfs_root *reloc_root;
7711 struct extent_buffer *eb;
7712 struct btrfs_root_item *root_item;
7713 struct btrfs_key root_key;
7714 int ret;
7715
7716 BUG_ON(!root->ref_cows);
7717 if (root->reloc_root)
7718 return 0;
7719
7720 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007721 if (!root_item)
7722 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007723
7724 ret = btrfs_copy_root(trans, root, root->commit_root,
7725 &eb, BTRFS_TREE_RELOC_OBJECTID);
7726 BUG_ON(ret);
7727
7728 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7729 root_key.offset = root->root_key.objectid;
7730 root_key.type = BTRFS_ROOT_ITEM_KEY;
7731
7732 memcpy(root_item, &root->root_item, sizeof(root_item));
7733 btrfs_set_root_refs(root_item, 0);
7734 btrfs_set_root_bytenr(root_item, eb->start);
7735 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04007736 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007737
7738 btrfs_tree_unlock(eb);
7739 free_extent_buffer(eb);
7740
7741 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7742 &root_key, root_item);
7743 BUG_ON(ret);
7744 kfree(root_item);
7745
7746 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7747 &root_key);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007748 BUG_ON(IS_ERR(reloc_root));
Zheng Yan1a40e232008-09-26 10:09:34 -04007749 reloc_root->last_trans = trans->transid;
7750 reloc_root->commit_root = NULL;
7751 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7752
7753 root->reloc_root = reloc_root;
7754 return 0;
7755}
7756
7757/*
7758 * Core function of space balance.
7759 *
7760 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04007761 * counted roots. There is one reloc tree for each subvol, and all
7762 * reloc trees share same root key objectid. Reloc trees are snapshots
7763 * of the latest committed roots of subvols (root->commit_root).
7764 *
7765 * To relocate a tree block referenced by a subvol, there are two steps.
7766 * COW the block through subvol's reloc tree, then update block pointer
7767 * in the subvol to point to the new block. Since all reloc trees share
7768 * same root key objectid, doing special handing for tree blocks owned
7769 * by them is easy. Once a tree block has been COWed in one reloc tree,
7770 * we can use the resulting new block directly when the same block is
7771 * required to COW again through other reloc trees. By this way, relocated
7772 * tree blocks are shared between reloc trees, so they are also shared
7773 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04007774 */
Chris Masond3977122009-01-05 21:25:51 -05007775static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007776 struct btrfs_root *root,
7777 struct btrfs_path *path,
7778 struct btrfs_key *first_key,
7779 struct btrfs_ref_path *ref_path,
7780 struct btrfs_block_group_cache *group,
7781 struct inode *reloc_inode)
7782{
7783 struct btrfs_root *reloc_root;
7784 struct extent_buffer *eb = NULL;
7785 struct btrfs_key *keys;
7786 u64 *nodes;
7787 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007788 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04007789 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007790 int ret;
7791
7792 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7793 lowest_level = ref_path->owner_objectid;
7794
Yan Zhengf82d02d2008-10-29 14:49:05 -04007795 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007796 path->lowest_level = lowest_level;
7797 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7798 BUG_ON(ret < 0);
7799 path->lowest_level = 0;
7800 btrfs_release_path(root, path);
7801 return 0;
7802 }
7803
Zheng Yan1a40e232008-09-26 10:09:34 -04007804 mutex_lock(&root->fs_info->tree_reloc_mutex);
7805 ret = init_reloc_tree(trans, root);
7806 BUG_ON(ret);
7807 reloc_root = root->reloc_root;
7808
Yan Zhengf82d02d2008-10-29 14:49:05 -04007809 shared_level = ref_path->shared_level;
7810 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007811
Yan Zhengf82d02d2008-10-29 14:49:05 -04007812 keys = ref_path->node_keys;
7813 nodes = ref_path->new_nodes;
7814 memset(&keys[shared_level + 1], 0,
7815 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7816 memset(&nodes[shared_level + 1], 0,
7817 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04007818
Yan Zhengf82d02d2008-10-29 14:49:05 -04007819 if (nodes[lowest_level] == 0) {
7820 path->lowest_level = lowest_level;
7821 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7822 0, 1);
7823 BUG_ON(ret);
7824 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7825 eb = path->nodes[level];
7826 if (!eb || eb == reloc_root->node)
7827 break;
7828 nodes[level] = eb->start;
7829 if (level == 0)
7830 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7831 else
7832 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7833 }
Yan Zheng2b820322008-11-17 21:11:30 -05007834 if (nodes[0] &&
7835 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007836 eb = path->nodes[0];
7837 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7838 group, reloc_inode);
7839 BUG_ON(ret);
7840 }
7841 btrfs_release_path(reloc_root, path);
7842 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007843 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007844 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007845 BUG_ON(ret);
7846 }
7847
Zheng Yan1a40e232008-09-26 10:09:34 -04007848 /*
7849 * replace tree blocks in the fs tree with tree blocks in
7850 * the reloc tree.
7851 */
7852 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7853 BUG_ON(ret < 0);
7854
7855 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007856 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7857 0, 0);
7858 BUG_ON(ret);
7859 extent_buffer_get(path->nodes[0]);
7860 eb = path->nodes[0];
7861 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007862 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7863 BUG_ON(ret);
7864 free_extent_buffer(eb);
7865 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007866
Yan Zhengf82d02d2008-10-29 14:49:05 -04007867 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007868 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007869 return 0;
7870}
7871
Chris Masond3977122009-01-05 21:25:51 -05007872static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007873 struct btrfs_root *root,
7874 struct btrfs_path *path,
7875 struct btrfs_key *first_key,
7876 struct btrfs_ref_path *ref_path)
7877{
7878 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007879
7880 ret = relocate_one_path(trans, root, path, first_key,
7881 ref_path, NULL, NULL);
7882 BUG_ON(ret);
7883
Zheng Yan1a40e232008-09-26 10:09:34 -04007884 return 0;
7885}
7886
Chris Masond3977122009-01-05 21:25:51 -05007887static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007888 struct btrfs_root *extent_root,
7889 struct btrfs_path *path,
7890 struct btrfs_key *extent_key)
7891{
7892 int ret;
7893
Zheng Yan1a40e232008-09-26 10:09:34 -04007894 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7895 if (ret)
7896 goto out;
7897 ret = btrfs_del_item(trans, extent_root, path);
7898out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007899 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007900 return ret;
7901}
7902
Chris Masond3977122009-01-05 21:25:51 -05007903static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007904 struct btrfs_ref_path *ref_path)
7905{
7906 struct btrfs_key root_key;
7907
7908 root_key.objectid = ref_path->root_objectid;
7909 root_key.type = BTRFS_ROOT_ITEM_KEY;
7910 if (is_cowonly_root(ref_path->root_objectid))
7911 root_key.offset = 0;
7912 else
7913 root_key.offset = (u64)-1;
7914
7915 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7916}
7917
Chris Masond3977122009-01-05 21:25:51 -05007918static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007919 struct btrfs_path *path,
7920 struct btrfs_key *extent_key,
7921 struct btrfs_block_group_cache *group,
7922 struct inode *reloc_inode, int pass)
7923{
7924 struct btrfs_trans_handle *trans;
7925 struct btrfs_root *found_root;
7926 struct btrfs_ref_path *ref_path = NULL;
7927 struct disk_extent *new_extents = NULL;
7928 int nr_extents = 0;
7929 int loops;
7930 int ret;
7931 int level;
7932 struct btrfs_key first_key;
7933 u64 prev_block = 0;
7934
Zheng Yan1a40e232008-09-26 10:09:34 -04007935
7936 trans = btrfs_start_transaction(extent_root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007937 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007938
7939 if (extent_key->objectid == 0) {
7940 ret = del_extent_zero(trans, extent_root, path, extent_key);
7941 goto out;
7942 }
7943
7944 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7945 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007946 ret = -ENOMEM;
7947 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007948 }
7949
7950 for (loops = 0; ; loops++) {
7951 if (loops == 0) {
7952 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7953 extent_key->objectid);
7954 } else {
7955 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7956 }
7957 if (ret < 0)
7958 goto out;
7959 if (ret > 0)
7960 break;
7961
7962 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7963 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7964 continue;
7965
7966 found_root = read_ref_root(extent_root->fs_info, ref_path);
7967 BUG_ON(!found_root);
7968 /*
7969 * for reference counted tree, only process reference paths
7970 * rooted at the latest committed root.
7971 */
7972 if (found_root->ref_cows &&
7973 ref_path->root_generation != found_root->root_key.offset)
7974 continue;
7975
7976 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7977 if (pass == 0) {
7978 /*
7979 * copy data extents to new locations
7980 */
7981 u64 group_start = group->key.objectid;
7982 ret = relocate_data_extent(reloc_inode,
7983 extent_key,
7984 group_start);
7985 if (ret < 0)
7986 goto out;
7987 break;
7988 }
7989 level = 0;
7990 } else {
7991 level = ref_path->owner_objectid;
7992 }
7993
7994 if (prev_block != ref_path->nodes[level]) {
7995 struct extent_buffer *eb;
7996 u64 block_start = ref_path->nodes[level];
7997 u64 block_size = btrfs_level_size(found_root, level);
7998
7999 eb = read_tree_block(found_root, block_start,
8000 block_size, 0);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008001 if (!eb) {
8002 ret = -EIO;
8003 goto out;
8004 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008005 btrfs_tree_lock(eb);
8006 BUG_ON(level != btrfs_header_level(eb));
8007
8008 if (level == 0)
8009 btrfs_item_key_to_cpu(eb, &first_key, 0);
8010 else
8011 btrfs_node_key_to_cpu(eb, &first_key, 0);
8012
8013 btrfs_tree_unlock(eb);
8014 free_extent_buffer(eb);
8015 prev_block = block_start;
8016 }
8017
Yan Zheng24562422009-02-12 14:14:53 -05008018 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05008019 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05008020 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05008021 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8022 /*
8023 * try to update data extent references while
8024 * keeping metadata shared between snapshots.
8025 */
8026 if (pass == 1) {
8027 ret = relocate_one_path(trans, found_root,
8028 path, &first_key, ref_path,
8029 group, reloc_inode);
8030 if (ret < 0)
8031 goto out;
8032 continue;
8033 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008034 /*
8035 * use fallback method to process the remaining
8036 * references.
8037 */
8038 if (!new_extents) {
8039 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04008040 new_extents = kmalloc(sizeof(*new_extents),
8041 GFP_NOFS);
8042 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008043 ret = get_new_locations(reloc_inode,
8044 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04008045 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04008046 &new_extents,
8047 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04008048 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04008049 goto out;
8050 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008051 ret = replace_one_extent(trans, found_root,
8052 path, extent_key,
8053 &first_key, ref_path,
8054 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05008055 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04008056 ret = relocate_tree_block(trans, found_root, path,
8057 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04008058 }
8059 if (ret < 0)
8060 goto out;
8061 }
8062 ret = 0;
8063out:
8064 btrfs_end_transaction(trans, extent_root);
8065 kfree(new_extents);
8066 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05008067 return ret;
8068}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008069#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05008070
Chris Masonec44a352008-04-28 15:29:52 -04008071static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8072{
8073 u64 num_devices;
8074 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
8075 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8076
Chris Masoncd02dca2010-12-13 14:56:23 -05008077 /*
8078 * we add in the count of missing devices because we want
8079 * to make sure that any RAID levels on a degraded FS
8080 * continue to be honored.
8081 */
8082 num_devices = root->fs_info->fs_devices->rw_devices +
8083 root->fs_info->fs_devices->missing_devices;
8084
Chris Masonec44a352008-04-28 15:29:52 -04008085 if (num_devices == 1) {
8086 stripped |= BTRFS_BLOCK_GROUP_DUP;
8087 stripped = flags & ~stripped;
8088
8089 /* turn raid0 into single device chunks */
8090 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8091 return stripped;
8092
8093 /* turn mirroring into duplication */
8094 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8095 BTRFS_BLOCK_GROUP_RAID10))
8096 return stripped | BTRFS_BLOCK_GROUP_DUP;
8097 return flags;
8098 } else {
8099 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04008100 if (flags & stripped)
8101 return flags;
8102
8103 stripped |= BTRFS_BLOCK_GROUP_DUP;
8104 stripped = flags & ~stripped;
8105
8106 /* switch duplicated blocks with raid1 */
8107 if (flags & BTRFS_BLOCK_GROUP_DUP)
8108 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8109
8110 /* turn single device chunks into raid0 */
8111 return stripped | BTRFS_BLOCK_GROUP_RAID0;
8112 }
8113 return flags;
8114}
8115
Yan, Zhengf0486c62010-05-16 10:46:25 -04008116static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04008117{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008118 struct btrfs_space_info *sinfo = cache->space_info;
8119 u64 num_bytes;
8120 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04008121
Yan, Zhengf0486c62010-05-16 10:46:25 -04008122 if (cache->ro)
8123 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04008124
Yan, Zhengf0486c62010-05-16 10:46:25 -04008125 spin_lock(&sinfo->lock);
8126 spin_lock(&cache->lock);
8127 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8128 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04008129
Yan, Zhengf0486c62010-05-16 10:46:25 -04008130 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
8131 sinfo->bytes_may_use + sinfo->bytes_readonly +
Chris Mason65e53412010-12-24 06:41:52 -05008132 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04008133 sinfo->bytes_readonly += num_bytes;
8134 sinfo->bytes_reserved += cache->reserved_pinned;
8135 cache->reserved_pinned = 0;
8136 cache->ro = 1;
8137 ret = 0;
8138 }
Chris Mason65e53412010-12-24 06:41:52 -05008139
Yan, Zhengf0486c62010-05-16 10:46:25 -04008140 spin_unlock(&cache->lock);
8141 spin_unlock(&sinfo->lock);
8142 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04008143}
8144
Yan, Zhengf0486c62010-05-16 10:46:25 -04008145int btrfs_set_block_group_ro(struct btrfs_root *root,
8146 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008147
8148{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008149 struct btrfs_trans_handle *trans;
8150 u64 alloc_flags;
8151 int ret;
8152
8153 BUG_ON(cache->ro);
8154
8155 trans = btrfs_join_transaction(root, 1);
8156 BUG_ON(IS_ERR(trans));
8157
8158 alloc_flags = update_block_group_flags(root, cache->flags);
8159 if (alloc_flags != cache->flags)
Chris Mason0e4f8f82011-04-15 16:05:44 -04008160 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8161 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008162
8163 ret = set_block_group_ro(cache);
8164 if (!ret)
8165 goto out;
8166 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Chris Mason0e4f8f82011-04-15 16:05:44 -04008167 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8168 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008169 if (ret < 0)
8170 goto out;
8171 ret = set_block_group_ro(cache);
8172out:
8173 btrfs_end_transaction(trans, root);
8174 return ret;
8175}
8176
Chris Masonc87f08c2011-02-16 13:57:04 -05008177int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8178 struct btrfs_root *root, u64 type)
8179{
8180 u64 alloc_flags = get_alloc_profile(root, type);
Chris Mason0e4f8f82011-04-15 16:05:44 -04008181 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8182 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05008183}
8184
Miao Xie6d07bce2011-01-05 10:07:31 +00008185/*
8186 * helper to account the unused space of all the readonly block group in the
8187 * list. takes mirrors into account.
8188 */
8189static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8190{
8191 struct btrfs_block_group_cache *block_group;
8192 u64 free_bytes = 0;
8193 int factor;
8194
8195 list_for_each_entry(block_group, groups_list, list) {
8196 spin_lock(&block_group->lock);
8197
8198 if (!block_group->ro) {
8199 spin_unlock(&block_group->lock);
8200 continue;
8201 }
8202
8203 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8204 BTRFS_BLOCK_GROUP_RAID10 |
8205 BTRFS_BLOCK_GROUP_DUP))
8206 factor = 2;
8207 else
8208 factor = 1;
8209
8210 free_bytes += (block_group->key.offset -
8211 btrfs_block_group_used(&block_group->item)) *
8212 factor;
8213
8214 spin_unlock(&block_group->lock);
8215 }
8216
8217 return free_bytes;
8218}
8219
8220/*
8221 * helper to account the unused space of all the readonly block group in the
8222 * space_info. takes mirrors into account.
8223 */
8224u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8225{
8226 int i;
8227 u64 free_bytes = 0;
8228
8229 spin_lock(&sinfo->lock);
8230
8231 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
8232 if (!list_empty(&sinfo->block_groups[i]))
8233 free_bytes += __btrfs_get_ro_block_group_free_space(
8234 &sinfo->block_groups[i]);
8235
8236 spin_unlock(&sinfo->lock);
8237
8238 return free_bytes;
8239}
8240
Yan, Zhengf0486c62010-05-16 10:46:25 -04008241int btrfs_set_block_group_rw(struct btrfs_root *root,
8242 struct btrfs_block_group_cache *cache)
8243{
8244 struct btrfs_space_info *sinfo = cache->space_info;
8245 u64 num_bytes;
8246
8247 BUG_ON(!cache->ro);
8248
8249 spin_lock(&sinfo->lock);
8250 spin_lock(&cache->lock);
8251 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8252 cache->bytes_super - btrfs_block_group_used(&cache->item);
8253 sinfo->bytes_readonly -= num_bytes;
8254 cache->ro = 0;
8255 spin_unlock(&cache->lock);
8256 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008257 return 0;
8258}
8259
Josef Bacikba1bf482009-09-11 16:11:19 -04008260/*
8261 * checks to see if its even possible to relocate this block group.
8262 *
8263 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8264 * ok to go ahead and try.
8265 */
8266int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008267{
Zheng Yan1a40e232008-09-26 10:09:34 -04008268 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008269 struct btrfs_space_info *space_info;
8270 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8271 struct btrfs_device *device;
8272 int full = 0;
8273 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008274
Josef Bacikba1bf482009-09-11 16:11:19 -04008275 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008276
Josef Bacikba1bf482009-09-11 16:11:19 -04008277 /* odd, couldn't find the block group, leave it alone */
8278 if (!block_group)
8279 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008280
Josef Bacikba1bf482009-09-11 16:11:19 -04008281 /* no bytes used, we're good */
8282 if (!btrfs_block_group_used(&block_group->item))
8283 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008284
Josef Bacikba1bf482009-09-11 16:11:19 -04008285 space_info = block_group->space_info;
8286 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008287
Josef Bacikba1bf482009-09-11 16:11:19 -04008288 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008289
Josef Bacikba1bf482009-09-11 16:11:19 -04008290 /*
8291 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008292 * relocate it unless we're able to allocate a new chunk below.
8293 *
8294 * Otherwise, we need to make sure we have room in the space to handle
8295 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008296 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008297 if ((space_info->total_bytes != block_group->key.offset) &&
8298 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04008299 space_info->bytes_pinned + space_info->bytes_readonly +
8300 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04008301 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008302 spin_unlock(&space_info->lock);
8303 goto out;
8304 }
8305 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008306
Josef Bacikba1bf482009-09-11 16:11:19 -04008307 /*
8308 * ok we don't have enough space, but maybe we have free space on our
8309 * devices to allocate new chunks for relocation, so loop through our
8310 * alloc devices and guess if we have enough space. However, if we
8311 * were marked as full, then we know there aren't enough chunks, and we
8312 * can just return.
8313 */
8314 ret = -1;
8315 if (full)
8316 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05008317
Josef Bacikba1bf482009-09-11 16:11:19 -04008318 mutex_lock(&root->fs_info->chunk_mutex);
8319 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8320 u64 min_free = btrfs_block_group_used(&block_group->item);
Miao Xie7bfc8372011-01-05 10:07:26 +00008321 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008322
Josef Bacikba1bf482009-09-11 16:11:19 -04008323 /*
8324 * check to make sure we can actually find a chunk with enough
8325 * space to fit our block group in.
8326 */
8327 if (device->total_bytes > device->bytes_used + min_free) {
8328 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008329 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008330 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05008331 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04008332 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008333 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008334 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008335 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05008336out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008337 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008338 return ret;
8339}
8340
Christoph Hellwigb2950862008-12-02 09:54:17 -05008341static int find_first_block_group(struct btrfs_root *root,
8342 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008343{
Chris Mason925baed2008-06-25 16:01:30 -04008344 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008345 struct btrfs_key found_key;
8346 struct extent_buffer *leaf;
8347 int slot;
8348
8349 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8350 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008351 goto out;
8352
Chris Masond3977122009-01-05 21:25:51 -05008353 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008354 slot = path->slots[0];
8355 leaf = path->nodes[0];
8356 if (slot >= btrfs_header_nritems(leaf)) {
8357 ret = btrfs_next_leaf(root, path);
8358 if (ret == 0)
8359 continue;
8360 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008361 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008362 break;
8363 }
8364 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8365
8366 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008367 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8368 ret = 0;
8369 goto out;
8370 }
Chris Mason0b86a832008-03-24 15:01:56 -04008371 path->slots[0]++;
8372 }
Chris Mason925baed2008-06-25 16:01:30 -04008373out:
Chris Mason0b86a832008-03-24 15:01:56 -04008374 return ret;
8375}
8376
Josef Bacik0af3d002010-06-21 14:48:16 -04008377void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8378{
8379 struct btrfs_block_group_cache *block_group;
8380 u64 last = 0;
8381
8382 while (1) {
8383 struct inode *inode;
8384
8385 block_group = btrfs_lookup_first_block_group(info, last);
8386 while (block_group) {
8387 spin_lock(&block_group->lock);
8388 if (block_group->iref)
8389 break;
8390 spin_unlock(&block_group->lock);
8391 block_group = next_block_group(info->tree_root,
8392 block_group);
8393 }
8394 if (!block_group) {
8395 if (last == 0)
8396 break;
8397 last = 0;
8398 continue;
8399 }
8400
8401 inode = block_group->inode;
8402 block_group->iref = 0;
8403 block_group->inode = NULL;
8404 spin_unlock(&block_group->lock);
8405 iput(inode);
8406 last = block_group->key.objectid + block_group->key.offset;
8407 btrfs_put_block_group(block_group);
8408 }
8409}
8410
Zheng Yan1a40e232008-09-26 10:09:34 -04008411int btrfs_free_block_groups(struct btrfs_fs_info *info)
8412{
8413 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008414 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008415 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008416 struct rb_node *n;
8417
Yan Zheng11833d62009-09-11 16:11:19 -04008418 down_write(&info->extent_commit_sem);
8419 while (!list_empty(&info->caching_block_groups)) {
8420 caching_ctl = list_entry(info->caching_block_groups.next,
8421 struct btrfs_caching_control, list);
8422 list_del(&caching_ctl->list);
8423 put_caching_control(caching_ctl);
8424 }
8425 up_write(&info->extent_commit_sem);
8426
Zheng Yan1a40e232008-09-26 10:09:34 -04008427 spin_lock(&info->block_group_cache_lock);
8428 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8429 block_group = rb_entry(n, struct btrfs_block_group_cache,
8430 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008431 rb_erase(&block_group->cache_node,
8432 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008433 spin_unlock(&info->block_group_cache_lock);
8434
Josef Bacik80eb2342008-10-29 14:49:05 -04008435 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008436 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008437 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008438
Josef Bacik817d52f2009-07-13 21:29:25 -04008439 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008440 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008441
Josef Bacik3c148742011-02-02 15:53:47 +00008442 /*
8443 * We haven't cached this block group, which means we could
8444 * possibly have excluded extents on this block group.
8445 */
8446 if (block_group->cached == BTRFS_CACHE_NO)
8447 free_excluded_extents(info->extent_root, block_group);
8448
Josef Bacik817d52f2009-07-13 21:29:25 -04008449 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008450 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008451
8452 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008453 }
8454 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008455
8456 /* now that all the block groups are freed, go through and
8457 * free all the space_info structs. This is only called during
8458 * the final stages of unmount, and so we know nobody is
8459 * using them. We call synchronize_rcu() once before we start,
8460 * just to be on the safe side.
8461 */
8462 synchronize_rcu();
8463
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008464 release_global_block_rsv(info);
8465
Chris Mason4184ea72009-03-10 12:39:20 -04008466 while(!list_empty(&info->space_info)) {
8467 space_info = list_entry(info->space_info.next,
8468 struct btrfs_space_info,
8469 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008470 if (space_info->bytes_pinned > 0 ||
8471 space_info->bytes_reserved > 0) {
8472 WARN_ON(1);
8473 dump_space_info(space_info, 0, 0);
8474 }
Chris Mason4184ea72009-03-10 12:39:20 -04008475 list_del(&space_info->list);
8476 kfree(space_info);
8477 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008478 return 0;
8479}
8480
Yan, Zhengb742bb82010-05-16 10:46:24 -04008481static void __link_block_group(struct btrfs_space_info *space_info,
8482 struct btrfs_block_group_cache *cache)
8483{
8484 int index = get_block_group_index(cache);
8485
8486 down_write(&space_info->groups_sem);
8487 list_add_tail(&cache->list, &space_info->block_groups[index]);
8488 up_write(&space_info->groups_sem);
8489}
8490
Chris Mason9078a3e2007-04-26 16:46:15 -04008491int btrfs_read_block_groups(struct btrfs_root *root)
8492{
8493 struct btrfs_path *path;
8494 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008495 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008496 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008497 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008498 struct btrfs_key key;
8499 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008500 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008501 int need_clear = 0;
8502 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008503
Chris Masonbe744172007-05-06 10:15:01 -04008504 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008505 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008506 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008507 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008508 path = btrfs_alloc_path();
8509 if (!path)
8510 return -ENOMEM;
8511
Josef Bacik0af3d002010-06-21 14:48:16 -04008512 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8513 if (cache_gen != 0 &&
8514 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8515 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008516 if (btrfs_test_opt(root, CLEAR_CACHE))
8517 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04008518 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
8519 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04008520
Chris Masond3977122009-01-05 21:25:51 -05008521 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008522 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008523 if (ret > 0)
8524 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008525 if (ret != 0)
8526 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008527 leaf = path->nodes[0];
8528 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008529 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008530 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008531 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008532 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008533 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008534
Yan Zhengd2fb3432008-12-11 16:30:39 -05008535 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008536 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008537 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008538 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008539 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008540 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008541
Josef Bacik0af3d002010-06-21 14:48:16 -04008542 if (need_clear)
8543 cache->disk_cache_state = BTRFS_DC_CLEAR;
8544
Josef Bacik96303082009-07-13 21:29:25 -04008545 /*
8546 * we only want to have 32k of ram per block group for keeping
8547 * track of free space, and if we pass 1/2 of that we want to
8548 * start converting things over to using bitmaps
8549 */
8550 cache->extents_thresh = ((1024 * 32) / 2) /
8551 sizeof(struct btrfs_free_space);
8552
Chris Mason5f39d392007-10-15 16:14:19 -04008553 read_extent_buffer(leaf, &cache->item,
8554 btrfs_item_ptr_offset(leaf, path->slots[0]),
8555 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008556 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008557
Chris Mason9078a3e2007-04-26 16:46:15 -04008558 key.objectid = found_key.objectid + found_key.offset;
8559 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04008560 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008561 cache->sectorsize = root->sectorsize;
8562
Josef Bacik817d52f2009-07-13 21:29:25 -04008563 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008564 * We need to exclude the super stripes now so that the space
8565 * info has super bytes accounted for, otherwise we'll think
8566 * we have more space than we actually do.
8567 */
8568 exclude_super_stripes(root, cache);
8569
8570 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008571 * check for two cases, either we are full, and therefore
8572 * don't need to bother with the caching work since we won't
8573 * find any space, or we are empty, and we can just add all
8574 * the space in and be done with it. This saves us _alot_ of
8575 * time, particularly in the full case.
8576 */
8577 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008578 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008579 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008580 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008581 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008582 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008583 cache->cached = BTRFS_CACHE_FINISHED;
8584 add_new_free_space(cache, root->fs_info,
8585 found_key.objectid,
8586 found_key.objectid +
8587 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008588 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008589 }
Chris Mason96b51792007-10-15 16:15:19 -04008590
Chris Mason6324fbf2008-03-24 15:01:59 -04008591 ret = update_space_info(info, cache->flags, found_key.offset,
8592 btrfs_block_group_used(&cache->item),
8593 &space_info);
8594 BUG_ON(ret);
8595 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008596 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008597 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008598 spin_unlock(&cache->space_info->lock);
8599
Yan, Zhengb742bb82010-05-16 10:46:24 -04008600 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008601
Josef Bacik0f9dd462008-09-23 13:14:11 -04008602 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8603 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04008604
8605 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008606 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04008607 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04008608 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008609
8610 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8611 if (!(get_alloc_profile(root, space_info->flags) &
8612 (BTRFS_BLOCK_GROUP_RAID10 |
8613 BTRFS_BLOCK_GROUP_RAID1 |
8614 BTRFS_BLOCK_GROUP_DUP)))
8615 continue;
8616 /*
8617 * avoid allocating from un-mirrored block group if there are
8618 * mirrored block groups.
8619 */
8620 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008621 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008622 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008623 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008624 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008625
8626 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008627 ret = 0;
8628error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008629 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008630 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008631}
Chris Mason6324fbf2008-03-24 15:01:59 -04008632
8633int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8634 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008635 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008636 u64 size)
8637{
8638 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008639 struct btrfs_root *extent_root;
8640 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008641
8642 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008643
Chris Mason12fcfd22009-03-24 10:24:20 -04008644 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008645
Chris Mason8f18cf12008-04-25 16:53:30 -04008646 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008647 if (!cache)
8648 return -ENOMEM;
8649
Chris Masone17cade2008-04-15 15:41:47 -04008650 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008651 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008652 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008653 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008654 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04008655
8656 /*
8657 * we only want to have 32k of ram per block group for keeping track
8658 * of free space, and if we pass 1/2 of that we want to start
8659 * converting things over to using bitmaps
8660 */
8661 cache->extents_thresh = ((1024 * 32) / 2) /
8662 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008663 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008664 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008665 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008666 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008667 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008668
Chris Mason6324fbf2008-03-24 15:01:59 -04008669 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008670 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8671 cache->flags = type;
8672 btrfs_set_block_group_flags(&cache->item, type);
8673
Yan Zheng11833d62009-09-11 16:11:19 -04008674 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008675 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04008676 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04008677
Josef Bacik817d52f2009-07-13 21:29:25 -04008678 add_new_free_space(cache, root->fs_info, chunk_offset,
8679 chunk_offset + size);
8680
Yan Zheng11833d62009-09-11 16:11:19 -04008681 free_excluded_extents(root, cache);
8682
Chris Mason6324fbf2008-03-24 15:01:59 -04008683 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8684 &cache->space_info);
8685 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04008686
8687 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008688 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008689 spin_unlock(&cache->space_info->lock);
8690
Yan, Zhengb742bb82010-05-16 10:46:24 -04008691 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008692
Josef Bacik0f9dd462008-09-23 13:14:11 -04008693 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8694 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04008695
Chris Mason6324fbf2008-03-24 15:01:59 -04008696 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8697 sizeof(cache->item));
8698 BUG_ON(ret);
8699
Chris Masond18a2c42008-04-04 15:40:00 -04008700 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008701
Chris Mason6324fbf2008-03-24 15:01:59 -04008702 return 0;
8703}
Zheng Yan1a40e232008-09-26 10:09:34 -04008704
8705int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8706 struct btrfs_root *root, u64 group_start)
8707{
8708 struct btrfs_path *path;
8709 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008710 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008711 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008712 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008713 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008714 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04008715 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008716
Zheng Yan1a40e232008-09-26 10:09:34 -04008717 root = root->fs_info->extent_root;
8718
8719 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8720 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008721 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008722
liubo9f7c43c2011-03-07 02:13:33 +00008723 /*
8724 * Free the reserved super bytes from this block group before
8725 * remove it.
8726 */
8727 free_excluded_extents(root, block_group);
8728
Zheng Yan1a40e232008-09-26 10:09:34 -04008729 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04008730 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8731 BTRFS_BLOCK_GROUP_RAID1 |
8732 BTRFS_BLOCK_GROUP_RAID10))
8733 factor = 2;
8734 else
8735 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008736
Chris Mason44fb5512009-06-04 15:34:51 -04008737 /* make sure this block group isn't part of an allocation cluster */
8738 cluster = &root->fs_info->data_alloc_cluster;
8739 spin_lock(&cluster->refill_lock);
8740 btrfs_return_cluster_to_free_space(block_group, cluster);
8741 spin_unlock(&cluster->refill_lock);
8742
8743 /*
8744 * make sure this block group isn't part of a metadata
8745 * allocation cluster
8746 */
8747 cluster = &root->fs_info->meta_alloc_cluster;
8748 spin_lock(&cluster->refill_lock);
8749 btrfs_return_cluster_to_free_space(block_group, cluster);
8750 spin_unlock(&cluster->refill_lock);
8751
Zheng Yan1a40e232008-09-26 10:09:34 -04008752 path = btrfs_alloc_path();
8753 BUG_ON(!path);
8754
Josef Bacik0af3d002010-06-21 14:48:16 -04008755 inode = lookup_free_space_inode(root, block_group, path);
8756 if (!IS_ERR(inode)) {
8757 btrfs_orphan_add(trans, inode);
8758 clear_nlink(inode);
8759 /* One for the block groups ref */
8760 spin_lock(&block_group->lock);
8761 if (block_group->iref) {
8762 block_group->iref = 0;
8763 block_group->inode = NULL;
8764 spin_unlock(&block_group->lock);
8765 iput(inode);
8766 } else {
8767 spin_unlock(&block_group->lock);
8768 }
8769 /* One for our lookup ref */
8770 iput(inode);
8771 }
8772
8773 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8774 key.offset = block_group->key.objectid;
8775 key.type = 0;
8776
8777 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8778 if (ret < 0)
8779 goto out;
8780 if (ret > 0)
8781 btrfs_release_path(tree_root, path);
8782 if (ret == 0) {
8783 ret = btrfs_del_item(trans, tree_root, path);
8784 if (ret)
8785 goto out;
8786 btrfs_release_path(tree_root, path);
8787 }
8788
Yan Zheng3dfdb932009-01-21 10:49:16 -05008789 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008790 rb_erase(&block_group->cache_node,
8791 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008792 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008793
Josef Bacik80eb2342008-10-29 14:49:05 -04008794 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008795 /*
8796 * we must use list_del_init so people can check to see if they
8797 * are still on the list after taking the semaphore
8798 */
8799 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008800 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008801
Josef Bacik817d52f2009-07-13 21:29:25 -04008802 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008803 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008804
8805 btrfs_remove_free_space_cache(block_group);
8806
Yan Zhengc146afa2008-11-12 14:34:12 -05008807 spin_lock(&block_group->space_info->lock);
8808 block_group->space_info->total_bytes -= block_group->key.offset;
8809 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008810 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008811 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008812
Josef Bacik0af3d002010-06-21 14:48:16 -04008813 memcpy(&key, &block_group->key, sizeof(key));
8814
Chris Mason283bb192009-07-24 16:30:55 -04008815 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008816
Chris Masonfa9c0d72009-04-03 09:47:43 -04008817 btrfs_put_block_group(block_group);
8818 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008819
8820 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8821 if (ret > 0)
8822 ret = -EIO;
8823 if (ret < 0)
8824 goto out;
8825
8826 ret = btrfs_del_item(trans, root, path);
8827out:
8828 btrfs_free_path(path);
8829 return ret;
8830}
liuboacce9522011-01-06 19:30:25 +08008831
liuboc59021f2011-03-07 02:13:14 +00008832int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8833{
8834 struct btrfs_space_info *space_info;
8835 int ret;
8836
8837 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0,
8838 &space_info);
8839 if (ret)
8840 return ret;
8841
8842 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0,
8843 &space_info);
8844 if (ret)
8845 return ret;
8846
8847 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0,
8848 &space_info);
8849 if (ret)
8850 return ret;
8851
8852 return ret;
8853}
8854
liuboacce9522011-01-06 19:30:25 +08008855int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8856{
8857 return unpin_extent_range(root, start, end);
8858}
8859
8860int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008861 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008862{
Li Dongyang5378e602011-03-24 10:24:27 +00008863 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008864}
Li Dongyangf7039b12011-03-24 10:24:28 +00008865
8866int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8867{
8868 struct btrfs_fs_info *fs_info = root->fs_info;
8869 struct btrfs_block_group_cache *cache = NULL;
8870 u64 group_trimmed;
8871 u64 start;
8872 u64 end;
8873 u64 trimmed = 0;
8874 int ret = 0;
8875
8876 cache = btrfs_lookup_block_group(fs_info, range->start);
8877
8878 while (cache) {
8879 if (cache->key.objectid >= (range->start + range->len)) {
8880 btrfs_put_block_group(cache);
8881 break;
8882 }
8883
8884 start = max(range->start, cache->key.objectid);
8885 end = min(range->start + range->len,
8886 cache->key.objectid + cache->key.offset);
8887
8888 if (end - start >= range->minlen) {
8889 if (!block_group_cache_done(cache)) {
8890 ret = cache_block_group(cache, NULL, root, 0);
8891 if (!ret)
8892 wait_block_group_cache_done(cache);
8893 }
8894 ret = btrfs_trim_block_group(cache,
8895 &group_trimmed,
8896 start,
8897 end,
8898 range->minlen);
8899
8900 trimmed += group_trimmed;
8901 if (ret) {
8902 btrfs_put_block_group(cache);
8903 break;
8904 }
8905 }
8906
8907 cache = next_block_group(fs_info->tree_root, cache);
8908 }
8909
8910 range->len = trimmed;
8911 return ret;
8912}