blob: fba1348cb2a05d445576689d68515922650251a0 [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
David Sterba62a45b62011-04-20 15:52:26 +020097static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +000098{
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;
David Sterbab3b4aa72011-04-21 01:20:15 +0200382 btrfs_release_path(path);
Yan Zheng11833d62009-09-11 16:11:19 -0400383 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
David Sterbab3b4aa72011-04-21 01:20:15 +0200757 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400758
David Sterba8cc33e52011-05-02 15:29:25 +0200759 /*
760 * Mutex was contended, block until it's released and try
761 * again
762 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400763 mutex_lock(&head->mutex);
764 mutex_unlock(&head->mutex);
765 btrfs_put_delayed_ref(&head->node);
766 goto again;
767 }
768 if (head->extent_op && head->extent_op->update_flags)
769 extent_flags |= head->extent_op->flags_to_set;
770 else
771 BUG_ON(num_refs == 0);
772
773 num_refs += head->node.ref_mod;
774 mutex_unlock(&head->mutex);
775 }
776 spin_unlock(&delayed_refs->lock);
777out:
778 WARN_ON(num_refs == 0);
779 if (refs)
780 *refs = num_refs;
781 if (flags)
782 *flags = extent_flags;
783out_free:
784 btrfs_free_path(path);
785 return ret;
786}
787
788/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500789 * Back reference rules. Back refs have three main goals:
790 *
791 * 1) differentiate between all holders of references to an extent so that
792 * when a reference is dropped we can make sure it was a valid reference
793 * before freeing the extent.
794 *
795 * 2) Provide enough information to quickly find the holders of an extent
796 * if we notice a given block is corrupted or bad.
797 *
798 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
799 * maintenance. This is actually the same as #2, but with a slightly
800 * different use case.
801 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400802 * There are two kinds of back refs. The implicit back refs is optimized
803 * for pointers in non-shared tree blocks. For a given pointer in a block,
804 * back refs of this kind provide information about the block's owner tree
805 * and the pointer's key. These information allow us to find the block by
806 * b-tree searching. The full back refs is for pointers in tree blocks not
807 * referenced by their owner trees. The location of tree block is recorded
808 * in the back refs. Actually the full back refs is generic, and can be
809 * used in all cases the implicit back refs is used. The major shortcoming
810 * of the full back refs is its overhead. Every time a tree block gets
811 * COWed, we have to update back refs entry for all pointers in it.
812 *
813 * For a newly allocated tree block, we use implicit back refs for
814 * pointers in it. This means most tree related operations only involve
815 * implicit back refs. For a tree block created in old transaction, the
816 * only way to drop a reference to it is COW it. So we can detect the
817 * event that tree block loses its owner tree's reference and do the
818 * back refs conversion.
819 *
820 * When a tree block is COW'd through a tree, there are four cases:
821 *
822 * The reference count of the block is one and the tree is the block's
823 * owner tree. Nothing to do in this case.
824 *
825 * The reference count of the block is one and the tree is not the
826 * block's owner tree. In this case, full back refs is used for pointers
827 * in the block. Remove these full back refs, add implicit back refs for
828 * every pointers in the new block.
829 *
830 * The reference count of the block is greater than one and the tree is
831 * the block's owner tree. In this case, implicit back refs is used for
832 * pointers in the block. Add full back refs for every pointers in the
833 * block, increase lower level extents' reference counts. The original
834 * implicit back refs are entailed to the new block.
835 *
836 * The reference count of the block is greater than one and the tree is
837 * not the block's owner tree. Add implicit back refs for every pointer in
838 * the new block, increase lower level extents' reference count.
839 *
840 * Back Reference Key composing:
841 *
842 * The key objectid corresponds to the first byte in the extent,
843 * The key type is used to differentiate between types of back refs.
844 * There are different meanings of the key offset for different types
845 * of back refs.
846 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500847 * File extents can be referenced by:
848 *
849 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400850 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500851 * - different offsets inside a file (bookend extents in file.c)
852 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400853 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500854 *
855 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500856 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857 * - original offset in the file
858 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400859 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400860 * The key offset for the implicit back refs is hash of the first
861 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500862 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400863 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500864 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400865 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500866 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400867 * The key offset for the implicit back refs is the first byte of
868 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500869 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400870 * When a file extent is allocated, The implicit back refs is used.
871 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500872 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400873 * (root_key.objectid, inode objectid, offset in file, 1)
874 *
875 * When a file extent is removed file truncation, we find the
876 * corresponding implicit back refs and check the following fields:
877 *
878 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500879 *
880 * Btree extents can be referenced by:
881 *
882 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500883 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400884 * Both the implicit back refs and the full back refs for tree blocks
885 * only consist of key. The key offset for the implicit back refs is
886 * objectid of block's owner tree. The key offset for the full back refs
887 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500888 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400889 * When implicit back refs is used, information about the lowest key and
890 * level of the tree block are required. These information are stored in
891 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500892 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400893
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400894#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
895static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
896 struct btrfs_root *root,
897 struct btrfs_path *path,
898 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500899{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400900 struct btrfs_extent_item *item;
901 struct btrfs_extent_item_v0 *ei0;
902 struct btrfs_extent_ref_v0 *ref0;
903 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400904 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400905 struct btrfs_key key;
906 struct btrfs_key found_key;
907 u32 new_size = sizeof(*item);
908 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500909 int ret;
910
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911 leaf = path->nodes[0];
912 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500913
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400914 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
915 ei0 = btrfs_item_ptr(leaf, path->slots[0],
916 struct btrfs_extent_item_v0);
917 refs = btrfs_extent_refs_v0(leaf, ei0);
918
919 if (owner == (u64)-1) {
920 while (1) {
921 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
922 ret = btrfs_next_leaf(root, path);
923 if (ret < 0)
924 return ret;
925 BUG_ON(ret > 0);
926 leaf = path->nodes[0];
927 }
928 btrfs_item_key_to_cpu(leaf, &found_key,
929 path->slots[0]);
930 BUG_ON(key.objectid != found_key.objectid);
931 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
932 path->slots[0]++;
933 continue;
934 }
935 ref0 = btrfs_item_ptr(leaf, path->slots[0],
936 struct btrfs_extent_ref_v0);
937 owner = btrfs_ref_objectid_v0(leaf, ref0);
938 break;
939 }
940 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200941 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400942
943 if (owner < BTRFS_FIRST_FREE_OBJECTID)
944 new_size += sizeof(*bi);
945
946 new_size -= sizeof(*ei0);
947 ret = btrfs_search_slot(trans, root, &key, path,
948 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400949 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 return ret;
951 BUG_ON(ret);
952
953 ret = btrfs_extend_item(trans, root, path, new_size);
954 BUG_ON(ret);
955
956 leaf = path->nodes[0];
957 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
958 btrfs_set_extent_refs(leaf, item, refs);
959 /* FIXME: get real generation */
960 btrfs_set_extent_generation(leaf, item, 0);
961 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
962 btrfs_set_extent_flags(leaf, item,
963 BTRFS_EXTENT_FLAG_TREE_BLOCK |
964 BTRFS_BLOCK_FLAG_FULL_BACKREF);
965 bi = (struct btrfs_tree_block_info *)(item + 1);
966 /* FIXME: get first key of the block */
967 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
968 btrfs_set_tree_block_level(leaf, bi, (int)owner);
969 } else {
970 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
971 }
972 btrfs_mark_buffer_dirty(leaf);
973 return 0;
974}
975#endif
976
977static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
978{
979 u32 high_crc = ~(u32)0;
980 u32 low_crc = ~(u32)0;
981 __le64 lenum;
982
983 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100984 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100986 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400987 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100988 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400989
990 return ((u64)high_crc << 31) ^ (u64)low_crc;
991}
992
993static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
994 struct btrfs_extent_data_ref *ref)
995{
996 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
997 btrfs_extent_data_ref_objectid(leaf, ref),
998 btrfs_extent_data_ref_offset(leaf, ref));
999}
1000
1001static int match_extent_data_ref(struct extent_buffer *leaf,
1002 struct btrfs_extent_data_ref *ref,
1003 u64 root_objectid, u64 owner, u64 offset)
1004{
1005 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1006 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1007 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1008 return 0;
1009 return 1;
1010}
1011
1012static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1013 struct btrfs_root *root,
1014 struct btrfs_path *path,
1015 u64 bytenr, u64 parent,
1016 u64 root_objectid,
1017 u64 owner, u64 offset)
1018{
1019 struct btrfs_key key;
1020 struct btrfs_extent_data_ref *ref;
1021 struct extent_buffer *leaf;
1022 u32 nritems;
1023 int ret;
1024 int recow;
1025 int err = -ENOENT;
1026
1027 key.objectid = bytenr;
1028 if (parent) {
1029 key.type = BTRFS_SHARED_DATA_REF_KEY;
1030 key.offset = parent;
1031 } else {
1032 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1033 key.offset = hash_extent_data_ref(root_objectid,
1034 owner, offset);
1035 }
1036again:
1037 recow = 0;
1038 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1039 if (ret < 0) {
1040 err = ret;
1041 goto fail;
1042 }
1043
1044 if (parent) {
1045 if (!ret)
1046 return 0;
1047#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1048 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001049 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001050 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1051 if (ret < 0) {
1052 err = ret;
1053 goto fail;
1054 }
1055 if (!ret)
1056 return 0;
1057#endif
1058 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001059 }
1060
1061 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001062 nritems = btrfs_header_nritems(leaf);
1063 while (1) {
1064 if (path->slots[0] >= nritems) {
1065 ret = btrfs_next_leaf(root, path);
1066 if (ret < 0)
1067 err = ret;
1068 if (ret)
1069 goto fail;
1070
1071 leaf = path->nodes[0];
1072 nritems = btrfs_header_nritems(leaf);
1073 recow = 1;
1074 }
1075
1076 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1077 if (key.objectid != bytenr ||
1078 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1079 goto fail;
1080
1081 ref = btrfs_item_ptr(leaf, path->slots[0],
1082 struct btrfs_extent_data_ref);
1083
1084 if (match_extent_data_ref(leaf, ref, root_objectid,
1085 owner, offset)) {
1086 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001087 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001088 goto again;
1089 }
1090 err = 0;
1091 break;
1092 }
1093 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001094 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095fail:
1096 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001097}
1098
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001099static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1100 struct btrfs_root *root,
1101 struct btrfs_path *path,
1102 u64 bytenr, u64 parent,
1103 u64 root_objectid, u64 owner,
1104 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001105{
1106 struct btrfs_key key;
1107 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001108 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001109 u32 num_refs;
1110 int ret;
1111
1112 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001113 if (parent) {
1114 key.type = BTRFS_SHARED_DATA_REF_KEY;
1115 key.offset = parent;
1116 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001117 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001118 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1119 key.offset = hash_extent_data_ref(root_objectid,
1120 owner, offset);
1121 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001122 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001123
1124 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1125 if (ret && ret != -EEXIST)
1126 goto fail;
1127
1128 leaf = path->nodes[0];
1129 if (parent) {
1130 struct btrfs_shared_data_ref *ref;
1131 ref = btrfs_item_ptr(leaf, path->slots[0],
1132 struct btrfs_shared_data_ref);
1133 if (ret == 0) {
1134 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1135 } else {
1136 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1137 num_refs += refs_to_add;
1138 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1139 }
1140 } else {
1141 struct btrfs_extent_data_ref *ref;
1142 while (ret == -EEXIST) {
1143 ref = btrfs_item_ptr(leaf, path->slots[0],
1144 struct btrfs_extent_data_ref);
1145 if (match_extent_data_ref(leaf, ref, root_objectid,
1146 owner, offset))
1147 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001148 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001149 key.offset++;
1150 ret = btrfs_insert_empty_item(trans, root, path, &key,
1151 size);
1152 if (ret && ret != -EEXIST)
1153 goto fail;
1154
1155 leaf = path->nodes[0];
1156 }
1157 ref = btrfs_item_ptr(leaf, path->slots[0],
1158 struct btrfs_extent_data_ref);
1159 if (ret == 0) {
1160 btrfs_set_extent_data_ref_root(leaf, ref,
1161 root_objectid);
1162 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1163 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1164 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1165 } else {
1166 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1167 num_refs += refs_to_add;
1168 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1169 }
1170 }
1171 btrfs_mark_buffer_dirty(leaf);
1172 ret = 0;
1173fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001174 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001175 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001176}
1177
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001178static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1179 struct btrfs_root *root,
1180 struct btrfs_path *path,
1181 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001182{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183 struct btrfs_key key;
1184 struct btrfs_extent_data_ref *ref1 = NULL;
1185 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001186 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001187 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001188 int ret = 0;
1189
1190 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001191 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1192
1193 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1194 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1195 struct btrfs_extent_data_ref);
1196 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1197 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1198 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1199 struct btrfs_shared_data_ref);
1200 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1201#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1202 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1203 struct btrfs_extent_ref_v0 *ref0;
1204 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1205 struct btrfs_extent_ref_v0);
1206 num_refs = btrfs_ref_count_v0(leaf, ref0);
1207#endif
1208 } else {
1209 BUG();
1210 }
1211
Chris Mason56bec292009-03-13 10:10:06 -04001212 BUG_ON(num_refs < refs_to_drop);
1213 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001214
Zheng Yan31840ae2008-09-23 13:14:14 -04001215 if (num_refs == 0) {
1216 ret = btrfs_del_item(trans, root, path);
1217 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001218 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1219 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1220 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1221 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1222#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1223 else {
1224 struct btrfs_extent_ref_v0 *ref0;
1225 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1226 struct btrfs_extent_ref_v0);
1227 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1228 }
1229#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001230 btrfs_mark_buffer_dirty(leaf);
1231 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001232 return ret;
1233}
1234
1235static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1236 struct btrfs_path *path,
1237 struct btrfs_extent_inline_ref *iref)
1238{
1239 struct btrfs_key key;
1240 struct extent_buffer *leaf;
1241 struct btrfs_extent_data_ref *ref1;
1242 struct btrfs_shared_data_ref *ref2;
1243 u32 num_refs = 0;
1244
1245 leaf = path->nodes[0];
1246 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1247 if (iref) {
1248 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1249 BTRFS_EXTENT_DATA_REF_KEY) {
1250 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1251 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1252 } else {
1253 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1254 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1255 }
1256 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1257 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1258 struct btrfs_extent_data_ref);
1259 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1260 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1261 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1262 struct btrfs_shared_data_ref);
1263 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1264#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1265 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1266 struct btrfs_extent_ref_v0 *ref0;
1267 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1268 struct btrfs_extent_ref_v0);
1269 num_refs = btrfs_ref_count_v0(leaf, ref0);
1270#endif
1271 } else {
1272 WARN_ON(1);
1273 }
1274 return num_refs;
1275}
1276
1277static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1278 struct btrfs_root *root,
1279 struct btrfs_path *path,
1280 u64 bytenr, u64 parent,
1281 u64 root_objectid)
1282{
1283 struct btrfs_key key;
1284 int ret;
1285
1286 key.objectid = bytenr;
1287 if (parent) {
1288 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1289 key.offset = parent;
1290 } else {
1291 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1292 key.offset = root_objectid;
1293 }
1294
1295 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1296 if (ret > 0)
1297 ret = -ENOENT;
1298#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1299 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001300 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001301 key.type = BTRFS_EXTENT_REF_V0_KEY;
1302 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1303 if (ret > 0)
1304 ret = -ENOENT;
1305 }
1306#endif
1307 return ret;
1308}
1309
1310static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1311 struct btrfs_root *root,
1312 struct btrfs_path *path,
1313 u64 bytenr, u64 parent,
1314 u64 root_objectid)
1315{
1316 struct btrfs_key key;
1317 int ret;
1318
1319 key.objectid = bytenr;
1320 if (parent) {
1321 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1322 key.offset = parent;
1323 } else {
1324 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1325 key.offset = root_objectid;
1326 }
1327
1328 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001329 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001330 return ret;
1331}
1332
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001333static inline int extent_ref_type(u64 parent, u64 owner)
1334{
1335 int type;
1336 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1337 if (parent > 0)
1338 type = BTRFS_SHARED_BLOCK_REF_KEY;
1339 else
1340 type = BTRFS_TREE_BLOCK_REF_KEY;
1341 } else {
1342 if (parent > 0)
1343 type = BTRFS_SHARED_DATA_REF_KEY;
1344 else
1345 type = BTRFS_EXTENT_DATA_REF_KEY;
1346 }
1347 return type;
1348}
1349
Yan Zheng2c47e6052009-06-27 21:07:35 -04001350static int find_next_key(struct btrfs_path *path, int level,
1351 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001352
1353{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001354 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001355 if (!path->nodes[level])
1356 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001357 if (path->slots[level] + 1 >=
1358 btrfs_header_nritems(path->nodes[level]))
1359 continue;
1360 if (level == 0)
1361 btrfs_item_key_to_cpu(path->nodes[level], key,
1362 path->slots[level] + 1);
1363 else
1364 btrfs_node_key_to_cpu(path->nodes[level], key,
1365 path->slots[level] + 1);
1366 return 0;
1367 }
1368 return 1;
1369}
1370
1371/*
1372 * look for inline back ref. if back ref is found, *ref_ret is set
1373 * to the address of inline back ref, and 0 is returned.
1374 *
1375 * if back ref isn't found, *ref_ret is set to the address where it
1376 * should be inserted, and -ENOENT is returned.
1377 *
1378 * if insert is true and there are too many inline back refs, the path
1379 * points to the extent item, and -EAGAIN is returned.
1380 *
1381 * NOTE: inline back refs are ordered in the same way that back ref
1382 * items in the tree are ordered.
1383 */
1384static noinline_for_stack
1385int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1386 struct btrfs_root *root,
1387 struct btrfs_path *path,
1388 struct btrfs_extent_inline_ref **ref_ret,
1389 u64 bytenr, u64 num_bytes,
1390 u64 parent, u64 root_objectid,
1391 u64 owner, u64 offset, int insert)
1392{
1393 struct btrfs_key key;
1394 struct extent_buffer *leaf;
1395 struct btrfs_extent_item *ei;
1396 struct btrfs_extent_inline_ref *iref;
1397 u64 flags;
1398 u64 item_size;
1399 unsigned long ptr;
1400 unsigned long end;
1401 int extra_size;
1402 int type;
1403 int want;
1404 int ret;
1405 int err = 0;
1406
1407 key.objectid = bytenr;
1408 key.type = BTRFS_EXTENT_ITEM_KEY;
1409 key.offset = num_bytes;
1410
1411 want = extent_ref_type(parent, owner);
1412 if (insert) {
1413 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001414 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001415 } else
1416 extra_size = -1;
1417 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1418 if (ret < 0) {
1419 err = ret;
1420 goto out;
1421 }
1422 BUG_ON(ret);
1423
1424 leaf = path->nodes[0];
1425 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1426#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1427 if (item_size < sizeof(*ei)) {
1428 if (!insert) {
1429 err = -ENOENT;
1430 goto out;
1431 }
1432 ret = convert_extent_item_v0(trans, root, path, owner,
1433 extra_size);
1434 if (ret < 0) {
1435 err = ret;
1436 goto out;
1437 }
1438 leaf = path->nodes[0];
1439 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1440 }
1441#endif
1442 BUG_ON(item_size < sizeof(*ei));
1443
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001444 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1445 flags = btrfs_extent_flags(leaf, ei);
1446
1447 ptr = (unsigned long)(ei + 1);
1448 end = (unsigned long)ei + item_size;
1449
1450 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1451 ptr += sizeof(struct btrfs_tree_block_info);
1452 BUG_ON(ptr > end);
1453 } else {
1454 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1455 }
1456
1457 err = -ENOENT;
1458 while (1) {
1459 if (ptr >= end) {
1460 WARN_ON(ptr > end);
1461 break;
1462 }
1463 iref = (struct btrfs_extent_inline_ref *)ptr;
1464 type = btrfs_extent_inline_ref_type(leaf, iref);
1465 if (want < type)
1466 break;
1467 if (want > type) {
1468 ptr += btrfs_extent_inline_ref_size(type);
1469 continue;
1470 }
1471
1472 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1473 struct btrfs_extent_data_ref *dref;
1474 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1475 if (match_extent_data_ref(leaf, dref, root_objectid,
1476 owner, offset)) {
1477 err = 0;
1478 break;
1479 }
1480 if (hash_extent_data_ref_item(leaf, dref) <
1481 hash_extent_data_ref(root_objectid, owner, offset))
1482 break;
1483 } else {
1484 u64 ref_offset;
1485 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1486 if (parent > 0) {
1487 if (parent == ref_offset) {
1488 err = 0;
1489 break;
1490 }
1491 if (ref_offset < parent)
1492 break;
1493 } else {
1494 if (root_objectid == ref_offset) {
1495 err = 0;
1496 break;
1497 }
1498 if (ref_offset < root_objectid)
1499 break;
1500 }
1501 }
1502 ptr += btrfs_extent_inline_ref_size(type);
1503 }
1504 if (err == -ENOENT && insert) {
1505 if (item_size + extra_size >=
1506 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1507 err = -EAGAIN;
1508 goto out;
1509 }
1510 /*
1511 * To add new inline back ref, we have to make sure
1512 * there is no corresponding back ref item.
1513 * For simplicity, we just do not add new inline back
1514 * ref if there is any kind of item for this block
1515 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001516 if (find_next_key(path, 0, &key) == 0 &&
1517 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001518 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001519 err = -EAGAIN;
1520 goto out;
1521 }
1522 }
1523 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1524out:
Yan Zheng85d41982009-06-11 08:51:10 -04001525 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001526 path->keep_locks = 0;
1527 btrfs_unlock_up_safe(path, 1);
1528 }
1529 return err;
1530}
1531
1532/*
1533 * helper to add new inline back ref
1534 */
1535static noinline_for_stack
1536int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1537 struct btrfs_root *root,
1538 struct btrfs_path *path,
1539 struct btrfs_extent_inline_ref *iref,
1540 u64 parent, u64 root_objectid,
1541 u64 owner, u64 offset, int refs_to_add,
1542 struct btrfs_delayed_extent_op *extent_op)
1543{
1544 struct extent_buffer *leaf;
1545 struct btrfs_extent_item *ei;
1546 unsigned long ptr;
1547 unsigned long end;
1548 unsigned long item_offset;
1549 u64 refs;
1550 int size;
1551 int type;
1552 int ret;
1553
1554 leaf = path->nodes[0];
1555 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1556 item_offset = (unsigned long)iref - (unsigned long)ei;
1557
1558 type = extent_ref_type(parent, owner);
1559 size = btrfs_extent_inline_ref_size(type);
1560
1561 ret = btrfs_extend_item(trans, root, path, size);
1562 BUG_ON(ret);
1563
1564 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1565 refs = btrfs_extent_refs(leaf, ei);
1566 refs += refs_to_add;
1567 btrfs_set_extent_refs(leaf, ei, refs);
1568 if (extent_op)
1569 __run_delayed_extent_op(extent_op, leaf, ei);
1570
1571 ptr = (unsigned long)ei + item_offset;
1572 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1573 if (ptr < end - size)
1574 memmove_extent_buffer(leaf, ptr + size, ptr,
1575 end - size - ptr);
1576
1577 iref = (struct btrfs_extent_inline_ref *)ptr;
1578 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1579 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1580 struct btrfs_extent_data_ref *dref;
1581 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1582 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1583 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1584 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1585 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1586 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1587 struct btrfs_shared_data_ref *sref;
1588 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1589 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1590 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1591 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1592 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1593 } else {
1594 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1595 }
1596 btrfs_mark_buffer_dirty(leaf);
1597 return 0;
1598}
1599
1600static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1601 struct btrfs_root *root,
1602 struct btrfs_path *path,
1603 struct btrfs_extent_inline_ref **ref_ret,
1604 u64 bytenr, u64 num_bytes, u64 parent,
1605 u64 root_objectid, u64 owner, u64 offset)
1606{
1607 int ret;
1608
1609 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1610 bytenr, num_bytes, parent,
1611 root_objectid, owner, offset, 0);
1612 if (ret != -ENOENT)
1613 return ret;
1614
David Sterbab3b4aa72011-04-21 01:20:15 +02001615 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001616 *ref_ret = NULL;
1617
1618 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1619 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1620 root_objectid);
1621 } else {
1622 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1623 root_objectid, owner, offset);
1624 }
1625 return ret;
1626}
1627
1628/*
1629 * helper to update/remove inline back ref
1630 */
1631static noinline_for_stack
1632int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1633 struct btrfs_root *root,
1634 struct btrfs_path *path,
1635 struct btrfs_extent_inline_ref *iref,
1636 int refs_to_mod,
1637 struct btrfs_delayed_extent_op *extent_op)
1638{
1639 struct extent_buffer *leaf;
1640 struct btrfs_extent_item *ei;
1641 struct btrfs_extent_data_ref *dref = NULL;
1642 struct btrfs_shared_data_ref *sref = NULL;
1643 unsigned long ptr;
1644 unsigned long end;
1645 u32 item_size;
1646 int size;
1647 int type;
1648 int ret;
1649 u64 refs;
1650
1651 leaf = path->nodes[0];
1652 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1653 refs = btrfs_extent_refs(leaf, ei);
1654 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1655 refs += refs_to_mod;
1656 btrfs_set_extent_refs(leaf, ei, refs);
1657 if (extent_op)
1658 __run_delayed_extent_op(extent_op, leaf, ei);
1659
1660 type = btrfs_extent_inline_ref_type(leaf, iref);
1661
1662 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1663 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1664 refs = btrfs_extent_data_ref_count(leaf, dref);
1665 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1666 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1667 refs = btrfs_shared_data_ref_count(leaf, sref);
1668 } else {
1669 refs = 1;
1670 BUG_ON(refs_to_mod != -1);
1671 }
1672
1673 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1674 refs += refs_to_mod;
1675
1676 if (refs > 0) {
1677 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1678 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1679 else
1680 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1681 } else {
1682 size = btrfs_extent_inline_ref_size(type);
1683 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1684 ptr = (unsigned long)iref;
1685 end = (unsigned long)ei + item_size;
1686 if (ptr + size < end)
1687 memmove_extent_buffer(leaf, ptr, ptr + size,
1688 end - ptr - size);
1689 item_size -= size;
1690 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1691 BUG_ON(ret);
1692 }
1693 btrfs_mark_buffer_dirty(leaf);
1694 return 0;
1695}
1696
1697static noinline_for_stack
1698int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1699 struct btrfs_root *root,
1700 struct btrfs_path *path,
1701 u64 bytenr, u64 num_bytes, u64 parent,
1702 u64 root_objectid, u64 owner,
1703 u64 offset, int refs_to_add,
1704 struct btrfs_delayed_extent_op *extent_op)
1705{
1706 struct btrfs_extent_inline_ref *iref;
1707 int ret;
1708
1709 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1710 bytenr, num_bytes, parent,
1711 root_objectid, owner, offset, 1);
1712 if (ret == 0) {
1713 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1714 ret = update_inline_extent_backref(trans, root, path, iref,
1715 refs_to_add, extent_op);
1716 } else if (ret == -ENOENT) {
1717 ret = setup_inline_extent_backref(trans, root, path, iref,
1718 parent, root_objectid,
1719 owner, offset, refs_to_add,
1720 extent_op);
1721 }
1722 return ret;
1723}
1724
1725static int insert_extent_backref(struct btrfs_trans_handle *trans,
1726 struct btrfs_root *root,
1727 struct btrfs_path *path,
1728 u64 bytenr, u64 parent, u64 root_objectid,
1729 u64 owner, u64 offset, int refs_to_add)
1730{
1731 int ret;
1732 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1733 BUG_ON(refs_to_add != 1);
1734 ret = insert_tree_block_ref(trans, root, path, bytenr,
1735 parent, root_objectid);
1736 } else {
1737 ret = insert_extent_data_ref(trans, root, path, bytenr,
1738 parent, root_objectid,
1739 owner, offset, refs_to_add);
1740 }
1741 return ret;
1742}
1743
1744static int remove_extent_backref(struct btrfs_trans_handle *trans,
1745 struct btrfs_root *root,
1746 struct btrfs_path *path,
1747 struct btrfs_extent_inline_ref *iref,
1748 int refs_to_drop, int is_data)
1749{
1750 int ret;
1751
1752 BUG_ON(!is_data && refs_to_drop != 1);
1753 if (iref) {
1754 ret = update_inline_extent_backref(trans, root, path, iref,
1755 -refs_to_drop, NULL);
1756 } else if (is_data) {
1757 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1758 } else {
1759 ret = btrfs_del_item(trans, root, path);
1760 }
1761 return ret;
1762}
1763
Li Dongyang5378e602011-03-24 10:24:27 +00001764static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001765 u64 start, u64 len)
1766{
Li Dongyang5378e602011-03-24 10:24:27 +00001767 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001768}
Chris Mason15916de2008-11-19 21:17:22 -05001769
Liu Hui1f3c79a2009-01-05 15:57:51 -05001770static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001771 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001772{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001773 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001774 u64 discarded_bytes = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001775 struct btrfs_multi_bio *multi = NULL;
1776
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001777
Liu Hui1f3c79a2009-01-05 15:57:51 -05001778 /* Tell the block device(s) that the sectors can be discarded */
Li Dongyang5378e602011-03-24 10:24:27 +00001779 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1780 bytenr, &num_bytes, &multi, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001781 if (!ret) {
1782 struct btrfs_bio_stripe *stripe = multi->stripes;
1783 int i;
1784
Liu Hui1f3c79a2009-01-05 15:57:51 -05001785
1786 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Li Dongyang5378e602011-03-24 10:24:27 +00001787 ret = btrfs_issue_discard(stripe->dev->bdev,
1788 stripe->physical,
1789 stripe->length);
1790 if (!ret)
1791 discarded_bytes += stripe->length;
1792 else if (ret != -EOPNOTSUPP)
1793 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001794 }
1795 kfree(multi);
1796 }
Li Dongyang5378e602011-03-24 10:24:27 +00001797 if (discarded_bytes && ret == -EOPNOTSUPP)
1798 ret = 0;
1799
1800 if (actual_bytes)
1801 *actual_bytes = discarded_bytes;
1802
Liu Hui1f3c79a2009-01-05 15:57:51 -05001803
1804 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001805}
1806
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001807int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1808 struct btrfs_root *root,
1809 u64 bytenr, u64 num_bytes, u64 parent,
1810 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001811{
1812 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001813 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1814 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001815
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001816 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1817 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1818 parent, root_objectid, (int)owner,
1819 BTRFS_ADD_DELAYED_REF, NULL);
1820 } else {
1821 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1822 parent, root_objectid, owner, offset,
1823 BTRFS_ADD_DELAYED_REF, NULL);
1824 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001825 return ret;
1826}
1827
Chris Mason925baed2008-06-25 16:01:30 -04001828static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001829 struct btrfs_root *root,
1830 u64 bytenr, u64 num_bytes,
1831 u64 parent, u64 root_objectid,
1832 u64 owner, u64 offset, int refs_to_add,
1833 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001834{
Chris Mason5caf2a02007-04-02 11:20:42 -04001835 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001836 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001837 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001838 u64 refs;
1839 int ret;
1840 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001841
Chris Mason5caf2a02007-04-02 11:20:42 -04001842 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001843 if (!path)
1844 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001845
Chris Mason3c12ac72008-04-21 12:01:38 -04001846 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001847 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001848 /* this will setup the path even if it fails to insert the back ref */
1849 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1850 path, bytenr, num_bytes, parent,
1851 root_objectid, owner, offset,
1852 refs_to_add, extent_op);
1853 if (ret == 0)
1854 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001855
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001856 if (ret != -EAGAIN) {
1857 err = ret;
1858 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001859 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001860
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001861 leaf = path->nodes[0];
1862 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1863 refs = btrfs_extent_refs(leaf, item);
1864 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1865 if (extent_op)
1866 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001868 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001869 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001870
Chris Mason3c12ac72008-04-21 12:01:38 -04001871 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001872 path->leave_spinning = 1;
1873
Chris Mason56bec292009-03-13 10:10:06 -04001874 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001875 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876 path, bytenr, parent, root_objectid,
1877 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001878 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001879out:
Chris Mason74493f72007-12-11 09:25:06 -05001880 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001881 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001882}
1883
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001884static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1885 struct btrfs_root *root,
1886 struct btrfs_delayed_ref_node *node,
1887 struct btrfs_delayed_extent_op *extent_op,
1888 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001889{
Chris Mason56bec292009-03-13 10:10:06 -04001890 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001891 struct btrfs_delayed_data_ref *ref;
1892 struct btrfs_key ins;
1893 u64 parent = 0;
1894 u64 ref_root = 0;
1895 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001896
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001897 ins.objectid = node->bytenr;
1898 ins.offset = node->num_bytes;
1899 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001900
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001901 ref = btrfs_delayed_node_to_data_ref(node);
1902 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1903 parent = ref->parent;
1904 else
1905 ref_root = ref->root;
1906
1907 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1908 if (extent_op) {
1909 BUG_ON(extent_op->update_key);
1910 flags |= extent_op->flags_to_set;
1911 }
1912 ret = alloc_reserved_file_extent(trans, root,
1913 parent, ref_root, flags,
1914 ref->objectid, ref->offset,
1915 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001916 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1917 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1918 node->num_bytes, parent,
1919 ref_root, ref->objectid,
1920 ref->offset, node->ref_mod,
1921 extent_op);
1922 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1923 ret = __btrfs_free_extent(trans, root, node->bytenr,
1924 node->num_bytes, parent,
1925 ref_root, ref->objectid,
1926 ref->offset, node->ref_mod,
1927 extent_op);
1928 } else {
1929 BUG();
1930 }
Chris Mason56bec292009-03-13 10:10:06 -04001931 return ret;
1932}
1933
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001934static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1935 struct extent_buffer *leaf,
1936 struct btrfs_extent_item *ei)
1937{
1938 u64 flags = btrfs_extent_flags(leaf, ei);
1939 if (extent_op->update_flags) {
1940 flags |= extent_op->flags_to_set;
1941 btrfs_set_extent_flags(leaf, ei, flags);
1942 }
1943
1944 if (extent_op->update_key) {
1945 struct btrfs_tree_block_info *bi;
1946 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1947 bi = (struct btrfs_tree_block_info *)(ei + 1);
1948 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1949 }
1950}
1951
1952static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1953 struct btrfs_root *root,
1954 struct btrfs_delayed_ref_node *node,
1955 struct btrfs_delayed_extent_op *extent_op)
1956{
1957 struct btrfs_key key;
1958 struct btrfs_path *path;
1959 struct btrfs_extent_item *ei;
1960 struct extent_buffer *leaf;
1961 u32 item_size;
1962 int ret;
1963 int err = 0;
1964
1965 path = btrfs_alloc_path();
1966 if (!path)
1967 return -ENOMEM;
1968
1969 key.objectid = node->bytenr;
1970 key.type = BTRFS_EXTENT_ITEM_KEY;
1971 key.offset = node->num_bytes;
1972
1973 path->reada = 1;
1974 path->leave_spinning = 1;
1975 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1976 path, 0, 1);
1977 if (ret < 0) {
1978 err = ret;
1979 goto out;
1980 }
1981 if (ret > 0) {
1982 err = -EIO;
1983 goto out;
1984 }
1985
1986 leaf = path->nodes[0];
1987 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1988#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1989 if (item_size < sizeof(*ei)) {
1990 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1991 path, (u64)-1, 0);
1992 if (ret < 0) {
1993 err = ret;
1994 goto out;
1995 }
1996 leaf = path->nodes[0];
1997 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1998 }
1999#endif
2000 BUG_ON(item_size < sizeof(*ei));
2001 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2002 __run_delayed_extent_op(extent_op, leaf, ei);
2003
2004 btrfs_mark_buffer_dirty(leaf);
2005out:
2006 btrfs_free_path(path);
2007 return err;
2008}
2009
2010static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2011 struct btrfs_root *root,
2012 struct btrfs_delayed_ref_node *node,
2013 struct btrfs_delayed_extent_op *extent_op,
2014 int insert_reserved)
2015{
2016 int ret = 0;
2017 struct btrfs_delayed_tree_ref *ref;
2018 struct btrfs_key ins;
2019 u64 parent = 0;
2020 u64 ref_root = 0;
2021
2022 ins.objectid = node->bytenr;
2023 ins.offset = node->num_bytes;
2024 ins.type = BTRFS_EXTENT_ITEM_KEY;
2025
2026 ref = btrfs_delayed_node_to_tree_ref(node);
2027 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2028 parent = ref->parent;
2029 else
2030 ref_root = ref->root;
2031
2032 BUG_ON(node->ref_mod != 1);
2033 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2034 BUG_ON(!extent_op || !extent_op->update_flags ||
2035 !extent_op->update_key);
2036 ret = alloc_reserved_tree_block(trans, root,
2037 parent, ref_root,
2038 extent_op->flags_to_set,
2039 &extent_op->key,
2040 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002041 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2042 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2043 node->num_bytes, parent, ref_root,
2044 ref->level, 0, 1, extent_op);
2045 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2046 ret = __btrfs_free_extent(trans, root, node->bytenr,
2047 node->num_bytes, parent, ref_root,
2048 ref->level, 0, 1, extent_op);
2049 } else {
2050 BUG();
2051 }
2052 return ret;
2053}
2054
Chris Mason56bec292009-03-13 10:10:06 -04002055/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002056static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2057 struct btrfs_root *root,
2058 struct btrfs_delayed_ref_node *node,
2059 struct btrfs_delayed_extent_op *extent_op,
2060 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002061{
Josef Bacikeb099672009-02-12 09:27:38 -05002062 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002063 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002064 struct btrfs_delayed_ref_head *head;
2065 /*
2066 * we've hit the end of the chain and we were supposed
2067 * to insert this extent into the tree. But, it got
2068 * deleted before we ever needed to insert it, so all
2069 * we have to do is clean up the accounting
2070 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002071 BUG_ON(extent_op);
2072 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002073 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002074 btrfs_pin_extent(root, node->bytenr,
2075 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002076 if (head->is_data) {
2077 ret = btrfs_del_csums(trans, root,
2078 node->bytenr,
2079 node->num_bytes);
2080 BUG_ON(ret);
2081 }
Chris Mason56bec292009-03-13 10:10:06 -04002082 }
Chris Mason56bec292009-03-13 10:10:06 -04002083 mutex_unlock(&head->mutex);
2084 return 0;
2085 }
Josef Bacikeb099672009-02-12 09:27:38 -05002086
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002087 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2088 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2089 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2090 insert_reserved);
2091 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2092 node->type == BTRFS_SHARED_DATA_REF_KEY)
2093 ret = run_delayed_data_ref(trans, root, node, extent_op,
2094 insert_reserved);
2095 else
2096 BUG();
2097 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002098}
2099
Chris Mason56bec292009-03-13 10:10:06 -04002100static noinline struct btrfs_delayed_ref_node *
2101select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002102{
Chris Mason56bec292009-03-13 10:10:06 -04002103 struct rb_node *node;
2104 struct btrfs_delayed_ref_node *ref;
2105 int action = BTRFS_ADD_DELAYED_REF;
2106again:
2107 /*
2108 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2109 * this prevents ref count from going down to zero when
2110 * there still are pending delayed ref.
2111 */
2112 node = rb_prev(&head->node.rb_node);
2113 while (1) {
2114 if (!node)
2115 break;
2116 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2117 rb_node);
2118 if (ref->bytenr != head->node.bytenr)
2119 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002120 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002121 return ref;
2122 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002123 }
Chris Mason56bec292009-03-13 10:10:06 -04002124 if (action == BTRFS_ADD_DELAYED_REF) {
2125 action = BTRFS_DROP_DELAYED_REF;
2126 goto again;
2127 }
2128 return NULL;
2129}
2130
Chris Masonc3e69d52009-03-13 10:17:05 -04002131static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2132 struct btrfs_root *root,
2133 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002134{
Chris Mason56bec292009-03-13 10:10:06 -04002135 struct btrfs_delayed_ref_root *delayed_refs;
2136 struct btrfs_delayed_ref_node *ref;
2137 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002138 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002139 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002140 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002141 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002142
2143 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002144 while (1) {
2145 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002146 /* pick a new head ref from the cluster list */
2147 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002148 break;
Chris Mason56bec292009-03-13 10:10:06 -04002149
Chris Masonc3e69d52009-03-13 10:17:05 -04002150 locked_ref = list_entry(cluster->next,
2151 struct btrfs_delayed_ref_head, cluster);
2152
2153 /* grab the lock that says we are going to process
2154 * all the refs for this head */
2155 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2156
2157 /*
2158 * we may have dropped the spin lock to get the head
2159 * mutex lock, and that might have given someone else
2160 * time to free the head. If that's true, it has been
2161 * removed from our list and we can move on.
2162 */
2163 if (ret == -EAGAIN) {
2164 locked_ref = NULL;
2165 count++;
2166 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002167 }
2168 }
2169
2170 /*
2171 * record the must insert reserved flag before we
2172 * drop the spin lock.
2173 */
2174 must_insert_reserved = locked_ref->must_insert_reserved;
2175 locked_ref->must_insert_reserved = 0;
2176
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002177 extent_op = locked_ref->extent_op;
2178 locked_ref->extent_op = NULL;
2179
Chris Mason56bec292009-03-13 10:10:06 -04002180 /*
2181 * locked_ref is the head node, so we have to go one
2182 * node back for any delayed ref updates
2183 */
Chris Mason56bec292009-03-13 10:10:06 -04002184 ref = select_delayed_ref(locked_ref);
2185 if (!ref) {
2186 /* All delayed refs have been processed, Go ahead
2187 * and send the head node to run_one_delayed_ref,
2188 * so that any accounting fixes can happen
2189 */
2190 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002191
2192 if (extent_op && must_insert_reserved) {
2193 kfree(extent_op);
2194 extent_op = NULL;
2195 }
2196
2197 if (extent_op) {
2198 spin_unlock(&delayed_refs->lock);
2199
2200 ret = run_delayed_extent_op(trans, root,
2201 ref, extent_op);
2202 BUG_ON(ret);
2203 kfree(extent_op);
2204
2205 cond_resched();
2206 spin_lock(&delayed_refs->lock);
2207 continue;
2208 }
2209
Chris Masonc3e69d52009-03-13 10:17:05 -04002210 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002211 locked_ref = NULL;
2212 }
2213
2214 ref->in_tree = 0;
2215 rb_erase(&ref->rb_node, &delayed_refs->root);
2216 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002217
Chris Mason56bec292009-03-13 10:10:06 -04002218 spin_unlock(&delayed_refs->lock);
2219
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002220 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002221 must_insert_reserved);
2222 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002223
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002224 btrfs_put_delayed_ref(ref);
2225 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002226 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002227
Chris Mason1887be62009-03-13 10:11:24 -04002228 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002229 spin_lock(&delayed_refs->lock);
2230 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002231 return count;
2232}
2233
2234/*
2235 * this starts processing the delayed reference count updates and
2236 * extent insertions we have queued up so far. count can be
2237 * 0, which means to process everything in the tree at the start
2238 * of the run (but not newly added entries), or it can be some target
2239 * number you'd like to process.
2240 */
2241int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2242 struct btrfs_root *root, unsigned long count)
2243{
2244 struct rb_node *node;
2245 struct btrfs_delayed_ref_root *delayed_refs;
2246 struct btrfs_delayed_ref_node *ref;
2247 struct list_head cluster;
2248 int ret;
2249 int run_all = count == (unsigned long)-1;
2250 int run_most = 0;
2251
2252 if (root == root->fs_info->extent_root)
2253 root = root->fs_info->tree_root;
2254
2255 delayed_refs = &trans->transaction->delayed_refs;
2256 INIT_LIST_HEAD(&cluster);
2257again:
2258 spin_lock(&delayed_refs->lock);
2259 if (count == 0) {
2260 count = delayed_refs->num_entries * 2;
2261 run_most = 1;
2262 }
2263 while (1) {
2264 if (!(run_all || run_most) &&
2265 delayed_refs->num_heads_ready < 64)
2266 break;
2267
2268 /*
2269 * go find something we can process in the rbtree. We start at
2270 * the beginning of the tree, and then build a cluster
2271 * of refs to process starting at the first one we are able to
2272 * lock
2273 */
2274 ret = btrfs_find_ref_cluster(trans, &cluster,
2275 delayed_refs->run_delayed_start);
2276 if (ret)
2277 break;
2278
2279 ret = run_clustered_refs(trans, root, &cluster);
2280 BUG_ON(ret < 0);
2281
2282 count -= min_t(unsigned long, ret, count);
2283
2284 if (count == 0)
2285 break;
2286 }
2287
Chris Mason56bec292009-03-13 10:10:06 -04002288 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002289 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002290 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002291 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002292 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002293
2294 while (node) {
2295 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2296 rb_node);
2297 if (btrfs_delayed_ref_is_head(ref)) {
2298 struct btrfs_delayed_ref_head *head;
2299
2300 head = btrfs_delayed_node_to_head(ref);
2301 atomic_inc(&ref->refs);
2302
2303 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002304 /*
2305 * Mutex was contended, block until it's
2306 * released and try again
2307 */
Chris Mason56bec292009-03-13 10:10:06 -04002308 mutex_lock(&head->mutex);
2309 mutex_unlock(&head->mutex);
2310
2311 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002312 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002313 goto again;
2314 }
2315 node = rb_next(node);
2316 }
2317 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002318 schedule_timeout(1);
2319 goto again;
2320 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002321out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002322 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002323 return 0;
2324}
2325
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002326int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2327 struct btrfs_root *root,
2328 u64 bytenr, u64 num_bytes, u64 flags,
2329 int is_data)
2330{
2331 struct btrfs_delayed_extent_op *extent_op;
2332 int ret;
2333
2334 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2335 if (!extent_op)
2336 return -ENOMEM;
2337
2338 extent_op->flags_to_set = flags;
2339 extent_op->update_flags = 1;
2340 extent_op->update_key = 0;
2341 extent_op->is_data = is_data ? 1 : 0;
2342
2343 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2344 if (ret)
2345 kfree(extent_op);
2346 return ret;
2347}
2348
2349static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2350 struct btrfs_root *root,
2351 struct btrfs_path *path,
2352 u64 objectid, u64 offset, u64 bytenr)
2353{
2354 struct btrfs_delayed_ref_head *head;
2355 struct btrfs_delayed_ref_node *ref;
2356 struct btrfs_delayed_data_ref *data_ref;
2357 struct btrfs_delayed_ref_root *delayed_refs;
2358 struct rb_node *node;
2359 int ret = 0;
2360
2361 ret = -ENOENT;
2362 delayed_refs = &trans->transaction->delayed_refs;
2363 spin_lock(&delayed_refs->lock);
2364 head = btrfs_find_delayed_ref_head(trans, bytenr);
2365 if (!head)
2366 goto out;
2367
2368 if (!mutex_trylock(&head->mutex)) {
2369 atomic_inc(&head->node.refs);
2370 spin_unlock(&delayed_refs->lock);
2371
David Sterbab3b4aa72011-04-21 01:20:15 +02002372 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002373
David Sterba8cc33e52011-05-02 15:29:25 +02002374 /*
2375 * Mutex was contended, block until it's released and let
2376 * caller try again
2377 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002378 mutex_lock(&head->mutex);
2379 mutex_unlock(&head->mutex);
2380 btrfs_put_delayed_ref(&head->node);
2381 return -EAGAIN;
2382 }
2383
2384 node = rb_prev(&head->node.rb_node);
2385 if (!node)
2386 goto out_unlock;
2387
2388 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2389
2390 if (ref->bytenr != bytenr)
2391 goto out_unlock;
2392
2393 ret = 1;
2394 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2395 goto out_unlock;
2396
2397 data_ref = btrfs_delayed_node_to_data_ref(ref);
2398
2399 node = rb_prev(node);
2400 if (node) {
2401 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2402 if (ref->bytenr == bytenr)
2403 goto out_unlock;
2404 }
2405
2406 if (data_ref->root != root->root_key.objectid ||
2407 data_ref->objectid != objectid || data_ref->offset != offset)
2408 goto out_unlock;
2409
2410 ret = 0;
2411out_unlock:
2412 mutex_unlock(&head->mutex);
2413out:
2414 spin_unlock(&delayed_refs->lock);
2415 return ret;
2416}
2417
2418static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2419 struct btrfs_root *root,
2420 struct btrfs_path *path,
2421 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002422{
2423 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002424 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002425 struct btrfs_extent_data_ref *ref;
2426 struct btrfs_extent_inline_ref *iref;
2427 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002428 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002429 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002430 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002431
Chris Masonbe20aa92007-12-17 20:14:01 -05002432 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002433 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002434 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002435
Chris Masonbe20aa92007-12-17 20:14:01 -05002436 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2437 if (ret < 0)
2438 goto out;
2439 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002440
2441 ret = -ENOENT;
2442 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002443 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002444
Zheng Yan31840ae2008-09-23 13:14:14 -04002445 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002446 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002447 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002448
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002449 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002450 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002451
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002452 ret = 1;
2453 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2454#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2455 if (item_size < sizeof(*ei)) {
2456 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2457 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002458 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002459#endif
2460 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2461
2462 if (item_size != sizeof(*ei) +
2463 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2464 goto out;
2465
2466 if (btrfs_extent_generation(leaf, ei) <=
2467 btrfs_root_last_snapshot(&root->root_item))
2468 goto out;
2469
2470 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2471 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2472 BTRFS_EXTENT_DATA_REF_KEY)
2473 goto out;
2474
2475 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2476 if (btrfs_extent_refs(leaf, ei) !=
2477 btrfs_extent_data_ref_count(leaf, ref) ||
2478 btrfs_extent_data_ref_root(leaf, ref) !=
2479 root->root_key.objectid ||
2480 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2481 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2482 goto out;
2483
Yan Zhengf321e492008-07-30 09:26:11 -04002484 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002485out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002486 return ret;
2487}
2488
2489int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2490 struct btrfs_root *root,
2491 u64 objectid, u64 offset, u64 bytenr)
2492{
2493 struct btrfs_path *path;
2494 int ret;
2495 int ret2;
2496
2497 path = btrfs_alloc_path();
2498 if (!path)
2499 return -ENOENT;
2500
2501 do {
2502 ret = check_committed_ref(trans, root, path, objectid,
2503 offset, bytenr);
2504 if (ret && ret != -ENOENT)
2505 goto out;
2506
2507 ret2 = check_delayed_ref(trans, root, path, objectid,
2508 offset, bytenr);
2509 } while (ret2 == -EAGAIN);
2510
2511 if (ret2 && ret2 != -ENOENT) {
2512 ret = ret2;
2513 goto out;
2514 }
2515
2516 if (ret != -ENOENT || ret2 != -ENOENT)
2517 ret = 0;
2518out:
Yan Zhengf321e492008-07-30 09:26:11 -04002519 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002520 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2521 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002522 return ret;
2523}
2524
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002525#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002526int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2527 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002528{
Chris Mason5f39d392007-10-15 16:14:19 -04002529 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002530 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002531 u64 root_gen;
2532 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002533 int i;
Chris Masondb945352007-10-15 16:15:53 -04002534 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002535 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002536 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002537
Chris Mason3768f362007-03-13 16:47:54 -04002538 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002539 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002540
Zheng Yane4657682008-09-26 10:04:53 -04002541 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2542 shared = 0;
2543 root_gen = root->root_key.offset;
2544 } else {
2545 shared = 1;
2546 root_gen = trans->transid - 1;
2547 }
2548
Chris Masondb945352007-10-15 16:15:53 -04002549 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002550 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002551
Zheng Yan31840ae2008-09-23 13:14:14 -04002552 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002553 struct btrfs_leaf_ref *ref;
2554 struct btrfs_extent_info *info;
2555
Zheng Yan31840ae2008-09-23 13:14:14 -04002556 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002557 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002558 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002559 goto out;
2560 }
2561
Zheng Yane4657682008-09-26 10:04:53 -04002562 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002563 ref->bytenr = buf->start;
2564 ref->owner = btrfs_header_owner(buf);
2565 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002566 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002567 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002568
Zheng Yan31840ae2008-09-23 13:14:14 -04002569 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002570 u64 disk_bytenr;
2571 btrfs_item_key_to_cpu(buf, &key, i);
2572 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2573 continue;
2574 fi = btrfs_item_ptr(buf, i,
2575 struct btrfs_file_extent_item);
2576 if (btrfs_file_extent_type(buf, fi) ==
2577 BTRFS_FILE_EXTENT_INLINE)
2578 continue;
2579 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2580 if (disk_bytenr == 0)
2581 continue;
2582
2583 info->bytenr = disk_bytenr;
2584 info->num_bytes =
2585 btrfs_file_extent_disk_num_bytes(buf, fi);
2586 info->objectid = key.objectid;
2587 info->offset = key.offset;
2588 info++;
2589 }
2590
Zheng Yane4657682008-09-26 10:04:53 -04002591 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002592 if (ret == -EEXIST && shared) {
2593 struct btrfs_leaf_ref *old;
2594 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2595 BUG_ON(!old);
2596 btrfs_remove_leaf_ref(root, old);
2597 btrfs_free_leaf_ref(root, old);
2598 ret = btrfs_add_leaf_ref(root, ref, shared);
2599 }
Yan Zheng31153d82008-07-28 15:32:19 -04002600 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002601 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002602 }
2603out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002604 return ret;
2605}
2606
Chris Masonb7a9f292009-02-04 09:23:45 -05002607/* when a block goes through cow, we update the reference counts of
2608 * everything that block points to. The internal pointers of the block
2609 * can be in just about any order, and it is likely to have clusters of
2610 * things that are close together and clusters of things that are not.
2611 *
2612 * To help reduce the seeks that come with updating all of these reference
2613 * counts, sort them by byte number before actual updates are done.
2614 *
2615 * struct refsort is used to match byte number to slot in the btree block.
2616 * we sort based on the byte number and then use the slot to actually
2617 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002618 *
2619 * struct refsort is smaller than strcut btrfs_item and smaller than
2620 * struct btrfs_key_ptr. Since we're currently limited to the page size
2621 * for a btree block, there's no way for a kmalloc of refsorts for a
2622 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002623 */
2624struct refsort {
2625 u64 bytenr;
2626 u32 slot;
2627};
2628
2629/*
2630 * for passing into sort()
2631 */
2632static int refsort_cmp(const void *a_void, const void *b_void)
2633{
2634 const struct refsort *a = a_void;
2635 const struct refsort *b = b_void;
2636
2637 if (a->bytenr < b->bytenr)
2638 return -1;
2639 if (a->bytenr > b->bytenr)
2640 return 1;
2641 return 0;
2642}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002643#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002644
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002645static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002646 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002647 struct extent_buffer *buf,
2648 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002649{
2650 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002651 u64 num_bytes;
2652 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002653 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002654 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002655 struct btrfs_key key;
2656 struct btrfs_file_extent_item *fi;
2657 int i;
2658 int level;
2659 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002660 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002661 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002662
2663 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002664 nritems = btrfs_header_nritems(buf);
2665 level = btrfs_header_level(buf);
2666
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002667 if (!root->ref_cows && level == 0)
2668 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002669
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002670 if (inc)
2671 process_func = btrfs_inc_extent_ref;
2672 else
2673 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002674
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002675 if (full_backref)
2676 parent = buf->start;
2677 else
2678 parent = 0;
2679
Zheng Yan31840ae2008-09-23 13:14:14 -04002680 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002681 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002682 btrfs_item_key_to_cpu(buf, &key, i);
2683 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002684 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002685 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002686 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002687 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002688 BTRFS_FILE_EXTENT_INLINE)
2689 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002690 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2691 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002692 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002693
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002694 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2695 key.offset -= btrfs_file_extent_offset(buf, fi);
2696 ret = process_func(trans, root, bytenr, num_bytes,
2697 parent, ref_root, key.objectid,
2698 key.offset);
2699 if (ret)
2700 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002701 } else {
2702 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002703 num_bytes = btrfs_level_size(root, level - 1);
2704 ret = process_func(trans, root, bytenr, num_bytes,
2705 parent, ref_root, level - 1, 0);
2706 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002707 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002708 }
2709 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002710 return 0;
2711fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002712 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002713 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002714}
2715
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002716int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2717 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002718{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002719 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2720}
Zheng Yan31840ae2008-09-23 13:14:14 -04002721
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002722int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2723 struct extent_buffer *buf, int full_backref)
2724{
2725 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002726}
2727
Chris Mason9078a3e2007-04-26 16:46:15 -04002728static int write_one_cache_group(struct btrfs_trans_handle *trans,
2729 struct btrfs_root *root,
2730 struct btrfs_path *path,
2731 struct btrfs_block_group_cache *cache)
2732{
2733 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002734 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002735 unsigned long bi;
2736 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002737
Chris Mason9078a3e2007-04-26 16:46:15 -04002738 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002739 if (ret < 0)
2740 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002741 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002742
2743 leaf = path->nodes[0];
2744 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2745 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2746 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002747 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002748fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002749 if (ret)
2750 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002751 return 0;
2752
2753}
2754
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002755static struct btrfs_block_group_cache *
2756next_block_group(struct btrfs_root *root,
2757 struct btrfs_block_group_cache *cache)
2758{
2759 struct rb_node *node;
2760 spin_lock(&root->fs_info->block_group_cache_lock);
2761 node = rb_next(&cache->cache_node);
2762 btrfs_put_block_group(cache);
2763 if (node) {
2764 cache = rb_entry(node, struct btrfs_block_group_cache,
2765 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002766 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002767 } else
2768 cache = NULL;
2769 spin_unlock(&root->fs_info->block_group_cache_lock);
2770 return cache;
2771}
2772
Josef Bacik0af3d002010-06-21 14:48:16 -04002773static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2774 struct btrfs_trans_handle *trans,
2775 struct btrfs_path *path)
2776{
2777 struct btrfs_root *root = block_group->fs_info->tree_root;
2778 struct inode *inode = NULL;
2779 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002780 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002781 int num_pages = 0;
2782 int retries = 0;
2783 int ret = 0;
2784
2785 /*
2786 * If this block group is smaller than 100 megs don't bother caching the
2787 * block group.
2788 */
2789 if (block_group->key.offset < (100 * 1024 * 1024)) {
2790 spin_lock(&block_group->lock);
2791 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2792 spin_unlock(&block_group->lock);
2793 return 0;
2794 }
2795
2796again:
2797 inode = lookup_free_space_inode(root, block_group, path);
2798 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2799 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002800 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002801 goto out;
2802 }
2803
2804 if (IS_ERR(inode)) {
2805 BUG_ON(retries);
2806 retries++;
2807
2808 if (block_group->ro)
2809 goto out_free;
2810
2811 ret = create_free_space_inode(root, trans, block_group, path);
2812 if (ret)
2813 goto out_free;
2814 goto again;
2815 }
2816
2817 /*
2818 * We want to set the generation to 0, that way if anything goes wrong
2819 * from here on out we know not to trust this cache when we load up next
2820 * time.
2821 */
2822 BTRFS_I(inode)->generation = 0;
2823 ret = btrfs_update_inode(trans, root, inode);
2824 WARN_ON(ret);
2825
2826 if (i_size_read(inode) > 0) {
2827 ret = btrfs_truncate_free_space_cache(root, trans, path,
2828 inode);
2829 if (ret)
2830 goto out_put;
2831 }
2832
2833 spin_lock(&block_group->lock);
2834 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002835 /* We're not cached, don't bother trying to write stuff out */
2836 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002837 spin_unlock(&block_group->lock);
2838 goto out_put;
2839 }
2840 spin_unlock(&block_group->lock);
2841
2842 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2843 if (!num_pages)
2844 num_pages = 1;
2845
2846 /*
2847 * Just to make absolutely sure we have enough space, we're going to
2848 * preallocate 12 pages worth of space for each block group. In
2849 * practice we ought to use at most 8, but we need extra space so we can
2850 * add our header and have a terminator between the extents and the
2851 * bitmaps.
2852 */
2853 num_pages *= 16;
2854 num_pages *= PAGE_CACHE_SIZE;
2855
2856 ret = btrfs_check_data_free_space(inode, num_pages);
2857 if (ret)
2858 goto out_put;
2859
2860 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2861 num_pages, num_pages,
2862 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002863 if (!ret)
2864 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002865 btrfs_free_reserved_data_space(inode, num_pages);
2866out_put:
2867 iput(inode);
2868out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02002869 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002870out:
2871 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002872 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002873 spin_unlock(&block_group->lock);
2874
2875 return ret;
2876}
2877
Chris Mason96b51792007-10-15 16:15:19 -04002878int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2879 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002880{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002881 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002882 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002883 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002884 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002885
2886 path = btrfs_alloc_path();
2887 if (!path)
2888 return -ENOMEM;
2889
Josef Bacik0af3d002010-06-21 14:48:16 -04002890again:
2891 while (1) {
2892 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2893 while (cache) {
2894 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2895 break;
2896 cache = next_block_group(root, cache);
2897 }
2898 if (!cache) {
2899 if (last == 0)
2900 break;
2901 last = 0;
2902 continue;
2903 }
2904 err = cache_save_setup(cache, trans, path);
2905 last = cache->key.objectid + cache->key.offset;
2906 btrfs_put_block_group(cache);
2907 }
2908
Chris Masond3977122009-01-05 21:25:51 -05002909 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002910 if (last == 0) {
2911 err = btrfs_run_delayed_refs(trans, root,
2912 (unsigned long)-1);
2913 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002914 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002915
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002916 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2917 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002918 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2919 btrfs_put_block_group(cache);
2920 goto again;
2921 }
2922
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002923 if (cache->dirty)
2924 break;
2925 cache = next_block_group(root, cache);
2926 }
2927 if (!cache) {
2928 if (last == 0)
2929 break;
2930 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002931 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002932 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002933
Josef Bacik0cb59c92010-07-02 12:14:14 -04002934 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2935 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002936 cache->dirty = 0;
2937 last = cache->key.objectid + cache->key.offset;
2938
2939 err = write_one_cache_group(trans, root, path, cache);
2940 BUG_ON(err);
2941 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002942 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002943
Josef Bacik0cb59c92010-07-02 12:14:14 -04002944 while (1) {
2945 /*
2946 * I don't think this is needed since we're just marking our
2947 * preallocated extent as written, but just in case it can't
2948 * hurt.
2949 */
2950 if (last == 0) {
2951 err = btrfs_run_delayed_refs(trans, root,
2952 (unsigned long)-1);
2953 BUG_ON(err);
2954 }
2955
2956 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2957 while (cache) {
2958 /*
2959 * Really this shouldn't happen, but it could if we
2960 * couldn't write the entire preallocated extent and
2961 * splitting the extent resulted in a new block.
2962 */
2963 if (cache->dirty) {
2964 btrfs_put_block_group(cache);
2965 goto again;
2966 }
2967 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2968 break;
2969 cache = next_block_group(root, cache);
2970 }
2971 if (!cache) {
2972 if (last == 0)
2973 break;
2974 last = 0;
2975 continue;
2976 }
2977
2978 btrfs_write_out_cache(root, trans, cache, path);
2979
2980 /*
2981 * If we didn't have an error then the cache state is still
2982 * NEED_WRITE, so we can set it to WRITTEN.
2983 */
2984 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2985 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2986 last = cache->key.objectid + cache->key.offset;
2987 btrfs_put_block_group(cache);
2988 }
2989
Chris Mason9078a3e2007-04-26 16:46:15 -04002990 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002991 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002992}
2993
Yan Zhengd2fb3432008-12-11 16:30:39 -05002994int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2995{
2996 struct btrfs_block_group_cache *block_group;
2997 int readonly = 0;
2998
2999 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3000 if (!block_group || block_group->ro)
3001 readonly = 1;
3002 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003003 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003004 return readonly;
3005}
3006
Chris Mason593060d2008-03-25 16:50:33 -04003007static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3008 u64 total_bytes, u64 bytes_used,
3009 struct btrfs_space_info **space_info)
3010{
3011 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003012 int i;
3013 int factor;
3014
3015 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3016 BTRFS_BLOCK_GROUP_RAID10))
3017 factor = 2;
3018 else
3019 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003020
3021 found = __find_space_info(info, flags);
3022 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003023 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003024 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003025 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003026 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003027 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003028 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003029 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003030 *space_info = found;
3031 return 0;
3032 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003033 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003034 if (!found)
3035 return -ENOMEM;
3036
Yan, Zhengb742bb82010-05-16 10:46:24 -04003037 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3038 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003039 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003040 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04003041 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
3042 BTRFS_BLOCK_GROUP_SYSTEM |
3043 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04003044 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003045 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003046 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003047 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003048 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003049 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003050 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003051 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003052 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003053 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003054 found->chunk_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003055 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003056 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04003057 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04003058 return 0;
3059}
3060
Chris Mason8790d502008-04-03 16:29:03 -04003061static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3062{
3063 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04003064 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04003065 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04003066 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04003067 if (extra_flags) {
3068 if (flags & BTRFS_BLOCK_GROUP_DATA)
3069 fs_info->avail_data_alloc_bits |= extra_flags;
3070 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3071 fs_info->avail_metadata_alloc_bits |= extra_flags;
3072 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3073 fs_info->avail_system_alloc_bits |= extra_flags;
3074 }
3075}
Chris Mason593060d2008-03-25 16:50:33 -04003076
Yan Zheng2b820322008-11-17 21:11:30 -05003077u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003078{
Chris Masoncd02dca2010-12-13 14:56:23 -05003079 /*
3080 * we add in the count of missing devices because we want
3081 * to make sure that any RAID levels on a degraded FS
3082 * continue to be honored.
3083 */
3084 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3085 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04003086
3087 if (num_devices == 1)
3088 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3089 if (num_devices < 4)
3090 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3091
Chris Masonec44a352008-04-28 15:29:52 -04003092 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3093 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003094 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003095 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003096 }
Chris Masonec44a352008-04-28 15:29:52 -04003097
3098 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003099 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003100 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003101 }
Chris Masonec44a352008-04-28 15:29:52 -04003102
3103 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3104 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3105 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3106 (flags & BTRFS_BLOCK_GROUP_DUP)))
3107 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3108 return flags;
3109}
3110
Yan, Zhengb742bb82010-05-16 10:46:24 -04003111static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003112{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003113 if (flags & BTRFS_BLOCK_GROUP_DATA)
3114 flags |= root->fs_info->avail_data_alloc_bits &
3115 root->fs_info->data_alloc_profile;
3116 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3117 flags |= root->fs_info->avail_system_alloc_bits &
3118 root->fs_info->system_alloc_profile;
3119 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3120 flags |= root->fs_info->avail_metadata_alloc_bits &
3121 root->fs_info->metadata_alloc_profile;
3122 return btrfs_reduce_alloc_profile(root, flags);
3123}
Josef Bacik6a632092009-02-20 11:00:09 -05003124
Miao Xie6d07bce2011-01-05 10:07:31 +00003125u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003126{
3127 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003128
Yan, Zhengb742bb82010-05-16 10:46:24 -04003129 if (data)
3130 flags = BTRFS_BLOCK_GROUP_DATA;
3131 else if (root == root->fs_info->chunk_root)
3132 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3133 else
3134 flags = BTRFS_BLOCK_GROUP_METADATA;
3135
3136 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003137}
3138
3139void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3140{
Josef Bacik6a632092009-02-20 11:00:09 -05003141 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003142 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003143}
3144
3145/*
3146 * This will check the space that the inode allocates from to make sure we have
3147 * enough space for bytes.
3148 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003149int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003150{
3151 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003152 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003153 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003154 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003155
3156 /* make sure bytes are sectorsize aligned */
3157 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3158
Josef Bacik0af3d002010-06-21 14:48:16 -04003159 if (root == root->fs_info->tree_root) {
3160 alloc_chunk = 0;
3161 committed = 1;
3162 }
3163
Josef Bacik6a632092009-02-20 11:00:09 -05003164 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003165 if (!data_sinfo)
3166 goto alloc;
3167
Josef Bacik6a632092009-02-20 11:00:09 -05003168again:
3169 /* make sure we have enough space to handle the data first */
3170 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003171 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3172 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3173 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003174
3175 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003176 struct btrfs_trans_handle *trans;
3177
Josef Bacik6a632092009-02-20 11:00:09 -05003178 /*
3179 * if we don't have enough free bytes in this space then we need
3180 * to alloc a new chunk.
3181 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003182 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003183 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003184
Chris Mason0e4f8f82011-04-15 16:05:44 -04003185 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003186 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003187alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003188 alloc_target = btrfs_get_alloc_profile(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003189 trans = btrfs_join_transaction(root, 1);
3190 if (IS_ERR(trans))
3191 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003192
3193 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3194 bytes + 2 * 1024 * 1024,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003195 alloc_target,
3196 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003197 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003198 if (ret < 0) {
3199 if (ret != -ENOSPC)
3200 return ret;
3201 else
3202 goto commit_trans;
3203 }
Chris Mason33b4d472009-09-22 14:45:50 -04003204
3205 if (!data_sinfo) {
3206 btrfs_set_inode_space_info(root, inode);
3207 data_sinfo = BTRFS_I(inode)->space_info;
3208 }
Josef Bacik6a632092009-02-20 11:00:09 -05003209 goto again;
3210 }
3211 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003212
3213 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003214commit_trans:
Sage Weildd7e0b72009-09-29 18:38:44 -04003215 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003216 committed = 1;
3217 trans = btrfs_join_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003218 if (IS_ERR(trans))
3219 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003220 ret = btrfs_commit_transaction(trans, root);
3221 if (ret)
3222 return ret;
3223 goto again;
3224 }
3225
Chris Mason933b5852010-05-26 11:31:00 -04003226#if 0 /* I hope we never need this code again, just in case */
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003227 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3228 "%llu bytes_reserved, " "%llu bytes_pinned, "
3229 "%llu bytes_readonly, %llu may use %llu total\n",
3230 (unsigned long long)bytes,
Joel Becker21380932009-04-21 12:38:29 -07003231 (unsigned long long)data_sinfo->bytes_used,
3232 (unsigned long long)data_sinfo->bytes_reserved,
3233 (unsigned long long)data_sinfo->bytes_pinned,
3234 (unsigned long long)data_sinfo->bytes_readonly,
3235 (unsigned long long)data_sinfo->bytes_may_use,
3236 (unsigned long long)data_sinfo->total_bytes);
Chris Mason933b5852010-05-26 11:31:00 -04003237#endif
Josef Bacik6a632092009-02-20 11:00:09 -05003238 return -ENOSPC;
3239 }
3240 data_sinfo->bytes_may_use += bytes;
3241 BTRFS_I(inode)->reserved_bytes += bytes;
3242 spin_unlock(&data_sinfo->lock);
3243
Josef Bacik9ed74f22009-09-11 16:12:44 -04003244 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003245}
3246
3247/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003248 * called when we are clearing an delalloc extent from the
3249 * inode's io_tree or there was an error for whatever reason
3250 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003251 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003252void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003253{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003254 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003255 struct btrfs_space_info *data_sinfo;
3256
3257 /* make sure bytes are sectorsize aligned */
3258 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3259
3260 data_sinfo = BTRFS_I(inode)->space_info;
3261 spin_lock(&data_sinfo->lock);
3262 data_sinfo->bytes_may_use -= bytes;
3263 BTRFS_I(inode)->reserved_bytes -= bytes;
3264 spin_unlock(&data_sinfo->lock);
3265}
3266
Josef Bacik97e728d2009-04-21 17:40:57 -04003267static void force_metadata_allocation(struct btrfs_fs_info *info)
3268{
3269 struct list_head *head = &info->space_info;
3270 struct btrfs_space_info *found;
3271
3272 rcu_read_lock();
3273 list_for_each_entry_rcu(found, head, list) {
3274 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003275 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003276 }
3277 rcu_read_unlock();
3278}
3279
Chris Masone5bc2452010-10-26 13:37:56 -04003280static int should_alloc_chunk(struct btrfs_root *root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003281 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3282 int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003283{
3284 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003285 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003286 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003287
Chris Mason0e4f8f82011-04-15 16:05:44 -04003288 if (force == CHUNK_ALLOC_FORCE)
3289 return 1;
3290
3291 /*
3292 * in limited mode, we want to have some free space up to
3293 * about 1% of the FS size.
3294 */
3295 if (force == CHUNK_ALLOC_LIMITED) {
3296 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3297 thresh = max_t(u64, 64 * 1024 * 1024,
3298 div_factor_fine(thresh, 1));
3299
3300 if (num_bytes - num_allocated < thresh)
3301 return 1;
3302 }
3303
3304 /*
3305 * we have two similar checks here, one based on percentage
3306 * and once based on a hard number of 256MB. The idea
3307 * is that if we have a good amount of free
3308 * room, don't allocate a chunk. A good mount is
3309 * less than 80% utilized of the chunks we have allocated,
3310 * or more than 256MB free
3311 */
3312 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003313 return 0;
3314
Chris Mason0e4f8f82011-04-15 16:05:44 -04003315 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
Yan, Zheng424499d2010-05-16 10:46:25 -04003316 return 0;
3317
Chris Masone5bc2452010-10-26 13:37:56 -04003318 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003319
3320 /* 256MB or 5% of the FS */
Chris Masone5bc2452010-10-26 13:37:56 -04003321 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3322
3323 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003324 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003325 return 1;
3326}
3327
Chris Mason6324fbf2008-03-24 15:01:59 -04003328static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3329 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003330 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003331{
3332 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003333 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003334 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003335 int ret = 0;
3336
Yan Zheng2b820322008-11-17 21:11:30 -05003337 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003338
Chris Mason6324fbf2008-03-24 15:01:59 -04003339 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003340 if (!space_info) {
3341 ret = update_space_info(extent_root->fs_info, flags,
3342 0, 0, &space_info);
3343 BUG_ON(ret);
3344 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003345 BUG_ON(!space_info);
3346
Josef Bacik6d741192011-04-11 20:20:11 -04003347again:
Josef Bacik25179202008-10-29 14:49:05 -04003348 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003349 if (space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003350 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003351 if (space_info->full) {
3352 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003353 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003354 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003355
Chris Mason0e4f8f82011-04-15 16:05:44 -04003356 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003357 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003358 return 0;
3359 } else if (space_info->chunk_alloc) {
3360 wait_for_alloc = 1;
3361 } else {
3362 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003363 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003364
Josef Bacik25179202008-10-29 14:49:05 -04003365 spin_unlock(&space_info->lock);
3366
Josef Bacik6d741192011-04-11 20:20:11 -04003367 mutex_lock(&fs_info->chunk_mutex);
3368
3369 /*
3370 * The chunk_mutex is held throughout the entirety of a chunk
3371 * allocation, so once we've acquired the chunk_mutex we know that the
3372 * other guy is done and we need to recheck and see if we should
3373 * allocate.
3374 */
3375 if (wait_for_alloc) {
3376 mutex_unlock(&fs_info->chunk_mutex);
3377 wait_for_alloc = 0;
3378 goto again;
3379 }
3380
Josef Bacik97e728d2009-04-21 17:40:57 -04003381 /*
Josef Bacik67377732010-09-16 16:19:09 -04003382 * If we have mixed data/metadata chunks we want to make sure we keep
3383 * allocating mixed chunks instead of individual chunks.
3384 */
3385 if (btrfs_mixed_space_info(space_info))
3386 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3387
3388 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003389 * if we're doing a data chunk, go ahead and make sure that
3390 * we keep a reasonable number of metadata chunks allocated in the
3391 * FS as well.
3392 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003393 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003394 fs_info->data_chunk_allocations++;
3395 if (!(fs_info->data_chunk_allocations %
3396 fs_info->metadata_ratio))
3397 force_metadata_allocation(fs_info);
3398 }
3399
Yan Zheng2b820322008-11-17 21:11:30 -05003400 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003401 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003402 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003403 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003404 else
3405 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003406
Chris Mason0e4f8f82011-04-15 16:05:44 -04003407 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003408 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003409 spin_unlock(&space_info->lock);
Yan Zhengc146afa2008-11-12 14:34:12 -05003410 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003411 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003412}
3413
Yan, Zheng5da9d012010-05-16 10:46:25 -04003414/*
3415 * shrink metadata reservation for delalloc
3416 */
3417static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003418 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003419{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003420 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003421 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003422 u64 reserved;
3423 u64 max_reclaim;
3424 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003425 long time_left;
Chris Masonbf9022e2010-10-26 13:40:45 -04003426 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003427 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003428 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003429
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003430 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003431 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003432
3433 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003434 reserved = space_info->bytes_reserved;
Chris Mason36e39c42011-03-12 07:08:42 -05003435 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003436
3437 if (reserved == 0)
3438 return 0;
3439
3440 max_reclaim = min(reserved, to_reclaim);
3441
Josef Bacikb1953bc2011-01-21 21:10:01 +00003442 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003443 /* have the flusher threads jump in and do some IO */
3444 smp_mb();
3445 nr_pages = min_t(unsigned long, nr_pages,
3446 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3447 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003448
Josef Bacik0019f102010-10-15 15:18:40 -04003449 spin_lock(&space_info->lock);
Chris Mason36e39c42011-03-12 07:08:42 -05003450 if (reserved > space_info->bytes_reserved)
Josef Bacik0019f102010-10-15 15:18:40 -04003451 reclaimed += reserved - space_info->bytes_reserved;
3452 reserved = space_info->bytes_reserved;
3453 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003454
Chris Mason36e39c42011-03-12 07:08:42 -05003455 loops++;
3456
Yan, Zheng5da9d012010-05-16 10:46:25 -04003457 if (reserved == 0 || reclaimed >= max_reclaim)
3458 break;
3459
3460 if (trans && trans->transaction->blocked)
3461 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003462
Chris Mason36e39c42011-03-12 07:08:42 -05003463 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003464
3465 /* We were interrupted, exit */
3466 if (time_left)
3467 break;
3468
Chris Mason36e39c42011-03-12 07:08:42 -05003469 /* we've kicked the IO a few times, if anything has been freed,
3470 * exit. There is no sense in looping here for a long time
3471 * when we really need to commit the transaction, or there are
3472 * just too many writers without enough free space
3473 */
3474
3475 if (loops > 3) {
3476 smp_mb();
3477 if (progress != space_info->reservation_progress)
3478 break;
3479 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003480
Yan, Zheng5da9d012010-05-16 10:46:25 -04003481 }
3482 return reclaimed >= to_reclaim;
3483}
3484
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003485/*
3486 * Retries tells us how many times we've called reserve_metadata_bytes. The
3487 * idea is if this is the first call (retries == 0) then we will add to our
3488 * reserved count if we can't make the allocation in order to hold our place
3489 * while we go and try and free up space. That way for retries > 1 we don't try
3490 * and add space, we just check to see if the amount of unused space is >= the
3491 * total space, meaning that our reservation is valid.
3492 *
3493 * However if we don't intend to retry this reservation, pass -1 as retries so
3494 * that it short circuits this logic.
3495 */
3496static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3497 struct btrfs_root *root,
3498 struct btrfs_block_rsv *block_rsv,
3499 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003500{
3501 struct btrfs_space_info *space_info = block_rsv->space_info;
3502 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003503 u64 num_bytes = orig_bytes;
3504 int retries = 0;
3505 int ret = 0;
3506 bool reserved = false;
Josef Bacik38227932010-10-26 12:52:53 -04003507 bool committed = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003508
3509again:
3510 ret = -ENOSPC;
3511 if (reserved)
3512 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003513
3514 spin_lock(&space_info->lock);
3515 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003516 space_info->bytes_pinned + space_info->bytes_readonly +
3517 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003518
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003519 /*
3520 * The idea here is that we've not already over-reserved the block group
3521 * then we can go ahead and save our reservation first and then start
3522 * flushing if we need to. Otherwise if we've already overcommitted
3523 * lets start flushing stuff first and then come back and try to make
3524 * our reservation.
3525 */
3526 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003527 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003528 if (unused >= num_bytes) {
3529 if (!reserved)
3530 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003531 ret = 0;
3532 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003533 /*
3534 * Ok set num_bytes to orig_bytes since we aren't
3535 * overocmmitted, this way we only try and reclaim what
3536 * we need.
3537 */
3538 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003539 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003540 } else {
3541 /*
3542 * Ok we're over committed, set num_bytes to the overcommitted
3543 * amount plus the amount of bytes that we need for this
3544 * reservation.
3545 */
3546 num_bytes = unused - space_info->total_bytes +
3547 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003548 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003549
3550 /*
3551 * Couldn't make our reservation, save our place so while we're trying
3552 * to reclaim space we can actually use it instead of somebody else
3553 * stealing it from us.
3554 */
3555 if (ret && !reserved) {
3556 space_info->bytes_reserved += orig_bytes;
3557 reserved = true;
3558 }
3559
Yan, Zhengf0486c62010-05-16 10:46:25 -04003560 spin_unlock(&space_info->lock);
3561
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003562 if (!ret)
3563 return 0;
3564
3565 if (!flush)
3566 goto out;
3567
3568 /*
3569 * We do synchronous shrinking since we don't actually unreserve
3570 * metadata until after the IO is completed.
3571 */
3572 ret = shrink_delalloc(trans, root, num_bytes, 1);
3573 if (ret > 0)
3574 return 0;
3575 else if (ret < 0)
3576 goto out;
3577
3578 /*
3579 * So if we were overcommitted it's possible that somebody else flushed
3580 * out enough space and we simply didn't have enough space to reclaim,
3581 * so go back around and try again.
3582 */
3583 if (retries < 2) {
3584 retries++;
3585 goto again;
3586 }
3587
3588 spin_lock(&space_info->lock);
3589 /*
3590 * Not enough space to be reclaimed, don't bother committing the
3591 * transaction.
3592 */
3593 if (space_info->bytes_pinned < orig_bytes)
3594 ret = -ENOSPC;
3595 spin_unlock(&space_info->lock);
3596 if (ret)
3597 goto out;
3598
3599 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003600 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003601 goto out;
3602
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003603 ret = -ENOSPC;
3604 trans = btrfs_join_transaction(root, 1);
3605 if (IS_ERR(trans))
3606 goto out;
3607 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003608 if (!ret) {
3609 trans = NULL;
3610 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003611 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003612 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003613
3614out:
3615 if (reserved) {
3616 spin_lock(&space_info->lock);
3617 space_info->bytes_reserved -= orig_bytes;
3618 spin_unlock(&space_info->lock);
3619 }
3620
Yan, Zhengf0486c62010-05-16 10:46:25 -04003621 return ret;
3622}
3623
3624static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3625 struct btrfs_root *root)
3626{
3627 struct btrfs_block_rsv *block_rsv;
3628 if (root->ref_cows)
3629 block_rsv = trans->block_rsv;
3630 else
3631 block_rsv = root->block_rsv;
3632
3633 if (!block_rsv)
3634 block_rsv = &root->fs_info->empty_block_rsv;
3635
3636 return block_rsv;
3637}
3638
3639static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3640 u64 num_bytes)
3641{
3642 int ret = -ENOSPC;
3643 spin_lock(&block_rsv->lock);
3644 if (block_rsv->reserved >= num_bytes) {
3645 block_rsv->reserved -= num_bytes;
3646 if (block_rsv->reserved < block_rsv->size)
3647 block_rsv->full = 0;
3648 ret = 0;
3649 }
3650 spin_unlock(&block_rsv->lock);
3651 return ret;
3652}
3653
3654static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3655 u64 num_bytes, int update_size)
3656{
3657 spin_lock(&block_rsv->lock);
3658 block_rsv->reserved += num_bytes;
3659 if (update_size)
3660 block_rsv->size += num_bytes;
3661 else if (block_rsv->reserved >= block_rsv->size)
3662 block_rsv->full = 1;
3663 spin_unlock(&block_rsv->lock);
3664}
3665
David Sterba62a45b62011-04-20 15:52:26 +02003666static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3667 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003668{
3669 struct btrfs_space_info *space_info = block_rsv->space_info;
3670
3671 spin_lock(&block_rsv->lock);
3672 if (num_bytes == (u64)-1)
3673 num_bytes = block_rsv->size;
3674 block_rsv->size -= num_bytes;
3675 if (block_rsv->reserved >= block_rsv->size) {
3676 num_bytes = block_rsv->reserved - block_rsv->size;
3677 block_rsv->reserved = block_rsv->size;
3678 block_rsv->full = 1;
3679 } else {
3680 num_bytes = 0;
3681 }
3682 spin_unlock(&block_rsv->lock);
3683
3684 if (num_bytes > 0) {
3685 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003686 spin_lock(&dest->lock);
3687 if (!dest->full) {
3688 u64 bytes_to_add;
3689
3690 bytes_to_add = dest->size - dest->reserved;
3691 bytes_to_add = min(num_bytes, bytes_to_add);
3692 dest->reserved += bytes_to_add;
3693 if (dest->reserved >= dest->size)
3694 dest->full = 1;
3695 num_bytes -= bytes_to_add;
3696 }
3697 spin_unlock(&dest->lock);
3698 }
3699 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003700 spin_lock(&space_info->lock);
3701 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003702 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003703 spin_unlock(&space_info->lock);
3704 }
3705 }
3706}
3707
3708static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3709 struct btrfs_block_rsv *dst, u64 num_bytes)
3710{
3711 int ret;
3712
3713 ret = block_rsv_use_bytes(src, num_bytes);
3714 if (ret)
3715 return ret;
3716
3717 block_rsv_add_bytes(dst, num_bytes, 1);
3718 return 0;
3719}
3720
3721void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3722{
3723 memset(rsv, 0, sizeof(*rsv));
3724 spin_lock_init(&rsv->lock);
3725 atomic_set(&rsv->usage, 1);
3726 rsv->priority = 6;
3727 INIT_LIST_HEAD(&rsv->list);
3728}
3729
3730struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3731{
3732 struct btrfs_block_rsv *block_rsv;
3733 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003734
3735 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3736 if (!block_rsv)
3737 return NULL;
3738
3739 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003740 block_rsv->space_info = __find_space_info(fs_info,
3741 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003742 return block_rsv;
3743}
3744
3745void btrfs_free_block_rsv(struct btrfs_root *root,
3746 struct btrfs_block_rsv *rsv)
3747{
3748 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3749 btrfs_block_rsv_release(root, rsv, (u64)-1);
3750 if (!rsv->durable)
3751 kfree(rsv);
3752 }
3753}
3754
3755/*
3756 * make the block_rsv struct be able to capture freed space.
3757 * the captured space will re-add to the the block_rsv struct
3758 * after transaction commit
3759 */
3760void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3761 struct btrfs_block_rsv *block_rsv)
3762{
3763 block_rsv->durable = 1;
3764 mutex_lock(&fs_info->durable_block_rsv_mutex);
3765 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3766 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3767}
3768
3769int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3770 struct btrfs_root *root,
3771 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003772 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003773{
3774 int ret;
3775
3776 if (num_bytes == 0)
3777 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003778
3779 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003780 if (!ret) {
3781 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3782 return 0;
3783 }
3784
Yan, Zhengf0486c62010-05-16 10:46:25 -04003785 return ret;
3786}
3787
3788int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3789 struct btrfs_root *root,
3790 struct btrfs_block_rsv *block_rsv,
3791 u64 min_reserved, int min_factor)
3792{
3793 u64 num_bytes = 0;
3794 int commit_trans = 0;
3795 int ret = -ENOSPC;
3796
3797 if (!block_rsv)
3798 return 0;
3799
3800 spin_lock(&block_rsv->lock);
3801 if (min_factor > 0)
3802 num_bytes = div_factor(block_rsv->size, min_factor);
3803 if (min_reserved > num_bytes)
3804 num_bytes = min_reserved;
3805
3806 if (block_rsv->reserved >= num_bytes) {
3807 ret = 0;
3808 } else {
3809 num_bytes -= block_rsv->reserved;
3810 if (block_rsv->durable &&
3811 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3812 commit_trans = 1;
3813 }
3814 spin_unlock(&block_rsv->lock);
3815 if (!ret)
3816 return 0;
3817
3818 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003819 ret = reserve_metadata_bytes(trans, root, block_rsv,
3820 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003821 if (!ret) {
3822 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3823 return 0;
3824 }
3825 }
3826
3827 if (commit_trans) {
3828 if (trans)
3829 return -EAGAIN;
3830
3831 trans = btrfs_join_transaction(root, 1);
3832 BUG_ON(IS_ERR(trans));
3833 ret = btrfs_commit_transaction(trans, root);
3834 return 0;
3835 }
3836
Yan, Zhengf0486c62010-05-16 10:46:25 -04003837 return -ENOSPC;
3838}
3839
3840int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3841 struct btrfs_block_rsv *dst_rsv,
3842 u64 num_bytes)
3843{
3844 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3845}
3846
3847void btrfs_block_rsv_release(struct btrfs_root *root,
3848 struct btrfs_block_rsv *block_rsv,
3849 u64 num_bytes)
3850{
3851 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3852 if (global_rsv->full || global_rsv == block_rsv ||
3853 block_rsv->space_info != global_rsv->space_info)
3854 global_rsv = NULL;
3855 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3856}
3857
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003858/*
3859 * helper to calculate size of global block reservation.
3860 * the desired value is sum of space used by extent tree,
3861 * checksum tree and root tree
3862 */
3863static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3864{
3865 struct btrfs_space_info *sinfo;
3866 u64 num_bytes;
3867 u64 meta_used;
3868 u64 data_used;
3869 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3870#if 0
3871 /*
3872 * per tree used space accounting can be inaccuracy, so we
3873 * can't rely on it.
3874 */
3875 spin_lock(&fs_info->extent_root->accounting_lock);
3876 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3877 spin_unlock(&fs_info->extent_root->accounting_lock);
3878
3879 spin_lock(&fs_info->csum_root->accounting_lock);
3880 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3881 spin_unlock(&fs_info->csum_root->accounting_lock);
3882
3883 spin_lock(&fs_info->tree_root->accounting_lock);
3884 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3885 spin_unlock(&fs_info->tree_root->accounting_lock);
3886#endif
3887 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3888 spin_lock(&sinfo->lock);
3889 data_used = sinfo->bytes_used;
3890 spin_unlock(&sinfo->lock);
3891
3892 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3893 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003894 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3895 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003896 meta_used = sinfo->bytes_used;
3897 spin_unlock(&sinfo->lock);
3898
3899 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3900 csum_size * 2;
3901 num_bytes += div64_u64(data_used + meta_used, 50);
3902
3903 if (num_bytes * 3 > meta_used)
3904 num_bytes = div64_u64(meta_used, 3);
3905
3906 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3907}
3908
3909static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3910{
3911 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3912 struct btrfs_space_info *sinfo = block_rsv->space_info;
3913 u64 num_bytes;
3914
3915 num_bytes = calc_global_metadata_size(fs_info);
3916
3917 spin_lock(&block_rsv->lock);
3918 spin_lock(&sinfo->lock);
3919
3920 block_rsv->size = num_bytes;
3921
3922 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003923 sinfo->bytes_reserved + sinfo->bytes_readonly +
3924 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003925
3926 if (sinfo->total_bytes > num_bytes) {
3927 num_bytes = sinfo->total_bytes - num_bytes;
3928 block_rsv->reserved += num_bytes;
3929 sinfo->bytes_reserved += num_bytes;
3930 }
3931
3932 if (block_rsv->reserved >= block_rsv->size) {
3933 num_bytes = block_rsv->reserved - block_rsv->size;
3934 sinfo->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003935 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003936 block_rsv->reserved = block_rsv->size;
3937 block_rsv->full = 1;
3938 }
3939#if 0
3940 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3941 block_rsv->size, block_rsv->reserved);
3942#endif
3943 spin_unlock(&sinfo->lock);
3944 spin_unlock(&block_rsv->lock);
3945}
3946
Yan, Zhengf0486c62010-05-16 10:46:25 -04003947static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3948{
3949 struct btrfs_space_info *space_info;
3950
3951 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3952 fs_info->chunk_block_rsv.space_info = space_info;
3953 fs_info->chunk_block_rsv.priority = 10;
3954
3955 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003956 fs_info->global_block_rsv.space_info = space_info;
3957 fs_info->global_block_rsv.priority = 10;
3958 fs_info->global_block_rsv.refill_used = 1;
3959 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003960 fs_info->trans_block_rsv.space_info = space_info;
3961 fs_info->empty_block_rsv.space_info = space_info;
3962 fs_info->empty_block_rsv.priority = 10;
3963
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003964 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3965 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3966 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3967 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003968 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003969
3970 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3971
3972 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3973
3974 update_global_block_rsv(fs_info);
3975}
3976
3977static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3978{
3979 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3980 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3981 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3982 WARN_ON(fs_info->trans_block_rsv.size > 0);
3983 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3984 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3985 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003986}
3987
Yan, Zhenga22285a2010-05-16 10:48:46 -04003988static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3989{
3990 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3991 3 * num_items;
3992}
3993
3994int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3995 struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003996 int num_items)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003997{
3998 u64 num_bytes;
3999 int ret;
4000
4001 if (num_items == 0 || root->fs_info->chunk_root == root)
4002 return 0;
4003
4004 num_bytes = calc_trans_metadata_size(root, num_items);
4005 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004006 num_bytes);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004007 if (!ret) {
4008 trans->bytes_reserved += num_bytes;
4009 trans->block_rsv = &root->fs_info->trans_block_rsv;
4010 }
4011 return ret;
4012}
4013
4014void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4015 struct btrfs_root *root)
4016{
4017 if (!trans->bytes_reserved)
4018 return;
4019
4020 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
4021 btrfs_block_rsv_release(root, trans->block_rsv,
4022 trans->bytes_reserved);
4023 trans->bytes_reserved = 0;
4024}
4025
Yan, Zhengd68fc572010-05-16 10:49:58 -04004026int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4027 struct inode *inode)
4028{
4029 struct btrfs_root *root = BTRFS_I(inode)->root;
4030 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4031 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4032
4033 /*
4034 * one for deleting orphan item, one for updating inode and
4035 * two for calling btrfs_truncate_inode_items.
4036 *
4037 * btrfs_truncate_inode_items is a delete operation, it frees
4038 * more space than it uses in most cases. So two units of
4039 * metadata space should be enough for calling it many times.
4040 * If all of the metadata space is used, we can commit
4041 * transaction and use space it freed.
4042 */
4043 u64 num_bytes = calc_trans_metadata_size(root, 4);
4044 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4045}
4046
4047void btrfs_orphan_release_metadata(struct inode *inode)
4048{
4049 struct btrfs_root *root = BTRFS_I(inode)->root;
4050 u64 num_bytes = calc_trans_metadata_size(root, 4);
4051 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4052}
4053
Yan, Zhenga22285a2010-05-16 10:48:46 -04004054int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4055 struct btrfs_pending_snapshot *pending)
4056{
4057 struct btrfs_root *root = pending->root;
4058 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4059 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4060 /*
4061 * two for root back/forward refs, two for directory entries
4062 * and one for root of the snapshot.
4063 */
4064 u64 num_bytes = calc_trans_metadata_size(root, 5);
4065 dst_rsv->space_info = src_rsv->space_info;
4066 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4067}
4068
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004069static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
4070{
4071 return num_bytes >>= 3;
4072}
4073
4074int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4075{
4076 struct btrfs_root *root = BTRFS_I(inode)->root;
4077 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
4078 u64 to_reserve;
4079 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004080 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004081 int ret;
4082
4083 if (btrfs_transaction_in_commit(root->fs_info))
4084 schedule_timeout(1);
4085
4086 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004087
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004088 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004089 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4090
4091 if (nr_extents > reserved_extents) {
4092 nr_extents -= reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004093 to_reserve = calc_trans_metadata_size(root, nr_extents);
4094 } else {
4095 nr_extents = 0;
4096 to_reserve = 0;
4097 }
Josef Bacik57a45ced2011-01-25 16:30:38 -05004098
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004099 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004100 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
4101 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004102 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004103
Josef Bacik57a45ced2011-01-25 16:30:38 -05004104 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004105 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004106
4107 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4108
4109 if (block_rsv->size > 512 * 1024 * 1024)
Josef Bacik0019f102010-10-15 15:18:40 -04004110 shrink_delalloc(NULL, root, to_reserve, 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004111
4112 return 0;
4113}
4114
4115void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4116{
4117 struct btrfs_root *root = BTRFS_I(inode)->root;
4118 u64 to_free;
4119 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004120 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004121
4122 num_bytes = ALIGN(num_bytes, root->sectorsize);
4123 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik3c148742011-02-02 15:53:47 +00004124 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004125
Josef Bacik57a45ced2011-01-25 16:30:38 -05004126 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4127 do {
4128 int old, new;
4129
4130 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4131 if (nr_extents >= reserved_extents) {
4132 nr_extents = 0;
4133 break;
4134 }
4135 old = reserved_extents;
4136 nr_extents = reserved_extents - nr_extents;
4137 new = reserved_extents - nr_extents;
4138 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
4139 reserved_extents, new);
4140 if (likely(old == reserved_extents))
4141 break;
4142 reserved_extents = old;
4143 } while (1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004144
4145 to_free = calc_csum_metadata_size(inode, num_bytes);
4146 if (nr_extents > 0)
4147 to_free += calc_trans_metadata_size(root, nr_extents);
4148
4149 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4150 to_free);
4151}
4152
4153int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4154{
4155 int ret;
4156
4157 ret = btrfs_check_data_free_space(inode, num_bytes);
4158 if (ret)
4159 return ret;
4160
4161 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4162 if (ret) {
4163 btrfs_free_reserved_data_space(inode, num_bytes);
4164 return ret;
4165 }
4166
4167 return 0;
4168}
4169
4170void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4171{
4172 btrfs_delalloc_release_metadata(inode, num_bytes);
4173 btrfs_free_reserved_data_space(inode, num_bytes);
4174}
4175
Chris Mason9078a3e2007-04-26 16:46:15 -04004176static int update_block_group(struct btrfs_trans_handle *trans,
4177 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004178 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004179{
Josef Bacik0af3d002010-06-21 14:48:16 -04004180 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004181 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004182 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004183 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004184 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004185 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004186
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004187 /* block accounting for super block */
4188 spin_lock(&info->delalloc_lock);
4189 old_val = btrfs_super_bytes_used(&info->super_copy);
4190 if (alloc)
4191 old_val += num_bytes;
4192 else
4193 old_val -= num_bytes;
4194 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004195 spin_unlock(&info->delalloc_lock);
4196
Chris Masond3977122009-01-05 21:25:51 -05004197 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004198 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004199 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004200 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004201 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4202 BTRFS_BLOCK_GROUP_RAID1 |
4203 BTRFS_BLOCK_GROUP_RAID10))
4204 factor = 2;
4205 else
4206 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004207 /*
4208 * If this block group has free space cache written out, we
4209 * need to make sure to load it if we are removing space. This
4210 * is because we need the unpinning stage to actually add the
4211 * space back to the block group, otherwise we will leak space.
4212 */
4213 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004214 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004215
Chris Masondb945352007-10-15 16:15:53 -04004216 byte_in_group = bytenr - cache->key.objectid;
4217 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004218
Josef Bacik25179202008-10-29 14:49:05 -04004219 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004220 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004221
4222 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4223 cache->disk_cache_state < BTRFS_DC_CLEAR)
4224 cache->disk_cache_state = BTRFS_DC_CLEAR;
4225
Josef Bacik0f9dd462008-09-23 13:14:11 -04004226 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004227 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004228 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004229 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004230 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004231 btrfs_set_block_group_used(&cache->item, old_val);
4232 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004233 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004234 cache->space_info->reservation_progress++;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004235 cache->space_info->bytes_used += num_bytes;
4236 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004237 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004238 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004239 } else {
Chris Masondb945352007-10-15 16:15:53 -04004240 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004241 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004242 cache->pinned += num_bytes;
4243 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004244 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004245 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004246 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004247 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004248
Yan, Zhengf0486c62010-05-16 10:46:25 -04004249 set_extent_dirty(info->pinned_extents,
4250 bytenr, bytenr + num_bytes - 1,
4251 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004252 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004253 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004254 total -= num_bytes;
4255 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004256 }
4257 return 0;
4258}
Chris Mason6324fbf2008-03-24 15:01:59 -04004259
Chris Masona061fc82008-05-07 11:43:44 -04004260static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4261{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004262 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004263 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004264
4265 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4266 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004267 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004268
Yan Zhengd2fb3432008-12-11 16:30:39 -05004269 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004270 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004271
4272 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004273}
4274
Yan, Zhengf0486c62010-05-16 10:46:25 -04004275static int pin_down_extent(struct btrfs_root *root,
4276 struct btrfs_block_group_cache *cache,
4277 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004278{
Yan Zheng11833d62009-09-11 16:11:19 -04004279 spin_lock(&cache->space_info->lock);
4280 spin_lock(&cache->lock);
4281 cache->pinned += num_bytes;
4282 cache->space_info->bytes_pinned += num_bytes;
4283 if (reserved) {
4284 cache->reserved -= num_bytes;
4285 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004286 cache->space_info->reservation_progress++;
Yan324ae4d2007-11-16 14:57:08 -05004287 }
Yan Zheng11833d62009-09-11 16:11:19 -04004288 spin_unlock(&cache->lock);
4289 spin_unlock(&cache->space_info->lock);
4290
Yan, Zhengf0486c62010-05-16 10:46:25 -04004291 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4292 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004293 return 0;
4294}
Chris Mason9078a3e2007-04-26 16:46:15 -04004295
Yan, Zhengf0486c62010-05-16 10:46:25 -04004296/*
4297 * this function must be called within transaction
4298 */
4299int btrfs_pin_extent(struct btrfs_root *root,
4300 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004301{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004302 struct btrfs_block_group_cache *cache;
4303
4304 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4305 BUG_ON(!cache);
4306
4307 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4308
4309 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004310 return 0;
4311}
Zheng Yane8569812008-09-26 10:05:48 -04004312
Yan, Zhengf0486c62010-05-16 10:46:25 -04004313/*
4314 * update size of reserved extents. this function may return -EAGAIN
4315 * if 'reserve' is true or 'sinfo' is false.
4316 */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004317int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4318 u64 num_bytes, int reserve, int sinfo)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004319{
4320 int ret = 0;
4321 if (sinfo) {
4322 struct btrfs_space_info *space_info = cache->space_info;
4323 spin_lock(&space_info->lock);
4324 spin_lock(&cache->lock);
4325 if (reserve) {
4326 if (cache->ro) {
4327 ret = -EAGAIN;
4328 } else {
4329 cache->reserved += num_bytes;
4330 space_info->bytes_reserved += num_bytes;
4331 }
4332 } else {
4333 if (cache->ro)
4334 space_info->bytes_readonly += num_bytes;
4335 cache->reserved -= num_bytes;
4336 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004337 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004338 }
4339 spin_unlock(&cache->lock);
4340 spin_unlock(&space_info->lock);
4341 } else {
4342 spin_lock(&cache->lock);
4343 if (cache->ro) {
4344 ret = -EAGAIN;
4345 } else {
4346 if (reserve)
4347 cache->reserved += num_bytes;
4348 else
4349 cache->reserved -= num_bytes;
4350 }
4351 spin_unlock(&cache->lock);
4352 }
4353 return ret;
4354}
4355
Yan Zheng11833d62009-09-11 16:11:19 -04004356int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4357 struct btrfs_root *root)
4358{
4359 struct btrfs_fs_info *fs_info = root->fs_info;
4360 struct btrfs_caching_control *next;
4361 struct btrfs_caching_control *caching_ctl;
4362 struct btrfs_block_group_cache *cache;
4363
4364 down_write(&fs_info->extent_commit_sem);
4365
4366 list_for_each_entry_safe(caching_ctl, next,
4367 &fs_info->caching_block_groups, list) {
4368 cache = caching_ctl->block_group;
4369 if (block_group_cache_done(cache)) {
4370 cache->last_byte_to_unpin = (u64)-1;
4371 list_del_init(&caching_ctl->list);
4372 put_caching_control(caching_ctl);
4373 } else {
4374 cache->last_byte_to_unpin = caching_ctl->progress;
4375 }
4376 }
4377
4378 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4379 fs_info->pinned_extents = &fs_info->freed_extents[1];
4380 else
4381 fs_info->pinned_extents = &fs_info->freed_extents[0];
4382
4383 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004384
4385 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004386 return 0;
4387}
4388
4389static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4390{
4391 struct btrfs_fs_info *fs_info = root->fs_info;
4392 struct btrfs_block_group_cache *cache = NULL;
4393 u64 len;
4394
4395 while (start <= end) {
4396 if (!cache ||
4397 start >= cache->key.objectid + cache->key.offset) {
4398 if (cache)
4399 btrfs_put_block_group(cache);
4400 cache = btrfs_lookup_block_group(fs_info, start);
4401 BUG_ON(!cache);
4402 }
4403
4404 len = cache->key.objectid + cache->key.offset - start;
4405 len = min(len, end + 1 - start);
4406
4407 if (start < cache->last_byte_to_unpin) {
4408 len = min(len, cache->last_byte_to_unpin - start);
4409 btrfs_add_free_space(cache, start, len);
4410 }
Josef Bacik25179202008-10-29 14:49:05 -04004411
Yan, Zhengf0486c62010-05-16 10:46:25 -04004412 start += len;
4413
Josef Bacik25179202008-10-29 14:49:05 -04004414 spin_lock(&cache->space_info->lock);
4415 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004416 cache->pinned -= len;
4417 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004418 if (cache->ro) {
4419 cache->space_info->bytes_readonly += len;
4420 } else if (cache->reserved_pinned > 0) {
4421 len = min(len, cache->reserved_pinned);
4422 cache->reserved_pinned -= len;
4423 cache->space_info->bytes_reserved += len;
4424 }
Josef Bacik25179202008-10-29 14:49:05 -04004425 spin_unlock(&cache->lock);
4426 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004427 }
4428
4429 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004430 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004431 return 0;
4432}
4433
4434int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004435 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004436{
Yan Zheng11833d62009-09-11 16:11:19 -04004437 struct btrfs_fs_info *fs_info = root->fs_info;
4438 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004439 struct btrfs_block_rsv *block_rsv;
4440 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004441 u64 start;
4442 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004443 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004444 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004445
Yan Zheng11833d62009-09-11 16:11:19 -04004446 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4447 unpin = &fs_info->freed_extents[1];
4448 else
4449 unpin = &fs_info->freed_extents[0];
4450
Chris Masond3977122009-01-05 21:25:51 -05004451 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004452 ret = find_first_extent_bit(unpin, 0, &start, &end,
4453 EXTENT_DIRTY);
4454 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004455 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004456
Li Dongyang5378e602011-03-24 10:24:27 +00004457 if (btrfs_test_opt(root, DISCARD))
4458 ret = btrfs_discard_extent(root, start,
4459 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004460
Chris Mason1a5bc162007-10-15 16:15:26 -04004461 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004462 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004463 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004464 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004465
Yan, Zhengf0486c62010-05-16 10:46:25 -04004466 mutex_lock(&fs_info->durable_block_rsv_mutex);
4467 list_for_each_entry_safe(block_rsv, next_rsv,
4468 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004469
Yan, Zhengf0486c62010-05-16 10:46:25 -04004470 idx = trans->transid & 0x1;
4471 if (block_rsv->freed[idx] > 0) {
4472 block_rsv_add_bytes(block_rsv,
4473 block_rsv->freed[idx], 0);
4474 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004475 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004476 if (atomic_read(&block_rsv->usage) == 0) {
4477 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004478
Yan, Zhengf0486c62010-05-16 10:46:25 -04004479 if (block_rsv->freed[0] == 0 &&
4480 block_rsv->freed[1] == 0) {
4481 list_del_init(&block_rsv->list);
4482 kfree(block_rsv);
4483 }
4484 } else {
4485 btrfs_block_rsv_release(root, block_rsv, 0);
4486 }
4487 }
4488 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4489
Chris Masone20d96d2007-03-22 12:13:20 -04004490 return 0;
4491}
4492
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004493static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4494 struct btrfs_root *root,
4495 u64 bytenr, u64 num_bytes, u64 parent,
4496 u64 root_objectid, u64 owner_objectid,
4497 u64 owner_offset, int refs_to_drop,
4498 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004499{
Chris Masone2fa7222007-03-12 16:22:34 -04004500 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004501 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004502 struct btrfs_fs_info *info = root->fs_info;
4503 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004504 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004505 struct btrfs_extent_item *ei;
4506 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004507 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004508 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004509 int extent_slot = 0;
4510 int found_extent = 0;
4511 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004512 u32 item_size;
4513 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004514
Chris Mason5caf2a02007-04-02 11:20:42 -04004515 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004516 if (!path)
4517 return -ENOMEM;
4518
Chris Mason3c12ac72008-04-21 12:01:38 -04004519 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004520 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004521
4522 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4523 BUG_ON(!is_data && refs_to_drop != 1);
4524
4525 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4526 bytenr, num_bytes, parent,
4527 root_objectid, owner_objectid,
4528 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004529 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004530 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004531 while (extent_slot >= 0) {
4532 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004533 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004534 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004535 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004536 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4537 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004538 found_extent = 1;
4539 break;
4540 }
4541 if (path->slots[0] - extent_slot > 5)
4542 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004543 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004544 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004545#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4546 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4547 if (found_extent && item_size < sizeof(*ei))
4548 found_extent = 0;
4549#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004550 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004551 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004552 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004553 NULL, refs_to_drop,
4554 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004555 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004556 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04004557 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004558
4559 key.objectid = bytenr;
4560 key.type = BTRFS_EXTENT_ITEM_KEY;
4561 key.offset = num_bytes;
4562
Zheng Yan31840ae2008-09-23 13:14:14 -04004563 ret = btrfs_search_slot(trans, extent_root,
4564 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004565 if (ret) {
4566 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004567 ", was looking for %llu\n", ret,
4568 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004569 btrfs_print_leaf(extent_root, path->nodes[0]);
4570 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004571 BUG_ON(ret);
4572 extent_slot = path->slots[0];
4573 }
Chris Mason7bb86312007-12-11 09:25:06 -05004574 } else {
4575 btrfs_print_leaf(extent_root, path->nodes[0]);
4576 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004577 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004578 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004579 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004580 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004581 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004582 (unsigned long long)owner_objectid,
4583 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004584 }
Chris Mason5f39d392007-10-15 16:14:19 -04004585
4586 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004587 item_size = btrfs_item_size_nr(leaf, extent_slot);
4588#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4589 if (item_size < sizeof(*ei)) {
4590 BUG_ON(found_extent || extent_slot != path->slots[0]);
4591 ret = convert_extent_item_v0(trans, extent_root, path,
4592 owner_objectid, 0);
4593 BUG_ON(ret < 0);
4594
David Sterbab3b4aa72011-04-21 01:20:15 +02004595 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004596 path->leave_spinning = 1;
4597
4598 key.objectid = bytenr;
4599 key.type = BTRFS_EXTENT_ITEM_KEY;
4600 key.offset = num_bytes;
4601
4602 ret = btrfs_search_slot(trans, extent_root, &key, path,
4603 -1, 1);
4604 if (ret) {
4605 printk(KERN_ERR "umm, got %d back from search"
4606 ", was looking for %llu\n", ret,
4607 (unsigned long long)bytenr);
4608 btrfs_print_leaf(extent_root, path->nodes[0]);
4609 }
4610 BUG_ON(ret);
4611 extent_slot = path->slots[0];
4612 leaf = path->nodes[0];
4613 item_size = btrfs_item_size_nr(leaf, extent_slot);
4614 }
4615#endif
4616 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004617 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004618 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004619 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4620 struct btrfs_tree_block_info *bi;
4621 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4622 bi = (struct btrfs_tree_block_info *)(ei + 1);
4623 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004624 }
4625
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004626 refs = btrfs_extent_refs(leaf, ei);
4627 BUG_ON(refs < refs_to_drop);
4628 refs -= refs_to_drop;
4629
4630 if (refs > 0) {
4631 if (extent_op)
4632 __run_delayed_extent_op(extent_op, leaf, ei);
4633 /*
4634 * In the case of inline back ref, reference count will
4635 * be updated by remove_extent_backref
4636 */
4637 if (iref) {
4638 BUG_ON(!found_extent);
4639 } else {
4640 btrfs_set_extent_refs(leaf, ei, refs);
4641 btrfs_mark_buffer_dirty(leaf);
4642 }
4643 if (found_extent) {
4644 ret = remove_extent_backref(trans, extent_root, path,
4645 iref, refs_to_drop,
4646 is_data);
4647 BUG_ON(ret);
4648 }
4649 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004650 if (found_extent) {
4651 BUG_ON(is_data && refs_to_drop !=
4652 extent_data_ref_count(root, path, iref));
4653 if (iref) {
4654 BUG_ON(path->slots[0] != extent_slot);
4655 } else {
4656 BUG_ON(path->slots[0] != extent_slot + 1);
4657 path->slots[0] = extent_slot;
4658 num_to_del = 2;
4659 }
Chris Mason78fae272007-03-25 11:35:08 -04004660 }
Chris Masonb9473432009-03-13 11:00:37 -04004661
Chris Mason952fcca2008-02-18 16:33:44 -05004662 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4663 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004664 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004665 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01004666
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004667 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004668 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4669 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004670 } else {
4671 invalidate_mapping_pages(info->btree_inode->i_mapping,
4672 bytenr >> PAGE_CACHE_SHIFT,
4673 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004674 }
4675
Yan, Zhengf0486c62010-05-16 10:46:25 -04004676 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004677 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004678 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004679 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004680 return ret;
4681}
4682
4683/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004684 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004685 * delayed ref for that extent as well. This searches the delayed ref tree for
4686 * a given extent, and if there are no other delayed refs to be processed, it
4687 * removes it from the tree.
4688 */
4689static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4690 struct btrfs_root *root, u64 bytenr)
4691{
4692 struct btrfs_delayed_ref_head *head;
4693 struct btrfs_delayed_ref_root *delayed_refs;
4694 struct btrfs_delayed_ref_node *ref;
4695 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004696 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004697
4698 delayed_refs = &trans->transaction->delayed_refs;
4699 spin_lock(&delayed_refs->lock);
4700 head = btrfs_find_delayed_ref_head(trans, bytenr);
4701 if (!head)
4702 goto out;
4703
4704 node = rb_prev(&head->node.rb_node);
4705 if (!node)
4706 goto out;
4707
4708 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4709
4710 /* there are still entries for this ref, we can't drop it */
4711 if (ref->bytenr == bytenr)
4712 goto out;
4713
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004714 if (head->extent_op) {
4715 if (!head->must_insert_reserved)
4716 goto out;
4717 kfree(head->extent_op);
4718 head->extent_op = NULL;
4719 }
4720
Chris Mason1887be62009-03-13 10:11:24 -04004721 /*
4722 * waiting for the lock here would deadlock. If someone else has it
4723 * locked they are already in the process of dropping it anyway
4724 */
4725 if (!mutex_trylock(&head->mutex))
4726 goto out;
4727
4728 /*
4729 * at this point we have a head with no other entries. Go
4730 * ahead and process it.
4731 */
4732 head->node.in_tree = 0;
4733 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004734
Chris Mason1887be62009-03-13 10:11:24 -04004735 delayed_refs->num_entries--;
4736
4737 /*
4738 * we don't take a ref on the node because we're removing it from the
4739 * tree, so we just steal the ref the tree was holding.
4740 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004741 delayed_refs->num_heads--;
4742 if (list_empty(&head->cluster))
4743 delayed_refs->num_heads_ready--;
4744
4745 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004746 spin_unlock(&delayed_refs->lock);
4747
Yan, Zhengf0486c62010-05-16 10:46:25 -04004748 BUG_ON(head->extent_op);
4749 if (head->must_insert_reserved)
4750 ret = 1;
4751
4752 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004753 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004754 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004755out:
4756 spin_unlock(&delayed_refs->lock);
4757 return 0;
4758}
4759
Yan, Zhengf0486c62010-05-16 10:46:25 -04004760void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4761 struct btrfs_root *root,
4762 struct extent_buffer *buf,
4763 u64 parent, int last_ref)
4764{
4765 struct btrfs_block_rsv *block_rsv;
4766 struct btrfs_block_group_cache *cache = NULL;
4767 int ret;
4768
4769 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4770 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4771 parent, root->root_key.objectid,
4772 btrfs_header_level(buf),
4773 BTRFS_DROP_DELAYED_REF, NULL);
4774 BUG_ON(ret);
4775 }
4776
4777 if (!last_ref)
4778 return;
4779
4780 block_rsv = get_block_rsv(trans, root);
4781 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004782 if (block_rsv->space_info != cache->space_info)
4783 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004784
4785 if (btrfs_header_generation(buf) == trans->transid) {
4786 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4787 ret = check_ref_cleanup(trans, root, buf->start);
4788 if (!ret)
4789 goto pin;
4790 }
4791
4792 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4793 pin_down_extent(root, cache, buf->start, buf->len, 1);
4794 goto pin;
4795 }
4796
4797 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4798
4799 btrfs_add_free_space(cache, buf->start, buf->len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00004800 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004801 if (ret == -EAGAIN) {
4802 /* block group became read-only */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004803 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004804 goto out;
4805 }
4806
4807 ret = 1;
4808 spin_lock(&block_rsv->lock);
4809 if (block_rsv->reserved < block_rsv->size) {
4810 block_rsv->reserved += buf->len;
4811 ret = 0;
4812 }
4813 spin_unlock(&block_rsv->lock);
4814
4815 if (ret) {
4816 spin_lock(&cache->space_info->lock);
4817 cache->space_info->bytes_reserved -= buf->len;
Chris Mason36e39c42011-03-12 07:08:42 -05004818 cache->space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004819 spin_unlock(&cache->space_info->lock);
4820 }
4821 goto out;
4822 }
4823pin:
4824 if (block_rsv->durable && !cache->ro) {
4825 ret = 0;
4826 spin_lock(&cache->lock);
4827 if (!cache->ro) {
4828 cache->reserved_pinned += buf->len;
4829 ret = 1;
4830 }
4831 spin_unlock(&cache->lock);
4832
4833 if (ret) {
4834 spin_lock(&block_rsv->lock);
4835 block_rsv->freed[trans->transid & 0x1] += buf->len;
4836 spin_unlock(&block_rsv->lock);
4837 }
4838 }
4839out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004840 /*
4841 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4842 * anymore.
4843 */
4844 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004845 btrfs_put_block_group(cache);
4846}
4847
Chris Mason925baed2008-06-25 16:01:30 -04004848int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004849 struct btrfs_root *root,
4850 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004851 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004852{
4853 int ret;
4854
Chris Mason56bec292009-03-13 10:10:06 -04004855 /*
4856 * tree log blocks never actually go into the extent allocation
4857 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004858 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004859 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4860 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004861 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004862 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004863 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004864 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4865 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4866 parent, root_objectid, (int)owner,
4867 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004868 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004869 } else {
4870 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4871 parent, root_objectid, owner,
4872 offset, BTRFS_DROP_DELAYED_REF, NULL);
4873 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004874 }
Chris Mason925baed2008-06-25 16:01:30 -04004875 return ret;
4876}
4877
Chris Mason87ee04e2007-11-30 11:30:34 -05004878static u64 stripe_align(struct btrfs_root *root, u64 val)
4879{
4880 u64 mask = ((u64)root->stripesize - 1);
4881 u64 ret = (val + mask) & ~mask;
4882 return ret;
4883}
4884
Chris Masonfec577f2007-02-26 10:40:21 -05004885/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004886 * when we wait for progress in the block group caching, its because
4887 * our allocation attempt failed at least once. So, we must sleep
4888 * and let some progress happen before we try again.
4889 *
4890 * This function will sleep at least once waiting for new free space to
4891 * show up, and then it will check the block group free space numbers
4892 * for our min num_bytes. Another option is to have it go ahead
4893 * and look in the rbtree for a free extent of a given size, but this
4894 * is a good start.
4895 */
4896static noinline int
4897wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4898 u64 num_bytes)
4899{
Yan Zheng11833d62009-09-11 16:11:19 -04004900 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004901 DEFINE_WAIT(wait);
4902
Yan Zheng11833d62009-09-11 16:11:19 -04004903 caching_ctl = get_caching_control(cache);
4904 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004905 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004906
Yan Zheng11833d62009-09-11 16:11:19 -04004907 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004908 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004909
4910 put_caching_control(caching_ctl);
4911 return 0;
4912}
4913
4914static noinline int
4915wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4916{
4917 struct btrfs_caching_control *caching_ctl;
4918 DEFINE_WAIT(wait);
4919
4920 caching_ctl = get_caching_control(cache);
4921 if (!caching_ctl)
4922 return 0;
4923
4924 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4925
4926 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004927 return 0;
4928}
4929
Yan, Zhengb742bb82010-05-16 10:46:24 -04004930static int get_block_group_index(struct btrfs_block_group_cache *cache)
4931{
4932 int index;
4933 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4934 index = 0;
4935 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4936 index = 1;
4937 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4938 index = 2;
4939 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4940 index = 3;
4941 else
4942 index = 4;
4943 return index;
4944}
4945
Josef Bacik817d52f2009-07-13 21:29:25 -04004946enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004947 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004948 LOOP_CACHING_NOWAIT = 1,
4949 LOOP_CACHING_WAIT = 2,
4950 LOOP_ALLOC_CHUNK = 3,
4951 LOOP_NO_EMPTY_SIZE = 4,
4952};
4953
4954/*
Chris Masonfec577f2007-02-26 10:40:21 -05004955 * walks the btree of allocated extents and find a hole of a given size.
4956 * The key ins is changed to record the hole:
4957 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004958 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004959 * ins->offset == number of blocks
4960 * Any available blocks before search_start are skipped.
4961 */
Chris Masond3977122009-01-05 21:25:51 -05004962static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004963 struct btrfs_root *orig_root,
4964 u64 num_bytes, u64 empty_size,
4965 u64 search_start, u64 search_end,
4966 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004967 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004968{
Josef Bacik80eb2342008-10-29 14:49:05 -04004969 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004970 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004971 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004972 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004973 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004974 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004975 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004976 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004977 int last_ptr_loop = 0;
4978 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004979 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004980 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004981 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004982 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004983 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004984 u64 ideal_cache_percent = 0;
4985 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004986
Chris Masondb945352007-10-15 16:15:53 -04004987 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004988 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004989 ins->objectid = 0;
4990 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004991
Josef Bacik2552d172009-04-03 10:14:19 -04004992 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004993 if (!space_info) {
4994 printk(KERN_ERR "No space info for %d\n", data);
4995 return -ENOSPC;
4996 }
Josef Bacik2552d172009-04-03 10:14:19 -04004997
Josef Bacik67377732010-09-16 16:19:09 -04004998 /*
4999 * If the space info is for both data and metadata it means we have a
5000 * small filesystem and we can't use the clustering stuff.
5001 */
5002 if (btrfs_mixed_space_info(space_info))
5003 use_cluster = false;
5004
Chris Mason0ef3e662008-05-24 14:04:53 -04005005 if (orig_root->ref_cows || empty_size)
5006 allowed_chunk_alloc = 1;
5007
Josef Bacik67377732010-09-16 16:19:09 -04005008 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005009 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005010 if (!btrfs_test_opt(root, SSD))
5011 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005012 }
5013
Josef Bacik67377732010-09-16 16:19:09 -04005014 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5015 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005016 last_ptr = &root->fs_info->data_alloc_cluster;
5017 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005018
Chris Mason239b14b2008-03-24 15:02:07 -04005019 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005020 spin_lock(&last_ptr->lock);
5021 if (last_ptr->block_group)
5022 hint_byte = last_ptr->window_start;
5023 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005024 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005025
Chris Masona061fc82008-05-07 11:43:44 -04005026 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005027 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005028
Josef Bacik817d52f2009-07-13 21:29:25 -04005029 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005030 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005031
Josef Bacik2552d172009-04-03 10:14:19 -04005032 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005033ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04005034 block_group = btrfs_lookup_block_group(root->fs_info,
5035 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04005036 /*
5037 * we don't want to use the block group if it doesn't match our
5038 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005039 *
5040 * However if we are re-searching with an ideal block group
5041 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005042 */
5043 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05005044 (block_group->cached != BTRFS_CACHE_NO ||
5045 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04005046 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005047 if (list_empty(&block_group->list) ||
5048 block_group->ro) {
5049 /*
5050 * someone is removing this block group,
5051 * we can't jump into the have_block_group
5052 * target because our list pointers are not
5053 * valid
5054 */
5055 btrfs_put_block_group(block_group);
5056 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005057 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005058 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005059 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005060 }
Josef Bacik2552d172009-04-03 10:14:19 -04005061 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005062 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005063 }
Chris Mason42e70e72008-11-07 18:17:11 -05005064 }
Josef Bacik2552d172009-04-03 10:14:19 -04005065search:
Josef Bacik80eb2342008-10-29 14:49:05 -04005066 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005067 list_for_each_entry(block_group, &space_info->block_groups[index],
5068 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005069 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005070 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005071
Josef Bacik11dfe352009-11-13 20:12:59 +00005072 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005073 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005074
Chris Mason83a50de2010-12-13 15:06:46 -05005075 /*
5076 * this can happen if we end up cycling through all the
5077 * raid types, but we want to make sure we only allocate
5078 * for the proper type.
5079 */
5080 if (!block_group_bits(block_group, data)) {
5081 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5082 BTRFS_BLOCK_GROUP_RAID1 |
5083 BTRFS_BLOCK_GROUP_RAID10;
5084
5085 /*
5086 * if they asked for extra copies and this block group
5087 * doesn't provide them, bail. This does allow us to
5088 * fill raid0 from raid1.
5089 */
5090 if ((data & extra) && !(block_group->flags & extra))
5091 goto loop;
5092 }
5093
Josef Bacik2552d172009-04-03 10:14:19 -04005094have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04005095 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005096 u64 free_percent;
5097
Josef Bacikb8399de2010-12-08 09:15:11 -05005098 ret = cache_block_group(block_group, trans,
5099 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04005100 if (block_group->cached == BTRFS_CACHE_FINISHED)
5101 goto have_block_group;
5102
Josef Bacikccf0e722009-11-10 21:23:48 -05005103 free_percent = btrfs_block_group_used(&block_group->item);
5104 free_percent *= 100;
5105 free_percent = div64_u64(free_percent,
5106 block_group->key.offset);
5107 free_percent = 100 - free_percent;
5108 if (free_percent > ideal_cache_percent &&
5109 likely(!block_group->ro)) {
5110 ideal_cache_offset = block_group->key.objectid;
5111 ideal_cache_percent = free_percent;
5112 }
5113
Josef Bacik817d52f2009-07-13 21:29:25 -04005114 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005115 * We only want to start kthread caching if we are at
5116 * the point where we will wait for caching to make
5117 * progress, or if our ideal search is over and we've
5118 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04005119 */
5120 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05005121 (loop > LOOP_FIND_IDEAL &&
5122 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacikb8399de2010-12-08 09:15:11 -05005123 ret = cache_block_group(block_group, trans,
5124 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005125 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005126 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005127 found_uncached_bg = true;
5128
5129 /*
5130 * If loop is set for cached only, try the next block
5131 * group.
5132 */
5133 if (loop == LOOP_FIND_IDEAL)
5134 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005135 }
5136
Josef Bacik817d52f2009-07-13 21:29:25 -04005137 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005138 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005139 found_uncached_bg = true;
5140
Josef Bacikea6a4782008-11-20 12:16:16 -05005141 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005142 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005143
Josef Bacik0a243252009-09-11 16:11:20 -04005144 /*
5145 * Ok we want to try and use the cluster allocator, so lets look
5146 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5147 * have tried the cluster allocator plenty of times at this
5148 * point and not have found anything, so we are likely way too
5149 * fragmented for the clustering stuff to find anything, so lets
5150 * just skip it and let the allocator find whatever block it can
5151 * find
5152 */
5153 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005154 /*
5155 * the refill lock keeps out other
5156 * people trying to start a new cluster
5157 */
5158 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005159 if (last_ptr->block_group &&
5160 (last_ptr->block_group->ro ||
5161 !block_group_bits(last_ptr->block_group, data))) {
5162 offset = 0;
5163 goto refill_cluster;
5164 }
5165
Chris Masonfa9c0d72009-04-03 09:47:43 -04005166 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5167 num_bytes, search_start);
5168 if (offset) {
5169 /* we have a block, we're done */
5170 spin_unlock(&last_ptr->refill_lock);
5171 goto checks;
5172 }
5173
5174 spin_lock(&last_ptr->lock);
5175 /*
5176 * whoops, this cluster doesn't actually point to
5177 * this block group. Get a ref on the block
5178 * group is does point to and try again
5179 */
5180 if (!last_ptr_loop && last_ptr->block_group &&
5181 last_ptr->block_group != block_group) {
5182
5183 btrfs_put_block_group(block_group);
5184 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005185 btrfs_get_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005186 spin_unlock(&last_ptr->lock);
5187 spin_unlock(&last_ptr->refill_lock);
5188
5189 last_ptr_loop = 1;
5190 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005191 /*
5192 * we know this block group is properly
5193 * in the list because
5194 * btrfs_remove_block_group, drops the
5195 * cluster before it removes the block
5196 * group from the list
5197 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04005198 goto have_block_group;
5199 }
5200 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005201refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04005202 /*
5203 * this cluster didn't work out, free it and
5204 * start over
5205 */
5206 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5207
5208 last_ptr_loop = 0;
5209
5210 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005211 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005212 block_group, last_ptr,
5213 offset, num_bytes,
5214 empty_cluster + empty_size);
5215 if (ret == 0) {
5216 /*
5217 * now pull our allocation out of this
5218 * cluster
5219 */
5220 offset = btrfs_alloc_from_cluster(block_group,
5221 last_ptr, num_bytes,
5222 search_start);
5223 if (offset) {
5224 /* we found one, proceed */
5225 spin_unlock(&last_ptr->refill_lock);
5226 goto checks;
5227 }
Josef Bacik0a243252009-09-11 16:11:20 -04005228 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5229 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005230 spin_unlock(&last_ptr->refill_lock);
5231
Josef Bacik0a243252009-09-11 16:11:20 -04005232 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005233 wait_block_group_cache_progress(block_group,
5234 num_bytes + empty_cluster + empty_size);
5235 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005236 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005237
Chris Masonfa9c0d72009-04-03 09:47:43 -04005238 /*
5239 * at this point we either didn't find a cluster
5240 * or we weren't able to allocate a block from our
5241 * cluster. Free the cluster we've been trying
5242 * to use, and go to the next block group
5243 */
Josef Bacik0a243252009-09-11 16:11:20 -04005244 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005245 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005246 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005247 }
5248
Josef Bacik6226cb02009-04-03 10:14:18 -04005249 offset = btrfs_find_space_for_alloc(block_group, search_start,
5250 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005251 /*
5252 * If we didn't find a chunk, and we haven't failed on this
5253 * block group before, and this block group is in the middle of
5254 * caching and we are ok with waiting, then go ahead and wait
5255 * for progress to be made, and set failed_alloc to true.
5256 *
5257 * If failed_alloc is true then we've already waited on this
5258 * block group once and should move on to the next block group.
5259 */
5260 if (!offset && !failed_alloc && !cached &&
5261 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005262 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005263 num_bytes + empty_size);
5264 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005265 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005266 } else if (!offset) {
5267 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005268 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005269checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005270 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005271 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005272 if (search_start + num_bytes >= search_end) {
5273 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005274 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005275 }
Chris Masone37c9e62007-05-09 20:13:14 -04005276
Josef Bacik2552d172009-04-03 10:14:19 -04005277 /* move on to the next group */
5278 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005279 block_group->key.objectid + block_group->key.offset) {
5280 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005281 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005282 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005283
Josef Bacik2552d172009-04-03 10:14:19 -04005284 ins->objectid = search_start;
5285 ins->offset = num_bytes;
5286
Josef Bacik6226cb02009-04-03 10:14:18 -04005287 if (offset < search_start)
5288 btrfs_add_free_space(block_group, offset,
5289 search_start - offset);
5290 BUG_ON(offset > search_start);
5291
Li Dongyangb4d00d52011-03-24 10:24:25 +00005292 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005293 (data & BTRFS_BLOCK_GROUP_DATA));
5294 if (ret == -EAGAIN) {
5295 btrfs_add_free_space(block_group, offset, num_bytes);
5296 goto loop;
5297 }
Yan Zheng11833d62009-09-11 16:11:19 -04005298
Josef Bacik2552d172009-04-03 10:14:19 -04005299 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005300 ins->objectid = search_start;
5301 ins->offset = num_bytes;
5302
5303 if (offset < search_start)
5304 btrfs_add_free_space(block_group, offset,
5305 search_start - offset);
5306 BUG_ON(offset > search_start);
Josef Bacik2552d172009-04-03 10:14:19 -04005307 break;
5308loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005309 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005310 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005311 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d72009-04-03 09:47:43 -04005312 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005313 }
5314 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005315
Yan, Zhengb742bb82010-05-16 10:46:24 -04005316 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5317 goto search;
5318
Josef Bacikccf0e722009-11-10 21:23:48 -05005319 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5320 * for them to make caching progress. Also
5321 * determine the best possible bg to cache
5322 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5323 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005324 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5325 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5326 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5327 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04005328 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005329 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5330 (found_uncached_bg || empty_size || empty_cluster ||
5331 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005332 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005333 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005334 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005335 loop++;
5336 if (!ideal_cache_percent &&
5337 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005338 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005339
5340 /*
5341 * 1 of the following 2 things have happened so far
5342 *
5343 * 1) We found an ideal block group for caching that
5344 * is mostly full and will cache quickly, so we might
5345 * as well wait for it.
5346 *
5347 * 2) We searched for cached only and we didn't find
5348 * anything, and we didn't start any caching kthreads
5349 * either, so chances are we will loop through and
5350 * start a couple caching kthreads, and then come back
5351 * around and just wait for them. This will be slower
5352 * because we will have 2 caching kthreads reading at
5353 * the same time when we could have just started one
5354 * and waited for it to get far enough to give us an
5355 * allocation, so go ahead and go to the wait caching
5356 * loop.
5357 */
5358 loop = LOOP_CACHING_WAIT;
5359 search_start = ideal_cache_offset;
5360 ideal_cache_percent = 0;
5361 goto ideal_cache;
5362 } else if (loop == LOOP_FIND_IDEAL) {
5363 /*
5364 * Didn't find a uncached bg, wait on anything we find
5365 * next.
5366 */
5367 loop = LOOP_CACHING_WAIT;
5368 goto search;
5369 }
5370
5371 if (loop < LOOP_CACHING_WAIT) {
5372 loop++;
5373 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005374 }
5375
5376 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005377 empty_size = 0;
5378 empty_cluster = 0;
5379 }
Chris Mason42e70e72008-11-07 18:17:11 -05005380
Josef Bacik2552d172009-04-03 10:14:19 -04005381 if (allowed_chunk_alloc) {
5382 ret = do_chunk_alloc(trans, root, num_bytes +
Chris Mason0e4f8f82011-04-15 16:05:44 -04005383 2 * 1024 * 1024, data,
5384 CHUNK_ALLOC_LIMITED);
Josef Bacik2552d172009-04-03 10:14:19 -04005385 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005386 done_chunk_alloc = 1;
Chris Mason0e4f8f82011-04-15 16:05:44 -04005387 } else if (!done_chunk_alloc &&
5388 space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) {
5389 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005390 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005391
Josef Bacik817d52f2009-07-13 21:29:25 -04005392 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005393 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005394 goto search;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005395 }
Josef Bacik2552d172009-04-03 10:14:19 -04005396 ret = -ENOSPC;
5397 } else if (!ins->objectid) {
5398 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04005399 }
Chris Mason0b86a832008-03-24 15:01:56 -04005400
Josef Bacik80eb2342008-10-29 14:49:05 -04005401 /* we found what we needed */
5402 if (ins->objectid) {
5403 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05005404 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04005405
Chris Masonfa9c0d72009-04-03 09:47:43 -04005406 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005407 ret = 0;
5408 }
Chris Mason0b86a832008-03-24 15:01:56 -04005409
Chris Mason0f70abe2007-02-28 16:46:22 -05005410 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005411}
Chris Masonec44a352008-04-28 15:29:52 -04005412
Josef Bacik9ed74f22009-09-11 16:12:44 -04005413static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5414 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005415{
5416 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005417 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005418
Josef Bacik9ed74f22009-09-11 16:12:44 -04005419 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005420 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5421 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005422 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005423 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005424 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005425 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5426 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005427 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005428 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005429 (unsigned long long)info->bytes_pinned,
5430 (unsigned long long)info->bytes_reserved,
5431 (unsigned long long)info->bytes_may_use,
5432 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005433 spin_unlock(&info->lock);
5434
5435 if (!dump_block_groups)
5436 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005437
Josef Bacik80eb2342008-10-29 14:49:05 -04005438 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005439again:
5440 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005441 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005442 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5443 "%llu pinned %llu reserved\n",
5444 (unsigned long long)cache->key.objectid,
5445 (unsigned long long)cache->key.offset,
5446 (unsigned long long)btrfs_block_group_used(&cache->item),
5447 (unsigned long long)cache->pinned,
5448 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005449 btrfs_dump_free_space(cache, bytes);
5450 spin_unlock(&cache->lock);
5451 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005452 if (++index < BTRFS_NR_RAID_TYPES)
5453 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005454 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005455}
Zheng Yane8569812008-09-26 10:05:48 -04005456
Yan Zheng11833d62009-09-11 16:11:19 -04005457int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5458 struct btrfs_root *root,
5459 u64 num_bytes, u64 min_alloc_size,
5460 u64 empty_size, u64 hint_byte,
5461 u64 search_end, struct btrfs_key *ins,
5462 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005463{
5464 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005465 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005466
Josef Bacik6a632092009-02-20 11:00:09 -05005467 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005468again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005469 /*
5470 * the only place that sets empty_size is btrfs_realloc_node, which
5471 * is not called recursively on allocations
5472 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005473 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005474 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005475 num_bytes + 2 * 1024 * 1024, data,
5476 CHUNK_ALLOC_NO_FORCE);
Chris Mason0b86a832008-03-24 15:01:56 -04005477
Chris Masondb945352007-10-15 16:15:53 -04005478 WARN_ON(num_bytes < root->sectorsize);
5479 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005480 search_start, search_end, hint_byte,
5481 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005482
Chris Mason98d20f62008-04-14 09:46:10 -04005483 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5484 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005485 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005486 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005487 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005488 num_bytes, data, CHUNK_ALLOC_FORCE);
Chris Mason98d20f62008-04-14 09:46:10 -04005489 goto again;
5490 }
Chris Mason91435652011-02-16 13:10:41 -05005491 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005492 struct btrfs_space_info *sinfo;
5493
5494 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005495 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5496 "wanted %llu\n", (unsigned long long)data,
5497 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005498 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005499 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005500
liubo1abe9b82011-03-24 11:18:59 +00005501 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5502
Josef Bacik0f9dd462008-09-23 13:14:11 -04005503 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005504}
5505
Chris Mason65b51a02008-08-01 15:11:20 -04005506int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5507{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005508 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005509 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005510
Josef Bacik0f9dd462008-09-23 13:14:11 -04005511 cache = btrfs_lookup_block_group(root->fs_info, start);
5512 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005513 printk(KERN_ERR "Unable to find block group for %llu\n",
5514 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005515 return -ENOSPC;
5516 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005517
Li Dongyang5378e602011-03-24 10:24:27 +00005518 if (btrfs_test_opt(root, DISCARD))
5519 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005520
Josef Bacik0f9dd462008-09-23 13:14:11 -04005521 btrfs_add_free_space(cache, start, len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00005522 btrfs_update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005523 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005524
liubo1abe9b82011-03-24 11:18:59 +00005525 trace_btrfs_reserved_extent_free(root, start, len);
5526
Chris Masone6dcd2d2008-07-17 12:53:50 -04005527 return ret;
5528}
5529
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005530static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5531 struct btrfs_root *root,
5532 u64 parent, u64 root_objectid,
5533 u64 flags, u64 owner, u64 offset,
5534 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005535{
5536 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005537 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005538 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005539 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005540 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005541 struct extent_buffer *leaf;
5542 int type;
5543 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005544
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005545 if (parent > 0)
5546 type = BTRFS_SHARED_DATA_REF_KEY;
5547 else
5548 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005549
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005550 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005551
5552 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005553 if (!path)
5554 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005555
Chris Masonb9473432009-03-13 11:00:37 -04005556 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005557 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5558 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005559 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005560
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561 leaf = path->nodes[0];
5562 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005563 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005564 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5565 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5566 btrfs_set_extent_flags(leaf, extent_item,
5567 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005568
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005569 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5570 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5571 if (parent > 0) {
5572 struct btrfs_shared_data_ref *ref;
5573 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5574 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5575 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5576 } else {
5577 struct btrfs_extent_data_ref *ref;
5578 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5579 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5580 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5581 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5582 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5583 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005584
5585 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005586 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005587
Yan, Zhengf0486c62010-05-16 10:46:25 -04005588 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005589 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005590 printk(KERN_ERR "btrfs update block group failed for %llu "
5591 "%llu\n", (unsigned long long)ins->objectid,
5592 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005593 BUG();
5594 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005595 return ret;
5596}
5597
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005598static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5599 struct btrfs_root *root,
5600 u64 parent, u64 root_objectid,
5601 u64 flags, struct btrfs_disk_key *key,
5602 int level, struct btrfs_key *ins)
5603{
5604 int ret;
5605 struct btrfs_fs_info *fs_info = root->fs_info;
5606 struct btrfs_extent_item *extent_item;
5607 struct btrfs_tree_block_info *block_info;
5608 struct btrfs_extent_inline_ref *iref;
5609 struct btrfs_path *path;
5610 struct extent_buffer *leaf;
5611 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5612
5613 path = btrfs_alloc_path();
5614 BUG_ON(!path);
5615
5616 path->leave_spinning = 1;
5617 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5618 ins, size);
5619 BUG_ON(ret);
5620
5621 leaf = path->nodes[0];
5622 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5623 struct btrfs_extent_item);
5624 btrfs_set_extent_refs(leaf, extent_item, 1);
5625 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5626 btrfs_set_extent_flags(leaf, extent_item,
5627 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5628 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5629
5630 btrfs_set_tree_block_key(leaf, block_info, key);
5631 btrfs_set_tree_block_level(leaf, block_info, level);
5632
5633 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5634 if (parent > 0) {
5635 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5636 btrfs_set_extent_inline_ref_type(leaf, iref,
5637 BTRFS_SHARED_BLOCK_REF_KEY);
5638 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5639 } else {
5640 btrfs_set_extent_inline_ref_type(leaf, iref,
5641 BTRFS_TREE_BLOCK_REF_KEY);
5642 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5643 }
5644
5645 btrfs_mark_buffer_dirty(leaf);
5646 btrfs_free_path(path);
5647
Yan, Zhengf0486c62010-05-16 10:46:25 -04005648 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005649 if (ret) {
5650 printk(KERN_ERR "btrfs update block group failed for %llu "
5651 "%llu\n", (unsigned long long)ins->objectid,
5652 (unsigned long long)ins->offset);
5653 BUG();
5654 }
5655 return ret;
5656}
5657
5658int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5659 struct btrfs_root *root,
5660 u64 root_objectid, u64 owner,
5661 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005662{
5663 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005664
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005665 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005666
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005667 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5668 0, root_objectid, owner, offset,
5669 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005670 return ret;
5671}
Chris Masone02119d2008-09-05 16:13:11 -04005672
5673/*
5674 * this is used by the tree logging recovery code. It records that
5675 * an extent has been allocated and makes sure to clear the free
5676 * space cache bits as well
5677 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005678int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5679 struct btrfs_root *root,
5680 u64 root_objectid, u64 owner, u64 offset,
5681 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005682{
5683 int ret;
5684 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005685 struct btrfs_caching_control *caching_ctl;
5686 u64 start = ins->objectid;
5687 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005688
Chris Masone02119d2008-09-05 16:13:11 -04005689 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005690 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005691 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005692
Yan Zheng11833d62009-09-11 16:11:19 -04005693 if (!caching_ctl) {
5694 BUG_ON(!block_group_cache_done(block_group));
5695 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5696 BUG_ON(ret);
5697 } else {
5698 mutex_lock(&caching_ctl->mutex);
5699
5700 if (start >= caching_ctl->progress) {
5701 ret = add_excluded_extent(root, start, num_bytes);
5702 BUG_ON(ret);
5703 } else if (start + num_bytes <= caching_ctl->progress) {
5704 ret = btrfs_remove_free_space(block_group,
5705 start, num_bytes);
5706 BUG_ON(ret);
5707 } else {
5708 num_bytes = caching_ctl->progress - start;
5709 ret = btrfs_remove_free_space(block_group,
5710 start, num_bytes);
5711 BUG_ON(ret);
5712
5713 start = caching_ctl->progress;
5714 num_bytes = ins->objectid + ins->offset -
5715 caching_ctl->progress;
5716 ret = add_excluded_extent(root, start, num_bytes);
5717 BUG_ON(ret);
5718 }
5719
5720 mutex_unlock(&caching_ctl->mutex);
5721 put_caching_control(caching_ctl);
5722 }
5723
Li Dongyangb4d00d52011-03-24 10:24:25 +00005724 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005725 BUG_ON(ret);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005726 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005727 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5728 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005729 return ret;
5730}
5731
Chris Mason65b51a02008-08-01 15:11:20 -04005732struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5733 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005734 u64 bytenr, u32 blocksize,
5735 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005736{
5737 struct extent_buffer *buf;
5738
5739 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5740 if (!buf)
5741 return ERR_PTR(-ENOMEM);
5742 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005743 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005744 btrfs_tree_lock(buf);
5745 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005746
5747 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005748 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005749
Chris Masond0c803c2008-09-11 16:17:57 -04005750 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005751 /*
5752 * we allow two log transactions at a time, use different
5753 * EXENT bit to differentiate dirty pages.
5754 */
5755 if (root->log_transid % 2 == 0)
5756 set_extent_dirty(&root->dirty_log_pages, buf->start,
5757 buf->start + buf->len - 1, GFP_NOFS);
5758 else
5759 set_extent_new(&root->dirty_log_pages, buf->start,
5760 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005761 } else {
5762 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5763 buf->start + buf->len - 1, GFP_NOFS);
5764 }
Chris Mason65b51a02008-08-01 15:11:20 -04005765 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005766 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005767 return buf;
5768}
5769
Yan, Zhengf0486c62010-05-16 10:46:25 -04005770static struct btrfs_block_rsv *
5771use_block_rsv(struct btrfs_trans_handle *trans,
5772 struct btrfs_root *root, u32 blocksize)
5773{
5774 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005775 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005776 int ret;
5777
5778 block_rsv = get_block_rsv(trans, root);
5779
5780 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005781 ret = reserve_metadata_bytes(trans, root, block_rsv,
5782 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005783 /*
5784 * If we couldn't reserve metadata bytes try and use some from
5785 * the global reserve.
5786 */
5787 if (ret && block_rsv != global_rsv) {
5788 ret = block_rsv_use_bytes(global_rsv, blocksize);
5789 if (!ret)
5790 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005791 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005792 } else if (ret) {
5793 return ERR_PTR(ret);
5794 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005795 return block_rsv;
5796 }
5797
5798 ret = block_rsv_use_bytes(block_rsv, blocksize);
5799 if (!ret)
5800 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005801 if (ret) {
5802 WARN_ON(1);
5803 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5804 0);
5805 if (!ret) {
5806 spin_lock(&block_rsv->lock);
5807 block_rsv->size += blocksize;
5808 spin_unlock(&block_rsv->lock);
5809 return block_rsv;
5810 } else if (ret && block_rsv != global_rsv) {
5811 ret = block_rsv_use_bytes(global_rsv, blocksize);
5812 if (!ret)
5813 return global_rsv;
5814 }
5815 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005816
Yan, Zhengf0486c62010-05-16 10:46:25 -04005817 return ERR_PTR(-ENOSPC);
5818}
5819
5820static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5821{
5822 block_rsv_add_bytes(block_rsv, blocksize, 0);
5823 block_rsv_release_bytes(block_rsv, NULL, 0);
5824}
5825
Chris Masonfec577f2007-02-26 10:40:21 -05005826/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005827 * finds a free extent and does all the dirty work required for allocation
5828 * returns the key for the extent through ins, and a tree buffer for
5829 * the first block of the extent through buf.
5830 *
Chris Masonfec577f2007-02-26 10:40:21 -05005831 * returns the tree buffer or NULL.
5832 */
Chris Mason5f39d392007-10-15 16:14:19 -04005833struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005834 struct btrfs_root *root, u32 blocksize,
5835 u64 parent, u64 root_objectid,
5836 struct btrfs_disk_key *key, int level,
5837 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005838{
Chris Masone2fa7222007-03-12 16:22:34 -04005839 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005840 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005841 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005842 u64 flags = 0;
5843 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005844
Yan, Zhengf0486c62010-05-16 10:46:25 -04005845
5846 block_rsv = use_block_rsv(trans, root, blocksize);
5847 if (IS_ERR(block_rsv))
5848 return ERR_CAST(block_rsv);
5849
5850 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5851 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005852 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005853 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005854 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005855 }
Chris Mason55c69072008-01-09 15:55:33 -05005856
Chris Mason4008c042009-02-12 14:09:45 -05005857 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5858 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005859 BUG_ON(IS_ERR(buf));
5860
5861 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5862 if (parent == 0)
5863 parent = ins.objectid;
5864 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5865 } else
5866 BUG_ON(parent > 0);
5867
5868 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5869 struct btrfs_delayed_extent_op *extent_op;
5870 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5871 BUG_ON(!extent_op);
5872 if (key)
5873 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5874 else
5875 memset(&extent_op->key, 0, sizeof(extent_op->key));
5876 extent_op->flags_to_set = flags;
5877 extent_op->update_key = 1;
5878 extent_op->update_flags = 1;
5879 extent_op->is_data = 0;
5880
5881 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5882 ins.offset, parent, root_objectid,
5883 level, BTRFS_ADD_DELAYED_EXTENT,
5884 extent_op);
5885 BUG_ON(ret);
5886 }
Chris Masonfec577f2007-02-26 10:40:21 -05005887 return buf;
5888}
Chris Masona28ec192007-03-06 20:08:01 -05005889
Yan Zheng2c47e6052009-06-27 21:07:35 -04005890struct walk_control {
5891 u64 refs[BTRFS_MAX_LEVEL];
5892 u64 flags[BTRFS_MAX_LEVEL];
5893 struct btrfs_key update_progress;
5894 int stage;
5895 int level;
5896 int shared_level;
5897 int update_ref;
5898 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005899 int reada_slot;
5900 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005901};
5902
5903#define DROP_REFERENCE 1
5904#define UPDATE_BACKREF 2
5905
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005906static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5907 struct btrfs_root *root,
5908 struct walk_control *wc,
5909 struct btrfs_path *path)
5910{
5911 u64 bytenr;
5912 u64 generation;
5913 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005914 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005915 u32 nritems;
5916 u32 blocksize;
5917 struct btrfs_key key;
5918 struct extent_buffer *eb;
5919 int ret;
5920 int slot;
5921 int nread = 0;
5922
5923 if (path->slots[wc->level] < wc->reada_slot) {
5924 wc->reada_count = wc->reada_count * 2 / 3;
5925 wc->reada_count = max(wc->reada_count, 2);
5926 } else {
5927 wc->reada_count = wc->reada_count * 3 / 2;
5928 wc->reada_count = min_t(int, wc->reada_count,
5929 BTRFS_NODEPTRS_PER_BLOCK(root));
5930 }
5931
5932 eb = path->nodes[wc->level];
5933 nritems = btrfs_header_nritems(eb);
5934 blocksize = btrfs_level_size(root, wc->level - 1);
5935
5936 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5937 if (nread >= wc->reada_count)
5938 break;
5939
5940 cond_resched();
5941 bytenr = btrfs_node_blockptr(eb, slot);
5942 generation = btrfs_node_ptr_generation(eb, slot);
5943
5944 if (slot == path->slots[wc->level])
5945 goto reada;
5946
5947 if (wc->stage == UPDATE_BACKREF &&
5948 generation <= root->root_key.offset)
5949 continue;
5950
Yan, Zheng94fcca92009-10-09 09:25:16 -04005951 /* We don't lock the tree block, it's OK to be racy here */
5952 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5953 &refs, &flags);
5954 BUG_ON(ret);
5955 BUG_ON(refs == 0);
5956
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005957 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005958 if (refs == 1)
5959 goto reada;
5960
Yan, Zheng94fcca92009-10-09 09:25:16 -04005961 if (wc->level == 1 &&
5962 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5963 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005964 if (!wc->update_ref ||
5965 generation <= root->root_key.offset)
5966 continue;
5967 btrfs_node_key_to_cpu(eb, &key, slot);
5968 ret = btrfs_comp_cpu_keys(&key,
5969 &wc->update_progress);
5970 if (ret < 0)
5971 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005972 } else {
5973 if (wc->level == 1 &&
5974 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5975 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005976 }
5977reada:
5978 ret = readahead_tree_block(root, bytenr, blocksize,
5979 generation);
5980 if (ret)
5981 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005982 nread++;
5983 }
5984 wc->reada_slot = slot;
5985}
5986
Chris Mason9aca1d52007-03-13 11:09:37 -04005987/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005988 * hepler to process tree block while walking down the tree.
5989 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005990 * when wc->stage == UPDATE_BACKREF, this function updates
5991 * back refs for pointers in the block.
5992 *
5993 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005994 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005995static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5996 struct btrfs_root *root,
5997 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005998 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005999{
6000 int level = wc->level;
6001 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006002 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6003 int ret;
6004
6005 if (wc->stage == UPDATE_BACKREF &&
6006 btrfs_header_owner(eb) != root->root_key.objectid)
6007 return 1;
6008
6009 /*
6010 * when reference count of tree block is 1, it won't increase
6011 * again. once full backref flag is set, we never clear it.
6012 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006013 if (lookup_info &&
6014 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6015 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006016 BUG_ON(!path->locks[level]);
6017 ret = btrfs_lookup_extent_info(trans, root,
6018 eb->start, eb->len,
6019 &wc->refs[level],
6020 &wc->flags[level]);
6021 BUG_ON(ret);
6022 BUG_ON(wc->refs[level] == 0);
6023 }
6024
Yan Zheng2c47e6052009-06-27 21:07:35 -04006025 if (wc->stage == DROP_REFERENCE) {
6026 if (wc->refs[level] > 1)
6027 return 1;
6028
6029 if (path->locks[level] && !wc->keep_locks) {
6030 btrfs_tree_unlock(eb);
6031 path->locks[level] = 0;
6032 }
6033 return 0;
6034 }
6035
6036 /* wc->stage == UPDATE_BACKREF */
6037 if (!(wc->flags[level] & flag)) {
6038 BUG_ON(!path->locks[level]);
6039 ret = btrfs_inc_ref(trans, root, eb, 1);
6040 BUG_ON(ret);
6041 ret = btrfs_dec_ref(trans, root, eb, 0);
6042 BUG_ON(ret);
6043 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6044 eb->len, flag, 0);
6045 BUG_ON(ret);
6046 wc->flags[level] |= flag;
6047 }
6048
6049 /*
6050 * the block is shared by multiple trees, so it's not good to
6051 * keep the tree lock
6052 */
6053 if (path->locks[level] && level > 0) {
6054 btrfs_tree_unlock(eb);
6055 path->locks[level] = 0;
6056 }
6057 return 0;
6058}
6059
6060/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006061 * hepler to process tree block pointer.
6062 *
6063 * when wc->stage == DROP_REFERENCE, this function checks
6064 * reference count of the block pointed to. if the block
6065 * is shared and we need update back refs for the subtree
6066 * rooted at the block, this function changes wc->stage to
6067 * UPDATE_BACKREF. if the block is shared and there is no
6068 * need to update back, this function drops the reference
6069 * to the block.
6070 *
6071 * NOTE: return value 1 means we should stop walking down.
6072 */
6073static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6074 struct btrfs_root *root,
6075 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006076 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006077{
6078 u64 bytenr;
6079 u64 generation;
6080 u64 parent;
6081 u32 blocksize;
6082 struct btrfs_key key;
6083 struct extent_buffer *next;
6084 int level = wc->level;
6085 int reada = 0;
6086 int ret = 0;
6087
6088 generation = btrfs_node_ptr_generation(path->nodes[level],
6089 path->slots[level]);
6090 /*
6091 * if the lower level block was created before the snapshot
6092 * was created, we know there is no need to update back refs
6093 * for the subtree
6094 */
6095 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006096 generation <= root->root_key.offset) {
6097 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006098 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006099 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006100
6101 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6102 blocksize = btrfs_level_size(root, level - 1);
6103
6104 next = btrfs_find_tree_block(root, bytenr, blocksize);
6105 if (!next) {
6106 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00006107 if (!next)
6108 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006109 reada = 1;
6110 }
6111 btrfs_tree_lock(next);
6112 btrfs_set_lock_blocking(next);
6113
Yan, Zheng94fcca92009-10-09 09:25:16 -04006114 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6115 &wc->refs[level - 1],
6116 &wc->flags[level - 1]);
6117 BUG_ON(ret);
6118 BUG_ON(wc->refs[level - 1] == 0);
6119 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006120
Yan, Zheng94fcca92009-10-09 09:25:16 -04006121 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006122 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006123 if (level == 1 &&
6124 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6125 goto skip;
6126
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006127 if (!wc->update_ref ||
6128 generation <= root->root_key.offset)
6129 goto skip;
6130
6131 btrfs_node_key_to_cpu(path->nodes[level], &key,
6132 path->slots[level]);
6133 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6134 if (ret < 0)
6135 goto skip;
6136
6137 wc->stage = UPDATE_BACKREF;
6138 wc->shared_level = level - 1;
6139 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006140 } else {
6141 if (level == 1 &&
6142 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6143 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006144 }
6145
6146 if (!btrfs_buffer_uptodate(next, generation)) {
6147 btrfs_tree_unlock(next);
6148 free_extent_buffer(next);
6149 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006150 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006151 }
6152
6153 if (!next) {
6154 if (reada && level == 1)
6155 reada_walk_down(trans, root, wc, path);
6156 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006157 if (!next)
6158 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006159 btrfs_tree_lock(next);
6160 btrfs_set_lock_blocking(next);
6161 }
6162
6163 level--;
6164 BUG_ON(level != btrfs_header_level(next));
6165 path->nodes[level] = next;
6166 path->slots[level] = 0;
6167 path->locks[level] = 1;
6168 wc->level = level;
6169 if (wc->level == 1)
6170 wc->reada_slot = 0;
6171 return 0;
6172skip:
6173 wc->refs[level - 1] = 0;
6174 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006175 if (wc->stage == DROP_REFERENCE) {
6176 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6177 parent = path->nodes[level]->start;
6178 } else {
6179 BUG_ON(root->root_key.objectid !=
6180 btrfs_header_owner(path->nodes[level]));
6181 parent = 0;
6182 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006183
Yan, Zheng94fcca92009-10-09 09:25:16 -04006184 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6185 root->root_key.objectid, level - 1, 0);
6186 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006187 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006188 btrfs_tree_unlock(next);
6189 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006190 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006191 return 1;
6192}
6193
6194/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006195 * hepler to process tree block while walking up the tree.
6196 *
6197 * when wc->stage == DROP_REFERENCE, this function drops
6198 * reference count on the block.
6199 *
6200 * when wc->stage == UPDATE_BACKREF, this function changes
6201 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6202 * to UPDATE_BACKREF previously while processing the block.
6203 *
6204 * NOTE: return value 1 means we should stop walking up.
6205 */
6206static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6207 struct btrfs_root *root,
6208 struct btrfs_path *path,
6209 struct walk_control *wc)
6210{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006211 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006212 int level = wc->level;
6213 struct extent_buffer *eb = path->nodes[level];
6214 u64 parent = 0;
6215
6216 if (wc->stage == UPDATE_BACKREF) {
6217 BUG_ON(wc->shared_level < level);
6218 if (level < wc->shared_level)
6219 goto out;
6220
Yan Zheng2c47e6052009-06-27 21:07:35 -04006221 ret = find_next_key(path, level + 1, &wc->update_progress);
6222 if (ret > 0)
6223 wc->update_ref = 0;
6224
6225 wc->stage = DROP_REFERENCE;
6226 wc->shared_level = -1;
6227 path->slots[level] = 0;
6228
6229 /*
6230 * check reference count again if the block isn't locked.
6231 * we should start walking down the tree again if reference
6232 * count is one.
6233 */
6234 if (!path->locks[level]) {
6235 BUG_ON(level == 0);
6236 btrfs_tree_lock(eb);
6237 btrfs_set_lock_blocking(eb);
6238 path->locks[level] = 1;
6239
6240 ret = btrfs_lookup_extent_info(trans, root,
6241 eb->start, eb->len,
6242 &wc->refs[level],
6243 &wc->flags[level]);
6244 BUG_ON(ret);
6245 BUG_ON(wc->refs[level] == 0);
6246 if (wc->refs[level] == 1) {
6247 btrfs_tree_unlock(eb);
6248 path->locks[level] = 0;
6249 return 1;
6250 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006251 }
6252 }
6253
6254 /* wc->stage == DROP_REFERENCE */
6255 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6256
6257 if (wc->refs[level] == 1) {
6258 if (level == 0) {
6259 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6260 ret = btrfs_dec_ref(trans, root, eb, 1);
6261 else
6262 ret = btrfs_dec_ref(trans, root, eb, 0);
6263 BUG_ON(ret);
6264 }
6265 /* make block locked assertion in clean_tree_block happy */
6266 if (!path->locks[level] &&
6267 btrfs_header_generation(eb) == trans->transid) {
6268 btrfs_tree_lock(eb);
6269 btrfs_set_lock_blocking(eb);
6270 path->locks[level] = 1;
6271 }
6272 clean_tree_block(trans, root, eb);
6273 }
6274
6275 if (eb == root->node) {
6276 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6277 parent = eb->start;
6278 else
6279 BUG_ON(root->root_key.objectid !=
6280 btrfs_header_owner(eb));
6281 } else {
6282 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6283 parent = path->nodes[level + 1]->start;
6284 else
6285 BUG_ON(root->root_key.objectid !=
6286 btrfs_header_owner(path->nodes[level + 1]));
6287 }
6288
Yan, Zhengf0486c62010-05-16 10:46:25 -04006289 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006290out:
6291 wc->refs[level] = 0;
6292 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006293 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006294}
6295
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006296static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6297 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006298 struct btrfs_path *path,
6299 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006300{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006301 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006302 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006303 int ret;
6304
Yan Zheng2c47e6052009-06-27 21:07:35 -04006305 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006306 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006307 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006308 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006309
Yan Zheng2c47e6052009-06-27 21:07:35 -04006310 if (level == 0)
6311 break;
6312
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006313 if (path->slots[level] >=
6314 btrfs_header_nritems(path->nodes[level]))
6315 break;
6316
Yan, Zheng94fcca92009-10-09 09:25:16 -04006317 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006318 if (ret > 0) {
6319 path->slots[level]++;
6320 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00006321 } else if (ret < 0)
6322 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006323 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006324 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006325 return 0;
6326}
6327
Chris Masond3977122009-01-05 21:25:51 -05006328static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006329 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006330 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006331 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006332{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006333 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006334 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006335
Yan Zheng2c47e6052009-06-27 21:07:35 -04006336 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6337 while (level < max_level && path->nodes[level]) {
6338 wc->level = level;
6339 if (path->slots[level] + 1 <
6340 btrfs_header_nritems(path->nodes[level])) {
6341 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006342 return 0;
6343 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006344 ret = walk_up_proc(trans, root, path, wc);
6345 if (ret > 0)
6346 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006347
Yan Zheng2c47e6052009-06-27 21:07:35 -04006348 if (path->locks[level]) {
6349 btrfs_tree_unlock(path->nodes[level]);
6350 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006351 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006352 free_extent_buffer(path->nodes[level]);
6353 path->nodes[level] = NULL;
6354 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006355 }
6356 }
6357 return 1;
6358}
6359
Chris Mason9aca1d52007-03-13 11:09:37 -04006360/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006361 * drop a subvolume tree.
6362 *
6363 * this function traverses the tree freeing any blocks that only
6364 * referenced by the tree.
6365 *
6366 * when a shared tree block is found. this function decreases its
6367 * reference count by one. if update_ref is true, this function
6368 * also make sure backrefs for the shared block and all lower level
6369 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006370 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006371int btrfs_drop_snapshot(struct btrfs_root *root,
6372 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006373{
Chris Mason5caf2a02007-04-02 11:20:42 -04006374 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006375 struct btrfs_trans_handle *trans;
6376 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006377 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006378 struct walk_control *wc;
6379 struct btrfs_key key;
6380 int err = 0;
6381 int ret;
6382 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006383
Chris Mason5caf2a02007-04-02 11:20:42 -04006384 path = btrfs_alloc_path();
6385 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006386
Yan Zheng2c47e6052009-06-27 21:07:35 -04006387 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6388 BUG_ON(!wc);
6389
Yan, Zhenga22285a2010-05-16 10:48:46 -04006390 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006391 BUG_ON(IS_ERR(trans));
6392
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006393 if (block_rsv)
6394 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006395
Chris Mason9f3a7422007-08-07 15:52:19 -04006396 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006397 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006398 path->nodes[level] = btrfs_lock_root_node(root);
6399 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006400 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006401 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006402 memset(&wc->update_progress, 0,
6403 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006404 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006405 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006406 memcpy(&wc->update_progress, &key,
6407 sizeof(wc->update_progress));
6408
Chris Mason6702ed42007-08-07 16:15:09 -04006409 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006410 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006411 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006412 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6413 path->lowest_level = 0;
6414 if (ret < 0) {
6415 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006416 goto out;
6417 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006418 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006419
Chris Mason7d9eb122008-07-08 14:19:17 -04006420 /*
6421 * unlock our path, this is safe because only this
6422 * function is allowed to delete this snapshot
6423 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006424 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006425
Yan Zheng2c47e6052009-06-27 21:07:35 -04006426 level = btrfs_header_level(root->node);
6427 while (1) {
6428 btrfs_tree_lock(path->nodes[level]);
6429 btrfs_set_lock_blocking(path->nodes[level]);
6430
6431 ret = btrfs_lookup_extent_info(trans, root,
6432 path->nodes[level]->start,
6433 path->nodes[level]->len,
6434 &wc->refs[level],
6435 &wc->flags[level]);
6436 BUG_ON(ret);
6437 BUG_ON(wc->refs[level] == 0);
6438
6439 if (level == root_item->drop_level)
6440 break;
6441
6442 btrfs_tree_unlock(path->nodes[level]);
6443 WARN_ON(wc->refs[level] != 1);
6444 level--;
6445 }
6446 }
6447
6448 wc->level = level;
6449 wc->shared_level = -1;
6450 wc->stage = DROP_REFERENCE;
6451 wc->update_ref = update_ref;
6452 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006453 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006454
6455 while (1) {
6456 ret = walk_down_tree(trans, root, path, wc);
6457 if (ret < 0) {
6458 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006459 break;
6460 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006461
6462 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6463 if (ret < 0) {
6464 err = ret;
6465 break;
6466 }
6467
6468 if (ret > 0) {
6469 BUG_ON(wc->stage != DROP_REFERENCE);
6470 break;
6471 }
6472
6473 if (wc->stage == DROP_REFERENCE) {
6474 level = wc->level;
6475 btrfs_node_key(path->nodes[level],
6476 &root_item->drop_progress,
6477 path->slots[level]);
6478 root_item->drop_level = level;
6479 }
6480
6481 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006482 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006483 ret = btrfs_update_root(trans, tree_root,
6484 &root->root_key,
6485 root_item);
6486 BUG_ON(ret);
6487
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006488 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006489 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006490 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006491 if (block_rsv)
6492 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006493 }
Chris Mason20524f02007-03-10 06:35:47 -05006494 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006495 btrfs_release_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006496 BUG_ON(err);
6497
6498 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6499 BUG_ON(ret);
6500
Yan, Zheng76dda932009-09-21 16:00:26 -04006501 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6502 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6503 NULL, NULL);
6504 BUG_ON(ret < 0);
6505 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006506 /* if we fail to delete the orphan item this time
6507 * around, it'll get picked up the next time.
6508 *
6509 * The most common failure here is just -ENOENT.
6510 */
6511 btrfs_del_orphan_item(trans, tree_root,
6512 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006513 }
6514 }
6515
6516 if (root->in_radix) {
6517 btrfs_free_fs_root(tree_root->fs_info, root);
6518 } else {
6519 free_extent_buffer(root->node);
6520 free_extent_buffer(root->commit_root);
6521 kfree(root);
6522 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006523out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006524 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006525 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006526 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006527 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006528}
Chris Mason9078a3e2007-04-26 16:46:15 -04006529
Yan Zheng2c47e6052009-06-27 21:07:35 -04006530/*
6531 * drop subtree rooted at tree block 'node'.
6532 *
6533 * NOTE: this function will unlock and release tree block 'node'
6534 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006535int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6536 struct btrfs_root *root,
6537 struct extent_buffer *node,
6538 struct extent_buffer *parent)
6539{
6540 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006541 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006542 int level;
6543 int parent_level;
6544 int ret = 0;
6545 int wret;
6546
Yan Zheng2c47e6052009-06-27 21:07:35 -04006547 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6548
Yan Zhengf82d02d2008-10-29 14:49:05 -04006549 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006550 if (!path)
6551 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006552
Yan Zheng2c47e6052009-06-27 21:07:35 -04006553 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006554 if (!wc) {
6555 btrfs_free_path(path);
6556 return -ENOMEM;
6557 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006558
Chris Masonb9447ef2009-03-09 11:45:38 -04006559 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006560 parent_level = btrfs_header_level(parent);
6561 extent_buffer_get(parent);
6562 path->nodes[parent_level] = parent;
6563 path->slots[parent_level] = btrfs_header_nritems(parent);
6564
Chris Masonb9447ef2009-03-09 11:45:38 -04006565 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006566 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006567 path->nodes[level] = node;
6568 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006569 path->locks[level] = 1;
6570
6571 wc->refs[parent_level] = 1;
6572 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6573 wc->level = level;
6574 wc->shared_level = -1;
6575 wc->stage = DROP_REFERENCE;
6576 wc->update_ref = 0;
6577 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006578 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006579
6580 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006581 wret = walk_down_tree(trans, root, path, wc);
6582 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006583 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006584 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006585 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006586
Yan Zheng2c47e6052009-06-27 21:07:35 -04006587 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006588 if (wret < 0)
6589 ret = wret;
6590 if (wret != 0)
6591 break;
6592 }
6593
Yan Zheng2c47e6052009-06-27 21:07:35 -04006594 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006595 btrfs_free_path(path);
6596 return ret;
6597}
6598
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006599#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04006600static unsigned long calc_ra(unsigned long start, unsigned long last,
6601 unsigned long nr)
6602{
6603 return min(last, start + nr - 1);
6604}
6605
Chris Masond3977122009-01-05 21:25:51 -05006606static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05006607 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05006608{
6609 u64 page_start;
6610 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04006611 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006612 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006613 unsigned long i;
6614 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05006615 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05006616 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04006617 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04006618 unsigned int total_read = 0;
6619 unsigned int total_dirty = 0;
6620 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05006621
6622 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00006623 if (!ra)
6624 return -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006625
6626 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006627 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05006628 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6629
Zheng Yan1a40e232008-09-26 10:09:34 -04006630 /* make sure the dirty trick played by the caller work */
6631 ret = invalidate_inode_pages2_range(inode->i_mapping,
6632 first_index, last_index);
6633 if (ret)
6634 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04006635
Chris Mason4313b392008-01-03 09:08:48 -05006636 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05006637
Zheng Yan1a40e232008-09-26 10:09:34 -04006638 for (i = first_index ; i <= last_index; i++) {
6639 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04006640 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04006641 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04006642 }
6643 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04006644again:
6645 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04006646 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05006647 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04006648 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006649 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006650 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04006651 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006652 if (!PageUptodate(page)) {
6653 btrfs_readpage(NULL, page);
6654 lock_page(page);
6655 if (!PageUptodate(page)) {
6656 unlock_page(page);
6657 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006658 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05006659 goto out_unlock;
6660 }
6661 }
Chris Masonec44a352008-04-28 15:29:52 -04006662 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04006663
Chris Masonedbd8d42007-12-21 16:27:24 -05006664 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6665 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006666 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006667
Chris Mason3eaa2882008-07-24 11:57:52 -04006668 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6669 if (ordered) {
6670 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6671 unlock_page(page);
6672 page_cache_release(page);
6673 btrfs_start_ordered_extent(inode, ordered, 1);
6674 btrfs_put_ordered_extent(ordered);
6675 goto again;
6676 }
6677 set_page_extent_mapped(page);
6678
Zheng Yan1a40e232008-09-26 10:09:34 -04006679 if (i == first_index)
6680 set_extent_bits(io_tree, page_start, page_end,
6681 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006682 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04006683
Chris Masona061fc82008-05-07 11:43:44 -04006684 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006685 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006686
Chris Masond1310b22008-01-24 16:13:08 -05006687 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006688 unlock_page(page);
6689 page_cache_release(page);
6690 }
6691
6692out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04006693 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05006694 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006695 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04006696 return ret;
6697}
6698
Chris Masond3977122009-01-05 21:25:51 -05006699static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006700 struct btrfs_key *extent_key,
6701 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05006702{
Zheng Yan1a40e232008-09-26 10:09:34 -04006703 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6704 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6705 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04006706 u64 start = extent_key->objectid - offset;
6707 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006708
David Sterba172ddd62011-04-21 00:48:27 +02006709 em = alloc_extent_map();
Tsutomu Itohc26a9202011-02-14 00:45:29 +00006710 BUG_ON(!em);
Zheng Yan1a40e232008-09-26 10:09:34 -04006711
Yan Zheng66435582008-10-30 14:19:50 -04006712 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006713 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04006714 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04006715 em->block_start = extent_key->objectid;
6716 em->bdev = root->fs_info->fs_devices->latest_bdev;
6717 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6718
6719 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04006720 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006721 while (1) {
6722 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04006723 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006724 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04006725 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006726 if (ret != -EEXIST) {
6727 free_extent_map(em);
6728 break;
6729 }
Yan Zheng66435582008-10-30 14:19:50 -04006730 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006731 }
Yan Zheng66435582008-10-30 14:19:50 -04006732 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006733
Yan Zheng66435582008-10-30 14:19:50 -04006734 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04006735}
6736
6737struct btrfs_ref_path {
6738 u64 extent_start;
6739 u64 nodes[BTRFS_MAX_LEVEL];
6740 u64 root_objectid;
6741 u64 root_generation;
6742 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006743 u32 num_refs;
6744 int lowest_level;
6745 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006746 int shared_level;
6747
6748 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6749 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04006750};
6751
6752struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04006753 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04006754 u64 disk_bytenr;
6755 u64 disk_num_bytes;
6756 u64 offset;
6757 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04006758 u8 compression;
6759 u8 encryption;
6760 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04006761};
6762
6763static int is_cowonly_root(u64 root_objectid)
6764{
6765 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6766 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6767 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6768 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05006769 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6770 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04006771 return 1;
6772 return 0;
6773}
6774
Chris Masond3977122009-01-05 21:25:51 -05006775static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006776 struct btrfs_root *extent_root,
6777 struct btrfs_ref_path *ref_path,
6778 int first_time)
6779{
6780 struct extent_buffer *leaf;
6781 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05006782 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05006783 struct btrfs_key key;
6784 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04006785 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05006786 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04006787 int level;
6788 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006789
Zheng Yan1a40e232008-09-26 10:09:34 -04006790 path = btrfs_alloc_path();
6791 if (!path)
6792 return -ENOMEM;
6793
Zheng Yan1a40e232008-09-26 10:09:34 -04006794 if (first_time) {
6795 ref_path->lowest_level = -1;
6796 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006797 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006798 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04006799 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006800walk_down:
6801 level = ref_path->current_level - 1;
6802 while (level >= -1) {
6803 u64 parent;
6804 if (level < ref_path->lowest_level)
6805 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006806
Chris Masond3977122009-01-05 21:25:51 -05006807 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006808 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006809 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006810 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006811 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006812
Zheng Yan1a40e232008-09-26 10:09:34 -04006813 parent = ref_path->nodes[level + 1];
6814 ref_path->nodes[level + 1] = 0;
6815 ref_path->current_level = level;
6816 BUG_ON(parent == 0);
6817
6818 key.objectid = bytenr;
6819 key.offset = parent + 1;
6820 key.type = BTRFS_EXTENT_REF_KEY;
6821
6822 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006823 if (ret < 0)
6824 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006825 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006826
Chris Masonedbd8d42007-12-21 16:27:24 -05006827 leaf = path->nodes[0];
6828 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04006829 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04006830 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04006831 if (ret < 0)
6832 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006833 if (ret > 0)
6834 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04006835 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04006836 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006837
6838 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04006839 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04006840 found_key.type == BTRFS_EXTENT_REF_KEY) {
6841 if (level < ref_path->shared_level)
6842 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006843 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006844 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006845next:
6846 level--;
6847 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006848 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006849 }
6850 /* reached lowest level */
6851 ret = 1;
6852 goto out;
6853walk_up:
6854 level = ref_path->current_level;
6855 while (level < BTRFS_MAX_LEVEL - 1) {
6856 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006857
6858 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006859 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006860 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006861 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006862
Zheng Yan1a40e232008-09-26 10:09:34 -04006863 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006864
Zheng Yan1a40e232008-09-26 10:09:34 -04006865 key.objectid = bytenr;
6866 key.offset = 0;
6867 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006868
Zheng Yan1a40e232008-09-26 10:09:34 -04006869 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6870 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006871 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006872
6873 leaf = path->nodes[0];
6874 nritems = btrfs_header_nritems(leaf);
6875 if (path->slots[0] >= nritems) {
6876 ret = btrfs_next_leaf(extent_root, path);
6877 if (ret < 0)
6878 goto out;
6879 if (ret > 0) {
6880 /* the extent was freed by someone */
6881 if (ref_path->lowest_level == level)
6882 goto out;
6883 btrfs_release_path(extent_root, path);
6884 goto walk_down;
6885 }
6886 leaf = path->nodes[0];
6887 }
6888
6889 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6890 if (found_key.objectid != bytenr ||
6891 found_key.type != BTRFS_EXTENT_REF_KEY) {
6892 /* the extent was freed by someone */
6893 if (ref_path->lowest_level == level) {
6894 ret = 1;
6895 goto out;
6896 }
6897 btrfs_release_path(extent_root, path);
6898 goto walk_down;
6899 }
6900found:
6901 ref = btrfs_item_ptr(leaf, path->slots[0],
6902 struct btrfs_extent_ref);
6903 ref_objectid = btrfs_ref_objectid(leaf, ref);
6904 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6905 if (first_time) {
6906 level = (int)ref_objectid;
6907 BUG_ON(level >= BTRFS_MAX_LEVEL);
6908 ref_path->lowest_level = level;
6909 ref_path->current_level = level;
6910 ref_path->nodes[level] = bytenr;
6911 } else {
6912 WARN_ON(ref_objectid != level);
6913 }
6914 } else {
6915 WARN_ON(level != -1);
6916 }
6917 first_time = 0;
6918
6919 if (ref_path->lowest_level == level) {
6920 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006921 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6922 }
6923
6924 /*
6925 * the block is tree root or the block isn't in reference
6926 * counted tree.
6927 */
6928 if (found_key.objectid == found_key.offset ||
6929 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6930 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6931 ref_path->root_generation =
6932 btrfs_ref_generation(leaf, ref);
6933 if (level < 0) {
6934 /* special reference from the tree log */
6935 ref_path->nodes[0] = found_key.offset;
6936 ref_path->current_level = 0;
6937 }
6938 ret = 0;
6939 goto out;
6940 }
6941
6942 level++;
6943 BUG_ON(ref_path->nodes[level] != 0);
6944 ref_path->nodes[level] = found_key.offset;
6945 ref_path->current_level = level;
6946
6947 /*
6948 * the reference was created in the running transaction,
6949 * no need to continue walking up.
6950 */
6951 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6952 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6953 ref_path->root_generation =
6954 btrfs_ref_generation(leaf, ref);
6955 ret = 0;
6956 goto out;
6957 }
6958
6959 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006960 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006961 }
6962 /* reached max tree level, but no tree root found. */
6963 BUG();
6964out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006965 btrfs_free_path(path);
6966 return ret;
6967}
6968
6969static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6970 struct btrfs_root *extent_root,
6971 struct btrfs_ref_path *ref_path,
6972 u64 extent_start)
6973{
6974 memset(ref_path, 0, sizeof(*ref_path));
6975 ref_path->extent_start = extent_start;
6976
6977 return __next_ref_path(trans, extent_root, ref_path, 1);
6978}
6979
6980static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6981 struct btrfs_root *extent_root,
6982 struct btrfs_ref_path *ref_path)
6983{
6984 return __next_ref_path(trans, extent_root, ref_path, 0);
6985}
6986
Chris Masond3977122009-01-05 21:25:51 -05006987static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006988 struct btrfs_key *extent_key,
6989 u64 offset, int no_fragment,
6990 struct disk_extent **extents,
6991 int *nr_extents)
6992{
6993 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6994 struct btrfs_path *path;
6995 struct btrfs_file_extent_item *fi;
6996 struct extent_buffer *leaf;
6997 struct disk_extent *exts = *extents;
6998 struct btrfs_key found_key;
6999 u64 cur_pos;
7000 u64 last_byte;
7001 u32 nritems;
7002 int nr = 0;
7003 int max = *nr_extents;
7004 int ret;
7005
7006 WARN_ON(!no_fragment && *extents);
7007 if (!exts) {
7008 max = 1;
7009 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
7010 if (!exts)
7011 return -ENOMEM;
7012 }
7013
7014 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007015 if (!path) {
7016 if (exts != *extents)
7017 kfree(exts);
7018 return -ENOMEM;
7019 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007020
7021 cur_pos = extent_key->objectid - offset;
7022 last_byte = extent_key->objectid + extent_key->offset;
7023 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
7024 cur_pos, 0);
7025 if (ret < 0)
7026 goto out;
7027 if (ret > 0) {
7028 ret = -ENOENT;
7029 goto out;
7030 }
7031
7032 while (1) {
7033 leaf = path->nodes[0];
7034 nritems = btrfs_header_nritems(leaf);
7035 if (path->slots[0] >= nritems) {
7036 ret = btrfs_next_leaf(root, path);
7037 if (ret < 0)
7038 goto out;
7039 if (ret > 0)
7040 break;
7041 leaf = path->nodes[0];
7042 }
7043
7044 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7045 if (found_key.offset != cur_pos ||
7046 found_key.type != BTRFS_EXTENT_DATA_KEY ||
7047 found_key.objectid != reloc_inode->i_ino)
7048 break;
7049
7050 fi = btrfs_item_ptr(leaf, path->slots[0],
7051 struct btrfs_file_extent_item);
7052 if (btrfs_file_extent_type(leaf, fi) !=
7053 BTRFS_FILE_EXTENT_REG ||
7054 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7055 break;
7056
7057 if (nr == max) {
7058 struct disk_extent *old = exts;
7059 max *= 2;
7060 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
Yoshinori Sanodac97e52011-02-15 12:01:42 +00007061 if (!exts) {
7062 ret = -ENOMEM;
7063 goto out;
7064 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007065 memcpy(exts, old, sizeof(*exts) * nr);
7066 if (old != *extents)
7067 kfree(old);
7068 }
7069
7070 exts[nr].disk_bytenr =
7071 btrfs_file_extent_disk_bytenr(leaf, fi);
7072 exts[nr].disk_num_bytes =
7073 btrfs_file_extent_disk_num_bytes(leaf, fi);
7074 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
7075 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04007076 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7077 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
7078 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
7079 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
7080 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04007081 BUG_ON(exts[nr].offset > 0);
7082 BUG_ON(exts[nr].compression || exts[nr].encryption);
7083 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007084
7085 cur_pos += exts[nr].num_bytes;
7086 nr++;
7087
7088 if (cur_pos + offset >= last_byte)
7089 break;
7090
7091 if (no_fragment) {
7092 ret = 1;
7093 goto out;
7094 }
7095 path->slots[0]++;
7096 }
7097
Yan Zheng1f80e4d2008-12-19 10:59:04 -05007098 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04007099 if (cur_pos + offset < last_byte) {
7100 ret = -ENOENT;
7101 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05007102 }
7103 ret = 0;
7104out:
Zheng Yan1a40e232008-09-26 10:09:34 -04007105 btrfs_free_path(path);
7106 if (ret) {
7107 if (exts != *extents)
7108 kfree(exts);
7109 } else {
7110 *extents = exts;
7111 *nr_extents = nr;
7112 }
7113 return ret;
7114}
7115
Chris Masond3977122009-01-05 21:25:51 -05007116static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007117 struct btrfs_root *root,
7118 struct btrfs_path *path,
7119 struct btrfs_key *extent_key,
7120 struct btrfs_key *leaf_key,
7121 struct btrfs_ref_path *ref_path,
7122 struct disk_extent *new_extents,
7123 int nr_extents)
7124{
7125 struct extent_buffer *leaf;
7126 struct btrfs_file_extent_item *fi;
7127 struct inode *inode = NULL;
7128 struct btrfs_key key;
7129 u64 lock_start = 0;
7130 u64 lock_end = 0;
7131 u64 num_bytes;
7132 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05007133 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007134 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007135 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007136 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04007137 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04007138 int ret;
7139
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007140 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04007141 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007142 if (key.objectid < ref_path->owner_objectid ||
7143 (key.objectid == ref_path->owner_objectid &&
7144 key.type < BTRFS_EXTENT_DATA_KEY)) {
7145 key.objectid = ref_path->owner_objectid;
7146 key.type = BTRFS_EXTENT_DATA_KEY;
7147 key.offset = 0;
7148 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007149 }
7150
7151 while (1) {
7152 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7153 if (ret < 0)
7154 goto out;
7155
7156 leaf = path->nodes[0];
7157 nritems = btrfs_header_nritems(leaf);
7158next:
7159 if (extent_locked && ret > 0) {
7160 /*
7161 * the file extent item was modified by someone
7162 * before the extent got locked.
7163 */
Zheng Yan1a40e232008-09-26 10:09:34 -04007164 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7165 lock_end, GFP_NOFS);
7166 extent_locked = 0;
7167 }
7168
7169 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007170 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04007171 break;
7172
7173 BUG_ON(extent_locked);
7174 ret = btrfs_next_leaf(root, path);
7175 if (ret < 0)
7176 goto out;
7177 if (ret > 0)
7178 break;
7179 leaf = path->nodes[0];
7180 nritems = btrfs_header_nritems(leaf);
7181 }
7182
7183 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7184
7185 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7186 if ((key.objectid > ref_path->owner_objectid) ||
7187 (key.objectid == ref_path->owner_objectid &&
7188 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05007189 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007190 break;
7191 }
7192
7193 if (inode && key.objectid != inode->i_ino) {
7194 BUG_ON(extent_locked);
7195 btrfs_release_path(root, path);
7196 mutex_unlock(&inode->i_mutex);
7197 iput(inode);
7198 inode = NULL;
7199 continue;
7200 }
7201
7202 if (key.type != BTRFS_EXTENT_DATA_KEY) {
7203 path->slots[0]++;
7204 ret = 1;
7205 goto next;
7206 }
7207 fi = btrfs_item_ptr(leaf, path->slots[0],
7208 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04007209 extent_type = btrfs_file_extent_type(leaf, fi);
7210 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
7211 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04007212 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
7213 extent_key->objectid)) {
7214 path->slots[0]++;
7215 ret = 1;
7216 goto next;
7217 }
7218
7219 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7220 ext_offset = btrfs_file_extent_offset(leaf, fi);
7221
Yan Zheng86288a12009-01-21 10:49:16 -05007222 if (search_end == (u64)-1) {
7223 search_end = key.offset - ext_offset +
7224 btrfs_file_extent_ram_bytes(leaf, fi);
7225 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007226
7227 if (!extent_locked) {
7228 lock_start = key.offset;
7229 lock_end = lock_start + num_bytes - 1;
7230 } else {
Yan Zheng66435582008-10-30 14:19:50 -04007231 if (lock_start > key.offset ||
7232 lock_end + 1 < key.offset + num_bytes) {
7233 unlock_extent(&BTRFS_I(inode)->io_tree,
7234 lock_start, lock_end, GFP_NOFS);
7235 extent_locked = 0;
7236 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007237 }
7238
7239 if (!inode) {
7240 btrfs_release_path(root, path);
7241
7242 inode = btrfs_iget_locked(root->fs_info->sb,
7243 key.objectid, root);
7244 if (inode->i_state & I_NEW) {
7245 BTRFS_I(inode)->root = root;
7246 BTRFS_I(inode)->location.objectid =
7247 key.objectid;
7248 BTRFS_I(inode)->location.type =
7249 BTRFS_INODE_ITEM_KEY;
7250 BTRFS_I(inode)->location.offset = 0;
7251 btrfs_read_locked_inode(inode);
7252 unlock_new_inode(inode);
7253 }
7254 /*
7255 * some code call btrfs_commit_transaction while
7256 * holding the i_mutex, so we can't use mutex_lock
7257 * here.
7258 */
7259 if (is_bad_inode(inode) ||
7260 !mutex_trylock(&inode->i_mutex)) {
7261 iput(inode);
7262 inode = NULL;
7263 key.offset = (u64)-1;
7264 goto skip;
7265 }
7266 }
7267
7268 if (!extent_locked) {
7269 struct btrfs_ordered_extent *ordered;
7270
7271 btrfs_release_path(root, path);
7272
7273 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7274 lock_end, GFP_NOFS);
7275 ordered = btrfs_lookup_first_ordered_extent(inode,
7276 lock_end);
7277 if (ordered &&
7278 ordered->file_offset <= lock_end &&
7279 ordered->file_offset + ordered->len > lock_start) {
7280 unlock_extent(&BTRFS_I(inode)->io_tree,
7281 lock_start, lock_end, GFP_NOFS);
7282 btrfs_start_ordered_extent(inode, ordered, 1);
7283 btrfs_put_ordered_extent(ordered);
7284 key.offset += num_bytes;
7285 goto skip;
7286 }
7287 if (ordered)
7288 btrfs_put_ordered_extent(ordered);
7289
Zheng Yan1a40e232008-09-26 10:09:34 -04007290 extent_locked = 1;
7291 continue;
7292 }
7293
7294 if (nr_extents == 1) {
7295 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04007296 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7297 new_extents[0].disk_bytenr);
7298 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7299 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007300 btrfs_mark_buffer_dirty(leaf);
7301
7302 btrfs_drop_extent_cache(inode, key.offset,
7303 key.offset + num_bytes - 1, 0);
7304
7305 ret = btrfs_inc_extent_ref(trans, root,
7306 new_extents[0].disk_bytenr,
7307 new_extents[0].disk_num_bytes,
7308 leaf->start,
7309 root->root_key.objectid,
7310 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007311 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007312 BUG_ON(ret);
7313
7314 ret = btrfs_free_extent(trans, root,
7315 extent_key->objectid,
7316 extent_key->offset,
7317 leaf->start,
7318 btrfs_header_owner(leaf),
7319 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007320 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007321 BUG_ON(ret);
7322
7323 btrfs_release_path(root, path);
7324 key.offset += num_bytes;
7325 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04007326 BUG_ON(1);
7327#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04007328 u64 alloc_hint;
7329 u64 extent_len;
7330 int i;
7331 /*
7332 * drop old extent pointer at first, then insert the
7333 * new pointers one bye one
7334 */
7335 btrfs_release_path(root, path);
7336 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7337 key.offset + num_bytes,
7338 key.offset, &alloc_hint);
7339 BUG_ON(ret);
7340
7341 for (i = 0; i < nr_extents; i++) {
7342 if (ext_offset >= new_extents[i].num_bytes) {
7343 ext_offset -= new_extents[i].num_bytes;
7344 continue;
7345 }
7346 extent_len = min(new_extents[i].num_bytes -
7347 ext_offset, num_bytes);
7348
7349 ret = btrfs_insert_empty_item(trans, root,
7350 path, &key,
7351 sizeof(*fi));
7352 BUG_ON(ret);
7353
7354 leaf = path->nodes[0];
7355 fi = btrfs_item_ptr(leaf, path->slots[0],
7356 struct btrfs_file_extent_item);
7357 btrfs_set_file_extent_generation(leaf, fi,
7358 trans->transid);
7359 btrfs_set_file_extent_type(leaf, fi,
7360 BTRFS_FILE_EXTENT_REG);
7361 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7362 new_extents[i].disk_bytenr);
7363 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7364 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04007365 btrfs_set_file_extent_ram_bytes(leaf, fi,
7366 new_extents[i].ram_bytes);
7367
7368 btrfs_set_file_extent_compression(leaf, fi,
7369 new_extents[i].compression);
7370 btrfs_set_file_extent_encryption(leaf, fi,
7371 new_extents[i].encryption);
7372 btrfs_set_file_extent_other_encoding(leaf, fi,
7373 new_extents[i].other_encoding);
7374
Zheng Yan1a40e232008-09-26 10:09:34 -04007375 btrfs_set_file_extent_num_bytes(leaf, fi,
7376 extent_len);
7377 ext_offset += new_extents[i].offset;
7378 btrfs_set_file_extent_offset(leaf, fi,
7379 ext_offset);
7380 btrfs_mark_buffer_dirty(leaf);
7381
7382 btrfs_drop_extent_cache(inode, key.offset,
7383 key.offset + extent_len - 1, 0);
7384
7385 ret = btrfs_inc_extent_ref(trans, root,
7386 new_extents[i].disk_bytenr,
7387 new_extents[i].disk_num_bytes,
7388 leaf->start,
7389 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007390 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007391 BUG_ON(ret);
7392 btrfs_release_path(root, path);
7393
Yan Zhenga76a3cd2008-10-09 11:46:29 -04007394 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04007395
7396 ext_offset = 0;
7397 num_bytes -= extent_len;
7398 key.offset += extent_len;
7399
7400 if (num_bytes == 0)
7401 break;
7402 }
7403 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007404#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04007405 }
7406
7407 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007408 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7409 lock_end, GFP_NOFS);
7410 extent_locked = 0;
7411 }
7412skip:
7413 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05007414 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007415 break;
7416
7417 cond_resched();
7418 }
7419 ret = 0;
7420out:
7421 btrfs_release_path(root, path);
7422 if (inode) {
7423 mutex_unlock(&inode->i_mutex);
7424 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007425 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7426 lock_end, GFP_NOFS);
7427 }
7428 iput(inode);
7429 }
7430 return ret;
7431}
7432
Zheng Yan1a40e232008-09-26 10:09:34 -04007433int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7434 struct btrfs_root *root,
7435 struct extent_buffer *buf, u64 orig_start)
7436{
7437 int level;
7438 int ret;
7439
7440 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7441 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7442
7443 level = btrfs_header_level(buf);
7444 if (level == 0) {
7445 struct btrfs_leaf_ref *ref;
7446 struct btrfs_leaf_ref *orig_ref;
7447
7448 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7449 if (!orig_ref)
7450 return -ENOENT;
7451
7452 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7453 if (!ref) {
7454 btrfs_free_leaf_ref(root, orig_ref);
7455 return -ENOMEM;
7456 }
7457
7458 ref->nritems = orig_ref->nritems;
7459 memcpy(ref->extents, orig_ref->extents,
7460 sizeof(ref->extents[0]) * ref->nritems);
7461
7462 btrfs_free_leaf_ref(root, orig_ref);
7463
7464 ref->root_gen = trans->transid;
7465 ref->bytenr = buf->start;
7466 ref->owner = btrfs_header_owner(buf);
7467 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05007468
Zheng Yan1a40e232008-09-26 10:09:34 -04007469 ret = btrfs_add_leaf_ref(root, ref, 0);
7470 WARN_ON(ret);
7471 btrfs_free_leaf_ref(root, ref);
7472 }
7473 return 0;
7474}
7475
Chris Masond3977122009-01-05 21:25:51 -05007476static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007477 struct extent_buffer *leaf,
7478 struct btrfs_block_group_cache *group,
7479 struct btrfs_root *target_root)
7480{
7481 struct btrfs_key key;
7482 struct inode *inode = NULL;
7483 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007484 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04007485 u64 num_bytes;
7486 u64 skip_objectid = 0;
7487 u32 nritems;
7488 u32 i;
7489
7490 nritems = btrfs_header_nritems(leaf);
7491 for (i = 0; i < nritems; i++) {
7492 btrfs_item_key_to_cpu(leaf, &key, i);
7493 if (key.objectid == skip_objectid ||
7494 key.type != BTRFS_EXTENT_DATA_KEY)
7495 continue;
7496 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7497 if (btrfs_file_extent_type(leaf, fi) ==
7498 BTRFS_FILE_EXTENT_INLINE)
7499 continue;
7500 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7501 continue;
7502 if (!inode || inode->i_ino != key.objectid) {
7503 iput(inode);
7504 inode = btrfs_ilookup(target_root->fs_info->sb,
7505 key.objectid, target_root, 1);
7506 }
7507 if (!inode) {
7508 skip_objectid = key.objectid;
7509 continue;
7510 }
7511 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7512
Josef Bacik2ac55d42010-02-03 19:33:23 +00007513 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7514 key.offset + num_bytes - 1, 0, &cached_state,
7515 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007516 btrfs_drop_extent_cache(inode, key.offset,
7517 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007518 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7519 key.offset + num_bytes - 1, &cached_state,
7520 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007521 cond_resched();
7522 }
7523 iput(inode);
7524 return 0;
7525}
7526
Chris Masond3977122009-01-05 21:25:51 -05007527static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007528 struct btrfs_root *root,
7529 struct extent_buffer *leaf,
7530 struct btrfs_block_group_cache *group,
7531 struct inode *reloc_inode)
7532{
7533 struct btrfs_key key;
7534 struct btrfs_key extent_key;
7535 struct btrfs_file_extent_item *fi;
7536 struct btrfs_leaf_ref *ref;
7537 struct disk_extent *new_extent;
7538 u64 bytenr;
7539 u64 num_bytes;
7540 u32 nritems;
7541 u32 i;
7542 int ext_index;
7543 int nr_extent;
7544 int ret;
7545
7546 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007547 if (!new_extent)
7548 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007549
7550 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7551 BUG_ON(!ref);
7552
7553 ext_index = -1;
7554 nritems = btrfs_header_nritems(leaf);
7555 for (i = 0; i < nritems; i++) {
7556 btrfs_item_key_to_cpu(leaf, &key, i);
7557 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7558 continue;
7559 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7560 if (btrfs_file_extent_type(leaf, fi) ==
7561 BTRFS_FILE_EXTENT_INLINE)
7562 continue;
7563 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7564 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7565 if (bytenr == 0)
7566 continue;
7567
7568 ext_index++;
7569 if (bytenr >= group->key.objectid + group->key.offset ||
7570 bytenr + num_bytes <= group->key.objectid)
7571 continue;
7572
7573 extent_key.objectid = bytenr;
7574 extent_key.offset = num_bytes;
7575 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7576 nr_extent = 1;
7577 ret = get_new_locations(reloc_inode, &extent_key,
7578 group->key.objectid, 1,
7579 &new_extent, &nr_extent);
7580 if (ret > 0)
7581 continue;
7582 BUG_ON(ret < 0);
7583
7584 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7585 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7586 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7587 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7588
Zheng Yan1a40e232008-09-26 10:09:34 -04007589 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7590 new_extent->disk_bytenr);
7591 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7592 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007593 btrfs_mark_buffer_dirty(leaf);
7594
7595 ret = btrfs_inc_extent_ref(trans, root,
7596 new_extent->disk_bytenr,
7597 new_extent->disk_num_bytes,
7598 leaf->start,
7599 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007600 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007601 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04007602
Zheng Yan1a40e232008-09-26 10:09:34 -04007603 ret = btrfs_free_extent(trans, root,
7604 bytenr, num_bytes, leaf->start,
7605 btrfs_header_owner(leaf),
7606 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007607 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007608 BUG_ON(ret);
7609 cond_resched();
7610 }
7611 kfree(new_extent);
7612 BUG_ON(ext_index + 1 != ref->nritems);
7613 btrfs_free_leaf_ref(root, ref);
7614 return 0;
7615}
7616
Yan Zhengf82d02d2008-10-29 14:49:05 -04007617int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7618 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04007619{
7620 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007621 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007622
7623 if (root->reloc_root) {
7624 reloc_root = root->reloc_root;
7625 root->reloc_root = NULL;
7626 list_add(&reloc_root->dead_list,
7627 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007628
7629 btrfs_set_root_bytenr(&reloc_root->root_item,
7630 reloc_root->node->start);
7631 btrfs_set_root_level(&root->root_item,
7632 btrfs_header_level(reloc_root->node));
7633 memset(&reloc_root->root_item.drop_progress, 0,
7634 sizeof(struct btrfs_disk_key));
7635 reloc_root->root_item.drop_level = 0;
7636
7637 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7638 &reloc_root->root_key,
7639 &reloc_root->root_item);
7640 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007641 }
7642 return 0;
7643}
7644
7645int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7646{
7647 struct btrfs_trans_handle *trans;
7648 struct btrfs_root *reloc_root;
7649 struct btrfs_root *prev_root = NULL;
7650 struct list_head dead_roots;
7651 int ret;
7652 unsigned long nr;
7653
7654 INIT_LIST_HEAD(&dead_roots);
7655 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7656
7657 while (!list_empty(&dead_roots)) {
7658 reloc_root = list_entry(dead_roots.prev,
7659 struct btrfs_root, dead_list);
7660 list_del_init(&reloc_root->dead_list);
7661
7662 BUG_ON(reloc_root->commit_root != NULL);
7663 while (1) {
7664 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007665 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007666
7667 mutex_lock(&root->fs_info->drop_mutex);
7668 ret = btrfs_drop_snapshot(trans, reloc_root);
7669 if (ret != -EAGAIN)
7670 break;
7671 mutex_unlock(&root->fs_info->drop_mutex);
7672
7673 nr = trans->blocks_used;
7674 ret = btrfs_end_transaction(trans, root);
7675 BUG_ON(ret);
7676 btrfs_btree_balance_dirty(root, nr);
7677 }
7678
7679 free_extent_buffer(reloc_root->node);
7680
7681 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7682 &reloc_root->root_key);
7683 BUG_ON(ret);
7684 mutex_unlock(&root->fs_info->drop_mutex);
7685
7686 nr = trans->blocks_used;
7687 ret = btrfs_end_transaction(trans, root);
7688 BUG_ON(ret);
7689 btrfs_btree_balance_dirty(root, nr);
7690
7691 kfree(prev_root);
7692 prev_root = reloc_root;
7693 }
7694 if (prev_root) {
7695 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7696 kfree(prev_root);
7697 }
7698 return 0;
7699}
7700
7701int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7702{
7703 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7704 return 0;
7705}
7706
7707int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7708{
7709 struct btrfs_root *reloc_root;
7710 struct btrfs_trans_handle *trans;
7711 struct btrfs_key location;
7712 int found;
7713 int ret;
7714
7715 mutex_lock(&root->fs_info->tree_reloc_mutex);
7716 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7717 BUG_ON(ret);
7718 found = !list_empty(&root->fs_info->dead_reloc_roots);
7719 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7720
7721 if (found) {
7722 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007723 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007724 ret = btrfs_commit_transaction(trans, root);
7725 BUG_ON(ret);
7726 }
7727
7728 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7729 location.offset = (u64)-1;
7730 location.type = BTRFS_ROOT_ITEM_KEY;
7731
7732 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7733 BUG_ON(!reloc_root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05007734 ret = btrfs_orphan_cleanup(reloc_root);
7735 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007736 return 0;
7737}
7738
Chris Masond3977122009-01-05 21:25:51 -05007739static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007740 struct btrfs_root *root)
7741{
7742 struct btrfs_root *reloc_root;
7743 struct extent_buffer *eb;
7744 struct btrfs_root_item *root_item;
7745 struct btrfs_key root_key;
7746 int ret;
7747
7748 BUG_ON(!root->ref_cows);
7749 if (root->reloc_root)
7750 return 0;
7751
7752 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007753 if (!root_item)
7754 return -ENOMEM;
Zheng Yan1a40e232008-09-26 10:09:34 -04007755
7756 ret = btrfs_copy_root(trans, root, root->commit_root,
7757 &eb, BTRFS_TREE_RELOC_OBJECTID);
7758 BUG_ON(ret);
7759
7760 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7761 root_key.offset = root->root_key.objectid;
7762 root_key.type = BTRFS_ROOT_ITEM_KEY;
7763
7764 memcpy(root_item, &root->root_item, sizeof(root_item));
7765 btrfs_set_root_refs(root_item, 0);
7766 btrfs_set_root_bytenr(root_item, eb->start);
7767 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04007768 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007769
7770 btrfs_tree_unlock(eb);
7771 free_extent_buffer(eb);
7772
7773 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7774 &root_key, root_item);
7775 BUG_ON(ret);
7776 kfree(root_item);
7777
7778 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7779 &root_key);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007780 BUG_ON(IS_ERR(reloc_root));
Zheng Yan1a40e232008-09-26 10:09:34 -04007781 reloc_root->last_trans = trans->transid;
7782 reloc_root->commit_root = NULL;
7783 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7784
7785 root->reloc_root = reloc_root;
7786 return 0;
7787}
7788
7789/*
7790 * Core function of space balance.
7791 *
7792 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04007793 * counted roots. There is one reloc tree for each subvol, and all
7794 * reloc trees share same root key objectid. Reloc trees are snapshots
7795 * of the latest committed roots of subvols (root->commit_root).
7796 *
7797 * To relocate a tree block referenced by a subvol, there are two steps.
7798 * COW the block through subvol's reloc tree, then update block pointer
7799 * in the subvol to point to the new block. Since all reloc trees share
7800 * same root key objectid, doing special handing for tree blocks owned
7801 * by them is easy. Once a tree block has been COWed in one reloc tree,
7802 * we can use the resulting new block directly when the same block is
7803 * required to COW again through other reloc trees. By this way, relocated
7804 * tree blocks are shared between reloc trees, so they are also shared
7805 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04007806 */
Chris Masond3977122009-01-05 21:25:51 -05007807static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007808 struct btrfs_root *root,
7809 struct btrfs_path *path,
7810 struct btrfs_key *first_key,
7811 struct btrfs_ref_path *ref_path,
7812 struct btrfs_block_group_cache *group,
7813 struct inode *reloc_inode)
7814{
7815 struct btrfs_root *reloc_root;
7816 struct extent_buffer *eb = NULL;
7817 struct btrfs_key *keys;
7818 u64 *nodes;
7819 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007820 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04007821 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007822 int ret;
7823
7824 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7825 lowest_level = ref_path->owner_objectid;
7826
Yan Zhengf82d02d2008-10-29 14:49:05 -04007827 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007828 path->lowest_level = lowest_level;
7829 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7830 BUG_ON(ret < 0);
7831 path->lowest_level = 0;
7832 btrfs_release_path(root, path);
7833 return 0;
7834 }
7835
Zheng Yan1a40e232008-09-26 10:09:34 -04007836 mutex_lock(&root->fs_info->tree_reloc_mutex);
7837 ret = init_reloc_tree(trans, root);
7838 BUG_ON(ret);
7839 reloc_root = root->reloc_root;
7840
Yan Zhengf82d02d2008-10-29 14:49:05 -04007841 shared_level = ref_path->shared_level;
7842 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007843
Yan Zhengf82d02d2008-10-29 14:49:05 -04007844 keys = ref_path->node_keys;
7845 nodes = ref_path->new_nodes;
7846 memset(&keys[shared_level + 1], 0,
7847 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7848 memset(&nodes[shared_level + 1], 0,
7849 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04007850
Yan Zhengf82d02d2008-10-29 14:49:05 -04007851 if (nodes[lowest_level] == 0) {
7852 path->lowest_level = lowest_level;
7853 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7854 0, 1);
7855 BUG_ON(ret);
7856 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7857 eb = path->nodes[level];
7858 if (!eb || eb == reloc_root->node)
7859 break;
7860 nodes[level] = eb->start;
7861 if (level == 0)
7862 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7863 else
7864 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7865 }
Yan Zheng2b820322008-11-17 21:11:30 -05007866 if (nodes[0] &&
7867 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007868 eb = path->nodes[0];
7869 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7870 group, reloc_inode);
7871 BUG_ON(ret);
7872 }
7873 btrfs_release_path(reloc_root, path);
7874 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007875 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007876 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007877 BUG_ON(ret);
7878 }
7879
Zheng Yan1a40e232008-09-26 10:09:34 -04007880 /*
7881 * replace tree blocks in the fs tree with tree blocks in
7882 * the reloc tree.
7883 */
7884 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7885 BUG_ON(ret < 0);
7886
7887 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007888 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7889 0, 0);
7890 BUG_ON(ret);
7891 extent_buffer_get(path->nodes[0]);
7892 eb = path->nodes[0];
7893 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007894 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7895 BUG_ON(ret);
7896 free_extent_buffer(eb);
7897 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007898
Yan Zhengf82d02d2008-10-29 14:49:05 -04007899 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007900 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007901 return 0;
7902}
7903
Chris Masond3977122009-01-05 21:25:51 -05007904static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007905 struct btrfs_root *root,
7906 struct btrfs_path *path,
7907 struct btrfs_key *first_key,
7908 struct btrfs_ref_path *ref_path)
7909{
7910 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007911
7912 ret = relocate_one_path(trans, root, path, first_key,
7913 ref_path, NULL, NULL);
7914 BUG_ON(ret);
7915
Zheng Yan1a40e232008-09-26 10:09:34 -04007916 return 0;
7917}
7918
Chris Masond3977122009-01-05 21:25:51 -05007919static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007920 struct btrfs_root *extent_root,
7921 struct btrfs_path *path,
7922 struct btrfs_key *extent_key)
7923{
7924 int ret;
7925
Zheng Yan1a40e232008-09-26 10:09:34 -04007926 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7927 if (ret)
7928 goto out;
7929 ret = btrfs_del_item(trans, extent_root, path);
7930out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007931 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007932 return ret;
7933}
7934
Chris Masond3977122009-01-05 21:25:51 -05007935static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007936 struct btrfs_ref_path *ref_path)
7937{
7938 struct btrfs_key root_key;
7939
7940 root_key.objectid = ref_path->root_objectid;
7941 root_key.type = BTRFS_ROOT_ITEM_KEY;
7942 if (is_cowonly_root(ref_path->root_objectid))
7943 root_key.offset = 0;
7944 else
7945 root_key.offset = (u64)-1;
7946
7947 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7948}
7949
Chris Masond3977122009-01-05 21:25:51 -05007950static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007951 struct btrfs_path *path,
7952 struct btrfs_key *extent_key,
7953 struct btrfs_block_group_cache *group,
7954 struct inode *reloc_inode, int pass)
7955{
7956 struct btrfs_trans_handle *trans;
7957 struct btrfs_root *found_root;
7958 struct btrfs_ref_path *ref_path = NULL;
7959 struct disk_extent *new_extents = NULL;
7960 int nr_extents = 0;
7961 int loops;
7962 int ret;
7963 int level;
7964 struct btrfs_key first_key;
7965 u64 prev_block = 0;
7966
Zheng Yan1a40e232008-09-26 10:09:34 -04007967
7968 trans = btrfs_start_transaction(extent_root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007969 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007970
7971 if (extent_key->objectid == 0) {
7972 ret = del_extent_zero(trans, extent_root, path, extent_key);
7973 goto out;
7974 }
7975
7976 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7977 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007978 ret = -ENOMEM;
7979 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007980 }
7981
7982 for (loops = 0; ; loops++) {
7983 if (loops == 0) {
7984 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7985 extent_key->objectid);
7986 } else {
7987 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7988 }
7989 if (ret < 0)
7990 goto out;
7991 if (ret > 0)
7992 break;
7993
7994 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7995 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7996 continue;
7997
7998 found_root = read_ref_root(extent_root->fs_info, ref_path);
7999 BUG_ON(!found_root);
8000 /*
8001 * for reference counted tree, only process reference paths
8002 * rooted at the latest committed root.
8003 */
8004 if (found_root->ref_cows &&
8005 ref_path->root_generation != found_root->root_key.offset)
8006 continue;
8007
8008 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8009 if (pass == 0) {
8010 /*
8011 * copy data extents to new locations
8012 */
8013 u64 group_start = group->key.objectid;
8014 ret = relocate_data_extent(reloc_inode,
8015 extent_key,
8016 group_start);
8017 if (ret < 0)
8018 goto out;
8019 break;
8020 }
8021 level = 0;
8022 } else {
8023 level = ref_path->owner_objectid;
8024 }
8025
8026 if (prev_block != ref_path->nodes[level]) {
8027 struct extent_buffer *eb;
8028 u64 block_start = ref_path->nodes[level];
8029 u64 block_size = btrfs_level_size(found_root, level);
8030
8031 eb = read_tree_block(found_root, block_start,
8032 block_size, 0);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008033 if (!eb) {
8034 ret = -EIO;
8035 goto out;
8036 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008037 btrfs_tree_lock(eb);
8038 BUG_ON(level != btrfs_header_level(eb));
8039
8040 if (level == 0)
8041 btrfs_item_key_to_cpu(eb, &first_key, 0);
8042 else
8043 btrfs_node_key_to_cpu(eb, &first_key, 0);
8044
8045 btrfs_tree_unlock(eb);
8046 free_extent_buffer(eb);
8047 prev_block = block_start;
8048 }
8049
Yan Zheng24562422009-02-12 14:14:53 -05008050 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05008051 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05008052 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05008053 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8054 /*
8055 * try to update data extent references while
8056 * keeping metadata shared between snapshots.
8057 */
8058 if (pass == 1) {
8059 ret = relocate_one_path(trans, found_root,
8060 path, &first_key, ref_path,
8061 group, reloc_inode);
8062 if (ret < 0)
8063 goto out;
8064 continue;
8065 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008066 /*
8067 * use fallback method to process the remaining
8068 * references.
8069 */
8070 if (!new_extents) {
8071 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04008072 new_extents = kmalloc(sizeof(*new_extents),
8073 GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04008074 if (!new_extents) {
8075 ret = -ENOMEM;
8076 goto out;
8077 }
Yan Zhengd899e052008-10-30 14:25:28 -04008078 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008079 ret = get_new_locations(reloc_inode,
8080 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04008081 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04008082 &new_extents,
8083 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04008084 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04008085 goto out;
8086 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008087 ret = replace_one_extent(trans, found_root,
8088 path, extent_key,
8089 &first_key, ref_path,
8090 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05008091 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04008092 ret = relocate_tree_block(trans, found_root, path,
8093 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04008094 }
8095 if (ret < 0)
8096 goto out;
8097 }
8098 ret = 0;
8099out:
8100 btrfs_end_transaction(trans, extent_root);
8101 kfree(new_extents);
8102 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05008103 return ret;
8104}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008105#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05008106
Chris Masonec44a352008-04-28 15:29:52 -04008107static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8108{
8109 u64 num_devices;
8110 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
8111 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8112
Chris Masoncd02dca2010-12-13 14:56:23 -05008113 /*
8114 * we add in the count of missing devices because we want
8115 * to make sure that any RAID levels on a degraded FS
8116 * continue to be honored.
8117 */
8118 num_devices = root->fs_info->fs_devices->rw_devices +
8119 root->fs_info->fs_devices->missing_devices;
8120
Chris Masonec44a352008-04-28 15:29:52 -04008121 if (num_devices == 1) {
8122 stripped |= BTRFS_BLOCK_GROUP_DUP;
8123 stripped = flags & ~stripped;
8124
8125 /* turn raid0 into single device chunks */
8126 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8127 return stripped;
8128
8129 /* turn mirroring into duplication */
8130 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8131 BTRFS_BLOCK_GROUP_RAID10))
8132 return stripped | BTRFS_BLOCK_GROUP_DUP;
8133 return flags;
8134 } else {
8135 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04008136 if (flags & stripped)
8137 return flags;
8138
8139 stripped |= BTRFS_BLOCK_GROUP_DUP;
8140 stripped = flags & ~stripped;
8141
8142 /* switch duplicated blocks with raid1 */
8143 if (flags & BTRFS_BLOCK_GROUP_DUP)
8144 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8145
8146 /* turn single device chunks into raid0 */
8147 return stripped | BTRFS_BLOCK_GROUP_RAID0;
8148 }
8149 return flags;
8150}
8151
Yan, Zhengf0486c62010-05-16 10:46:25 -04008152static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04008153{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008154 struct btrfs_space_info *sinfo = cache->space_info;
8155 u64 num_bytes;
8156 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04008157
Yan, Zhengf0486c62010-05-16 10:46:25 -04008158 if (cache->ro)
8159 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04008160
Yan, Zhengf0486c62010-05-16 10:46:25 -04008161 spin_lock(&sinfo->lock);
8162 spin_lock(&cache->lock);
8163 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8164 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04008165
Yan, Zhengf0486c62010-05-16 10:46:25 -04008166 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
8167 sinfo->bytes_may_use + sinfo->bytes_readonly +
Chris Mason65e53412010-12-24 06:41:52 -05008168 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04008169 sinfo->bytes_readonly += num_bytes;
8170 sinfo->bytes_reserved += cache->reserved_pinned;
8171 cache->reserved_pinned = 0;
8172 cache->ro = 1;
8173 ret = 0;
8174 }
Chris Mason65e53412010-12-24 06:41:52 -05008175
Yan, Zhengf0486c62010-05-16 10:46:25 -04008176 spin_unlock(&cache->lock);
8177 spin_unlock(&sinfo->lock);
8178 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04008179}
8180
Yan, Zhengf0486c62010-05-16 10:46:25 -04008181int btrfs_set_block_group_ro(struct btrfs_root *root,
8182 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008183
8184{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008185 struct btrfs_trans_handle *trans;
8186 u64 alloc_flags;
8187 int ret;
8188
8189 BUG_ON(cache->ro);
8190
8191 trans = btrfs_join_transaction(root, 1);
8192 BUG_ON(IS_ERR(trans));
8193
8194 alloc_flags = update_block_group_flags(root, cache->flags);
8195 if (alloc_flags != cache->flags)
Chris Mason0e4f8f82011-04-15 16:05:44 -04008196 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8197 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008198
8199 ret = set_block_group_ro(cache);
8200 if (!ret)
8201 goto out;
8202 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Chris Mason0e4f8f82011-04-15 16:05:44 -04008203 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8204 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008205 if (ret < 0)
8206 goto out;
8207 ret = set_block_group_ro(cache);
8208out:
8209 btrfs_end_transaction(trans, root);
8210 return ret;
8211}
8212
Chris Masonc87f08c2011-02-16 13:57:04 -05008213int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8214 struct btrfs_root *root, u64 type)
8215{
8216 u64 alloc_flags = get_alloc_profile(root, type);
Chris Mason0e4f8f82011-04-15 16:05:44 -04008217 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8218 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05008219}
8220
Miao Xie6d07bce2011-01-05 10:07:31 +00008221/*
8222 * helper to account the unused space of all the readonly block group in the
8223 * list. takes mirrors into account.
8224 */
8225static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8226{
8227 struct btrfs_block_group_cache *block_group;
8228 u64 free_bytes = 0;
8229 int factor;
8230
8231 list_for_each_entry(block_group, groups_list, list) {
8232 spin_lock(&block_group->lock);
8233
8234 if (!block_group->ro) {
8235 spin_unlock(&block_group->lock);
8236 continue;
8237 }
8238
8239 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8240 BTRFS_BLOCK_GROUP_RAID10 |
8241 BTRFS_BLOCK_GROUP_DUP))
8242 factor = 2;
8243 else
8244 factor = 1;
8245
8246 free_bytes += (block_group->key.offset -
8247 btrfs_block_group_used(&block_group->item)) *
8248 factor;
8249
8250 spin_unlock(&block_group->lock);
8251 }
8252
8253 return free_bytes;
8254}
8255
8256/*
8257 * helper to account the unused space of all the readonly block group in the
8258 * space_info. takes mirrors into account.
8259 */
8260u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8261{
8262 int i;
8263 u64 free_bytes = 0;
8264
8265 spin_lock(&sinfo->lock);
8266
8267 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
8268 if (!list_empty(&sinfo->block_groups[i]))
8269 free_bytes += __btrfs_get_ro_block_group_free_space(
8270 &sinfo->block_groups[i]);
8271
8272 spin_unlock(&sinfo->lock);
8273
8274 return free_bytes;
8275}
8276
Yan, Zhengf0486c62010-05-16 10:46:25 -04008277int btrfs_set_block_group_rw(struct btrfs_root *root,
8278 struct btrfs_block_group_cache *cache)
8279{
8280 struct btrfs_space_info *sinfo = cache->space_info;
8281 u64 num_bytes;
8282
8283 BUG_ON(!cache->ro);
8284
8285 spin_lock(&sinfo->lock);
8286 spin_lock(&cache->lock);
8287 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8288 cache->bytes_super - btrfs_block_group_used(&cache->item);
8289 sinfo->bytes_readonly -= num_bytes;
8290 cache->ro = 0;
8291 spin_unlock(&cache->lock);
8292 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008293 return 0;
8294}
8295
Josef Bacikba1bf482009-09-11 16:11:19 -04008296/*
8297 * checks to see if its even possible to relocate this block group.
8298 *
8299 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8300 * ok to go ahead and try.
8301 */
8302int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008303{
Zheng Yan1a40e232008-09-26 10:09:34 -04008304 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008305 struct btrfs_space_info *space_info;
8306 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8307 struct btrfs_device *device;
8308 int full = 0;
8309 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008310
Josef Bacikba1bf482009-09-11 16:11:19 -04008311 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008312
Josef Bacikba1bf482009-09-11 16:11:19 -04008313 /* odd, couldn't find the block group, leave it alone */
8314 if (!block_group)
8315 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008316
Josef Bacikba1bf482009-09-11 16:11:19 -04008317 /* no bytes used, we're good */
8318 if (!btrfs_block_group_used(&block_group->item))
8319 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008320
Josef Bacikba1bf482009-09-11 16:11:19 -04008321 space_info = block_group->space_info;
8322 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008323
Josef Bacikba1bf482009-09-11 16:11:19 -04008324 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008325
Josef Bacikba1bf482009-09-11 16:11:19 -04008326 /*
8327 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008328 * relocate it unless we're able to allocate a new chunk below.
8329 *
8330 * Otherwise, we need to make sure we have room in the space to handle
8331 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008332 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008333 if ((space_info->total_bytes != block_group->key.offset) &&
8334 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04008335 space_info->bytes_pinned + space_info->bytes_readonly +
8336 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04008337 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008338 spin_unlock(&space_info->lock);
8339 goto out;
8340 }
8341 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008342
Josef Bacikba1bf482009-09-11 16:11:19 -04008343 /*
8344 * ok we don't have enough space, but maybe we have free space on our
8345 * devices to allocate new chunks for relocation, so loop through our
8346 * alloc devices and guess if we have enough space. However, if we
8347 * were marked as full, then we know there aren't enough chunks, and we
8348 * can just return.
8349 */
8350 ret = -1;
8351 if (full)
8352 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05008353
Josef Bacikba1bf482009-09-11 16:11:19 -04008354 mutex_lock(&root->fs_info->chunk_mutex);
8355 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8356 u64 min_free = btrfs_block_group_used(&block_group->item);
Miao Xie7bfc8372011-01-05 10:07:26 +00008357 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008358
Josef Bacikba1bf482009-09-11 16:11:19 -04008359 /*
8360 * check to make sure we can actually find a chunk with enough
8361 * space to fit our block group in.
8362 */
8363 if (device->total_bytes > device->bytes_used + min_free) {
8364 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008365 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008366 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05008367 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04008368 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008369 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008370 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008371 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05008372out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008373 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008374 return ret;
8375}
8376
Christoph Hellwigb2950862008-12-02 09:54:17 -05008377static int find_first_block_group(struct btrfs_root *root,
8378 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008379{
Chris Mason925baed2008-06-25 16:01:30 -04008380 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008381 struct btrfs_key found_key;
8382 struct extent_buffer *leaf;
8383 int slot;
8384
8385 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8386 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008387 goto out;
8388
Chris Masond3977122009-01-05 21:25:51 -05008389 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008390 slot = path->slots[0];
8391 leaf = path->nodes[0];
8392 if (slot >= btrfs_header_nritems(leaf)) {
8393 ret = btrfs_next_leaf(root, path);
8394 if (ret == 0)
8395 continue;
8396 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008397 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008398 break;
8399 }
8400 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8401
8402 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008403 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8404 ret = 0;
8405 goto out;
8406 }
Chris Mason0b86a832008-03-24 15:01:56 -04008407 path->slots[0]++;
8408 }
Chris Mason925baed2008-06-25 16:01:30 -04008409out:
Chris Mason0b86a832008-03-24 15:01:56 -04008410 return ret;
8411}
8412
Josef Bacik0af3d002010-06-21 14:48:16 -04008413void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8414{
8415 struct btrfs_block_group_cache *block_group;
8416 u64 last = 0;
8417
8418 while (1) {
8419 struct inode *inode;
8420
8421 block_group = btrfs_lookup_first_block_group(info, last);
8422 while (block_group) {
8423 spin_lock(&block_group->lock);
8424 if (block_group->iref)
8425 break;
8426 spin_unlock(&block_group->lock);
8427 block_group = next_block_group(info->tree_root,
8428 block_group);
8429 }
8430 if (!block_group) {
8431 if (last == 0)
8432 break;
8433 last = 0;
8434 continue;
8435 }
8436
8437 inode = block_group->inode;
8438 block_group->iref = 0;
8439 block_group->inode = NULL;
8440 spin_unlock(&block_group->lock);
8441 iput(inode);
8442 last = block_group->key.objectid + block_group->key.offset;
8443 btrfs_put_block_group(block_group);
8444 }
8445}
8446
Zheng Yan1a40e232008-09-26 10:09:34 -04008447int btrfs_free_block_groups(struct btrfs_fs_info *info)
8448{
8449 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008450 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008451 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008452 struct rb_node *n;
8453
Yan Zheng11833d62009-09-11 16:11:19 -04008454 down_write(&info->extent_commit_sem);
8455 while (!list_empty(&info->caching_block_groups)) {
8456 caching_ctl = list_entry(info->caching_block_groups.next,
8457 struct btrfs_caching_control, list);
8458 list_del(&caching_ctl->list);
8459 put_caching_control(caching_ctl);
8460 }
8461 up_write(&info->extent_commit_sem);
8462
Zheng Yan1a40e232008-09-26 10:09:34 -04008463 spin_lock(&info->block_group_cache_lock);
8464 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8465 block_group = rb_entry(n, struct btrfs_block_group_cache,
8466 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008467 rb_erase(&block_group->cache_node,
8468 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008469 spin_unlock(&info->block_group_cache_lock);
8470
Josef Bacik80eb2342008-10-29 14:49:05 -04008471 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008472 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008473 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008474
Josef Bacik817d52f2009-07-13 21:29:25 -04008475 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008476 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008477
Josef Bacik3c148742011-02-02 15:53:47 +00008478 /*
8479 * We haven't cached this block group, which means we could
8480 * possibly have excluded extents on this block group.
8481 */
8482 if (block_group->cached == BTRFS_CACHE_NO)
8483 free_excluded_extents(info->extent_root, block_group);
8484
Josef Bacik817d52f2009-07-13 21:29:25 -04008485 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008486 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008487
8488 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008489 }
8490 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008491
8492 /* now that all the block groups are freed, go through and
8493 * free all the space_info structs. This is only called during
8494 * the final stages of unmount, and so we know nobody is
8495 * using them. We call synchronize_rcu() once before we start,
8496 * just to be on the safe side.
8497 */
8498 synchronize_rcu();
8499
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008500 release_global_block_rsv(info);
8501
Chris Mason4184ea72009-03-10 12:39:20 -04008502 while(!list_empty(&info->space_info)) {
8503 space_info = list_entry(info->space_info.next,
8504 struct btrfs_space_info,
8505 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008506 if (space_info->bytes_pinned > 0 ||
8507 space_info->bytes_reserved > 0) {
8508 WARN_ON(1);
8509 dump_space_info(space_info, 0, 0);
8510 }
Chris Mason4184ea72009-03-10 12:39:20 -04008511 list_del(&space_info->list);
8512 kfree(space_info);
8513 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008514 return 0;
8515}
8516
Yan, Zhengb742bb82010-05-16 10:46:24 -04008517static void __link_block_group(struct btrfs_space_info *space_info,
8518 struct btrfs_block_group_cache *cache)
8519{
8520 int index = get_block_group_index(cache);
8521
8522 down_write(&space_info->groups_sem);
8523 list_add_tail(&cache->list, &space_info->block_groups[index]);
8524 up_write(&space_info->groups_sem);
8525}
8526
Chris Mason9078a3e2007-04-26 16:46:15 -04008527int btrfs_read_block_groups(struct btrfs_root *root)
8528{
8529 struct btrfs_path *path;
8530 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008531 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008532 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008533 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008534 struct btrfs_key key;
8535 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008536 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008537 int need_clear = 0;
8538 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008539
Chris Masonbe744172007-05-06 10:15:01 -04008540 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008541 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008542 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008543 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008544 path = btrfs_alloc_path();
8545 if (!path)
8546 return -ENOMEM;
8547
Josef Bacik0af3d002010-06-21 14:48:16 -04008548 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8549 if (cache_gen != 0 &&
8550 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8551 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008552 if (btrfs_test_opt(root, CLEAR_CACHE))
8553 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04008554 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
8555 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04008556
Chris Masond3977122009-01-05 21:25:51 -05008557 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008558 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008559 if (ret > 0)
8560 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008561 if (ret != 0)
8562 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008563 leaf = path->nodes[0];
8564 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008565 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008566 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008567 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008568 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008569 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008570
Yan Zhengd2fb3432008-12-11 16:30:39 -05008571 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008572 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008573 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008574 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008575 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008576 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008577
Josef Bacik0af3d002010-06-21 14:48:16 -04008578 if (need_clear)
8579 cache->disk_cache_state = BTRFS_DC_CLEAR;
8580
Josef Bacik96303082009-07-13 21:29:25 -04008581 /*
8582 * we only want to have 32k of ram per block group for keeping
8583 * track of free space, and if we pass 1/2 of that we want to
8584 * start converting things over to using bitmaps
8585 */
8586 cache->extents_thresh = ((1024 * 32) / 2) /
8587 sizeof(struct btrfs_free_space);
8588
Chris Mason5f39d392007-10-15 16:14:19 -04008589 read_extent_buffer(leaf, &cache->item,
8590 btrfs_item_ptr_offset(leaf, path->slots[0]),
8591 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008592 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008593
Chris Mason9078a3e2007-04-26 16:46:15 -04008594 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02008595 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008596 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008597 cache->sectorsize = root->sectorsize;
8598
Josef Bacik817d52f2009-07-13 21:29:25 -04008599 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008600 * We need to exclude the super stripes now so that the space
8601 * info has super bytes accounted for, otherwise we'll think
8602 * we have more space than we actually do.
8603 */
8604 exclude_super_stripes(root, cache);
8605
8606 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008607 * check for two cases, either we are full, and therefore
8608 * don't need to bother with the caching work since we won't
8609 * find any space, or we are empty, and we can just add all
8610 * the space in and be done with it. This saves us _alot_ of
8611 * time, particularly in the full case.
8612 */
8613 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008614 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008615 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008616 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008617 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008618 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008619 cache->cached = BTRFS_CACHE_FINISHED;
8620 add_new_free_space(cache, root->fs_info,
8621 found_key.objectid,
8622 found_key.objectid +
8623 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008624 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008625 }
Chris Mason96b51792007-10-15 16:15:19 -04008626
Chris Mason6324fbf2008-03-24 15:01:59 -04008627 ret = update_space_info(info, cache->flags, found_key.offset,
8628 btrfs_block_group_used(&cache->item),
8629 &space_info);
8630 BUG_ON(ret);
8631 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008632 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008633 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008634 spin_unlock(&cache->space_info->lock);
8635
Yan, Zhengb742bb82010-05-16 10:46:24 -04008636 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008637
Josef Bacik0f9dd462008-09-23 13:14:11 -04008638 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8639 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04008640
8641 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008642 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04008643 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04008644 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008645
8646 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8647 if (!(get_alloc_profile(root, space_info->flags) &
8648 (BTRFS_BLOCK_GROUP_RAID10 |
8649 BTRFS_BLOCK_GROUP_RAID1 |
8650 BTRFS_BLOCK_GROUP_DUP)))
8651 continue;
8652 /*
8653 * avoid allocating from un-mirrored block group if there are
8654 * mirrored block groups.
8655 */
8656 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008657 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008658 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008659 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008660 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008661
8662 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008663 ret = 0;
8664error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008665 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008666 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008667}
Chris Mason6324fbf2008-03-24 15:01:59 -04008668
8669int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8670 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008671 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008672 u64 size)
8673{
8674 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008675 struct btrfs_root *extent_root;
8676 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008677
8678 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008679
Chris Mason12fcfd22009-03-24 10:24:20 -04008680 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008681
Chris Mason8f18cf12008-04-25 16:53:30 -04008682 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008683 if (!cache)
8684 return -ENOMEM;
8685
Chris Masone17cade2008-04-15 15:41:47 -04008686 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008687 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008688 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008689 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008690 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04008691
8692 /*
8693 * we only want to have 32k of ram per block group for keeping track
8694 * of free space, and if we pass 1/2 of that we want to start
8695 * converting things over to using bitmaps
8696 */
8697 cache->extents_thresh = ((1024 * 32) / 2) /
8698 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008699 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008700 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008701 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008702 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008703 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008704
Chris Mason6324fbf2008-03-24 15:01:59 -04008705 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008706 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8707 cache->flags = type;
8708 btrfs_set_block_group_flags(&cache->item, type);
8709
Yan Zheng11833d62009-09-11 16:11:19 -04008710 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008711 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04008712 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04008713
Josef Bacik817d52f2009-07-13 21:29:25 -04008714 add_new_free_space(cache, root->fs_info, chunk_offset,
8715 chunk_offset + size);
8716
Yan Zheng11833d62009-09-11 16:11:19 -04008717 free_excluded_extents(root, cache);
8718
Chris Mason6324fbf2008-03-24 15:01:59 -04008719 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8720 &cache->space_info);
8721 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04008722
8723 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008724 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008725 spin_unlock(&cache->space_info->lock);
8726
Yan, Zhengb742bb82010-05-16 10:46:24 -04008727 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008728
Josef Bacik0f9dd462008-09-23 13:14:11 -04008729 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8730 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04008731
Chris Mason6324fbf2008-03-24 15:01:59 -04008732 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8733 sizeof(cache->item));
8734 BUG_ON(ret);
8735
Chris Masond18a2c42008-04-04 15:40:00 -04008736 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008737
Chris Mason6324fbf2008-03-24 15:01:59 -04008738 return 0;
8739}
Zheng Yan1a40e232008-09-26 10:09:34 -04008740
8741int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8742 struct btrfs_root *root, u64 group_start)
8743{
8744 struct btrfs_path *path;
8745 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008746 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008747 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008748 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008749 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008750 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04008751 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008752
Zheng Yan1a40e232008-09-26 10:09:34 -04008753 root = root->fs_info->extent_root;
8754
8755 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8756 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008757 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008758
liubo9f7c43c2011-03-07 02:13:33 +00008759 /*
8760 * Free the reserved super bytes from this block group before
8761 * remove it.
8762 */
8763 free_excluded_extents(root, block_group);
8764
Zheng Yan1a40e232008-09-26 10:09:34 -04008765 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04008766 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8767 BTRFS_BLOCK_GROUP_RAID1 |
8768 BTRFS_BLOCK_GROUP_RAID10))
8769 factor = 2;
8770 else
8771 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008772
Chris Mason44fb5512009-06-04 15:34:51 -04008773 /* make sure this block group isn't part of an allocation cluster */
8774 cluster = &root->fs_info->data_alloc_cluster;
8775 spin_lock(&cluster->refill_lock);
8776 btrfs_return_cluster_to_free_space(block_group, cluster);
8777 spin_unlock(&cluster->refill_lock);
8778
8779 /*
8780 * make sure this block group isn't part of a metadata
8781 * allocation cluster
8782 */
8783 cluster = &root->fs_info->meta_alloc_cluster;
8784 spin_lock(&cluster->refill_lock);
8785 btrfs_return_cluster_to_free_space(block_group, cluster);
8786 spin_unlock(&cluster->refill_lock);
8787
Zheng Yan1a40e232008-09-26 10:09:34 -04008788 path = btrfs_alloc_path();
8789 BUG_ON(!path);
8790
Josef Bacik0af3d002010-06-21 14:48:16 -04008791 inode = lookup_free_space_inode(root, block_group, path);
8792 if (!IS_ERR(inode)) {
8793 btrfs_orphan_add(trans, inode);
8794 clear_nlink(inode);
8795 /* One for the block groups ref */
8796 spin_lock(&block_group->lock);
8797 if (block_group->iref) {
8798 block_group->iref = 0;
8799 block_group->inode = NULL;
8800 spin_unlock(&block_group->lock);
8801 iput(inode);
8802 } else {
8803 spin_unlock(&block_group->lock);
8804 }
8805 /* One for our lookup ref */
8806 iput(inode);
8807 }
8808
8809 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8810 key.offset = block_group->key.objectid;
8811 key.type = 0;
8812
8813 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8814 if (ret < 0)
8815 goto out;
8816 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008817 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008818 if (ret == 0) {
8819 ret = btrfs_del_item(trans, tree_root, path);
8820 if (ret)
8821 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008822 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008823 }
8824
Yan Zheng3dfdb932009-01-21 10:49:16 -05008825 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008826 rb_erase(&block_group->cache_node,
8827 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008828 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008829
Josef Bacik80eb2342008-10-29 14:49:05 -04008830 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008831 /*
8832 * we must use list_del_init so people can check to see if they
8833 * are still on the list after taking the semaphore
8834 */
8835 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008836 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008837
Josef Bacik817d52f2009-07-13 21:29:25 -04008838 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008839 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008840
8841 btrfs_remove_free_space_cache(block_group);
8842
Yan Zhengc146afa2008-11-12 14:34:12 -05008843 spin_lock(&block_group->space_info->lock);
8844 block_group->space_info->total_bytes -= block_group->key.offset;
8845 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008846 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008847 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008848
Josef Bacik0af3d002010-06-21 14:48:16 -04008849 memcpy(&key, &block_group->key, sizeof(key));
8850
Chris Mason283bb192009-07-24 16:30:55 -04008851 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008852
Chris Masonfa9c0d72009-04-03 09:47:43 -04008853 btrfs_put_block_group(block_group);
8854 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008855
8856 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8857 if (ret > 0)
8858 ret = -EIO;
8859 if (ret < 0)
8860 goto out;
8861
8862 ret = btrfs_del_item(trans, root, path);
8863out:
8864 btrfs_free_path(path);
8865 return ret;
8866}
liuboacce9522011-01-06 19:30:25 +08008867
liuboc59021f2011-03-07 02:13:14 +00008868int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8869{
8870 struct btrfs_space_info *space_info;
8871 int ret;
8872
8873 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0,
8874 &space_info);
8875 if (ret)
8876 return ret;
8877
8878 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0,
8879 &space_info);
8880 if (ret)
8881 return ret;
8882
8883 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0,
8884 &space_info);
8885 if (ret)
8886 return ret;
8887
8888 return ret;
8889}
8890
liuboacce9522011-01-06 19:30:25 +08008891int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8892{
8893 return unpin_extent_range(root, start, end);
8894}
8895
8896int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008897 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008898{
Li Dongyang5378e602011-03-24 10:24:27 +00008899 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008900}
Li Dongyangf7039b12011-03-24 10:24:28 +00008901
8902int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8903{
8904 struct btrfs_fs_info *fs_info = root->fs_info;
8905 struct btrfs_block_group_cache *cache = NULL;
8906 u64 group_trimmed;
8907 u64 start;
8908 u64 end;
8909 u64 trimmed = 0;
8910 int ret = 0;
8911
8912 cache = btrfs_lookup_block_group(fs_info, range->start);
8913
8914 while (cache) {
8915 if (cache->key.objectid >= (range->start + range->len)) {
8916 btrfs_put_block_group(cache);
8917 break;
8918 }
8919
8920 start = max(range->start, cache->key.objectid);
8921 end = min(range->start + range->len,
8922 cache->key.objectid + cache->key.offset);
8923
8924 if (end - start >= range->minlen) {
8925 if (!block_group_cache_done(cache)) {
8926 ret = cache_block_group(cache, NULL, root, 0);
8927 if (!ret)
8928 wait_block_group_cache_done(cache);
8929 }
8930 ret = btrfs_trim_block_group(cache,
8931 &group_trimmed,
8932 start,
8933 end,
8934 range->minlen);
8935
8936 trimmed += group_trimmed;
8937 if (ret) {
8938 btrfs_put_block_group(cache);
8939 break;
8940 }
8941 }
8942
8943 cache = next_block_group(fs_info->tree_root, cache);
8944 }
8945
8946 range->len = trimmed;
8947 return ret;
8948}