blob: 9de4ff03882acafe8d5f651c045899891464001a [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
Josef Bacikdde5abe2010-09-16 16:17:03 -0400245 /* We're loading it the fast way, so we don't have a caching_ctl. */
246 if (!cache->caching_ctl) {
247 spin_unlock(&cache->lock);
248 return NULL;
249 }
250
Yan Zheng11833d62009-09-11 16:11:19 -0400251 ctl = cache->caching_ctl;
252 atomic_inc(&ctl->count);
253 spin_unlock(&cache->lock);
254 return ctl;
255}
256
257static void put_caching_control(struct btrfs_caching_control *ctl)
258{
259 if (atomic_dec_and_test(&ctl->count))
260 kfree(ctl);
261}
262
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263/*
264 * this is only called by cache_block_group, since we could have freed extents
265 * we need to check the pinned_extents for any extents that can't be used yet
266 * since their free space will be released as soon as the transaction commits.
267 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400268static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400269 struct btrfs_fs_info *info, u64 start, u64 end)
270{
Josef Bacik817d52f2009-07-13 21:29:25 -0400271 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400272 int ret;
273
274 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400275 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400276 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400277 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400278 if (ret)
279 break;
280
Yan, Zheng06b23312009-11-26 09:31:11 +0000281 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400282 start = extent_end + 1;
283 } else if (extent_start > start && extent_start < end) {
284 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400285 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500286 ret = btrfs_add_free_space(block_group, start,
287 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400288 BUG_ON(ret);
289 start = extent_end + 1;
290 } else {
291 break;
292 }
293 }
294
295 if (start < end) {
296 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400297 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500298 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400299 BUG_ON(ret);
300 }
301
Josef Bacik817d52f2009-07-13 21:29:25 -0400302 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400303}
304
Josef Bacik817d52f2009-07-13 21:29:25 -0400305static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400306{
Josef Bacik817d52f2009-07-13 21:29:25 -0400307 struct btrfs_block_group_cache *block_group = data;
308 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400309 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
310 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400311 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400312 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400313 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400314 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400315 u64 last = 0;
316 u32 nritems;
317 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400318
Chris Masone37c9e62007-05-09 20:13:14 -0400319 path = btrfs_alloc_path();
320 if (!path)
321 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400322
Yan Zheng11833d62009-09-11 16:11:19 -0400323 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400324 spin_lock(&block_group->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400325 block_group->space_info->bytes_readonly += block_group->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -0400326 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400327
Josef Bacik817d52f2009-07-13 21:29:25 -0400328 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400329
Chris Mason5cd57b22008-06-25 16:01:30 -0400330 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 * We don't want to deadlock with somebody trying to allocate a new
332 * extent for the extent root while also trying to search the extent
333 * root to add free space. So we skip locking and search the commit
334 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400335 */
336 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400337 path->search_commit_root = 1;
338 path->reada = 2;
339
Yan Zhenge4404d62008-12-12 10:03:26 -0500340 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400341 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400342 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400343again:
Yan Zheng11833d62009-09-11 16:11:19 -0400344 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400345 /* need to make sure the commit_root doesn't disappear */
346 down_read(&fs_info->extent_commit_sem);
347
Yan Zheng11833d62009-09-11 16:11:19 -0400348 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400349 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400350 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500351
Yan Zheng11833d62009-09-11 16:11:19 -0400352 leaf = path->nodes[0];
353 nritems = btrfs_header_nritems(leaf);
354
Chris Masond3977122009-01-05 21:25:51 -0500355 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400356 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400357 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400358 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400359 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400360 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400361
Yan Zheng11833d62009-09-11 16:11:19 -0400362 if (path->slots[0] < nritems) {
363 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
364 } else {
365 ret = find_next_key(path, 0, &key);
366 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400367 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400368
Yan Zheng11833d62009-09-11 16:11:19 -0400369 caching_ctl->progress = last;
370 btrfs_release_path(extent_root, path);
371 up_read(&fs_info->extent_commit_sem);
372 mutex_unlock(&caching_ctl->mutex);
373 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400374 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400375 else
376 cond_resched();
377 goto again;
378 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400379
Yan Zheng11833d62009-09-11 16:11:19 -0400380 if (key.objectid < block_group->key.objectid) {
381 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400382 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400383 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400384
Chris Masone37c9e62007-05-09 20:13:14 -0400385 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400386 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400387 break;
Yan7d7d6062007-09-14 16:15:28 -0400388
Yan Zheng11833d62009-09-11 16:11:19 -0400389 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400390 total_found += add_new_free_space(block_group,
391 fs_info, last,
392 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400393 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400394
Yan Zheng11833d62009-09-11 16:11:19 -0400395 if (total_found > (1024 * 1024 * 2)) {
396 total_found = 0;
397 wake_up(&caching_ctl->wait);
398 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400399 }
Chris Masone37c9e62007-05-09 20:13:14 -0400400 path->slots[0]++;
401 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400402 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400403
404 total_found += add_new_free_space(block_group, fs_info, last,
405 block_group->key.objectid +
406 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400407 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400408
409 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400410 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400411 block_group->cached = BTRFS_CACHE_FINISHED;
412 spin_unlock(&block_group->lock);
413
Chris Mason54aa1f42007-06-22 14:16:25 -0400414err:
Chris Masone37c9e62007-05-09 20:13:14 -0400415 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400416 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400417
Yan Zheng11833d62009-09-11 16:11:19 -0400418 free_excluded_extents(extent_root, block_group);
419
420 mutex_unlock(&caching_ctl->mutex);
421 wake_up(&caching_ctl->wait);
422
423 put_caching_control(caching_ctl);
424 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000425 btrfs_put_block_group(block_group);
426
Josef Bacik817d52f2009-07-13 21:29:25 -0400427 return 0;
428}
429
Josef Bacik9d66e232010-08-25 16:54:15 -0400430static int cache_block_group(struct btrfs_block_group_cache *cache,
431 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500432 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400433 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400434{
Yan Zheng11833d62009-09-11 16:11:19 -0400435 struct btrfs_fs_info *fs_info = cache->fs_info;
436 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400437 struct task_struct *tsk;
438 int ret = 0;
439
Yan Zheng11833d62009-09-11 16:11:19 -0400440 smp_mb();
441 if (cache->cached != BTRFS_CACHE_NO)
442 return 0;
443
Josef Bacik9d66e232010-08-25 16:54:15 -0400444 /*
445 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500446 * to have the normal tree locking. Also if we are currently trying to
447 * allocate blocks for the tree root we can't do the fast caching since
448 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400449 */
Josef Bacikb8399de2010-12-08 09:15:11 -0500450 if (!trans->transaction->in_commit &&
451 (root && root != root->fs_info->tree_root)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400452 spin_lock(&cache->lock);
453 if (cache->cached != BTRFS_CACHE_NO) {
454 spin_unlock(&cache->lock);
455 return 0;
456 }
457 cache->cached = BTRFS_CACHE_STARTED;
458 spin_unlock(&cache->lock);
459
460 ret = load_free_space_cache(fs_info, cache);
461
462 spin_lock(&cache->lock);
463 if (ret == 1) {
464 cache->cached = BTRFS_CACHE_FINISHED;
465 cache->last_byte_to_unpin = (u64)-1;
466 } else {
467 cache->cached = BTRFS_CACHE_NO;
468 }
469 spin_unlock(&cache->lock);
470 if (ret == 1)
471 return 0;
472 }
473
474 if (load_cache_only)
475 return 0;
476
Yan Zheng11833d62009-09-11 16:11:19 -0400477 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
478 BUG_ON(!caching_ctl);
479
480 INIT_LIST_HEAD(&caching_ctl->list);
481 mutex_init(&caching_ctl->mutex);
482 init_waitqueue_head(&caching_ctl->wait);
483 caching_ctl->block_group = cache;
484 caching_ctl->progress = cache->key.objectid;
485 /* one for caching kthread, one for caching block group list */
486 atomic_set(&caching_ctl->count, 2);
487
Josef Bacik817d52f2009-07-13 21:29:25 -0400488 spin_lock(&cache->lock);
489 if (cache->cached != BTRFS_CACHE_NO) {
490 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400491 kfree(caching_ctl);
492 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400493 }
Yan Zheng11833d62009-09-11 16:11:19 -0400494 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400495 cache->cached = BTRFS_CACHE_STARTED;
496 spin_unlock(&cache->lock);
497
Yan Zheng11833d62009-09-11 16:11:19 -0400498 down_write(&fs_info->extent_commit_sem);
499 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
500 up_write(&fs_info->extent_commit_sem);
501
502 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000503 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400504
Josef Bacik817d52f2009-07-13 21:29:25 -0400505 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
506 cache->key.objectid);
507 if (IS_ERR(tsk)) {
508 ret = PTR_ERR(tsk);
509 printk(KERN_ERR "error running thread %d\n", ret);
510 BUG();
511 }
512
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400513 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400514}
515
Josef Bacik0f9dd462008-09-23 13:14:11 -0400516/*
517 * return the block group that starts at or after bytenr
518 */
Chris Masond3977122009-01-05 21:25:51 -0500519static struct btrfs_block_group_cache *
520btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400521{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400522 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400523
Josef Bacik0f9dd462008-09-23 13:14:11 -0400524 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400525
Josef Bacik0f9dd462008-09-23 13:14:11 -0400526 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400527}
528
Josef Bacik0f9dd462008-09-23 13:14:11 -0400529/*
Sankar P9f556842009-05-14 13:52:22 -0400530 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400531 */
Chris Masond3977122009-01-05 21:25:51 -0500532struct btrfs_block_group_cache *btrfs_lookup_block_group(
533 struct btrfs_fs_info *info,
534 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400535{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400536 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400537
Josef Bacik0f9dd462008-09-23 13:14:11 -0400538 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400539
Josef Bacik0f9dd462008-09-23 13:14:11 -0400540 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400541}
Chris Mason0b86a832008-03-24 15:01:56 -0400542
Josef Bacik0f9dd462008-09-23 13:14:11 -0400543static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
544 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400545{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400546 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400547 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400548
Yan, Zhengb742bb822010-05-16 10:46:24 -0400549 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
550 BTRFS_BLOCK_GROUP_METADATA;
551
Chris Mason4184ea72009-03-10 12:39:20 -0400552 rcu_read_lock();
553 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400554 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400555 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400556 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400557 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400558 }
Chris Mason4184ea72009-03-10 12:39:20 -0400559 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400560 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400561}
562
Chris Mason4184ea72009-03-10 12:39:20 -0400563/*
564 * after adding space to the filesystem, we need to clear the full flags
565 * on all the space infos.
566 */
567void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
568{
569 struct list_head *head = &info->space_info;
570 struct btrfs_space_info *found;
571
572 rcu_read_lock();
573 list_for_each_entry_rcu(found, head, list)
574 found->full = 0;
575 rcu_read_unlock();
576}
577
Josef Bacik80eb2342008-10-29 14:49:05 -0400578static u64 div_factor(u64 num, int factor)
579{
580 if (factor == 10)
581 return num;
582 num *= factor;
583 do_div(num, 10);
584 return num;
585}
586
Chris Masone5bc2452010-10-26 13:37:56 -0400587static u64 div_factor_fine(u64 num, int factor)
588{
589 if (factor == 100)
590 return num;
591 num *= factor;
592 do_div(num, 100);
593 return num;
594}
595
Yan Zhengd2fb3432008-12-11 16:30:39 -0500596u64 btrfs_find_block_group(struct btrfs_root *root,
597 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400598{
Chris Mason96b51792007-10-15 16:15:19 -0400599 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400600 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500601 u64 last = max(search_hint, search_start);
602 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400603 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500604 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400605 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400606again:
Zheng Yane8569812008-09-26 10:05:48 -0400607 while (1) {
608 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400609 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400610 break;
Chris Mason96b51792007-10-15 16:15:19 -0400611
Chris Masonc286ac42008-07-22 23:06:41 -0400612 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400613 last = cache->key.objectid + cache->key.offset;
614 used = btrfs_block_group_used(&cache->item);
615
Yan Zhengd2fb3432008-12-11 16:30:39 -0500616 if ((full_search || !cache->ro) &&
617 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400618 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500619 div_factor(cache->key.offset, factor)) {
620 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400621 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400622 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400623 goto found;
624 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400625 }
Chris Masonc286ac42008-07-22 23:06:41 -0400626 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400627 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400628 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400629 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400630 if (!wrapped) {
631 last = search_start;
632 wrapped = 1;
633 goto again;
634 }
635 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400636 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400637 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400638 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400639 goto again;
640 }
Chris Masonbe744172007-05-06 10:15:01 -0400641found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500642 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400643}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400644
Chris Masone02119d2008-09-05 16:13:11 -0400645/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400646int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400647{
648 int ret;
649 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400650 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400651
Zheng Yan31840ae2008-09-23 13:14:14 -0400652 path = btrfs_alloc_path();
653 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400654 key.objectid = start;
655 key.offset = len;
656 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
657 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
658 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400659 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500660 return ret;
661}
662
Chris Masond8d5f3e2007-12-11 12:42:00 -0500663/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400664 * helper function to lookup reference count and flags of extent.
665 *
666 * the head node for delayed ref is used to store the sum of all the
667 * reference count modifications queued up in the rbtree. the head
668 * node may also store the extent flags to set. This way you can check
669 * to see what the reference count and extent flags would be if all of
670 * the delayed refs are not processed.
671 */
672int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
673 struct btrfs_root *root, u64 bytenr,
674 u64 num_bytes, u64 *refs, u64 *flags)
675{
676 struct btrfs_delayed_ref_head *head;
677 struct btrfs_delayed_ref_root *delayed_refs;
678 struct btrfs_path *path;
679 struct btrfs_extent_item *ei;
680 struct extent_buffer *leaf;
681 struct btrfs_key key;
682 u32 item_size;
683 u64 num_refs;
684 u64 extent_flags;
685 int ret;
686
687 path = btrfs_alloc_path();
688 if (!path)
689 return -ENOMEM;
690
691 key.objectid = bytenr;
692 key.type = BTRFS_EXTENT_ITEM_KEY;
693 key.offset = num_bytes;
694 if (!trans) {
695 path->skip_locking = 1;
696 path->search_commit_root = 1;
697 }
698again:
699 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
700 &key, path, 0, 0);
701 if (ret < 0)
702 goto out_free;
703
704 if (ret == 0) {
705 leaf = path->nodes[0];
706 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
707 if (item_size >= sizeof(*ei)) {
708 ei = btrfs_item_ptr(leaf, path->slots[0],
709 struct btrfs_extent_item);
710 num_refs = btrfs_extent_refs(leaf, ei);
711 extent_flags = btrfs_extent_flags(leaf, ei);
712 } else {
713#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
714 struct btrfs_extent_item_v0 *ei0;
715 BUG_ON(item_size != sizeof(*ei0));
716 ei0 = btrfs_item_ptr(leaf, path->slots[0],
717 struct btrfs_extent_item_v0);
718 num_refs = btrfs_extent_refs_v0(leaf, ei0);
719 /* FIXME: this isn't correct for data */
720 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
721#else
722 BUG();
723#endif
724 }
725 BUG_ON(num_refs == 0);
726 } else {
727 num_refs = 0;
728 extent_flags = 0;
729 ret = 0;
730 }
731
732 if (!trans)
733 goto out;
734
735 delayed_refs = &trans->transaction->delayed_refs;
736 spin_lock(&delayed_refs->lock);
737 head = btrfs_find_delayed_ref_head(trans, bytenr);
738 if (head) {
739 if (!mutex_trylock(&head->mutex)) {
740 atomic_inc(&head->node.refs);
741 spin_unlock(&delayed_refs->lock);
742
743 btrfs_release_path(root->fs_info->extent_root, path);
744
745 mutex_lock(&head->mutex);
746 mutex_unlock(&head->mutex);
747 btrfs_put_delayed_ref(&head->node);
748 goto again;
749 }
750 if (head->extent_op && head->extent_op->update_flags)
751 extent_flags |= head->extent_op->flags_to_set;
752 else
753 BUG_ON(num_refs == 0);
754
755 num_refs += head->node.ref_mod;
756 mutex_unlock(&head->mutex);
757 }
758 spin_unlock(&delayed_refs->lock);
759out:
760 WARN_ON(num_refs == 0);
761 if (refs)
762 *refs = num_refs;
763 if (flags)
764 *flags = extent_flags;
765out_free:
766 btrfs_free_path(path);
767 return ret;
768}
769
770/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500771 * Back reference rules. Back refs have three main goals:
772 *
773 * 1) differentiate between all holders of references to an extent so that
774 * when a reference is dropped we can make sure it was a valid reference
775 * before freeing the extent.
776 *
777 * 2) Provide enough information to quickly find the holders of an extent
778 * if we notice a given block is corrupted or bad.
779 *
780 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
781 * maintenance. This is actually the same as #2, but with a slightly
782 * different use case.
783 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400784 * There are two kinds of back refs. The implicit back refs is optimized
785 * for pointers in non-shared tree blocks. For a given pointer in a block,
786 * back refs of this kind provide information about the block's owner tree
787 * and the pointer's key. These information allow us to find the block by
788 * b-tree searching. The full back refs is for pointers in tree blocks not
789 * referenced by their owner trees. The location of tree block is recorded
790 * in the back refs. Actually the full back refs is generic, and can be
791 * used in all cases the implicit back refs is used. The major shortcoming
792 * of the full back refs is its overhead. Every time a tree block gets
793 * COWed, we have to update back refs entry for all pointers in it.
794 *
795 * For a newly allocated tree block, we use implicit back refs for
796 * pointers in it. This means most tree related operations only involve
797 * implicit back refs. For a tree block created in old transaction, the
798 * only way to drop a reference to it is COW it. So we can detect the
799 * event that tree block loses its owner tree's reference and do the
800 * back refs conversion.
801 *
802 * When a tree block is COW'd through a tree, there are four cases:
803 *
804 * The reference count of the block is one and the tree is the block's
805 * owner tree. Nothing to do in this case.
806 *
807 * The reference count of the block is one and the tree is not the
808 * block's owner tree. In this case, full back refs is used for pointers
809 * in the block. Remove these full back refs, add implicit back refs for
810 * every pointers in the new block.
811 *
812 * The reference count of the block is greater than one and the tree is
813 * the block's owner tree. In this case, implicit back refs is used for
814 * pointers in the block. Add full back refs for every pointers in the
815 * block, increase lower level extents' reference counts. The original
816 * implicit back refs are entailed to the new block.
817 *
818 * The reference count of the block is greater than one and the tree is
819 * not the block's owner tree. Add implicit back refs for every pointer in
820 * the new block, increase lower level extents' reference count.
821 *
822 * Back Reference Key composing:
823 *
824 * The key objectid corresponds to the first byte in the extent,
825 * The key type is used to differentiate between types of back refs.
826 * There are different meanings of the key offset for different types
827 * of back refs.
828 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500829 * File extents can be referenced by:
830 *
831 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400832 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500833 * - different offsets inside a file (bookend extents in file.c)
834 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400835 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500836 *
837 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500838 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400839 * - original offset in the file
840 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400841 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400842 * The key offset for the implicit back refs is hash of the first
843 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500844 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400845 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500846 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400847 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500848 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400849 * The key offset for the implicit back refs is the first byte of
850 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500851 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400852 * When a file extent is allocated, The implicit back refs is used.
853 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500854 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400855 * (root_key.objectid, inode objectid, offset in file, 1)
856 *
857 * When a file extent is removed file truncation, we find the
858 * corresponding implicit back refs and check the following fields:
859 *
860 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500861 *
862 * Btree extents can be referenced by:
863 *
864 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500865 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 * Both the implicit back refs and the full back refs for tree blocks
867 * only consist of key. The key offset for the implicit back refs is
868 * objectid of block's owner tree. The key offset for the full back refs
869 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500870 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871 * When implicit back refs is used, information about the lowest key and
872 * level of the tree block are required. These information are stored in
873 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500874 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400875
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400876#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
877static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
878 struct btrfs_root *root,
879 struct btrfs_path *path,
880 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500881{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400882 struct btrfs_extent_item *item;
883 struct btrfs_extent_item_v0 *ei0;
884 struct btrfs_extent_ref_v0 *ref0;
885 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400886 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400887 struct btrfs_key key;
888 struct btrfs_key found_key;
889 u32 new_size = sizeof(*item);
890 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500891 int ret;
892
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400893 leaf = path->nodes[0];
894 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500895
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400896 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
897 ei0 = btrfs_item_ptr(leaf, path->slots[0],
898 struct btrfs_extent_item_v0);
899 refs = btrfs_extent_refs_v0(leaf, ei0);
900
901 if (owner == (u64)-1) {
902 while (1) {
903 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
904 ret = btrfs_next_leaf(root, path);
905 if (ret < 0)
906 return ret;
907 BUG_ON(ret > 0);
908 leaf = path->nodes[0];
909 }
910 btrfs_item_key_to_cpu(leaf, &found_key,
911 path->slots[0]);
912 BUG_ON(key.objectid != found_key.objectid);
913 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
914 path->slots[0]++;
915 continue;
916 }
917 ref0 = btrfs_item_ptr(leaf, path->slots[0],
918 struct btrfs_extent_ref_v0);
919 owner = btrfs_ref_objectid_v0(leaf, ref0);
920 break;
921 }
922 }
923 btrfs_release_path(root, path);
924
925 if (owner < BTRFS_FIRST_FREE_OBJECTID)
926 new_size += sizeof(*bi);
927
928 new_size -= sizeof(*ei0);
929 ret = btrfs_search_slot(trans, root, &key, path,
930 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400931 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400932 return ret;
933 BUG_ON(ret);
934
935 ret = btrfs_extend_item(trans, root, path, new_size);
936 BUG_ON(ret);
937
938 leaf = path->nodes[0];
939 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
940 btrfs_set_extent_refs(leaf, item, refs);
941 /* FIXME: get real generation */
942 btrfs_set_extent_generation(leaf, item, 0);
943 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
944 btrfs_set_extent_flags(leaf, item,
945 BTRFS_EXTENT_FLAG_TREE_BLOCK |
946 BTRFS_BLOCK_FLAG_FULL_BACKREF);
947 bi = (struct btrfs_tree_block_info *)(item + 1);
948 /* FIXME: get first key of the block */
949 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
950 btrfs_set_tree_block_level(leaf, bi, (int)owner);
951 } else {
952 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
953 }
954 btrfs_mark_buffer_dirty(leaf);
955 return 0;
956}
957#endif
958
959static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
960{
961 u32 high_crc = ~(u32)0;
962 u32 low_crc = ~(u32)0;
963 __le64 lenum;
964
965 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100966 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400967 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100968 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400969 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100970 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400971
972 return ((u64)high_crc << 31) ^ (u64)low_crc;
973}
974
975static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
976 struct btrfs_extent_data_ref *ref)
977{
978 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
979 btrfs_extent_data_ref_objectid(leaf, ref),
980 btrfs_extent_data_ref_offset(leaf, ref));
981}
982
983static int match_extent_data_ref(struct extent_buffer *leaf,
984 struct btrfs_extent_data_ref *ref,
985 u64 root_objectid, u64 owner, u64 offset)
986{
987 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
988 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
989 btrfs_extent_data_ref_offset(leaf, ref) != offset)
990 return 0;
991 return 1;
992}
993
994static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
995 struct btrfs_root *root,
996 struct btrfs_path *path,
997 u64 bytenr, u64 parent,
998 u64 root_objectid,
999 u64 owner, u64 offset)
1000{
1001 struct btrfs_key key;
1002 struct btrfs_extent_data_ref *ref;
1003 struct extent_buffer *leaf;
1004 u32 nritems;
1005 int ret;
1006 int recow;
1007 int err = -ENOENT;
1008
1009 key.objectid = bytenr;
1010 if (parent) {
1011 key.type = BTRFS_SHARED_DATA_REF_KEY;
1012 key.offset = parent;
1013 } else {
1014 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1015 key.offset = hash_extent_data_ref(root_objectid,
1016 owner, offset);
1017 }
1018again:
1019 recow = 0;
1020 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1021 if (ret < 0) {
1022 err = ret;
1023 goto fail;
1024 }
1025
1026 if (parent) {
1027 if (!ret)
1028 return 0;
1029#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1030 key.type = BTRFS_EXTENT_REF_V0_KEY;
1031 btrfs_release_path(root, path);
1032 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1033 if (ret < 0) {
1034 err = ret;
1035 goto fail;
1036 }
1037 if (!ret)
1038 return 0;
1039#endif
1040 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001041 }
1042
1043 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044 nritems = btrfs_header_nritems(leaf);
1045 while (1) {
1046 if (path->slots[0] >= nritems) {
1047 ret = btrfs_next_leaf(root, path);
1048 if (ret < 0)
1049 err = ret;
1050 if (ret)
1051 goto fail;
1052
1053 leaf = path->nodes[0];
1054 nritems = btrfs_header_nritems(leaf);
1055 recow = 1;
1056 }
1057
1058 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1059 if (key.objectid != bytenr ||
1060 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1061 goto fail;
1062
1063 ref = btrfs_item_ptr(leaf, path->slots[0],
1064 struct btrfs_extent_data_ref);
1065
1066 if (match_extent_data_ref(leaf, ref, root_objectid,
1067 owner, offset)) {
1068 if (recow) {
1069 btrfs_release_path(root, path);
1070 goto again;
1071 }
1072 err = 0;
1073 break;
1074 }
1075 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001076 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001077fail:
1078 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001079}
1080
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001081static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1082 struct btrfs_root *root,
1083 struct btrfs_path *path,
1084 u64 bytenr, u64 parent,
1085 u64 root_objectid, u64 owner,
1086 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001087{
1088 struct btrfs_key key;
1089 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001091 u32 num_refs;
1092 int ret;
1093
1094 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095 if (parent) {
1096 key.type = BTRFS_SHARED_DATA_REF_KEY;
1097 key.offset = parent;
1098 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001099 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001100 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1101 key.offset = hash_extent_data_ref(root_objectid,
1102 owner, offset);
1103 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001104 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001105
1106 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1107 if (ret && ret != -EEXIST)
1108 goto fail;
1109
1110 leaf = path->nodes[0];
1111 if (parent) {
1112 struct btrfs_shared_data_ref *ref;
1113 ref = btrfs_item_ptr(leaf, path->slots[0],
1114 struct btrfs_shared_data_ref);
1115 if (ret == 0) {
1116 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1117 } else {
1118 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1119 num_refs += refs_to_add;
1120 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1121 }
1122 } else {
1123 struct btrfs_extent_data_ref *ref;
1124 while (ret == -EEXIST) {
1125 ref = btrfs_item_ptr(leaf, path->slots[0],
1126 struct btrfs_extent_data_ref);
1127 if (match_extent_data_ref(leaf, ref, root_objectid,
1128 owner, offset))
1129 break;
1130 btrfs_release_path(root, path);
1131 key.offset++;
1132 ret = btrfs_insert_empty_item(trans, root, path, &key,
1133 size);
1134 if (ret && ret != -EEXIST)
1135 goto fail;
1136
1137 leaf = path->nodes[0];
1138 }
1139 ref = btrfs_item_ptr(leaf, path->slots[0],
1140 struct btrfs_extent_data_ref);
1141 if (ret == 0) {
1142 btrfs_set_extent_data_ref_root(leaf, ref,
1143 root_objectid);
1144 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1145 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1146 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1147 } else {
1148 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1149 num_refs += refs_to_add;
1150 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1151 }
1152 }
1153 btrfs_mark_buffer_dirty(leaf);
1154 ret = 0;
1155fail:
Chris Mason7bb86312007-12-11 09:25:06 -05001156 btrfs_release_path(root, path);
1157 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001158}
1159
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001160static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root,
1162 struct btrfs_path *path,
1163 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001164{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001165 struct btrfs_key key;
1166 struct btrfs_extent_data_ref *ref1 = NULL;
1167 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001168 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001169 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001170 int ret = 0;
1171
1172 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001173 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1174
1175 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1176 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1177 struct btrfs_extent_data_ref);
1178 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1179 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1180 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1181 struct btrfs_shared_data_ref);
1182 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1183#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1184 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1185 struct btrfs_extent_ref_v0 *ref0;
1186 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1187 struct btrfs_extent_ref_v0);
1188 num_refs = btrfs_ref_count_v0(leaf, ref0);
1189#endif
1190 } else {
1191 BUG();
1192 }
1193
Chris Mason56bec292009-03-13 10:10:06 -04001194 BUG_ON(num_refs < refs_to_drop);
1195 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001196
Zheng Yan31840ae2008-09-23 13:14:14 -04001197 if (num_refs == 0) {
1198 ret = btrfs_del_item(trans, root, path);
1199 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001200 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1201 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1202 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1203 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1204#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1205 else {
1206 struct btrfs_extent_ref_v0 *ref0;
1207 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1208 struct btrfs_extent_ref_v0);
1209 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1210 }
1211#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001212 btrfs_mark_buffer_dirty(leaf);
1213 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001214 return ret;
1215}
1216
1217static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1218 struct btrfs_path *path,
1219 struct btrfs_extent_inline_ref *iref)
1220{
1221 struct btrfs_key key;
1222 struct extent_buffer *leaf;
1223 struct btrfs_extent_data_ref *ref1;
1224 struct btrfs_shared_data_ref *ref2;
1225 u32 num_refs = 0;
1226
1227 leaf = path->nodes[0];
1228 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1229 if (iref) {
1230 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1231 BTRFS_EXTENT_DATA_REF_KEY) {
1232 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1233 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1234 } else {
1235 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1236 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1237 }
1238 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1239 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1240 struct btrfs_extent_data_ref);
1241 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1242 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1243 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1244 struct btrfs_shared_data_ref);
1245 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1246#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1247 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1248 struct btrfs_extent_ref_v0 *ref0;
1249 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1250 struct btrfs_extent_ref_v0);
1251 num_refs = btrfs_ref_count_v0(leaf, ref0);
1252#endif
1253 } else {
1254 WARN_ON(1);
1255 }
1256 return num_refs;
1257}
1258
1259static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1260 struct btrfs_root *root,
1261 struct btrfs_path *path,
1262 u64 bytenr, u64 parent,
1263 u64 root_objectid)
1264{
1265 struct btrfs_key key;
1266 int ret;
1267
1268 key.objectid = bytenr;
1269 if (parent) {
1270 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1271 key.offset = parent;
1272 } else {
1273 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1274 key.offset = root_objectid;
1275 }
1276
1277 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1278 if (ret > 0)
1279 ret = -ENOENT;
1280#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1281 if (ret == -ENOENT && parent) {
1282 btrfs_release_path(root, path);
1283 key.type = BTRFS_EXTENT_REF_V0_KEY;
1284 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1285 if (ret > 0)
1286 ret = -ENOENT;
1287 }
1288#endif
1289 return ret;
1290}
1291
1292static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1293 struct btrfs_root *root,
1294 struct btrfs_path *path,
1295 u64 bytenr, u64 parent,
1296 u64 root_objectid)
1297{
1298 struct btrfs_key key;
1299 int ret;
1300
1301 key.objectid = bytenr;
1302 if (parent) {
1303 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1304 key.offset = parent;
1305 } else {
1306 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1307 key.offset = root_objectid;
1308 }
1309
1310 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001311 btrfs_release_path(root, path);
1312 return ret;
1313}
1314
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001315static inline int extent_ref_type(u64 parent, u64 owner)
1316{
1317 int type;
1318 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1319 if (parent > 0)
1320 type = BTRFS_SHARED_BLOCK_REF_KEY;
1321 else
1322 type = BTRFS_TREE_BLOCK_REF_KEY;
1323 } else {
1324 if (parent > 0)
1325 type = BTRFS_SHARED_DATA_REF_KEY;
1326 else
1327 type = BTRFS_EXTENT_DATA_REF_KEY;
1328 }
1329 return type;
1330}
1331
Yan Zheng2c47e6052009-06-27 21:07:35 -04001332static int find_next_key(struct btrfs_path *path, int level,
1333 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001334
1335{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001336 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001337 if (!path->nodes[level])
1338 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001339 if (path->slots[level] + 1 >=
1340 btrfs_header_nritems(path->nodes[level]))
1341 continue;
1342 if (level == 0)
1343 btrfs_item_key_to_cpu(path->nodes[level], key,
1344 path->slots[level] + 1);
1345 else
1346 btrfs_node_key_to_cpu(path->nodes[level], key,
1347 path->slots[level] + 1);
1348 return 0;
1349 }
1350 return 1;
1351}
1352
1353/*
1354 * look for inline back ref. if back ref is found, *ref_ret is set
1355 * to the address of inline back ref, and 0 is returned.
1356 *
1357 * if back ref isn't found, *ref_ret is set to the address where it
1358 * should be inserted, and -ENOENT is returned.
1359 *
1360 * if insert is true and there are too many inline back refs, the path
1361 * points to the extent item, and -EAGAIN is returned.
1362 *
1363 * NOTE: inline back refs are ordered in the same way that back ref
1364 * items in the tree are ordered.
1365 */
1366static noinline_for_stack
1367int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1368 struct btrfs_root *root,
1369 struct btrfs_path *path,
1370 struct btrfs_extent_inline_ref **ref_ret,
1371 u64 bytenr, u64 num_bytes,
1372 u64 parent, u64 root_objectid,
1373 u64 owner, u64 offset, int insert)
1374{
1375 struct btrfs_key key;
1376 struct extent_buffer *leaf;
1377 struct btrfs_extent_item *ei;
1378 struct btrfs_extent_inline_ref *iref;
1379 u64 flags;
1380 u64 item_size;
1381 unsigned long ptr;
1382 unsigned long end;
1383 int extra_size;
1384 int type;
1385 int want;
1386 int ret;
1387 int err = 0;
1388
1389 key.objectid = bytenr;
1390 key.type = BTRFS_EXTENT_ITEM_KEY;
1391 key.offset = num_bytes;
1392
1393 want = extent_ref_type(parent, owner);
1394 if (insert) {
1395 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001396 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 } else
1398 extra_size = -1;
1399 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1400 if (ret < 0) {
1401 err = ret;
1402 goto out;
1403 }
1404 BUG_ON(ret);
1405
1406 leaf = path->nodes[0];
1407 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1408#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1409 if (item_size < sizeof(*ei)) {
1410 if (!insert) {
1411 err = -ENOENT;
1412 goto out;
1413 }
1414 ret = convert_extent_item_v0(trans, root, path, owner,
1415 extra_size);
1416 if (ret < 0) {
1417 err = ret;
1418 goto out;
1419 }
1420 leaf = path->nodes[0];
1421 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1422 }
1423#endif
1424 BUG_ON(item_size < sizeof(*ei));
1425
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001426 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1427 flags = btrfs_extent_flags(leaf, ei);
1428
1429 ptr = (unsigned long)(ei + 1);
1430 end = (unsigned long)ei + item_size;
1431
1432 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1433 ptr += sizeof(struct btrfs_tree_block_info);
1434 BUG_ON(ptr > end);
1435 } else {
1436 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1437 }
1438
1439 err = -ENOENT;
1440 while (1) {
1441 if (ptr >= end) {
1442 WARN_ON(ptr > end);
1443 break;
1444 }
1445 iref = (struct btrfs_extent_inline_ref *)ptr;
1446 type = btrfs_extent_inline_ref_type(leaf, iref);
1447 if (want < type)
1448 break;
1449 if (want > type) {
1450 ptr += btrfs_extent_inline_ref_size(type);
1451 continue;
1452 }
1453
1454 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1455 struct btrfs_extent_data_ref *dref;
1456 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1457 if (match_extent_data_ref(leaf, dref, root_objectid,
1458 owner, offset)) {
1459 err = 0;
1460 break;
1461 }
1462 if (hash_extent_data_ref_item(leaf, dref) <
1463 hash_extent_data_ref(root_objectid, owner, offset))
1464 break;
1465 } else {
1466 u64 ref_offset;
1467 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1468 if (parent > 0) {
1469 if (parent == ref_offset) {
1470 err = 0;
1471 break;
1472 }
1473 if (ref_offset < parent)
1474 break;
1475 } else {
1476 if (root_objectid == ref_offset) {
1477 err = 0;
1478 break;
1479 }
1480 if (ref_offset < root_objectid)
1481 break;
1482 }
1483 }
1484 ptr += btrfs_extent_inline_ref_size(type);
1485 }
1486 if (err == -ENOENT && insert) {
1487 if (item_size + extra_size >=
1488 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1489 err = -EAGAIN;
1490 goto out;
1491 }
1492 /*
1493 * To add new inline back ref, we have to make sure
1494 * there is no corresponding back ref item.
1495 * For simplicity, we just do not add new inline back
1496 * ref if there is any kind of item for this block
1497 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001498 if (find_next_key(path, 0, &key) == 0 &&
1499 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001500 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001501 err = -EAGAIN;
1502 goto out;
1503 }
1504 }
1505 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1506out:
Yan Zheng85d41982009-06-11 08:51:10 -04001507 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001508 path->keep_locks = 0;
1509 btrfs_unlock_up_safe(path, 1);
1510 }
1511 return err;
1512}
1513
1514/*
1515 * helper to add new inline back ref
1516 */
1517static noinline_for_stack
1518int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1519 struct btrfs_root *root,
1520 struct btrfs_path *path,
1521 struct btrfs_extent_inline_ref *iref,
1522 u64 parent, u64 root_objectid,
1523 u64 owner, u64 offset, int refs_to_add,
1524 struct btrfs_delayed_extent_op *extent_op)
1525{
1526 struct extent_buffer *leaf;
1527 struct btrfs_extent_item *ei;
1528 unsigned long ptr;
1529 unsigned long end;
1530 unsigned long item_offset;
1531 u64 refs;
1532 int size;
1533 int type;
1534 int ret;
1535
1536 leaf = path->nodes[0];
1537 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1538 item_offset = (unsigned long)iref - (unsigned long)ei;
1539
1540 type = extent_ref_type(parent, owner);
1541 size = btrfs_extent_inline_ref_size(type);
1542
1543 ret = btrfs_extend_item(trans, root, path, size);
1544 BUG_ON(ret);
1545
1546 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1547 refs = btrfs_extent_refs(leaf, ei);
1548 refs += refs_to_add;
1549 btrfs_set_extent_refs(leaf, ei, refs);
1550 if (extent_op)
1551 __run_delayed_extent_op(extent_op, leaf, ei);
1552
1553 ptr = (unsigned long)ei + item_offset;
1554 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1555 if (ptr < end - size)
1556 memmove_extent_buffer(leaf, ptr + size, ptr,
1557 end - size - ptr);
1558
1559 iref = (struct btrfs_extent_inline_ref *)ptr;
1560 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1561 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1562 struct btrfs_extent_data_ref *dref;
1563 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1564 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1565 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1566 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1567 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1568 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1569 struct btrfs_shared_data_ref *sref;
1570 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1571 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1572 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1573 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1574 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1575 } else {
1576 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1577 }
1578 btrfs_mark_buffer_dirty(leaf);
1579 return 0;
1580}
1581
1582static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1583 struct btrfs_root *root,
1584 struct btrfs_path *path,
1585 struct btrfs_extent_inline_ref **ref_ret,
1586 u64 bytenr, u64 num_bytes, u64 parent,
1587 u64 root_objectid, u64 owner, u64 offset)
1588{
1589 int ret;
1590
1591 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1592 bytenr, num_bytes, parent,
1593 root_objectid, owner, offset, 0);
1594 if (ret != -ENOENT)
1595 return ret;
1596
1597 btrfs_release_path(root, path);
1598 *ref_ret = NULL;
1599
1600 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1601 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1602 root_objectid);
1603 } else {
1604 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1605 root_objectid, owner, offset);
1606 }
1607 return ret;
1608}
1609
1610/*
1611 * helper to update/remove inline back ref
1612 */
1613static noinline_for_stack
1614int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1615 struct btrfs_root *root,
1616 struct btrfs_path *path,
1617 struct btrfs_extent_inline_ref *iref,
1618 int refs_to_mod,
1619 struct btrfs_delayed_extent_op *extent_op)
1620{
1621 struct extent_buffer *leaf;
1622 struct btrfs_extent_item *ei;
1623 struct btrfs_extent_data_ref *dref = NULL;
1624 struct btrfs_shared_data_ref *sref = NULL;
1625 unsigned long ptr;
1626 unsigned long end;
1627 u32 item_size;
1628 int size;
1629 int type;
1630 int ret;
1631 u64 refs;
1632
1633 leaf = path->nodes[0];
1634 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1635 refs = btrfs_extent_refs(leaf, ei);
1636 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1637 refs += refs_to_mod;
1638 btrfs_set_extent_refs(leaf, ei, refs);
1639 if (extent_op)
1640 __run_delayed_extent_op(extent_op, leaf, ei);
1641
1642 type = btrfs_extent_inline_ref_type(leaf, iref);
1643
1644 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1645 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1646 refs = btrfs_extent_data_ref_count(leaf, dref);
1647 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1648 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1649 refs = btrfs_shared_data_ref_count(leaf, sref);
1650 } else {
1651 refs = 1;
1652 BUG_ON(refs_to_mod != -1);
1653 }
1654
1655 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1656 refs += refs_to_mod;
1657
1658 if (refs > 0) {
1659 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1660 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1661 else
1662 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1663 } else {
1664 size = btrfs_extent_inline_ref_size(type);
1665 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1666 ptr = (unsigned long)iref;
1667 end = (unsigned long)ei + item_size;
1668 if (ptr + size < end)
1669 memmove_extent_buffer(leaf, ptr, ptr + size,
1670 end - ptr - size);
1671 item_size -= size;
1672 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1673 BUG_ON(ret);
1674 }
1675 btrfs_mark_buffer_dirty(leaf);
1676 return 0;
1677}
1678
1679static noinline_for_stack
1680int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1681 struct btrfs_root *root,
1682 struct btrfs_path *path,
1683 u64 bytenr, u64 num_bytes, u64 parent,
1684 u64 root_objectid, u64 owner,
1685 u64 offset, int refs_to_add,
1686 struct btrfs_delayed_extent_op *extent_op)
1687{
1688 struct btrfs_extent_inline_ref *iref;
1689 int ret;
1690
1691 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1692 bytenr, num_bytes, parent,
1693 root_objectid, owner, offset, 1);
1694 if (ret == 0) {
1695 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1696 ret = update_inline_extent_backref(trans, root, path, iref,
1697 refs_to_add, extent_op);
1698 } else if (ret == -ENOENT) {
1699 ret = setup_inline_extent_backref(trans, root, path, iref,
1700 parent, root_objectid,
1701 owner, offset, refs_to_add,
1702 extent_op);
1703 }
1704 return ret;
1705}
1706
1707static int insert_extent_backref(struct btrfs_trans_handle *trans,
1708 struct btrfs_root *root,
1709 struct btrfs_path *path,
1710 u64 bytenr, u64 parent, u64 root_objectid,
1711 u64 owner, u64 offset, int refs_to_add)
1712{
1713 int ret;
1714 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1715 BUG_ON(refs_to_add != 1);
1716 ret = insert_tree_block_ref(trans, root, path, bytenr,
1717 parent, root_objectid);
1718 } else {
1719 ret = insert_extent_data_ref(trans, root, path, bytenr,
1720 parent, root_objectid,
1721 owner, offset, refs_to_add);
1722 }
1723 return ret;
1724}
1725
1726static int remove_extent_backref(struct btrfs_trans_handle *trans,
1727 struct btrfs_root *root,
1728 struct btrfs_path *path,
1729 struct btrfs_extent_inline_ref *iref,
1730 int refs_to_drop, int is_data)
1731{
1732 int ret;
1733
1734 BUG_ON(!is_data && refs_to_drop != 1);
1735 if (iref) {
1736 ret = update_inline_extent_backref(trans, root, path, iref,
1737 -refs_to_drop, NULL);
1738 } else if (is_data) {
1739 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1740 } else {
1741 ret = btrfs_del_item(trans, root, path);
1742 }
1743 return ret;
1744}
1745
Chris Mason15916de2008-11-19 21:17:22 -05001746static void btrfs_issue_discard(struct block_device *bdev,
1747 u64 start, u64 len)
1748{
Christoph Hellwig746cd1e2009-09-12 07:35:43 +02001749 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +04001750 BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
Chris Mason15916de2008-11-19 21:17:22 -05001751}
Chris Mason15916de2008-11-19 21:17:22 -05001752
Liu Hui1f3c79a2009-01-05 15:57:51 -05001753static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1754 u64 num_bytes)
1755{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001756 int ret;
1757 u64 map_length = num_bytes;
1758 struct btrfs_multi_bio *multi = NULL;
1759
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001760 if (!btrfs_test_opt(root, DISCARD))
1761 return 0;
1762
Liu Hui1f3c79a2009-01-05 15:57:51 -05001763 /* Tell the block device(s) that the sectors can be discarded */
1764 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1765 bytenr, &map_length, &multi, 0);
1766 if (!ret) {
1767 struct btrfs_bio_stripe *stripe = multi->stripes;
1768 int i;
1769
1770 if (map_length > num_bytes)
1771 map_length = num_bytes;
1772
1773 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1774 btrfs_issue_discard(stripe->dev->bdev,
1775 stripe->physical,
1776 map_length);
1777 }
1778 kfree(multi);
1779 }
1780
1781 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001782}
1783
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001784int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1785 struct btrfs_root *root,
1786 u64 bytenr, u64 num_bytes, u64 parent,
1787 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001788{
1789 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001790 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1791 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001792
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001793 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1794 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1795 parent, root_objectid, (int)owner,
1796 BTRFS_ADD_DELAYED_REF, NULL);
1797 } else {
1798 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1799 parent, root_objectid, owner, offset,
1800 BTRFS_ADD_DELAYED_REF, NULL);
1801 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001802 return ret;
1803}
1804
Chris Mason925baed2008-06-25 16:01:30 -04001805static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001806 struct btrfs_root *root,
1807 u64 bytenr, u64 num_bytes,
1808 u64 parent, u64 root_objectid,
1809 u64 owner, u64 offset, int refs_to_add,
1810 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001811{
Chris Mason5caf2a02007-04-02 11:20:42 -04001812 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001813 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001814 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001815 u64 refs;
1816 int ret;
1817 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001818
Chris Mason5caf2a02007-04-02 11:20:42 -04001819 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001820 if (!path)
1821 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001822
Chris Mason3c12ac72008-04-21 12:01:38 -04001823 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001824 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001825 /* this will setup the path even if it fails to insert the back ref */
1826 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1827 path, bytenr, num_bytes, parent,
1828 root_objectid, owner, offset,
1829 refs_to_add, extent_op);
1830 if (ret == 0)
1831 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001832
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001833 if (ret != -EAGAIN) {
1834 err = ret;
1835 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001836 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001837
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001838 leaf = path->nodes[0];
1839 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1840 refs = btrfs_extent_refs(leaf, item);
1841 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1842 if (extent_op)
1843 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001844
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001845 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001846 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001847
Chris Mason3c12ac72008-04-21 12:01:38 -04001848 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001849 path->leave_spinning = 1;
1850
Chris Mason56bec292009-03-13 10:10:06 -04001851 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001852 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001853 path, bytenr, parent, root_objectid,
1854 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001855 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001856out:
Chris Mason74493f72007-12-11 09:25:06 -05001857 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001858 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001859}
1860
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001861static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1862 struct btrfs_root *root,
1863 struct btrfs_delayed_ref_node *node,
1864 struct btrfs_delayed_extent_op *extent_op,
1865 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001866{
Chris Mason56bec292009-03-13 10:10:06 -04001867 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001868 struct btrfs_delayed_data_ref *ref;
1869 struct btrfs_key ins;
1870 u64 parent = 0;
1871 u64 ref_root = 0;
1872 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001873
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001874 ins.objectid = node->bytenr;
1875 ins.offset = node->num_bytes;
1876 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001877
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 ref = btrfs_delayed_node_to_data_ref(node);
1879 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1880 parent = ref->parent;
1881 else
1882 ref_root = ref->root;
1883
1884 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1885 if (extent_op) {
1886 BUG_ON(extent_op->update_key);
1887 flags |= extent_op->flags_to_set;
1888 }
1889 ret = alloc_reserved_file_extent(trans, root,
1890 parent, ref_root, flags,
1891 ref->objectid, ref->offset,
1892 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001893 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1894 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1895 node->num_bytes, parent,
1896 ref_root, ref->objectid,
1897 ref->offset, node->ref_mod,
1898 extent_op);
1899 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1900 ret = __btrfs_free_extent(trans, root, node->bytenr,
1901 node->num_bytes, parent,
1902 ref_root, ref->objectid,
1903 ref->offset, node->ref_mod,
1904 extent_op);
1905 } else {
1906 BUG();
1907 }
Chris Mason56bec292009-03-13 10:10:06 -04001908 return ret;
1909}
1910
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001911static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1912 struct extent_buffer *leaf,
1913 struct btrfs_extent_item *ei)
1914{
1915 u64 flags = btrfs_extent_flags(leaf, ei);
1916 if (extent_op->update_flags) {
1917 flags |= extent_op->flags_to_set;
1918 btrfs_set_extent_flags(leaf, ei, flags);
1919 }
1920
1921 if (extent_op->update_key) {
1922 struct btrfs_tree_block_info *bi;
1923 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1924 bi = (struct btrfs_tree_block_info *)(ei + 1);
1925 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1926 }
1927}
1928
1929static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1930 struct btrfs_root *root,
1931 struct btrfs_delayed_ref_node *node,
1932 struct btrfs_delayed_extent_op *extent_op)
1933{
1934 struct btrfs_key key;
1935 struct btrfs_path *path;
1936 struct btrfs_extent_item *ei;
1937 struct extent_buffer *leaf;
1938 u32 item_size;
1939 int ret;
1940 int err = 0;
1941
1942 path = btrfs_alloc_path();
1943 if (!path)
1944 return -ENOMEM;
1945
1946 key.objectid = node->bytenr;
1947 key.type = BTRFS_EXTENT_ITEM_KEY;
1948 key.offset = node->num_bytes;
1949
1950 path->reada = 1;
1951 path->leave_spinning = 1;
1952 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1953 path, 0, 1);
1954 if (ret < 0) {
1955 err = ret;
1956 goto out;
1957 }
1958 if (ret > 0) {
1959 err = -EIO;
1960 goto out;
1961 }
1962
1963 leaf = path->nodes[0];
1964 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1965#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1966 if (item_size < sizeof(*ei)) {
1967 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1968 path, (u64)-1, 0);
1969 if (ret < 0) {
1970 err = ret;
1971 goto out;
1972 }
1973 leaf = path->nodes[0];
1974 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1975 }
1976#endif
1977 BUG_ON(item_size < sizeof(*ei));
1978 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1979 __run_delayed_extent_op(extent_op, leaf, ei);
1980
1981 btrfs_mark_buffer_dirty(leaf);
1982out:
1983 btrfs_free_path(path);
1984 return err;
1985}
1986
1987static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1988 struct btrfs_root *root,
1989 struct btrfs_delayed_ref_node *node,
1990 struct btrfs_delayed_extent_op *extent_op,
1991 int insert_reserved)
1992{
1993 int ret = 0;
1994 struct btrfs_delayed_tree_ref *ref;
1995 struct btrfs_key ins;
1996 u64 parent = 0;
1997 u64 ref_root = 0;
1998
1999 ins.objectid = node->bytenr;
2000 ins.offset = node->num_bytes;
2001 ins.type = BTRFS_EXTENT_ITEM_KEY;
2002
2003 ref = btrfs_delayed_node_to_tree_ref(node);
2004 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2005 parent = ref->parent;
2006 else
2007 ref_root = ref->root;
2008
2009 BUG_ON(node->ref_mod != 1);
2010 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2011 BUG_ON(!extent_op || !extent_op->update_flags ||
2012 !extent_op->update_key);
2013 ret = alloc_reserved_tree_block(trans, root,
2014 parent, ref_root,
2015 extent_op->flags_to_set,
2016 &extent_op->key,
2017 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002018 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2019 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2020 node->num_bytes, parent, ref_root,
2021 ref->level, 0, 1, extent_op);
2022 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2023 ret = __btrfs_free_extent(trans, root, node->bytenr,
2024 node->num_bytes, parent, ref_root,
2025 ref->level, 0, 1, extent_op);
2026 } else {
2027 BUG();
2028 }
2029 return ret;
2030}
2031
Chris Mason56bec292009-03-13 10:10:06 -04002032/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002033static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2034 struct btrfs_root *root,
2035 struct btrfs_delayed_ref_node *node,
2036 struct btrfs_delayed_extent_op *extent_op,
2037 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002038{
Josef Bacikeb099672009-02-12 09:27:38 -05002039 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002040 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002041 struct btrfs_delayed_ref_head *head;
2042 /*
2043 * we've hit the end of the chain and we were supposed
2044 * to insert this extent into the tree. But, it got
2045 * deleted before we ever needed to insert it, so all
2046 * we have to do is clean up the accounting
2047 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002048 BUG_ON(extent_op);
2049 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002050 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002051 btrfs_pin_extent(root, node->bytenr,
2052 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002053 if (head->is_data) {
2054 ret = btrfs_del_csums(trans, root,
2055 node->bytenr,
2056 node->num_bytes);
2057 BUG_ON(ret);
2058 }
Chris Mason56bec292009-03-13 10:10:06 -04002059 }
Chris Mason56bec292009-03-13 10:10:06 -04002060 mutex_unlock(&head->mutex);
2061 return 0;
2062 }
Josef Bacikeb099672009-02-12 09:27:38 -05002063
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2065 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2066 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2067 insert_reserved);
2068 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2069 node->type == BTRFS_SHARED_DATA_REF_KEY)
2070 ret = run_delayed_data_ref(trans, root, node, extent_op,
2071 insert_reserved);
2072 else
2073 BUG();
2074 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002075}
2076
Chris Mason56bec292009-03-13 10:10:06 -04002077static noinline struct btrfs_delayed_ref_node *
2078select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002079{
Chris Mason56bec292009-03-13 10:10:06 -04002080 struct rb_node *node;
2081 struct btrfs_delayed_ref_node *ref;
2082 int action = BTRFS_ADD_DELAYED_REF;
2083again:
2084 /*
2085 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2086 * this prevents ref count from going down to zero when
2087 * there still are pending delayed ref.
2088 */
2089 node = rb_prev(&head->node.rb_node);
2090 while (1) {
2091 if (!node)
2092 break;
2093 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2094 rb_node);
2095 if (ref->bytenr != head->node.bytenr)
2096 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002097 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002098 return ref;
2099 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002100 }
Chris Mason56bec292009-03-13 10:10:06 -04002101 if (action == BTRFS_ADD_DELAYED_REF) {
2102 action = BTRFS_DROP_DELAYED_REF;
2103 goto again;
2104 }
2105 return NULL;
2106}
2107
Chris Masonc3e69d52009-03-13 10:17:05 -04002108static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2109 struct btrfs_root *root,
2110 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002111{
Chris Mason56bec292009-03-13 10:10:06 -04002112 struct btrfs_delayed_ref_root *delayed_refs;
2113 struct btrfs_delayed_ref_node *ref;
2114 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002115 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002116 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002117 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002118 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002119
2120 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002121 while (1) {
2122 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002123 /* pick a new head ref from the cluster list */
2124 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002125 break;
Chris Mason56bec292009-03-13 10:10:06 -04002126
Chris Masonc3e69d52009-03-13 10:17:05 -04002127 locked_ref = list_entry(cluster->next,
2128 struct btrfs_delayed_ref_head, cluster);
2129
2130 /* grab the lock that says we are going to process
2131 * all the refs for this head */
2132 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2133
2134 /*
2135 * we may have dropped the spin lock to get the head
2136 * mutex lock, and that might have given someone else
2137 * time to free the head. If that's true, it has been
2138 * removed from our list and we can move on.
2139 */
2140 if (ret == -EAGAIN) {
2141 locked_ref = NULL;
2142 count++;
2143 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002144 }
2145 }
2146
2147 /*
2148 * record the must insert reserved flag before we
2149 * drop the spin lock.
2150 */
2151 must_insert_reserved = locked_ref->must_insert_reserved;
2152 locked_ref->must_insert_reserved = 0;
2153
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002154 extent_op = locked_ref->extent_op;
2155 locked_ref->extent_op = NULL;
2156
Chris Mason56bec292009-03-13 10:10:06 -04002157 /*
2158 * locked_ref is the head node, so we have to go one
2159 * node back for any delayed ref updates
2160 */
Chris Mason56bec292009-03-13 10:10:06 -04002161 ref = select_delayed_ref(locked_ref);
2162 if (!ref) {
2163 /* All delayed refs have been processed, Go ahead
2164 * and send the head node to run_one_delayed_ref,
2165 * so that any accounting fixes can happen
2166 */
2167 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002168
2169 if (extent_op && must_insert_reserved) {
2170 kfree(extent_op);
2171 extent_op = NULL;
2172 }
2173
2174 if (extent_op) {
2175 spin_unlock(&delayed_refs->lock);
2176
2177 ret = run_delayed_extent_op(trans, root,
2178 ref, extent_op);
2179 BUG_ON(ret);
2180 kfree(extent_op);
2181
2182 cond_resched();
2183 spin_lock(&delayed_refs->lock);
2184 continue;
2185 }
2186
Chris Masonc3e69d52009-03-13 10:17:05 -04002187 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002188 locked_ref = NULL;
2189 }
2190
2191 ref->in_tree = 0;
2192 rb_erase(&ref->rb_node, &delayed_refs->root);
2193 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002194
Chris Mason56bec292009-03-13 10:10:06 -04002195 spin_unlock(&delayed_refs->lock);
2196
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002197 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002198 must_insert_reserved);
2199 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002200
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002201 btrfs_put_delayed_ref(ref);
2202 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002203 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002204
Chris Mason1887be62009-03-13 10:11:24 -04002205 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002206 spin_lock(&delayed_refs->lock);
2207 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002208 return count;
2209}
2210
2211/*
2212 * this starts processing the delayed reference count updates and
2213 * extent insertions we have queued up so far. count can be
2214 * 0, which means to process everything in the tree at the start
2215 * of the run (but not newly added entries), or it can be some target
2216 * number you'd like to process.
2217 */
2218int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2219 struct btrfs_root *root, unsigned long count)
2220{
2221 struct rb_node *node;
2222 struct btrfs_delayed_ref_root *delayed_refs;
2223 struct btrfs_delayed_ref_node *ref;
2224 struct list_head cluster;
2225 int ret;
2226 int run_all = count == (unsigned long)-1;
2227 int run_most = 0;
2228
2229 if (root == root->fs_info->extent_root)
2230 root = root->fs_info->tree_root;
2231
2232 delayed_refs = &trans->transaction->delayed_refs;
2233 INIT_LIST_HEAD(&cluster);
2234again:
2235 spin_lock(&delayed_refs->lock);
2236 if (count == 0) {
2237 count = delayed_refs->num_entries * 2;
2238 run_most = 1;
2239 }
2240 while (1) {
2241 if (!(run_all || run_most) &&
2242 delayed_refs->num_heads_ready < 64)
2243 break;
2244
2245 /*
2246 * go find something we can process in the rbtree. We start at
2247 * the beginning of the tree, and then build a cluster
2248 * of refs to process starting at the first one we are able to
2249 * lock
2250 */
2251 ret = btrfs_find_ref_cluster(trans, &cluster,
2252 delayed_refs->run_delayed_start);
2253 if (ret)
2254 break;
2255
2256 ret = run_clustered_refs(trans, root, &cluster);
2257 BUG_ON(ret < 0);
2258
2259 count -= min_t(unsigned long, ret, count);
2260
2261 if (count == 0)
2262 break;
2263 }
2264
Chris Mason56bec292009-03-13 10:10:06 -04002265 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002266 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002267 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002268 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002269 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002270
2271 while (node) {
2272 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2273 rb_node);
2274 if (btrfs_delayed_ref_is_head(ref)) {
2275 struct btrfs_delayed_ref_head *head;
2276
2277 head = btrfs_delayed_node_to_head(ref);
2278 atomic_inc(&ref->refs);
2279
2280 spin_unlock(&delayed_refs->lock);
2281 mutex_lock(&head->mutex);
2282 mutex_unlock(&head->mutex);
2283
2284 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002285 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002286 goto again;
2287 }
2288 node = rb_next(node);
2289 }
2290 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002291 schedule_timeout(1);
2292 goto again;
2293 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002294out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002295 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002296 return 0;
2297}
2298
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2300 struct btrfs_root *root,
2301 u64 bytenr, u64 num_bytes, u64 flags,
2302 int is_data)
2303{
2304 struct btrfs_delayed_extent_op *extent_op;
2305 int ret;
2306
2307 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2308 if (!extent_op)
2309 return -ENOMEM;
2310
2311 extent_op->flags_to_set = flags;
2312 extent_op->update_flags = 1;
2313 extent_op->update_key = 0;
2314 extent_op->is_data = is_data ? 1 : 0;
2315
2316 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2317 if (ret)
2318 kfree(extent_op);
2319 return ret;
2320}
2321
2322static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2323 struct btrfs_root *root,
2324 struct btrfs_path *path,
2325 u64 objectid, u64 offset, u64 bytenr)
2326{
2327 struct btrfs_delayed_ref_head *head;
2328 struct btrfs_delayed_ref_node *ref;
2329 struct btrfs_delayed_data_ref *data_ref;
2330 struct btrfs_delayed_ref_root *delayed_refs;
2331 struct rb_node *node;
2332 int ret = 0;
2333
2334 ret = -ENOENT;
2335 delayed_refs = &trans->transaction->delayed_refs;
2336 spin_lock(&delayed_refs->lock);
2337 head = btrfs_find_delayed_ref_head(trans, bytenr);
2338 if (!head)
2339 goto out;
2340
2341 if (!mutex_trylock(&head->mutex)) {
2342 atomic_inc(&head->node.refs);
2343 spin_unlock(&delayed_refs->lock);
2344
2345 btrfs_release_path(root->fs_info->extent_root, path);
2346
2347 mutex_lock(&head->mutex);
2348 mutex_unlock(&head->mutex);
2349 btrfs_put_delayed_ref(&head->node);
2350 return -EAGAIN;
2351 }
2352
2353 node = rb_prev(&head->node.rb_node);
2354 if (!node)
2355 goto out_unlock;
2356
2357 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2358
2359 if (ref->bytenr != bytenr)
2360 goto out_unlock;
2361
2362 ret = 1;
2363 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2364 goto out_unlock;
2365
2366 data_ref = btrfs_delayed_node_to_data_ref(ref);
2367
2368 node = rb_prev(node);
2369 if (node) {
2370 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2371 if (ref->bytenr == bytenr)
2372 goto out_unlock;
2373 }
2374
2375 if (data_ref->root != root->root_key.objectid ||
2376 data_ref->objectid != objectid || data_ref->offset != offset)
2377 goto out_unlock;
2378
2379 ret = 0;
2380out_unlock:
2381 mutex_unlock(&head->mutex);
2382out:
2383 spin_unlock(&delayed_refs->lock);
2384 return ret;
2385}
2386
2387static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2388 struct btrfs_root *root,
2389 struct btrfs_path *path,
2390 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002391{
2392 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002393 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002394 struct btrfs_extent_data_ref *ref;
2395 struct btrfs_extent_inline_ref *iref;
2396 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002397 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002398 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002399 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002400
Chris Masonbe20aa92007-12-17 20:14:01 -05002401 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002402 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002403 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002404
Chris Masonbe20aa92007-12-17 20:14:01 -05002405 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2406 if (ret < 0)
2407 goto out;
2408 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002409
2410 ret = -ENOENT;
2411 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002412 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002413
Zheng Yan31840ae2008-09-23 13:14:14 -04002414 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002415 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002416 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002417
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002418 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002419 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002420
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002421 ret = 1;
2422 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2423#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2424 if (item_size < sizeof(*ei)) {
2425 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2426 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002427 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002428#endif
2429 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2430
2431 if (item_size != sizeof(*ei) +
2432 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2433 goto out;
2434
2435 if (btrfs_extent_generation(leaf, ei) <=
2436 btrfs_root_last_snapshot(&root->root_item))
2437 goto out;
2438
2439 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2440 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2441 BTRFS_EXTENT_DATA_REF_KEY)
2442 goto out;
2443
2444 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2445 if (btrfs_extent_refs(leaf, ei) !=
2446 btrfs_extent_data_ref_count(leaf, ref) ||
2447 btrfs_extent_data_ref_root(leaf, ref) !=
2448 root->root_key.objectid ||
2449 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2450 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2451 goto out;
2452
Yan Zhengf321e492008-07-30 09:26:11 -04002453 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002454out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002455 return ret;
2456}
2457
2458int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2459 struct btrfs_root *root,
2460 u64 objectid, u64 offset, u64 bytenr)
2461{
2462 struct btrfs_path *path;
2463 int ret;
2464 int ret2;
2465
2466 path = btrfs_alloc_path();
2467 if (!path)
2468 return -ENOENT;
2469
2470 do {
2471 ret = check_committed_ref(trans, root, path, objectid,
2472 offset, bytenr);
2473 if (ret && ret != -ENOENT)
2474 goto out;
2475
2476 ret2 = check_delayed_ref(trans, root, path, objectid,
2477 offset, bytenr);
2478 } while (ret2 == -EAGAIN);
2479
2480 if (ret2 && ret2 != -ENOENT) {
2481 ret = ret2;
2482 goto out;
2483 }
2484
2485 if (ret != -ENOENT || ret2 != -ENOENT)
2486 ret = 0;
2487out:
Yan Zhengf321e492008-07-30 09:26:11 -04002488 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002489 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2490 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002491 return ret;
2492}
2493
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002494#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002495int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2496 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002497{
Chris Mason5f39d392007-10-15 16:14:19 -04002498 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002499 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002500 u64 root_gen;
2501 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002502 int i;
Chris Masondb945352007-10-15 16:15:53 -04002503 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002504 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002505 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002506
Chris Mason3768f362007-03-13 16:47:54 -04002507 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002508 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002509
Zheng Yane4657682008-09-26 10:04:53 -04002510 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2511 shared = 0;
2512 root_gen = root->root_key.offset;
2513 } else {
2514 shared = 1;
2515 root_gen = trans->transid - 1;
2516 }
2517
Chris Masondb945352007-10-15 16:15:53 -04002518 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002519 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002520
Zheng Yan31840ae2008-09-23 13:14:14 -04002521 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002522 struct btrfs_leaf_ref *ref;
2523 struct btrfs_extent_info *info;
2524
Zheng Yan31840ae2008-09-23 13:14:14 -04002525 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002526 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002527 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002528 goto out;
2529 }
2530
Zheng Yane4657682008-09-26 10:04:53 -04002531 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002532 ref->bytenr = buf->start;
2533 ref->owner = btrfs_header_owner(buf);
2534 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002535 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002536 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002537
Zheng Yan31840ae2008-09-23 13:14:14 -04002538 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002539 u64 disk_bytenr;
2540 btrfs_item_key_to_cpu(buf, &key, i);
2541 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2542 continue;
2543 fi = btrfs_item_ptr(buf, i,
2544 struct btrfs_file_extent_item);
2545 if (btrfs_file_extent_type(buf, fi) ==
2546 BTRFS_FILE_EXTENT_INLINE)
2547 continue;
2548 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2549 if (disk_bytenr == 0)
2550 continue;
2551
2552 info->bytenr = disk_bytenr;
2553 info->num_bytes =
2554 btrfs_file_extent_disk_num_bytes(buf, fi);
2555 info->objectid = key.objectid;
2556 info->offset = key.offset;
2557 info++;
2558 }
2559
Zheng Yane4657682008-09-26 10:04:53 -04002560 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002561 if (ret == -EEXIST && shared) {
2562 struct btrfs_leaf_ref *old;
2563 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2564 BUG_ON(!old);
2565 btrfs_remove_leaf_ref(root, old);
2566 btrfs_free_leaf_ref(root, old);
2567 ret = btrfs_add_leaf_ref(root, ref, shared);
2568 }
Yan Zheng31153d82008-07-28 15:32:19 -04002569 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002570 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002571 }
2572out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002573 return ret;
2574}
2575
Chris Masonb7a9f292009-02-04 09:23:45 -05002576/* when a block goes through cow, we update the reference counts of
2577 * everything that block points to. The internal pointers of the block
2578 * can be in just about any order, and it is likely to have clusters of
2579 * things that are close together and clusters of things that are not.
2580 *
2581 * To help reduce the seeks that come with updating all of these reference
2582 * counts, sort them by byte number before actual updates are done.
2583 *
2584 * struct refsort is used to match byte number to slot in the btree block.
2585 * we sort based on the byte number and then use the slot to actually
2586 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002587 *
2588 * struct refsort is smaller than strcut btrfs_item and smaller than
2589 * struct btrfs_key_ptr. Since we're currently limited to the page size
2590 * for a btree block, there's no way for a kmalloc of refsorts for a
2591 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002592 */
2593struct refsort {
2594 u64 bytenr;
2595 u32 slot;
2596};
2597
2598/*
2599 * for passing into sort()
2600 */
2601static int refsort_cmp(const void *a_void, const void *b_void)
2602{
2603 const struct refsort *a = a_void;
2604 const struct refsort *b = b_void;
2605
2606 if (a->bytenr < b->bytenr)
2607 return -1;
2608 if (a->bytenr > b->bytenr)
2609 return 1;
2610 return 0;
2611}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002612#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002613
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002614static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002615 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002616 struct extent_buffer *buf,
2617 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002618{
2619 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002620 u64 num_bytes;
2621 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002622 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002623 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002624 struct btrfs_key key;
2625 struct btrfs_file_extent_item *fi;
2626 int i;
2627 int level;
2628 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002629 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002630 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002631
2632 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002633 nritems = btrfs_header_nritems(buf);
2634 level = btrfs_header_level(buf);
2635
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002636 if (!root->ref_cows && level == 0)
2637 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002638
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002639 if (inc)
2640 process_func = btrfs_inc_extent_ref;
2641 else
2642 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002643
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002644 if (full_backref)
2645 parent = buf->start;
2646 else
2647 parent = 0;
2648
Zheng Yan31840ae2008-09-23 13:14:14 -04002649 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002650 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002651 btrfs_item_key_to_cpu(buf, &key, i);
2652 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002653 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002654 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002655 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002656 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002657 BTRFS_FILE_EXTENT_INLINE)
2658 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002659 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2660 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002661 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002662
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002663 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2664 key.offset -= btrfs_file_extent_offset(buf, fi);
2665 ret = process_func(trans, root, bytenr, num_bytes,
2666 parent, ref_root, key.objectid,
2667 key.offset);
2668 if (ret)
2669 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002670 } else {
2671 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002672 num_bytes = btrfs_level_size(root, level - 1);
2673 ret = process_func(trans, root, bytenr, num_bytes,
2674 parent, ref_root, level - 1, 0);
2675 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002676 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002677 }
2678 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002679 return 0;
2680fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002681 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002682 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002683}
2684
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002685int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2686 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002687{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002688 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2689}
Zheng Yan31840ae2008-09-23 13:14:14 -04002690
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002691int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2692 struct extent_buffer *buf, int full_backref)
2693{
2694 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002695}
2696
Chris Mason9078a3e2007-04-26 16:46:15 -04002697static int write_one_cache_group(struct btrfs_trans_handle *trans,
2698 struct btrfs_root *root,
2699 struct btrfs_path *path,
2700 struct btrfs_block_group_cache *cache)
2701{
2702 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002703 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002704 unsigned long bi;
2705 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002706
Chris Mason9078a3e2007-04-26 16:46:15 -04002707 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002708 if (ret < 0)
2709 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002710 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002711
2712 leaf = path->nodes[0];
2713 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2714 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2715 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002716 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002717fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002718 if (ret)
2719 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002720 return 0;
2721
2722}
2723
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002724static struct btrfs_block_group_cache *
2725next_block_group(struct btrfs_root *root,
2726 struct btrfs_block_group_cache *cache)
2727{
2728 struct rb_node *node;
2729 spin_lock(&root->fs_info->block_group_cache_lock);
2730 node = rb_next(&cache->cache_node);
2731 btrfs_put_block_group(cache);
2732 if (node) {
2733 cache = rb_entry(node, struct btrfs_block_group_cache,
2734 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002735 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002736 } else
2737 cache = NULL;
2738 spin_unlock(&root->fs_info->block_group_cache_lock);
2739 return cache;
2740}
2741
Josef Bacik0af3d002010-06-21 14:48:16 -04002742static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2743 struct btrfs_trans_handle *trans,
2744 struct btrfs_path *path)
2745{
2746 struct btrfs_root *root = block_group->fs_info->tree_root;
2747 struct inode *inode = NULL;
2748 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002749 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002750 int num_pages = 0;
2751 int retries = 0;
2752 int ret = 0;
2753
2754 /*
2755 * If this block group is smaller than 100 megs don't bother caching the
2756 * block group.
2757 */
2758 if (block_group->key.offset < (100 * 1024 * 1024)) {
2759 spin_lock(&block_group->lock);
2760 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2761 spin_unlock(&block_group->lock);
2762 return 0;
2763 }
2764
2765again:
2766 inode = lookup_free_space_inode(root, block_group, path);
2767 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2768 ret = PTR_ERR(inode);
2769 btrfs_release_path(root, path);
2770 goto out;
2771 }
2772
2773 if (IS_ERR(inode)) {
2774 BUG_ON(retries);
2775 retries++;
2776
2777 if (block_group->ro)
2778 goto out_free;
2779
2780 ret = create_free_space_inode(root, trans, block_group, path);
2781 if (ret)
2782 goto out_free;
2783 goto again;
2784 }
2785
2786 /*
2787 * We want to set the generation to 0, that way if anything goes wrong
2788 * from here on out we know not to trust this cache when we load up next
2789 * time.
2790 */
2791 BTRFS_I(inode)->generation = 0;
2792 ret = btrfs_update_inode(trans, root, inode);
2793 WARN_ON(ret);
2794
2795 if (i_size_read(inode) > 0) {
2796 ret = btrfs_truncate_free_space_cache(root, trans, path,
2797 inode);
2798 if (ret)
2799 goto out_put;
2800 }
2801
2802 spin_lock(&block_group->lock);
2803 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002804 /* We're not cached, don't bother trying to write stuff out */
2805 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002806 spin_unlock(&block_group->lock);
2807 goto out_put;
2808 }
2809 spin_unlock(&block_group->lock);
2810
2811 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2812 if (!num_pages)
2813 num_pages = 1;
2814
2815 /*
2816 * Just to make absolutely sure we have enough space, we're going to
2817 * preallocate 12 pages worth of space for each block group. In
2818 * practice we ought to use at most 8, but we need extra space so we can
2819 * add our header and have a terminator between the extents and the
2820 * bitmaps.
2821 */
2822 num_pages *= 16;
2823 num_pages *= PAGE_CACHE_SIZE;
2824
2825 ret = btrfs_check_data_free_space(inode, num_pages);
2826 if (ret)
2827 goto out_put;
2828
2829 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2830 num_pages, num_pages,
2831 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002832 if (!ret)
2833 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002834 btrfs_free_reserved_data_space(inode, num_pages);
2835out_put:
2836 iput(inode);
2837out_free:
2838 btrfs_release_path(root, path);
2839out:
2840 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002841 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002842 spin_unlock(&block_group->lock);
2843
2844 return ret;
2845}
2846
Chris Mason96b51792007-10-15 16:15:19 -04002847int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2848 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002849{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002850 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002851 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002852 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002853 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002854
2855 path = btrfs_alloc_path();
2856 if (!path)
2857 return -ENOMEM;
2858
Josef Bacik0af3d002010-06-21 14:48:16 -04002859again:
2860 while (1) {
2861 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2862 while (cache) {
2863 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2864 break;
2865 cache = next_block_group(root, cache);
2866 }
2867 if (!cache) {
2868 if (last == 0)
2869 break;
2870 last = 0;
2871 continue;
2872 }
2873 err = cache_save_setup(cache, trans, path);
2874 last = cache->key.objectid + cache->key.offset;
2875 btrfs_put_block_group(cache);
2876 }
2877
Chris Masond3977122009-01-05 21:25:51 -05002878 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002879 if (last == 0) {
2880 err = btrfs_run_delayed_refs(trans, root,
2881 (unsigned long)-1);
2882 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002883 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002884
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002885 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2886 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002887 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2888 btrfs_put_block_group(cache);
2889 goto again;
2890 }
2891
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002892 if (cache->dirty)
2893 break;
2894 cache = next_block_group(root, cache);
2895 }
2896 if (!cache) {
2897 if (last == 0)
2898 break;
2899 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002900 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002901 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002902
Josef Bacik0cb59c92010-07-02 12:14:14 -04002903 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2904 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002905 cache->dirty = 0;
2906 last = cache->key.objectid + cache->key.offset;
2907
2908 err = write_one_cache_group(trans, root, path, cache);
2909 BUG_ON(err);
2910 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002911 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002912
Josef Bacik0cb59c92010-07-02 12:14:14 -04002913 while (1) {
2914 /*
2915 * I don't think this is needed since we're just marking our
2916 * preallocated extent as written, but just in case it can't
2917 * hurt.
2918 */
2919 if (last == 0) {
2920 err = btrfs_run_delayed_refs(trans, root,
2921 (unsigned long)-1);
2922 BUG_ON(err);
2923 }
2924
2925 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2926 while (cache) {
2927 /*
2928 * Really this shouldn't happen, but it could if we
2929 * couldn't write the entire preallocated extent and
2930 * splitting the extent resulted in a new block.
2931 */
2932 if (cache->dirty) {
2933 btrfs_put_block_group(cache);
2934 goto again;
2935 }
2936 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2937 break;
2938 cache = next_block_group(root, cache);
2939 }
2940 if (!cache) {
2941 if (last == 0)
2942 break;
2943 last = 0;
2944 continue;
2945 }
2946
2947 btrfs_write_out_cache(root, trans, cache, path);
2948
2949 /*
2950 * If we didn't have an error then the cache state is still
2951 * NEED_WRITE, so we can set it to WRITTEN.
2952 */
2953 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2954 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2955 last = cache->key.objectid + cache->key.offset;
2956 btrfs_put_block_group(cache);
2957 }
2958
Chris Mason9078a3e2007-04-26 16:46:15 -04002959 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002960 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002961}
2962
Yan Zhengd2fb3432008-12-11 16:30:39 -05002963int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2964{
2965 struct btrfs_block_group_cache *block_group;
2966 int readonly = 0;
2967
2968 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2969 if (!block_group || block_group->ro)
2970 readonly = 1;
2971 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002972 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002973 return readonly;
2974}
2975
Chris Mason593060d2008-03-25 16:50:33 -04002976static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2977 u64 total_bytes, u64 bytes_used,
2978 struct btrfs_space_info **space_info)
2979{
2980 struct btrfs_space_info *found;
Yan, Zhengb742bb822010-05-16 10:46:24 -04002981 int i;
2982 int factor;
2983
2984 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2985 BTRFS_BLOCK_GROUP_RAID10))
2986 factor = 2;
2987 else
2988 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002989
2990 found = __find_space_info(info, flags);
2991 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002992 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002993 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002994 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002995 found->bytes_used += bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04002996 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002997 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002998 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002999 *space_info = found;
3000 return 0;
3001 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003002 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003003 if (!found)
3004 return -ENOMEM;
3005
Yan, Zhengb742bb822010-05-16 10:46:24 -04003006 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3007 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003008 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003009 spin_lock_init(&found->lock);
Yan, Zhengb742bb822010-05-16 10:46:24 -04003010 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
3011 BTRFS_BLOCK_GROUP_SYSTEM |
3012 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04003013 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003014 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003015 found->bytes_used = bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003016 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003017 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003018 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003019 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003020 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003021 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04003022 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003023 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003024 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04003025 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04003026 return 0;
3027}
3028
Chris Mason8790d502008-04-03 16:29:03 -04003029static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3030{
3031 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04003032 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04003033 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04003034 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04003035 if (extra_flags) {
3036 if (flags & BTRFS_BLOCK_GROUP_DATA)
3037 fs_info->avail_data_alloc_bits |= extra_flags;
3038 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3039 fs_info->avail_metadata_alloc_bits |= extra_flags;
3040 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3041 fs_info->avail_system_alloc_bits |= extra_flags;
3042 }
3043}
Chris Mason593060d2008-03-25 16:50:33 -04003044
Yan Zheng2b820322008-11-17 21:11:30 -05003045u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003046{
Chris Masoncd02dca2010-12-13 14:56:23 -05003047 /*
3048 * we add in the count of missing devices because we want
3049 * to make sure that any RAID levels on a degraded FS
3050 * continue to be honored.
3051 */
3052 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3053 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04003054
3055 if (num_devices == 1)
3056 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3057 if (num_devices < 4)
3058 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3059
Chris Masonec44a352008-04-28 15:29:52 -04003060 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3061 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003062 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003063 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003064 }
Chris Masonec44a352008-04-28 15:29:52 -04003065
3066 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003067 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003068 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003069 }
Chris Masonec44a352008-04-28 15:29:52 -04003070
3071 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3072 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3073 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3074 (flags & BTRFS_BLOCK_GROUP_DUP)))
3075 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3076 return flags;
3077}
3078
Yan, Zhengb742bb822010-05-16 10:46:24 -04003079static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003080{
Yan, Zhengb742bb822010-05-16 10:46:24 -04003081 if (flags & BTRFS_BLOCK_GROUP_DATA)
3082 flags |= root->fs_info->avail_data_alloc_bits &
3083 root->fs_info->data_alloc_profile;
3084 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3085 flags |= root->fs_info->avail_system_alloc_bits &
3086 root->fs_info->system_alloc_profile;
3087 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3088 flags |= root->fs_info->avail_metadata_alloc_bits &
3089 root->fs_info->metadata_alloc_profile;
3090 return btrfs_reduce_alloc_profile(root, flags);
3091}
Josef Bacik6a632092009-02-20 11:00:09 -05003092
Miao Xie6d07bce2011-01-05 10:07:31 +00003093u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04003094{
3095 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003096
Yan, Zhengb742bb822010-05-16 10:46:24 -04003097 if (data)
3098 flags = BTRFS_BLOCK_GROUP_DATA;
3099 else if (root == root->fs_info->chunk_root)
3100 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3101 else
3102 flags = BTRFS_BLOCK_GROUP_METADATA;
3103
3104 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003105}
3106
3107void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3108{
Josef Bacik6a632092009-02-20 11:00:09 -05003109 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003110 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003111}
3112
3113/*
3114 * This will check the space that the inode allocates from to make sure we have
3115 * enough space for bytes.
3116 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003117int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003118{
3119 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003120 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003121 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003122 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003123
3124 /* make sure bytes are sectorsize aligned */
3125 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3126
Josef Bacik0af3d002010-06-21 14:48:16 -04003127 if (root == root->fs_info->tree_root) {
3128 alloc_chunk = 0;
3129 committed = 1;
3130 }
3131
Josef Bacik6a632092009-02-20 11:00:09 -05003132 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003133 if (!data_sinfo)
3134 goto alloc;
3135
Josef Bacik6a632092009-02-20 11:00:09 -05003136again:
3137 /* make sure we have enough space to handle the data first */
3138 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003139 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3140 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3141 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003142
3143 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003144 struct btrfs_trans_handle *trans;
3145
Josef Bacik6a632092009-02-20 11:00:09 -05003146 /*
3147 * if we don't have enough free bytes in this space then we need
3148 * to alloc a new chunk.
3149 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003150 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003151 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003152
3153 data_sinfo->force_alloc = 1;
3154 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003155alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003156 alloc_target = btrfs_get_alloc_profile(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003157 trans = btrfs_join_transaction(root, 1);
3158 if (IS_ERR(trans))
3159 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003160
3161 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3162 bytes + 2 * 1024 * 1024,
3163 alloc_target, 0);
3164 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003165 if (ret < 0) {
3166 if (ret != -ENOSPC)
3167 return ret;
3168 else
3169 goto commit_trans;
3170 }
Chris Mason33b4d472009-09-22 14:45:50 -04003171
3172 if (!data_sinfo) {
3173 btrfs_set_inode_space_info(root, inode);
3174 data_sinfo = BTRFS_I(inode)->space_info;
3175 }
Josef Bacik6a632092009-02-20 11:00:09 -05003176 goto again;
3177 }
3178 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003179
3180 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003181commit_trans:
Sage Weildd7e0b72009-09-29 18:38:44 -04003182 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003183 committed = 1;
3184 trans = btrfs_join_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003185 if (IS_ERR(trans))
3186 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003187 ret = btrfs_commit_transaction(trans, root);
3188 if (ret)
3189 return ret;
3190 goto again;
3191 }
3192
Chris Mason933b5852010-05-26 11:31:00 -04003193#if 0 /* I hope we never need this code again, just in case */
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003194 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3195 "%llu bytes_reserved, " "%llu bytes_pinned, "
3196 "%llu bytes_readonly, %llu may use %llu total\n",
3197 (unsigned long long)bytes,
Joel Becker21380932009-04-21 12:38:29 -07003198 (unsigned long long)data_sinfo->bytes_used,
3199 (unsigned long long)data_sinfo->bytes_reserved,
3200 (unsigned long long)data_sinfo->bytes_pinned,
3201 (unsigned long long)data_sinfo->bytes_readonly,
3202 (unsigned long long)data_sinfo->bytes_may_use,
3203 (unsigned long long)data_sinfo->total_bytes);
Chris Mason933b5852010-05-26 11:31:00 -04003204#endif
Josef Bacik6a632092009-02-20 11:00:09 -05003205 return -ENOSPC;
3206 }
3207 data_sinfo->bytes_may_use += bytes;
3208 BTRFS_I(inode)->reserved_bytes += bytes;
3209 spin_unlock(&data_sinfo->lock);
3210
Josef Bacik9ed74f22009-09-11 16:12:44 -04003211 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003212}
3213
3214/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003215 * called when we are clearing an delalloc extent from the
3216 * inode's io_tree or there was an error for whatever reason
3217 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003218 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003219void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003220{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003221 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003222 struct btrfs_space_info *data_sinfo;
3223
3224 /* make sure bytes are sectorsize aligned */
3225 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3226
3227 data_sinfo = BTRFS_I(inode)->space_info;
3228 spin_lock(&data_sinfo->lock);
3229 data_sinfo->bytes_may_use -= bytes;
3230 BTRFS_I(inode)->reserved_bytes -= bytes;
3231 spin_unlock(&data_sinfo->lock);
3232}
3233
Josef Bacik97e728d2009-04-21 17:40:57 -04003234static void force_metadata_allocation(struct btrfs_fs_info *info)
3235{
3236 struct list_head *head = &info->space_info;
3237 struct btrfs_space_info *found;
3238
3239 rcu_read_lock();
3240 list_for_each_entry_rcu(found, head, list) {
3241 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3242 found->force_alloc = 1;
3243 }
3244 rcu_read_unlock();
3245}
3246
Chris Masone5bc2452010-10-26 13:37:56 -04003247static int should_alloc_chunk(struct btrfs_root *root,
3248 struct btrfs_space_info *sinfo, u64 alloc_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003249{
3250 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Masone5bc2452010-10-26 13:37:56 -04003251 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003252
3253 if (sinfo->bytes_used + sinfo->bytes_reserved +
3254 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3255 return 0;
3256
3257 if (sinfo->bytes_used + sinfo->bytes_reserved +
3258 alloc_bytes < div_factor(num_bytes, 8))
3259 return 0;
3260
Chris Masone5bc2452010-10-26 13:37:56 -04003261 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3262 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3263
3264 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003265 return 0;
3266
Yan, Zheng424499d2010-05-16 10:46:25 -04003267 return 1;
3268}
3269
Chris Mason6324fbf2008-03-24 15:01:59 -04003270static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3271 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003272 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003273{
3274 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003275 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Yan Zhengc146afa2008-11-12 14:34:12 -05003276 int ret = 0;
3277
Josef Bacik97e728d2009-04-21 17:40:57 -04003278 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003279
Yan Zheng2b820322008-11-17 21:11:30 -05003280 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003281
Chris Mason6324fbf2008-03-24 15:01:59 -04003282 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003283 if (!space_info) {
3284 ret = update_space_info(extent_root->fs_info, flags,
3285 0, 0, &space_info);
3286 BUG_ON(ret);
3287 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003288 BUG_ON(!space_info);
3289
Josef Bacik25179202008-10-29 14:49:05 -04003290 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003291 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003292 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003293 if (space_info->full) {
3294 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003295 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003296 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003297
Chris Masone5bc2452010-10-26 13:37:56 -04003298 if (!force && !should_alloc_chunk(extent_root, space_info,
3299 alloc_bytes)) {
Josef Bacik25179202008-10-29 14:49:05 -04003300 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003301 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003302 }
Josef Bacik25179202008-10-29 14:49:05 -04003303 spin_unlock(&space_info->lock);
3304
Josef Bacik97e728d2009-04-21 17:40:57 -04003305 /*
Josef Bacik67377732010-09-16 16:19:09 -04003306 * If we have mixed data/metadata chunks we want to make sure we keep
3307 * allocating mixed chunks instead of individual chunks.
3308 */
3309 if (btrfs_mixed_space_info(space_info))
3310 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3311
3312 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003313 * if we're doing a data chunk, go ahead and make sure that
3314 * we keep a reasonable number of metadata chunks allocated in the
3315 * FS as well.
3316 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003317 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003318 fs_info->data_chunk_allocations++;
3319 if (!(fs_info->data_chunk_allocations %
3320 fs_info->metadata_ratio))
3321 force_metadata_allocation(fs_info);
3322 }
3323
Yan Zheng2b820322008-11-17 21:11:30 -05003324 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003325 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003326 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003327 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003328 else
3329 ret = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003330 space_info->force_alloc = 0;
3331 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003332out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003333 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003334 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003335}
3336
Yan, Zheng5da9d012010-05-16 10:46:25 -04003337/*
3338 * shrink metadata reservation for delalloc
3339 */
3340static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003341 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003342{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003343 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003344 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003345 u64 reserved;
3346 u64 max_reclaim;
3347 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003348 long time_left;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003349 int pause = 1;
Chris Masonbf9022e2010-10-26 13:40:45 -04003350 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003351 int loops = 0;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003352
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003353 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003354 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003355
3356 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003357 reserved = space_info->bytes_reserved;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003358
3359 if (reserved == 0)
3360 return 0;
3361
3362 max_reclaim = min(reserved, to_reclaim);
3363
Josef Bacikb1953bc2011-01-21 21:10:01 +00003364 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003365 /* have the flusher threads jump in and do some IO */
3366 smp_mb();
3367 nr_pages = min_t(unsigned long, nr_pages,
3368 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3369 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003370
Josef Bacik0019f102010-10-15 15:18:40 -04003371 spin_lock(&space_info->lock);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003372 if (reserved > space_info->bytes_reserved) {
3373 loops = 0;
Josef Bacik0019f102010-10-15 15:18:40 -04003374 reclaimed += reserved - space_info->bytes_reserved;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003375 } else {
3376 loops++;
3377 }
Josef Bacik0019f102010-10-15 15:18:40 -04003378 reserved = space_info->bytes_reserved;
3379 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003380
3381 if (reserved == 0 || reclaimed >= max_reclaim)
3382 break;
3383
3384 if (trans && trans->transaction->blocked)
3385 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003386
3387 __set_current_state(TASK_INTERRUPTIBLE);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003388 time_left = schedule_timeout(pause);
3389
3390 /* We were interrupted, exit */
3391 if (time_left)
3392 break;
3393
Chris Masonbf9022e2010-10-26 13:40:45 -04003394 pause <<= 1;
3395 if (pause > HZ / 10)
3396 pause = HZ / 10;
3397
Yan, Zheng5da9d012010-05-16 10:46:25 -04003398 }
3399 return reclaimed >= to_reclaim;
3400}
3401
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003402/*
3403 * Retries tells us how many times we've called reserve_metadata_bytes. The
3404 * idea is if this is the first call (retries == 0) then we will add to our
3405 * reserved count if we can't make the allocation in order to hold our place
3406 * while we go and try and free up space. That way for retries > 1 we don't try
3407 * and add space, we just check to see if the amount of unused space is >= the
3408 * total space, meaning that our reservation is valid.
3409 *
3410 * However if we don't intend to retry this reservation, pass -1 as retries so
3411 * that it short circuits this logic.
3412 */
3413static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3414 struct btrfs_root *root,
3415 struct btrfs_block_rsv *block_rsv,
3416 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003417{
3418 struct btrfs_space_info *space_info = block_rsv->space_info;
3419 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003420 u64 num_bytes = orig_bytes;
3421 int retries = 0;
3422 int ret = 0;
3423 bool reserved = false;
Josef Bacik38227932010-10-26 12:52:53 -04003424 bool committed = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003425
3426again:
3427 ret = -ENOSPC;
3428 if (reserved)
3429 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003430
3431 spin_lock(&space_info->lock);
3432 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003433 space_info->bytes_pinned + space_info->bytes_readonly +
3434 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003435
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003436 /*
3437 * The idea here is that we've not already over-reserved the block group
3438 * then we can go ahead and save our reservation first and then start
3439 * flushing if we need to. Otherwise if we've already overcommitted
3440 * lets start flushing stuff first and then come back and try to make
3441 * our reservation.
3442 */
3443 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003444 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003445 if (unused >= num_bytes) {
3446 if (!reserved)
3447 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003448 ret = 0;
3449 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003450 /*
3451 * Ok set num_bytes to orig_bytes since we aren't
3452 * overocmmitted, this way we only try and reclaim what
3453 * we need.
3454 */
3455 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003456 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003457 } else {
3458 /*
3459 * Ok we're over committed, set num_bytes to the overcommitted
3460 * amount plus the amount of bytes that we need for this
3461 * reservation.
3462 */
3463 num_bytes = unused - space_info->total_bytes +
3464 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003465 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003466
3467 /*
3468 * Couldn't make our reservation, save our place so while we're trying
3469 * to reclaim space we can actually use it instead of somebody else
3470 * stealing it from us.
3471 */
3472 if (ret && !reserved) {
3473 space_info->bytes_reserved += orig_bytes;
3474 reserved = true;
3475 }
3476
Yan, Zhengf0486c62010-05-16 10:46:25 -04003477 spin_unlock(&space_info->lock);
3478
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003479 if (!ret)
3480 return 0;
3481
3482 if (!flush)
3483 goto out;
3484
3485 /*
3486 * We do synchronous shrinking since we don't actually unreserve
3487 * metadata until after the IO is completed.
3488 */
3489 ret = shrink_delalloc(trans, root, num_bytes, 1);
3490 if (ret > 0)
3491 return 0;
3492 else if (ret < 0)
3493 goto out;
3494
3495 /*
3496 * So if we were overcommitted it's possible that somebody else flushed
3497 * out enough space and we simply didn't have enough space to reclaim,
3498 * so go back around and try again.
3499 */
3500 if (retries < 2) {
3501 retries++;
3502 goto again;
3503 }
3504
3505 spin_lock(&space_info->lock);
3506 /*
3507 * Not enough space to be reclaimed, don't bother committing the
3508 * transaction.
3509 */
3510 if (space_info->bytes_pinned < orig_bytes)
3511 ret = -ENOSPC;
3512 spin_unlock(&space_info->lock);
3513 if (ret)
3514 goto out;
3515
3516 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003517 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003518 goto out;
3519
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003520 ret = -ENOSPC;
3521 trans = btrfs_join_transaction(root, 1);
3522 if (IS_ERR(trans))
3523 goto out;
3524 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003525 if (!ret) {
3526 trans = NULL;
3527 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003528 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003529 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003530
3531out:
3532 if (reserved) {
3533 spin_lock(&space_info->lock);
3534 space_info->bytes_reserved -= orig_bytes;
3535 spin_unlock(&space_info->lock);
3536 }
3537
Yan, Zhengf0486c62010-05-16 10:46:25 -04003538 return ret;
3539}
3540
3541static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3542 struct btrfs_root *root)
3543{
3544 struct btrfs_block_rsv *block_rsv;
3545 if (root->ref_cows)
3546 block_rsv = trans->block_rsv;
3547 else
3548 block_rsv = root->block_rsv;
3549
3550 if (!block_rsv)
3551 block_rsv = &root->fs_info->empty_block_rsv;
3552
3553 return block_rsv;
3554}
3555
3556static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3557 u64 num_bytes)
3558{
3559 int ret = -ENOSPC;
3560 spin_lock(&block_rsv->lock);
3561 if (block_rsv->reserved >= num_bytes) {
3562 block_rsv->reserved -= num_bytes;
3563 if (block_rsv->reserved < block_rsv->size)
3564 block_rsv->full = 0;
3565 ret = 0;
3566 }
3567 spin_unlock(&block_rsv->lock);
3568 return ret;
3569}
3570
3571static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3572 u64 num_bytes, int update_size)
3573{
3574 spin_lock(&block_rsv->lock);
3575 block_rsv->reserved += num_bytes;
3576 if (update_size)
3577 block_rsv->size += num_bytes;
3578 else if (block_rsv->reserved >= block_rsv->size)
3579 block_rsv->full = 1;
3580 spin_unlock(&block_rsv->lock);
3581}
3582
3583void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3584 struct btrfs_block_rsv *dest, u64 num_bytes)
3585{
3586 struct btrfs_space_info *space_info = block_rsv->space_info;
3587
3588 spin_lock(&block_rsv->lock);
3589 if (num_bytes == (u64)-1)
3590 num_bytes = block_rsv->size;
3591 block_rsv->size -= num_bytes;
3592 if (block_rsv->reserved >= block_rsv->size) {
3593 num_bytes = block_rsv->reserved - block_rsv->size;
3594 block_rsv->reserved = block_rsv->size;
3595 block_rsv->full = 1;
3596 } else {
3597 num_bytes = 0;
3598 }
3599 spin_unlock(&block_rsv->lock);
3600
3601 if (num_bytes > 0) {
3602 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003603 spin_lock(&dest->lock);
3604 if (!dest->full) {
3605 u64 bytes_to_add;
3606
3607 bytes_to_add = dest->size - dest->reserved;
3608 bytes_to_add = min(num_bytes, bytes_to_add);
3609 dest->reserved += bytes_to_add;
3610 if (dest->reserved >= dest->size)
3611 dest->full = 1;
3612 num_bytes -= bytes_to_add;
3613 }
3614 spin_unlock(&dest->lock);
3615 }
3616 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003617 spin_lock(&space_info->lock);
3618 space_info->bytes_reserved -= num_bytes;
3619 spin_unlock(&space_info->lock);
3620 }
3621 }
3622}
3623
3624static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3625 struct btrfs_block_rsv *dst, u64 num_bytes)
3626{
3627 int ret;
3628
3629 ret = block_rsv_use_bytes(src, num_bytes);
3630 if (ret)
3631 return ret;
3632
3633 block_rsv_add_bytes(dst, num_bytes, 1);
3634 return 0;
3635}
3636
3637void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3638{
3639 memset(rsv, 0, sizeof(*rsv));
3640 spin_lock_init(&rsv->lock);
3641 atomic_set(&rsv->usage, 1);
3642 rsv->priority = 6;
3643 INIT_LIST_HEAD(&rsv->list);
3644}
3645
3646struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3647{
3648 struct btrfs_block_rsv *block_rsv;
3649 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003650
3651 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3652 if (!block_rsv)
3653 return NULL;
3654
3655 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003656 block_rsv->space_info = __find_space_info(fs_info,
3657 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003658 return block_rsv;
3659}
3660
3661void btrfs_free_block_rsv(struct btrfs_root *root,
3662 struct btrfs_block_rsv *rsv)
3663{
3664 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3665 btrfs_block_rsv_release(root, rsv, (u64)-1);
3666 if (!rsv->durable)
3667 kfree(rsv);
3668 }
3669}
3670
3671/*
3672 * make the block_rsv struct be able to capture freed space.
3673 * the captured space will re-add to the the block_rsv struct
3674 * after transaction commit
3675 */
3676void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3677 struct btrfs_block_rsv *block_rsv)
3678{
3679 block_rsv->durable = 1;
3680 mutex_lock(&fs_info->durable_block_rsv_mutex);
3681 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3682 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3683}
3684
3685int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3686 struct btrfs_root *root,
3687 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003688 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003689{
3690 int ret;
3691
3692 if (num_bytes == 0)
3693 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003694
3695 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003696 if (!ret) {
3697 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3698 return 0;
3699 }
3700
Yan, Zhengf0486c62010-05-16 10:46:25 -04003701 return ret;
3702}
3703
3704int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3705 struct btrfs_root *root,
3706 struct btrfs_block_rsv *block_rsv,
3707 u64 min_reserved, int min_factor)
3708{
3709 u64 num_bytes = 0;
3710 int commit_trans = 0;
3711 int ret = -ENOSPC;
3712
3713 if (!block_rsv)
3714 return 0;
3715
3716 spin_lock(&block_rsv->lock);
3717 if (min_factor > 0)
3718 num_bytes = div_factor(block_rsv->size, min_factor);
3719 if (min_reserved > num_bytes)
3720 num_bytes = min_reserved;
3721
3722 if (block_rsv->reserved >= num_bytes) {
3723 ret = 0;
3724 } else {
3725 num_bytes -= block_rsv->reserved;
3726 if (block_rsv->durable &&
3727 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3728 commit_trans = 1;
3729 }
3730 spin_unlock(&block_rsv->lock);
3731 if (!ret)
3732 return 0;
3733
3734 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003735 ret = reserve_metadata_bytes(trans, root, block_rsv,
3736 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003737 if (!ret) {
3738 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3739 return 0;
3740 }
3741 }
3742
3743 if (commit_trans) {
3744 if (trans)
3745 return -EAGAIN;
3746
3747 trans = btrfs_join_transaction(root, 1);
3748 BUG_ON(IS_ERR(trans));
3749 ret = btrfs_commit_transaction(trans, root);
3750 return 0;
3751 }
3752
Yan, Zhengf0486c62010-05-16 10:46:25 -04003753 return -ENOSPC;
3754}
3755
3756int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3757 struct btrfs_block_rsv *dst_rsv,
3758 u64 num_bytes)
3759{
3760 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3761}
3762
3763void btrfs_block_rsv_release(struct btrfs_root *root,
3764 struct btrfs_block_rsv *block_rsv,
3765 u64 num_bytes)
3766{
3767 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3768 if (global_rsv->full || global_rsv == block_rsv ||
3769 block_rsv->space_info != global_rsv->space_info)
3770 global_rsv = NULL;
3771 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3772}
3773
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003774/*
3775 * helper to calculate size of global block reservation.
3776 * the desired value is sum of space used by extent tree,
3777 * checksum tree and root tree
3778 */
3779static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3780{
3781 struct btrfs_space_info *sinfo;
3782 u64 num_bytes;
3783 u64 meta_used;
3784 u64 data_used;
3785 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3786#if 0
3787 /*
3788 * per tree used space accounting can be inaccuracy, so we
3789 * can't rely on it.
3790 */
3791 spin_lock(&fs_info->extent_root->accounting_lock);
3792 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3793 spin_unlock(&fs_info->extent_root->accounting_lock);
3794
3795 spin_lock(&fs_info->csum_root->accounting_lock);
3796 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3797 spin_unlock(&fs_info->csum_root->accounting_lock);
3798
3799 spin_lock(&fs_info->tree_root->accounting_lock);
3800 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3801 spin_unlock(&fs_info->tree_root->accounting_lock);
3802#endif
3803 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3804 spin_lock(&sinfo->lock);
3805 data_used = sinfo->bytes_used;
3806 spin_unlock(&sinfo->lock);
3807
3808 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3809 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003810 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3811 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003812 meta_used = sinfo->bytes_used;
3813 spin_unlock(&sinfo->lock);
3814
3815 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3816 csum_size * 2;
3817 num_bytes += div64_u64(data_used + meta_used, 50);
3818
3819 if (num_bytes * 3 > meta_used)
3820 num_bytes = div64_u64(meta_used, 3);
3821
3822 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3823}
3824
3825static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3826{
3827 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3828 struct btrfs_space_info *sinfo = block_rsv->space_info;
3829 u64 num_bytes;
3830
3831 num_bytes = calc_global_metadata_size(fs_info);
3832
3833 spin_lock(&block_rsv->lock);
3834 spin_lock(&sinfo->lock);
3835
3836 block_rsv->size = num_bytes;
3837
3838 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003839 sinfo->bytes_reserved + sinfo->bytes_readonly +
3840 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003841
3842 if (sinfo->total_bytes > num_bytes) {
3843 num_bytes = sinfo->total_bytes - num_bytes;
3844 block_rsv->reserved += num_bytes;
3845 sinfo->bytes_reserved += num_bytes;
3846 }
3847
3848 if (block_rsv->reserved >= block_rsv->size) {
3849 num_bytes = block_rsv->reserved - block_rsv->size;
3850 sinfo->bytes_reserved -= num_bytes;
3851 block_rsv->reserved = block_rsv->size;
3852 block_rsv->full = 1;
3853 }
3854#if 0
3855 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3856 block_rsv->size, block_rsv->reserved);
3857#endif
3858 spin_unlock(&sinfo->lock);
3859 spin_unlock(&block_rsv->lock);
3860}
3861
Yan, Zhengf0486c62010-05-16 10:46:25 -04003862static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3863{
3864 struct btrfs_space_info *space_info;
3865
3866 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3867 fs_info->chunk_block_rsv.space_info = space_info;
3868 fs_info->chunk_block_rsv.priority = 10;
3869
3870 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003871 fs_info->global_block_rsv.space_info = space_info;
3872 fs_info->global_block_rsv.priority = 10;
3873 fs_info->global_block_rsv.refill_used = 1;
3874 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003875 fs_info->trans_block_rsv.space_info = space_info;
3876 fs_info->empty_block_rsv.space_info = space_info;
3877 fs_info->empty_block_rsv.priority = 10;
3878
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003879 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3880 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3881 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3882 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003883 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003884
3885 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3886
3887 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3888
3889 update_global_block_rsv(fs_info);
3890}
3891
3892static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3893{
3894 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3895 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3896 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3897 WARN_ON(fs_info->trans_block_rsv.size > 0);
3898 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3899 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3900 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003901}
3902
Yan, Zhenga22285a2010-05-16 10:48:46 -04003903static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3904{
3905 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3906 3 * num_items;
3907}
3908
3909int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3910 struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003911 int num_items)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003912{
3913 u64 num_bytes;
3914 int ret;
3915
3916 if (num_items == 0 || root->fs_info->chunk_root == root)
3917 return 0;
3918
3919 num_bytes = calc_trans_metadata_size(root, num_items);
3920 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003921 num_bytes);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003922 if (!ret) {
3923 trans->bytes_reserved += num_bytes;
3924 trans->block_rsv = &root->fs_info->trans_block_rsv;
3925 }
3926 return ret;
3927}
3928
3929void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3930 struct btrfs_root *root)
3931{
3932 if (!trans->bytes_reserved)
3933 return;
3934
3935 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3936 btrfs_block_rsv_release(root, trans->block_rsv,
3937 trans->bytes_reserved);
3938 trans->bytes_reserved = 0;
3939}
3940
Yan, Zhengd68fc572010-05-16 10:49:58 -04003941int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3942 struct inode *inode)
3943{
3944 struct btrfs_root *root = BTRFS_I(inode)->root;
3945 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3946 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3947
3948 /*
3949 * one for deleting orphan item, one for updating inode and
3950 * two for calling btrfs_truncate_inode_items.
3951 *
3952 * btrfs_truncate_inode_items is a delete operation, it frees
3953 * more space than it uses in most cases. So two units of
3954 * metadata space should be enough for calling it many times.
3955 * If all of the metadata space is used, we can commit
3956 * transaction and use space it freed.
3957 */
3958 u64 num_bytes = calc_trans_metadata_size(root, 4);
3959 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3960}
3961
3962void btrfs_orphan_release_metadata(struct inode *inode)
3963{
3964 struct btrfs_root *root = BTRFS_I(inode)->root;
3965 u64 num_bytes = calc_trans_metadata_size(root, 4);
3966 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3967}
3968
Yan, Zhenga22285a2010-05-16 10:48:46 -04003969int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3970 struct btrfs_pending_snapshot *pending)
3971{
3972 struct btrfs_root *root = pending->root;
3973 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3974 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3975 /*
3976 * two for root back/forward refs, two for directory entries
3977 * and one for root of the snapshot.
3978 */
3979 u64 num_bytes = calc_trans_metadata_size(root, 5);
3980 dst_rsv->space_info = src_rsv->space_info;
3981 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3982}
3983
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003984static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3985{
3986 return num_bytes >>= 3;
3987}
3988
3989int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3990{
3991 struct btrfs_root *root = BTRFS_I(inode)->root;
3992 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3993 u64 to_reserve;
3994 int nr_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003995 int ret;
3996
3997 if (btrfs_transaction_in_commit(root->fs_info))
3998 schedule_timeout(1);
3999
4000 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004001
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004002 spin_lock(&BTRFS_I(inode)->accounting_lock);
4003 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
4004 if (nr_extents > BTRFS_I(inode)->reserved_extents) {
4005 nr_extents -= BTRFS_I(inode)->reserved_extents;
4006 to_reserve = calc_trans_metadata_size(root, nr_extents);
4007 } else {
4008 nr_extents = 0;
4009 to_reserve = 0;
4010 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004011 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004012
4013 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004014 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
4015 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004016 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004017
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004018 spin_lock(&BTRFS_I(inode)->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004019 BTRFS_I(inode)->reserved_extents += nr_extents;
4020 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
4021 spin_unlock(&BTRFS_I(inode)->accounting_lock);
4022
4023 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4024
4025 if (block_rsv->size > 512 * 1024 * 1024)
Josef Bacik0019f102010-10-15 15:18:40 -04004026 shrink_delalloc(NULL, root, to_reserve, 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004027
4028 return 0;
4029}
4030
4031void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4032{
4033 struct btrfs_root *root = BTRFS_I(inode)->root;
4034 u64 to_free;
4035 int nr_extents;
4036
4037 num_bytes = ALIGN(num_bytes, root->sectorsize);
4038 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
4039
4040 spin_lock(&BTRFS_I(inode)->accounting_lock);
4041 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4042 if (nr_extents < BTRFS_I(inode)->reserved_extents) {
4043 nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents;
4044 BTRFS_I(inode)->reserved_extents -= nr_extents;
4045 } else {
4046 nr_extents = 0;
4047 }
4048 spin_unlock(&BTRFS_I(inode)->accounting_lock);
4049
4050 to_free = calc_csum_metadata_size(inode, num_bytes);
4051 if (nr_extents > 0)
4052 to_free += calc_trans_metadata_size(root, nr_extents);
4053
4054 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4055 to_free);
4056}
4057
4058int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4059{
4060 int ret;
4061
4062 ret = btrfs_check_data_free_space(inode, num_bytes);
4063 if (ret)
4064 return ret;
4065
4066 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4067 if (ret) {
4068 btrfs_free_reserved_data_space(inode, num_bytes);
4069 return ret;
4070 }
4071
4072 return 0;
4073}
4074
4075void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4076{
4077 btrfs_delalloc_release_metadata(inode, num_bytes);
4078 btrfs_free_reserved_data_space(inode, num_bytes);
4079}
4080
Chris Mason9078a3e2007-04-26 16:46:15 -04004081static int update_block_group(struct btrfs_trans_handle *trans,
4082 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004083 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004084{
Josef Bacik0af3d002010-06-21 14:48:16 -04004085 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004086 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004087 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004088 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004089 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004090 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004091
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004092 /* block accounting for super block */
4093 spin_lock(&info->delalloc_lock);
4094 old_val = btrfs_super_bytes_used(&info->super_copy);
4095 if (alloc)
4096 old_val += num_bytes;
4097 else
4098 old_val -= num_bytes;
4099 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004100 spin_unlock(&info->delalloc_lock);
4101
Chris Masond3977122009-01-05 21:25:51 -05004102 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004103 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004104 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004105 return -1;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004106 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4107 BTRFS_BLOCK_GROUP_RAID1 |
4108 BTRFS_BLOCK_GROUP_RAID10))
4109 factor = 2;
4110 else
4111 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004112 /*
4113 * If this block group has free space cache written out, we
4114 * need to make sure to load it if we are removing space. This
4115 * is because we need the unpinning stage to actually add the
4116 * space back to the block group, otherwise we will leak space.
4117 */
4118 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004119 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004120
Chris Masondb945352007-10-15 16:15:53 -04004121 byte_in_group = bytenr - cache->key.objectid;
4122 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004123
Josef Bacik25179202008-10-29 14:49:05 -04004124 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004125 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004126
4127 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4128 cache->disk_cache_state < BTRFS_DC_CLEAR)
4129 cache->disk_cache_state = BTRFS_DC_CLEAR;
4130
Josef Bacik0f9dd462008-09-23 13:14:11 -04004131 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004132 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004133 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004134 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004135 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004136 btrfs_set_block_group_used(&cache->item, old_val);
4137 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004138 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004139 cache->space_info->bytes_used += num_bytes;
4140 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004141 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004142 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004143 } else {
Chris Masondb945352007-10-15 16:15:53 -04004144 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004145 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004146 cache->pinned += num_bytes;
4147 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004148 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004149 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004150 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004151 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004152
Yan, Zhengf0486c62010-05-16 10:46:25 -04004153 set_extent_dirty(info->pinned_extents,
4154 bytenr, bytenr + num_bytes - 1,
4155 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004156 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004157 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004158 total -= num_bytes;
4159 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004160 }
4161 return 0;
4162}
Chris Mason6324fbf2008-03-24 15:01:59 -04004163
Chris Masona061fc82008-05-07 11:43:44 -04004164static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4165{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004166 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004167 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004168
4169 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4170 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004171 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004172
Yan Zhengd2fb3432008-12-11 16:30:39 -05004173 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004174 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004175
4176 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004177}
4178
Yan, Zhengf0486c62010-05-16 10:46:25 -04004179static int pin_down_extent(struct btrfs_root *root,
4180 struct btrfs_block_group_cache *cache,
4181 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004182{
Yan Zheng11833d62009-09-11 16:11:19 -04004183 spin_lock(&cache->space_info->lock);
4184 spin_lock(&cache->lock);
4185 cache->pinned += num_bytes;
4186 cache->space_info->bytes_pinned += num_bytes;
4187 if (reserved) {
4188 cache->reserved -= num_bytes;
4189 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004190 }
Yan Zheng11833d62009-09-11 16:11:19 -04004191 spin_unlock(&cache->lock);
4192 spin_unlock(&cache->space_info->lock);
4193
Yan, Zhengf0486c62010-05-16 10:46:25 -04004194 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4195 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004196 return 0;
4197}
Chris Mason9078a3e2007-04-26 16:46:15 -04004198
Yan, Zhengf0486c62010-05-16 10:46:25 -04004199/*
4200 * this function must be called within transaction
4201 */
4202int btrfs_pin_extent(struct btrfs_root *root,
4203 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004204{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004205 struct btrfs_block_group_cache *cache;
4206
4207 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4208 BUG_ON(!cache);
4209
4210 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4211
4212 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004213 return 0;
4214}
Zheng Yane8569812008-09-26 10:05:48 -04004215
Yan, Zhengf0486c62010-05-16 10:46:25 -04004216/*
4217 * update size of reserved extents. this function may return -EAGAIN
4218 * if 'reserve' is true or 'sinfo' is false.
4219 */
4220static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
4221 u64 num_bytes, int reserve, int sinfo)
4222{
4223 int ret = 0;
4224 if (sinfo) {
4225 struct btrfs_space_info *space_info = cache->space_info;
4226 spin_lock(&space_info->lock);
4227 spin_lock(&cache->lock);
4228 if (reserve) {
4229 if (cache->ro) {
4230 ret = -EAGAIN;
4231 } else {
4232 cache->reserved += num_bytes;
4233 space_info->bytes_reserved += num_bytes;
4234 }
4235 } else {
4236 if (cache->ro)
4237 space_info->bytes_readonly += num_bytes;
4238 cache->reserved -= num_bytes;
4239 space_info->bytes_reserved -= num_bytes;
4240 }
4241 spin_unlock(&cache->lock);
4242 spin_unlock(&space_info->lock);
4243 } else {
4244 spin_lock(&cache->lock);
4245 if (cache->ro) {
4246 ret = -EAGAIN;
4247 } else {
4248 if (reserve)
4249 cache->reserved += num_bytes;
4250 else
4251 cache->reserved -= num_bytes;
4252 }
4253 spin_unlock(&cache->lock);
4254 }
4255 return ret;
4256}
4257
Yan Zheng11833d62009-09-11 16:11:19 -04004258int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4259 struct btrfs_root *root)
4260{
4261 struct btrfs_fs_info *fs_info = root->fs_info;
4262 struct btrfs_caching_control *next;
4263 struct btrfs_caching_control *caching_ctl;
4264 struct btrfs_block_group_cache *cache;
4265
4266 down_write(&fs_info->extent_commit_sem);
4267
4268 list_for_each_entry_safe(caching_ctl, next,
4269 &fs_info->caching_block_groups, list) {
4270 cache = caching_ctl->block_group;
4271 if (block_group_cache_done(cache)) {
4272 cache->last_byte_to_unpin = (u64)-1;
4273 list_del_init(&caching_ctl->list);
4274 put_caching_control(caching_ctl);
4275 } else {
4276 cache->last_byte_to_unpin = caching_ctl->progress;
4277 }
4278 }
4279
4280 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4281 fs_info->pinned_extents = &fs_info->freed_extents[1];
4282 else
4283 fs_info->pinned_extents = &fs_info->freed_extents[0];
4284
4285 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004286
4287 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004288 return 0;
4289}
4290
4291static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4292{
4293 struct btrfs_fs_info *fs_info = root->fs_info;
4294 struct btrfs_block_group_cache *cache = NULL;
4295 u64 len;
4296
4297 while (start <= end) {
4298 if (!cache ||
4299 start >= cache->key.objectid + cache->key.offset) {
4300 if (cache)
4301 btrfs_put_block_group(cache);
4302 cache = btrfs_lookup_block_group(fs_info, start);
4303 BUG_ON(!cache);
4304 }
4305
4306 len = cache->key.objectid + cache->key.offset - start;
4307 len = min(len, end + 1 - start);
4308
4309 if (start < cache->last_byte_to_unpin) {
4310 len = min(len, cache->last_byte_to_unpin - start);
4311 btrfs_add_free_space(cache, start, len);
4312 }
Josef Bacik25179202008-10-29 14:49:05 -04004313
Yan, Zhengf0486c62010-05-16 10:46:25 -04004314 start += len;
4315
Josef Bacik25179202008-10-29 14:49:05 -04004316 spin_lock(&cache->space_info->lock);
4317 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004318 cache->pinned -= len;
4319 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004320 if (cache->ro) {
4321 cache->space_info->bytes_readonly += len;
4322 } else if (cache->reserved_pinned > 0) {
4323 len = min(len, cache->reserved_pinned);
4324 cache->reserved_pinned -= len;
4325 cache->space_info->bytes_reserved += len;
4326 }
Josef Bacik25179202008-10-29 14:49:05 -04004327 spin_unlock(&cache->lock);
4328 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004329 }
4330
4331 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004332 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004333 return 0;
4334}
4335
4336int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004337 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004338{
Yan Zheng11833d62009-09-11 16:11:19 -04004339 struct btrfs_fs_info *fs_info = root->fs_info;
4340 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004341 struct btrfs_block_rsv *block_rsv;
4342 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004343 u64 start;
4344 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004345 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004346 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004347
Yan Zheng11833d62009-09-11 16:11:19 -04004348 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4349 unpin = &fs_info->freed_extents[1];
4350 else
4351 unpin = &fs_info->freed_extents[0];
4352
Chris Masond3977122009-01-05 21:25:51 -05004353 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004354 ret = find_first_extent_bit(unpin, 0, &start, &end,
4355 EXTENT_DIRTY);
4356 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004357 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004358
4359 ret = btrfs_discard_extent(root, start, end + 1 - start);
4360
Chris Mason1a5bc162007-10-15 16:15:26 -04004361 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004362 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004363 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004364 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004365
Yan, Zhengf0486c62010-05-16 10:46:25 -04004366 mutex_lock(&fs_info->durable_block_rsv_mutex);
4367 list_for_each_entry_safe(block_rsv, next_rsv,
4368 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004369
Yan, Zhengf0486c62010-05-16 10:46:25 -04004370 idx = trans->transid & 0x1;
4371 if (block_rsv->freed[idx] > 0) {
4372 block_rsv_add_bytes(block_rsv,
4373 block_rsv->freed[idx], 0);
4374 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004375 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004376 if (atomic_read(&block_rsv->usage) == 0) {
4377 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004378
Yan, Zhengf0486c62010-05-16 10:46:25 -04004379 if (block_rsv->freed[0] == 0 &&
4380 block_rsv->freed[1] == 0) {
4381 list_del_init(&block_rsv->list);
4382 kfree(block_rsv);
4383 }
4384 } else {
4385 btrfs_block_rsv_release(root, block_rsv, 0);
4386 }
4387 }
4388 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4389
Chris Masone20d96d2007-03-22 12:13:20 -04004390 return 0;
4391}
4392
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004393static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4394 struct btrfs_root *root,
4395 u64 bytenr, u64 num_bytes, u64 parent,
4396 u64 root_objectid, u64 owner_objectid,
4397 u64 owner_offset, int refs_to_drop,
4398 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004399{
Chris Masone2fa7222007-03-12 16:22:34 -04004400 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004401 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004402 struct btrfs_fs_info *info = root->fs_info;
4403 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004404 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004405 struct btrfs_extent_item *ei;
4406 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004407 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004408 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004409 int extent_slot = 0;
4410 int found_extent = 0;
4411 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004412 u32 item_size;
4413 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004414
Chris Mason5caf2a02007-04-02 11:20:42 -04004415 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004416 if (!path)
4417 return -ENOMEM;
4418
Chris Mason3c12ac72008-04-21 12:01:38 -04004419 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004420 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004421
4422 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4423 BUG_ON(!is_data && refs_to_drop != 1);
4424
4425 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4426 bytenr, num_bytes, parent,
4427 root_objectid, owner_objectid,
4428 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004429 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004430 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004431 while (extent_slot >= 0) {
4432 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004433 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004434 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004435 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004436 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4437 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004438 found_extent = 1;
4439 break;
4440 }
4441 if (path->slots[0] - extent_slot > 5)
4442 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004443 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004444 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004445#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4446 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4447 if (found_extent && item_size < sizeof(*ei))
4448 found_extent = 0;
4449#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004450 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004451 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004452 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004453 NULL, refs_to_drop,
4454 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004455 BUG_ON(ret);
4456 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04004457 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004458
4459 key.objectid = bytenr;
4460 key.type = BTRFS_EXTENT_ITEM_KEY;
4461 key.offset = num_bytes;
4462
Zheng Yan31840ae2008-09-23 13:14:14 -04004463 ret = btrfs_search_slot(trans, extent_root,
4464 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004465 if (ret) {
4466 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004467 ", was looking for %llu\n", ret,
4468 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004469 btrfs_print_leaf(extent_root, path->nodes[0]);
4470 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004471 BUG_ON(ret);
4472 extent_slot = path->slots[0];
4473 }
Chris Mason7bb86312007-12-11 09:25:06 -05004474 } else {
4475 btrfs_print_leaf(extent_root, path->nodes[0]);
4476 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004477 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004478 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004479 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004480 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004481 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004482 (unsigned long long)owner_objectid,
4483 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004484 }
Chris Mason5f39d392007-10-15 16:14:19 -04004485
4486 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004487 item_size = btrfs_item_size_nr(leaf, extent_slot);
4488#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4489 if (item_size < sizeof(*ei)) {
4490 BUG_ON(found_extent || extent_slot != path->slots[0]);
4491 ret = convert_extent_item_v0(trans, extent_root, path,
4492 owner_objectid, 0);
4493 BUG_ON(ret < 0);
4494
4495 btrfs_release_path(extent_root, path);
4496 path->leave_spinning = 1;
4497
4498 key.objectid = bytenr;
4499 key.type = BTRFS_EXTENT_ITEM_KEY;
4500 key.offset = num_bytes;
4501
4502 ret = btrfs_search_slot(trans, extent_root, &key, path,
4503 -1, 1);
4504 if (ret) {
4505 printk(KERN_ERR "umm, got %d back from search"
4506 ", was looking for %llu\n", ret,
4507 (unsigned long long)bytenr);
4508 btrfs_print_leaf(extent_root, path->nodes[0]);
4509 }
4510 BUG_ON(ret);
4511 extent_slot = path->slots[0];
4512 leaf = path->nodes[0];
4513 item_size = btrfs_item_size_nr(leaf, extent_slot);
4514 }
4515#endif
4516 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004517 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004518 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004519 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4520 struct btrfs_tree_block_info *bi;
4521 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4522 bi = (struct btrfs_tree_block_info *)(ei + 1);
4523 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004524 }
4525
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004526 refs = btrfs_extent_refs(leaf, ei);
4527 BUG_ON(refs < refs_to_drop);
4528 refs -= refs_to_drop;
4529
4530 if (refs > 0) {
4531 if (extent_op)
4532 __run_delayed_extent_op(extent_op, leaf, ei);
4533 /*
4534 * In the case of inline back ref, reference count will
4535 * be updated by remove_extent_backref
4536 */
4537 if (iref) {
4538 BUG_ON(!found_extent);
4539 } else {
4540 btrfs_set_extent_refs(leaf, ei, refs);
4541 btrfs_mark_buffer_dirty(leaf);
4542 }
4543 if (found_extent) {
4544 ret = remove_extent_backref(trans, extent_root, path,
4545 iref, refs_to_drop,
4546 is_data);
4547 BUG_ON(ret);
4548 }
4549 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004550 if (found_extent) {
4551 BUG_ON(is_data && refs_to_drop !=
4552 extent_data_ref_count(root, path, iref));
4553 if (iref) {
4554 BUG_ON(path->slots[0] != extent_slot);
4555 } else {
4556 BUG_ON(path->slots[0] != extent_slot + 1);
4557 path->slots[0] = extent_slot;
4558 num_to_del = 2;
4559 }
Chris Mason78fae272007-03-25 11:35:08 -04004560 }
Chris Masonb9473432009-03-13 11:00:37 -04004561
Chris Mason952fcca2008-02-18 16:33:44 -05004562 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4563 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004564 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04004565 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01004566
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004567 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004568 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4569 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004570 } else {
4571 invalidate_mapping_pages(info->btree_inode->i_mapping,
4572 bytenr >> PAGE_CACHE_SHIFT,
4573 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004574 }
4575
Yan, Zhengf0486c62010-05-16 10:46:25 -04004576 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004577 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004578 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004579 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004580 return ret;
4581}
4582
4583/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004584 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004585 * delayed ref for that extent as well. This searches the delayed ref tree for
4586 * a given extent, and if there are no other delayed refs to be processed, it
4587 * removes it from the tree.
4588 */
4589static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4590 struct btrfs_root *root, u64 bytenr)
4591{
4592 struct btrfs_delayed_ref_head *head;
4593 struct btrfs_delayed_ref_root *delayed_refs;
4594 struct btrfs_delayed_ref_node *ref;
4595 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004596 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004597
4598 delayed_refs = &trans->transaction->delayed_refs;
4599 spin_lock(&delayed_refs->lock);
4600 head = btrfs_find_delayed_ref_head(trans, bytenr);
4601 if (!head)
4602 goto out;
4603
4604 node = rb_prev(&head->node.rb_node);
4605 if (!node)
4606 goto out;
4607
4608 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4609
4610 /* there are still entries for this ref, we can't drop it */
4611 if (ref->bytenr == bytenr)
4612 goto out;
4613
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004614 if (head->extent_op) {
4615 if (!head->must_insert_reserved)
4616 goto out;
4617 kfree(head->extent_op);
4618 head->extent_op = NULL;
4619 }
4620
Chris Mason1887be62009-03-13 10:11:24 -04004621 /*
4622 * waiting for the lock here would deadlock. If someone else has it
4623 * locked they are already in the process of dropping it anyway
4624 */
4625 if (!mutex_trylock(&head->mutex))
4626 goto out;
4627
4628 /*
4629 * at this point we have a head with no other entries. Go
4630 * ahead and process it.
4631 */
4632 head->node.in_tree = 0;
4633 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004634
Chris Mason1887be62009-03-13 10:11:24 -04004635 delayed_refs->num_entries--;
4636
4637 /*
4638 * we don't take a ref on the node because we're removing it from the
4639 * tree, so we just steal the ref the tree was holding.
4640 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004641 delayed_refs->num_heads--;
4642 if (list_empty(&head->cluster))
4643 delayed_refs->num_heads_ready--;
4644
4645 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004646 spin_unlock(&delayed_refs->lock);
4647
Yan, Zhengf0486c62010-05-16 10:46:25 -04004648 BUG_ON(head->extent_op);
4649 if (head->must_insert_reserved)
4650 ret = 1;
4651
4652 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004653 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004654 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004655out:
4656 spin_unlock(&delayed_refs->lock);
4657 return 0;
4658}
4659
Yan, Zhengf0486c62010-05-16 10:46:25 -04004660void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4661 struct btrfs_root *root,
4662 struct extent_buffer *buf,
4663 u64 parent, int last_ref)
4664{
4665 struct btrfs_block_rsv *block_rsv;
4666 struct btrfs_block_group_cache *cache = NULL;
4667 int ret;
4668
4669 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4670 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4671 parent, root->root_key.objectid,
4672 btrfs_header_level(buf),
4673 BTRFS_DROP_DELAYED_REF, NULL);
4674 BUG_ON(ret);
4675 }
4676
4677 if (!last_ref)
4678 return;
4679
4680 block_rsv = get_block_rsv(trans, root);
4681 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004682 if (block_rsv->space_info != cache->space_info)
4683 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004684
4685 if (btrfs_header_generation(buf) == trans->transid) {
4686 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4687 ret = check_ref_cleanup(trans, root, buf->start);
4688 if (!ret)
4689 goto pin;
4690 }
4691
4692 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4693 pin_down_extent(root, cache, buf->start, buf->len, 1);
4694 goto pin;
4695 }
4696
4697 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4698
4699 btrfs_add_free_space(cache, buf->start, buf->len);
4700 ret = update_reserved_bytes(cache, buf->len, 0, 0);
4701 if (ret == -EAGAIN) {
4702 /* block group became read-only */
4703 update_reserved_bytes(cache, buf->len, 0, 1);
4704 goto out;
4705 }
4706
4707 ret = 1;
4708 spin_lock(&block_rsv->lock);
4709 if (block_rsv->reserved < block_rsv->size) {
4710 block_rsv->reserved += buf->len;
4711 ret = 0;
4712 }
4713 spin_unlock(&block_rsv->lock);
4714
4715 if (ret) {
4716 spin_lock(&cache->space_info->lock);
4717 cache->space_info->bytes_reserved -= buf->len;
4718 spin_unlock(&cache->space_info->lock);
4719 }
4720 goto out;
4721 }
4722pin:
4723 if (block_rsv->durable && !cache->ro) {
4724 ret = 0;
4725 spin_lock(&cache->lock);
4726 if (!cache->ro) {
4727 cache->reserved_pinned += buf->len;
4728 ret = 1;
4729 }
4730 spin_unlock(&cache->lock);
4731
4732 if (ret) {
4733 spin_lock(&block_rsv->lock);
4734 block_rsv->freed[trans->transid & 0x1] += buf->len;
4735 spin_unlock(&block_rsv->lock);
4736 }
4737 }
4738out:
4739 btrfs_put_block_group(cache);
4740}
4741
Chris Mason925baed2008-06-25 16:01:30 -04004742int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004743 struct btrfs_root *root,
4744 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004745 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004746{
4747 int ret;
4748
Chris Mason56bec292009-03-13 10:10:06 -04004749 /*
4750 * tree log blocks never actually go into the extent allocation
4751 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004752 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004753 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4754 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004755 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004756 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004757 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004758 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4759 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4760 parent, root_objectid, (int)owner,
4761 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004762 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004763 } else {
4764 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4765 parent, root_objectid, owner,
4766 offset, BTRFS_DROP_DELAYED_REF, NULL);
4767 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004768 }
Chris Mason925baed2008-06-25 16:01:30 -04004769 return ret;
4770}
4771
Chris Mason87ee04e2007-11-30 11:30:34 -05004772static u64 stripe_align(struct btrfs_root *root, u64 val)
4773{
4774 u64 mask = ((u64)root->stripesize - 1);
4775 u64 ret = (val + mask) & ~mask;
4776 return ret;
4777}
4778
Chris Masonfec577f2007-02-26 10:40:21 -05004779/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004780 * when we wait for progress in the block group caching, its because
4781 * our allocation attempt failed at least once. So, we must sleep
4782 * and let some progress happen before we try again.
4783 *
4784 * This function will sleep at least once waiting for new free space to
4785 * show up, and then it will check the block group free space numbers
4786 * for our min num_bytes. Another option is to have it go ahead
4787 * and look in the rbtree for a free extent of a given size, but this
4788 * is a good start.
4789 */
4790static noinline int
4791wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4792 u64 num_bytes)
4793{
Yan Zheng11833d62009-09-11 16:11:19 -04004794 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004795 DEFINE_WAIT(wait);
4796
Yan Zheng11833d62009-09-11 16:11:19 -04004797 caching_ctl = get_caching_control(cache);
4798 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004799 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004800
Yan Zheng11833d62009-09-11 16:11:19 -04004801 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004802 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004803
4804 put_caching_control(caching_ctl);
4805 return 0;
4806}
4807
4808static noinline int
4809wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4810{
4811 struct btrfs_caching_control *caching_ctl;
4812 DEFINE_WAIT(wait);
4813
4814 caching_ctl = get_caching_control(cache);
4815 if (!caching_ctl)
4816 return 0;
4817
4818 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4819
4820 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004821 return 0;
4822}
4823
Yan, Zhengb742bb822010-05-16 10:46:24 -04004824static int get_block_group_index(struct btrfs_block_group_cache *cache)
4825{
4826 int index;
4827 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4828 index = 0;
4829 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4830 index = 1;
4831 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4832 index = 2;
4833 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4834 index = 3;
4835 else
4836 index = 4;
4837 return index;
4838}
4839
Josef Bacik817d52f2009-07-13 21:29:25 -04004840enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004841 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004842 LOOP_CACHING_NOWAIT = 1,
4843 LOOP_CACHING_WAIT = 2,
4844 LOOP_ALLOC_CHUNK = 3,
4845 LOOP_NO_EMPTY_SIZE = 4,
4846};
4847
4848/*
Chris Masonfec577f2007-02-26 10:40:21 -05004849 * walks the btree of allocated extents and find a hole of a given size.
4850 * The key ins is changed to record the hole:
4851 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004852 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004853 * ins->offset == number of blocks
4854 * Any available blocks before search_start are skipped.
4855 */
Chris Masond3977122009-01-05 21:25:51 -05004856static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004857 struct btrfs_root *orig_root,
4858 u64 num_bytes, u64 empty_size,
4859 u64 search_start, u64 search_end,
4860 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004861 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004862{
Josef Bacik80eb2342008-10-29 14:49:05 -04004863 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004864 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004865 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004866 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004867 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004868 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004869 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004870 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004871 int last_ptr_loop = 0;
4872 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004873 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004874 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004875 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004876 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004877 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004878 u64 ideal_cache_percent = 0;
4879 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004880
Chris Masondb945352007-10-15 16:15:53 -04004881 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004882 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004883 ins->objectid = 0;
4884 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004885
Josef Bacik2552d172009-04-03 10:14:19 -04004886 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004887 if (!space_info) {
4888 printk(KERN_ERR "No space info for %d\n", data);
4889 return -ENOSPC;
4890 }
Josef Bacik2552d172009-04-03 10:14:19 -04004891
Josef Bacik67377732010-09-16 16:19:09 -04004892 /*
4893 * If the space info is for both data and metadata it means we have a
4894 * small filesystem and we can't use the clustering stuff.
4895 */
4896 if (btrfs_mixed_space_info(space_info))
4897 use_cluster = false;
4898
Chris Mason0ef3e662008-05-24 14:04:53 -04004899 if (orig_root->ref_cows || empty_size)
4900 allowed_chunk_alloc = 1;
4901
Josef Bacik67377732010-09-16 16:19:09 -04004902 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004903 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004904 if (!btrfs_test_opt(root, SSD))
4905 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004906 }
4907
Josef Bacik67377732010-09-16 16:19:09 -04004908 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4909 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004910 last_ptr = &root->fs_info->data_alloc_cluster;
4911 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004912
Chris Mason239b14b2008-03-24 15:02:07 -04004913 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004914 spin_lock(&last_ptr->lock);
4915 if (last_ptr->block_group)
4916 hint_byte = last_ptr->window_start;
4917 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004918 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004919
Chris Masona061fc82008-05-07 11:43:44 -04004920 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004921 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004922
Josef Bacik817d52f2009-07-13 21:29:25 -04004923 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004924 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004925
Josef Bacik2552d172009-04-03 10:14:19 -04004926 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004927ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004928 block_group = btrfs_lookup_block_group(root->fs_info,
4929 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004930 /*
4931 * we don't want to use the block group if it doesn't match our
4932 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004933 *
4934 * However if we are re-searching with an ideal block group
4935 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004936 */
4937 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004938 (block_group->cached != BTRFS_CACHE_NO ||
4939 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004940 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004941 if (list_empty(&block_group->list) ||
4942 block_group->ro) {
4943 /*
4944 * someone is removing this block group,
4945 * we can't jump into the have_block_group
4946 * target because our list pointers are not
4947 * valid
4948 */
4949 btrfs_put_block_group(block_group);
4950 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004951 } else {
Yan, Zhengb742bb822010-05-16 10:46:24 -04004952 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004953 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004954 }
Josef Bacik2552d172009-04-03 10:14:19 -04004955 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004956 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004957 }
Chris Mason42e70e72008-11-07 18:17:11 -05004958 }
Josef Bacik2552d172009-04-03 10:14:19 -04004959search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004960 down_read(&space_info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04004961 list_for_each_entry(block_group, &space_info->block_groups[index],
4962 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004963 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004964 int cached;
Chris Mason8a1413a22008-11-10 16:13:54 -05004965
Josef Bacik11dfe352009-11-13 20:12:59 +00004966 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004967 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004968
Chris Mason83a50de2010-12-13 15:06:46 -05004969 /*
4970 * this can happen if we end up cycling through all the
4971 * raid types, but we want to make sure we only allocate
4972 * for the proper type.
4973 */
4974 if (!block_group_bits(block_group, data)) {
4975 u64 extra = BTRFS_BLOCK_GROUP_DUP |
4976 BTRFS_BLOCK_GROUP_RAID1 |
4977 BTRFS_BLOCK_GROUP_RAID10;
4978
4979 /*
4980 * if they asked for extra copies and this block group
4981 * doesn't provide them, bail. This does allow us to
4982 * fill raid0 from raid1.
4983 */
4984 if ((data & extra) && !(block_group->flags & extra))
4985 goto loop;
4986 }
4987
Josef Bacik2552d172009-04-03 10:14:19 -04004988have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004989 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004990 u64 free_percent;
4991
Josef Bacikb8399de2010-12-08 09:15:11 -05004992 ret = cache_block_group(block_group, trans,
4993 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04004994 if (block_group->cached == BTRFS_CACHE_FINISHED)
4995 goto have_block_group;
4996
Josef Bacikccf0e722009-11-10 21:23:48 -05004997 free_percent = btrfs_block_group_used(&block_group->item);
4998 free_percent *= 100;
4999 free_percent = div64_u64(free_percent,
5000 block_group->key.offset);
5001 free_percent = 100 - free_percent;
5002 if (free_percent > ideal_cache_percent &&
5003 likely(!block_group->ro)) {
5004 ideal_cache_offset = block_group->key.objectid;
5005 ideal_cache_percent = free_percent;
5006 }
5007
Josef Bacik817d52f2009-07-13 21:29:25 -04005008 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005009 * We only want to start kthread caching if we are at
5010 * the point where we will wait for caching to make
5011 * progress, or if our ideal search is over and we've
5012 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04005013 */
5014 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05005015 (loop > LOOP_FIND_IDEAL &&
5016 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacikb8399de2010-12-08 09:15:11 -05005017 ret = cache_block_group(block_group, trans,
5018 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005019 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005020 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005021 found_uncached_bg = true;
5022
5023 /*
5024 * If loop is set for cached only, try the next block
5025 * group.
5026 */
5027 if (loop == LOOP_FIND_IDEAL)
5028 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005029 }
5030
Josef Bacik817d52f2009-07-13 21:29:25 -04005031 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005032 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005033 found_uncached_bg = true;
5034
Josef Bacikea6a4782008-11-20 12:16:16 -05005035 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005036 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005037
Josef Bacik0a243252009-09-11 16:11:20 -04005038 /*
5039 * Ok we want to try and use the cluster allocator, so lets look
5040 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5041 * have tried the cluster allocator plenty of times at this
5042 * point and not have found anything, so we are likely way too
5043 * fragmented for the clustering stuff to find anything, so lets
5044 * just skip it and let the allocator find whatever block it can
5045 * find
5046 */
5047 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005048 /*
5049 * the refill lock keeps out other
5050 * people trying to start a new cluster
5051 */
5052 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005053 if (last_ptr->block_group &&
5054 (last_ptr->block_group->ro ||
5055 !block_group_bits(last_ptr->block_group, data))) {
5056 offset = 0;
5057 goto refill_cluster;
5058 }
5059
Chris Masonfa9c0d792009-04-03 09:47:43 -04005060 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5061 num_bytes, search_start);
5062 if (offset) {
5063 /* we have a block, we're done */
5064 spin_unlock(&last_ptr->refill_lock);
5065 goto checks;
5066 }
5067
5068 spin_lock(&last_ptr->lock);
5069 /*
5070 * whoops, this cluster doesn't actually point to
5071 * this block group. Get a ref on the block
5072 * group is does point to and try again
5073 */
5074 if (!last_ptr_loop && last_ptr->block_group &&
5075 last_ptr->block_group != block_group) {
5076
5077 btrfs_put_block_group(block_group);
5078 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005079 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005080 spin_unlock(&last_ptr->lock);
5081 spin_unlock(&last_ptr->refill_lock);
5082
5083 last_ptr_loop = 1;
5084 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005085 /*
5086 * we know this block group is properly
5087 * in the list because
5088 * btrfs_remove_block_group, drops the
5089 * cluster before it removes the block
5090 * group from the list
5091 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04005092 goto have_block_group;
5093 }
5094 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005095refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04005096 /*
5097 * this cluster didn't work out, free it and
5098 * start over
5099 */
5100 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5101
5102 last_ptr_loop = 0;
5103
5104 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005105 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005106 block_group, last_ptr,
5107 offset, num_bytes,
5108 empty_cluster + empty_size);
5109 if (ret == 0) {
5110 /*
5111 * now pull our allocation out of this
5112 * cluster
5113 */
5114 offset = btrfs_alloc_from_cluster(block_group,
5115 last_ptr, num_bytes,
5116 search_start);
5117 if (offset) {
5118 /* we found one, proceed */
5119 spin_unlock(&last_ptr->refill_lock);
5120 goto checks;
5121 }
Josef Bacik0a243252009-09-11 16:11:20 -04005122 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5123 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005124 spin_unlock(&last_ptr->refill_lock);
5125
Josef Bacik0a243252009-09-11 16:11:20 -04005126 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005127 wait_block_group_cache_progress(block_group,
5128 num_bytes + empty_cluster + empty_size);
5129 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005130 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005131
Chris Masonfa9c0d792009-04-03 09:47:43 -04005132 /*
5133 * at this point we either didn't find a cluster
5134 * or we weren't able to allocate a block from our
5135 * cluster. Free the cluster we've been trying
5136 * to use, and go to the next block group
5137 */
Josef Bacik0a243252009-09-11 16:11:20 -04005138 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005139 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005140 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005141 }
5142
Josef Bacik6226cb02009-04-03 10:14:18 -04005143 offset = btrfs_find_space_for_alloc(block_group, search_start,
5144 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005145 /*
5146 * If we didn't find a chunk, and we haven't failed on this
5147 * block group before, and this block group is in the middle of
5148 * caching and we are ok with waiting, then go ahead and wait
5149 * for progress to be made, and set failed_alloc to true.
5150 *
5151 * If failed_alloc is true then we've already waited on this
5152 * block group once and should move on to the next block group.
5153 */
5154 if (!offset && !failed_alloc && !cached &&
5155 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005156 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005157 num_bytes + empty_size);
5158 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005159 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005160 } else if (!offset) {
5161 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005162 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005163checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005164 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005165 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005166 if (search_start + num_bytes >= search_end) {
5167 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005168 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005169 }
Chris Masone37c9e62007-05-09 20:13:14 -04005170
Josef Bacik2552d172009-04-03 10:14:19 -04005171 /* move on to the next group */
5172 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005173 block_group->key.objectid + block_group->key.offset) {
5174 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005175 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005176 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005177
Josef Bacik2552d172009-04-03 10:14:19 -04005178 ins->objectid = search_start;
5179 ins->offset = num_bytes;
5180
Josef Bacik6226cb02009-04-03 10:14:18 -04005181 if (offset < search_start)
5182 btrfs_add_free_space(block_group, offset,
5183 search_start - offset);
5184 BUG_ON(offset > search_start);
5185
Yan, Zhengf0486c62010-05-16 10:46:25 -04005186 ret = update_reserved_bytes(block_group, num_bytes, 1,
5187 (data & BTRFS_BLOCK_GROUP_DATA));
5188 if (ret == -EAGAIN) {
5189 btrfs_add_free_space(block_group, offset, num_bytes);
5190 goto loop;
5191 }
Yan Zheng11833d62009-09-11 16:11:19 -04005192
Josef Bacik2552d172009-04-03 10:14:19 -04005193 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005194 ins->objectid = search_start;
5195 ins->offset = num_bytes;
5196
5197 if (offset < search_start)
5198 btrfs_add_free_space(block_group, offset,
5199 search_start - offset);
5200 BUG_ON(offset > search_start);
Josef Bacik2552d172009-04-03 10:14:19 -04005201 break;
5202loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005203 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005204 failed_alloc = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005205 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d792009-04-03 09:47:43 -04005206 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005207 }
5208 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005209
Yan, Zhengb742bb822010-05-16 10:46:24 -04005210 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5211 goto search;
5212
Josef Bacikccf0e722009-11-10 21:23:48 -05005213 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5214 * for them to make caching progress. Also
5215 * determine the best possible bg to cache
5216 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5217 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005218 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5219 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5220 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5221 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04005222 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005223 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5224 (found_uncached_bg || empty_size || empty_cluster ||
5225 allowed_chunk_alloc)) {
Yan, Zhengb742bb822010-05-16 10:46:24 -04005226 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005227 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005228 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005229 loop++;
5230 if (!ideal_cache_percent &&
5231 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005232 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005233
5234 /*
5235 * 1 of the following 2 things have happened so far
5236 *
5237 * 1) We found an ideal block group for caching that
5238 * is mostly full and will cache quickly, so we might
5239 * as well wait for it.
5240 *
5241 * 2) We searched for cached only and we didn't find
5242 * anything, and we didn't start any caching kthreads
5243 * either, so chances are we will loop through and
5244 * start a couple caching kthreads, and then come back
5245 * around and just wait for them. This will be slower
5246 * because we will have 2 caching kthreads reading at
5247 * the same time when we could have just started one
5248 * and waited for it to get far enough to give us an
5249 * allocation, so go ahead and go to the wait caching
5250 * loop.
5251 */
5252 loop = LOOP_CACHING_WAIT;
5253 search_start = ideal_cache_offset;
5254 ideal_cache_percent = 0;
5255 goto ideal_cache;
5256 } else if (loop == LOOP_FIND_IDEAL) {
5257 /*
5258 * Didn't find a uncached bg, wait on anything we find
5259 * next.
5260 */
5261 loop = LOOP_CACHING_WAIT;
5262 goto search;
5263 }
5264
5265 if (loop < LOOP_CACHING_WAIT) {
5266 loop++;
5267 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005268 }
5269
5270 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005271 empty_size = 0;
5272 empty_cluster = 0;
5273 }
Chris Mason42e70e72008-11-07 18:17:11 -05005274
Josef Bacik2552d172009-04-03 10:14:19 -04005275 if (allowed_chunk_alloc) {
5276 ret = do_chunk_alloc(trans, root, num_bytes +
5277 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04005278 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005279 done_chunk_alloc = 1;
5280 } else if (!done_chunk_alloc) {
Josef Bacik2552d172009-04-03 10:14:19 -04005281 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005282 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005283
Josef Bacik817d52f2009-07-13 21:29:25 -04005284 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005285 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005286 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005287 }
Josef Bacik2552d172009-04-03 10:14:19 -04005288 ret = -ENOSPC;
5289 } else if (!ins->objectid) {
5290 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04005291 }
Chris Mason0b86a832008-03-24 15:01:56 -04005292
Josef Bacik80eb2342008-10-29 14:49:05 -04005293 /* we found what we needed */
5294 if (ins->objectid) {
5295 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05005296 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04005297
Chris Masonfa9c0d792009-04-03 09:47:43 -04005298 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005299 ret = 0;
5300 }
Chris Mason0b86a832008-03-24 15:01:56 -04005301
Chris Mason0f70abe2007-02-28 16:46:22 -05005302 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005303}
Chris Masonec44a352008-04-28 15:29:52 -04005304
Josef Bacik9ed74f22009-09-11 16:12:44 -04005305static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5306 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005307{
5308 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005309 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005310
Josef Bacik9ed74f22009-09-11 16:12:44 -04005311 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005312 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5313 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005314 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005315 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005316 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005317 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5318 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005319 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005320 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005321 (unsigned long long)info->bytes_pinned,
5322 (unsigned long long)info->bytes_reserved,
5323 (unsigned long long)info->bytes_may_use,
5324 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005325 spin_unlock(&info->lock);
5326
5327 if (!dump_block_groups)
5328 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005329
Josef Bacik80eb2342008-10-29 14:49:05 -04005330 down_read(&info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04005331again:
5332 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005333 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005334 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5335 "%llu pinned %llu reserved\n",
5336 (unsigned long long)cache->key.objectid,
5337 (unsigned long long)cache->key.offset,
5338 (unsigned long long)btrfs_block_group_used(&cache->item),
5339 (unsigned long long)cache->pinned,
5340 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005341 btrfs_dump_free_space(cache, bytes);
5342 spin_unlock(&cache->lock);
5343 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04005344 if (++index < BTRFS_NR_RAID_TYPES)
5345 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005346 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005347}
Zheng Yane8569812008-09-26 10:05:48 -04005348
Yan Zheng11833d62009-09-11 16:11:19 -04005349int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5350 struct btrfs_root *root,
5351 u64 num_bytes, u64 min_alloc_size,
5352 u64 empty_size, u64 hint_byte,
5353 u64 search_end, struct btrfs_key *ins,
5354 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005355{
5356 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005357 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005358
Josef Bacik6a632092009-02-20 11:00:09 -05005359 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005360again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005361 /*
5362 * the only place that sets empty_size is btrfs_realloc_node, which
5363 * is not called recursively on allocations
5364 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005365 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005366 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005367 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04005368
Chris Masondb945352007-10-15 16:15:53 -04005369 WARN_ON(num_bytes < root->sectorsize);
5370 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005371 search_start, search_end, hint_byte,
5372 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005373
Chris Mason98d20f62008-04-14 09:46:10 -04005374 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5375 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005376 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005377 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005378 do_chunk_alloc(trans, root->fs_info->extent_root,
5379 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005380 goto again;
5381 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005382 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005383 struct btrfs_space_info *sinfo;
5384
5385 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005386 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5387 "wanted %llu\n", (unsigned long long)data,
5388 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005389 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005390 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005391
5392 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005393}
5394
Chris Mason65b51a02008-08-01 15:11:20 -04005395int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5396{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005397 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005398 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005399
Josef Bacik0f9dd462008-09-23 13:14:11 -04005400 cache = btrfs_lookup_block_group(root->fs_info, start);
5401 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005402 printk(KERN_ERR "Unable to find block group for %llu\n",
5403 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005404 return -ENOSPC;
5405 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005406
5407 ret = btrfs_discard_extent(root, start, len);
5408
Josef Bacik0f9dd462008-09-23 13:14:11 -04005409 btrfs_add_free_space(cache, start, len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005410 update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005411 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005412
Chris Masone6dcd2d2008-07-17 12:53:50 -04005413 return ret;
5414}
5415
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005416static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5417 struct btrfs_root *root,
5418 u64 parent, u64 root_objectid,
5419 u64 flags, u64 owner, u64 offset,
5420 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005421{
5422 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005423 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005424 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005425 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005426 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005427 struct extent_buffer *leaf;
5428 int type;
5429 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005430
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005431 if (parent > 0)
5432 type = BTRFS_SHARED_DATA_REF_KEY;
5433 else
5434 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005435
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005436 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005437
5438 path = btrfs_alloc_path();
5439 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05005440
Chris Masonb9473432009-03-13 11:00:37 -04005441 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005442 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5443 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005444 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005445
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005446 leaf = path->nodes[0];
5447 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005448 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005449 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5450 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5451 btrfs_set_extent_flags(leaf, extent_item,
5452 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005453
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005454 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5455 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5456 if (parent > 0) {
5457 struct btrfs_shared_data_ref *ref;
5458 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5459 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5460 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5461 } else {
5462 struct btrfs_extent_data_ref *ref;
5463 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5464 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5465 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5466 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5467 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5468 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005469
5470 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005471 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005472
Yan, Zhengf0486c62010-05-16 10:46:25 -04005473 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005474 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005475 printk(KERN_ERR "btrfs update block group failed for %llu "
5476 "%llu\n", (unsigned long long)ins->objectid,
5477 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005478 BUG();
5479 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005480 return ret;
5481}
5482
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005483static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5484 struct btrfs_root *root,
5485 u64 parent, u64 root_objectid,
5486 u64 flags, struct btrfs_disk_key *key,
5487 int level, struct btrfs_key *ins)
5488{
5489 int ret;
5490 struct btrfs_fs_info *fs_info = root->fs_info;
5491 struct btrfs_extent_item *extent_item;
5492 struct btrfs_tree_block_info *block_info;
5493 struct btrfs_extent_inline_ref *iref;
5494 struct btrfs_path *path;
5495 struct extent_buffer *leaf;
5496 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5497
5498 path = btrfs_alloc_path();
5499 BUG_ON(!path);
5500
5501 path->leave_spinning = 1;
5502 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5503 ins, size);
5504 BUG_ON(ret);
5505
5506 leaf = path->nodes[0];
5507 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5508 struct btrfs_extent_item);
5509 btrfs_set_extent_refs(leaf, extent_item, 1);
5510 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5511 btrfs_set_extent_flags(leaf, extent_item,
5512 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5513 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5514
5515 btrfs_set_tree_block_key(leaf, block_info, key);
5516 btrfs_set_tree_block_level(leaf, block_info, level);
5517
5518 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5519 if (parent > 0) {
5520 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5521 btrfs_set_extent_inline_ref_type(leaf, iref,
5522 BTRFS_SHARED_BLOCK_REF_KEY);
5523 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5524 } else {
5525 btrfs_set_extent_inline_ref_type(leaf, iref,
5526 BTRFS_TREE_BLOCK_REF_KEY);
5527 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5528 }
5529
5530 btrfs_mark_buffer_dirty(leaf);
5531 btrfs_free_path(path);
5532
Yan, Zhengf0486c62010-05-16 10:46:25 -04005533 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005534 if (ret) {
5535 printk(KERN_ERR "btrfs update block group failed for %llu "
5536 "%llu\n", (unsigned long long)ins->objectid,
5537 (unsigned long long)ins->offset);
5538 BUG();
5539 }
5540 return ret;
5541}
5542
5543int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5544 struct btrfs_root *root,
5545 u64 root_objectid, u64 owner,
5546 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005547{
5548 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04005549
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005550 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005551
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005552 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5553 0, root_objectid, owner, offset,
5554 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005555 return ret;
5556}
Chris Masone02119d2008-09-05 16:13:11 -04005557
5558/*
5559 * this is used by the tree logging recovery code. It records that
5560 * an extent has been allocated and makes sure to clear the free
5561 * space cache bits as well
5562 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005563int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5564 struct btrfs_root *root,
5565 u64 root_objectid, u64 owner, u64 offset,
5566 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005567{
5568 int ret;
5569 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005570 struct btrfs_caching_control *caching_ctl;
5571 u64 start = ins->objectid;
5572 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005573
Chris Masone02119d2008-09-05 16:13:11 -04005574 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005575 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005576 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005577
Yan Zheng11833d62009-09-11 16:11:19 -04005578 if (!caching_ctl) {
5579 BUG_ON(!block_group_cache_done(block_group));
5580 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5581 BUG_ON(ret);
5582 } else {
5583 mutex_lock(&caching_ctl->mutex);
5584
5585 if (start >= caching_ctl->progress) {
5586 ret = add_excluded_extent(root, start, num_bytes);
5587 BUG_ON(ret);
5588 } else if (start + num_bytes <= caching_ctl->progress) {
5589 ret = btrfs_remove_free_space(block_group,
5590 start, num_bytes);
5591 BUG_ON(ret);
5592 } else {
5593 num_bytes = caching_ctl->progress - start;
5594 ret = btrfs_remove_free_space(block_group,
5595 start, num_bytes);
5596 BUG_ON(ret);
5597
5598 start = caching_ctl->progress;
5599 num_bytes = ins->objectid + ins->offset -
5600 caching_ctl->progress;
5601 ret = add_excluded_extent(root, start, num_bytes);
5602 BUG_ON(ret);
5603 }
5604
5605 mutex_unlock(&caching_ctl->mutex);
5606 put_caching_control(caching_ctl);
5607 }
5608
Yan, Zhengf0486c62010-05-16 10:46:25 -04005609 ret = update_reserved_bytes(block_group, ins->offset, 1, 1);
5610 BUG_ON(ret);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005611 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005612 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5613 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005614 return ret;
5615}
5616
Chris Mason65b51a02008-08-01 15:11:20 -04005617struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5618 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005619 u64 bytenr, u32 blocksize,
5620 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005621{
5622 struct extent_buffer *buf;
5623
5624 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5625 if (!buf)
5626 return ERR_PTR(-ENOMEM);
5627 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005628 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005629 btrfs_tree_lock(buf);
5630 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005631
5632 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005633 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005634
Chris Masond0c803c2008-09-11 16:17:57 -04005635 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005636 /*
5637 * we allow two log transactions at a time, use different
5638 * EXENT bit to differentiate dirty pages.
5639 */
5640 if (root->log_transid % 2 == 0)
5641 set_extent_dirty(&root->dirty_log_pages, buf->start,
5642 buf->start + buf->len - 1, GFP_NOFS);
5643 else
5644 set_extent_new(&root->dirty_log_pages, buf->start,
5645 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005646 } else {
5647 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5648 buf->start + buf->len - 1, GFP_NOFS);
5649 }
Chris Mason65b51a02008-08-01 15:11:20 -04005650 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005651 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005652 return buf;
5653}
5654
Yan, Zhengf0486c62010-05-16 10:46:25 -04005655static struct btrfs_block_rsv *
5656use_block_rsv(struct btrfs_trans_handle *trans,
5657 struct btrfs_root *root, u32 blocksize)
5658{
5659 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005660 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005661 int ret;
5662
5663 block_rsv = get_block_rsv(trans, root);
5664
5665 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005666 ret = reserve_metadata_bytes(trans, root, block_rsv,
5667 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005668 /*
5669 * If we couldn't reserve metadata bytes try and use some from
5670 * the global reserve.
5671 */
5672 if (ret && block_rsv != global_rsv) {
5673 ret = block_rsv_use_bytes(global_rsv, blocksize);
5674 if (!ret)
5675 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005676 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005677 } else if (ret) {
5678 return ERR_PTR(ret);
5679 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005680 return block_rsv;
5681 }
5682
5683 ret = block_rsv_use_bytes(block_rsv, blocksize);
5684 if (!ret)
5685 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005686 if (ret) {
5687 WARN_ON(1);
5688 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5689 0);
5690 if (!ret) {
5691 spin_lock(&block_rsv->lock);
5692 block_rsv->size += blocksize;
5693 spin_unlock(&block_rsv->lock);
5694 return block_rsv;
5695 } else if (ret && block_rsv != global_rsv) {
5696 ret = block_rsv_use_bytes(global_rsv, blocksize);
5697 if (!ret)
5698 return global_rsv;
5699 }
5700 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005701
Yan, Zhengf0486c62010-05-16 10:46:25 -04005702 return ERR_PTR(-ENOSPC);
5703}
5704
5705static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5706{
5707 block_rsv_add_bytes(block_rsv, blocksize, 0);
5708 block_rsv_release_bytes(block_rsv, NULL, 0);
5709}
5710
Chris Masonfec577f2007-02-26 10:40:21 -05005711/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005712 * finds a free extent and does all the dirty work required for allocation
5713 * returns the key for the extent through ins, and a tree buffer for
5714 * the first block of the extent through buf.
5715 *
Chris Masonfec577f2007-02-26 10:40:21 -05005716 * returns the tree buffer or NULL.
5717 */
Chris Mason5f39d392007-10-15 16:14:19 -04005718struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005719 struct btrfs_root *root, u32 blocksize,
5720 u64 parent, u64 root_objectid,
5721 struct btrfs_disk_key *key, int level,
5722 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005723{
Chris Masone2fa7222007-03-12 16:22:34 -04005724 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005725 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005726 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005727 u64 flags = 0;
5728 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005729
Yan, Zhengf0486c62010-05-16 10:46:25 -04005730
5731 block_rsv = use_block_rsv(trans, root, blocksize);
5732 if (IS_ERR(block_rsv))
5733 return ERR_CAST(block_rsv);
5734
5735 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5736 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005737 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005738 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005739 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005740 }
Chris Mason55c69072008-01-09 15:55:33 -05005741
Chris Mason4008c042009-02-12 14:09:45 -05005742 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5743 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005744 BUG_ON(IS_ERR(buf));
5745
5746 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5747 if (parent == 0)
5748 parent = ins.objectid;
5749 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5750 } else
5751 BUG_ON(parent > 0);
5752
5753 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5754 struct btrfs_delayed_extent_op *extent_op;
5755 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5756 BUG_ON(!extent_op);
5757 if (key)
5758 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5759 else
5760 memset(&extent_op->key, 0, sizeof(extent_op->key));
5761 extent_op->flags_to_set = flags;
5762 extent_op->update_key = 1;
5763 extent_op->update_flags = 1;
5764 extent_op->is_data = 0;
5765
5766 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5767 ins.offset, parent, root_objectid,
5768 level, BTRFS_ADD_DELAYED_EXTENT,
5769 extent_op);
5770 BUG_ON(ret);
5771 }
Chris Masonfec577f2007-02-26 10:40:21 -05005772 return buf;
5773}
Chris Masona28ec192007-03-06 20:08:01 -05005774
Yan Zheng2c47e6052009-06-27 21:07:35 -04005775struct walk_control {
5776 u64 refs[BTRFS_MAX_LEVEL];
5777 u64 flags[BTRFS_MAX_LEVEL];
5778 struct btrfs_key update_progress;
5779 int stage;
5780 int level;
5781 int shared_level;
5782 int update_ref;
5783 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005784 int reada_slot;
5785 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005786};
5787
5788#define DROP_REFERENCE 1
5789#define UPDATE_BACKREF 2
5790
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005791static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5792 struct btrfs_root *root,
5793 struct walk_control *wc,
5794 struct btrfs_path *path)
5795{
5796 u64 bytenr;
5797 u64 generation;
5798 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005799 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005800 u32 nritems;
5801 u32 blocksize;
5802 struct btrfs_key key;
5803 struct extent_buffer *eb;
5804 int ret;
5805 int slot;
5806 int nread = 0;
5807
5808 if (path->slots[wc->level] < wc->reada_slot) {
5809 wc->reada_count = wc->reada_count * 2 / 3;
5810 wc->reada_count = max(wc->reada_count, 2);
5811 } else {
5812 wc->reada_count = wc->reada_count * 3 / 2;
5813 wc->reada_count = min_t(int, wc->reada_count,
5814 BTRFS_NODEPTRS_PER_BLOCK(root));
5815 }
5816
5817 eb = path->nodes[wc->level];
5818 nritems = btrfs_header_nritems(eb);
5819 blocksize = btrfs_level_size(root, wc->level - 1);
5820
5821 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5822 if (nread >= wc->reada_count)
5823 break;
5824
5825 cond_resched();
5826 bytenr = btrfs_node_blockptr(eb, slot);
5827 generation = btrfs_node_ptr_generation(eb, slot);
5828
5829 if (slot == path->slots[wc->level])
5830 goto reada;
5831
5832 if (wc->stage == UPDATE_BACKREF &&
5833 generation <= root->root_key.offset)
5834 continue;
5835
Yan, Zheng94fcca92009-10-09 09:25:16 -04005836 /* We don't lock the tree block, it's OK to be racy here */
5837 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5838 &refs, &flags);
5839 BUG_ON(ret);
5840 BUG_ON(refs == 0);
5841
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005842 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005843 if (refs == 1)
5844 goto reada;
5845
Yan, Zheng94fcca92009-10-09 09:25:16 -04005846 if (wc->level == 1 &&
5847 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5848 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005849 if (!wc->update_ref ||
5850 generation <= root->root_key.offset)
5851 continue;
5852 btrfs_node_key_to_cpu(eb, &key, slot);
5853 ret = btrfs_comp_cpu_keys(&key,
5854 &wc->update_progress);
5855 if (ret < 0)
5856 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005857 } else {
5858 if (wc->level == 1 &&
5859 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5860 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005861 }
5862reada:
5863 ret = readahead_tree_block(root, bytenr, blocksize,
5864 generation);
5865 if (ret)
5866 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005867 nread++;
5868 }
5869 wc->reada_slot = slot;
5870}
5871
Chris Mason9aca1d52007-03-13 11:09:37 -04005872/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005873 * hepler to process tree block while walking down the tree.
5874 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005875 * when wc->stage == UPDATE_BACKREF, this function updates
5876 * back refs for pointers in the block.
5877 *
5878 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005879 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005880static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5881 struct btrfs_root *root,
5882 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005883 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005884{
5885 int level = wc->level;
5886 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005887 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5888 int ret;
5889
5890 if (wc->stage == UPDATE_BACKREF &&
5891 btrfs_header_owner(eb) != root->root_key.objectid)
5892 return 1;
5893
5894 /*
5895 * when reference count of tree block is 1, it won't increase
5896 * again. once full backref flag is set, we never clear it.
5897 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005898 if (lookup_info &&
5899 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5900 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005901 BUG_ON(!path->locks[level]);
5902 ret = btrfs_lookup_extent_info(trans, root,
5903 eb->start, eb->len,
5904 &wc->refs[level],
5905 &wc->flags[level]);
5906 BUG_ON(ret);
5907 BUG_ON(wc->refs[level] == 0);
5908 }
5909
Yan Zheng2c47e6052009-06-27 21:07:35 -04005910 if (wc->stage == DROP_REFERENCE) {
5911 if (wc->refs[level] > 1)
5912 return 1;
5913
5914 if (path->locks[level] && !wc->keep_locks) {
5915 btrfs_tree_unlock(eb);
5916 path->locks[level] = 0;
5917 }
5918 return 0;
5919 }
5920
5921 /* wc->stage == UPDATE_BACKREF */
5922 if (!(wc->flags[level] & flag)) {
5923 BUG_ON(!path->locks[level]);
5924 ret = btrfs_inc_ref(trans, root, eb, 1);
5925 BUG_ON(ret);
5926 ret = btrfs_dec_ref(trans, root, eb, 0);
5927 BUG_ON(ret);
5928 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5929 eb->len, flag, 0);
5930 BUG_ON(ret);
5931 wc->flags[level] |= flag;
5932 }
5933
5934 /*
5935 * the block is shared by multiple trees, so it's not good to
5936 * keep the tree lock
5937 */
5938 if (path->locks[level] && level > 0) {
5939 btrfs_tree_unlock(eb);
5940 path->locks[level] = 0;
5941 }
5942 return 0;
5943}
5944
5945/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005946 * hepler to process tree block pointer.
5947 *
5948 * when wc->stage == DROP_REFERENCE, this function checks
5949 * reference count of the block pointed to. if the block
5950 * is shared and we need update back refs for the subtree
5951 * rooted at the block, this function changes wc->stage to
5952 * UPDATE_BACKREF. if the block is shared and there is no
5953 * need to update back, this function drops the reference
5954 * to the block.
5955 *
5956 * NOTE: return value 1 means we should stop walking down.
5957 */
5958static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5959 struct btrfs_root *root,
5960 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005961 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005962{
5963 u64 bytenr;
5964 u64 generation;
5965 u64 parent;
5966 u32 blocksize;
5967 struct btrfs_key key;
5968 struct extent_buffer *next;
5969 int level = wc->level;
5970 int reada = 0;
5971 int ret = 0;
5972
5973 generation = btrfs_node_ptr_generation(path->nodes[level],
5974 path->slots[level]);
5975 /*
5976 * if the lower level block was created before the snapshot
5977 * was created, we know there is no need to update back refs
5978 * for the subtree
5979 */
5980 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005981 generation <= root->root_key.offset) {
5982 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005983 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005984 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005985
5986 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5987 blocksize = btrfs_level_size(root, level - 1);
5988
5989 next = btrfs_find_tree_block(root, bytenr, blocksize);
5990 if (!next) {
5991 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00005992 if (!next)
5993 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005994 reada = 1;
5995 }
5996 btrfs_tree_lock(next);
5997 btrfs_set_lock_blocking(next);
5998
Yan, Zheng94fcca92009-10-09 09:25:16 -04005999 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6000 &wc->refs[level - 1],
6001 &wc->flags[level - 1]);
6002 BUG_ON(ret);
6003 BUG_ON(wc->refs[level - 1] == 0);
6004 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006005
Yan, Zheng94fcca92009-10-09 09:25:16 -04006006 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006007 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006008 if (level == 1 &&
6009 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6010 goto skip;
6011
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006012 if (!wc->update_ref ||
6013 generation <= root->root_key.offset)
6014 goto skip;
6015
6016 btrfs_node_key_to_cpu(path->nodes[level], &key,
6017 path->slots[level]);
6018 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6019 if (ret < 0)
6020 goto skip;
6021
6022 wc->stage = UPDATE_BACKREF;
6023 wc->shared_level = level - 1;
6024 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006025 } else {
6026 if (level == 1 &&
6027 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6028 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006029 }
6030
6031 if (!btrfs_buffer_uptodate(next, generation)) {
6032 btrfs_tree_unlock(next);
6033 free_extent_buffer(next);
6034 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006035 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006036 }
6037
6038 if (!next) {
6039 if (reada && level == 1)
6040 reada_walk_down(trans, root, wc, path);
6041 next = read_tree_block(root, bytenr, blocksize, generation);
6042 btrfs_tree_lock(next);
6043 btrfs_set_lock_blocking(next);
6044 }
6045
6046 level--;
6047 BUG_ON(level != btrfs_header_level(next));
6048 path->nodes[level] = next;
6049 path->slots[level] = 0;
6050 path->locks[level] = 1;
6051 wc->level = level;
6052 if (wc->level == 1)
6053 wc->reada_slot = 0;
6054 return 0;
6055skip:
6056 wc->refs[level - 1] = 0;
6057 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006058 if (wc->stage == DROP_REFERENCE) {
6059 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6060 parent = path->nodes[level]->start;
6061 } else {
6062 BUG_ON(root->root_key.objectid !=
6063 btrfs_header_owner(path->nodes[level]));
6064 parent = 0;
6065 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006066
Yan, Zheng94fcca92009-10-09 09:25:16 -04006067 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6068 root->root_key.objectid, level - 1, 0);
6069 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006070 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006071 btrfs_tree_unlock(next);
6072 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006073 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006074 return 1;
6075}
6076
6077/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006078 * hepler to process tree block while walking up the tree.
6079 *
6080 * when wc->stage == DROP_REFERENCE, this function drops
6081 * reference count on the block.
6082 *
6083 * when wc->stage == UPDATE_BACKREF, this function changes
6084 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6085 * to UPDATE_BACKREF previously while processing the block.
6086 *
6087 * NOTE: return value 1 means we should stop walking up.
6088 */
6089static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6090 struct btrfs_root *root,
6091 struct btrfs_path *path,
6092 struct walk_control *wc)
6093{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006094 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006095 int level = wc->level;
6096 struct extent_buffer *eb = path->nodes[level];
6097 u64 parent = 0;
6098
6099 if (wc->stage == UPDATE_BACKREF) {
6100 BUG_ON(wc->shared_level < level);
6101 if (level < wc->shared_level)
6102 goto out;
6103
Yan Zheng2c47e6052009-06-27 21:07:35 -04006104 ret = find_next_key(path, level + 1, &wc->update_progress);
6105 if (ret > 0)
6106 wc->update_ref = 0;
6107
6108 wc->stage = DROP_REFERENCE;
6109 wc->shared_level = -1;
6110 path->slots[level] = 0;
6111
6112 /*
6113 * check reference count again if the block isn't locked.
6114 * we should start walking down the tree again if reference
6115 * count is one.
6116 */
6117 if (!path->locks[level]) {
6118 BUG_ON(level == 0);
6119 btrfs_tree_lock(eb);
6120 btrfs_set_lock_blocking(eb);
6121 path->locks[level] = 1;
6122
6123 ret = btrfs_lookup_extent_info(trans, root,
6124 eb->start, eb->len,
6125 &wc->refs[level],
6126 &wc->flags[level]);
6127 BUG_ON(ret);
6128 BUG_ON(wc->refs[level] == 0);
6129 if (wc->refs[level] == 1) {
6130 btrfs_tree_unlock(eb);
6131 path->locks[level] = 0;
6132 return 1;
6133 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006134 }
6135 }
6136
6137 /* wc->stage == DROP_REFERENCE */
6138 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6139
6140 if (wc->refs[level] == 1) {
6141 if (level == 0) {
6142 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6143 ret = btrfs_dec_ref(trans, root, eb, 1);
6144 else
6145 ret = btrfs_dec_ref(trans, root, eb, 0);
6146 BUG_ON(ret);
6147 }
6148 /* make block locked assertion in clean_tree_block happy */
6149 if (!path->locks[level] &&
6150 btrfs_header_generation(eb) == trans->transid) {
6151 btrfs_tree_lock(eb);
6152 btrfs_set_lock_blocking(eb);
6153 path->locks[level] = 1;
6154 }
6155 clean_tree_block(trans, root, eb);
6156 }
6157
6158 if (eb == root->node) {
6159 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6160 parent = eb->start;
6161 else
6162 BUG_ON(root->root_key.objectid !=
6163 btrfs_header_owner(eb));
6164 } else {
6165 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6166 parent = path->nodes[level + 1]->start;
6167 else
6168 BUG_ON(root->root_key.objectid !=
6169 btrfs_header_owner(path->nodes[level + 1]));
6170 }
6171
Yan, Zhengf0486c62010-05-16 10:46:25 -04006172 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006173out:
6174 wc->refs[level] = 0;
6175 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006176 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006177}
6178
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006179static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6180 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006181 struct btrfs_path *path,
6182 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006183{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006184 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006185 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006186 int ret;
6187
Yan Zheng2c47e6052009-06-27 21:07:35 -04006188 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006189 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006190 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006191 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006192
Yan Zheng2c47e6052009-06-27 21:07:35 -04006193 if (level == 0)
6194 break;
6195
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006196 if (path->slots[level] >=
6197 btrfs_header_nritems(path->nodes[level]))
6198 break;
6199
Yan, Zheng94fcca92009-10-09 09:25:16 -04006200 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006201 if (ret > 0) {
6202 path->slots[level]++;
6203 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006204 } else if (ret < 0)
6205 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006206 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006207 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006208 return 0;
6209}
6210
Chris Masond3977122009-01-05 21:25:51 -05006211static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006212 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006213 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006214 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006215{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006216 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006217 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006218
Yan Zheng2c47e6052009-06-27 21:07:35 -04006219 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6220 while (level < max_level && path->nodes[level]) {
6221 wc->level = level;
6222 if (path->slots[level] + 1 <
6223 btrfs_header_nritems(path->nodes[level])) {
6224 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006225 return 0;
6226 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006227 ret = walk_up_proc(trans, root, path, wc);
6228 if (ret > 0)
6229 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006230
Yan Zheng2c47e6052009-06-27 21:07:35 -04006231 if (path->locks[level]) {
6232 btrfs_tree_unlock(path->nodes[level]);
6233 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006234 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006235 free_extent_buffer(path->nodes[level]);
6236 path->nodes[level] = NULL;
6237 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006238 }
6239 }
6240 return 1;
6241}
6242
Chris Mason9aca1d52007-03-13 11:09:37 -04006243/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006244 * drop a subvolume tree.
6245 *
6246 * this function traverses the tree freeing any blocks that only
6247 * referenced by the tree.
6248 *
6249 * when a shared tree block is found. this function decreases its
6250 * reference count by one. if update_ref is true, this function
6251 * also make sure backrefs for the shared block and all lower level
6252 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006253 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006254int btrfs_drop_snapshot(struct btrfs_root *root,
6255 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006256{
Chris Mason5caf2a02007-04-02 11:20:42 -04006257 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006258 struct btrfs_trans_handle *trans;
6259 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006260 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006261 struct walk_control *wc;
6262 struct btrfs_key key;
6263 int err = 0;
6264 int ret;
6265 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006266
Chris Mason5caf2a02007-04-02 11:20:42 -04006267 path = btrfs_alloc_path();
6268 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006269
Yan Zheng2c47e6052009-06-27 21:07:35 -04006270 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6271 BUG_ON(!wc);
6272
Yan, Zhenga22285a2010-05-16 10:48:46 -04006273 trans = btrfs_start_transaction(tree_root, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006274 if (block_rsv)
6275 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006276
Chris Mason9f3a7422007-08-07 15:52:19 -04006277 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006278 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006279 path->nodes[level] = btrfs_lock_root_node(root);
6280 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006281 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006282 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006283 memset(&wc->update_progress, 0,
6284 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006285 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006286 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006287 memcpy(&wc->update_progress, &key,
6288 sizeof(wc->update_progress));
6289
Chris Mason6702ed42007-08-07 16:15:09 -04006290 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006291 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006292 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006293 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6294 path->lowest_level = 0;
6295 if (ret < 0) {
6296 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006297 goto out;
6298 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006299 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006300
Chris Mason7d9eb122008-07-08 14:19:17 -04006301 /*
6302 * unlock our path, this is safe because only this
6303 * function is allowed to delete this snapshot
6304 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006305 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006306
Yan Zheng2c47e6052009-06-27 21:07:35 -04006307 level = btrfs_header_level(root->node);
6308 while (1) {
6309 btrfs_tree_lock(path->nodes[level]);
6310 btrfs_set_lock_blocking(path->nodes[level]);
6311
6312 ret = btrfs_lookup_extent_info(trans, root,
6313 path->nodes[level]->start,
6314 path->nodes[level]->len,
6315 &wc->refs[level],
6316 &wc->flags[level]);
6317 BUG_ON(ret);
6318 BUG_ON(wc->refs[level] == 0);
6319
6320 if (level == root_item->drop_level)
6321 break;
6322
6323 btrfs_tree_unlock(path->nodes[level]);
6324 WARN_ON(wc->refs[level] != 1);
6325 level--;
6326 }
6327 }
6328
6329 wc->level = level;
6330 wc->shared_level = -1;
6331 wc->stage = DROP_REFERENCE;
6332 wc->update_ref = update_ref;
6333 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006334 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006335
6336 while (1) {
6337 ret = walk_down_tree(trans, root, path, wc);
6338 if (ret < 0) {
6339 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006340 break;
6341 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006342
6343 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6344 if (ret < 0) {
6345 err = ret;
6346 break;
6347 }
6348
6349 if (ret > 0) {
6350 BUG_ON(wc->stage != DROP_REFERENCE);
6351 break;
6352 }
6353
6354 if (wc->stage == DROP_REFERENCE) {
6355 level = wc->level;
6356 btrfs_node_key(path->nodes[level],
6357 &root_item->drop_progress,
6358 path->slots[level]);
6359 root_item->drop_level = level;
6360 }
6361
6362 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006363 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006364 ret = btrfs_update_root(trans, tree_root,
6365 &root->root_key,
6366 root_item);
6367 BUG_ON(ret);
6368
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006369 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006370 trans = btrfs_start_transaction(tree_root, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006371 if (block_rsv)
6372 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006373 }
Chris Mason20524f02007-03-10 06:35:47 -05006374 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006375 btrfs_release_path(root, path);
6376 BUG_ON(err);
6377
6378 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6379 BUG_ON(ret);
6380
Yan, Zheng76dda932009-09-21 16:00:26 -04006381 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6382 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6383 NULL, NULL);
6384 BUG_ON(ret < 0);
6385 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006386 /* if we fail to delete the orphan item this time
6387 * around, it'll get picked up the next time.
6388 *
6389 * The most common failure here is just -ENOENT.
6390 */
6391 btrfs_del_orphan_item(trans, tree_root,
6392 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006393 }
6394 }
6395
6396 if (root->in_radix) {
6397 btrfs_free_fs_root(tree_root->fs_info, root);
6398 } else {
6399 free_extent_buffer(root->node);
6400 free_extent_buffer(root->commit_root);
6401 kfree(root);
6402 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006403out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006404 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006405 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006406 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006407 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006408}
Chris Mason9078a3e2007-04-26 16:46:15 -04006409
Yan Zheng2c47e6052009-06-27 21:07:35 -04006410/*
6411 * drop subtree rooted at tree block 'node'.
6412 *
6413 * NOTE: this function will unlock and release tree block 'node'
6414 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006415int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6416 struct btrfs_root *root,
6417 struct extent_buffer *node,
6418 struct extent_buffer *parent)
6419{
6420 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006421 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006422 int level;
6423 int parent_level;
6424 int ret = 0;
6425 int wret;
6426
Yan Zheng2c47e6052009-06-27 21:07:35 -04006427 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6428
Yan Zhengf82d02d2008-10-29 14:49:05 -04006429 path = btrfs_alloc_path();
6430 BUG_ON(!path);
6431
Yan Zheng2c47e6052009-06-27 21:07:35 -04006432 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6433 BUG_ON(!wc);
6434
Chris Masonb9447ef82009-03-09 11:45:38 -04006435 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006436 parent_level = btrfs_header_level(parent);
6437 extent_buffer_get(parent);
6438 path->nodes[parent_level] = parent;
6439 path->slots[parent_level] = btrfs_header_nritems(parent);
6440
Chris Masonb9447ef82009-03-09 11:45:38 -04006441 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006442 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006443 path->nodes[level] = node;
6444 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006445 path->locks[level] = 1;
6446
6447 wc->refs[parent_level] = 1;
6448 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6449 wc->level = level;
6450 wc->shared_level = -1;
6451 wc->stage = DROP_REFERENCE;
6452 wc->update_ref = 0;
6453 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006454 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006455
6456 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006457 wret = walk_down_tree(trans, root, path, wc);
6458 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006459 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006460 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006461 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006462
Yan Zheng2c47e6052009-06-27 21:07:35 -04006463 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006464 if (wret < 0)
6465 ret = wret;
6466 if (wret != 0)
6467 break;
6468 }
6469
Yan Zheng2c47e6052009-06-27 21:07:35 -04006470 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006471 btrfs_free_path(path);
6472 return ret;
6473}
6474
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006475#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04006476static unsigned long calc_ra(unsigned long start, unsigned long last,
6477 unsigned long nr)
6478{
6479 return min(last, start + nr - 1);
6480}
6481
Chris Masond3977122009-01-05 21:25:51 -05006482static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05006483 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05006484{
6485 u64 page_start;
6486 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04006487 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006488 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05006489 unsigned long i;
6490 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05006491 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05006492 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04006493 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04006494 unsigned int total_read = 0;
6495 unsigned int total_dirty = 0;
6496 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05006497
6498 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00006499 if (!ra)
6500 return -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006501
6502 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006503 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05006504 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6505
Zheng Yan1a40e232008-09-26 10:09:34 -04006506 /* make sure the dirty trick played by the caller work */
6507 ret = invalidate_inode_pages2_range(inode->i_mapping,
6508 first_index, last_index);
6509 if (ret)
6510 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04006511
Chris Mason4313b392008-01-03 09:08:48 -05006512 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05006513
Zheng Yan1a40e232008-09-26 10:09:34 -04006514 for (i = first_index ; i <= last_index; i++) {
6515 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04006516 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04006517 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04006518 }
6519 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04006520again:
6521 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04006522 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05006523 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04006524 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006525 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05006526 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04006527 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006528 if (!PageUptodate(page)) {
6529 btrfs_readpage(NULL, page);
6530 lock_page(page);
6531 if (!PageUptodate(page)) {
6532 unlock_page(page);
6533 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006534 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05006535 goto out_unlock;
6536 }
6537 }
Chris Masonec44a352008-04-28 15:29:52 -04006538 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04006539
Chris Masonedbd8d42007-12-21 16:27:24 -05006540 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6541 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05006542 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006543
Chris Mason3eaa2882008-07-24 11:57:52 -04006544 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6545 if (ordered) {
6546 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6547 unlock_page(page);
6548 page_cache_release(page);
6549 btrfs_start_ordered_extent(inode, ordered, 1);
6550 btrfs_put_ordered_extent(ordered);
6551 goto again;
6552 }
6553 set_page_extent_mapped(page);
6554
Zheng Yan1a40e232008-09-26 10:09:34 -04006555 if (i == first_index)
6556 set_extent_bits(io_tree, page_start, page_end,
6557 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006558 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04006559
Chris Masona061fc82008-05-07 11:43:44 -04006560 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04006561 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006562
Chris Masond1310b22008-01-24 16:13:08 -05006563 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05006564 unlock_page(page);
6565 page_cache_release(page);
6566 }
6567
6568out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04006569 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05006570 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006571 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04006572 return ret;
6573}
6574
Chris Masond3977122009-01-05 21:25:51 -05006575static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006576 struct btrfs_key *extent_key,
6577 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05006578{
Zheng Yan1a40e232008-09-26 10:09:34 -04006579 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6580 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6581 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04006582 u64 start = extent_key->objectid - offset;
6583 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006584
6585 em = alloc_extent_map(GFP_NOFS);
6586 BUG_ON(!em || IS_ERR(em));
6587
Yan Zheng66435582008-10-30 14:19:50 -04006588 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006589 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04006590 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04006591 em->block_start = extent_key->objectid;
6592 em->bdev = root->fs_info->fs_devices->latest_bdev;
6593 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6594
6595 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04006596 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006597 while (1) {
6598 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04006599 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006600 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04006601 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006602 if (ret != -EEXIST) {
6603 free_extent_map(em);
6604 break;
6605 }
Yan Zheng66435582008-10-30 14:19:50 -04006606 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006607 }
Yan Zheng66435582008-10-30 14:19:50 -04006608 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006609
Yan Zheng66435582008-10-30 14:19:50 -04006610 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04006611}
6612
6613struct btrfs_ref_path {
6614 u64 extent_start;
6615 u64 nodes[BTRFS_MAX_LEVEL];
6616 u64 root_objectid;
6617 u64 root_generation;
6618 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006619 u32 num_refs;
6620 int lowest_level;
6621 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006622 int shared_level;
6623
6624 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6625 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04006626};
6627
6628struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04006629 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04006630 u64 disk_bytenr;
6631 u64 disk_num_bytes;
6632 u64 offset;
6633 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04006634 u8 compression;
6635 u8 encryption;
6636 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04006637};
6638
6639static int is_cowonly_root(u64 root_objectid)
6640{
6641 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6642 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6643 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6644 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05006645 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6646 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04006647 return 1;
6648 return 0;
6649}
6650
Chris Masond3977122009-01-05 21:25:51 -05006651static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006652 struct btrfs_root *extent_root,
6653 struct btrfs_ref_path *ref_path,
6654 int first_time)
6655{
6656 struct extent_buffer *leaf;
6657 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05006658 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05006659 struct btrfs_key key;
6660 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04006661 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05006662 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04006663 int level;
6664 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006665
Zheng Yan1a40e232008-09-26 10:09:34 -04006666 path = btrfs_alloc_path();
6667 if (!path)
6668 return -ENOMEM;
6669
Zheng Yan1a40e232008-09-26 10:09:34 -04006670 if (first_time) {
6671 ref_path->lowest_level = -1;
6672 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006673 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006674 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04006675 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006676walk_down:
6677 level = ref_path->current_level - 1;
6678 while (level >= -1) {
6679 u64 parent;
6680 if (level < ref_path->lowest_level)
6681 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006682
Chris Masond3977122009-01-05 21:25:51 -05006683 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006684 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006685 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006686 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04006687 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006688
Zheng Yan1a40e232008-09-26 10:09:34 -04006689 parent = ref_path->nodes[level + 1];
6690 ref_path->nodes[level + 1] = 0;
6691 ref_path->current_level = level;
6692 BUG_ON(parent == 0);
6693
6694 key.objectid = bytenr;
6695 key.offset = parent + 1;
6696 key.type = BTRFS_EXTENT_REF_KEY;
6697
6698 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006699 if (ret < 0)
6700 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006701 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006702
Chris Masonedbd8d42007-12-21 16:27:24 -05006703 leaf = path->nodes[0];
6704 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04006705 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04006706 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04006707 if (ret < 0)
6708 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006709 if (ret > 0)
6710 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04006711 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04006712 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006713
6714 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04006715 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04006716 found_key.type == BTRFS_EXTENT_REF_KEY) {
6717 if (level < ref_path->shared_level)
6718 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006719 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006720 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006721next:
6722 level--;
6723 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006724 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006725 }
6726 /* reached lowest level */
6727 ret = 1;
6728 goto out;
6729walk_up:
6730 level = ref_path->current_level;
6731 while (level < BTRFS_MAX_LEVEL - 1) {
6732 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05006733
6734 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04006735 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05006736 else
Zheng Yan1a40e232008-09-26 10:09:34 -04006737 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05006738
Zheng Yan1a40e232008-09-26 10:09:34 -04006739 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05006740
Zheng Yan1a40e232008-09-26 10:09:34 -04006741 key.objectid = bytenr;
6742 key.offset = 0;
6743 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05006744
Zheng Yan1a40e232008-09-26 10:09:34 -04006745 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6746 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05006747 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04006748
6749 leaf = path->nodes[0];
6750 nritems = btrfs_header_nritems(leaf);
6751 if (path->slots[0] >= nritems) {
6752 ret = btrfs_next_leaf(extent_root, path);
6753 if (ret < 0)
6754 goto out;
6755 if (ret > 0) {
6756 /* the extent was freed by someone */
6757 if (ref_path->lowest_level == level)
6758 goto out;
6759 btrfs_release_path(extent_root, path);
6760 goto walk_down;
6761 }
6762 leaf = path->nodes[0];
6763 }
6764
6765 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6766 if (found_key.objectid != bytenr ||
6767 found_key.type != BTRFS_EXTENT_REF_KEY) {
6768 /* the extent was freed by someone */
6769 if (ref_path->lowest_level == level) {
6770 ret = 1;
6771 goto out;
6772 }
6773 btrfs_release_path(extent_root, path);
6774 goto walk_down;
6775 }
6776found:
6777 ref = btrfs_item_ptr(leaf, path->slots[0],
6778 struct btrfs_extent_ref);
6779 ref_objectid = btrfs_ref_objectid(leaf, ref);
6780 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6781 if (first_time) {
6782 level = (int)ref_objectid;
6783 BUG_ON(level >= BTRFS_MAX_LEVEL);
6784 ref_path->lowest_level = level;
6785 ref_path->current_level = level;
6786 ref_path->nodes[level] = bytenr;
6787 } else {
6788 WARN_ON(ref_objectid != level);
6789 }
6790 } else {
6791 WARN_ON(level != -1);
6792 }
6793 first_time = 0;
6794
6795 if (ref_path->lowest_level == level) {
6796 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006797 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6798 }
6799
6800 /*
6801 * the block is tree root or the block isn't in reference
6802 * counted tree.
6803 */
6804 if (found_key.objectid == found_key.offset ||
6805 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6806 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6807 ref_path->root_generation =
6808 btrfs_ref_generation(leaf, ref);
6809 if (level < 0) {
6810 /* special reference from the tree log */
6811 ref_path->nodes[0] = found_key.offset;
6812 ref_path->current_level = 0;
6813 }
6814 ret = 0;
6815 goto out;
6816 }
6817
6818 level++;
6819 BUG_ON(ref_path->nodes[level] != 0);
6820 ref_path->nodes[level] = found_key.offset;
6821 ref_path->current_level = level;
6822
6823 /*
6824 * the reference was created in the running transaction,
6825 * no need to continue walking up.
6826 */
6827 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6828 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6829 ref_path->root_generation =
6830 btrfs_ref_generation(leaf, ref);
6831 ret = 0;
6832 goto out;
6833 }
6834
6835 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006836 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006837 }
6838 /* reached max tree level, but no tree root found. */
6839 BUG();
6840out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006841 btrfs_free_path(path);
6842 return ret;
6843}
6844
6845static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6846 struct btrfs_root *extent_root,
6847 struct btrfs_ref_path *ref_path,
6848 u64 extent_start)
6849{
6850 memset(ref_path, 0, sizeof(*ref_path));
6851 ref_path->extent_start = extent_start;
6852
6853 return __next_ref_path(trans, extent_root, ref_path, 1);
6854}
6855
6856static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6857 struct btrfs_root *extent_root,
6858 struct btrfs_ref_path *ref_path)
6859{
6860 return __next_ref_path(trans, extent_root, ref_path, 0);
6861}
6862
Chris Masond3977122009-01-05 21:25:51 -05006863static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006864 struct btrfs_key *extent_key,
6865 u64 offset, int no_fragment,
6866 struct disk_extent **extents,
6867 int *nr_extents)
6868{
6869 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6870 struct btrfs_path *path;
6871 struct btrfs_file_extent_item *fi;
6872 struct extent_buffer *leaf;
6873 struct disk_extent *exts = *extents;
6874 struct btrfs_key found_key;
6875 u64 cur_pos;
6876 u64 last_byte;
6877 u32 nritems;
6878 int nr = 0;
6879 int max = *nr_extents;
6880 int ret;
6881
6882 WARN_ON(!no_fragment && *extents);
6883 if (!exts) {
6884 max = 1;
6885 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6886 if (!exts)
6887 return -ENOMEM;
6888 }
6889
6890 path = btrfs_alloc_path();
6891 BUG_ON(!path);
6892
6893 cur_pos = extent_key->objectid - offset;
6894 last_byte = extent_key->objectid + extent_key->offset;
6895 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6896 cur_pos, 0);
6897 if (ret < 0)
6898 goto out;
6899 if (ret > 0) {
6900 ret = -ENOENT;
6901 goto out;
6902 }
6903
6904 while (1) {
6905 leaf = path->nodes[0];
6906 nritems = btrfs_header_nritems(leaf);
6907 if (path->slots[0] >= nritems) {
6908 ret = btrfs_next_leaf(root, path);
6909 if (ret < 0)
6910 goto out;
6911 if (ret > 0)
6912 break;
6913 leaf = path->nodes[0];
6914 }
6915
6916 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6917 if (found_key.offset != cur_pos ||
6918 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6919 found_key.objectid != reloc_inode->i_ino)
6920 break;
6921
6922 fi = btrfs_item_ptr(leaf, path->slots[0],
6923 struct btrfs_file_extent_item);
6924 if (btrfs_file_extent_type(leaf, fi) !=
6925 BTRFS_FILE_EXTENT_REG ||
6926 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6927 break;
6928
6929 if (nr == max) {
6930 struct disk_extent *old = exts;
6931 max *= 2;
6932 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6933 memcpy(exts, old, sizeof(*exts) * nr);
6934 if (old != *extents)
6935 kfree(old);
6936 }
6937
6938 exts[nr].disk_bytenr =
6939 btrfs_file_extent_disk_bytenr(leaf, fi);
6940 exts[nr].disk_num_bytes =
6941 btrfs_file_extent_disk_num_bytes(leaf, fi);
6942 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6943 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04006944 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6945 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6946 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6947 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6948 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04006949 BUG_ON(exts[nr].offset > 0);
6950 BUG_ON(exts[nr].compression || exts[nr].encryption);
6951 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006952
6953 cur_pos += exts[nr].num_bytes;
6954 nr++;
6955
6956 if (cur_pos + offset >= last_byte)
6957 break;
6958
6959 if (no_fragment) {
6960 ret = 1;
6961 goto out;
6962 }
6963 path->slots[0]++;
6964 }
6965
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006966 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04006967 if (cur_pos + offset < last_byte) {
6968 ret = -ENOENT;
6969 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05006970 }
6971 ret = 0;
6972out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006973 btrfs_free_path(path);
6974 if (ret) {
6975 if (exts != *extents)
6976 kfree(exts);
6977 } else {
6978 *extents = exts;
6979 *nr_extents = nr;
6980 }
6981 return ret;
6982}
6983
Chris Masond3977122009-01-05 21:25:51 -05006984static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006985 struct btrfs_root *root,
6986 struct btrfs_path *path,
6987 struct btrfs_key *extent_key,
6988 struct btrfs_key *leaf_key,
6989 struct btrfs_ref_path *ref_path,
6990 struct disk_extent *new_extents,
6991 int nr_extents)
6992{
6993 struct extent_buffer *leaf;
6994 struct btrfs_file_extent_item *fi;
6995 struct inode *inode = NULL;
6996 struct btrfs_key key;
6997 u64 lock_start = 0;
6998 u64 lock_end = 0;
6999 u64 num_bytes;
7000 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05007001 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007002 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007003 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007004 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04007005 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04007006 int ret;
7007
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007008 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04007009 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007010 if (key.objectid < ref_path->owner_objectid ||
7011 (key.objectid == ref_path->owner_objectid &&
7012 key.type < BTRFS_EXTENT_DATA_KEY)) {
7013 key.objectid = ref_path->owner_objectid;
7014 key.type = BTRFS_EXTENT_DATA_KEY;
7015 key.offset = 0;
7016 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007017 }
7018
7019 while (1) {
7020 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7021 if (ret < 0)
7022 goto out;
7023
7024 leaf = path->nodes[0];
7025 nritems = btrfs_header_nritems(leaf);
7026next:
7027 if (extent_locked && ret > 0) {
7028 /*
7029 * the file extent item was modified by someone
7030 * before the extent got locked.
7031 */
Zheng Yan1a40e232008-09-26 10:09:34 -04007032 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7033 lock_end, GFP_NOFS);
7034 extent_locked = 0;
7035 }
7036
7037 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007038 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04007039 break;
7040
7041 BUG_ON(extent_locked);
7042 ret = btrfs_next_leaf(root, path);
7043 if (ret < 0)
7044 goto out;
7045 if (ret > 0)
7046 break;
7047 leaf = path->nodes[0];
7048 nritems = btrfs_header_nritems(leaf);
7049 }
7050
7051 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7052
7053 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7054 if ((key.objectid > ref_path->owner_objectid) ||
7055 (key.objectid == ref_path->owner_objectid &&
7056 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05007057 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007058 break;
7059 }
7060
7061 if (inode && key.objectid != inode->i_ino) {
7062 BUG_ON(extent_locked);
7063 btrfs_release_path(root, path);
7064 mutex_unlock(&inode->i_mutex);
7065 iput(inode);
7066 inode = NULL;
7067 continue;
7068 }
7069
7070 if (key.type != BTRFS_EXTENT_DATA_KEY) {
7071 path->slots[0]++;
7072 ret = 1;
7073 goto next;
7074 }
7075 fi = btrfs_item_ptr(leaf, path->slots[0],
7076 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04007077 extent_type = btrfs_file_extent_type(leaf, fi);
7078 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
7079 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04007080 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
7081 extent_key->objectid)) {
7082 path->slots[0]++;
7083 ret = 1;
7084 goto next;
7085 }
7086
7087 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7088 ext_offset = btrfs_file_extent_offset(leaf, fi);
7089
Yan Zheng86288a12009-01-21 10:49:16 -05007090 if (search_end == (u64)-1) {
7091 search_end = key.offset - ext_offset +
7092 btrfs_file_extent_ram_bytes(leaf, fi);
7093 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007094
7095 if (!extent_locked) {
7096 lock_start = key.offset;
7097 lock_end = lock_start + num_bytes - 1;
7098 } else {
Yan Zheng66435582008-10-30 14:19:50 -04007099 if (lock_start > key.offset ||
7100 lock_end + 1 < key.offset + num_bytes) {
7101 unlock_extent(&BTRFS_I(inode)->io_tree,
7102 lock_start, lock_end, GFP_NOFS);
7103 extent_locked = 0;
7104 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007105 }
7106
7107 if (!inode) {
7108 btrfs_release_path(root, path);
7109
7110 inode = btrfs_iget_locked(root->fs_info->sb,
7111 key.objectid, root);
7112 if (inode->i_state & I_NEW) {
7113 BTRFS_I(inode)->root = root;
7114 BTRFS_I(inode)->location.objectid =
7115 key.objectid;
7116 BTRFS_I(inode)->location.type =
7117 BTRFS_INODE_ITEM_KEY;
7118 BTRFS_I(inode)->location.offset = 0;
7119 btrfs_read_locked_inode(inode);
7120 unlock_new_inode(inode);
7121 }
7122 /*
7123 * some code call btrfs_commit_transaction while
7124 * holding the i_mutex, so we can't use mutex_lock
7125 * here.
7126 */
7127 if (is_bad_inode(inode) ||
7128 !mutex_trylock(&inode->i_mutex)) {
7129 iput(inode);
7130 inode = NULL;
7131 key.offset = (u64)-1;
7132 goto skip;
7133 }
7134 }
7135
7136 if (!extent_locked) {
7137 struct btrfs_ordered_extent *ordered;
7138
7139 btrfs_release_path(root, path);
7140
7141 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7142 lock_end, GFP_NOFS);
7143 ordered = btrfs_lookup_first_ordered_extent(inode,
7144 lock_end);
7145 if (ordered &&
7146 ordered->file_offset <= lock_end &&
7147 ordered->file_offset + ordered->len > lock_start) {
7148 unlock_extent(&BTRFS_I(inode)->io_tree,
7149 lock_start, lock_end, GFP_NOFS);
7150 btrfs_start_ordered_extent(inode, ordered, 1);
7151 btrfs_put_ordered_extent(ordered);
7152 key.offset += num_bytes;
7153 goto skip;
7154 }
7155 if (ordered)
7156 btrfs_put_ordered_extent(ordered);
7157
Zheng Yan1a40e232008-09-26 10:09:34 -04007158 extent_locked = 1;
7159 continue;
7160 }
7161
7162 if (nr_extents == 1) {
7163 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04007164 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7165 new_extents[0].disk_bytenr);
7166 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7167 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007168 btrfs_mark_buffer_dirty(leaf);
7169
7170 btrfs_drop_extent_cache(inode, key.offset,
7171 key.offset + num_bytes - 1, 0);
7172
7173 ret = btrfs_inc_extent_ref(trans, root,
7174 new_extents[0].disk_bytenr,
7175 new_extents[0].disk_num_bytes,
7176 leaf->start,
7177 root->root_key.objectid,
7178 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007179 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007180 BUG_ON(ret);
7181
7182 ret = btrfs_free_extent(trans, root,
7183 extent_key->objectid,
7184 extent_key->offset,
7185 leaf->start,
7186 btrfs_header_owner(leaf),
7187 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007188 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007189 BUG_ON(ret);
7190
7191 btrfs_release_path(root, path);
7192 key.offset += num_bytes;
7193 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04007194 BUG_ON(1);
7195#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04007196 u64 alloc_hint;
7197 u64 extent_len;
7198 int i;
7199 /*
7200 * drop old extent pointer at first, then insert the
7201 * new pointers one bye one
7202 */
7203 btrfs_release_path(root, path);
7204 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7205 key.offset + num_bytes,
7206 key.offset, &alloc_hint);
7207 BUG_ON(ret);
7208
7209 for (i = 0; i < nr_extents; i++) {
7210 if (ext_offset >= new_extents[i].num_bytes) {
7211 ext_offset -= new_extents[i].num_bytes;
7212 continue;
7213 }
7214 extent_len = min(new_extents[i].num_bytes -
7215 ext_offset, num_bytes);
7216
7217 ret = btrfs_insert_empty_item(trans, root,
7218 path, &key,
7219 sizeof(*fi));
7220 BUG_ON(ret);
7221
7222 leaf = path->nodes[0];
7223 fi = btrfs_item_ptr(leaf, path->slots[0],
7224 struct btrfs_file_extent_item);
7225 btrfs_set_file_extent_generation(leaf, fi,
7226 trans->transid);
7227 btrfs_set_file_extent_type(leaf, fi,
7228 BTRFS_FILE_EXTENT_REG);
7229 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7230 new_extents[i].disk_bytenr);
7231 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7232 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04007233 btrfs_set_file_extent_ram_bytes(leaf, fi,
7234 new_extents[i].ram_bytes);
7235
7236 btrfs_set_file_extent_compression(leaf, fi,
7237 new_extents[i].compression);
7238 btrfs_set_file_extent_encryption(leaf, fi,
7239 new_extents[i].encryption);
7240 btrfs_set_file_extent_other_encoding(leaf, fi,
7241 new_extents[i].other_encoding);
7242
Zheng Yan1a40e232008-09-26 10:09:34 -04007243 btrfs_set_file_extent_num_bytes(leaf, fi,
7244 extent_len);
7245 ext_offset += new_extents[i].offset;
7246 btrfs_set_file_extent_offset(leaf, fi,
7247 ext_offset);
7248 btrfs_mark_buffer_dirty(leaf);
7249
7250 btrfs_drop_extent_cache(inode, key.offset,
7251 key.offset + extent_len - 1, 0);
7252
7253 ret = btrfs_inc_extent_ref(trans, root,
7254 new_extents[i].disk_bytenr,
7255 new_extents[i].disk_num_bytes,
7256 leaf->start,
7257 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007258 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007259 BUG_ON(ret);
7260 btrfs_release_path(root, path);
7261
Yan Zhenga76a3cd2008-10-09 11:46:29 -04007262 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04007263
7264 ext_offset = 0;
7265 num_bytes -= extent_len;
7266 key.offset += extent_len;
7267
7268 if (num_bytes == 0)
7269 break;
7270 }
7271 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007272#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04007273 }
7274
7275 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007276 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7277 lock_end, GFP_NOFS);
7278 extent_locked = 0;
7279 }
7280skip:
7281 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05007282 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04007283 break;
7284
7285 cond_resched();
7286 }
7287 ret = 0;
7288out:
7289 btrfs_release_path(root, path);
7290 if (inode) {
7291 mutex_unlock(&inode->i_mutex);
7292 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007293 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7294 lock_end, GFP_NOFS);
7295 }
7296 iput(inode);
7297 }
7298 return ret;
7299}
7300
Zheng Yan1a40e232008-09-26 10:09:34 -04007301int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7302 struct btrfs_root *root,
7303 struct extent_buffer *buf, u64 orig_start)
7304{
7305 int level;
7306 int ret;
7307
7308 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7309 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7310
7311 level = btrfs_header_level(buf);
7312 if (level == 0) {
7313 struct btrfs_leaf_ref *ref;
7314 struct btrfs_leaf_ref *orig_ref;
7315
7316 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7317 if (!orig_ref)
7318 return -ENOENT;
7319
7320 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7321 if (!ref) {
7322 btrfs_free_leaf_ref(root, orig_ref);
7323 return -ENOMEM;
7324 }
7325
7326 ref->nritems = orig_ref->nritems;
7327 memcpy(ref->extents, orig_ref->extents,
7328 sizeof(ref->extents[0]) * ref->nritems);
7329
7330 btrfs_free_leaf_ref(root, orig_ref);
7331
7332 ref->root_gen = trans->transid;
7333 ref->bytenr = buf->start;
7334 ref->owner = btrfs_header_owner(buf);
7335 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05007336
Zheng Yan1a40e232008-09-26 10:09:34 -04007337 ret = btrfs_add_leaf_ref(root, ref, 0);
7338 WARN_ON(ret);
7339 btrfs_free_leaf_ref(root, ref);
7340 }
7341 return 0;
7342}
7343
Chris Masond3977122009-01-05 21:25:51 -05007344static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007345 struct extent_buffer *leaf,
7346 struct btrfs_block_group_cache *group,
7347 struct btrfs_root *target_root)
7348{
7349 struct btrfs_key key;
7350 struct inode *inode = NULL;
7351 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007352 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04007353 u64 num_bytes;
7354 u64 skip_objectid = 0;
7355 u32 nritems;
7356 u32 i;
7357
7358 nritems = btrfs_header_nritems(leaf);
7359 for (i = 0; i < nritems; i++) {
7360 btrfs_item_key_to_cpu(leaf, &key, i);
7361 if (key.objectid == skip_objectid ||
7362 key.type != BTRFS_EXTENT_DATA_KEY)
7363 continue;
7364 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7365 if (btrfs_file_extent_type(leaf, fi) ==
7366 BTRFS_FILE_EXTENT_INLINE)
7367 continue;
7368 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7369 continue;
7370 if (!inode || inode->i_ino != key.objectid) {
7371 iput(inode);
7372 inode = btrfs_ilookup(target_root->fs_info->sb,
7373 key.objectid, target_root, 1);
7374 }
7375 if (!inode) {
7376 skip_objectid = key.objectid;
7377 continue;
7378 }
7379 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7380
Josef Bacik2ac55d42010-02-03 19:33:23 +00007381 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7382 key.offset + num_bytes - 1, 0, &cached_state,
7383 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007384 btrfs_drop_extent_cache(inode, key.offset,
7385 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007386 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7387 key.offset + num_bytes - 1, &cached_state,
7388 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04007389 cond_resched();
7390 }
7391 iput(inode);
7392 return 0;
7393}
7394
Chris Masond3977122009-01-05 21:25:51 -05007395static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007396 struct btrfs_root *root,
7397 struct extent_buffer *leaf,
7398 struct btrfs_block_group_cache *group,
7399 struct inode *reloc_inode)
7400{
7401 struct btrfs_key key;
7402 struct btrfs_key extent_key;
7403 struct btrfs_file_extent_item *fi;
7404 struct btrfs_leaf_ref *ref;
7405 struct disk_extent *new_extent;
7406 u64 bytenr;
7407 u64 num_bytes;
7408 u32 nritems;
7409 u32 i;
7410 int ext_index;
7411 int nr_extent;
7412 int ret;
7413
7414 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
7415 BUG_ON(!new_extent);
7416
7417 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7418 BUG_ON(!ref);
7419
7420 ext_index = -1;
7421 nritems = btrfs_header_nritems(leaf);
7422 for (i = 0; i < nritems; i++) {
7423 btrfs_item_key_to_cpu(leaf, &key, i);
7424 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7425 continue;
7426 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7427 if (btrfs_file_extent_type(leaf, fi) ==
7428 BTRFS_FILE_EXTENT_INLINE)
7429 continue;
7430 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7431 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7432 if (bytenr == 0)
7433 continue;
7434
7435 ext_index++;
7436 if (bytenr >= group->key.objectid + group->key.offset ||
7437 bytenr + num_bytes <= group->key.objectid)
7438 continue;
7439
7440 extent_key.objectid = bytenr;
7441 extent_key.offset = num_bytes;
7442 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7443 nr_extent = 1;
7444 ret = get_new_locations(reloc_inode, &extent_key,
7445 group->key.objectid, 1,
7446 &new_extent, &nr_extent);
7447 if (ret > 0)
7448 continue;
7449 BUG_ON(ret < 0);
7450
7451 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7452 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7453 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7454 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7455
Zheng Yan1a40e232008-09-26 10:09:34 -04007456 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7457 new_extent->disk_bytenr);
7458 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7459 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04007460 btrfs_mark_buffer_dirty(leaf);
7461
7462 ret = btrfs_inc_extent_ref(trans, root,
7463 new_extent->disk_bytenr,
7464 new_extent->disk_num_bytes,
7465 leaf->start,
7466 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007467 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007468 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04007469
Zheng Yan1a40e232008-09-26 10:09:34 -04007470 ret = btrfs_free_extent(trans, root,
7471 bytenr, num_bytes, leaf->start,
7472 btrfs_header_owner(leaf),
7473 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04007474 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04007475 BUG_ON(ret);
7476 cond_resched();
7477 }
7478 kfree(new_extent);
7479 BUG_ON(ext_index + 1 != ref->nritems);
7480 btrfs_free_leaf_ref(root, ref);
7481 return 0;
7482}
7483
Yan Zhengf82d02d2008-10-29 14:49:05 -04007484int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7485 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04007486{
7487 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007488 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007489
7490 if (root->reloc_root) {
7491 reloc_root = root->reloc_root;
7492 root->reloc_root = NULL;
7493 list_add(&reloc_root->dead_list,
7494 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007495
7496 btrfs_set_root_bytenr(&reloc_root->root_item,
7497 reloc_root->node->start);
7498 btrfs_set_root_level(&root->root_item,
7499 btrfs_header_level(reloc_root->node));
7500 memset(&reloc_root->root_item.drop_progress, 0,
7501 sizeof(struct btrfs_disk_key));
7502 reloc_root->root_item.drop_level = 0;
7503
7504 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7505 &reloc_root->root_key,
7506 &reloc_root->root_item);
7507 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04007508 }
7509 return 0;
7510}
7511
7512int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7513{
7514 struct btrfs_trans_handle *trans;
7515 struct btrfs_root *reloc_root;
7516 struct btrfs_root *prev_root = NULL;
7517 struct list_head dead_roots;
7518 int ret;
7519 unsigned long nr;
7520
7521 INIT_LIST_HEAD(&dead_roots);
7522 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7523
7524 while (!list_empty(&dead_roots)) {
7525 reloc_root = list_entry(dead_roots.prev,
7526 struct btrfs_root, dead_list);
7527 list_del_init(&reloc_root->dead_list);
7528
7529 BUG_ON(reloc_root->commit_root != NULL);
7530 while (1) {
7531 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007532 BUG_ON(IS_ERR(trans));
Zheng Yan1a40e232008-09-26 10:09:34 -04007533
7534 mutex_lock(&root->fs_info->drop_mutex);
7535 ret = btrfs_drop_snapshot(trans, reloc_root);
7536 if (ret != -EAGAIN)
7537 break;
7538 mutex_unlock(&root->fs_info->drop_mutex);
7539
7540 nr = trans->blocks_used;
7541 ret = btrfs_end_transaction(trans, root);
7542 BUG_ON(ret);
7543 btrfs_btree_balance_dirty(root, nr);
7544 }
7545
7546 free_extent_buffer(reloc_root->node);
7547
7548 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7549 &reloc_root->root_key);
7550 BUG_ON(ret);
7551 mutex_unlock(&root->fs_info->drop_mutex);
7552
7553 nr = trans->blocks_used;
7554 ret = btrfs_end_transaction(trans, root);
7555 BUG_ON(ret);
7556 btrfs_btree_balance_dirty(root, nr);
7557
7558 kfree(prev_root);
7559 prev_root = reloc_root;
7560 }
7561 if (prev_root) {
7562 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7563 kfree(prev_root);
7564 }
7565 return 0;
7566}
7567
7568int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7569{
7570 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7571 return 0;
7572}
7573
7574int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7575{
7576 struct btrfs_root *reloc_root;
7577 struct btrfs_trans_handle *trans;
7578 struct btrfs_key location;
7579 int found;
7580 int ret;
7581
7582 mutex_lock(&root->fs_info->tree_reloc_mutex);
7583 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7584 BUG_ON(ret);
7585 found = !list_empty(&root->fs_info->dead_reloc_roots);
7586 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7587
7588 if (found) {
7589 trans = btrfs_start_transaction(root, 1);
7590 BUG_ON(!trans);
7591 ret = btrfs_commit_transaction(trans, root);
7592 BUG_ON(ret);
7593 }
7594
7595 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7596 location.offset = (u64)-1;
7597 location.type = BTRFS_ROOT_ITEM_KEY;
7598
7599 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7600 BUG_ON(!reloc_root);
7601 btrfs_orphan_cleanup(reloc_root);
7602 return 0;
7603}
7604
Chris Masond3977122009-01-05 21:25:51 -05007605static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007606 struct btrfs_root *root)
7607{
7608 struct btrfs_root *reloc_root;
7609 struct extent_buffer *eb;
7610 struct btrfs_root_item *root_item;
7611 struct btrfs_key root_key;
7612 int ret;
7613
7614 BUG_ON(!root->ref_cows);
7615 if (root->reloc_root)
7616 return 0;
7617
7618 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7619 BUG_ON(!root_item);
7620
7621 ret = btrfs_copy_root(trans, root, root->commit_root,
7622 &eb, BTRFS_TREE_RELOC_OBJECTID);
7623 BUG_ON(ret);
7624
7625 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7626 root_key.offset = root->root_key.objectid;
7627 root_key.type = BTRFS_ROOT_ITEM_KEY;
7628
7629 memcpy(root_item, &root->root_item, sizeof(root_item));
7630 btrfs_set_root_refs(root_item, 0);
7631 btrfs_set_root_bytenr(root_item, eb->start);
7632 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04007633 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04007634
7635 btrfs_tree_unlock(eb);
7636 free_extent_buffer(eb);
7637
7638 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7639 &root_key, root_item);
7640 BUG_ON(ret);
7641 kfree(root_item);
7642
7643 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7644 &root_key);
7645 BUG_ON(!reloc_root);
7646 reloc_root->last_trans = trans->transid;
7647 reloc_root->commit_root = NULL;
7648 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7649
7650 root->reloc_root = reloc_root;
7651 return 0;
7652}
7653
7654/*
7655 * Core function of space balance.
7656 *
7657 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04007658 * counted roots. There is one reloc tree for each subvol, and all
7659 * reloc trees share same root key objectid. Reloc trees are snapshots
7660 * of the latest committed roots of subvols (root->commit_root).
7661 *
7662 * To relocate a tree block referenced by a subvol, there are two steps.
7663 * COW the block through subvol's reloc tree, then update block pointer
7664 * in the subvol to point to the new block. Since all reloc trees share
7665 * same root key objectid, doing special handing for tree blocks owned
7666 * by them is easy. Once a tree block has been COWed in one reloc tree,
7667 * we can use the resulting new block directly when the same block is
7668 * required to COW again through other reloc trees. By this way, relocated
7669 * tree blocks are shared between reloc trees, so they are also shared
7670 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04007671 */
Chris Masond3977122009-01-05 21:25:51 -05007672static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007673 struct btrfs_root *root,
7674 struct btrfs_path *path,
7675 struct btrfs_key *first_key,
7676 struct btrfs_ref_path *ref_path,
7677 struct btrfs_block_group_cache *group,
7678 struct inode *reloc_inode)
7679{
7680 struct btrfs_root *reloc_root;
7681 struct extent_buffer *eb = NULL;
7682 struct btrfs_key *keys;
7683 u64 *nodes;
7684 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007685 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04007686 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007687 int ret;
7688
7689 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7690 lowest_level = ref_path->owner_objectid;
7691
Yan Zhengf82d02d2008-10-29 14:49:05 -04007692 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04007693 path->lowest_level = lowest_level;
7694 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7695 BUG_ON(ret < 0);
7696 path->lowest_level = 0;
7697 btrfs_release_path(root, path);
7698 return 0;
7699 }
7700
Zheng Yan1a40e232008-09-26 10:09:34 -04007701 mutex_lock(&root->fs_info->tree_reloc_mutex);
7702 ret = init_reloc_tree(trans, root);
7703 BUG_ON(ret);
7704 reloc_root = root->reloc_root;
7705
Yan Zhengf82d02d2008-10-29 14:49:05 -04007706 shared_level = ref_path->shared_level;
7707 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007708
Yan Zhengf82d02d2008-10-29 14:49:05 -04007709 keys = ref_path->node_keys;
7710 nodes = ref_path->new_nodes;
7711 memset(&keys[shared_level + 1], 0,
7712 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7713 memset(&nodes[shared_level + 1], 0,
7714 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04007715
Yan Zhengf82d02d2008-10-29 14:49:05 -04007716 if (nodes[lowest_level] == 0) {
7717 path->lowest_level = lowest_level;
7718 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7719 0, 1);
7720 BUG_ON(ret);
7721 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7722 eb = path->nodes[level];
7723 if (!eb || eb == reloc_root->node)
7724 break;
7725 nodes[level] = eb->start;
7726 if (level == 0)
7727 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7728 else
7729 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7730 }
Yan Zheng2b820322008-11-17 21:11:30 -05007731 if (nodes[0] &&
7732 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007733 eb = path->nodes[0];
7734 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7735 group, reloc_inode);
7736 BUG_ON(ret);
7737 }
7738 btrfs_release_path(reloc_root, path);
7739 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007740 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007741 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04007742 BUG_ON(ret);
7743 }
7744
Zheng Yan1a40e232008-09-26 10:09:34 -04007745 /*
7746 * replace tree blocks in the fs tree with tree blocks in
7747 * the reloc tree.
7748 */
7749 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7750 BUG_ON(ret < 0);
7751
7752 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007753 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7754 0, 0);
7755 BUG_ON(ret);
7756 extent_buffer_get(path->nodes[0]);
7757 eb = path->nodes[0];
7758 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007759 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7760 BUG_ON(ret);
7761 free_extent_buffer(eb);
7762 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007763
Yan Zhengf82d02d2008-10-29 14:49:05 -04007764 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04007765 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04007766 return 0;
7767}
7768
Chris Masond3977122009-01-05 21:25:51 -05007769static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007770 struct btrfs_root *root,
7771 struct btrfs_path *path,
7772 struct btrfs_key *first_key,
7773 struct btrfs_ref_path *ref_path)
7774{
7775 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007776
7777 ret = relocate_one_path(trans, root, path, first_key,
7778 ref_path, NULL, NULL);
7779 BUG_ON(ret);
7780
Zheng Yan1a40e232008-09-26 10:09:34 -04007781 return 0;
7782}
7783
Chris Masond3977122009-01-05 21:25:51 -05007784static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007785 struct btrfs_root *extent_root,
7786 struct btrfs_path *path,
7787 struct btrfs_key *extent_key)
7788{
7789 int ret;
7790
Zheng Yan1a40e232008-09-26 10:09:34 -04007791 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7792 if (ret)
7793 goto out;
7794 ret = btrfs_del_item(trans, extent_root, path);
7795out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007796 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007797 return ret;
7798}
7799
Chris Masond3977122009-01-05 21:25:51 -05007800static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007801 struct btrfs_ref_path *ref_path)
7802{
7803 struct btrfs_key root_key;
7804
7805 root_key.objectid = ref_path->root_objectid;
7806 root_key.type = BTRFS_ROOT_ITEM_KEY;
7807 if (is_cowonly_root(ref_path->root_objectid))
7808 root_key.offset = 0;
7809 else
7810 root_key.offset = (u64)-1;
7811
7812 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7813}
7814
Chris Masond3977122009-01-05 21:25:51 -05007815static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007816 struct btrfs_path *path,
7817 struct btrfs_key *extent_key,
7818 struct btrfs_block_group_cache *group,
7819 struct inode *reloc_inode, int pass)
7820{
7821 struct btrfs_trans_handle *trans;
7822 struct btrfs_root *found_root;
7823 struct btrfs_ref_path *ref_path = NULL;
7824 struct disk_extent *new_extents = NULL;
7825 int nr_extents = 0;
7826 int loops;
7827 int ret;
7828 int level;
7829 struct btrfs_key first_key;
7830 u64 prev_block = 0;
7831
Zheng Yan1a40e232008-09-26 10:09:34 -04007832
7833 trans = btrfs_start_transaction(extent_root, 1);
7834 BUG_ON(!trans);
7835
7836 if (extent_key->objectid == 0) {
7837 ret = del_extent_zero(trans, extent_root, path, extent_key);
7838 goto out;
7839 }
7840
7841 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7842 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007843 ret = -ENOMEM;
7844 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007845 }
7846
7847 for (loops = 0; ; loops++) {
7848 if (loops == 0) {
7849 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7850 extent_key->objectid);
7851 } else {
7852 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7853 }
7854 if (ret < 0)
7855 goto out;
7856 if (ret > 0)
7857 break;
7858
7859 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7860 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7861 continue;
7862
7863 found_root = read_ref_root(extent_root->fs_info, ref_path);
7864 BUG_ON(!found_root);
7865 /*
7866 * for reference counted tree, only process reference paths
7867 * rooted at the latest committed root.
7868 */
7869 if (found_root->ref_cows &&
7870 ref_path->root_generation != found_root->root_key.offset)
7871 continue;
7872
7873 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7874 if (pass == 0) {
7875 /*
7876 * copy data extents to new locations
7877 */
7878 u64 group_start = group->key.objectid;
7879 ret = relocate_data_extent(reloc_inode,
7880 extent_key,
7881 group_start);
7882 if (ret < 0)
7883 goto out;
7884 break;
7885 }
7886 level = 0;
7887 } else {
7888 level = ref_path->owner_objectid;
7889 }
7890
7891 if (prev_block != ref_path->nodes[level]) {
7892 struct extent_buffer *eb;
7893 u64 block_start = ref_path->nodes[level];
7894 u64 block_size = btrfs_level_size(found_root, level);
7895
7896 eb = read_tree_block(found_root, block_start,
7897 block_size, 0);
7898 btrfs_tree_lock(eb);
7899 BUG_ON(level != btrfs_header_level(eb));
7900
7901 if (level == 0)
7902 btrfs_item_key_to_cpu(eb, &first_key, 0);
7903 else
7904 btrfs_node_key_to_cpu(eb, &first_key, 0);
7905
7906 btrfs_tree_unlock(eb);
7907 free_extent_buffer(eb);
7908 prev_block = block_start;
7909 }
7910
Yan Zheng24562422009-02-12 14:14:53 -05007911 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007912 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05007913 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007914 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7915 /*
7916 * try to update data extent references while
7917 * keeping metadata shared between snapshots.
7918 */
7919 if (pass == 1) {
7920 ret = relocate_one_path(trans, found_root,
7921 path, &first_key, ref_path,
7922 group, reloc_inode);
7923 if (ret < 0)
7924 goto out;
7925 continue;
7926 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007927 /*
7928 * use fallback method to process the remaining
7929 * references.
7930 */
7931 if (!new_extents) {
7932 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04007933 new_extents = kmalloc(sizeof(*new_extents),
7934 GFP_NOFS);
7935 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007936 ret = get_new_locations(reloc_inode,
7937 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04007938 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04007939 &new_extents,
7940 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007941 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04007942 goto out;
7943 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007944 ret = replace_one_extent(trans, found_root,
7945 path, extent_key,
7946 &first_key, ref_path,
7947 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05007948 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007949 ret = relocate_tree_block(trans, found_root, path,
7950 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007951 }
7952 if (ret < 0)
7953 goto out;
7954 }
7955 ret = 0;
7956out:
7957 btrfs_end_transaction(trans, extent_root);
7958 kfree(new_extents);
7959 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007960 return ret;
7961}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007962#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007963
Chris Masonec44a352008-04-28 15:29:52 -04007964static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7965{
7966 u64 num_devices;
7967 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7968 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7969
Chris Masoncd02dca2010-12-13 14:56:23 -05007970 /*
7971 * we add in the count of missing devices because we want
7972 * to make sure that any RAID levels on a degraded FS
7973 * continue to be honored.
7974 */
7975 num_devices = root->fs_info->fs_devices->rw_devices +
7976 root->fs_info->fs_devices->missing_devices;
7977
Chris Masonec44a352008-04-28 15:29:52 -04007978 if (num_devices == 1) {
7979 stripped |= BTRFS_BLOCK_GROUP_DUP;
7980 stripped = flags & ~stripped;
7981
7982 /* turn raid0 into single device chunks */
7983 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7984 return stripped;
7985
7986 /* turn mirroring into duplication */
7987 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7988 BTRFS_BLOCK_GROUP_RAID10))
7989 return stripped | BTRFS_BLOCK_GROUP_DUP;
7990 return flags;
7991 } else {
7992 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007993 if (flags & stripped)
7994 return flags;
7995
7996 stripped |= BTRFS_BLOCK_GROUP_DUP;
7997 stripped = flags & ~stripped;
7998
7999 /* switch duplicated blocks with raid1 */
8000 if (flags & BTRFS_BLOCK_GROUP_DUP)
8001 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8002
8003 /* turn single device chunks into raid0 */
8004 return stripped | BTRFS_BLOCK_GROUP_RAID0;
8005 }
8006 return flags;
8007}
8008
Yan, Zhengf0486c62010-05-16 10:46:25 -04008009static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04008010{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008011 struct btrfs_space_info *sinfo = cache->space_info;
8012 u64 num_bytes;
8013 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04008014
Yan, Zhengf0486c62010-05-16 10:46:25 -04008015 if (cache->ro)
8016 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04008017
Yan, Zhengf0486c62010-05-16 10:46:25 -04008018 spin_lock(&sinfo->lock);
8019 spin_lock(&cache->lock);
8020 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8021 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04008022
Yan, Zhengf0486c62010-05-16 10:46:25 -04008023 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
8024 sinfo->bytes_may_use + sinfo->bytes_readonly +
Chris Mason65e53412010-12-24 06:41:52 -05008025 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04008026 sinfo->bytes_readonly += num_bytes;
8027 sinfo->bytes_reserved += cache->reserved_pinned;
8028 cache->reserved_pinned = 0;
8029 cache->ro = 1;
8030 ret = 0;
8031 }
Chris Mason65e53412010-12-24 06:41:52 -05008032
Yan, Zhengf0486c62010-05-16 10:46:25 -04008033 spin_unlock(&cache->lock);
8034 spin_unlock(&sinfo->lock);
8035 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04008036}
8037
Yan, Zhengf0486c62010-05-16 10:46:25 -04008038int btrfs_set_block_group_ro(struct btrfs_root *root,
8039 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008040
8041{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008042 struct btrfs_trans_handle *trans;
8043 u64 alloc_flags;
8044 int ret;
8045
8046 BUG_ON(cache->ro);
8047
8048 trans = btrfs_join_transaction(root, 1);
8049 BUG_ON(IS_ERR(trans));
8050
8051 alloc_flags = update_block_group_flags(root, cache->flags);
8052 if (alloc_flags != cache->flags)
8053 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
8054
8055 ret = set_block_group_ro(cache);
8056 if (!ret)
8057 goto out;
8058 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
8059 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
8060 if (ret < 0)
8061 goto out;
8062 ret = set_block_group_ro(cache);
8063out:
8064 btrfs_end_transaction(trans, root);
8065 return ret;
8066}
8067
Miao Xie6d07bce2011-01-05 10:07:31 +00008068/*
8069 * helper to account the unused space of all the readonly block group in the
8070 * list. takes mirrors into account.
8071 */
8072static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8073{
8074 struct btrfs_block_group_cache *block_group;
8075 u64 free_bytes = 0;
8076 int factor;
8077
8078 list_for_each_entry(block_group, groups_list, list) {
8079 spin_lock(&block_group->lock);
8080
8081 if (!block_group->ro) {
8082 spin_unlock(&block_group->lock);
8083 continue;
8084 }
8085
8086 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8087 BTRFS_BLOCK_GROUP_RAID10 |
8088 BTRFS_BLOCK_GROUP_DUP))
8089 factor = 2;
8090 else
8091 factor = 1;
8092
8093 free_bytes += (block_group->key.offset -
8094 btrfs_block_group_used(&block_group->item)) *
8095 factor;
8096
8097 spin_unlock(&block_group->lock);
8098 }
8099
8100 return free_bytes;
8101}
8102
8103/*
8104 * helper to account the unused space of all the readonly block group in the
8105 * space_info. takes mirrors into account.
8106 */
8107u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8108{
8109 int i;
8110 u64 free_bytes = 0;
8111
8112 spin_lock(&sinfo->lock);
8113
8114 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
8115 if (!list_empty(&sinfo->block_groups[i]))
8116 free_bytes += __btrfs_get_ro_block_group_free_space(
8117 &sinfo->block_groups[i]);
8118
8119 spin_unlock(&sinfo->lock);
8120
8121 return free_bytes;
8122}
8123
Yan, Zhengf0486c62010-05-16 10:46:25 -04008124int btrfs_set_block_group_rw(struct btrfs_root *root,
8125 struct btrfs_block_group_cache *cache)
8126{
8127 struct btrfs_space_info *sinfo = cache->space_info;
8128 u64 num_bytes;
8129
8130 BUG_ON(!cache->ro);
8131
8132 spin_lock(&sinfo->lock);
8133 spin_lock(&cache->lock);
8134 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8135 cache->bytes_super - btrfs_block_group_used(&cache->item);
8136 sinfo->bytes_readonly -= num_bytes;
8137 cache->ro = 0;
8138 spin_unlock(&cache->lock);
8139 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008140 return 0;
8141}
8142
Josef Bacikba1bf482009-09-11 16:11:19 -04008143/*
8144 * checks to see if its even possible to relocate this block group.
8145 *
8146 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8147 * ok to go ahead and try.
8148 */
8149int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008150{
Zheng Yan1a40e232008-09-26 10:09:34 -04008151 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008152 struct btrfs_space_info *space_info;
8153 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8154 struct btrfs_device *device;
8155 int full = 0;
8156 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008157
Josef Bacikba1bf482009-09-11 16:11:19 -04008158 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008159
Josef Bacikba1bf482009-09-11 16:11:19 -04008160 /* odd, couldn't find the block group, leave it alone */
8161 if (!block_group)
8162 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008163
Josef Bacikba1bf482009-09-11 16:11:19 -04008164 /* no bytes used, we're good */
8165 if (!btrfs_block_group_used(&block_group->item))
8166 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008167
Josef Bacikba1bf482009-09-11 16:11:19 -04008168 space_info = block_group->space_info;
8169 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008170
Josef Bacikba1bf482009-09-11 16:11:19 -04008171 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008172
Josef Bacikba1bf482009-09-11 16:11:19 -04008173 /*
8174 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008175 * relocate it unless we're able to allocate a new chunk below.
8176 *
8177 * Otherwise, we need to make sure we have room in the space to handle
8178 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008179 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008180 if ((space_info->total_bytes != block_group->key.offset) &&
8181 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04008182 space_info->bytes_pinned + space_info->bytes_readonly +
8183 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04008184 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008185 spin_unlock(&space_info->lock);
8186 goto out;
8187 }
8188 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008189
Josef Bacikba1bf482009-09-11 16:11:19 -04008190 /*
8191 * ok we don't have enough space, but maybe we have free space on our
8192 * devices to allocate new chunks for relocation, so loop through our
8193 * alloc devices and guess if we have enough space. However, if we
8194 * were marked as full, then we know there aren't enough chunks, and we
8195 * can just return.
8196 */
8197 ret = -1;
8198 if (full)
8199 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05008200
Josef Bacikba1bf482009-09-11 16:11:19 -04008201 mutex_lock(&root->fs_info->chunk_mutex);
8202 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8203 u64 min_free = btrfs_block_group_used(&block_group->item);
Miao Xie7bfc8372011-01-05 10:07:26 +00008204 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008205
Josef Bacikba1bf482009-09-11 16:11:19 -04008206 /*
8207 * check to make sure we can actually find a chunk with enough
8208 * space to fit our block group in.
8209 */
8210 if (device->total_bytes > device->bytes_used + min_free) {
8211 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008212 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008213 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05008214 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04008215 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008216 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008217 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008218 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05008219out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008220 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008221 return ret;
8222}
8223
Christoph Hellwigb2950862008-12-02 09:54:17 -05008224static int find_first_block_group(struct btrfs_root *root,
8225 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008226{
Chris Mason925baed2008-06-25 16:01:30 -04008227 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008228 struct btrfs_key found_key;
8229 struct extent_buffer *leaf;
8230 int slot;
8231
8232 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8233 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008234 goto out;
8235
Chris Masond3977122009-01-05 21:25:51 -05008236 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008237 slot = path->slots[0];
8238 leaf = path->nodes[0];
8239 if (slot >= btrfs_header_nritems(leaf)) {
8240 ret = btrfs_next_leaf(root, path);
8241 if (ret == 0)
8242 continue;
8243 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008244 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008245 break;
8246 }
8247 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8248
8249 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008250 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8251 ret = 0;
8252 goto out;
8253 }
Chris Mason0b86a832008-03-24 15:01:56 -04008254 path->slots[0]++;
8255 }
Chris Mason925baed2008-06-25 16:01:30 -04008256out:
Chris Mason0b86a832008-03-24 15:01:56 -04008257 return ret;
8258}
8259
Josef Bacik0af3d002010-06-21 14:48:16 -04008260void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8261{
8262 struct btrfs_block_group_cache *block_group;
8263 u64 last = 0;
8264
8265 while (1) {
8266 struct inode *inode;
8267
8268 block_group = btrfs_lookup_first_block_group(info, last);
8269 while (block_group) {
8270 spin_lock(&block_group->lock);
8271 if (block_group->iref)
8272 break;
8273 spin_unlock(&block_group->lock);
8274 block_group = next_block_group(info->tree_root,
8275 block_group);
8276 }
8277 if (!block_group) {
8278 if (last == 0)
8279 break;
8280 last = 0;
8281 continue;
8282 }
8283
8284 inode = block_group->inode;
8285 block_group->iref = 0;
8286 block_group->inode = NULL;
8287 spin_unlock(&block_group->lock);
8288 iput(inode);
8289 last = block_group->key.objectid + block_group->key.offset;
8290 btrfs_put_block_group(block_group);
8291 }
8292}
8293
Zheng Yan1a40e232008-09-26 10:09:34 -04008294int btrfs_free_block_groups(struct btrfs_fs_info *info)
8295{
8296 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008297 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008298 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008299 struct rb_node *n;
8300
Yan Zheng11833d62009-09-11 16:11:19 -04008301 down_write(&info->extent_commit_sem);
8302 while (!list_empty(&info->caching_block_groups)) {
8303 caching_ctl = list_entry(info->caching_block_groups.next,
8304 struct btrfs_caching_control, list);
8305 list_del(&caching_ctl->list);
8306 put_caching_control(caching_ctl);
8307 }
8308 up_write(&info->extent_commit_sem);
8309
Zheng Yan1a40e232008-09-26 10:09:34 -04008310 spin_lock(&info->block_group_cache_lock);
8311 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8312 block_group = rb_entry(n, struct btrfs_block_group_cache,
8313 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008314 rb_erase(&block_group->cache_node,
8315 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008316 spin_unlock(&info->block_group_cache_lock);
8317
Josef Bacik80eb2342008-10-29 14:49:05 -04008318 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008319 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008320 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008321
Josef Bacik817d52f2009-07-13 21:29:25 -04008322 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008323 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008324
8325 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008326 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008327
8328 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008329 }
8330 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008331
8332 /* now that all the block groups are freed, go through and
8333 * free all the space_info structs. This is only called during
8334 * the final stages of unmount, and so we know nobody is
8335 * using them. We call synchronize_rcu() once before we start,
8336 * just to be on the safe side.
8337 */
8338 synchronize_rcu();
8339
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008340 release_global_block_rsv(info);
8341
Chris Mason4184ea72009-03-10 12:39:20 -04008342 while(!list_empty(&info->space_info)) {
8343 space_info = list_entry(info->space_info.next,
8344 struct btrfs_space_info,
8345 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008346 if (space_info->bytes_pinned > 0 ||
8347 space_info->bytes_reserved > 0) {
8348 WARN_ON(1);
8349 dump_space_info(space_info, 0, 0);
8350 }
Chris Mason4184ea72009-03-10 12:39:20 -04008351 list_del(&space_info->list);
8352 kfree(space_info);
8353 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008354 return 0;
8355}
8356
Yan, Zhengb742bb822010-05-16 10:46:24 -04008357static void __link_block_group(struct btrfs_space_info *space_info,
8358 struct btrfs_block_group_cache *cache)
8359{
8360 int index = get_block_group_index(cache);
8361
8362 down_write(&space_info->groups_sem);
8363 list_add_tail(&cache->list, &space_info->block_groups[index]);
8364 up_write(&space_info->groups_sem);
8365}
8366
Chris Mason9078a3e2007-04-26 16:46:15 -04008367int btrfs_read_block_groups(struct btrfs_root *root)
8368{
8369 struct btrfs_path *path;
8370 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008371 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008372 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008373 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008374 struct btrfs_key key;
8375 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008376 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008377 int need_clear = 0;
8378 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008379
Chris Masonbe744172007-05-06 10:15:01 -04008380 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008381 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008382 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008383 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008384 path = btrfs_alloc_path();
8385 if (!path)
8386 return -ENOMEM;
8387
Josef Bacik0af3d002010-06-21 14:48:16 -04008388 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8389 if (cache_gen != 0 &&
8390 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8391 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008392 if (btrfs_test_opt(root, CLEAR_CACHE))
8393 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04008394 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
8395 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04008396
Chris Masond3977122009-01-05 21:25:51 -05008397 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008398 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb822010-05-16 10:46:24 -04008399 if (ret > 0)
8400 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008401 if (ret != 0)
8402 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008403 leaf = path->nodes[0];
8404 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008405 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008406 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008407 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008408 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008409 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008410
Yan Zhengd2fb3432008-12-11 16:30:39 -05008411 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008412 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008413 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008414 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008415 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008416 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008417
Josef Bacik0af3d002010-06-21 14:48:16 -04008418 if (need_clear)
8419 cache->disk_cache_state = BTRFS_DC_CLEAR;
8420
Josef Bacik96303082009-07-13 21:29:25 -04008421 /*
8422 * we only want to have 32k of ram per block group for keeping
8423 * track of free space, and if we pass 1/2 of that we want to
8424 * start converting things over to using bitmaps
8425 */
8426 cache->extents_thresh = ((1024 * 32) / 2) /
8427 sizeof(struct btrfs_free_space);
8428
Chris Mason5f39d392007-10-15 16:14:19 -04008429 read_extent_buffer(leaf, &cache->item,
8430 btrfs_item_ptr_offset(leaf, path->slots[0]),
8431 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008432 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008433
Chris Mason9078a3e2007-04-26 16:46:15 -04008434 key.objectid = found_key.objectid + found_key.offset;
8435 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04008436 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008437 cache->sectorsize = root->sectorsize;
8438
Josef Bacik817d52f2009-07-13 21:29:25 -04008439 /*
8440 * check for two cases, either we are full, and therefore
8441 * don't need to bother with the caching work since we won't
8442 * find any space, or we are empty, and we can just add all
8443 * the space in and be done with it. This saves us _alot_ of
8444 * time, particularly in the full case.
8445 */
8446 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04008447 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04008448 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008449 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008450 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008451 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008452 exclude_super_stripes(root, cache);
8453 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008454 cache->cached = BTRFS_CACHE_FINISHED;
8455 add_new_free_space(cache, root->fs_info,
8456 found_key.objectid,
8457 found_key.objectid +
8458 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008459 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008460 }
Chris Mason96b51792007-10-15 16:15:19 -04008461
Chris Mason6324fbf2008-03-24 15:01:59 -04008462 ret = update_space_info(info, cache->flags, found_key.offset,
8463 btrfs_block_group_used(&cache->item),
8464 &space_info);
8465 BUG_ON(ret);
8466 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008467 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008468 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008469 spin_unlock(&cache->space_info->lock);
8470
Yan, Zhengb742bb822010-05-16 10:46:24 -04008471 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008472
Josef Bacik0f9dd462008-09-23 13:14:11 -04008473 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8474 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04008475
8476 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008477 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04008478 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04008479 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04008480
8481 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8482 if (!(get_alloc_profile(root, space_info->flags) &
8483 (BTRFS_BLOCK_GROUP_RAID10 |
8484 BTRFS_BLOCK_GROUP_RAID1 |
8485 BTRFS_BLOCK_GROUP_DUP)))
8486 continue;
8487 /*
8488 * avoid allocating from un-mirrored block group if there are
8489 * mirrored block groups.
8490 */
8491 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008492 set_block_group_ro(cache);
Yan, Zhengb742bb822010-05-16 10:46:24 -04008493 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008494 set_block_group_ro(cache);
Yan, Zhengb742bb822010-05-16 10:46:24 -04008495 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008496
8497 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008498 ret = 0;
8499error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008500 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008501 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008502}
Chris Mason6324fbf2008-03-24 15:01:59 -04008503
8504int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8505 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008506 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008507 u64 size)
8508{
8509 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008510 struct btrfs_root *extent_root;
8511 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008512
8513 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008514
Chris Mason12fcfd22009-03-24 10:24:20 -04008515 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008516
Chris Mason8f18cf12008-04-25 16:53:30 -04008517 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008518 if (!cache)
8519 return -ENOMEM;
8520
Chris Masone17cade2008-04-15 15:41:47 -04008521 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008522 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008523 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008524 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008525 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04008526
8527 /*
8528 * we only want to have 32k of ram per block group for keeping track
8529 * of free space, and if we pass 1/2 of that we want to start
8530 * converting things over to using bitmaps
8531 */
8532 cache->extents_thresh = ((1024 * 32) / 2) /
8533 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008534 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008535 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04008536 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008537 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008538 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008539
Chris Mason6324fbf2008-03-24 15:01:59 -04008540 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008541 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8542 cache->flags = type;
8543 btrfs_set_block_group_flags(&cache->item, type);
8544
Yan Zheng11833d62009-09-11 16:11:19 -04008545 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008546 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04008547 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04008548
Josef Bacik817d52f2009-07-13 21:29:25 -04008549 add_new_free_space(cache, root->fs_info, chunk_offset,
8550 chunk_offset + size);
8551
Yan Zheng11833d62009-09-11 16:11:19 -04008552 free_excluded_extents(root, cache);
8553
Chris Mason6324fbf2008-03-24 15:01:59 -04008554 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8555 &cache->space_info);
8556 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04008557
8558 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008559 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008560 spin_unlock(&cache->space_info->lock);
8561
Yan, Zhengb742bb822010-05-16 10:46:24 -04008562 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008563
Josef Bacik0f9dd462008-09-23 13:14:11 -04008564 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8565 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04008566
Chris Mason6324fbf2008-03-24 15:01:59 -04008567 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8568 sizeof(cache->item));
8569 BUG_ON(ret);
8570
Chris Masond18a2c42008-04-04 15:40:00 -04008571 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008572
Chris Mason6324fbf2008-03-24 15:01:59 -04008573 return 0;
8574}
Zheng Yan1a40e232008-09-26 10:09:34 -04008575
8576int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8577 struct btrfs_root *root, u64 group_start)
8578{
8579 struct btrfs_path *path;
8580 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008581 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008582 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008583 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008584 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008585 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04008586 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008587
Zheng Yan1a40e232008-09-26 10:09:34 -04008588 root = root->fs_info->extent_root;
8589
8590 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8591 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008592 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008593
8594 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04008595 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8596 BTRFS_BLOCK_GROUP_RAID1 |
8597 BTRFS_BLOCK_GROUP_RAID10))
8598 factor = 2;
8599 else
8600 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008601
Chris Mason44fb5512009-06-04 15:34:51 -04008602 /* make sure this block group isn't part of an allocation cluster */
8603 cluster = &root->fs_info->data_alloc_cluster;
8604 spin_lock(&cluster->refill_lock);
8605 btrfs_return_cluster_to_free_space(block_group, cluster);
8606 spin_unlock(&cluster->refill_lock);
8607
8608 /*
8609 * make sure this block group isn't part of a metadata
8610 * allocation cluster
8611 */
8612 cluster = &root->fs_info->meta_alloc_cluster;
8613 spin_lock(&cluster->refill_lock);
8614 btrfs_return_cluster_to_free_space(block_group, cluster);
8615 spin_unlock(&cluster->refill_lock);
8616
Zheng Yan1a40e232008-09-26 10:09:34 -04008617 path = btrfs_alloc_path();
8618 BUG_ON(!path);
8619
Josef Bacik0af3d002010-06-21 14:48:16 -04008620 inode = lookup_free_space_inode(root, block_group, path);
8621 if (!IS_ERR(inode)) {
8622 btrfs_orphan_add(trans, inode);
8623 clear_nlink(inode);
8624 /* One for the block groups ref */
8625 spin_lock(&block_group->lock);
8626 if (block_group->iref) {
8627 block_group->iref = 0;
8628 block_group->inode = NULL;
8629 spin_unlock(&block_group->lock);
8630 iput(inode);
8631 } else {
8632 spin_unlock(&block_group->lock);
8633 }
8634 /* One for our lookup ref */
8635 iput(inode);
8636 }
8637
8638 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8639 key.offset = block_group->key.objectid;
8640 key.type = 0;
8641
8642 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8643 if (ret < 0)
8644 goto out;
8645 if (ret > 0)
8646 btrfs_release_path(tree_root, path);
8647 if (ret == 0) {
8648 ret = btrfs_del_item(trans, tree_root, path);
8649 if (ret)
8650 goto out;
8651 btrfs_release_path(tree_root, path);
8652 }
8653
Yan Zheng3dfdb932009-01-21 10:49:16 -05008654 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008655 rb_erase(&block_group->cache_node,
8656 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008657 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008658
Josef Bacik80eb2342008-10-29 14:49:05 -04008659 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008660 /*
8661 * we must use list_del_init so people can check to see if they
8662 * are still on the list after taking the semaphore
8663 */
8664 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008665 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008666
Josef Bacik817d52f2009-07-13 21:29:25 -04008667 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008668 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008669
8670 btrfs_remove_free_space_cache(block_group);
8671
Yan Zhengc146afa2008-11-12 14:34:12 -05008672 spin_lock(&block_group->space_info->lock);
8673 block_group->space_info->total_bytes -= block_group->key.offset;
8674 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008675 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008676 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008677
Josef Bacik0af3d002010-06-21 14:48:16 -04008678 memcpy(&key, &block_group->key, sizeof(key));
8679
Chris Mason283bb192009-07-24 16:30:55 -04008680 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008681
Chris Masonfa9c0d792009-04-03 09:47:43 -04008682 btrfs_put_block_group(block_group);
8683 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008684
8685 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8686 if (ret > 0)
8687 ret = -EIO;
8688 if (ret < 0)
8689 goto out;
8690
8691 ret = btrfs_del_item(trans, root, path);
8692out:
8693 btrfs_free_path(path);
8694 return ret;
8695}
liuboacce9522011-01-06 19:30:25 +08008696
8697int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8698{
8699 return unpin_extent_range(root, start, end);
8700}
8701
8702int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
8703 u64 num_bytes)
8704{
8705 return btrfs_discard_extent(root, bytenr, num_bytes);
8706}