blob: d5455a2bf60bac7dfaaa0cd67626437ede0de6bc [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050027#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040031#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040033#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040034#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Josef Bacikf3465ca2008-11-12 14:19:50 -050036static int update_block_group(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040038 u64 bytenr, u64 num_bytes, int alloc);
39static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve, int sinfo);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040041static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root,
43 u64 bytenr, u64 num_bytes, u64 parent,
44 u64 root_objectid, u64 owner_objectid,
45 u64 owner_offset, int refs_to_drop,
46 struct btrfs_delayed_extent_op *extra_op);
47static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
48 struct extent_buffer *leaf,
49 struct btrfs_extent_item *ei);
50static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root,
52 u64 parent, u64 root_objectid,
53 u64 flags, u64 owner, u64 offset,
54 struct btrfs_key *ins, int ref_mod);
55static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 parent, u64 root_objectid,
58 u64 flags, struct btrfs_disk_key *key,
59 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050060static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61 struct btrfs_root *extent_root, u64 alloc_bytes,
62 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040063static int find_next_key(struct btrfs_path *path, int level,
64 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040065static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
66 int dump_block_groups);
Josef Bacik6a632092009-02-20 11:00:09 -050067
Josef Bacik817d52f2009-07-13 21:29:25 -040068static noinline int
69block_group_cache_done(struct btrfs_block_group_cache *cache)
70{
71 smp_mb();
72 return cache->cached == BTRFS_CACHE_FINISHED;
73}
74
Josef Bacik0f9dd462008-09-23 13:14:11 -040075static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
76{
77 return (cache->flags & bits) == bits;
78}
79
Josef Bacik11dfe352009-11-13 20:12:59 +000080void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
81{
82 atomic_inc(&cache->count);
83}
84
85void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
86{
Yan, Zhengf0486c62010-05-16 10:46:25 -040087 if (atomic_dec_and_test(&cache->count)) {
88 WARN_ON(cache->pinned > 0);
89 WARN_ON(cache->reserved > 0);
90 WARN_ON(cache->reserved_pinned > 0);
Josef Bacik11dfe352009-11-13 20:12:59 +000091 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -040092 }
Josef Bacik11dfe352009-11-13 20:12:59 +000093}
94
Josef Bacik0f9dd462008-09-23 13:14:11 -040095/*
96 * this adds the block group to the fs_info rb tree for the block group
97 * cache
98 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050099static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400100 struct btrfs_block_group_cache *block_group)
101{
102 struct rb_node **p;
103 struct rb_node *parent = NULL;
104 struct btrfs_block_group_cache *cache;
105
106 spin_lock(&info->block_group_cache_lock);
107 p = &info->block_group_cache_tree.rb_node;
108
109 while (*p) {
110 parent = *p;
111 cache = rb_entry(parent, struct btrfs_block_group_cache,
112 cache_node);
113 if (block_group->key.objectid < cache->key.objectid) {
114 p = &(*p)->rb_left;
115 } else if (block_group->key.objectid > cache->key.objectid) {
116 p = &(*p)->rb_right;
117 } else {
118 spin_unlock(&info->block_group_cache_lock);
119 return -EEXIST;
120 }
121 }
122
123 rb_link_node(&block_group->cache_node, parent, p);
124 rb_insert_color(&block_group->cache_node,
125 &info->block_group_cache_tree);
126 spin_unlock(&info->block_group_cache_lock);
127
128 return 0;
129}
130
131/*
132 * This will return the block group at or after bytenr if contains is 0, else
133 * it will return the block group that contains the bytenr
134 */
135static struct btrfs_block_group_cache *
136block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
137 int contains)
138{
139 struct btrfs_block_group_cache *cache, *ret = NULL;
140 struct rb_node *n;
141 u64 end, start;
142
143 spin_lock(&info->block_group_cache_lock);
144 n = info->block_group_cache_tree.rb_node;
145
146 while (n) {
147 cache = rb_entry(n, struct btrfs_block_group_cache,
148 cache_node);
149 end = cache->key.objectid + cache->key.offset - 1;
150 start = cache->key.objectid;
151
152 if (bytenr < start) {
153 if (!contains && (!ret || start < ret->key.objectid))
154 ret = cache;
155 n = n->rb_left;
156 } else if (bytenr > start) {
157 if (contains && bytenr <= end) {
158 ret = cache;
159 break;
160 }
161 n = n->rb_right;
162 } else {
163 ret = cache;
164 break;
165 }
166 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500167 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000168 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400169 spin_unlock(&info->block_group_cache_lock);
170
171 return ret;
172}
173
Yan Zheng11833d62009-09-11 16:11:19 -0400174static int add_excluded_extent(struct btrfs_root *root,
175 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400176{
Yan Zheng11833d62009-09-11 16:11:19 -0400177 u64 end = start + num_bytes - 1;
178 set_extent_bits(&root->fs_info->freed_extents[0],
179 start, end, EXTENT_UPTODATE, GFP_NOFS);
180 set_extent_bits(&root->fs_info->freed_extents[1],
181 start, end, EXTENT_UPTODATE, GFP_NOFS);
182 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400183}
184
Yan Zheng11833d62009-09-11 16:11:19 -0400185static void free_excluded_extents(struct btrfs_root *root,
186 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400187{
Yan Zheng11833d62009-09-11 16:11:19 -0400188 u64 start, end;
189
190 start = cache->key.objectid;
191 end = start + cache->key.offset - 1;
192
193 clear_extent_bits(&root->fs_info->freed_extents[0],
194 start, end, EXTENT_UPTODATE, GFP_NOFS);
195 clear_extent_bits(&root->fs_info->freed_extents[1],
196 start, end, EXTENT_UPTODATE, GFP_NOFS);
197}
198
199static int exclude_super_stripes(struct btrfs_root *root,
200 struct btrfs_block_group_cache *cache)
201{
Josef Bacik817d52f2009-07-13 21:29:25 -0400202 u64 bytenr;
203 u64 *logical;
204 int stripe_len;
205 int i, nr, ret;
206
Yan, Zheng06b23312009-11-26 09:31:11 +0000207 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
208 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
209 cache->bytes_super += stripe_len;
210 ret = add_excluded_extent(root, cache->key.objectid,
211 stripe_len);
212 BUG_ON(ret);
213 }
214
Josef Bacik817d52f2009-07-13 21:29:25 -0400215 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
216 bytenr = btrfs_sb_offset(i);
217 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
218 cache->key.objectid, bytenr,
219 0, &logical, &nr, &stripe_len);
220 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400221
Josef Bacik817d52f2009-07-13 21:29:25 -0400222 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400223 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400224 ret = add_excluded_extent(root, logical[nr],
225 stripe_len);
226 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400227 }
Yan Zheng11833d62009-09-11 16:11:19 -0400228
Josef Bacik817d52f2009-07-13 21:29:25 -0400229 kfree(logical);
230 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400231 return 0;
232}
233
Yan Zheng11833d62009-09-11 16:11:19 -0400234static struct btrfs_caching_control *
235get_caching_control(struct btrfs_block_group_cache *cache)
236{
237 struct btrfs_caching_control *ctl;
238
239 spin_lock(&cache->lock);
240 if (cache->cached != BTRFS_CACHE_STARTED) {
241 spin_unlock(&cache->lock);
242 return NULL;
243 }
244
245 ctl = cache->caching_ctl;
246 atomic_inc(&ctl->count);
247 spin_unlock(&cache->lock);
248 return ctl;
249}
250
251static void put_caching_control(struct btrfs_caching_control *ctl)
252{
253 if (atomic_dec_and_test(&ctl->count))
254 kfree(ctl);
255}
256
Josef Bacik0f9dd462008-09-23 13:14:11 -0400257/*
258 * this is only called by cache_block_group, since we could have freed extents
259 * we need to check the pinned_extents for any extents that can't be used yet
260 * since their free space will be released as soon as the transaction commits.
261 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400262static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263 struct btrfs_fs_info *info, u64 start, u64 end)
264{
Josef Bacik817d52f2009-07-13 21:29:25 -0400265 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400266 int ret;
267
268 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400269 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400270 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400271 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400272 if (ret)
273 break;
274
Yan, Zheng06b23312009-11-26 09:31:11 +0000275 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400276 start = extent_end + 1;
277 } else if (extent_start > start && extent_start < end) {
278 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400279 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500280 ret = btrfs_add_free_space(block_group, start,
281 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400282 BUG_ON(ret);
283 start = extent_end + 1;
284 } else {
285 break;
286 }
287 }
288
289 if (start < end) {
290 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400291 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500292 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400293 BUG_ON(ret);
294 }
295
Josef Bacik817d52f2009-07-13 21:29:25 -0400296 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400297}
298
Josef Bacik817d52f2009-07-13 21:29:25 -0400299static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400300{
Josef Bacik817d52f2009-07-13 21:29:25 -0400301 struct btrfs_block_group_cache *block_group = data;
302 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400303 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
304 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400305 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400306 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400307 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400308 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400309 u64 last = 0;
310 u32 nritems;
311 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400312
Chris Masone37c9e62007-05-09 20:13:14 -0400313 path = btrfs_alloc_path();
314 if (!path)
315 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400316
Yan Zheng11833d62009-09-11 16:11:19 -0400317 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400318 spin_lock(&block_group->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400319 block_group->space_info->bytes_readonly += block_group->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -0400320 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400321
Josef Bacik817d52f2009-07-13 21:29:25 -0400322 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400323
Chris Mason5cd57b22008-06-25 16:01:30 -0400324 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400325 * We don't want to deadlock with somebody trying to allocate a new
326 * extent for the extent root while also trying to search the extent
327 * root to add free space. So we skip locking and search the commit
328 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400329 */
330 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 path->search_commit_root = 1;
332 path->reada = 2;
333
Yan Zhenge4404d62008-12-12 10:03:26 -0500334 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400335 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400336 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400337again:
Yan Zheng11833d62009-09-11 16:11:19 -0400338 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400339 /* need to make sure the commit_root doesn't disappear */
340 down_read(&fs_info->extent_commit_sem);
341
Yan Zheng11833d62009-09-11 16:11:19 -0400342 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400343 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400344 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500345
Yan Zheng11833d62009-09-11 16:11:19 -0400346 leaf = path->nodes[0];
347 nritems = btrfs_header_nritems(leaf);
348
Chris Masond3977122009-01-05 21:25:51 -0500349 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400350 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400351 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400352 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400353 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400354 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400355
Yan Zheng11833d62009-09-11 16:11:19 -0400356 if (path->slots[0] < nritems) {
357 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
358 } else {
359 ret = find_next_key(path, 0, &key);
360 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400361 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400362
Yan Zheng11833d62009-09-11 16:11:19 -0400363 caching_ctl->progress = last;
364 btrfs_release_path(extent_root, path);
365 up_read(&fs_info->extent_commit_sem);
366 mutex_unlock(&caching_ctl->mutex);
367 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400368 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400369 else
370 cond_resched();
371 goto again;
372 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400373
Yan Zheng11833d62009-09-11 16:11:19 -0400374 if (key.objectid < block_group->key.objectid) {
375 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400376 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400377 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400378
Chris Masone37c9e62007-05-09 20:13:14 -0400379 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400380 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400381 break;
Yan7d7d6062007-09-14 16:15:28 -0400382
Yan Zheng11833d62009-09-11 16:11:19 -0400383 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400384 total_found += add_new_free_space(block_group,
385 fs_info, last,
386 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400387 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400388
Yan Zheng11833d62009-09-11 16:11:19 -0400389 if (total_found > (1024 * 1024 * 2)) {
390 total_found = 0;
391 wake_up(&caching_ctl->wait);
392 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400393 }
Chris Masone37c9e62007-05-09 20:13:14 -0400394 path->slots[0]++;
395 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400396 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400397
398 total_found += add_new_free_space(block_group, fs_info, last,
399 block_group->key.objectid +
400 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400401 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400402
403 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400404 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400405 block_group->cached = BTRFS_CACHE_FINISHED;
406 spin_unlock(&block_group->lock);
407
Chris Mason54aa1f42007-06-22 14:16:25 -0400408err:
Chris Masone37c9e62007-05-09 20:13:14 -0400409 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400410 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400411
Yan Zheng11833d62009-09-11 16:11:19 -0400412 free_excluded_extents(extent_root, block_group);
413
414 mutex_unlock(&caching_ctl->mutex);
415 wake_up(&caching_ctl->wait);
416
417 put_caching_control(caching_ctl);
418 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000419 btrfs_put_block_group(block_group);
420
Josef Bacik817d52f2009-07-13 21:29:25 -0400421 return 0;
422}
423
424static int cache_block_group(struct btrfs_block_group_cache *cache)
425{
Yan Zheng11833d62009-09-11 16:11:19 -0400426 struct btrfs_fs_info *fs_info = cache->fs_info;
427 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400428 struct task_struct *tsk;
429 int ret = 0;
430
Yan Zheng11833d62009-09-11 16:11:19 -0400431 smp_mb();
432 if (cache->cached != BTRFS_CACHE_NO)
433 return 0;
434
435 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
436 BUG_ON(!caching_ctl);
437
438 INIT_LIST_HEAD(&caching_ctl->list);
439 mutex_init(&caching_ctl->mutex);
440 init_waitqueue_head(&caching_ctl->wait);
441 caching_ctl->block_group = cache;
442 caching_ctl->progress = cache->key.objectid;
443 /* one for caching kthread, one for caching block group list */
444 atomic_set(&caching_ctl->count, 2);
445
Josef Bacik817d52f2009-07-13 21:29:25 -0400446 spin_lock(&cache->lock);
447 if (cache->cached != BTRFS_CACHE_NO) {
448 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400449 kfree(caching_ctl);
450 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400451 }
Yan Zheng11833d62009-09-11 16:11:19 -0400452 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400453 cache->cached = BTRFS_CACHE_STARTED;
454 spin_unlock(&cache->lock);
455
Yan Zheng11833d62009-09-11 16:11:19 -0400456 down_write(&fs_info->extent_commit_sem);
457 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
458 up_write(&fs_info->extent_commit_sem);
459
460 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000461 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400462
Josef Bacik817d52f2009-07-13 21:29:25 -0400463 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
464 cache->key.objectid);
465 if (IS_ERR(tsk)) {
466 ret = PTR_ERR(tsk);
467 printk(KERN_ERR "error running thread %d\n", ret);
468 BUG();
469 }
470
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400471 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400472}
473
Josef Bacik0f9dd462008-09-23 13:14:11 -0400474/*
475 * return the block group that starts at or after bytenr
476 */
Chris Masond3977122009-01-05 21:25:51 -0500477static struct btrfs_block_group_cache *
478btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400479{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400480 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400481
Josef Bacik0f9dd462008-09-23 13:14:11 -0400482 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400483
Josef Bacik0f9dd462008-09-23 13:14:11 -0400484 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400485}
486
Josef Bacik0f9dd462008-09-23 13:14:11 -0400487/*
Sankar P9f556842009-05-14 13:52:22 -0400488 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400489 */
Chris Masond3977122009-01-05 21:25:51 -0500490struct btrfs_block_group_cache *btrfs_lookup_block_group(
491 struct btrfs_fs_info *info,
492 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400493{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400494 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400495
Josef Bacik0f9dd462008-09-23 13:14:11 -0400496 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400497
Josef Bacik0f9dd462008-09-23 13:14:11 -0400498 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400499}
Chris Mason0b86a832008-03-24 15:01:56 -0400500
Josef Bacik0f9dd462008-09-23 13:14:11 -0400501static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
502 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400503{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400504 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400505 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400506
Yan, Zhengb742bb82010-05-16 10:46:24 -0400507 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
508 BTRFS_BLOCK_GROUP_METADATA;
509
Chris Mason4184ea72009-03-10 12:39:20 -0400510 rcu_read_lock();
511 list_for_each_entry_rcu(found, head, list) {
512 if (found->flags == flags) {
513 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400514 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400515 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400516 }
Chris Mason4184ea72009-03-10 12:39:20 -0400517 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400518 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400519}
520
Chris Mason4184ea72009-03-10 12:39:20 -0400521/*
522 * after adding space to the filesystem, we need to clear the full flags
523 * on all the space infos.
524 */
525void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
526{
527 struct list_head *head = &info->space_info;
528 struct btrfs_space_info *found;
529
530 rcu_read_lock();
531 list_for_each_entry_rcu(found, head, list)
532 found->full = 0;
533 rcu_read_unlock();
534}
535
Josef Bacik80eb2342008-10-29 14:49:05 -0400536static u64 div_factor(u64 num, int factor)
537{
538 if (factor == 10)
539 return num;
540 num *= factor;
541 do_div(num, 10);
542 return num;
543}
544
Yan Zhengd2fb3432008-12-11 16:30:39 -0500545u64 btrfs_find_block_group(struct btrfs_root *root,
546 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400547{
Chris Mason96b51792007-10-15 16:15:19 -0400548 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400549 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500550 u64 last = max(search_hint, search_start);
551 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400552 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500553 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400554 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400555again:
Zheng Yane8569812008-09-26 10:05:48 -0400556 while (1) {
557 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400558 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400559 break;
Chris Mason96b51792007-10-15 16:15:19 -0400560
Chris Masonc286ac42008-07-22 23:06:41 -0400561 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400562 last = cache->key.objectid + cache->key.offset;
563 used = btrfs_block_group_used(&cache->item);
564
Yan Zhengd2fb3432008-12-11 16:30:39 -0500565 if ((full_search || !cache->ro) &&
566 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400567 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500568 div_factor(cache->key.offset, factor)) {
569 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400570 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400571 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400572 goto found;
573 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400574 }
Chris Masonc286ac42008-07-22 23:06:41 -0400575 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400576 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400577 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400578 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400579 if (!wrapped) {
580 last = search_start;
581 wrapped = 1;
582 goto again;
583 }
584 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400585 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400586 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400587 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400588 goto again;
589 }
Chris Masonbe744172007-05-06 10:15:01 -0400590found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500591 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400592}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400593
Chris Masone02119d2008-09-05 16:13:11 -0400594/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400595int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400596{
597 int ret;
598 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400599 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400600
Zheng Yan31840ae2008-09-23 13:14:14 -0400601 path = btrfs_alloc_path();
602 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400603 key.objectid = start;
604 key.offset = len;
605 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
606 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
607 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400608 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500609 return ret;
610}
611
Chris Masond8d5f3e2007-12-11 12:42:00 -0500612/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400613 * helper function to lookup reference count and flags of extent.
614 *
615 * the head node for delayed ref is used to store the sum of all the
616 * reference count modifications queued up in the rbtree. the head
617 * node may also store the extent flags to set. This way you can check
618 * to see what the reference count and extent flags would be if all of
619 * the delayed refs are not processed.
620 */
621int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
622 struct btrfs_root *root, u64 bytenr,
623 u64 num_bytes, u64 *refs, u64 *flags)
624{
625 struct btrfs_delayed_ref_head *head;
626 struct btrfs_delayed_ref_root *delayed_refs;
627 struct btrfs_path *path;
628 struct btrfs_extent_item *ei;
629 struct extent_buffer *leaf;
630 struct btrfs_key key;
631 u32 item_size;
632 u64 num_refs;
633 u64 extent_flags;
634 int ret;
635
636 path = btrfs_alloc_path();
637 if (!path)
638 return -ENOMEM;
639
640 key.objectid = bytenr;
641 key.type = BTRFS_EXTENT_ITEM_KEY;
642 key.offset = num_bytes;
643 if (!trans) {
644 path->skip_locking = 1;
645 path->search_commit_root = 1;
646 }
647again:
648 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
649 &key, path, 0, 0);
650 if (ret < 0)
651 goto out_free;
652
653 if (ret == 0) {
654 leaf = path->nodes[0];
655 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
656 if (item_size >= sizeof(*ei)) {
657 ei = btrfs_item_ptr(leaf, path->slots[0],
658 struct btrfs_extent_item);
659 num_refs = btrfs_extent_refs(leaf, ei);
660 extent_flags = btrfs_extent_flags(leaf, ei);
661 } else {
662#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
663 struct btrfs_extent_item_v0 *ei0;
664 BUG_ON(item_size != sizeof(*ei0));
665 ei0 = btrfs_item_ptr(leaf, path->slots[0],
666 struct btrfs_extent_item_v0);
667 num_refs = btrfs_extent_refs_v0(leaf, ei0);
668 /* FIXME: this isn't correct for data */
669 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
670#else
671 BUG();
672#endif
673 }
674 BUG_ON(num_refs == 0);
675 } else {
676 num_refs = 0;
677 extent_flags = 0;
678 ret = 0;
679 }
680
681 if (!trans)
682 goto out;
683
684 delayed_refs = &trans->transaction->delayed_refs;
685 spin_lock(&delayed_refs->lock);
686 head = btrfs_find_delayed_ref_head(trans, bytenr);
687 if (head) {
688 if (!mutex_trylock(&head->mutex)) {
689 atomic_inc(&head->node.refs);
690 spin_unlock(&delayed_refs->lock);
691
692 btrfs_release_path(root->fs_info->extent_root, path);
693
694 mutex_lock(&head->mutex);
695 mutex_unlock(&head->mutex);
696 btrfs_put_delayed_ref(&head->node);
697 goto again;
698 }
699 if (head->extent_op && head->extent_op->update_flags)
700 extent_flags |= head->extent_op->flags_to_set;
701 else
702 BUG_ON(num_refs == 0);
703
704 num_refs += head->node.ref_mod;
705 mutex_unlock(&head->mutex);
706 }
707 spin_unlock(&delayed_refs->lock);
708out:
709 WARN_ON(num_refs == 0);
710 if (refs)
711 *refs = num_refs;
712 if (flags)
713 *flags = extent_flags;
714out_free:
715 btrfs_free_path(path);
716 return ret;
717}
718
719/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500720 * Back reference rules. Back refs have three main goals:
721 *
722 * 1) differentiate between all holders of references to an extent so that
723 * when a reference is dropped we can make sure it was a valid reference
724 * before freeing the extent.
725 *
726 * 2) Provide enough information to quickly find the holders of an extent
727 * if we notice a given block is corrupted or bad.
728 *
729 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
730 * maintenance. This is actually the same as #2, but with a slightly
731 * different use case.
732 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400733 * There are two kinds of back refs. The implicit back refs is optimized
734 * for pointers in non-shared tree blocks. For a given pointer in a block,
735 * back refs of this kind provide information about the block's owner tree
736 * and the pointer's key. These information allow us to find the block by
737 * b-tree searching. The full back refs is for pointers in tree blocks not
738 * referenced by their owner trees. The location of tree block is recorded
739 * in the back refs. Actually the full back refs is generic, and can be
740 * used in all cases the implicit back refs is used. The major shortcoming
741 * of the full back refs is its overhead. Every time a tree block gets
742 * COWed, we have to update back refs entry for all pointers in it.
743 *
744 * For a newly allocated tree block, we use implicit back refs for
745 * pointers in it. This means most tree related operations only involve
746 * implicit back refs. For a tree block created in old transaction, the
747 * only way to drop a reference to it is COW it. So we can detect the
748 * event that tree block loses its owner tree's reference and do the
749 * back refs conversion.
750 *
751 * When a tree block is COW'd through a tree, there are four cases:
752 *
753 * The reference count of the block is one and the tree is the block's
754 * owner tree. Nothing to do in this case.
755 *
756 * The reference count of the block is one and the tree is not the
757 * block's owner tree. In this case, full back refs is used for pointers
758 * in the block. Remove these full back refs, add implicit back refs for
759 * every pointers in the new block.
760 *
761 * The reference count of the block is greater than one and the tree is
762 * the block's owner tree. In this case, implicit back refs is used for
763 * pointers in the block. Add full back refs for every pointers in the
764 * block, increase lower level extents' reference counts. The original
765 * implicit back refs are entailed to the new block.
766 *
767 * The reference count of the block is greater than one and the tree is
768 * not the block's owner tree. Add implicit back refs for every pointer in
769 * the new block, increase lower level extents' reference count.
770 *
771 * Back Reference Key composing:
772 *
773 * The key objectid corresponds to the first byte in the extent,
774 * The key type is used to differentiate between types of back refs.
775 * There are different meanings of the key offset for different types
776 * of back refs.
777 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500778 * File extents can be referenced by:
779 *
780 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400781 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500782 * - different offsets inside a file (bookend extents in file.c)
783 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400784 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500785 *
786 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500787 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400788 * - original offset in the file
789 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400790 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400791 * The key offset for the implicit back refs is hash of the first
792 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500793 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400794 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500795 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400796 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500797 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400798 * The key offset for the implicit back refs is the first byte of
799 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500800 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400801 * When a file extent is allocated, The implicit back refs is used.
802 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500803 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400804 * (root_key.objectid, inode objectid, offset in file, 1)
805 *
806 * When a file extent is removed file truncation, we find the
807 * corresponding implicit back refs and check the following fields:
808 *
809 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500810 *
811 * Btree extents can be referenced by:
812 *
813 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500814 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400815 * Both the implicit back refs and the full back refs for tree blocks
816 * only consist of key. The key offset for the implicit back refs is
817 * objectid of block's owner tree. The key offset for the full back refs
818 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500819 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400820 * When implicit back refs is used, information about the lowest key and
821 * level of the tree block are required. These information are stored in
822 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500823 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400824
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400825#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
826static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
827 struct btrfs_root *root,
828 struct btrfs_path *path,
829 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500830{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400831 struct btrfs_extent_item *item;
832 struct btrfs_extent_item_v0 *ei0;
833 struct btrfs_extent_ref_v0 *ref0;
834 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400835 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400836 struct btrfs_key key;
837 struct btrfs_key found_key;
838 u32 new_size = sizeof(*item);
839 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500840 int ret;
841
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400842 leaf = path->nodes[0];
843 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500844
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400845 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
846 ei0 = btrfs_item_ptr(leaf, path->slots[0],
847 struct btrfs_extent_item_v0);
848 refs = btrfs_extent_refs_v0(leaf, ei0);
849
850 if (owner == (u64)-1) {
851 while (1) {
852 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
853 ret = btrfs_next_leaf(root, path);
854 if (ret < 0)
855 return ret;
856 BUG_ON(ret > 0);
857 leaf = path->nodes[0];
858 }
859 btrfs_item_key_to_cpu(leaf, &found_key,
860 path->slots[0]);
861 BUG_ON(key.objectid != found_key.objectid);
862 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
863 path->slots[0]++;
864 continue;
865 }
866 ref0 = btrfs_item_ptr(leaf, path->slots[0],
867 struct btrfs_extent_ref_v0);
868 owner = btrfs_ref_objectid_v0(leaf, ref0);
869 break;
870 }
871 }
872 btrfs_release_path(root, path);
873
874 if (owner < BTRFS_FIRST_FREE_OBJECTID)
875 new_size += sizeof(*bi);
876
877 new_size -= sizeof(*ei0);
878 ret = btrfs_search_slot(trans, root, &key, path,
879 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400880 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881 return ret;
882 BUG_ON(ret);
883
884 ret = btrfs_extend_item(trans, root, path, new_size);
885 BUG_ON(ret);
886
887 leaf = path->nodes[0];
888 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
889 btrfs_set_extent_refs(leaf, item, refs);
890 /* FIXME: get real generation */
891 btrfs_set_extent_generation(leaf, item, 0);
892 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
893 btrfs_set_extent_flags(leaf, item,
894 BTRFS_EXTENT_FLAG_TREE_BLOCK |
895 BTRFS_BLOCK_FLAG_FULL_BACKREF);
896 bi = (struct btrfs_tree_block_info *)(item + 1);
897 /* FIXME: get first key of the block */
898 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
899 btrfs_set_tree_block_level(leaf, bi, (int)owner);
900 } else {
901 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
902 }
903 btrfs_mark_buffer_dirty(leaf);
904 return 0;
905}
906#endif
907
908static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
909{
910 u32 high_crc = ~(u32)0;
911 u32 low_crc = ~(u32)0;
912 __le64 lenum;
913
914 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100915 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400916 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100917 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100919 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400920
921 return ((u64)high_crc << 31) ^ (u64)low_crc;
922}
923
924static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
925 struct btrfs_extent_data_ref *ref)
926{
927 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
928 btrfs_extent_data_ref_objectid(leaf, ref),
929 btrfs_extent_data_ref_offset(leaf, ref));
930}
931
932static int match_extent_data_ref(struct extent_buffer *leaf,
933 struct btrfs_extent_data_ref *ref,
934 u64 root_objectid, u64 owner, u64 offset)
935{
936 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
937 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
938 btrfs_extent_data_ref_offset(leaf, ref) != offset)
939 return 0;
940 return 1;
941}
942
943static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
944 struct btrfs_root *root,
945 struct btrfs_path *path,
946 u64 bytenr, u64 parent,
947 u64 root_objectid,
948 u64 owner, u64 offset)
949{
950 struct btrfs_key key;
951 struct btrfs_extent_data_ref *ref;
952 struct extent_buffer *leaf;
953 u32 nritems;
954 int ret;
955 int recow;
956 int err = -ENOENT;
957
958 key.objectid = bytenr;
959 if (parent) {
960 key.type = BTRFS_SHARED_DATA_REF_KEY;
961 key.offset = parent;
962 } else {
963 key.type = BTRFS_EXTENT_DATA_REF_KEY;
964 key.offset = hash_extent_data_ref(root_objectid,
965 owner, offset);
966 }
967again:
968 recow = 0;
969 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
970 if (ret < 0) {
971 err = ret;
972 goto fail;
973 }
974
975 if (parent) {
976 if (!ret)
977 return 0;
978#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
979 key.type = BTRFS_EXTENT_REF_V0_KEY;
980 btrfs_release_path(root, path);
981 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
982 if (ret < 0) {
983 err = ret;
984 goto fail;
985 }
986 if (!ret)
987 return 0;
988#endif
989 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400990 }
991
992 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400993 nritems = btrfs_header_nritems(leaf);
994 while (1) {
995 if (path->slots[0] >= nritems) {
996 ret = btrfs_next_leaf(root, path);
997 if (ret < 0)
998 err = ret;
999 if (ret)
1000 goto fail;
1001
1002 leaf = path->nodes[0];
1003 nritems = btrfs_header_nritems(leaf);
1004 recow = 1;
1005 }
1006
1007 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1008 if (key.objectid != bytenr ||
1009 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1010 goto fail;
1011
1012 ref = btrfs_item_ptr(leaf, path->slots[0],
1013 struct btrfs_extent_data_ref);
1014
1015 if (match_extent_data_ref(leaf, ref, root_objectid,
1016 owner, offset)) {
1017 if (recow) {
1018 btrfs_release_path(root, path);
1019 goto again;
1020 }
1021 err = 0;
1022 break;
1023 }
1024 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001025 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001026fail:
1027 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001028}
1029
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001030static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1031 struct btrfs_root *root,
1032 struct btrfs_path *path,
1033 u64 bytenr, u64 parent,
1034 u64 root_objectid, u64 owner,
1035 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001036{
1037 struct btrfs_key key;
1038 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001039 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001040 u32 num_refs;
1041 int ret;
1042
1043 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044 if (parent) {
1045 key.type = BTRFS_SHARED_DATA_REF_KEY;
1046 key.offset = parent;
1047 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001048 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001049 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1050 key.offset = hash_extent_data_ref(root_objectid,
1051 owner, offset);
1052 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001053 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001054
1055 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1056 if (ret && ret != -EEXIST)
1057 goto fail;
1058
1059 leaf = path->nodes[0];
1060 if (parent) {
1061 struct btrfs_shared_data_ref *ref;
1062 ref = btrfs_item_ptr(leaf, path->slots[0],
1063 struct btrfs_shared_data_ref);
1064 if (ret == 0) {
1065 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1066 } else {
1067 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1068 num_refs += refs_to_add;
1069 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1070 }
1071 } else {
1072 struct btrfs_extent_data_ref *ref;
1073 while (ret == -EEXIST) {
1074 ref = btrfs_item_ptr(leaf, path->slots[0],
1075 struct btrfs_extent_data_ref);
1076 if (match_extent_data_ref(leaf, ref, root_objectid,
1077 owner, offset))
1078 break;
1079 btrfs_release_path(root, path);
1080 key.offset++;
1081 ret = btrfs_insert_empty_item(trans, root, path, &key,
1082 size);
1083 if (ret && ret != -EEXIST)
1084 goto fail;
1085
1086 leaf = path->nodes[0];
1087 }
1088 ref = btrfs_item_ptr(leaf, path->slots[0],
1089 struct btrfs_extent_data_ref);
1090 if (ret == 0) {
1091 btrfs_set_extent_data_ref_root(leaf, ref,
1092 root_objectid);
1093 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1094 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1095 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1096 } else {
1097 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1098 num_refs += refs_to_add;
1099 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1100 }
1101 }
1102 btrfs_mark_buffer_dirty(leaf);
1103 ret = 0;
1104fail:
Chris Mason7bb86312007-12-11 09:25:06 -05001105 btrfs_release_path(root, path);
1106 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001107}
1108
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001109static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1110 struct btrfs_root *root,
1111 struct btrfs_path *path,
1112 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001113{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001114 struct btrfs_key key;
1115 struct btrfs_extent_data_ref *ref1 = NULL;
1116 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001117 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001118 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001119 int ret = 0;
1120
1121 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001122 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1123
1124 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1125 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1126 struct btrfs_extent_data_ref);
1127 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1128 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1129 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1130 struct btrfs_shared_data_ref);
1131 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1132#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1133 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1134 struct btrfs_extent_ref_v0 *ref0;
1135 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1136 struct btrfs_extent_ref_v0);
1137 num_refs = btrfs_ref_count_v0(leaf, ref0);
1138#endif
1139 } else {
1140 BUG();
1141 }
1142
Chris Mason56bec292009-03-13 10:10:06 -04001143 BUG_ON(num_refs < refs_to_drop);
1144 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001145
Zheng Yan31840ae2008-09-23 13:14:14 -04001146 if (num_refs == 0) {
1147 ret = btrfs_del_item(trans, root, path);
1148 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001149 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1150 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1151 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1152 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1153#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1154 else {
1155 struct btrfs_extent_ref_v0 *ref0;
1156 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1157 struct btrfs_extent_ref_v0);
1158 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1159 }
1160#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001161 btrfs_mark_buffer_dirty(leaf);
1162 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001163 return ret;
1164}
1165
1166static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1167 struct btrfs_path *path,
1168 struct btrfs_extent_inline_ref *iref)
1169{
1170 struct btrfs_key key;
1171 struct extent_buffer *leaf;
1172 struct btrfs_extent_data_ref *ref1;
1173 struct btrfs_shared_data_ref *ref2;
1174 u32 num_refs = 0;
1175
1176 leaf = path->nodes[0];
1177 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1178 if (iref) {
1179 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1180 BTRFS_EXTENT_DATA_REF_KEY) {
1181 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1182 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1183 } else {
1184 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1185 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1186 }
1187 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1188 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1189 struct btrfs_extent_data_ref);
1190 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1191 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1192 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1193 struct btrfs_shared_data_ref);
1194 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1195#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1196 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1197 struct btrfs_extent_ref_v0 *ref0;
1198 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1199 struct btrfs_extent_ref_v0);
1200 num_refs = btrfs_ref_count_v0(leaf, ref0);
1201#endif
1202 } else {
1203 WARN_ON(1);
1204 }
1205 return num_refs;
1206}
1207
1208static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1209 struct btrfs_root *root,
1210 struct btrfs_path *path,
1211 u64 bytenr, u64 parent,
1212 u64 root_objectid)
1213{
1214 struct btrfs_key key;
1215 int ret;
1216
1217 key.objectid = bytenr;
1218 if (parent) {
1219 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1220 key.offset = parent;
1221 } else {
1222 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1223 key.offset = root_objectid;
1224 }
1225
1226 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1227 if (ret > 0)
1228 ret = -ENOENT;
1229#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1230 if (ret == -ENOENT && parent) {
1231 btrfs_release_path(root, path);
1232 key.type = BTRFS_EXTENT_REF_V0_KEY;
1233 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1234 if (ret > 0)
1235 ret = -ENOENT;
1236 }
1237#endif
1238 return ret;
1239}
1240
1241static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1242 struct btrfs_root *root,
1243 struct btrfs_path *path,
1244 u64 bytenr, u64 parent,
1245 u64 root_objectid)
1246{
1247 struct btrfs_key key;
1248 int ret;
1249
1250 key.objectid = bytenr;
1251 if (parent) {
1252 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1253 key.offset = parent;
1254 } else {
1255 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1256 key.offset = root_objectid;
1257 }
1258
1259 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001260 btrfs_release_path(root, path);
1261 return ret;
1262}
1263
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001264static inline int extent_ref_type(u64 parent, u64 owner)
1265{
1266 int type;
1267 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1268 if (parent > 0)
1269 type = BTRFS_SHARED_BLOCK_REF_KEY;
1270 else
1271 type = BTRFS_TREE_BLOCK_REF_KEY;
1272 } else {
1273 if (parent > 0)
1274 type = BTRFS_SHARED_DATA_REF_KEY;
1275 else
1276 type = BTRFS_EXTENT_DATA_REF_KEY;
1277 }
1278 return type;
1279}
1280
Yan Zheng2c47e6052009-06-27 21:07:35 -04001281static int find_next_key(struct btrfs_path *path, int level,
1282 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001283
1284{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001285 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001286 if (!path->nodes[level])
1287 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001288 if (path->slots[level] + 1 >=
1289 btrfs_header_nritems(path->nodes[level]))
1290 continue;
1291 if (level == 0)
1292 btrfs_item_key_to_cpu(path->nodes[level], key,
1293 path->slots[level] + 1);
1294 else
1295 btrfs_node_key_to_cpu(path->nodes[level], key,
1296 path->slots[level] + 1);
1297 return 0;
1298 }
1299 return 1;
1300}
1301
1302/*
1303 * look for inline back ref. if back ref is found, *ref_ret is set
1304 * to the address of inline back ref, and 0 is returned.
1305 *
1306 * if back ref isn't found, *ref_ret is set to the address where it
1307 * should be inserted, and -ENOENT is returned.
1308 *
1309 * if insert is true and there are too many inline back refs, the path
1310 * points to the extent item, and -EAGAIN is returned.
1311 *
1312 * NOTE: inline back refs are ordered in the same way that back ref
1313 * items in the tree are ordered.
1314 */
1315static noinline_for_stack
1316int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1317 struct btrfs_root *root,
1318 struct btrfs_path *path,
1319 struct btrfs_extent_inline_ref **ref_ret,
1320 u64 bytenr, u64 num_bytes,
1321 u64 parent, u64 root_objectid,
1322 u64 owner, u64 offset, int insert)
1323{
1324 struct btrfs_key key;
1325 struct extent_buffer *leaf;
1326 struct btrfs_extent_item *ei;
1327 struct btrfs_extent_inline_ref *iref;
1328 u64 flags;
1329 u64 item_size;
1330 unsigned long ptr;
1331 unsigned long end;
1332 int extra_size;
1333 int type;
1334 int want;
1335 int ret;
1336 int err = 0;
1337
1338 key.objectid = bytenr;
1339 key.type = BTRFS_EXTENT_ITEM_KEY;
1340 key.offset = num_bytes;
1341
1342 want = extent_ref_type(parent, owner);
1343 if (insert) {
1344 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001345 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001346 } else
1347 extra_size = -1;
1348 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1349 if (ret < 0) {
1350 err = ret;
1351 goto out;
1352 }
1353 BUG_ON(ret);
1354
1355 leaf = path->nodes[0];
1356 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1357#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1358 if (item_size < sizeof(*ei)) {
1359 if (!insert) {
1360 err = -ENOENT;
1361 goto out;
1362 }
1363 ret = convert_extent_item_v0(trans, root, path, owner,
1364 extra_size);
1365 if (ret < 0) {
1366 err = ret;
1367 goto out;
1368 }
1369 leaf = path->nodes[0];
1370 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1371 }
1372#endif
1373 BUG_ON(item_size < sizeof(*ei));
1374
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001375 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1376 flags = btrfs_extent_flags(leaf, ei);
1377
1378 ptr = (unsigned long)(ei + 1);
1379 end = (unsigned long)ei + item_size;
1380
1381 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1382 ptr += sizeof(struct btrfs_tree_block_info);
1383 BUG_ON(ptr > end);
1384 } else {
1385 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1386 }
1387
1388 err = -ENOENT;
1389 while (1) {
1390 if (ptr >= end) {
1391 WARN_ON(ptr > end);
1392 break;
1393 }
1394 iref = (struct btrfs_extent_inline_ref *)ptr;
1395 type = btrfs_extent_inline_ref_type(leaf, iref);
1396 if (want < type)
1397 break;
1398 if (want > type) {
1399 ptr += btrfs_extent_inline_ref_size(type);
1400 continue;
1401 }
1402
1403 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1404 struct btrfs_extent_data_ref *dref;
1405 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1406 if (match_extent_data_ref(leaf, dref, root_objectid,
1407 owner, offset)) {
1408 err = 0;
1409 break;
1410 }
1411 if (hash_extent_data_ref_item(leaf, dref) <
1412 hash_extent_data_ref(root_objectid, owner, offset))
1413 break;
1414 } else {
1415 u64 ref_offset;
1416 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1417 if (parent > 0) {
1418 if (parent == ref_offset) {
1419 err = 0;
1420 break;
1421 }
1422 if (ref_offset < parent)
1423 break;
1424 } else {
1425 if (root_objectid == ref_offset) {
1426 err = 0;
1427 break;
1428 }
1429 if (ref_offset < root_objectid)
1430 break;
1431 }
1432 }
1433 ptr += btrfs_extent_inline_ref_size(type);
1434 }
1435 if (err == -ENOENT && insert) {
1436 if (item_size + extra_size >=
1437 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1438 err = -EAGAIN;
1439 goto out;
1440 }
1441 /*
1442 * To add new inline back ref, we have to make sure
1443 * there is no corresponding back ref item.
1444 * For simplicity, we just do not add new inline back
1445 * ref if there is any kind of item for this block
1446 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001447 if (find_next_key(path, 0, &key) == 0 &&
1448 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001449 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001450 err = -EAGAIN;
1451 goto out;
1452 }
1453 }
1454 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1455out:
Yan Zheng85d41982009-06-11 08:51:10 -04001456 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001457 path->keep_locks = 0;
1458 btrfs_unlock_up_safe(path, 1);
1459 }
1460 return err;
1461}
1462
1463/*
1464 * helper to add new inline back ref
1465 */
1466static noinline_for_stack
1467int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1468 struct btrfs_root *root,
1469 struct btrfs_path *path,
1470 struct btrfs_extent_inline_ref *iref,
1471 u64 parent, u64 root_objectid,
1472 u64 owner, u64 offset, int refs_to_add,
1473 struct btrfs_delayed_extent_op *extent_op)
1474{
1475 struct extent_buffer *leaf;
1476 struct btrfs_extent_item *ei;
1477 unsigned long ptr;
1478 unsigned long end;
1479 unsigned long item_offset;
1480 u64 refs;
1481 int size;
1482 int type;
1483 int ret;
1484
1485 leaf = path->nodes[0];
1486 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1487 item_offset = (unsigned long)iref - (unsigned long)ei;
1488
1489 type = extent_ref_type(parent, owner);
1490 size = btrfs_extent_inline_ref_size(type);
1491
1492 ret = btrfs_extend_item(trans, root, path, size);
1493 BUG_ON(ret);
1494
1495 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1496 refs = btrfs_extent_refs(leaf, ei);
1497 refs += refs_to_add;
1498 btrfs_set_extent_refs(leaf, ei, refs);
1499 if (extent_op)
1500 __run_delayed_extent_op(extent_op, leaf, ei);
1501
1502 ptr = (unsigned long)ei + item_offset;
1503 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1504 if (ptr < end - size)
1505 memmove_extent_buffer(leaf, ptr + size, ptr,
1506 end - size - ptr);
1507
1508 iref = (struct btrfs_extent_inline_ref *)ptr;
1509 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1510 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1511 struct btrfs_extent_data_ref *dref;
1512 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1513 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1514 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1515 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1516 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1517 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1518 struct btrfs_shared_data_ref *sref;
1519 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1520 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1521 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1522 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1523 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1524 } else {
1525 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1526 }
1527 btrfs_mark_buffer_dirty(leaf);
1528 return 0;
1529}
1530
1531static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1532 struct btrfs_root *root,
1533 struct btrfs_path *path,
1534 struct btrfs_extent_inline_ref **ref_ret,
1535 u64 bytenr, u64 num_bytes, u64 parent,
1536 u64 root_objectid, u64 owner, u64 offset)
1537{
1538 int ret;
1539
1540 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1541 bytenr, num_bytes, parent,
1542 root_objectid, owner, offset, 0);
1543 if (ret != -ENOENT)
1544 return ret;
1545
1546 btrfs_release_path(root, path);
1547 *ref_ret = NULL;
1548
1549 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1550 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1551 root_objectid);
1552 } else {
1553 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1554 root_objectid, owner, offset);
1555 }
1556 return ret;
1557}
1558
1559/*
1560 * helper to update/remove inline back ref
1561 */
1562static noinline_for_stack
1563int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1564 struct btrfs_root *root,
1565 struct btrfs_path *path,
1566 struct btrfs_extent_inline_ref *iref,
1567 int refs_to_mod,
1568 struct btrfs_delayed_extent_op *extent_op)
1569{
1570 struct extent_buffer *leaf;
1571 struct btrfs_extent_item *ei;
1572 struct btrfs_extent_data_ref *dref = NULL;
1573 struct btrfs_shared_data_ref *sref = NULL;
1574 unsigned long ptr;
1575 unsigned long end;
1576 u32 item_size;
1577 int size;
1578 int type;
1579 int ret;
1580 u64 refs;
1581
1582 leaf = path->nodes[0];
1583 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1584 refs = btrfs_extent_refs(leaf, ei);
1585 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1586 refs += refs_to_mod;
1587 btrfs_set_extent_refs(leaf, ei, refs);
1588 if (extent_op)
1589 __run_delayed_extent_op(extent_op, leaf, ei);
1590
1591 type = btrfs_extent_inline_ref_type(leaf, iref);
1592
1593 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1594 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1595 refs = btrfs_extent_data_ref_count(leaf, dref);
1596 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1597 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1598 refs = btrfs_shared_data_ref_count(leaf, sref);
1599 } else {
1600 refs = 1;
1601 BUG_ON(refs_to_mod != -1);
1602 }
1603
1604 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1605 refs += refs_to_mod;
1606
1607 if (refs > 0) {
1608 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1609 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1610 else
1611 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1612 } else {
1613 size = btrfs_extent_inline_ref_size(type);
1614 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1615 ptr = (unsigned long)iref;
1616 end = (unsigned long)ei + item_size;
1617 if (ptr + size < end)
1618 memmove_extent_buffer(leaf, ptr, ptr + size,
1619 end - ptr - size);
1620 item_size -= size;
1621 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1622 BUG_ON(ret);
1623 }
1624 btrfs_mark_buffer_dirty(leaf);
1625 return 0;
1626}
1627
1628static noinline_for_stack
1629int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1630 struct btrfs_root *root,
1631 struct btrfs_path *path,
1632 u64 bytenr, u64 num_bytes, u64 parent,
1633 u64 root_objectid, u64 owner,
1634 u64 offset, int refs_to_add,
1635 struct btrfs_delayed_extent_op *extent_op)
1636{
1637 struct btrfs_extent_inline_ref *iref;
1638 int ret;
1639
1640 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1641 bytenr, num_bytes, parent,
1642 root_objectid, owner, offset, 1);
1643 if (ret == 0) {
1644 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1645 ret = update_inline_extent_backref(trans, root, path, iref,
1646 refs_to_add, extent_op);
1647 } else if (ret == -ENOENT) {
1648 ret = setup_inline_extent_backref(trans, root, path, iref,
1649 parent, root_objectid,
1650 owner, offset, refs_to_add,
1651 extent_op);
1652 }
1653 return ret;
1654}
1655
1656static int insert_extent_backref(struct btrfs_trans_handle *trans,
1657 struct btrfs_root *root,
1658 struct btrfs_path *path,
1659 u64 bytenr, u64 parent, u64 root_objectid,
1660 u64 owner, u64 offset, int refs_to_add)
1661{
1662 int ret;
1663 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1664 BUG_ON(refs_to_add != 1);
1665 ret = insert_tree_block_ref(trans, root, path, bytenr,
1666 parent, root_objectid);
1667 } else {
1668 ret = insert_extent_data_ref(trans, root, path, bytenr,
1669 parent, root_objectid,
1670 owner, offset, refs_to_add);
1671 }
1672 return ret;
1673}
1674
1675static int remove_extent_backref(struct btrfs_trans_handle *trans,
1676 struct btrfs_root *root,
1677 struct btrfs_path *path,
1678 struct btrfs_extent_inline_ref *iref,
1679 int refs_to_drop, int is_data)
1680{
1681 int ret;
1682
1683 BUG_ON(!is_data && refs_to_drop != 1);
1684 if (iref) {
1685 ret = update_inline_extent_backref(trans, root, path, iref,
1686 -refs_to_drop, NULL);
1687 } else if (is_data) {
1688 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1689 } else {
1690 ret = btrfs_del_item(trans, root, path);
1691 }
1692 return ret;
1693}
1694
Chris Mason15916de2008-11-19 21:17:22 -05001695static void btrfs_issue_discard(struct block_device *bdev,
1696 u64 start, u64 len)
1697{
Christoph Hellwig746cd1e2009-09-12 07:35:43 +02001698 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +04001699 BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
Chris Mason15916de2008-11-19 21:17:22 -05001700}
Chris Mason15916de2008-11-19 21:17:22 -05001701
Liu Hui1f3c79a2009-01-05 15:57:51 -05001702static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1703 u64 num_bytes)
1704{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001705 int ret;
1706 u64 map_length = num_bytes;
1707 struct btrfs_multi_bio *multi = NULL;
1708
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001709 if (!btrfs_test_opt(root, DISCARD))
1710 return 0;
1711
Liu Hui1f3c79a2009-01-05 15:57:51 -05001712 /* Tell the block device(s) that the sectors can be discarded */
1713 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1714 bytenr, &map_length, &multi, 0);
1715 if (!ret) {
1716 struct btrfs_bio_stripe *stripe = multi->stripes;
1717 int i;
1718
1719 if (map_length > num_bytes)
1720 map_length = num_bytes;
1721
1722 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1723 btrfs_issue_discard(stripe->dev->bdev,
1724 stripe->physical,
1725 map_length);
1726 }
1727 kfree(multi);
1728 }
1729
1730 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001731}
1732
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001733int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1734 struct btrfs_root *root,
1735 u64 bytenr, u64 num_bytes, u64 parent,
1736 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001737{
1738 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001739 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1740 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001741
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001742 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1743 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1744 parent, root_objectid, (int)owner,
1745 BTRFS_ADD_DELAYED_REF, NULL);
1746 } else {
1747 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1748 parent, root_objectid, owner, offset,
1749 BTRFS_ADD_DELAYED_REF, NULL);
1750 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001751 return ret;
1752}
1753
Chris Mason925baed2008-06-25 16:01:30 -04001754static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001755 struct btrfs_root *root,
1756 u64 bytenr, u64 num_bytes,
1757 u64 parent, u64 root_objectid,
1758 u64 owner, u64 offset, int refs_to_add,
1759 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001760{
Chris Mason5caf2a02007-04-02 11:20:42 -04001761 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001762 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001763 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001764 u64 refs;
1765 int ret;
1766 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001767
Chris Mason5caf2a02007-04-02 11:20:42 -04001768 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001769 if (!path)
1770 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001771
Chris Mason3c12ac72008-04-21 12:01:38 -04001772 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001773 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001774 /* this will setup the path even if it fails to insert the back ref */
1775 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1776 path, bytenr, num_bytes, parent,
1777 root_objectid, owner, offset,
1778 refs_to_add, extent_op);
1779 if (ret == 0)
1780 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001781
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001782 if (ret != -EAGAIN) {
1783 err = ret;
1784 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001785 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001786
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001787 leaf = path->nodes[0];
1788 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1789 refs = btrfs_extent_refs(leaf, item);
1790 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1791 if (extent_op)
1792 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001793
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001794 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001795 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001796
Chris Mason3c12ac72008-04-21 12:01:38 -04001797 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001798 path->leave_spinning = 1;
1799
Chris Mason56bec292009-03-13 10:10:06 -04001800 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001801 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001802 path, bytenr, parent, root_objectid,
1803 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001804 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001805out:
Chris Mason74493f72007-12-11 09:25:06 -05001806 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001807 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001808}
1809
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001810static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1811 struct btrfs_root *root,
1812 struct btrfs_delayed_ref_node *node,
1813 struct btrfs_delayed_extent_op *extent_op,
1814 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001815{
Chris Mason56bec292009-03-13 10:10:06 -04001816 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001817 struct btrfs_delayed_data_ref *ref;
1818 struct btrfs_key ins;
1819 u64 parent = 0;
1820 u64 ref_root = 0;
1821 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001822
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001823 ins.objectid = node->bytenr;
1824 ins.offset = node->num_bytes;
1825 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001826
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001827 ref = btrfs_delayed_node_to_data_ref(node);
1828 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1829 parent = ref->parent;
1830 else
1831 ref_root = ref->root;
1832
1833 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1834 if (extent_op) {
1835 BUG_ON(extent_op->update_key);
1836 flags |= extent_op->flags_to_set;
1837 }
1838 ret = alloc_reserved_file_extent(trans, root,
1839 parent, ref_root, flags,
1840 ref->objectid, ref->offset,
1841 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001842 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1843 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1844 node->num_bytes, parent,
1845 ref_root, ref->objectid,
1846 ref->offset, node->ref_mod,
1847 extent_op);
1848 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1849 ret = __btrfs_free_extent(trans, root, node->bytenr,
1850 node->num_bytes, parent,
1851 ref_root, ref->objectid,
1852 ref->offset, node->ref_mod,
1853 extent_op);
1854 } else {
1855 BUG();
1856 }
Chris Mason56bec292009-03-13 10:10:06 -04001857 return ret;
1858}
1859
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001860static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1861 struct extent_buffer *leaf,
1862 struct btrfs_extent_item *ei)
1863{
1864 u64 flags = btrfs_extent_flags(leaf, ei);
1865 if (extent_op->update_flags) {
1866 flags |= extent_op->flags_to_set;
1867 btrfs_set_extent_flags(leaf, ei, flags);
1868 }
1869
1870 if (extent_op->update_key) {
1871 struct btrfs_tree_block_info *bi;
1872 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1873 bi = (struct btrfs_tree_block_info *)(ei + 1);
1874 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1875 }
1876}
1877
1878static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1879 struct btrfs_root *root,
1880 struct btrfs_delayed_ref_node *node,
1881 struct btrfs_delayed_extent_op *extent_op)
1882{
1883 struct btrfs_key key;
1884 struct btrfs_path *path;
1885 struct btrfs_extent_item *ei;
1886 struct extent_buffer *leaf;
1887 u32 item_size;
1888 int ret;
1889 int err = 0;
1890
1891 path = btrfs_alloc_path();
1892 if (!path)
1893 return -ENOMEM;
1894
1895 key.objectid = node->bytenr;
1896 key.type = BTRFS_EXTENT_ITEM_KEY;
1897 key.offset = node->num_bytes;
1898
1899 path->reada = 1;
1900 path->leave_spinning = 1;
1901 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1902 path, 0, 1);
1903 if (ret < 0) {
1904 err = ret;
1905 goto out;
1906 }
1907 if (ret > 0) {
1908 err = -EIO;
1909 goto out;
1910 }
1911
1912 leaf = path->nodes[0];
1913 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1914#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1915 if (item_size < sizeof(*ei)) {
1916 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1917 path, (u64)-1, 0);
1918 if (ret < 0) {
1919 err = ret;
1920 goto out;
1921 }
1922 leaf = path->nodes[0];
1923 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1924 }
1925#endif
1926 BUG_ON(item_size < sizeof(*ei));
1927 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1928 __run_delayed_extent_op(extent_op, leaf, ei);
1929
1930 btrfs_mark_buffer_dirty(leaf);
1931out:
1932 btrfs_free_path(path);
1933 return err;
1934}
1935
1936static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1937 struct btrfs_root *root,
1938 struct btrfs_delayed_ref_node *node,
1939 struct btrfs_delayed_extent_op *extent_op,
1940 int insert_reserved)
1941{
1942 int ret = 0;
1943 struct btrfs_delayed_tree_ref *ref;
1944 struct btrfs_key ins;
1945 u64 parent = 0;
1946 u64 ref_root = 0;
1947
1948 ins.objectid = node->bytenr;
1949 ins.offset = node->num_bytes;
1950 ins.type = BTRFS_EXTENT_ITEM_KEY;
1951
1952 ref = btrfs_delayed_node_to_tree_ref(node);
1953 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1954 parent = ref->parent;
1955 else
1956 ref_root = ref->root;
1957
1958 BUG_ON(node->ref_mod != 1);
1959 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1960 BUG_ON(!extent_op || !extent_op->update_flags ||
1961 !extent_op->update_key);
1962 ret = alloc_reserved_tree_block(trans, root,
1963 parent, ref_root,
1964 extent_op->flags_to_set,
1965 &extent_op->key,
1966 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001967 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1968 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1969 node->num_bytes, parent, ref_root,
1970 ref->level, 0, 1, extent_op);
1971 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1972 ret = __btrfs_free_extent(trans, root, node->bytenr,
1973 node->num_bytes, parent, ref_root,
1974 ref->level, 0, 1, extent_op);
1975 } else {
1976 BUG();
1977 }
1978 return ret;
1979}
1980
Chris Mason56bec292009-03-13 10:10:06 -04001981/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001982static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1983 struct btrfs_root *root,
1984 struct btrfs_delayed_ref_node *node,
1985 struct btrfs_delayed_extent_op *extent_op,
1986 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001987{
Josef Bacikeb099672009-02-12 09:27:38 -05001988 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001989 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001990 struct btrfs_delayed_ref_head *head;
1991 /*
1992 * we've hit the end of the chain and we were supposed
1993 * to insert this extent into the tree. But, it got
1994 * deleted before we ever needed to insert it, so all
1995 * we have to do is clean up the accounting
1996 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001997 BUG_ON(extent_op);
1998 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001999 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002000 btrfs_pin_extent(root, node->bytenr,
2001 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002002 if (head->is_data) {
2003 ret = btrfs_del_csums(trans, root,
2004 node->bytenr,
2005 node->num_bytes);
2006 BUG_ON(ret);
2007 }
Chris Mason56bec292009-03-13 10:10:06 -04002008 }
Chris Mason56bec292009-03-13 10:10:06 -04002009 mutex_unlock(&head->mutex);
2010 return 0;
2011 }
Josef Bacikeb099672009-02-12 09:27:38 -05002012
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002013 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2014 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2015 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2016 insert_reserved);
2017 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2018 node->type == BTRFS_SHARED_DATA_REF_KEY)
2019 ret = run_delayed_data_ref(trans, root, node, extent_op,
2020 insert_reserved);
2021 else
2022 BUG();
2023 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002024}
2025
Chris Mason56bec292009-03-13 10:10:06 -04002026static noinline struct btrfs_delayed_ref_node *
2027select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002028{
Chris Mason56bec292009-03-13 10:10:06 -04002029 struct rb_node *node;
2030 struct btrfs_delayed_ref_node *ref;
2031 int action = BTRFS_ADD_DELAYED_REF;
2032again:
2033 /*
2034 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2035 * this prevents ref count from going down to zero when
2036 * there still are pending delayed ref.
2037 */
2038 node = rb_prev(&head->node.rb_node);
2039 while (1) {
2040 if (!node)
2041 break;
2042 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2043 rb_node);
2044 if (ref->bytenr != head->node.bytenr)
2045 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002046 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002047 return ref;
2048 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002049 }
Chris Mason56bec292009-03-13 10:10:06 -04002050 if (action == BTRFS_ADD_DELAYED_REF) {
2051 action = BTRFS_DROP_DELAYED_REF;
2052 goto again;
2053 }
2054 return NULL;
2055}
2056
Chris Masonc3e69d52009-03-13 10:17:05 -04002057static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2058 struct btrfs_root *root,
2059 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002060{
Chris Mason56bec292009-03-13 10:10:06 -04002061 struct btrfs_delayed_ref_root *delayed_refs;
2062 struct btrfs_delayed_ref_node *ref;
2063 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002065 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002066 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002067 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002068
2069 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002070 while (1) {
2071 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002072 /* pick a new head ref from the cluster list */
2073 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002074 break;
Chris Mason56bec292009-03-13 10:10:06 -04002075
Chris Masonc3e69d52009-03-13 10:17:05 -04002076 locked_ref = list_entry(cluster->next,
2077 struct btrfs_delayed_ref_head, cluster);
2078
2079 /* grab the lock that says we are going to process
2080 * all the refs for this head */
2081 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2082
2083 /*
2084 * we may have dropped the spin lock to get the head
2085 * mutex lock, and that might have given someone else
2086 * time to free the head. If that's true, it has been
2087 * removed from our list and we can move on.
2088 */
2089 if (ret == -EAGAIN) {
2090 locked_ref = NULL;
2091 count++;
2092 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002093 }
2094 }
2095
2096 /*
2097 * record the must insert reserved flag before we
2098 * drop the spin lock.
2099 */
2100 must_insert_reserved = locked_ref->must_insert_reserved;
2101 locked_ref->must_insert_reserved = 0;
2102
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002103 extent_op = locked_ref->extent_op;
2104 locked_ref->extent_op = NULL;
2105
Chris Mason56bec292009-03-13 10:10:06 -04002106 /*
2107 * locked_ref is the head node, so we have to go one
2108 * node back for any delayed ref updates
2109 */
Chris Mason56bec292009-03-13 10:10:06 -04002110 ref = select_delayed_ref(locked_ref);
2111 if (!ref) {
2112 /* All delayed refs have been processed, Go ahead
2113 * and send the head node to run_one_delayed_ref,
2114 * so that any accounting fixes can happen
2115 */
2116 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002117
2118 if (extent_op && must_insert_reserved) {
2119 kfree(extent_op);
2120 extent_op = NULL;
2121 }
2122
2123 if (extent_op) {
2124 spin_unlock(&delayed_refs->lock);
2125
2126 ret = run_delayed_extent_op(trans, root,
2127 ref, extent_op);
2128 BUG_ON(ret);
2129 kfree(extent_op);
2130
2131 cond_resched();
2132 spin_lock(&delayed_refs->lock);
2133 continue;
2134 }
2135
Chris Masonc3e69d52009-03-13 10:17:05 -04002136 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002137 locked_ref = NULL;
2138 }
2139
2140 ref->in_tree = 0;
2141 rb_erase(&ref->rb_node, &delayed_refs->root);
2142 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002143
Chris Mason56bec292009-03-13 10:10:06 -04002144 spin_unlock(&delayed_refs->lock);
2145
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002146 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002147 must_insert_reserved);
2148 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002149
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002150 btrfs_put_delayed_ref(ref);
2151 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002152 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002153
Chris Mason1887be62009-03-13 10:11:24 -04002154 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002155 spin_lock(&delayed_refs->lock);
2156 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002157 return count;
2158}
2159
2160/*
2161 * this starts processing the delayed reference count updates and
2162 * extent insertions we have queued up so far. count can be
2163 * 0, which means to process everything in the tree at the start
2164 * of the run (but not newly added entries), or it can be some target
2165 * number you'd like to process.
2166 */
2167int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2168 struct btrfs_root *root, unsigned long count)
2169{
2170 struct rb_node *node;
2171 struct btrfs_delayed_ref_root *delayed_refs;
2172 struct btrfs_delayed_ref_node *ref;
2173 struct list_head cluster;
2174 int ret;
2175 int run_all = count == (unsigned long)-1;
2176 int run_most = 0;
2177
2178 if (root == root->fs_info->extent_root)
2179 root = root->fs_info->tree_root;
2180
2181 delayed_refs = &trans->transaction->delayed_refs;
2182 INIT_LIST_HEAD(&cluster);
2183again:
2184 spin_lock(&delayed_refs->lock);
2185 if (count == 0) {
2186 count = delayed_refs->num_entries * 2;
2187 run_most = 1;
2188 }
2189 while (1) {
2190 if (!(run_all || run_most) &&
2191 delayed_refs->num_heads_ready < 64)
2192 break;
2193
2194 /*
2195 * go find something we can process in the rbtree. We start at
2196 * the beginning of the tree, and then build a cluster
2197 * of refs to process starting at the first one we are able to
2198 * lock
2199 */
2200 ret = btrfs_find_ref_cluster(trans, &cluster,
2201 delayed_refs->run_delayed_start);
2202 if (ret)
2203 break;
2204
2205 ret = run_clustered_refs(trans, root, &cluster);
2206 BUG_ON(ret < 0);
2207
2208 count -= min_t(unsigned long, ret, count);
2209
2210 if (count == 0)
2211 break;
2212 }
2213
Chris Mason56bec292009-03-13 10:10:06 -04002214 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002215 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002216 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002217 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002218 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002219
2220 while (node) {
2221 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2222 rb_node);
2223 if (btrfs_delayed_ref_is_head(ref)) {
2224 struct btrfs_delayed_ref_head *head;
2225
2226 head = btrfs_delayed_node_to_head(ref);
2227 atomic_inc(&ref->refs);
2228
2229 spin_unlock(&delayed_refs->lock);
2230 mutex_lock(&head->mutex);
2231 mutex_unlock(&head->mutex);
2232
2233 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002234 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002235 goto again;
2236 }
2237 node = rb_next(node);
2238 }
2239 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002240 schedule_timeout(1);
2241 goto again;
2242 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002243out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002244 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002245 return 0;
2246}
2247
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002248int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2249 struct btrfs_root *root,
2250 u64 bytenr, u64 num_bytes, u64 flags,
2251 int is_data)
2252{
2253 struct btrfs_delayed_extent_op *extent_op;
2254 int ret;
2255
2256 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2257 if (!extent_op)
2258 return -ENOMEM;
2259
2260 extent_op->flags_to_set = flags;
2261 extent_op->update_flags = 1;
2262 extent_op->update_key = 0;
2263 extent_op->is_data = is_data ? 1 : 0;
2264
2265 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2266 if (ret)
2267 kfree(extent_op);
2268 return ret;
2269}
2270
2271static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2272 struct btrfs_root *root,
2273 struct btrfs_path *path,
2274 u64 objectid, u64 offset, u64 bytenr)
2275{
2276 struct btrfs_delayed_ref_head *head;
2277 struct btrfs_delayed_ref_node *ref;
2278 struct btrfs_delayed_data_ref *data_ref;
2279 struct btrfs_delayed_ref_root *delayed_refs;
2280 struct rb_node *node;
2281 int ret = 0;
2282
2283 ret = -ENOENT;
2284 delayed_refs = &trans->transaction->delayed_refs;
2285 spin_lock(&delayed_refs->lock);
2286 head = btrfs_find_delayed_ref_head(trans, bytenr);
2287 if (!head)
2288 goto out;
2289
2290 if (!mutex_trylock(&head->mutex)) {
2291 atomic_inc(&head->node.refs);
2292 spin_unlock(&delayed_refs->lock);
2293
2294 btrfs_release_path(root->fs_info->extent_root, path);
2295
2296 mutex_lock(&head->mutex);
2297 mutex_unlock(&head->mutex);
2298 btrfs_put_delayed_ref(&head->node);
2299 return -EAGAIN;
2300 }
2301
2302 node = rb_prev(&head->node.rb_node);
2303 if (!node)
2304 goto out_unlock;
2305
2306 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2307
2308 if (ref->bytenr != bytenr)
2309 goto out_unlock;
2310
2311 ret = 1;
2312 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2313 goto out_unlock;
2314
2315 data_ref = btrfs_delayed_node_to_data_ref(ref);
2316
2317 node = rb_prev(node);
2318 if (node) {
2319 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2320 if (ref->bytenr == bytenr)
2321 goto out_unlock;
2322 }
2323
2324 if (data_ref->root != root->root_key.objectid ||
2325 data_ref->objectid != objectid || data_ref->offset != offset)
2326 goto out_unlock;
2327
2328 ret = 0;
2329out_unlock:
2330 mutex_unlock(&head->mutex);
2331out:
2332 spin_unlock(&delayed_refs->lock);
2333 return ret;
2334}
2335
2336static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2337 struct btrfs_root *root,
2338 struct btrfs_path *path,
2339 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002340{
2341 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002342 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002343 struct btrfs_extent_data_ref *ref;
2344 struct btrfs_extent_inline_ref *iref;
2345 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002346 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002347 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002348 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002349
Chris Masonbe20aa92007-12-17 20:14:01 -05002350 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002351 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002352 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002353
Chris Masonbe20aa92007-12-17 20:14:01 -05002354 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2355 if (ret < 0)
2356 goto out;
2357 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002358
2359 ret = -ENOENT;
2360 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002361 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002362
Zheng Yan31840ae2008-09-23 13:14:14 -04002363 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002364 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002365 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002366
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002367 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002368 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002369
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002370 ret = 1;
2371 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2372#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2373 if (item_size < sizeof(*ei)) {
2374 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2375 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002376 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002377#endif
2378 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2379
2380 if (item_size != sizeof(*ei) +
2381 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2382 goto out;
2383
2384 if (btrfs_extent_generation(leaf, ei) <=
2385 btrfs_root_last_snapshot(&root->root_item))
2386 goto out;
2387
2388 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2389 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2390 BTRFS_EXTENT_DATA_REF_KEY)
2391 goto out;
2392
2393 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2394 if (btrfs_extent_refs(leaf, ei) !=
2395 btrfs_extent_data_ref_count(leaf, ref) ||
2396 btrfs_extent_data_ref_root(leaf, ref) !=
2397 root->root_key.objectid ||
2398 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2399 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2400 goto out;
2401
Yan Zhengf321e492008-07-30 09:26:11 -04002402 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002403out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002404 return ret;
2405}
2406
2407int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2408 struct btrfs_root *root,
2409 u64 objectid, u64 offset, u64 bytenr)
2410{
2411 struct btrfs_path *path;
2412 int ret;
2413 int ret2;
2414
2415 path = btrfs_alloc_path();
2416 if (!path)
2417 return -ENOENT;
2418
2419 do {
2420 ret = check_committed_ref(trans, root, path, objectid,
2421 offset, bytenr);
2422 if (ret && ret != -ENOENT)
2423 goto out;
2424
2425 ret2 = check_delayed_ref(trans, root, path, objectid,
2426 offset, bytenr);
2427 } while (ret2 == -EAGAIN);
2428
2429 if (ret2 && ret2 != -ENOENT) {
2430 ret = ret2;
2431 goto out;
2432 }
2433
2434 if (ret != -ENOENT || ret2 != -ENOENT)
2435 ret = 0;
2436out:
Yan Zhengf321e492008-07-30 09:26:11 -04002437 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002438 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2439 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002440 return ret;
2441}
2442
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002443#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002444int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2445 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002446{
Chris Mason5f39d392007-10-15 16:14:19 -04002447 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002448 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002449 u64 root_gen;
2450 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002451 int i;
Chris Masondb945352007-10-15 16:15:53 -04002452 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002453 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002454 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002455
Chris Mason3768f362007-03-13 16:47:54 -04002456 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002457 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002458
Zheng Yane4657682008-09-26 10:04:53 -04002459 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2460 shared = 0;
2461 root_gen = root->root_key.offset;
2462 } else {
2463 shared = 1;
2464 root_gen = trans->transid - 1;
2465 }
2466
Chris Masondb945352007-10-15 16:15:53 -04002467 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002468 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002469
Zheng Yan31840ae2008-09-23 13:14:14 -04002470 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002471 struct btrfs_leaf_ref *ref;
2472 struct btrfs_extent_info *info;
2473
Zheng Yan31840ae2008-09-23 13:14:14 -04002474 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002475 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002476 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002477 goto out;
2478 }
2479
Zheng Yane4657682008-09-26 10:04:53 -04002480 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002481 ref->bytenr = buf->start;
2482 ref->owner = btrfs_header_owner(buf);
2483 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002484 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002485 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002486
Zheng Yan31840ae2008-09-23 13:14:14 -04002487 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002488 u64 disk_bytenr;
2489 btrfs_item_key_to_cpu(buf, &key, i);
2490 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2491 continue;
2492 fi = btrfs_item_ptr(buf, i,
2493 struct btrfs_file_extent_item);
2494 if (btrfs_file_extent_type(buf, fi) ==
2495 BTRFS_FILE_EXTENT_INLINE)
2496 continue;
2497 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2498 if (disk_bytenr == 0)
2499 continue;
2500
2501 info->bytenr = disk_bytenr;
2502 info->num_bytes =
2503 btrfs_file_extent_disk_num_bytes(buf, fi);
2504 info->objectid = key.objectid;
2505 info->offset = key.offset;
2506 info++;
2507 }
2508
Zheng Yane4657682008-09-26 10:04:53 -04002509 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002510 if (ret == -EEXIST && shared) {
2511 struct btrfs_leaf_ref *old;
2512 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2513 BUG_ON(!old);
2514 btrfs_remove_leaf_ref(root, old);
2515 btrfs_free_leaf_ref(root, old);
2516 ret = btrfs_add_leaf_ref(root, ref, shared);
2517 }
Yan Zheng31153d82008-07-28 15:32:19 -04002518 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002519 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002520 }
2521out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002522 return ret;
2523}
2524
Chris Masonb7a9f292009-02-04 09:23:45 -05002525/* when a block goes through cow, we update the reference counts of
2526 * everything that block points to. The internal pointers of the block
2527 * can be in just about any order, and it is likely to have clusters of
2528 * things that are close together and clusters of things that are not.
2529 *
2530 * To help reduce the seeks that come with updating all of these reference
2531 * counts, sort them by byte number before actual updates are done.
2532 *
2533 * struct refsort is used to match byte number to slot in the btree block.
2534 * we sort based on the byte number and then use the slot to actually
2535 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002536 *
2537 * struct refsort is smaller than strcut btrfs_item and smaller than
2538 * struct btrfs_key_ptr. Since we're currently limited to the page size
2539 * for a btree block, there's no way for a kmalloc of refsorts for a
2540 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002541 */
2542struct refsort {
2543 u64 bytenr;
2544 u32 slot;
2545};
2546
2547/*
2548 * for passing into sort()
2549 */
2550static int refsort_cmp(const void *a_void, const void *b_void)
2551{
2552 const struct refsort *a = a_void;
2553 const struct refsort *b = b_void;
2554
2555 if (a->bytenr < b->bytenr)
2556 return -1;
2557 if (a->bytenr > b->bytenr)
2558 return 1;
2559 return 0;
2560}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002561#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002562
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002563static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002564 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002565 struct extent_buffer *buf,
2566 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002567{
2568 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002569 u64 num_bytes;
2570 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002571 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002572 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002573 struct btrfs_key key;
2574 struct btrfs_file_extent_item *fi;
2575 int i;
2576 int level;
2577 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002578 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002579 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002580
2581 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002582 nritems = btrfs_header_nritems(buf);
2583 level = btrfs_header_level(buf);
2584
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002585 if (!root->ref_cows && level == 0)
2586 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002587
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002588 if (inc)
2589 process_func = btrfs_inc_extent_ref;
2590 else
2591 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002592
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002593 if (full_backref)
2594 parent = buf->start;
2595 else
2596 parent = 0;
2597
Zheng Yan31840ae2008-09-23 13:14:14 -04002598 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002599 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002600 btrfs_item_key_to_cpu(buf, &key, i);
2601 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002602 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002603 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002604 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002605 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002606 BTRFS_FILE_EXTENT_INLINE)
2607 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002608 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2609 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002610 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002611
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002612 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2613 key.offset -= btrfs_file_extent_offset(buf, fi);
2614 ret = process_func(trans, root, bytenr, num_bytes,
2615 parent, ref_root, key.objectid,
2616 key.offset);
2617 if (ret)
2618 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002619 } else {
2620 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002621 num_bytes = btrfs_level_size(root, level - 1);
2622 ret = process_func(trans, root, bytenr, num_bytes,
2623 parent, ref_root, level - 1, 0);
2624 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002625 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002626 }
2627 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002628 return 0;
2629fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002630 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002631 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002632}
2633
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002634int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2635 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002636{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002637 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2638}
Zheng Yan31840ae2008-09-23 13:14:14 -04002639
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002640int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2641 struct extent_buffer *buf, int full_backref)
2642{
2643 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002644}
2645
Chris Mason9078a3e2007-04-26 16:46:15 -04002646static int write_one_cache_group(struct btrfs_trans_handle *trans,
2647 struct btrfs_root *root,
2648 struct btrfs_path *path,
2649 struct btrfs_block_group_cache *cache)
2650{
2651 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002652 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002653 unsigned long bi;
2654 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002655
Chris Mason9078a3e2007-04-26 16:46:15 -04002656 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002657 if (ret < 0)
2658 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002659 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002660
2661 leaf = path->nodes[0];
2662 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2663 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2664 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002665 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002666fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002667 if (ret)
2668 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002669 return 0;
2670
2671}
2672
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002673static struct btrfs_block_group_cache *
2674next_block_group(struct btrfs_root *root,
2675 struct btrfs_block_group_cache *cache)
2676{
2677 struct rb_node *node;
2678 spin_lock(&root->fs_info->block_group_cache_lock);
2679 node = rb_next(&cache->cache_node);
2680 btrfs_put_block_group(cache);
2681 if (node) {
2682 cache = rb_entry(node, struct btrfs_block_group_cache,
2683 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002684 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002685 } else
2686 cache = NULL;
2687 spin_unlock(&root->fs_info->block_group_cache_lock);
2688 return cache;
2689}
2690
Josef Bacik0af3d002010-06-21 14:48:16 -04002691static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2692 struct btrfs_trans_handle *trans,
2693 struct btrfs_path *path)
2694{
2695 struct btrfs_root *root = block_group->fs_info->tree_root;
2696 struct inode *inode = NULL;
2697 u64 alloc_hint = 0;
2698 int num_pages = 0;
2699 int retries = 0;
2700 int ret = 0;
2701
2702 /*
2703 * If this block group is smaller than 100 megs don't bother caching the
2704 * block group.
2705 */
2706 if (block_group->key.offset < (100 * 1024 * 1024)) {
2707 spin_lock(&block_group->lock);
2708 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2709 spin_unlock(&block_group->lock);
2710 return 0;
2711 }
2712
2713again:
2714 inode = lookup_free_space_inode(root, block_group, path);
2715 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2716 ret = PTR_ERR(inode);
2717 btrfs_release_path(root, path);
2718 goto out;
2719 }
2720
2721 if (IS_ERR(inode)) {
2722 BUG_ON(retries);
2723 retries++;
2724
2725 if (block_group->ro)
2726 goto out_free;
2727
2728 ret = create_free_space_inode(root, trans, block_group, path);
2729 if (ret)
2730 goto out_free;
2731 goto again;
2732 }
2733
2734 /*
2735 * We want to set the generation to 0, that way if anything goes wrong
2736 * from here on out we know not to trust this cache when we load up next
2737 * time.
2738 */
2739 BTRFS_I(inode)->generation = 0;
2740 ret = btrfs_update_inode(trans, root, inode);
2741 WARN_ON(ret);
2742
2743 if (i_size_read(inode) > 0) {
2744 ret = btrfs_truncate_free_space_cache(root, trans, path,
2745 inode);
2746 if (ret)
2747 goto out_put;
2748 }
2749
2750 spin_lock(&block_group->lock);
2751 if (block_group->cached != BTRFS_CACHE_FINISHED) {
2752 spin_unlock(&block_group->lock);
2753 goto out_put;
2754 }
2755 spin_unlock(&block_group->lock);
2756
2757 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2758 if (!num_pages)
2759 num_pages = 1;
2760
2761 /*
2762 * Just to make absolutely sure we have enough space, we're going to
2763 * preallocate 12 pages worth of space for each block group. In
2764 * practice we ought to use at most 8, but we need extra space so we can
2765 * add our header and have a terminator between the extents and the
2766 * bitmaps.
2767 */
2768 num_pages *= 16;
2769 num_pages *= PAGE_CACHE_SIZE;
2770
2771 ret = btrfs_check_data_free_space(inode, num_pages);
2772 if (ret)
2773 goto out_put;
2774
2775 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2776 num_pages, num_pages,
2777 &alloc_hint);
2778 btrfs_free_reserved_data_space(inode, num_pages);
2779out_put:
2780 iput(inode);
2781out_free:
2782 btrfs_release_path(root, path);
2783out:
2784 spin_lock(&block_group->lock);
2785 if (ret)
2786 block_group->disk_cache_state = BTRFS_DC_ERROR;
2787 else
2788 block_group->disk_cache_state = BTRFS_DC_SETUP;
2789 spin_unlock(&block_group->lock);
2790
2791 return ret;
2792}
2793
Chris Mason96b51792007-10-15 16:15:19 -04002794int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2795 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002796{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002797 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002798 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002799 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002800 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002801
2802 path = btrfs_alloc_path();
2803 if (!path)
2804 return -ENOMEM;
2805
Josef Bacik0af3d002010-06-21 14:48:16 -04002806again:
2807 while (1) {
2808 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2809 while (cache) {
2810 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2811 break;
2812 cache = next_block_group(root, cache);
2813 }
2814 if (!cache) {
2815 if (last == 0)
2816 break;
2817 last = 0;
2818 continue;
2819 }
2820 err = cache_save_setup(cache, trans, path);
2821 last = cache->key.objectid + cache->key.offset;
2822 btrfs_put_block_group(cache);
2823 }
2824
Chris Masond3977122009-01-05 21:25:51 -05002825 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002826 if (last == 0) {
2827 err = btrfs_run_delayed_refs(trans, root,
2828 (unsigned long)-1);
2829 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002830 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002831
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002832 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2833 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002834 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2835 btrfs_put_block_group(cache);
2836 goto again;
2837 }
2838
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002839 if (cache->dirty)
2840 break;
2841 cache = next_block_group(root, cache);
2842 }
2843 if (!cache) {
2844 if (last == 0)
2845 break;
2846 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002847 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002848 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002849
Josef Bacik0cb59c92010-07-02 12:14:14 -04002850 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2851 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002852 cache->dirty = 0;
2853 last = cache->key.objectid + cache->key.offset;
2854
2855 err = write_one_cache_group(trans, root, path, cache);
2856 BUG_ON(err);
2857 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002858 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002859
Josef Bacik0cb59c92010-07-02 12:14:14 -04002860 while (1) {
2861 /*
2862 * I don't think this is needed since we're just marking our
2863 * preallocated extent as written, but just in case it can't
2864 * hurt.
2865 */
2866 if (last == 0) {
2867 err = btrfs_run_delayed_refs(trans, root,
2868 (unsigned long)-1);
2869 BUG_ON(err);
2870 }
2871
2872 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2873 while (cache) {
2874 /*
2875 * Really this shouldn't happen, but it could if we
2876 * couldn't write the entire preallocated extent and
2877 * splitting the extent resulted in a new block.
2878 */
2879 if (cache->dirty) {
2880 btrfs_put_block_group(cache);
2881 goto again;
2882 }
2883 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2884 break;
2885 cache = next_block_group(root, cache);
2886 }
2887 if (!cache) {
2888 if (last == 0)
2889 break;
2890 last = 0;
2891 continue;
2892 }
2893
2894 btrfs_write_out_cache(root, trans, cache, path);
2895
2896 /*
2897 * If we didn't have an error then the cache state is still
2898 * NEED_WRITE, so we can set it to WRITTEN.
2899 */
2900 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2901 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2902 last = cache->key.objectid + cache->key.offset;
2903 btrfs_put_block_group(cache);
2904 }
2905
Chris Mason9078a3e2007-04-26 16:46:15 -04002906 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002907 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002908}
2909
Yan Zhengd2fb3432008-12-11 16:30:39 -05002910int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2911{
2912 struct btrfs_block_group_cache *block_group;
2913 int readonly = 0;
2914
2915 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2916 if (!block_group || block_group->ro)
2917 readonly = 1;
2918 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002919 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002920 return readonly;
2921}
2922
Chris Mason593060d2008-03-25 16:50:33 -04002923static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2924 u64 total_bytes, u64 bytes_used,
2925 struct btrfs_space_info **space_info)
2926{
2927 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002928 int i;
2929 int factor;
2930
2931 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2932 BTRFS_BLOCK_GROUP_RAID10))
2933 factor = 2;
2934 else
2935 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002936
2937 found = __find_space_info(info, flags);
2938 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002939 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002940 found->total_bytes += total_bytes;
2941 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002942 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002943 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002944 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002945 *space_info = found;
2946 return 0;
2947 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002948 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002949 if (!found)
2950 return -ENOMEM;
2951
Yan, Zhengb742bb82010-05-16 10:46:24 -04002952 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2953 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04002954 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002955 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04002956 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2957 BTRFS_BLOCK_GROUP_SYSTEM |
2958 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04002959 found->total_bytes = total_bytes;
2960 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002961 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002962 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002963 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002964 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002965 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002966 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002967 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002968 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002969 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002970 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002971 return 0;
2972}
2973
Chris Mason8790d502008-04-03 16:29:03 -04002974static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2975{
2976 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002977 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002978 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002979 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002980 if (extra_flags) {
2981 if (flags & BTRFS_BLOCK_GROUP_DATA)
2982 fs_info->avail_data_alloc_bits |= extra_flags;
2983 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2984 fs_info->avail_metadata_alloc_bits |= extra_flags;
2985 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2986 fs_info->avail_system_alloc_bits |= extra_flags;
2987 }
2988}
Chris Mason593060d2008-03-25 16:50:33 -04002989
Yan Zheng2b820322008-11-17 21:11:30 -05002990u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002991{
Yan Zheng2b820322008-11-17 21:11:30 -05002992 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002993
2994 if (num_devices == 1)
2995 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2996 if (num_devices < 4)
2997 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2998
Chris Masonec44a352008-04-28 15:29:52 -04002999 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3000 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003001 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003002 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003003 }
Chris Masonec44a352008-04-28 15:29:52 -04003004
3005 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003006 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003007 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003008 }
Chris Masonec44a352008-04-28 15:29:52 -04003009
3010 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3011 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3012 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3013 (flags & BTRFS_BLOCK_GROUP_DUP)))
3014 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3015 return flags;
3016}
3017
Yan, Zhengb742bb82010-05-16 10:46:24 -04003018static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003019{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003020 if (flags & BTRFS_BLOCK_GROUP_DATA)
3021 flags |= root->fs_info->avail_data_alloc_bits &
3022 root->fs_info->data_alloc_profile;
3023 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3024 flags |= root->fs_info->avail_system_alloc_bits &
3025 root->fs_info->system_alloc_profile;
3026 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3027 flags |= root->fs_info->avail_metadata_alloc_bits &
3028 root->fs_info->metadata_alloc_profile;
3029 return btrfs_reduce_alloc_profile(root, flags);
3030}
Josef Bacik6a632092009-02-20 11:00:09 -05003031
Yan, Zhengb742bb82010-05-16 10:46:24 -04003032static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
3033{
3034 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003035
Yan, Zhengb742bb82010-05-16 10:46:24 -04003036 if (data)
3037 flags = BTRFS_BLOCK_GROUP_DATA;
3038 else if (root == root->fs_info->chunk_root)
3039 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3040 else
3041 flags = BTRFS_BLOCK_GROUP_METADATA;
3042
3043 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003044}
3045
3046void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3047{
Josef Bacik6a632092009-02-20 11:00:09 -05003048 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003049 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003050}
3051
3052/*
3053 * This will check the space that the inode allocates from to make sure we have
3054 * enough space for bytes.
3055 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003056int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003057{
3058 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003059 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003060 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003061 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003062
3063 /* make sure bytes are sectorsize aligned */
3064 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3065
Josef Bacik0af3d002010-06-21 14:48:16 -04003066 if (root == root->fs_info->tree_root) {
3067 alloc_chunk = 0;
3068 committed = 1;
3069 }
3070
Josef Bacik6a632092009-02-20 11:00:09 -05003071 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003072 if (!data_sinfo)
3073 goto alloc;
3074
Josef Bacik6a632092009-02-20 11:00:09 -05003075again:
3076 /* make sure we have enough space to handle the data first */
3077 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003078 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3079 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3080 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003081
3082 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003083 struct btrfs_trans_handle *trans;
3084
Josef Bacik6a632092009-02-20 11:00:09 -05003085 /*
3086 * if we don't have enough free bytes in this space then we need
3087 * to alloc a new chunk.
3088 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003089 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003090 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003091
3092 data_sinfo->force_alloc = 1;
3093 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003094alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003095 alloc_target = btrfs_get_alloc_profile(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003096 trans = btrfs_join_transaction(root, 1);
3097 if (IS_ERR(trans))
3098 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003099
3100 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3101 bytes + 2 * 1024 * 1024,
3102 alloc_target, 0);
3103 btrfs_end_transaction(trans, root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003104 if (ret < 0)
Josef Bacik6a632092009-02-20 11:00:09 -05003105 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04003106
3107 if (!data_sinfo) {
3108 btrfs_set_inode_space_info(root, inode);
3109 data_sinfo = BTRFS_I(inode)->space_info;
3110 }
Josef Bacik6a632092009-02-20 11:00:09 -05003111 goto again;
3112 }
3113 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003114
3115 /* commit the current transaction and try again */
Sage Weildd7e0b72009-09-29 18:38:44 -04003116 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003117 committed = 1;
3118 trans = btrfs_join_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003119 if (IS_ERR(trans))
3120 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003121 ret = btrfs_commit_transaction(trans, root);
3122 if (ret)
3123 return ret;
3124 goto again;
3125 }
3126
Chris Mason933b5852010-05-26 11:31:00 -04003127#if 0 /* I hope we never need this code again, just in case */
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003128 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3129 "%llu bytes_reserved, " "%llu bytes_pinned, "
3130 "%llu bytes_readonly, %llu may use %llu total\n",
3131 (unsigned long long)bytes,
Joel Becker21380932009-04-21 12:38:29 -07003132 (unsigned long long)data_sinfo->bytes_used,
3133 (unsigned long long)data_sinfo->bytes_reserved,
3134 (unsigned long long)data_sinfo->bytes_pinned,
3135 (unsigned long long)data_sinfo->bytes_readonly,
3136 (unsigned long long)data_sinfo->bytes_may_use,
3137 (unsigned long long)data_sinfo->total_bytes);
Chris Mason933b5852010-05-26 11:31:00 -04003138#endif
Josef Bacik6a632092009-02-20 11:00:09 -05003139 return -ENOSPC;
3140 }
3141 data_sinfo->bytes_may_use += bytes;
3142 BTRFS_I(inode)->reserved_bytes += bytes;
3143 spin_unlock(&data_sinfo->lock);
3144
Josef Bacik9ed74f22009-09-11 16:12:44 -04003145 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003146}
3147
3148/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003149 * called when we are clearing an delalloc extent from the
3150 * inode's io_tree or there was an error for whatever reason
3151 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003152 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003153void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003154{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003155 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003156 struct btrfs_space_info *data_sinfo;
3157
3158 /* make sure bytes are sectorsize aligned */
3159 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3160
3161 data_sinfo = BTRFS_I(inode)->space_info;
3162 spin_lock(&data_sinfo->lock);
3163 data_sinfo->bytes_may_use -= bytes;
3164 BTRFS_I(inode)->reserved_bytes -= bytes;
3165 spin_unlock(&data_sinfo->lock);
3166}
3167
Josef Bacik97e728d2009-04-21 17:40:57 -04003168static void force_metadata_allocation(struct btrfs_fs_info *info)
3169{
3170 struct list_head *head = &info->space_info;
3171 struct btrfs_space_info *found;
3172
3173 rcu_read_lock();
3174 list_for_each_entry_rcu(found, head, list) {
3175 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3176 found->force_alloc = 1;
3177 }
3178 rcu_read_unlock();
3179}
3180
Yan, Zheng424499d2010-05-16 10:46:25 -04003181static int should_alloc_chunk(struct btrfs_space_info *sinfo,
3182 u64 alloc_bytes)
3183{
3184 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3185
3186 if (sinfo->bytes_used + sinfo->bytes_reserved +
3187 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3188 return 0;
3189
3190 if (sinfo->bytes_used + sinfo->bytes_reserved +
3191 alloc_bytes < div_factor(num_bytes, 8))
3192 return 0;
3193
3194 return 1;
3195}
3196
Chris Mason6324fbf2008-03-24 15:01:59 -04003197static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3198 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003199 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003200{
3201 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003202 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Yan Zhengc146afa2008-11-12 14:34:12 -05003203 int ret = 0;
3204
Josef Bacik97e728d2009-04-21 17:40:57 -04003205 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003206
Yan Zheng2b820322008-11-17 21:11:30 -05003207 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003208
Chris Mason6324fbf2008-03-24 15:01:59 -04003209 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003210 if (!space_info) {
3211 ret = update_space_info(extent_root->fs_info, flags,
3212 0, 0, &space_info);
3213 BUG_ON(ret);
3214 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003215 BUG_ON(!space_info);
3216
Josef Bacik25179202008-10-29 14:49:05 -04003217 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003218 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003219 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003220 if (space_info->full) {
3221 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003222 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003223 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003224
Yan, Zheng424499d2010-05-16 10:46:25 -04003225 if (!force && !should_alloc_chunk(space_info, alloc_bytes)) {
Josef Bacik25179202008-10-29 14:49:05 -04003226 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003227 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003228 }
Josef Bacik25179202008-10-29 14:49:05 -04003229 spin_unlock(&space_info->lock);
3230
Josef Bacik97e728d2009-04-21 17:40:57 -04003231 /*
3232 * if we're doing a data chunk, go ahead and make sure that
3233 * we keep a reasonable number of metadata chunks allocated in the
3234 * FS as well.
3235 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003236 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003237 fs_info->data_chunk_allocations++;
3238 if (!(fs_info->data_chunk_allocations %
3239 fs_info->metadata_ratio))
3240 force_metadata_allocation(fs_info);
3241 }
3242
Yan Zheng2b820322008-11-17 21:11:30 -05003243 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003244 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003245 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003246 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003247 else
3248 ret = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003249 space_info->force_alloc = 0;
3250 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003251out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003252 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003253 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003254}
3255
Yan, Zheng424499d2010-05-16 10:46:25 -04003256static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
3257 struct btrfs_root *root,
3258 struct btrfs_space_info *sinfo, u64 num_bytes)
3259{
3260 int ret;
3261 int end_trans = 0;
3262
3263 if (sinfo->full)
3264 return 0;
3265
3266 spin_lock(&sinfo->lock);
3267 ret = should_alloc_chunk(sinfo, num_bytes + 2 * 1024 * 1024);
3268 spin_unlock(&sinfo->lock);
3269 if (!ret)
3270 return 0;
3271
3272 if (!trans) {
3273 trans = btrfs_join_transaction(root, 1);
3274 BUG_ON(IS_ERR(trans));
3275 end_trans = 1;
3276 }
3277
3278 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3279 num_bytes + 2 * 1024 * 1024,
3280 get_alloc_profile(root, sinfo->flags), 0);
3281
3282 if (end_trans)
3283 btrfs_end_transaction(trans, root);
3284
3285 return ret == 1 ? 1 : 0;
3286}
3287
Yan, Zheng5da9d012010-05-16 10:46:25 -04003288/*
3289 * shrink metadata reservation for delalloc
3290 */
3291static int shrink_delalloc(struct btrfs_trans_handle *trans,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003292 struct btrfs_root *root, u64 to_reclaim)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003293{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003294 struct btrfs_block_rsv *block_rsv;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003295 u64 reserved;
3296 u64 max_reclaim;
3297 u64 reclaimed = 0;
3298 int pause = 1;
3299 int ret;
3300
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003301 block_rsv = &root->fs_info->delalloc_block_rsv;
3302 spin_lock(&block_rsv->lock);
3303 reserved = block_rsv->reserved;
3304 spin_unlock(&block_rsv->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003305
3306 if (reserved == 0)
3307 return 0;
3308
3309 max_reclaim = min(reserved, to_reclaim);
3310
3311 while (1) {
3312 ret = btrfs_start_one_delalloc_inode(root, trans ? 1 : 0);
3313 if (!ret) {
3314 __set_current_state(TASK_INTERRUPTIBLE);
3315 schedule_timeout(pause);
3316 pause <<= 1;
3317 if (pause > HZ / 10)
3318 pause = HZ / 10;
3319 } else {
3320 pause = 1;
3321 }
3322
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003323 spin_lock(&block_rsv->lock);
3324 if (reserved > block_rsv->reserved)
3325 reclaimed = reserved - block_rsv->reserved;
3326 reserved = block_rsv->reserved;
3327 spin_unlock(&block_rsv->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003328
3329 if (reserved == 0 || reclaimed >= max_reclaim)
3330 break;
3331
3332 if (trans && trans->transaction->blocked)
3333 return -EAGAIN;
3334 }
3335 return reclaimed >= to_reclaim;
3336}
3337
Yan, Zhengf0486c62010-05-16 10:46:25 -04003338static int should_retry_reserve(struct btrfs_trans_handle *trans,
3339 struct btrfs_root *root,
3340 struct btrfs_block_rsv *block_rsv,
3341 u64 num_bytes, int *retries)
3342{
3343 struct btrfs_space_info *space_info = block_rsv->space_info;
3344 int ret;
3345
3346 if ((*retries) > 2)
3347 return -ENOSPC;
3348
3349 ret = maybe_allocate_chunk(trans, root, space_info, num_bytes);
3350 if (ret)
3351 return 1;
3352
3353 if (trans && trans->transaction->in_commit)
3354 return -ENOSPC;
3355
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003356 ret = shrink_delalloc(trans, root, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003357 if (ret)
3358 return ret;
3359
3360 spin_lock(&space_info->lock);
3361 if (space_info->bytes_pinned < num_bytes)
3362 ret = 1;
3363 spin_unlock(&space_info->lock);
3364 if (ret)
3365 return -ENOSPC;
3366
3367 (*retries)++;
3368
3369 if (trans)
3370 return -EAGAIN;
3371
3372 trans = btrfs_join_transaction(root, 1);
3373 BUG_ON(IS_ERR(trans));
3374 ret = btrfs_commit_transaction(trans, root);
3375 BUG_ON(ret);
3376
3377 return 1;
3378}
3379
3380static int reserve_metadata_bytes(struct btrfs_block_rsv *block_rsv,
3381 u64 num_bytes)
3382{
3383 struct btrfs_space_info *space_info = block_rsv->space_info;
3384 u64 unused;
3385 int ret = -ENOSPC;
3386
3387 spin_lock(&space_info->lock);
3388 unused = space_info->bytes_used + space_info->bytes_reserved +
3389 space_info->bytes_pinned + space_info->bytes_readonly;
3390
3391 if (unused < space_info->total_bytes)
3392 unused = space_info->total_bytes - unused;
3393 else
3394 unused = 0;
3395
3396 if (unused >= num_bytes) {
3397 if (block_rsv->priority >= 10) {
3398 space_info->bytes_reserved += num_bytes;
3399 ret = 0;
3400 } else {
3401 if ((unused + block_rsv->reserved) *
3402 block_rsv->priority >=
3403 (num_bytes + block_rsv->reserved) * 10) {
3404 space_info->bytes_reserved += num_bytes;
3405 ret = 0;
3406 }
3407 }
3408 }
3409 spin_unlock(&space_info->lock);
3410
3411 return ret;
3412}
3413
3414static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3415 struct btrfs_root *root)
3416{
3417 struct btrfs_block_rsv *block_rsv;
3418 if (root->ref_cows)
3419 block_rsv = trans->block_rsv;
3420 else
3421 block_rsv = root->block_rsv;
3422
3423 if (!block_rsv)
3424 block_rsv = &root->fs_info->empty_block_rsv;
3425
3426 return block_rsv;
3427}
3428
3429static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3430 u64 num_bytes)
3431{
3432 int ret = -ENOSPC;
3433 spin_lock(&block_rsv->lock);
3434 if (block_rsv->reserved >= num_bytes) {
3435 block_rsv->reserved -= num_bytes;
3436 if (block_rsv->reserved < block_rsv->size)
3437 block_rsv->full = 0;
3438 ret = 0;
3439 }
3440 spin_unlock(&block_rsv->lock);
3441 return ret;
3442}
3443
3444static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3445 u64 num_bytes, int update_size)
3446{
3447 spin_lock(&block_rsv->lock);
3448 block_rsv->reserved += num_bytes;
3449 if (update_size)
3450 block_rsv->size += num_bytes;
3451 else if (block_rsv->reserved >= block_rsv->size)
3452 block_rsv->full = 1;
3453 spin_unlock(&block_rsv->lock);
3454}
3455
3456void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3457 struct btrfs_block_rsv *dest, u64 num_bytes)
3458{
3459 struct btrfs_space_info *space_info = block_rsv->space_info;
3460
3461 spin_lock(&block_rsv->lock);
3462 if (num_bytes == (u64)-1)
3463 num_bytes = block_rsv->size;
3464 block_rsv->size -= num_bytes;
3465 if (block_rsv->reserved >= block_rsv->size) {
3466 num_bytes = block_rsv->reserved - block_rsv->size;
3467 block_rsv->reserved = block_rsv->size;
3468 block_rsv->full = 1;
3469 } else {
3470 num_bytes = 0;
3471 }
3472 spin_unlock(&block_rsv->lock);
3473
3474 if (num_bytes > 0) {
3475 if (dest) {
3476 block_rsv_add_bytes(dest, num_bytes, 0);
3477 } else {
3478 spin_lock(&space_info->lock);
3479 space_info->bytes_reserved -= num_bytes;
3480 spin_unlock(&space_info->lock);
3481 }
3482 }
3483}
3484
3485static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3486 struct btrfs_block_rsv *dst, u64 num_bytes)
3487{
3488 int ret;
3489
3490 ret = block_rsv_use_bytes(src, num_bytes);
3491 if (ret)
3492 return ret;
3493
3494 block_rsv_add_bytes(dst, num_bytes, 1);
3495 return 0;
3496}
3497
3498void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3499{
3500 memset(rsv, 0, sizeof(*rsv));
3501 spin_lock_init(&rsv->lock);
3502 atomic_set(&rsv->usage, 1);
3503 rsv->priority = 6;
3504 INIT_LIST_HEAD(&rsv->list);
3505}
3506
3507struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3508{
3509 struct btrfs_block_rsv *block_rsv;
3510 struct btrfs_fs_info *fs_info = root->fs_info;
3511 u64 alloc_target;
3512
3513 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3514 if (!block_rsv)
3515 return NULL;
3516
3517 btrfs_init_block_rsv(block_rsv);
3518
3519 alloc_target = btrfs_get_alloc_profile(root, 0);
3520 block_rsv->space_info = __find_space_info(fs_info,
3521 BTRFS_BLOCK_GROUP_METADATA);
3522
3523 return block_rsv;
3524}
3525
3526void btrfs_free_block_rsv(struct btrfs_root *root,
3527 struct btrfs_block_rsv *rsv)
3528{
3529 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3530 btrfs_block_rsv_release(root, rsv, (u64)-1);
3531 if (!rsv->durable)
3532 kfree(rsv);
3533 }
3534}
3535
3536/*
3537 * make the block_rsv struct be able to capture freed space.
3538 * the captured space will re-add to the the block_rsv struct
3539 * after transaction commit
3540 */
3541void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3542 struct btrfs_block_rsv *block_rsv)
3543{
3544 block_rsv->durable = 1;
3545 mutex_lock(&fs_info->durable_block_rsv_mutex);
3546 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3547 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3548}
3549
3550int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3551 struct btrfs_root *root,
3552 struct btrfs_block_rsv *block_rsv,
3553 u64 num_bytes, int *retries)
3554{
3555 int ret;
3556
3557 if (num_bytes == 0)
3558 return 0;
3559again:
3560 ret = reserve_metadata_bytes(block_rsv, num_bytes);
3561 if (!ret) {
3562 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3563 return 0;
3564 }
3565
3566 ret = should_retry_reserve(trans, root, block_rsv, num_bytes, retries);
3567 if (ret > 0)
3568 goto again;
3569
3570 return ret;
3571}
3572
3573int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3574 struct btrfs_root *root,
3575 struct btrfs_block_rsv *block_rsv,
3576 u64 min_reserved, int min_factor)
3577{
3578 u64 num_bytes = 0;
3579 int commit_trans = 0;
3580 int ret = -ENOSPC;
3581
3582 if (!block_rsv)
3583 return 0;
3584
3585 spin_lock(&block_rsv->lock);
3586 if (min_factor > 0)
3587 num_bytes = div_factor(block_rsv->size, min_factor);
3588 if (min_reserved > num_bytes)
3589 num_bytes = min_reserved;
3590
3591 if (block_rsv->reserved >= num_bytes) {
3592 ret = 0;
3593 } else {
3594 num_bytes -= block_rsv->reserved;
3595 if (block_rsv->durable &&
3596 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3597 commit_trans = 1;
3598 }
3599 spin_unlock(&block_rsv->lock);
3600 if (!ret)
3601 return 0;
3602
3603 if (block_rsv->refill_used) {
3604 ret = reserve_metadata_bytes(block_rsv, num_bytes);
3605 if (!ret) {
3606 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3607 return 0;
3608 }
3609 }
3610
3611 if (commit_trans) {
3612 if (trans)
3613 return -EAGAIN;
3614
3615 trans = btrfs_join_transaction(root, 1);
3616 BUG_ON(IS_ERR(trans));
3617 ret = btrfs_commit_transaction(trans, root);
3618 return 0;
3619 }
3620
3621 WARN_ON(1);
3622 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
3623 block_rsv->size, block_rsv->reserved,
3624 block_rsv->freed[0], block_rsv->freed[1]);
3625
3626 return -ENOSPC;
3627}
3628
3629int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3630 struct btrfs_block_rsv *dst_rsv,
3631 u64 num_bytes)
3632{
3633 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3634}
3635
3636void btrfs_block_rsv_release(struct btrfs_root *root,
3637 struct btrfs_block_rsv *block_rsv,
3638 u64 num_bytes)
3639{
3640 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3641 if (global_rsv->full || global_rsv == block_rsv ||
3642 block_rsv->space_info != global_rsv->space_info)
3643 global_rsv = NULL;
3644 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3645}
3646
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003647/*
3648 * helper to calculate size of global block reservation.
3649 * the desired value is sum of space used by extent tree,
3650 * checksum tree and root tree
3651 */
3652static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3653{
3654 struct btrfs_space_info *sinfo;
3655 u64 num_bytes;
3656 u64 meta_used;
3657 u64 data_used;
3658 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3659#if 0
3660 /*
3661 * per tree used space accounting can be inaccuracy, so we
3662 * can't rely on it.
3663 */
3664 spin_lock(&fs_info->extent_root->accounting_lock);
3665 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3666 spin_unlock(&fs_info->extent_root->accounting_lock);
3667
3668 spin_lock(&fs_info->csum_root->accounting_lock);
3669 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3670 spin_unlock(&fs_info->csum_root->accounting_lock);
3671
3672 spin_lock(&fs_info->tree_root->accounting_lock);
3673 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3674 spin_unlock(&fs_info->tree_root->accounting_lock);
3675#endif
3676 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3677 spin_lock(&sinfo->lock);
3678 data_used = sinfo->bytes_used;
3679 spin_unlock(&sinfo->lock);
3680
3681 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3682 spin_lock(&sinfo->lock);
3683 meta_used = sinfo->bytes_used;
3684 spin_unlock(&sinfo->lock);
3685
3686 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3687 csum_size * 2;
3688 num_bytes += div64_u64(data_used + meta_used, 50);
3689
3690 if (num_bytes * 3 > meta_used)
3691 num_bytes = div64_u64(meta_used, 3);
3692
3693 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3694}
3695
3696static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3697{
3698 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3699 struct btrfs_space_info *sinfo = block_rsv->space_info;
3700 u64 num_bytes;
3701
3702 num_bytes = calc_global_metadata_size(fs_info);
3703
3704 spin_lock(&block_rsv->lock);
3705 spin_lock(&sinfo->lock);
3706
3707 block_rsv->size = num_bytes;
3708
3709 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
3710 sinfo->bytes_reserved + sinfo->bytes_readonly;
3711
3712 if (sinfo->total_bytes > num_bytes) {
3713 num_bytes = sinfo->total_bytes - num_bytes;
3714 block_rsv->reserved += num_bytes;
3715 sinfo->bytes_reserved += num_bytes;
3716 }
3717
3718 if (block_rsv->reserved >= block_rsv->size) {
3719 num_bytes = block_rsv->reserved - block_rsv->size;
3720 sinfo->bytes_reserved -= num_bytes;
3721 block_rsv->reserved = block_rsv->size;
3722 block_rsv->full = 1;
3723 }
3724#if 0
3725 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3726 block_rsv->size, block_rsv->reserved);
3727#endif
3728 spin_unlock(&sinfo->lock);
3729 spin_unlock(&block_rsv->lock);
3730}
3731
Yan, Zhengf0486c62010-05-16 10:46:25 -04003732static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3733{
3734 struct btrfs_space_info *space_info;
3735
3736 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3737 fs_info->chunk_block_rsv.space_info = space_info;
3738 fs_info->chunk_block_rsv.priority = 10;
3739
3740 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003741 fs_info->global_block_rsv.space_info = space_info;
3742 fs_info->global_block_rsv.priority = 10;
3743 fs_info->global_block_rsv.refill_used = 1;
3744 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003745 fs_info->trans_block_rsv.space_info = space_info;
3746 fs_info->empty_block_rsv.space_info = space_info;
3747 fs_info->empty_block_rsv.priority = 10;
3748
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003749 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3750 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3751 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3752 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003753 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003754
3755 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3756
3757 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3758
3759 update_global_block_rsv(fs_info);
3760}
3761
3762static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3763{
3764 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3765 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3766 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3767 WARN_ON(fs_info->trans_block_rsv.size > 0);
3768 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3769 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3770 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003771}
3772
Yan, Zhenga22285a2010-05-16 10:48:46 -04003773static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3774{
3775 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3776 3 * num_items;
3777}
3778
3779int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3780 struct btrfs_root *root,
3781 int num_items, int *retries)
3782{
3783 u64 num_bytes;
3784 int ret;
3785
3786 if (num_items == 0 || root->fs_info->chunk_root == root)
3787 return 0;
3788
3789 num_bytes = calc_trans_metadata_size(root, num_items);
3790 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
3791 num_bytes, retries);
3792 if (!ret) {
3793 trans->bytes_reserved += num_bytes;
3794 trans->block_rsv = &root->fs_info->trans_block_rsv;
3795 }
3796 return ret;
3797}
3798
3799void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3800 struct btrfs_root *root)
3801{
3802 if (!trans->bytes_reserved)
3803 return;
3804
3805 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3806 btrfs_block_rsv_release(root, trans->block_rsv,
3807 trans->bytes_reserved);
3808 trans->bytes_reserved = 0;
3809}
3810
Yan, Zhengd68fc572010-05-16 10:49:58 -04003811int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3812 struct inode *inode)
3813{
3814 struct btrfs_root *root = BTRFS_I(inode)->root;
3815 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3816 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3817
3818 /*
3819 * one for deleting orphan item, one for updating inode and
3820 * two for calling btrfs_truncate_inode_items.
3821 *
3822 * btrfs_truncate_inode_items is a delete operation, it frees
3823 * more space than it uses in most cases. So two units of
3824 * metadata space should be enough for calling it many times.
3825 * If all of the metadata space is used, we can commit
3826 * transaction and use space it freed.
3827 */
3828 u64 num_bytes = calc_trans_metadata_size(root, 4);
3829 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3830}
3831
3832void btrfs_orphan_release_metadata(struct inode *inode)
3833{
3834 struct btrfs_root *root = BTRFS_I(inode)->root;
3835 u64 num_bytes = calc_trans_metadata_size(root, 4);
3836 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3837}
3838
Yan, Zhenga22285a2010-05-16 10:48:46 -04003839int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3840 struct btrfs_pending_snapshot *pending)
3841{
3842 struct btrfs_root *root = pending->root;
3843 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3844 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3845 /*
3846 * two for root back/forward refs, two for directory entries
3847 * and one for root of the snapshot.
3848 */
3849 u64 num_bytes = calc_trans_metadata_size(root, 5);
3850 dst_rsv->space_info = src_rsv->space_info;
3851 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3852}
3853
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003854static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3855{
3856 return num_bytes >>= 3;
3857}
3858
3859int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3860{
3861 struct btrfs_root *root = BTRFS_I(inode)->root;
3862 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3863 u64 to_reserve;
3864 int nr_extents;
3865 int retries = 0;
3866 int ret;
3867
3868 if (btrfs_transaction_in_commit(root->fs_info))
3869 schedule_timeout(1);
3870
3871 num_bytes = ALIGN(num_bytes, root->sectorsize);
3872again:
3873 spin_lock(&BTRFS_I(inode)->accounting_lock);
3874 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
3875 if (nr_extents > BTRFS_I(inode)->reserved_extents) {
3876 nr_extents -= BTRFS_I(inode)->reserved_extents;
3877 to_reserve = calc_trans_metadata_size(root, nr_extents);
3878 } else {
3879 nr_extents = 0;
3880 to_reserve = 0;
3881 }
3882
3883 to_reserve += calc_csum_metadata_size(inode, num_bytes);
3884 ret = reserve_metadata_bytes(block_rsv, to_reserve);
3885 if (ret) {
3886 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3887 ret = should_retry_reserve(NULL, root, block_rsv, to_reserve,
3888 &retries);
3889 if (ret > 0)
3890 goto again;
3891 return ret;
3892 }
3893
3894 BTRFS_I(inode)->reserved_extents += nr_extents;
3895 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
3896 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3897
3898 block_rsv_add_bytes(block_rsv, to_reserve, 1);
3899
3900 if (block_rsv->size > 512 * 1024 * 1024)
3901 shrink_delalloc(NULL, root, to_reserve);
3902
3903 return 0;
3904}
3905
3906void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
3907{
3908 struct btrfs_root *root = BTRFS_I(inode)->root;
3909 u64 to_free;
3910 int nr_extents;
3911
3912 num_bytes = ALIGN(num_bytes, root->sectorsize);
3913 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
3914
3915 spin_lock(&BTRFS_I(inode)->accounting_lock);
3916 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
3917 if (nr_extents < BTRFS_I(inode)->reserved_extents) {
3918 nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents;
3919 BTRFS_I(inode)->reserved_extents -= nr_extents;
3920 } else {
3921 nr_extents = 0;
3922 }
3923 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3924
3925 to_free = calc_csum_metadata_size(inode, num_bytes);
3926 if (nr_extents > 0)
3927 to_free += calc_trans_metadata_size(root, nr_extents);
3928
3929 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
3930 to_free);
3931}
3932
3933int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
3934{
3935 int ret;
3936
3937 ret = btrfs_check_data_free_space(inode, num_bytes);
3938 if (ret)
3939 return ret;
3940
3941 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
3942 if (ret) {
3943 btrfs_free_reserved_data_space(inode, num_bytes);
3944 return ret;
3945 }
3946
3947 return 0;
3948}
3949
3950void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
3951{
3952 btrfs_delalloc_release_metadata(inode, num_bytes);
3953 btrfs_free_reserved_data_space(inode, num_bytes);
3954}
3955
Chris Mason9078a3e2007-04-26 16:46:15 -04003956static int update_block_group(struct btrfs_trans_handle *trans,
3957 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003958 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04003959{
Josef Bacik0af3d002010-06-21 14:48:16 -04003960 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04003961 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04003962 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003963 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003964 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04003965 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04003966
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003967 /* block accounting for super block */
3968 spin_lock(&info->delalloc_lock);
3969 old_val = btrfs_super_bytes_used(&info->super_copy);
3970 if (alloc)
3971 old_val += num_bytes;
3972 else
3973 old_val -= num_bytes;
3974 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003975 spin_unlock(&info->delalloc_lock);
3976
Chris Masond3977122009-01-05 21:25:51 -05003977 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003978 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003979 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04003980 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003981 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
3982 BTRFS_BLOCK_GROUP_RAID1 |
3983 BTRFS_BLOCK_GROUP_RAID10))
3984 factor = 2;
3985 else
3986 factor = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04003987
Chris Masondb945352007-10-15 16:15:53 -04003988 byte_in_group = bytenr - cache->key.objectid;
3989 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003990
Josef Bacik25179202008-10-29 14:49:05 -04003991 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003992 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04003993
3994 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
3995 cache->disk_cache_state < BTRFS_DC_CLEAR)
3996 cache->disk_cache_state = BTRFS_DC_CLEAR;
3997
Josef Bacik0f9dd462008-09-23 13:14:11 -04003998 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04003999 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004000 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004001 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004002 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004003 btrfs_set_block_group_used(&cache->item, old_val);
4004 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004005 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004006 cache->space_info->bytes_used += num_bytes;
4007 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004008 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004009 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004010 } else {
Chris Masondb945352007-10-15 16:15:53 -04004011 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004012 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004013 cache->pinned += num_bytes;
4014 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004015 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004016 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004017 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004018 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004019
Yan, Zhengf0486c62010-05-16 10:46:25 -04004020 set_extent_dirty(info->pinned_extents,
4021 bytenr, bytenr + num_bytes - 1,
4022 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004023 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004024 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004025 total -= num_bytes;
4026 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004027 }
4028 return 0;
4029}
Chris Mason6324fbf2008-03-24 15:01:59 -04004030
Chris Masona061fc82008-05-07 11:43:44 -04004031static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4032{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004033 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004034 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004035
4036 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4037 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004038 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004039
Yan Zhengd2fb3432008-12-11 16:30:39 -05004040 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004041 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004042
4043 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004044}
4045
Yan, Zhengf0486c62010-05-16 10:46:25 -04004046static int pin_down_extent(struct btrfs_root *root,
4047 struct btrfs_block_group_cache *cache,
4048 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004049{
Yan Zheng11833d62009-09-11 16:11:19 -04004050 spin_lock(&cache->space_info->lock);
4051 spin_lock(&cache->lock);
4052 cache->pinned += num_bytes;
4053 cache->space_info->bytes_pinned += num_bytes;
4054 if (reserved) {
4055 cache->reserved -= num_bytes;
4056 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004057 }
Yan Zheng11833d62009-09-11 16:11:19 -04004058 spin_unlock(&cache->lock);
4059 spin_unlock(&cache->space_info->lock);
4060
Yan, Zhengf0486c62010-05-16 10:46:25 -04004061 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4062 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004063 return 0;
4064}
Chris Mason9078a3e2007-04-26 16:46:15 -04004065
Yan, Zhengf0486c62010-05-16 10:46:25 -04004066/*
4067 * this function must be called within transaction
4068 */
4069int btrfs_pin_extent(struct btrfs_root *root,
4070 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004071{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004072 struct btrfs_block_group_cache *cache;
4073
4074 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4075 BUG_ON(!cache);
4076
4077 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4078
4079 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004080 return 0;
4081}
Zheng Yane8569812008-09-26 10:05:48 -04004082
Yan, Zhengf0486c62010-05-16 10:46:25 -04004083/*
4084 * update size of reserved extents. this function may return -EAGAIN
4085 * if 'reserve' is true or 'sinfo' is false.
4086 */
4087static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
4088 u64 num_bytes, int reserve, int sinfo)
4089{
4090 int ret = 0;
4091 if (sinfo) {
4092 struct btrfs_space_info *space_info = cache->space_info;
4093 spin_lock(&space_info->lock);
4094 spin_lock(&cache->lock);
4095 if (reserve) {
4096 if (cache->ro) {
4097 ret = -EAGAIN;
4098 } else {
4099 cache->reserved += num_bytes;
4100 space_info->bytes_reserved += num_bytes;
4101 }
4102 } else {
4103 if (cache->ro)
4104 space_info->bytes_readonly += num_bytes;
4105 cache->reserved -= num_bytes;
4106 space_info->bytes_reserved -= num_bytes;
4107 }
4108 spin_unlock(&cache->lock);
4109 spin_unlock(&space_info->lock);
4110 } else {
4111 spin_lock(&cache->lock);
4112 if (cache->ro) {
4113 ret = -EAGAIN;
4114 } else {
4115 if (reserve)
4116 cache->reserved += num_bytes;
4117 else
4118 cache->reserved -= num_bytes;
4119 }
4120 spin_unlock(&cache->lock);
4121 }
4122 return ret;
4123}
4124
Yan Zheng11833d62009-09-11 16:11:19 -04004125int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4126 struct btrfs_root *root)
4127{
4128 struct btrfs_fs_info *fs_info = root->fs_info;
4129 struct btrfs_caching_control *next;
4130 struct btrfs_caching_control *caching_ctl;
4131 struct btrfs_block_group_cache *cache;
4132
4133 down_write(&fs_info->extent_commit_sem);
4134
4135 list_for_each_entry_safe(caching_ctl, next,
4136 &fs_info->caching_block_groups, list) {
4137 cache = caching_ctl->block_group;
4138 if (block_group_cache_done(cache)) {
4139 cache->last_byte_to_unpin = (u64)-1;
4140 list_del_init(&caching_ctl->list);
4141 put_caching_control(caching_ctl);
4142 } else {
4143 cache->last_byte_to_unpin = caching_ctl->progress;
4144 }
4145 }
4146
4147 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4148 fs_info->pinned_extents = &fs_info->freed_extents[1];
4149 else
4150 fs_info->pinned_extents = &fs_info->freed_extents[0];
4151
4152 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004153
4154 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004155 return 0;
4156}
4157
4158static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4159{
4160 struct btrfs_fs_info *fs_info = root->fs_info;
4161 struct btrfs_block_group_cache *cache = NULL;
4162 u64 len;
4163
4164 while (start <= end) {
4165 if (!cache ||
4166 start >= cache->key.objectid + cache->key.offset) {
4167 if (cache)
4168 btrfs_put_block_group(cache);
4169 cache = btrfs_lookup_block_group(fs_info, start);
4170 BUG_ON(!cache);
4171 }
4172
4173 len = cache->key.objectid + cache->key.offset - start;
4174 len = min(len, end + 1 - start);
4175
4176 if (start < cache->last_byte_to_unpin) {
4177 len = min(len, cache->last_byte_to_unpin - start);
4178 btrfs_add_free_space(cache, start, len);
4179 }
Josef Bacik25179202008-10-29 14:49:05 -04004180
Yan, Zhengf0486c62010-05-16 10:46:25 -04004181 start += len;
4182
Josef Bacik25179202008-10-29 14:49:05 -04004183 spin_lock(&cache->space_info->lock);
4184 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004185 cache->pinned -= len;
4186 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004187 if (cache->ro) {
4188 cache->space_info->bytes_readonly += len;
4189 } else if (cache->reserved_pinned > 0) {
4190 len = min(len, cache->reserved_pinned);
4191 cache->reserved_pinned -= len;
4192 cache->space_info->bytes_reserved += len;
4193 }
Josef Bacik25179202008-10-29 14:49:05 -04004194 spin_unlock(&cache->lock);
4195 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004196 }
4197
4198 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004199 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004200 return 0;
4201}
4202
4203int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004204 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004205{
Yan Zheng11833d62009-09-11 16:11:19 -04004206 struct btrfs_fs_info *fs_info = root->fs_info;
4207 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004208 struct btrfs_block_rsv *block_rsv;
4209 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004210 u64 start;
4211 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004212 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004213 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004214
Yan Zheng11833d62009-09-11 16:11:19 -04004215 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4216 unpin = &fs_info->freed_extents[1];
4217 else
4218 unpin = &fs_info->freed_extents[0];
4219
Chris Masond3977122009-01-05 21:25:51 -05004220 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004221 ret = find_first_extent_bit(unpin, 0, &start, &end,
4222 EXTENT_DIRTY);
4223 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004224 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004225
4226 ret = btrfs_discard_extent(root, start, end + 1 - start);
4227
Chris Mason1a5bc162007-10-15 16:15:26 -04004228 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004229 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004230 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004231 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004232
Yan, Zhengf0486c62010-05-16 10:46:25 -04004233 mutex_lock(&fs_info->durable_block_rsv_mutex);
4234 list_for_each_entry_safe(block_rsv, next_rsv,
4235 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004236
Yan, Zhengf0486c62010-05-16 10:46:25 -04004237 idx = trans->transid & 0x1;
4238 if (block_rsv->freed[idx] > 0) {
4239 block_rsv_add_bytes(block_rsv,
4240 block_rsv->freed[idx], 0);
4241 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004242 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004243 if (atomic_read(&block_rsv->usage) == 0) {
4244 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004245
Yan, Zhengf0486c62010-05-16 10:46:25 -04004246 if (block_rsv->freed[0] == 0 &&
4247 block_rsv->freed[1] == 0) {
4248 list_del_init(&block_rsv->list);
4249 kfree(block_rsv);
4250 }
4251 } else {
4252 btrfs_block_rsv_release(root, block_rsv, 0);
4253 }
4254 }
4255 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4256
Chris Masone20d96d2007-03-22 12:13:20 -04004257 return 0;
4258}
4259
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004260static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4261 struct btrfs_root *root,
4262 u64 bytenr, u64 num_bytes, u64 parent,
4263 u64 root_objectid, u64 owner_objectid,
4264 u64 owner_offset, int refs_to_drop,
4265 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004266{
Chris Masone2fa7222007-03-12 16:22:34 -04004267 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004268 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004269 struct btrfs_fs_info *info = root->fs_info;
4270 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004271 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004272 struct btrfs_extent_item *ei;
4273 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004274 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004275 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004276 int extent_slot = 0;
4277 int found_extent = 0;
4278 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004279 u32 item_size;
4280 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004281
Chris Mason5caf2a02007-04-02 11:20:42 -04004282 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004283 if (!path)
4284 return -ENOMEM;
4285
Chris Mason3c12ac72008-04-21 12:01:38 -04004286 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004287 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004288
4289 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4290 BUG_ON(!is_data && refs_to_drop != 1);
4291
4292 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4293 bytenr, num_bytes, parent,
4294 root_objectid, owner_objectid,
4295 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004296 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004297 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004298 while (extent_slot >= 0) {
4299 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004300 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004301 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004302 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004303 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4304 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004305 found_extent = 1;
4306 break;
4307 }
4308 if (path->slots[0] - extent_slot > 5)
4309 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004310 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004311 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004312#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4313 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4314 if (found_extent && item_size < sizeof(*ei))
4315 found_extent = 0;
4316#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004317 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004318 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004319 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004320 NULL, refs_to_drop,
4321 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004322 BUG_ON(ret);
4323 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04004324 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004325
4326 key.objectid = bytenr;
4327 key.type = BTRFS_EXTENT_ITEM_KEY;
4328 key.offset = num_bytes;
4329
Zheng Yan31840ae2008-09-23 13:14:14 -04004330 ret = btrfs_search_slot(trans, extent_root,
4331 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004332 if (ret) {
4333 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004334 ", was looking for %llu\n", ret,
4335 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004336 btrfs_print_leaf(extent_root, path->nodes[0]);
4337 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004338 BUG_ON(ret);
4339 extent_slot = path->slots[0];
4340 }
Chris Mason7bb86312007-12-11 09:25:06 -05004341 } else {
4342 btrfs_print_leaf(extent_root, path->nodes[0]);
4343 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004344 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004345 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004346 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004347 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004348 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004349 (unsigned long long)owner_objectid,
4350 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004351 }
Chris Mason5f39d392007-10-15 16:14:19 -04004352
4353 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004354 item_size = btrfs_item_size_nr(leaf, extent_slot);
4355#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4356 if (item_size < sizeof(*ei)) {
4357 BUG_ON(found_extent || extent_slot != path->slots[0]);
4358 ret = convert_extent_item_v0(trans, extent_root, path,
4359 owner_objectid, 0);
4360 BUG_ON(ret < 0);
4361
4362 btrfs_release_path(extent_root, path);
4363 path->leave_spinning = 1;
4364
4365 key.objectid = bytenr;
4366 key.type = BTRFS_EXTENT_ITEM_KEY;
4367 key.offset = num_bytes;
4368
4369 ret = btrfs_search_slot(trans, extent_root, &key, path,
4370 -1, 1);
4371 if (ret) {
4372 printk(KERN_ERR "umm, got %d back from search"
4373 ", was looking for %llu\n", ret,
4374 (unsigned long long)bytenr);
4375 btrfs_print_leaf(extent_root, path->nodes[0]);
4376 }
4377 BUG_ON(ret);
4378 extent_slot = path->slots[0];
4379 leaf = path->nodes[0];
4380 item_size = btrfs_item_size_nr(leaf, extent_slot);
4381 }
4382#endif
4383 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004384 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004385 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004386 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4387 struct btrfs_tree_block_info *bi;
4388 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4389 bi = (struct btrfs_tree_block_info *)(ei + 1);
4390 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004391 }
4392
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004393 refs = btrfs_extent_refs(leaf, ei);
4394 BUG_ON(refs < refs_to_drop);
4395 refs -= refs_to_drop;
4396
4397 if (refs > 0) {
4398 if (extent_op)
4399 __run_delayed_extent_op(extent_op, leaf, ei);
4400 /*
4401 * In the case of inline back ref, reference count will
4402 * be updated by remove_extent_backref
4403 */
4404 if (iref) {
4405 BUG_ON(!found_extent);
4406 } else {
4407 btrfs_set_extent_refs(leaf, ei, refs);
4408 btrfs_mark_buffer_dirty(leaf);
4409 }
4410 if (found_extent) {
4411 ret = remove_extent_backref(trans, extent_root, path,
4412 iref, refs_to_drop,
4413 is_data);
4414 BUG_ON(ret);
4415 }
4416 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004417 if (found_extent) {
4418 BUG_ON(is_data && refs_to_drop !=
4419 extent_data_ref_count(root, path, iref));
4420 if (iref) {
4421 BUG_ON(path->slots[0] != extent_slot);
4422 } else {
4423 BUG_ON(path->slots[0] != extent_slot + 1);
4424 path->slots[0] = extent_slot;
4425 num_to_del = 2;
4426 }
Chris Mason78fae272007-03-25 11:35:08 -04004427 }
Chris Masonb9473432009-03-13 11:00:37 -04004428
Chris Mason952fcca2008-02-18 16:33:44 -05004429 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4430 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004431 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04004432 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01004433
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004434 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004435 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4436 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004437 } else {
4438 invalidate_mapping_pages(info->btree_inode->i_mapping,
4439 bytenr >> PAGE_CACHE_SHIFT,
4440 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004441 }
4442
Yan, Zhengf0486c62010-05-16 10:46:25 -04004443 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004444 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004445 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004446 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004447 return ret;
4448}
4449
4450/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004451 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004452 * delayed ref for that extent as well. This searches the delayed ref tree for
4453 * a given extent, and if there are no other delayed refs to be processed, it
4454 * removes it from the tree.
4455 */
4456static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4457 struct btrfs_root *root, u64 bytenr)
4458{
4459 struct btrfs_delayed_ref_head *head;
4460 struct btrfs_delayed_ref_root *delayed_refs;
4461 struct btrfs_delayed_ref_node *ref;
4462 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004463 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004464
4465 delayed_refs = &trans->transaction->delayed_refs;
4466 spin_lock(&delayed_refs->lock);
4467 head = btrfs_find_delayed_ref_head(trans, bytenr);
4468 if (!head)
4469 goto out;
4470
4471 node = rb_prev(&head->node.rb_node);
4472 if (!node)
4473 goto out;
4474
4475 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4476
4477 /* there are still entries for this ref, we can't drop it */
4478 if (ref->bytenr == bytenr)
4479 goto out;
4480
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004481 if (head->extent_op) {
4482 if (!head->must_insert_reserved)
4483 goto out;
4484 kfree(head->extent_op);
4485 head->extent_op = NULL;
4486 }
4487
Chris Mason1887be62009-03-13 10:11:24 -04004488 /*
4489 * waiting for the lock here would deadlock. If someone else has it
4490 * locked they are already in the process of dropping it anyway
4491 */
4492 if (!mutex_trylock(&head->mutex))
4493 goto out;
4494
4495 /*
4496 * at this point we have a head with no other entries. Go
4497 * ahead and process it.
4498 */
4499 head->node.in_tree = 0;
4500 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004501
Chris Mason1887be62009-03-13 10:11:24 -04004502 delayed_refs->num_entries--;
4503
4504 /*
4505 * we don't take a ref on the node because we're removing it from the
4506 * tree, so we just steal the ref the tree was holding.
4507 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004508 delayed_refs->num_heads--;
4509 if (list_empty(&head->cluster))
4510 delayed_refs->num_heads_ready--;
4511
4512 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004513 spin_unlock(&delayed_refs->lock);
4514
Yan, Zhengf0486c62010-05-16 10:46:25 -04004515 BUG_ON(head->extent_op);
4516 if (head->must_insert_reserved)
4517 ret = 1;
4518
4519 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004520 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004521 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004522out:
4523 spin_unlock(&delayed_refs->lock);
4524 return 0;
4525}
4526
Yan, Zhengf0486c62010-05-16 10:46:25 -04004527void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4528 struct btrfs_root *root,
4529 struct extent_buffer *buf,
4530 u64 parent, int last_ref)
4531{
4532 struct btrfs_block_rsv *block_rsv;
4533 struct btrfs_block_group_cache *cache = NULL;
4534 int ret;
4535
4536 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4537 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4538 parent, root->root_key.objectid,
4539 btrfs_header_level(buf),
4540 BTRFS_DROP_DELAYED_REF, NULL);
4541 BUG_ON(ret);
4542 }
4543
4544 if (!last_ref)
4545 return;
4546
4547 block_rsv = get_block_rsv(trans, root);
4548 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004549 if (block_rsv->space_info != cache->space_info)
4550 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004551
4552 if (btrfs_header_generation(buf) == trans->transid) {
4553 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4554 ret = check_ref_cleanup(trans, root, buf->start);
4555 if (!ret)
4556 goto pin;
4557 }
4558
4559 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4560 pin_down_extent(root, cache, buf->start, buf->len, 1);
4561 goto pin;
4562 }
4563
4564 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4565
4566 btrfs_add_free_space(cache, buf->start, buf->len);
4567 ret = update_reserved_bytes(cache, buf->len, 0, 0);
4568 if (ret == -EAGAIN) {
4569 /* block group became read-only */
4570 update_reserved_bytes(cache, buf->len, 0, 1);
4571 goto out;
4572 }
4573
4574 ret = 1;
4575 spin_lock(&block_rsv->lock);
4576 if (block_rsv->reserved < block_rsv->size) {
4577 block_rsv->reserved += buf->len;
4578 ret = 0;
4579 }
4580 spin_unlock(&block_rsv->lock);
4581
4582 if (ret) {
4583 spin_lock(&cache->space_info->lock);
4584 cache->space_info->bytes_reserved -= buf->len;
4585 spin_unlock(&cache->space_info->lock);
4586 }
4587 goto out;
4588 }
4589pin:
4590 if (block_rsv->durable && !cache->ro) {
4591 ret = 0;
4592 spin_lock(&cache->lock);
4593 if (!cache->ro) {
4594 cache->reserved_pinned += buf->len;
4595 ret = 1;
4596 }
4597 spin_unlock(&cache->lock);
4598
4599 if (ret) {
4600 spin_lock(&block_rsv->lock);
4601 block_rsv->freed[trans->transid & 0x1] += buf->len;
4602 spin_unlock(&block_rsv->lock);
4603 }
4604 }
4605out:
4606 btrfs_put_block_group(cache);
4607}
4608
Chris Mason925baed2008-06-25 16:01:30 -04004609int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004610 struct btrfs_root *root,
4611 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004612 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004613{
4614 int ret;
4615
Chris Mason56bec292009-03-13 10:10:06 -04004616 /*
4617 * tree log blocks never actually go into the extent allocation
4618 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004619 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004620 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4621 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004622 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004623 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004624 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004625 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4626 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4627 parent, root_objectid, (int)owner,
4628 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004629 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004630 } else {
4631 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4632 parent, root_objectid, owner,
4633 offset, BTRFS_DROP_DELAYED_REF, NULL);
4634 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004635 }
Chris Mason925baed2008-06-25 16:01:30 -04004636 return ret;
4637}
4638
Chris Mason87ee04e2007-11-30 11:30:34 -05004639static u64 stripe_align(struct btrfs_root *root, u64 val)
4640{
4641 u64 mask = ((u64)root->stripesize - 1);
4642 u64 ret = (val + mask) & ~mask;
4643 return ret;
4644}
4645
Chris Masonfec577f2007-02-26 10:40:21 -05004646/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004647 * when we wait for progress in the block group caching, its because
4648 * our allocation attempt failed at least once. So, we must sleep
4649 * and let some progress happen before we try again.
4650 *
4651 * This function will sleep at least once waiting for new free space to
4652 * show up, and then it will check the block group free space numbers
4653 * for our min num_bytes. Another option is to have it go ahead
4654 * and look in the rbtree for a free extent of a given size, but this
4655 * is a good start.
4656 */
4657static noinline int
4658wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4659 u64 num_bytes)
4660{
Yan Zheng11833d62009-09-11 16:11:19 -04004661 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004662 DEFINE_WAIT(wait);
4663
Yan Zheng11833d62009-09-11 16:11:19 -04004664 caching_ctl = get_caching_control(cache);
4665 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004666 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004667
Yan Zheng11833d62009-09-11 16:11:19 -04004668 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004669 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004670
4671 put_caching_control(caching_ctl);
4672 return 0;
4673}
4674
4675static noinline int
4676wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4677{
4678 struct btrfs_caching_control *caching_ctl;
4679 DEFINE_WAIT(wait);
4680
4681 caching_ctl = get_caching_control(cache);
4682 if (!caching_ctl)
4683 return 0;
4684
4685 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4686
4687 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004688 return 0;
4689}
4690
Yan, Zhengb742bb82010-05-16 10:46:24 -04004691static int get_block_group_index(struct btrfs_block_group_cache *cache)
4692{
4693 int index;
4694 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4695 index = 0;
4696 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4697 index = 1;
4698 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4699 index = 2;
4700 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4701 index = 3;
4702 else
4703 index = 4;
4704 return index;
4705}
4706
Josef Bacik817d52f2009-07-13 21:29:25 -04004707enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004708 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004709 LOOP_CACHING_NOWAIT = 1,
4710 LOOP_CACHING_WAIT = 2,
4711 LOOP_ALLOC_CHUNK = 3,
4712 LOOP_NO_EMPTY_SIZE = 4,
4713};
4714
4715/*
Chris Masonfec577f2007-02-26 10:40:21 -05004716 * walks the btree of allocated extents and find a hole of a given size.
4717 * The key ins is changed to record the hole:
4718 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004719 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004720 * ins->offset == number of blocks
4721 * Any available blocks before search_start are skipped.
4722 */
Chris Masond3977122009-01-05 21:25:51 -05004723static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004724 struct btrfs_root *orig_root,
4725 u64 num_bytes, u64 empty_size,
4726 u64 search_start, u64 search_end,
4727 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004728 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004729{
Josef Bacik80eb2342008-10-29 14:49:05 -04004730 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004731 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004732 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004733 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004734 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004735 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004736 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004737 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004738 int last_ptr_loop = 0;
4739 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004740 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004741 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004742 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004743 bool failed_alloc = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004744 u64 ideal_cache_percent = 0;
4745 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004746
Chris Masondb945352007-10-15 16:15:53 -04004747 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004748 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004749 ins->objectid = 0;
4750 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004751
Josef Bacik2552d172009-04-03 10:14:19 -04004752 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004753 if (!space_info) {
4754 printk(KERN_ERR "No space info for %d\n", data);
4755 return -ENOSPC;
4756 }
Josef Bacik2552d172009-04-03 10:14:19 -04004757
Chris Mason0ef3e662008-05-24 14:04:53 -04004758 if (orig_root->ref_cows || empty_size)
4759 allowed_chunk_alloc = 1;
4760
Chris Mason239b14b2008-03-24 15:02:07 -04004761 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004762 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004763 if (!btrfs_test_opt(root, SSD))
4764 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004765 }
4766
Chris Masonfa9c0d792009-04-03 09:47:43 -04004767 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4768 last_ptr = &root->fs_info->data_alloc_cluster;
4769 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004770
Chris Mason239b14b2008-03-24 15:02:07 -04004771 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004772 spin_lock(&last_ptr->lock);
4773 if (last_ptr->block_group)
4774 hint_byte = last_ptr->window_start;
4775 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004776 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004777
Chris Masona061fc82008-05-07 11:43:44 -04004778 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004779 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004780
Josef Bacik817d52f2009-07-13 21:29:25 -04004781 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004782 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004783
Josef Bacik2552d172009-04-03 10:14:19 -04004784 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004785ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004786 block_group = btrfs_lookup_block_group(root->fs_info,
4787 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004788 /*
4789 * we don't want to use the block group if it doesn't match our
4790 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004791 *
4792 * However if we are re-searching with an ideal block group
4793 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004794 */
4795 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004796 (block_group->cached != BTRFS_CACHE_NO ||
4797 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004798 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004799 if (list_empty(&block_group->list) ||
4800 block_group->ro) {
4801 /*
4802 * someone is removing this block group,
4803 * we can't jump into the have_block_group
4804 * target because our list pointers are not
4805 * valid
4806 */
4807 btrfs_put_block_group(block_group);
4808 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004809 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004810 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004811 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004812 }
Josef Bacik2552d172009-04-03 10:14:19 -04004813 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004814 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004815 }
Chris Mason42e70e72008-11-07 18:17:11 -05004816 }
Josef Bacik2552d172009-04-03 10:14:19 -04004817search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004818 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004819 list_for_each_entry(block_group, &space_info->block_groups[index],
4820 list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004821 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004822 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004823
Josef Bacik11dfe352009-11-13 20:12:59 +00004824 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004825 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004826
Josef Bacik2552d172009-04-03 10:14:19 -04004827have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004828 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004829 u64 free_percent;
4830
4831 free_percent = btrfs_block_group_used(&block_group->item);
4832 free_percent *= 100;
4833 free_percent = div64_u64(free_percent,
4834 block_group->key.offset);
4835 free_percent = 100 - free_percent;
4836 if (free_percent > ideal_cache_percent &&
4837 likely(!block_group->ro)) {
4838 ideal_cache_offset = block_group->key.objectid;
4839 ideal_cache_percent = free_percent;
4840 }
4841
Josef Bacik817d52f2009-07-13 21:29:25 -04004842 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05004843 * We only want to start kthread caching if we are at
4844 * the point where we will wait for caching to make
4845 * progress, or if our ideal search is over and we've
4846 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04004847 */
4848 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05004849 (loop > LOOP_FIND_IDEAL &&
4850 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004851 ret = cache_block_group(block_group);
4852 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004853 }
Josef Bacikccf0e722009-11-10 21:23:48 -05004854 found_uncached_bg = true;
4855
4856 /*
4857 * If loop is set for cached only, try the next block
4858 * group.
4859 */
4860 if (loop == LOOP_FIND_IDEAL)
4861 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05004862 }
4863
Josef Bacik817d52f2009-07-13 21:29:25 -04004864 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05004865 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04004866 found_uncached_bg = true;
4867
Josef Bacikea6a4782008-11-20 12:16:16 -05004868 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004869 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004870
Josef Bacik0a243252009-09-11 16:11:20 -04004871 /*
4872 * Ok we want to try and use the cluster allocator, so lets look
4873 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4874 * have tried the cluster allocator plenty of times at this
4875 * point and not have found anything, so we are likely way too
4876 * fragmented for the clustering stuff to find anything, so lets
4877 * just skip it and let the allocator find whatever block it can
4878 * find
4879 */
4880 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004881 /*
4882 * the refill lock keeps out other
4883 * people trying to start a new cluster
4884 */
4885 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004886 if (last_ptr->block_group &&
4887 (last_ptr->block_group->ro ||
4888 !block_group_bits(last_ptr->block_group, data))) {
4889 offset = 0;
4890 goto refill_cluster;
4891 }
4892
Chris Masonfa9c0d792009-04-03 09:47:43 -04004893 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4894 num_bytes, search_start);
4895 if (offset) {
4896 /* we have a block, we're done */
4897 spin_unlock(&last_ptr->refill_lock);
4898 goto checks;
4899 }
4900
4901 spin_lock(&last_ptr->lock);
4902 /*
4903 * whoops, this cluster doesn't actually point to
4904 * this block group. Get a ref on the block
4905 * group is does point to and try again
4906 */
4907 if (!last_ptr_loop && last_ptr->block_group &&
4908 last_ptr->block_group != block_group) {
4909
4910 btrfs_put_block_group(block_group);
4911 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00004912 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004913 spin_unlock(&last_ptr->lock);
4914 spin_unlock(&last_ptr->refill_lock);
4915
4916 last_ptr_loop = 1;
4917 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04004918 /*
4919 * we know this block group is properly
4920 * in the list because
4921 * btrfs_remove_block_group, drops the
4922 * cluster before it removes the block
4923 * group from the list
4924 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04004925 goto have_block_group;
4926 }
4927 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004928refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04004929 /*
4930 * this cluster didn't work out, free it and
4931 * start over
4932 */
4933 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4934
4935 last_ptr_loop = 0;
4936
4937 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04004938 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04004939 block_group, last_ptr,
4940 offset, num_bytes,
4941 empty_cluster + empty_size);
4942 if (ret == 0) {
4943 /*
4944 * now pull our allocation out of this
4945 * cluster
4946 */
4947 offset = btrfs_alloc_from_cluster(block_group,
4948 last_ptr, num_bytes,
4949 search_start);
4950 if (offset) {
4951 /* we found one, proceed */
4952 spin_unlock(&last_ptr->refill_lock);
4953 goto checks;
4954 }
Josef Bacik0a243252009-09-11 16:11:20 -04004955 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4956 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004957 spin_unlock(&last_ptr->refill_lock);
4958
Josef Bacik0a243252009-09-11 16:11:20 -04004959 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004960 wait_block_group_cache_progress(block_group,
4961 num_bytes + empty_cluster + empty_size);
4962 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004963 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004964
Chris Masonfa9c0d792009-04-03 09:47:43 -04004965 /*
4966 * at this point we either didn't find a cluster
4967 * or we weren't able to allocate a block from our
4968 * cluster. Free the cluster we've been trying
4969 * to use, and go to the next block group
4970 */
Josef Bacik0a243252009-09-11 16:11:20 -04004971 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004972 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04004973 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004974 }
4975
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004976 offset = btrfs_find_space_for_alloc(block_group, search_start,
4977 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004978 /*
4979 * If we didn't find a chunk, and we haven't failed on this
4980 * block group before, and this block group is in the middle of
4981 * caching and we are ok with waiting, then go ahead and wait
4982 * for progress to be made, and set failed_alloc to true.
4983 *
4984 * If failed_alloc is true then we've already waited on this
4985 * block group once and should move on to the next block group.
4986 */
4987 if (!offset && !failed_alloc && !cached &&
4988 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004989 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004990 num_bytes + empty_size);
4991 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004992 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004993 } else if (!offset) {
4994 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04004995 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004996checks:
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004997 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04004998 /* move on to the next group */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04004999 if (search_start + num_bytes >= search_end) {
5000 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005001 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005002 }
Chris Masone37c9e62007-05-09 20:13:14 -04005003
Josef Bacik2552d172009-04-03 10:14:19 -04005004 /* move on to the next group */
5005 if (search_start + num_bytes >
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005006 block_group->key.objectid + block_group->key.offset) {
5007 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005008 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005009 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005010
Josef Bacik2552d172009-04-03 10:14:19 -04005011 ins->objectid = search_start;
5012 ins->offset = num_bytes;
5013
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005014 if (offset < search_start)
5015 btrfs_add_free_space(block_group, offset,
5016 search_start - offset);
5017 BUG_ON(offset > search_start);
5018
Yan, Zhengf0486c62010-05-16 10:46:25 -04005019 ret = update_reserved_bytes(block_group, num_bytes, 1,
5020 (data & BTRFS_BLOCK_GROUP_DATA));
5021 if (ret == -EAGAIN) {
5022 btrfs_add_free_space(block_group, offset, num_bytes);
5023 goto loop;
5024 }
Yan Zheng11833d62009-09-11 16:11:19 -04005025
Josef Bacik2552d172009-04-03 10:14:19 -04005026 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005027 ins->objectid = search_start;
5028 ins->offset = num_bytes;
5029
5030 if (offset < search_start)
5031 btrfs_add_free_space(block_group, offset,
5032 search_start - offset);
5033 BUG_ON(offset > search_start);
Josef Bacik2552d172009-04-03 10:14:19 -04005034 break;
5035loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005036 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005037 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005038 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d792009-04-03 09:47:43 -04005039 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005040 }
5041 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005042
Yan, Zhengb742bb82010-05-16 10:46:24 -04005043 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5044 goto search;
5045
Josef Bacikccf0e722009-11-10 21:23:48 -05005046 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5047 * for them to make caching progress. Also
5048 * determine the best possible bg to cache
5049 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5050 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005051 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5052 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5053 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5054 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04005055 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005056 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5057 (found_uncached_bg || empty_size || empty_cluster ||
5058 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005059 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005060 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005061 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005062 loop++;
5063 if (!ideal_cache_percent &&
5064 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005065 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005066
5067 /*
5068 * 1 of the following 2 things have happened so far
5069 *
5070 * 1) We found an ideal block group for caching that
5071 * is mostly full and will cache quickly, so we might
5072 * as well wait for it.
5073 *
5074 * 2) We searched for cached only and we didn't find
5075 * anything, and we didn't start any caching kthreads
5076 * either, so chances are we will loop through and
5077 * start a couple caching kthreads, and then come back
5078 * around and just wait for them. This will be slower
5079 * because we will have 2 caching kthreads reading at
5080 * the same time when we could have just started one
5081 * and waited for it to get far enough to give us an
5082 * allocation, so go ahead and go to the wait caching
5083 * loop.
5084 */
5085 loop = LOOP_CACHING_WAIT;
5086 search_start = ideal_cache_offset;
5087 ideal_cache_percent = 0;
5088 goto ideal_cache;
5089 } else if (loop == LOOP_FIND_IDEAL) {
5090 /*
5091 * Didn't find a uncached bg, wait on anything we find
5092 * next.
5093 */
5094 loop = LOOP_CACHING_WAIT;
5095 goto search;
5096 }
5097
5098 if (loop < LOOP_CACHING_WAIT) {
5099 loop++;
5100 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005101 }
5102
5103 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005104 empty_size = 0;
5105 empty_cluster = 0;
5106 }
Chris Mason42e70e72008-11-07 18:17:11 -05005107
Josef Bacik2552d172009-04-03 10:14:19 -04005108 if (allowed_chunk_alloc) {
5109 ret = do_chunk_alloc(trans, root, num_bytes +
5110 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04005111 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005112 done_chunk_alloc = 1;
5113 } else if (!done_chunk_alloc) {
Josef Bacik2552d172009-04-03 10:14:19 -04005114 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005115 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005116
Josef Bacik817d52f2009-07-13 21:29:25 -04005117 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005118 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005119 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005120 }
Josef Bacik2552d172009-04-03 10:14:19 -04005121 ret = -ENOSPC;
5122 } else if (!ins->objectid) {
5123 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04005124 }
Chris Mason0b86a832008-03-24 15:01:56 -04005125
Josef Bacik80eb2342008-10-29 14:49:05 -04005126 /* we found what we needed */
5127 if (ins->objectid) {
5128 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05005129 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04005130
Chris Masonfa9c0d792009-04-03 09:47:43 -04005131 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005132 ret = 0;
5133 }
Chris Mason0b86a832008-03-24 15:01:56 -04005134
Chris Mason0f70abe2007-02-28 16:46:22 -05005135 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005136}
Chris Masonec44a352008-04-28 15:29:52 -04005137
Josef Bacik9ed74f22009-09-11 16:12:44 -04005138static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5139 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005140{
5141 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005142 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005143
Josef Bacik9ed74f22009-09-11 16:12:44 -04005144 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005145 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5146 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005147 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005148 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005149 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005150 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5151 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005152 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005153 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005154 (unsigned long long)info->bytes_pinned,
5155 (unsigned long long)info->bytes_reserved,
5156 (unsigned long long)info->bytes_may_use,
5157 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005158 spin_unlock(&info->lock);
5159
5160 if (!dump_block_groups)
5161 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005162
Josef Bacik80eb2342008-10-29 14:49:05 -04005163 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005164again:
5165 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005166 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005167 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5168 "%llu pinned %llu reserved\n",
5169 (unsigned long long)cache->key.objectid,
5170 (unsigned long long)cache->key.offset,
5171 (unsigned long long)btrfs_block_group_used(&cache->item),
5172 (unsigned long long)cache->pinned,
5173 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005174 btrfs_dump_free_space(cache, bytes);
5175 spin_unlock(&cache->lock);
5176 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005177 if (++index < BTRFS_NR_RAID_TYPES)
5178 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005179 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005180}
Zheng Yane8569812008-09-26 10:05:48 -04005181
Yan Zheng11833d62009-09-11 16:11:19 -04005182int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5183 struct btrfs_root *root,
5184 u64 num_bytes, u64 min_alloc_size,
5185 u64 empty_size, u64 hint_byte,
5186 u64 search_end, struct btrfs_key *ins,
5187 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005188{
5189 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005190 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005191
Josef Bacik6a632092009-02-20 11:00:09 -05005192 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005193again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005194 /*
5195 * the only place that sets empty_size is btrfs_realloc_node, which
5196 * is not called recursively on allocations
5197 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005198 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005199 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005200 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04005201
Chris Masondb945352007-10-15 16:15:53 -04005202 WARN_ON(num_bytes < root->sectorsize);
5203 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005204 search_start, search_end, hint_byte,
5205 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005206
Chris Mason98d20f62008-04-14 09:46:10 -04005207 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5208 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005209 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005210 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005211 do_chunk_alloc(trans, root->fs_info->extent_root,
5212 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005213 goto again;
5214 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005215 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005216 struct btrfs_space_info *sinfo;
5217
5218 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005219 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5220 "wanted %llu\n", (unsigned long long)data,
5221 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005222 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005223 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005224
5225 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005226}
5227
Chris Mason65b51a02008-08-01 15:11:20 -04005228int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5229{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005230 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005231 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005232
Josef Bacik0f9dd462008-09-23 13:14:11 -04005233 cache = btrfs_lookup_block_group(root->fs_info, start);
5234 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005235 printk(KERN_ERR "Unable to find block group for %llu\n",
5236 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005237 return -ENOSPC;
5238 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005239
5240 ret = btrfs_discard_extent(root, start, len);
5241
Josef Bacik0f9dd462008-09-23 13:14:11 -04005242 btrfs_add_free_space(cache, start, len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005243 update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005244 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005245
Chris Masone6dcd2d2008-07-17 12:53:50 -04005246 return ret;
5247}
5248
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005249static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5250 struct btrfs_root *root,
5251 u64 parent, u64 root_objectid,
5252 u64 flags, u64 owner, u64 offset,
5253 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005254{
5255 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005256 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005257 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005258 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005259 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005260 struct extent_buffer *leaf;
5261 int type;
5262 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005263
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005264 if (parent > 0)
5265 type = BTRFS_SHARED_DATA_REF_KEY;
5266 else
5267 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005268
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005269 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005270
5271 path = btrfs_alloc_path();
5272 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05005273
Chris Masonb9473432009-03-13 11:00:37 -04005274 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005275 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5276 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005277 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005278
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005279 leaf = path->nodes[0];
5280 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005281 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005282 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5283 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5284 btrfs_set_extent_flags(leaf, extent_item,
5285 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005286
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005287 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5288 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5289 if (parent > 0) {
5290 struct btrfs_shared_data_ref *ref;
5291 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5292 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5293 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5294 } else {
5295 struct btrfs_extent_data_ref *ref;
5296 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5297 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5298 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5299 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5300 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5301 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005302
5303 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005304 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005305
Yan, Zhengf0486c62010-05-16 10:46:25 -04005306 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005307 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005308 printk(KERN_ERR "btrfs update block group failed for %llu "
5309 "%llu\n", (unsigned long long)ins->objectid,
5310 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005311 BUG();
5312 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005313 return ret;
5314}
5315
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005316static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5317 struct btrfs_root *root,
5318 u64 parent, u64 root_objectid,
5319 u64 flags, struct btrfs_disk_key *key,
5320 int level, struct btrfs_key *ins)
5321{
5322 int ret;
5323 struct btrfs_fs_info *fs_info = root->fs_info;
5324 struct btrfs_extent_item *extent_item;
5325 struct btrfs_tree_block_info *block_info;
5326 struct btrfs_extent_inline_ref *iref;
5327 struct btrfs_path *path;
5328 struct extent_buffer *leaf;
5329 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5330
5331 path = btrfs_alloc_path();
5332 BUG_ON(!path);
5333
5334 path->leave_spinning = 1;
5335 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5336 ins, size);
5337 BUG_ON(ret);
5338
5339 leaf = path->nodes[0];
5340 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5341 struct btrfs_extent_item);
5342 btrfs_set_extent_refs(leaf, extent_item, 1);
5343 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5344 btrfs_set_extent_flags(leaf, extent_item,
5345 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5346 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5347
5348 btrfs_set_tree_block_key(leaf, block_info, key);
5349 btrfs_set_tree_block_level(leaf, block_info, level);
5350
5351 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5352 if (parent > 0) {
5353 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5354 btrfs_set_extent_inline_ref_type(leaf, iref,
5355 BTRFS_SHARED_BLOCK_REF_KEY);
5356 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5357 } else {
5358 btrfs_set_extent_inline_ref_type(leaf, iref,
5359 BTRFS_TREE_BLOCK_REF_KEY);
5360 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5361 }
5362
5363 btrfs_mark_buffer_dirty(leaf);
5364 btrfs_free_path(path);
5365
Yan, Zhengf0486c62010-05-16 10:46:25 -04005366 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005367 if (ret) {
5368 printk(KERN_ERR "btrfs update block group failed for %llu "
5369 "%llu\n", (unsigned long long)ins->objectid,
5370 (unsigned long long)ins->offset);
5371 BUG();
5372 }
5373 return ret;
5374}
5375
5376int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5377 struct btrfs_root *root,
5378 u64 root_objectid, u64 owner,
5379 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005380{
5381 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005382
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005383 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005384
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005385 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5386 0, root_objectid, owner, offset,
5387 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005388 return ret;
5389}
Chris Masone02119d2008-09-05 16:13:11 -04005390
5391/*
5392 * this is used by the tree logging recovery code. It records that
5393 * an extent has been allocated and makes sure to clear the free
5394 * space cache bits as well
5395 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005396int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5397 struct btrfs_root *root,
5398 u64 root_objectid, u64 owner, u64 offset,
5399 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005400{
5401 int ret;
5402 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005403 struct btrfs_caching_control *caching_ctl;
5404 u64 start = ins->objectid;
5405 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005406
Chris Masone02119d2008-09-05 16:13:11 -04005407 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04005408 cache_block_group(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04005409 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005410
Yan Zheng11833d62009-09-11 16:11:19 -04005411 if (!caching_ctl) {
5412 BUG_ON(!block_group_cache_done(block_group));
5413 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5414 BUG_ON(ret);
5415 } else {
5416 mutex_lock(&caching_ctl->mutex);
5417
5418 if (start >= caching_ctl->progress) {
5419 ret = add_excluded_extent(root, start, num_bytes);
5420 BUG_ON(ret);
5421 } else if (start + num_bytes <= caching_ctl->progress) {
5422 ret = btrfs_remove_free_space(block_group,
5423 start, num_bytes);
5424 BUG_ON(ret);
5425 } else {
5426 num_bytes = caching_ctl->progress - start;
5427 ret = btrfs_remove_free_space(block_group,
5428 start, num_bytes);
5429 BUG_ON(ret);
5430
5431 start = caching_ctl->progress;
5432 num_bytes = ins->objectid + ins->offset -
5433 caching_ctl->progress;
5434 ret = add_excluded_extent(root, start, num_bytes);
5435 BUG_ON(ret);
5436 }
5437
5438 mutex_unlock(&caching_ctl->mutex);
5439 put_caching_control(caching_ctl);
5440 }
5441
Yan, Zhengf0486c62010-05-16 10:46:25 -04005442 ret = update_reserved_bytes(block_group, ins->offset, 1, 1);
5443 BUG_ON(ret);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005444 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005445 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5446 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005447 return ret;
5448}
5449
Chris Mason65b51a02008-08-01 15:11:20 -04005450struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5451 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005452 u64 bytenr, u32 blocksize,
5453 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005454{
5455 struct extent_buffer *buf;
5456
5457 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5458 if (!buf)
5459 return ERR_PTR(-ENOMEM);
5460 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005461 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005462 btrfs_tree_lock(buf);
5463 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005464
5465 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005466 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005467
Chris Masond0c803c2008-09-11 16:17:57 -04005468 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005469 /*
5470 * we allow two log transactions at a time, use different
5471 * EXENT bit to differentiate dirty pages.
5472 */
5473 if (root->log_transid % 2 == 0)
5474 set_extent_dirty(&root->dirty_log_pages, buf->start,
5475 buf->start + buf->len - 1, GFP_NOFS);
5476 else
5477 set_extent_new(&root->dirty_log_pages, buf->start,
5478 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005479 } else {
5480 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5481 buf->start + buf->len - 1, GFP_NOFS);
5482 }
Chris Mason65b51a02008-08-01 15:11:20 -04005483 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005484 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005485 return buf;
5486}
5487
Yan, Zhengf0486c62010-05-16 10:46:25 -04005488static struct btrfs_block_rsv *
5489use_block_rsv(struct btrfs_trans_handle *trans,
5490 struct btrfs_root *root, u32 blocksize)
5491{
5492 struct btrfs_block_rsv *block_rsv;
5493 int ret;
5494
5495 block_rsv = get_block_rsv(trans, root);
5496
5497 if (block_rsv->size == 0) {
5498 ret = reserve_metadata_bytes(block_rsv, blocksize);
5499 if (ret)
5500 return ERR_PTR(ret);
5501 return block_rsv;
5502 }
5503
5504 ret = block_rsv_use_bytes(block_rsv, blocksize);
5505 if (!ret)
5506 return block_rsv;
5507
5508 WARN_ON(1);
5509 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
5510 block_rsv->size, block_rsv->reserved,
5511 block_rsv->freed[0], block_rsv->freed[1]);
5512
5513 return ERR_PTR(-ENOSPC);
5514}
5515
5516static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5517{
5518 block_rsv_add_bytes(block_rsv, blocksize, 0);
5519 block_rsv_release_bytes(block_rsv, NULL, 0);
5520}
5521
Chris Masonfec577f2007-02-26 10:40:21 -05005522/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005523 * finds a free extent and does all the dirty work required for allocation
5524 * returns the key for the extent through ins, and a tree buffer for
5525 * the first block of the extent through buf.
5526 *
Chris Masonfec577f2007-02-26 10:40:21 -05005527 * returns the tree buffer or NULL.
5528 */
Chris Mason5f39d392007-10-15 16:14:19 -04005529struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005530 struct btrfs_root *root, u32 blocksize,
5531 u64 parent, u64 root_objectid,
5532 struct btrfs_disk_key *key, int level,
5533 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005534{
Chris Masone2fa7222007-03-12 16:22:34 -04005535 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005536 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005537 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005538 u64 flags = 0;
5539 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005540
Yan, Zhengf0486c62010-05-16 10:46:25 -04005541
5542 block_rsv = use_block_rsv(trans, root, blocksize);
5543 if (IS_ERR(block_rsv))
5544 return ERR_CAST(block_rsv);
5545
5546 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5547 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005548 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005549 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005550 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005551 }
Chris Mason55c69072008-01-09 15:55:33 -05005552
Chris Mason4008c042009-02-12 14:09:45 -05005553 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5554 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005555 BUG_ON(IS_ERR(buf));
5556
5557 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5558 if (parent == 0)
5559 parent = ins.objectid;
5560 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5561 } else
5562 BUG_ON(parent > 0);
5563
5564 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5565 struct btrfs_delayed_extent_op *extent_op;
5566 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5567 BUG_ON(!extent_op);
5568 if (key)
5569 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5570 else
5571 memset(&extent_op->key, 0, sizeof(extent_op->key));
5572 extent_op->flags_to_set = flags;
5573 extent_op->update_key = 1;
5574 extent_op->update_flags = 1;
5575 extent_op->is_data = 0;
5576
5577 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5578 ins.offset, parent, root_objectid,
5579 level, BTRFS_ADD_DELAYED_EXTENT,
5580 extent_op);
5581 BUG_ON(ret);
5582 }
Chris Masonfec577f2007-02-26 10:40:21 -05005583 return buf;
5584}
Chris Masona28ec192007-03-06 20:08:01 -05005585
Yan Zheng2c47e6052009-06-27 21:07:35 -04005586struct walk_control {
5587 u64 refs[BTRFS_MAX_LEVEL];
5588 u64 flags[BTRFS_MAX_LEVEL];
5589 struct btrfs_key update_progress;
5590 int stage;
5591 int level;
5592 int shared_level;
5593 int update_ref;
5594 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005595 int reada_slot;
5596 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005597};
5598
5599#define DROP_REFERENCE 1
5600#define UPDATE_BACKREF 2
5601
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005602static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5603 struct btrfs_root *root,
5604 struct walk_control *wc,
5605 struct btrfs_path *path)
5606{
5607 u64 bytenr;
5608 u64 generation;
5609 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005610 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005611 u64 last = 0;
5612 u32 nritems;
5613 u32 blocksize;
5614 struct btrfs_key key;
5615 struct extent_buffer *eb;
5616 int ret;
5617 int slot;
5618 int nread = 0;
5619
5620 if (path->slots[wc->level] < wc->reada_slot) {
5621 wc->reada_count = wc->reada_count * 2 / 3;
5622 wc->reada_count = max(wc->reada_count, 2);
5623 } else {
5624 wc->reada_count = wc->reada_count * 3 / 2;
5625 wc->reada_count = min_t(int, wc->reada_count,
5626 BTRFS_NODEPTRS_PER_BLOCK(root));
5627 }
5628
5629 eb = path->nodes[wc->level];
5630 nritems = btrfs_header_nritems(eb);
5631 blocksize = btrfs_level_size(root, wc->level - 1);
5632
5633 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5634 if (nread >= wc->reada_count)
5635 break;
5636
5637 cond_resched();
5638 bytenr = btrfs_node_blockptr(eb, slot);
5639 generation = btrfs_node_ptr_generation(eb, slot);
5640
5641 if (slot == path->slots[wc->level])
5642 goto reada;
5643
5644 if (wc->stage == UPDATE_BACKREF &&
5645 generation <= root->root_key.offset)
5646 continue;
5647
Yan, Zheng94fcca92009-10-09 09:25:16 -04005648 /* We don't lock the tree block, it's OK to be racy here */
5649 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5650 &refs, &flags);
5651 BUG_ON(ret);
5652 BUG_ON(refs == 0);
5653
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005654 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005655 if (refs == 1)
5656 goto reada;
5657
Yan, Zheng94fcca92009-10-09 09:25:16 -04005658 if (wc->level == 1 &&
5659 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5660 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005661 if (!wc->update_ref ||
5662 generation <= root->root_key.offset)
5663 continue;
5664 btrfs_node_key_to_cpu(eb, &key, slot);
5665 ret = btrfs_comp_cpu_keys(&key,
5666 &wc->update_progress);
5667 if (ret < 0)
5668 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005669 } else {
5670 if (wc->level == 1 &&
5671 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5672 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005673 }
5674reada:
5675 ret = readahead_tree_block(root, bytenr, blocksize,
5676 generation);
5677 if (ret)
5678 break;
5679 last = bytenr + blocksize;
5680 nread++;
5681 }
5682 wc->reada_slot = slot;
5683}
5684
Chris Mason9aca1d52007-03-13 11:09:37 -04005685/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005686 * hepler to process tree block while walking down the tree.
5687 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005688 * when wc->stage == UPDATE_BACKREF, this function updates
5689 * back refs for pointers in the block.
5690 *
5691 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005692 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005693static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5694 struct btrfs_root *root,
5695 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005696 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005697{
5698 int level = wc->level;
5699 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005700 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5701 int ret;
5702
5703 if (wc->stage == UPDATE_BACKREF &&
5704 btrfs_header_owner(eb) != root->root_key.objectid)
5705 return 1;
5706
5707 /*
5708 * when reference count of tree block is 1, it won't increase
5709 * again. once full backref flag is set, we never clear it.
5710 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005711 if (lookup_info &&
5712 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5713 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005714 BUG_ON(!path->locks[level]);
5715 ret = btrfs_lookup_extent_info(trans, root,
5716 eb->start, eb->len,
5717 &wc->refs[level],
5718 &wc->flags[level]);
5719 BUG_ON(ret);
5720 BUG_ON(wc->refs[level] == 0);
5721 }
5722
Yan Zheng2c47e6052009-06-27 21:07:35 -04005723 if (wc->stage == DROP_REFERENCE) {
5724 if (wc->refs[level] > 1)
5725 return 1;
5726
5727 if (path->locks[level] && !wc->keep_locks) {
5728 btrfs_tree_unlock(eb);
5729 path->locks[level] = 0;
5730 }
5731 return 0;
5732 }
5733
5734 /* wc->stage == UPDATE_BACKREF */
5735 if (!(wc->flags[level] & flag)) {
5736 BUG_ON(!path->locks[level]);
5737 ret = btrfs_inc_ref(trans, root, eb, 1);
5738 BUG_ON(ret);
5739 ret = btrfs_dec_ref(trans, root, eb, 0);
5740 BUG_ON(ret);
5741 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5742 eb->len, flag, 0);
5743 BUG_ON(ret);
5744 wc->flags[level] |= flag;
5745 }
5746
5747 /*
5748 * the block is shared by multiple trees, so it's not good to
5749 * keep the tree lock
5750 */
5751 if (path->locks[level] && level > 0) {
5752 btrfs_tree_unlock(eb);
5753 path->locks[level] = 0;
5754 }
5755 return 0;
5756}
5757
5758/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005759 * hepler to process tree block pointer.
5760 *
5761 * when wc->stage == DROP_REFERENCE, this function checks
5762 * reference count of the block pointed to. if the block
5763 * is shared and we need update back refs for the subtree
5764 * rooted at the block, this function changes wc->stage to
5765 * UPDATE_BACKREF. if the block is shared and there is no
5766 * need to update back, this function drops the reference
5767 * to the block.
5768 *
5769 * NOTE: return value 1 means we should stop walking down.
5770 */
5771static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5772 struct btrfs_root *root,
5773 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005774 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005775{
5776 u64 bytenr;
5777 u64 generation;
5778 u64 parent;
5779 u32 blocksize;
5780 struct btrfs_key key;
5781 struct extent_buffer *next;
5782 int level = wc->level;
5783 int reada = 0;
5784 int ret = 0;
5785
5786 generation = btrfs_node_ptr_generation(path->nodes[level],
5787 path->slots[level]);
5788 /*
5789 * if the lower level block was created before the snapshot
5790 * was created, we know there is no need to update back refs
5791 * for the subtree
5792 */
5793 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005794 generation <= root->root_key.offset) {
5795 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005796 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005797 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005798
5799 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5800 blocksize = btrfs_level_size(root, level - 1);
5801
5802 next = btrfs_find_tree_block(root, bytenr, blocksize);
5803 if (!next) {
5804 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00005805 if (!next)
5806 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005807 reada = 1;
5808 }
5809 btrfs_tree_lock(next);
5810 btrfs_set_lock_blocking(next);
5811
Yan, Zheng94fcca92009-10-09 09:25:16 -04005812 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5813 &wc->refs[level - 1],
5814 &wc->flags[level - 1]);
5815 BUG_ON(ret);
5816 BUG_ON(wc->refs[level - 1] == 0);
5817 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005818
Yan, Zheng94fcca92009-10-09 09:25:16 -04005819 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005820 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005821 if (level == 1 &&
5822 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5823 goto skip;
5824
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005825 if (!wc->update_ref ||
5826 generation <= root->root_key.offset)
5827 goto skip;
5828
5829 btrfs_node_key_to_cpu(path->nodes[level], &key,
5830 path->slots[level]);
5831 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5832 if (ret < 0)
5833 goto skip;
5834
5835 wc->stage = UPDATE_BACKREF;
5836 wc->shared_level = level - 1;
5837 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005838 } else {
5839 if (level == 1 &&
5840 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5841 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005842 }
5843
5844 if (!btrfs_buffer_uptodate(next, generation)) {
5845 btrfs_tree_unlock(next);
5846 free_extent_buffer(next);
5847 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005848 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005849 }
5850
5851 if (!next) {
5852 if (reada && level == 1)
5853 reada_walk_down(trans, root, wc, path);
5854 next = read_tree_block(root, bytenr, blocksize, generation);
5855 btrfs_tree_lock(next);
5856 btrfs_set_lock_blocking(next);
5857 }
5858
5859 level--;
5860 BUG_ON(level != btrfs_header_level(next));
5861 path->nodes[level] = next;
5862 path->slots[level] = 0;
5863 path->locks[level] = 1;
5864 wc->level = level;
5865 if (wc->level == 1)
5866 wc->reada_slot = 0;
5867 return 0;
5868skip:
5869 wc->refs[level - 1] = 0;
5870 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005871 if (wc->stage == DROP_REFERENCE) {
5872 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5873 parent = path->nodes[level]->start;
5874 } else {
5875 BUG_ON(root->root_key.objectid !=
5876 btrfs_header_owner(path->nodes[level]));
5877 parent = 0;
5878 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005879
Yan, Zheng94fcca92009-10-09 09:25:16 -04005880 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5881 root->root_key.objectid, level - 1, 0);
5882 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005883 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005884 btrfs_tree_unlock(next);
5885 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04005886 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005887 return 1;
5888}
5889
5890/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005891 * hepler to process tree block while walking up the tree.
5892 *
5893 * when wc->stage == DROP_REFERENCE, this function drops
5894 * reference count on the block.
5895 *
5896 * when wc->stage == UPDATE_BACKREF, this function changes
5897 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5898 * to UPDATE_BACKREF previously while processing the block.
5899 *
5900 * NOTE: return value 1 means we should stop walking up.
5901 */
5902static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5903 struct btrfs_root *root,
5904 struct btrfs_path *path,
5905 struct walk_control *wc)
5906{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005907 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005908 int level = wc->level;
5909 struct extent_buffer *eb = path->nodes[level];
5910 u64 parent = 0;
5911
5912 if (wc->stage == UPDATE_BACKREF) {
5913 BUG_ON(wc->shared_level < level);
5914 if (level < wc->shared_level)
5915 goto out;
5916
Yan Zheng2c47e6052009-06-27 21:07:35 -04005917 ret = find_next_key(path, level + 1, &wc->update_progress);
5918 if (ret > 0)
5919 wc->update_ref = 0;
5920
5921 wc->stage = DROP_REFERENCE;
5922 wc->shared_level = -1;
5923 path->slots[level] = 0;
5924
5925 /*
5926 * check reference count again if the block isn't locked.
5927 * we should start walking down the tree again if reference
5928 * count is one.
5929 */
5930 if (!path->locks[level]) {
5931 BUG_ON(level == 0);
5932 btrfs_tree_lock(eb);
5933 btrfs_set_lock_blocking(eb);
5934 path->locks[level] = 1;
5935
5936 ret = btrfs_lookup_extent_info(trans, root,
5937 eb->start, eb->len,
5938 &wc->refs[level],
5939 &wc->flags[level]);
5940 BUG_ON(ret);
5941 BUG_ON(wc->refs[level] == 0);
5942 if (wc->refs[level] == 1) {
5943 btrfs_tree_unlock(eb);
5944 path->locks[level] = 0;
5945 return 1;
5946 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005947 }
5948 }
5949
5950 /* wc->stage == DROP_REFERENCE */
5951 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5952
5953 if (wc->refs[level] == 1) {
5954 if (level == 0) {
5955 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5956 ret = btrfs_dec_ref(trans, root, eb, 1);
5957 else
5958 ret = btrfs_dec_ref(trans, root, eb, 0);
5959 BUG_ON(ret);
5960 }
5961 /* make block locked assertion in clean_tree_block happy */
5962 if (!path->locks[level] &&
5963 btrfs_header_generation(eb) == trans->transid) {
5964 btrfs_tree_lock(eb);
5965 btrfs_set_lock_blocking(eb);
5966 path->locks[level] = 1;
5967 }
5968 clean_tree_block(trans, root, eb);
5969 }
5970
5971 if (eb == root->node) {
5972 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5973 parent = eb->start;
5974 else
5975 BUG_ON(root->root_key.objectid !=
5976 btrfs_header_owner(eb));
5977 } else {
5978 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5979 parent = path->nodes[level + 1]->start;
5980 else
5981 BUG_ON(root->root_key.objectid !=
5982 btrfs_header_owner(path->nodes[level + 1]));
5983 }
5984
Yan, Zhengf0486c62010-05-16 10:46:25 -04005985 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005986out:
5987 wc->refs[level] = 0;
5988 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005989 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005990}
5991
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005992static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5993 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005994 struct btrfs_path *path,
5995 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005996{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005997 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005998 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005999 int ret;
6000
Yan Zheng2c47e6052009-06-27 21:07:35 -04006001 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006002 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006003 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006004 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006005
Yan Zheng2c47e6052009-06-27 21:07:35 -04006006 if (level == 0)
6007 break;
6008
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006009 if (path->slots[level] >=
6010 btrfs_header_nritems(path->nodes[level]))
6011 break;
6012
Yan, Zheng94fcca92009-10-09 09:25:16 -04006013 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006014 if (ret > 0) {
6015 path->slots[level]++;
6016 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006017 } else if (ret < 0)
6018 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006019 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006020 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006021 return 0;
6022}
6023
Chris Masond3977122009-01-05 21:25:51 -05006024static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006025 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006026 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006027 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006028{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006029 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006030 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006031
Yan Zheng2c47e6052009-06-27 21:07:35 -04006032 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6033 while (level < max_level && path->nodes[level]) {
6034 wc->level = level;
6035 if (path->slots[level] + 1 <
6036 btrfs_header_nritems(path->nodes[level])) {
6037 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006038 return 0;
6039 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006040 ret = walk_up_proc(trans, root, path, wc);
6041 if (ret > 0)
6042 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006043
Yan Zheng2c47e6052009-06-27 21:07:35 -04006044 if (path->locks[level]) {
6045 btrfs_tree_unlock(path->nodes[level]);
6046 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006047 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006048 free_extent_buffer(path->nodes[level]);
6049 path->nodes[level] = NULL;
6050 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006051 }
6052 }
6053 return 1;
6054}
6055
Chris Mason9aca1d52007-03-13 11:09:37 -04006056/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006057 * drop a subvolume tree.
6058 *
6059 * this function traverses the tree freeing any blocks that only
6060 * referenced by the tree.
6061 *
6062 * when a shared tree block is found. this function decreases its
6063 * reference count by one. if update_ref is true, this function
6064 * also make sure backrefs for the shared block and all lower level
6065 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006066 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006067int btrfs_drop_snapshot(struct btrfs_root *root,
6068 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006069{
Chris Mason5caf2a02007-04-02 11:20:42 -04006070 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006071 struct btrfs_trans_handle *trans;
6072 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006073 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006074 struct walk_control *wc;
6075 struct btrfs_key key;
6076 int err = 0;
6077 int ret;
6078 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006079
Chris Mason5caf2a02007-04-02 11:20:42 -04006080 path = btrfs_alloc_path();
6081 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006082
Yan Zheng2c47e6052009-06-27 21:07:35 -04006083 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6084 BUG_ON(!wc);
6085
Yan, Zhenga22285a2010-05-16 10:48:46 -04006086 trans = btrfs_start_transaction(tree_root, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006087 if (block_rsv)
6088 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006089
Chris Mason9f3a7422007-08-07 15:52:19 -04006090 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006091 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006092 path->nodes[level] = btrfs_lock_root_node(root);
6093 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006094 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006095 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006096 memset(&wc->update_progress, 0,
6097 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006098 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006099 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006100 memcpy(&wc->update_progress, &key,
6101 sizeof(wc->update_progress));
6102
Chris Mason6702ed42007-08-07 16:15:09 -04006103 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006104 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006105 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006106 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6107 path->lowest_level = 0;
6108 if (ret < 0) {
6109 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006110 goto out;
6111 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006112 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006113
Chris Mason7d9eb122008-07-08 14:19:17 -04006114 /*
6115 * unlock our path, this is safe because only this
6116 * function is allowed to delete this snapshot
6117 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006118 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006119
Yan Zheng2c47e6052009-06-27 21:07:35 -04006120 level = btrfs_header_level(root->node);
6121 while (1) {
6122 btrfs_tree_lock(path->nodes[level]);
6123 btrfs_set_lock_blocking(path->nodes[level]);
6124
6125 ret = btrfs_lookup_extent_info(trans, root,
6126 path->nodes[level]->start,
6127 path->nodes[level]->len,
6128 &wc->refs[level],
6129 &wc->flags[level]);
6130 BUG_ON(ret);
6131 BUG_ON(wc->refs[level] == 0);
6132
6133 if (level == root_item->drop_level)
6134 break;
6135
6136 btrfs_tree_unlock(path->nodes[level]);
6137 WARN_ON(wc->refs[level] != 1);
6138 level--;
6139 }
6140 }
6141
6142 wc->level = level;
6143 wc->shared_level = -1;
6144 wc->stage = DROP_REFERENCE;
6145 wc->update_ref = update_ref;
6146 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006147 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006148
6149 while (1) {
6150 ret = walk_down_tree(trans, root, path, wc);
6151 if (ret < 0) {
6152 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006153 break;
6154 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006155
6156 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6157 if (ret < 0) {
6158 err = ret;
6159 break;
6160 }
6161
6162 if (ret > 0) {
6163 BUG_ON(wc->stage != DROP_REFERENCE);
6164 break;
6165 }
6166
6167 if (wc->stage == DROP_REFERENCE) {
6168 level = wc->level;
6169 btrfs_node_key(path->nodes[level],
6170 &root_item->drop_progress,
6171 path->slots[level]);
6172 root_item->drop_level = level;
6173 }
6174
6175 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006176 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006177 ret = btrfs_update_root(trans, tree_root,
6178 &root->root_key,
6179 root_item);
6180 BUG_ON(ret);
6181
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006182 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006183 trans = btrfs_start_transaction(tree_root, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006184 if (block_rsv)
6185 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006186 }
Chris Mason20524f02007-03-10 06:35:47 -05006187 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006188 btrfs_release_path(root, path);
6189 BUG_ON(err);
6190
6191 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6192 BUG_ON(ret);
6193
Yan, Zheng76dda932009-09-21 16:00:26 -04006194 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6195 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6196 NULL, NULL);
6197 BUG_ON(ret < 0);
6198 if (ret > 0) {
6199 ret = btrfs_del_orphan_item(trans, tree_root,
6200 root->root_key.objectid);
6201 BUG_ON(ret);
6202 }
6203 }
6204
6205 if (root->in_radix) {
6206 btrfs_free_fs_root(tree_root->fs_info, root);
6207 } else {
6208 free_extent_buffer(root->node);
6209 free_extent_buffer(root->commit_root);
6210 kfree(root);
6211 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006212out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006213 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006214 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006215 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006216 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006217}
Chris Mason9078a3e2007-04-26 16:46:15 -04006218
Yan Zheng2c47e6052009-06-27 21:07:35 -04006219/*
6220 * drop subtree rooted at tree block 'node'.
6221 *
6222 * NOTE: this function will unlock and release tree block 'node'
6223 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006224int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6225 struct btrfs_root *root,
6226 struct extent_buffer *node,
6227 struct extent_buffer *parent)
6228{
6229 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006230 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006231 int level;
6232 int parent_level;
6233 int ret = 0;
6234 int wret;
6235
Yan Zheng2c47e6052009-06-27 21:07:35 -04006236 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6237
Yan Zhengf82d02d2008-10-29 14:49:05 -04006238 path = btrfs_alloc_path();
6239 BUG_ON(!path);
6240
Yan Zheng2c47e6052009-06-27 21:07:35 -04006241 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6242 BUG_ON(!wc);
6243
Chris Masonb9447ef2009-03-09 11:45:38 -04006244 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006245 parent_level = btrfs_header_level(parent);
6246 extent_buffer_get(parent);
6247 path->nodes[parent_level] = parent;
6248 path->slots[parent_level] = btrfs_header_nritems(parent);
6249
Chris Masonb9447ef2009-03-09 11:45:38 -04006250 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006251 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006252 path->nodes[level] = node;
6253 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006254 path->locks[level] = 1;
6255
6256 wc->refs[parent_level] = 1;
6257 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6258 wc->level = level;
6259 wc->shared_level = -1;
6260 wc->stage = DROP_REFERENCE;
6261 wc->update_ref = 0;
6262 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006263 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006264
6265 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006266 wret = walk_down_tree(trans, root, path, wc);
6267 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006268 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006269 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006270 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006271
Yan Zheng2c47e6052009-06-27 21:07:35 -04006272 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006273 if (wret < 0)
6274 ret = wret;
6275 if (wret != 0)
6276 break;
6277 }
6278
Yan Zheng2c47e6052009-06-27 21:07:35 -04006279 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006280 btrfs_free_path(path);
6281 return ret;
6282}
6283
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006284#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04006285static unsigned long calc_ra(unsigned long start, unsigned long last,
6286 unsigned long nr)
6287{
6288 return min(last, start + nr - 1);
6289}
6290
Chris Masond3977122009-01-05 21:25:51 -05006291static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05006292 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05006293{
6294 u64 page_start;
6295 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04006296 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006297 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006298 unsigned long i;
6299 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05006300 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05006301 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04006302 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04006303 unsigned int total_read = 0;
6304 unsigned int total_dirty = 0;
6305 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05006306
6307 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006308
6309 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006310 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05006311 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6312
Zheng Yan1a40e232008-09-26 10:09:34 -04006313 /* make sure the dirty trick played by the caller work */
6314 ret = invalidate_inode_pages2_range(inode->i_mapping,
6315 first_index, last_index);
6316 if (ret)
6317 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04006318
Chris Mason4313b392008-01-03 09:08:48 -05006319 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05006320
Zheng Yan1a40e232008-09-26 10:09:34 -04006321 for (i = first_index ; i <= last_index; i++) {
6322 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04006323 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04006324 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04006325 }
6326 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04006327again:
6328 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04006329 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05006330 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04006331 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006332 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006333 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04006334 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006335 if (!PageUptodate(page)) {
6336 btrfs_readpage(NULL, page);
6337 lock_page(page);
6338 if (!PageUptodate(page)) {
6339 unlock_page(page);
6340 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006341 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05006342 goto out_unlock;
6343 }
6344 }
Chris Masonec44a352008-04-28 15:29:52 -04006345 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04006346
Chris Masonedbd8d42007-12-21 16:27:24 -05006347 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6348 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006349 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006350
Chris Mason3eaa2882008-07-24 11:57:52 -04006351 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6352 if (ordered) {
6353 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6354 unlock_page(page);
6355 page_cache_release(page);
6356 btrfs_start_ordered_extent(inode, ordered, 1);
6357 btrfs_put_ordered_extent(ordered);
6358 goto again;
6359 }
6360 set_page_extent_mapped(page);
6361
Zheng Yan1a40e232008-09-26 10:09:34 -04006362 if (i == first_index)
6363 set_extent_bits(io_tree, page_start, page_end,
6364 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006365 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04006366
Chris Masona061fc82008-05-07 11:43:44 -04006367 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006368 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006369
Chris Masond1310b22008-01-24 16:13:08 -05006370 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006371 unlock_page(page);
6372 page_cache_release(page);
6373 }
6374
6375out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04006376 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05006377 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006378 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04006379 return ret;
6380}
6381
Chris Masond3977122009-01-05 21:25:51 -05006382static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006383 struct btrfs_key *extent_key,
6384 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05006385{
Zheng Yan1a40e232008-09-26 10:09:34 -04006386 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6387 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6388 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04006389 u64 start = extent_key->objectid - offset;
6390 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006391
6392 em = alloc_extent_map(GFP_NOFS);
6393 BUG_ON(!em || IS_ERR(em));
6394
Yan Zheng66435582008-10-30 14:19:50 -04006395 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006396 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04006397 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04006398 em->block_start = extent_key->objectid;
6399 em->bdev = root->fs_info->fs_devices->latest_bdev;
6400 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6401
6402 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04006403 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006404 while (1) {
6405 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04006406 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006407 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04006408 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006409 if (ret != -EEXIST) {
6410 free_extent_map(em);
6411 break;
6412 }
Yan Zheng66435582008-10-30 14:19:50 -04006413 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006414 }
Yan Zheng66435582008-10-30 14:19:50 -04006415 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006416
Yan Zheng66435582008-10-30 14:19:50 -04006417 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04006418}
6419
6420struct btrfs_ref_path {
6421 u64 extent_start;
6422 u64 nodes[BTRFS_MAX_LEVEL];
6423 u64 root_objectid;
6424 u64 root_generation;
6425 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006426 u32 num_refs;
6427 int lowest_level;
6428 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006429 int shared_level;
6430
6431 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6432 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04006433};
6434
6435struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04006436 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04006437 u64 disk_bytenr;
6438 u64 disk_num_bytes;
6439 u64 offset;
6440 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04006441 u8 compression;
6442 u8 encryption;
6443 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04006444};
6445
6446static int is_cowonly_root(u64 root_objectid)
6447{
6448 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6449 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6450 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6451 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05006452 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6453 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04006454 return 1;
6455 return 0;
6456}
6457
Chris Masond3977122009-01-05 21:25:51 -05006458static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006459 struct btrfs_root *extent_root,
6460 struct btrfs_ref_path *ref_path,
6461 int first_time)
6462{
6463 struct extent_buffer *leaf;
6464 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05006465 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05006466 struct btrfs_key key;
6467 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04006468 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05006469 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04006470 int level;
6471 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006472
Zheng Yan1a40e232008-09-26 10:09:34 -04006473 path = btrfs_alloc_path();
6474 if (!path)
6475 return -ENOMEM;
6476
Zheng Yan1a40e232008-09-26 10:09:34 -04006477 if (first_time) {
6478 ref_path->lowest_level = -1;
6479 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006480 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006481 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04006482 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006483walk_down:
6484 level = ref_path->current_level - 1;
6485 while (level >= -1) {
6486 u64 parent;
6487 if (level < ref_path->lowest_level)
6488 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006489
Chris Masond3977122009-01-05 21:25:51 -05006490 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006491 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006492 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006493 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006494 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006495
Zheng Yan1a40e232008-09-26 10:09:34 -04006496 parent = ref_path->nodes[level + 1];
6497 ref_path->nodes[level + 1] = 0;
6498 ref_path->current_level = level;
6499 BUG_ON(parent == 0);
6500
6501 key.objectid = bytenr;
6502 key.offset = parent + 1;
6503 key.type = BTRFS_EXTENT_REF_KEY;
6504
6505 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006506 if (ret < 0)
6507 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006508 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006509
Chris Masonedbd8d42007-12-21 16:27:24 -05006510 leaf = path->nodes[0];
6511 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04006512 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04006513 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04006514 if (ret < 0)
6515 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006516 if (ret > 0)
6517 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04006518 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04006519 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006520
6521 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04006522 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04006523 found_key.type == BTRFS_EXTENT_REF_KEY) {
6524 if (level < ref_path->shared_level)
6525 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006526 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006527 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006528next:
6529 level--;
6530 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006531 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006532 }
6533 /* reached lowest level */
6534 ret = 1;
6535 goto out;
6536walk_up:
6537 level = ref_path->current_level;
6538 while (level < BTRFS_MAX_LEVEL - 1) {
6539 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006540
6541 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006542 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006543 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006544 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006545
Zheng Yan1a40e232008-09-26 10:09:34 -04006546 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006547
Zheng Yan1a40e232008-09-26 10:09:34 -04006548 key.objectid = bytenr;
6549 key.offset = 0;
6550 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006551
Zheng Yan1a40e232008-09-26 10:09:34 -04006552 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6553 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006554 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006555
6556 leaf = path->nodes[0];
6557 nritems = btrfs_header_nritems(leaf);
6558 if (path->slots[0] >= nritems) {
6559 ret = btrfs_next_leaf(extent_root, path);
6560 if (ret < 0)
6561 goto out;
6562 if (ret > 0) {
6563 /* the extent was freed by someone */
6564 if (ref_path->lowest_level == level)
6565 goto out;
6566 btrfs_release_path(extent_root, path);
6567 goto walk_down;
6568 }
6569 leaf = path->nodes[0];
6570 }
6571
6572 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6573 if (found_key.objectid != bytenr ||
6574 found_key.type != BTRFS_EXTENT_REF_KEY) {
6575 /* the extent was freed by someone */
6576 if (ref_path->lowest_level == level) {
6577 ret = 1;
6578 goto out;
6579 }
6580 btrfs_release_path(extent_root, path);
6581 goto walk_down;
6582 }
6583found:
6584 ref = btrfs_item_ptr(leaf, path->slots[0],
6585 struct btrfs_extent_ref);
6586 ref_objectid = btrfs_ref_objectid(leaf, ref);
6587 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6588 if (first_time) {
6589 level = (int)ref_objectid;
6590 BUG_ON(level >= BTRFS_MAX_LEVEL);
6591 ref_path->lowest_level = level;
6592 ref_path->current_level = level;
6593 ref_path->nodes[level] = bytenr;
6594 } else {
6595 WARN_ON(ref_objectid != level);
6596 }
6597 } else {
6598 WARN_ON(level != -1);
6599 }
6600 first_time = 0;
6601
6602 if (ref_path->lowest_level == level) {
6603 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006604 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6605 }
6606
6607 /*
6608 * the block is tree root or the block isn't in reference
6609 * counted tree.
6610 */
6611 if (found_key.objectid == found_key.offset ||
6612 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6613 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6614 ref_path->root_generation =
6615 btrfs_ref_generation(leaf, ref);
6616 if (level < 0) {
6617 /* special reference from the tree log */
6618 ref_path->nodes[0] = found_key.offset;
6619 ref_path->current_level = 0;
6620 }
6621 ret = 0;
6622 goto out;
6623 }
6624
6625 level++;
6626 BUG_ON(ref_path->nodes[level] != 0);
6627 ref_path->nodes[level] = found_key.offset;
6628 ref_path->current_level = level;
6629
6630 /*
6631 * the reference was created in the running transaction,
6632 * no need to continue walking up.
6633 */
6634 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6635 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6636 ref_path->root_generation =
6637 btrfs_ref_generation(leaf, ref);
6638 ret = 0;
6639 goto out;
6640 }
6641
6642 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006643 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006644 }
6645 /* reached max tree level, but no tree root found. */
6646 BUG();
6647out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006648 btrfs_free_path(path);
6649 return ret;
6650}
6651
6652static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6653 struct btrfs_root *extent_root,
6654 struct btrfs_ref_path *ref_path,
6655 u64 extent_start)
6656{
6657 memset(ref_path, 0, sizeof(*ref_path));
6658 ref_path->extent_start = extent_start;
6659
6660 return __next_ref_path(trans, extent_root, ref_path, 1);
6661}
6662
6663static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6664 struct btrfs_root *extent_root,
6665 struct btrfs_ref_path *ref_path)
6666{
6667 return __next_ref_path(trans, extent_root, ref_path, 0);
6668}
6669
Chris Masond3977122009-01-05 21:25:51 -05006670static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006671 struct btrfs_key *extent_key,
6672 u64 offset, int no_fragment,
6673 struct disk_extent **extents,
6674 int *nr_extents)
6675{
6676 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6677 struct btrfs_path *path;
6678 struct btrfs_file_extent_item *fi;
6679 struct extent_buffer *leaf;
6680 struct disk_extent *exts = *extents;
6681 struct btrfs_key found_key;
6682 u64 cur_pos;
6683 u64 last_byte;
6684 u32 nritems;
6685 int nr = 0;
6686 int max = *nr_extents;
6687 int ret;
6688
6689 WARN_ON(!no_fragment && *extents);
6690 if (!exts) {
6691 max = 1;
6692 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6693 if (!exts)
6694 return -ENOMEM;
6695 }
6696
6697 path = btrfs_alloc_path();
6698 BUG_ON(!path);
6699
6700 cur_pos = extent_key->objectid - offset;
6701 last_byte = extent_key->objectid + extent_key->offset;
6702 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6703 cur_pos, 0);
6704 if (ret < 0)
6705 goto out;
6706 if (ret > 0) {
6707 ret = -ENOENT;
6708 goto out;
6709 }
6710
6711 while (1) {
6712 leaf = path->nodes[0];
6713 nritems = btrfs_header_nritems(leaf);
6714 if (path->slots[0] >= nritems) {
6715 ret = btrfs_next_leaf(root, path);
6716 if (ret < 0)
6717 goto out;
6718 if (ret > 0)
6719 break;
6720 leaf = path->nodes[0];
6721 }
6722
6723 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6724 if (found_key.offset != cur_pos ||
6725 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6726 found_key.objectid != reloc_inode->i_ino)
6727 break;
6728
6729 fi = btrfs_item_ptr(leaf, path->slots[0],
6730 struct btrfs_file_extent_item);
6731 if (btrfs_file_extent_type(leaf, fi) !=
6732 BTRFS_FILE_EXTENT_REG ||
6733 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6734 break;
6735
6736 if (nr == max) {
6737 struct disk_extent *old = exts;
6738 max *= 2;
6739 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6740 memcpy(exts, old, sizeof(*exts) * nr);
6741 if (old != *extents)
6742 kfree(old);
6743 }
6744
6745 exts[nr].disk_bytenr =
6746 btrfs_file_extent_disk_bytenr(leaf, fi);
6747 exts[nr].disk_num_bytes =
6748 btrfs_file_extent_disk_num_bytes(leaf, fi);
6749 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6750 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04006751 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6752 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6753 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6754 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6755 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04006756 BUG_ON(exts[nr].offset > 0);
6757 BUG_ON(exts[nr].compression || exts[nr].encryption);
6758 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006759
6760 cur_pos += exts[nr].num_bytes;
6761 nr++;
6762
6763 if (cur_pos + offset >= last_byte)
6764 break;
6765
6766 if (no_fragment) {
6767 ret = 1;
6768 goto out;
6769 }
6770 path->slots[0]++;
6771 }
6772
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006773 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04006774 if (cur_pos + offset < last_byte) {
6775 ret = -ENOENT;
6776 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05006777 }
6778 ret = 0;
6779out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006780 btrfs_free_path(path);
6781 if (ret) {
6782 if (exts != *extents)
6783 kfree(exts);
6784 } else {
6785 *extents = exts;
6786 *nr_extents = nr;
6787 }
6788 return ret;
6789}
6790
Chris Masond3977122009-01-05 21:25:51 -05006791static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006792 struct btrfs_root *root,
6793 struct btrfs_path *path,
6794 struct btrfs_key *extent_key,
6795 struct btrfs_key *leaf_key,
6796 struct btrfs_ref_path *ref_path,
6797 struct disk_extent *new_extents,
6798 int nr_extents)
6799{
6800 struct extent_buffer *leaf;
6801 struct btrfs_file_extent_item *fi;
6802 struct inode *inode = NULL;
6803 struct btrfs_key key;
6804 u64 lock_start = 0;
6805 u64 lock_end = 0;
6806 u64 num_bytes;
6807 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05006808 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006809 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006810 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006811 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04006812 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04006813 int ret;
6814
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006815 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04006816 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006817 if (key.objectid < ref_path->owner_objectid ||
6818 (key.objectid == ref_path->owner_objectid &&
6819 key.type < BTRFS_EXTENT_DATA_KEY)) {
6820 key.objectid = ref_path->owner_objectid;
6821 key.type = BTRFS_EXTENT_DATA_KEY;
6822 key.offset = 0;
6823 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006824 }
6825
6826 while (1) {
6827 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6828 if (ret < 0)
6829 goto out;
6830
6831 leaf = path->nodes[0];
6832 nritems = btrfs_header_nritems(leaf);
6833next:
6834 if (extent_locked && ret > 0) {
6835 /*
6836 * the file extent item was modified by someone
6837 * before the extent got locked.
6838 */
Zheng Yan1a40e232008-09-26 10:09:34 -04006839 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6840 lock_end, GFP_NOFS);
6841 extent_locked = 0;
6842 }
6843
6844 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006845 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04006846 break;
6847
6848 BUG_ON(extent_locked);
6849 ret = btrfs_next_leaf(root, path);
6850 if (ret < 0)
6851 goto out;
6852 if (ret > 0)
6853 break;
6854 leaf = path->nodes[0];
6855 nritems = btrfs_header_nritems(leaf);
6856 }
6857
6858 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6859
6860 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6861 if ((key.objectid > ref_path->owner_objectid) ||
6862 (key.objectid == ref_path->owner_objectid &&
6863 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05006864 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006865 break;
6866 }
6867
6868 if (inode && key.objectid != inode->i_ino) {
6869 BUG_ON(extent_locked);
6870 btrfs_release_path(root, path);
6871 mutex_unlock(&inode->i_mutex);
6872 iput(inode);
6873 inode = NULL;
6874 continue;
6875 }
6876
6877 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6878 path->slots[0]++;
6879 ret = 1;
6880 goto next;
6881 }
6882 fi = btrfs_item_ptr(leaf, path->slots[0],
6883 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04006884 extent_type = btrfs_file_extent_type(leaf, fi);
6885 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6886 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04006887 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6888 extent_key->objectid)) {
6889 path->slots[0]++;
6890 ret = 1;
6891 goto next;
6892 }
6893
6894 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6895 ext_offset = btrfs_file_extent_offset(leaf, fi);
6896
Yan Zheng86288a12009-01-21 10:49:16 -05006897 if (search_end == (u64)-1) {
6898 search_end = key.offset - ext_offset +
6899 btrfs_file_extent_ram_bytes(leaf, fi);
6900 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006901
6902 if (!extent_locked) {
6903 lock_start = key.offset;
6904 lock_end = lock_start + num_bytes - 1;
6905 } else {
Yan Zheng66435582008-10-30 14:19:50 -04006906 if (lock_start > key.offset ||
6907 lock_end + 1 < key.offset + num_bytes) {
6908 unlock_extent(&BTRFS_I(inode)->io_tree,
6909 lock_start, lock_end, GFP_NOFS);
6910 extent_locked = 0;
6911 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006912 }
6913
6914 if (!inode) {
6915 btrfs_release_path(root, path);
6916
6917 inode = btrfs_iget_locked(root->fs_info->sb,
6918 key.objectid, root);
6919 if (inode->i_state & I_NEW) {
6920 BTRFS_I(inode)->root = root;
6921 BTRFS_I(inode)->location.objectid =
6922 key.objectid;
6923 BTRFS_I(inode)->location.type =
6924 BTRFS_INODE_ITEM_KEY;
6925 BTRFS_I(inode)->location.offset = 0;
6926 btrfs_read_locked_inode(inode);
6927 unlock_new_inode(inode);
6928 }
6929 /*
6930 * some code call btrfs_commit_transaction while
6931 * holding the i_mutex, so we can't use mutex_lock
6932 * here.
6933 */
6934 if (is_bad_inode(inode) ||
6935 !mutex_trylock(&inode->i_mutex)) {
6936 iput(inode);
6937 inode = NULL;
6938 key.offset = (u64)-1;
6939 goto skip;
6940 }
6941 }
6942
6943 if (!extent_locked) {
6944 struct btrfs_ordered_extent *ordered;
6945
6946 btrfs_release_path(root, path);
6947
6948 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6949 lock_end, GFP_NOFS);
6950 ordered = btrfs_lookup_first_ordered_extent(inode,
6951 lock_end);
6952 if (ordered &&
6953 ordered->file_offset <= lock_end &&
6954 ordered->file_offset + ordered->len > lock_start) {
6955 unlock_extent(&BTRFS_I(inode)->io_tree,
6956 lock_start, lock_end, GFP_NOFS);
6957 btrfs_start_ordered_extent(inode, ordered, 1);
6958 btrfs_put_ordered_extent(ordered);
6959 key.offset += num_bytes;
6960 goto skip;
6961 }
6962 if (ordered)
6963 btrfs_put_ordered_extent(ordered);
6964
Zheng Yan1a40e232008-09-26 10:09:34 -04006965 extent_locked = 1;
6966 continue;
6967 }
6968
6969 if (nr_extents == 1) {
6970 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04006971 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6972 new_extents[0].disk_bytenr);
6973 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6974 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006975 btrfs_mark_buffer_dirty(leaf);
6976
6977 btrfs_drop_extent_cache(inode, key.offset,
6978 key.offset + num_bytes - 1, 0);
6979
6980 ret = btrfs_inc_extent_ref(trans, root,
6981 new_extents[0].disk_bytenr,
6982 new_extents[0].disk_num_bytes,
6983 leaf->start,
6984 root->root_key.objectid,
6985 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006986 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006987 BUG_ON(ret);
6988
6989 ret = btrfs_free_extent(trans, root,
6990 extent_key->objectid,
6991 extent_key->offset,
6992 leaf->start,
6993 btrfs_header_owner(leaf),
6994 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006995 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006996 BUG_ON(ret);
6997
6998 btrfs_release_path(root, path);
6999 key.offset += num_bytes;
7000 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04007001 BUG_ON(1);
7002#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04007003 u64 alloc_hint;
7004 u64 extent_len;
7005 int i;
7006 /*
7007 * drop old extent pointer at first, then insert the
7008 * new pointers one bye one
7009 */
7010 btrfs_release_path(root, path);
7011 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7012 key.offset + num_bytes,
7013 key.offset, &alloc_hint);
7014 BUG_ON(ret);
7015
7016 for (i = 0; i < nr_extents; i++) {
7017 if (ext_offset >= new_extents[i].num_bytes) {
7018 ext_offset -= new_extents[i].num_bytes;
7019 continue;
7020 }
7021 extent_len = min(new_extents[i].num_bytes -
7022 ext_offset, num_bytes);
7023
7024 ret = btrfs_insert_empty_item(trans, root,
7025 path, &key,
7026 sizeof(*fi));
7027 BUG_ON(ret);
7028
7029 leaf = path->nodes[0];
7030 fi = btrfs_item_ptr(leaf, path->slots[0],
7031 struct btrfs_file_extent_item);
7032 btrfs_set_file_extent_generation(leaf, fi,
7033 trans->transid);
7034 btrfs_set_file_extent_type(leaf, fi,
7035 BTRFS_FILE_EXTENT_REG);
7036 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7037 new_extents[i].disk_bytenr);
7038 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7039 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04007040 btrfs_set_file_extent_ram_bytes(leaf, fi,
7041 new_extents[i].ram_bytes);
7042
7043 btrfs_set_file_extent_compression(leaf, fi,
7044 new_extents[i].compression);
7045 btrfs_set_file_extent_encryption(leaf, fi,
7046 new_extents[i].encryption);
7047 btrfs_set_file_extent_other_encoding(leaf, fi,
7048 new_extents[i].other_encoding);
7049
Zheng Yan1a40e232008-09-26 10:09:34 -04007050 btrfs_set_file_extent_num_bytes(leaf, fi,
7051 extent_len);
7052 ext_offset += new_extents[i].offset;
7053 btrfs_set_file_extent_offset(leaf, fi,
7054 ext_offset);
7055 btrfs_mark_buffer_dirty(leaf);
7056
7057 btrfs_drop_extent_cache(inode, key.offset,
7058 key.offset + extent_len - 1, 0);
7059
7060 ret = btrfs_inc_extent_ref(trans, root,
7061 new_extents[i].disk_bytenr,
7062 new_extents[i].disk_num_bytes,
7063 leaf->start,
7064 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007065 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007066 BUG_ON(ret);
7067 btrfs_release_path(root, path);
7068
Yan Zhenga76a3cd2008-10-09 11:46:29 -04007069 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04007070
7071 ext_offset = 0;
7072 num_bytes -= extent_len;
7073 key.offset += extent_len;
7074
7075 if (num_bytes == 0)
7076 break;
7077 }
7078 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007079#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04007080 }
7081
7082 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007083 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7084 lock_end, GFP_NOFS);
7085 extent_locked = 0;
7086 }
7087skip:
7088 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05007089 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007090 break;
7091
7092 cond_resched();
7093 }
7094 ret = 0;
7095out:
7096 btrfs_release_path(root, path);
7097 if (inode) {
7098 mutex_unlock(&inode->i_mutex);
7099 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007100 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7101 lock_end, GFP_NOFS);
7102 }
7103 iput(inode);
7104 }
7105 return ret;
7106}
7107
Zheng Yan1a40e232008-09-26 10:09:34 -04007108int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7109 struct btrfs_root *root,
7110 struct extent_buffer *buf, u64 orig_start)
7111{
7112 int level;
7113 int ret;
7114
7115 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7116 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7117
7118 level = btrfs_header_level(buf);
7119 if (level == 0) {
7120 struct btrfs_leaf_ref *ref;
7121 struct btrfs_leaf_ref *orig_ref;
7122
7123 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7124 if (!orig_ref)
7125 return -ENOENT;
7126
7127 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7128 if (!ref) {
7129 btrfs_free_leaf_ref(root, orig_ref);
7130 return -ENOMEM;
7131 }
7132
7133 ref->nritems = orig_ref->nritems;
7134 memcpy(ref->extents, orig_ref->extents,
7135 sizeof(ref->extents[0]) * ref->nritems);
7136
7137 btrfs_free_leaf_ref(root, orig_ref);
7138
7139 ref->root_gen = trans->transid;
7140 ref->bytenr = buf->start;
7141 ref->owner = btrfs_header_owner(buf);
7142 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05007143
Zheng Yan1a40e232008-09-26 10:09:34 -04007144 ret = btrfs_add_leaf_ref(root, ref, 0);
7145 WARN_ON(ret);
7146 btrfs_free_leaf_ref(root, ref);
7147 }
7148 return 0;
7149}
7150
Chris Masond3977122009-01-05 21:25:51 -05007151static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007152 struct extent_buffer *leaf,
7153 struct btrfs_block_group_cache *group,
7154 struct btrfs_root *target_root)
7155{
7156 struct btrfs_key key;
7157 struct inode *inode = NULL;
7158 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007159 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04007160 u64 num_bytes;
7161 u64 skip_objectid = 0;
7162 u32 nritems;
7163 u32 i;
7164
7165 nritems = btrfs_header_nritems(leaf);
7166 for (i = 0; i < nritems; i++) {
7167 btrfs_item_key_to_cpu(leaf, &key, i);
7168 if (key.objectid == skip_objectid ||
7169 key.type != BTRFS_EXTENT_DATA_KEY)
7170 continue;
7171 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7172 if (btrfs_file_extent_type(leaf, fi) ==
7173 BTRFS_FILE_EXTENT_INLINE)
7174 continue;
7175 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7176 continue;
7177 if (!inode || inode->i_ino != key.objectid) {
7178 iput(inode);
7179 inode = btrfs_ilookup(target_root->fs_info->sb,
7180 key.objectid, target_root, 1);
7181 }
7182 if (!inode) {
7183 skip_objectid = key.objectid;
7184 continue;
7185 }
7186 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7187
Josef Bacik2ac55d42010-02-03 19:33:23 +00007188 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7189 key.offset + num_bytes - 1, 0, &cached_state,
7190 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007191 btrfs_drop_extent_cache(inode, key.offset,
7192 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007193 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7194 key.offset + num_bytes - 1, &cached_state,
7195 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007196 cond_resched();
7197 }
7198 iput(inode);
7199 return 0;
7200}
7201
Chris Masond3977122009-01-05 21:25:51 -05007202static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007203 struct btrfs_root *root,
7204 struct extent_buffer *leaf,
7205 struct btrfs_block_group_cache *group,
7206 struct inode *reloc_inode)
7207{
7208 struct btrfs_key key;
7209 struct btrfs_key extent_key;
7210 struct btrfs_file_extent_item *fi;
7211 struct btrfs_leaf_ref *ref;
7212 struct disk_extent *new_extent;
7213 u64 bytenr;
7214 u64 num_bytes;
7215 u32 nritems;
7216 u32 i;
7217 int ext_index;
7218 int nr_extent;
7219 int ret;
7220
7221 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
7222 BUG_ON(!new_extent);
7223
7224 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7225 BUG_ON(!ref);
7226
7227 ext_index = -1;
7228 nritems = btrfs_header_nritems(leaf);
7229 for (i = 0; i < nritems; i++) {
7230 btrfs_item_key_to_cpu(leaf, &key, i);
7231 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7232 continue;
7233 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7234 if (btrfs_file_extent_type(leaf, fi) ==
7235 BTRFS_FILE_EXTENT_INLINE)
7236 continue;
7237 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7238 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7239 if (bytenr == 0)
7240 continue;
7241
7242 ext_index++;
7243 if (bytenr >= group->key.objectid + group->key.offset ||
7244 bytenr + num_bytes <= group->key.objectid)
7245 continue;
7246
7247 extent_key.objectid = bytenr;
7248 extent_key.offset = num_bytes;
7249 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7250 nr_extent = 1;
7251 ret = get_new_locations(reloc_inode, &extent_key,
7252 group->key.objectid, 1,
7253 &new_extent, &nr_extent);
7254 if (ret > 0)
7255 continue;
7256 BUG_ON(ret < 0);
7257
7258 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7259 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7260 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7261 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7262
Zheng Yan1a40e232008-09-26 10:09:34 -04007263 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7264 new_extent->disk_bytenr);
7265 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7266 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007267 btrfs_mark_buffer_dirty(leaf);
7268
7269 ret = btrfs_inc_extent_ref(trans, root,
7270 new_extent->disk_bytenr,
7271 new_extent->disk_num_bytes,
7272 leaf->start,
7273 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007274 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007275 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04007276
Zheng Yan1a40e232008-09-26 10:09:34 -04007277 ret = btrfs_free_extent(trans, root,
7278 bytenr, num_bytes, leaf->start,
7279 btrfs_header_owner(leaf),
7280 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007281 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007282 BUG_ON(ret);
7283 cond_resched();
7284 }
7285 kfree(new_extent);
7286 BUG_ON(ext_index + 1 != ref->nritems);
7287 btrfs_free_leaf_ref(root, ref);
7288 return 0;
7289}
7290
Yan Zhengf82d02d2008-10-29 14:49:05 -04007291int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7292 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04007293{
7294 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007295 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007296
7297 if (root->reloc_root) {
7298 reloc_root = root->reloc_root;
7299 root->reloc_root = NULL;
7300 list_add(&reloc_root->dead_list,
7301 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007302
7303 btrfs_set_root_bytenr(&reloc_root->root_item,
7304 reloc_root->node->start);
7305 btrfs_set_root_level(&root->root_item,
7306 btrfs_header_level(reloc_root->node));
7307 memset(&reloc_root->root_item.drop_progress, 0,
7308 sizeof(struct btrfs_disk_key));
7309 reloc_root->root_item.drop_level = 0;
7310
7311 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7312 &reloc_root->root_key,
7313 &reloc_root->root_item);
7314 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007315 }
7316 return 0;
7317}
7318
7319int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7320{
7321 struct btrfs_trans_handle *trans;
7322 struct btrfs_root *reloc_root;
7323 struct btrfs_root *prev_root = NULL;
7324 struct list_head dead_roots;
7325 int ret;
7326 unsigned long nr;
7327
7328 INIT_LIST_HEAD(&dead_roots);
7329 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7330
7331 while (!list_empty(&dead_roots)) {
7332 reloc_root = list_entry(dead_roots.prev,
7333 struct btrfs_root, dead_list);
7334 list_del_init(&reloc_root->dead_list);
7335
7336 BUG_ON(reloc_root->commit_root != NULL);
7337 while (1) {
7338 trans = btrfs_join_transaction(root, 1);
7339 BUG_ON(!trans);
7340
7341 mutex_lock(&root->fs_info->drop_mutex);
7342 ret = btrfs_drop_snapshot(trans, reloc_root);
7343 if (ret != -EAGAIN)
7344 break;
7345 mutex_unlock(&root->fs_info->drop_mutex);
7346
7347 nr = trans->blocks_used;
7348 ret = btrfs_end_transaction(trans, root);
7349 BUG_ON(ret);
7350 btrfs_btree_balance_dirty(root, nr);
7351 }
7352
7353 free_extent_buffer(reloc_root->node);
7354
7355 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7356 &reloc_root->root_key);
7357 BUG_ON(ret);
7358 mutex_unlock(&root->fs_info->drop_mutex);
7359
7360 nr = trans->blocks_used;
7361 ret = btrfs_end_transaction(trans, root);
7362 BUG_ON(ret);
7363 btrfs_btree_balance_dirty(root, nr);
7364
7365 kfree(prev_root);
7366 prev_root = reloc_root;
7367 }
7368 if (prev_root) {
7369 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7370 kfree(prev_root);
7371 }
7372 return 0;
7373}
7374
7375int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7376{
7377 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7378 return 0;
7379}
7380
7381int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7382{
7383 struct btrfs_root *reloc_root;
7384 struct btrfs_trans_handle *trans;
7385 struct btrfs_key location;
7386 int found;
7387 int ret;
7388
7389 mutex_lock(&root->fs_info->tree_reloc_mutex);
7390 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7391 BUG_ON(ret);
7392 found = !list_empty(&root->fs_info->dead_reloc_roots);
7393 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7394
7395 if (found) {
7396 trans = btrfs_start_transaction(root, 1);
7397 BUG_ON(!trans);
7398 ret = btrfs_commit_transaction(trans, root);
7399 BUG_ON(ret);
7400 }
7401
7402 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7403 location.offset = (u64)-1;
7404 location.type = BTRFS_ROOT_ITEM_KEY;
7405
7406 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7407 BUG_ON(!reloc_root);
7408 btrfs_orphan_cleanup(reloc_root);
7409 return 0;
7410}
7411
Chris Masond3977122009-01-05 21:25:51 -05007412static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007413 struct btrfs_root *root)
7414{
7415 struct btrfs_root *reloc_root;
7416 struct extent_buffer *eb;
7417 struct btrfs_root_item *root_item;
7418 struct btrfs_key root_key;
7419 int ret;
7420
7421 BUG_ON(!root->ref_cows);
7422 if (root->reloc_root)
7423 return 0;
7424
7425 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7426 BUG_ON(!root_item);
7427
7428 ret = btrfs_copy_root(trans, root, root->commit_root,
7429 &eb, BTRFS_TREE_RELOC_OBJECTID);
7430 BUG_ON(ret);
7431
7432 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7433 root_key.offset = root->root_key.objectid;
7434 root_key.type = BTRFS_ROOT_ITEM_KEY;
7435
7436 memcpy(root_item, &root->root_item, sizeof(root_item));
7437 btrfs_set_root_refs(root_item, 0);
7438 btrfs_set_root_bytenr(root_item, eb->start);
7439 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04007440 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007441
7442 btrfs_tree_unlock(eb);
7443 free_extent_buffer(eb);
7444
7445 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7446 &root_key, root_item);
7447 BUG_ON(ret);
7448 kfree(root_item);
7449
7450 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7451 &root_key);
7452 BUG_ON(!reloc_root);
7453 reloc_root->last_trans = trans->transid;
7454 reloc_root->commit_root = NULL;
7455 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7456
7457 root->reloc_root = reloc_root;
7458 return 0;
7459}
7460
7461/*
7462 * Core function of space balance.
7463 *
7464 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04007465 * counted roots. There is one reloc tree for each subvol, and all
7466 * reloc trees share same root key objectid. Reloc trees are snapshots
7467 * of the latest committed roots of subvols (root->commit_root).
7468 *
7469 * To relocate a tree block referenced by a subvol, there are two steps.
7470 * COW the block through subvol's reloc tree, then update block pointer
7471 * in the subvol to point to the new block. Since all reloc trees share
7472 * same root key objectid, doing special handing for tree blocks owned
7473 * by them is easy. Once a tree block has been COWed in one reloc tree,
7474 * we can use the resulting new block directly when the same block is
7475 * required to COW again through other reloc trees. By this way, relocated
7476 * tree blocks are shared between reloc trees, so they are also shared
7477 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04007478 */
Chris Masond3977122009-01-05 21:25:51 -05007479static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007480 struct btrfs_root *root,
7481 struct btrfs_path *path,
7482 struct btrfs_key *first_key,
7483 struct btrfs_ref_path *ref_path,
7484 struct btrfs_block_group_cache *group,
7485 struct inode *reloc_inode)
7486{
7487 struct btrfs_root *reloc_root;
7488 struct extent_buffer *eb = NULL;
7489 struct btrfs_key *keys;
7490 u64 *nodes;
7491 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007492 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04007493 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007494 int ret;
7495
7496 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7497 lowest_level = ref_path->owner_objectid;
7498
Yan Zhengf82d02d2008-10-29 14:49:05 -04007499 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007500 path->lowest_level = lowest_level;
7501 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7502 BUG_ON(ret < 0);
7503 path->lowest_level = 0;
7504 btrfs_release_path(root, path);
7505 return 0;
7506 }
7507
Zheng Yan1a40e232008-09-26 10:09:34 -04007508 mutex_lock(&root->fs_info->tree_reloc_mutex);
7509 ret = init_reloc_tree(trans, root);
7510 BUG_ON(ret);
7511 reloc_root = root->reloc_root;
7512
Yan Zhengf82d02d2008-10-29 14:49:05 -04007513 shared_level = ref_path->shared_level;
7514 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007515
Yan Zhengf82d02d2008-10-29 14:49:05 -04007516 keys = ref_path->node_keys;
7517 nodes = ref_path->new_nodes;
7518 memset(&keys[shared_level + 1], 0,
7519 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7520 memset(&nodes[shared_level + 1], 0,
7521 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04007522
Yan Zhengf82d02d2008-10-29 14:49:05 -04007523 if (nodes[lowest_level] == 0) {
7524 path->lowest_level = lowest_level;
7525 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7526 0, 1);
7527 BUG_ON(ret);
7528 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7529 eb = path->nodes[level];
7530 if (!eb || eb == reloc_root->node)
7531 break;
7532 nodes[level] = eb->start;
7533 if (level == 0)
7534 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7535 else
7536 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7537 }
Yan Zheng2b820322008-11-17 21:11:30 -05007538 if (nodes[0] &&
7539 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007540 eb = path->nodes[0];
7541 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7542 group, reloc_inode);
7543 BUG_ON(ret);
7544 }
7545 btrfs_release_path(reloc_root, path);
7546 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007547 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007548 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007549 BUG_ON(ret);
7550 }
7551
Zheng Yan1a40e232008-09-26 10:09:34 -04007552 /*
7553 * replace tree blocks in the fs tree with tree blocks in
7554 * the reloc tree.
7555 */
7556 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7557 BUG_ON(ret < 0);
7558
7559 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007560 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7561 0, 0);
7562 BUG_ON(ret);
7563 extent_buffer_get(path->nodes[0]);
7564 eb = path->nodes[0];
7565 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007566 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7567 BUG_ON(ret);
7568 free_extent_buffer(eb);
7569 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007570
Yan Zhengf82d02d2008-10-29 14:49:05 -04007571 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007572 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007573 return 0;
7574}
7575
Chris Masond3977122009-01-05 21:25:51 -05007576static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007577 struct btrfs_root *root,
7578 struct btrfs_path *path,
7579 struct btrfs_key *first_key,
7580 struct btrfs_ref_path *ref_path)
7581{
7582 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007583
7584 ret = relocate_one_path(trans, root, path, first_key,
7585 ref_path, NULL, NULL);
7586 BUG_ON(ret);
7587
Zheng Yan1a40e232008-09-26 10:09:34 -04007588 return 0;
7589}
7590
Chris Masond3977122009-01-05 21:25:51 -05007591static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007592 struct btrfs_root *extent_root,
7593 struct btrfs_path *path,
7594 struct btrfs_key *extent_key)
7595{
7596 int ret;
7597
Zheng Yan1a40e232008-09-26 10:09:34 -04007598 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7599 if (ret)
7600 goto out;
7601 ret = btrfs_del_item(trans, extent_root, path);
7602out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007603 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007604 return ret;
7605}
7606
Chris Masond3977122009-01-05 21:25:51 -05007607static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007608 struct btrfs_ref_path *ref_path)
7609{
7610 struct btrfs_key root_key;
7611
7612 root_key.objectid = ref_path->root_objectid;
7613 root_key.type = BTRFS_ROOT_ITEM_KEY;
7614 if (is_cowonly_root(ref_path->root_objectid))
7615 root_key.offset = 0;
7616 else
7617 root_key.offset = (u64)-1;
7618
7619 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7620}
7621
Chris Masond3977122009-01-05 21:25:51 -05007622static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007623 struct btrfs_path *path,
7624 struct btrfs_key *extent_key,
7625 struct btrfs_block_group_cache *group,
7626 struct inode *reloc_inode, int pass)
7627{
7628 struct btrfs_trans_handle *trans;
7629 struct btrfs_root *found_root;
7630 struct btrfs_ref_path *ref_path = NULL;
7631 struct disk_extent *new_extents = NULL;
7632 int nr_extents = 0;
7633 int loops;
7634 int ret;
7635 int level;
7636 struct btrfs_key first_key;
7637 u64 prev_block = 0;
7638
Zheng Yan1a40e232008-09-26 10:09:34 -04007639
7640 trans = btrfs_start_transaction(extent_root, 1);
7641 BUG_ON(!trans);
7642
7643 if (extent_key->objectid == 0) {
7644 ret = del_extent_zero(trans, extent_root, path, extent_key);
7645 goto out;
7646 }
7647
7648 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7649 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007650 ret = -ENOMEM;
7651 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007652 }
7653
7654 for (loops = 0; ; loops++) {
7655 if (loops == 0) {
7656 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7657 extent_key->objectid);
7658 } else {
7659 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7660 }
7661 if (ret < 0)
7662 goto out;
7663 if (ret > 0)
7664 break;
7665
7666 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7667 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7668 continue;
7669
7670 found_root = read_ref_root(extent_root->fs_info, ref_path);
7671 BUG_ON(!found_root);
7672 /*
7673 * for reference counted tree, only process reference paths
7674 * rooted at the latest committed root.
7675 */
7676 if (found_root->ref_cows &&
7677 ref_path->root_generation != found_root->root_key.offset)
7678 continue;
7679
7680 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7681 if (pass == 0) {
7682 /*
7683 * copy data extents to new locations
7684 */
7685 u64 group_start = group->key.objectid;
7686 ret = relocate_data_extent(reloc_inode,
7687 extent_key,
7688 group_start);
7689 if (ret < 0)
7690 goto out;
7691 break;
7692 }
7693 level = 0;
7694 } else {
7695 level = ref_path->owner_objectid;
7696 }
7697
7698 if (prev_block != ref_path->nodes[level]) {
7699 struct extent_buffer *eb;
7700 u64 block_start = ref_path->nodes[level];
7701 u64 block_size = btrfs_level_size(found_root, level);
7702
7703 eb = read_tree_block(found_root, block_start,
7704 block_size, 0);
7705 btrfs_tree_lock(eb);
7706 BUG_ON(level != btrfs_header_level(eb));
7707
7708 if (level == 0)
7709 btrfs_item_key_to_cpu(eb, &first_key, 0);
7710 else
7711 btrfs_node_key_to_cpu(eb, &first_key, 0);
7712
7713 btrfs_tree_unlock(eb);
7714 free_extent_buffer(eb);
7715 prev_block = block_start;
7716 }
7717
Yan Zheng24562422009-02-12 14:14:53 -05007718 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007719 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05007720 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007721 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7722 /*
7723 * try to update data extent references while
7724 * keeping metadata shared between snapshots.
7725 */
7726 if (pass == 1) {
7727 ret = relocate_one_path(trans, found_root,
7728 path, &first_key, ref_path,
7729 group, reloc_inode);
7730 if (ret < 0)
7731 goto out;
7732 continue;
7733 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007734 /*
7735 * use fallback method to process the remaining
7736 * references.
7737 */
7738 if (!new_extents) {
7739 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04007740 new_extents = kmalloc(sizeof(*new_extents),
7741 GFP_NOFS);
7742 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007743 ret = get_new_locations(reloc_inode,
7744 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04007745 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04007746 &new_extents,
7747 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007748 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04007749 goto out;
7750 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007751 ret = replace_one_extent(trans, found_root,
7752 path, extent_key,
7753 &first_key, ref_path,
7754 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05007755 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007756 ret = relocate_tree_block(trans, found_root, path,
7757 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007758 }
7759 if (ret < 0)
7760 goto out;
7761 }
7762 ret = 0;
7763out:
7764 btrfs_end_transaction(trans, extent_root);
7765 kfree(new_extents);
7766 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007767 return ret;
7768}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007769#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007770
Chris Masonec44a352008-04-28 15:29:52 -04007771static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7772{
7773 u64 num_devices;
7774 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7775 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7776
Yan Zheng2b820322008-11-17 21:11:30 -05007777 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04007778 if (num_devices == 1) {
7779 stripped |= BTRFS_BLOCK_GROUP_DUP;
7780 stripped = flags & ~stripped;
7781
7782 /* turn raid0 into single device chunks */
7783 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7784 return stripped;
7785
7786 /* turn mirroring into duplication */
7787 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7788 BTRFS_BLOCK_GROUP_RAID10))
7789 return stripped | BTRFS_BLOCK_GROUP_DUP;
7790 return flags;
7791 } else {
7792 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007793 if (flags & stripped)
7794 return flags;
7795
7796 stripped |= BTRFS_BLOCK_GROUP_DUP;
7797 stripped = flags & ~stripped;
7798
7799 /* switch duplicated blocks with raid1 */
7800 if (flags & BTRFS_BLOCK_GROUP_DUP)
7801 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7802
7803 /* turn single device chunks into raid0 */
7804 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7805 }
7806 return flags;
7807}
7808
Yan, Zhengf0486c62010-05-16 10:46:25 -04007809static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04007810{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007811 struct btrfs_space_info *sinfo = cache->space_info;
7812 u64 num_bytes;
7813 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007814
Yan, Zhengf0486c62010-05-16 10:46:25 -04007815 if (cache->ro)
7816 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04007817
Yan, Zhengf0486c62010-05-16 10:46:25 -04007818 spin_lock(&sinfo->lock);
7819 spin_lock(&cache->lock);
7820 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7821 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007822
Yan, Zhengf0486c62010-05-16 10:46:25 -04007823 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
7824 sinfo->bytes_may_use + sinfo->bytes_readonly +
7825 cache->reserved_pinned + num_bytes < sinfo->total_bytes) {
7826 sinfo->bytes_readonly += num_bytes;
7827 sinfo->bytes_reserved += cache->reserved_pinned;
7828 cache->reserved_pinned = 0;
7829 cache->ro = 1;
7830 ret = 0;
7831 }
7832 spin_unlock(&cache->lock);
7833 spin_unlock(&sinfo->lock);
7834 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007835}
7836
Yan, Zhengf0486c62010-05-16 10:46:25 -04007837int btrfs_set_block_group_ro(struct btrfs_root *root,
7838 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007839
7840{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007841 struct btrfs_trans_handle *trans;
7842 u64 alloc_flags;
7843 int ret;
7844
7845 BUG_ON(cache->ro);
7846
7847 trans = btrfs_join_transaction(root, 1);
7848 BUG_ON(IS_ERR(trans));
7849
7850 alloc_flags = update_block_group_flags(root, cache->flags);
7851 if (alloc_flags != cache->flags)
7852 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7853
7854 ret = set_block_group_ro(cache);
7855 if (!ret)
7856 goto out;
7857 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
7858 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7859 if (ret < 0)
7860 goto out;
7861 ret = set_block_group_ro(cache);
7862out:
7863 btrfs_end_transaction(trans, root);
7864 return ret;
7865}
7866
7867int btrfs_set_block_group_rw(struct btrfs_root *root,
7868 struct btrfs_block_group_cache *cache)
7869{
7870 struct btrfs_space_info *sinfo = cache->space_info;
7871 u64 num_bytes;
7872
7873 BUG_ON(!cache->ro);
7874
7875 spin_lock(&sinfo->lock);
7876 spin_lock(&cache->lock);
7877 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7878 cache->bytes_super - btrfs_block_group_used(&cache->item);
7879 sinfo->bytes_readonly -= num_bytes;
7880 cache->ro = 0;
7881 spin_unlock(&cache->lock);
7882 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007883 return 0;
7884}
7885
Josef Bacikba1bf482009-09-11 16:11:19 -04007886/*
7887 * checks to see if its even possible to relocate this block group.
7888 *
7889 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7890 * ok to go ahead and try.
7891 */
7892int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007893{
Zheng Yan1a40e232008-09-26 10:09:34 -04007894 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007895 struct btrfs_space_info *space_info;
7896 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7897 struct btrfs_device *device;
7898 int full = 0;
7899 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007900
Josef Bacikba1bf482009-09-11 16:11:19 -04007901 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007902
Josef Bacikba1bf482009-09-11 16:11:19 -04007903 /* odd, couldn't find the block group, leave it alone */
7904 if (!block_group)
7905 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007906
Josef Bacikba1bf482009-09-11 16:11:19 -04007907 /* no bytes used, we're good */
7908 if (!btrfs_block_group_used(&block_group->item))
7909 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007910
Josef Bacikba1bf482009-09-11 16:11:19 -04007911 space_info = block_group->space_info;
7912 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007913
Josef Bacikba1bf482009-09-11 16:11:19 -04007914 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007915
Josef Bacikba1bf482009-09-11 16:11:19 -04007916 /*
7917 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007918 * relocate it unless we're able to allocate a new chunk below.
7919 *
7920 * Otherwise, we need to make sure we have room in the space to handle
7921 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007922 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007923 if ((space_info->total_bytes != block_group->key.offset) &&
7924 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04007925 space_info->bytes_pinned + space_info->bytes_readonly +
7926 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04007927 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007928 spin_unlock(&space_info->lock);
7929 goto out;
7930 }
7931 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007932
Josef Bacikba1bf482009-09-11 16:11:19 -04007933 /*
7934 * ok we don't have enough space, but maybe we have free space on our
7935 * devices to allocate new chunks for relocation, so loop through our
7936 * alloc devices and guess if we have enough space. However, if we
7937 * were marked as full, then we know there aren't enough chunks, and we
7938 * can just return.
7939 */
7940 ret = -1;
7941 if (full)
7942 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05007943
Josef Bacikba1bf482009-09-11 16:11:19 -04007944 mutex_lock(&root->fs_info->chunk_mutex);
7945 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7946 u64 min_free = btrfs_block_group_used(&block_group->item);
7947 u64 dev_offset, max_avail;
Chris Masonea8c2812008-08-04 23:17:27 -04007948
Josef Bacikba1bf482009-09-11 16:11:19 -04007949 /*
7950 * check to make sure we can actually find a chunk with enough
7951 * space to fit our block group in.
7952 */
7953 if (device->total_bytes > device->bytes_used + min_free) {
7954 ret = find_free_dev_extent(NULL, device, min_free,
7955 &dev_offset, &max_avail);
7956 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05007957 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04007958 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007959 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007960 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007961 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007962out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007963 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007964 return ret;
7965}
7966
Christoph Hellwigb2950862008-12-02 09:54:17 -05007967static int find_first_block_group(struct btrfs_root *root,
7968 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007969{
Chris Mason925baed2008-06-25 16:01:30 -04007970 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007971 struct btrfs_key found_key;
7972 struct extent_buffer *leaf;
7973 int slot;
7974
7975 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7976 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007977 goto out;
7978
Chris Masond3977122009-01-05 21:25:51 -05007979 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007980 slot = path->slots[0];
7981 leaf = path->nodes[0];
7982 if (slot >= btrfs_header_nritems(leaf)) {
7983 ret = btrfs_next_leaf(root, path);
7984 if (ret == 0)
7985 continue;
7986 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007987 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007988 break;
7989 }
7990 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7991
7992 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007993 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7994 ret = 0;
7995 goto out;
7996 }
Chris Mason0b86a832008-03-24 15:01:56 -04007997 path->slots[0]++;
7998 }
Chris Mason925baed2008-06-25 16:01:30 -04007999out:
Chris Mason0b86a832008-03-24 15:01:56 -04008000 return ret;
8001}
8002
Josef Bacik0af3d002010-06-21 14:48:16 -04008003void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8004{
8005 struct btrfs_block_group_cache *block_group;
8006 u64 last = 0;
8007
8008 while (1) {
8009 struct inode *inode;
8010
8011 block_group = btrfs_lookup_first_block_group(info, last);
8012 while (block_group) {
8013 spin_lock(&block_group->lock);
8014 if (block_group->iref)
8015 break;
8016 spin_unlock(&block_group->lock);
8017 block_group = next_block_group(info->tree_root,
8018 block_group);
8019 }
8020 if (!block_group) {
8021 if (last == 0)
8022 break;
8023 last = 0;
8024 continue;
8025 }
8026
8027 inode = block_group->inode;
8028 block_group->iref = 0;
8029 block_group->inode = NULL;
8030 spin_unlock(&block_group->lock);
8031 iput(inode);
8032 last = block_group->key.objectid + block_group->key.offset;
8033 btrfs_put_block_group(block_group);
8034 }
8035}
8036
Zheng Yan1a40e232008-09-26 10:09:34 -04008037int btrfs_free_block_groups(struct btrfs_fs_info *info)
8038{
8039 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008040 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008041 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008042 struct rb_node *n;
8043
Yan Zheng11833d62009-09-11 16:11:19 -04008044 down_write(&info->extent_commit_sem);
8045 while (!list_empty(&info->caching_block_groups)) {
8046 caching_ctl = list_entry(info->caching_block_groups.next,
8047 struct btrfs_caching_control, list);
8048 list_del(&caching_ctl->list);
8049 put_caching_control(caching_ctl);
8050 }
8051 up_write(&info->extent_commit_sem);
8052
Zheng Yan1a40e232008-09-26 10:09:34 -04008053 spin_lock(&info->block_group_cache_lock);
8054 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8055 block_group = rb_entry(n, struct btrfs_block_group_cache,
8056 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008057 rb_erase(&block_group->cache_node,
8058 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008059 spin_unlock(&info->block_group_cache_lock);
8060
Josef Bacik80eb2342008-10-29 14:49:05 -04008061 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008062 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008063 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008064
Josef Bacik817d52f2009-07-13 21:29:25 -04008065 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008066 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008067
8068 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008069 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008070
8071 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008072 }
8073 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008074
8075 /* now that all the block groups are freed, go through and
8076 * free all the space_info structs. This is only called during
8077 * the final stages of unmount, and so we know nobody is
8078 * using them. We call synchronize_rcu() once before we start,
8079 * just to be on the safe side.
8080 */
8081 synchronize_rcu();
8082
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008083 release_global_block_rsv(info);
8084
Chris Mason4184ea72009-03-10 12:39:20 -04008085 while(!list_empty(&info->space_info)) {
8086 space_info = list_entry(info->space_info.next,
8087 struct btrfs_space_info,
8088 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008089 if (space_info->bytes_pinned > 0 ||
8090 space_info->bytes_reserved > 0) {
8091 WARN_ON(1);
8092 dump_space_info(space_info, 0, 0);
8093 }
Chris Mason4184ea72009-03-10 12:39:20 -04008094 list_del(&space_info->list);
8095 kfree(space_info);
8096 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008097 return 0;
8098}
8099
Yan, Zhengb742bb82010-05-16 10:46:24 -04008100static void __link_block_group(struct btrfs_space_info *space_info,
8101 struct btrfs_block_group_cache *cache)
8102{
8103 int index = get_block_group_index(cache);
8104
8105 down_write(&space_info->groups_sem);
8106 list_add_tail(&cache->list, &space_info->block_groups[index]);
8107 up_write(&space_info->groups_sem);
8108}
8109
Chris Mason9078a3e2007-04-26 16:46:15 -04008110int btrfs_read_block_groups(struct btrfs_root *root)
8111{
8112 struct btrfs_path *path;
8113 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008114 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008115 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008116 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008117 struct btrfs_key key;
8118 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008119 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008120 int need_clear = 0;
8121 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008122
Chris Masonbe744172007-05-06 10:15:01 -04008123 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008124 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008125 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008126 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008127 path = btrfs_alloc_path();
8128 if (!path)
8129 return -ENOMEM;
8130
Josef Bacik0af3d002010-06-21 14:48:16 -04008131 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8132 if (cache_gen != 0 &&
8133 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8134 need_clear = 1;
8135
Chris Masond3977122009-01-05 21:25:51 -05008136 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008137 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008138 if (ret > 0)
8139 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008140 if (ret != 0)
8141 goto error;
8142
Chris Mason5f39d392007-10-15 16:14:19 -04008143 leaf = path->nodes[0];
8144 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008145 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008146 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008147 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008148 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008149 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008150
Yan Zhengd2fb3432008-12-11 16:30:39 -05008151 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008152 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04008153 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008154 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008155 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008156 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008157
Josef Bacik0af3d002010-06-21 14:48:16 -04008158 if (need_clear)
8159 cache->disk_cache_state = BTRFS_DC_CLEAR;
8160
Josef Bacik96303082009-07-13 21:29:25 -04008161 /*
8162 * we only want to have 32k of ram per block group for keeping
8163 * track of free space, and if we pass 1/2 of that we want to
8164 * start converting things over to using bitmaps
8165 */
8166 cache->extents_thresh = ((1024 * 32) / 2) /
8167 sizeof(struct btrfs_free_space);
8168
Chris Mason5f39d392007-10-15 16:14:19 -04008169 read_extent_buffer(leaf, &cache->item,
8170 btrfs_item_ptr_offset(leaf, path->slots[0]),
8171 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008172 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008173
Chris Mason9078a3e2007-04-26 16:46:15 -04008174 key.objectid = found_key.objectid + found_key.offset;
8175 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04008176 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008177 cache->sectorsize = root->sectorsize;
8178
Josef Bacik817d52f2009-07-13 21:29:25 -04008179 /*
8180 * check for two cases, either we are full, and therefore
8181 * don't need to bother with the caching work since we won't
8182 * find any space, or we are empty, and we can just add all
8183 * the space in and be done with it. This saves us _alot_ of
8184 * time, particularly in the full case.
8185 */
8186 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04008187 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04008188 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008189 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008190 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008191 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008192 exclude_super_stripes(root, cache);
8193 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008194 cache->cached = BTRFS_CACHE_FINISHED;
8195 add_new_free_space(cache, root->fs_info,
8196 found_key.objectid,
8197 found_key.objectid +
8198 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008199 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008200 }
Chris Mason96b51792007-10-15 16:15:19 -04008201
Chris Mason6324fbf2008-03-24 15:01:59 -04008202 ret = update_space_info(info, cache->flags, found_key.offset,
8203 btrfs_block_group_used(&cache->item),
8204 &space_info);
8205 BUG_ON(ret);
8206 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008207 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008208 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008209 spin_unlock(&cache->space_info->lock);
8210
Yan, Zhengb742bb82010-05-16 10:46:24 -04008211 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008212
Josef Bacik0f9dd462008-09-23 13:14:11 -04008213 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8214 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04008215
8216 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008217 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04008218 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04008219 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008220
8221 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8222 if (!(get_alloc_profile(root, space_info->flags) &
8223 (BTRFS_BLOCK_GROUP_RAID10 |
8224 BTRFS_BLOCK_GROUP_RAID1 |
8225 BTRFS_BLOCK_GROUP_DUP)))
8226 continue;
8227 /*
8228 * avoid allocating from un-mirrored block group if there are
8229 * mirrored block groups.
8230 */
8231 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008232 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008233 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008234 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008235 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008236
8237 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008238 ret = 0;
8239error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008240 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008241 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008242}
Chris Mason6324fbf2008-03-24 15:01:59 -04008243
8244int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8245 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008246 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008247 u64 size)
8248{
8249 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008250 struct btrfs_root *extent_root;
8251 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008252
8253 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008254
Chris Mason12fcfd22009-03-24 10:24:20 -04008255 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008256
Chris Mason8f18cf12008-04-25 16:53:30 -04008257 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008258 if (!cache)
8259 return -ENOMEM;
8260
Chris Masone17cade2008-04-15 15:41:47 -04008261 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008262 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008263 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008264 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008265 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04008266
8267 /*
8268 * we only want to have 32k of ram per block group for keeping track
8269 * of free space, and if we pass 1/2 of that we want to start
8270 * converting things over to using bitmaps
8271 */
8272 cache->extents_thresh = ((1024 * 32) / 2) /
8273 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008274 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008275 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04008276 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008277 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008278 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008279
Chris Mason6324fbf2008-03-24 15:01:59 -04008280 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008281 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8282 cache->flags = type;
8283 btrfs_set_block_group_flags(&cache->item, type);
8284
Yan Zheng11833d62009-09-11 16:11:19 -04008285 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008286 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04008287 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04008288
Josef Bacik817d52f2009-07-13 21:29:25 -04008289 add_new_free_space(cache, root->fs_info, chunk_offset,
8290 chunk_offset + size);
8291
Yan Zheng11833d62009-09-11 16:11:19 -04008292 free_excluded_extents(root, cache);
8293
Chris Mason6324fbf2008-03-24 15:01:59 -04008294 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8295 &cache->space_info);
8296 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04008297
8298 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008299 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008300 spin_unlock(&cache->space_info->lock);
8301
Yan, Zhengb742bb82010-05-16 10:46:24 -04008302 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008303
Josef Bacik0f9dd462008-09-23 13:14:11 -04008304 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8305 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04008306
Chris Mason6324fbf2008-03-24 15:01:59 -04008307 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8308 sizeof(cache->item));
8309 BUG_ON(ret);
8310
Chris Masond18a2c42008-04-04 15:40:00 -04008311 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008312
Chris Mason6324fbf2008-03-24 15:01:59 -04008313 return 0;
8314}
Zheng Yan1a40e232008-09-26 10:09:34 -04008315
8316int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8317 struct btrfs_root *root, u64 group_start)
8318{
8319 struct btrfs_path *path;
8320 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008321 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008322 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008323 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008324 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008325 int ret;
8326
Zheng Yan1a40e232008-09-26 10:09:34 -04008327 root = root->fs_info->extent_root;
8328
8329 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8330 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008331 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008332
Chris Mason44fb5512009-06-04 15:34:51 -04008333 /* make sure this block group isn't part of an allocation cluster */
8334 cluster = &root->fs_info->data_alloc_cluster;
8335 spin_lock(&cluster->refill_lock);
8336 btrfs_return_cluster_to_free_space(block_group, cluster);
8337 spin_unlock(&cluster->refill_lock);
8338
8339 /*
8340 * make sure this block group isn't part of a metadata
8341 * allocation cluster
8342 */
8343 cluster = &root->fs_info->meta_alloc_cluster;
8344 spin_lock(&cluster->refill_lock);
8345 btrfs_return_cluster_to_free_space(block_group, cluster);
8346 spin_unlock(&cluster->refill_lock);
8347
Zheng Yan1a40e232008-09-26 10:09:34 -04008348 path = btrfs_alloc_path();
8349 BUG_ON(!path);
8350
Josef Bacik0af3d002010-06-21 14:48:16 -04008351 inode = lookup_free_space_inode(root, block_group, path);
8352 if (!IS_ERR(inode)) {
8353 btrfs_orphan_add(trans, inode);
8354 clear_nlink(inode);
8355 /* One for the block groups ref */
8356 spin_lock(&block_group->lock);
8357 if (block_group->iref) {
8358 block_group->iref = 0;
8359 block_group->inode = NULL;
8360 spin_unlock(&block_group->lock);
8361 iput(inode);
8362 } else {
8363 spin_unlock(&block_group->lock);
8364 }
8365 /* One for our lookup ref */
8366 iput(inode);
8367 }
8368
8369 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8370 key.offset = block_group->key.objectid;
8371 key.type = 0;
8372
8373 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8374 if (ret < 0)
8375 goto out;
8376 if (ret > 0)
8377 btrfs_release_path(tree_root, path);
8378 if (ret == 0) {
8379 ret = btrfs_del_item(trans, tree_root, path);
8380 if (ret)
8381 goto out;
8382 btrfs_release_path(tree_root, path);
8383 }
8384
Yan Zheng3dfdb932009-01-21 10:49:16 -05008385 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008386 rb_erase(&block_group->cache_node,
8387 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008388 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008389
Josef Bacik80eb2342008-10-29 14:49:05 -04008390 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008391 /*
8392 * we must use list_del_init so people can check to see if they
8393 * are still on the list after taking the semaphore
8394 */
8395 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008396 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008397
Josef Bacik817d52f2009-07-13 21:29:25 -04008398 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008399 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008400
8401 btrfs_remove_free_space_cache(block_group);
8402
Yan Zhengc146afa2008-11-12 14:34:12 -05008403 spin_lock(&block_group->space_info->lock);
8404 block_group->space_info->total_bytes -= block_group->key.offset;
8405 block_group->space_info->bytes_readonly -= block_group->key.offset;
8406 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008407
Josef Bacik0af3d002010-06-21 14:48:16 -04008408 memcpy(&key, &block_group->key, sizeof(key));
8409
Chris Mason283bb192009-07-24 16:30:55 -04008410 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008411
Chris Masonfa9c0d792009-04-03 09:47:43 -04008412 btrfs_put_block_group(block_group);
8413 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008414
8415 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8416 if (ret > 0)
8417 ret = -EIO;
8418 if (ret < 0)
8419 goto out;
8420
8421 ret = btrfs_del_item(trans, root, path);
8422out:
8423 btrfs_free_path(path);
8424 return ret;
8425}