blob: 786899da3eb9a79a495a2b95be042c506355f521 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050025#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050026#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027#include "ctree.h"
28#include "disk-io.h"
29#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040030#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040032#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040033#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Josef Bacikf3465ca2008-11-12 14:19:50 -050035static int update_block_group(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 u64 bytenr, u64 num_bytes, int alloc,
38 int mark_free);
Yan Zheng11833d62009-09-11 16:11:19 -040039static int update_reserved_extents(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve);
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 pin_down_bytes(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root,
65 struct btrfs_path *path,
66 u64 bytenr, u64 num_bytes,
67 int is_data, int reserved,
68 struct extent_buffer **must_clean);
69static int find_next_key(struct btrfs_path *path, int level,
70 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040071static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
72 int dump_block_groups);
Josef Bacik6a632092009-02-20 11:00:09 -050073
Josef Bacik817d52f2009-07-13 21:29:25 -040074static noinline int
75block_group_cache_done(struct btrfs_block_group_cache *cache)
76{
77 smp_mb();
78 return cache->cached == BTRFS_CACHE_FINISHED;
79}
80
Josef Bacik0f9dd462008-09-23 13:14:11 -040081static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
82{
83 return (cache->flags & bits) == bits;
84}
85
Josef Bacik11dfe352009-11-13 20:12:59 +000086void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
87{
88 atomic_inc(&cache->count);
89}
90
91void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
92{
93 if (atomic_dec_and_test(&cache->count))
94 kfree(cache);
95}
96
Josef Bacik0f9dd462008-09-23 13:14:11 -040097/*
98 * this adds the block group to the fs_info rb tree for the block group
99 * cache
100 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500101static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400102 struct btrfs_block_group_cache *block_group)
103{
104 struct rb_node **p;
105 struct rb_node *parent = NULL;
106 struct btrfs_block_group_cache *cache;
107
108 spin_lock(&info->block_group_cache_lock);
109 p = &info->block_group_cache_tree.rb_node;
110
111 while (*p) {
112 parent = *p;
113 cache = rb_entry(parent, struct btrfs_block_group_cache,
114 cache_node);
115 if (block_group->key.objectid < cache->key.objectid) {
116 p = &(*p)->rb_left;
117 } else if (block_group->key.objectid > cache->key.objectid) {
118 p = &(*p)->rb_right;
119 } else {
120 spin_unlock(&info->block_group_cache_lock);
121 return -EEXIST;
122 }
123 }
124
125 rb_link_node(&block_group->cache_node, parent, p);
126 rb_insert_color(&block_group->cache_node,
127 &info->block_group_cache_tree);
128 spin_unlock(&info->block_group_cache_lock);
129
130 return 0;
131}
132
133/*
134 * This will return the block group at or after bytenr if contains is 0, else
135 * it will return the block group that contains the bytenr
136 */
137static struct btrfs_block_group_cache *
138block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
139 int contains)
140{
141 struct btrfs_block_group_cache *cache, *ret = NULL;
142 struct rb_node *n;
143 u64 end, start;
144
145 spin_lock(&info->block_group_cache_lock);
146 n = info->block_group_cache_tree.rb_node;
147
148 while (n) {
149 cache = rb_entry(n, struct btrfs_block_group_cache,
150 cache_node);
151 end = cache->key.objectid + cache->key.offset - 1;
152 start = cache->key.objectid;
153
154 if (bytenr < start) {
155 if (!contains && (!ret || start < ret->key.objectid))
156 ret = cache;
157 n = n->rb_left;
158 } else if (bytenr > start) {
159 if (contains && bytenr <= end) {
160 ret = cache;
161 break;
162 }
163 n = n->rb_right;
164 } else {
165 ret = cache;
166 break;
167 }
168 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500169 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000170 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400171 spin_unlock(&info->block_group_cache_lock);
172
173 return ret;
174}
175
Yan Zheng11833d62009-09-11 16:11:19 -0400176static int add_excluded_extent(struct btrfs_root *root,
177 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400178{
Yan Zheng11833d62009-09-11 16:11:19 -0400179 u64 end = start + num_bytes - 1;
180 set_extent_bits(&root->fs_info->freed_extents[0],
181 start, end, EXTENT_UPTODATE, GFP_NOFS);
182 set_extent_bits(&root->fs_info->freed_extents[1],
183 start, end, EXTENT_UPTODATE, GFP_NOFS);
184 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400185}
186
Yan Zheng11833d62009-09-11 16:11:19 -0400187static void free_excluded_extents(struct btrfs_root *root,
188 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400189{
Yan Zheng11833d62009-09-11 16:11:19 -0400190 u64 start, end;
191
192 start = cache->key.objectid;
193 end = start + cache->key.offset - 1;
194
195 clear_extent_bits(&root->fs_info->freed_extents[0],
196 start, end, EXTENT_UPTODATE, GFP_NOFS);
197 clear_extent_bits(&root->fs_info->freed_extents[1],
198 start, end, EXTENT_UPTODATE, GFP_NOFS);
199}
200
201static int exclude_super_stripes(struct btrfs_root *root,
202 struct btrfs_block_group_cache *cache)
203{
Josef Bacik817d52f2009-07-13 21:29:25 -0400204 u64 bytenr;
205 u64 *logical;
206 int stripe_len;
207 int i, nr, ret;
208
Yan, Zheng06b23312009-11-26 09:31:11 +0000209 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
210 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
211 cache->bytes_super += stripe_len;
212 ret = add_excluded_extent(root, cache->key.objectid,
213 stripe_len);
214 BUG_ON(ret);
215 }
216
Josef Bacik817d52f2009-07-13 21:29:25 -0400217 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
218 bytenr = btrfs_sb_offset(i);
219 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
220 cache->key.objectid, bytenr,
221 0, &logical, &nr, &stripe_len);
222 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400223
Josef Bacik817d52f2009-07-13 21:29:25 -0400224 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400225 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400226 ret = add_excluded_extent(root, logical[nr],
227 stripe_len);
228 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400229 }
Yan Zheng11833d62009-09-11 16:11:19 -0400230
Josef Bacik817d52f2009-07-13 21:29:25 -0400231 kfree(logical);
232 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400233 return 0;
234}
235
Yan Zheng11833d62009-09-11 16:11:19 -0400236static struct btrfs_caching_control *
237get_caching_control(struct btrfs_block_group_cache *cache)
238{
239 struct btrfs_caching_control *ctl;
240
241 spin_lock(&cache->lock);
242 if (cache->cached != BTRFS_CACHE_STARTED) {
243 spin_unlock(&cache->lock);
244 return NULL;
245 }
246
247 ctl = cache->caching_ctl;
248 atomic_inc(&ctl->count);
249 spin_unlock(&cache->lock);
250 return ctl;
251}
252
253static void put_caching_control(struct btrfs_caching_control *ctl)
254{
255 if (atomic_dec_and_test(&ctl->count))
256 kfree(ctl);
257}
258
Josef Bacik0f9dd462008-09-23 13:14:11 -0400259/*
260 * this is only called by cache_block_group, since we could have freed extents
261 * we need to check the pinned_extents for any extents that can't be used yet
262 * since their free space will be released as soon as the transaction commits.
263 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400264static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265 struct btrfs_fs_info *info, u64 start, u64 end)
266{
Josef Bacik817d52f2009-07-13 21:29:25 -0400267 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400268 int ret;
269
270 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400271 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400272 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400273 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400274 if (ret)
275 break;
276
Yan, Zheng06b23312009-11-26 09:31:11 +0000277 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400278 start = extent_end + 1;
279 } else if (extent_start > start && extent_start < end) {
280 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400281 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500282 ret = btrfs_add_free_space(block_group, start,
283 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400284 BUG_ON(ret);
285 start = extent_end + 1;
286 } else {
287 break;
288 }
289 }
290
291 if (start < end) {
292 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400293 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500294 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400295 BUG_ON(ret);
296 }
297
Josef Bacik817d52f2009-07-13 21:29:25 -0400298 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400299}
300
Josef Bacik817d52f2009-07-13 21:29:25 -0400301static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400302{
Josef Bacik817d52f2009-07-13 21:29:25 -0400303 struct btrfs_block_group_cache *block_group = data;
304 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400305 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
306 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400307 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400308 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400309 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400310 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400311 u64 last = 0;
312 u32 nritems;
313 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400314
Chris Masone37c9e62007-05-09 20:13:14 -0400315 path = btrfs_alloc_path();
316 if (!path)
317 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400318
Yan Zheng11833d62009-09-11 16:11:19 -0400319 exclude_super_stripes(extent_root, block_group);
Josef Bacik1b2da372009-09-11 16:11:20 -0400320 spin_lock(&block_group->space_info->lock);
321 block_group->space_info->bytes_super += block_group->bytes_super;
322 spin_unlock(&block_group->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400323
Josef Bacik817d52f2009-07-13 21:29:25 -0400324 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400325
Chris Mason5cd57b22008-06-25 16:01:30 -0400326 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400327 * We don't want to deadlock with somebody trying to allocate a new
328 * extent for the extent root while also trying to search the extent
329 * root to add free space. So we skip locking and search the commit
330 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400331 */
332 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400333 path->search_commit_root = 1;
334 path->reada = 2;
335
Yan Zhenge4404d62008-12-12 10:03:26 -0500336 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400337 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400338 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400339again:
Yan Zheng11833d62009-09-11 16:11:19 -0400340 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400341 /* need to make sure the commit_root doesn't disappear */
342 down_read(&fs_info->extent_commit_sem);
343
Yan Zheng11833d62009-09-11 16:11:19 -0400344 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400345 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400346 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500347
Yan Zheng11833d62009-09-11 16:11:19 -0400348 leaf = path->nodes[0];
349 nritems = btrfs_header_nritems(leaf);
350
Chris Masond3977122009-01-05 21:25:51 -0500351 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400352 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400353 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400354 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400355 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400356 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400357
Yan Zheng11833d62009-09-11 16:11:19 -0400358 if (path->slots[0] < nritems) {
359 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
360 } else {
361 ret = find_next_key(path, 0, &key);
362 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400363 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400364
Yan Zheng11833d62009-09-11 16:11:19 -0400365 caching_ctl->progress = last;
366 btrfs_release_path(extent_root, path);
367 up_read(&fs_info->extent_commit_sem);
368 mutex_unlock(&caching_ctl->mutex);
369 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400370 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400371 else
372 cond_resched();
373 goto again;
374 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400375
Yan Zheng11833d62009-09-11 16:11:19 -0400376 if (key.objectid < block_group->key.objectid) {
377 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400378 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400379 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400380
Chris Masone37c9e62007-05-09 20:13:14 -0400381 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400382 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400383 break;
Yan7d7d6062007-09-14 16:15:28 -0400384
Yan Zheng11833d62009-09-11 16:11:19 -0400385 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400386 total_found += add_new_free_space(block_group,
387 fs_info, last,
388 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400389 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400390
Yan Zheng11833d62009-09-11 16:11:19 -0400391 if (total_found > (1024 * 1024 * 2)) {
392 total_found = 0;
393 wake_up(&caching_ctl->wait);
394 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400395 }
Chris Masone37c9e62007-05-09 20:13:14 -0400396 path->slots[0]++;
397 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400398 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400399
400 total_found += add_new_free_space(block_group, fs_info, last,
401 block_group->key.objectid +
402 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400403 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400404
405 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400406 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400407 block_group->cached = BTRFS_CACHE_FINISHED;
408 spin_unlock(&block_group->lock);
409
Chris Mason54aa1f42007-06-22 14:16:25 -0400410err:
Chris Masone37c9e62007-05-09 20:13:14 -0400411 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400412 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400413
Yan Zheng11833d62009-09-11 16:11:19 -0400414 free_excluded_extents(extent_root, block_group);
415
416 mutex_unlock(&caching_ctl->mutex);
417 wake_up(&caching_ctl->wait);
418
419 put_caching_control(caching_ctl);
420 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000421 btrfs_put_block_group(block_group);
422
Josef Bacik817d52f2009-07-13 21:29:25 -0400423 return 0;
424}
425
426static int cache_block_group(struct btrfs_block_group_cache *cache)
427{
Yan Zheng11833d62009-09-11 16:11:19 -0400428 struct btrfs_fs_info *fs_info = cache->fs_info;
429 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400430 struct task_struct *tsk;
431 int ret = 0;
432
Yan Zheng11833d62009-09-11 16:11:19 -0400433 smp_mb();
434 if (cache->cached != BTRFS_CACHE_NO)
435 return 0;
436
437 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
438 BUG_ON(!caching_ctl);
439
440 INIT_LIST_HEAD(&caching_ctl->list);
441 mutex_init(&caching_ctl->mutex);
442 init_waitqueue_head(&caching_ctl->wait);
443 caching_ctl->block_group = cache;
444 caching_ctl->progress = cache->key.objectid;
445 /* one for caching kthread, one for caching block group list */
446 atomic_set(&caching_ctl->count, 2);
447
Josef Bacik817d52f2009-07-13 21:29:25 -0400448 spin_lock(&cache->lock);
449 if (cache->cached != BTRFS_CACHE_NO) {
450 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400451 kfree(caching_ctl);
452 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400453 }
Yan Zheng11833d62009-09-11 16:11:19 -0400454 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400455 cache->cached = BTRFS_CACHE_STARTED;
456 spin_unlock(&cache->lock);
457
Yan Zheng11833d62009-09-11 16:11:19 -0400458 down_write(&fs_info->extent_commit_sem);
459 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
460 up_write(&fs_info->extent_commit_sem);
461
462 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000463 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400464
Josef Bacik817d52f2009-07-13 21:29:25 -0400465 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
466 cache->key.objectid);
467 if (IS_ERR(tsk)) {
468 ret = PTR_ERR(tsk);
469 printk(KERN_ERR "error running thread %d\n", ret);
470 BUG();
471 }
472
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400473 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400474}
475
Josef Bacik0f9dd462008-09-23 13:14:11 -0400476/*
477 * return the block group that starts at or after bytenr
478 */
Chris Masond3977122009-01-05 21:25:51 -0500479static struct btrfs_block_group_cache *
480btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400481{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400482 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400483
Josef Bacik0f9dd462008-09-23 13:14:11 -0400484 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400485
Josef Bacik0f9dd462008-09-23 13:14:11 -0400486 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400487}
488
Josef Bacik0f9dd462008-09-23 13:14:11 -0400489/*
Sankar P9f556842009-05-14 13:52:22 -0400490 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400491 */
Chris Masond3977122009-01-05 21:25:51 -0500492struct btrfs_block_group_cache *btrfs_lookup_block_group(
493 struct btrfs_fs_info *info,
494 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400495{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400496 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400497
Josef Bacik0f9dd462008-09-23 13:14:11 -0400498 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400499
Josef Bacik0f9dd462008-09-23 13:14:11 -0400500 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400501}
Chris Mason0b86a832008-03-24 15:01:56 -0400502
Josef Bacik0f9dd462008-09-23 13:14:11 -0400503static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
504 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400505{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400506 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400507 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400508
509 rcu_read_lock();
510 list_for_each_entry_rcu(found, head, list) {
511 if (found->flags == flags) {
512 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400513 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400514 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400515 }
Chris Mason4184ea72009-03-10 12:39:20 -0400516 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400517 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400518}
519
Chris Mason4184ea72009-03-10 12:39:20 -0400520/*
521 * after adding space to the filesystem, we need to clear the full flags
522 * on all the space infos.
523 */
524void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
525{
526 struct list_head *head = &info->space_info;
527 struct btrfs_space_info *found;
528
529 rcu_read_lock();
530 list_for_each_entry_rcu(found, head, list)
531 found->full = 0;
532 rcu_read_unlock();
533}
534
Josef Bacik80eb2342008-10-29 14:49:05 -0400535static u64 div_factor(u64 num, int factor)
536{
537 if (factor == 10)
538 return num;
539 num *= factor;
540 do_div(num, 10);
541 return num;
542}
543
Yan Zhengd2fb3432008-12-11 16:30:39 -0500544u64 btrfs_find_block_group(struct btrfs_root *root,
545 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400546{
Chris Mason96b51792007-10-15 16:15:19 -0400547 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400548 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500549 u64 last = max(search_hint, search_start);
550 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400551 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500552 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400553 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400554again:
Zheng Yane8569812008-09-26 10:05:48 -0400555 while (1) {
556 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400557 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400558 break;
Chris Mason96b51792007-10-15 16:15:19 -0400559
Chris Masonc286ac42008-07-22 23:06:41 -0400560 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400561 last = cache->key.objectid + cache->key.offset;
562 used = btrfs_block_group_used(&cache->item);
563
Yan Zhengd2fb3432008-12-11 16:30:39 -0500564 if ((full_search || !cache->ro) &&
565 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400566 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500567 div_factor(cache->key.offset, factor)) {
568 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400569 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400570 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400571 goto found;
572 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400573 }
Chris Masonc286ac42008-07-22 23:06:41 -0400574 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400575 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400576 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400577 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400578 if (!wrapped) {
579 last = search_start;
580 wrapped = 1;
581 goto again;
582 }
583 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400584 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400585 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400586 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400587 goto again;
588 }
Chris Masonbe744172007-05-06 10:15:01 -0400589found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500590 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400591}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400592
Chris Masone02119d2008-09-05 16:13:11 -0400593/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400594int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400595{
596 int ret;
597 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400598 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400599
Zheng Yan31840ae2008-09-23 13:14:14 -0400600 path = btrfs_alloc_path();
601 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400602 key.objectid = start;
603 key.offset = len;
604 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
605 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
606 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400607 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500608 return ret;
609}
610
Chris Masond8d5f3e2007-12-11 12:42:00 -0500611/*
612 * Back reference rules. Back refs have three main goals:
613 *
614 * 1) differentiate between all holders of references to an extent so that
615 * when a reference is dropped we can make sure it was a valid reference
616 * before freeing the extent.
617 *
618 * 2) Provide enough information to quickly find the holders of an extent
619 * if we notice a given block is corrupted or bad.
620 *
621 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
622 * maintenance. This is actually the same as #2, but with a slightly
623 * different use case.
624 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400625 * There are two kinds of back refs. The implicit back refs is optimized
626 * for pointers in non-shared tree blocks. For a given pointer in a block,
627 * back refs of this kind provide information about the block's owner tree
628 * and the pointer's key. These information allow us to find the block by
629 * b-tree searching. The full back refs is for pointers in tree blocks not
630 * referenced by their owner trees. The location of tree block is recorded
631 * in the back refs. Actually the full back refs is generic, and can be
632 * used in all cases the implicit back refs is used. The major shortcoming
633 * of the full back refs is its overhead. Every time a tree block gets
634 * COWed, we have to update back refs entry for all pointers in it.
635 *
636 * For a newly allocated tree block, we use implicit back refs for
637 * pointers in it. This means most tree related operations only involve
638 * implicit back refs. For a tree block created in old transaction, the
639 * only way to drop a reference to it is COW it. So we can detect the
640 * event that tree block loses its owner tree's reference and do the
641 * back refs conversion.
642 *
643 * When a tree block is COW'd through a tree, there are four cases:
644 *
645 * The reference count of the block is one and the tree is the block's
646 * owner tree. Nothing to do in this case.
647 *
648 * The reference count of the block is one and the tree is not the
649 * block's owner tree. In this case, full back refs is used for pointers
650 * in the block. Remove these full back refs, add implicit back refs for
651 * every pointers in the new block.
652 *
653 * The reference count of the block is greater than one and the tree is
654 * the block's owner tree. In this case, implicit back refs is used for
655 * pointers in the block. Add full back refs for every pointers in the
656 * block, increase lower level extents' reference counts. The original
657 * implicit back refs are entailed to the new block.
658 *
659 * The reference count of the block is greater than one and the tree is
660 * not the block's owner tree. Add implicit back refs for every pointer in
661 * the new block, increase lower level extents' reference count.
662 *
663 * Back Reference Key composing:
664 *
665 * The key objectid corresponds to the first byte in the extent,
666 * The key type is used to differentiate between types of back refs.
667 * There are different meanings of the key offset for different types
668 * of back refs.
669 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500670 * File extents can be referenced by:
671 *
672 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400673 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500674 * - different offsets inside a file (bookend extents in file.c)
675 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400676 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500677 *
678 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500679 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400680 * - original offset in the file
681 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400682 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400683 * The key offset for the implicit back refs is hash of the first
684 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500685 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400686 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500687 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400688 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500689 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400690 * The key offset for the implicit back refs is the first byte of
691 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500692 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400693 * When a file extent is allocated, The implicit back refs is used.
694 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500695 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400696 * (root_key.objectid, inode objectid, offset in file, 1)
697 *
698 * When a file extent is removed file truncation, we find the
699 * corresponding implicit back refs and check the following fields:
700 *
701 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500702 *
703 * Btree extents can be referenced by:
704 *
705 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500706 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400707 * Both the implicit back refs and the full back refs for tree blocks
708 * only consist of key. The key offset for the implicit back refs is
709 * objectid of block's owner tree. The key offset for the full back refs
710 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500711 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400712 * When implicit back refs is used, information about the lowest key and
713 * level of the tree block are required. These information are stored in
714 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500715 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400716
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400717#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
718static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
719 struct btrfs_root *root,
720 struct btrfs_path *path,
721 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500722{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400723 struct btrfs_extent_item *item;
724 struct btrfs_extent_item_v0 *ei0;
725 struct btrfs_extent_ref_v0 *ref0;
726 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400727 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400728 struct btrfs_key key;
729 struct btrfs_key found_key;
730 u32 new_size = sizeof(*item);
731 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500732 int ret;
733
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400734 leaf = path->nodes[0];
735 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500736
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400737 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
738 ei0 = btrfs_item_ptr(leaf, path->slots[0],
739 struct btrfs_extent_item_v0);
740 refs = btrfs_extent_refs_v0(leaf, ei0);
741
742 if (owner == (u64)-1) {
743 while (1) {
744 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
745 ret = btrfs_next_leaf(root, path);
746 if (ret < 0)
747 return ret;
748 BUG_ON(ret > 0);
749 leaf = path->nodes[0];
750 }
751 btrfs_item_key_to_cpu(leaf, &found_key,
752 path->slots[0]);
753 BUG_ON(key.objectid != found_key.objectid);
754 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
755 path->slots[0]++;
756 continue;
757 }
758 ref0 = btrfs_item_ptr(leaf, path->slots[0],
759 struct btrfs_extent_ref_v0);
760 owner = btrfs_ref_objectid_v0(leaf, ref0);
761 break;
762 }
763 }
764 btrfs_release_path(root, path);
765
766 if (owner < BTRFS_FIRST_FREE_OBJECTID)
767 new_size += sizeof(*bi);
768
769 new_size -= sizeof(*ei0);
770 ret = btrfs_search_slot(trans, root, &key, path,
771 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400772 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400773 return ret;
774 BUG_ON(ret);
775
776 ret = btrfs_extend_item(trans, root, path, new_size);
777 BUG_ON(ret);
778
779 leaf = path->nodes[0];
780 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
781 btrfs_set_extent_refs(leaf, item, refs);
782 /* FIXME: get real generation */
783 btrfs_set_extent_generation(leaf, item, 0);
784 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
785 btrfs_set_extent_flags(leaf, item,
786 BTRFS_EXTENT_FLAG_TREE_BLOCK |
787 BTRFS_BLOCK_FLAG_FULL_BACKREF);
788 bi = (struct btrfs_tree_block_info *)(item + 1);
789 /* FIXME: get first key of the block */
790 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
791 btrfs_set_tree_block_level(leaf, bi, (int)owner);
792 } else {
793 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
794 }
795 btrfs_mark_buffer_dirty(leaf);
796 return 0;
797}
798#endif
799
800static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
801{
802 u32 high_crc = ~(u32)0;
803 u32 low_crc = ~(u32)0;
804 __le64 lenum;
805
806 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100807 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400808 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100809 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400810 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100811 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400812
813 return ((u64)high_crc << 31) ^ (u64)low_crc;
814}
815
816static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
817 struct btrfs_extent_data_ref *ref)
818{
819 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
820 btrfs_extent_data_ref_objectid(leaf, ref),
821 btrfs_extent_data_ref_offset(leaf, ref));
822}
823
824static int match_extent_data_ref(struct extent_buffer *leaf,
825 struct btrfs_extent_data_ref *ref,
826 u64 root_objectid, u64 owner, u64 offset)
827{
828 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
829 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
830 btrfs_extent_data_ref_offset(leaf, ref) != offset)
831 return 0;
832 return 1;
833}
834
835static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
836 struct btrfs_root *root,
837 struct btrfs_path *path,
838 u64 bytenr, u64 parent,
839 u64 root_objectid,
840 u64 owner, u64 offset)
841{
842 struct btrfs_key key;
843 struct btrfs_extent_data_ref *ref;
844 struct extent_buffer *leaf;
845 u32 nritems;
846 int ret;
847 int recow;
848 int err = -ENOENT;
849
850 key.objectid = bytenr;
851 if (parent) {
852 key.type = BTRFS_SHARED_DATA_REF_KEY;
853 key.offset = parent;
854 } else {
855 key.type = BTRFS_EXTENT_DATA_REF_KEY;
856 key.offset = hash_extent_data_ref(root_objectid,
857 owner, offset);
858 }
859again:
860 recow = 0;
861 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
862 if (ret < 0) {
863 err = ret;
864 goto fail;
865 }
866
867 if (parent) {
868 if (!ret)
869 return 0;
870#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
871 key.type = BTRFS_EXTENT_REF_V0_KEY;
872 btrfs_release_path(root, path);
873 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
874 if (ret < 0) {
875 err = ret;
876 goto fail;
877 }
878 if (!ret)
879 return 0;
880#endif
881 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400882 }
883
884 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400885 nritems = btrfs_header_nritems(leaf);
886 while (1) {
887 if (path->slots[0] >= nritems) {
888 ret = btrfs_next_leaf(root, path);
889 if (ret < 0)
890 err = ret;
891 if (ret)
892 goto fail;
893
894 leaf = path->nodes[0];
895 nritems = btrfs_header_nritems(leaf);
896 recow = 1;
897 }
898
899 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
900 if (key.objectid != bytenr ||
901 key.type != BTRFS_EXTENT_DATA_REF_KEY)
902 goto fail;
903
904 ref = btrfs_item_ptr(leaf, path->slots[0],
905 struct btrfs_extent_data_ref);
906
907 if (match_extent_data_ref(leaf, ref, root_objectid,
908 owner, offset)) {
909 if (recow) {
910 btrfs_release_path(root, path);
911 goto again;
912 }
913 err = 0;
914 break;
915 }
916 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400917 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918fail:
919 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400920}
921
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400922static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
923 struct btrfs_root *root,
924 struct btrfs_path *path,
925 u64 bytenr, u64 parent,
926 u64 root_objectid, u64 owner,
927 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400928{
929 struct btrfs_key key;
930 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400931 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400932 u32 num_refs;
933 int ret;
934
935 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400936 if (parent) {
937 key.type = BTRFS_SHARED_DATA_REF_KEY;
938 key.offset = parent;
939 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400940 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400941 key.type = BTRFS_EXTENT_DATA_REF_KEY;
942 key.offset = hash_extent_data_ref(root_objectid,
943 owner, offset);
944 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400945 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946
947 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
948 if (ret && ret != -EEXIST)
949 goto fail;
950
951 leaf = path->nodes[0];
952 if (parent) {
953 struct btrfs_shared_data_ref *ref;
954 ref = btrfs_item_ptr(leaf, path->slots[0],
955 struct btrfs_shared_data_ref);
956 if (ret == 0) {
957 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
958 } else {
959 num_refs = btrfs_shared_data_ref_count(leaf, ref);
960 num_refs += refs_to_add;
961 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
962 }
963 } else {
964 struct btrfs_extent_data_ref *ref;
965 while (ret == -EEXIST) {
966 ref = btrfs_item_ptr(leaf, path->slots[0],
967 struct btrfs_extent_data_ref);
968 if (match_extent_data_ref(leaf, ref, root_objectid,
969 owner, offset))
970 break;
971 btrfs_release_path(root, path);
972 key.offset++;
973 ret = btrfs_insert_empty_item(trans, root, path, &key,
974 size);
975 if (ret && ret != -EEXIST)
976 goto fail;
977
978 leaf = path->nodes[0];
979 }
980 ref = btrfs_item_ptr(leaf, path->slots[0],
981 struct btrfs_extent_data_ref);
982 if (ret == 0) {
983 btrfs_set_extent_data_ref_root(leaf, ref,
984 root_objectid);
985 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
986 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
987 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
988 } else {
989 num_refs = btrfs_extent_data_ref_count(leaf, ref);
990 num_refs += refs_to_add;
991 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
992 }
993 }
994 btrfs_mark_buffer_dirty(leaf);
995 ret = 0;
996fail:
Chris Mason7bb86312007-12-11 09:25:06 -0500997 btrfs_release_path(root, path);
998 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500999}
1000
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001001static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1002 struct btrfs_root *root,
1003 struct btrfs_path *path,
1004 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001005{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001006 struct btrfs_key key;
1007 struct btrfs_extent_data_ref *ref1 = NULL;
1008 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001009 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001010 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001011 int ret = 0;
1012
1013 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001014 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1015
1016 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1017 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1018 struct btrfs_extent_data_ref);
1019 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1020 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1021 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1022 struct btrfs_shared_data_ref);
1023 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1024#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1025 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1026 struct btrfs_extent_ref_v0 *ref0;
1027 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1028 struct btrfs_extent_ref_v0);
1029 num_refs = btrfs_ref_count_v0(leaf, ref0);
1030#endif
1031 } else {
1032 BUG();
1033 }
1034
Chris Mason56bec292009-03-13 10:10:06 -04001035 BUG_ON(num_refs < refs_to_drop);
1036 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037
Zheng Yan31840ae2008-09-23 13:14:14 -04001038 if (num_refs == 0) {
1039 ret = btrfs_del_item(trans, root, path);
1040 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001041 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1042 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1043 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1044 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1045#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1046 else {
1047 struct btrfs_extent_ref_v0 *ref0;
1048 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1049 struct btrfs_extent_ref_v0);
1050 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1051 }
1052#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001053 btrfs_mark_buffer_dirty(leaf);
1054 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001055 return ret;
1056}
1057
1058static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1059 struct btrfs_path *path,
1060 struct btrfs_extent_inline_ref *iref)
1061{
1062 struct btrfs_key key;
1063 struct extent_buffer *leaf;
1064 struct btrfs_extent_data_ref *ref1;
1065 struct btrfs_shared_data_ref *ref2;
1066 u32 num_refs = 0;
1067
1068 leaf = path->nodes[0];
1069 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1070 if (iref) {
1071 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1072 BTRFS_EXTENT_DATA_REF_KEY) {
1073 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1074 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1075 } else {
1076 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1077 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1078 }
1079 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1080 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1081 struct btrfs_extent_data_ref);
1082 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1083 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1084 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1085 struct btrfs_shared_data_ref);
1086 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1087#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1088 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1089 struct btrfs_extent_ref_v0 *ref0;
1090 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1091 struct btrfs_extent_ref_v0);
1092 num_refs = btrfs_ref_count_v0(leaf, ref0);
1093#endif
1094 } else {
1095 WARN_ON(1);
1096 }
1097 return num_refs;
1098}
1099
1100static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1101 struct btrfs_root *root,
1102 struct btrfs_path *path,
1103 u64 bytenr, u64 parent,
1104 u64 root_objectid)
1105{
1106 struct btrfs_key key;
1107 int ret;
1108
1109 key.objectid = bytenr;
1110 if (parent) {
1111 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1112 key.offset = parent;
1113 } else {
1114 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1115 key.offset = root_objectid;
1116 }
1117
1118 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1119 if (ret > 0)
1120 ret = -ENOENT;
1121#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1122 if (ret == -ENOENT && parent) {
1123 btrfs_release_path(root, path);
1124 key.type = BTRFS_EXTENT_REF_V0_KEY;
1125 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1126 if (ret > 0)
1127 ret = -ENOENT;
1128 }
1129#endif
1130 return ret;
1131}
1132
1133static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1134 struct btrfs_root *root,
1135 struct btrfs_path *path,
1136 u64 bytenr, u64 parent,
1137 u64 root_objectid)
1138{
1139 struct btrfs_key key;
1140 int ret;
1141
1142 key.objectid = bytenr;
1143 if (parent) {
1144 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1145 key.offset = parent;
1146 } else {
1147 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1148 key.offset = root_objectid;
1149 }
1150
1151 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001152 btrfs_release_path(root, path);
1153 return ret;
1154}
1155
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001156static inline int extent_ref_type(u64 parent, u64 owner)
1157{
1158 int type;
1159 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1160 if (parent > 0)
1161 type = BTRFS_SHARED_BLOCK_REF_KEY;
1162 else
1163 type = BTRFS_TREE_BLOCK_REF_KEY;
1164 } else {
1165 if (parent > 0)
1166 type = BTRFS_SHARED_DATA_REF_KEY;
1167 else
1168 type = BTRFS_EXTENT_DATA_REF_KEY;
1169 }
1170 return type;
1171}
1172
Yan Zheng2c47e6052009-06-27 21:07:35 -04001173static int find_next_key(struct btrfs_path *path, int level,
1174 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001175
1176{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001177 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001178 if (!path->nodes[level])
1179 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001180 if (path->slots[level] + 1 >=
1181 btrfs_header_nritems(path->nodes[level]))
1182 continue;
1183 if (level == 0)
1184 btrfs_item_key_to_cpu(path->nodes[level], key,
1185 path->slots[level] + 1);
1186 else
1187 btrfs_node_key_to_cpu(path->nodes[level], key,
1188 path->slots[level] + 1);
1189 return 0;
1190 }
1191 return 1;
1192}
1193
1194/*
1195 * look for inline back ref. if back ref is found, *ref_ret is set
1196 * to the address of inline back ref, and 0 is returned.
1197 *
1198 * if back ref isn't found, *ref_ret is set to the address where it
1199 * should be inserted, and -ENOENT is returned.
1200 *
1201 * if insert is true and there are too many inline back refs, the path
1202 * points to the extent item, and -EAGAIN is returned.
1203 *
1204 * NOTE: inline back refs are ordered in the same way that back ref
1205 * items in the tree are ordered.
1206 */
1207static noinline_for_stack
1208int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1209 struct btrfs_root *root,
1210 struct btrfs_path *path,
1211 struct btrfs_extent_inline_ref **ref_ret,
1212 u64 bytenr, u64 num_bytes,
1213 u64 parent, u64 root_objectid,
1214 u64 owner, u64 offset, int insert)
1215{
1216 struct btrfs_key key;
1217 struct extent_buffer *leaf;
1218 struct btrfs_extent_item *ei;
1219 struct btrfs_extent_inline_ref *iref;
1220 u64 flags;
1221 u64 item_size;
1222 unsigned long ptr;
1223 unsigned long end;
1224 int extra_size;
1225 int type;
1226 int want;
1227 int ret;
1228 int err = 0;
1229
1230 key.objectid = bytenr;
1231 key.type = BTRFS_EXTENT_ITEM_KEY;
1232 key.offset = num_bytes;
1233
1234 want = extent_ref_type(parent, owner);
1235 if (insert) {
1236 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001237 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001238 } else
1239 extra_size = -1;
1240 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1241 if (ret < 0) {
1242 err = ret;
1243 goto out;
1244 }
1245 BUG_ON(ret);
1246
1247 leaf = path->nodes[0];
1248 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1249#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1250 if (item_size < sizeof(*ei)) {
1251 if (!insert) {
1252 err = -ENOENT;
1253 goto out;
1254 }
1255 ret = convert_extent_item_v0(trans, root, path, owner,
1256 extra_size);
1257 if (ret < 0) {
1258 err = ret;
1259 goto out;
1260 }
1261 leaf = path->nodes[0];
1262 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1263 }
1264#endif
1265 BUG_ON(item_size < sizeof(*ei));
1266
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001267 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1268 flags = btrfs_extent_flags(leaf, ei);
1269
1270 ptr = (unsigned long)(ei + 1);
1271 end = (unsigned long)ei + item_size;
1272
1273 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1274 ptr += sizeof(struct btrfs_tree_block_info);
1275 BUG_ON(ptr > end);
1276 } else {
1277 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1278 }
1279
1280 err = -ENOENT;
1281 while (1) {
1282 if (ptr >= end) {
1283 WARN_ON(ptr > end);
1284 break;
1285 }
1286 iref = (struct btrfs_extent_inline_ref *)ptr;
1287 type = btrfs_extent_inline_ref_type(leaf, iref);
1288 if (want < type)
1289 break;
1290 if (want > type) {
1291 ptr += btrfs_extent_inline_ref_size(type);
1292 continue;
1293 }
1294
1295 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1296 struct btrfs_extent_data_ref *dref;
1297 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1298 if (match_extent_data_ref(leaf, dref, root_objectid,
1299 owner, offset)) {
1300 err = 0;
1301 break;
1302 }
1303 if (hash_extent_data_ref_item(leaf, dref) <
1304 hash_extent_data_ref(root_objectid, owner, offset))
1305 break;
1306 } else {
1307 u64 ref_offset;
1308 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1309 if (parent > 0) {
1310 if (parent == ref_offset) {
1311 err = 0;
1312 break;
1313 }
1314 if (ref_offset < parent)
1315 break;
1316 } else {
1317 if (root_objectid == ref_offset) {
1318 err = 0;
1319 break;
1320 }
1321 if (ref_offset < root_objectid)
1322 break;
1323 }
1324 }
1325 ptr += btrfs_extent_inline_ref_size(type);
1326 }
1327 if (err == -ENOENT && insert) {
1328 if (item_size + extra_size >=
1329 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1330 err = -EAGAIN;
1331 goto out;
1332 }
1333 /*
1334 * To add new inline back ref, we have to make sure
1335 * there is no corresponding back ref item.
1336 * For simplicity, we just do not add new inline back
1337 * ref if there is any kind of item for this block
1338 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001339 if (find_next_key(path, 0, &key) == 0 &&
1340 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001341 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001342 err = -EAGAIN;
1343 goto out;
1344 }
1345 }
1346 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1347out:
Yan Zheng85d41982009-06-11 08:51:10 -04001348 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001349 path->keep_locks = 0;
1350 btrfs_unlock_up_safe(path, 1);
1351 }
1352 return err;
1353}
1354
1355/*
1356 * helper to add new inline back ref
1357 */
1358static noinline_for_stack
1359int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1360 struct btrfs_root *root,
1361 struct btrfs_path *path,
1362 struct btrfs_extent_inline_ref *iref,
1363 u64 parent, u64 root_objectid,
1364 u64 owner, u64 offset, int refs_to_add,
1365 struct btrfs_delayed_extent_op *extent_op)
1366{
1367 struct extent_buffer *leaf;
1368 struct btrfs_extent_item *ei;
1369 unsigned long ptr;
1370 unsigned long end;
1371 unsigned long item_offset;
1372 u64 refs;
1373 int size;
1374 int type;
1375 int ret;
1376
1377 leaf = path->nodes[0];
1378 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1379 item_offset = (unsigned long)iref - (unsigned long)ei;
1380
1381 type = extent_ref_type(parent, owner);
1382 size = btrfs_extent_inline_ref_size(type);
1383
1384 ret = btrfs_extend_item(trans, root, path, size);
1385 BUG_ON(ret);
1386
1387 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1388 refs = btrfs_extent_refs(leaf, ei);
1389 refs += refs_to_add;
1390 btrfs_set_extent_refs(leaf, ei, refs);
1391 if (extent_op)
1392 __run_delayed_extent_op(extent_op, leaf, ei);
1393
1394 ptr = (unsigned long)ei + item_offset;
1395 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1396 if (ptr < end - size)
1397 memmove_extent_buffer(leaf, ptr + size, ptr,
1398 end - size - ptr);
1399
1400 iref = (struct btrfs_extent_inline_ref *)ptr;
1401 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1402 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1403 struct btrfs_extent_data_ref *dref;
1404 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1405 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1406 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1407 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1408 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1409 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1410 struct btrfs_shared_data_ref *sref;
1411 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1412 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1413 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1414 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1415 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1416 } else {
1417 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1418 }
1419 btrfs_mark_buffer_dirty(leaf);
1420 return 0;
1421}
1422
1423static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1424 struct btrfs_root *root,
1425 struct btrfs_path *path,
1426 struct btrfs_extent_inline_ref **ref_ret,
1427 u64 bytenr, u64 num_bytes, u64 parent,
1428 u64 root_objectid, u64 owner, u64 offset)
1429{
1430 int ret;
1431
1432 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1433 bytenr, num_bytes, parent,
1434 root_objectid, owner, offset, 0);
1435 if (ret != -ENOENT)
1436 return ret;
1437
1438 btrfs_release_path(root, path);
1439 *ref_ret = NULL;
1440
1441 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1442 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1443 root_objectid);
1444 } else {
1445 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1446 root_objectid, owner, offset);
1447 }
1448 return ret;
1449}
1450
1451/*
1452 * helper to update/remove inline back ref
1453 */
1454static noinline_for_stack
1455int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1456 struct btrfs_root *root,
1457 struct btrfs_path *path,
1458 struct btrfs_extent_inline_ref *iref,
1459 int refs_to_mod,
1460 struct btrfs_delayed_extent_op *extent_op)
1461{
1462 struct extent_buffer *leaf;
1463 struct btrfs_extent_item *ei;
1464 struct btrfs_extent_data_ref *dref = NULL;
1465 struct btrfs_shared_data_ref *sref = NULL;
1466 unsigned long ptr;
1467 unsigned long end;
1468 u32 item_size;
1469 int size;
1470 int type;
1471 int ret;
1472 u64 refs;
1473
1474 leaf = path->nodes[0];
1475 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1476 refs = btrfs_extent_refs(leaf, ei);
1477 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1478 refs += refs_to_mod;
1479 btrfs_set_extent_refs(leaf, ei, refs);
1480 if (extent_op)
1481 __run_delayed_extent_op(extent_op, leaf, ei);
1482
1483 type = btrfs_extent_inline_ref_type(leaf, iref);
1484
1485 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1486 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1487 refs = btrfs_extent_data_ref_count(leaf, dref);
1488 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1489 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1490 refs = btrfs_shared_data_ref_count(leaf, sref);
1491 } else {
1492 refs = 1;
1493 BUG_ON(refs_to_mod != -1);
1494 }
1495
1496 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1497 refs += refs_to_mod;
1498
1499 if (refs > 0) {
1500 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1501 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1502 else
1503 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1504 } else {
1505 size = btrfs_extent_inline_ref_size(type);
1506 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1507 ptr = (unsigned long)iref;
1508 end = (unsigned long)ei + item_size;
1509 if (ptr + size < end)
1510 memmove_extent_buffer(leaf, ptr, ptr + size,
1511 end - ptr - size);
1512 item_size -= size;
1513 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1514 BUG_ON(ret);
1515 }
1516 btrfs_mark_buffer_dirty(leaf);
1517 return 0;
1518}
1519
1520static noinline_for_stack
1521int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1522 struct btrfs_root *root,
1523 struct btrfs_path *path,
1524 u64 bytenr, u64 num_bytes, u64 parent,
1525 u64 root_objectid, u64 owner,
1526 u64 offset, int refs_to_add,
1527 struct btrfs_delayed_extent_op *extent_op)
1528{
1529 struct btrfs_extent_inline_ref *iref;
1530 int ret;
1531
1532 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1533 bytenr, num_bytes, parent,
1534 root_objectid, owner, offset, 1);
1535 if (ret == 0) {
1536 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1537 ret = update_inline_extent_backref(trans, root, path, iref,
1538 refs_to_add, extent_op);
1539 } else if (ret == -ENOENT) {
1540 ret = setup_inline_extent_backref(trans, root, path, iref,
1541 parent, root_objectid,
1542 owner, offset, refs_to_add,
1543 extent_op);
1544 }
1545 return ret;
1546}
1547
1548static int insert_extent_backref(struct btrfs_trans_handle *trans,
1549 struct btrfs_root *root,
1550 struct btrfs_path *path,
1551 u64 bytenr, u64 parent, u64 root_objectid,
1552 u64 owner, u64 offset, int refs_to_add)
1553{
1554 int ret;
1555 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1556 BUG_ON(refs_to_add != 1);
1557 ret = insert_tree_block_ref(trans, root, path, bytenr,
1558 parent, root_objectid);
1559 } else {
1560 ret = insert_extent_data_ref(trans, root, path, bytenr,
1561 parent, root_objectid,
1562 owner, offset, refs_to_add);
1563 }
1564 return ret;
1565}
1566
1567static int remove_extent_backref(struct btrfs_trans_handle *trans,
1568 struct btrfs_root *root,
1569 struct btrfs_path *path,
1570 struct btrfs_extent_inline_ref *iref,
1571 int refs_to_drop, int is_data)
1572{
1573 int ret;
1574
1575 BUG_ON(!is_data && refs_to_drop != 1);
1576 if (iref) {
1577 ret = update_inline_extent_backref(trans, root, path, iref,
1578 -refs_to_drop, NULL);
1579 } else if (is_data) {
1580 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1581 } else {
1582 ret = btrfs_del_item(trans, root, path);
1583 }
1584 return ret;
1585}
1586
Chris Mason15916de2008-11-19 21:17:22 -05001587static void btrfs_issue_discard(struct block_device *bdev,
1588 u64 start, u64 len)
1589{
Christoph Hellwig746cd1e2009-09-12 07:35:43 +02001590 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
1591 DISCARD_FL_BARRIER);
Chris Mason15916de2008-11-19 21:17:22 -05001592}
Chris Mason15916de2008-11-19 21:17:22 -05001593
Liu Hui1f3c79a2009-01-05 15:57:51 -05001594static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1595 u64 num_bytes)
1596{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001597 int ret;
1598 u64 map_length = num_bytes;
1599 struct btrfs_multi_bio *multi = NULL;
1600
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001601 if (!btrfs_test_opt(root, DISCARD))
1602 return 0;
1603
Liu Hui1f3c79a2009-01-05 15:57:51 -05001604 /* Tell the block device(s) that the sectors can be discarded */
1605 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1606 bytenr, &map_length, &multi, 0);
1607 if (!ret) {
1608 struct btrfs_bio_stripe *stripe = multi->stripes;
1609 int i;
1610
1611 if (map_length > num_bytes)
1612 map_length = num_bytes;
1613
1614 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1615 btrfs_issue_discard(stripe->dev->bdev,
1616 stripe->physical,
1617 map_length);
1618 }
1619 kfree(multi);
1620 }
1621
1622 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001623}
1624
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001625int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1626 struct btrfs_root *root,
1627 u64 bytenr, u64 num_bytes, u64 parent,
1628 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001629{
1630 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001631 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1632 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001633
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001634 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1635 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1636 parent, root_objectid, (int)owner,
1637 BTRFS_ADD_DELAYED_REF, NULL);
1638 } else {
1639 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1640 parent, root_objectid, owner, offset,
1641 BTRFS_ADD_DELAYED_REF, NULL);
1642 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001643 return ret;
1644}
1645
Chris Mason925baed2008-06-25 16:01:30 -04001646static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001647 struct btrfs_root *root,
1648 u64 bytenr, u64 num_bytes,
1649 u64 parent, u64 root_objectid,
1650 u64 owner, u64 offset, int refs_to_add,
1651 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001652{
Chris Mason5caf2a02007-04-02 11:20:42 -04001653 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001654 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001655 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001656 u64 refs;
1657 int ret;
1658 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001659
Chris Mason5caf2a02007-04-02 11:20:42 -04001660 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001661 if (!path)
1662 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001663
Chris Mason3c12ac72008-04-21 12:01:38 -04001664 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001665 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001666 /* this will setup the path even if it fails to insert the back ref */
1667 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1668 path, bytenr, num_bytes, parent,
1669 root_objectid, owner, offset,
1670 refs_to_add, extent_op);
1671 if (ret == 0)
1672 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001673
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001674 if (ret != -EAGAIN) {
1675 err = ret;
1676 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001677 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001678
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001679 leaf = path->nodes[0];
1680 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1681 refs = btrfs_extent_refs(leaf, item);
1682 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1683 if (extent_op)
1684 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001685
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686 btrfs_mark_buffer_dirty(leaf);
Chris Mason5caf2a02007-04-02 11:20:42 -04001687 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001688
Chris Mason3c12ac72008-04-21 12:01:38 -04001689 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001690 path->leave_spinning = 1;
1691
Chris Mason56bec292009-03-13 10:10:06 -04001692 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001693 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001694 path, bytenr, parent, root_objectid,
1695 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001696 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001697out:
Chris Mason74493f72007-12-11 09:25:06 -05001698 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001699 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001700}
1701
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001702static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1703 struct btrfs_root *root,
1704 struct btrfs_delayed_ref_node *node,
1705 struct btrfs_delayed_extent_op *extent_op,
1706 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001707{
Chris Mason56bec292009-03-13 10:10:06 -04001708 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001709 struct btrfs_delayed_data_ref *ref;
1710 struct btrfs_key ins;
1711 u64 parent = 0;
1712 u64 ref_root = 0;
1713 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001714
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001715 ins.objectid = node->bytenr;
1716 ins.offset = node->num_bytes;
1717 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001718
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001719 ref = btrfs_delayed_node_to_data_ref(node);
1720 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1721 parent = ref->parent;
1722 else
1723 ref_root = ref->root;
1724
1725 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1726 if (extent_op) {
1727 BUG_ON(extent_op->update_key);
1728 flags |= extent_op->flags_to_set;
1729 }
1730 ret = alloc_reserved_file_extent(trans, root,
1731 parent, ref_root, flags,
1732 ref->objectid, ref->offset,
1733 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001734 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1735 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1736 node->num_bytes, parent,
1737 ref_root, ref->objectid,
1738 ref->offset, node->ref_mod,
1739 extent_op);
1740 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1741 ret = __btrfs_free_extent(trans, root, node->bytenr,
1742 node->num_bytes, parent,
1743 ref_root, ref->objectid,
1744 ref->offset, node->ref_mod,
1745 extent_op);
1746 } else {
1747 BUG();
1748 }
Chris Mason56bec292009-03-13 10:10:06 -04001749 return ret;
1750}
1751
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001752static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1753 struct extent_buffer *leaf,
1754 struct btrfs_extent_item *ei)
1755{
1756 u64 flags = btrfs_extent_flags(leaf, ei);
1757 if (extent_op->update_flags) {
1758 flags |= extent_op->flags_to_set;
1759 btrfs_set_extent_flags(leaf, ei, flags);
1760 }
1761
1762 if (extent_op->update_key) {
1763 struct btrfs_tree_block_info *bi;
1764 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1765 bi = (struct btrfs_tree_block_info *)(ei + 1);
1766 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1767 }
1768}
1769
1770static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1771 struct btrfs_root *root,
1772 struct btrfs_delayed_ref_node *node,
1773 struct btrfs_delayed_extent_op *extent_op)
1774{
1775 struct btrfs_key key;
1776 struct btrfs_path *path;
1777 struct btrfs_extent_item *ei;
1778 struct extent_buffer *leaf;
1779 u32 item_size;
1780 int ret;
1781 int err = 0;
1782
1783 path = btrfs_alloc_path();
1784 if (!path)
1785 return -ENOMEM;
1786
1787 key.objectid = node->bytenr;
1788 key.type = BTRFS_EXTENT_ITEM_KEY;
1789 key.offset = node->num_bytes;
1790
1791 path->reada = 1;
1792 path->leave_spinning = 1;
1793 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1794 path, 0, 1);
1795 if (ret < 0) {
1796 err = ret;
1797 goto out;
1798 }
1799 if (ret > 0) {
1800 err = -EIO;
1801 goto out;
1802 }
1803
1804 leaf = path->nodes[0];
1805 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1806#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1807 if (item_size < sizeof(*ei)) {
1808 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1809 path, (u64)-1, 0);
1810 if (ret < 0) {
1811 err = ret;
1812 goto out;
1813 }
1814 leaf = path->nodes[0];
1815 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1816 }
1817#endif
1818 BUG_ON(item_size < sizeof(*ei));
1819 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1820 __run_delayed_extent_op(extent_op, leaf, ei);
1821
1822 btrfs_mark_buffer_dirty(leaf);
1823out:
1824 btrfs_free_path(path);
1825 return err;
1826}
1827
1828static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1829 struct btrfs_root *root,
1830 struct btrfs_delayed_ref_node *node,
1831 struct btrfs_delayed_extent_op *extent_op,
1832 int insert_reserved)
1833{
1834 int ret = 0;
1835 struct btrfs_delayed_tree_ref *ref;
1836 struct btrfs_key ins;
1837 u64 parent = 0;
1838 u64 ref_root = 0;
1839
1840 ins.objectid = node->bytenr;
1841 ins.offset = node->num_bytes;
1842 ins.type = BTRFS_EXTENT_ITEM_KEY;
1843
1844 ref = btrfs_delayed_node_to_tree_ref(node);
1845 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1846 parent = ref->parent;
1847 else
1848 ref_root = ref->root;
1849
1850 BUG_ON(node->ref_mod != 1);
1851 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1852 BUG_ON(!extent_op || !extent_op->update_flags ||
1853 !extent_op->update_key);
1854 ret = alloc_reserved_tree_block(trans, root,
1855 parent, ref_root,
1856 extent_op->flags_to_set,
1857 &extent_op->key,
1858 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001859 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1860 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1861 node->num_bytes, parent, ref_root,
1862 ref->level, 0, 1, extent_op);
1863 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1864 ret = __btrfs_free_extent(trans, root, node->bytenr,
1865 node->num_bytes, parent, ref_root,
1866 ref->level, 0, 1, extent_op);
1867 } else {
1868 BUG();
1869 }
1870 return ret;
1871}
1872
1873
Chris Mason56bec292009-03-13 10:10:06 -04001874/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001875static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1876 struct btrfs_root *root,
1877 struct btrfs_delayed_ref_node *node,
1878 struct btrfs_delayed_extent_op *extent_op,
1879 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001880{
Josef Bacikeb099672009-02-12 09:27:38 -05001881 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001882 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04001883 struct btrfs_delayed_ref_head *head;
1884 /*
1885 * we've hit the end of the chain and we were supposed
1886 * to insert this extent into the tree. But, it got
1887 * deleted before we ever needed to insert it, so all
1888 * we have to do is clean up the accounting
1889 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001890 BUG_ON(extent_op);
1891 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04001892 if (insert_reserved) {
Yan Zheng11833d62009-09-11 16:11:19 -04001893 int mark_free = 0;
1894 struct extent_buffer *must_clean = NULL;
1895
1896 ret = pin_down_bytes(trans, root, NULL,
1897 node->bytenr, node->num_bytes,
1898 head->is_data, 1, &must_clean);
1899 if (ret > 0)
1900 mark_free = 1;
1901
1902 if (must_clean) {
1903 clean_tree_block(NULL, root, must_clean);
1904 btrfs_tree_unlock(must_clean);
1905 free_extent_buffer(must_clean);
1906 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001907 if (head->is_data) {
1908 ret = btrfs_del_csums(trans, root,
1909 node->bytenr,
1910 node->num_bytes);
1911 BUG_ON(ret);
1912 }
Yan Zheng11833d62009-09-11 16:11:19 -04001913 if (mark_free) {
1914 ret = btrfs_free_reserved_extent(root,
1915 node->bytenr,
1916 node->num_bytes);
1917 BUG_ON(ret);
1918 }
Chris Mason56bec292009-03-13 10:10:06 -04001919 }
Chris Mason56bec292009-03-13 10:10:06 -04001920 mutex_unlock(&head->mutex);
1921 return 0;
1922 }
Josef Bacikeb099672009-02-12 09:27:38 -05001923
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001924 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1925 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1926 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1927 insert_reserved);
1928 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1929 node->type == BTRFS_SHARED_DATA_REF_KEY)
1930 ret = run_delayed_data_ref(trans, root, node, extent_op,
1931 insert_reserved);
1932 else
1933 BUG();
1934 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001935}
1936
Chris Mason56bec292009-03-13 10:10:06 -04001937static noinline struct btrfs_delayed_ref_node *
1938select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001939{
Chris Mason56bec292009-03-13 10:10:06 -04001940 struct rb_node *node;
1941 struct btrfs_delayed_ref_node *ref;
1942 int action = BTRFS_ADD_DELAYED_REF;
1943again:
1944 /*
1945 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1946 * this prevents ref count from going down to zero when
1947 * there still are pending delayed ref.
1948 */
1949 node = rb_prev(&head->node.rb_node);
1950 while (1) {
1951 if (!node)
1952 break;
1953 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1954 rb_node);
1955 if (ref->bytenr != head->node.bytenr)
1956 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001957 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04001958 return ref;
1959 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04001960 }
Chris Mason56bec292009-03-13 10:10:06 -04001961 if (action == BTRFS_ADD_DELAYED_REF) {
1962 action = BTRFS_DROP_DELAYED_REF;
1963 goto again;
1964 }
1965 return NULL;
1966}
1967
Chris Masonc3e69d52009-03-13 10:17:05 -04001968static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1969 struct btrfs_root *root,
1970 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04001971{
Chris Mason56bec292009-03-13 10:10:06 -04001972 struct btrfs_delayed_ref_root *delayed_refs;
1973 struct btrfs_delayed_ref_node *ref;
1974 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001975 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04001976 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04001977 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001978 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001979
1980 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001981 while (1) {
1982 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04001983 /* pick a new head ref from the cluster list */
1984 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04001985 break;
Chris Mason56bec292009-03-13 10:10:06 -04001986
Chris Masonc3e69d52009-03-13 10:17:05 -04001987 locked_ref = list_entry(cluster->next,
1988 struct btrfs_delayed_ref_head, cluster);
1989
1990 /* grab the lock that says we are going to process
1991 * all the refs for this head */
1992 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1993
1994 /*
1995 * we may have dropped the spin lock to get the head
1996 * mutex lock, and that might have given someone else
1997 * time to free the head. If that's true, it has been
1998 * removed from our list and we can move on.
1999 */
2000 if (ret == -EAGAIN) {
2001 locked_ref = NULL;
2002 count++;
2003 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002004 }
2005 }
2006
2007 /*
2008 * record the must insert reserved flag before we
2009 * drop the spin lock.
2010 */
2011 must_insert_reserved = locked_ref->must_insert_reserved;
2012 locked_ref->must_insert_reserved = 0;
2013
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002014 extent_op = locked_ref->extent_op;
2015 locked_ref->extent_op = NULL;
2016
Chris Mason56bec292009-03-13 10:10:06 -04002017 /*
2018 * locked_ref is the head node, so we have to go one
2019 * node back for any delayed ref updates
2020 */
Chris Mason56bec292009-03-13 10:10:06 -04002021 ref = select_delayed_ref(locked_ref);
2022 if (!ref) {
2023 /* All delayed refs have been processed, Go ahead
2024 * and send the head node to run_one_delayed_ref,
2025 * so that any accounting fixes can happen
2026 */
2027 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002028
2029 if (extent_op && must_insert_reserved) {
2030 kfree(extent_op);
2031 extent_op = NULL;
2032 }
2033
2034 if (extent_op) {
2035 spin_unlock(&delayed_refs->lock);
2036
2037 ret = run_delayed_extent_op(trans, root,
2038 ref, extent_op);
2039 BUG_ON(ret);
2040 kfree(extent_op);
2041
2042 cond_resched();
2043 spin_lock(&delayed_refs->lock);
2044 continue;
2045 }
2046
Chris Masonc3e69d52009-03-13 10:17:05 -04002047 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002048 locked_ref = NULL;
2049 }
2050
2051 ref->in_tree = 0;
2052 rb_erase(&ref->rb_node, &delayed_refs->root);
2053 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002054
Chris Mason56bec292009-03-13 10:10:06 -04002055 spin_unlock(&delayed_refs->lock);
2056
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002057 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002058 must_insert_reserved);
2059 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002060
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002061 btrfs_put_delayed_ref(ref);
2062 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002063 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064
Chris Mason1887be62009-03-13 10:11:24 -04002065 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002066 spin_lock(&delayed_refs->lock);
2067 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002068 return count;
2069}
2070
2071/*
2072 * this starts processing the delayed reference count updates and
2073 * extent insertions we have queued up so far. count can be
2074 * 0, which means to process everything in the tree at the start
2075 * of the run (but not newly added entries), or it can be some target
2076 * number you'd like to process.
2077 */
2078int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2079 struct btrfs_root *root, unsigned long count)
2080{
2081 struct rb_node *node;
2082 struct btrfs_delayed_ref_root *delayed_refs;
2083 struct btrfs_delayed_ref_node *ref;
2084 struct list_head cluster;
2085 int ret;
2086 int run_all = count == (unsigned long)-1;
2087 int run_most = 0;
2088
2089 if (root == root->fs_info->extent_root)
2090 root = root->fs_info->tree_root;
2091
2092 delayed_refs = &trans->transaction->delayed_refs;
2093 INIT_LIST_HEAD(&cluster);
2094again:
2095 spin_lock(&delayed_refs->lock);
2096 if (count == 0) {
2097 count = delayed_refs->num_entries * 2;
2098 run_most = 1;
2099 }
2100 while (1) {
2101 if (!(run_all || run_most) &&
2102 delayed_refs->num_heads_ready < 64)
2103 break;
2104
2105 /*
2106 * go find something we can process in the rbtree. We start at
2107 * the beginning of the tree, and then build a cluster
2108 * of refs to process starting at the first one we are able to
2109 * lock
2110 */
2111 ret = btrfs_find_ref_cluster(trans, &cluster,
2112 delayed_refs->run_delayed_start);
2113 if (ret)
2114 break;
2115
2116 ret = run_clustered_refs(trans, root, &cluster);
2117 BUG_ON(ret < 0);
2118
2119 count -= min_t(unsigned long, ret, count);
2120
2121 if (count == 0)
2122 break;
2123 }
2124
Chris Mason56bec292009-03-13 10:10:06 -04002125 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002126 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002127 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002128 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002129 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002130
2131 while (node) {
2132 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2133 rb_node);
2134 if (btrfs_delayed_ref_is_head(ref)) {
2135 struct btrfs_delayed_ref_head *head;
2136
2137 head = btrfs_delayed_node_to_head(ref);
2138 atomic_inc(&ref->refs);
2139
2140 spin_unlock(&delayed_refs->lock);
2141 mutex_lock(&head->mutex);
2142 mutex_unlock(&head->mutex);
2143
2144 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002145 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002146 goto again;
2147 }
2148 node = rb_next(node);
2149 }
2150 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002151 schedule_timeout(1);
2152 goto again;
2153 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002154out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002155 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002156 return 0;
2157}
2158
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002159int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2160 struct btrfs_root *root,
2161 u64 bytenr, u64 num_bytes, u64 flags,
2162 int is_data)
2163{
2164 struct btrfs_delayed_extent_op *extent_op;
2165 int ret;
2166
2167 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2168 if (!extent_op)
2169 return -ENOMEM;
2170
2171 extent_op->flags_to_set = flags;
2172 extent_op->update_flags = 1;
2173 extent_op->update_key = 0;
2174 extent_op->is_data = is_data ? 1 : 0;
2175
2176 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2177 if (ret)
2178 kfree(extent_op);
2179 return ret;
2180}
2181
2182static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2183 struct btrfs_root *root,
2184 struct btrfs_path *path,
2185 u64 objectid, u64 offset, u64 bytenr)
2186{
2187 struct btrfs_delayed_ref_head *head;
2188 struct btrfs_delayed_ref_node *ref;
2189 struct btrfs_delayed_data_ref *data_ref;
2190 struct btrfs_delayed_ref_root *delayed_refs;
2191 struct rb_node *node;
2192 int ret = 0;
2193
2194 ret = -ENOENT;
2195 delayed_refs = &trans->transaction->delayed_refs;
2196 spin_lock(&delayed_refs->lock);
2197 head = btrfs_find_delayed_ref_head(trans, bytenr);
2198 if (!head)
2199 goto out;
2200
2201 if (!mutex_trylock(&head->mutex)) {
2202 atomic_inc(&head->node.refs);
2203 spin_unlock(&delayed_refs->lock);
2204
2205 btrfs_release_path(root->fs_info->extent_root, path);
2206
2207 mutex_lock(&head->mutex);
2208 mutex_unlock(&head->mutex);
2209 btrfs_put_delayed_ref(&head->node);
2210 return -EAGAIN;
2211 }
2212
2213 node = rb_prev(&head->node.rb_node);
2214 if (!node)
2215 goto out_unlock;
2216
2217 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2218
2219 if (ref->bytenr != bytenr)
2220 goto out_unlock;
2221
2222 ret = 1;
2223 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2224 goto out_unlock;
2225
2226 data_ref = btrfs_delayed_node_to_data_ref(ref);
2227
2228 node = rb_prev(node);
2229 if (node) {
2230 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2231 if (ref->bytenr == bytenr)
2232 goto out_unlock;
2233 }
2234
2235 if (data_ref->root != root->root_key.objectid ||
2236 data_ref->objectid != objectid || data_ref->offset != offset)
2237 goto out_unlock;
2238
2239 ret = 0;
2240out_unlock:
2241 mutex_unlock(&head->mutex);
2242out:
2243 spin_unlock(&delayed_refs->lock);
2244 return ret;
2245}
2246
2247static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2248 struct btrfs_root *root,
2249 struct btrfs_path *path,
2250 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002251{
2252 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002253 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002254 struct btrfs_extent_data_ref *ref;
2255 struct btrfs_extent_inline_ref *iref;
2256 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002257 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002258 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002259 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002260
Chris Masonbe20aa92007-12-17 20:14:01 -05002261 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002262 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002263 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002264
Chris Masonbe20aa92007-12-17 20:14:01 -05002265 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2266 if (ret < 0)
2267 goto out;
2268 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002269
2270 ret = -ENOENT;
2271 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002272 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002273
Zheng Yan31840ae2008-09-23 13:14:14 -04002274 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002275 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002276 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002277
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002278 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002279 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002280
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002281 ret = 1;
2282 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2283#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2284 if (item_size < sizeof(*ei)) {
2285 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2286 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002287 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288#endif
2289 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2290
2291 if (item_size != sizeof(*ei) +
2292 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2293 goto out;
2294
2295 if (btrfs_extent_generation(leaf, ei) <=
2296 btrfs_root_last_snapshot(&root->root_item))
2297 goto out;
2298
2299 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2300 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2301 BTRFS_EXTENT_DATA_REF_KEY)
2302 goto out;
2303
2304 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2305 if (btrfs_extent_refs(leaf, ei) !=
2306 btrfs_extent_data_ref_count(leaf, ref) ||
2307 btrfs_extent_data_ref_root(leaf, ref) !=
2308 root->root_key.objectid ||
2309 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2310 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2311 goto out;
2312
Yan Zhengf321e492008-07-30 09:26:11 -04002313 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002314out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002315 return ret;
2316}
2317
2318int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2319 struct btrfs_root *root,
2320 u64 objectid, u64 offset, u64 bytenr)
2321{
2322 struct btrfs_path *path;
2323 int ret;
2324 int ret2;
2325
2326 path = btrfs_alloc_path();
2327 if (!path)
2328 return -ENOENT;
2329
2330 do {
2331 ret = check_committed_ref(trans, root, path, objectid,
2332 offset, bytenr);
2333 if (ret && ret != -ENOENT)
2334 goto out;
2335
2336 ret2 = check_delayed_ref(trans, root, path, objectid,
2337 offset, bytenr);
2338 } while (ret2 == -EAGAIN);
2339
2340 if (ret2 && ret2 != -ENOENT) {
2341 ret = ret2;
2342 goto out;
2343 }
2344
2345 if (ret != -ENOENT || ret2 != -ENOENT)
2346 ret = 0;
2347out:
Yan Zhengf321e492008-07-30 09:26:11 -04002348 btrfs_free_path(path);
2349 return ret;
2350}
2351
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002352#if 0
Zheng Yan31840ae2008-09-23 13:14:14 -04002353int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2354 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05002355{
Chris Mason5f39d392007-10-15 16:14:19 -04002356 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002357 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04002358 u64 root_gen;
2359 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05002360 int i;
Chris Masondb945352007-10-15 16:15:53 -04002361 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04002362 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04002363 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05002364
Chris Mason3768f362007-03-13 16:47:54 -04002365 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05002366 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002367
Zheng Yane4657682008-09-26 10:04:53 -04002368 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2369 shared = 0;
2370 root_gen = root->root_key.offset;
2371 } else {
2372 shared = 1;
2373 root_gen = trans->transid - 1;
2374 }
2375
Chris Masondb945352007-10-15 16:15:53 -04002376 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002377 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04002378
Zheng Yan31840ae2008-09-23 13:14:14 -04002379 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04002380 struct btrfs_leaf_ref *ref;
2381 struct btrfs_extent_info *info;
2382
Zheng Yan31840ae2008-09-23 13:14:14 -04002383 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04002384 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002385 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04002386 goto out;
2387 }
2388
Zheng Yane4657682008-09-26 10:04:53 -04002389 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04002390 ref->bytenr = buf->start;
2391 ref->owner = btrfs_header_owner(buf);
2392 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002393 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04002394 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04002395
Zheng Yan31840ae2008-09-23 13:14:14 -04002396 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04002397 u64 disk_bytenr;
2398 btrfs_item_key_to_cpu(buf, &key, i);
2399 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2400 continue;
2401 fi = btrfs_item_ptr(buf, i,
2402 struct btrfs_file_extent_item);
2403 if (btrfs_file_extent_type(buf, fi) ==
2404 BTRFS_FILE_EXTENT_INLINE)
2405 continue;
2406 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2407 if (disk_bytenr == 0)
2408 continue;
2409
2410 info->bytenr = disk_bytenr;
2411 info->num_bytes =
2412 btrfs_file_extent_disk_num_bytes(buf, fi);
2413 info->objectid = key.objectid;
2414 info->offset = key.offset;
2415 info++;
2416 }
2417
Zheng Yane4657682008-09-26 10:04:53 -04002418 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002419 if (ret == -EEXIST && shared) {
2420 struct btrfs_leaf_ref *old;
2421 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2422 BUG_ON(!old);
2423 btrfs_remove_leaf_ref(root, old);
2424 btrfs_free_leaf_ref(root, old);
2425 ret = btrfs_add_leaf_ref(root, ref, shared);
2426 }
Yan Zheng31153d82008-07-28 15:32:19 -04002427 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04002428 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002429 }
2430out:
Zheng Yan31840ae2008-09-23 13:14:14 -04002431 return ret;
2432}
2433
Chris Masonb7a9f292009-02-04 09:23:45 -05002434/* when a block goes through cow, we update the reference counts of
2435 * everything that block points to. The internal pointers of the block
2436 * can be in just about any order, and it is likely to have clusters of
2437 * things that are close together and clusters of things that are not.
2438 *
2439 * To help reduce the seeks that come with updating all of these reference
2440 * counts, sort them by byte number before actual updates are done.
2441 *
2442 * struct refsort is used to match byte number to slot in the btree block.
2443 * we sort based on the byte number and then use the slot to actually
2444 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05002445 *
2446 * struct refsort is smaller than strcut btrfs_item and smaller than
2447 * struct btrfs_key_ptr. Since we're currently limited to the page size
2448 * for a btree block, there's no way for a kmalloc of refsorts for a
2449 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05002450 */
2451struct refsort {
2452 u64 bytenr;
2453 u32 slot;
2454};
2455
2456/*
2457 * for passing into sort()
2458 */
2459static int refsort_cmp(const void *a_void, const void *b_void)
2460{
2461 const struct refsort *a = a_void;
2462 const struct refsort *b = b_void;
2463
2464 if (a->bytenr < b->bytenr)
2465 return -1;
2466 if (a->bytenr > b->bytenr)
2467 return 1;
2468 return 0;
2469}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002470#endif
Chris Masonb7a9f292009-02-04 09:23:45 -05002471
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002472static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002473 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002474 struct extent_buffer *buf,
2475 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002476{
2477 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002478 u64 num_bytes;
2479 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002480 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002481 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002482 struct btrfs_key key;
2483 struct btrfs_file_extent_item *fi;
2484 int i;
2485 int level;
2486 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002487 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002488 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002489
2490 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002491 nritems = btrfs_header_nritems(buf);
2492 level = btrfs_header_level(buf);
2493
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002494 if (!root->ref_cows && level == 0)
2495 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002496
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002497 if (inc)
2498 process_func = btrfs_inc_extent_ref;
2499 else
2500 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002501
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002502 if (full_backref)
2503 parent = buf->start;
2504 else
2505 parent = 0;
2506
Zheng Yan31840ae2008-09-23 13:14:14 -04002507 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002508 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002509 btrfs_item_key_to_cpu(buf, &key, i);
2510 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002511 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002512 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002513 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002514 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002515 BTRFS_FILE_EXTENT_INLINE)
2516 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002517 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2518 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002519 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002520
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002521 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2522 key.offset -= btrfs_file_extent_offset(buf, fi);
2523 ret = process_func(trans, root, bytenr, num_bytes,
2524 parent, ref_root, key.objectid,
2525 key.offset);
2526 if (ret)
2527 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002528 } else {
2529 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002530 num_bytes = btrfs_level_size(root, level - 1);
2531 ret = process_func(trans, root, bytenr, num_bytes,
2532 parent, ref_root, level - 1, 0);
2533 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002534 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002535 }
2536 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002537 return 0;
2538fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002539 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002540 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002541}
2542
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002543int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2544 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002545{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002546 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2547}
Zheng Yan31840ae2008-09-23 13:14:14 -04002548
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002549int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2550 struct extent_buffer *buf, int full_backref)
2551{
2552 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002553}
2554
Chris Mason9078a3e2007-04-26 16:46:15 -04002555static int write_one_cache_group(struct btrfs_trans_handle *trans,
2556 struct btrfs_root *root,
2557 struct btrfs_path *path,
2558 struct btrfs_block_group_cache *cache)
2559{
2560 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002561 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002562 unsigned long bi;
2563 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002564
Chris Mason9078a3e2007-04-26 16:46:15 -04002565 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002566 if (ret < 0)
2567 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002568 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002569
2570 leaf = path->nodes[0];
2571 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2572 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2573 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04002574 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002575fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002576 if (ret)
2577 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002578 return 0;
2579
2580}
2581
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002582static struct btrfs_block_group_cache *
2583next_block_group(struct btrfs_root *root,
2584 struct btrfs_block_group_cache *cache)
2585{
2586 struct rb_node *node;
2587 spin_lock(&root->fs_info->block_group_cache_lock);
2588 node = rb_next(&cache->cache_node);
2589 btrfs_put_block_group(cache);
2590 if (node) {
2591 cache = rb_entry(node, struct btrfs_block_group_cache,
2592 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002593 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002594 } else
2595 cache = NULL;
2596 spin_unlock(&root->fs_info->block_group_cache_lock);
2597 return cache;
2598}
2599
Chris Mason96b51792007-10-15 16:15:19 -04002600int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2601 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002602{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002603 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002604 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002605 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002606 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002607
2608 path = btrfs_alloc_path();
2609 if (!path)
2610 return -ENOMEM;
2611
Chris Masond3977122009-01-05 21:25:51 -05002612 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002613 if (last == 0) {
2614 err = btrfs_run_delayed_refs(trans, root,
2615 (unsigned long)-1);
2616 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002617 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002618
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002619 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2620 while (cache) {
2621 if (cache->dirty)
2622 break;
2623 cache = next_block_group(root, cache);
2624 }
2625 if (!cache) {
2626 if (last == 0)
2627 break;
2628 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002629 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002630 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002631
2632 cache->dirty = 0;
2633 last = cache->key.objectid + cache->key.offset;
2634
2635 err = write_one_cache_group(trans, root, path, cache);
2636 BUG_ON(err);
2637 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002638 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002639
Chris Mason9078a3e2007-04-26 16:46:15 -04002640 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002641 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002642}
2643
Yan Zhengd2fb3432008-12-11 16:30:39 -05002644int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2645{
2646 struct btrfs_block_group_cache *block_group;
2647 int readonly = 0;
2648
2649 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2650 if (!block_group || block_group->ro)
2651 readonly = 1;
2652 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002653 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002654 return readonly;
2655}
2656
Chris Mason593060d2008-03-25 16:50:33 -04002657static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2658 u64 total_bytes, u64 bytes_used,
2659 struct btrfs_space_info **space_info)
2660{
2661 struct btrfs_space_info *found;
2662
2663 found = __find_space_info(info, flags);
2664 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002665 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002666 found->total_bytes += total_bytes;
2667 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04002668 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002669 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002670 *space_info = found;
2671 return 0;
2672 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002673 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002674 if (!found)
2675 return -ENOMEM;
2676
Josef Bacik0f9dd462008-09-23 13:14:11 -04002677 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04002678 init_rwsem(&found->groups_sem);
Josef Bacikb5cb1602010-03-12 19:28:18 +00002679 init_waitqueue_head(&found->flush_wait);
2680 init_waitqueue_head(&found->allocate_wait);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002681 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002682 found->flags = flags;
2683 found->total_bytes = total_bytes;
2684 found->bytes_used = bytes_used;
2685 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002686 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002687 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002688 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002689 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002690 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002691 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002692 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002693 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002694 return 0;
2695}
2696
Chris Mason8790d502008-04-03 16:29:03 -04002697static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2698{
2699 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002700 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002701 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002702 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002703 if (extra_flags) {
2704 if (flags & BTRFS_BLOCK_GROUP_DATA)
2705 fs_info->avail_data_alloc_bits |= extra_flags;
2706 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2707 fs_info->avail_metadata_alloc_bits |= extra_flags;
2708 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2709 fs_info->avail_system_alloc_bits |= extra_flags;
2710 }
2711}
Chris Mason593060d2008-03-25 16:50:33 -04002712
Yan Zhengc146afa2008-11-12 14:34:12 -05002713static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2714{
2715 spin_lock(&cache->space_info->lock);
2716 spin_lock(&cache->lock);
2717 if (!cache->ro) {
2718 cache->space_info->bytes_readonly += cache->key.offset -
2719 btrfs_block_group_used(&cache->item);
2720 cache->ro = 1;
2721 }
2722 spin_unlock(&cache->lock);
2723 spin_unlock(&cache->space_info->lock);
2724}
2725
Yan Zheng2b820322008-11-17 21:11:30 -05002726u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002727{
Yan Zheng2b820322008-11-17 21:11:30 -05002728 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002729
2730 if (num_devices == 1)
2731 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2732 if (num_devices < 4)
2733 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2734
Chris Masonec44a352008-04-28 15:29:52 -04002735 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2736 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002737 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002738 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002739 }
Chris Masonec44a352008-04-28 15:29:52 -04002740
2741 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002742 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002743 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002744 }
Chris Masonec44a352008-04-28 15:29:52 -04002745
2746 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2747 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2748 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2749 (flags & BTRFS_BLOCK_GROUP_DUP)))
2750 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2751 return flags;
2752}
2753
Josef Bacik6a632092009-02-20 11:00:09 -05002754static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2755{
2756 struct btrfs_fs_info *info = root->fs_info;
2757 u64 alloc_profile;
2758
2759 if (data) {
2760 alloc_profile = info->avail_data_alloc_bits &
2761 info->data_alloc_profile;
2762 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2763 } else if (root == root->fs_info->chunk_root) {
2764 alloc_profile = info->avail_system_alloc_bits &
2765 info->system_alloc_profile;
2766 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2767 } else {
2768 alloc_profile = info->avail_metadata_alloc_bits &
2769 info->metadata_alloc_profile;
2770 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2771 }
2772
2773 return btrfs_reduce_alloc_profile(root, data);
2774}
2775
2776void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2777{
2778 u64 alloc_target;
2779
2780 alloc_target = btrfs_get_alloc_profile(root, 1);
2781 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2782 alloc_target);
2783}
2784
Josef Bacik9ed74f22009-09-11 16:12:44 -04002785static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2786{
2787 u64 num_bytes;
2788 int level;
2789
2790 level = BTRFS_MAX_LEVEL - 2;
2791 /*
2792 * NOTE: these calculations are absolutely the worst possible case.
2793 * This assumes that _every_ item we insert will require a new leaf, and
2794 * that the tree has grown to its maximum level size.
2795 */
2796
2797 /*
2798 * for every item we insert we could insert both an extent item and a
2799 * extent ref item. Then for ever item we insert, we will need to cow
2800 * both the original leaf, plus the leaf to the left and right of it.
2801 *
2802 * Unless we are talking about the extent root, then we just want the
2803 * number of items * 2, since we just need the extent item plus its ref.
2804 */
2805 if (root == root->fs_info->extent_root)
2806 num_bytes = num_items * 2;
2807 else
2808 num_bytes = (num_items + (2 * num_items)) * 3;
2809
2810 /*
2811 * num_bytes is total number of leaves we could need times the leaf
2812 * size, and then for every leaf we could end up cow'ing 2 nodes per
2813 * level, down to the leaf level.
2814 */
2815 num_bytes = (num_bytes * root->leafsize) +
2816 (num_bytes * (level * 2)) * root->nodesize;
2817
2818 return num_bytes;
2819}
2820
Josef Bacik6a632092009-02-20 11:00:09 -05002821/*
Josef Bacik9ed74f22009-09-11 16:12:44 -04002822 * Unreserve metadata space for delalloc. If we have less reserved credits than
2823 * we have extents, this function does nothing.
Josef Bacik6a632092009-02-20 11:00:09 -05002824 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04002825int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2826 struct inode *inode, int num_items)
Josef Bacik6a632092009-02-20 11:00:09 -05002827{
2828 struct btrfs_fs_info *info = root->fs_info;
2829 struct btrfs_space_info *meta_sinfo;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002830 u64 num_bytes;
2831 u64 alloc_target;
2832 bool bug = false;
Josef Bacik6a632092009-02-20 11:00:09 -05002833
2834 /* get the space info for where the metadata will live */
2835 alloc_target = btrfs_get_alloc_profile(root, 0);
2836 meta_sinfo = __find_space_info(info, alloc_target);
2837
Josef Bacik9ed74f22009-09-11 16:12:44 -04002838 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2839 num_items);
2840
2841 spin_lock(&meta_sinfo->lock);
Josef Bacik32c00af2009-10-08 13:34:05 -04002842 spin_lock(&BTRFS_I(inode)->accounting_lock);
2843 if (BTRFS_I(inode)->reserved_extents <=
2844 BTRFS_I(inode)->outstanding_extents) {
2845 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002846 spin_unlock(&meta_sinfo->lock);
2847 return 0;
2848 }
Josef Bacik32c00af2009-10-08 13:34:05 -04002849 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002850
Josef Bacik287a0ab2010-03-19 18:07:23 +00002851 BTRFS_I(inode)->reserved_extents -= num_items;
Josef Bacik32c00af2009-10-08 13:34:05 -04002852 BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002853
2854 if (meta_sinfo->bytes_delalloc < num_bytes) {
2855 bug = true;
2856 meta_sinfo->bytes_delalloc = 0;
2857 } else {
2858 meta_sinfo->bytes_delalloc -= num_bytes;
2859 }
2860 spin_unlock(&meta_sinfo->lock);
2861
2862 BUG_ON(bug);
2863
2864 return 0;
2865}
2866
2867static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2868{
2869 u64 thresh;
2870
2871 thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2872 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2873 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2874 meta_sinfo->bytes_may_use;
2875
2876 thresh = meta_sinfo->total_bytes - thresh;
2877 thresh *= 80;
2878 do_div(thresh, 100);
2879 if (thresh <= meta_sinfo->bytes_delalloc)
2880 meta_sinfo->force_delalloc = 1;
2881 else
2882 meta_sinfo->force_delalloc = 0;
2883}
2884
Josef Bacike3ccfa92009-10-07 20:44:34 -04002885struct async_flush {
2886 struct btrfs_root *root;
2887 struct btrfs_space_info *info;
2888 struct btrfs_work work;
2889};
2890
2891static noinline void flush_delalloc_async(struct btrfs_work *work)
2892{
2893 struct async_flush *async;
2894 struct btrfs_root *root;
2895 struct btrfs_space_info *info;
2896
2897 async = container_of(work, struct async_flush, work);
2898 root = async->root;
2899 info = async->info;
2900
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002901 btrfs_start_delalloc_inodes(root, 0);
Josef Bacike3ccfa92009-10-07 20:44:34 -04002902 wake_up(&info->flush_wait);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002903 btrfs_wait_ordered_extents(root, 0, 0);
Josef Bacike3ccfa92009-10-07 20:44:34 -04002904
2905 spin_lock(&info->lock);
2906 info->flushing = 0;
2907 spin_unlock(&info->lock);
2908 wake_up(&info->flush_wait);
2909
2910 kfree(async);
2911}
2912
2913static void wait_on_flush(struct btrfs_space_info *info)
2914{
2915 DEFINE_WAIT(wait);
2916 u64 used;
2917
2918 while (1) {
2919 prepare_to_wait(&info->flush_wait, &wait,
2920 TASK_UNINTERRUPTIBLE);
2921 spin_lock(&info->lock);
2922 if (!info->flushing) {
2923 spin_unlock(&info->lock);
2924 break;
2925 }
2926
2927 used = info->bytes_used + info->bytes_reserved +
2928 info->bytes_pinned + info->bytes_readonly +
2929 info->bytes_super + info->bytes_root +
2930 info->bytes_may_use + info->bytes_delalloc;
2931 if (used < info->total_bytes) {
2932 spin_unlock(&info->lock);
2933 break;
2934 }
2935 spin_unlock(&info->lock);
2936 schedule();
2937 }
2938 finish_wait(&info->flush_wait, &wait);
2939}
2940
Josef Bacik32c00af2009-10-08 13:34:05 -04002941static void flush_delalloc(struct btrfs_root *root,
2942 struct btrfs_space_info *info)
2943{
Josef Bacike3ccfa92009-10-07 20:44:34 -04002944 struct async_flush *async;
Josef Bacik32c00af2009-10-08 13:34:05 -04002945 bool wait = false;
2946
2947 spin_lock(&info->lock);
2948
Josef Bacikb5cb1602010-03-12 19:28:18 +00002949 if (!info->flushing)
Josef Bacik32c00af2009-10-08 13:34:05 -04002950 info->flushing = 1;
Josef Bacikb5cb1602010-03-12 19:28:18 +00002951 else
Josef Bacik32c00af2009-10-08 13:34:05 -04002952 wait = true;
Josef Bacik32c00af2009-10-08 13:34:05 -04002953
2954 spin_unlock(&info->lock);
2955
2956 if (wait) {
Josef Bacike3ccfa92009-10-07 20:44:34 -04002957 wait_on_flush(info);
Josef Bacik32c00af2009-10-08 13:34:05 -04002958 return;
2959 }
2960
Josef Bacike3ccfa92009-10-07 20:44:34 -04002961 async = kzalloc(sizeof(*async), GFP_NOFS);
2962 if (!async)
2963 goto flush;
2964
2965 async->root = root;
2966 async->info = info;
2967 async->work.func = flush_delalloc_async;
2968
2969 btrfs_queue_worker(&root->fs_info->enospc_workers,
2970 &async->work);
2971 wait_on_flush(info);
2972 return;
2973
2974flush:
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002975 btrfs_start_delalloc_inodes(root, 0);
2976 btrfs_wait_ordered_extents(root, 0, 0);
Josef Bacik32c00af2009-10-08 13:34:05 -04002977
2978 spin_lock(&info->lock);
2979 info->flushing = 0;
2980 spin_unlock(&info->lock);
2981 wake_up(&info->flush_wait);
2982}
2983
Josef Bacik9ed74f22009-09-11 16:12:44 -04002984static int maybe_allocate_chunk(struct btrfs_root *root,
2985 struct btrfs_space_info *info)
2986{
2987 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
2988 struct btrfs_trans_handle *trans;
2989 bool wait = false;
2990 int ret = 0;
2991 u64 min_metadata;
2992 u64 free_space;
2993
2994 free_space = btrfs_super_total_bytes(disk_super);
2995 /*
Chris Mason33b25802009-11-11 10:16:57 -05002996 * we allow the metadata to grow to a max of either 10gb or 5% of the
Josef Bacik9ed74f22009-09-11 16:12:44 -04002997 * space in the volume.
2998 */
Chris Mason33b25802009-11-11 10:16:57 -05002999 min_metadata = min((u64)10 * 1024 * 1024 * 1024,
Josef Bacik9ed74f22009-09-11 16:12:44 -04003000 div64_u64(free_space * 5, 100));
3001 if (info->total_bytes >= min_metadata) {
3002 spin_unlock(&info->lock);
3003 return 0;
3004 }
3005
3006 if (info->full) {
3007 spin_unlock(&info->lock);
3008 return 0;
3009 }
3010
3011 if (!info->allocating_chunk) {
3012 info->force_alloc = 1;
3013 info->allocating_chunk = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003014 } else {
3015 wait = true;
3016 }
3017
3018 spin_unlock(&info->lock);
3019
3020 if (wait) {
Josef Bacike3ccfa92009-10-07 20:44:34 -04003021 wait_event(info->allocate_wait,
Josef Bacik9ed74f22009-09-11 16:12:44 -04003022 !info->allocating_chunk);
3023 return 1;
3024 }
3025
3026 trans = btrfs_start_transaction(root, 1);
3027 if (!trans) {
3028 ret = -ENOMEM;
3029 goto out;
3030 }
3031
3032 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3033 4096 + 2 * 1024 * 1024,
3034 info->flags, 0);
3035 btrfs_end_transaction(trans, root);
3036 if (ret)
3037 goto out;
3038out:
3039 spin_lock(&info->lock);
3040 info->allocating_chunk = 0;
3041 spin_unlock(&info->lock);
Josef Bacike3ccfa92009-10-07 20:44:34 -04003042 wake_up(&info->allocate_wait);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003043
3044 if (ret)
3045 return 0;
3046 return 1;
3047}
3048
3049/*
3050 * Reserve metadata space for delalloc.
3051 */
3052int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
3053 struct inode *inode, int num_items)
3054{
3055 struct btrfs_fs_info *info = root->fs_info;
3056 struct btrfs_space_info *meta_sinfo;
3057 u64 num_bytes;
3058 u64 used;
3059 u64 alloc_target;
3060 int flushed = 0;
3061 int force_delalloc;
3062
3063 /* get the space info for where the metadata will live */
3064 alloc_target = btrfs_get_alloc_profile(root, 0);
3065 meta_sinfo = __find_space_info(info, alloc_target);
3066
3067 num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
3068 num_items);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003069again:
Josef Bacik6a632092009-02-20 11:00:09 -05003070 spin_lock(&meta_sinfo->lock);
Josef Bacik6a632092009-02-20 11:00:09 -05003071
Josef Bacik9ed74f22009-09-11 16:12:44 -04003072 force_delalloc = meta_sinfo->force_delalloc;
Josef Bacik6a632092009-02-20 11:00:09 -05003073
Josef Bacik9ed74f22009-09-11 16:12:44 -04003074 if (unlikely(!meta_sinfo->bytes_root))
3075 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3076
3077 if (!flushed)
3078 meta_sinfo->bytes_delalloc += num_bytes;
3079
3080 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3081 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3082 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3083 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3084
3085 if (used > meta_sinfo->total_bytes) {
3086 flushed++;
3087
3088 if (flushed == 1) {
3089 if (maybe_allocate_chunk(root, meta_sinfo))
3090 goto again;
3091 flushed++;
3092 } else {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003093 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003094 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003095
Josef Bacik9ed74f22009-09-11 16:12:44 -04003096 if (flushed == 2) {
3097 filemap_flush(inode->i_mapping);
3098 goto again;
3099 } else if (flushed == 3) {
Josef Bacik32c00af2009-10-08 13:34:05 -04003100 flush_delalloc(root, meta_sinfo);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003101 goto again;
3102 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003103 spin_lock(&meta_sinfo->lock);
3104 meta_sinfo->bytes_delalloc -= num_bytes;
Josef Bacik6a632092009-02-20 11:00:09 -05003105 spin_unlock(&meta_sinfo->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003106 printk(KERN_ERR "enospc, has %d, reserved %d\n",
Josef Bacik32c00af2009-10-08 13:34:05 -04003107 BTRFS_I(inode)->outstanding_extents,
3108 BTRFS_I(inode)->reserved_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003109 dump_space_info(meta_sinfo, 0, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003110 return -ENOSPC;
3111 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003112
Josef Bacik287a0ab2010-03-19 18:07:23 +00003113 BTRFS_I(inode)->reserved_extents += num_items;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003114 check_force_delalloc(meta_sinfo);
3115 spin_unlock(&meta_sinfo->lock);
3116
3117 if (!flushed && force_delalloc)
3118 filemap_flush(inode->i_mapping);
3119
3120 return 0;
3121}
3122
3123/*
3124 * unreserve num_items number of items worth of metadata space. This needs to
3125 * be paired with btrfs_reserve_metadata_space.
3126 *
3127 * NOTE: if you have the option, run this _AFTER_ you do a
3128 * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
3129 * oprations which will result in more used metadata, so we want to make sure we
3130 * can do that without issue.
3131 */
3132int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
3133{
3134 struct btrfs_fs_info *info = root->fs_info;
3135 struct btrfs_space_info *meta_sinfo;
3136 u64 num_bytes;
3137 u64 alloc_target;
3138 bool bug = false;
3139
3140 /* get the space info for where the metadata will live */
3141 alloc_target = btrfs_get_alloc_profile(root, 0);
3142 meta_sinfo = __find_space_info(info, alloc_target);
3143
3144 num_bytes = calculate_bytes_needed(root, num_items);
3145
3146 spin_lock(&meta_sinfo->lock);
3147 if (meta_sinfo->bytes_may_use < num_bytes) {
3148 bug = true;
3149 meta_sinfo->bytes_may_use = 0;
3150 } else {
3151 meta_sinfo->bytes_may_use -= num_bytes;
3152 }
3153 spin_unlock(&meta_sinfo->lock);
3154
3155 BUG_ON(bug);
3156
3157 return 0;
3158}
3159
3160/*
3161 * Reserve some metadata space for use. We'll calculate the worste case number
3162 * of bytes that would be needed to modify num_items number of items. If we
3163 * have space, fantastic, if not, you get -ENOSPC. Please call
3164 * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
3165 * items you reserved, since whatever metadata you needed should have already
3166 * been allocated.
3167 *
3168 * This will commit the transaction to make more space if we don't have enough
3169 * metadata space. THe only time we don't do this is if we're reserving space
3170 * inside of a transaction, then we will just return -ENOSPC and it is the
3171 * callers responsibility to handle it properly.
3172 */
3173int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3174{
3175 struct btrfs_fs_info *info = root->fs_info;
3176 struct btrfs_space_info *meta_sinfo;
3177 u64 num_bytes;
3178 u64 used;
3179 u64 alloc_target;
3180 int retries = 0;
3181
3182 /* get the space info for where the metadata will live */
3183 alloc_target = btrfs_get_alloc_profile(root, 0);
3184 meta_sinfo = __find_space_info(info, alloc_target);
3185
3186 num_bytes = calculate_bytes_needed(root, num_items);
3187again:
3188 spin_lock(&meta_sinfo->lock);
3189
3190 if (unlikely(!meta_sinfo->bytes_root))
3191 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3192
3193 if (!retries)
3194 meta_sinfo->bytes_may_use += num_bytes;
3195
3196 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3197 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3198 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3199 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3200
3201 if (used > meta_sinfo->total_bytes) {
3202 retries++;
3203 if (retries == 1) {
3204 if (maybe_allocate_chunk(root, meta_sinfo))
3205 goto again;
3206 retries++;
3207 } else {
3208 spin_unlock(&meta_sinfo->lock);
3209 }
3210
3211 if (retries == 2) {
Josef Bacik32c00af2009-10-08 13:34:05 -04003212 flush_delalloc(root, meta_sinfo);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003213 goto again;
3214 }
3215 spin_lock(&meta_sinfo->lock);
3216 meta_sinfo->bytes_may_use -= num_bytes;
3217 spin_unlock(&meta_sinfo->lock);
3218
3219 dump_space_info(meta_sinfo, 0, 0);
3220 return -ENOSPC;
3221 }
3222
3223 check_force_delalloc(meta_sinfo);
Josef Bacik6a632092009-02-20 11:00:09 -05003224 spin_unlock(&meta_sinfo->lock);
3225
3226 return 0;
3227}
3228
3229/*
3230 * This will check the space that the inode allocates from to make sure we have
3231 * enough space for bytes.
3232 */
3233int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3234 u64 bytes)
3235{
3236 struct btrfs_space_info *data_sinfo;
Josef Bacikab6e24102010-03-19 14:38:13 +00003237 u64 used;
3238 int ret = 0, committed = 0, flushed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003239
3240 /* make sure bytes are sectorsize aligned */
3241 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3242
3243 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003244 if (!data_sinfo)
3245 goto alloc;
3246
Josef Bacik6a632092009-02-20 11:00:09 -05003247again:
3248 /* make sure we have enough space to handle the data first */
3249 spin_lock(&data_sinfo->lock);
Josef Bacikab6e24102010-03-19 14:38:13 +00003250 used = data_sinfo->bytes_used + data_sinfo->bytes_delalloc +
3251 data_sinfo->bytes_reserved + data_sinfo->bytes_pinned +
3252 data_sinfo->bytes_readonly + data_sinfo->bytes_may_use +
3253 data_sinfo->bytes_super;
3254
3255 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003256 struct btrfs_trans_handle *trans;
3257
Josef Bacikab6e24102010-03-19 14:38:13 +00003258 if (!flushed) {
3259 spin_unlock(&data_sinfo->lock);
3260 flush_delalloc(root, data_sinfo);
3261 flushed = 1;
3262 goto again;
3263 }
3264
Josef Bacik6a632092009-02-20 11:00:09 -05003265 /*
3266 * if we don't have enough free bytes in this space then we need
3267 * to alloc a new chunk.
3268 */
3269 if (!data_sinfo->full) {
3270 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003271
3272 data_sinfo->force_alloc = 1;
3273 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003274alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003275 alloc_target = btrfs_get_alloc_profile(root, 1);
3276 trans = btrfs_start_transaction(root, 1);
3277 if (!trans)
3278 return -ENOMEM;
3279
3280 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3281 bytes + 2 * 1024 * 1024,
3282 alloc_target, 0);
3283 btrfs_end_transaction(trans, root);
3284 if (ret)
3285 return ret;
Chris Mason33b4d472009-09-22 14:45:50 -04003286
3287 if (!data_sinfo) {
3288 btrfs_set_inode_space_info(root, inode);
3289 data_sinfo = BTRFS_I(inode)->space_info;
3290 }
Josef Bacik6a632092009-02-20 11:00:09 -05003291 goto again;
3292 }
3293 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003294
3295 /* commit the current transaction and try again */
Sage Weildd7e0b72009-09-29 18:38:44 -04003296 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003297 committed = 1;
3298 trans = btrfs_join_transaction(root, 1);
3299 if (!trans)
3300 return -ENOMEM;
3301 ret = btrfs_commit_transaction(trans, root);
3302 if (ret)
3303 return ret;
3304 goto again;
3305 }
3306
Josef Bacik6a632092009-02-20 11:00:09 -05003307 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3308 ", %llu bytes_used, %llu bytes_reserved, "
Hu Tao68f5a382009-07-02 13:55:45 -04003309 "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
Joel Becker21380932009-04-21 12:38:29 -07003310 "%llu total\n", (unsigned long long)bytes,
3311 (unsigned long long)data_sinfo->bytes_delalloc,
3312 (unsigned long long)data_sinfo->bytes_used,
3313 (unsigned long long)data_sinfo->bytes_reserved,
3314 (unsigned long long)data_sinfo->bytes_pinned,
3315 (unsigned long long)data_sinfo->bytes_readonly,
3316 (unsigned long long)data_sinfo->bytes_may_use,
3317 (unsigned long long)data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003318 return -ENOSPC;
3319 }
3320 data_sinfo->bytes_may_use += bytes;
3321 BTRFS_I(inode)->reserved_bytes += bytes;
3322 spin_unlock(&data_sinfo->lock);
3323
Josef Bacik9ed74f22009-09-11 16:12:44 -04003324 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003325}
3326
3327/*
3328 * if there was an error for whatever reason after calling
3329 * btrfs_check_data_free_space, call this so we can cleanup the counters.
3330 */
3331void btrfs_free_reserved_data_space(struct btrfs_root *root,
3332 struct inode *inode, u64 bytes)
3333{
3334 struct btrfs_space_info *data_sinfo;
3335
3336 /* make sure bytes are sectorsize aligned */
3337 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3338
3339 data_sinfo = BTRFS_I(inode)->space_info;
3340 spin_lock(&data_sinfo->lock);
3341 data_sinfo->bytes_may_use -= bytes;
3342 BTRFS_I(inode)->reserved_bytes -= bytes;
3343 spin_unlock(&data_sinfo->lock);
3344}
3345
3346/* called when we are adding a delalloc extent to the inode's io_tree */
3347void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3348 u64 bytes)
3349{
3350 struct btrfs_space_info *data_sinfo;
3351
3352 /* get the space info for where this inode will be storing its data */
3353 data_sinfo = BTRFS_I(inode)->space_info;
3354
3355 /* make sure we have enough space to handle the data first */
3356 spin_lock(&data_sinfo->lock);
3357 data_sinfo->bytes_delalloc += bytes;
3358
3359 /*
3360 * we are adding a delalloc extent without calling
3361 * btrfs_check_data_free_space first. This happens on a weird
3362 * writepage condition, but shouldn't hurt our accounting
3363 */
3364 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3365 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3366 BTRFS_I(inode)->reserved_bytes = 0;
3367 } else {
3368 data_sinfo->bytes_may_use -= bytes;
3369 BTRFS_I(inode)->reserved_bytes -= bytes;
3370 }
3371
3372 spin_unlock(&data_sinfo->lock);
3373}
3374
3375/* called when we are clearing an delalloc extent from the inode's io_tree */
3376void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3377 u64 bytes)
3378{
3379 struct btrfs_space_info *info;
3380
3381 info = BTRFS_I(inode)->space_info;
3382
3383 spin_lock(&info->lock);
3384 info->bytes_delalloc -= bytes;
3385 spin_unlock(&info->lock);
3386}
3387
Josef Bacik97e728d2009-04-21 17:40:57 -04003388static void force_metadata_allocation(struct btrfs_fs_info *info)
3389{
3390 struct list_head *head = &info->space_info;
3391 struct btrfs_space_info *found;
3392
3393 rcu_read_lock();
3394 list_for_each_entry_rcu(found, head, list) {
3395 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3396 found->force_alloc = 1;
3397 }
3398 rcu_read_unlock();
3399}
3400
Chris Mason6324fbf2008-03-24 15:01:59 -04003401static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3402 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003403 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003404{
3405 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003406 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003407 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05003408 int ret = 0;
3409
Josef Bacik97e728d2009-04-21 17:40:57 -04003410 mutex_lock(&fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04003411
Yan Zheng2b820322008-11-17 21:11:30 -05003412 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003413
Chris Mason6324fbf2008-03-24 15:01:59 -04003414 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003415 if (!space_info) {
3416 ret = update_space_info(extent_root->fs_info, flags,
3417 0, 0, &space_info);
3418 BUG_ON(ret);
3419 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003420 BUG_ON(!space_info);
3421
Josef Bacik25179202008-10-29 14:49:05 -04003422 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003423 if (space_info->force_alloc)
Chris Mason0ef3e662008-05-24 14:04:53 -04003424 force = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003425 if (space_info->full) {
3426 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003427 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003428 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003429
Yan Zhengc146afa2008-11-12 14:34:12 -05003430 thresh = space_info->total_bytes - space_info->bytes_readonly;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003431 thresh = div_factor(thresh, 8);
Chris Mason0ef3e662008-05-24 14:04:53 -04003432 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04003433 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04003434 space_info->bytes_reserved + alloc_bytes) < thresh) {
3435 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04003436 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04003437 }
Josef Bacik25179202008-10-29 14:49:05 -04003438 spin_unlock(&space_info->lock);
3439
Josef Bacik97e728d2009-04-21 17:40:57 -04003440 /*
3441 * if we're doing a data chunk, go ahead and make sure that
3442 * we keep a reasonable number of metadata chunks allocated in the
3443 * FS as well.
3444 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003445 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003446 fs_info->data_chunk_allocations++;
3447 if (!(fs_info->data_chunk_allocations %
3448 fs_info->metadata_ratio))
3449 force_metadata_allocation(fs_info);
3450 }
3451
Yan Zheng2b820322008-11-17 21:11:30 -05003452 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003453 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003454 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003455 space_info->full = 1;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003456 space_info->force_alloc = 0;
3457 spin_unlock(&space_info->lock);
Chris Masona74a4b92008-06-25 16:01:31 -04003458out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003459 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003460 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003461}
3462
Chris Mason9078a3e2007-04-26 16:46:15 -04003463static int update_block_group(struct btrfs_trans_handle *trans,
3464 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04003465 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04003466 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04003467{
3468 struct btrfs_block_group_cache *cache;
3469 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04003470 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003471 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003472 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04003473
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003474 /* block accounting for super block */
3475 spin_lock(&info->delalloc_lock);
3476 old_val = btrfs_super_bytes_used(&info->super_copy);
3477 if (alloc)
3478 old_val += num_bytes;
3479 else
3480 old_val -= num_bytes;
3481 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003482 spin_unlock(&info->delalloc_lock);
3483
Chris Masond3977122009-01-05 21:25:51 -05003484 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003485 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003486 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04003487 return -1;
Chris Masondb945352007-10-15 16:15:53 -04003488 byte_in_group = bytenr - cache->key.objectid;
3489 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003490
Josef Bacik25179202008-10-29 14:49:05 -04003491 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003492 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003493 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04003494 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04003495 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04003496 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04003497 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003498 btrfs_set_block_group_used(&cache->item, old_val);
3499 cache->reserved -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003500 cache->space_info->bytes_used += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003501 cache->space_info->bytes_reserved -= num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003502 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05003503 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003504 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003505 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04003506 } else {
Chris Masondb945352007-10-15 16:15:53 -04003507 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04003508 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05003509 if (cache->ro)
3510 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04003511 btrfs_set_block_group_used(&cache->item, old_val);
3512 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003513 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04003514 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003515 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003516
3517 ret = btrfs_discard_extent(root, bytenr,
3518 num_bytes);
3519 WARN_ON(ret);
3520
Josef Bacik0f9dd462008-09-23 13:14:11 -04003521 ret = btrfs_add_free_space(cache, bytenr,
3522 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003523 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04003524 }
Chris Masoncd1bc462007-04-27 10:08:34 -04003525 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003526 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04003527 total -= num_bytes;
3528 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003529 }
3530 return 0;
3531}
Chris Mason6324fbf2008-03-24 15:01:59 -04003532
Chris Masona061fc82008-05-07 11:43:44 -04003533static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3534{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003535 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003536 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003537
3538 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3539 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04003540 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003541
Yan Zhengd2fb3432008-12-11 16:30:39 -05003542 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003543 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003544
3545 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04003546}
3547
Yan Zheng11833d62009-09-11 16:11:19 -04003548/*
3549 * this function must be called within transaction
3550 */
3551int btrfs_pin_extent(struct btrfs_root *root,
3552 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05003553{
Yan324ae4d2007-11-16 14:57:08 -05003554 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -04003555 struct btrfs_block_group_cache *cache;
Yan324ae4d2007-11-16 14:57:08 -05003556
Yan Zheng11833d62009-09-11 16:11:19 -04003557 cache = btrfs_lookup_block_group(fs_info, bytenr);
3558 BUG_ON(!cache);
Chris Masonb9473432009-03-13 11:00:37 -04003559
Yan Zheng11833d62009-09-11 16:11:19 -04003560 spin_lock(&cache->space_info->lock);
3561 spin_lock(&cache->lock);
3562 cache->pinned += num_bytes;
3563 cache->space_info->bytes_pinned += num_bytes;
3564 if (reserved) {
3565 cache->reserved -= num_bytes;
3566 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05003567 }
Yan Zheng11833d62009-09-11 16:11:19 -04003568 spin_unlock(&cache->lock);
3569 spin_unlock(&cache->space_info->lock);
3570
3571 btrfs_put_block_group(cache);
3572
3573 set_extent_dirty(fs_info->pinned_extents,
3574 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
Yan324ae4d2007-11-16 14:57:08 -05003575 return 0;
3576}
Chris Mason9078a3e2007-04-26 16:46:15 -04003577
Yan Zheng11833d62009-09-11 16:11:19 -04003578static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3579 u64 num_bytes, int reserve)
Zheng Yane8569812008-09-26 10:05:48 -04003580{
Yan Zheng11833d62009-09-11 16:11:19 -04003581 spin_lock(&cache->space_info->lock);
3582 spin_lock(&cache->lock);
3583 if (reserve) {
3584 cache->reserved += num_bytes;
3585 cache->space_info->bytes_reserved += num_bytes;
3586 } else {
3587 cache->reserved -= num_bytes;
3588 cache->space_info->bytes_reserved -= num_bytes;
3589 }
3590 spin_unlock(&cache->lock);
3591 spin_unlock(&cache->space_info->lock);
3592 return 0;
3593}
Zheng Yane8569812008-09-26 10:05:48 -04003594
Yan Zheng11833d62009-09-11 16:11:19 -04003595int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3596 struct btrfs_root *root)
3597{
3598 struct btrfs_fs_info *fs_info = root->fs_info;
3599 struct btrfs_caching_control *next;
3600 struct btrfs_caching_control *caching_ctl;
3601 struct btrfs_block_group_cache *cache;
3602
3603 down_write(&fs_info->extent_commit_sem);
3604
3605 list_for_each_entry_safe(caching_ctl, next,
3606 &fs_info->caching_block_groups, list) {
3607 cache = caching_ctl->block_group;
3608 if (block_group_cache_done(cache)) {
3609 cache->last_byte_to_unpin = (u64)-1;
3610 list_del_init(&caching_ctl->list);
3611 put_caching_control(caching_ctl);
3612 } else {
3613 cache->last_byte_to_unpin = caching_ctl->progress;
3614 }
3615 }
3616
3617 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3618 fs_info->pinned_extents = &fs_info->freed_extents[1];
3619 else
3620 fs_info->pinned_extents = &fs_info->freed_extents[0];
3621
3622 up_write(&fs_info->extent_commit_sem);
3623 return 0;
3624}
3625
3626static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3627{
3628 struct btrfs_fs_info *fs_info = root->fs_info;
3629 struct btrfs_block_group_cache *cache = NULL;
3630 u64 len;
3631
3632 while (start <= end) {
3633 if (!cache ||
3634 start >= cache->key.objectid + cache->key.offset) {
3635 if (cache)
3636 btrfs_put_block_group(cache);
3637 cache = btrfs_lookup_block_group(fs_info, start);
3638 BUG_ON(!cache);
3639 }
3640
3641 len = cache->key.objectid + cache->key.offset - start;
3642 len = min(len, end + 1 - start);
3643
3644 if (start < cache->last_byte_to_unpin) {
3645 len = min(len, cache->last_byte_to_unpin - start);
3646 btrfs_add_free_space(cache, start, len);
3647 }
Josef Bacik25179202008-10-29 14:49:05 -04003648
3649 spin_lock(&cache->space_info->lock);
3650 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003651 cache->pinned -= len;
3652 cache->space_info->bytes_pinned -= len;
Josef Bacik25179202008-10-29 14:49:05 -04003653 spin_unlock(&cache->lock);
3654 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04003655
3656 start += len;
3657 }
3658
3659 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003660 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04003661 return 0;
3662}
3663
3664int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04003665 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05003666{
Yan Zheng11833d62009-09-11 16:11:19 -04003667 struct btrfs_fs_info *fs_info = root->fs_info;
3668 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04003669 u64 start;
3670 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05003671 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05003672
Yan Zheng11833d62009-09-11 16:11:19 -04003673 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3674 unpin = &fs_info->freed_extents[1];
3675 else
3676 unpin = &fs_info->freed_extents[0];
3677
Chris Masond3977122009-01-05 21:25:51 -05003678 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04003679 ret = find_first_extent_bit(unpin, 0, &start, &end,
3680 EXTENT_DIRTY);
3681 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05003682 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003683
3684 ret = btrfs_discard_extent(root, start, end + 1 - start);
3685
Chris Mason1a5bc162007-10-15 16:15:26 -04003686 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04003687 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04003688 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05003689 }
Josef Bacik817d52f2009-07-13 21:29:25 -04003690
Liu Hui1f3c79a2009-01-05 15:57:51 -05003691 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05003692}
3693
Zheng Yan31840ae2008-09-23 13:14:14 -04003694static int pin_down_bytes(struct btrfs_trans_handle *trans,
3695 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04003696 struct btrfs_path *path,
Yan Zheng11833d62009-09-11 16:11:19 -04003697 u64 bytenr, u64 num_bytes,
3698 int is_data, int reserved,
Chris Masonb9473432009-03-13 11:00:37 -04003699 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04003700{
Chris Mason1a5bc162007-10-15 16:15:26 -04003701 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003702 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04003703
Zheng Yan31840ae2008-09-23 13:14:14 -04003704 if (is_data)
3705 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003706
Chris Mason444528b2009-10-14 09:38:28 -04003707 /*
3708 * discard is sloooow, and so triggering discards on
3709 * individual btree blocks isn't a good plan. Just
3710 * pin everything in discard mode.
3711 */
3712 if (btrfs_test_opt(root, DISCARD))
3713 goto pinit;
3714
Zheng Yan31840ae2008-09-23 13:14:14 -04003715 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3716 if (!buf)
3717 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04003718
Zheng Yan31840ae2008-09-23 13:14:14 -04003719 /* we can reuse a block if it hasn't been written
3720 * and it is from this transaction. We can't
3721 * reuse anything from the tree log root because
3722 * it has tiny sub-transactions.
3723 */
3724 if (btrfs_buffer_uptodate(buf, 0) &&
3725 btrfs_try_tree_lock(buf)) {
3726 u64 header_owner = btrfs_header_owner(buf);
3727 u64 header_transid = btrfs_header_generation(buf);
3728 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3729 header_transid == trans->transid &&
3730 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04003731 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04003732 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04003733 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003734 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04003735 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003736 free_extent_buffer(buf);
3737pinit:
Yan Zheng11833d62009-09-11 16:11:19 -04003738 if (path)
3739 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04003740 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04003741 btrfs_pin_extent(root, bytenr, num_bytes, reserved);
Zheng Yan31840ae2008-09-23 13:14:14 -04003742
Chris Masonbe744172007-05-06 10:15:01 -04003743 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04003744 return 0;
3745}
3746
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003747static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3748 struct btrfs_root *root,
3749 u64 bytenr, u64 num_bytes, u64 parent,
3750 u64 root_objectid, u64 owner_objectid,
3751 u64 owner_offset, int refs_to_drop,
3752 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05003753{
Chris Masone2fa7222007-03-12 16:22:34 -04003754 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003755 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04003756 struct btrfs_fs_info *info = root->fs_info;
3757 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003758 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003759 struct btrfs_extent_item *ei;
3760 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05003761 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003762 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05003763 int extent_slot = 0;
3764 int found_extent = 0;
3765 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003766 u32 item_size;
3767 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05003768
Chris Mason5caf2a02007-04-02 11:20:42 -04003769 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003770 if (!path)
3771 return -ENOMEM;
3772
Chris Mason3c12ac72008-04-21 12:01:38 -04003773 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04003774 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003775
3776 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3777 BUG_ON(!is_data && refs_to_drop != 1);
3778
3779 ret = lookup_extent_backref(trans, extent_root, path, &iref,
3780 bytenr, num_bytes, parent,
3781 root_objectid, owner_objectid,
3782 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003783 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003784 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003785 while (extent_slot >= 0) {
3786 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003787 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003788 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003789 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003790 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3791 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003792 found_extent = 1;
3793 break;
3794 }
3795 if (path->slots[0] - extent_slot > 5)
3796 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003797 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003798 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003799#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3800 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3801 if (found_extent && item_size < sizeof(*ei))
3802 found_extent = 0;
3803#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003804 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003805 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04003806 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003807 NULL, refs_to_drop,
3808 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04003809 BUG_ON(ret);
3810 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04003811 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003812
3813 key.objectid = bytenr;
3814 key.type = BTRFS_EXTENT_ITEM_KEY;
3815 key.offset = num_bytes;
3816
Zheng Yan31840ae2008-09-23 13:14:14 -04003817 ret = btrfs_search_slot(trans, extent_root,
3818 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003819 if (ret) {
3820 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05003821 ", was looking for %llu\n", ret,
3822 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003823 btrfs_print_leaf(extent_root, path->nodes[0]);
3824 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003825 BUG_ON(ret);
3826 extent_slot = path->slots[0];
3827 }
Chris Mason7bb86312007-12-11 09:25:06 -05003828 } else {
3829 btrfs_print_leaf(extent_root, path->nodes[0]);
3830 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05003831 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003832 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003833 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04003834 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05003835 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003836 (unsigned long long)owner_objectid,
3837 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003838 }
Chris Mason5f39d392007-10-15 16:14:19 -04003839
3840 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003841 item_size = btrfs_item_size_nr(leaf, extent_slot);
3842#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3843 if (item_size < sizeof(*ei)) {
3844 BUG_ON(found_extent || extent_slot != path->slots[0]);
3845 ret = convert_extent_item_v0(trans, extent_root, path,
3846 owner_objectid, 0);
3847 BUG_ON(ret < 0);
3848
3849 btrfs_release_path(extent_root, path);
3850 path->leave_spinning = 1;
3851
3852 key.objectid = bytenr;
3853 key.type = BTRFS_EXTENT_ITEM_KEY;
3854 key.offset = num_bytes;
3855
3856 ret = btrfs_search_slot(trans, extent_root, &key, path,
3857 -1, 1);
3858 if (ret) {
3859 printk(KERN_ERR "umm, got %d back from search"
3860 ", was looking for %llu\n", ret,
3861 (unsigned long long)bytenr);
3862 btrfs_print_leaf(extent_root, path->nodes[0]);
3863 }
3864 BUG_ON(ret);
3865 extent_slot = path->slots[0];
3866 leaf = path->nodes[0];
3867 item_size = btrfs_item_size_nr(leaf, extent_slot);
3868 }
3869#endif
3870 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05003871 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003872 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003873 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3874 struct btrfs_tree_block_info *bi;
3875 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3876 bi = (struct btrfs_tree_block_info *)(ei + 1);
3877 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003878 }
3879
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003880 refs = btrfs_extent_refs(leaf, ei);
3881 BUG_ON(refs < refs_to_drop);
3882 refs -= refs_to_drop;
3883
3884 if (refs > 0) {
3885 if (extent_op)
3886 __run_delayed_extent_op(extent_op, leaf, ei);
3887 /*
3888 * In the case of inline back ref, reference count will
3889 * be updated by remove_extent_backref
3890 */
3891 if (iref) {
3892 BUG_ON(!found_extent);
3893 } else {
3894 btrfs_set_extent_refs(leaf, ei, refs);
3895 btrfs_mark_buffer_dirty(leaf);
3896 }
3897 if (found_extent) {
3898 ret = remove_extent_backref(trans, extent_root, path,
3899 iref, refs_to_drop,
3900 is_data);
3901 BUG_ON(ret);
3902 }
3903 } else {
3904 int mark_free = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003905 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04003906
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003907 if (found_extent) {
3908 BUG_ON(is_data && refs_to_drop !=
3909 extent_data_ref_count(root, path, iref));
3910 if (iref) {
3911 BUG_ON(path->slots[0] != extent_slot);
3912 } else {
3913 BUG_ON(path->slots[0] != extent_slot + 1);
3914 path->slots[0] = extent_slot;
3915 num_to_del = 2;
3916 }
Chris Mason78fae272007-03-25 11:35:08 -04003917 }
Chris Masonb9473432009-03-13 11:00:37 -04003918
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003919 ret = pin_down_bytes(trans, root, path, bytenr,
Yan Zheng11833d62009-09-11 16:11:19 -04003920 num_bytes, is_data, 0, &must_clean);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003921 if (ret > 0)
3922 mark_free = 1;
3923 BUG_ON(ret < 0);
Chris Masonb9473432009-03-13 11:00:37 -04003924 /*
3925 * it is going to be very rare for someone to be waiting
3926 * on the block we're freeing. del_items might need to
3927 * schedule, so rather than get fancy, just force it
3928 * to blocking here
3929 */
3930 if (must_clean)
3931 btrfs_set_lock_blocking(must_clean);
3932
Chris Mason952fcca2008-02-18 16:33:44 -05003933 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3934 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04003935 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04003936 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01003937
Chris Masonb9473432009-03-13 11:00:37 -04003938 if (must_clean) {
3939 clean_tree_block(NULL, root, must_clean);
3940 btrfs_tree_unlock(must_clean);
3941 free_extent_buffer(must_clean);
3942 }
3943
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003944 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05003945 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3946 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04003947 } else {
3948 invalidate_mapping_pages(info->btree_inode->i_mapping,
3949 bytenr >> PAGE_CACHE_SHIFT,
3950 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05003951 }
3952
Chris Masondcbdd4d2008-12-16 13:51:01 -05003953 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3954 mark_free);
3955 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05003956 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003957 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003958 return ret;
3959}
3960
3961/*
Chris Mason1887be62009-03-13 10:11:24 -04003962 * when we free an extent, it is possible (and likely) that we free the last
3963 * delayed ref for that extent as well. This searches the delayed ref tree for
3964 * a given extent, and if there are no other delayed refs to be processed, it
3965 * removes it from the tree.
3966 */
3967static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3968 struct btrfs_root *root, u64 bytenr)
3969{
3970 struct btrfs_delayed_ref_head *head;
3971 struct btrfs_delayed_ref_root *delayed_refs;
3972 struct btrfs_delayed_ref_node *ref;
3973 struct rb_node *node;
3974 int ret;
3975
3976 delayed_refs = &trans->transaction->delayed_refs;
3977 spin_lock(&delayed_refs->lock);
3978 head = btrfs_find_delayed_ref_head(trans, bytenr);
3979 if (!head)
3980 goto out;
3981
3982 node = rb_prev(&head->node.rb_node);
3983 if (!node)
3984 goto out;
3985
3986 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3987
3988 /* there are still entries for this ref, we can't drop it */
3989 if (ref->bytenr == bytenr)
3990 goto out;
3991
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003992 if (head->extent_op) {
3993 if (!head->must_insert_reserved)
3994 goto out;
3995 kfree(head->extent_op);
3996 head->extent_op = NULL;
3997 }
3998
Chris Mason1887be62009-03-13 10:11:24 -04003999 /*
4000 * waiting for the lock here would deadlock. If someone else has it
4001 * locked they are already in the process of dropping it anyway
4002 */
4003 if (!mutex_trylock(&head->mutex))
4004 goto out;
4005
4006 /*
4007 * at this point we have a head with no other entries. Go
4008 * ahead and process it.
4009 */
4010 head->node.in_tree = 0;
4011 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004012
Chris Mason1887be62009-03-13 10:11:24 -04004013 delayed_refs->num_entries--;
4014
4015 /*
4016 * we don't take a ref on the node because we're removing it from the
4017 * tree, so we just steal the ref the tree was holding.
4018 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004019 delayed_refs->num_heads--;
4020 if (list_empty(&head->cluster))
4021 delayed_refs->num_heads_ready--;
4022
4023 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004024 spin_unlock(&delayed_refs->lock);
4025
4026 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004027 &head->node, head->extent_op,
4028 head->must_insert_reserved);
Chris Mason1887be62009-03-13 10:11:24 -04004029 BUG_ON(ret);
4030 btrfs_put_delayed_ref(&head->node);
4031 return 0;
4032out:
4033 spin_unlock(&delayed_refs->lock);
4034 return 0;
4035}
4036
Chris Mason925baed2008-06-25 16:01:30 -04004037int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004038 struct btrfs_root *root,
4039 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004040 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004041{
4042 int ret;
4043
Chris Mason56bec292009-03-13 10:10:06 -04004044 /*
4045 * tree log blocks never actually go into the extent allocation
4046 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004047 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004048 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4049 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004050 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004051 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004052 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004053 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4054 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4055 parent, root_objectid, (int)owner,
4056 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004057 BUG_ON(ret);
4058 ret = check_ref_cleanup(trans, root, bytenr);
4059 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004060 } else {
4061 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4062 parent, root_objectid, owner,
4063 offset, BTRFS_DROP_DELAYED_REF, NULL);
4064 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004065 }
Chris Mason925baed2008-06-25 16:01:30 -04004066 return ret;
4067}
4068
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00004069int btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4070 struct btrfs_root *root,
4071 u64 bytenr, u32 blocksize,
4072 u64 parent, u64 root_objectid, int level)
4073{
4074 u64 used;
4075 spin_lock(&root->node_lock);
4076 used = btrfs_root_used(&root->root_item) - blocksize;
4077 btrfs_set_root_used(&root->root_item, used);
4078 spin_unlock(&root->node_lock);
4079
4080 return btrfs_free_extent(trans, root, bytenr, blocksize,
4081 parent, root_objectid, level, 0);
4082}
4083
Chris Mason87ee04e2007-11-30 11:30:34 -05004084static u64 stripe_align(struct btrfs_root *root, u64 val)
4085{
4086 u64 mask = ((u64)root->stripesize - 1);
4087 u64 ret = (val + mask) & ~mask;
4088 return ret;
4089}
4090
Chris Masonfec577f2007-02-26 10:40:21 -05004091/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004092 * when we wait for progress in the block group caching, its because
4093 * our allocation attempt failed at least once. So, we must sleep
4094 * and let some progress happen before we try again.
4095 *
4096 * This function will sleep at least once waiting for new free space to
4097 * show up, and then it will check the block group free space numbers
4098 * for our min num_bytes. Another option is to have it go ahead
4099 * and look in the rbtree for a free extent of a given size, but this
4100 * is a good start.
4101 */
4102static noinline int
4103wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4104 u64 num_bytes)
4105{
Yan Zheng11833d62009-09-11 16:11:19 -04004106 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004107 DEFINE_WAIT(wait);
4108
Yan Zheng11833d62009-09-11 16:11:19 -04004109 caching_ctl = get_caching_control(cache);
4110 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004111 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004112
Yan Zheng11833d62009-09-11 16:11:19 -04004113 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004114 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004115
4116 put_caching_control(caching_ctl);
4117 return 0;
4118}
4119
4120static noinline int
4121wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4122{
4123 struct btrfs_caching_control *caching_ctl;
4124 DEFINE_WAIT(wait);
4125
4126 caching_ctl = get_caching_control(cache);
4127 if (!caching_ctl)
4128 return 0;
4129
4130 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4131
4132 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004133 return 0;
4134}
4135
4136enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004137 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004138 LOOP_CACHING_NOWAIT = 1,
4139 LOOP_CACHING_WAIT = 2,
4140 LOOP_ALLOC_CHUNK = 3,
4141 LOOP_NO_EMPTY_SIZE = 4,
4142};
4143
4144/*
Chris Masonfec577f2007-02-26 10:40:21 -05004145 * walks the btree of allocated extents and find a hole of a given size.
4146 * The key ins is changed to record the hole:
4147 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004148 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004149 * ins->offset == number of blocks
4150 * Any available blocks before search_start are skipped.
4151 */
Chris Masond3977122009-01-05 21:25:51 -05004152static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004153 struct btrfs_root *orig_root,
4154 u64 num_bytes, u64 empty_size,
4155 u64 search_start, u64 search_end,
4156 u64 hint_byte, struct btrfs_key *ins,
4157 u64 exclude_start, u64 exclude_nr,
4158 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004159{
Josef Bacik80eb2342008-10-29 14:49:05 -04004160 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004161 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004162 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004163 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004164 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004165 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004166 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004167 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004168 int last_ptr_loop = 0;
4169 int loop = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004170 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004171 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004172 bool failed_alloc = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004173 u64 ideal_cache_percent = 0;
4174 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004175
Chris Masondb945352007-10-15 16:15:53 -04004176 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004177 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004178 ins->objectid = 0;
4179 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004180
Josef Bacik2552d172009-04-03 10:14:19 -04004181 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004182 if (!space_info) {
4183 printk(KERN_ERR "No space info for %d\n", data);
4184 return -ENOSPC;
4185 }
Josef Bacik2552d172009-04-03 10:14:19 -04004186
Chris Mason0ef3e662008-05-24 14:04:53 -04004187 if (orig_root->ref_cows || empty_size)
4188 allowed_chunk_alloc = 1;
4189
Chris Mason239b14b2008-03-24 15:02:07 -04004190 if (data & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004191 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004192 if (!btrfs_test_opt(root, SSD))
4193 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004194 }
4195
Chris Masonfa9c0d792009-04-03 09:47:43 -04004196 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4197 last_ptr = &root->fs_info->data_alloc_cluster;
4198 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004199
Chris Mason239b14b2008-03-24 15:02:07 -04004200 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004201 spin_lock(&last_ptr->lock);
4202 if (last_ptr->block_group)
4203 hint_byte = last_ptr->window_start;
4204 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004205 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004206
Chris Masona061fc82008-05-07 11:43:44 -04004207 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004208 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004209
Josef Bacik817d52f2009-07-13 21:29:25 -04004210 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004211 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004212
Josef Bacik2552d172009-04-03 10:14:19 -04004213 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004214ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004215 block_group = btrfs_lookup_block_group(root->fs_info,
4216 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004217 /*
4218 * we don't want to use the block group if it doesn't match our
4219 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004220 *
4221 * However if we are re-searching with an ideal block group
4222 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004223 */
4224 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004225 (block_group->cached != BTRFS_CACHE_NO ||
4226 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004227 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004228 if (list_empty(&block_group->list) ||
4229 block_group->ro) {
4230 /*
4231 * someone is removing this block group,
4232 * we can't jump into the have_block_group
4233 * target because our list pointers are not
4234 * valid
4235 */
4236 btrfs_put_block_group(block_group);
4237 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004238 } else {
Chris Mason44fb5512009-06-04 15:34:51 -04004239 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004240 }
Josef Bacik2552d172009-04-03 10:14:19 -04004241 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004242 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004243 }
Chris Mason42e70e72008-11-07 18:17:11 -05004244 }
Josef Bacik2552d172009-04-03 10:14:19 -04004245search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004246 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04004247 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004248 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004249 int cached;
Chris Mason8a1413a22008-11-10 16:13:54 -05004250
Josef Bacik11dfe352009-11-13 20:12:59 +00004251 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004252 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004253
Josef Bacik2552d172009-04-03 10:14:19 -04004254have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004255 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004256 u64 free_percent;
4257
4258 free_percent = btrfs_block_group_used(&block_group->item);
4259 free_percent *= 100;
4260 free_percent = div64_u64(free_percent,
4261 block_group->key.offset);
4262 free_percent = 100 - free_percent;
4263 if (free_percent > ideal_cache_percent &&
4264 likely(!block_group->ro)) {
4265 ideal_cache_offset = block_group->key.objectid;
4266 ideal_cache_percent = free_percent;
4267 }
4268
Josef Bacik817d52f2009-07-13 21:29:25 -04004269 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05004270 * We only want to start kthread caching if we are at
4271 * the point where we will wait for caching to make
4272 * progress, or if our ideal search is over and we've
4273 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04004274 */
4275 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05004276 (loop > LOOP_FIND_IDEAL &&
4277 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004278 ret = cache_block_group(block_group);
4279 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004280 }
Josef Bacikccf0e722009-11-10 21:23:48 -05004281 found_uncached_bg = true;
4282
4283 /*
4284 * If loop is set for cached only, try the next block
4285 * group.
4286 */
4287 if (loop == LOOP_FIND_IDEAL)
4288 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05004289 }
4290
Josef Bacik817d52f2009-07-13 21:29:25 -04004291 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05004292 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04004293 found_uncached_bg = true;
4294
Josef Bacikea6a4782008-11-20 12:16:16 -05004295 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004296 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004297
Josef Bacik0a243252009-09-11 16:11:20 -04004298 /*
4299 * Ok we want to try and use the cluster allocator, so lets look
4300 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4301 * have tried the cluster allocator plenty of times at this
4302 * point and not have found anything, so we are likely way too
4303 * fragmented for the clustering stuff to find anything, so lets
4304 * just skip it and let the allocator find whatever block it can
4305 * find
4306 */
4307 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004308 /*
4309 * the refill lock keeps out other
4310 * people trying to start a new cluster
4311 */
4312 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004313 if (last_ptr->block_group &&
4314 (last_ptr->block_group->ro ||
4315 !block_group_bits(last_ptr->block_group, data))) {
4316 offset = 0;
4317 goto refill_cluster;
4318 }
4319
Chris Masonfa9c0d792009-04-03 09:47:43 -04004320 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4321 num_bytes, search_start);
4322 if (offset) {
4323 /* we have a block, we're done */
4324 spin_unlock(&last_ptr->refill_lock);
4325 goto checks;
4326 }
4327
4328 spin_lock(&last_ptr->lock);
4329 /*
4330 * whoops, this cluster doesn't actually point to
4331 * this block group. Get a ref on the block
4332 * group is does point to and try again
4333 */
4334 if (!last_ptr_loop && last_ptr->block_group &&
4335 last_ptr->block_group != block_group) {
4336
4337 btrfs_put_block_group(block_group);
4338 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00004339 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004340 spin_unlock(&last_ptr->lock);
4341 spin_unlock(&last_ptr->refill_lock);
4342
4343 last_ptr_loop = 1;
4344 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04004345 /*
4346 * we know this block group is properly
4347 * in the list because
4348 * btrfs_remove_block_group, drops the
4349 * cluster before it removes the block
4350 * group from the list
4351 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04004352 goto have_block_group;
4353 }
4354 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04004355refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04004356 /*
4357 * this cluster didn't work out, free it and
4358 * start over
4359 */
4360 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4361
4362 last_ptr_loop = 0;
4363
4364 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04004365 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04004366 block_group, last_ptr,
4367 offset, num_bytes,
4368 empty_cluster + empty_size);
4369 if (ret == 0) {
4370 /*
4371 * now pull our allocation out of this
4372 * cluster
4373 */
4374 offset = btrfs_alloc_from_cluster(block_group,
4375 last_ptr, num_bytes,
4376 search_start);
4377 if (offset) {
4378 /* we found one, proceed */
4379 spin_unlock(&last_ptr->refill_lock);
4380 goto checks;
4381 }
Josef Bacik0a243252009-09-11 16:11:20 -04004382 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4383 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004384 spin_unlock(&last_ptr->refill_lock);
4385
Josef Bacik0a243252009-09-11 16:11:20 -04004386 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004387 wait_block_group_cache_progress(block_group,
4388 num_bytes + empty_cluster + empty_size);
4389 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004390 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004391
Chris Masonfa9c0d792009-04-03 09:47:43 -04004392 /*
4393 * at this point we either didn't find a cluster
4394 * or we weren't able to allocate a block from our
4395 * cluster. Free the cluster we've been trying
4396 * to use, and go to the next block group
4397 */
Josef Bacik0a243252009-09-11 16:11:20 -04004398 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004399 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04004400 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004401 }
4402
Josef Bacik6226cb02009-04-03 10:14:18 -04004403 offset = btrfs_find_space_for_alloc(block_group, search_start,
4404 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004405 /*
4406 * If we didn't find a chunk, and we haven't failed on this
4407 * block group before, and this block group is in the middle of
4408 * caching and we are ok with waiting, then go ahead and wait
4409 * for progress to be made, and set failed_alloc to true.
4410 *
4411 * If failed_alloc is true then we've already waited on this
4412 * block group once and should move on to the next block group.
4413 */
4414 if (!offset && !failed_alloc && !cached &&
4415 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004416 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004417 num_bytes + empty_size);
4418 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04004419 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004420 } else if (!offset) {
4421 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04004422 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004423checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04004424 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04004425 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04004426 if (search_start + num_bytes >= search_end) {
4427 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004428 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004429 }
Chris Masone37c9e62007-05-09 20:13:14 -04004430
Josef Bacik2552d172009-04-03 10:14:19 -04004431 /* move on to the next group */
4432 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04004433 block_group->key.objectid + block_group->key.offset) {
4434 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004435 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004436 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004437
Josef Bacik2552d172009-04-03 10:14:19 -04004438 if (exclude_nr > 0 &&
4439 (search_start + num_bytes > exclude_start &&
4440 search_start < exclude_start + exclude_nr)) {
4441 search_start = exclude_start + exclude_nr;
4442
Josef Bacik6226cb02009-04-03 10:14:18 -04004443 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004444 /*
4445 * if search_start is still in this block group
4446 * then we just re-search this block group
4447 */
4448 if (search_start >= block_group->key.objectid &&
4449 search_start < (block_group->key.objectid +
Josef Bacik6226cb02009-04-03 10:14:18 -04004450 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04004451 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04004452 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004453 }
Josef Bacik2552d172009-04-03 10:14:19 -04004454
4455 ins->objectid = search_start;
4456 ins->offset = num_bytes;
4457
Josef Bacik6226cb02009-04-03 10:14:18 -04004458 if (offset < search_start)
4459 btrfs_add_free_space(block_group, offset,
4460 search_start - offset);
4461 BUG_ON(offset > search_start);
4462
Yan Zheng11833d62009-09-11 16:11:19 -04004463 update_reserved_extents(block_group, num_bytes, 1);
4464
Josef Bacik2552d172009-04-03 10:14:19 -04004465 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04004466 break;
4467loop:
Josef Bacik0a243252009-09-11 16:11:20 -04004468 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004469 failed_alloc = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004470 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004471 }
4472 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004473
Josef Bacikccf0e722009-11-10 21:23:48 -05004474 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
4475 * for them to make caching progress. Also
4476 * determine the best possible bg to cache
4477 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
4478 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04004479 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4480 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4481 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4482 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04004483 */
Josef Bacik817d52f2009-07-13 21:29:25 -04004484 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4485 (found_uncached_bg || empty_size || empty_cluster ||
4486 allowed_chunk_alloc)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004487 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004488 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05004489 loop++;
4490 if (!ideal_cache_percent &&
4491 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04004492 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05004493
4494 /*
4495 * 1 of the following 2 things have happened so far
4496 *
4497 * 1) We found an ideal block group for caching that
4498 * is mostly full and will cache quickly, so we might
4499 * as well wait for it.
4500 *
4501 * 2) We searched for cached only and we didn't find
4502 * anything, and we didn't start any caching kthreads
4503 * either, so chances are we will loop through and
4504 * start a couple caching kthreads, and then come back
4505 * around and just wait for them. This will be slower
4506 * because we will have 2 caching kthreads reading at
4507 * the same time when we could have just started one
4508 * and waited for it to get far enough to give us an
4509 * allocation, so go ahead and go to the wait caching
4510 * loop.
4511 */
4512 loop = LOOP_CACHING_WAIT;
4513 search_start = ideal_cache_offset;
4514 ideal_cache_percent = 0;
4515 goto ideal_cache;
4516 } else if (loop == LOOP_FIND_IDEAL) {
4517 /*
4518 * Didn't find a uncached bg, wait on anything we find
4519 * next.
4520 */
4521 loop = LOOP_CACHING_WAIT;
4522 goto search;
4523 }
4524
4525 if (loop < LOOP_CACHING_WAIT) {
4526 loop++;
4527 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04004528 }
4529
4530 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004531 empty_size = 0;
4532 empty_cluster = 0;
4533 }
Chris Mason42e70e72008-11-07 18:17:11 -05004534
Josef Bacik2552d172009-04-03 10:14:19 -04004535 if (allowed_chunk_alloc) {
4536 ret = do_chunk_alloc(trans, root, num_bytes +
4537 2 * 1024 * 1024, data, 1);
Josef Bacik2552d172009-04-03 10:14:19 -04004538 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004539 done_chunk_alloc = 1;
4540 } else if (!done_chunk_alloc) {
Josef Bacik2552d172009-04-03 10:14:19 -04004541 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004542 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004543
Josef Bacik817d52f2009-07-13 21:29:25 -04004544 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004545 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04004546 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004547 }
Josef Bacik2552d172009-04-03 10:14:19 -04004548 ret = -ENOSPC;
4549 } else if (!ins->objectid) {
4550 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04004551 }
Chris Mason0b86a832008-03-24 15:01:56 -04004552
Josef Bacik80eb2342008-10-29 14:49:05 -04004553 /* we found what we needed */
4554 if (ins->objectid) {
4555 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05004556 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04004557
Chris Masonfa9c0d792009-04-03 09:47:43 -04004558 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004559 ret = 0;
4560 }
Chris Mason0b86a832008-03-24 15:01:56 -04004561
Chris Mason0f70abe2007-02-28 16:46:22 -05004562 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004563}
Chris Masonec44a352008-04-28 15:29:52 -04004564
Josef Bacik9ed74f22009-09-11 16:12:44 -04004565static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4566 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04004567{
4568 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004569
Josef Bacik9ed74f22009-09-11 16:12:44 -04004570 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05004571 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4572 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04004573 info->bytes_pinned - info->bytes_reserved -
4574 info->bytes_super),
Chris Masond3977122009-01-05 21:25:51 -05004575 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05004576 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
Josef Bacik9ed74f22009-09-11 16:12:44 -04004577 " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4578 "\n",
Joel Becker21380932009-04-21 12:38:29 -07004579 (unsigned long long)info->total_bytes,
4580 (unsigned long long)info->bytes_pinned,
4581 (unsigned long long)info->bytes_delalloc,
4582 (unsigned long long)info->bytes_may_use,
Josef Bacik9ed74f22009-09-11 16:12:44 -04004583 (unsigned long long)info->bytes_used,
4584 (unsigned long long)info->bytes_root,
4585 (unsigned long long)info->bytes_super,
4586 (unsigned long long)info->bytes_reserved);
4587 spin_unlock(&info->lock);
4588
4589 if (!dump_block_groups)
4590 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004591
Josef Bacik80eb2342008-10-29 14:49:05 -04004592 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05004593 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004594 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05004595 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4596 "%llu pinned %llu reserved\n",
4597 (unsigned long long)cache->key.objectid,
4598 (unsigned long long)cache->key.offset,
4599 (unsigned long long)btrfs_block_group_used(&cache->item),
4600 (unsigned long long)cache->pinned,
4601 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004602 btrfs_dump_free_space(cache, bytes);
4603 spin_unlock(&cache->lock);
4604 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004605 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004606}
Zheng Yane8569812008-09-26 10:05:48 -04004607
Yan Zheng11833d62009-09-11 16:11:19 -04004608int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4609 struct btrfs_root *root,
4610 u64 num_bytes, u64 min_alloc_size,
4611 u64 empty_size, u64 hint_byte,
4612 u64 search_end, struct btrfs_key *ins,
4613 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004614{
4615 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04004616 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004617
Josef Bacik6a632092009-02-20 11:00:09 -05004618 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04004619again:
Chris Mason0ef3e662008-05-24 14:04:53 -04004620 /*
4621 * the only place that sets empty_size is btrfs_realloc_node, which
4622 * is not called recursively on allocations
4623 */
Josef Bacik83d3c962009-12-07 21:45:59 +00004624 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04004625 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04004626 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04004627
Chris Masondb945352007-10-15 16:15:53 -04004628 WARN_ON(num_bytes < root->sectorsize);
4629 ret = find_free_extent(trans, root, num_bytes, empty_size,
4630 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04004631 trans->alloc_exclude_start,
4632 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04004633
Chris Mason98d20f62008-04-14 09:46:10 -04004634 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4635 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004636 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004637 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04004638 do_chunk_alloc(trans, root->fs_info->extent_root,
4639 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04004640 goto again;
4641 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004642 if (ret == -ENOSPC) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04004643 struct btrfs_space_info *sinfo;
4644
4645 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05004646 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4647 "wanted %llu\n", (unsigned long long)data,
4648 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004649 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04004650 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004651
4652 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004653}
4654
Chris Mason65b51a02008-08-01 15:11:20 -04004655int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4656{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004657 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004658 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004659
Josef Bacik0f9dd462008-09-23 13:14:11 -04004660 cache = btrfs_lookup_block_group(root->fs_info, start);
4661 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05004662 printk(KERN_ERR "Unable to find block group for %llu\n",
4663 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004664 return -ENOSPC;
4665 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004666
4667 ret = btrfs_discard_extent(root, start, len);
4668
Josef Bacik0f9dd462008-09-23 13:14:11 -04004669 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04004670 update_reserved_extents(cache, len, 0);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004671 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04004672
Chris Masone6dcd2d2008-07-17 12:53:50 -04004673 return ret;
4674}
4675
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004676static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4677 struct btrfs_root *root,
4678 u64 parent, u64 root_objectid,
4679 u64 flags, u64 owner, u64 offset,
4680 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004681{
4682 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004683 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004684 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004685 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004686 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004687 struct extent_buffer *leaf;
4688 int type;
4689 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004690
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004691 if (parent > 0)
4692 type = BTRFS_SHARED_DATA_REF_KEY;
4693 else
4694 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004695
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004696 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004697
4698 path = btrfs_alloc_path();
4699 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05004700
Chris Masonb9473432009-03-13 11:00:37 -04004701 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004702 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4703 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04004704 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004705
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004706 leaf = path->nodes[0];
4707 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004708 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004709 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4710 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4711 btrfs_set_extent_flags(leaf, extent_item,
4712 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004713
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004714 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4715 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4716 if (parent > 0) {
4717 struct btrfs_shared_data_ref *ref;
4718 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4719 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4720 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4721 } else {
4722 struct btrfs_extent_data_ref *ref;
4723 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4724 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4725 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4726 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4727 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4728 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004729
4730 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004731 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004732
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004733 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4734 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05004735 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05004736 printk(KERN_ERR "btrfs update block group failed for %llu "
4737 "%llu\n", (unsigned long long)ins->objectid,
4738 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004739 BUG();
4740 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004741 return ret;
4742}
4743
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004744static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4745 struct btrfs_root *root,
4746 u64 parent, u64 root_objectid,
4747 u64 flags, struct btrfs_disk_key *key,
4748 int level, struct btrfs_key *ins)
4749{
4750 int ret;
4751 struct btrfs_fs_info *fs_info = root->fs_info;
4752 struct btrfs_extent_item *extent_item;
4753 struct btrfs_tree_block_info *block_info;
4754 struct btrfs_extent_inline_ref *iref;
4755 struct btrfs_path *path;
4756 struct extent_buffer *leaf;
4757 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4758
4759 path = btrfs_alloc_path();
4760 BUG_ON(!path);
4761
4762 path->leave_spinning = 1;
4763 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4764 ins, size);
4765 BUG_ON(ret);
4766
4767 leaf = path->nodes[0];
4768 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4769 struct btrfs_extent_item);
4770 btrfs_set_extent_refs(leaf, extent_item, 1);
4771 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4772 btrfs_set_extent_flags(leaf, extent_item,
4773 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4774 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4775
4776 btrfs_set_tree_block_key(leaf, block_info, key);
4777 btrfs_set_tree_block_level(leaf, block_info, level);
4778
4779 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4780 if (parent > 0) {
4781 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4782 btrfs_set_extent_inline_ref_type(leaf, iref,
4783 BTRFS_SHARED_BLOCK_REF_KEY);
4784 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4785 } else {
4786 btrfs_set_extent_inline_ref_type(leaf, iref,
4787 BTRFS_TREE_BLOCK_REF_KEY);
4788 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4789 }
4790
4791 btrfs_mark_buffer_dirty(leaf);
4792 btrfs_free_path(path);
4793
4794 ret = update_block_group(trans, root, ins->objectid, ins->offset,
4795 1, 0);
4796 if (ret) {
4797 printk(KERN_ERR "btrfs update block group failed for %llu "
4798 "%llu\n", (unsigned long long)ins->objectid,
4799 (unsigned long long)ins->offset);
4800 BUG();
4801 }
4802 return ret;
4803}
4804
4805int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4806 struct btrfs_root *root,
4807 u64 root_objectid, u64 owner,
4808 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004809{
4810 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04004811
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004812 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004813
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004814 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4815 0, root_objectid, owner, offset,
4816 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004817 return ret;
4818}
Chris Masone02119d2008-09-05 16:13:11 -04004819
4820/*
4821 * this is used by the tree logging recovery code. It records that
4822 * an extent has been allocated and makes sure to clear the free
4823 * space cache bits as well
4824 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004825int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4826 struct btrfs_root *root,
4827 u64 root_objectid, u64 owner, u64 offset,
4828 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004829{
4830 int ret;
4831 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04004832 struct btrfs_caching_control *caching_ctl;
4833 u64 start = ins->objectid;
4834 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04004835
Chris Masone02119d2008-09-05 16:13:11 -04004836 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik817d52f2009-07-13 21:29:25 -04004837 cache_block_group(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004838 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004839
Yan Zheng11833d62009-09-11 16:11:19 -04004840 if (!caching_ctl) {
4841 BUG_ON(!block_group_cache_done(block_group));
4842 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4843 BUG_ON(ret);
4844 } else {
4845 mutex_lock(&caching_ctl->mutex);
4846
4847 if (start >= caching_ctl->progress) {
4848 ret = add_excluded_extent(root, start, num_bytes);
4849 BUG_ON(ret);
4850 } else if (start + num_bytes <= caching_ctl->progress) {
4851 ret = btrfs_remove_free_space(block_group,
4852 start, num_bytes);
4853 BUG_ON(ret);
4854 } else {
4855 num_bytes = caching_ctl->progress - start;
4856 ret = btrfs_remove_free_space(block_group,
4857 start, num_bytes);
4858 BUG_ON(ret);
4859
4860 start = caching_ctl->progress;
4861 num_bytes = ins->objectid + ins->offset -
4862 caching_ctl->progress;
4863 ret = add_excluded_extent(root, start, num_bytes);
4864 BUG_ON(ret);
4865 }
4866
4867 mutex_unlock(&caching_ctl->mutex);
4868 put_caching_control(caching_ctl);
4869 }
4870
4871 update_reserved_extents(block_group, ins->offset, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04004872 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004873 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4874 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004875 return ret;
4876}
4877
Chris Masone6dcd2d2008-07-17 12:53:50 -04004878/*
4879 * finds a free extent and does all the dirty work required for allocation
4880 * returns the key for the extent through ins, and a tree buffer for
4881 * the first block of the extent through buf.
4882 *
4883 * returns 0 if everything worked, non-zero otherwise.
4884 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004885static int alloc_tree_block(struct btrfs_trans_handle *trans,
4886 struct btrfs_root *root,
4887 u64 num_bytes, u64 parent, u64 root_objectid,
4888 struct btrfs_disk_key *key, int level,
4889 u64 empty_size, u64 hint_byte, u64 search_end,
4890 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004891{
4892 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004893 u64 flags = 0;
4894
Yan Zheng11833d62009-09-11 16:11:19 -04004895 ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4896 empty_size, hint_byte, search_end,
4897 ins, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004898 if (ret)
4899 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004900
4901 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4902 if (parent == 0)
4903 parent = ins->objectid;
4904 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4905 } else
4906 BUG_ON(parent > 0);
4907
Chris Masond00aff02008-09-11 15:54:42 -04004908 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004909 struct btrfs_delayed_extent_op *extent_op;
4910 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4911 BUG_ON(!extent_op);
4912 if (key)
4913 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4914 else
4915 memset(&extent_op->key, 0, sizeof(extent_op->key));
4916 extent_op->flags_to_set = flags;
4917 extent_op->update_key = 1;
4918 extent_op->update_flags = 1;
4919 extent_op->is_data = 0;
4920
4921 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4922 ins->offset, parent, root_objectid,
4923 level, BTRFS_ADD_DELAYED_EXTENT,
4924 extent_op);
Chris Masond00aff02008-09-11 15:54:42 -04004925 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04004926 }
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00004927
4928 if (root_objectid == root->root_key.objectid) {
4929 u64 used;
4930 spin_lock(&root->node_lock);
4931 used = btrfs_root_used(&root->root_item) + num_bytes;
4932 btrfs_set_root_used(&root->root_item, used);
4933 spin_unlock(&root->node_lock);
4934 }
Chris Mason925baed2008-06-25 16:01:30 -04004935 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004936}
Chris Mason65b51a02008-08-01 15:11:20 -04004937
4938struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4939 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05004940 u64 bytenr, u32 blocksize,
4941 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04004942{
4943 struct extent_buffer *buf;
4944
4945 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4946 if (!buf)
4947 return ERR_PTR(-ENOMEM);
4948 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05004949 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004950 btrfs_tree_lock(buf);
4951 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004952
4953 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04004954 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004955
Chris Masond0c803c2008-09-11 16:17:57 -04004956 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004957 /*
4958 * we allow two log transactions at a time, use different
4959 * EXENT bit to differentiate dirty pages.
4960 */
4961 if (root->log_transid % 2 == 0)
4962 set_extent_dirty(&root->dirty_log_pages, buf->start,
4963 buf->start + buf->len - 1, GFP_NOFS);
4964 else
4965 set_extent_new(&root->dirty_log_pages, buf->start,
4966 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04004967 } else {
4968 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4969 buf->start + buf->len - 1, GFP_NOFS);
4970 }
Chris Mason65b51a02008-08-01 15:11:20 -04004971 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004972 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004973 return buf;
4974}
4975
Chris Masonfec577f2007-02-26 10:40:21 -05004976/*
4977 * helper function to allocate a block for a given tree
4978 * returns the tree buffer or NULL.
4979 */
Chris Mason5f39d392007-10-15 16:14:19 -04004980struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004981 struct btrfs_root *root, u32 blocksize,
4982 u64 parent, u64 root_objectid,
4983 struct btrfs_disk_key *key, int level,
4984 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004985{
Chris Masone2fa7222007-03-12 16:22:34 -04004986 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05004987 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004988 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05004989
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004990 ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4991 key, level, empty_size, hint, (u64)-1, &ins);
Chris Masonfec577f2007-02-26 10:40:21 -05004992 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04004993 BUG_ON(ret > 0);
4994 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004995 }
Chris Mason55c69072008-01-09 15:55:33 -05004996
Chris Mason4008c042009-02-12 14:09:45 -05004997 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4998 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05004999 return buf;
5000}
Chris Masona28ec192007-03-06 20:08:01 -05005001
Yan Zheng2c47e6052009-06-27 21:07:35 -04005002struct walk_control {
5003 u64 refs[BTRFS_MAX_LEVEL];
5004 u64 flags[BTRFS_MAX_LEVEL];
5005 struct btrfs_key update_progress;
5006 int stage;
5007 int level;
5008 int shared_level;
5009 int update_ref;
5010 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005011 int reada_slot;
5012 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005013};
5014
5015#define DROP_REFERENCE 1
5016#define UPDATE_BACKREF 2
5017
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005018static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5019 struct btrfs_root *root,
5020 struct walk_control *wc,
5021 struct btrfs_path *path)
5022{
5023 u64 bytenr;
5024 u64 generation;
5025 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005026 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005027 u64 last = 0;
5028 u32 nritems;
5029 u32 blocksize;
5030 struct btrfs_key key;
5031 struct extent_buffer *eb;
5032 int ret;
5033 int slot;
5034 int nread = 0;
5035
5036 if (path->slots[wc->level] < wc->reada_slot) {
5037 wc->reada_count = wc->reada_count * 2 / 3;
5038 wc->reada_count = max(wc->reada_count, 2);
5039 } else {
5040 wc->reada_count = wc->reada_count * 3 / 2;
5041 wc->reada_count = min_t(int, wc->reada_count,
5042 BTRFS_NODEPTRS_PER_BLOCK(root));
5043 }
5044
5045 eb = path->nodes[wc->level];
5046 nritems = btrfs_header_nritems(eb);
5047 blocksize = btrfs_level_size(root, wc->level - 1);
5048
5049 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5050 if (nread >= wc->reada_count)
5051 break;
5052
5053 cond_resched();
5054 bytenr = btrfs_node_blockptr(eb, slot);
5055 generation = btrfs_node_ptr_generation(eb, slot);
5056
5057 if (slot == path->slots[wc->level])
5058 goto reada;
5059
5060 if (wc->stage == UPDATE_BACKREF &&
5061 generation <= root->root_key.offset)
5062 continue;
5063
Yan, Zheng94fcca92009-10-09 09:25:16 -04005064 /* We don't lock the tree block, it's OK to be racy here */
5065 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5066 &refs, &flags);
5067 BUG_ON(ret);
5068 BUG_ON(refs == 0);
5069
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005070 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005071 if (refs == 1)
5072 goto reada;
5073
Yan, Zheng94fcca92009-10-09 09:25:16 -04005074 if (wc->level == 1 &&
5075 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5076 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005077 if (!wc->update_ref ||
5078 generation <= root->root_key.offset)
5079 continue;
5080 btrfs_node_key_to_cpu(eb, &key, slot);
5081 ret = btrfs_comp_cpu_keys(&key,
5082 &wc->update_progress);
5083 if (ret < 0)
5084 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005085 } else {
5086 if (wc->level == 1 &&
5087 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5088 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005089 }
5090reada:
5091 ret = readahead_tree_block(root, bytenr, blocksize,
5092 generation);
5093 if (ret)
5094 break;
5095 last = bytenr + blocksize;
5096 nread++;
5097 }
5098 wc->reada_slot = slot;
5099}
5100
Chris Mason9aca1d52007-03-13 11:09:37 -04005101/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005102 * hepler to process tree block while walking down the tree.
5103 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005104 * when wc->stage == UPDATE_BACKREF, this function updates
5105 * back refs for pointers in the block.
5106 *
5107 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005108 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005109static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5110 struct btrfs_root *root,
5111 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005112 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005113{
5114 int level = wc->level;
5115 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005116 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5117 int ret;
5118
5119 if (wc->stage == UPDATE_BACKREF &&
5120 btrfs_header_owner(eb) != root->root_key.objectid)
5121 return 1;
5122
5123 /*
5124 * when reference count of tree block is 1, it won't increase
5125 * again. once full backref flag is set, we never clear it.
5126 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005127 if (lookup_info &&
5128 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5129 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005130 BUG_ON(!path->locks[level]);
5131 ret = btrfs_lookup_extent_info(trans, root,
5132 eb->start, eb->len,
5133 &wc->refs[level],
5134 &wc->flags[level]);
5135 BUG_ON(ret);
5136 BUG_ON(wc->refs[level] == 0);
5137 }
5138
Yan Zheng2c47e6052009-06-27 21:07:35 -04005139 if (wc->stage == DROP_REFERENCE) {
5140 if (wc->refs[level] > 1)
5141 return 1;
5142
5143 if (path->locks[level] && !wc->keep_locks) {
5144 btrfs_tree_unlock(eb);
5145 path->locks[level] = 0;
5146 }
5147 return 0;
5148 }
5149
5150 /* wc->stage == UPDATE_BACKREF */
5151 if (!(wc->flags[level] & flag)) {
5152 BUG_ON(!path->locks[level]);
5153 ret = btrfs_inc_ref(trans, root, eb, 1);
5154 BUG_ON(ret);
5155 ret = btrfs_dec_ref(trans, root, eb, 0);
5156 BUG_ON(ret);
5157 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5158 eb->len, flag, 0);
5159 BUG_ON(ret);
5160 wc->flags[level] |= flag;
5161 }
5162
5163 /*
5164 * the block is shared by multiple trees, so it's not good to
5165 * keep the tree lock
5166 */
5167 if (path->locks[level] && level > 0) {
5168 btrfs_tree_unlock(eb);
5169 path->locks[level] = 0;
5170 }
5171 return 0;
5172}
5173
5174/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005175 * hepler to process tree block pointer.
5176 *
5177 * when wc->stage == DROP_REFERENCE, this function checks
5178 * reference count of the block pointed to. if the block
5179 * is shared and we need update back refs for the subtree
5180 * rooted at the block, this function changes wc->stage to
5181 * UPDATE_BACKREF. if the block is shared and there is no
5182 * need to update back, this function drops the reference
5183 * to the block.
5184 *
5185 * NOTE: return value 1 means we should stop walking down.
5186 */
5187static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5188 struct btrfs_root *root,
5189 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005190 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005191{
5192 u64 bytenr;
5193 u64 generation;
5194 u64 parent;
5195 u32 blocksize;
5196 struct btrfs_key key;
5197 struct extent_buffer *next;
5198 int level = wc->level;
5199 int reada = 0;
5200 int ret = 0;
5201
5202 generation = btrfs_node_ptr_generation(path->nodes[level],
5203 path->slots[level]);
5204 /*
5205 * if the lower level block was created before the snapshot
5206 * was created, we know there is no need to update back refs
5207 * for the subtree
5208 */
5209 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005210 generation <= root->root_key.offset) {
5211 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005212 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005213 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005214
5215 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5216 blocksize = btrfs_level_size(root, level - 1);
5217
5218 next = btrfs_find_tree_block(root, bytenr, blocksize);
5219 if (!next) {
5220 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00005221 if (!next)
5222 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005223 reada = 1;
5224 }
5225 btrfs_tree_lock(next);
5226 btrfs_set_lock_blocking(next);
5227
Yan, Zheng94fcca92009-10-09 09:25:16 -04005228 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5229 &wc->refs[level - 1],
5230 &wc->flags[level - 1]);
5231 BUG_ON(ret);
5232 BUG_ON(wc->refs[level - 1] == 0);
5233 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005234
Yan, Zheng94fcca92009-10-09 09:25:16 -04005235 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005236 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005237 if (level == 1 &&
5238 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5239 goto skip;
5240
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005241 if (!wc->update_ref ||
5242 generation <= root->root_key.offset)
5243 goto skip;
5244
5245 btrfs_node_key_to_cpu(path->nodes[level], &key,
5246 path->slots[level]);
5247 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5248 if (ret < 0)
5249 goto skip;
5250
5251 wc->stage = UPDATE_BACKREF;
5252 wc->shared_level = level - 1;
5253 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005254 } else {
5255 if (level == 1 &&
5256 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5257 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005258 }
5259
5260 if (!btrfs_buffer_uptodate(next, generation)) {
5261 btrfs_tree_unlock(next);
5262 free_extent_buffer(next);
5263 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005264 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005265 }
5266
5267 if (!next) {
5268 if (reada && level == 1)
5269 reada_walk_down(trans, root, wc, path);
5270 next = read_tree_block(root, bytenr, blocksize, generation);
5271 btrfs_tree_lock(next);
5272 btrfs_set_lock_blocking(next);
5273 }
5274
5275 level--;
5276 BUG_ON(level != btrfs_header_level(next));
5277 path->nodes[level] = next;
5278 path->slots[level] = 0;
5279 path->locks[level] = 1;
5280 wc->level = level;
5281 if (wc->level == 1)
5282 wc->reada_slot = 0;
5283 return 0;
5284skip:
5285 wc->refs[level - 1] = 0;
5286 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005287 if (wc->stage == DROP_REFERENCE) {
5288 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5289 parent = path->nodes[level]->start;
5290 } else {
5291 BUG_ON(root->root_key.objectid !=
5292 btrfs_header_owner(path->nodes[level]));
5293 parent = 0;
5294 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005295
Yan, Zheng94fcca92009-10-09 09:25:16 -04005296 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5297 root->root_key.objectid, level - 1, 0);
5298 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005299 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005300 btrfs_tree_unlock(next);
5301 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04005302 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005303 return 1;
5304}
5305
5306/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005307 * hepler to process tree block while walking up the tree.
5308 *
5309 * when wc->stage == DROP_REFERENCE, this function drops
5310 * reference count on the block.
5311 *
5312 * when wc->stage == UPDATE_BACKREF, this function changes
5313 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5314 * to UPDATE_BACKREF previously while processing the block.
5315 *
5316 * NOTE: return value 1 means we should stop walking up.
5317 */
5318static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5319 struct btrfs_root *root,
5320 struct btrfs_path *path,
5321 struct walk_control *wc)
5322{
5323 int ret = 0;
5324 int level = wc->level;
5325 struct extent_buffer *eb = path->nodes[level];
5326 u64 parent = 0;
5327
5328 if (wc->stage == UPDATE_BACKREF) {
5329 BUG_ON(wc->shared_level < level);
5330 if (level < wc->shared_level)
5331 goto out;
5332
Yan Zheng2c47e6052009-06-27 21:07:35 -04005333 ret = find_next_key(path, level + 1, &wc->update_progress);
5334 if (ret > 0)
5335 wc->update_ref = 0;
5336
5337 wc->stage = DROP_REFERENCE;
5338 wc->shared_level = -1;
5339 path->slots[level] = 0;
5340
5341 /*
5342 * check reference count again if the block isn't locked.
5343 * we should start walking down the tree again if reference
5344 * count is one.
5345 */
5346 if (!path->locks[level]) {
5347 BUG_ON(level == 0);
5348 btrfs_tree_lock(eb);
5349 btrfs_set_lock_blocking(eb);
5350 path->locks[level] = 1;
5351
5352 ret = btrfs_lookup_extent_info(trans, root,
5353 eb->start, eb->len,
5354 &wc->refs[level],
5355 &wc->flags[level]);
5356 BUG_ON(ret);
5357 BUG_ON(wc->refs[level] == 0);
5358 if (wc->refs[level] == 1) {
5359 btrfs_tree_unlock(eb);
5360 path->locks[level] = 0;
5361 return 1;
5362 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005363 }
5364 }
5365
5366 /* wc->stage == DROP_REFERENCE */
5367 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5368
5369 if (wc->refs[level] == 1) {
5370 if (level == 0) {
5371 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5372 ret = btrfs_dec_ref(trans, root, eb, 1);
5373 else
5374 ret = btrfs_dec_ref(trans, root, eb, 0);
5375 BUG_ON(ret);
5376 }
5377 /* make block locked assertion in clean_tree_block happy */
5378 if (!path->locks[level] &&
5379 btrfs_header_generation(eb) == trans->transid) {
5380 btrfs_tree_lock(eb);
5381 btrfs_set_lock_blocking(eb);
5382 path->locks[level] = 1;
5383 }
5384 clean_tree_block(trans, root, eb);
5385 }
5386
5387 if (eb == root->node) {
5388 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5389 parent = eb->start;
5390 else
5391 BUG_ON(root->root_key.objectid !=
5392 btrfs_header_owner(eb));
5393 } else {
5394 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5395 parent = path->nodes[level + 1]->start;
5396 else
5397 BUG_ON(root->root_key.objectid !=
5398 btrfs_header_owner(path->nodes[level + 1]));
5399 }
5400
5401 ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5402 root->root_key.objectid, level, 0);
5403 BUG_ON(ret);
5404out:
5405 wc->refs[level] = 0;
5406 wc->flags[level] = 0;
5407 return ret;
5408}
5409
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005410static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5411 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005412 struct btrfs_path *path,
5413 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005414{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005415 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005416 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005417 int ret;
5418
Yan Zheng2c47e6052009-06-27 21:07:35 -04005419 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005420 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005421 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005422 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005423
Yan Zheng2c47e6052009-06-27 21:07:35 -04005424 if (level == 0)
5425 break;
5426
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005427 if (path->slots[level] >=
5428 btrfs_header_nritems(path->nodes[level]))
5429 break;
5430
Yan, Zheng94fcca92009-10-09 09:25:16 -04005431 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005432 if (ret > 0) {
5433 path->slots[level]++;
5434 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00005435 } else if (ret < 0)
5436 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005437 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005438 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005439 return 0;
5440}
5441
Chris Masond3977122009-01-05 21:25:51 -05005442static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005443 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005444 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005445 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005446{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005447 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005448 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005449
Yan Zheng2c47e6052009-06-27 21:07:35 -04005450 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5451 while (level < max_level && path->nodes[level]) {
5452 wc->level = level;
5453 if (path->slots[level] + 1 <
5454 btrfs_header_nritems(path->nodes[level])) {
5455 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005456 return 0;
5457 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005458 ret = walk_up_proc(trans, root, path, wc);
5459 if (ret > 0)
5460 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05005461
Yan Zheng2c47e6052009-06-27 21:07:35 -04005462 if (path->locks[level]) {
5463 btrfs_tree_unlock(path->nodes[level]);
5464 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005465 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005466 free_extent_buffer(path->nodes[level]);
5467 path->nodes[level] = NULL;
5468 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005469 }
5470 }
5471 return 1;
5472}
5473
Chris Mason9aca1d52007-03-13 11:09:37 -04005474/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005475 * drop a subvolume tree.
5476 *
5477 * this function traverses the tree freeing any blocks that only
5478 * referenced by the tree.
5479 *
5480 * when a shared tree block is found. this function decreases its
5481 * reference count by one. if update_ref is true, this function
5482 * also make sure backrefs for the shared block and all lower level
5483 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04005484 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005485int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05005486{
Chris Mason5caf2a02007-04-02 11:20:42 -04005487 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005488 struct btrfs_trans_handle *trans;
5489 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005490 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005491 struct walk_control *wc;
5492 struct btrfs_key key;
5493 int err = 0;
5494 int ret;
5495 int level;
Chris Mason20524f02007-03-10 06:35:47 -05005496
Chris Mason5caf2a02007-04-02 11:20:42 -04005497 path = btrfs_alloc_path();
5498 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05005499
Yan Zheng2c47e6052009-06-27 21:07:35 -04005500 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5501 BUG_ON(!wc);
5502
5503 trans = btrfs_start_transaction(tree_root, 1);
5504
Chris Mason9f3a7422007-08-07 15:52:19 -04005505 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005506 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005507 path->nodes[level] = btrfs_lock_root_node(root);
5508 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005509 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005510 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005511 memset(&wc->update_progress, 0,
5512 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005513 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005514 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005515 memcpy(&wc->update_progress, &key,
5516 sizeof(wc->update_progress));
5517
Chris Mason6702ed42007-08-07 16:15:09 -04005518 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005519 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005520 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005521 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5522 path->lowest_level = 0;
5523 if (ret < 0) {
5524 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005525 goto out;
5526 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005527 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005528
Chris Mason7d9eb122008-07-08 14:19:17 -04005529 /*
5530 * unlock our path, this is safe because only this
5531 * function is allowed to delete this snapshot
5532 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005533 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005534
Yan Zheng2c47e6052009-06-27 21:07:35 -04005535 level = btrfs_header_level(root->node);
5536 while (1) {
5537 btrfs_tree_lock(path->nodes[level]);
5538 btrfs_set_lock_blocking(path->nodes[level]);
5539
5540 ret = btrfs_lookup_extent_info(trans, root,
5541 path->nodes[level]->start,
5542 path->nodes[level]->len,
5543 &wc->refs[level],
5544 &wc->flags[level]);
5545 BUG_ON(ret);
5546 BUG_ON(wc->refs[level] == 0);
5547
5548 if (level == root_item->drop_level)
5549 break;
5550
5551 btrfs_tree_unlock(path->nodes[level]);
5552 WARN_ON(wc->refs[level] != 1);
5553 level--;
5554 }
5555 }
5556
5557 wc->level = level;
5558 wc->shared_level = -1;
5559 wc->stage = DROP_REFERENCE;
5560 wc->update_ref = update_ref;
5561 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005562 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005563
5564 while (1) {
5565 ret = walk_down_tree(trans, root, path, wc);
5566 if (ret < 0) {
5567 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005568 break;
5569 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005570
5571 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5572 if (ret < 0) {
5573 err = ret;
5574 break;
5575 }
5576
5577 if (ret > 0) {
5578 BUG_ON(wc->stage != DROP_REFERENCE);
5579 break;
5580 }
5581
5582 if (wc->stage == DROP_REFERENCE) {
5583 level = wc->level;
5584 btrfs_node_key(path->nodes[level],
5585 &root_item->drop_progress,
5586 path->slots[level]);
5587 root_item->drop_level = level;
5588 }
5589
5590 BUG_ON(wc->level == 0);
5591 if (trans->transaction->in_commit ||
5592 trans->transaction->delayed_refs.flushing) {
5593 ret = btrfs_update_root(trans, tree_root,
5594 &root->root_key,
5595 root_item);
5596 BUG_ON(ret);
5597
5598 btrfs_end_transaction(trans, tree_root);
5599 trans = btrfs_start_transaction(tree_root, 1);
5600 } else {
5601 unsigned long update;
Chris Masonc3e69d52009-03-13 10:17:05 -04005602 update = trans->delayed_ref_updates;
5603 trans->delayed_ref_updates = 0;
5604 if (update)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005605 btrfs_run_delayed_refs(trans, tree_root,
5606 update);
Chris Masonc3e69d52009-03-13 10:17:05 -04005607 }
Chris Mason20524f02007-03-10 06:35:47 -05005608 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005609 btrfs_release_path(root, path);
5610 BUG_ON(err);
5611
5612 ret = btrfs_del_root(trans, tree_root, &root->root_key);
5613 BUG_ON(ret);
5614
Yan, Zheng76dda932009-09-21 16:00:26 -04005615 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5616 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5617 NULL, NULL);
5618 BUG_ON(ret < 0);
5619 if (ret > 0) {
5620 ret = btrfs_del_orphan_item(trans, tree_root,
5621 root->root_key.objectid);
5622 BUG_ON(ret);
5623 }
5624 }
5625
5626 if (root->in_radix) {
5627 btrfs_free_fs_root(tree_root->fs_info, root);
5628 } else {
5629 free_extent_buffer(root->node);
5630 free_extent_buffer(root->commit_root);
5631 kfree(root);
5632 }
Chris Mason9f3a7422007-08-07 15:52:19 -04005633out:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005634 btrfs_end_transaction(trans, tree_root);
5635 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005636 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005637 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005638}
Chris Mason9078a3e2007-04-26 16:46:15 -04005639
Yan Zheng2c47e6052009-06-27 21:07:35 -04005640/*
5641 * drop subtree rooted at tree block 'node'.
5642 *
5643 * NOTE: this function will unlock and release tree block 'node'
5644 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005645int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5646 struct btrfs_root *root,
5647 struct extent_buffer *node,
5648 struct extent_buffer *parent)
5649{
5650 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005651 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005652 int level;
5653 int parent_level;
5654 int ret = 0;
5655 int wret;
5656
Yan Zheng2c47e6052009-06-27 21:07:35 -04005657 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5658
Yan Zhengf82d02d2008-10-29 14:49:05 -04005659 path = btrfs_alloc_path();
5660 BUG_ON(!path);
5661
Yan Zheng2c47e6052009-06-27 21:07:35 -04005662 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5663 BUG_ON(!wc);
5664
Chris Masonb9447ef82009-03-09 11:45:38 -04005665 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005666 parent_level = btrfs_header_level(parent);
5667 extent_buffer_get(parent);
5668 path->nodes[parent_level] = parent;
5669 path->slots[parent_level] = btrfs_header_nritems(parent);
5670
Chris Masonb9447ef82009-03-09 11:45:38 -04005671 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005672 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005673 path->nodes[level] = node;
5674 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005675 path->locks[level] = 1;
5676
5677 wc->refs[parent_level] = 1;
5678 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5679 wc->level = level;
5680 wc->shared_level = -1;
5681 wc->stage = DROP_REFERENCE;
5682 wc->update_ref = 0;
5683 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005684 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005685
5686 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005687 wret = walk_down_tree(trans, root, path, wc);
5688 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005689 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005690 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005691 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005692
Yan Zheng2c47e6052009-06-27 21:07:35 -04005693 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005694 if (wret < 0)
5695 ret = wret;
5696 if (wret != 0)
5697 break;
5698 }
5699
Yan Zheng2c47e6052009-06-27 21:07:35 -04005700 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005701 btrfs_free_path(path);
5702 return ret;
5703}
5704
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005705#if 0
Chris Mason8e7bf942008-04-28 09:02:36 -04005706static unsigned long calc_ra(unsigned long start, unsigned long last,
5707 unsigned long nr)
5708{
5709 return min(last, start + nr - 1);
5710}
5711
Chris Masond3977122009-01-05 21:25:51 -05005712static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05005713 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05005714{
5715 u64 page_start;
5716 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04005717 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005718 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05005719 unsigned long i;
5720 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05005721 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05005722 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04005723 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04005724 unsigned int total_read = 0;
5725 unsigned int total_dirty = 0;
5726 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05005727
5728 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005729
5730 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005731 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05005732 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5733
Zheng Yan1a40e232008-09-26 10:09:34 -04005734 /* make sure the dirty trick played by the caller work */
5735 ret = invalidate_inode_pages2_range(inode->i_mapping,
5736 first_index, last_index);
5737 if (ret)
5738 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04005739
Chris Mason4313b392008-01-03 09:08:48 -05005740 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05005741
Zheng Yan1a40e232008-09-26 10:09:34 -04005742 for (i = first_index ; i <= last_index; i++) {
5743 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04005744 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04005745 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04005746 }
5747 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04005748again:
5749 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04005750 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05005751 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04005752 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005753 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05005754 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04005755 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005756 if (!PageUptodate(page)) {
5757 btrfs_readpage(NULL, page);
5758 lock_page(page);
5759 if (!PageUptodate(page)) {
5760 unlock_page(page);
5761 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005762 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05005763 goto out_unlock;
5764 }
5765 }
Chris Masonec44a352008-04-28 15:29:52 -04005766 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04005767
Chris Masonedbd8d42007-12-21 16:27:24 -05005768 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5769 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05005770 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005771
Chris Mason3eaa2882008-07-24 11:57:52 -04005772 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5773 if (ordered) {
5774 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5775 unlock_page(page);
5776 page_cache_release(page);
5777 btrfs_start_ordered_extent(inode, ordered, 1);
5778 btrfs_put_ordered_extent(ordered);
5779 goto again;
5780 }
5781 set_page_extent_mapped(page);
5782
Zheng Yan1a40e232008-09-26 10:09:34 -04005783 if (i == first_index)
5784 set_extent_bits(io_tree, page_start, page_end,
5785 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05005786 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04005787
Chris Masona061fc82008-05-07 11:43:44 -04005788 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04005789 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005790
Chris Masond1310b22008-01-24 16:13:08 -05005791 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05005792 unlock_page(page);
5793 page_cache_release(page);
5794 }
5795
5796out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04005797 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05005798 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005799 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04005800 return ret;
5801}
5802
Chris Masond3977122009-01-05 21:25:51 -05005803static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04005804 struct btrfs_key *extent_key,
5805 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05005806{
Zheng Yan1a40e232008-09-26 10:09:34 -04005807 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5808 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5809 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04005810 u64 start = extent_key->objectid - offset;
5811 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005812
5813 em = alloc_extent_map(GFP_NOFS);
5814 BUG_ON(!em || IS_ERR(em));
5815
Yan Zheng66435582008-10-30 14:19:50 -04005816 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005817 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005818 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04005819 em->block_start = extent_key->objectid;
5820 em->bdev = root->fs_info->fs_devices->latest_bdev;
5821 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5822
5823 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04005824 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005825 while (1) {
5826 int ret;
Chris Mason890871b2009-09-02 16:24:52 -04005827 write_lock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005828 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005829 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005830 if (ret != -EEXIST) {
5831 free_extent_map(em);
5832 break;
5833 }
Yan Zheng66435582008-10-30 14:19:50 -04005834 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005835 }
Yan Zheng66435582008-10-30 14:19:50 -04005836 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005837
Yan Zheng66435582008-10-30 14:19:50 -04005838 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04005839}
5840
5841struct btrfs_ref_path {
5842 u64 extent_start;
5843 u64 nodes[BTRFS_MAX_LEVEL];
5844 u64 root_objectid;
5845 u64 root_generation;
5846 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005847 u32 num_refs;
5848 int lowest_level;
5849 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005850 int shared_level;
5851
5852 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5853 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04005854};
5855
5856struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04005857 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04005858 u64 disk_bytenr;
5859 u64 disk_num_bytes;
5860 u64 offset;
5861 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04005862 u8 compression;
5863 u8 encryption;
5864 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04005865};
5866
5867static int is_cowonly_root(u64 root_objectid)
5868{
5869 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5870 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5871 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5872 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05005873 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5874 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04005875 return 1;
5876 return 0;
5877}
5878
Chris Masond3977122009-01-05 21:25:51 -05005879static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005880 struct btrfs_root *extent_root,
5881 struct btrfs_ref_path *ref_path,
5882 int first_time)
5883{
5884 struct extent_buffer *leaf;
5885 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05005886 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05005887 struct btrfs_key key;
5888 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04005889 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05005890 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04005891 int level;
5892 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05005893
Zheng Yan1a40e232008-09-26 10:09:34 -04005894 path = btrfs_alloc_path();
5895 if (!path)
5896 return -ENOMEM;
5897
Zheng Yan1a40e232008-09-26 10:09:34 -04005898 if (first_time) {
5899 ref_path->lowest_level = -1;
5900 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005901 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005902 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04005903 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005904walk_down:
5905 level = ref_path->current_level - 1;
5906 while (level >= -1) {
5907 u64 parent;
5908 if (level < ref_path->lowest_level)
5909 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005910
Chris Masond3977122009-01-05 21:25:51 -05005911 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005912 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005913 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005914 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04005915 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005916
Zheng Yan1a40e232008-09-26 10:09:34 -04005917 parent = ref_path->nodes[level + 1];
5918 ref_path->nodes[level + 1] = 0;
5919 ref_path->current_level = level;
5920 BUG_ON(parent == 0);
5921
5922 key.objectid = bytenr;
5923 key.offset = parent + 1;
5924 key.type = BTRFS_EXTENT_REF_KEY;
5925
5926 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005927 if (ret < 0)
5928 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005929 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005930
Chris Masonedbd8d42007-12-21 16:27:24 -05005931 leaf = path->nodes[0];
5932 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04005933 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04005934 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04005935 if (ret < 0)
5936 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005937 if (ret > 0)
5938 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04005939 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04005940 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005941
5942 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04005943 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04005944 found_key.type == BTRFS_EXTENT_REF_KEY) {
5945 if (level < ref_path->shared_level)
5946 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005947 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005948 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005949next:
5950 level--;
5951 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04005952 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04005953 }
5954 /* reached lowest level */
5955 ret = 1;
5956 goto out;
5957walk_up:
5958 level = ref_path->current_level;
5959 while (level < BTRFS_MAX_LEVEL - 1) {
5960 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05005961
5962 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04005963 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05005964 else
Zheng Yan1a40e232008-09-26 10:09:34 -04005965 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05005966
Zheng Yan1a40e232008-09-26 10:09:34 -04005967 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05005968
Zheng Yan1a40e232008-09-26 10:09:34 -04005969 key.objectid = bytenr;
5970 key.offset = 0;
5971 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05005972
Zheng Yan1a40e232008-09-26 10:09:34 -04005973 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5974 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05005975 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005976
5977 leaf = path->nodes[0];
5978 nritems = btrfs_header_nritems(leaf);
5979 if (path->slots[0] >= nritems) {
5980 ret = btrfs_next_leaf(extent_root, path);
5981 if (ret < 0)
5982 goto out;
5983 if (ret > 0) {
5984 /* the extent was freed by someone */
5985 if (ref_path->lowest_level == level)
5986 goto out;
5987 btrfs_release_path(extent_root, path);
5988 goto walk_down;
5989 }
5990 leaf = path->nodes[0];
5991 }
5992
5993 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5994 if (found_key.objectid != bytenr ||
5995 found_key.type != BTRFS_EXTENT_REF_KEY) {
5996 /* the extent was freed by someone */
5997 if (ref_path->lowest_level == level) {
5998 ret = 1;
5999 goto out;
6000 }
6001 btrfs_release_path(extent_root, path);
6002 goto walk_down;
6003 }
6004found:
6005 ref = btrfs_item_ptr(leaf, path->slots[0],
6006 struct btrfs_extent_ref);
6007 ref_objectid = btrfs_ref_objectid(leaf, ref);
6008 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6009 if (first_time) {
6010 level = (int)ref_objectid;
6011 BUG_ON(level >= BTRFS_MAX_LEVEL);
6012 ref_path->lowest_level = level;
6013 ref_path->current_level = level;
6014 ref_path->nodes[level] = bytenr;
6015 } else {
6016 WARN_ON(ref_objectid != level);
6017 }
6018 } else {
6019 WARN_ON(level != -1);
6020 }
6021 first_time = 0;
6022
6023 if (ref_path->lowest_level == level) {
6024 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006025 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6026 }
6027
6028 /*
6029 * the block is tree root or the block isn't in reference
6030 * counted tree.
6031 */
6032 if (found_key.objectid == found_key.offset ||
6033 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6034 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6035 ref_path->root_generation =
6036 btrfs_ref_generation(leaf, ref);
6037 if (level < 0) {
6038 /* special reference from the tree log */
6039 ref_path->nodes[0] = found_key.offset;
6040 ref_path->current_level = 0;
6041 }
6042 ret = 0;
6043 goto out;
6044 }
6045
6046 level++;
6047 BUG_ON(ref_path->nodes[level] != 0);
6048 ref_path->nodes[level] = found_key.offset;
6049 ref_path->current_level = level;
6050
6051 /*
6052 * the reference was created in the running transaction,
6053 * no need to continue walking up.
6054 */
6055 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6056 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6057 ref_path->root_generation =
6058 btrfs_ref_generation(leaf, ref);
6059 ret = 0;
6060 goto out;
6061 }
6062
6063 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04006064 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04006065 }
6066 /* reached max tree level, but no tree root found. */
6067 BUG();
6068out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006069 btrfs_free_path(path);
6070 return ret;
6071}
6072
6073static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6074 struct btrfs_root *extent_root,
6075 struct btrfs_ref_path *ref_path,
6076 u64 extent_start)
6077{
6078 memset(ref_path, 0, sizeof(*ref_path));
6079 ref_path->extent_start = extent_start;
6080
6081 return __next_ref_path(trans, extent_root, ref_path, 1);
6082}
6083
6084static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6085 struct btrfs_root *extent_root,
6086 struct btrfs_ref_path *ref_path)
6087{
6088 return __next_ref_path(trans, extent_root, ref_path, 0);
6089}
6090
Chris Masond3977122009-01-05 21:25:51 -05006091static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04006092 struct btrfs_key *extent_key,
6093 u64 offset, int no_fragment,
6094 struct disk_extent **extents,
6095 int *nr_extents)
6096{
6097 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6098 struct btrfs_path *path;
6099 struct btrfs_file_extent_item *fi;
6100 struct extent_buffer *leaf;
6101 struct disk_extent *exts = *extents;
6102 struct btrfs_key found_key;
6103 u64 cur_pos;
6104 u64 last_byte;
6105 u32 nritems;
6106 int nr = 0;
6107 int max = *nr_extents;
6108 int ret;
6109
6110 WARN_ON(!no_fragment && *extents);
6111 if (!exts) {
6112 max = 1;
6113 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6114 if (!exts)
6115 return -ENOMEM;
6116 }
6117
6118 path = btrfs_alloc_path();
6119 BUG_ON(!path);
6120
6121 cur_pos = extent_key->objectid - offset;
6122 last_byte = extent_key->objectid + extent_key->offset;
6123 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6124 cur_pos, 0);
6125 if (ret < 0)
6126 goto out;
6127 if (ret > 0) {
6128 ret = -ENOENT;
6129 goto out;
6130 }
6131
6132 while (1) {
6133 leaf = path->nodes[0];
6134 nritems = btrfs_header_nritems(leaf);
6135 if (path->slots[0] >= nritems) {
6136 ret = btrfs_next_leaf(root, path);
6137 if (ret < 0)
6138 goto out;
6139 if (ret > 0)
6140 break;
6141 leaf = path->nodes[0];
6142 }
6143
6144 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6145 if (found_key.offset != cur_pos ||
6146 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6147 found_key.objectid != reloc_inode->i_ino)
6148 break;
6149
6150 fi = btrfs_item_ptr(leaf, path->slots[0],
6151 struct btrfs_file_extent_item);
6152 if (btrfs_file_extent_type(leaf, fi) !=
6153 BTRFS_FILE_EXTENT_REG ||
6154 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6155 break;
6156
6157 if (nr == max) {
6158 struct disk_extent *old = exts;
6159 max *= 2;
6160 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6161 memcpy(exts, old, sizeof(*exts) * nr);
6162 if (old != *extents)
6163 kfree(old);
6164 }
6165
6166 exts[nr].disk_bytenr =
6167 btrfs_file_extent_disk_bytenr(leaf, fi);
6168 exts[nr].disk_num_bytes =
6169 btrfs_file_extent_disk_num_bytes(leaf, fi);
6170 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6171 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04006172 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6173 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6174 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6175 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6176 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04006177 BUG_ON(exts[nr].offset > 0);
6178 BUG_ON(exts[nr].compression || exts[nr].encryption);
6179 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006180
6181 cur_pos += exts[nr].num_bytes;
6182 nr++;
6183
6184 if (cur_pos + offset >= last_byte)
6185 break;
6186
6187 if (no_fragment) {
6188 ret = 1;
6189 goto out;
6190 }
6191 path->slots[0]++;
6192 }
6193
Yan Zheng1f80e4d2008-12-19 10:59:04 -05006194 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04006195 if (cur_pos + offset < last_byte) {
6196 ret = -ENOENT;
6197 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05006198 }
6199 ret = 0;
6200out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006201 btrfs_free_path(path);
6202 if (ret) {
6203 if (exts != *extents)
6204 kfree(exts);
6205 } else {
6206 *extents = exts;
6207 *nr_extents = nr;
6208 }
6209 return ret;
6210}
6211
Chris Masond3977122009-01-05 21:25:51 -05006212static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006213 struct btrfs_root *root,
6214 struct btrfs_path *path,
6215 struct btrfs_key *extent_key,
6216 struct btrfs_key *leaf_key,
6217 struct btrfs_ref_path *ref_path,
6218 struct disk_extent *new_extents,
6219 int nr_extents)
6220{
6221 struct extent_buffer *leaf;
6222 struct btrfs_file_extent_item *fi;
6223 struct inode *inode = NULL;
6224 struct btrfs_key key;
6225 u64 lock_start = 0;
6226 u64 lock_end = 0;
6227 u64 num_bytes;
6228 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05006229 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006230 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006231 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006232 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04006233 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04006234 int ret;
6235
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006236 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04006237 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006238 if (key.objectid < ref_path->owner_objectid ||
6239 (key.objectid == ref_path->owner_objectid &&
6240 key.type < BTRFS_EXTENT_DATA_KEY)) {
6241 key.objectid = ref_path->owner_objectid;
6242 key.type = BTRFS_EXTENT_DATA_KEY;
6243 key.offset = 0;
6244 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006245 }
6246
6247 while (1) {
6248 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6249 if (ret < 0)
6250 goto out;
6251
6252 leaf = path->nodes[0];
6253 nritems = btrfs_header_nritems(leaf);
6254next:
6255 if (extent_locked && ret > 0) {
6256 /*
6257 * the file extent item was modified by someone
6258 * before the extent got locked.
6259 */
Zheng Yan1a40e232008-09-26 10:09:34 -04006260 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6261 lock_end, GFP_NOFS);
6262 extent_locked = 0;
6263 }
6264
6265 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006266 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04006267 break;
6268
6269 BUG_ON(extent_locked);
6270 ret = btrfs_next_leaf(root, path);
6271 if (ret < 0)
6272 goto out;
6273 if (ret > 0)
6274 break;
6275 leaf = path->nodes[0];
6276 nritems = btrfs_header_nritems(leaf);
6277 }
6278
6279 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6280
6281 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6282 if ((key.objectid > ref_path->owner_objectid) ||
6283 (key.objectid == ref_path->owner_objectid &&
6284 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05006285 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006286 break;
6287 }
6288
6289 if (inode && key.objectid != inode->i_ino) {
6290 BUG_ON(extent_locked);
6291 btrfs_release_path(root, path);
6292 mutex_unlock(&inode->i_mutex);
6293 iput(inode);
6294 inode = NULL;
6295 continue;
6296 }
6297
6298 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6299 path->slots[0]++;
6300 ret = 1;
6301 goto next;
6302 }
6303 fi = btrfs_item_ptr(leaf, path->slots[0],
6304 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04006305 extent_type = btrfs_file_extent_type(leaf, fi);
6306 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6307 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04006308 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6309 extent_key->objectid)) {
6310 path->slots[0]++;
6311 ret = 1;
6312 goto next;
6313 }
6314
6315 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6316 ext_offset = btrfs_file_extent_offset(leaf, fi);
6317
Yan Zheng86288a12009-01-21 10:49:16 -05006318 if (search_end == (u64)-1) {
6319 search_end = key.offset - ext_offset +
6320 btrfs_file_extent_ram_bytes(leaf, fi);
6321 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006322
6323 if (!extent_locked) {
6324 lock_start = key.offset;
6325 lock_end = lock_start + num_bytes - 1;
6326 } else {
Yan Zheng66435582008-10-30 14:19:50 -04006327 if (lock_start > key.offset ||
6328 lock_end + 1 < key.offset + num_bytes) {
6329 unlock_extent(&BTRFS_I(inode)->io_tree,
6330 lock_start, lock_end, GFP_NOFS);
6331 extent_locked = 0;
6332 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006333 }
6334
6335 if (!inode) {
6336 btrfs_release_path(root, path);
6337
6338 inode = btrfs_iget_locked(root->fs_info->sb,
6339 key.objectid, root);
6340 if (inode->i_state & I_NEW) {
6341 BTRFS_I(inode)->root = root;
6342 BTRFS_I(inode)->location.objectid =
6343 key.objectid;
6344 BTRFS_I(inode)->location.type =
6345 BTRFS_INODE_ITEM_KEY;
6346 BTRFS_I(inode)->location.offset = 0;
6347 btrfs_read_locked_inode(inode);
6348 unlock_new_inode(inode);
6349 }
6350 /*
6351 * some code call btrfs_commit_transaction while
6352 * holding the i_mutex, so we can't use mutex_lock
6353 * here.
6354 */
6355 if (is_bad_inode(inode) ||
6356 !mutex_trylock(&inode->i_mutex)) {
6357 iput(inode);
6358 inode = NULL;
6359 key.offset = (u64)-1;
6360 goto skip;
6361 }
6362 }
6363
6364 if (!extent_locked) {
6365 struct btrfs_ordered_extent *ordered;
6366
6367 btrfs_release_path(root, path);
6368
6369 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6370 lock_end, GFP_NOFS);
6371 ordered = btrfs_lookup_first_ordered_extent(inode,
6372 lock_end);
6373 if (ordered &&
6374 ordered->file_offset <= lock_end &&
6375 ordered->file_offset + ordered->len > lock_start) {
6376 unlock_extent(&BTRFS_I(inode)->io_tree,
6377 lock_start, lock_end, GFP_NOFS);
6378 btrfs_start_ordered_extent(inode, ordered, 1);
6379 btrfs_put_ordered_extent(ordered);
6380 key.offset += num_bytes;
6381 goto skip;
6382 }
6383 if (ordered)
6384 btrfs_put_ordered_extent(ordered);
6385
Zheng Yan1a40e232008-09-26 10:09:34 -04006386 extent_locked = 1;
6387 continue;
6388 }
6389
6390 if (nr_extents == 1) {
6391 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04006392 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6393 new_extents[0].disk_bytenr);
6394 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6395 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006396 btrfs_mark_buffer_dirty(leaf);
6397
6398 btrfs_drop_extent_cache(inode, key.offset,
6399 key.offset + num_bytes - 1, 0);
6400
6401 ret = btrfs_inc_extent_ref(trans, root,
6402 new_extents[0].disk_bytenr,
6403 new_extents[0].disk_num_bytes,
6404 leaf->start,
6405 root->root_key.objectid,
6406 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006407 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006408 BUG_ON(ret);
6409
6410 ret = btrfs_free_extent(trans, root,
6411 extent_key->objectid,
6412 extent_key->offset,
6413 leaf->start,
6414 btrfs_header_owner(leaf),
6415 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006416 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006417 BUG_ON(ret);
6418
6419 btrfs_release_path(root, path);
6420 key.offset += num_bytes;
6421 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04006422 BUG_ON(1);
6423#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04006424 u64 alloc_hint;
6425 u64 extent_len;
6426 int i;
6427 /*
6428 * drop old extent pointer at first, then insert the
6429 * new pointers one bye one
6430 */
6431 btrfs_release_path(root, path);
6432 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6433 key.offset + num_bytes,
6434 key.offset, &alloc_hint);
6435 BUG_ON(ret);
6436
6437 for (i = 0; i < nr_extents; i++) {
6438 if (ext_offset >= new_extents[i].num_bytes) {
6439 ext_offset -= new_extents[i].num_bytes;
6440 continue;
6441 }
6442 extent_len = min(new_extents[i].num_bytes -
6443 ext_offset, num_bytes);
6444
6445 ret = btrfs_insert_empty_item(trans, root,
6446 path, &key,
6447 sizeof(*fi));
6448 BUG_ON(ret);
6449
6450 leaf = path->nodes[0];
6451 fi = btrfs_item_ptr(leaf, path->slots[0],
6452 struct btrfs_file_extent_item);
6453 btrfs_set_file_extent_generation(leaf, fi,
6454 trans->transid);
6455 btrfs_set_file_extent_type(leaf, fi,
6456 BTRFS_FILE_EXTENT_REG);
6457 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6458 new_extents[i].disk_bytenr);
6459 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6460 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04006461 btrfs_set_file_extent_ram_bytes(leaf, fi,
6462 new_extents[i].ram_bytes);
6463
6464 btrfs_set_file_extent_compression(leaf, fi,
6465 new_extents[i].compression);
6466 btrfs_set_file_extent_encryption(leaf, fi,
6467 new_extents[i].encryption);
6468 btrfs_set_file_extent_other_encoding(leaf, fi,
6469 new_extents[i].other_encoding);
6470
Zheng Yan1a40e232008-09-26 10:09:34 -04006471 btrfs_set_file_extent_num_bytes(leaf, fi,
6472 extent_len);
6473 ext_offset += new_extents[i].offset;
6474 btrfs_set_file_extent_offset(leaf, fi,
6475 ext_offset);
6476 btrfs_mark_buffer_dirty(leaf);
6477
6478 btrfs_drop_extent_cache(inode, key.offset,
6479 key.offset + extent_len - 1, 0);
6480
6481 ret = btrfs_inc_extent_ref(trans, root,
6482 new_extents[i].disk_bytenr,
6483 new_extents[i].disk_num_bytes,
6484 leaf->start,
6485 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006486 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006487 BUG_ON(ret);
6488 btrfs_release_path(root, path);
6489
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006490 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04006491
6492 ext_offset = 0;
6493 num_bytes -= extent_len;
6494 key.offset += extent_len;
6495
6496 if (num_bytes == 0)
6497 break;
6498 }
6499 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04006500#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04006501 }
6502
6503 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006504 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6505 lock_end, GFP_NOFS);
6506 extent_locked = 0;
6507 }
6508skip:
6509 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05006510 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04006511 break;
6512
6513 cond_resched();
6514 }
6515 ret = 0;
6516out:
6517 btrfs_release_path(root, path);
6518 if (inode) {
6519 mutex_unlock(&inode->i_mutex);
6520 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006521 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6522 lock_end, GFP_NOFS);
6523 }
6524 iput(inode);
6525 }
6526 return ret;
6527}
6528
Zheng Yan1a40e232008-09-26 10:09:34 -04006529int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6530 struct btrfs_root *root,
6531 struct extent_buffer *buf, u64 orig_start)
6532{
6533 int level;
6534 int ret;
6535
6536 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6537 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6538
6539 level = btrfs_header_level(buf);
6540 if (level == 0) {
6541 struct btrfs_leaf_ref *ref;
6542 struct btrfs_leaf_ref *orig_ref;
6543
6544 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6545 if (!orig_ref)
6546 return -ENOENT;
6547
6548 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6549 if (!ref) {
6550 btrfs_free_leaf_ref(root, orig_ref);
6551 return -ENOMEM;
6552 }
6553
6554 ref->nritems = orig_ref->nritems;
6555 memcpy(ref->extents, orig_ref->extents,
6556 sizeof(ref->extents[0]) * ref->nritems);
6557
6558 btrfs_free_leaf_ref(root, orig_ref);
6559
6560 ref->root_gen = trans->transid;
6561 ref->bytenr = buf->start;
6562 ref->owner = btrfs_header_owner(buf);
6563 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05006564
Zheng Yan1a40e232008-09-26 10:09:34 -04006565 ret = btrfs_add_leaf_ref(root, ref, 0);
6566 WARN_ON(ret);
6567 btrfs_free_leaf_ref(root, ref);
6568 }
6569 return 0;
6570}
6571
Chris Masond3977122009-01-05 21:25:51 -05006572static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04006573 struct extent_buffer *leaf,
6574 struct btrfs_block_group_cache *group,
6575 struct btrfs_root *target_root)
6576{
6577 struct btrfs_key key;
6578 struct inode *inode = NULL;
6579 struct btrfs_file_extent_item *fi;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006580 struct extent_state *cached_state = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04006581 u64 num_bytes;
6582 u64 skip_objectid = 0;
6583 u32 nritems;
6584 u32 i;
6585
6586 nritems = btrfs_header_nritems(leaf);
6587 for (i = 0; i < nritems; i++) {
6588 btrfs_item_key_to_cpu(leaf, &key, i);
6589 if (key.objectid == skip_objectid ||
6590 key.type != BTRFS_EXTENT_DATA_KEY)
6591 continue;
6592 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6593 if (btrfs_file_extent_type(leaf, fi) ==
6594 BTRFS_FILE_EXTENT_INLINE)
6595 continue;
6596 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6597 continue;
6598 if (!inode || inode->i_ino != key.objectid) {
6599 iput(inode);
6600 inode = btrfs_ilookup(target_root->fs_info->sb,
6601 key.objectid, target_root, 1);
6602 }
6603 if (!inode) {
6604 skip_objectid = key.objectid;
6605 continue;
6606 }
6607 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6608
Josef Bacik2ac55d42010-02-03 19:33:23 +00006609 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
6610 key.offset + num_bytes - 1, 0, &cached_state,
6611 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006612 btrfs_drop_extent_cache(inode, key.offset,
6613 key.offset + num_bytes - 1, 1);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006614 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
6615 key.offset + num_bytes - 1, &cached_state,
6616 GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006617 cond_resched();
6618 }
6619 iput(inode);
6620 return 0;
6621}
6622
Chris Masond3977122009-01-05 21:25:51 -05006623static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006624 struct btrfs_root *root,
6625 struct extent_buffer *leaf,
6626 struct btrfs_block_group_cache *group,
6627 struct inode *reloc_inode)
6628{
6629 struct btrfs_key key;
6630 struct btrfs_key extent_key;
6631 struct btrfs_file_extent_item *fi;
6632 struct btrfs_leaf_ref *ref;
6633 struct disk_extent *new_extent;
6634 u64 bytenr;
6635 u64 num_bytes;
6636 u32 nritems;
6637 u32 i;
6638 int ext_index;
6639 int nr_extent;
6640 int ret;
6641
6642 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6643 BUG_ON(!new_extent);
6644
6645 ref = btrfs_lookup_leaf_ref(root, leaf->start);
6646 BUG_ON(!ref);
6647
6648 ext_index = -1;
6649 nritems = btrfs_header_nritems(leaf);
6650 for (i = 0; i < nritems; i++) {
6651 btrfs_item_key_to_cpu(leaf, &key, i);
6652 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6653 continue;
6654 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6655 if (btrfs_file_extent_type(leaf, fi) ==
6656 BTRFS_FILE_EXTENT_INLINE)
6657 continue;
6658 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6659 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6660 if (bytenr == 0)
6661 continue;
6662
6663 ext_index++;
6664 if (bytenr >= group->key.objectid + group->key.offset ||
6665 bytenr + num_bytes <= group->key.objectid)
6666 continue;
6667
6668 extent_key.objectid = bytenr;
6669 extent_key.offset = num_bytes;
6670 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6671 nr_extent = 1;
6672 ret = get_new_locations(reloc_inode, &extent_key,
6673 group->key.objectid, 1,
6674 &new_extent, &nr_extent);
6675 if (ret > 0)
6676 continue;
6677 BUG_ON(ret < 0);
6678
6679 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6680 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6681 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6682 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6683
Zheng Yan1a40e232008-09-26 10:09:34 -04006684 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6685 new_extent->disk_bytenr);
6686 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6687 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04006688 btrfs_mark_buffer_dirty(leaf);
6689
6690 ret = btrfs_inc_extent_ref(trans, root,
6691 new_extent->disk_bytenr,
6692 new_extent->disk_num_bytes,
6693 leaf->start,
6694 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006695 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006696 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04006697
Zheng Yan1a40e232008-09-26 10:09:34 -04006698 ret = btrfs_free_extent(trans, root,
6699 bytenr, num_bytes, leaf->start,
6700 btrfs_header_owner(leaf),
6701 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04006702 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006703 BUG_ON(ret);
6704 cond_resched();
6705 }
6706 kfree(new_extent);
6707 BUG_ON(ext_index + 1 != ref->nritems);
6708 btrfs_free_leaf_ref(root, ref);
6709 return 0;
6710}
6711
Yan Zhengf82d02d2008-10-29 14:49:05 -04006712int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6713 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04006714{
6715 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006716 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04006717
6718 if (root->reloc_root) {
6719 reloc_root = root->reloc_root;
6720 root->reloc_root = NULL;
6721 list_add(&reloc_root->dead_list,
6722 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006723
6724 btrfs_set_root_bytenr(&reloc_root->root_item,
6725 reloc_root->node->start);
6726 btrfs_set_root_level(&root->root_item,
6727 btrfs_header_level(reloc_root->node));
6728 memset(&reloc_root->root_item.drop_progress, 0,
6729 sizeof(struct btrfs_disk_key));
6730 reloc_root->root_item.drop_level = 0;
6731
6732 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6733 &reloc_root->root_key,
6734 &reloc_root->root_item);
6735 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04006736 }
6737 return 0;
6738}
6739
6740int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6741{
6742 struct btrfs_trans_handle *trans;
6743 struct btrfs_root *reloc_root;
6744 struct btrfs_root *prev_root = NULL;
6745 struct list_head dead_roots;
6746 int ret;
6747 unsigned long nr;
6748
6749 INIT_LIST_HEAD(&dead_roots);
6750 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6751
6752 while (!list_empty(&dead_roots)) {
6753 reloc_root = list_entry(dead_roots.prev,
6754 struct btrfs_root, dead_list);
6755 list_del_init(&reloc_root->dead_list);
6756
6757 BUG_ON(reloc_root->commit_root != NULL);
6758 while (1) {
6759 trans = btrfs_join_transaction(root, 1);
6760 BUG_ON(!trans);
6761
6762 mutex_lock(&root->fs_info->drop_mutex);
6763 ret = btrfs_drop_snapshot(trans, reloc_root);
6764 if (ret != -EAGAIN)
6765 break;
6766 mutex_unlock(&root->fs_info->drop_mutex);
6767
6768 nr = trans->blocks_used;
6769 ret = btrfs_end_transaction(trans, root);
6770 BUG_ON(ret);
6771 btrfs_btree_balance_dirty(root, nr);
6772 }
6773
6774 free_extent_buffer(reloc_root->node);
6775
6776 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6777 &reloc_root->root_key);
6778 BUG_ON(ret);
6779 mutex_unlock(&root->fs_info->drop_mutex);
6780
6781 nr = trans->blocks_used;
6782 ret = btrfs_end_transaction(trans, root);
6783 BUG_ON(ret);
6784 btrfs_btree_balance_dirty(root, nr);
6785
6786 kfree(prev_root);
6787 prev_root = reloc_root;
6788 }
6789 if (prev_root) {
6790 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6791 kfree(prev_root);
6792 }
6793 return 0;
6794}
6795
6796int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6797{
6798 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6799 return 0;
6800}
6801
6802int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6803{
6804 struct btrfs_root *reloc_root;
6805 struct btrfs_trans_handle *trans;
6806 struct btrfs_key location;
6807 int found;
6808 int ret;
6809
6810 mutex_lock(&root->fs_info->tree_reloc_mutex);
6811 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6812 BUG_ON(ret);
6813 found = !list_empty(&root->fs_info->dead_reloc_roots);
6814 mutex_unlock(&root->fs_info->tree_reloc_mutex);
6815
6816 if (found) {
6817 trans = btrfs_start_transaction(root, 1);
6818 BUG_ON(!trans);
6819 ret = btrfs_commit_transaction(trans, root);
6820 BUG_ON(ret);
6821 }
6822
6823 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6824 location.offset = (u64)-1;
6825 location.type = BTRFS_ROOT_ITEM_KEY;
6826
6827 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6828 BUG_ON(!reloc_root);
6829 btrfs_orphan_cleanup(reloc_root);
6830 return 0;
6831}
6832
Chris Masond3977122009-01-05 21:25:51 -05006833static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006834 struct btrfs_root *root)
6835{
6836 struct btrfs_root *reloc_root;
6837 struct extent_buffer *eb;
6838 struct btrfs_root_item *root_item;
6839 struct btrfs_key root_key;
6840 int ret;
6841
6842 BUG_ON(!root->ref_cows);
6843 if (root->reloc_root)
6844 return 0;
6845
6846 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6847 BUG_ON(!root_item);
6848
6849 ret = btrfs_copy_root(trans, root, root->commit_root,
6850 &eb, BTRFS_TREE_RELOC_OBJECTID);
6851 BUG_ON(ret);
6852
6853 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6854 root_key.offset = root->root_key.objectid;
6855 root_key.type = BTRFS_ROOT_ITEM_KEY;
6856
6857 memcpy(root_item, &root->root_item, sizeof(root_item));
6858 btrfs_set_root_refs(root_item, 0);
6859 btrfs_set_root_bytenr(root_item, eb->start);
6860 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04006861 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04006862
6863 btrfs_tree_unlock(eb);
6864 free_extent_buffer(eb);
6865
6866 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6867 &root_key, root_item);
6868 BUG_ON(ret);
6869 kfree(root_item);
6870
6871 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6872 &root_key);
6873 BUG_ON(!reloc_root);
6874 reloc_root->last_trans = trans->transid;
6875 reloc_root->commit_root = NULL;
6876 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6877
6878 root->reloc_root = reloc_root;
6879 return 0;
6880}
6881
6882/*
6883 * Core function of space balance.
6884 *
6885 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04006886 * counted roots. There is one reloc tree for each subvol, and all
6887 * reloc trees share same root key objectid. Reloc trees are snapshots
6888 * of the latest committed roots of subvols (root->commit_root).
6889 *
6890 * To relocate a tree block referenced by a subvol, there are two steps.
6891 * COW the block through subvol's reloc tree, then update block pointer
6892 * in the subvol to point to the new block. Since all reloc trees share
6893 * same root key objectid, doing special handing for tree blocks owned
6894 * by them is easy. Once a tree block has been COWed in one reloc tree,
6895 * we can use the resulting new block directly when the same block is
6896 * required to COW again through other reloc trees. By this way, relocated
6897 * tree blocks are shared between reloc trees, so they are also shared
6898 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04006899 */
Chris Masond3977122009-01-05 21:25:51 -05006900static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006901 struct btrfs_root *root,
6902 struct btrfs_path *path,
6903 struct btrfs_key *first_key,
6904 struct btrfs_ref_path *ref_path,
6905 struct btrfs_block_group_cache *group,
6906 struct inode *reloc_inode)
6907{
6908 struct btrfs_root *reloc_root;
6909 struct extent_buffer *eb = NULL;
6910 struct btrfs_key *keys;
6911 u64 *nodes;
6912 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006913 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04006914 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006915 int ret;
6916
6917 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6918 lowest_level = ref_path->owner_objectid;
6919
Yan Zhengf82d02d2008-10-29 14:49:05 -04006920 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04006921 path->lowest_level = lowest_level;
6922 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6923 BUG_ON(ret < 0);
6924 path->lowest_level = 0;
6925 btrfs_release_path(root, path);
6926 return 0;
6927 }
6928
Zheng Yan1a40e232008-09-26 10:09:34 -04006929 mutex_lock(&root->fs_info->tree_reloc_mutex);
6930 ret = init_reloc_tree(trans, root);
6931 BUG_ON(ret);
6932 reloc_root = root->reloc_root;
6933
Yan Zhengf82d02d2008-10-29 14:49:05 -04006934 shared_level = ref_path->shared_level;
6935 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04006936
Yan Zhengf82d02d2008-10-29 14:49:05 -04006937 keys = ref_path->node_keys;
6938 nodes = ref_path->new_nodes;
6939 memset(&keys[shared_level + 1], 0,
6940 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6941 memset(&nodes[shared_level + 1], 0,
6942 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04006943
Yan Zhengf82d02d2008-10-29 14:49:05 -04006944 if (nodes[lowest_level] == 0) {
6945 path->lowest_level = lowest_level;
6946 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6947 0, 1);
6948 BUG_ON(ret);
6949 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6950 eb = path->nodes[level];
6951 if (!eb || eb == reloc_root->node)
6952 break;
6953 nodes[level] = eb->start;
6954 if (level == 0)
6955 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6956 else
6957 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6958 }
Yan Zheng2b820322008-11-17 21:11:30 -05006959 if (nodes[0] &&
6960 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006961 eb = path->nodes[0];
6962 ret = replace_extents_in_leaf(trans, reloc_root, eb,
6963 group, reloc_inode);
6964 BUG_ON(ret);
6965 }
6966 btrfs_release_path(reloc_root, path);
6967 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04006968 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006969 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04006970 BUG_ON(ret);
6971 }
6972
Zheng Yan1a40e232008-09-26 10:09:34 -04006973 /*
6974 * replace tree blocks in the fs tree with tree blocks in
6975 * the reloc tree.
6976 */
6977 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6978 BUG_ON(ret < 0);
6979
6980 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006981 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6982 0, 0);
6983 BUG_ON(ret);
6984 extent_buffer_get(path->nodes[0]);
6985 eb = path->nodes[0];
6986 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006987 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6988 BUG_ON(ret);
6989 free_extent_buffer(eb);
6990 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006991
Yan Zhengf82d02d2008-10-29 14:49:05 -04006992 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04006993 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006994 return 0;
6995}
6996
Chris Masond3977122009-01-05 21:25:51 -05006997static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04006998 struct btrfs_root *root,
6999 struct btrfs_path *path,
7000 struct btrfs_key *first_key,
7001 struct btrfs_ref_path *ref_path)
7002{
7003 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04007004
7005 ret = relocate_one_path(trans, root, path, first_key,
7006 ref_path, NULL, NULL);
7007 BUG_ON(ret);
7008
Zheng Yan1a40e232008-09-26 10:09:34 -04007009 return 0;
7010}
7011
Chris Masond3977122009-01-05 21:25:51 -05007012static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04007013 struct btrfs_root *extent_root,
7014 struct btrfs_path *path,
7015 struct btrfs_key *extent_key)
7016{
7017 int ret;
7018
Zheng Yan1a40e232008-09-26 10:09:34 -04007019 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7020 if (ret)
7021 goto out;
7022 ret = btrfs_del_item(trans, extent_root, path);
7023out:
Chris Masonedbd8d42007-12-21 16:27:24 -05007024 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007025 return ret;
7026}
7027
Chris Masond3977122009-01-05 21:25:51 -05007028static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04007029 struct btrfs_ref_path *ref_path)
7030{
7031 struct btrfs_key root_key;
7032
7033 root_key.objectid = ref_path->root_objectid;
7034 root_key.type = BTRFS_ROOT_ITEM_KEY;
7035 if (is_cowonly_root(ref_path->root_objectid))
7036 root_key.offset = 0;
7037 else
7038 root_key.offset = (u64)-1;
7039
7040 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7041}
7042
Chris Masond3977122009-01-05 21:25:51 -05007043static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04007044 struct btrfs_path *path,
7045 struct btrfs_key *extent_key,
7046 struct btrfs_block_group_cache *group,
7047 struct inode *reloc_inode, int pass)
7048{
7049 struct btrfs_trans_handle *trans;
7050 struct btrfs_root *found_root;
7051 struct btrfs_ref_path *ref_path = NULL;
7052 struct disk_extent *new_extents = NULL;
7053 int nr_extents = 0;
7054 int loops;
7055 int ret;
7056 int level;
7057 struct btrfs_key first_key;
7058 u64 prev_block = 0;
7059
Zheng Yan1a40e232008-09-26 10:09:34 -04007060
7061 trans = btrfs_start_transaction(extent_root, 1);
7062 BUG_ON(!trans);
7063
7064 if (extent_key->objectid == 0) {
7065 ret = del_extent_zero(trans, extent_root, path, extent_key);
7066 goto out;
7067 }
7068
7069 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7070 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05007071 ret = -ENOMEM;
7072 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04007073 }
7074
7075 for (loops = 0; ; loops++) {
7076 if (loops == 0) {
7077 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7078 extent_key->objectid);
7079 } else {
7080 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7081 }
7082 if (ret < 0)
7083 goto out;
7084 if (ret > 0)
7085 break;
7086
7087 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7088 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7089 continue;
7090
7091 found_root = read_ref_root(extent_root->fs_info, ref_path);
7092 BUG_ON(!found_root);
7093 /*
7094 * for reference counted tree, only process reference paths
7095 * rooted at the latest committed root.
7096 */
7097 if (found_root->ref_cows &&
7098 ref_path->root_generation != found_root->root_key.offset)
7099 continue;
7100
7101 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7102 if (pass == 0) {
7103 /*
7104 * copy data extents to new locations
7105 */
7106 u64 group_start = group->key.objectid;
7107 ret = relocate_data_extent(reloc_inode,
7108 extent_key,
7109 group_start);
7110 if (ret < 0)
7111 goto out;
7112 break;
7113 }
7114 level = 0;
7115 } else {
7116 level = ref_path->owner_objectid;
7117 }
7118
7119 if (prev_block != ref_path->nodes[level]) {
7120 struct extent_buffer *eb;
7121 u64 block_start = ref_path->nodes[level];
7122 u64 block_size = btrfs_level_size(found_root, level);
7123
7124 eb = read_tree_block(found_root, block_start,
7125 block_size, 0);
7126 btrfs_tree_lock(eb);
7127 BUG_ON(level != btrfs_header_level(eb));
7128
7129 if (level == 0)
7130 btrfs_item_key_to_cpu(eb, &first_key, 0);
7131 else
7132 btrfs_node_key_to_cpu(eb, &first_key, 0);
7133
7134 btrfs_tree_unlock(eb);
7135 free_extent_buffer(eb);
7136 prev_block = block_start;
7137 }
7138
Yan Zheng24562422009-02-12 14:14:53 -05007139 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007140 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05007141 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05007142 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7143 /*
7144 * try to update data extent references while
7145 * keeping metadata shared between snapshots.
7146 */
7147 if (pass == 1) {
7148 ret = relocate_one_path(trans, found_root,
7149 path, &first_key, ref_path,
7150 group, reloc_inode);
7151 if (ret < 0)
7152 goto out;
7153 continue;
7154 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007155 /*
7156 * use fallback method to process the remaining
7157 * references.
7158 */
7159 if (!new_extents) {
7160 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04007161 new_extents = kmalloc(sizeof(*new_extents),
7162 GFP_NOFS);
7163 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007164 ret = get_new_locations(reloc_inode,
7165 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04007166 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04007167 &new_extents,
7168 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04007169 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04007170 goto out;
7171 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007172 ret = replace_one_extent(trans, found_root,
7173 path, extent_key,
7174 &first_key, ref_path,
7175 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05007176 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04007177 ret = relocate_tree_block(trans, found_root, path,
7178 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04007179 }
7180 if (ret < 0)
7181 goto out;
7182 }
7183 ret = 0;
7184out:
7185 btrfs_end_transaction(trans, extent_root);
7186 kfree(new_extents);
7187 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05007188 return ret;
7189}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007190#endif
Chris Masonedbd8d42007-12-21 16:27:24 -05007191
Chris Masonec44a352008-04-28 15:29:52 -04007192static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7193{
7194 u64 num_devices;
7195 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7196 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7197
Yan Zheng2b820322008-11-17 21:11:30 -05007198 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04007199 if (num_devices == 1) {
7200 stripped |= BTRFS_BLOCK_GROUP_DUP;
7201 stripped = flags & ~stripped;
7202
7203 /* turn raid0 into single device chunks */
7204 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7205 return stripped;
7206
7207 /* turn mirroring into duplication */
7208 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7209 BTRFS_BLOCK_GROUP_RAID10))
7210 return stripped | BTRFS_BLOCK_GROUP_DUP;
7211 return flags;
7212 } else {
7213 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007214 if (flags & stripped)
7215 return flags;
7216
7217 stripped |= BTRFS_BLOCK_GROUP_DUP;
7218 stripped = flags & ~stripped;
7219
7220 /* switch duplicated blocks with raid1 */
7221 if (flags & BTRFS_BLOCK_GROUP_DUP)
7222 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7223
7224 /* turn single device chunks into raid0 */
7225 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7226 }
7227 return flags;
7228}
7229
Christoph Hellwigb2950862008-12-02 09:54:17 -05007230static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04007231 struct btrfs_block_group_cache *shrink_block_group,
7232 int force)
7233{
7234 struct btrfs_trans_handle *trans;
7235 u64 new_alloc_flags;
7236 u64 calc;
7237
Chris Masonc286ac42008-07-22 23:06:41 -04007238 spin_lock(&shrink_block_group->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007239 if (btrfs_block_group_used(&shrink_block_group->item) +
7240 shrink_block_group->reserved > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04007241 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04007242
Chris Mason0ef3e662008-05-24 14:04:53 -04007243 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007244 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04007245
Chris Mason0ef3e662008-05-24 14:04:53 -04007246 new_alloc_flags = update_block_group_flags(root,
7247 shrink_block_group->flags);
7248 if (new_alloc_flags != shrink_block_group->flags) {
7249 calc =
7250 btrfs_block_group_used(&shrink_block_group->item);
7251 } else {
7252 calc = shrink_block_group->key.offset;
7253 }
Chris Masonc286ac42008-07-22 23:06:41 -04007254 spin_unlock(&shrink_block_group->lock);
7255
Chris Mason0ef3e662008-05-24 14:04:53 -04007256 do_chunk_alloc(trans, root->fs_info->extent_root,
7257 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04007258
Chris Mason0ef3e662008-05-24 14:04:53 -04007259 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04007260 } else
7261 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04007262 return 0;
7263}
7264
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007265
7266int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
7267 struct btrfs_block_group_cache *group)
7268
7269{
7270 __alloc_chunk_for_shrink(root, group, 1);
7271 set_block_group_readonly(group);
7272 return 0;
7273}
7274
Josef Bacikba1bf482009-09-11 16:11:19 -04007275/*
7276 * checks to see if its even possible to relocate this block group.
7277 *
7278 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7279 * ok to go ahead and try.
7280 */
7281int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007282{
Zheng Yan1a40e232008-09-26 10:09:34 -04007283 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007284 struct btrfs_space_info *space_info;
7285 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7286 struct btrfs_device *device;
7287 int full = 0;
7288 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007289
Josef Bacikba1bf482009-09-11 16:11:19 -04007290 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007291
Josef Bacikba1bf482009-09-11 16:11:19 -04007292 /* odd, couldn't find the block group, leave it alone */
7293 if (!block_group)
7294 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007295
Josef Bacikba1bf482009-09-11 16:11:19 -04007296 /* no bytes used, we're good */
7297 if (!btrfs_block_group_used(&block_group->item))
7298 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007299
Josef Bacikba1bf482009-09-11 16:11:19 -04007300 space_info = block_group->space_info;
7301 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007302
Josef Bacikba1bf482009-09-11 16:11:19 -04007303 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007304
Josef Bacikba1bf482009-09-11 16:11:19 -04007305 /*
7306 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007307 * relocate it unless we're able to allocate a new chunk below.
7308 *
7309 * Otherwise, we need to make sure we have room in the space to handle
7310 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007311 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007312 if ((space_info->total_bytes != block_group->key.offset) &&
7313 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04007314 space_info->bytes_pinned + space_info->bytes_readonly +
7315 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04007316 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007317 spin_unlock(&space_info->lock);
7318 goto out;
7319 }
7320 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007321
Josef Bacikba1bf482009-09-11 16:11:19 -04007322 /*
7323 * ok we don't have enough space, but maybe we have free space on our
7324 * devices to allocate new chunks for relocation, so loop through our
7325 * alloc devices and guess if we have enough space. However, if we
7326 * were marked as full, then we know there aren't enough chunks, and we
7327 * can just return.
7328 */
7329 ret = -1;
7330 if (full)
7331 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05007332
Josef Bacikba1bf482009-09-11 16:11:19 -04007333 mutex_lock(&root->fs_info->chunk_mutex);
7334 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7335 u64 min_free = btrfs_block_group_used(&block_group->item);
7336 u64 dev_offset, max_avail;
Chris Masonea8c2812008-08-04 23:17:27 -04007337
Josef Bacikba1bf482009-09-11 16:11:19 -04007338 /*
7339 * check to make sure we can actually find a chunk with enough
7340 * space to fit our block group in.
7341 */
7342 if (device->total_bytes > device->bytes_used + min_free) {
7343 ret = find_free_dev_extent(NULL, device, min_free,
7344 &dev_offset, &max_avail);
7345 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05007346 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04007347 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007348 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007349 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007350 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007351out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007352 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007353 return ret;
7354}
7355
Christoph Hellwigb2950862008-12-02 09:54:17 -05007356static int find_first_block_group(struct btrfs_root *root,
7357 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007358{
Chris Mason925baed2008-06-25 16:01:30 -04007359 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007360 struct btrfs_key found_key;
7361 struct extent_buffer *leaf;
7362 int slot;
7363
7364 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7365 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007366 goto out;
7367
Chris Masond3977122009-01-05 21:25:51 -05007368 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007369 slot = path->slots[0];
7370 leaf = path->nodes[0];
7371 if (slot >= btrfs_header_nritems(leaf)) {
7372 ret = btrfs_next_leaf(root, path);
7373 if (ret == 0)
7374 continue;
7375 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007376 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007377 break;
7378 }
7379 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7380
7381 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007382 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7383 ret = 0;
7384 goto out;
7385 }
Chris Mason0b86a832008-03-24 15:01:56 -04007386 path->slots[0]++;
7387 }
Chris Mason925baed2008-06-25 16:01:30 -04007388out:
Chris Mason0b86a832008-03-24 15:01:56 -04007389 return ret;
7390}
7391
Zheng Yan1a40e232008-09-26 10:09:34 -04007392int btrfs_free_block_groups(struct btrfs_fs_info *info)
7393{
7394 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007395 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007396 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007397 struct rb_node *n;
7398
Yan Zheng11833d62009-09-11 16:11:19 -04007399 down_write(&info->extent_commit_sem);
7400 while (!list_empty(&info->caching_block_groups)) {
7401 caching_ctl = list_entry(info->caching_block_groups.next,
7402 struct btrfs_caching_control, list);
7403 list_del(&caching_ctl->list);
7404 put_caching_control(caching_ctl);
7405 }
7406 up_write(&info->extent_commit_sem);
7407
Zheng Yan1a40e232008-09-26 10:09:34 -04007408 spin_lock(&info->block_group_cache_lock);
7409 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7410 block_group = rb_entry(n, struct btrfs_block_group_cache,
7411 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007412 rb_erase(&block_group->cache_node,
7413 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007414 spin_unlock(&info->block_group_cache_lock);
7415
Josef Bacik80eb2342008-10-29 14:49:05 -04007416 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007417 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007418 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007419
Josef Bacik817d52f2009-07-13 21:29:25 -04007420 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007421 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007422
7423 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007424 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007425
7426 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007427 }
7428 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007429
7430 /* now that all the block groups are freed, go through and
7431 * free all the space_info structs. This is only called during
7432 * the final stages of unmount, and so we know nobody is
7433 * using them. We call synchronize_rcu() once before we start,
7434 * just to be on the safe side.
7435 */
7436 synchronize_rcu();
7437
7438 while(!list_empty(&info->space_info)) {
7439 space_info = list_entry(info->space_info.next,
7440 struct btrfs_space_info,
7441 list);
7442
7443 list_del(&space_info->list);
7444 kfree(space_info);
7445 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007446 return 0;
7447}
7448
Chris Mason9078a3e2007-04-26 16:46:15 -04007449int btrfs_read_block_groups(struct btrfs_root *root)
7450{
7451 struct btrfs_path *path;
7452 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007453 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007454 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007455 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007456 struct btrfs_key key;
7457 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007458 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04007459
Chris Masonbe744172007-05-06 10:15:01 -04007460 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007461 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007462 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007463 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007464 path = btrfs_alloc_path();
7465 if (!path)
7466 return -ENOMEM;
7467
Chris Masond3977122009-01-05 21:25:51 -05007468 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007469 ret = find_first_block_group(root, path, &key);
7470 if (ret > 0) {
7471 ret = 0;
7472 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007473 }
Chris Mason0b86a832008-03-24 15:01:56 -04007474 if (ret != 0)
7475 goto error;
7476
Chris Mason5f39d392007-10-15 16:14:19 -04007477 leaf = path->nodes[0];
7478 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007479 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007480 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007481 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04007482 break;
7483 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007484
Yan Zhengd2fb3432008-12-11 16:30:39 -05007485 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007486 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007487 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007488 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007489 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007490 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007491
7492 /*
7493 * we only want to have 32k of ram per block group for keeping
7494 * track of free space, and if we pass 1/2 of that we want to
7495 * start converting things over to using bitmaps
7496 */
7497 cache->extents_thresh = ((1024 * 32) / 2) /
7498 sizeof(struct btrfs_free_space);
7499
Chris Mason5f39d392007-10-15 16:14:19 -04007500 read_extent_buffer(leaf, &cache->item,
7501 btrfs_item_ptr_offset(leaf, path->slots[0]),
7502 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007503 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007504
Chris Mason9078a3e2007-04-26 16:46:15 -04007505 key.objectid = found_key.objectid + found_key.offset;
7506 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04007507 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007508 cache->sectorsize = root->sectorsize;
7509
Josef Bacik817d52f2009-07-13 21:29:25 -04007510 /*
7511 * check for two cases, either we are full, and therefore
7512 * don't need to bother with the caching work since we won't
7513 * find any space, or we are empty, and we can just add all
7514 * the space in and be done with it. This saves us _alot_ of
7515 * time, particularly in the full case.
7516 */
7517 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Josef Bacik1b2da372009-09-11 16:11:20 -04007518 exclude_super_stripes(root, cache);
Yan Zheng11833d62009-09-11 16:11:19 -04007519 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007520 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007521 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007522 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007523 exclude_super_stripes(root, cache);
7524 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007525 cache->cached = BTRFS_CACHE_FINISHED;
7526 add_new_free_space(cache, root->fs_info,
7527 found_key.objectid,
7528 found_key.objectid +
7529 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007530 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007531 }
Chris Mason96b51792007-10-15 16:15:19 -04007532
Chris Mason6324fbf2008-03-24 15:01:59 -04007533 ret = update_space_info(info, cache->flags, found_key.offset,
7534 btrfs_block_group_used(&cache->item),
7535 &space_info);
7536 BUG_ON(ret);
7537 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007538 spin_lock(&cache->space_info->lock);
7539 cache->space_info->bytes_super += cache->bytes_super;
7540 spin_unlock(&cache->space_info->lock);
7541
Josef Bacik80eb2342008-10-29 14:49:05 -04007542 down_write(&space_info->groups_sem);
7543 list_add_tail(&cache->list, &space_info->block_groups);
7544 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007545
Josef Bacik0f9dd462008-09-23 13:14:11 -04007546 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7547 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007548
7549 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007550 if (btrfs_chunk_readonly(root, cache->key.objectid))
7551 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04007552 }
Chris Mason0b86a832008-03-24 15:01:56 -04007553 ret = 0;
7554error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007555 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007556 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007557}
Chris Mason6324fbf2008-03-24 15:01:59 -04007558
7559int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7560 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007561 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007562 u64 size)
7563{
7564 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007565 struct btrfs_root *extent_root;
7566 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007567
7568 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007569
Chris Mason12fcfd22009-03-24 10:24:20 -04007570 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007571
Chris Mason8f18cf12008-04-25 16:53:30 -04007572 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007573 if (!cache)
7574 return -ENOMEM;
7575
Chris Masone17cade2008-04-15 15:41:47 -04007576 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007577 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007578 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007579 cache->sectorsize = root->sectorsize;
7580
7581 /*
7582 * we only want to have 32k of ram per block group for keeping track
7583 * of free space, and if we pass 1/2 of that we want to start
7584 * converting things over to using bitmaps
7585 */
7586 cache->extents_thresh = ((1024 * 32) / 2) /
7587 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007588 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007589 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007590 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007591 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007592 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007593
Chris Mason6324fbf2008-03-24 15:01:59 -04007594 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007595 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7596 cache->flags = type;
7597 btrfs_set_block_group_flags(&cache->item, type);
7598
Yan Zheng11833d62009-09-11 16:11:19 -04007599 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007600 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007601 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007602
Josef Bacik817d52f2009-07-13 21:29:25 -04007603 add_new_free_space(cache, root->fs_info, chunk_offset,
7604 chunk_offset + size);
7605
Yan Zheng11833d62009-09-11 16:11:19 -04007606 free_excluded_extents(root, cache);
7607
Chris Mason6324fbf2008-03-24 15:01:59 -04007608 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7609 &cache->space_info);
7610 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007611
7612 spin_lock(&cache->space_info->lock);
7613 cache->space_info->bytes_super += cache->bytes_super;
7614 spin_unlock(&cache->space_info->lock);
7615
Josef Bacik80eb2342008-10-29 14:49:05 -04007616 down_write(&cache->space_info->groups_sem);
7617 list_add_tail(&cache->list, &cache->space_info->block_groups);
7618 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04007619
Josef Bacik0f9dd462008-09-23 13:14:11 -04007620 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7621 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007622
Chris Mason6324fbf2008-03-24 15:01:59 -04007623 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7624 sizeof(cache->item));
7625 BUG_ON(ret);
7626
Chris Masond18a2c42008-04-04 15:40:00 -04007627 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007628
Chris Mason6324fbf2008-03-24 15:01:59 -04007629 return 0;
7630}
Zheng Yan1a40e232008-09-26 10:09:34 -04007631
7632int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7633 struct btrfs_root *root, u64 group_start)
7634{
7635 struct btrfs_path *path;
7636 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007637 struct btrfs_free_cluster *cluster;
Zheng Yan1a40e232008-09-26 10:09:34 -04007638 struct btrfs_key key;
7639 int ret;
7640
Zheng Yan1a40e232008-09-26 10:09:34 -04007641 root = root->fs_info->extent_root;
7642
7643 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7644 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007645 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007646
7647 memcpy(&key, &block_group->key, sizeof(key));
7648
Chris Mason44fb5512009-06-04 15:34:51 -04007649 /* make sure this block group isn't part of an allocation cluster */
7650 cluster = &root->fs_info->data_alloc_cluster;
7651 spin_lock(&cluster->refill_lock);
7652 btrfs_return_cluster_to_free_space(block_group, cluster);
7653 spin_unlock(&cluster->refill_lock);
7654
7655 /*
7656 * make sure this block group isn't part of a metadata
7657 * allocation cluster
7658 */
7659 cluster = &root->fs_info->meta_alloc_cluster;
7660 spin_lock(&cluster->refill_lock);
7661 btrfs_return_cluster_to_free_space(block_group, cluster);
7662 spin_unlock(&cluster->refill_lock);
7663
Zheng Yan1a40e232008-09-26 10:09:34 -04007664 path = btrfs_alloc_path();
7665 BUG_ON(!path);
7666
Yan Zheng3dfdb932009-01-21 10:49:16 -05007667 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007668 rb_erase(&block_group->cache_node,
7669 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007670 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007671
Josef Bacik80eb2342008-10-29 14:49:05 -04007672 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007673 /*
7674 * we must use list_del_init so people can check to see if they
7675 * are still on the list after taking the semaphore
7676 */
7677 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007678 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007679
Josef Bacik817d52f2009-07-13 21:29:25 -04007680 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007681 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007682
7683 btrfs_remove_free_space_cache(block_group);
7684
Yan Zhengc146afa2008-11-12 14:34:12 -05007685 spin_lock(&block_group->space_info->lock);
7686 block_group->space_info->total_bytes -= block_group->key.offset;
7687 block_group->space_info->bytes_readonly -= block_group->key.offset;
7688 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007689
7690 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007691
Chris Masonfa9c0d792009-04-03 09:47:43 -04007692 btrfs_put_block_group(block_group);
7693 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007694
7695 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7696 if (ret > 0)
7697 ret = -EIO;
7698 if (ret < 0)
7699 goto out;
7700
7701 ret = btrfs_del_item(trans, root, path);
7702out:
7703 btrfs_free_path(path);
7704 return ret;
7705}