blob: a2ca561c70f040d540cb1af14e347a53df1d19a1 [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;
Josef Bacik6d741192011-04-11 20:20:11 -04003042 found->chunk_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003043 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003044 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04003045 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04003046 return 0;
3047}
3048
Chris Mason8790d502008-04-03 16:29:03 -04003049static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3050{
3051 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04003052 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04003053 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04003054 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04003055 if (extra_flags) {
3056 if (flags & BTRFS_BLOCK_GROUP_DATA)
3057 fs_info->avail_data_alloc_bits |= extra_flags;
3058 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3059 fs_info->avail_metadata_alloc_bits |= extra_flags;
3060 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3061 fs_info->avail_system_alloc_bits |= extra_flags;
3062 }
3063}
Chris Mason593060d2008-03-25 16:50:33 -04003064
Yan Zheng2b820322008-11-17 21:11:30 -05003065u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003066{
Chris Masoncd02dca2010-12-13 14:56:23 -05003067 /*
3068 * we add in the count of missing devices because we want
3069 * to make sure that any RAID levels on a degraded FS
3070 * continue to be honored.
3071 */
3072 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3073 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04003074
3075 if (num_devices == 1)
3076 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3077 if (num_devices < 4)
3078 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3079
Chris Masonec44a352008-04-28 15:29:52 -04003080 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3081 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003082 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003083 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003084 }
Chris Masonec44a352008-04-28 15:29:52 -04003085
3086 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003087 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003088 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003089 }
Chris Masonec44a352008-04-28 15:29:52 -04003090
3091 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3092 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3093 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3094 (flags & BTRFS_BLOCK_GROUP_DUP)))
3095 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3096 return flags;
3097}
3098
Yan, Zhengb742bb82010-05-16 10:46:24 -04003099static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003100{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003101 if (flags & BTRFS_BLOCK_GROUP_DATA)
3102 flags |= root->fs_info->avail_data_alloc_bits &
3103 root->fs_info->data_alloc_profile;
3104 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3105 flags |= root->fs_info->avail_system_alloc_bits &
3106 root->fs_info->system_alloc_profile;
3107 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3108 flags |= root->fs_info->avail_metadata_alloc_bits &
3109 root->fs_info->metadata_alloc_profile;
3110 return btrfs_reduce_alloc_profile(root, flags);
3111}
Josef Bacik6a632092009-02-20 11:00:09 -05003112
Miao Xie6d07bce2011-01-05 10:07:31 +00003113u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003114{
3115 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003116
Yan, Zhengb742bb82010-05-16 10:46:24 -04003117 if (data)
3118 flags = BTRFS_BLOCK_GROUP_DATA;
3119 else if (root == root->fs_info->chunk_root)
3120 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3121 else
3122 flags = BTRFS_BLOCK_GROUP_METADATA;
3123
3124 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003125}
3126
3127void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3128{
Josef Bacik6a632092009-02-20 11:00:09 -05003129 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003130 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003131}
3132
3133/*
3134 * This will check the space that the inode allocates from to make sure we have
3135 * enough space for bytes.
3136 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003137int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003138{
3139 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003140 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e2412010-03-19 14:38:13 +00003141 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003142 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003143
3144 /* make sure bytes are sectorsize aligned */
3145 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3146
Josef Bacik0af3d002010-06-21 14:48:16 -04003147 if (root == root->fs_info->tree_root) {
3148 alloc_chunk = 0;
3149 committed = 1;
3150 }
3151
Josef Bacik6a632092009-02-20 11:00:09 -05003152 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003153 if (!data_sinfo)
3154 goto alloc;
3155
Josef Bacik6a632092009-02-20 11:00:09 -05003156again:
3157 /* make sure we have enough space to handle the data first */
3158 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003159 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3160 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3161 data_sinfo->bytes_may_use;
Josef Bacikab6e2412010-03-19 14:38:13 +00003162
3163 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003164 struct btrfs_trans_handle *trans;
3165
Josef Bacik6a632092009-02-20 11:00:09 -05003166 /*
3167 * if we don't have enough free bytes in this space then we need
3168 * to alloc a new chunk.
3169 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003170 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003171 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003172
Chris Mason0e4f8f82011-04-15 16:05:44 -04003173 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003174 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003175alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003176 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003177 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003178 if (IS_ERR(trans))
3179 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003180
3181 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3182 bytes + 2 * 1024 * 1024,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003183 alloc_target,
3184 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003185 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003186 if (ret < 0) {
3187 if (ret != -ENOSPC)
3188 return ret;
3189 else
3190 goto commit_trans;
3191 }
Chris Mason33b4d472009-09-22 14:45:50 -04003192
3193 if (!data_sinfo) {
3194 btrfs_set_inode_space_info(root, inode);
3195 data_sinfo = BTRFS_I(inode)->space_info;
3196 }
Josef Bacik6a632092009-02-20 11:00:09 -05003197 goto again;
3198 }
3199 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003200
3201 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003202commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003203 if (!committed &&
3204 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003205 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003206 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003207 if (IS_ERR(trans))
3208 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003209 ret = btrfs_commit_transaction(trans, root);
3210 if (ret)
3211 return ret;
3212 goto again;
3213 }
3214
Chris Mason933b5852010-05-26 11:31:00 -04003215#if 0 /* I hope we never need this code again, just in case */
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003216 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3217 "%llu bytes_reserved, " "%llu bytes_pinned, "
3218 "%llu bytes_readonly, %llu may use %llu total\n",
3219 (unsigned long long)bytes,
Joel Becker21380932009-04-21 12:38:29 -07003220 (unsigned long long)data_sinfo->bytes_used,
3221 (unsigned long long)data_sinfo->bytes_reserved,
3222 (unsigned long long)data_sinfo->bytes_pinned,
3223 (unsigned long long)data_sinfo->bytes_readonly,
3224 (unsigned long long)data_sinfo->bytes_may_use,
3225 (unsigned long long)data_sinfo->total_bytes);
Chris Mason933b5852010-05-26 11:31:00 -04003226#endif
Josef Bacik6a632092009-02-20 11:00:09 -05003227 return -ENOSPC;
3228 }
3229 data_sinfo->bytes_may_use += bytes;
3230 BTRFS_I(inode)->reserved_bytes += bytes;
3231 spin_unlock(&data_sinfo->lock);
3232
Josef Bacik9ed74f22009-09-11 16:12:44 -04003233 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003234}
3235
3236/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003237 * called when we are clearing an delalloc extent from the
3238 * inode's io_tree or there was an error for whatever reason
3239 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003240 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003241void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003242{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003243 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003244 struct btrfs_space_info *data_sinfo;
3245
3246 /* make sure bytes are sectorsize aligned */
3247 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3248
3249 data_sinfo = BTRFS_I(inode)->space_info;
3250 spin_lock(&data_sinfo->lock);
3251 data_sinfo->bytes_may_use -= bytes;
3252 BTRFS_I(inode)->reserved_bytes -= bytes;
3253 spin_unlock(&data_sinfo->lock);
3254}
3255
Josef Bacik97e728d2009-04-21 17:40:57 -04003256static void force_metadata_allocation(struct btrfs_fs_info *info)
3257{
3258 struct list_head *head = &info->space_info;
3259 struct btrfs_space_info *found;
3260
3261 rcu_read_lock();
3262 list_for_each_entry_rcu(found, head, list) {
3263 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003264 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003265 }
3266 rcu_read_unlock();
3267}
3268
Chris Masone5bc2452010-10-26 13:37:56 -04003269static int should_alloc_chunk(struct btrfs_root *root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003270 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3271 int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003272{
3273 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003274 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003275 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003276
Chris Mason0e4f8f82011-04-15 16:05:44 -04003277 if (force == CHUNK_ALLOC_FORCE)
3278 return 1;
3279
3280 /*
3281 * in limited mode, we want to have some free space up to
3282 * about 1% of the FS size.
3283 */
3284 if (force == CHUNK_ALLOC_LIMITED) {
3285 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3286 thresh = max_t(u64, 64 * 1024 * 1024,
3287 div_factor_fine(thresh, 1));
3288
3289 if (num_bytes - num_allocated < thresh)
3290 return 1;
3291 }
3292
3293 /*
3294 * we have two similar checks here, one based on percentage
3295 * and once based on a hard number of 256MB. The idea
3296 * is that if we have a good amount of free
3297 * room, don't allocate a chunk. A good mount is
3298 * less than 80% utilized of the chunks we have allocated,
3299 * or more than 256MB free
3300 */
3301 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003302 return 0;
3303
Chris Mason0e4f8f82011-04-15 16:05:44 -04003304 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
Yan, Zheng424499d2010-05-16 10:46:25 -04003305 return 0;
3306
Chris Masone5bc2452010-10-26 13:37:56 -04003307 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003308
3309 /* 256MB or 5% of the FS */
Chris Masone5bc2452010-10-26 13:37:56 -04003310 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3311
3312 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003313 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003314 return 1;
3315}
3316
Chris Mason6324fbf2008-03-24 15:01:59 -04003317static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3318 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003319 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003320{
3321 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003322 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003323 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003324 int ret = 0;
3325
Yan Zheng2b820322008-11-17 21:11:30 -05003326 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003327
Chris Mason6324fbf2008-03-24 15:01:59 -04003328 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003329 if (!space_info) {
3330 ret = update_space_info(extent_root->fs_info, flags,
3331 0, 0, &space_info);
3332 BUG_ON(ret);
3333 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003334 BUG_ON(!space_info);
3335
Josef Bacik6d741192011-04-11 20:20:11 -04003336again:
Josef Bacik25179202008-10-29 14:49:05 -04003337 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003338 if (space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003339 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003340 if (space_info->full) {
3341 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003342 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003343 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003344
Chris Mason0e4f8f82011-04-15 16:05:44 -04003345 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003346 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003347 return 0;
3348 } else if (space_info->chunk_alloc) {
3349 wait_for_alloc = 1;
3350 } else {
3351 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003352 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003353
Josef Bacik25179202008-10-29 14:49:05 -04003354 spin_unlock(&space_info->lock);
3355
Josef Bacik6d741192011-04-11 20:20:11 -04003356 mutex_lock(&fs_info->chunk_mutex);
3357
3358 /*
3359 * The chunk_mutex is held throughout the entirety of a chunk
3360 * allocation, so once we've acquired the chunk_mutex we know that the
3361 * other guy is done and we need to recheck and see if we should
3362 * allocate.
3363 */
3364 if (wait_for_alloc) {
3365 mutex_unlock(&fs_info->chunk_mutex);
3366 wait_for_alloc = 0;
3367 goto again;
3368 }
3369
Josef Bacik97e728d2009-04-21 17:40:57 -04003370 /*
Josef Bacik67377732010-09-16 16:19:09 -04003371 * If we have mixed data/metadata chunks we want to make sure we keep
3372 * allocating mixed chunks instead of individual chunks.
3373 */
3374 if (btrfs_mixed_space_info(space_info))
3375 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3376
3377 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003378 * if we're doing a data chunk, go ahead and make sure that
3379 * we keep a reasonable number of metadata chunks allocated in the
3380 * FS as well.
3381 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003382 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003383 fs_info->data_chunk_allocations++;
3384 if (!(fs_info->data_chunk_allocations %
3385 fs_info->metadata_ratio))
3386 force_metadata_allocation(fs_info);
3387 }
3388
Yan Zheng2b820322008-11-17 21:11:30 -05003389 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003390 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003391 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003392 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003393 else
3394 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003395
Chris Mason0e4f8f82011-04-15 16:05:44 -04003396 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003397 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003398 spin_unlock(&space_info->lock);
Yan Zhengc146afa2008-11-12 14:34:12 -05003399 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003400 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003401}
3402
Yan, Zheng5da9d012010-05-16 10:46:25 -04003403/*
3404 * shrink metadata reservation for delalloc
3405 */
3406static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003407 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003408{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003409 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003410 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003411 u64 reserved;
3412 u64 max_reclaim;
3413 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003414 long time_left;
Chris Masonbf9022e2010-10-26 13:40:45 -04003415 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003416 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003417 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003418
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003419 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003420 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003421
3422 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003423 reserved = space_info->bytes_reserved;
Chris Mason36e39c42011-03-12 07:08:42 -05003424 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003425
3426 if (reserved == 0)
3427 return 0;
3428
3429 max_reclaim = min(reserved, to_reclaim);
3430
Josef Bacikb1953bc2011-01-21 21:10:01 +00003431 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003432 /* have the flusher threads jump in and do some IO */
3433 smp_mb();
3434 nr_pages = min_t(unsigned long, nr_pages,
3435 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3436 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003437
Josef Bacik0019f102010-10-15 15:18:40 -04003438 spin_lock(&space_info->lock);
Chris Mason36e39c42011-03-12 07:08:42 -05003439 if (reserved > space_info->bytes_reserved)
Josef Bacik0019f102010-10-15 15:18:40 -04003440 reclaimed += reserved - space_info->bytes_reserved;
3441 reserved = space_info->bytes_reserved;
3442 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003443
Chris Mason36e39c42011-03-12 07:08:42 -05003444 loops++;
3445
Yan, Zheng5da9d012010-05-16 10:46:25 -04003446 if (reserved == 0 || reclaimed >= max_reclaim)
3447 break;
3448
3449 if (trans && trans->transaction->blocked)
3450 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003451
Chris Mason36e39c42011-03-12 07:08:42 -05003452 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003453
3454 /* We were interrupted, exit */
3455 if (time_left)
3456 break;
3457
Chris Mason36e39c42011-03-12 07:08:42 -05003458 /* we've kicked the IO a few times, if anything has been freed,
3459 * exit. There is no sense in looping here for a long time
3460 * when we really need to commit the transaction, or there are
3461 * just too many writers without enough free space
3462 */
3463
3464 if (loops > 3) {
3465 smp_mb();
3466 if (progress != space_info->reservation_progress)
3467 break;
3468 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003469
Yan, Zheng5da9d012010-05-16 10:46:25 -04003470 }
3471 return reclaimed >= to_reclaim;
3472}
3473
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003474/*
3475 * Retries tells us how many times we've called reserve_metadata_bytes. The
3476 * idea is if this is the first call (retries == 0) then we will add to our
3477 * reserved count if we can't make the allocation in order to hold our place
3478 * while we go and try and free up space. That way for retries > 1 we don't try
3479 * and add space, we just check to see if the amount of unused space is >= the
3480 * total space, meaning that our reservation is valid.
3481 *
3482 * However if we don't intend to retry this reservation, pass -1 as retries so
3483 * that it short circuits this logic.
3484 */
3485static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3486 struct btrfs_root *root,
3487 struct btrfs_block_rsv *block_rsv,
3488 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003489{
3490 struct btrfs_space_info *space_info = block_rsv->space_info;
3491 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003492 u64 num_bytes = orig_bytes;
3493 int retries = 0;
3494 int ret = 0;
3495 bool reserved = false;
Josef Bacik38227932010-10-26 12:52:53 -04003496 bool committed = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003497
3498again:
3499 ret = -ENOSPC;
3500 if (reserved)
3501 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003502
3503 spin_lock(&space_info->lock);
3504 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003505 space_info->bytes_pinned + space_info->bytes_readonly +
3506 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003507
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003508 /*
3509 * The idea here is that we've not already over-reserved the block group
3510 * then we can go ahead and save our reservation first and then start
3511 * flushing if we need to. Otherwise if we've already overcommitted
3512 * lets start flushing stuff first and then come back and try to make
3513 * our reservation.
3514 */
3515 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003516 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003517 if (unused >= num_bytes) {
3518 if (!reserved)
3519 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003520 ret = 0;
3521 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003522 /*
3523 * Ok set num_bytes to orig_bytes since we aren't
3524 * overocmmitted, this way we only try and reclaim what
3525 * we need.
3526 */
3527 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003528 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003529 } else {
3530 /*
3531 * Ok we're over committed, set num_bytes to the overcommitted
3532 * amount plus the amount of bytes that we need for this
3533 * reservation.
3534 */
3535 num_bytes = unused - space_info->total_bytes +
3536 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003537 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003538
3539 /*
3540 * Couldn't make our reservation, save our place so while we're trying
3541 * to reclaim space we can actually use it instead of somebody else
3542 * stealing it from us.
3543 */
3544 if (ret && !reserved) {
3545 space_info->bytes_reserved += orig_bytes;
3546 reserved = true;
3547 }
3548
Yan, Zhengf0486c62010-05-16 10:46:25 -04003549 spin_unlock(&space_info->lock);
3550
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003551 if (!ret)
3552 return 0;
3553
3554 if (!flush)
3555 goto out;
3556
3557 /*
3558 * We do synchronous shrinking since we don't actually unreserve
3559 * metadata until after the IO is completed.
3560 */
3561 ret = shrink_delalloc(trans, root, num_bytes, 1);
3562 if (ret > 0)
3563 return 0;
3564 else if (ret < 0)
3565 goto out;
3566
3567 /*
3568 * So if we were overcommitted it's possible that somebody else flushed
3569 * out enough space and we simply didn't have enough space to reclaim,
3570 * so go back around and try again.
3571 */
3572 if (retries < 2) {
3573 retries++;
3574 goto again;
3575 }
3576
3577 spin_lock(&space_info->lock);
3578 /*
3579 * Not enough space to be reclaimed, don't bother committing the
3580 * transaction.
3581 */
3582 if (space_info->bytes_pinned < orig_bytes)
3583 ret = -ENOSPC;
3584 spin_unlock(&space_info->lock);
3585 if (ret)
3586 goto out;
3587
3588 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003589 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003590 goto out;
3591
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003592 ret = -ENOSPC;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003593 trans = btrfs_join_transaction(root);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003594 if (IS_ERR(trans))
3595 goto out;
3596 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003597 if (!ret) {
3598 trans = NULL;
3599 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003600 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003601 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003602
3603out:
3604 if (reserved) {
3605 spin_lock(&space_info->lock);
3606 space_info->bytes_reserved -= orig_bytes;
3607 spin_unlock(&space_info->lock);
3608 }
3609
Yan, Zhengf0486c62010-05-16 10:46:25 -04003610 return ret;
3611}
3612
3613static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3614 struct btrfs_root *root)
3615{
3616 struct btrfs_block_rsv *block_rsv;
3617 if (root->ref_cows)
3618 block_rsv = trans->block_rsv;
3619 else
3620 block_rsv = root->block_rsv;
3621
3622 if (!block_rsv)
3623 block_rsv = &root->fs_info->empty_block_rsv;
3624
3625 return block_rsv;
3626}
3627
3628static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3629 u64 num_bytes)
3630{
3631 int ret = -ENOSPC;
3632 spin_lock(&block_rsv->lock);
3633 if (block_rsv->reserved >= num_bytes) {
3634 block_rsv->reserved -= num_bytes;
3635 if (block_rsv->reserved < block_rsv->size)
3636 block_rsv->full = 0;
3637 ret = 0;
3638 }
3639 spin_unlock(&block_rsv->lock);
3640 return ret;
3641}
3642
3643static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3644 u64 num_bytes, int update_size)
3645{
3646 spin_lock(&block_rsv->lock);
3647 block_rsv->reserved += num_bytes;
3648 if (update_size)
3649 block_rsv->size += num_bytes;
3650 else if (block_rsv->reserved >= block_rsv->size)
3651 block_rsv->full = 1;
3652 spin_unlock(&block_rsv->lock);
3653}
3654
3655void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3656 struct btrfs_block_rsv *dest, u64 num_bytes)
3657{
3658 struct btrfs_space_info *space_info = block_rsv->space_info;
3659
3660 spin_lock(&block_rsv->lock);
3661 if (num_bytes == (u64)-1)
3662 num_bytes = block_rsv->size;
3663 block_rsv->size -= num_bytes;
3664 if (block_rsv->reserved >= block_rsv->size) {
3665 num_bytes = block_rsv->reserved - block_rsv->size;
3666 block_rsv->reserved = block_rsv->size;
3667 block_rsv->full = 1;
3668 } else {
3669 num_bytes = 0;
3670 }
3671 spin_unlock(&block_rsv->lock);
3672
3673 if (num_bytes > 0) {
3674 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003675 spin_lock(&dest->lock);
3676 if (!dest->full) {
3677 u64 bytes_to_add;
3678
3679 bytes_to_add = dest->size - dest->reserved;
3680 bytes_to_add = min(num_bytes, bytes_to_add);
3681 dest->reserved += bytes_to_add;
3682 if (dest->reserved >= dest->size)
3683 dest->full = 1;
3684 num_bytes -= bytes_to_add;
3685 }
3686 spin_unlock(&dest->lock);
3687 }
3688 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003689 spin_lock(&space_info->lock);
3690 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003691 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003692 spin_unlock(&space_info->lock);
3693 }
3694 }
3695}
3696
3697static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3698 struct btrfs_block_rsv *dst, u64 num_bytes)
3699{
3700 int ret;
3701
3702 ret = block_rsv_use_bytes(src, num_bytes);
3703 if (ret)
3704 return ret;
3705
3706 block_rsv_add_bytes(dst, num_bytes, 1);
3707 return 0;
3708}
3709
3710void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3711{
3712 memset(rsv, 0, sizeof(*rsv));
3713 spin_lock_init(&rsv->lock);
3714 atomic_set(&rsv->usage, 1);
3715 rsv->priority = 6;
3716 INIT_LIST_HEAD(&rsv->list);
3717}
3718
3719struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3720{
3721 struct btrfs_block_rsv *block_rsv;
3722 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003723
3724 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3725 if (!block_rsv)
3726 return NULL;
3727
3728 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003729 block_rsv->space_info = __find_space_info(fs_info,
3730 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003731 return block_rsv;
3732}
3733
3734void btrfs_free_block_rsv(struct btrfs_root *root,
3735 struct btrfs_block_rsv *rsv)
3736{
3737 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3738 btrfs_block_rsv_release(root, rsv, (u64)-1);
3739 if (!rsv->durable)
3740 kfree(rsv);
3741 }
3742}
3743
3744/*
3745 * make the block_rsv struct be able to capture freed space.
3746 * the captured space will re-add to the the block_rsv struct
3747 * after transaction commit
3748 */
3749void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3750 struct btrfs_block_rsv *block_rsv)
3751{
3752 block_rsv->durable = 1;
3753 mutex_lock(&fs_info->durable_block_rsv_mutex);
3754 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3755 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3756}
3757
3758int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3759 struct btrfs_root *root,
3760 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003761 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003762{
3763 int ret;
3764
3765 if (num_bytes == 0)
3766 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003767
3768 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003769 if (!ret) {
3770 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3771 return 0;
3772 }
3773
Yan, Zhengf0486c62010-05-16 10:46:25 -04003774 return ret;
3775}
3776
3777int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3778 struct btrfs_root *root,
3779 struct btrfs_block_rsv *block_rsv,
3780 u64 min_reserved, int min_factor)
3781{
3782 u64 num_bytes = 0;
3783 int commit_trans = 0;
3784 int ret = -ENOSPC;
3785
3786 if (!block_rsv)
3787 return 0;
3788
3789 spin_lock(&block_rsv->lock);
3790 if (min_factor > 0)
3791 num_bytes = div_factor(block_rsv->size, min_factor);
3792 if (min_reserved > num_bytes)
3793 num_bytes = min_reserved;
3794
3795 if (block_rsv->reserved >= num_bytes) {
3796 ret = 0;
3797 } else {
3798 num_bytes -= block_rsv->reserved;
3799 if (block_rsv->durable &&
3800 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3801 commit_trans = 1;
3802 }
3803 spin_unlock(&block_rsv->lock);
3804 if (!ret)
3805 return 0;
3806
3807 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003808 ret = reserve_metadata_bytes(trans, root, block_rsv,
3809 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003810 if (!ret) {
3811 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3812 return 0;
3813 }
3814 }
3815
3816 if (commit_trans) {
3817 if (trans)
3818 return -EAGAIN;
3819
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003820 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003821 BUG_ON(IS_ERR(trans));
3822 ret = btrfs_commit_transaction(trans, root);
3823 return 0;
3824 }
3825
Yan, Zhengf0486c62010-05-16 10:46:25 -04003826 return -ENOSPC;
3827}
3828
3829int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3830 struct btrfs_block_rsv *dst_rsv,
3831 u64 num_bytes)
3832{
3833 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3834}
3835
3836void btrfs_block_rsv_release(struct btrfs_root *root,
3837 struct btrfs_block_rsv *block_rsv,
3838 u64 num_bytes)
3839{
3840 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3841 if (global_rsv->full || global_rsv == block_rsv ||
3842 block_rsv->space_info != global_rsv->space_info)
3843 global_rsv = NULL;
3844 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3845}
3846
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003847/*
3848 * helper to calculate size of global block reservation.
3849 * the desired value is sum of space used by extent tree,
3850 * checksum tree and root tree
3851 */
3852static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3853{
3854 struct btrfs_space_info *sinfo;
3855 u64 num_bytes;
3856 u64 meta_used;
3857 u64 data_used;
3858 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3859#if 0
3860 /*
3861 * per tree used space accounting can be inaccuracy, so we
3862 * can't rely on it.
3863 */
3864 spin_lock(&fs_info->extent_root->accounting_lock);
3865 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3866 spin_unlock(&fs_info->extent_root->accounting_lock);
3867
3868 spin_lock(&fs_info->csum_root->accounting_lock);
3869 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3870 spin_unlock(&fs_info->csum_root->accounting_lock);
3871
3872 spin_lock(&fs_info->tree_root->accounting_lock);
3873 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3874 spin_unlock(&fs_info->tree_root->accounting_lock);
3875#endif
3876 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3877 spin_lock(&sinfo->lock);
3878 data_used = sinfo->bytes_used;
3879 spin_unlock(&sinfo->lock);
3880
3881 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3882 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003883 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3884 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003885 meta_used = sinfo->bytes_used;
3886 spin_unlock(&sinfo->lock);
3887
3888 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3889 csum_size * 2;
3890 num_bytes += div64_u64(data_used + meta_used, 50);
3891
3892 if (num_bytes * 3 > meta_used)
3893 num_bytes = div64_u64(meta_used, 3);
3894
3895 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3896}
3897
3898static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3899{
3900 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3901 struct btrfs_space_info *sinfo = block_rsv->space_info;
3902 u64 num_bytes;
3903
3904 num_bytes = calc_global_metadata_size(fs_info);
3905
3906 spin_lock(&block_rsv->lock);
3907 spin_lock(&sinfo->lock);
3908
3909 block_rsv->size = num_bytes;
3910
3911 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003912 sinfo->bytes_reserved + sinfo->bytes_readonly +
3913 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003914
3915 if (sinfo->total_bytes > num_bytes) {
3916 num_bytes = sinfo->total_bytes - num_bytes;
3917 block_rsv->reserved += num_bytes;
3918 sinfo->bytes_reserved += num_bytes;
3919 }
3920
3921 if (block_rsv->reserved >= block_rsv->size) {
3922 num_bytes = block_rsv->reserved - block_rsv->size;
3923 sinfo->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003924 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003925 block_rsv->reserved = block_rsv->size;
3926 block_rsv->full = 1;
3927 }
3928#if 0
3929 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3930 block_rsv->size, block_rsv->reserved);
3931#endif
3932 spin_unlock(&sinfo->lock);
3933 spin_unlock(&block_rsv->lock);
3934}
3935
Yan, Zhengf0486c62010-05-16 10:46:25 -04003936static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3937{
3938 struct btrfs_space_info *space_info;
3939
3940 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3941 fs_info->chunk_block_rsv.space_info = space_info;
3942 fs_info->chunk_block_rsv.priority = 10;
3943
3944 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003945 fs_info->global_block_rsv.space_info = space_info;
3946 fs_info->global_block_rsv.priority = 10;
3947 fs_info->global_block_rsv.refill_used = 1;
3948 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003949 fs_info->trans_block_rsv.space_info = space_info;
3950 fs_info->empty_block_rsv.space_info = space_info;
3951 fs_info->empty_block_rsv.priority = 10;
3952
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003953 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3954 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3955 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3956 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003957 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003958
3959 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3960
3961 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3962
3963 update_global_block_rsv(fs_info);
3964}
3965
3966static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3967{
3968 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3969 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3970 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3971 WARN_ON(fs_info->trans_block_rsv.size > 0);
3972 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3973 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3974 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003975}
3976
Yan, Zhenga22285a2010-05-16 10:48:46 -04003977static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3978{
3979 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3980 3 * num_items;
3981}
3982
Josef Bacikfcb80c22011-05-03 10:40:22 -04003983int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans,
3984 struct btrfs_root *root,
3985 struct btrfs_block_rsv *rsv)
3986{
3987 struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv;
3988 u64 num_bytes;
3989 int ret;
3990
3991 /*
3992 * Truncate should be freeing data, but give us 2 items just in case it
3993 * needs to use some space. We may want to be smarter about this in the
3994 * future.
3995 */
3996 num_bytes = calc_trans_metadata_size(root, 2);
3997
3998 /* We already have enough bytes, just return */
3999 if (rsv->reserved >= num_bytes)
4000 return 0;
4001
4002 num_bytes -= rsv->reserved;
4003
4004 /*
4005 * You should have reserved enough space before hand to do this, so this
4006 * should not fail.
4007 */
4008 ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes);
4009 BUG_ON(ret);
4010
4011 return 0;
4012}
4013
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
4015 struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004016 int num_items)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004017{
4018 u64 num_bytes;
4019 int ret;
4020
4021 if (num_items == 0 || root->fs_info->chunk_root == root)
4022 return 0;
4023
4024 num_bytes = calc_trans_metadata_size(root, num_items);
4025 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004026 num_bytes);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004027 if (!ret) {
4028 trans->bytes_reserved += num_bytes;
4029 trans->block_rsv = &root->fs_info->trans_block_rsv;
4030 }
4031 return ret;
4032}
4033
4034void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4035 struct btrfs_root *root)
4036{
4037 if (!trans->bytes_reserved)
4038 return;
4039
4040 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
4041 btrfs_block_rsv_release(root, trans->block_rsv,
4042 trans->bytes_reserved);
4043 trans->bytes_reserved = 0;
4044}
4045
Yan, Zhengd68fc572010-05-16 10:49:58 -04004046int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4047 struct inode *inode)
4048{
4049 struct btrfs_root *root = BTRFS_I(inode)->root;
4050 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4051 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4052
4053 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004054 * We need to hold space in order to delete our orphan item once we've
4055 * added it, so this takes the reservation so we can release it later
4056 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004057 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04004058 u64 num_bytes = calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004059 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4060}
4061
4062void btrfs_orphan_release_metadata(struct inode *inode)
4063{
4064 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04004065 u64 num_bytes = calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004066 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4067}
4068
Yan, Zhenga22285a2010-05-16 10:48:46 -04004069int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4070 struct btrfs_pending_snapshot *pending)
4071{
4072 struct btrfs_root *root = pending->root;
4073 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4074 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4075 /*
4076 * two for root back/forward refs, two for directory entries
4077 * and one for root of the snapshot.
4078 */
4079 u64 num_bytes = calc_trans_metadata_size(root, 5);
4080 dst_rsv->space_info = src_rsv->space_info;
4081 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4082}
4083
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004084static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
4085{
4086 return num_bytes >>= 3;
4087}
4088
4089int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4090{
4091 struct btrfs_root *root = BTRFS_I(inode)->root;
4092 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
4093 u64 to_reserve;
4094 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004095 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004096 int ret;
4097
4098 if (btrfs_transaction_in_commit(root->fs_info))
4099 schedule_timeout(1);
4100
4101 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004102
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004103 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004104 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4105
4106 if (nr_extents > reserved_extents) {
4107 nr_extents -= reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004108 to_reserve = calc_trans_metadata_size(root, nr_extents);
4109 } else {
4110 nr_extents = 0;
4111 to_reserve = 0;
4112 }
Josef Bacik57a45ced2011-01-25 16:30:38 -05004113
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004114 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004115 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
4116 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004117 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004118
Josef Bacik57a45ced2011-01-25 16:30:38 -05004119 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004120 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004121
4122 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4123
4124 if (block_rsv->size > 512 * 1024 * 1024)
Josef Bacik0019f102010-10-15 15:18:40 -04004125 shrink_delalloc(NULL, root, to_reserve, 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004126
4127 return 0;
4128}
4129
4130void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4131{
4132 struct btrfs_root *root = BTRFS_I(inode)->root;
4133 u64 to_free;
4134 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004135 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004136
4137 num_bytes = ALIGN(num_bytes, root->sectorsize);
4138 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik3c148742011-02-02 15:53:47 +00004139 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004140
Josef Bacik57a45ced2011-01-25 16:30:38 -05004141 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4142 do {
4143 int old, new;
4144
4145 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4146 if (nr_extents >= reserved_extents) {
4147 nr_extents = 0;
4148 break;
4149 }
4150 old = reserved_extents;
4151 nr_extents = reserved_extents - nr_extents;
4152 new = reserved_extents - nr_extents;
4153 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
4154 reserved_extents, new);
4155 if (likely(old == reserved_extents))
4156 break;
4157 reserved_extents = old;
4158 } while (1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004159
4160 to_free = calc_csum_metadata_size(inode, num_bytes);
4161 if (nr_extents > 0)
4162 to_free += calc_trans_metadata_size(root, nr_extents);
4163
4164 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4165 to_free);
4166}
4167
4168int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4169{
4170 int ret;
4171
4172 ret = btrfs_check_data_free_space(inode, num_bytes);
4173 if (ret)
4174 return ret;
4175
4176 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4177 if (ret) {
4178 btrfs_free_reserved_data_space(inode, num_bytes);
4179 return ret;
4180 }
4181
4182 return 0;
4183}
4184
4185void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4186{
4187 btrfs_delalloc_release_metadata(inode, num_bytes);
4188 btrfs_free_reserved_data_space(inode, num_bytes);
4189}
4190
Chris Mason9078a3e2007-04-26 16:46:15 -04004191static int update_block_group(struct btrfs_trans_handle *trans,
4192 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004193 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004194{
Josef Bacik0af3d002010-06-21 14:48:16 -04004195 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004196 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004197 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004198 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004199 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004200 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004201
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004202 /* block accounting for super block */
4203 spin_lock(&info->delalloc_lock);
4204 old_val = btrfs_super_bytes_used(&info->super_copy);
4205 if (alloc)
4206 old_val += num_bytes;
4207 else
4208 old_val -= num_bytes;
4209 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004210 spin_unlock(&info->delalloc_lock);
4211
Chris Masond3977122009-01-05 21:25:51 -05004212 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004213 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004214 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004215 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004216 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4217 BTRFS_BLOCK_GROUP_RAID1 |
4218 BTRFS_BLOCK_GROUP_RAID10))
4219 factor = 2;
4220 else
4221 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004222 /*
4223 * If this block group has free space cache written out, we
4224 * need to make sure to load it if we are removing space. This
4225 * is because we need the unpinning stage to actually add the
4226 * space back to the block group, otherwise we will leak space.
4227 */
4228 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004229 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004230
Chris Masondb945352007-10-15 16:15:53 -04004231 byte_in_group = bytenr - cache->key.objectid;
4232 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004233
Josef Bacik25179202008-10-29 14:49:05 -04004234 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004235 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004236
4237 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4238 cache->disk_cache_state < BTRFS_DC_CLEAR)
4239 cache->disk_cache_state = BTRFS_DC_CLEAR;
4240
Josef Bacik0f9dd462008-09-23 13:14:11 -04004241 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004242 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004243 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004244 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004245 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004246 btrfs_set_block_group_used(&cache->item, old_val);
4247 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004248 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004249 cache->space_info->reservation_progress++;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004250 cache->space_info->bytes_used += num_bytes;
4251 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004252 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004253 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004254 } else {
Chris Masondb945352007-10-15 16:15:53 -04004255 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004256 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004257 cache->pinned += num_bytes;
4258 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004259 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004260 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004261 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004262 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004263
Yan, Zhengf0486c62010-05-16 10:46:25 -04004264 set_extent_dirty(info->pinned_extents,
4265 bytenr, bytenr + num_bytes - 1,
4266 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004267 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004268 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004269 total -= num_bytes;
4270 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004271 }
4272 return 0;
4273}
Chris Mason6324fbf2008-03-24 15:01:59 -04004274
Chris Masona061fc82008-05-07 11:43:44 -04004275static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4276{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004277 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004278 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004279
4280 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4281 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004282 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004283
Yan Zhengd2fb3432008-12-11 16:30:39 -05004284 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004285 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004286
4287 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004288}
4289
Yan, Zhengf0486c62010-05-16 10:46:25 -04004290static int pin_down_extent(struct btrfs_root *root,
4291 struct btrfs_block_group_cache *cache,
4292 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004293{
Yan Zheng11833d62009-09-11 16:11:19 -04004294 spin_lock(&cache->space_info->lock);
4295 spin_lock(&cache->lock);
4296 cache->pinned += num_bytes;
4297 cache->space_info->bytes_pinned += num_bytes;
4298 if (reserved) {
4299 cache->reserved -= num_bytes;
4300 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004301 cache->space_info->reservation_progress++;
Yan324ae4d2007-11-16 14:57:08 -05004302 }
Yan Zheng11833d62009-09-11 16:11:19 -04004303 spin_unlock(&cache->lock);
4304 spin_unlock(&cache->space_info->lock);
4305
Yan, Zhengf0486c62010-05-16 10:46:25 -04004306 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4307 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004308 return 0;
4309}
Chris Mason9078a3e2007-04-26 16:46:15 -04004310
Yan, Zhengf0486c62010-05-16 10:46:25 -04004311/*
4312 * this function must be called within transaction
4313 */
4314int btrfs_pin_extent(struct btrfs_root *root,
4315 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004316{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004317 struct btrfs_block_group_cache *cache;
4318
4319 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4320 BUG_ON(!cache);
4321
4322 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4323
4324 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004325 return 0;
4326}
Zheng Yane8569812008-09-26 10:05:48 -04004327
Yan, Zhengf0486c62010-05-16 10:46:25 -04004328/*
4329 * update size of reserved extents. this function may return -EAGAIN
4330 * if 'reserve' is true or 'sinfo' is false.
4331 */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004332int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4333 u64 num_bytes, int reserve, int sinfo)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004334{
4335 int ret = 0;
4336 if (sinfo) {
4337 struct btrfs_space_info *space_info = cache->space_info;
4338 spin_lock(&space_info->lock);
4339 spin_lock(&cache->lock);
4340 if (reserve) {
4341 if (cache->ro) {
4342 ret = -EAGAIN;
4343 } else {
4344 cache->reserved += num_bytes;
4345 space_info->bytes_reserved += num_bytes;
4346 }
4347 } else {
4348 if (cache->ro)
4349 space_info->bytes_readonly += num_bytes;
4350 cache->reserved -= num_bytes;
4351 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004352 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004353 }
4354 spin_unlock(&cache->lock);
4355 spin_unlock(&space_info->lock);
4356 } else {
4357 spin_lock(&cache->lock);
4358 if (cache->ro) {
4359 ret = -EAGAIN;
4360 } else {
4361 if (reserve)
4362 cache->reserved += num_bytes;
4363 else
4364 cache->reserved -= num_bytes;
4365 }
4366 spin_unlock(&cache->lock);
4367 }
4368 return ret;
4369}
4370
Yan Zheng11833d62009-09-11 16:11:19 -04004371int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4372 struct btrfs_root *root)
4373{
4374 struct btrfs_fs_info *fs_info = root->fs_info;
4375 struct btrfs_caching_control *next;
4376 struct btrfs_caching_control *caching_ctl;
4377 struct btrfs_block_group_cache *cache;
4378
4379 down_write(&fs_info->extent_commit_sem);
4380
4381 list_for_each_entry_safe(caching_ctl, next,
4382 &fs_info->caching_block_groups, list) {
4383 cache = caching_ctl->block_group;
4384 if (block_group_cache_done(cache)) {
4385 cache->last_byte_to_unpin = (u64)-1;
4386 list_del_init(&caching_ctl->list);
4387 put_caching_control(caching_ctl);
4388 } else {
4389 cache->last_byte_to_unpin = caching_ctl->progress;
4390 }
4391 }
4392
4393 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4394 fs_info->pinned_extents = &fs_info->freed_extents[1];
4395 else
4396 fs_info->pinned_extents = &fs_info->freed_extents[0];
4397
4398 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004399
4400 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004401 return 0;
4402}
4403
4404static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4405{
4406 struct btrfs_fs_info *fs_info = root->fs_info;
4407 struct btrfs_block_group_cache *cache = NULL;
4408 u64 len;
4409
4410 while (start <= end) {
4411 if (!cache ||
4412 start >= cache->key.objectid + cache->key.offset) {
4413 if (cache)
4414 btrfs_put_block_group(cache);
4415 cache = btrfs_lookup_block_group(fs_info, start);
4416 BUG_ON(!cache);
4417 }
4418
4419 len = cache->key.objectid + cache->key.offset - start;
4420 len = min(len, end + 1 - start);
4421
4422 if (start < cache->last_byte_to_unpin) {
4423 len = min(len, cache->last_byte_to_unpin - start);
4424 btrfs_add_free_space(cache, start, len);
4425 }
Josef Bacik25179202008-10-29 14:49:05 -04004426
Yan, Zhengf0486c62010-05-16 10:46:25 -04004427 start += len;
4428
Josef Bacik25179202008-10-29 14:49:05 -04004429 spin_lock(&cache->space_info->lock);
4430 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004431 cache->pinned -= len;
4432 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004433 if (cache->ro) {
4434 cache->space_info->bytes_readonly += len;
4435 } else if (cache->reserved_pinned > 0) {
4436 len = min(len, cache->reserved_pinned);
4437 cache->reserved_pinned -= len;
4438 cache->space_info->bytes_reserved += len;
4439 }
Josef Bacik25179202008-10-29 14:49:05 -04004440 spin_unlock(&cache->lock);
4441 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004442 }
4443
4444 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004445 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004446 return 0;
4447}
4448
4449int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004450 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004451{
Yan Zheng11833d62009-09-11 16:11:19 -04004452 struct btrfs_fs_info *fs_info = root->fs_info;
4453 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004454 struct btrfs_block_rsv *block_rsv;
4455 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004456 u64 start;
4457 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004458 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004459 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004460
Yan Zheng11833d62009-09-11 16:11:19 -04004461 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4462 unpin = &fs_info->freed_extents[1];
4463 else
4464 unpin = &fs_info->freed_extents[0];
4465
Chris Masond3977122009-01-05 21:25:51 -05004466 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004467 ret = find_first_extent_bit(unpin, 0, &start, &end,
4468 EXTENT_DIRTY);
4469 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004470 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004471
Li Dongyang5378e602011-03-24 10:24:27 +00004472 if (btrfs_test_opt(root, DISCARD))
4473 ret = btrfs_discard_extent(root, start,
4474 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004475
Chris Mason1a5bc162007-10-15 16:15:26 -04004476 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004477 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004478 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004479 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004480
Yan, Zhengf0486c62010-05-16 10:46:25 -04004481 mutex_lock(&fs_info->durable_block_rsv_mutex);
4482 list_for_each_entry_safe(block_rsv, next_rsv,
4483 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004484
Yan, Zhengf0486c62010-05-16 10:46:25 -04004485 idx = trans->transid & 0x1;
4486 if (block_rsv->freed[idx] > 0) {
4487 block_rsv_add_bytes(block_rsv,
4488 block_rsv->freed[idx], 0);
4489 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004490 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004491 if (atomic_read(&block_rsv->usage) == 0) {
4492 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004493
Yan, Zhengf0486c62010-05-16 10:46:25 -04004494 if (block_rsv->freed[0] == 0 &&
4495 block_rsv->freed[1] == 0) {
4496 list_del_init(&block_rsv->list);
4497 kfree(block_rsv);
4498 }
4499 } else {
4500 btrfs_block_rsv_release(root, block_rsv, 0);
4501 }
4502 }
4503 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4504
Chris Masone20d96d2007-03-22 12:13:20 -04004505 return 0;
4506}
4507
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004508static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4509 struct btrfs_root *root,
4510 u64 bytenr, u64 num_bytes, u64 parent,
4511 u64 root_objectid, u64 owner_objectid,
4512 u64 owner_offset, int refs_to_drop,
4513 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004514{
Chris Masone2fa7222007-03-12 16:22:34 -04004515 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004516 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004517 struct btrfs_fs_info *info = root->fs_info;
4518 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004519 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004520 struct btrfs_extent_item *ei;
4521 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004522 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004523 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004524 int extent_slot = 0;
4525 int found_extent = 0;
4526 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004527 u32 item_size;
4528 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004529
Chris Mason5caf2a02007-04-02 11:20:42 -04004530 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004531 if (!path)
4532 return -ENOMEM;
4533
Chris Mason3c12ac72008-04-21 12:01:38 -04004534 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004535 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004536
4537 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4538 BUG_ON(!is_data && refs_to_drop != 1);
4539
4540 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4541 bytenr, num_bytes, parent,
4542 root_objectid, owner_objectid,
4543 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004544 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004545 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004546 while (extent_slot >= 0) {
4547 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004548 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004549 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004550 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004551 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4552 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004553 found_extent = 1;
4554 break;
4555 }
4556 if (path->slots[0] - extent_slot > 5)
4557 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004558 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004559 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004560#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4561 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4562 if (found_extent && item_size < sizeof(*ei))
4563 found_extent = 0;
4564#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004565 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004566 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004567 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004568 NULL, refs_to_drop,
4569 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004570 BUG_ON(ret);
4571 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04004572 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004573
4574 key.objectid = bytenr;
4575 key.type = BTRFS_EXTENT_ITEM_KEY;
4576 key.offset = num_bytes;
4577
Zheng Yan31840ae2008-09-23 13:14:14 -04004578 ret = btrfs_search_slot(trans, extent_root,
4579 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004580 if (ret) {
4581 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004582 ", was looking for %llu\n", ret,
4583 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004584 btrfs_print_leaf(extent_root, path->nodes[0]);
4585 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004586 BUG_ON(ret);
4587 extent_slot = path->slots[0];
4588 }
Chris Mason7bb86312007-12-11 09:25:06 -05004589 } else {
4590 btrfs_print_leaf(extent_root, path->nodes[0]);
4591 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004592 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004593 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004594 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004595 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004596 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004597 (unsigned long long)owner_objectid,
4598 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004599 }
Chris Mason5f39d392007-10-15 16:14:19 -04004600
4601 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004602 item_size = btrfs_item_size_nr(leaf, extent_slot);
4603#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4604 if (item_size < sizeof(*ei)) {
4605 BUG_ON(found_extent || extent_slot != path->slots[0]);
4606 ret = convert_extent_item_v0(trans, extent_root, path,
4607 owner_objectid, 0);
4608 BUG_ON(ret < 0);
4609
4610 btrfs_release_path(extent_root, path);
4611 path->leave_spinning = 1;
4612
4613 key.objectid = bytenr;
4614 key.type = BTRFS_EXTENT_ITEM_KEY;
4615 key.offset = num_bytes;
4616
4617 ret = btrfs_search_slot(trans, extent_root, &key, path,
4618 -1, 1);
4619 if (ret) {
4620 printk(KERN_ERR "umm, got %d back from search"
4621 ", was looking for %llu\n", ret,
4622 (unsigned long long)bytenr);
4623 btrfs_print_leaf(extent_root, path->nodes[0]);
4624 }
4625 BUG_ON(ret);
4626 extent_slot = path->slots[0];
4627 leaf = path->nodes[0];
4628 item_size = btrfs_item_size_nr(leaf, extent_slot);
4629 }
4630#endif
4631 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004632 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004633 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004634 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4635 struct btrfs_tree_block_info *bi;
4636 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4637 bi = (struct btrfs_tree_block_info *)(ei + 1);
4638 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004639 }
4640
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004641 refs = btrfs_extent_refs(leaf, ei);
4642 BUG_ON(refs < refs_to_drop);
4643 refs -= refs_to_drop;
4644
4645 if (refs > 0) {
4646 if (extent_op)
4647 __run_delayed_extent_op(extent_op, leaf, ei);
4648 /*
4649 * In the case of inline back ref, reference count will
4650 * be updated by remove_extent_backref
4651 */
4652 if (iref) {
4653 BUG_ON(!found_extent);
4654 } else {
4655 btrfs_set_extent_refs(leaf, ei, refs);
4656 btrfs_mark_buffer_dirty(leaf);
4657 }
4658 if (found_extent) {
4659 ret = remove_extent_backref(trans, extent_root, path,
4660 iref, refs_to_drop,
4661 is_data);
4662 BUG_ON(ret);
4663 }
4664 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004665 if (found_extent) {
4666 BUG_ON(is_data && refs_to_drop !=
4667 extent_data_ref_count(root, path, iref));
4668 if (iref) {
4669 BUG_ON(path->slots[0] != extent_slot);
4670 } else {
4671 BUG_ON(path->slots[0] != extent_slot + 1);
4672 path->slots[0] = extent_slot;
4673 num_to_del = 2;
4674 }
Chris Mason78fae272007-03-25 11:35:08 -04004675 }
Chris Masonb9473432009-03-13 11:00:37 -04004676
Chris Mason952fcca2008-02-18 16:33:44 -05004677 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4678 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004679 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04004680 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01004681
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004682 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004683 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4684 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004685 } else {
4686 invalidate_mapping_pages(info->btree_inode->i_mapping,
4687 bytenr >> PAGE_CACHE_SHIFT,
4688 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004689 }
4690
Yan, Zhengf0486c62010-05-16 10:46:25 -04004691 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004692 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004693 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004694 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004695 return ret;
4696}
4697
4698/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004699 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004700 * delayed ref for that extent as well. This searches the delayed ref tree for
4701 * a given extent, and if there are no other delayed refs to be processed, it
4702 * removes it from the tree.
4703 */
4704static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4705 struct btrfs_root *root, u64 bytenr)
4706{
4707 struct btrfs_delayed_ref_head *head;
4708 struct btrfs_delayed_ref_root *delayed_refs;
4709 struct btrfs_delayed_ref_node *ref;
4710 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004711 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004712
4713 delayed_refs = &trans->transaction->delayed_refs;
4714 spin_lock(&delayed_refs->lock);
4715 head = btrfs_find_delayed_ref_head(trans, bytenr);
4716 if (!head)
4717 goto out;
4718
4719 node = rb_prev(&head->node.rb_node);
4720 if (!node)
4721 goto out;
4722
4723 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4724
4725 /* there are still entries for this ref, we can't drop it */
4726 if (ref->bytenr == bytenr)
4727 goto out;
4728
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004729 if (head->extent_op) {
4730 if (!head->must_insert_reserved)
4731 goto out;
4732 kfree(head->extent_op);
4733 head->extent_op = NULL;
4734 }
4735
Chris Mason1887be62009-03-13 10:11:24 -04004736 /*
4737 * waiting for the lock here would deadlock. If someone else has it
4738 * locked they are already in the process of dropping it anyway
4739 */
4740 if (!mutex_trylock(&head->mutex))
4741 goto out;
4742
4743 /*
4744 * at this point we have a head with no other entries. Go
4745 * ahead and process it.
4746 */
4747 head->node.in_tree = 0;
4748 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004749
Chris Mason1887be62009-03-13 10:11:24 -04004750 delayed_refs->num_entries--;
4751
4752 /*
4753 * we don't take a ref on the node because we're removing it from the
4754 * tree, so we just steal the ref the tree was holding.
4755 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004756 delayed_refs->num_heads--;
4757 if (list_empty(&head->cluster))
4758 delayed_refs->num_heads_ready--;
4759
4760 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004761 spin_unlock(&delayed_refs->lock);
4762
Yan, Zhengf0486c62010-05-16 10:46:25 -04004763 BUG_ON(head->extent_op);
4764 if (head->must_insert_reserved)
4765 ret = 1;
4766
4767 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004768 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004769 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004770out:
4771 spin_unlock(&delayed_refs->lock);
4772 return 0;
4773}
4774
Yan, Zhengf0486c62010-05-16 10:46:25 -04004775void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4776 struct btrfs_root *root,
4777 struct extent_buffer *buf,
4778 u64 parent, int last_ref)
4779{
4780 struct btrfs_block_rsv *block_rsv;
4781 struct btrfs_block_group_cache *cache = NULL;
4782 int ret;
4783
4784 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4785 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4786 parent, root->root_key.objectid,
4787 btrfs_header_level(buf),
4788 BTRFS_DROP_DELAYED_REF, NULL);
4789 BUG_ON(ret);
4790 }
4791
4792 if (!last_ref)
4793 return;
4794
4795 block_rsv = get_block_rsv(trans, root);
4796 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004797 if (block_rsv->space_info != cache->space_info)
4798 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004799
4800 if (btrfs_header_generation(buf) == trans->transid) {
4801 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4802 ret = check_ref_cleanup(trans, root, buf->start);
4803 if (!ret)
4804 goto pin;
4805 }
4806
4807 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4808 pin_down_extent(root, cache, buf->start, buf->len, 1);
4809 goto pin;
4810 }
4811
4812 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4813
4814 btrfs_add_free_space(cache, buf->start, buf->len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00004815 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004816 if (ret == -EAGAIN) {
4817 /* block group became read-only */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004818 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004819 goto out;
4820 }
4821
4822 ret = 1;
4823 spin_lock(&block_rsv->lock);
4824 if (block_rsv->reserved < block_rsv->size) {
4825 block_rsv->reserved += buf->len;
4826 ret = 0;
4827 }
4828 spin_unlock(&block_rsv->lock);
4829
4830 if (ret) {
4831 spin_lock(&cache->space_info->lock);
4832 cache->space_info->bytes_reserved -= buf->len;
Chris Mason36e39c42011-03-12 07:08:42 -05004833 cache->space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004834 spin_unlock(&cache->space_info->lock);
4835 }
4836 goto out;
4837 }
4838pin:
4839 if (block_rsv->durable && !cache->ro) {
4840 ret = 0;
4841 spin_lock(&cache->lock);
4842 if (!cache->ro) {
4843 cache->reserved_pinned += buf->len;
4844 ret = 1;
4845 }
4846 spin_unlock(&cache->lock);
4847
4848 if (ret) {
4849 spin_lock(&block_rsv->lock);
4850 block_rsv->freed[trans->transid & 0x1] += buf->len;
4851 spin_unlock(&block_rsv->lock);
4852 }
4853 }
4854out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004855 /*
4856 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4857 * anymore.
4858 */
4859 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004860 btrfs_put_block_group(cache);
4861}
4862
Chris Mason925baed2008-06-25 16:01:30 -04004863int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004864 struct btrfs_root *root,
4865 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004866 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004867{
4868 int ret;
4869
Chris Mason56bec292009-03-13 10:10:06 -04004870 /*
4871 * tree log blocks never actually go into the extent allocation
4872 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004873 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004874 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4875 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004876 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004877 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004878 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004879 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4880 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4881 parent, root_objectid, (int)owner,
4882 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004883 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004884 } else {
4885 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4886 parent, root_objectid, owner,
4887 offset, BTRFS_DROP_DELAYED_REF, NULL);
4888 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004889 }
Chris Mason925baed2008-06-25 16:01:30 -04004890 return ret;
4891}
4892
Chris Mason87ee04e2007-11-30 11:30:34 -05004893static u64 stripe_align(struct btrfs_root *root, u64 val)
4894{
4895 u64 mask = ((u64)root->stripesize - 1);
4896 u64 ret = (val + mask) & ~mask;
4897 return ret;
4898}
4899
Chris Masonfec577f2007-02-26 10:40:21 -05004900/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004901 * when we wait for progress in the block group caching, its because
4902 * our allocation attempt failed at least once. So, we must sleep
4903 * and let some progress happen before we try again.
4904 *
4905 * This function will sleep at least once waiting for new free space to
4906 * show up, and then it will check the block group free space numbers
4907 * for our min num_bytes. Another option is to have it go ahead
4908 * and look in the rbtree for a free extent of a given size, but this
4909 * is a good start.
4910 */
4911static noinline int
4912wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4913 u64 num_bytes)
4914{
Yan Zheng11833d62009-09-11 16:11:19 -04004915 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004916 DEFINE_WAIT(wait);
4917
Yan Zheng11833d62009-09-11 16:11:19 -04004918 caching_ctl = get_caching_control(cache);
4919 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004920 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004921
Yan Zheng11833d62009-09-11 16:11:19 -04004922 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004923 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004924
4925 put_caching_control(caching_ctl);
4926 return 0;
4927}
4928
4929static noinline int
4930wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4931{
4932 struct btrfs_caching_control *caching_ctl;
4933 DEFINE_WAIT(wait);
4934
4935 caching_ctl = get_caching_control(cache);
4936 if (!caching_ctl)
4937 return 0;
4938
4939 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4940
4941 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004942 return 0;
4943}
4944
Yan, Zhengb742bb82010-05-16 10:46:24 -04004945static int get_block_group_index(struct btrfs_block_group_cache *cache)
4946{
4947 int index;
4948 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4949 index = 0;
4950 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4951 index = 1;
4952 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4953 index = 2;
4954 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4955 index = 3;
4956 else
4957 index = 4;
4958 return index;
4959}
4960
Josef Bacik817d52f2009-07-13 21:29:25 -04004961enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004962 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004963 LOOP_CACHING_NOWAIT = 1,
4964 LOOP_CACHING_WAIT = 2,
4965 LOOP_ALLOC_CHUNK = 3,
4966 LOOP_NO_EMPTY_SIZE = 4,
4967};
4968
4969/*
Chris Masonfec577f2007-02-26 10:40:21 -05004970 * walks the btree of allocated extents and find a hole of a given size.
4971 * The key ins is changed to record the hole:
4972 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004973 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004974 * ins->offset == number of blocks
4975 * Any available blocks before search_start are skipped.
4976 */
Chris Masond3977122009-01-05 21:25:51 -05004977static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004978 struct btrfs_root *orig_root,
4979 u64 num_bytes, u64 empty_size,
4980 u64 search_start, u64 search_end,
4981 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004982 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004983{
Josef Bacik80eb2342008-10-29 14:49:05 -04004984 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004985 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004986 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004987 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004988 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004989 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004990 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004991 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004992 int last_ptr_loop = 0;
4993 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004994 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004995 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004996 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004997 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004998 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004999 u64 ideal_cache_percent = 0;
5000 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05005001
Chris Masondb945352007-10-15 16:15:53 -04005002 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005003 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005004 ins->objectid = 0;
5005 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005006
Josef Bacik2552d172009-04-03 10:14:19 -04005007 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005008 if (!space_info) {
5009 printk(KERN_ERR "No space info for %d\n", data);
5010 return -ENOSPC;
5011 }
Josef Bacik2552d172009-04-03 10:14:19 -04005012
Josef Bacik67377732010-09-16 16:19:09 -04005013 /*
5014 * If the space info is for both data and metadata it means we have a
5015 * small filesystem and we can't use the clustering stuff.
5016 */
5017 if (btrfs_mixed_space_info(space_info))
5018 use_cluster = false;
5019
Chris Mason0ef3e662008-05-24 14:04:53 -04005020 if (orig_root->ref_cows || empty_size)
5021 allowed_chunk_alloc = 1;
5022
Josef Bacik67377732010-09-16 16:19:09 -04005023 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005024 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005025 if (!btrfs_test_opt(root, SSD))
5026 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005027 }
5028
Josef Bacik67377732010-09-16 16:19:09 -04005029 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5030 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005031 last_ptr = &root->fs_info->data_alloc_cluster;
5032 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005033
Chris Mason239b14b2008-03-24 15:02:07 -04005034 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005035 spin_lock(&last_ptr->lock);
5036 if (last_ptr->block_group)
5037 hint_byte = last_ptr->window_start;
5038 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005039 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005040
Chris Masona061fc82008-05-07 11:43:44 -04005041 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005042 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005043
Josef Bacik817d52f2009-07-13 21:29:25 -04005044 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005045 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005046
Josef Bacik2552d172009-04-03 10:14:19 -04005047 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005048ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04005049 block_group = btrfs_lookup_block_group(root->fs_info,
5050 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04005051 /*
5052 * we don't want to use the block group if it doesn't match our
5053 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005054 *
5055 * However if we are re-searching with an ideal block group
5056 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005057 */
5058 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05005059 (block_group->cached != BTRFS_CACHE_NO ||
5060 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04005061 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005062 if (list_empty(&block_group->list) ||
5063 block_group->ro) {
5064 /*
5065 * someone is removing this block group,
5066 * we can't jump into the have_block_group
5067 * target because our list pointers are not
5068 * valid
5069 */
5070 btrfs_put_block_group(block_group);
5071 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005072 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005073 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005074 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005075 }
Josef Bacik2552d172009-04-03 10:14:19 -04005076 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005077 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005078 }
Chris Mason42e70e72008-11-07 18:17:11 -05005079 }
Josef Bacik2552d172009-04-03 10:14:19 -04005080search:
Josef Bacik80eb2342008-10-29 14:49:05 -04005081 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005082 list_for_each_entry(block_group, &space_info->block_groups[index],
5083 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005084 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005085 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005086
Josef Bacik11dfe352009-11-13 20:12:59 +00005087 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005088 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005089
Chris Mason83a50de2010-12-13 15:06:46 -05005090 /*
5091 * this can happen if we end up cycling through all the
5092 * raid types, but we want to make sure we only allocate
5093 * for the proper type.
5094 */
5095 if (!block_group_bits(block_group, data)) {
5096 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5097 BTRFS_BLOCK_GROUP_RAID1 |
5098 BTRFS_BLOCK_GROUP_RAID10;
5099
5100 /*
5101 * if they asked for extra copies and this block group
5102 * doesn't provide them, bail. This does allow us to
5103 * fill raid0 from raid1.
5104 */
5105 if ((data & extra) && !(block_group->flags & extra))
5106 goto loop;
5107 }
5108
Josef Bacik2552d172009-04-03 10:14:19 -04005109have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04005110 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005111 u64 free_percent;
5112
Josef Bacikb8399de2010-12-08 09:15:11 -05005113 ret = cache_block_group(block_group, trans,
5114 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04005115 if (block_group->cached == BTRFS_CACHE_FINISHED)
5116 goto have_block_group;
5117
Josef Bacikccf0e722009-11-10 21:23:48 -05005118 free_percent = btrfs_block_group_used(&block_group->item);
5119 free_percent *= 100;
5120 free_percent = div64_u64(free_percent,
5121 block_group->key.offset);
5122 free_percent = 100 - free_percent;
5123 if (free_percent > ideal_cache_percent &&
5124 likely(!block_group->ro)) {
5125 ideal_cache_offset = block_group->key.objectid;
5126 ideal_cache_percent = free_percent;
5127 }
5128
Josef Bacik817d52f2009-07-13 21:29:25 -04005129 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005130 * We only want to start kthread caching if we are at
5131 * the point where we will wait for caching to make
5132 * progress, or if our ideal search is over and we've
5133 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04005134 */
5135 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05005136 (loop > LOOP_FIND_IDEAL &&
5137 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacikb8399de2010-12-08 09:15:11 -05005138 ret = cache_block_group(block_group, trans,
5139 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005140 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005141 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005142 found_uncached_bg = true;
5143
5144 /*
5145 * If loop is set for cached only, try the next block
5146 * group.
5147 */
5148 if (loop == LOOP_FIND_IDEAL)
5149 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005150 }
5151
Josef Bacik817d52f2009-07-13 21:29:25 -04005152 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005153 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005154 found_uncached_bg = true;
5155
Josef Bacikea6a4782008-11-20 12:16:16 -05005156 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005157 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005158
Josef Bacik0a243252009-09-11 16:11:20 -04005159 /*
5160 * Ok we want to try and use the cluster allocator, so lets look
5161 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5162 * have tried the cluster allocator plenty of times at this
5163 * point and not have found anything, so we are likely way too
5164 * fragmented for the clustering stuff to find anything, so lets
5165 * just skip it and let the allocator find whatever block it can
5166 * find
5167 */
5168 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005169 /*
5170 * the refill lock keeps out other
5171 * people trying to start a new cluster
5172 */
5173 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005174 if (last_ptr->block_group &&
5175 (last_ptr->block_group->ro ||
5176 !block_group_bits(last_ptr->block_group, data))) {
5177 offset = 0;
5178 goto refill_cluster;
5179 }
5180
Chris Masonfa9c0d72009-04-03 09:47:43 -04005181 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5182 num_bytes, search_start);
5183 if (offset) {
5184 /* we have a block, we're done */
5185 spin_unlock(&last_ptr->refill_lock);
5186 goto checks;
5187 }
5188
5189 spin_lock(&last_ptr->lock);
5190 /*
5191 * whoops, this cluster doesn't actually point to
5192 * this block group. Get a ref on the block
5193 * group is does point to and try again
5194 */
5195 if (!last_ptr_loop && last_ptr->block_group &&
5196 last_ptr->block_group != block_group) {
5197
5198 btrfs_put_block_group(block_group);
5199 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005200 btrfs_get_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005201 spin_unlock(&last_ptr->lock);
5202 spin_unlock(&last_ptr->refill_lock);
5203
5204 last_ptr_loop = 1;
5205 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005206 /*
5207 * we know this block group is properly
5208 * in the list because
5209 * btrfs_remove_block_group, drops the
5210 * cluster before it removes the block
5211 * group from the list
5212 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04005213 goto have_block_group;
5214 }
5215 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005216refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04005217 /*
5218 * this cluster didn't work out, free it and
5219 * start over
5220 */
5221 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5222
5223 last_ptr_loop = 0;
5224
5225 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005226 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005227 block_group, last_ptr,
5228 offset, num_bytes,
5229 empty_cluster + empty_size);
5230 if (ret == 0) {
5231 /*
5232 * now pull our allocation out of this
5233 * cluster
5234 */
5235 offset = btrfs_alloc_from_cluster(block_group,
5236 last_ptr, num_bytes,
5237 search_start);
5238 if (offset) {
5239 /* we found one, proceed */
5240 spin_unlock(&last_ptr->refill_lock);
5241 goto checks;
5242 }
Josef Bacik0a243252009-09-11 16:11:20 -04005243 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5244 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005245 spin_unlock(&last_ptr->refill_lock);
5246
Josef Bacik0a243252009-09-11 16:11:20 -04005247 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005248 wait_block_group_cache_progress(block_group,
5249 num_bytes + empty_cluster + empty_size);
5250 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005251 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005252
Chris Masonfa9c0d72009-04-03 09:47:43 -04005253 /*
5254 * at this point we either didn't find a cluster
5255 * or we weren't able to allocate a block from our
5256 * cluster. Free the cluster we've been trying
5257 * to use, and go to the next block group
5258 */
Josef Bacik0a243252009-09-11 16:11:20 -04005259 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005260 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005261 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005262 }
5263
Josef Bacik6226cb02009-04-03 10:14:18 -04005264 offset = btrfs_find_space_for_alloc(block_group, search_start,
5265 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005266 /*
5267 * If we didn't find a chunk, and we haven't failed on this
5268 * block group before, and this block group is in the middle of
5269 * caching and we are ok with waiting, then go ahead and wait
5270 * for progress to be made, and set failed_alloc to true.
5271 *
5272 * If failed_alloc is true then we've already waited on this
5273 * block group once and should move on to the next block group.
5274 */
5275 if (!offset && !failed_alloc && !cached &&
5276 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005277 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005278 num_bytes + empty_size);
5279 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005280 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005281 } else if (!offset) {
5282 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005283 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005284checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005285 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005286 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005287 if (search_start + num_bytes >= search_end) {
5288 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005289 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005290 }
Chris Masone37c9e62007-05-09 20:13:14 -04005291
Josef Bacik2552d172009-04-03 10:14:19 -04005292 /* move on to the next group */
5293 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005294 block_group->key.objectid + block_group->key.offset) {
5295 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005296 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005297 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005298
Josef Bacik2552d172009-04-03 10:14:19 -04005299 ins->objectid = search_start;
5300 ins->offset = num_bytes;
5301
Josef Bacik6226cb02009-04-03 10:14:18 -04005302 if (offset < search_start)
5303 btrfs_add_free_space(block_group, offset,
5304 search_start - offset);
5305 BUG_ON(offset > search_start);
5306
Li Dongyangb4d00d52011-03-24 10:24:25 +00005307 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005308 (data & BTRFS_BLOCK_GROUP_DATA));
5309 if (ret == -EAGAIN) {
5310 btrfs_add_free_space(block_group, offset, num_bytes);
5311 goto loop;
5312 }
Yan Zheng11833d62009-09-11 16:11:19 -04005313
Josef Bacik2552d172009-04-03 10:14:19 -04005314 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005315 ins->objectid = search_start;
5316 ins->offset = num_bytes;
5317
5318 if (offset < search_start)
5319 btrfs_add_free_space(block_group, offset,
5320 search_start - offset);
5321 BUG_ON(offset > search_start);
Josef Bacik2552d172009-04-03 10:14:19 -04005322 break;
5323loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005324 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005325 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005326 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d72009-04-03 09:47:43 -04005327 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005328 }
5329 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005330
Yan, Zhengb742bb82010-05-16 10:46:24 -04005331 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5332 goto search;
5333
Josef Bacikccf0e722009-11-10 21:23:48 -05005334 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5335 * for them to make caching progress. Also
5336 * determine the best possible bg to cache
5337 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5338 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005339 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5340 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5341 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5342 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04005343 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005344 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5345 (found_uncached_bg || empty_size || empty_cluster ||
5346 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005347 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005348 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005349 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005350 loop++;
5351 if (!ideal_cache_percent &&
5352 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005353 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005354
5355 /*
5356 * 1 of the following 2 things have happened so far
5357 *
5358 * 1) We found an ideal block group for caching that
5359 * is mostly full and will cache quickly, so we might
5360 * as well wait for it.
5361 *
5362 * 2) We searched for cached only and we didn't find
5363 * anything, and we didn't start any caching kthreads
5364 * either, so chances are we will loop through and
5365 * start a couple caching kthreads, and then come back
5366 * around and just wait for them. This will be slower
5367 * because we will have 2 caching kthreads reading at
5368 * the same time when we could have just started one
5369 * and waited for it to get far enough to give us an
5370 * allocation, so go ahead and go to the wait caching
5371 * loop.
5372 */
5373 loop = LOOP_CACHING_WAIT;
5374 search_start = ideal_cache_offset;
5375 ideal_cache_percent = 0;
5376 goto ideal_cache;
5377 } else if (loop == LOOP_FIND_IDEAL) {
5378 /*
5379 * Didn't find a uncached bg, wait on anything we find
5380 * next.
5381 */
5382 loop = LOOP_CACHING_WAIT;
5383 goto search;
5384 }
5385
5386 if (loop < LOOP_CACHING_WAIT) {
5387 loop++;
5388 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005389 }
5390
5391 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005392 empty_size = 0;
5393 empty_cluster = 0;
5394 }
Chris Mason42e70e72008-11-07 18:17:11 -05005395
Josef Bacik2552d172009-04-03 10:14:19 -04005396 if (allowed_chunk_alloc) {
5397 ret = do_chunk_alloc(trans, root, num_bytes +
Chris Mason0e4f8f82011-04-15 16:05:44 -04005398 2 * 1024 * 1024, data,
5399 CHUNK_ALLOC_LIMITED);
Josef Bacik2552d172009-04-03 10:14:19 -04005400 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005401 done_chunk_alloc = 1;
Chris Mason0e4f8f82011-04-15 16:05:44 -04005402 } else if (!done_chunk_alloc &&
5403 space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) {
5404 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005405 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005406
Josef Bacik817d52f2009-07-13 21:29:25 -04005407 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005408 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005409 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005410 }
Josef Bacik2552d172009-04-03 10:14:19 -04005411 ret = -ENOSPC;
5412 } else if (!ins->objectid) {
5413 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04005414 }
Chris Mason0b86a832008-03-24 15:01:56 -04005415
Josef Bacik80eb2342008-10-29 14:49:05 -04005416 /* we found what we needed */
5417 if (ins->objectid) {
5418 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05005419 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04005420
Chris Masonfa9c0d72009-04-03 09:47:43 -04005421 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005422 ret = 0;
5423 }
Chris Mason0b86a832008-03-24 15:01:56 -04005424
Chris Mason0f70abe2007-02-28 16:46:22 -05005425 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005426}
Chris Masonec44a352008-04-28 15:29:52 -04005427
Josef Bacik9ed74f22009-09-11 16:12:44 -04005428static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5429 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005430{
5431 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005432 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005433
Josef Bacik9ed74f22009-09-11 16:12:44 -04005434 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005435 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5436 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005437 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005438 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005439 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005440 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5441 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005442 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005443 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005444 (unsigned long long)info->bytes_pinned,
5445 (unsigned long long)info->bytes_reserved,
5446 (unsigned long long)info->bytes_may_use,
5447 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005448 spin_unlock(&info->lock);
5449
5450 if (!dump_block_groups)
5451 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005452
Josef Bacik80eb2342008-10-29 14:49:05 -04005453 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005454again:
5455 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005456 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005457 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5458 "%llu pinned %llu reserved\n",
5459 (unsigned long long)cache->key.objectid,
5460 (unsigned long long)cache->key.offset,
5461 (unsigned long long)btrfs_block_group_used(&cache->item),
5462 (unsigned long long)cache->pinned,
5463 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005464 btrfs_dump_free_space(cache, bytes);
5465 spin_unlock(&cache->lock);
5466 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005467 if (++index < BTRFS_NR_RAID_TYPES)
5468 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005469 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005470}
Zheng Yane8569812008-09-26 10:05:48 -04005471
Yan Zheng11833d62009-09-11 16:11:19 -04005472int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5473 struct btrfs_root *root,
5474 u64 num_bytes, u64 min_alloc_size,
5475 u64 empty_size, u64 hint_byte,
5476 u64 search_end, struct btrfs_key *ins,
5477 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005478{
5479 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005480 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005481
Josef Bacik6a632092009-02-20 11:00:09 -05005482 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005483again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005484 /*
5485 * the only place that sets empty_size is btrfs_realloc_node, which
5486 * is not called recursively on allocations
5487 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005488 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005489 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005490 num_bytes + 2 * 1024 * 1024, data,
5491 CHUNK_ALLOC_NO_FORCE);
Chris Mason0b86a832008-03-24 15:01:56 -04005492
Chris Masondb945352007-10-15 16:15:53 -04005493 WARN_ON(num_bytes < root->sectorsize);
5494 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005495 search_start, search_end, hint_byte,
5496 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005497
Chris Mason98d20f62008-04-14 09:46:10 -04005498 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5499 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005500 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005501 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005502 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005503 num_bytes, data, CHUNK_ALLOC_FORCE);
Chris Mason98d20f62008-04-14 09:46:10 -04005504 goto again;
5505 }
Chris Mason91435652011-02-16 13:10:41 -05005506 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005507 struct btrfs_space_info *sinfo;
5508
5509 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005510 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5511 "wanted %llu\n", (unsigned long long)data,
5512 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005513 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005514 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005515
liubo1abe9b82011-03-24 11:18:59 +00005516 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5517
Josef Bacik0f9dd462008-09-23 13:14:11 -04005518 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005519}
5520
Chris Mason65b51a02008-08-01 15:11:20 -04005521int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5522{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005523 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005524 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005525
Josef Bacik0f9dd462008-09-23 13:14:11 -04005526 cache = btrfs_lookup_block_group(root->fs_info, start);
5527 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005528 printk(KERN_ERR "Unable to find block group for %llu\n",
5529 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005530 return -ENOSPC;
5531 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005532
Li Dongyang5378e602011-03-24 10:24:27 +00005533 if (btrfs_test_opt(root, DISCARD))
5534 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005535
Josef Bacik0f9dd462008-09-23 13:14:11 -04005536 btrfs_add_free_space(cache, start, len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00005537 btrfs_update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005538 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005539
liubo1abe9b82011-03-24 11:18:59 +00005540 trace_btrfs_reserved_extent_free(root, start, len);
5541
Chris Masone6dcd2d2008-07-17 12:53:50 -04005542 return ret;
5543}
5544
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005545static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5546 struct btrfs_root *root,
5547 u64 parent, u64 root_objectid,
5548 u64 flags, u64 owner, u64 offset,
5549 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005550{
5551 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005552 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005553 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005554 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005555 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005556 struct extent_buffer *leaf;
5557 int type;
5558 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005559
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005560 if (parent > 0)
5561 type = BTRFS_SHARED_DATA_REF_KEY;
5562 else
5563 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005564
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005565 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005566
5567 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005568 if (!path)
5569 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005570
Chris Masonb9473432009-03-13 11:00:37 -04005571 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005572 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5573 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005574 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005575
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005576 leaf = path->nodes[0];
5577 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005578 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005579 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5580 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5581 btrfs_set_extent_flags(leaf, extent_item,
5582 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005583
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005584 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5585 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5586 if (parent > 0) {
5587 struct btrfs_shared_data_ref *ref;
5588 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5589 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5590 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5591 } else {
5592 struct btrfs_extent_data_ref *ref;
5593 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5594 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5595 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5596 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5597 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5598 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005599
5600 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005601 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005602
Yan, Zhengf0486c62010-05-16 10:46:25 -04005603 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005604 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005605 printk(KERN_ERR "btrfs update block group failed for %llu "
5606 "%llu\n", (unsigned long long)ins->objectid,
5607 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005608 BUG();
5609 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005610 return ret;
5611}
5612
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005613static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5614 struct btrfs_root *root,
5615 u64 parent, u64 root_objectid,
5616 u64 flags, struct btrfs_disk_key *key,
5617 int level, struct btrfs_key *ins)
5618{
5619 int ret;
5620 struct btrfs_fs_info *fs_info = root->fs_info;
5621 struct btrfs_extent_item *extent_item;
5622 struct btrfs_tree_block_info *block_info;
5623 struct btrfs_extent_inline_ref *iref;
5624 struct btrfs_path *path;
5625 struct extent_buffer *leaf;
5626 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5627
5628 path = btrfs_alloc_path();
5629 BUG_ON(!path);
5630
5631 path->leave_spinning = 1;
5632 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5633 ins, size);
5634 BUG_ON(ret);
5635
5636 leaf = path->nodes[0];
5637 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5638 struct btrfs_extent_item);
5639 btrfs_set_extent_refs(leaf, extent_item, 1);
5640 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5641 btrfs_set_extent_flags(leaf, extent_item,
5642 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5643 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5644
5645 btrfs_set_tree_block_key(leaf, block_info, key);
5646 btrfs_set_tree_block_level(leaf, block_info, level);
5647
5648 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5649 if (parent > 0) {
5650 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5651 btrfs_set_extent_inline_ref_type(leaf, iref,
5652 BTRFS_SHARED_BLOCK_REF_KEY);
5653 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5654 } else {
5655 btrfs_set_extent_inline_ref_type(leaf, iref,
5656 BTRFS_TREE_BLOCK_REF_KEY);
5657 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5658 }
5659
5660 btrfs_mark_buffer_dirty(leaf);
5661 btrfs_free_path(path);
5662
Yan, Zhengf0486c62010-05-16 10:46:25 -04005663 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005664 if (ret) {
5665 printk(KERN_ERR "btrfs update block group failed for %llu "
5666 "%llu\n", (unsigned long long)ins->objectid,
5667 (unsigned long long)ins->offset);
5668 BUG();
5669 }
5670 return ret;
5671}
5672
5673int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5674 struct btrfs_root *root,
5675 u64 root_objectid, u64 owner,
5676 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005677{
5678 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005679
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005680 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005681
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005682 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5683 0, root_objectid, owner, offset,
5684 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005685 return ret;
5686}
Chris Masone02119d2008-09-05 16:13:11 -04005687
5688/*
5689 * this is used by the tree logging recovery code. It records that
5690 * an extent has been allocated and makes sure to clear the free
5691 * space cache bits as well
5692 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005693int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5694 struct btrfs_root *root,
5695 u64 root_objectid, u64 owner, u64 offset,
5696 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005697{
5698 int ret;
5699 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005700 struct btrfs_caching_control *caching_ctl;
5701 u64 start = ins->objectid;
5702 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005703
Chris Masone02119d2008-09-05 16:13:11 -04005704 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005705 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005706 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005707
Yan Zheng11833d62009-09-11 16:11:19 -04005708 if (!caching_ctl) {
5709 BUG_ON(!block_group_cache_done(block_group));
5710 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5711 BUG_ON(ret);
5712 } else {
5713 mutex_lock(&caching_ctl->mutex);
5714
5715 if (start >= caching_ctl->progress) {
5716 ret = add_excluded_extent(root, start, num_bytes);
5717 BUG_ON(ret);
5718 } else if (start + num_bytes <= caching_ctl->progress) {
5719 ret = btrfs_remove_free_space(block_group,
5720 start, num_bytes);
5721 BUG_ON(ret);
5722 } else {
5723 num_bytes = caching_ctl->progress - start;
5724 ret = btrfs_remove_free_space(block_group,
5725 start, num_bytes);
5726 BUG_ON(ret);
5727
5728 start = caching_ctl->progress;
5729 num_bytes = ins->objectid + ins->offset -
5730 caching_ctl->progress;
5731 ret = add_excluded_extent(root, start, num_bytes);
5732 BUG_ON(ret);
5733 }
5734
5735 mutex_unlock(&caching_ctl->mutex);
5736 put_caching_control(caching_ctl);
5737 }
5738
Li Dongyangb4d00d52011-03-24 10:24:25 +00005739 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005740 BUG_ON(ret);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005741 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005742 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5743 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005744 return ret;
5745}
5746
Chris Mason65b51a02008-08-01 15:11:20 -04005747struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5748 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005749 u64 bytenr, u32 blocksize,
5750 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005751{
5752 struct extent_buffer *buf;
5753
5754 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5755 if (!buf)
5756 return ERR_PTR(-ENOMEM);
5757 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005758 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005759 btrfs_tree_lock(buf);
5760 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005761
5762 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005763 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005764
Chris Masond0c803c2008-09-11 16:17:57 -04005765 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005766 /*
5767 * we allow two log transactions at a time, use different
5768 * EXENT bit to differentiate dirty pages.
5769 */
5770 if (root->log_transid % 2 == 0)
5771 set_extent_dirty(&root->dirty_log_pages, buf->start,
5772 buf->start + buf->len - 1, GFP_NOFS);
5773 else
5774 set_extent_new(&root->dirty_log_pages, buf->start,
5775 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005776 } else {
5777 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5778 buf->start + buf->len - 1, GFP_NOFS);
5779 }
Chris Mason65b51a02008-08-01 15:11:20 -04005780 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005781 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005782 return buf;
5783}
5784
Yan, Zhengf0486c62010-05-16 10:46:25 -04005785static struct btrfs_block_rsv *
5786use_block_rsv(struct btrfs_trans_handle *trans,
5787 struct btrfs_root *root, u32 blocksize)
5788{
5789 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005790 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005791 int ret;
5792
5793 block_rsv = get_block_rsv(trans, root);
5794
5795 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005796 ret = reserve_metadata_bytes(trans, root, block_rsv,
5797 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005798 /*
5799 * If we couldn't reserve metadata bytes try and use some from
5800 * the global reserve.
5801 */
5802 if (ret && block_rsv != global_rsv) {
5803 ret = block_rsv_use_bytes(global_rsv, blocksize);
5804 if (!ret)
5805 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005806 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005807 } else if (ret) {
5808 return ERR_PTR(ret);
5809 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005810 return block_rsv;
5811 }
5812
5813 ret = block_rsv_use_bytes(block_rsv, blocksize);
5814 if (!ret)
5815 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005816 if (ret) {
5817 WARN_ON(1);
5818 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5819 0);
5820 if (!ret) {
5821 spin_lock(&block_rsv->lock);
5822 block_rsv->size += blocksize;
5823 spin_unlock(&block_rsv->lock);
5824 return block_rsv;
5825 } else if (ret && block_rsv != global_rsv) {
5826 ret = block_rsv_use_bytes(global_rsv, blocksize);
5827 if (!ret)
5828 return global_rsv;
5829 }
5830 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005831
Yan, Zhengf0486c62010-05-16 10:46:25 -04005832 return ERR_PTR(-ENOSPC);
5833}
5834
5835static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5836{
5837 block_rsv_add_bytes(block_rsv, blocksize, 0);
5838 block_rsv_release_bytes(block_rsv, NULL, 0);
5839}
5840
Chris Masonfec577f2007-02-26 10:40:21 -05005841/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005842 * finds a free extent and does all the dirty work required for allocation
5843 * returns the key for the extent through ins, and a tree buffer for
5844 * the first block of the extent through buf.
5845 *
Chris Masonfec577f2007-02-26 10:40:21 -05005846 * returns the tree buffer or NULL.
5847 */
Chris Mason5f39d392007-10-15 16:14:19 -04005848struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005849 struct btrfs_root *root, u32 blocksize,
5850 u64 parent, u64 root_objectid,
5851 struct btrfs_disk_key *key, int level,
5852 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005853{
Chris Masone2fa7222007-03-12 16:22:34 -04005854 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005855 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005856 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005857 u64 flags = 0;
5858 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005859
Yan, Zhengf0486c62010-05-16 10:46:25 -04005860
5861 block_rsv = use_block_rsv(trans, root, blocksize);
5862 if (IS_ERR(block_rsv))
5863 return ERR_CAST(block_rsv);
5864
5865 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5866 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005867 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005868 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005869 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005870 }
Chris Mason55c69072008-01-09 15:55:33 -05005871
Chris Mason4008c042009-02-12 14:09:45 -05005872 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5873 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005874 BUG_ON(IS_ERR(buf));
5875
5876 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5877 if (parent == 0)
5878 parent = ins.objectid;
5879 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5880 } else
5881 BUG_ON(parent > 0);
5882
5883 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5884 struct btrfs_delayed_extent_op *extent_op;
5885 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5886 BUG_ON(!extent_op);
5887 if (key)
5888 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5889 else
5890 memset(&extent_op->key, 0, sizeof(extent_op->key));
5891 extent_op->flags_to_set = flags;
5892 extent_op->update_key = 1;
5893 extent_op->update_flags = 1;
5894 extent_op->is_data = 0;
5895
5896 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5897 ins.offset, parent, root_objectid,
5898 level, BTRFS_ADD_DELAYED_EXTENT,
5899 extent_op);
5900 BUG_ON(ret);
5901 }
Chris Masonfec577f2007-02-26 10:40:21 -05005902 return buf;
5903}
Chris Masona28ec192007-03-06 20:08:01 -05005904
Yan Zheng2c47e6052009-06-27 21:07:35 -04005905struct walk_control {
5906 u64 refs[BTRFS_MAX_LEVEL];
5907 u64 flags[BTRFS_MAX_LEVEL];
5908 struct btrfs_key update_progress;
5909 int stage;
5910 int level;
5911 int shared_level;
5912 int update_ref;
5913 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005914 int reada_slot;
5915 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005916};
5917
5918#define DROP_REFERENCE 1
5919#define UPDATE_BACKREF 2
5920
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005921static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5922 struct btrfs_root *root,
5923 struct walk_control *wc,
5924 struct btrfs_path *path)
5925{
5926 u64 bytenr;
5927 u64 generation;
5928 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005929 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005930 u32 nritems;
5931 u32 blocksize;
5932 struct btrfs_key key;
5933 struct extent_buffer *eb;
5934 int ret;
5935 int slot;
5936 int nread = 0;
5937
5938 if (path->slots[wc->level] < wc->reada_slot) {
5939 wc->reada_count = wc->reada_count * 2 / 3;
5940 wc->reada_count = max(wc->reada_count, 2);
5941 } else {
5942 wc->reada_count = wc->reada_count * 3 / 2;
5943 wc->reada_count = min_t(int, wc->reada_count,
5944 BTRFS_NODEPTRS_PER_BLOCK(root));
5945 }
5946
5947 eb = path->nodes[wc->level];
5948 nritems = btrfs_header_nritems(eb);
5949 blocksize = btrfs_level_size(root, wc->level - 1);
5950
5951 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5952 if (nread >= wc->reada_count)
5953 break;
5954
5955 cond_resched();
5956 bytenr = btrfs_node_blockptr(eb, slot);
5957 generation = btrfs_node_ptr_generation(eb, slot);
5958
5959 if (slot == path->slots[wc->level])
5960 goto reada;
5961
5962 if (wc->stage == UPDATE_BACKREF &&
5963 generation <= root->root_key.offset)
5964 continue;
5965
Yan, Zheng94fcca92009-10-09 09:25:16 -04005966 /* We don't lock the tree block, it's OK to be racy here */
5967 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5968 &refs, &flags);
5969 BUG_ON(ret);
5970 BUG_ON(refs == 0);
5971
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005972 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005973 if (refs == 1)
5974 goto reada;
5975
Yan, Zheng94fcca92009-10-09 09:25:16 -04005976 if (wc->level == 1 &&
5977 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5978 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005979 if (!wc->update_ref ||
5980 generation <= root->root_key.offset)
5981 continue;
5982 btrfs_node_key_to_cpu(eb, &key, slot);
5983 ret = btrfs_comp_cpu_keys(&key,
5984 &wc->update_progress);
5985 if (ret < 0)
5986 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005987 } else {
5988 if (wc->level == 1 &&
5989 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5990 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005991 }
5992reada:
5993 ret = readahead_tree_block(root, bytenr, blocksize,
5994 generation);
5995 if (ret)
5996 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005997 nread++;
5998 }
5999 wc->reada_slot = slot;
6000}
6001
Chris Mason9aca1d52007-03-13 11:09:37 -04006002/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006003 * hepler to process tree block while walking down the tree.
6004 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006005 * when wc->stage == UPDATE_BACKREF, this function updates
6006 * back refs for pointers in the block.
6007 *
6008 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006009 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006010static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6011 struct btrfs_root *root,
6012 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006013 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006014{
6015 int level = wc->level;
6016 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006017 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6018 int ret;
6019
6020 if (wc->stage == UPDATE_BACKREF &&
6021 btrfs_header_owner(eb) != root->root_key.objectid)
6022 return 1;
6023
6024 /*
6025 * when reference count of tree block is 1, it won't increase
6026 * again. once full backref flag is set, we never clear it.
6027 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006028 if (lookup_info &&
6029 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6030 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006031 BUG_ON(!path->locks[level]);
6032 ret = btrfs_lookup_extent_info(trans, root,
6033 eb->start, eb->len,
6034 &wc->refs[level],
6035 &wc->flags[level]);
6036 BUG_ON(ret);
6037 BUG_ON(wc->refs[level] == 0);
6038 }
6039
Yan Zheng2c47e6052009-06-27 21:07:35 -04006040 if (wc->stage == DROP_REFERENCE) {
6041 if (wc->refs[level] > 1)
6042 return 1;
6043
6044 if (path->locks[level] && !wc->keep_locks) {
6045 btrfs_tree_unlock(eb);
6046 path->locks[level] = 0;
6047 }
6048 return 0;
6049 }
6050
6051 /* wc->stage == UPDATE_BACKREF */
6052 if (!(wc->flags[level] & flag)) {
6053 BUG_ON(!path->locks[level]);
6054 ret = btrfs_inc_ref(trans, root, eb, 1);
6055 BUG_ON(ret);
6056 ret = btrfs_dec_ref(trans, root, eb, 0);
6057 BUG_ON(ret);
6058 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6059 eb->len, flag, 0);
6060 BUG_ON(ret);
6061 wc->flags[level] |= flag;
6062 }
6063
6064 /*
6065 * the block is shared by multiple trees, so it's not good to
6066 * keep the tree lock
6067 */
6068 if (path->locks[level] && level > 0) {
6069 btrfs_tree_unlock(eb);
6070 path->locks[level] = 0;
6071 }
6072 return 0;
6073}
6074
6075/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006076 * hepler to process tree block pointer.
6077 *
6078 * when wc->stage == DROP_REFERENCE, this function checks
6079 * reference count of the block pointed to. if the block
6080 * is shared and we need update back refs for the subtree
6081 * rooted at the block, this function changes wc->stage to
6082 * UPDATE_BACKREF. if the block is shared and there is no
6083 * need to update back, this function drops the reference
6084 * to the block.
6085 *
6086 * NOTE: return value 1 means we should stop walking down.
6087 */
6088static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6089 struct btrfs_root *root,
6090 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006091 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006092{
6093 u64 bytenr;
6094 u64 generation;
6095 u64 parent;
6096 u32 blocksize;
6097 struct btrfs_key key;
6098 struct extent_buffer *next;
6099 int level = wc->level;
6100 int reada = 0;
6101 int ret = 0;
6102
6103 generation = btrfs_node_ptr_generation(path->nodes[level],
6104 path->slots[level]);
6105 /*
6106 * if the lower level block was created before the snapshot
6107 * was created, we know there is no need to update back refs
6108 * for the subtree
6109 */
6110 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006111 generation <= root->root_key.offset) {
6112 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006113 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006114 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006115
6116 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6117 blocksize = btrfs_level_size(root, level - 1);
6118
6119 next = btrfs_find_tree_block(root, bytenr, blocksize);
6120 if (!next) {
6121 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00006122 if (!next)
6123 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006124 reada = 1;
6125 }
6126 btrfs_tree_lock(next);
6127 btrfs_set_lock_blocking(next);
6128
Yan, Zheng94fcca92009-10-09 09:25:16 -04006129 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6130 &wc->refs[level - 1],
6131 &wc->flags[level - 1]);
6132 BUG_ON(ret);
6133 BUG_ON(wc->refs[level - 1] == 0);
6134 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006135
Yan, Zheng94fcca92009-10-09 09:25:16 -04006136 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006137 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006138 if (level == 1 &&
6139 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6140 goto skip;
6141
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006142 if (!wc->update_ref ||
6143 generation <= root->root_key.offset)
6144 goto skip;
6145
6146 btrfs_node_key_to_cpu(path->nodes[level], &key,
6147 path->slots[level]);
6148 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6149 if (ret < 0)
6150 goto skip;
6151
6152 wc->stage = UPDATE_BACKREF;
6153 wc->shared_level = level - 1;
6154 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006155 } else {
6156 if (level == 1 &&
6157 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6158 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006159 }
6160
6161 if (!btrfs_buffer_uptodate(next, generation)) {
6162 btrfs_tree_unlock(next);
6163 free_extent_buffer(next);
6164 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006165 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006166 }
6167
6168 if (!next) {
6169 if (reada && level == 1)
6170 reada_walk_down(trans, root, wc, path);
6171 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006172 if (!next)
6173 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006174 btrfs_tree_lock(next);
6175 btrfs_set_lock_blocking(next);
6176 }
6177
6178 level--;
6179 BUG_ON(level != btrfs_header_level(next));
6180 path->nodes[level] = next;
6181 path->slots[level] = 0;
6182 path->locks[level] = 1;
6183 wc->level = level;
6184 if (wc->level == 1)
6185 wc->reada_slot = 0;
6186 return 0;
6187skip:
6188 wc->refs[level - 1] = 0;
6189 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006190 if (wc->stage == DROP_REFERENCE) {
6191 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6192 parent = path->nodes[level]->start;
6193 } else {
6194 BUG_ON(root->root_key.objectid !=
6195 btrfs_header_owner(path->nodes[level]));
6196 parent = 0;
6197 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006198
Yan, Zheng94fcca92009-10-09 09:25:16 -04006199 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6200 root->root_key.objectid, level - 1, 0);
6201 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006202 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006203 btrfs_tree_unlock(next);
6204 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006205 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006206 return 1;
6207}
6208
6209/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006210 * hepler to process tree block while walking up the tree.
6211 *
6212 * when wc->stage == DROP_REFERENCE, this function drops
6213 * reference count on the block.
6214 *
6215 * when wc->stage == UPDATE_BACKREF, this function changes
6216 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6217 * to UPDATE_BACKREF previously while processing the block.
6218 *
6219 * NOTE: return value 1 means we should stop walking up.
6220 */
6221static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6222 struct btrfs_root *root,
6223 struct btrfs_path *path,
6224 struct walk_control *wc)
6225{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006226 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006227 int level = wc->level;
6228 struct extent_buffer *eb = path->nodes[level];
6229 u64 parent = 0;
6230
6231 if (wc->stage == UPDATE_BACKREF) {
6232 BUG_ON(wc->shared_level < level);
6233 if (level < wc->shared_level)
6234 goto out;
6235
Yan Zheng2c47e6052009-06-27 21:07:35 -04006236 ret = find_next_key(path, level + 1, &wc->update_progress);
6237 if (ret > 0)
6238 wc->update_ref = 0;
6239
6240 wc->stage = DROP_REFERENCE;
6241 wc->shared_level = -1;
6242 path->slots[level] = 0;
6243
6244 /*
6245 * check reference count again if the block isn't locked.
6246 * we should start walking down the tree again if reference
6247 * count is one.
6248 */
6249 if (!path->locks[level]) {
6250 BUG_ON(level == 0);
6251 btrfs_tree_lock(eb);
6252 btrfs_set_lock_blocking(eb);
6253 path->locks[level] = 1;
6254
6255 ret = btrfs_lookup_extent_info(trans, root,
6256 eb->start, eb->len,
6257 &wc->refs[level],
6258 &wc->flags[level]);
6259 BUG_ON(ret);
6260 BUG_ON(wc->refs[level] == 0);
6261 if (wc->refs[level] == 1) {
6262 btrfs_tree_unlock(eb);
6263 path->locks[level] = 0;
6264 return 1;
6265 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006266 }
6267 }
6268
6269 /* wc->stage == DROP_REFERENCE */
6270 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6271
6272 if (wc->refs[level] == 1) {
6273 if (level == 0) {
6274 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6275 ret = btrfs_dec_ref(trans, root, eb, 1);
6276 else
6277 ret = btrfs_dec_ref(trans, root, eb, 0);
6278 BUG_ON(ret);
6279 }
6280 /* make block locked assertion in clean_tree_block happy */
6281 if (!path->locks[level] &&
6282 btrfs_header_generation(eb) == trans->transid) {
6283 btrfs_tree_lock(eb);
6284 btrfs_set_lock_blocking(eb);
6285 path->locks[level] = 1;
6286 }
6287 clean_tree_block(trans, root, eb);
6288 }
6289
6290 if (eb == root->node) {
6291 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6292 parent = eb->start;
6293 else
6294 BUG_ON(root->root_key.objectid !=
6295 btrfs_header_owner(eb));
6296 } else {
6297 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6298 parent = path->nodes[level + 1]->start;
6299 else
6300 BUG_ON(root->root_key.objectid !=
6301 btrfs_header_owner(path->nodes[level + 1]));
6302 }
6303
Yan, Zhengf0486c62010-05-16 10:46:25 -04006304 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006305out:
6306 wc->refs[level] = 0;
6307 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006308 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006309}
6310
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006311static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6312 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006313 struct btrfs_path *path,
6314 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006315{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006316 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006317 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006318 int ret;
6319
Yan Zheng2c47e6052009-06-27 21:07:35 -04006320 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006321 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006322 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006323 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006324
Yan Zheng2c47e6052009-06-27 21:07:35 -04006325 if (level == 0)
6326 break;
6327
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006328 if (path->slots[level] >=
6329 btrfs_header_nritems(path->nodes[level]))
6330 break;
6331
Yan, Zheng94fcca92009-10-09 09:25:16 -04006332 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006333 if (ret > 0) {
6334 path->slots[level]++;
6335 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00006336 } else if (ret < 0)
6337 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006338 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006339 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006340 return 0;
6341}
6342
Chris Masond3977122009-01-05 21:25:51 -05006343static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006344 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006345 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006346 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006347{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006348 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006349 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006350
Yan Zheng2c47e6052009-06-27 21:07:35 -04006351 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6352 while (level < max_level && path->nodes[level]) {
6353 wc->level = level;
6354 if (path->slots[level] + 1 <
6355 btrfs_header_nritems(path->nodes[level])) {
6356 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006357 return 0;
6358 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006359 ret = walk_up_proc(trans, root, path, wc);
6360 if (ret > 0)
6361 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006362
Yan Zheng2c47e6052009-06-27 21:07:35 -04006363 if (path->locks[level]) {
6364 btrfs_tree_unlock(path->nodes[level]);
6365 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006366 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006367 free_extent_buffer(path->nodes[level]);
6368 path->nodes[level] = NULL;
6369 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006370 }
6371 }
6372 return 1;
6373}
6374
Chris Mason9aca1d52007-03-13 11:09:37 -04006375/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006376 * drop a subvolume tree.
6377 *
6378 * this function traverses the tree freeing any blocks that only
6379 * referenced by the tree.
6380 *
6381 * when a shared tree block is found. this function decreases its
6382 * reference count by one. if update_ref is true, this function
6383 * also make sure backrefs for the shared block and all lower level
6384 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006385 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006386int btrfs_drop_snapshot(struct btrfs_root *root,
6387 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006388{
Chris Mason5caf2a02007-04-02 11:20:42 -04006389 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006390 struct btrfs_trans_handle *trans;
6391 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006392 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006393 struct walk_control *wc;
6394 struct btrfs_key key;
6395 int err = 0;
6396 int ret;
6397 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006398
Chris Mason5caf2a02007-04-02 11:20:42 -04006399 path = btrfs_alloc_path();
6400 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006401
Yan Zheng2c47e6052009-06-27 21:07:35 -04006402 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6403 BUG_ON(!wc);
6404
Yan, Zhenga22285a2010-05-16 10:48:46 -04006405 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006406 BUG_ON(IS_ERR(trans));
6407
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006408 if (block_rsv)
6409 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006410
Chris Mason9f3a7422007-08-07 15:52:19 -04006411 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006412 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006413 path->nodes[level] = btrfs_lock_root_node(root);
6414 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006415 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006416 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006417 memset(&wc->update_progress, 0,
6418 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006419 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006420 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006421 memcpy(&wc->update_progress, &key,
6422 sizeof(wc->update_progress));
6423
Chris Mason6702ed42007-08-07 16:15:09 -04006424 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006425 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006426 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006427 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6428 path->lowest_level = 0;
6429 if (ret < 0) {
6430 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006431 goto out;
6432 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006433 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006434
Chris Mason7d9eb122008-07-08 14:19:17 -04006435 /*
6436 * unlock our path, this is safe because only this
6437 * function is allowed to delete this snapshot
6438 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006439 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006440
Yan Zheng2c47e6052009-06-27 21:07:35 -04006441 level = btrfs_header_level(root->node);
6442 while (1) {
6443 btrfs_tree_lock(path->nodes[level]);
6444 btrfs_set_lock_blocking(path->nodes[level]);
6445
6446 ret = btrfs_lookup_extent_info(trans, root,
6447 path->nodes[level]->start,
6448 path->nodes[level]->len,
6449 &wc->refs[level],
6450 &wc->flags[level]);
6451 BUG_ON(ret);
6452 BUG_ON(wc->refs[level] == 0);
6453
6454 if (level == root_item->drop_level)
6455 break;
6456
6457 btrfs_tree_unlock(path->nodes[level]);
6458 WARN_ON(wc->refs[level] != 1);
6459 level--;
6460 }
6461 }
6462
6463 wc->level = level;
6464 wc->shared_level = -1;
6465 wc->stage = DROP_REFERENCE;
6466 wc->update_ref = update_ref;
6467 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006468 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006469
6470 while (1) {
6471 ret = walk_down_tree(trans, root, path, wc);
6472 if (ret < 0) {
6473 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006474 break;
6475 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006476
6477 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6478 if (ret < 0) {
6479 err = ret;
6480 break;
6481 }
6482
6483 if (ret > 0) {
6484 BUG_ON(wc->stage != DROP_REFERENCE);
6485 break;
6486 }
6487
6488 if (wc->stage == DROP_REFERENCE) {
6489 level = wc->level;
6490 btrfs_node_key(path->nodes[level],
6491 &root_item->drop_progress,
6492 path->slots[level]);
6493 root_item->drop_level = level;
6494 }
6495
6496 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006497 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006498 ret = btrfs_update_root(trans, tree_root,
6499 &root->root_key,
6500 root_item);
6501 BUG_ON(ret);
6502
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006503 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006504 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006505 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006506 if (block_rsv)
6507 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006508 }
Chris Mason20524f02007-03-10 06:35:47 -05006509 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006510 btrfs_release_path(root, path);
6511 BUG_ON(err);
6512
6513 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6514 BUG_ON(ret);
6515
Yan, Zheng76dda932009-09-21 16:00:26 -04006516 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6517 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6518 NULL, NULL);
6519 BUG_ON(ret < 0);
6520 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006521 /* if we fail to delete the orphan item this time
6522 * around, it'll get picked up the next time.
6523 *
6524 * The most common failure here is just -ENOENT.
6525 */
6526 btrfs_del_orphan_item(trans, tree_root,
6527 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006528 }
6529 }
6530
6531 if (root->in_radix) {
6532 btrfs_free_fs_root(tree_root->fs_info, root);
6533 } else {
6534 free_extent_buffer(root->node);
6535 free_extent_buffer(root->commit_root);
6536 kfree(root);
6537 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006538out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006539 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006540 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006541 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006542 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006543}
Chris Mason9078a3e2007-04-26 16:46:15 -04006544
Yan Zheng2c47e6052009-06-27 21:07:35 -04006545/*
6546 * drop subtree rooted at tree block 'node'.
6547 *
6548 * NOTE: this function will unlock and release tree block 'node'
6549 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006550int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6551 struct btrfs_root *root,
6552 struct extent_buffer *node,
6553 struct extent_buffer *parent)
6554{
6555 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006556 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006557 int level;
6558 int parent_level;
6559 int ret = 0;
6560 int wret;
6561
Yan Zheng2c47e6052009-06-27 21:07:35 -04006562 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6563
Yan Zhengf82d02d2008-10-29 14:49:05 -04006564 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006565 if (!path)
6566 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006567
Yan Zheng2c47e6052009-06-27 21:07:35 -04006568 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006569 if (!wc) {
6570 btrfs_free_path(path);
6571 return -ENOMEM;
6572 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006573
Chris Masonb9447ef2009-03-09 11:45:38 -04006574 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006575 parent_level = btrfs_header_level(parent);
6576 extent_buffer_get(parent);
6577 path->nodes[parent_level] = parent;
6578 path->slots[parent_level] = btrfs_header_nritems(parent);
6579
Chris Masonb9447ef2009-03-09 11:45:38 -04006580 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006581 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006582 path->nodes[level] = node;
6583 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006584 path->locks[level] = 1;
6585
6586 wc->refs[parent_level] = 1;
6587 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6588 wc->level = level;
6589 wc->shared_level = -1;
6590 wc->stage = DROP_REFERENCE;
6591 wc->update_ref = 0;
6592 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006593 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006594
6595 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006596 wret = walk_down_tree(trans, root, path, wc);
6597 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006598 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006599 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006600 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006601
Yan Zheng2c47e6052009-06-27 21:07:35 -04006602 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006603 if (wret < 0)
6604 ret = wret;
6605 if (wret != 0)
6606 break;
6607 }
6608
Yan Zheng2c47e6052009-06-27 21:07:35 -04006609 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006610 btrfs_free_path(path);
6611 return ret;
6612}
6613
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006614#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04006615static unsigned long calc_ra(unsigned long start, unsigned long last,
6616 unsigned long nr)
6617{
6618 return min(last, start + nr - 1);
6619}
6620
Chris Masond3977122009-01-05 21:25:51 -05006621static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05006622 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05006623{
6624 u64 page_start;
6625 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04006626 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006627 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006628 unsigned long i;
6629 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05006630 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05006631 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04006632 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04006633 unsigned int total_read = 0;
6634 unsigned int total_dirty = 0;
6635 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05006636
6637 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00006638 if (!ra)
6639 return -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006640
6641 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006642 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05006643 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6644
Zheng Yan1a40e232008-09-26 10:09:34 -04006645 /* make sure the dirty trick played by the caller work */
6646 ret = invalidate_inode_pages2_range(inode->i_mapping,
6647 first_index, last_index);
6648 if (ret)
6649 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04006650
Chris Mason4313b392008-01-03 09:08:48 -05006651 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05006652
Zheng Yan1a40e232008-09-26 10:09:34 -04006653 for (i = first_index ; i <= last_index; i++) {
6654 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04006655 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04006656 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04006657 }
6658 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04006659again:
6660 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04006661 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05006662 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04006663 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006664 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006665 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04006666 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006667 if (!PageUptodate(page)) {
6668 btrfs_readpage(NULL, page);
6669 lock_page(page);
6670 if (!PageUptodate(page)) {
6671 unlock_page(page);
6672 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006673 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05006674 goto out_unlock;
6675 }
6676 }
Chris Masonec44a352008-04-28 15:29:52 -04006677 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04006678
Chris Masonedbd8d42007-12-21 16:27:24 -05006679 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6680 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006681 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006682
Chris Mason3eaa2882008-07-24 11:57:52 -04006683 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6684 if (ordered) {
6685 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6686 unlock_page(page);
6687 page_cache_release(page);
6688 btrfs_start_ordered_extent(inode, ordered, 1);
6689 btrfs_put_ordered_extent(ordered);
6690 goto again;
6691 }
6692 set_page_extent_mapped(page);
6693
Zheng Yan1a40e232008-09-26 10:09:34 -04006694 if (i == first_index)
6695 set_extent_bits(io_tree, page_start, page_end,
6696 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006697 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04006698
Chris Masona061fc82008-05-07 11:43:44 -04006699 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006700 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006701
Chris Masond1310b22008-01-24 16:13:08 -05006702 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006703 unlock_page(page);
6704 page_cache_release(page);
6705 }
6706
6707out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04006708 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05006709 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006710 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04006711 return ret;
6712}
6713
Chris Masond3977122009-01-05 21:25:51 -05006714static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006715 struct btrfs_key *extent_key,
6716 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05006717{
Zheng Yan1a40e232008-09-26 10:09:34 -04006718 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6719 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6720 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04006721 u64 start = extent_key->objectid - offset;
6722 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006723
6724 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +00006725 BUG_ON(!em);
Zheng Yan1a40e232008-09-26 10:09:34 -04006726
Yan Zheng66435582008-10-30 14:19:50 -04006727 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006728 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04006729 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04006730 em->block_start = extent_key->objectid;
6731 em->bdev = root->fs_info->fs_devices->latest_bdev;
6732 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6733
6734 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04006735 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006736 while (1) {
6737 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04006738 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006739 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04006740 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006741 if (ret != -EEXIST) {
6742 free_extent_map(em);
6743 break;
6744 }
Yan Zheng66435582008-10-30 14:19:50 -04006745 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006746 }
Yan Zheng66435582008-10-30 14:19:50 -04006747 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006748
Yan Zheng66435582008-10-30 14:19:50 -04006749 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04006750}
6751
6752struct btrfs_ref_path {
6753 u64 extent_start;
6754 u64 nodes[BTRFS_MAX_LEVEL];
6755 u64 root_objectid;
6756 u64 root_generation;
6757 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006758 u32 num_refs;
6759 int lowest_level;
6760 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006761 int shared_level;
6762
6763 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6764 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04006765};
6766
6767struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04006768 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04006769 u64 disk_bytenr;
6770 u64 disk_num_bytes;
6771 u64 offset;
6772 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04006773 u8 compression;
6774 u8 encryption;
6775 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04006776};
6777
6778static int is_cowonly_root(u64 root_objectid)
6779{
6780 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6781 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6782 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6783 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05006784 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6785 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04006786 return 1;
6787 return 0;
6788}
6789
Chris Masond3977122009-01-05 21:25:51 -05006790static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006791 struct btrfs_root *extent_root,
6792 struct btrfs_ref_path *ref_path,
6793 int first_time)
6794{
6795 struct extent_buffer *leaf;
6796 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05006797 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05006798 struct btrfs_key key;
6799 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04006800 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05006801 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04006802 int level;
6803 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006804
Zheng Yan1a40e232008-09-26 10:09:34 -04006805 path = btrfs_alloc_path();
6806 if (!path)
6807 return -ENOMEM;
6808
Zheng Yan1a40e232008-09-26 10:09:34 -04006809 if (first_time) {
6810 ref_path->lowest_level = -1;
6811 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006812 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006813 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04006814 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006815walk_down:
6816 level = ref_path->current_level - 1;
6817 while (level >= -1) {
6818 u64 parent;
6819 if (level < ref_path->lowest_level)
6820 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006821
Chris Masond3977122009-01-05 21:25:51 -05006822 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006823 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006824 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006825 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006826 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006827
Zheng Yan1a40e232008-09-26 10:09:34 -04006828 parent = ref_path->nodes[level + 1];
6829 ref_path->nodes[level + 1] = 0;
6830 ref_path->current_level = level;
6831 BUG_ON(parent == 0);
6832
6833 key.objectid = bytenr;
6834 key.offset = parent + 1;
6835 key.type = BTRFS_EXTENT_REF_KEY;
6836
6837 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006838 if (ret < 0)
6839 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006840 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006841
Chris Masonedbd8d42007-12-21 16:27:24 -05006842 leaf = path->nodes[0];
6843 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04006844 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04006845 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04006846 if (ret < 0)
6847 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006848 if (ret > 0)
6849 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04006850 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04006851 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006852
6853 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04006854 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04006855 found_key.type == BTRFS_EXTENT_REF_KEY) {
6856 if (level < ref_path->shared_level)
6857 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006858 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006859 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006860next:
6861 level--;
6862 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006863 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006864 }
6865 /* reached lowest level */
6866 ret = 1;
6867 goto out;
6868walk_up:
6869 level = ref_path->current_level;
6870 while (level < BTRFS_MAX_LEVEL - 1) {
6871 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006872
6873 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006874 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006875 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006876 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006877
Zheng Yan1a40e232008-09-26 10:09:34 -04006878 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006879
Zheng Yan1a40e232008-09-26 10:09:34 -04006880 key.objectid = bytenr;
6881 key.offset = 0;
6882 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006883
Zheng Yan1a40e232008-09-26 10:09:34 -04006884 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6885 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006886 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006887
6888 leaf = path->nodes[0];
6889 nritems = btrfs_header_nritems(leaf);
6890 if (path->slots[0] >= nritems) {
6891 ret = btrfs_next_leaf(extent_root, path);
6892 if (ret < 0)
6893 goto out;
6894 if (ret > 0) {
6895 /* the extent was freed by someone */
6896 if (ref_path->lowest_level == level)
6897 goto out;
6898 btrfs_release_path(extent_root, path);
6899 goto walk_down;
6900 }
6901 leaf = path->nodes[0];
6902 }
6903
6904 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6905 if (found_key.objectid != bytenr ||
6906 found_key.type != BTRFS_EXTENT_REF_KEY) {
6907 /* the extent was freed by someone */
6908 if (ref_path->lowest_level == level) {
6909 ret = 1;
6910 goto out;
6911 }
6912 btrfs_release_path(extent_root, path);
6913 goto walk_down;
6914 }
6915found:
6916 ref = btrfs_item_ptr(leaf, path->slots[0],
6917 struct btrfs_extent_ref);
6918 ref_objectid = btrfs_ref_objectid(leaf, ref);
6919 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6920 if (first_time) {
6921 level = (int)ref_objectid;
6922 BUG_ON(level >= BTRFS_MAX_LEVEL);
6923 ref_path->lowest_level = level;
6924 ref_path->current_level = level;
6925 ref_path->nodes[level] = bytenr;
6926 } else {
6927 WARN_ON(ref_objectid != level);
6928 }
6929 } else {
6930 WARN_ON(level != -1);
6931 }
6932 first_time = 0;
6933
6934 if (ref_path->lowest_level == level) {
6935 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006936 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6937 }
6938
6939 /*
6940 * the block is tree root or the block isn't in reference
6941 * counted tree.
6942 */
6943 if (found_key.objectid == found_key.offset ||
6944 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6945 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6946 ref_path->root_generation =
6947 btrfs_ref_generation(leaf, ref);
6948 if (level < 0) {
6949 /* special reference from the tree log */
6950 ref_path->nodes[0] = found_key.offset;
6951 ref_path->current_level = 0;
6952 }
6953 ret = 0;
6954 goto out;
6955 }
6956
6957 level++;
6958 BUG_ON(ref_path->nodes[level] != 0);
6959 ref_path->nodes[level] = found_key.offset;
6960 ref_path->current_level = level;
6961
6962 /*
6963 * the reference was created in the running transaction,
6964 * no need to continue walking up.
6965 */
6966 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6967 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6968 ref_path->root_generation =
6969 btrfs_ref_generation(leaf, ref);
6970 ret = 0;
6971 goto out;
6972 }
6973
6974 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006975 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006976 }
6977 /* reached max tree level, but no tree root found. */
6978 BUG();
6979out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006980 btrfs_free_path(path);
6981 return ret;
6982}
6983
6984static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6985 struct btrfs_root *extent_root,
6986 struct btrfs_ref_path *ref_path,
6987 u64 extent_start)
6988{
6989 memset(ref_path, 0, sizeof(*ref_path));
6990 ref_path->extent_start = extent_start;
6991
6992 return __next_ref_path(trans, extent_root, ref_path, 1);
6993}
6994
6995static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6996 struct btrfs_root *extent_root,
6997 struct btrfs_ref_path *ref_path)
6998{
6999 return __next_ref_path(trans, extent_root, ref_path, 0);
7000}
7001
Chris Masond3977122009-01-05 21:25:51 -05007002static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04007003 struct btrfs_key *extent_key,
7004 u64 offset, int no_fragment,
7005 struct disk_extent **extents,
7006 int *nr_extents)
7007{
7008 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
7009 struct btrfs_path *path;
7010 struct btrfs_file_extent_item *fi;
7011 struct extent_buffer *leaf;
7012 struct disk_extent *exts = *extents;
7013 struct btrfs_key found_key;
7014 u64 cur_pos;
7015 u64 last_byte;
7016 u32 nritems;
7017 int nr = 0;
7018 int max = *nr_extents;
7019 int ret;
7020
7021 WARN_ON(!no_fragment && *extents);
7022 if (!exts) {
7023 max = 1;
7024 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
7025 if (!exts)
7026 return -ENOMEM;
7027 }
7028
7029 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007030 if (!path) {
7031 if (exts != *extents)
7032 kfree(exts);
7033 return -ENOMEM;
7034 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007035
7036 cur_pos = extent_key->objectid - offset;
7037 last_byte = extent_key->objectid + extent_key->offset;
7038 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
7039 cur_pos, 0);
7040 if (ret < 0)
7041 goto out;
7042 if (ret > 0) {
7043 ret = -ENOENT;
7044 goto out;
7045 }
7046
7047 while (1) {
7048 leaf = path->nodes[0];
7049 nritems = btrfs_header_nritems(leaf);
7050 if (path->slots[0] >= nritems) {
7051 ret = btrfs_next_leaf(root, path);
7052 if (ret < 0)
7053 goto out;
7054 if (ret > 0)
7055 break;
7056 leaf = path->nodes[0];
7057 }
7058
7059 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7060 if (found_key.offset != cur_pos ||
7061 found_key.type != BTRFS_EXTENT_DATA_KEY ||
7062 found_key.objectid != reloc_inode->i_ino)
7063 break;
7064
7065 fi = btrfs_item_ptr(leaf, path->slots[0],
7066 struct btrfs_file_extent_item);
7067 if (btrfs_file_extent_type(leaf, fi) !=
7068 BTRFS_FILE_EXTENT_REG ||
7069 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7070 break;
7071
7072 if (nr == max) {
7073 struct disk_extent *old = exts;
7074 max *= 2;
7075 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
Yoshinori Sanodac97e52011-02-15 12:01:42 +00007076 if (!exts) {
7077 ret = -ENOMEM;
7078 goto out;
7079 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007080 memcpy(exts, old, sizeof(*exts) * nr);
7081 if (old != *extents)
7082 kfree(old);
7083 }
7084
7085 exts[nr].disk_bytenr =
7086 btrfs_file_extent_disk_bytenr(leaf, fi);
7087 exts[nr].disk_num_bytes =
7088 btrfs_file_extent_disk_num_bytes(leaf, fi);
7089 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
7090 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04007091 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7092 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
7093 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
7094 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
7095 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04007096 BUG_ON(exts[nr].offset > 0);
7097 BUG_ON(exts[nr].compression || exts[nr].encryption);
7098 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007099
7100 cur_pos += exts[nr].num_bytes;
7101 nr++;
7102
7103 if (cur_pos + offset >= last_byte)
7104 break;
7105
7106 if (no_fragment) {
7107 ret = 1;
7108 goto out;
7109 }
7110 path->slots[0]++;
7111 }
7112
Yan Zheng1f80e4d2008-12-19 10:59:04 -05007113 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04007114 if (cur_pos + offset < last_byte) {
7115 ret = -ENOENT;
7116 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05007117 }
7118 ret = 0;
7119out:
Zheng Yan1a40e232008-09-26 10:09:34 -04007120 btrfs_free_path(path);
7121 if (ret) {
7122 if (exts != *extents)
7123 kfree(exts);
7124 } else {
7125 *extents = exts;
7126 *nr_extents = nr;
7127 }
7128 return ret;
7129}
7130
Chris Masond3977122009-01-05 21:25:51 -05007131static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007132 struct btrfs_root *root,
7133 struct btrfs_path *path,
7134 struct btrfs_key *extent_key,
7135 struct btrfs_key *leaf_key,
7136 struct btrfs_ref_path *ref_path,
7137 struct disk_extent *new_extents,
7138 int nr_extents)
7139{
7140 struct extent_buffer *leaf;
7141 struct btrfs_file_extent_item *fi;
7142 struct inode *inode = NULL;
7143 struct btrfs_key key;
7144 u64 lock_start = 0;
7145 u64 lock_end = 0;
7146 u64 num_bytes;
7147 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05007148 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007149 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007150 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007151 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04007152 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04007153 int ret;
7154
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007155 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04007156 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007157 if (key.objectid < ref_path->owner_objectid ||
7158 (key.objectid == ref_path->owner_objectid &&
7159 key.type < BTRFS_EXTENT_DATA_KEY)) {
7160 key.objectid = ref_path->owner_objectid;
7161 key.type = BTRFS_EXTENT_DATA_KEY;
7162 key.offset = 0;
7163 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007164 }
7165
7166 while (1) {
7167 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7168 if (ret < 0)
7169 goto out;
7170
7171 leaf = path->nodes[0];
7172 nritems = btrfs_header_nritems(leaf);
7173next:
7174 if (extent_locked && ret > 0) {
7175 /*
7176 * the file extent item was modified by someone
7177 * before the extent got locked.
7178 */
Zheng Yan1a40e232008-09-26 10:09:34 -04007179 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7180 lock_end, GFP_NOFS);
7181 extent_locked = 0;
7182 }
7183
7184 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007185 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04007186 break;
7187
7188 BUG_ON(extent_locked);
7189 ret = btrfs_next_leaf(root, path);
7190 if (ret < 0)
7191 goto out;
7192 if (ret > 0)
7193 break;
7194 leaf = path->nodes[0];
7195 nritems = btrfs_header_nritems(leaf);
7196 }
7197
7198 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7199
7200 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7201 if ((key.objectid > ref_path->owner_objectid) ||
7202 (key.objectid == ref_path->owner_objectid &&
7203 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05007204 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007205 break;
7206 }
7207
7208 if (inode && key.objectid != inode->i_ino) {
7209 BUG_ON(extent_locked);
7210 btrfs_release_path(root, path);
7211 mutex_unlock(&inode->i_mutex);
7212 iput(inode);
7213 inode = NULL;
7214 continue;
7215 }
7216
7217 if (key.type != BTRFS_EXTENT_DATA_KEY) {
7218 path->slots[0]++;
7219 ret = 1;
7220 goto next;
7221 }
7222 fi = btrfs_item_ptr(leaf, path->slots[0],
7223 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04007224 extent_type = btrfs_file_extent_type(leaf, fi);
7225 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
7226 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04007227 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
7228 extent_key->objectid)) {
7229 path->slots[0]++;
7230 ret = 1;
7231 goto next;
7232 }
7233
7234 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7235 ext_offset = btrfs_file_extent_offset(leaf, fi);
7236
Yan Zheng86288a12009-01-21 10:49:16 -05007237 if (search_end == (u64)-1) {
7238 search_end = key.offset - ext_offset +
7239 btrfs_file_extent_ram_bytes(leaf, fi);
7240 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007241
7242 if (!extent_locked) {
7243 lock_start = key.offset;
7244 lock_end = lock_start + num_bytes - 1;
7245 } else {
Yan Zheng66435582008-10-30 14:19:50 -04007246 if (lock_start > key.offset ||
7247 lock_end + 1 < key.offset + num_bytes) {
7248 unlock_extent(&BTRFS_I(inode)->io_tree,
7249 lock_start, lock_end, GFP_NOFS);
7250 extent_locked = 0;
7251 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007252 }
7253
7254 if (!inode) {
7255 btrfs_release_path(root, path);
7256
7257 inode = btrfs_iget_locked(root->fs_info->sb,
7258 key.objectid, root);
7259 if (inode->i_state & I_NEW) {
7260 BTRFS_I(inode)->root = root;
7261 BTRFS_I(inode)->location.objectid =
7262 key.objectid;
7263 BTRFS_I(inode)->location.type =
7264 BTRFS_INODE_ITEM_KEY;
7265 BTRFS_I(inode)->location.offset = 0;
7266 btrfs_read_locked_inode(inode);
7267 unlock_new_inode(inode);
7268 }
7269 /*
7270 * some code call btrfs_commit_transaction while
7271 * holding the i_mutex, so we can't use mutex_lock
7272 * here.
7273 */
7274 if (is_bad_inode(inode) ||
7275 !mutex_trylock(&inode->i_mutex)) {
7276 iput(inode);
7277 inode = NULL;
7278 key.offset = (u64)-1;
7279 goto skip;
7280 }
7281 }
7282
7283 if (!extent_locked) {
7284 struct btrfs_ordered_extent *ordered;
7285
7286 btrfs_release_path(root, path);
7287
7288 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7289 lock_end, GFP_NOFS);
7290 ordered = btrfs_lookup_first_ordered_extent(inode,
7291 lock_end);
7292 if (ordered &&
7293 ordered->file_offset <= lock_end &&
7294 ordered->file_offset + ordered->len > lock_start) {
7295 unlock_extent(&BTRFS_I(inode)->io_tree,
7296 lock_start, lock_end, GFP_NOFS);
7297 btrfs_start_ordered_extent(inode, ordered, 1);
7298 btrfs_put_ordered_extent(ordered);
7299 key.offset += num_bytes;
7300 goto skip;
7301 }
7302 if (ordered)
7303 btrfs_put_ordered_extent(ordered);
7304
Zheng Yan1a40e232008-09-26 10:09:34 -04007305 extent_locked = 1;
7306 continue;
7307 }
7308
7309 if (nr_extents == 1) {
7310 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04007311 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7312 new_extents[0].disk_bytenr);
7313 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7314 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007315 btrfs_mark_buffer_dirty(leaf);
7316
7317 btrfs_drop_extent_cache(inode, key.offset,
7318 key.offset + num_bytes - 1, 0);
7319
7320 ret = btrfs_inc_extent_ref(trans, root,
7321 new_extents[0].disk_bytenr,
7322 new_extents[0].disk_num_bytes,
7323 leaf->start,
7324 root->root_key.objectid,
7325 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007326 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007327 BUG_ON(ret);
7328
7329 ret = btrfs_free_extent(trans, root,
7330 extent_key->objectid,
7331 extent_key->offset,
7332 leaf->start,
7333 btrfs_header_owner(leaf),
7334 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007335 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007336 BUG_ON(ret);
7337
7338 btrfs_release_path(root, path);
7339 key.offset += num_bytes;
7340 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04007341 BUG_ON(1);
7342#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04007343 u64 alloc_hint;
7344 u64 extent_len;
7345 int i;
7346 /*
7347 * drop old extent pointer at first, then insert the
7348 * new pointers one bye one
7349 */
7350 btrfs_release_path(root, path);
7351 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7352 key.offset + num_bytes,
7353 key.offset, &alloc_hint);
7354 BUG_ON(ret);
7355
7356 for (i = 0; i < nr_extents; i++) {
7357 if (ext_offset >= new_extents[i].num_bytes) {
7358 ext_offset -= new_extents[i].num_bytes;
7359 continue;
7360 }
7361 extent_len = min(new_extents[i].num_bytes -
7362 ext_offset, num_bytes);
7363
7364 ret = btrfs_insert_empty_item(trans, root,
7365 path, &key,
7366 sizeof(*fi));
7367 BUG_ON(ret);
7368
7369 leaf = path->nodes[0];
7370 fi = btrfs_item_ptr(leaf, path->slots[0],
7371 struct btrfs_file_extent_item);
7372 btrfs_set_file_extent_generation(leaf, fi,
7373 trans->transid);
7374 btrfs_set_file_extent_type(leaf, fi,
7375 BTRFS_FILE_EXTENT_REG);
7376 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7377 new_extents[i].disk_bytenr);
7378 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7379 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04007380 btrfs_set_file_extent_ram_bytes(leaf, fi,
7381 new_extents[i].ram_bytes);
7382
7383 btrfs_set_file_extent_compression(leaf, fi,
7384 new_extents[i].compression);
7385 btrfs_set_file_extent_encryption(leaf, fi,
7386 new_extents[i].encryption);
7387 btrfs_set_file_extent_other_encoding(leaf, fi,
7388 new_extents[i].other_encoding);
7389
Zheng Yan1a40e232008-09-26 10:09:34 -04007390 btrfs_set_file_extent_num_bytes(leaf, fi,
7391 extent_len);
7392 ext_offset += new_extents[i].offset;
7393 btrfs_set_file_extent_offset(leaf, fi,
7394 ext_offset);
7395 btrfs_mark_buffer_dirty(leaf);
7396
7397 btrfs_drop_extent_cache(inode, key.offset,
7398 key.offset + extent_len - 1, 0);
7399
7400 ret = btrfs_inc_extent_ref(trans, root,
7401 new_extents[i].disk_bytenr,
7402 new_extents[i].disk_num_bytes,
7403 leaf->start,
7404 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007405 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007406 BUG_ON(ret);
7407 btrfs_release_path(root, path);
7408
Yan Zhenga76a3cd2008-10-09 11:46:29 -04007409 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04007410
7411 ext_offset = 0;
7412 num_bytes -= extent_len;
7413 key.offset += extent_len;
7414
7415 if (num_bytes == 0)
7416 break;
7417 }
7418 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007419#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04007420 }
7421
7422 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007423 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7424 lock_end, GFP_NOFS);
7425 extent_locked = 0;
7426 }
7427skip:
7428 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05007429 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007430 break;
7431
7432 cond_resched();
7433 }
7434 ret = 0;
7435out:
7436 btrfs_release_path(root, path);
7437 if (inode) {
7438 mutex_unlock(&inode->i_mutex);
7439 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007440 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7441 lock_end, GFP_NOFS);
7442 }
7443 iput(inode);
7444 }
7445 return ret;
7446}
7447
Zheng Yan1a40e232008-09-26 10:09:34 -04007448int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7449 struct btrfs_root *root,
7450 struct extent_buffer *buf, u64 orig_start)
7451{
7452 int level;
7453 int ret;
7454
7455 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7456 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7457
7458 level = btrfs_header_level(buf);
7459 if (level == 0) {
7460 struct btrfs_leaf_ref *ref;
7461 struct btrfs_leaf_ref *orig_ref;
7462
7463 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7464 if (!orig_ref)
7465 return -ENOENT;
7466
7467 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7468 if (!ref) {
7469 btrfs_free_leaf_ref(root, orig_ref);
7470 return -ENOMEM;
7471 }
7472
7473 ref->nritems = orig_ref->nritems;
7474 memcpy(ref->extents, orig_ref->extents,
7475 sizeof(ref->extents[0]) * ref->nritems);
7476
7477 btrfs_free_leaf_ref(root, orig_ref);
7478
7479 ref->root_gen = trans->transid;
7480 ref->bytenr = buf->start;
7481 ref->owner = btrfs_header_owner(buf);
7482 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05007483
Zheng Yan1a40e232008-09-26 10:09:34 -04007484 ret = btrfs_add_leaf_ref(root, ref, 0);
7485 WARN_ON(ret);
7486 btrfs_free_leaf_ref(root, ref);
7487 }
7488 return 0;
7489}
7490
Chris Masond3977122009-01-05 21:25:51 -05007491static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007492 struct extent_buffer *leaf,
7493 struct btrfs_block_group_cache *group,
7494 struct btrfs_root *target_root)
7495{
7496 struct btrfs_key key;
7497 struct inode *inode = NULL;
7498 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007499 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04007500 u64 num_bytes;
7501 u64 skip_objectid = 0;
7502 u32 nritems;
7503 u32 i;
7504
7505 nritems = btrfs_header_nritems(leaf);
7506 for (i = 0; i < nritems; i++) {
7507 btrfs_item_key_to_cpu(leaf, &key, i);
7508 if (key.objectid == skip_objectid ||
7509 key.type != BTRFS_EXTENT_DATA_KEY)
7510 continue;
7511 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7512 if (btrfs_file_extent_type(leaf, fi) ==
7513 BTRFS_FILE_EXTENT_INLINE)
7514 continue;
7515 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7516 continue;
7517 if (!inode || inode->i_ino != key.objectid) {
7518 iput(inode);
7519 inode = btrfs_ilookup(target_root->fs_info->sb,
7520 key.objectid, target_root, 1);
7521 }
7522 if (!inode) {
7523 skip_objectid = key.objectid;
7524 continue;
7525 }
7526 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7527
Josef Bacik2ac55d42010-02-03 19:33:23 +00007528 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7529 key.offset + num_bytes - 1, 0, &cached_state,
7530 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007531 btrfs_drop_extent_cache(inode, key.offset,
7532 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007533 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7534 key.offset + num_bytes - 1, &cached_state,
7535 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007536 cond_resched();
7537 }
7538 iput(inode);
7539 return 0;
7540}
7541
Chris Masond3977122009-01-05 21:25:51 -05007542static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007543 struct btrfs_root *root,
7544 struct extent_buffer *leaf,
7545 struct btrfs_block_group_cache *group,
7546 struct inode *reloc_inode)
7547{
7548 struct btrfs_key key;
7549 struct btrfs_key extent_key;
7550 struct btrfs_file_extent_item *fi;
7551 struct btrfs_leaf_ref *ref;
7552 struct disk_extent *new_extent;
7553 u64 bytenr;
7554 u64 num_bytes;
7555 u32 nritems;
7556 u32 i;
7557 int ext_index;
7558 int nr_extent;
7559 int ret;
7560
7561 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007562 if (!new_extent)
7563 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007564
7565 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7566 BUG_ON(!ref);
7567
7568 ext_index = -1;
7569 nritems = btrfs_header_nritems(leaf);
7570 for (i = 0; i < nritems; i++) {
7571 btrfs_item_key_to_cpu(leaf, &key, i);
7572 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7573 continue;
7574 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7575 if (btrfs_file_extent_type(leaf, fi) ==
7576 BTRFS_FILE_EXTENT_INLINE)
7577 continue;
7578 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7579 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7580 if (bytenr == 0)
7581 continue;
7582
7583 ext_index++;
7584 if (bytenr >= group->key.objectid + group->key.offset ||
7585 bytenr + num_bytes <= group->key.objectid)
7586 continue;
7587
7588 extent_key.objectid = bytenr;
7589 extent_key.offset = num_bytes;
7590 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7591 nr_extent = 1;
7592 ret = get_new_locations(reloc_inode, &extent_key,
7593 group->key.objectid, 1,
7594 &new_extent, &nr_extent);
7595 if (ret > 0)
7596 continue;
7597 BUG_ON(ret < 0);
7598
7599 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7600 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7601 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7602 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7603
Zheng Yan1a40e232008-09-26 10:09:34 -04007604 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7605 new_extent->disk_bytenr);
7606 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7607 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007608 btrfs_mark_buffer_dirty(leaf);
7609
7610 ret = btrfs_inc_extent_ref(trans, root,
7611 new_extent->disk_bytenr,
7612 new_extent->disk_num_bytes,
7613 leaf->start,
7614 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007615 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007616 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04007617
Zheng Yan1a40e232008-09-26 10:09:34 -04007618 ret = btrfs_free_extent(trans, root,
7619 bytenr, num_bytes, leaf->start,
7620 btrfs_header_owner(leaf),
7621 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007622 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007623 BUG_ON(ret);
7624 cond_resched();
7625 }
7626 kfree(new_extent);
7627 BUG_ON(ext_index + 1 != ref->nritems);
7628 btrfs_free_leaf_ref(root, ref);
7629 return 0;
7630}
7631
Yan Zhengf82d02d2008-10-29 14:49:05 -04007632int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7633 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04007634{
7635 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007636 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007637
7638 if (root->reloc_root) {
7639 reloc_root = root->reloc_root;
7640 root->reloc_root = NULL;
7641 list_add(&reloc_root->dead_list,
7642 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007643
7644 btrfs_set_root_bytenr(&reloc_root->root_item,
7645 reloc_root->node->start);
7646 btrfs_set_root_level(&root->root_item,
7647 btrfs_header_level(reloc_root->node));
7648 memset(&reloc_root->root_item.drop_progress, 0,
7649 sizeof(struct btrfs_disk_key));
7650 reloc_root->root_item.drop_level = 0;
7651
7652 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7653 &reloc_root->root_key,
7654 &reloc_root->root_item);
7655 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007656 }
7657 return 0;
7658}
7659
7660int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7661{
7662 struct btrfs_trans_handle *trans;
7663 struct btrfs_root *reloc_root;
7664 struct btrfs_root *prev_root = NULL;
7665 struct list_head dead_roots;
7666 int ret;
7667 unsigned long nr;
7668
7669 INIT_LIST_HEAD(&dead_roots);
7670 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7671
7672 while (!list_empty(&dead_roots)) {
7673 reloc_root = list_entry(dead_roots.prev,
7674 struct btrfs_root, dead_list);
7675 list_del_init(&reloc_root->dead_list);
7676
7677 BUG_ON(reloc_root->commit_root != NULL);
7678 while (1) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007679 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007680 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007681
7682 mutex_lock(&root->fs_info->drop_mutex);
7683 ret = btrfs_drop_snapshot(trans, reloc_root);
7684 if (ret != -EAGAIN)
7685 break;
7686 mutex_unlock(&root->fs_info->drop_mutex);
7687
7688 nr = trans->blocks_used;
7689 ret = btrfs_end_transaction(trans, root);
7690 BUG_ON(ret);
7691 btrfs_btree_balance_dirty(root, nr);
7692 }
7693
7694 free_extent_buffer(reloc_root->node);
7695
7696 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7697 &reloc_root->root_key);
7698 BUG_ON(ret);
7699 mutex_unlock(&root->fs_info->drop_mutex);
7700
7701 nr = trans->blocks_used;
7702 ret = btrfs_end_transaction(trans, root);
7703 BUG_ON(ret);
7704 btrfs_btree_balance_dirty(root, nr);
7705
7706 kfree(prev_root);
7707 prev_root = reloc_root;
7708 }
7709 if (prev_root) {
7710 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7711 kfree(prev_root);
7712 }
7713 return 0;
7714}
7715
7716int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7717{
7718 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7719 return 0;
7720}
7721
7722int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7723{
7724 struct btrfs_root *reloc_root;
7725 struct btrfs_trans_handle *trans;
7726 struct btrfs_key location;
7727 int found;
7728 int ret;
7729
7730 mutex_lock(&root->fs_info->tree_reloc_mutex);
7731 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7732 BUG_ON(ret);
7733 found = !list_empty(&root->fs_info->dead_reloc_roots);
7734 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7735
7736 if (found) {
7737 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007738 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007739 ret = btrfs_commit_transaction(trans, root);
7740 BUG_ON(ret);
7741 }
7742
7743 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7744 location.offset = (u64)-1;
7745 location.type = BTRFS_ROOT_ITEM_KEY;
7746
7747 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7748 BUG_ON(!reloc_root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05007749 ret = btrfs_orphan_cleanup(reloc_root);
7750 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007751 return 0;
7752}
7753
Chris Masond3977122009-01-05 21:25:51 -05007754static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007755 struct btrfs_root *root)
7756{
7757 struct btrfs_root *reloc_root;
7758 struct extent_buffer *eb;
7759 struct btrfs_root_item *root_item;
7760 struct btrfs_key root_key;
7761 int ret;
7762
7763 BUG_ON(!root->ref_cows);
7764 if (root->reloc_root)
7765 return 0;
7766
7767 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007768 if (!root_item)
7769 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007770
7771 ret = btrfs_copy_root(trans, root, root->commit_root,
7772 &eb, BTRFS_TREE_RELOC_OBJECTID);
7773 BUG_ON(ret);
7774
7775 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7776 root_key.offset = root->root_key.objectid;
7777 root_key.type = BTRFS_ROOT_ITEM_KEY;
7778
7779 memcpy(root_item, &root->root_item, sizeof(root_item));
7780 btrfs_set_root_refs(root_item, 0);
7781 btrfs_set_root_bytenr(root_item, eb->start);
7782 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04007783 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007784
7785 btrfs_tree_unlock(eb);
7786 free_extent_buffer(eb);
7787
7788 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7789 &root_key, root_item);
7790 BUG_ON(ret);
7791 kfree(root_item);
7792
7793 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7794 &root_key);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007795 BUG_ON(IS_ERR(reloc_root));
Zheng Yan1a40e232008-09-26 10:09:34 -04007796 reloc_root->last_trans = trans->transid;
7797 reloc_root->commit_root = NULL;
7798 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7799
7800 root->reloc_root = reloc_root;
7801 return 0;
7802}
7803
7804/*
7805 * Core function of space balance.
7806 *
7807 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04007808 * counted roots. There is one reloc tree for each subvol, and all
7809 * reloc trees share same root key objectid. Reloc trees are snapshots
7810 * of the latest committed roots of subvols (root->commit_root).
7811 *
7812 * To relocate a tree block referenced by a subvol, there are two steps.
7813 * COW the block through subvol's reloc tree, then update block pointer
7814 * in the subvol to point to the new block. Since all reloc trees share
7815 * same root key objectid, doing special handing for tree blocks owned
7816 * by them is easy. Once a tree block has been COWed in one reloc tree,
7817 * we can use the resulting new block directly when the same block is
7818 * required to COW again through other reloc trees. By this way, relocated
7819 * tree blocks are shared between reloc trees, so they are also shared
7820 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04007821 */
Chris Masond3977122009-01-05 21:25:51 -05007822static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007823 struct btrfs_root *root,
7824 struct btrfs_path *path,
7825 struct btrfs_key *first_key,
7826 struct btrfs_ref_path *ref_path,
7827 struct btrfs_block_group_cache *group,
7828 struct inode *reloc_inode)
7829{
7830 struct btrfs_root *reloc_root;
7831 struct extent_buffer *eb = NULL;
7832 struct btrfs_key *keys;
7833 u64 *nodes;
7834 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007835 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04007836 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007837 int ret;
7838
7839 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7840 lowest_level = ref_path->owner_objectid;
7841
Yan Zhengf82d02d2008-10-29 14:49:05 -04007842 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007843 path->lowest_level = lowest_level;
7844 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7845 BUG_ON(ret < 0);
7846 path->lowest_level = 0;
7847 btrfs_release_path(root, path);
7848 return 0;
7849 }
7850
Zheng Yan1a40e232008-09-26 10:09:34 -04007851 mutex_lock(&root->fs_info->tree_reloc_mutex);
7852 ret = init_reloc_tree(trans, root);
7853 BUG_ON(ret);
7854 reloc_root = root->reloc_root;
7855
Yan Zhengf82d02d2008-10-29 14:49:05 -04007856 shared_level = ref_path->shared_level;
7857 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007858
Yan Zhengf82d02d2008-10-29 14:49:05 -04007859 keys = ref_path->node_keys;
7860 nodes = ref_path->new_nodes;
7861 memset(&keys[shared_level + 1], 0,
7862 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7863 memset(&nodes[shared_level + 1], 0,
7864 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04007865
Yan Zhengf82d02d2008-10-29 14:49:05 -04007866 if (nodes[lowest_level] == 0) {
7867 path->lowest_level = lowest_level;
7868 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7869 0, 1);
7870 BUG_ON(ret);
7871 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7872 eb = path->nodes[level];
7873 if (!eb || eb == reloc_root->node)
7874 break;
7875 nodes[level] = eb->start;
7876 if (level == 0)
7877 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7878 else
7879 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7880 }
Yan Zheng2b820322008-11-17 21:11:30 -05007881 if (nodes[0] &&
7882 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007883 eb = path->nodes[0];
7884 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7885 group, reloc_inode);
7886 BUG_ON(ret);
7887 }
7888 btrfs_release_path(reloc_root, path);
7889 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007890 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007891 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007892 BUG_ON(ret);
7893 }
7894
Zheng Yan1a40e232008-09-26 10:09:34 -04007895 /*
7896 * replace tree blocks in the fs tree with tree blocks in
7897 * the reloc tree.
7898 */
7899 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7900 BUG_ON(ret < 0);
7901
7902 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007903 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7904 0, 0);
7905 BUG_ON(ret);
7906 extent_buffer_get(path->nodes[0]);
7907 eb = path->nodes[0];
7908 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007909 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7910 BUG_ON(ret);
7911 free_extent_buffer(eb);
7912 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007913
Yan Zhengf82d02d2008-10-29 14:49:05 -04007914 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007915 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007916 return 0;
7917}
7918
Chris Masond3977122009-01-05 21:25:51 -05007919static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007920 struct btrfs_root *root,
7921 struct btrfs_path *path,
7922 struct btrfs_key *first_key,
7923 struct btrfs_ref_path *ref_path)
7924{
7925 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007926
7927 ret = relocate_one_path(trans, root, path, first_key,
7928 ref_path, NULL, NULL);
7929 BUG_ON(ret);
7930
Zheng Yan1a40e232008-09-26 10:09:34 -04007931 return 0;
7932}
7933
Chris Masond3977122009-01-05 21:25:51 -05007934static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007935 struct btrfs_root *extent_root,
7936 struct btrfs_path *path,
7937 struct btrfs_key *extent_key)
7938{
7939 int ret;
7940
Zheng Yan1a40e232008-09-26 10:09:34 -04007941 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7942 if (ret)
7943 goto out;
7944 ret = btrfs_del_item(trans, extent_root, path);
7945out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007946 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007947 return ret;
7948}
7949
Chris Masond3977122009-01-05 21:25:51 -05007950static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007951 struct btrfs_ref_path *ref_path)
7952{
7953 struct btrfs_key root_key;
7954
7955 root_key.objectid = ref_path->root_objectid;
7956 root_key.type = BTRFS_ROOT_ITEM_KEY;
7957 if (is_cowonly_root(ref_path->root_objectid))
7958 root_key.offset = 0;
7959 else
7960 root_key.offset = (u64)-1;
7961
7962 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7963}
7964
Chris Masond3977122009-01-05 21:25:51 -05007965static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007966 struct btrfs_path *path,
7967 struct btrfs_key *extent_key,
7968 struct btrfs_block_group_cache *group,
7969 struct inode *reloc_inode, int pass)
7970{
7971 struct btrfs_trans_handle *trans;
7972 struct btrfs_root *found_root;
7973 struct btrfs_ref_path *ref_path = NULL;
7974 struct disk_extent *new_extents = NULL;
7975 int nr_extents = 0;
7976 int loops;
7977 int ret;
7978 int level;
7979 struct btrfs_key first_key;
7980 u64 prev_block = 0;
7981
Zheng Yan1a40e232008-09-26 10:09:34 -04007982
7983 trans = btrfs_start_transaction(extent_root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007984 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007985
7986 if (extent_key->objectid == 0) {
7987 ret = del_extent_zero(trans, extent_root, path, extent_key);
7988 goto out;
7989 }
7990
7991 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7992 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007993 ret = -ENOMEM;
7994 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007995 }
7996
7997 for (loops = 0; ; loops++) {
7998 if (loops == 0) {
7999 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
8000 extent_key->objectid);
8001 } else {
8002 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
8003 }
8004 if (ret < 0)
8005 goto out;
8006 if (ret > 0)
8007 break;
8008
8009 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
8010 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
8011 continue;
8012
8013 found_root = read_ref_root(extent_root->fs_info, ref_path);
8014 BUG_ON(!found_root);
8015 /*
8016 * for reference counted tree, only process reference paths
8017 * rooted at the latest committed root.
8018 */
8019 if (found_root->ref_cows &&
8020 ref_path->root_generation != found_root->root_key.offset)
8021 continue;
8022
8023 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8024 if (pass == 0) {
8025 /*
8026 * copy data extents to new locations
8027 */
8028 u64 group_start = group->key.objectid;
8029 ret = relocate_data_extent(reloc_inode,
8030 extent_key,
8031 group_start);
8032 if (ret < 0)
8033 goto out;
8034 break;
8035 }
8036 level = 0;
8037 } else {
8038 level = ref_path->owner_objectid;
8039 }
8040
8041 if (prev_block != ref_path->nodes[level]) {
8042 struct extent_buffer *eb;
8043 u64 block_start = ref_path->nodes[level];
8044 u64 block_size = btrfs_level_size(found_root, level);
8045
8046 eb = read_tree_block(found_root, block_start,
8047 block_size, 0);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008048 if (!eb) {
8049 ret = -EIO;
8050 goto out;
8051 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008052 btrfs_tree_lock(eb);
8053 BUG_ON(level != btrfs_header_level(eb));
8054
8055 if (level == 0)
8056 btrfs_item_key_to_cpu(eb, &first_key, 0);
8057 else
8058 btrfs_node_key_to_cpu(eb, &first_key, 0);
8059
8060 btrfs_tree_unlock(eb);
8061 free_extent_buffer(eb);
8062 prev_block = block_start;
8063 }
8064
Yan Zheng24562422009-02-12 14:14:53 -05008065 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05008066 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05008067 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05008068 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8069 /*
8070 * try to update data extent references while
8071 * keeping metadata shared between snapshots.
8072 */
8073 if (pass == 1) {
8074 ret = relocate_one_path(trans, found_root,
8075 path, &first_key, ref_path,
8076 group, reloc_inode);
8077 if (ret < 0)
8078 goto out;
8079 continue;
8080 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008081 /*
8082 * use fallback method to process the remaining
8083 * references.
8084 */
8085 if (!new_extents) {
8086 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04008087 new_extents = kmalloc(sizeof(*new_extents),
8088 GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04008089 if (!new_extents) {
8090 ret = -ENOMEM;
8091 goto out;
8092 }
Yan Zhengd899e052008-10-30 14:25:28 -04008093 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008094 ret = get_new_locations(reloc_inode,
8095 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04008096 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04008097 &new_extents,
8098 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04008099 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04008100 goto out;
8101 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008102 ret = replace_one_extent(trans, found_root,
8103 path, extent_key,
8104 &first_key, ref_path,
8105 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05008106 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04008107 ret = relocate_tree_block(trans, found_root, path,
8108 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04008109 }
8110 if (ret < 0)
8111 goto out;
8112 }
8113 ret = 0;
8114out:
8115 btrfs_end_transaction(trans, extent_root);
8116 kfree(new_extents);
8117 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05008118 return ret;
8119}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008120#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05008121
Chris Masonec44a352008-04-28 15:29:52 -04008122static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8123{
8124 u64 num_devices;
8125 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
8126 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8127
Chris Masoncd02dca2010-12-13 14:56:23 -05008128 /*
8129 * we add in the count of missing devices because we want
8130 * to make sure that any RAID levels on a degraded FS
8131 * continue to be honored.
8132 */
8133 num_devices = root->fs_info->fs_devices->rw_devices +
8134 root->fs_info->fs_devices->missing_devices;
8135
Chris Masonec44a352008-04-28 15:29:52 -04008136 if (num_devices == 1) {
8137 stripped |= BTRFS_BLOCK_GROUP_DUP;
8138 stripped = flags & ~stripped;
8139
8140 /* turn raid0 into single device chunks */
8141 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8142 return stripped;
8143
8144 /* turn mirroring into duplication */
8145 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8146 BTRFS_BLOCK_GROUP_RAID10))
8147 return stripped | BTRFS_BLOCK_GROUP_DUP;
8148 return flags;
8149 } else {
8150 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04008151 if (flags & stripped)
8152 return flags;
8153
8154 stripped |= BTRFS_BLOCK_GROUP_DUP;
8155 stripped = flags & ~stripped;
8156
8157 /* switch duplicated blocks with raid1 */
8158 if (flags & BTRFS_BLOCK_GROUP_DUP)
8159 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8160
8161 /* turn single device chunks into raid0 */
8162 return stripped | BTRFS_BLOCK_GROUP_RAID0;
8163 }
8164 return flags;
8165}
8166
Yan, Zhengf0486c62010-05-16 10:46:25 -04008167static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04008168{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008169 struct btrfs_space_info *sinfo = cache->space_info;
8170 u64 num_bytes;
8171 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04008172
Yan, Zhengf0486c62010-05-16 10:46:25 -04008173 if (cache->ro)
8174 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04008175
Yan, Zhengf0486c62010-05-16 10:46:25 -04008176 spin_lock(&sinfo->lock);
8177 spin_lock(&cache->lock);
8178 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8179 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04008180
Yan, Zhengf0486c62010-05-16 10:46:25 -04008181 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
8182 sinfo->bytes_may_use + sinfo->bytes_readonly +
Chris Mason65e53412010-12-24 06:41:52 -05008183 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04008184 sinfo->bytes_readonly += num_bytes;
8185 sinfo->bytes_reserved += cache->reserved_pinned;
8186 cache->reserved_pinned = 0;
8187 cache->ro = 1;
8188 ret = 0;
8189 }
Chris Mason65e53412010-12-24 06:41:52 -05008190
Yan, Zhengf0486c62010-05-16 10:46:25 -04008191 spin_unlock(&cache->lock);
8192 spin_unlock(&sinfo->lock);
8193 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04008194}
8195
Yan, Zhengf0486c62010-05-16 10:46:25 -04008196int btrfs_set_block_group_ro(struct btrfs_root *root,
8197 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008198
8199{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008200 struct btrfs_trans_handle *trans;
8201 u64 alloc_flags;
8202 int ret;
8203
8204 BUG_ON(cache->ro);
8205
Josef Bacik7a7eaa42011-04-13 12:54:33 -04008206 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008207 BUG_ON(IS_ERR(trans));
8208
8209 alloc_flags = update_block_group_flags(root, cache->flags);
8210 if (alloc_flags != cache->flags)
Chris Mason0e4f8f82011-04-15 16:05:44 -04008211 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8212 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008213
8214 ret = set_block_group_ro(cache);
8215 if (!ret)
8216 goto out;
8217 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Chris Mason0e4f8f82011-04-15 16:05:44 -04008218 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8219 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008220 if (ret < 0)
8221 goto out;
8222 ret = set_block_group_ro(cache);
8223out:
8224 btrfs_end_transaction(trans, root);
8225 return ret;
8226}
8227
Chris Masonc87f08c2011-02-16 13:57:04 -05008228int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8229 struct btrfs_root *root, u64 type)
8230{
8231 u64 alloc_flags = get_alloc_profile(root, type);
Chris Mason0e4f8f82011-04-15 16:05:44 -04008232 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8233 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05008234}
8235
Miao Xie6d07bce2011-01-05 10:07:31 +00008236/*
8237 * helper to account the unused space of all the readonly block group in the
8238 * list. takes mirrors into account.
8239 */
8240static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8241{
8242 struct btrfs_block_group_cache *block_group;
8243 u64 free_bytes = 0;
8244 int factor;
8245
8246 list_for_each_entry(block_group, groups_list, list) {
8247 spin_lock(&block_group->lock);
8248
8249 if (!block_group->ro) {
8250 spin_unlock(&block_group->lock);
8251 continue;
8252 }
8253
8254 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8255 BTRFS_BLOCK_GROUP_RAID10 |
8256 BTRFS_BLOCK_GROUP_DUP))
8257 factor = 2;
8258 else
8259 factor = 1;
8260
8261 free_bytes += (block_group->key.offset -
8262 btrfs_block_group_used(&block_group->item)) *
8263 factor;
8264
8265 spin_unlock(&block_group->lock);
8266 }
8267
8268 return free_bytes;
8269}
8270
8271/*
8272 * helper to account the unused space of all the readonly block group in the
8273 * space_info. takes mirrors into account.
8274 */
8275u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8276{
8277 int i;
8278 u64 free_bytes = 0;
8279
8280 spin_lock(&sinfo->lock);
8281
8282 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
8283 if (!list_empty(&sinfo->block_groups[i]))
8284 free_bytes += __btrfs_get_ro_block_group_free_space(
8285 &sinfo->block_groups[i]);
8286
8287 spin_unlock(&sinfo->lock);
8288
8289 return free_bytes;
8290}
8291
Yan, Zhengf0486c62010-05-16 10:46:25 -04008292int btrfs_set_block_group_rw(struct btrfs_root *root,
8293 struct btrfs_block_group_cache *cache)
8294{
8295 struct btrfs_space_info *sinfo = cache->space_info;
8296 u64 num_bytes;
8297
8298 BUG_ON(!cache->ro);
8299
8300 spin_lock(&sinfo->lock);
8301 spin_lock(&cache->lock);
8302 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8303 cache->bytes_super - btrfs_block_group_used(&cache->item);
8304 sinfo->bytes_readonly -= num_bytes;
8305 cache->ro = 0;
8306 spin_unlock(&cache->lock);
8307 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008308 return 0;
8309}
8310
Josef Bacikba1bf482009-09-11 16:11:19 -04008311/*
8312 * checks to see if its even possible to relocate this block group.
8313 *
8314 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8315 * ok to go ahead and try.
8316 */
8317int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008318{
Zheng Yan1a40e232008-09-26 10:09:34 -04008319 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008320 struct btrfs_space_info *space_info;
8321 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8322 struct btrfs_device *device;
8323 int full = 0;
8324 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008325
Josef Bacikba1bf482009-09-11 16:11:19 -04008326 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008327
Josef Bacikba1bf482009-09-11 16:11:19 -04008328 /* odd, couldn't find the block group, leave it alone */
8329 if (!block_group)
8330 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008331
Josef Bacikba1bf482009-09-11 16:11:19 -04008332 /* no bytes used, we're good */
8333 if (!btrfs_block_group_used(&block_group->item))
8334 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008335
Josef Bacikba1bf482009-09-11 16:11:19 -04008336 space_info = block_group->space_info;
8337 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008338
Josef Bacikba1bf482009-09-11 16:11:19 -04008339 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008340
Josef Bacikba1bf482009-09-11 16:11:19 -04008341 /*
8342 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008343 * relocate it unless we're able to allocate a new chunk below.
8344 *
8345 * Otherwise, we need to make sure we have room in the space to handle
8346 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008347 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008348 if ((space_info->total_bytes != block_group->key.offset) &&
8349 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04008350 space_info->bytes_pinned + space_info->bytes_readonly +
8351 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04008352 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008353 spin_unlock(&space_info->lock);
8354 goto out;
8355 }
8356 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008357
Josef Bacikba1bf482009-09-11 16:11:19 -04008358 /*
8359 * ok we don't have enough space, but maybe we have free space on our
8360 * devices to allocate new chunks for relocation, so loop through our
8361 * alloc devices and guess if we have enough space. However, if we
8362 * were marked as full, then we know there aren't enough chunks, and we
8363 * can just return.
8364 */
8365 ret = -1;
8366 if (full)
8367 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05008368
Josef Bacikba1bf482009-09-11 16:11:19 -04008369 mutex_lock(&root->fs_info->chunk_mutex);
8370 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8371 u64 min_free = btrfs_block_group_used(&block_group->item);
Miao Xie7bfc8372011-01-05 10:07:26 +00008372 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008373
Josef Bacikba1bf482009-09-11 16:11:19 -04008374 /*
8375 * check to make sure we can actually find a chunk with enough
8376 * space to fit our block group in.
8377 */
8378 if (device->total_bytes > device->bytes_used + min_free) {
8379 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008380 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008381 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05008382 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04008383 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008384 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008385 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008386 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05008387out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008388 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008389 return ret;
8390}
8391
Christoph Hellwigb2950862008-12-02 09:54:17 -05008392static int find_first_block_group(struct btrfs_root *root,
8393 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008394{
Chris Mason925baed2008-06-25 16:01:30 -04008395 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008396 struct btrfs_key found_key;
8397 struct extent_buffer *leaf;
8398 int slot;
8399
8400 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8401 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008402 goto out;
8403
Chris Masond3977122009-01-05 21:25:51 -05008404 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008405 slot = path->slots[0];
8406 leaf = path->nodes[0];
8407 if (slot >= btrfs_header_nritems(leaf)) {
8408 ret = btrfs_next_leaf(root, path);
8409 if (ret == 0)
8410 continue;
8411 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008412 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008413 break;
8414 }
8415 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8416
8417 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008418 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8419 ret = 0;
8420 goto out;
8421 }
Chris Mason0b86a832008-03-24 15:01:56 -04008422 path->slots[0]++;
8423 }
Chris Mason925baed2008-06-25 16:01:30 -04008424out:
Chris Mason0b86a832008-03-24 15:01:56 -04008425 return ret;
8426}
8427
Josef Bacik0af3d002010-06-21 14:48:16 -04008428void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8429{
8430 struct btrfs_block_group_cache *block_group;
8431 u64 last = 0;
8432
8433 while (1) {
8434 struct inode *inode;
8435
8436 block_group = btrfs_lookup_first_block_group(info, last);
8437 while (block_group) {
8438 spin_lock(&block_group->lock);
8439 if (block_group->iref)
8440 break;
8441 spin_unlock(&block_group->lock);
8442 block_group = next_block_group(info->tree_root,
8443 block_group);
8444 }
8445 if (!block_group) {
8446 if (last == 0)
8447 break;
8448 last = 0;
8449 continue;
8450 }
8451
8452 inode = block_group->inode;
8453 block_group->iref = 0;
8454 block_group->inode = NULL;
8455 spin_unlock(&block_group->lock);
8456 iput(inode);
8457 last = block_group->key.objectid + block_group->key.offset;
8458 btrfs_put_block_group(block_group);
8459 }
8460}
8461
Zheng Yan1a40e232008-09-26 10:09:34 -04008462int btrfs_free_block_groups(struct btrfs_fs_info *info)
8463{
8464 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008465 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008466 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008467 struct rb_node *n;
8468
Yan Zheng11833d62009-09-11 16:11:19 -04008469 down_write(&info->extent_commit_sem);
8470 while (!list_empty(&info->caching_block_groups)) {
8471 caching_ctl = list_entry(info->caching_block_groups.next,
8472 struct btrfs_caching_control, list);
8473 list_del(&caching_ctl->list);
8474 put_caching_control(caching_ctl);
8475 }
8476 up_write(&info->extent_commit_sem);
8477
Zheng Yan1a40e232008-09-26 10:09:34 -04008478 spin_lock(&info->block_group_cache_lock);
8479 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8480 block_group = rb_entry(n, struct btrfs_block_group_cache,
8481 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008482 rb_erase(&block_group->cache_node,
8483 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008484 spin_unlock(&info->block_group_cache_lock);
8485
Josef Bacik80eb2342008-10-29 14:49:05 -04008486 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008487 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008488 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008489
Josef Bacik817d52f2009-07-13 21:29:25 -04008490 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008491 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008492
Josef Bacik3c148742011-02-02 15:53:47 +00008493 /*
8494 * We haven't cached this block group, which means we could
8495 * possibly have excluded extents on this block group.
8496 */
8497 if (block_group->cached == BTRFS_CACHE_NO)
8498 free_excluded_extents(info->extent_root, block_group);
8499
Josef Bacik817d52f2009-07-13 21:29:25 -04008500 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008501 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008502
8503 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008504 }
8505 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008506
8507 /* now that all the block groups are freed, go through and
8508 * free all the space_info structs. This is only called during
8509 * the final stages of unmount, and so we know nobody is
8510 * using them. We call synchronize_rcu() once before we start,
8511 * just to be on the safe side.
8512 */
8513 synchronize_rcu();
8514
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008515 release_global_block_rsv(info);
8516
Chris Mason4184ea72009-03-10 12:39:20 -04008517 while(!list_empty(&info->space_info)) {
8518 space_info = list_entry(info->space_info.next,
8519 struct btrfs_space_info,
8520 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008521 if (space_info->bytes_pinned > 0 ||
8522 space_info->bytes_reserved > 0) {
8523 WARN_ON(1);
8524 dump_space_info(space_info, 0, 0);
8525 }
Chris Mason4184ea72009-03-10 12:39:20 -04008526 list_del(&space_info->list);
8527 kfree(space_info);
8528 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008529 return 0;
8530}
8531
Yan, Zhengb742bb82010-05-16 10:46:24 -04008532static void __link_block_group(struct btrfs_space_info *space_info,
8533 struct btrfs_block_group_cache *cache)
8534{
8535 int index = get_block_group_index(cache);
8536
8537 down_write(&space_info->groups_sem);
8538 list_add_tail(&cache->list, &space_info->block_groups[index]);
8539 up_write(&space_info->groups_sem);
8540}
8541
Chris Mason9078a3e2007-04-26 16:46:15 -04008542int btrfs_read_block_groups(struct btrfs_root *root)
8543{
8544 struct btrfs_path *path;
8545 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008546 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008547 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008548 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008549 struct btrfs_key key;
8550 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008551 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008552 int need_clear = 0;
8553 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008554
Chris Masonbe744172007-05-06 10:15:01 -04008555 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008556 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008557 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008558 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008559 path = btrfs_alloc_path();
8560 if (!path)
8561 return -ENOMEM;
8562
Josef Bacik0af3d002010-06-21 14:48:16 -04008563 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8564 if (cache_gen != 0 &&
8565 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8566 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008567 if (btrfs_test_opt(root, CLEAR_CACHE))
8568 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04008569 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
8570 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04008571
Chris Masond3977122009-01-05 21:25:51 -05008572 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008573 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008574 if (ret > 0)
8575 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008576 if (ret != 0)
8577 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008578 leaf = path->nodes[0];
8579 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008580 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008581 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008582 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008583 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008584 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008585
Yan Zhengd2fb3432008-12-11 16:30:39 -05008586 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008587 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008588 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008589 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008590 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008591 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008592
Josef Bacik0af3d002010-06-21 14:48:16 -04008593 if (need_clear)
8594 cache->disk_cache_state = BTRFS_DC_CLEAR;
8595
Josef Bacik96303082009-07-13 21:29:25 -04008596 /*
8597 * we only want to have 32k of ram per block group for keeping
8598 * track of free space, and if we pass 1/2 of that we want to
8599 * start converting things over to using bitmaps
8600 */
8601 cache->extents_thresh = ((1024 * 32) / 2) /
8602 sizeof(struct btrfs_free_space);
8603
Chris Mason5f39d392007-10-15 16:14:19 -04008604 read_extent_buffer(leaf, &cache->item,
8605 btrfs_item_ptr_offset(leaf, path->slots[0]),
8606 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008607 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008608
Chris Mason9078a3e2007-04-26 16:46:15 -04008609 key.objectid = found_key.objectid + found_key.offset;
8610 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04008611 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008612 cache->sectorsize = root->sectorsize;
8613
Josef Bacik817d52f2009-07-13 21:29:25 -04008614 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008615 * We need to exclude the super stripes now so that the space
8616 * info has super bytes accounted for, otherwise we'll think
8617 * we have more space than we actually do.
8618 */
8619 exclude_super_stripes(root, cache);
8620
8621 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008622 * check for two cases, either we are full, and therefore
8623 * don't need to bother with the caching work since we won't
8624 * find any space, or we are empty, and we can just add all
8625 * the space in and be done with it. This saves us _alot_ of
8626 * time, particularly in the full case.
8627 */
8628 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008629 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008630 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008631 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008632 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008633 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008634 cache->cached = BTRFS_CACHE_FINISHED;
8635 add_new_free_space(cache, root->fs_info,
8636 found_key.objectid,
8637 found_key.objectid +
8638 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008639 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008640 }
Chris Mason96b51792007-10-15 16:15:19 -04008641
Chris Mason6324fbf2008-03-24 15:01:59 -04008642 ret = update_space_info(info, cache->flags, found_key.offset,
8643 btrfs_block_group_used(&cache->item),
8644 &space_info);
8645 BUG_ON(ret);
8646 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008647 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008648 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008649 spin_unlock(&cache->space_info->lock);
8650
Yan, Zhengb742bb82010-05-16 10:46:24 -04008651 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008652
Josef Bacik0f9dd462008-09-23 13:14:11 -04008653 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8654 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04008655
8656 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008657 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04008658 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04008659 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008660
8661 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8662 if (!(get_alloc_profile(root, space_info->flags) &
8663 (BTRFS_BLOCK_GROUP_RAID10 |
8664 BTRFS_BLOCK_GROUP_RAID1 |
8665 BTRFS_BLOCK_GROUP_DUP)))
8666 continue;
8667 /*
8668 * avoid allocating from un-mirrored block group if there are
8669 * mirrored block groups.
8670 */
8671 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008672 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008673 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008674 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008675 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008676
8677 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008678 ret = 0;
8679error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008680 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008681 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008682}
Chris Mason6324fbf2008-03-24 15:01:59 -04008683
8684int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8685 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008686 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008687 u64 size)
8688{
8689 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008690 struct btrfs_root *extent_root;
8691 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008692
8693 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008694
Chris Mason12fcfd22009-03-24 10:24:20 -04008695 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008696
Chris Mason8f18cf12008-04-25 16:53:30 -04008697 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008698 if (!cache)
8699 return -ENOMEM;
8700
Chris Masone17cade2008-04-15 15:41:47 -04008701 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008702 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008703 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008704 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008705 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04008706
8707 /*
8708 * we only want to have 32k of ram per block group for keeping track
8709 * of free space, and if we pass 1/2 of that we want to start
8710 * converting things over to using bitmaps
8711 */
8712 cache->extents_thresh = ((1024 * 32) / 2) /
8713 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008714 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008715 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008716 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008717 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008718 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008719
Chris Mason6324fbf2008-03-24 15:01:59 -04008720 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008721 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8722 cache->flags = type;
8723 btrfs_set_block_group_flags(&cache->item, type);
8724
Yan Zheng11833d62009-09-11 16:11:19 -04008725 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008726 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04008727 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04008728
Josef Bacik817d52f2009-07-13 21:29:25 -04008729 add_new_free_space(cache, root->fs_info, chunk_offset,
8730 chunk_offset + size);
8731
Yan Zheng11833d62009-09-11 16:11:19 -04008732 free_excluded_extents(root, cache);
8733
Chris Mason6324fbf2008-03-24 15:01:59 -04008734 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8735 &cache->space_info);
8736 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04008737
8738 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008739 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008740 spin_unlock(&cache->space_info->lock);
8741
Yan, Zhengb742bb82010-05-16 10:46:24 -04008742 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008743
Josef Bacik0f9dd462008-09-23 13:14:11 -04008744 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8745 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04008746
Chris Mason6324fbf2008-03-24 15:01:59 -04008747 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8748 sizeof(cache->item));
8749 BUG_ON(ret);
8750
Chris Masond18a2c42008-04-04 15:40:00 -04008751 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008752
Chris Mason6324fbf2008-03-24 15:01:59 -04008753 return 0;
8754}
Zheng Yan1a40e232008-09-26 10:09:34 -04008755
8756int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8757 struct btrfs_root *root, u64 group_start)
8758{
8759 struct btrfs_path *path;
8760 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008761 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008762 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008763 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008764 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008765 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04008766 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008767
Zheng Yan1a40e232008-09-26 10:09:34 -04008768 root = root->fs_info->extent_root;
8769
8770 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8771 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008772 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008773
liubo9f7c43c2011-03-07 02:13:33 +00008774 /*
8775 * Free the reserved super bytes from this block group before
8776 * remove it.
8777 */
8778 free_excluded_extents(root, block_group);
8779
Zheng Yan1a40e232008-09-26 10:09:34 -04008780 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04008781 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8782 BTRFS_BLOCK_GROUP_RAID1 |
8783 BTRFS_BLOCK_GROUP_RAID10))
8784 factor = 2;
8785 else
8786 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008787
Chris Mason44fb5512009-06-04 15:34:51 -04008788 /* make sure this block group isn't part of an allocation cluster */
8789 cluster = &root->fs_info->data_alloc_cluster;
8790 spin_lock(&cluster->refill_lock);
8791 btrfs_return_cluster_to_free_space(block_group, cluster);
8792 spin_unlock(&cluster->refill_lock);
8793
8794 /*
8795 * make sure this block group isn't part of a metadata
8796 * allocation cluster
8797 */
8798 cluster = &root->fs_info->meta_alloc_cluster;
8799 spin_lock(&cluster->refill_lock);
8800 btrfs_return_cluster_to_free_space(block_group, cluster);
8801 spin_unlock(&cluster->refill_lock);
8802
Zheng Yan1a40e232008-09-26 10:09:34 -04008803 path = btrfs_alloc_path();
8804 BUG_ON(!path);
8805
Josef Bacik0af3d002010-06-21 14:48:16 -04008806 inode = lookup_free_space_inode(root, block_group, path);
8807 if (!IS_ERR(inode)) {
8808 btrfs_orphan_add(trans, inode);
8809 clear_nlink(inode);
8810 /* One for the block groups ref */
8811 spin_lock(&block_group->lock);
8812 if (block_group->iref) {
8813 block_group->iref = 0;
8814 block_group->inode = NULL;
8815 spin_unlock(&block_group->lock);
8816 iput(inode);
8817 } else {
8818 spin_unlock(&block_group->lock);
8819 }
8820 /* One for our lookup ref */
8821 iput(inode);
8822 }
8823
8824 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8825 key.offset = block_group->key.objectid;
8826 key.type = 0;
8827
8828 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8829 if (ret < 0)
8830 goto out;
8831 if (ret > 0)
8832 btrfs_release_path(tree_root, path);
8833 if (ret == 0) {
8834 ret = btrfs_del_item(trans, tree_root, path);
8835 if (ret)
8836 goto out;
8837 btrfs_release_path(tree_root, path);
8838 }
8839
Yan Zheng3dfdb932009-01-21 10:49:16 -05008840 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008841 rb_erase(&block_group->cache_node,
8842 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008843 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008844
Josef Bacik80eb2342008-10-29 14:49:05 -04008845 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008846 /*
8847 * we must use list_del_init so people can check to see if they
8848 * are still on the list after taking the semaphore
8849 */
8850 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008851 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008852
Josef Bacik817d52f2009-07-13 21:29:25 -04008853 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008854 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008855
8856 btrfs_remove_free_space_cache(block_group);
8857
Yan Zhengc146afa2008-11-12 14:34:12 -05008858 spin_lock(&block_group->space_info->lock);
8859 block_group->space_info->total_bytes -= block_group->key.offset;
8860 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008861 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008862 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008863
Josef Bacik0af3d002010-06-21 14:48:16 -04008864 memcpy(&key, &block_group->key, sizeof(key));
8865
Chris Mason283bb192009-07-24 16:30:55 -04008866 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008867
Chris Masonfa9c0d72009-04-03 09:47:43 -04008868 btrfs_put_block_group(block_group);
8869 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008870
8871 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8872 if (ret > 0)
8873 ret = -EIO;
8874 if (ret < 0)
8875 goto out;
8876
8877 ret = btrfs_del_item(trans, root, path);
8878out:
8879 btrfs_free_path(path);
8880 return ret;
8881}
liuboacce9522011-01-06 19:30:25 +08008882
liuboc59021f2011-03-07 02:13:14 +00008883int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8884{
8885 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008886 struct btrfs_super_block *disk_super;
8887 u64 features;
8888 u64 flags;
8889 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008890 int ret;
8891
liubo1aba86d2011-04-08 08:44:37 +00008892 disk_super = &fs_info->super_copy;
8893 if (!btrfs_super_root(disk_super))
8894 return 1;
liuboc59021f2011-03-07 02:13:14 +00008895
liubo1aba86d2011-04-08 08:44:37 +00008896 features = btrfs_super_incompat_flags(disk_super);
8897 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8898 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008899
liubo1aba86d2011-04-08 08:44:37 +00008900 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8901 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008902 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008903 goto out;
liuboc59021f2011-03-07 02:13:14 +00008904
liubo1aba86d2011-04-08 08:44:37 +00008905 if (mixed) {
8906 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8907 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8908 } else {
8909 flags = BTRFS_BLOCK_GROUP_METADATA;
8910 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8911 if (ret)
8912 goto out;
8913
8914 flags = BTRFS_BLOCK_GROUP_DATA;
8915 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8916 }
8917out:
liuboc59021f2011-03-07 02:13:14 +00008918 return ret;
8919}
8920
liuboacce9522011-01-06 19:30:25 +08008921int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8922{
8923 return unpin_extent_range(root, start, end);
8924}
8925
8926int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008927 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008928{
Li Dongyang5378e602011-03-24 10:24:27 +00008929 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008930}
Li Dongyangf7039b12011-03-24 10:24:28 +00008931
8932int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8933{
8934 struct btrfs_fs_info *fs_info = root->fs_info;
8935 struct btrfs_block_group_cache *cache = NULL;
8936 u64 group_trimmed;
8937 u64 start;
8938 u64 end;
8939 u64 trimmed = 0;
8940 int ret = 0;
8941
8942 cache = btrfs_lookup_block_group(fs_info, range->start);
8943
8944 while (cache) {
8945 if (cache->key.objectid >= (range->start + range->len)) {
8946 btrfs_put_block_group(cache);
8947 break;
8948 }
8949
8950 start = max(range->start, cache->key.objectid);
8951 end = min(range->start + range->len,
8952 cache->key.objectid + cache->key.offset);
8953
8954 if (end - start >= range->minlen) {
8955 if (!block_group_cache_done(cache)) {
8956 ret = cache_block_group(cache, NULL, root, 0);
8957 if (!ret)
8958 wait_block_group_cache_done(cache);
8959 }
8960 ret = btrfs_trim_block_group(cache,
8961 &group_trimmed,
8962 start,
8963 end,
8964 range->minlen);
8965
8966 trimmed += group_trimmed;
8967 if (ret) {
8968 btrfs_put_block_group(cache);
8969 break;
8970 }
8971 }
8972
8973 cache = next_block_group(fs_info->tree_root, cache);
8974 }
8975
8976 range->len = trimmed;
8977 return ret;
8978}