blob: 30c0558eae845fee8078988af186b800b1917179 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050027#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040031#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040033#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040034#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Chris Mason0e4f8f82011-04-15 16:05:44 -040036/* control flags for do_chunk_alloc's force field
37 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
38 * if we really need one.
39 *
40 * CHUNK_ALLOC_FORCE means it must try to allocate one
41 *
42 * CHUNK_ALLOC_LIMITED means to only try and allocate one
43 * if we have very few chunks already allocated. This is
44 * used as part of the clustering code to help make sure
45 * we have a good pool of storage to cluster in, without
46 * filling the FS with empty chunks
47 *
48 */
49enum {
50 CHUNK_ALLOC_NO_FORCE = 0,
51 CHUNK_ALLOC_FORCE = 1,
52 CHUNK_ALLOC_LIMITED = 2,
53};
54
Josef Bacikfb25e912011-07-26 17:00:46 -040055/*
56 * Control how reservations are dealt with.
57 *
58 * RESERVE_FREE - freeing a reservation.
59 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
60 * ENOSPC accounting
61 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
62 * bytes_may_use as the ENOSPC accounting is done elsewhere
63 */
64enum {
65 RESERVE_FREE = 0,
66 RESERVE_ALLOC = 1,
67 RESERVE_ALLOC_NO_ACCOUNT = 2,
68};
69
Josef Bacikf3465ca2008-11-12 14:19:50 -050070static int update_block_group(struct btrfs_trans_handle *trans,
71 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040072 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040073static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
74 struct btrfs_root *root,
75 u64 bytenr, u64 num_bytes, u64 parent,
76 u64 root_objectid, u64 owner_objectid,
77 u64 owner_offset, int refs_to_drop,
78 struct btrfs_delayed_extent_op *extra_op);
79static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
80 struct extent_buffer *leaf,
81 struct btrfs_extent_item *ei);
82static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
83 struct btrfs_root *root,
84 u64 parent, u64 root_objectid,
85 u64 flags, u64 owner, u64 offset,
86 struct btrfs_key *ins, int ref_mod);
87static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
88 struct btrfs_root *root,
89 u64 parent, u64 root_objectid,
90 u64 flags, struct btrfs_disk_key *key,
91 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050092static int do_chunk_alloc(struct btrfs_trans_handle *trans,
93 struct btrfs_root *extent_root, u64 alloc_bytes,
94 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040095static int find_next_key(struct btrfs_path *path, int level,
96 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040097static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
98 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -040099static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
100 u64 num_bytes, int reserve);
Josef Bacik6a632092009-02-20 11:00:09 -0500101
Josef Bacik817d52f2009-07-13 21:29:25 -0400102static noinline int
103block_group_cache_done(struct btrfs_block_group_cache *cache)
104{
105 smp_mb();
106 return cache->cached == BTRFS_CACHE_FINISHED;
107}
108
Josef Bacik0f9dd462008-09-23 13:14:11 -0400109static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
110{
111 return (cache->flags & bits) == bits;
112}
113
David Sterba62a45b62011-04-20 15:52:26 +0200114static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000115{
116 atomic_inc(&cache->count);
117}
118
119void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
120{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400121 if (atomic_dec_and_test(&cache->count)) {
122 WARN_ON(cache->pinned > 0);
123 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800124 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000125 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400126 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000127}
128
Josef Bacik0f9dd462008-09-23 13:14:11 -0400129/*
130 * this adds the block group to the fs_info rb tree for the block group
131 * cache
132 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500133static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400134 struct btrfs_block_group_cache *block_group)
135{
136 struct rb_node **p;
137 struct rb_node *parent = NULL;
138 struct btrfs_block_group_cache *cache;
139
140 spin_lock(&info->block_group_cache_lock);
141 p = &info->block_group_cache_tree.rb_node;
142
143 while (*p) {
144 parent = *p;
145 cache = rb_entry(parent, struct btrfs_block_group_cache,
146 cache_node);
147 if (block_group->key.objectid < cache->key.objectid) {
148 p = &(*p)->rb_left;
149 } else if (block_group->key.objectid > cache->key.objectid) {
150 p = &(*p)->rb_right;
151 } else {
152 spin_unlock(&info->block_group_cache_lock);
153 return -EEXIST;
154 }
155 }
156
157 rb_link_node(&block_group->cache_node, parent, p);
158 rb_insert_color(&block_group->cache_node,
159 &info->block_group_cache_tree);
160 spin_unlock(&info->block_group_cache_lock);
161
162 return 0;
163}
164
165/*
166 * This will return the block group at or after bytenr if contains is 0, else
167 * it will return the block group that contains the bytenr
168 */
169static struct btrfs_block_group_cache *
170block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
171 int contains)
172{
173 struct btrfs_block_group_cache *cache, *ret = NULL;
174 struct rb_node *n;
175 u64 end, start;
176
177 spin_lock(&info->block_group_cache_lock);
178 n = info->block_group_cache_tree.rb_node;
179
180 while (n) {
181 cache = rb_entry(n, struct btrfs_block_group_cache,
182 cache_node);
183 end = cache->key.objectid + cache->key.offset - 1;
184 start = cache->key.objectid;
185
186 if (bytenr < start) {
187 if (!contains && (!ret || start < ret->key.objectid))
188 ret = cache;
189 n = n->rb_left;
190 } else if (bytenr > start) {
191 if (contains && bytenr <= end) {
192 ret = cache;
193 break;
194 }
195 n = n->rb_right;
196 } else {
197 ret = cache;
198 break;
199 }
200 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500201 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000202 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400203 spin_unlock(&info->block_group_cache_lock);
204
205 return ret;
206}
207
Yan Zheng11833d62009-09-11 16:11:19 -0400208static int add_excluded_extent(struct btrfs_root *root,
209 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400210{
Yan Zheng11833d62009-09-11 16:11:19 -0400211 u64 end = start + num_bytes - 1;
212 set_extent_bits(&root->fs_info->freed_extents[0],
213 start, end, EXTENT_UPTODATE, GFP_NOFS);
214 set_extent_bits(&root->fs_info->freed_extents[1],
215 start, end, EXTENT_UPTODATE, GFP_NOFS);
216 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400217}
218
Yan Zheng11833d62009-09-11 16:11:19 -0400219static void free_excluded_extents(struct btrfs_root *root,
220 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400221{
Yan Zheng11833d62009-09-11 16:11:19 -0400222 u64 start, end;
223
224 start = cache->key.objectid;
225 end = start + cache->key.offset - 1;
226
227 clear_extent_bits(&root->fs_info->freed_extents[0],
228 start, end, EXTENT_UPTODATE, GFP_NOFS);
229 clear_extent_bits(&root->fs_info->freed_extents[1],
230 start, end, EXTENT_UPTODATE, GFP_NOFS);
231}
232
233static int exclude_super_stripes(struct btrfs_root *root,
234 struct btrfs_block_group_cache *cache)
235{
Josef Bacik817d52f2009-07-13 21:29:25 -0400236 u64 bytenr;
237 u64 *logical;
238 int stripe_len;
239 int i, nr, ret;
240
Yan, Zheng06b23312009-11-26 09:31:11 +0000241 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
242 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
243 cache->bytes_super += stripe_len;
244 ret = add_excluded_extent(root, cache->key.objectid,
245 stripe_len);
246 BUG_ON(ret);
247 }
248
Josef Bacik817d52f2009-07-13 21:29:25 -0400249 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
250 bytenr = btrfs_sb_offset(i);
251 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
252 cache->key.objectid, bytenr,
253 0, &logical, &nr, &stripe_len);
254 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400255
Josef Bacik817d52f2009-07-13 21:29:25 -0400256 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400257 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400258 ret = add_excluded_extent(root, logical[nr],
259 stripe_len);
260 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400261 }
Yan Zheng11833d62009-09-11 16:11:19 -0400262
Josef Bacik817d52f2009-07-13 21:29:25 -0400263 kfree(logical);
264 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400265 return 0;
266}
267
Yan Zheng11833d62009-09-11 16:11:19 -0400268static struct btrfs_caching_control *
269get_caching_control(struct btrfs_block_group_cache *cache)
270{
271 struct btrfs_caching_control *ctl;
272
273 spin_lock(&cache->lock);
274 if (cache->cached != BTRFS_CACHE_STARTED) {
275 spin_unlock(&cache->lock);
276 return NULL;
277 }
278
Josef Bacikdde5abe2010-09-16 16:17:03 -0400279 /* We're loading it the fast way, so we don't have a caching_ctl. */
280 if (!cache->caching_ctl) {
281 spin_unlock(&cache->lock);
282 return NULL;
283 }
284
Yan Zheng11833d62009-09-11 16:11:19 -0400285 ctl = cache->caching_ctl;
286 atomic_inc(&ctl->count);
287 spin_unlock(&cache->lock);
288 return ctl;
289}
290
291static void put_caching_control(struct btrfs_caching_control *ctl)
292{
293 if (atomic_dec_and_test(&ctl->count))
294 kfree(ctl);
295}
296
Josef Bacik0f9dd462008-09-23 13:14:11 -0400297/*
298 * this is only called by cache_block_group, since we could have freed extents
299 * we need to check the pinned_extents for any extents that can't be used yet
300 * since their free space will be released as soon as the transaction commits.
301 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400302static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400303 struct btrfs_fs_info *info, u64 start, u64 end)
304{
Josef Bacik817d52f2009-07-13 21:29:25 -0400305 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400306 int ret;
307
308 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400309 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400310 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400311 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400312 if (ret)
313 break;
314
Yan, Zheng06b23312009-11-26 09:31:11 +0000315 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316 start = extent_end + 1;
317 } else if (extent_start > start && extent_start < end) {
318 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400319 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500320 ret = btrfs_add_free_space(block_group, start,
321 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400322 BUG_ON(ret);
323 start = extent_end + 1;
324 } else {
325 break;
326 }
327 }
328
329 if (start < end) {
330 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500332 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400333 BUG_ON(ret);
334 }
335
Josef Bacik817d52f2009-07-13 21:29:25 -0400336 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400337}
338
Josef Bacikbab39bf2011-06-30 14:42:28 -0400339static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400340{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400341 struct btrfs_block_group_cache *block_group;
342 struct btrfs_fs_info *fs_info;
343 struct btrfs_caching_control *caching_ctl;
344 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400345 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400346 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400347 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400348 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400349 u64 last = 0;
350 u32 nritems;
351 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400352
Josef Bacikbab39bf2011-06-30 14:42:28 -0400353 caching_ctl = container_of(work, struct btrfs_caching_control, work);
354 block_group = caching_ctl->block_group;
355 fs_info = block_group->fs_info;
356 extent_root = fs_info->extent_root;
357
Chris Masone37c9e62007-05-09 20:13:14 -0400358 path = btrfs_alloc_path();
359 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400360 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400361
Josef Bacik817d52f2009-07-13 21:29:25 -0400362 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400363
Chris Mason5cd57b22008-06-25 16:01:30 -0400364 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400365 * We don't want to deadlock with somebody trying to allocate a new
366 * extent for the extent root while also trying to search the extent
367 * root to add free space. So we skip locking and search the commit
368 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400369 */
370 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400371 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400372 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400373
Yan Zhenge4404d62008-12-12 10:03:26 -0500374 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400375 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400376 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400377again:
Yan Zheng11833d62009-09-11 16:11:19 -0400378 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400379 /* need to make sure the commit_root doesn't disappear */
380 down_read(&fs_info->extent_commit_sem);
381
Yan Zheng11833d62009-09-11 16:11:19 -0400382 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400383 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400384 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500385
Yan Zheng11833d62009-09-11 16:11:19 -0400386 leaf = path->nodes[0];
387 nritems = btrfs_header_nritems(leaf);
388
Chris Masond3977122009-01-05 21:25:51 -0500389 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200390 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400391 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400392 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400393 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400394
Yan Zheng11833d62009-09-11 16:11:19 -0400395 if (path->slots[0] < nritems) {
396 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
397 } else {
398 ret = find_next_key(path, 0, &key);
399 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400400 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400401
Josef Bacik589d8ad2011-05-11 17:30:53 -0400402 if (need_resched() ||
403 btrfs_next_leaf(extent_root, path)) {
404 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400405 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400406 up_read(&fs_info->extent_commit_sem);
407 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400408 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400409 goto again;
410 }
411 leaf = path->nodes[0];
412 nritems = btrfs_header_nritems(leaf);
413 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400414 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400415
Yan Zheng11833d62009-09-11 16:11:19 -0400416 if (key.objectid < block_group->key.objectid) {
417 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400418 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400419 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400420
Chris Masone37c9e62007-05-09 20:13:14 -0400421 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400422 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400423 break;
Yan7d7d6062007-09-14 16:15:28 -0400424
Yan Zheng11833d62009-09-11 16:11:19 -0400425 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400426 total_found += add_new_free_space(block_group,
427 fs_info, last,
428 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400429 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400430
Yan Zheng11833d62009-09-11 16:11:19 -0400431 if (total_found > (1024 * 1024 * 2)) {
432 total_found = 0;
433 wake_up(&caching_ctl->wait);
434 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400435 }
Chris Masone37c9e62007-05-09 20:13:14 -0400436 path->slots[0]++;
437 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400438 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400439
440 total_found += add_new_free_space(block_group, fs_info, last,
441 block_group->key.objectid +
442 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400443 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400444
445 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400446 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400447 block_group->cached = BTRFS_CACHE_FINISHED;
448 spin_unlock(&block_group->lock);
449
Chris Mason54aa1f42007-06-22 14:16:25 -0400450err:
Chris Masone37c9e62007-05-09 20:13:14 -0400451 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400452 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400453
Yan Zheng11833d62009-09-11 16:11:19 -0400454 free_excluded_extents(extent_root, block_group);
455
456 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400457out:
Yan Zheng11833d62009-09-11 16:11:19 -0400458 wake_up(&caching_ctl->wait);
459
460 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000461 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400462}
463
Josef Bacik9d66e232010-08-25 16:54:15 -0400464static int cache_block_group(struct btrfs_block_group_cache *cache,
465 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500466 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400467 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400468{
Yan Zheng11833d62009-09-11 16:11:19 -0400469 struct btrfs_fs_info *fs_info = cache->fs_info;
470 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400471 int ret = 0;
472
Yan Zheng11833d62009-09-11 16:11:19 -0400473 smp_mb();
474 if (cache->cached != BTRFS_CACHE_NO)
475 return 0;
476
Josef Bacik9d66e232010-08-25 16:54:15 -0400477 /*
478 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500479 * to have the normal tree locking. Also if we are currently trying to
480 * allocate blocks for the tree root we can't do the fast caching since
481 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400482 */
Li Dongyangf7039b12011-03-24 10:24:28 +0000483 if (trans && (!trans->transaction->in_commit) &&
Josef Bacikb8399de2010-12-08 09:15:11 -0500484 (root && root != root->fs_info->tree_root)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400485 spin_lock(&cache->lock);
486 if (cache->cached != BTRFS_CACHE_NO) {
487 spin_unlock(&cache->lock);
488 return 0;
489 }
490 cache->cached = BTRFS_CACHE_STARTED;
491 spin_unlock(&cache->lock);
492
493 ret = load_free_space_cache(fs_info, cache);
494
495 spin_lock(&cache->lock);
496 if (ret == 1) {
497 cache->cached = BTRFS_CACHE_FINISHED;
498 cache->last_byte_to_unpin = (u64)-1;
499 } else {
500 cache->cached = BTRFS_CACHE_NO;
501 }
502 spin_unlock(&cache->lock);
Josef Bacik3c148742011-02-02 15:53:47 +0000503 if (ret == 1) {
504 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400505 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000506 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400507 }
508
509 if (load_cache_only)
510 return 0;
511
Miao Xiefc0e4a32011-03-24 11:41:21 +0000512 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -0400513 BUG_ON(!caching_ctl);
514
515 INIT_LIST_HEAD(&caching_ctl->list);
516 mutex_init(&caching_ctl->mutex);
517 init_waitqueue_head(&caching_ctl->wait);
518 caching_ctl->block_group = cache;
519 caching_ctl->progress = cache->key.objectid;
520 /* one for caching kthread, one for caching block group list */
521 atomic_set(&caching_ctl->count, 2);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400522 caching_ctl->work.func = caching_thread;
Yan Zheng11833d62009-09-11 16:11:19 -0400523
Josef Bacik817d52f2009-07-13 21:29:25 -0400524 spin_lock(&cache->lock);
525 if (cache->cached != BTRFS_CACHE_NO) {
526 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400527 kfree(caching_ctl);
528 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400529 }
Yan Zheng11833d62009-09-11 16:11:19 -0400530 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400531 cache->cached = BTRFS_CACHE_STARTED;
532 spin_unlock(&cache->lock);
533
Yan Zheng11833d62009-09-11 16:11:19 -0400534 down_write(&fs_info->extent_commit_sem);
535 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
536 up_write(&fs_info->extent_commit_sem);
537
Josef Bacik11dfe352009-11-13 20:12:59 +0000538 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400539
Josef Bacikbab39bf2011-06-30 14:42:28 -0400540 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400541
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400542 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400543}
544
Josef Bacik0f9dd462008-09-23 13:14:11 -0400545/*
546 * return the block group that starts at or after bytenr
547 */
Chris Masond3977122009-01-05 21:25:51 -0500548static struct btrfs_block_group_cache *
549btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400550{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400551 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400552
Josef Bacik0f9dd462008-09-23 13:14:11 -0400553 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400554
Josef Bacik0f9dd462008-09-23 13:14:11 -0400555 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400556}
557
Josef Bacik0f9dd462008-09-23 13:14:11 -0400558/*
Sankar P9f556842009-05-14 13:52:22 -0400559 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400560 */
Chris Masond3977122009-01-05 21:25:51 -0500561struct btrfs_block_group_cache *btrfs_lookup_block_group(
562 struct btrfs_fs_info *info,
563 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400564{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400565 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400566
Josef Bacik0f9dd462008-09-23 13:14:11 -0400567 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400568
Josef Bacik0f9dd462008-09-23 13:14:11 -0400569 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400570}
Chris Mason0b86a832008-03-24 15:01:56 -0400571
Josef Bacik0f9dd462008-09-23 13:14:11 -0400572static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
573 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400574{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400575 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400576 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400577
Yan, Zhengb742bb82010-05-16 10:46:24 -0400578 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
579 BTRFS_BLOCK_GROUP_METADATA;
580
Chris Mason4184ea72009-03-10 12:39:20 -0400581 rcu_read_lock();
582 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400583 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400584 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400585 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400586 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400587 }
Chris Mason4184ea72009-03-10 12:39:20 -0400588 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400589 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400590}
591
Chris Mason4184ea72009-03-10 12:39:20 -0400592/*
593 * after adding space to the filesystem, we need to clear the full flags
594 * on all the space infos.
595 */
596void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
597{
598 struct list_head *head = &info->space_info;
599 struct btrfs_space_info *found;
600
601 rcu_read_lock();
602 list_for_each_entry_rcu(found, head, list)
603 found->full = 0;
604 rcu_read_unlock();
605}
606
Josef Bacik80eb2342008-10-29 14:49:05 -0400607static u64 div_factor(u64 num, int factor)
608{
609 if (factor == 10)
610 return num;
611 num *= factor;
612 do_div(num, 10);
613 return num;
614}
615
Chris Masone5bc2452010-10-26 13:37:56 -0400616static u64 div_factor_fine(u64 num, int factor)
617{
618 if (factor == 100)
619 return num;
620 num *= factor;
621 do_div(num, 100);
622 return num;
623}
624
Yan Zhengd2fb3432008-12-11 16:30:39 -0500625u64 btrfs_find_block_group(struct btrfs_root *root,
626 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400627{
Chris Mason96b51792007-10-15 16:15:19 -0400628 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400629 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500630 u64 last = max(search_hint, search_start);
631 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400632 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500633 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400634 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400635again:
Zheng Yane8569812008-09-26 10:05:48 -0400636 while (1) {
637 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400638 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400639 break;
Chris Mason96b51792007-10-15 16:15:19 -0400640
Chris Masonc286ac42008-07-22 23:06:41 -0400641 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400642 last = cache->key.objectid + cache->key.offset;
643 used = btrfs_block_group_used(&cache->item);
644
Yan Zhengd2fb3432008-12-11 16:30:39 -0500645 if ((full_search || !cache->ro) &&
646 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400647 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500648 div_factor(cache->key.offset, factor)) {
649 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400650 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400651 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400652 goto found;
653 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400654 }
Chris Masonc286ac42008-07-22 23:06:41 -0400655 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400656 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400657 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400658 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400659 if (!wrapped) {
660 last = search_start;
661 wrapped = 1;
662 goto again;
663 }
664 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400665 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400666 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400667 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400668 goto again;
669 }
Chris Masonbe744172007-05-06 10:15:01 -0400670found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500671 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400672}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400673
Chris Masone02119d2008-09-05 16:13:11 -0400674/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400675int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400676{
677 int ret;
678 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400679 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400680
Zheng Yan31840ae2008-09-23 13:14:14 -0400681 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700682 if (!path)
683 return -ENOMEM;
684
Chris Masone02119d2008-09-05 16:13:11 -0400685 key.objectid = start;
686 key.offset = len;
687 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
688 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
689 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400690 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500691 return ret;
692}
693
Chris Masond8d5f3e2007-12-11 12:42:00 -0500694/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400695 * helper function to lookup reference count and flags of extent.
696 *
697 * the head node for delayed ref is used to store the sum of all the
698 * reference count modifications queued up in the rbtree. the head
699 * node may also store the extent flags to set. This way you can check
700 * to see what the reference count and extent flags would be if all of
701 * the delayed refs are not processed.
702 */
703int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
704 struct btrfs_root *root, u64 bytenr,
705 u64 num_bytes, u64 *refs, u64 *flags)
706{
707 struct btrfs_delayed_ref_head *head;
708 struct btrfs_delayed_ref_root *delayed_refs;
709 struct btrfs_path *path;
710 struct btrfs_extent_item *ei;
711 struct extent_buffer *leaf;
712 struct btrfs_key key;
713 u32 item_size;
714 u64 num_refs;
715 u64 extent_flags;
716 int ret;
717
718 path = btrfs_alloc_path();
719 if (!path)
720 return -ENOMEM;
721
722 key.objectid = bytenr;
723 key.type = BTRFS_EXTENT_ITEM_KEY;
724 key.offset = num_bytes;
725 if (!trans) {
726 path->skip_locking = 1;
727 path->search_commit_root = 1;
728 }
729again:
730 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
731 &key, path, 0, 0);
732 if (ret < 0)
733 goto out_free;
734
735 if (ret == 0) {
736 leaf = path->nodes[0];
737 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
738 if (item_size >= sizeof(*ei)) {
739 ei = btrfs_item_ptr(leaf, path->slots[0],
740 struct btrfs_extent_item);
741 num_refs = btrfs_extent_refs(leaf, ei);
742 extent_flags = btrfs_extent_flags(leaf, ei);
743 } else {
744#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
745 struct btrfs_extent_item_v0 *ei0;
746 BUG_ON(item_size != sizeof(*ei0));
747 ei0 = btrfs_item_ptr(leaf, path->slots[0],
748 struct btrfs_extent_item_v0);
749 num_refs = btrfs_extent_refs_v0(leaf, ei0);
750 /* FIXME: this isn't correct for data */
751 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
752#else
753 BUG();
754#endif
755 }
756 BUG_ON(num_refs == 0);
757 } else {
758 num_refs = 0;
759 extent_flags = 0;
760 ret = 0;
761 }
762
763 if (!trans)
764 goto out;
765
766 delayed_refs = &trans->transaction->delayed_refs;
767 spin_lock(&delayed_refs->lock);
768 head = btrfs_find_delayed_ref_head(trans, bytenr);
769 if (head) {
770 if (!mutex_trylock(&head->mutex)) {
771 atomic_inc(&head->node.refs);
772 spin_unlock(&delayed_refs->lock);
773
David Sterbab3b4aa72011-04-21 01:20:15 +0200774 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400775
David Sterba8cc33e52011-05-02 15:29:25 +0200776 /*
777 * Mutex was contended, block until it's released and try
778 * again
779 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400780 mutex_lock(&head->mutex);
781 mutex_unlock(&head->mutex);
782 btrfs_put_delayed_ref(&head->node);
783 goto again;
784 }
785 if (head->extent_op && head->extent_op->update_flags)
786 extent_flags |= head->extent_op->flags_to_set;
787 else
788 BUG_ON(num_refs == 0);
789
790 num_refs += head->node.ref_mod;
791 mutex_unlock(&head->mutex);
792 }
793 spin_unlock(&delayed_refs->lock);
794out:
795 WARN_ON(num_refs == 0);
796 if (refs)
797 *refs = num_refs;
798 if (flags)
799 *flags = extent_flags;
800out_free:
801 btrfs_free_path(path);
802 return ret;
803}
804
805/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500806 * Back reference rules. Back refs have three main goals:
807 *
808 * 1) differentiate between all holders of references to an extent so that
809 * when a reference is dropped we can make sure it was a valid reference
810 * before freeing the extent.
811 *
812 * 2) Provide enough information to quickly find the holders of an extent
813 * if we notice a given block is corrupted or bad.
814 *
815 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
816 * maintenance. This is actually the same as #2, but with a slightly
817 * different use case.
818 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400819 * There are two kinds of back refs. The implicit back refs is optimized
820 * for pointers in non-shared tree blocks. For a given pointer in a block,
821 * back refs of this kind provide information about the block's owner tree
822 * and the pointer's key. These information allow us to find the block by
823 * b-tree searching. The full back refs is for pointers in tree blocks not
824 * referenced by their owner trees. The location of tree block is recorded
825 * in the back refs. Actually the full back refs is generic, and can be
826 * used in all cases the implicit back refs is used. The major shortcoming
827 * of the full back refs is its overhead. Every time a tree block gets
828 * COWed, we have to update back refs entry for all pointers in it.
829 *
830 * For a newly allocated tree block, we use implicit back refs for
831 * pointers in it. This means most tree related operations only involve
832 * implicit back refs. For a tree block created in old transaction, the
833 * only way to drop a reference to it is COW it. So we can detect the
834 * event that tree block loses its owner tree's reference and do the
835 * back refs conversion.
836 *
837 * When a tree block is COW'd through a tree, there are four cases:
838 *
839 * The reference count of the block is one and the tree is the block's
840 * owner tree. Nothing to do in this case.
841 *
842 * The reference count of the block is one and the tree is not the
843 * block's owner tree. In this case, full back refs is used for pointers
844 * in the block. Remove these full back refs, add implicit back refs for
845 * every pointers in the new block.
846 *
847 * The reference count of the block is greater than one and the tree is
848 * the block's owner tree. In this case, implicit back refs is used for
849 * pointers in the block. Add full back refs for every pointers in the
850 * block, increase lower level extents' reference counts. The original
851 * implicit back refs are entailed to the new block.
852 *
853 * The reference count of the block is greater than one and the tree is
854 * not the block's owner tree. Add implicit back refs for every pointer in
855 * the new block, increase lower level extents' reference count.
856 *
857 * Back Reference Key composing:
858 *
859 * The key objectid corresponds to the first byte in the extent,
860 * The key type is used to differentiate between types of back refs.
861 * There are different meanings of the key offset for different types
862 * of back refs.
863 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500864 * File extents can be referenced by:
865 *
866 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400867 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500868 * - different offsets inside a file (bookend extents in file.c)
869 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400870 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500871 *
872 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500873 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400874 * - original offset in the file
875 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400876 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400877 * The key offset for the implicit back refs is hash of the first
878 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500879 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400880 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500881 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400882 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500883 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400884 * The key offset for the implicit back refs is the first byte of
885 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500886 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400887 * When a file extent is allocated, The implicit back refs is used.
888 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500889 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400890 * (root_key.objectid, inode objectid, offset in file, 1)
891 *
892 * When a file extent is removed file truncation, we find the
893 * corresponding implicit back refs and check the following fields:
894 *
895 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500896 *
897 * Btree extents can be referenced by:
898 *
899 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500900 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400901 * Both the implicit back refs and the full back refs for tree blocks
902 * only consist of key. The key offset for the implicit back refs is
903 * objectid of block's owner tree. The key offset for the full back refs
904 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500905 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400906 * When implicit back refs is used, information about the lowest key and
907 * level of the tree block are required. These information are stored in
908 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500909 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400910
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
912static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
913 struct btrfs_root *root,
914 struct btrfs_path *path,
915 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500916{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400917 struct btrfs_extent_item *item;
918 struct btrfs_extent_item_v0 *ei0;
919 struct btrfs_extent_ref_v0 *ref0;
920 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400921 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400922 struct btrfs_key key;
923 struct btrfs_key found_key;
924 u32 new_size = sizeof(*item);
925 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500926 int ret;
927
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400928 leaf = path->nodes[0];
929 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500930
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400931 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
932 ei0 = btrfs_item_ptr(leaf, path->slots[0],
933 struct btrfs_extent_item_v0);
934 refs = btrfs_extent_refs_v0(leaf, ei0);
935
936 if (owner == (u64)-1) {
937 while (1) {
938 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
939 ret = btrfs_next_leaf(root, path);
940 if (ret < 0)
941 return ret;
942 BUG_ON(ret > 0);
943 leaf = path->nodes[0];
944 }
945 btrfs_item_key_to_cpu(leaf, &found_key,
946 path->slots[0]);
947 BUG_ON(key.objectid != found_key.objectid);
948 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
949 path->slots[0]++;
950 continue;
951 }
952 ref0 = btrfs_item_ptr(leaf, path->slots[0],
953 struct btrfs_extent_ref_v0);
954 owner = btrfs_ref_objectid_v0(leaf, ref0);
955 break;
956 }
957 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200958 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400959
960 if (owner < BTRFS_FIRST_FREE_OBJECTID)
961 new_size += sizeof(*bi);
962
963 new_size -= sizeof(*ei0);
964 ret = btrfs_search_slot(trans, root, &key, path,
965 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400966 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400967 return ret;
968 BUG_ON(ret);
969
970 ret = btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400971
972 leaf = path->nodes[0];
973 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
974 btrfs_set_extent_refs(leaf, item, refs);
975 /* FIXME: get real generation */
976 btrfs_set_extent_generation(leaf, item, 0);
977 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
978 btrfs_set_extent_flags(leaf, item,
979 BTRFS_EXTENT_FLAG_TREE_BLOCK |
980 BTRFS_BLOCK_FLAG_FULL_BACKREF);
981 bi = (struct btrfs_tree_block_info *)(item + 1);
982 /* FIXME: get first key of the block */
983 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
984 btrfs_set_tree_block_level(leaf, bi, (int)owner);
985 } else {
986 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
987 }
988 btrfs_mark_buffer_dirty(leaf);
989 return 0;
990}
991#endif
992
993static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
994{
995 u32 high_crc = ~(u32)0;
996 u32 low_crc = ~(u32)0;
997 __le64 lenum;
998
999 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001000 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001001 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001002 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001003 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001004 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001005
1006 return ((u64)high_crc << 31) ^ (u64)low_crc;
1007}
1008
1009static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1010 struct btrfs_extent_data_ref *ref)
1011{
1012 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1013 btrfs_extent_data_ref_objectid(leaf, ref),
1014 btrfs_extent_data_ref_offset(leaf, ref));
1015}
1016
1017static int match_extent_data_ref(struct extent_buffer *leaf,
1018 struct btrfs_extent_data_ref *ref,
1019 u64 root_objectid, u64 owner, u64 offset)
1020{
1021 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1022 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1023 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1024 return 0;
1025 return 1;
1026}
1027
1028static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1029 struct btrfs_root *root,
1030 struct btrfs_path *path,
1031 u64 bytenr, u64 parent,
1032 u64 root_objectid,
1033 u64 owner, u64 offset)
1034{
1035 struct btrfs_key key;
1036 struct btrfs_extent_data_ref *ref;
1037 struct extent_buffer *leaf;
1038 u32 nritems;
1039 int ret;
1040 int recow;
1041 int err = -ENOENT;
1042
1043 key.objectid = bytenr;
1044 if (parent) {
1045 key.type = BTRFS_SHARED_DATA_REF_KEY;
1046 key.offset = parent;
1047 } else {
1048 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1049 key.offset = hash_extent_data_ref(root_objectid,
1050 owner, offset);
1051 }
1052again:
1053 recow = 0;
1054 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1055 if (ret < 0) {
1056 err = ret;
1057 goto fail;
1058 }
1059
1060 if (parent) {
1061 if (!ret)
1062 return 0;
1063#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1064 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001065 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001066 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1067 if (ret < 0) {
1068 err = ret;
1069 goto fail;
1070 }
1071 if (!ret)
1072 return 0;
1073#endif
1074 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001075 }
1076
1077 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001078 nritems = btrfs_header_nritems(leaf);
1079 while (1) {
1080 if (path->slots[0] >= nritems) {
1081 ret = btrfs_next_leaf(root, path);
1082 if (ret < 0)
1083 err = ret;
1084 if (ret)
1085 goto fail;
1086
1087 leaf = path->nodes[0];
1088 nritems = btrfs_header_nritems(leaf);
1089 recow = 1;
1090 }
1091
1092 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1093 if (key.objectid != bytenr ||
1094 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1095 goto fail;
1096
1097 ref = btrfs_item_ptr(leaf, path->slots[0],
1098 struct btrfs_extent_data_ref);
1099
1100 if (match_extent_data_ref(leaf, ref, root_objectid,
1101 owner, offset)) {
1102 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001103 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001104 goto again;
1105 }
1106 err = 0;
1107 break;
1108 }
1109 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001110 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001111fail:
1112 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001113}
1114
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001115static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1116 struct btrfs_root *root,
1117 struct btrfs_path *path,
1118 u64 bytenr, u64 parent,
1119 u64 root_objectid, u64 owner,
1120 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001121{
1122 struct btrfs_key key;
1123 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001124 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001125 u32 num_refs;
1126 int ret;
1127
1128 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001129 if (parent) {
1130 key.type = BTRFS_SHARED_DATA_REF_KEY;
1131 key.offset = parent;
1132 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001133 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001134 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1135 key.offset = hash_extent_data_ref(root_objectid,
1136 owner, offset);
1137 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001138 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001139
1140 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1141 if (ret && ret != -EEXIST)
1142 goto fail;
1143
1144 leaf = path->nodes[0];
1145 if (parent) {
1146 struct btrfs_shared_data_ref *ref;
1147 ref = btrfs_item_ptr(leaf, path->slots[0],
1148 struct btrfs_shared_data_ref);
1149 if (ret == 0) {
1150 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1151 } else {
1152 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1153 num_refs += refs_to_add;
1154 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1155 }
1156 } else {
1157 struct btrfs_extent_data_ref *ref;
1158 while (ret == -EEXIST) {
1159 ref = btrfs_item_ptr(leaf, path->slots[0],
1160 struct btrfs_extent_data_ref);
1161 if (match_extent_data_ref(leaf, ref, root_objectid,
1162 owner, offset))
1163 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001164 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001165 key.offset++;
1166 ret = btrfs_insert_empty_item(trans, root, path, &key,
1167 size);
1168 if (ret && ret != -EEXIST)
1169 goto fail;
1170
1171 leaf = path->nodes[0];
1172 }
1173 ref = btrfs_item_ptr(leaf, path->slots[0],
1174 struct btrfs_extent_data_ref);
1175 if (ret == 0) {
1176 btrfs_set_extent_data_ref_root(leaf, ref,
1177 root_objectid);
1178 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1179 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1180 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1181 } else {
1182 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1183 num_refs += refs_to_add;
1184 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1185 }
1186 }
1187 btrfs_mark_buffer_dirty(leaf);
1188 ret = 0;
1189fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001190 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001191 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001192}
1193
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001194static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1195 struct btrfs_root *root,
1196 struct btrfs_path *path,
1197 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001198{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001199 struct btrfs_key key;
1200 struct btrfs_extent_data_ref *ref1 = NULL;
1201 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001202 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001203 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001204 int ret = 0;
1205
1206 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001207 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1208
1209 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1210 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1211 struct btrfs_extent_data_ref);
1212 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1213 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1214 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1215 struct btrfs_shared_data_ref);
1216 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1217#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1218 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1219 struct btrfs_extent_ref_v0 *ref0;
1220 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1221 struct btrfs_extent_ref_v0);
1222 num_refs = btrfs_ref_count_v0(leaf, ref0);
1223#endif
1224 } else {
1225 BUG();
1226 }
1227
Chris Mason56bec292009-03-13 10:10:06 -04001228 BUG_ON(num_refs < refs_to_drop);
1229 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001230
Zheng Yan31840ae2008-09-23 13:14:14 -04001231 if (num_refs == 0) {
1232 ret = btrfs_del_item(trans, root, path);
1233 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001234 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1235 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1236 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1237 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1238#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1239 else {
1240 struct btrfs_extent_ref_v0 *ref0;
1241 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1242 struct btrfs_extent_ref_v0);
1243 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1244 }
1245#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001246 btrfs_mark_buffer_dirty(leaf);
1247 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001248 return ret;
1249}
1250
1251static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1252 struct btrfs_path *path,
1253 struct btrfs_extent_inline_ref *iref)
1254{
1255 struct btrfs_key key;
1256 struct extent_buffer *leaf;
1257 struct btrfs_extent_data_ref *ref1;
1258 struct btrfs_shared_data_ref *ref2;
1259 u32 num_refs = 0;
1260
1261 leaf = path->nodes[0];
1262 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1263 if (iref) {
1264 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1265 BTRFS_EXTENT_DATA_REF_KEY) {
1266 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1267 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1268 } else {
1269 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1270 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1271 }
1272 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1273 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1274 struct btrfs_extent_data_ref);
1275 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1276 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1277 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1278 struct btrfs_shared_data_ref);
1279 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1280#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1281 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1282 struct btrfs_extent_ref_v0 *ref0;
1283 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1284 struct btrfs_extent_ref_v0);
1285 num_refs = btrfs_ref_count_v0(leaf, ref0);
1286#endif
1287 } else {
1288 WARN_ON(1);
1289 }
1290 return num_refs;
1291}
1292
1293static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1294 struct btrfs_root *root,
1295 struct btrfs_path *path,
1296 u64 bytenr, u64 parent,
1297 u64 root_objectid)
1298{
1299 struct btrfs_key key;
1300 int ret;
1301
1302 key.objectid = bytenr;
1303 if (parent) {
1304 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1305 key.offset = parent;
1306 } else {
1307 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1308 key.offset = root_objectid;
1309 }
1310
1311 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1312 if (ret > 0)
1313 ret = -ENOENT;
1314#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1315 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001316 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001317 key.type = BTRFS_EXTENT_REF_V0_KEY;
1318 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1319 if (ret > 0)
1320 ret = -ENOENT;
1321 }
1322#endif
1323 return ret;
1324}
1325
1326static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1327 struct btrfs_root *root,
1328 struct btrfs_path *path,
1329 u64 bytenr, u64 parent,
1330 u64 root_objectid)
1331{
1332 struct btrfs_key key;
1333 int ret;
1334
1335 key.objectid = bytenr;
1336 if (parent) {
1337 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1338 key.offset = parent;
1339 } else {
1340 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1341 key.offset = root_objectid;
1342 }
1343
1344 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001345 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001346 return ret;
1347}
1348
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001349static inline int extent_ref_type(u64 parent, u64 owner)
1350{
1351 int type;
1352 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1353 if (parent > 0)
1354 type = BTRFS_SHARED_BLOCK_REF_KEY;
1355 else
1356 type = BTRFS_TREE_BLOCK_REF_KEY;
1357 } else {
1358 if (parent > 0)
1359 type = BTRFS_SHARED_DATA_REF_KEY;
1360 else
1361 type = BTRFS_EXTENT_DATA_REF_KEY;
1362 }
1363 return type;
1364}
1365
Yan Zheng2c47e6052009-06-27 21:07:35 -04001366static int find_next_key(struct btrfs_path *path, int level,
1367 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001368
1369{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001370 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001371 if (!path->nodes[level])
1372 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001373 if (path->slots[level] + 1 >=
1374 btrfs_header_nritems(path->nodes[level]))
1375 continue;
1376 if (level == 0)
1377 btrfs_item_key_to_cpu(path->nodes[level], key,
1378 path->slots[level] + 1);
1379 else
1380 btrfs_node_key_to_cpu(path->nodes[level], key,
1381 path->slots[level] + 1);
1382 return 0;
1383 }
1384 return 1;
1385}
1386
1387/*
1388 * look for inline back ref. if back ref is found, *ref_ret is set
1389 * to the address of inline back ref, and 0 is returned.
1390 *
1391 * if back ref isn't found, *ref_ret is set to the address where it
1392 * should be inserted, and -ENOENT is returned.
1393 *
1394 * if insert is true and there are too many inline back refs, the path
1395 * points to the extent item, and -EAGAIN is returned.
1396 *
1397 * NOTE: inline back refs are ordered in the same way that back ref
1398 * items in the tree are ordered.
1399 */
1400static noinline_for_stack
1401int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1402 struct btrfs_root *root,
1403 struct btrfs_path *path,
1404 struct btrfs_extent_inline_ref **ref_ret,
1405 u64 bytenr, u64 num_bytes,
1406 u64 parent, u64 root_objectid,
1407 u64 owner, u64 offset, int insert)
1408{
1409 struct btrfs_key key;
1410 struct extent_buffer *leaf;
1411 struct btrfs_extent_item *ei;
1412 struct btrfs_extent_inline_ref *iref;
1413 u64 flags;
1414 u64 item_size;
1415 unsigned long ptr;
1416 unsigned long end;
1417 int extra_size;
1418 int type;
1419 int want;
1420 int ret;
1421 int err = 0;
1422
1423 key.objectid = bytenr;
1424 key.type = BTRFS_EXTENT_ITEM_KEY;
1425 key.offset = num_bytes;
1426
1427 want = extent_ref_type(parent, owner);
1428 if (insert) {
1429 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001430 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001431 } else
1432 extra_size = -1;
1433 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1434 if (ret < 0) {
1435 err = ret;
1436 goto out;
1437 }
1438 BUG_ON(ret);
1439
1440 leaf = path->nodes[0];
1441 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1442#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1443 if (item_size < sizeof(*ei)) {
1444 if (!insert) {
1445 err = -ENOENT;
1446 goto out;
1447 }
1448 ret = convert_extent_item_v0(trans, root, path, owner,
1449 extra_size);
1450 if (ret < 0) {
1451 err = ret;
1452 goto out;
1453 }
1454 leaf = path->nodes[0];
1455 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1456 }
1457#endif
1458 BUG_ON(item_size < sizeof(*ei));
1459
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001460 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1461 flags = btrfs_extent_flags(leaf, ei);
1462
1463 ptr = (unsigned long)(ei + 1);
1464 end = (unsigned long)ei + item_size;
1465
1466 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1467 ptr += sizeof(struct btrfs_tree_block_info);
1468 BUG_ON(ptr > end);
1469 } else {
1470 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1471 }
1472
1473 err = -ENOENT;
1474 while (1) {
1475 if (ptr >= end) {
1476 WARN_ON(ptr > end);
1477 break;
1478 }
1479 iref = (struct btrfs_extent_inline_ref *)ptr;
1480 type = btrfs_extent_inline_ref_type(leaf, iref);
1481 if (want < type)
1482 break;
1483 if (want > type) {
1484 ptr += btrfs_extent_inline_ref_size(type);
1485 continue;
1486 }
1487
1488 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1489 struct btrfs_extent_data_ref *dref;
1490 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1491 if (match_extent_data_ref(leaf, dref, root_objectid,
1492 owner, offset)) {
1493 err = 0;
1494 break;
1495 }
1496 if (hash_extent_data_ref_item(leaf, dref) <
1497 hash_extent_data_ref(root_objectid, owner, offset))
1498 break;
1499 } else {
1500 u64 ref_offset;
1501 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1502 if (parent > 0) {
1503 if (parent == ref_offset) {
1504 err = 0;
1505 break;
1506 }
1507 if (ref_offset < parent)
1508 break;
1509 } else {
1510 if (root_objectid == ref_offset) {
1511 err = 0;
1512 break;
1513 }
1514 if (ref_offset < root_objectid)
1515 break;
1516 }
1517 }
1518 ptr += btrfs_extent_inline_ref_size(type);
1519 }
1520 if (err == -ENOENT && insert) {
1521 if (item_size + extra_size >=
1522 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1523 err = -EAGAIN;
1524 goto out;
1525 }
1526 /*
1527 * To add new inline back ref, we have to make sure
1528 * there is no corresponding back ref item.
1529 * For simplicity, we just do not add new inline back
1530 * ref if there is any kind of item for this block
1531 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001532 if (find_next_key(path, 0, &key) == 0 &&
1533 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001534 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001535 err = -EAGAIN;
1536 goto out;
1537 }
1538 }
1539 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1540out:
Yan Zheng85d41982009-06-11 08:51:10 -04001541 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001542 path->keep_locks = 0;
1543 btrfs_unlock_up_safe(path, 1);
1544 }
1545 return err;
1546}
1547
1548/*
1549 * helper to add new inline back ref
1550 */
1551static noinline_for_stack
1552int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1553 struct btrfs_root *root,
1554 struct btrfs_path *path,
1555 struct btrfs_extent_inline_ref *iref,
1556 u64 parent, u64 root_objectid,
1557 u64 owner, u64 offset, int refs_to_add,
1558 struct btrfs_delayed_extent_op *extent_op)
1559{
1560 struct extent_buffer *leaf;
1561 struct btrfs_extent_item *ei;
1562 unsigned long ptr;
1563 unsigned long end;
1564 unsigned long item_offset;
1565 u64 refs;
1566 int size;
1567 int type;
1568 int ret;
1569
1570 leaf = path->nodes[0];
1571 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1572 item_offset = (unsigned long)iref - (unsigned long)ei;
1573
1574 type = extent_ref_type(parent, owner);
1575 size = btrfs_extent_inline_ref_size(type);
1576
1577 ret = btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001578
1579 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1580 refs = btrfs_extent_refs(leaf, ei);
1581 refs += refs_to_add;
1582 btrfs_set_extent_refs(leaf, ei, refs);
1583 if (extent_op)
1584 __run_delayed_extent_op(extent_op, leaf, ei);
1585
1586 ptr = (unsigned long)ei + item_offset;
1587 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1588 if (ptr < end - size)
1589 memmove_extent_buffer(leaf, ptr + size, ptr,
1590 end - size - ptr);
1591
1592 iref = (struct btrfs_extent_inline_ref *)ptr;
1593 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1594 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1595 struct btrfs_extent_data_ref *dref;
1596 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1597 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1598 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1599 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1600 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1601 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1602 struct btrfs_shared_data_ref *sref;
1603 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1604 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1605 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1606 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1607 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1608 } else {
1609 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1610 }
1611 btrfs_mark_buffer_dirty(leaf);
1612 return 0;
1613}
1614
1615static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1616 struct btrfs_root *root,
1617 struct btrfs_path *path,
1618 struct btrfs_extent_inline_ref **ref_ret,
1619 u64 bytenr, u64 num_bytes, u64 parent,
1620 u64 root_objectid, u64 owner, u64 offset)
1621{
1622 int ret;
1623
1624 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1625 bytenr, num_bytes, parent,
1626 root_objectid, owner, offset, 0);
1627 if (ret != -ENOENT)
1628 return ret;
1629
David Sterbab3b4aa72011-04-21 01:20:15 +02001630 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001631 *ref_ret = NULL;
1632
1633 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1634 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1635 root_objectid);
1636 } else {
1637 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1638 root_objectid, owner, offset);
1639 }
1640 return ret;
1641}
1642
1643/*
1644 * helper to update/remove inline back ref
1645 */
1646static noinline_for_stack
1647int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1648 struct btrfs_root *root,
1649 struct btrfs_path *path,
1650 struct btrfs_extent_inline_ref *iref,
1651 int refs_to_mod,
1652 struct btrfs_delayed_extent_op *extent_op)
1653{
1654 struct extent_buffer *leaf;
1655 struct btrfs_extent_item *ei;
1656 struct btrfs_extent_data_ref *dref = NULL;
1657 struct btrfs_shared_data_ref *sref = NULL;
1658 unsigned long ptr;
1659 unsigned long end;
1660 u32 item_size;
1661 int size;
1662 int type;
1663 int ret;
1664 u64 refs;
1665
1666 leaf = path->nodes[0];
1667 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1668 refs = btrfs_extent_refs(leaf, ei);
1669 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1670 refs += refs_to_mod;
1671 btrfs_set_extent_refs(leaf, ei, refs);
1672 if (extent_op)
1673 __run_delayed_extent_op(extent_op, leaf, ei);
1674
1675 type = btrfs_extent_inline_ref_type(leaf, iref);
1676
1677 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1678 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1679 refs = btrfs_extent_data_ref_count(leaf, dref);
1680 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1681 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1682 refs = btrfs_shared_data_ref_count(leaf, sref);
1683 } else {
1684 refs = 1;
1685 BUG_ON(refs_to_mod != -1);
1686 }
1687
1688 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1689 refs += refs_to_mod;
1690
1691 if (refs > 0) {
1692 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1693 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1694 else
1695 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1696 } else {
1697 size = btrfs_extent_inline_ref_size(type);
1698 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1699 ptr = (unsigned long)iref;
1700 end = (unsigned long)ei + item_size;
1701 if (ptr + size < end)
1702 memmove_extent_buffer(leaf, ptr, ptr + size,
1703 end - ptr - size);
1704 item_size -= size;
1705 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001706 }
1707 btrfs_mark_buffer_dirty(leaf);
1708 return 0;
1709}
1710
1711static noinline_for_stack
1712int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1713 struct btrfs_root *root,
1714 struct btrfs_path *path,
1715 u64 bytenr, u64 num_bytes, u64 parent,
1716 u64 root_objectid, u64 owner,
1717 u64 offset, int refs_to_add,
1718 struct btrfs_delayed_extent_op *extent_op)
1719{
1720 struct btrfs_extent_inline_ref *iref;
1721 int ret;
1722
1723 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1724 bytenr, num_bytes, parent,
1725 root_objectid, owner, offset, 1);
1726 if (ret == 0) {
1727 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1728 ret = update_inline_extent_backref(trans, root, path, iref,
1729 refs_to_add, extent_op);
1730 } else if (ret == -ENOENT) {
1731 ret = setup_inline_extent_backref(trans, root, path, iref,
1732 parent, root_objectid,
1733 owner, offset, refs_to_add,
1734 extent_op);
1735 }
1736 return ret;
1737}
1738
1739static int insert_extent_backref(struct btrfs_trans_handle *trans,
1740 struct btrfs_root *root,
1741 struct btrfs_path *path,
1742 u64 bytenr, u64 parent, u64 root_objectid,
1743 u64 owner, u64 offset, int refs_to_add)
1744{
1745 int ret;
1746 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1747 BUG_ON(refs_to_add != 1);
1748 ret = insert_tree_block_ref(trans, root, path, bytenr,
1749 parent, root_objectid);
1750 } else {
1751 ret = insert_extent_data_ref(trans, root, path, bytenr,
1752 parent, root_objectid,
1753 owner, offset, refs_to_add);
1754 }
1755 return ret;
1756}
1757
1758static int remove_extent_backref(struct btrfs_trans_handle *trans,
1759 struct btrfs_root *root,
1760 struct btrfs_path *path,
1761 struct btrfs_extent_inline_ref *iref,
1762 int refs_to_drop, int is_data)
1763{
1764 int ret;
1765
1766 BUG_ON(!is_data && refs_to_drop != 1);
1767 if (iref) {
1768 ret = update_inline_extent_backref(trans, root, path, iref,
1769 -refs_to_drop, NULL);
1770 } else if (is_data) {
1771 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1772 } else {
1773 ret = btrfs_del_item(trans, root, path);
1774 }
1775 return ret;
1776}
1777
Li Dongyang5378e602011-03-24 10:24:27 +00001778static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001779 u64 start, u64 len)
1780{
Li Dongyang5378e602011-03-24 10:24:27 +00001781 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001782}
Chris Mason15916de2008-11-19 21:17:22 -05001783
Liu Hui1f3c79a2009-01-05 15:57:51 -05001784static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001785 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001786{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001787 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001788 u64 discarded_bytes = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001789 struct btrfs_multi_bio *multi = NULL;
1790
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001791
Liu Hui1f3c79a2009-01-05 15:57:51 -05001792 /* Tell the block device(s) that the sectors can be discarded */
Li Dongyang5378e602011-03-24 10:24:27 +00001793 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1794 bytenr, &num_bytes, &multi, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001795 if (!ret) {
1796 struct btrfs_bio_stripe *stripe = multi->stripes;
1797 int i;
1798
Liu Hui1f3c79a2009-01-05 15:57:51 -05001799
1800 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001801 if (!stripe->dev->can_discard)
1802 continue;
1803
Li Dongyang5378e602011-03-24 10:24:27 +00001804 ret = btrfs_issue_discard(stripe->dev->bdev,
1805 stripe->physical,
1806 stripe->length);
1807 if (!ret)
1808 discarded_bytes += stripe->length;
1809 else if (ret != -EOPNOTSUPP)
1810 break;
Josef Bacikd5e20032011-08-04 14:52:27 +00001811
1812 /*
1813 * Just in case we get back EOPNOTSUPP for some reason,
1814 * just ignore the return value so we don't screw up
1815 * people calling discard_extent.
1816 */
1817 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001818 }
1819 kfree(multi);
1820 }
Li Dongyang5378e602011-03-24 10:24:27 +00001821
1822 if (actual_bytes)
1823 *actual_bytes = discarded_bytes;
1824
Liu Hui1f3c79a2009-01-05 15:57:51 -05001825
1826 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001827}
1828
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001829int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1830 struct btrfs_root *root,
1831 u64 bytenr, u64 num_bytes, u64 parent,
1832 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001833{
1834 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001835 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1836 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001837
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001838 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1839 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1840 parent, root_objectid, (int)owner,
1841 BTRFS_ADD_DELAYED_REF, NULL);
1842 } else {
1843 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1844 parent, root_objectid, owner, offset,
1845 BTRFS_ADD_DELAYED_REF, NULL);
1846 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001847 return ret;
1848}
1849
Chris Mason925baed2008-06-25 16:01:30 -04001850static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001851 struct btrfs_root *root,
1852 u64 bytenr, u64 num_bytes,
1853 u64 parent, u64 root_objectid,
1854 u64 owner, u64 offset, int refs_to_add,
1855 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001856{
Chris Mason5caf2a02007-04-02 11:20:42 -04001857 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001858 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001859 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001860 u64 refs;
1861 int ret;
1862 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001863
Chris Mason5caf2a02007-04-02 11:20:42 -04001864 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001865 if (!path)
1866 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001867
Chris Mason3c12ac72008-04-21 12:01:38 -04001868 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001869 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 /* this will setup the path even if it fails to insert the back ref */
1871 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1872 path, bytenr, num_bytes, parent,
1873 root_objectid, owner, offset,
1874 refs_to_add, extent_op);
1875 if (ret == 0)
1876 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001877
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 if (ret != -EAGAIN) {
1879 err = ret;
1880 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001881 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001882
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001883 leaf = path->nodes[0];
1884 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1885 refs = btrfs_extent_refs(leaf, item);
1886 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1887 if (extent_op)
1888 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001889
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001890 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001891 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001892
Chris Mason3c12ac72008-04-21 12:01:38 -04001893 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001894 path->leave_spinning = 1;
1895
Chris Mason56bec292009-03-13 10:10:06 -04001896 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001897 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001898 path, bytenr, parent, root_objectid,
1899 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001900 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001901out:
Chris Mason74493f72007-12-11 09:25:06 -05001902 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001903 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001904}
1905
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001906static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1907 struct btrfs_root *root,
1908 struct btrfs_delayed_ref_node *node,
1909 struct btrfs_delayed_extent_op *extent_op,
1910 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001911{
Chris Mason56bec292009-03-13 10:10:06 -04001912 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001913 struct btrfs_delayed_data_ref *ref;
1914 struct btrfs_key ins;
1915 u64 parent = 0;
1916 u64 ref_root = 0;
1917 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001918
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001919 ins.objectid = node->bytenr;
1920 ins.offset = node->num_bytes;
1921 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001922
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001923 ref = btrfs_delayed_node_to_data_ref(node);
1924 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1925 parent = ref->parent;
1926 else
1927 ref_root = ref->root;
1928
1929 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1930 if (extent_op) {
1931 BUG_ON(extent_op->update_key);
1932 flags |= extent_op->flags_to_set;
1933 }
1934 ret = alloc_reserved_file_extent(trans, root,
1935 parent, ref_root, flags,
1936 ref->objectid, ref->offset,
1937 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001938 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1939 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1940 node->num_bytes, parent,
1941 ref_root, ref->objectid,
1942 ref->offset, node->ref_mod,
1943 extent_op);
1944 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1945 ret = __btrfs_free_extent(trans, root, node->bytenr,
1946 node->num_bytes, parent,
1947 ref_root, ref->objectid,
1948 ref->offset, node->ref_mod,
1949 extent_op);
1950 } else {
1951 BUG();
1952 }
Chris Mason56bec292009-03-13 10:10:06 -04001953 return ret;
1954}
1955
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001956static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1957 struct extent_buffer *leaf,
1958 struct btrfs_extent_item *ei)
1959{
1960 u64 flags = btrfs_extent_flags(leaf, ei);
1961 if (extent_op->update_flags) {
1962 flags |= extent_op->flags_to_set;
1963 btrfs_set_extent_flags(leaf, ei, flags);
1964 }
1965
1966 if (extent_op->update_key) {
1967 struct btrfs_tree_block_info *bi;
1968 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1969 bi = (struct btrfs_tree_block_info *)(ei + 1);
1970 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1971 }
1972}
1973
1974static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1975 struct btrfs_root *root,
1976 struct btrfs_delayed_ref_node *node,
1977 struct btrfs_delayed_extent_op *extent_op)
1978{
1979 struct btrfs_key key;
1980 struct btrfs_path *path;
1981 struct btrfs_extent_item *ei;
1982 struct extent_buffer *leaf;
1983 u32 item_size;
1984 int ret;
1985 int err = 0;
1986
1987 path = btrfs_alloc_path();
1988 if (!path)
1989 return -ENOMEM;
1990
1991 key.objectid = node->bytenr;
1992 key.type = BTRFS_EXTENT_ITEM_KEY;
1993 key.offset = node->num_bytes;
1994
1995 path->reada = 1;
1996 path->leave_spinning = 1;
1997 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1998 path, 0, 1);
1999 if (ret < 0) {
2000 err = ret;
2001 goto out;
2002 }
2003 if (ret > 0) {
2004 err = -EIO;
2005 goto out;
2006 }
2007
2008 leaf = path->nodes[0];
2009 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2010#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2011 if (item_size < sizeof(*ei)) {
2012 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2013 path, (u64)-1, 0);
2014 if (ret < 0) {
2015 err = ret;
2016 goto out;
2017 }
2018 leaf = path->nodes[0];
2019 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2020 }
2021#endif
2022 BUG_ON(item_size < sizeof(*ei));
2023 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2024 __run_delayed_extent_op(extent_op, leaf, ei);
2025
2026 btrfs_mark_buffer_dirty(leaf);
2027out:
2028 btrfs_free_path(path);
2029 return err;
2030}
2031
2032static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2033 struct btrfs_root *root,
2034 struct btrfs_delayed_ref_node *node,
2035 struct btrfs_delayed_extent_op *extent_op,
2036 int insert_reserved)
2037{
2038 int ret = 0;
2039 struct btrfs_delayed_tree_ref *ref;
2040 struct btrfs_key ins;
2041 u64 parent = 0;
2042 u64 ref_root = 0;
2043
2044 ins.objectid = node->bytenr;
2045 ins.offset = node->num_bytes;
2046 ins.type = BTRFS_EXTENT_ITEM_KEY;
2047
2048 ref = btrfs_delayed_node_to_tree_ref(node);
2049 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2050 parent = ref->parent;
2051 else
2052 ref_root = ref->root;
2053
2054 BUG_ON(node->ref_mod != 1);
2055 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2056 BUG_ON(!extent_op || !extent_op->update_flags ||
2057 !extent_op->update_key);
2058 ret = alloc_reserved_tree_block(trans, root,
2059 parent, ref_root,
2060 extent_op->flags_to_set,
2061 &extent_op->key,
2062 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002063 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2064 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2065 node->num_bytes, parent, ref_root,
2066 ref->level, 0, 1, extent_op);
2067 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2068 ret = __btrfs_free_extent(trans, root, node->bytenr,
2069 node->num_bytes, parent, ref_root,
2070 ref->level, 0, 1, extent_op);
2071 } else {
2072 BUG();
2073 }
2074 return ret;
2075}
2076
Chris Mason56bec292009-03-13 10:10:06 -04002077/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002078static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2079 struct btrfs_root *root,
2080 struct btrfs_delayed_ref_node *node,
2081 struct btrfs_delayed_extent_op *extent_op,
2082 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002083{
Josef Bacikeb099672009-02-12 09:27:38 -05002084 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002085 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002086 struct btrfs_delayed_ref_head *head;
2087 /*
2088 * we've hit the end of the chain and we were supposed
2089 * to insert this extent into the tree. But, it got
2090 * deleted before we ever needed to insert it, so all
2091 * we have to do is clean up the accounting
2092 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002093 BUG_ON(extent_op);
2094 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002095 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002096 btrfs_pin_extent(root, node->bytenr,
2097 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002098 if (head->is_data) {
2099 ret = btrfs_del_csums(trans, root,
2100 node->bytenr,
2101 node->num_bytes);
2102 BUG_ON(ret);
2103 }
Chris Mason56bec292009-03-13 10:10:06 -04002104 }
Chris Mason56bec292009-03-13 10:10:06 -04002105 mutex_unlock(&head->mutex);
2106 return 0;
2107 }
Josef Bacikeb099672009-02-12 09:27:38 -05002108
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002109 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2110 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2111 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2112 insert_reserved);
2113 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2114 node->type == BTRFS_SHARED_DATA_REF_KEY)
2115 ret = run_delayed_data_ref(trans, root, node, extent_op,
2116 insert_reserved);
2117 else
2118 BUG();
2119 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002120}
2121
Chris Mason56bec292009-03-13 10:10:06 -04002122static noinline struct btrfs_delayed_ref_node *
2123select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002124{
Chris Mason56bec292009-03-13 10:10:06 -04002125 struct rb_node *node;
2126 struct btrfs_delayed_ref_node *ref;
2127 int action = BTRFS_ADD_DELAYED_REF;
2128again:
2129 /*
2130 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2131 * this prevents ref count from going down to zero when
2132 * there still are pending delayed ref.
2133 */
2134 node = rb_prev(&head->node.rb_node);
2135 while (1) {
2136 if (!node)
2137 break;
2138 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2139 rb_node);
2140 if (ref->bytenr != head->node.bytenr)
2141 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002142 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002143 return ref;
2144 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002145 }
Chris Mason56bec292009-03-13 10:10:06 -04002146 if (action == BTRFS_ADD_DELAYED_REF) {
2147 action = BTRFS_DROP_DELAYED_REF;
2148 goto again;
2149 }
2150 return NULL;
2151}
2152
Chris Masonc3e69d52009-03-13 10:17:05 -04002153static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2154 struct btrfs_root *root,
2155 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002156{
Chris Mason56bec292009-03-13 10:10:06 -04002157 struct btrfs_delayed_ref_root *delayed_refs;
2158 struct btrfs_delayed_ref_node *ref;
2159 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002160 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002161 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002162 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002163 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002164
2165 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002166 while (1) {
2167 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002168 /* pick a new head ref from the cluster list */
2169 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002170 break;
Chris Mason56bec292009-03-13 10:10:06 -04002171
Chris Masonc3e69d52009-03-13 10:17:05 -04002172 locked_ref = list_entry(cluster->next,
2173 struct btrfs_delayed_ref_head, cluster);
2174
2175 /* grab the lock that says we are going to process
2176 * all the refs for this head */
2177 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2178
2179 /*
2180 * we may have dropped the spin lock to get the head
2181 * mutex lock, and that might have given someone else
2182 * time to free the head. If that's true, it has been
2183 * removed from our list and we can move on.
2184 */
2185 if (ret == -EAGAIN) {
2186 locked_ref = NULL;
2187 count++;
2188 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002189 }
2190 }
2191
2192 /*
2193 * record the must insert reserved flag before we
2194 * drop the spin lock.
2195 */
2196 must_insert_reserved = locked_ref->must_insert_reserved;
2197 locked_ref->must_insert_reserved = 0;
2198
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002199 extent_op = locked_ref->extent_op;
2200 locked_ref->extent_op = NULL;
2201
Chris Mason56bec292009-03-13 10:10:06 -04002202 /*
2203 * locked_ref is the head node, so we have to go one
2204 * node back for any delayed ref updates
2205 */
Chris Mason56bec292009-03-13 10:10:06 -04002206 ref = select_delayed_ref(locked_ref);
2207 if (!ref) {
2208 /* All delayed refs have been processed, Go ahead
2209 * and send the head node to run_one_delayed_ref,
2210 * so that any accounting fixes can happen
2211 */
2212 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002213
2214 if (extent_op && must_insert_reserved) {
2215 kfree(extent_op);
2216 extent_op = NULL;
2217 }
2218
2219 if (extent_op) {
2220 spin_unlock(&delayed_refs->lock);
2221
2222 ret = run_delayed_extent_op(trans, root,
2223 ref, extent_op);
2224 BUG_ON(ret);
2225 kfree(extent_op);
2226
2227 cond_resched();
2228 spin_lock(&delayed_refs->lock);
2229 continue;
2230 }
2231
Chris Masonc3e69d52009-03-13 10:17:05 -04002232 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002233 locked_ref = NULL;
2234 }
2235
2236 ref->in_tree = 0;
2237 rb_erase(&ref->rb_node, &delayed_refs->root);
2238 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002239
Chris Mason56bec292009-03-13 10:10:06 -04002240 spin_unlock(&delayed_refs->lock);
2241
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002243 must_insert_reserved);
2244 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002245
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002246 btrfs_put_delayed_ref(ref);
2247 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002248 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002249
Chris Mason1887be62009-03-13 10:11:24 -04002250 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002251 spin_lock(&delayed_refs->lock);
2252 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002253 return count;
2254}
2255
2256/*
2257 * this starts processing the delayed reference count updates and
2258 * extent insertions we have queued up so far. count can be
2259 * 0, which means to process everything in the tree at the start
2260 * of the run (but not newly added entries), or it can be some target
2261 * number you'd like to process.
2262 */
2263int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2264 struct btrfs_root *root, unsigned long count)
2265{
2266 struct rb_node *node;
2267 struct btrfs_delayed_ref_root *delayed_refs;
2268 struct btrfs_delayed_ref_node *ref;
2269 struct list_head cluster;
2270 int ret;
2271 int run_all = count == (unsigned long)-1;
2272 int run_most = 0;
2273
2274 if (root == root->fs_info->extent_root)
2275 root = root->fs_info->tree_root;
2276
2277 delayed_refs = &trans->transaction->delayed_refs;
2278 INIT_LIST_HEAD(&cluster);
2279again:
2280 spin_lock(&delayed_refs->lock);
2281 if (count == 0) {
2282 count = delayed_refs->num_entries * 2;
2283 run_most = 1;
2284 }
2285 while (1) {
2286 if (!(run_all || run_most) &&
2287 delayed_refs->num_heads_ready < 64)
2288 break;
2289
2290 /*
2291 * go find something we can process in the rbtree. We start at
2292 * the beginning of the tree, and then build a cluster
2293 * of refs to process starting at the first one we are able to
2294 * lock
2295 */
2296 ret = btrfs_find_ref_cluster(trans, &cluster,
2297 delayed_refs->run_delayed_start);
2298 if (ret)
2299 break;
2300
2301 ret = run_clustered_refs(trans, root, &cluster);
2302 BUG_ON(ret < 0);
2303
2304 count -= min_t(unsigned long, ret, count);
2305
2306 if (count == 0)
2307 break;
2308 }
2309
Chris Mason56bec292009-03-13 10:10:06 -04002310 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002311 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002312 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002313 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002314 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002315
2316 while (node) {
2317 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2318 rb_node);
2319 if (btrfs_delayed_ref_is_head(ref)) {
2320 struct btrfs_delayed_ref_head *head;
2321
2322 head = btrfs_delayed_node_to_head(ref);
2323 atomic_inc(&ref->refs);
2324
2325 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002326 /*
2327 * Mutex was contended, block until it's
2328 * released and try again
2329 */
Chris Mason56bec292009-03-13 10:10:06 -04002330 mutex_lock(&head->mutex);
2331 mutex_unlock(&head->mutex);
2332
2333 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002334 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002335 goto again;
2336 }
2337 node = rb_next(node);
2338 }
2339 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002340 schedule_timeout(1);
2341 goto again;
2342 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002343out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002344 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002345 return 0;
2346}
2347
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002348int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2349 struct btrfs_root *root,
2350 u64 bytenr, u64 num_bytes, u64 flags,
2351 int is_data)
2352{
2353 struct btrfs_delayed_extent_op *extent_op;
2354 int ret;
2355
2356 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2357 if (!extent_op)
2358 return -ENOMEM;
2359
2360 extent_op->flags_to_set = flags;
2361 extent_op->update_flags = 1;
2362 extent_op->update_key = 0;
2363 extent_op->is_data = is_data ? 1 : 0;
2364
2365 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2366 if (ret)
2367 kfree(extent_op);
2368 return ret;
2369}
2370
2371static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2372 struct btrfs_root *root,
2373 struct btrfs_path *path,
2374 u64 objectid, u64 offset, u64 bytenr)
2375{
2376 struct btrfs_delayed_ref_head *head;
2377 struct btrfs_delayed_ref_node *ref;
2378 struct btrfs_delayed_data_ref *data_ref;
2379 struct btrfs_delayed_ref_root *delayed_refs;
2380 struct rb_node *node;
2381 int ret = 0;
2382
2383 ret = -ENOENT;
2384 delayed_refs = &trans->transaction->delayed_refs;
2385 spin_lock(&delayed_refs->lock);
2386 head = btrfs_find_delayed_ref_head(trans, bytenr);
2387 if (!head)
2388 goto out;
2389
2390 if (!mutex_trylock(&head->mutex)) {
2391 atomic_inc(&head->node.refs);
2392 spin_unlock(&delayed_refs->lock);
2393
David Sterbab3b4aa72011-04-21 01:20:15 +02002394 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002395
David Sterba8cc33e52011-05-02 15:29:25 +02002396 /*
2397 * Mutex was contended, block until it's released and let
2398 * caller try again
2399 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002400 mutex_lock(&head->mutex);
2401 mutex_unlock(&head->mutex);
2402 btrfs_put_delayed_ref(&head->node);
2403 return -EAGAIN;
2404 }
2405
2406 node = rb_prev(&head->node.rb_node);
2407 if (!node)
2408 goto out_unlock;
2409
2410 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2411
2412 if (ref->bytenr != bytenr)
2413 goto out_unlock;
2414
2415 ret = 1;
2416 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2417 goto out_unlock;
2418
2419 data_ref = btrfs_delayed_node_to_data_ref(ref);
2420
2421 node = rb_prev(node);
2422 if (node) {
2423 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2424 if (ref->bytenr == bytenr)
2425 goto out_unlock;
2426 }
2427
2428 if (data_ref->root != root->root_key.objectid ||
2429 data_ref->objectid != objectid || data_ref->offset != offset)
2430 goto out_unlock;
2431
2432 ret = 0;
2433out_unlock:
2434 mutex_unlock(&head->mutex);
2435out:
2436 spin_unlock(&delayed_refs->lock);
2437 return ret;
2438}
2439
2440static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2441 struct btrfs_root *root,
2442 struct btrfs_path *path,
2443 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002444{
2445 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002446 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002447 struct btrfs_extent_data_ref *ref;
2448 struct btrfs_extent_inline_ref *iref;
2449 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002450 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002451 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002452 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002453
Chris Masonbe20aa92007-12-17 20:14:01 -05002454 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002455 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002456 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002457
Chris Masonbe20aa92007-12-17 20:14:01 -05002458 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2459 if (ret < 0)
2460 goto out;
2461 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002462
2463 ret = -ENOENT;
2464 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002465 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002466
Zheng Yan31840ae2008-09-23 13:14:14 -04002467 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002468 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002469 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002470
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002471 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002472 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002473
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002474 ret = 1;
2475 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2476#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2477 if (item_size < sizeof(*ei)) {
2478 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2479 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002480 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002481#endif
2482 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2483
2484 if (item_size != sizeof(*ei) +
2485 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2486 goto out;
2487
2488 if (btrfs_extent_generation(leaf, ei) <=
2489 btrfs_root_last_snapshot(&root->root_item))
2490 goto out;
2491
2492 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2493 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2494 BTRFS_EXTENT_DATA_REF_KEY)
2495 goto out;
2496
2497 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2498 if (btrfs_extent_refs(leaf, ei) !=
2499 btrfs_extent_data_ref_count(leaf, ref) ||
2500 btrfs_extent_data_ref_root(leaf, ref) !=
2501 root->root_key.objectid ||
2502 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2503 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2504 goto out;
2505
Yan Zhengf321e492008-07-30 09:26:11 -04002506 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002507out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002508 return ret;
2509}
2510
2511int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2512 struct btrfs_root *root,
2513 u64 objectid, u64 offset, u64 bytenr)
2514{
2515 struct btrfs_path *path;
2516 int ret;
2517 int ret2;
2518
2519 path = btrfs_alloc_path();
2520 if (!path)
2521 return -ENOENT;
2522
2523 do {
2524 ret = check_committed_ref(trans, root, path, objectid,
2525 offset, bytenr);
2526 if (ret && ret != -ENOENT)
2527 goto out;
2528
2529 ret2 = check_delayed_ref(trans, root, path, objectid,
2530 offset, bytenr);
2531 } while (ret2 == -EAGAIN);
2532
2533 if (ret2 && ret2 != -ENOENT) {
2534 ret = ret2;
2535 goto out;
2536 }
2537
2538 if (ret != -ENOENT || ret2 != -ENOENT)
2539 ret = 0;
2540out:
Yan Zhengf321e492008-07-30 09:26:11 -04002541 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002542 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2543 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002544 return ret;
2545}
2546
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002547static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002548 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002549 struct extent_buffer *buf,
2550 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002551{
2552 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002553 u64 num_bytes;
2554 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002555 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002556 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002557 struct btrfs_key key;
2558 struct btrfs_file_extent_item *fi;
2559 int i;
2560 int level;
2561 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002562 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002563 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002564
2565 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002566 nritems = btrfs_header_nritems(buf);
2567 level = btrfs_header_level(buf);
2568
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002569 if (!root->ref_cows && level == 0)
2570 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002571
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002572 if (inc)
2573 process_func = btrfs_inc_extent_ref;
2574 else
2575 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002576
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002577 if (full_backref)
2578 parent = buf->start;
2579 else
2580 parent = 0;
2581
Zheng Yan31840ae2008-09-23 13:14:14 -04002582 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002583 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002584 btrfs_item_key_to_cpu(buf, &key, i);
2585 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002586 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002587 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002588 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002589 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002590 BTRFS_FILE_EXTENT_INLINE)
2591 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002592 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2593 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002594 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002595
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002596 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2597 key.offset -= btrfs_file_extent_offset(buf, fi);
2598 ret = process_func(trans, root, bytenr, num_bytes,
2599 parent, ref_root, key.objectid,
2600 key.offset);
2601 if (ret)
2602 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002603 } else {
2604 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002605 num_bytes = btrfs_level_size(root, level - 1);
2606 ret = process_func(trans, root, bytenr, num_bytes,
2607 parent, ref_root, level - 1, 0);
2608 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002609 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002610 }
2611 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002612 return 0;
2613fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002614 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002615 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002616}
2617
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002618int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2619 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002620{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002621 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2622}
Zheng Yan31840ae2008-09-23 13:14:14 -04002623
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002624int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2625 struct extent_buffer *buf, int full_backref)
2626{
2627 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002628}
2629
Chris Mason9078a3e2007-04-26 16:46:15 -04002630static int write_one_cache_group(struct btrfs_trans_handle *trans,
2631 struct btrfs_root *root,
2632 struct btrfs_path *path,
2633 struct btrfs_block_group_cache *cache)
2634{
2635 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002636 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002637 unsigned long bi;
2638 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002639
Chris Mason9078a3e2007-04-26 16:46:15 -04002640 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002641 if (ret < 0)
2642 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002643 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002644
2645 leaf = path->nodes[0];
2646 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2647 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2648 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002649 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002650fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002651 if (ret)
2652 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002653 return 0;
2654
2655}
2656
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002657static struct btrfs_block_group_cache *
2658next_block_group(struct btrfs_root *root,
2659 struct btrfs_block_group_cache *cache)
2660{
2661 struct rb_node *node;
2662 spin_lock(&root->fs_info->block_group_cache_lock);
2663 node = rb_next(&cache->cache_node);
2664 btrfs_put_block_group(cache);
2665 if (node) {
2666 cache = rb_entry(node, struct btrfs_block_group_cache,
2667 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002668 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002669 } else
2670 cache = NULL;
2671 spin_unlock(&root->fs_info->block_group_cache_lock);
2672 return cache;
2673}
2674
Josef Bacik0af3d002010-06-21 14:48:16 -04002675static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2676 struct btrfs_trans_handle *trans,
2677 struct btrfs_path *path)
2678{
2679 struct btrfs_root *root = block_group->fs_info->tree_root;
2680 struct inode *inode = NULL;
2681 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002682 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002683 int num_pages = 0;
2684 int retries = 0;
2685 int ret = 0;
2686
2687 /*
2688 * If this block group is smaller than 100 megs don't bother caching the
2689 * block group.
2690 */
2691 if (block_group->key.offset < (100 * 1024 * 1024)) {
2692 spin_lock(&block_group->lock);
2693 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2694 spin_unlock(&block_group->lock);
2695 return 0;
2696 }
2697
2698again:
2699 inode = lookup_free_space_inode(root, block_group, path);
2700 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2701 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002702 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002703 goto out;
2704 }
2705
2706 if (IS_ERR(inode)) {
2707 BUG_ON(retries);
2708 retries++;
2709
2710 if (block_group->ro)
2711 goto out_free;
2712
2713 ret = create_free_space_inode(root, trans, block_group, path);
2714 if (ret)
2715 goto out_free;
2716 goto again;
2717 }
2718
2719 /*
2720 * We want to set the generation to 0, that way if anything goes wrong
2721 * from here on out we know not to trust this cache when we load up next
2722 * time.
2723 */
2724 BTRFS_I(inode)->generation = 0;
2725 ret = btrfs_update_inode(trans, root, inode);
2726 WARN_ON(ret);
2727
2728 if (i_size_read(inode) > 0) {
2729 ret = btrfs_truncate_free_space_cache(root, trans, path,
2730 inode);
2731 if (ret)
2732 goto out_put;
2733 }
2734
2735 spin_lock(&block_group->lock);
2736 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002737 /* We're not cached, don't bother trying to write stuff out */
2738 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002739 spin_unlock(&block_group->lock);
2740 goto out_put;
2741 }
2742 spin_unlock(&block_group->lock);
2743
2744 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2745 if (!num_pages)
2746 num_pages = 1;
2747
2748 /*
2749 * Just to make absolutely sure we have enough space, we're going to
2750 * preallocate 12 pages worth of space for each block group. In
2751 * practice we ought to use at most 8, but we need extra space so we can
2752 * add our header and have a terminator between the extents and the
2753 * bitmaps.
2754 */
2755 num_pages *= 16;
2756 num_pages *= PAGE_CACHE_SIZE;
2757
2758 ret = btrfs_check_data_free_space(inode, num_pages);
2759 if (ret)
2760 goto out_put;
2761
2762 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2763 num_pages, num_pages,
2764 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002765 if (!ret)
2766 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002767 btrfs_free_reserved_data_space(inode, num_pages);
2768out_put:
2769 iput(inode);
2770out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02002771 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002772out:
2773 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002774 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002775 spin_unlock(&block_group->lock);
2776
2777 return ret;
2778}
2779
Chris Mason96b51792007-10-15 16:15:19 -04002780int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2781 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002782{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002783 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002784 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002785 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002786 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002787
2788 path = btrfs_alloc_path();
2789 if (!path)
2790 return -ENOMEM;
2791
Josef Bacik0af3d002010-06-21 14:48:16 -04002792again:
2793 while (1) {
2794 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2795 while (cache) {
2796 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2797 break;
2798 cache = next_block_group(root, cache);
2799 }
2800 if (!cache) {
2801 if (last == 0)
2802 break;
2803 last = 0;
2804 continue;
2805 }
2806 err = cache_save_setup(cache, trans, path);
2807 last = cache->key.objectid + cache->key.offset;
2808 btrfs_put_block_group(cache);
2809 }
2810
Chris Masond3977122009-01-05 21:25:51 -05002811 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002812 if (last == 0) {
2813 err = btrfs_run_delayed_refs(trans, root,
2814 (unsigned long)-1);
2815 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002816 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002817
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002818 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2819 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002820 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2821 btrfs_put_block_group(cache);
2822 goto again;
2823 }
2824
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002825 if (cache->dirty)
2826 break;
2827 cache = next_block_group(root, cache);
2828 }
2829 if (!cache) {
2830 if (last == 0)
2831 break;
2832 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002833 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002834 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002835
Josef Bacik0cb59c92010-07-02 12:14:14 -04002836 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2837 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002838 cache->dirty = 0;
2839 last = cache->key.objectid + cache->key.offset;
2840
2841 err = write_one_cache_group(trans, root, path, cache);
2842 BUG_ON(err);
2843 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002844 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002845
Josef Bacik0cb59c92010-07-02 12:14:14 -04002846 while (1) {
2847 /*
2848 * I don't think this is needed since we're just marking our
2849 * preallocated extent as written, but just in case it can't
2850 * hurt.
2851 */
2852 if (last == 0) {
2853 err = btrfs_run_delayed_refs(trans, root,
2854 (unsigned long)-1);
2855 BUG_ON(err);
2856 }
2857
2858 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2859 while (cache) {
2860 /*
2861 * Really this shouldn't happen, but it could if we
2862 * couldn't write the entire preallocated extent and
2863 * splitting the extent resulted in a new block.
2864 */
2865 if (cache->dirty) {
2866 btrfs_put_block_group(cache);
2867 goto again;
2868 }
2869 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2870 break;
2871 cache = next_block_group(root, cache);
2872 }
2873 if (!cache) {
2874 if (last == 0)
2875 break;
2876 last = 0;
2877 continue;
2878 }
2879
2880 btrfs_write_out_cache(root, trans, cache, path);
2881
2882 /*
2883 * If we didn't have an error then the cache state is still
2884 * NEED_WRITE, so we can set it to WRITTEN.
2885 */
2886 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2887 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2888 last = cache->key.objectid + cache->key.offset;
2889 btrfs_put_block_group(cache);
2890 }
2891
Chris Mason9078a3e2007-04-26 16:46:15 -04002892 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002893 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002894}
2895
Yan Zhengd2fb3432008-12-11 16:30:39 -05002896int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2897{
2898 struct btrfs_block_group_cache *block_group;
2899 int readonly = 0;
2900
2901 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2902 if (!block_group || block_group->ro)
2903 readonly = 1;
2904 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002905 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002906 return readonly;
2907}
2908
Chris Mason593060d2008-03-25 16:50:33 -04002909static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2910 u64 total_bytes, u64 bytes_used,
2911 struct btrfs_space_info **space_info)
2912{
2913 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002914 int i;
2915 int factor;
2916
2917 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2918 BTRFS_BLOCK_GROUP_RAID10))
2919 factor = 2;
2920 else
2921 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002922
2923 found = __find_space_info(info, flags);
2924 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002925 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002926 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002927 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002928 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002929 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002930 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002931 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002932 *space_info = found;
2933 return 0;
2934 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002935 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002936 if (!found)
2937 return -ENOMEM;
2938
Yan, Zhengb742bb82010-05-16 10:46:24 -04002939 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2940 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04002941 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002942 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04002943 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2944 BTRFS_BLOCK_GROUP_SYSTEM |
2945 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04002946 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002947 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002948 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002949 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002950 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002951 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002952 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002953 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002954 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04002955 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04002956 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04002957 found->flush = 0;
2958 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04002959 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002960 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04002961 return 0;
2962}
2963
Chris Mason8790d502008-04-03 16:29:03 -04002964static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2965{
2966 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002967 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002968 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002969 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002970 if (extra_flags) {
2971 if (flags & BTRFS_BLOCK_GROUP_DATA)
2972 fs_info->avail_data_alloc_bits |= extra_flags;
2973 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2974 fs_info->avail_metadata_alloc_bits |= extra_flags;
2975 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2976 fs_info->avail_system_alloc_bits |= extra_flags;
2977 }
2978}
Chris Mason593060d2008-03-25 16:50:33 -04002979
Yan Zheng2b820322008-11-17 21:11:30 -05002980u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002981{
Chris Masoncd02dca2010-12-13 14:56:23 -05002982 /*
2983 * we add in the count of missing devices because we want
2984 * to make sure that any RAID levels on a degraded FS
2985 * continue to be honored.
2986 */
2987 u64 num_devices = root->fs_info->fs_devices->rw_devices +
2988 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002989
2990 if (num_devices == 1)
2991 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2992 if (num_devices < 4)
2993 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2994
Chris Masonec44a352008-04-28 15:29:52 -04002995 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2996 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002997 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002998 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002999 }
Chris Masonec44a352008-04-28 15:29:52 -04003000
3001 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003002 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003003 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003004 }
Chris Masonec44a352008-04-28 15:29:52 -04003005
3006 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3007 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3008 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3009 (flags & BTRFS_BLOCK_GROUP_DUP)))
3010 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3011 return flags;
3012}
3013
Yan, Zhengb742bb82010-05-16 10:46:24 -04003014static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003015{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003016 if (flags & BTRFS_BLOCK_GROUP_DATA)
3017 flags |= root->fs_info->avail_data_alloc_bits &
3018 root->fs_info->data_alloc_profile;
3019 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3020 flags |= root->fs_info->avail_system_alloc_bits &
3021 root->fs_info->system_alloc_profile;
3022 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3023 flags |= root->fs_info->avail_metadata_alloc_bits &
3024 root->fs_info->metadata_alloc_profile;
3025 return btrfs_reduce_alloc_profile(root, flags);
3026}
Josef Bacik6a632092009-02-20 11:00:09 -05003027
Miao Xie6d07bce2011-01-05 10:07:31 +00003028u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003029{
3030 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003031
Yan, Zhengb742bb82010-05-16 10:46:24 -04003032 if (data)
3033 flags = BTRFS_BLOCK_GROUP_DATA;
3034 else if (root == root->fs_info->chunk_root)
3035 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3036 else
3037 flags = BTRFS_BLOCK_GROUP_METADATA;
3038
3039 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003040}
3041
3042void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3043{
Josef Bacik6a632092009-02-20 11:00:09 -05003044 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003045 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003046}
3047
3048/*
3049 * This will check the space that the inode allocates from to make sure we have
3050 * enough space for bytes.
3051 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003052int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003053{
3054 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003055 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e2412010-03-19 14:38:13 +00003056 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003057 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003058
3059 /* make sure bytes are sectorsize aligned */
3060 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3061
Li Zefan82d59022011-04-20 10:33:24 +08003062 if (root == root->fs_info->tree_root ||
3063 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003064 alloc_chunk = 0;
3065 committed = 1;
3066 }
3067
Josef Bacik6a632092009-02-20 11:00:09 -05003068 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003069 if (!data_sinfo)
3070 goto alloc;
3071
Josef Bacik6a632092009-02-20 11:00:09 -05003072again:
3073 /* make sure we have enough space to handle the data first */
3074 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003075 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3076 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3077 data_sinfo->bytes_may_use;
Josef Bacikab6e2412010-03-19 14:38:13 +00003078
3079 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003080 struct btrfs_trans_handle *trans;
3081
Josef Bacik6a632092009-02-20 11:00:09 -05003082 /*
3083 * if we don't have enough free bytes in this space then we need
3084 * to alloc a new chunk.
3085 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003086 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003087 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003088
Chris Mason0e4f8f82011-04-15 16:05:44 -04003089 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003090 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003091alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003092 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003093 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003094 if (IS_ERR(trans))
3095 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003096
3097 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3098 bytes + 2 * 1024 * 1024,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003099 alloc_target,
3100 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003101 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003102 if (ret < 0) {
3103 if (ret != -ENOSPC)
3104 return ret;
3105 else
3106 goto commit_trans;
3107 }
Chris Mason33b4d472009-09-22 14:45:50 -04003108
3109 if (!data_sinfo) {
3110 btrfs_set_inode_space_info(root, inode);
3111 data_sinfo = BTRFS_I(inode)->space_info;
3112 }
Josef Bacik6a632092009-02-20 11:00:09 -05003113 goto again;
3114 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003115
3116 /*
3117 * If we have less pinned bytes than we want to allocate then
3118 * don't bother committing the transaction, it won't help us.
3119 */
3120 if (data_sinfo->bytes_pinned < bytes)
3121 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003122 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003123
3124 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003125commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003126 if (!committed &&
3127 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003128 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003129 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003130 if (IS_ERR(trans))
3131 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003132 ret = btrfs_commit_transaction(trans, root);
3133 if (ret)
3134 return ret;
3135 goto again;
3136 }
3137
Josef Bacik6a632092009-02-20 11:00:09 -05003138 return -ENOSPC;
3139 }
3140 data_sinfo->bytes_may_use += bytes;
Josef Bacik6a632092009-02-20 11:00:09 -05003141 spin_unlock(&data_sinfo->lock);
3142
Josef Bacik9ed74f22009-09-11 16:12:44 -04003143 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003144}
3145
3146/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003147 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003148 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003149void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003150{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003151 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003152 struct btrfs_space_info *data_sinfo;
3153
3154 /* make sure bytes are sectorsize aligned */
3155 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3156
3157 data_sinfo = BTRFS_I(inode)->space_info;
3158 spin_lock(&data_sinfo->lock);
3159 data_sinfo->bytes_may_use -= bytes;
Josef Bacik6a632092009-02-20 11:00:09 -05003160 spin_unlock(&data_sinfo->lock);
3161}
3162
Josef Bacik97e728d2009-04-21 17:40:57 -04003163static void force_metadata_allocation(struct btrfs_fs_info *info)
3164{
3165 struct list_head *head = &info->space_info;
3166 struct btrfs_space_info *found;
3167
3168 rcu_read_lock();
3169 list_for_each_entry_rcu(found, head, list) {
3170 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003171 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003172 }
3173 rcu_read_unlock();
3174}
3175
Chris Masone5bc2452010-10-26 13:37:56 -04003176static int should_alloc_chunk(struct btrfs_root *root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003177 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3178 int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003179{
Josef Bacikfb25e912011-07-26 17:00:46 -04003180 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003181 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003182 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003183 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003184
Chris Mason0e4f8f82011-04-15 16:05:44 -04003185 if (force == CHUNK_ALLOC_FORCE)
3186 return 1;
3187
3188 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003189 * We need to take into account the global rsv because for all intents
3190 * and purposes it's used space. Don't worry about locking the
3191 * global_rsv, it doesn't change except when the transaction commits.
3192 */
3193 num_allocated += global_rsv->size;
3194
3195 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003196 * in limited mode, we want to have some free space up to
3197 * about 1% of the FS size.
3198 */
3199 if (force == CHUNK_ALLOC_LIMITED) {
3200 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3201 thresh = max_t(u64, 64 * 1024 * 1024,
3202 div_factor_fine(thresh, 1));
3203
3204 if (num_bytes - num_allocated < thresh)
3205 return 1;
3206 }
3207
3208 /*
3209 * we have two similar checks here, one based on percentage
3210 * and once based on a hard number of 256MB. The idea
3211 * is that if we have a good amount of free
3212 * room, don't allocate a chunk. A good mount is
3213 * less than 80% utilized of the chunks we have allocated,
3214 * or more than 256MB free
3215 */
3216 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003217 return 0;
3218
Chris Mason0e4f8f82011-04-15 16:05:44 -04003219 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
Yan, Zheng424499d2010-05-16 10:46:25 -04003220 return 0;
3221
Chris Masone5bc2452010-10-26 13:37:56 -04003222 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003223
3224 /* 256MB or 5% of the FS */
Chris Masone5bc2452010-10-26 13:37:56 -04003225 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3226
3227 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003228 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003229 return 1;
3230}
3231
Chris Mason6324fbf2008-03-24 15:01:59 -04003232static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3233 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003234 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003235{
3236 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003237 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003238 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003239 int ret = 0;
3240
Yan Zheng2b820322008-11-17 21:11:30 -05003241 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003242
Chris Mason6324fbf2008-03-24 15:01:59 -04003243 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003244 if (!space_info) {
3245 ret = update_space_info(extent_root->fs_info, flags,
3246 0, 0, &space_info);
3247 BUG_ON(ret);
3248 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003249 BUG_ON(!space_info);
3250
Josef Bacik6d741192011-04-11 20:20:11 -04003251again:
Josef Bacik25179202008-10-29 14:49:05 -04003252 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003253 if (space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003254 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003255 if (space_info->full) {
3256 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003257 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003258 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003259
Chris Mason0e4f8f82011-04-15 16:05:44 -04003260 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003261 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003262 return 0;
3263 } else if (space_info->chunk_alloc) {
3264 wait_for_alloc = 1;
3265 } else {
3266 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003267 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003268
Josef Bacik25179202008-10-29 14:49:05 -04003269 spin_unlock(&space_info->lock);
3270
Josef Bacik6d741192011-04-11 20:20:11 -04003271 mutex_lock(&fs_info->chunk_mutex);
3272
3273 /*
3274 * The chunk_mutex is held throughout the entirety of a chunk
3275 * allocation, so once we've acquired the chunk_mutex we know that the
3276 * other guy is done and we need to recheck and see if we should
3277 * allocate.
3278 */
3279 if (wait_for_alloc) {
3280 mutex_unlock(&fs_info->chunk_mutex);
3281 wait_for_alloc = 0;
3282 goto again;
3283 }
3284
Josef Bacik97e728d2009-04-21 17:40:57 -04003285 /*
Josef Bacik67377732010-09-16 16:19:09 -04003286 * If we have mixed data/metadata chunks we want to make sure we keep
3287 * allocating mixed chunks instead of individual chunks.
3288 */
3289 if (btrfs_mixed_space_info(space_info))
3290 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3291
3292 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003293 * if we're doing a data chunk, go ahead and make sure that
3294 * we keep a reasonable number of metadata chunks allocated in the
3295 * FS as well.
3296 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003297 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003298 fs_info->data_chunk_allocations++;
3299 if (!(fs_info->data_chunk_allocations %
3300 fs_info->metadata_ratio))
3301 force_metadata_allocation(fs_info);
3302 }
3303
Yan Zheng2b820322008-11-17 21:11:30 -05003304 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003305 if (ret < 0 && ret != -ENOSPC)
3306 goto out;
3307
Josef Bacik9ed74f22009-09-11 16:12:44 -04003308 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003309 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003310 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003311 else
3312 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003313
Chris Mason0e4f8f82011-04-15 16:05:44 -04003314 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003315 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003316 spin_unlock(&space_info->lock);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003317out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003318 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003319 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003320}
3321
Yan, Zheng5da9d012010-05-16 10:46:25 -04003322/*
3323 * shrink metadata reservation for delalloc
3324 */
3325static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003326 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003327{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003328 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003329 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003330 u64 reserved;
3331 u64 max_reclaim;
3332 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003333 long time_left;
Chris Masonbf9022e2010-10-26 13:40:45 -04003334 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003335 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003336 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003337
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003338 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003339 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003340
3341 smp_mb();
Josef Bacikfb25e912011-07-26 17:00:46 -04003342 reserved = space_info->bytes_may_use;
Chris Mason36e39c42011-03-12 07:08:42 -05003343 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003344
3345 if (reserved == 0)
3346 return 0;
3347
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003348 smp_mb();
3349 if (root->fs_info->delalloc_bytes == 0) {
3350 if (trans)
3351 return 0;
3352 btrfs_wait_ordered_extents(root, 0, 0);
3353 return 0;
3354 }
3355
Yan, Zheng5da9d012010-05-16 10:46:25 -04003356 max_reclaim = min(reserved, to_reclaim);
3357
Josef Bacikb1953bc2011-01-21 21:10:01 +00003358 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003359 /* have the flusher threads jump in and do some IO */
3360 smp_mb();
3361 nr_pages = min_t(unsigned long, nr_pages,
3362 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3363 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003364
Josef Bacik0019f102010-10-15 15:18:40 -04003365 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04003366 if (reserved > space_info->bytes_may_use)
3367 reclaimed += reserved - space_info->bytes_may_use;
3368 reserved = space_info->bytes_may_use;
Josef Bacik0019f102010-10-15 15:18:40 -04003369 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003370
Chris Mason36e39c42011-03-12 07:08:42 -05003371 loops++;
3372
Yan, Zheng5da9d012010-05-16 10:46:25 -04003373 if (reserved == 0 || reclaimed >= max_reclaim)
3374 break;
3375
3376 if (trans && trans->transaction->blocked)
3377 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003378
Chris Mason36e39c42011-03-12 07:08:42 -05003379 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003380
3381 /* We were interrupted, exit */
3382 if (time_left)
3383 break;
3384
Chris Mason36e39c42011-03-12 07:08:42 -05003385 /* we've kicked the IO a few times, if anything has been freed,
3386 * exit. There is no sense in looping here for a long time
3387 * when we really need to commit the transaction, or there are
3388 * just too many writers without enough free space
3389 */
3390
3391 if (loops > 3) {
3392 smp_mb();
3393 if (progress != space_info->reservation_progress)
3394 break;
3395 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003396
Yan, Zheng5da9d012010-05-16 10:46:25 -04003397 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003398 if (reclaimed >= to_reclaim && !trans)
3399 btrfs_wait_ordered_extents(root, 0, 0);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003400 return reclaimed >= to_reclaim;
3401}
3402
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003403/*
3404 * Retries tells us how many times we've called reserve_metadata_bytes. The
3405 * idea is if this is the first call (retries == 0) then we will add to our
3406 * reserved count if we can't make the allocation in order to hold our place
3407 * while we go and try and free up space. That way for retries > 1 we don't try
3408 * and add space, we just check to see if the amount of unused space is >= the
3409 * total space, meaning that our reservation is valid.
3410 *
3411 * However if we don't intend to retry this reservation, pass -1 as retries so
3412 * that it short circuits this logic.
3413 */
3414static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3415 struct btrfs_root *root,
3416 struct btrfs_block_rsv *block_rsv,
3417 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003418{
3419 struct btrfs_space_info *space_info = block_rsv->space_info;
3420 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003421 u64 num_bytes = orig_bytes;
3422 int retries = 0;
3423 int ret = 0;
Josef Bacik38227932010-10-26 12:52:53 -04003424 bool committed = false;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003425 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003426again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003427 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003428 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003429 /*
3430 * We only want to wait if somebody other than us is flushing and we are
3431 * actually alloed to flush.
3432 */
3433 while (flush && !flushing && space_info->flush) {
3434 spin_unlock(&space_info->lock);
3435 /*
3436 * If we have a trans handle we can't wait because the flusher
3437 * may have to commit the transaction, which would mean we would
3438 * deadlock since we are waiting for the flusher to finish, but
3439 * hold the current transaction open.
3440 */
3441 if (trans)
3442 return -EAGAIN;
3443 ret = wait_event_interruptible(space_info->wait,
3444 !space_info->flush);
3445 /* Must have been interrupted, return */
3446 if (ret)
3447 return -EINTR;
3448
3449 spin_lock(&space_info->lock);
3450 }
3451
3452 ret = -ENOSPC;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003453 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003454 space_info->bytes_pinned + space_info->bytes_readonly +
3455 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003456
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003457 /*
3458 * The idea here is that we've not already over-reserved the block group
3459 * then we can go ahead and save our reservation first and then start
3460 * flushing if we need to. Otherwise if we've already overcommitted
3461 * lets start flushing stuff first and then come back and try to make
3462 * our reservation.
3463 */
3464 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003465 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003466 if (unused >= num_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04003467 space_info->bytes_may_use += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003468 ret = 0;
3469 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003470 /*
3471 * Ok set num_bytes to orig_bytes since we aren't
3472 * overocmmitted, this way we only try and reclaim what
3473 * we need.
3474 */
3475 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003476 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003477 } else {
3478 /*
3479 * Ok we're over committed, set num_bytes to the overcommitted
3480 * amount plus the amount of bytes that we need for this
3481 * reservation.
3482 */
3483 num_bytes = unused - space_info->total_bytes +
3484 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003485 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003486
3487 /*
3488 * Couldn't make our reservation, save our place so while we're trying
3489 * to reclaim space we can actually use it instead of somebody else
3490 * stealing it from us.
3491 */
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003492 if (ret && flush) {
3493 flushing = true;
3494 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003495 }
3496
Yan, Zhengf0486c62010-05-16 10:46:25 -04003497 spin_unlock(&space_info->lock);
3498
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003499 if (!ret || !flush)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003500 goto out;
3501
3502 /*
3503 * We do synchronous shrinking since we don't actually unreserve
3504 * metadata until after the IO is completed.
3505 */
3506 ret = shrink_delalloc(trans, root, num_bytes, 1);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003507 if (ret < 0)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003508 goto out;
3509
Chris Mason75c195a2011-07-27 15:57:44 -04003510 ret = 0;
3511
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003512 /*
3513 * So if we were overcommitted it's possible that somebody else flushed
3514 * out enough space and we simply didn't have enough space to reclaim,
3515 * so go back around and try again.
3516 */
3517 if (retries < 2) {
3518 retries++;
3519 goto again;
3520 }
3521
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003522 /*
3523 * Not enough space to be reclaimed, don't bother committing the
3524 * transaction.
3525 */
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003526 spin_lock(&space_info->lock);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003527 if (space_info->bytes_pinned < orig_bytes)
3528 ret = -ENOSPC;
3529 spin_unlock(&space_info->lock);
3530 if (ret)
3531 goto out;
3532
3533 ret = -EAGAIN;
Chris Mason75c195a2011-07-27 15:57:44 -04003534 if (trans)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003535 goto out;
3536
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003537 ret = -ENOSPC;
Chris Mason75c195a2011-07-27 15:57:44 -04003538 if (committed)
3539 goto out;
3540
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003541 trans = btrfs_join_transaction(root);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003542 if (IS_ERR(trans))
3543 goto out;
3544 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003545 if (!ret) {
3546 trans = NULL;
3547 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003548 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003549 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003550
3551out:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003552 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003553 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003554 space_info->flush = 0;
3555 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003556 spin_unlock(&space_info->lock);
3557 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04003558 return ret;
3559}
3560
3561static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3562 struct btrfs_root *root)
3563{
3564 struct btrfs_block_rsv *block_rsv;
3565 if (root->ref_cows)
3566 block_rsv = trans->block_rsv;
3567 else
3568 block_rsv = root->block_rsv;
3569
3570 if (!block_rsv)
3571 block_rsv = &root->fs_info->empty_block_rsv;
3572
3573 return block_rsv;
3574}
3575
3576static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3577 u64 num_bytes)
3578{
3579 int ret = -ENOSPC;
3580 spin_lock(&block_rsv->lock);
3581 if (block_rsv->reserved >= num_bytes) {
3582 block_rsv->reserved -= num_bytes;
3583 if (block_rsv->reserved < block_rsv->size)
3584 block_rsv->full = 0;
3585 ret = 0;
3586 }
3587 spin_unlock(&block_rsv->lock);
3588 return ret;
3589}
3590
3591static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3592 u64 num_bytes, int update_size)
3593{
3594 spin_lock(&block_rsv->lock);
3595 block_rsv->reserved += num_bytes;
3596 if (update_size)
3597 block_rsv->size += num_bytes;
3598 else if (block_rsv->reserved >= block_rsv->size)
3599 block_rsv->full = 1;
3600 spin_unlock(&block_rsv->lock);
3601}
3602
David Sterba62a45b62011-04-20 15:52:26 +02003603static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3604 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003605{
3606 struct btrfs_space_info *space_info = block_rsv->space_info;
3607
3608 spin_lock(&block_rsv->lock);
3609 if (num_bytes == (u64)-1)
3610 num_bytes = block_rsv->size;
3611 block_rsv->size -= num_bytes;
3612 if (block_rsv->reserved >= block_rsv->size) {
3613 num_bytes = block_rsv->reserved - block_rsv->size;
3614 block_rsv->reserved = block_rsv->size;
3615 block_rsv->full = 1;
3616 } else {
3617 num_bytes = 0;
3618 }
3619 spin_unlock(&block_rsv->lock);
3620
3621 if (num_bytes > 0) {
3622 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003623 spin_lock(&dest->lock);
3624 if (!dest->full) {
3625 u64 bytes_to_add;
3626
3627 bytes_to_add = dest->size - dest->reserved;
3628 bytes_to_add = min(num_bytes, bytes_to_add);
3629 dest->reserved += bytes_to_add;
3630 if (dest->reserved >= dest->size)
3631 dest->full = 1;
3632 num_bytes -= bytes_to_add;
3633 }
3634 spin_unlock(&dest->lock);
3635 }
3636 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003637 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04003638 space_info->bytes_may_use -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003639 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003640 spin_unlock(&space_info->lock);
3641 }
3642 }
3643}
3644
3645static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3646 struct btrfs_block_rsv *dst, u64 num_bytes)
3647{
3648 int ret;
3649
3650 ret = block_rsv_use_bytes(src, num_bytes);
3651 if (ret)
3652 return ret;
3653
3654 block_rsv_add_bytes(dst, num_bytes, 1);
3655 return 0;
3656}
3657
3658void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3659{
3660 memset(rsv, 0, sizeof(*rsv));
3661 spin_lock_init(&rsv->lock);
3662 atomic_set(&rsv->usage, 1);
3663 rsv->priority = 6;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003664}
3665
3666struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3667{
3668 struct btrfs_block_rsv *block_rsv;
3669 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003670
3671 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3672 if (!block_rsv)
3673 return NULL;
3674
3675 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003676 block_rsv->space_info = __find_space_info(fs_info,
3677 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003678 return block_rsv;
3679}
3680
3681void btrfs_free_block_rsv(struct btrfs_root *root,
3682 struct btrfs_block_rsv *rsv)
3683{
3684 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3685 btrfs_block_rsv_release(root, rsv, (u64)-1);
Josef Bacik37be25b2011-08-05 10:25:38 -04003686 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003687 }
3688}
3689
Yan, Zhengf0486c62010-05-16 10:46:25 -04003690int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3691 struct btrfs_root *root,
3692 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003693 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003694{
3695 int ret;
3696
3697 if (num_bytes == 0)
3698 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003699
3700 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003701 if (!ret) {
3702 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3703 return 0;
3704 }
3705
Yan, Zhengf0486c62010-05-16 10:46:25 -04003706 return ret;
3707}
3708
3709int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3710 struct btrfs_root *root,
3711 struct btrfs_block_rsv *block_rsv,
3712 u64 min_reserved, int min_factor)
3713{
3714 u64 num_bytes = 0;
3715 int commit_trans = 0;
3716 int ret = -ENOSPC;
3717
3718 if (!block_rsv)
3719 return 0;
3720
3721 spin_lock(&block_rsv->lock);
3722 if (min_factor > 0)
3723 num_bytes = div_factor(block_rsv->size, min_factor);
3724 if (min_reserved > num_bytes)
3725 num_bytes = min_reserved;
3726
3727 if (block_rsv->reserved >= num_bytes) {
3728 ret = 0;
3729 } else {
3730 num_bytes -= block_rsv->reserved;
Josef Bacik37be25b2011-08-05 10:25:38 -04003731 commit_trans = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003732 }
3733 spin_unlock(&block_rsv->lock);
3734 if (!ret)
3735 return 0;
3736
3737 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003738 ret = reserve_metadata_bytes(trans, root, block_rsv,
3739 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003740 if (!ret) {
3741 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3742 return 0;
3743 }
3744 }
3745
3746 if (commit_trans) {
Josef Bacik37be25b2011-08-05 10:25:38 -04003747 struct btrfs_space_info *sinfo = block_rsv->space_info;
3748
Yan, Zhengf0486c62010-05-16 10:46:25 -04003749 if (trans)
3750 return -EAGAIN;
Josef Bacik37be25b2011-08-05 10:25:38 -04003751
3752 spin_lock(&sinfo->lock);
3753 if (sinfo->bytes_pinned < num_bytes) {
3754 spin_unlock(&sinfo->lock);
3755 return -ENOSPC;
3756 }
3757 spin_unlock(&sinfo->lock);
3758
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003759 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003760 BUG_ON(IS_ERR(trans));
3761 ret = btrfs_commit_transaction(trans, root);
3762 return 0;
3763 }
3764
Yan, Zhengf0486c62010-05-16 10:46:25 -04003765 return -ENOSPC;
3766}
3767
3768int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3769 struct btrfs_block_rsv *dst_rsv,
3770 u64 num_bytes)
3771{
3772 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3773}
3774
3775void btrfs_block_rsv_release(struct btrfs_root *root,
3776 struct btrfs_block_rsv *block_rsv,
3777 u64 num_bytes)
3778{
3779 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3780 if (global_rsv->full || global_rsv == block_rsv ||
3781 block_rsv->space_info != global_rsv->space_info)
3782 global_rsv = NULL;
3783 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3784}
3785
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003786/*
3787 * helper to calculate size of global block reservation.
3788 * the desired value is sum of space used by extent tree,
3789 * checksum tree and root tree
3790 */
3791static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3792{
3793 struct btrfs_space_info *sinfo;
3794 u64 num_bytes;
3795 u64 meta_used;
3796 u64 data_used;
3797 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003798
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003799 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3800 spin_lock(&sinfo->lock);
3801 data_used = sinfo->bytes_used;
3802 spin_unlock(&sinfo->lock);
3803
3804 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3805 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003806 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3807 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003808 meta_used = sinfo->bytes_used;
3809 spin_unlock(&sinfo->lock);
3810
3811 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3812 csum_size * 2;
3813 num_bytes += div64_u64(data_used + meta_used, 50);
3814
3815 if (num_bytes * 3 > meta_used)
3816 num_bytes = div64_u64(meta_used, 3);
3817
3818 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3819}
3820
3821static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3822{
3823 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3824 struct btrfs_space_info *sinfo = block_rsv->space_info;
3825 u64 num_bytes;
3826
3827 num_bytes = calc_global_metadata_size(fs_info);
3828
3829 spin_lock(&block_rsv->lock);
3830 spin_lock(&sinfo->lock);
3831
3832 block_rsv->size = num_bytes;
3833
3834 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003835 sinfo->bytes_reserved + sinfo->bytes_readonly +
3836 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003837
3838 if (sinfo->total_bytes > num_bytes) {
3839 num_bytes = sinfo->total_bytes - num_bytes;
3840 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04003841 sinfo->bytes_may_use += num_bytes;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003842 }
3843
3844 if (block_rsv->reserved >= block_rsv->size) {
3845 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003846 sinfo->bytes_may_use -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003847 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003848 block_rsv->reserved = block_rsv->size;
3849 block_rsv->full = 1;
3850 }
David Sterba182608c2011-05-05 13:13:16 +02003851
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003852 spin_unlock(&sinfo->lock);
3853 spin_unlock(&block_rsv->lock);
3854}
3855
Yan, Zhengf0486c62010-05-16 10:46:25 -04003856static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3857{
3858 struct btrfs_space_info *space_info;
3859
3860 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3861 fs_info->chunk_block_rsv.space_info = space_info;
3862 fs_info->chunk_block_rsv.priority = 10;
3863
3864 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003865 fs_info->global_block_rsv.space_info = space_info;
3866 fs_info->global_block_rsv.priority = 10;
3867 fs_info->global_block_rsv.refill_used = 1;
3868 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003869 fs_info->trans_block_rsv.space_info = space_info;
3870 fs_info->empty_block_rsv.space_info = space_info;
3871 fs_info->empty_block_rsv.priority = 10;
3872
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003873 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3874 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3875 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3876 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003877 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003878
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003879 update_global_block_rsv(fs_info);
3880}
3881
3882static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3883{
3884 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3885 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3886 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3887 WARN_ON(fs_info->trans_block_rsv.size > 0);
3888 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3889 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3890 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003891}
3892
Josef Bacikfcb80c22011-05-03 10:40:22 -04003893int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans,
3894 struct btrfs_root *root,
3895 struct btrfs_block_rsv *rsv)
3896{
3897 struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv;
3898 u64 num_bytes;
3899 int ret;
3900
3901 /*
3902 * Truncate should be freeing data, but give us 2 items just in case it
3903 * needs to use some space. We may want to be smarter about this in the
3904 * future.
3905 */
Chris Masonff5714c2011-05-28 07:00:39 -04003906 num_bytes = btrfs_calc_trans_metadata_size(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04003907
3908 /* We already have enough bytes, just return */
3909 if (rsv->reserved >= num_bytes)
3910 return 0;
3911
3912 num_bytes -= rsv->reserved;
3913
3914 /*
3915 * You should have reserved enough space before hand to do this, so this
3916 * should not fail.
3917 */
3918 ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes);
3919 BUG_ON(ret);
3920
3921 return 0;
3922}
3923
Yan, Zhenga22285a2010-05-16 10:48:46 -04003924void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3925 struct btrfs_root *root)
3926{
3927 if (!trans->bytes_reserved)
3928 return;
3929
3930 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3931 btrfs_block_rsv_release(root, trans->block_rsv,
3932 trans->bytes_reserved);
3933 trans->bytes_reserved = 0;
3934}
3935
Yan, Zhengd68fc572010-05-16 10:49:58 -04003936int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3937 struct inode *inode)
3938{
3939 struct btrfs_root *root = BTRFS_I(inode)->root;
3940 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3941 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3942
3943 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04003944 * We need to hold space in order to delete our orphan item once we've
3945 * added it, so this takes the reservation so we can release it later
3946 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003947 */
Chris Masonff5714c2011-05-28 07:00:39 -04003948 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003949 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3950}
3951
3952void btrfs_orphan_release_metadata(struct inode *inode)
3953{
3954 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04003955 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003956 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3957}
3958
Yan, Zhenga22285a2010-05-16 10:48:46 -04003959int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3960 struct btrfs_pending_snapshot *pending)
3961{
3962 struct btrfs_root *root = pending->root;
3963 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3964 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3965 /*
3966 * two for root back/forward refs, two for directory entries
3967 * and one for root of the snapshot.
3968 */
Miao Xie16cdcec2011-04-22 18:12:22 +08003969 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003970 dst_rsv->space_info = src_rsv->space_info;
3971 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3972}
3973
Josef Bacik7709cde2011-08-04 10:25:02 -04003974/**
3975 * drop_outstanding_extent - drop an outstanding extent
3976 * @inode: the inode we're dropping the extent for
3977 *
3978 * This is called when we are freeing up an outstanding extent, either called
3979 * after an error or after an extent is written. This will return the number of
3980 * reserved extents that need to be freed. This must be called with
3981 * BTRFS_I(inode)->lock held.
3982 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00003983static unsigned drop_outstanding_extent(struct inode *inode)
3984{
3985 unsigned dropped_extents = 0;
3986
Josef Bacik9e0baf62011-07-15 15:16:44 +00003987 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
3988 BTRFS_I(inode)->outstanding_extents--;
3989
3990 /*
3991 * If we have more or the same amount of outsanding extents than we have
3992 * reserved then we need to leave the reserved extents count alone.
3993 */
3994 if (BTRFS_I(inode)->outstanding_extents >=
3995 BTRFS_I(inode)->reserved_extents)
Josef Bacik7709cde2011-08-04 10:25:02 -04003996 return 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00003997
3998 dropped_extents = BTRFS_I(inode)->reserved_extents -
3999 BTRFS_I(inode)->outstanding_extents;
4000 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004001 return dropped_extents;
4002}
4003
Josef Bacik7709cde2011-08-04 10:25:02 -04004004/**
4005 * calc_csum_metadata_size - return the amount of metada space that must be
4006 * reserved/free'd for the given bytes.
4007 * @inode: the inode we're manipulating
4008 * @num_bytes: the number of bytes in question
4009 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4010 *
4011 * This adjusts the number of csum_bytes in the inode and then returns the
4012 * correct amount of metadata that must either be reserved or freed. We
4013 * calculate how many checksums we can fit into one leaf and then divide the
4014 * number of bytes that will need to be checksumed by this value to figure out
4015 * how many checksums will be required. If we are adding bytes then the number
4016 * may go up and we will return the number of additional bytes that must be
4017 * reserved. If it is going down we will return the number of bytes that must
4018 * be freed.
4019 *
4020 * This must be called with BTRFS_I(inode)->lock held.
4021 */
4022static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4023 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004024{
Josef Bacik7709cde2011-08-04 10:25:02 -04004025 struct btrfs_root *root = BTRFS_I(inode)->root;
4026 u64 csum_size;
4027 int num_csums_per_leaf;
4028 int num_csums;
4029 int old_csums;
4030
4031 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4032 BTRFS_I(inode)->csum_bytes == 0)
4033 return 0;
4034
4035 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4036 if (reserve)
4037 BTRFS_I(inode)->csum_bytes += num_bytes;
4038 else
4039 BTRFS_I(inode)->csum_bytes -= num_bytes;
4040 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4041 num_csums_per_leaf = (int)div64_u64(csum_size,
4042 sizeof(struct btrfs_csum_item) +
4043 sizeof(struct btrfs_disk_key));
4044 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4045 num_csums = num_csums + num_csums_per_leaf - 1;
4046 num_csums = num_csums / num_csums_per_leaf;
4047
4048 old_csums = old_csums + num_csums_per_leaf - 1;
4049 old_csums = old_csums / num_csums_per_leaf;
4050
4051 /* No change, no need to reserve more */
4052 if (old_csums == num_csums)
4053 return 0;
4054
4055 if (reserve)
4056 return btrfs_calc_trans_metadata_size(root,
4057 num_csums - old_csums);
4058
4059 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004060}
4061
4062int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4063{
4064 struct btrfs_root *root = BTRFS_I(inode)->root;
4065 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004066 u64 to_reserve = 0;
4067 unsigned nr_extents = 0;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004068 int ret;
4069
4070 if (btrfs_transaction_in_commit(root->fs_info))
4071 schedule_timeout(1);
4072
4073 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004074
Josef Bacik9e0baf62011-07-15 15:16:44 +00004075 spin_lock(&BTRFS_I(inode)->lock);
4076 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004077
Josef Bacik9e0baf62011-07-15 15:16:44 +00004078 if (BTRFS_I(inode)->outstanding_extents >
4079 BTRFS_I(inode)->reserved_extents) {
4080 nr_extents = BTRFS_I(inode)->outstanding_extents -
4081 BTRFS_I(inode)->reserved_extents;
4082 BTRFS_I(inode)->reserved_extents += nr_extents;
4083
Miao Xie16cdcec2011-04-22 18:12:22 +08004084 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004085 }
Josef Bacik7709cde2011-08-04 10:25:02 -04004086 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004087 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004088
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004089 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004090 if (ret) {
4091 unsigned dropped;
4092 /*
4093 * We don't need the return value since our reservation failed,
4094 * we just need to clean up our counter.
4095 */
Josef Bacik7709cde2011-08-04 10:25:02 -04004096 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004097 dropped = drop_outstanding_extent(inode);
4098 WARN_ON(dropped > 1);
Josef Bacik7709cde2011-08-04 10:25:02 -04004099 BTRFS_I(inode)->csum_bytes -= num_bytes;
4100 spin_unlock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004101 return ret;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004102 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004103
4104 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4105
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004106 return 0;
4107}
4108
Josef Bacik7709cde2011-08-04 10:25:02 -04004109/**
4110 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4111 * @inode: the inode to release the reservation for
4112 * @num_bytes: the number of bytes we're releasing
4113 *
4114 * This will release the metadata reservation for an inode. This can be called
4115 * once we complete IO for a given set of bytes to release their metadata
4116 * reservations.
4117 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004118void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4119{
4120 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004121 u64 to_free = 0;
4122 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004123
4124 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004125 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004126 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004127
Josef Bacik7709cde2011-08-04 10:25:02 -04004128 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4129 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004130 if (dropped > 0)
4131 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004132
4133 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4134 to_free);
4135}
4136
Josef Bacik7709cde2011-08-04 10:25:02 -04004137/**
4138 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4139 * @inode: inode we're writing to
4140 * @num_bytes: the number of bytes we want to allocate
4141 *
4142 * This will do the following things
4143 *
4144 * o reserve space in the data space info for num_bytes
4145 * o reserve space in the metadata space info based on number of outstanding
4146 * extents and how much csums will be needed
4147 * o add to the inodes ->delalloc_bytes
4148 * o add it to the fs_info's delalloc inodes list.
4149 *
4150 * This will return 0 for success and -ENOSPC if there is no space left.
4151 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004152int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4153{
4154 int ret;
4155
4156 ret = btrfs_check_data_free_space(inode, num_bytes);
4157 if (ret)
4158 return ret;
4159
4160 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4161 if (ret) {
4162 btrfs_free_reserved_data_space(inode, num_bytes);
4163 return ret;
4164 }
4165
4166 return 0;
4167}
4168
Josef Bacik7709cde2011-08-04 10:25:02 -04004169/**
4170 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4171 * @inode: inode we're releasing space for
4172 * @num_bytes: the number of bytes we want to free up
4173 *
4174 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4175 * called in the case that we don't need the metadata AND data reservations
4176 * anymore. So if there is an error or we insert an inline extent.
4177 *
4178 * This function will release the metadata space that was not used and will
4179 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4180 * list if there are no delalloc bytes left.
4181 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004182void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4183{
4184 btrfs_delalloc_release_metadata(inode, num_bytes);
4185 btrfs_free_reserved_data_space(inode, num_bytes);
4186}
4187
Chris Mason9078a3e2007-04-26 16:46:15 -04004188static int update_block_group(struct btrfs_trans_handle *trans,
4189 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004190 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004191{
Josef Bacik0af3d002010-06-21 14:48:16 -04004192 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004193 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004194 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004195 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004196 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004197 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004198
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004199 /* block accounting for super block */
4200 spin_lock(&info->delalloc_lock);
4201 old_val = btrfs_super_bytes_used(&info->super_copy);
4202 if (alloc)
4203 old_val += num_bytes;
4204 else
4205 old_val -= num_bytes;
4206 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004207 spin_unlock(&info->delalloc_lock);
4208
Chris Masond3977122009-01-05 21:25:51 -05004209 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004210 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004211 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004212 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004213 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4214 BTRFS_BLOCK_GROUP_RAID1 |
4215 BTRFS_BLOCK_GROUP_RAID10))
4216 factor = 2;
4217 else
4218 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004219 /*
4220 * If this block group has free space cache written out, we
4221 * need to make sure to load it if we are removing space. This
4222 * is because we need the unpinning stage to actually add the
4223 * space back to the block group, otherwise we will leak space.
4224 */
4225 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004226 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004227
Chris Masondb945352007-10-15 16:15:53 -04004228 byte_in_group = bytenr - cache->key.objectid;
4229 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004230
Josef Bacik25179202008-10-29 14:49:05 -04004231 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004232 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004233
4234 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4235 cache->disk_cache_state < BTRFS_DC_CLEAR)
4236 cache->disk_cache_state = BTRFS_DC_CLEAR;
4237
Josef Bacik0f9dd462008-09-23 13:14:11 -04004238 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004239 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004240 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004241 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004242 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004243 btrfs_set_block_group_used(&cache->item, old_val);
4244 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004245 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004246 cache->space_info->bytes_used += num_bytes;
4247 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004248 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004249 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004250 } else {
Chris Masondb945352007-10-15 16:15:53 -04004251 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004252 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004253 cache->pinned += num_bytes;
4254 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004255 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004256 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004257 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004258 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004259
Yan, Zhengf0486c62010-05-16 10:46:25 -04004260 set_extent_dirty(info->pinned_extents,
4261 bytenr, bytenr + num_bytes - 1,
4262 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004263 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004264 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004265 total -= num_bytes;
4266 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004267 }
4268 return 0;
4269}
Chris Mason6324fbf2008-03-24 15:01:59 -04004270
Chris Masona061fc82008-05-07 11:43:44 -04004271static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4272{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004273 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004274 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004275
4276 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4277 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004278 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004279
Yan Zhengd2fb3432008-12-11 16:30:39 -05004280 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004281 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004282
4283 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004284}
4285
Yan, Zhengf0486c62010-05-16 10:46:25 -04004286static int pin_down_extent(struct btrfs_root *root,
4287 struct btrfs_block_group_cache *cache,
4288 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004289{
Yan Zheng11833d62009-09-11 16:11:19 -04004290 spin_lock(&cache->space_info->lock);
4291 spin_lock(&cache->lock);
4292 cache->pinned += num_bytes;
4293 cache->space_info->bytes_pinned += num_bytes;
4294 if (reserved) {
4295 cache->reserved -= num_bytes;
4296 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004297 }
Yan Zheng11833d62009-09-11 16:11:19 -04004298 spin_unlock(&cache->lock);
4299 spin_unlock(&cache->space_info->lock);
4300
Yan, Zhengf0486c62010-05-16 10:46:25 -04004301 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4302 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004303 return 0;
4304}
Chris Mason9078a3e2007-04-26 16:46:15 -04004305
Yan, Zhengf0486c62010-05-16 10:46:25 -04004306/*
4307 * this function must be called within transaction
4308 */
4309int btrfs_pin_extent(struct btrfs_root *root,
4310 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004311{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004312 struct btrfs_block_group_cache *cache;
4313
4314 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4315 BUG_ON(!cache);
4316
4317 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4318
4319 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004320 return 0;
4321}
Zheng Yane8569812008-09-26 10:05:48 -04004322
Josef Bacikfb25e912011-07-26 17:00:46 -04004323/**
4324 * btrfs_update_reserved_bytes - update the block_group and space info counters
4325 * @cache: The cache we are manipulating
4326 * @num_bytes: The number of bytes in question
4327 * @reserve: One of the reservation enums
4328 *
4329 * This is called by the allocator when it reserves space, or by somebody who is
4330 * freeing space that was never actually used on disk. For example if you
4331 * reserve some space for a new leaf in transaction A and before transaction A
4332 * commits you free that leaf, you call this with reserve set to 0 in order to
4333 * clear the reservation.
4334 *
4335 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4336 * ENOSPC accounting. For data we handle the reservation through clearing the
4337 * delalloc bits in the io_tree. We have to do this since we could end up
4338 * allocating less disk space for the amount of data we have reserved in the
4339 * case of compression.
4340 *
4341 * If this is a reservation and the block group has become read only we cannot
4342 * make the reservation and return -EAGAIN, otherwise this function always
4343 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04004344 */
Josef Bacikfb25e912011-07-26 17:00:46 -04004345static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4346 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004347{
Josef Bacikfb25e912011-07-26 17:00:46 -04004348 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004349 int ret = 0;
Josef Bacikfb25e912011-07-26 17:00:46 -04004350 spin_lock(&space_info->lock);
4351 spin_lock(&cache->lock);
4352 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004353 if (cache->ro) {
4354 ret = -EAGAIN;
4355 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04004356 cache->reserved += num_bytes;
4357 space_info->bytes_reserved += num_bytes;
4358 if (reserve == RESERVE_ALLOC) {
4359 BUG_ON(space_info->bytes_may_use < num_bytes);
4360 space_info->bytes_may_use -= num_bytes;
4361 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004362 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004363 } else {
4364 if (cache->ro)
4365 space_info->bytes_readonly += num_bytes;
4366 cache->reserved -= num_bytes;
4367 space_info->bytes_reserved -= num_bytes;
4368 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004369 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004370 spin_unlock(&cache->lock);
4371 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004372 return ret;
4373}
4374
Yan Zheng11833d62009-09-11 16:11:19 -04004375int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4376 struct btrfs_root *root)
4377{
4378 struct btrfs_fs_info *fs_info = root->fs_info;
4379 struct btrfs_caching_control *next;
4380 struct btrfs_caching_control *caching_ctl;
4381 struct btrfs_block_group_cache *cache;
4382
4383 down_write(&fs_info->extent_commit_sem);
4384
4385 list_for_each_entry_safe(caching_ctl, next,
4386 &fs_info->caching_block_groups, list) {
4387 cache = caching_ctl->block_group;
4388 if (block_group_cache_done(cache)) {
4389 cache->last_byte_to_unpin = (u64)-1;
4390 list_del_init(&caching_ctl->list);
4391 put_caching_control(caching_ctl);
4392 } else {
4393 cache->last_byte_to_unpin = caching_ctl->progress;
4394 }
4395 }
4396
4397 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4398 fs_info->pinned_extents = &fs_info->freed_extents[1];
4399 else
4400 fs_info->pinned_extents = &fs_info->freed_extents[0];
4401
4402 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004403
4404 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004405 return 0;
4406}
4407
4408static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4409{
4410 struct btrfs_fs_info *fs_info = root->fs_info;
4411 struct btrfs_block_group_cache *cache = NULL;
4412 u64 len;
4413
4414 while (start <= end) {
4415 if (!cache ||
4416 start >= cache->key.objectid + cache->key.offset) {
4417 if (cache)
4418 btrfs_put_block_group(cache);
4419 cache = btrfs_lookup_block_group(fs_info, start);
4420 BUG_ON(!cache);
4421 }
4422
4423 len = cache->key.objectid + cache->key.offset - start;
4424 len = min(len, end + 1 - start);
4425
4426 if (start < cache->last_byte_to_unpin) {
4427 len = min(len, cache->last_byte_to_unpin - start);
4428 btrfs_add_free_space(cache, start, len);
4429 }
Josef Bacik25179202008-10-29 14:49:05 -04004430
Yan, Zhengf0486c62010-05-16 10:46:25 -04004431 start += len;
4432
Josef Bacik25179202008-10-29 14:49:05 -04004433 spin_lock(&cache->space_info->lock);
4434 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004435 cache->pinned -= len;
4436 cache->space_info->bytes_pinned -= len;
Josef Bacik37be25b2011-08-05 10:25:38 -04004437 if (cache->ro)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004438 cache->space_info->bytes_readonly += len;
Josef Bacik25179202008-10-29 14:49:05 -04004439 spin_unlock(&cache->lock);
4440 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004441 }
4442
4443 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004444 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004445 return 0;
4446}
4447
4448int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004449 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004450{
Yan Zheng11833d62009-09-11 16:11:19 -04004451 struct btrfs_fs_info *fs_info = root->fs_info;
4452 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04004453 u64 start;
4454 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05004455 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004456
Yan Zheng11833d62009-09-11 16:11:19 -04004457 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4458 unpin = &fs_info->freed_extents[1];
4459 else
4460 unpin = &fs_info->freed_extents[0];
4461
Chris Masond3977122009-01-05 21:25:51 -05004462 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004463 ret = find_first_extent_bit(unpin, 0, &start, &end,
4464 EXTENT_DIRTY);
4465 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004466 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004467
Li Dongyang5378e602011-03-24 10:24:27 +00004468 if (btrfs_test_opt(root, DISCARD))
4469 ret = btrfs_discard_extent(root, start,
4470 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004471
Chris Mason1a5bc162007-10-15 16:15:26 -04004472 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004473 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004474 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004475 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004476
Chris Masone20d96d2007-03-22 12:13:20 -04004477 return 0;
4478}
4479
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004480static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4481 struct btrfs_root *root,
4482 u64 bytenr, u64 num_bytes, u64 parent,
4483 u64 root_objectid, u64 owner_objectid,
4484 u64 owner_offset, int refs_to_drop,
4485 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004486{
Chris Masone2fa7222007-03-12 16:22:34 -04004487 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004488 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004489 struct btrfs_fs_info *info = root->fs_info;
4490 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004491 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004492 struct btrfs_extent_item *ei;
4493 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004494 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004495 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004496 int extent_slot = 0;
4497 int found_extent = 0;
4498 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004499 u32 item_size;
4500 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004501
Chris Mason5caf2a02007-04-02 11:20:42 -04004502 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004503 if (!path)
4504 return -ENOMEM;
4505
Chris Mason3c12ac72008-04-21 12:01:38 -04004506 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004507 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004508
4509 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4510 BUG_ON(!is_data && refs_to_drop != 1);
4511
4512 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4513 bytenr, num_bytes, parent,
4514 root_objectid, owner_objectid,
4515 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004516 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004517 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004518 while (extent_slot >= 0) {
4519 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004520 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004521 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004522 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004523 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4524 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004525 found_extent = 1;
4526 break;
4527 }
4528 if (path->slots[0] - extent_slot > 5)
4529 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004530 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004531 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004532#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4533 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4534 if (found_extent && item_size < sizeof(*ei))
4535 found_extent = 0;
4536#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004537 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004538 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004539 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004540 NULL, refs_to_drop,
4541 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004542 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004543 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04004544 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004545
4546 key.objectid = bytenr;
4547 key.type = BTRFS_EXTENT_ITEM_KEY;
4548 key.offset = num_bytes;
4549
Zheng Yan31840ae2008-09-23 13:14:14 -04004550 ret = btrfs_search_slot(trans, extent_root,
4551 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004552 if (ret) {
4553 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004554 ", was looking for %llu\n", ret,
4555 (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00004556 if (ret > 0)
4557 btrfs_print_leaf(extent_root,
4558 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004559 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004560 BUG_ON(ret);
4561 extent_slot = path->slots[0];
4562 }
Chris Mason7bb86312007-12-11 09:25:06 -05004563 } else {
4564 btrfs_print_leaf(extent_root, path->nodes[0]);
4565 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004566 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004567 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004568 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004569 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004570 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004571 (unsigned long long)owner_objectid,
4572 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004573 }
Chris Mason5f39d392007-10-15 16:14:19 -04004574
4575 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004576 item_size = btrfs_item_size_nr(leaf, extent_slot);
4577#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4578 if (item_size < sizeof(*ei)) {
4579 BUG_ON(found_extent || extent_slot != path->slots[0]);
4580 ret = convert_extent_item_v0(trans, extent_root, path,
4581 owner_objectid, 0);
4582 BUG_ON(ret < 0);
4583
David Sterbab3b4aa72011-04-21 01:20:15 +02004584 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004585 path->leave_spinning = 1;
4586
4587 key.objectid = bytenr;
4588 key.type = BTRFS_EXTENT_ITEM_KEY;
4589 key.offset = num_bytes;
4590
4591 ret = btrfs_search_slot(trans, extent_root, &key, path,
4592 -1, 1);
4593 if (ret) {
4594 printk(KERN_ERR "umm, got %d back from search"
4595 ", was looking for %llu\n", ret,
4596 (unsigned long long)bytenr);
4597 btrfs_print_leaf(extent_root, path->nodes[0]);
4598 }
4599 BUG_ON(ret);
4600 extent_slot = path->slots[0];
4601 leaf = path->nodes[0];
4602 item_size = btrfs_item_size_nr(leaf, extent_slot);
4603 }
4604#endif
4605 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004606 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004607 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004608 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4609 struct btrfs_tree_block_info *bi;
4610 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4611 bi = (struct btrfs_tree_block_info *)(ei + 1);
4612 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004613 }
4614
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004615 refs = btrfs_extent_refs(leaf, ei);
4616 BUG_ON(refs < refs_to_drop);
4617 refs -= refs_to_drop;
4618
4619 if (refs > 0) {
4620 if (extent_op)
4621 __run_delayed_extent_op(extent_op, leaf, ei);
4622 /*
4623 * In the case of inline back ref, reference count will
4624 * be updated by remove_extent_backref
4625 */
4626 if (iref) {
4627 BUG_ON(!found_extent);
4628 } else {
4629 btrfs_set_extent_refs(leaf, ei, refs);
4630 btrfs_mark_buffer_dirty(leaf);
4631 }
4632 if (found_extent) {
4633 ret = remove_extent_backref(trans, extent_root, path,
4634 iref, refs_to_drop,
4635 is_data);
4636 BUG_ON(ret);
4637 }
4638 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004639 if (found_extent) {
4640 BUG_ON(is_data && refs_to_drop !=
4641 extent_data_ref_count(root, path, iref));
4642 if (iref) {
4643 BUG_ON(path->slots[0] != extent_slot);
4644 } else {
4645 BUG_ON(path->slots[0] != extent_slot + 1);
4646 path->slots[0] = extent_slot;
4647 num_to_del = 2;
4648 }
Chris Mason78fae272007-03-25 11:35:08 -04004649 }
Chris Masonb9473432009-03-13 11:00:37 -04004650
Chris Mason952fcca2008-02-18 16:33:44 -05004651 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4652 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004653 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004654 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01004655
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004656 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004657 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4658 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004659 } else {
4660 invalidate_mapping_pages(info->btree_inode->i_mapping,
4661 bytenr >> PAGE_CACHE_SHIFT,
4662 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004663 }
4664
Yan, Zhengf0486c62010-05-16 10:46:25 -04004665 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004666 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004667 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004668 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004669 return ret;
4670}
4671
4672/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004673 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004674 * delayed ref for that extent as well. This searches the delayed ref tree for
4675 * a given extent, and if there are no other delayed refs to be processed, it
4676 * removes it from the tree.
4677 */
4678static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4679 struct btrfs_root *root, u64 bytenr)
4680{
4681 struct btrfs_delayed_ref_head *head;
4682 struct btrfs_delayed_ref_root *delayed_refs;
4683 struct btrfs_delayed_ref_node *ref;
4684 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004685 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004686
4687 delayed_refs = &trans->transaction->delayed_refs;
4688 spin_lock(&delayed_refs->lock);
4689 head = btrfs_find_delayed_ref_head(trans, bytenr);
4690 if (!head)
4691 goto out;
4692
4693 node = rb_prev(&head->node.rb_node);
4694 if (!node)
4695 goto out;
4696
4697 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4698
4699 /* there are still entries for this ref, we can't drop it */
4700 if (ref->bytenr == bytenr)
4701 goto out;
4702
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004703 if (head->extent_op) {
4704 if (!head->must_insert_reserved)
4705 goto out;
4706 kfree(head->extent_op);
4707 head->extent_op = NULL;
4708 }
4709
Chris Mason1887be62009-03-13 10:11:24 -04004710 /*
4711 * waiting for the lock here would deadlock. If someone else has it
4712 * locked they are already in the process of dropping it anyway
4713 */
4714 if (!mutex_trylock(&head->mutex))
4715 goto out;
4716
4717 /*
4718 * at this point we have a head with no other entries. Go
4719 * ahead and process it.
4720 */
4721 head->node.in_tree = 0;
4722 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004723
Chris Mason1887be62009-03-13 10:11:24 -04004724 delayed_refs->num_entries--;
4725
4726 /*
4727 * we don't take a ref on the node because we're removing it from the
4728 * tree, so we just steal the ref the tree was holding.
4729 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004730 delayed_refs->num_heads--;
4731 if (list_empty(&head->cluster))
4732 delayed_refs->num_heads_ready--;
4733
4734 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004735 spin_unlock(&delayed_refs->lock);
4736
Yan, Zhengf0486c62010-05-16 10:46:25 -04004737 BUG_ON(head->extent_op);
4738 if (head->must_insert_reserved)
4739 ret = 1;
4740
4741 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004742 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004743 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004744out:
4745 spin_unlock(&delayed_refs->lock);
4746 return 0;
4747}
4748
Yan, Zhengf0486c62010-05-16 10:46:25 -04004749void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4750 struct btrfs_root *root,
4751 struct extent_buffer *buf,
4752 u64 parent, int last_ref)
4753{
4754 struct btrfs_block_rsv *block_rsv;
4755 struct btrfs_block_group_cache *cache = NULL;
4756 int ret;
4757
4758 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4759 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4760 parent, root->root_key.objectid,
4761 btrfs_header_level(buf),
4762 BTRFS_DROP_DELAYED_REF, NULL);
4763 BUG_ON(ret);
4764 }
4765
4766 if (!last_ref)
4767 return;
4768
4769 block_rsv = get_block_rsv(trans, root);
4770 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004771 if (block_rsv->space_info != cache->space_info)
4772 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004773
4774 if (btrfs_header_generation(buf) == trans->transid) {
4775 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4776 ret = check_ref_cleanup(trans, root, buf->start);
4777 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04004778 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004779 }
4780
4781 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4782 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04004783 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004784 }
4785
4786 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4787
4788 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04004789 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004790 }
4791out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004792 /*
4793 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4794 * anymore.
4795 */
4796 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004797 btrfs_put_block_group(cache);
4798}
4799
Chris Mason925baed2008-06-25 16:01:30 -04004800int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004801 struct btrfs_root *root,
4802 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004803 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004804{
4805 int ret;
4806
Chris Mason56bec292009-03-13 10:10:06 -04004807 /*
4808 * tree log blocks never actually go into the extent allocation
4809 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004810 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004811 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4812 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004813 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004814 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004815 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004816 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4817 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4818 parent, root_objectid, (int)owner,
4819 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004820 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004821 } else {
4822 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4823 parent, root_objectid, owner,
4824 offset, BTRFS_DROP_DELAYED_REF, NULL);
4825 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004826 }
Chris Mason925baed2008-06-25 16:01:30 -04004827 return ret;
4828}
4829
Chris Mason87ee04e2007-11-30 11:30:34 -05004830static u64 stripe_align(struct btrfs_root *root, u64 val)
4831{
4832 u64 mask = ((u64)root->stripesize - 1);
4833 u64 ret = (val + mask) & ~mask;
4834 return ret;
4835}
4836
Chris Masonfec577f2007-02-26 10:40:21 -05004837/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004838 * when we wait for progress in the block group caching, its because
4839 * our allocation attempt failed at least once. So, we must sleep
4840 * and let some progress happen before we try again.
4841 *
4842 * This function will sleep at least once waiting for new free space to
4843 * show up, and then it will check the block group free space numbers
4844 * for our min num_bytes. Another option is to have it go ahead
4845 * and look in the rbtree for a free extent of a given size, but this
4846 * is a good start.
4847 */
4848static noinline int
4849wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4850 u64 num_bytes)
4851{
Yan Zheng11833d62009-09-11 16:11:19 -04004852 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004853 DEFINE_WAIT(wait);
4854
Yan Zheng11833d62009-09-11 16:11:19 -04004855 caching_ctl = get_caching_control(cache);
4856 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004857 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004858
Yan Zheng11833d62009-09-11 16:11:19 -04004859 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08004860 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004861
4862 put_caching_control(caching_ctl);
4863 return 0;
4864}
4865
4866static noinline int
4867wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4868{
4869 struct btrfs_caching_control *caching_ctl;
4870 DEFINE_WAIT(wait);
4871
4872 caching_ctl = get_caching_control(cache);
4873 if (!caching_ctl)
4874 return 0;
4875
4876 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4877
4878 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004879 return 0;
4880}
4881
Yan, Zhengb742bb82010-05-16 10:46:24 -04004882static int get_block_group_index(struct btrfs_block_group_cache *cache)
4883{
4884 int index;
4885 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4886 index = 0;
4887 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4888 index = 1;
4889 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4890 index = 2;
4891 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4892 index = 3;
4893 else
4894 index = 4;
4895 return index;
4896}
4897
Josef Bacik817d52f2009-07-13 21:29:25 -04004898enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004899 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004900 LOOP_CACHING_NOWAIT = 1,
4901 LOOP_CACHING_WAIT = 2,
4902 LOOP_ALLOC_CHUNK = 3,
4903 LOOP_NO_EMPTY_SIZE = 4,
4904};
4905
4906/*
Chris Masonfec577f2007-02-26 10:40:21 -05004907 * walks the btree of allocated extents and find a hole of a given size.
4908 * The key ins is changed to record the hole:
4909 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004910 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004911 * ins->offset == number of blocks
4912 * Any available blocks before search_start are skipped.
4913 */
Chris Masond3977122009-01-05 21:25:51 -05004914static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004915 struct btrfs_root *orig_root,
4916 u64 num_bytes, u64 empty_size,
4917 u64 search_start, u64 search_end,
4918 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00004919 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004920{
Josef Bacik80eb2342008-10-29 14:49:05 -04004921 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004922 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004923 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004924 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004925 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004926 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004927 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004928 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004929 int last_ptr_loop = 0;
4930 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004931 int index = 0;
Josef Bacikfb25e912011-07-26 17:00:46 -04004932 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
4933 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04004934 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004935 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004936 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004937 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004938 u64 ideal_cache_percent = 0;
4939 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004940
Chris Masondb945352007-10-15 16:15:53 -04004941 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004942 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004943 ins->objectid = 0;
4944 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004945
Josef Bacik2552d172009-04-03 10:14:19 -04004946 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004947 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00004948 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004949 return -ENOSPC;
4950 }
Josef Bacik2552d172009-04-03 10:14:19 -04004951
Josef Bacik67377732010-09-16 16:19:09 -04004952 /*
4953 * If the space info is for both data and metadata it means we have a
4954 * small filesystem and we can't use the clustering stuff.
4955 */
4956 if (btrfs_mixed_space_info(space_info))
4957 use_cluster = false;
4958
Chris Mason0ef3e662008-05-24 14:04:53 -04004959 if (orig_root->ref_cows || empty_size)
4960 allowed_chunk_alloc = 1;
4961
Josef Bacik67377732010-09-16 16:19:09 -04004962 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004963 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004964 if (!btrfs_test_opt(root, SSD))
4965 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004966 }
4967
Josef Bacik67377732010-09-16 16:19:09 -04004968 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4969 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004970 last_ptr = &root->fs_info->data_alloc_cluster;
4971 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004972
Chris Mason239b14b2008-03-24 15:02:07 -04004973 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04004974 spin_lock(&last_ptr->lock);
4975 if (last_ptr->block_group)
4976 hint_byte = last_ptr->window_start;
4977 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004978 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004979
Chris Masona061fc82008-05-07 11:43:44 -04004980 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004981 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004982
Josef Bacik817d52f2009-07-13 21:29:25 -04004983 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04004984 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004985
Josef Bacik2552d172009-04-03 10:14:19 -04004986 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004987ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004988 block_group = btrfs_lookup_block_group(root->fs_info,
4989 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004990 /*
4991 * we don't want to use the block group if it doesn't match our
4992 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004993 *
4994 * However if we are re-searching with an ideal block group
4995 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004996 */
4997 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004998 (block_group->cached != BTRFS_CACHE_NO ||
4999 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04005000 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005001 if (list_empty(&block_group->list) ||
5002 block_group->ro) {
5003 /*
5004 * someone is removing this block group,
5005 * we can't jump into the have_block_group
5006 * target because our list pointers are not
5007 * valid
5008 */
5009 btrfs_put_block_group(block_group);
5010 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005011 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005012 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005013 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005014 }
Josef Bacik2552d172009-04-03 10:14:19 -04005015 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005016 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005017 }
Chris Mason42e70e72008-11-07 18:17:11 -05005018 }
Josef Bacik2552d172009-04-03 10:14:19 -04005019search:
Josef Bacik80eb2342008-10-29 14:49:05 -04005020 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005021 list_for_each_entry(block_group, &space_info->block_groups[index],
5022 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005023 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005024 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005025
Josef Bacik11dfe352009-11-13 20:12:59 +00005026 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005027 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005028
Chris Mason83a50de2010-12-13 15:06:46 -05005029 /*
5030 * this can happen if we end up cycling through all the
5031 * raid types, but we want to make sure we only allocate
5032 * for the proper type.
5033 */
5034 if (!block_group_bits(block_group, data)) {
5035 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5036 BTRFS_BLOCK_GROUP_RAID1 |
5037 BTRFS_BLOCK_GROUP_RAID10;
5038
5039 /*
5040 * if they asked for extra copies and this block group
5041 * doesn't provide them, bail. This does allow us to
5042 * fill raid0 from raid1.
5043 */
5044 if ((data & extra) && !(block_group->flags & extra))
5045 goto loop;
5046 }
5047
Josef Bacik2552d172009-04-03 10:14:19 -04005048have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04005049 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005050 u64 free_percent;
5051
Josef Bacikb8399de2010-12-08 09:15:11 -05005052 ret = cache_block_group(block_group, trans,
5053 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04005054 if (block_group->cached == BTRFS_CACHE_FINISHED)
5055 goto have_block_group;
5056
Josef Bacikccf0e722009-11-10 21:23:48 -05005057 free_percent = btrfs_block_group_used(&block_group->item);
5058 free_percent *= 100;
5059 free_percent = div64_u64(free_percent,
5060 block_group->key.offset);
5061 free_percent = 100 - free_percent;
5062 if (free_percent > ideal_cache_percent &&
5063 likely(!block_group->ro)) {
5064 ideal_cache_offset = block_group->key.objectid;
5065 ideal_cache_percent = free_percent;
5066 }
5067
Josef Bacik817d52f2009-07-13 21:29:25 -04005068 /*
Josef Bacikbab39bf2011-06-30 14:42:28 -04005069 * The caching workers are limited to 2 threads, so we
5070 * can queue as much work as we care to.
Josef Bacik817d52f2009-07-13 21:29:25 -04005071 */
Josef Bacikbab39bf2011-06-30 14:42:28 -04005072 if (loop > LOOP_FIND_IDEAL) {
Josef Bacikb8399de2010-12-08 09:15:11 -05005073 ret = cache_block_group(block_group, trans,
5074 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005075 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005076 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005077 found_uncached_bg = true;
5078
5079 /*
5080 * If loop is set for cached only, try the next block
5081 * group.
5082 */
5083 if (loop == LOOP_FIND_IDEAL)
5084 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005085 }
5086
Josef Bacik817d52f2009-07-13 21:29:25 -04005087 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005088 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005089 found_uncached_bg = true;
5090
Josef Bacikea6a4782008-11-20 12:16:16 -05005091 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005092 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005093
Chris Masonff5714c2011-05-28 07:00:39 -04005094 spin_lock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005095 if (cached &&
Chris Masonff5714c2011-05-28 07:00:39 -04005096 block_group->free_space_ctl->free_space <
5097 num_bytes + empty_size) {
5098 spin_unlock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005099 goto loop;
5100 }
Chris Masonff5714c2011-05-28 07:00:39 -04005101 spin_unlock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005102
Josef Bacik0a243252009-09-11 16:11:20 -04005103 /*
5104 * Ok we want to try and use the cluster allocator, so lets look
5105 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5106 * have tried the cluster allocator plenty of times at this
5107 * point and not have found anything, so we are likely way too
5108 * fragmented for the clustering stuff to find anything, so lets
5109 * just skip it and let the allocator find whatever block it can
5110 * find
5111 */
5112 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005113 /*
5114 * the refill lock keeps out other
5115 * people trying to start a new cluster
5116 */
5117 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005118 if (last_ptr->block_group &&
5119 (last_ptr->block_group->ro ||
5120 !block_group_bits(last_ptr->block_group, data))) {
5121 offset = 0;
5122 goto refill_cluster;
5123 }
5124
Chris Masonfa9c0d72009-04-03 09:47:43 -04005125 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5126 num_bytes, search_start);
5127 if (offset) {
5128 /* we have a block, we're done */
5129 spin_unlock(&last_ptr->refill_lock);
5130 goto checks;
5131 }
5132
5133 spin_lock(&last_ptr->lock);
5134 /*
5135 * whoops, this cluster doesn't actually point to
5136 * this block group. Get a ref on the block
5137 * group is does point to and try again
5138 */
5139 if (!last_ptr_loop && last_ptr->block_group &&
liuboff1f2b42011-07-27 09:49:18 +00005140 last_ptr->block_group != block_group &&
5141 index <=
5142 get_block_group_index(last_ptr->block_group)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005143
5144 btrfs_put_block_group(block_group);
5145 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005146 btrfs_get_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005147 spin_unlock(&last_ptr->lock);
5148 spin_unlock(&last_ptr->refill_lock);
5149
5150 last_ptr_loop = 1;
5151 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005152 /*
5153 * we know this block group is properly
5154 * in the list because
5155 * btrfs_remove_block_group, drops the
5156 * cluster before it removes the block
5157 * group from the list
5158 */
Chris Masonfa9c0d72009-04-03 09:47:43 -04005159 goto have_block_group;
5160 }
5161 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005162refill_cluster:
Chris Masonfa9c0d72009-04-03 09:47:43 -04005163 /*
5164 * this cluster didn't work out, free it and
5165 * start over
5166 */
5167 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5168
5169 last_ptr_loop = 0;
5170
5171 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005172 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005173 block_group, last_ptr,
5174 offset, num_bytes,
5175 empty_cluster + empty_size);
5176 if (ret == 0) {
5177 /*
5178 * now pull our allocation out of this
5179 * cluster
5180 */
5181 offset = btrfs_alloc_from_cluster(block_group,
5182 last_ptr, num_bytes,
5183 search_start);
5184 if (offset) {
5185 /* we found one, proceed */
5186 spin_unlock(&last_ptr->refill_lock);
5187 goto checks;
5188 }
Josef Bacik0a243252009-09-11 16:11:20 -04005189 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5190 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005191 spin_unlock(&last_ptr->refill_lock);
5192
Josef Bacik0a243252009-09-11 16:11:20 -04005193 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005194 wait_block_group_cache_progress(block_group,
5195 num_bytes + empty_cluster + empty_size);
5196 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005197 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005198
Chris Masonfa9c0d72009-04-03 09:47:43 -04005199 /*
5200 * at this point we either didn't find a cluster
5201 * or we weren't able to allocate a block from our
5202 * cluster. Free the cluster we've been trying
5203 * to use, and go to the next block group
5204 */
Josef Bacik0a243252009-09-11 16:11:20 -04005205 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005206 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005207 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005208 }
5209
Josef Bacik6226cb02009-04-03 10:14:18 -04005210 offset = btrfs_find_space_for_alloc(block_group, search_start,
5211 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005212 /*
5213 * If we didn't find a chunk, and we haven't failed on this
5214 * block group before, and this block group is in the middle of
5215 * caching and we are ok with waiting, then go ahead and wait
5216 * for progress to be made, and set failed_alloc to true.
5217 *
5218 * If failed_alloc is true then we've already waited on this
5219 * block group once and should move on to the next block group.
5220 */
5221 if (!offset && !failed_alloc && !cached &&
5222 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005223 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005224 num_bytes + empty_size);
5225 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005226 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005227 } else if (!offset) {
5228 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005229 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005230checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005231 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005232 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005233 if (search_start + num_bytes >= search_end) {
5234 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005235 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005236 }
Chris Masone37c9e62007-05-09 20:13:14 -04005237
Josef Bacik2552d172009-04-03 10:14:19 -04005238 /* move on to the next group */
5239 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005240 block_group->key.objectid + block_group->key.offset) {
5241 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005242 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005243 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005244
Josef Bacik2552d172009-04-03 10:14:19 -04005245 ins->objectid = search_start;
5246 ins->offset = num_bytes;
5247
Josef Bacik6226cb02009-04-03 10:14:18 -04005248 if (offset < search_start)
5249 btrfs_add_free_space(block_group, offset,
5250 search_start - offset);
5251 BUG_ON(offset > search_start);
5252
Josef Bacikfb25e912011-07-26 17:00:46 -04005253 ret = btrfs_update_reserved_bytes(block_group, num_bytes,
5254 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005255 if (ret == -EAGAIN) {
5256 btrfs_add_free_space(block_group, offset, num_bytes);
5257 goto loop;
5258 }
Yan Zheng11833d62009-09-11 16:11:19 -04005259
Josef Bacik2552d172009-04-03 10:14:19 -04005260 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005261 ins->objectid = search_start;
5262 ins->offset = num_bytes;
5263
5264 if (offset < search_start)
5265 btrfs_add_free_space(block_group, offset,
5266 search_start - offset);
5267 BUG_ON(offset > search_start);
Josef Bacikd82a6f12011-05-11 15:26:06 -04005268 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005269 break;
5270loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005271 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005272 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005273 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d72009-04-03 09:47:43 -04005274 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005275 }
5276 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005277
Yan, Zhengb742bb82010-05-16 10:46:24 -04005278 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5279 goto search;
5280
Josef Bacikccf0e722009-11-10 21:23:48 -05005281 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5282 * for them to make caching progress. Also
5283 * determine the best possible bg to cache
5284 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5285 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005286 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5287 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5288 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5289 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04005290 */
Josef Bacik723bda22011-05-27 16:11:38 -04005291 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005292 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005293 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005294 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005295 loop++;
Josef Bacikbab39bf2011-06-30 14:42:28 -04005296 if (!ideal_cache_percent)
Josef Bacik817d52f2009-07-13 21:29:25 -04005297 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005298
5299 /*
5300 * 1 of the following 2 things have happened so far
5301 *
5302 * 1) We found an ideal block group for caching that
5303 * is mostly full and will cache quickly, so we might
5304 * as well wait for it.
5305 *
5306 * 2) We searched for cached only and we didn't find
5307 * anything, and we didn't start any caching kthreads
5308 * either, so chances are we will loop through and
5309 * start a couple caching kthreads, and then come back
5310 * around and just wait for them. This will be slower
5311 * because we will have 2 caching kthreads reading at
5312 * the same time when we could have just started one
5313 * and waited for it to get far enough to give us an
5314 * allocation, so go ahead and go to the wait caching
5315 * loop.
5316 */
5317 loop = LOOP_CACHING_WAIT;
5318 search_start = ideal_cache_offset;
5319 ideal_cache_percent = 0;
5320 goto ideal_cache;
5321 } else if (loop == LOOP_FIND_IDEAL) {
5322 /*
5323 * Didn't find a uncached bg, wait on anything we find
5324 * next.
5325 */
5326 loop = LOOP_CACHING_WAIT;
5327 goto search;
5328 }
5329
Josef Bacik723bda22011-05-27 16:11:38 -04005330 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04005331
5332 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik723bda22011-05-27 16:11:38 -04005333 if (allowed_chunk_alloc) {
5334 ret = do_chunk_alloc(trans, root, num_bytes +
5335 2 * 1024 * 1024, data,
5336 CHUNK_ALLOC_LIMITED);
5337 allowed_chunk_alloc = 0;
5338 if (ret == 1)
5339 done_chunk_alloc = 1;
5340 } else if (!done_chunk_alloc &&
5341 space_info->force_alloc ==
5342 CHUNK_ALLOC_NO_FORCE) {
5343 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
5344 }
5345
5346 /*
5347 * We didn't allocate a chunk, go ahead and drop the
5348 * empty size and loop again.
5349 */
5350 if (!done_chunk_alloc)
5351 loop = LOOP_NO_EMPTY_SIZE;
5352 }
5353
5354 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005355 empty_size = 0;
5356 empty_cluster = 0;
5357 }
Chris Mason42e70e72008-11-07 18:17:11 -05005358
Josef Bacik723bda22011-05-27 16:11:38 -04005359 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04005360 } else if (!ins->objectid) {
5361 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04005362 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005363 ret = 0;
5364 }
Chris Mason0b86a832008-03-24 15:01:56 -04005365
Chris Mason0f70abe2007-02-28 16:46:22 -05005366 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005367}
Chris Masonec44a352008-04-28 15:29:52 -04005368
Josef Bacik9ed74f22009-09-11 16:12:44 -04005369static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5370 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005371{
5372 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005373 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005374
Josef Bacik9ed74f22009-09-11 16:12:44 -04005375 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04005376 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
5377 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05005378 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005379 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005380 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005381 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005382 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5383 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005384 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005385 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005386 (unsigned long long)info->bytes_pinned,
5387 (unsigned long long)info->bytes_reserved,
5388 (unsigned long long)info->bytes_may_use,
5389 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005390 spin_unlock(&info->lock);
5391
5392 if (!dump_block_groups)
5393 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005394
Josef Bacik80eb2342008-10-29 14:49:05 -04005395 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005396again:
5397 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005398 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005399 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5400 "%llu pinned %llu reserved\n",
5401 (unsigned long long)cache->key.objectid,
5402 (unsigned long long)cache->key.offset,
5403 (unsigned long long)btrfs_block_group_used(&cache->item),
5404 (unsigned long long)cache->pinned,
5405 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005406 btrfs_dump_free_space(cache, bytes);
5407 spin_unlock(&cache->lock);
5408 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005409 if (++index < BTRFS_NR_RAID_TYPES)
5410 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005411 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005412}
Zheng Yane8569812008-09-26 10:05:48 -04005413
Yan Zheng11833d62009-09-11 16:11:19 -04005414int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5415 struct btrfs_root *root,
5416 u64 num_bytes, u64 min_alloc_size,
5417 u64 empty_size, u64 hint_byte,
5418 u64 search_end, struct btrfs_key *ins,
5419 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005420{
5421 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005422 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005423
Josef Bacik6a632092009-02-20 11:00:09 -05005424 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005425again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005426 /*
5427 * the only place that sets empty_size is btrfs_realloc_node, which
5428 * is not called recursively on allocations
5429 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005430 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005431 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005432 num_bytes + 2 * 1024 * 1024, data,
5433 CHUNK_ALLOC_NO_FORCE);
Chris Mason0b86a832008-03-24 15:01:56 -04005434
Chris Masondb945352007-10-15 16:15:53 -04005435 WARN_ON(num_bytes < root->sectorsize);
5436 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005437 search_start, search_end, hint_byte,
5438 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005439
Chris Mason98d20f62008-04-14 09:46:10 -04005440 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5441 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005442 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005443 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005444 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005445 num_bytes, data, CHUNK_ALLOC_FORCE);
Chris Mason98d20f62008-04-14 09:46:10 -04005446 goto again;
5447 }
Chris Mason91435652011-02-16 13:10:41 -05005448 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005449 struct btrfs_space_info *sinfo;
5450
5451 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005452 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5453 "wanted %llu\n", (unsigned long long)data,
5454 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005455 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005456 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005457
liubo1abe9b82011-03-24 11:18:59 +00005458 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5459
Josef Bacik0f9dd462008-09-23 13:14:11 -04005460 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005461}
5462
Chris Mason65b51a02008-08-01 15:11:20 -04005463int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5464{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005465 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005466 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005467
Josef Bacik0f9dd462008-09-23 13:14:11 -04005468 cache = btrfs_lookup_block_group(root->fs_info, start);
5469 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005470 printk(KERN_ERR "Unable to find block group for %llu\n",
5471 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005472 return -ENOSPC;
5473 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005474
Li Dongyang5378e602011-03-24 10:24:27 +00005475 if (btrfs_test_opt(root, DISCARD))
5476 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005477
Josef Bacik0f9dd462008-09-23 13:14:11 -04005478 btrfs_add_free_space(cache, start, len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005479 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005480 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005481
liubo1abe9b82011-03-24 11:18:59 +00005482 trace_btrfs_reserved_extent_free(root, start, len);
5483
Chris Masone6dcd2d2008-07-17 12:53:50 -04005484 return ret;
5485}
5486
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005487static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5488 struct btrfs_root *root,
5489 u64 parent, u64 root_objectid,
5490 u64 flags, u64 owner, u64 offset,
5491 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005492{
5493 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005494 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005495 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005496 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005497 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005498 struct extent_buffer *leaf;
5499 int type;
5500 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005501
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005502 if (parent > 0)
5503 type = BTRFS_SHARED_DATA_REF_KEY;
5504 else
5505 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005506
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005507 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005508
5509 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005510 if (!path)
5511 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005512
Chris Masonb9473432009-03-13 11:00:37 -04005513 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005514 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5515 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005516 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005517
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005518 leaf = path->nodes[0];
5519 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005520 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005521 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5522 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5523 btrfs_set_extent_flags(leaf, extent_item,
5524 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005525
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005526 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5527 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5528 if (parent > 0) {
5529 struct btrfs_shared_data_ref *ref;
5530 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5531 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5532 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5533 } else {
5534 struct btrfs_extent_data_ref *ref;
5535 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5536 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5537 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5538 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5539 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5540 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005541
5542 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005543 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005544
Yan, Zhengf0486c62010-05-16 10:46:25 -04005545 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005546 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005547 printk(KERN_ERR "btrfs update block group failed for %llu "
5548 "%llu\n", (unsigned long long)ins->objectid,
5549 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005550 BUG();
5551 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005552 return ret;
5553}
5554
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005555static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5556 struct btrfs_root *root,
5557 u64 parent, u64 root_objectid,
5558 u64 flags, struct btrfs_disk_key *key,
5559 int level, struct btrfs_key *ins)
5560{
5561 int ret;
5562 struct btrfs_fs_info *fs_info = root->fs_info;
5563 struct btrfs_extent_item *extent_item;
5564 struct btrfs_tree_block_info *block_info;
5565 struct btrfs_extent_inline_ref *iref;
5566 struct btrfs_path *path;
5567 struct extent_buffer *leaf;
5568 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5569
5570 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005571 if (!path)
5572 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005573
5574 path->leave_spinning = 1;
5575 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5576 ins, size);
5577 BUG_ON(ret);
5578
5579 leaf = path->nodes[0];
5580 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5581 struct btrfs_extent_item);
5582 btrfs_set_extent_refs(leaf, extent_item, 1);
5583 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5584 btrfs_set_extent_flags(leaf, extent_item,
5585 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5586 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5587
5588 btrfs_set_tree_block_key(leaf, block_info, key);
5589 btrfs_set_tree_block_level(leaf, block_info, level);
5590
5591 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5592 if (parent > 0) {
5593 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5594 btrfs_set_extent_inline_ref_type(leaf, iref,
5595 BTRFS_SHARED_BLOCK_REF_KEY);
5596 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5597 } else {
5598 btrfs_set_extent_inline_ref_type(leaf, iref,
5599 BTRFS_TREE_BLOCK_REF_KEY);
5600 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5601 }
5602
5603 btrfs_mark_buffer_dirty(leaf);
5604 btrfs_free_path(path);
5605
Yan, Zhengf0486c62010-05-16 10:46:25 -04005606 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 if (ret) {
5608 printk(KERN_ERR "btrfs update block group failed for %llu "
5609 "%llu\n", (unsigned long long)ins->objectid,
5610 (unsigned long long)ins->offset);
5611 BUG();
5612 }
5613 return ret;
5614}
5615
5616int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5617 struct btrfs_root *root,
5618 u64 root_objectid, u64 owner,
5619 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005620{
5621 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005622
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005623 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005624
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005625 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5626 0, root_objectid, owner, offset,
5627 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005628 return ret;
5629}
Chris Masone02119d2008-09-05 16:13:11 -04005630
5631/*
5632 * this is used by the tree logging recovery code. It records that
5633 * an extent has been allocated and makes sure to clear the free
5634 * space cache bits as well
5635 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005636int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5637 struct btrfs_root *root,
5638 u64 root_objectid, u64 owner, u64 offset,
5639 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005640{
5641 int ret;
5642 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005643 struct btrfs_caching_control *caching_ctl;
5644 u64 start = ins->objectid;
5645 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005646
Chris Masone02119d2008-09-05 16:13:11 -04005647 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005648 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005649 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005650
Yan Zheng11833d62009-09-11 16:11:19 -04005651 if (!caching_ctl) {
5652 BUG_ON(!block_group_cache_done(block_group));
5653 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5654 BUG_ON(ret);
5655 } else {
5656 mutex_lock(&caching_ctl->mutex);
5657
5658 if (start >= caching_ctl->progress) {
5659 ret = add_excluded_extent(root, start, num_bytes);
5660 BUG_ON(ret);
5661 } else if (start + num_bytes <= caching_ctl->progress) {
5662 ret = btrfs_remove_free_space(block_group,
5663 start, num_bytes);
5664 BUG_ON(ret);
5665 } else {
5666 num_bytes = caching_ctl->progress - start;
5667 ret = btrfs_remove_free_space(block_group,
5668 start, num_bytes);
5669 BUG_ON(ret);
5670
5671 start = caching_ctl->progress;
5672 num_bytes = ins->objectid + ins->offset -
5673 caching_ctl->progress;
5674 ret = add_excluded_extent(root, start, num_bytes);
5675 BUG_ON(ret);
5676 }
5677
5678 mutex_unlock(&caching_ctl->mutex);
5679 put_caching_control(caching_ctl);
5680 }
5681
Josef Bacikfb25e912011-07-26 17:00:46 -04005682 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
5683 RESERVE_ALLOC_NO_ACCOUNT);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005684 BUG_ON(ret);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005685 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005686 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5687 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005688 return ret;
5689}
5690
Chris Mason65b51a02008-08-01 15:11:20 -04005691struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5692 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005693 u64 bytenr, u32 blocksize,
5694 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005695{
5696 struct extent_buffer *buf;
5697
5698 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5699 if (!buf)
5700 return ERR_PTR(-ENOMEM);
5701 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04005702 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005703 btrfs_tree_lock(buf);
5704 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005705
5706 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005707 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005708
Chris Masond0c803c2008-09-11 16:17:57 -04005709 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005710 /*
5711 * we allow two log transactions at a time, use different
5712 * EXENT bit to differentiate dirty pages.
5713 */
5714 if (root->log_transid % 2 == 0)
5715 set_extent_dirty(&root->dirty_log_pages, buf->start,
5716 buf->start + buf->len - 1, GFP_NOFS);
5717 else
5718 set_extent_new(&root->dirty_log_pages, buf->start,
5719 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005720 } else {
5721 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5722 buf->start + buf->len - 1, GFP_NOFS);
5723 }
Chris Mason65b51a02008-08-01 15:11:20 -04005724 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005725 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005726 return buf;
5727}
5728
Yan, Zhengf0486c62010-05-16 10:46:25 -04005729static struct btrfs_block_rsv *
5730use_block_rsv(struct btrfs_trans_handle *trans,
5731 struct btrfs_root *root, u32 blocksize)
5732{
5733 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005734 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005735 int ret;
5736
5737 block_rsv = get_block_rsv(trans, root);
5738
5739 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005740 ret = reserve_metadata_bytes(trans, root, block_rsv,
5741 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005742 /*
5743 * If we couldn't reserve metadata bytes try and use some from
5744 * the global reserve.
5745 */
5746 if (ret && block_rsv != global_rsv) {
5747 ret = block_rsv_use_bytes(global_rsv, blocksize);
5748 if (!ret)
5749 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005750 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005751 } else if (ret) {
5752 return ERR_PTR(ret);
5753 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005754 return block_rsv;
5755 }
5756
5757 ret = block_rsv_use_bytes(block_rsv, blocksize);
5758 if (!ret)
5759 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005760 if (ret) {
5761 WARN_ON(1);
5762 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5763 0);
5764 if (!ret) {
5765 spin_lock(&block_rsv->lock);
5766 block_rsv->size += blocksize;
5767 spin_unlock(&block_rsv->lock);
5768 return block_rsv;
5769 } else if (ret && block_rsv != global_rsv) {
5770 ret = block_rsv_use_bytes(global_rsv, blocksize);
5771 if (!ret)
5772 return global_rsv;
5773 }
5774 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005775
Yan, Zhengf0486c62010-05-16 10:46:25 -04005776 return ERR_PTR(-ENOSPC);
5777}
5778
5779static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5780{
5781 block_rsv_add_bytes(block_rsv, blocksize, 0);
5782 block_rsv_release_bytes(block_rsv, NULL, 0);
5783}
5784
Chris Masonfec577f2007-02-26 10:40:21 -05005785/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005786 * finds a free extent and does all the dirty work required for allocation
5787 * returns the key for the extent through ins, and a tree buffer for
5788 * the first block of the extent through buf.
5789 *
Chris Masonfec577f2007-02-26 10:40:21 -05005790 * returns the tree buffer or NULL.
5791 */
Chris Mason5f39d392007-10-15 16:14:19 -04005792struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005793 struct btrfs_root *root, u32 blocksize,
5794 u64 parent, u64 root_objectid,
5795 struct btrfs_disk_key *key, int level,
5796 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005797{
Chris Masone2fa7222007-03-12 16:22:34 -04005798 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005799 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005800 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005801 u64 flags = 0;
5802 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005803
Yan, Zhengf0486c62010-05-16 10:46:25 -04005804
5805 block_rsv = use_block_rsv(trans, root, blocksize);
5806 if (IS_ERR(block_rsv))
5807 return ERR_CAST(block_rsv);
5808
5809 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5810 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005811 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005812 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005813 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005814 }
Chris Mason55c69072008-01-09 15:55:33 -05005815
Chris Mason4008c042009-02-12 14:09:45 -05005816 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5817 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005818 BUG_ON(IS_ERR(buf));
5819
5820 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5821 if (parent == 0)
5822 parent = ins.objectid;
5823 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5824 } else
5825 BUG_ON(parent > 0);
5826
5827 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5828 struct btrfs_delayed_extent_op *extent_op;
5829 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5830 BUG_ON(!extent_op);
5831 if (key)
5832 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5833 else
5834 memset(&extent_op->key, 0, sizeof(extent_op->key));
5835 extent_op->flags_to_set = flags;
5836 extent_op->update_key = 1;
5837 extent_op->update_flags = 1;
5838 extent_op->is_data = 0;
5839
5840 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5841 ins.offset, parent, root_objectid,
5842 level, BTRFS_ADD_DELAYED_EXTENT,
5843 extent_op);
5844 BUG_ON(ret);
5845 }
Chris Masonfec577f2007-02-26 10:40:21 -05005846 return buf;
5847}
Chris Masona28ec192007-03-06 20:08:01 -05005848
Yan Zheng2c47e6052009-06-27 21:07:35 -04005849struct walk_control {
5850 u64 refs[BTRFS_MAX_LEVEL];
5851 u64 flags[BTRFS_MAX_LEVEL];
5852 struct btrfs_key update_progress;
5853 int stage;
5854 int level;
5855 int shared_level;
5856 int update_ref;
5857 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005858 int reada_slot;
5859 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005860};
5861
5862#define DROP_REFERENCE 1
5863#define UPDATE_BACKREF 2
5864
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005865static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5866 struct btrfs_root *root,
5867 struct walk_control *wc,
5868 struct btrfs_path *path)
5869{
5870 u64 bytenr;
5871 u64 generation;
5872 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005873 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005874 u32 nritems;
5875 u32 blocksize;
5876 struct btrfs_key key;
5877 struct extent_buffer *eb;
5878 int ret;
5879 int slot;
5880 int nread = 0;
5881
5882 if (path->slots[wc->level] < wc->reada_slot) {
5883 wc->reada_count = wc->reada_count * 2 / 3;
5884 wc->reada_count = max(wc->reada_count, 2);
5885 } else {
5886 wc->reada_count = wc->reada_count * 3 / 2;
5887 wc->reada_count = min_t(int, wc->reada_count,
5888 BTRFS_NODEPTRS_PER_BLOCK(root));
5889 }
5890
5891 eb = path->nodes[wc->level];
5892 nritems = btrfs_header_nritems(eb);
5893 blocksize = btrfs_level_size(root, wc->level - 1);
5894
5895 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5896 if (nread >= wc->reada_count)
5897 break;
5898
5899 cond_resched();
5900 bytenr = btrfs_node_blockptr(eb, slot);
5901 generation = btrfs_node_ptr_generation(eb, slot);
5902
5903 if (slot == path->slots[wc->level])
5904 goto reada;
5905
5906 if (wc->stage == UPDATE_BACKREF &&
5907 generation <= root->root_key.offset)
5908 continue;
5909
Yan, Zheng94fcca92009-10-09 09:25:16 -04005910 /* We don't lock the tree block, it's OK to be racy here */
5911 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5912 &refs, &flags);
5913 BUG_ON(ret);
5914 BUG_ON(refs == 0);
5915
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005916 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005917 if (refs == 1)
5918 goto reada;
5919
Yan, Zheng94fcca92009-10-09 09:25:16 -04005920 if (wc->level == 1 &&
5921 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5922 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005923 if (!wc->update_ref ||
5924 generation <= root->root_key.offset)
5925 continue;
5926 btrfs_node_key_to_cpu(eb, &key, slot);
5927 ret = btrfs_comp_cpu_keys(&key,
5928 &wc->update_progress);
5929 if (ret < 0)
5930 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005931 } else {
5932 if (wc->level == 1 &&
5933 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5934 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005935 }
5936reada:
5937 ret = readahead_tree_block(root, bytenr, blocksize,
5938 generation);
5939 if (ret)
5940 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005941 nread++;
5942 }
5943 wc->reada_slot = slot;
5944}
5945
Chris Mason9aca1d52007-03-13 11:09:37 -04005946/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005947 * hepler to process tree block while walking down the tree.
5948 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005949 * when wc->stage == UPDATE_BACKREF, this function updates
5950 * back refs for pointers in the block.
5951 *
5952 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005953 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005954static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5955 struct btrfs_root *root,
5956 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005957 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005958{
5959 int level = wc->level;
5960 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005961 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5962 int ret;
5963
5964 if (wc->stage == UPDATE_BACKREF &&
5965 btrfs_header_owner(eb) != root->root_key.objectid)
5966 return 1;
5967
5968 /*
5969 * when reference count of tree block is 1, it won't increase
5970 * again. once full backref flag is set, we never clear it.
5971 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005972 if (lookup_info &&
5973 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5974 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005975 BUG_ON(!path->locks[level]);
5976 ret = btrfs_lookup_extent_info(trans, root,
5977 eb->start, eb->len,
5978 &wc->refs[level],
5979 &wc->flags[level]);
5980 BUG_ON(ret);
5981 BUG_ON(wc->refs[level] == 0);
5982 }
5983
Yan Zheng2c47e6052009-06-27 21:07:35 -04005984 if (wc->stage == DROP_REFERENCE) {
5985 if (wc->refs[level] > 1)
5986 return 1;
5987
5988 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04005989 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005990 path->locks[level] = 0;
5991 }
5992 return 0;
5993 }
5994
5995 /* wc->stage == UPDATE_BACKREF */
5996 if (!(wc->flags[level] & flag)) {
5997 BUG_ON(!path->locks[level]);
5998 ret = btrfs_inc_ref(trans, root, eb, 1);
5999 BUG_ON(ret);
6000 ret = btrfs_dec_ref(trans, root, eb, 0);
6001 BUG_ON(ret);
6002 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6003 eb->len, flag, 0);
6004 BUG_ON(ret);
6005 wc->flags[level] |= flag;
6006 }
6007
6008 /*
6009 * the block is shared by multiple trees, so it's not good to
6010 * keep the tree lock
6011 */
6012 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006013 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006014 path->locks[level] = 0;
6015 }
6016 return 0;
6017}
6018
6019/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006020 * hepler to process tree block pointer.
6021 *
6022 * when wc->stage == DROP_REFERENCE, this function checks
6023 * reference count of the block pointed to. if the block
6024 * is shared and we need update back refs for the subtree
6025 * rooted at the block, this function changes wc->stage to
6026 * UPDATE_BACKREF. if the block is shared and there is no
6027 * need to update back, this function drops the reference
6028 * to the block.
6029 *
6030 * NOTE: return value 1 means we should stop walking down.
6031 */
6032static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6033 struct btrfs_root *root,
6034 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006035 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006036{
6037 u64 bytenr;
6038 u64 generation;
6039 u64 parent;
6040 u32 blocksize;
6041 struct btrfs_key key;
6042 struct extent_buffer *next;
6043 int level = wc->level;
6044 int reada = 0;
6045 int ret = 0;
6046
6047 generation = btrfs_node_ptr_generation(path->nodes[level],
6048 path->slots[level]);
6049 /*
6050 * if the lower level block was created before the snapshot
6051 * was created, we know there is no need to update back refs
6052 * for the subtree
6053 */
6054 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006055 generation <= root->root_key.offset) {
6056 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006057 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006058 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006059
6060 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6061 blocksize = btrfs_level_size(root, level - 1);
6062
6063 next = btrfs_find_tree_block(root, bytenr, blocksize);
6064 if (!next) {
6065 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00006066 if (!next)
6067 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006068 reada = 1;
6069 }
6070 btrfs_tree_lock(next);
6071 btrfs_set_lock_blocking(next);
6072
Yan, Zheng94fcca92009-10-09 09:25:16 -04006073 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6074 &wc->refs[level - 1],
6075 &wc->flags[level - 1]);
6076 BUG_ON(ret);
6077 BUG_ON(wc->refs[level - 1] == 0);
6078 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006079
Yan, Zheng94fcca92009-10-09 09:25:16 -04006080 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006081 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006082 if (level == 1 &&
6083 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6084 goto skip;
6085
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006086 if (!wc->update_ref ||
6087 generation <= root->root_key.offset)
6088 goto skip;
6089
6090 btrfs_node_key_to_cpu(path->nodes[level], &key,
6091 path->slots[level]);
6092 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6093 if (ret < 0)
6094 goto skip;
6095
6096 wc->stage = UPDATE_BACKREF;
6097 wc->shared_level = level - 1;
6098 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006099 } else {
6100 if (level == 1 &&
6101 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6102 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006103 }
6104
6105 if (!btrfs_buffer_uptodate(next, generation)) {
6106 btrfs_tree_unlock(next);
6107 free_extent_buffer(next);
6108 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006109 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006110 }
6111
6112 if (!next) {
6113 if (reada && level == 1)
6114 reada_walk_down(trans, root, wc, path);
6115 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006116 if (!next)
6117 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006118 btrfs_tree_lock(next);
6119 btrfs_set_lock_blocking(next);
6120 }
6121
6122 level--;
6123 BUG_ON(level != btrfs_header_level(next));
6124 path->nodes[level] = next;
6125 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006126 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006127 wc->level = level;
6128 if (wc->level == 1)
6129 wc->reada_slot = 0;
6130 return 0;
6131skip:
6132 wc->refs[level - 1] = 0;
6133 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006134 if (wc->stage == DROP_REFERENCE) {
6135 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6136 parent = path->nodes[level]->start;
6137 } else {
6138 BUG_ON(root->root_key.objectid !=
6139 btrfs_header_owner(path->nodes[level]));
6140 parent = 0;
6141 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006142
Yan, Zheng94fcca92009-10-09 09:25:16 -04006143 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6144 root->root_key.objectid, level - 1, 0);
6145 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006146 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006147 btrfs_tree_unlock(next);
6148 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006149 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006150 return 1;
6151}
6152
6153/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006154 * hepler to process tree block while walking up the tree.
6155 *
6156 * when wc->stage == DROP_REFERENCE, this function drops
6157 * reference count on the block.
6158 *
6159 * when wc->stage == UPDATE_BACKREF, this function changes
6160 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6161 * to UPDATE_BACKREF previously while processing the block.
6162 *
6163 * NOTE: return value 1 means we should stop walking up.
6164 */
6165static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6166 struct btrfs_root *root,
6167 struct btrfs_path *path,
6168 struct walk_control *wc)
6169{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006170 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006171 int level = wc->level;
6172 struct extent_buffer *eb = path->nodes[level];
6173 u64 parent = 0;
6174
6175 if (wc->stage == UPDATE_BACKREF) {
6176 BUG_ON(wc->shared_level < level);
6177 if (level < wc->shared_level)
6178 goto out;
6179
Yan Zheng2c47e6052009-06-27 21:07:35 -04006180 ret = find_next_key(path, level + 1, &wc->update_progress);
6181 if (ret > 0)
6182 wc->update_ref = 0;
6183
6184 wc->stage = DROP_REFERENCE;
6185 wc->shared_level = -1;
6186 path->slots[level] = 0;
6187
6188 /*
6189 * check reference count again if the block isn't locked.
6190 * we should start walking down the tree again if reference
6191 * count is one.
6192 */
6193 if (!path->locks[level]) {
6194 BUG_ON(level == 0);
6195 btrfs_tree_lock(eb);
6196 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006197 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006198
6199 ret = btrfs_lookup_extent_info(trans, root,
6200 eb->start, eb->len,
6201 &wc->refs[level],
6202 &wc->flags[level]);
6203 BUG_ON(ret);
6204 BUG_ON(wc->refs[level] == 0);
6205 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04006206 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006207 return 1;
6208 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006209 }
6210 }
6211
6212 /* wc->stage == DROP_REFERENCE */
6213 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6214
6215 if (wc->refs[level] == 1) {
6216 if (level == 0) {
6217 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6218 ret = btrfs_dec_ref(trans, root, eb, 1);
6219 else
6220 ret = btrfs_dec_ref(trans, root, eb, 0);
6221 BUG_ON(ret);
6222 }
6223 /* make block locked assertion in clean_tree_block happy */
6224 if (!path->locks[level] &&
6225 btrfs_header_generation(eb) == trans->transid) {
6226 btrfs_tree_lock(eb);
6227 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006228 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006229 }
6230 clean_tree_block(trans, root, eb);
6231 }
6232
6233 if (eb == root->node) {
6234 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6235 parent = eb->start;
6236 else
6237 BUG_ON(root->root_key.objectid !=
6238 btrfs_header_owner(eb));
6239 } else {
6240 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6241 parent = path->nodes[level + 1]->start;
6242 else
6243 BUG_ON(root->root_key.objectid !=
6244 btrfs_header_owner(path->nodes[level + 1]));
6245 }
6246
Yan, Zhengf0486c62010-05-16 10:46:25 -04006247 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006248out:
6249 wc->refs[level] = 0;
6250 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006251 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006252}
6253
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006254static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6255 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006256 struct btrfs_path *path,
6257 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006258{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006259 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006260 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006261 int ret;
6262
Yan Zheng2c47e6052009-06-27 21:07:35 -04006263 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006264 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006265 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006266 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006267
Yan Zheng2c47e6052009-06-27 21:07:35 -04006268 if (level == 0)
6269 break;
6270
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006271 if (path->slots[level] >=
6272 btrfs_header_nritems(path->nodes[level]))
6273 break;
6274
Yan, Zheng94fcca92009-10-09 09:25:16 -04006275 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006276 if (ret > 0) {
6277 path->slots[level]++;
6278 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00006279 } else if (ret < 0)
6280 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006281 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006282 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006283 return 0;
6284}
6285
Chris Masond3977122009-01-05 21:25:51 -05006286static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006287 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006288 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006289 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006290{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006291 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006292 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006293
Yan Zheng2c47e6052009-06-27 21:07:35 -04006294 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6295 while (level < max_level && path->nodes[level]) {
6296 wc->level = level;
6297 if (path->slots[level] + 1 <
6298 btrfs_header_nritems(path->nodes[level])) {
6299 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006300 return 0;
6301 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006302 ret = walk_up_proc(trans, root, path, wc);
6303 if (ret > 0)
6304 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006305
Yan Zheng2c47e6052009-06-27 21:07:35 -04006306 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04006307 btrfs_tree_unlock_rw(path->nodes[level],
6308 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006309 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006310 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006311 free_extent_buffer(path->nodes[level]);
6312 path->nodes[level] = NULL;
6313 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006314 }
6315 }
6316 return 1;
6317}
6318
Chris Mason9aca1d52007-03-13 11:09:37 -04006319/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006320 * drop a subvolume tree.
6321 *
6322 * this function traverses the tree freeing any blocks that only
6323 * referenced by the tree.
6324 *
6325 * when a shared tree block is found. this function decreases its
6326 * reference count by one. if update_ref is true, this function
6327 * also make sure backrefs for the shared block and all lower level
6328 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006329 */
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006330void btrfs_drop_snapshot(struct btrfs_root *root,
6331 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006332{
Chris Mason5caf2a02007-04-02 11:20:42 -04006333 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006334 struct btrfs_trans_handle *trans;
6335 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006336 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006337 struct walk_control *wc;
6338 struct btrfs_key key;
6339 int err = 0;
6340 int ret;
6341 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006342
Chris Mason5caf2a02007-04-02 11:20:42 -04006343 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006344 if (!path) {
6345 err = -ENOMEM;
6346 goto out;
6347 }
Chris Mason20524f02007-03-10 06:35:47 -05006348
Yan Zheng2c47e6052009-06-27 21:07:35 -04006349 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07006350 if (!wc) {
6351 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006352 err = -ENOMEM;
6353 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07006354 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006355
Yan, Zhenga22285a2010-05-16 10:48:46 -04006356 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006357 BUG_ON(IS_ERR(trans));
6358
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006359 if (block_rsv)
6360 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006361
Chris Mason9f3a7422007-08-07 15:52:19 -04006362 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006363 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006364 path->nodes[level] = btrfs_lock_root_node(root);
6365 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006366 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006367 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006368 memset(&wc->update_progress, 0,
6369 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006370 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006371 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006372 memcpy(&wc->update_progress, &key,
6373 sizeof(wc->update_progress));
6374
Chris Mason6702ed42007-08-07 16:15:09 -04006375 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006376 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006377 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006378 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6379 path->lowest_level = 0;
6380 if (ret < 0) {
6381 err = ret;
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006382 goto out_free;
Chris Mason9f3a7422007-08-07 15:52:19 -04006383 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006384 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006385
Chris Mason7d9eb122008-07-08 14:19:17 -04006386 /*
6387 * unlock our path, this is safe because only this
6388 * function is allowed to delete this snapshot
6389 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006390 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006391
Yan Zheng2c47e6052009-06-27 21:07:35 -04006392 level = btrfs_header_level(root->node);
6393 while (1) {
6394 btrfs_tree_lock(path->nodes[level]);
6395 btrfs_set_lock_blocking(path->nodes[level]);
6396
6397 ret = btrfs_lookup_extent_info(trans, root,
6398 path->nodes[level]->start,
6399 path->nodes[level]->len,
6400 &wc->refs[level],
6401 &wc->flags[level]);
6402 BUG_ON(ret);
6403 BUG_ON(wc->refs[level] == 0);
6404
6405 if (level == root_item->drop_level)
6406 break;
6407
6408 btrfs_tree_unlock(path->nodes[level]);
6409 WARN_ON(wc->refs[level] != 1);
6410 level--;
6411 }
6412 }
6413
6414 wc->level = level;
6415 wc->shared_level = -1;
6416 wc->stage = DROP_REFERENCE;
6417 wc->update_ref = update_ref;
6418 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006419 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006420
6421 while (1) {
6422 ret = walk_down_tree(trans, root, path, wc);
6423 if (ret < 0) {
6424 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006425 break;
6426 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006427
6428 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6429 if (ret < 0) {
6430 err = ret;
6431 break;
6432 }
6433
6434 if (ret > 0) {
6435 BUG_ON(wc->stage != DROP_REFERENCE);
6436 break;
6437 }
6438
6439 if (wc->stage == DROP_REFERENCE) {
6440 level = wc->level;
6441 btrfs_node_key(path->nodes[level],
6442 &root_item->drop_progress,
6443 path->slots[level]);
6444 root_item->drop_level = level;
6445 }
6446
6447 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006448 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006449 ret = btrfs_update_root(trans, tree_root,
6450 &root->root_key,
6451 root_item);
6452 BUG_ON(ret);
6453
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006454 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006455 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006456 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006457 if (block_rsv)
6458 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006459 }
Chris Mason20524f02007-03-10 06:35:47 -05006460 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006461 btrfs_release_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006462 BUG_ON(err);
6463
6464 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6465 BUG_ON(ret);
6466
Yan, Zheng76dda932009-09-21 16:00:26 -04006467 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6468 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6469 NULL, NULL);
6470 BUG_ON(ret < 0);
6471 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006472 /* if we fail to delete the orphan item this time
6473 * around, it'll get picked up the next time.
6474 *
6475 * The most common failure here is just -ENOENT.
6476 */
6477 btrfs_del_orphan_item(trans, tree_root,
6478 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006479 }
6480 }
6481
6482 if (root->in_radix) {
6483 btrfs_free_fs_root(tree_root->fs_info, root);
6484 } else {
6485 free_extent_buffer(root->node);
6486 free_extent_buffer(root->commit_root);
6487 kfree(root);
6488 }
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006489out_free:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006490 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006491 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006492 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006493out:
6494 if (err)
6495 btrfs_std_error(root->fs_info, err);
6496 return;
Chris Mason20524f02007-03-10 06:35:47 -05006497}
Chris Mason9078a3e2007-04-26 16:46:15 -04006498
Yan Zheng2c47e6052009-06-27 21:07:35 -04006499/*
6500 * drop subtree rooted at tree block 'node'.
6501 *
6502 * NOTE: this function will unlock and release tree block 'node'
6503 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006504int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6505 struct btrfs_root *root,
6506 struct extent_buffer *node,
6507 struct extent_buffer *parent)
6508{
6509 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006510 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006511 int level;
6512 int parent_level;
6513 int ret = 0;
6514 int wret;
6515
Yan Zheng2c47e6052009-06-27 21:07:35 -04006516 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6517
Yan Zhengf82d02d2008-10-29 14:49:05 -04006518 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006519 if (!path)
6520 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006521
Yan Zheng2c47e6052009-06-27 21:07:35 -04006522 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006523 if (!wc) {
6524 btrfs_free_path(path);
6525 return -ENOMEM;
6526 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006527
Chris Masonb9447ef2009-03-09 11:45:38 -04006528 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006529 parent_level = btrfs_header_level(parent);
6530 extent_buffer_get(parent);
6531 path->nodes[parent_level] = parent;
6532 path->slots[parent_level] = btrfs_header_nritems(parent);
6533
Chris Masonb9447ef2009-03-09 11:45:38 -04006534 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006535 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006536 path->nodes[level] = node;
6537 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006538 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006539
6540 wc->refs[parent_level] = 1;
6541 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6542 wc->level = level;
6543 wc->shared_level = -1;
6544 wc->stage = DROP_REFERENCE;
6545 wc->update_ref = 0;
6546 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006547 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006548
6549 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006550 wret = walk_down_tree(trans, root, path, wc);
6551 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006552 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006553 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006554 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006555
Yan Zheng2c47e6052009-06-27 21:07:35 -04006556 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006557 if (wret < 0)
6558 ret = wret;
6559 if (wret != 0)
6560 break;
6561 }
6562
Yan Zheng2c47e6052009-06-27 21:07:35 -04006563 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006564 btrfs_free_path(path);
6565 return ret;
6566}
6567
Chris Masonec44a352008-04-28 15:29:52 -04006568static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6569{
6570 u64 num_devices;
6571 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6572 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6573
Chris Masoncd02dca2010-12-13 14:56:23 -05006574 /*
6575 * we add in the count of missing devices because we want
6576 * to make sure that any RAID levels on a degraded FS
6577 * continue to be honored.
6578 */
6579 num_devices = root->fs_info->fs_devices->rw_devices +
6580 root->fs_info->fs_devices->missing_devices;
6581
Chris Masonec44a352008-04-28 15:29:52 -04006582 if (num_devices == 1) {
6583 stripped |= BTRFS_BLOCK_GROUP_DUP;
6584 stripped = flags & ~stripped;
6585
6586 /* turn raid0 into single device chunks */
6587 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6588 return stripped;
6589
6590 /* turn mirroring into duplication */
6591 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6592 BTRFS_BLOCK_GROUP_RAID10))
6593 return stripped | BTRFS_BLOCK_GROUP_DUP;
6594 return flags;
6595 } else {
6596 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006597 if (flags & stripped)
6598 return flags;
6599
6600 stripped |= BTRFS_BLOCK_GROUP_DUP;
6601 stripped = flags & ~stripped;
6602
6603 /* switch duplicated blocks with raid1 */
6604 if (flags & BTRFS_BLOCK_GROUP_DUP)
6605 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6606
6607 /* turn single device chunks into raid0 */
6608 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6609 }
6610 return flags;
6611}
6612
Miao Xie199c36e2011-07-15 10:34:36 +00006613static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04006614{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006615 struct btrfs_space_info *sinfo = cache->space_info;
6616 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00006617 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006618 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04006619
Chris Masonc286ac42008-07-22 23:06:41 -04006620
Miao Xie199c36e2011-07-15 10:34:36 +00006621 /*
6622 * We need some metadata space and system metadata space for
6623 * allocating chunks in some corner cases until we force to set
6624 * it to be readonly.
6625 */
6626 if ((sinfo->flags &
6627 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
6628 !force)
6629 min_allocable_bytes = 1 * 1024 * 1024;
6630 else
6631 min_allocable_bytes = 0;
6632
Yan, Zhengf0486c62010-05-16 10:46:25 -04006633 spin_lock(&sinfo->lock);
6634 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00006635
6636 if (cache->ro) {
6637 ret = 0;
6638 goto out;
6639 }
6640
Yan, Zhengf0486c62010-05-16 10:46:25 -04006641 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6642 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04006643
Yan, Zhengf0486c62010-05-16 10:46:25 -04006644 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04006645 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
6646 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04006647 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006648 cache->ro = 1;
6649 ret = 0;
6650 }
WuBo61cfea92011-07-26 03:30:11 +00006651out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04006652 spin_unlock(&cache->lock);
6653 spin_unlock(&sinfo->lock);
6654 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04006655}
6656
Yan, Zhengf0486c62010-05-16 10:46:25 -04006657int btrfs_set_block_group_ro(struct btrfs_root *root,
6658 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006659
6660{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006661 struct btrfs_trans_handle *trans;
6662 u64 alloc_flags;
6663 int ret;
6664
6665 BUG_ON(cache->ro);
6666
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006667 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006668 BUG_ON(IS_ERR(trans));
6669
6670 alloc_flags = update_block_group_flags(root, cache->flags);
6671 if (alloc_flags != cache->flags)
Chris Mason0e4f8f82011-04-15 16:05:44 -04006672 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6673 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006674
Miao Xie199c36e2011-07-15 10:34:36 +00006675 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006676 if (!ret)
6677 goto out;
6678 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006679 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6680 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006681 if (ret < 0)
6682 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00006683 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006684out:
6685 btrfs_end_transaction(trans, root);
6686 return ret;
6687}
6688
Chris Masonc87f08c2011-02-16 13:57:04 -05006689int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
6690 struct btrfs_root *root, u64 type)
6691{
6692 u64 alloc_flags = get_alloc_profile(root, type);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006693 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6694 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05006695}
6696
Miao Xie6d07bce2011-01-05 10:07:31 +00006697/*
6698 * helper to account the unused space of all the readonly block group in the
6699 * list. takes mirrors into account.
6700 */
6701static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
6702{
6703 struct btrfs_block_group_cache *block_group;
6704 u64 free_bytes = 0;
6705 int factor;
6706
6707 list_for_each_entry(block_group, groups_list, list) {
6708 spin_lock(&block_group->lock);
6709
6710 if (!block_group->ro) {
6711 spin_unlock(&block_group->lock);
6712 continue;
6713 }
6714
6715 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
6716 BTRFS_BLOCK_GROUP_RAID10 |
6717 BTRFS_BLOCK_GROUP_DUP))
6718 factor = 2;
6719 else
6720 factor = 1;
6721
6722 free_bytes += (block_group->key.offset -
6723 btrfs_block_group_used(&block_group->item)) *
6724 factor;
6725
6726 spin_unlock(&block_group->lock);
6727 }
6728
6729 return free_bytes;
6730}
6731
6732/*
6733 * helper to account the unused space of all the readonly block group in the
6734 * space_info. takes mirrors into account.
6735 */
6736u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
6737{
6738 int i;
6739 u64 free_bytes = 0;
6740
6741 spin_lock(&sinfo->lock);
6742
6743 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
6744 if (!list_empty(&sinfo->block_groups[i]))
6745 free_bytes += __btrfs_get_ro_block_group_free_space(
6746 &sinfo->block_groups[i]);
6747
6748 spin_unlock(&sinfo->lock);
6749
6750 return free_bytes;
6751}
6752
Yan, Zhengf0486c62010-05-16 10:46:25 -04006753int btrfs_set_block_group_rw(struct btrfs_root *root,
6754 struct btrfs_block_group_cache *cache)
6755{
6756 struct btrfs_space_info *sinfo = cache->space_info;
6757 u64 num_bytes;
6758
6759 BUG_ON(!cache->ro);
6760
6761 spin_lock(&sinfo->lock);
6762 spin_lock(&cache->lock);
6763 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6764 cache->bytes_super - btrfs_block_group_used(&cache->item);
6765 sinfo->bytes_readonly -= num_bytes;
6766 cache->ro = 0;
6767 spin_unlock(&cache->lock);
6768 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006769 return 0;
6770}
6771
Josef Bacikba1bf482009-09-11 16:11:19 -04006772/*
6773 * checks to see if its even possible to relocate this block group.
6774 *
6775 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6776 * ok to go ahead and try.
6777 */
6778int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04006779{
Zheng Yan1a40e232008-09-26 10:09:34 -04006780 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04006781 struct btrfs_space_info *space_info;
6782 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6783 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00006784 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04006785 u64 dev_min = 1;
6786 u64 dev_nr = 0;
liubocdcb7252011-08-03 10:15:25 +00006787 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04006788 int full = 0;
6789 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006790
Josef Bacikba1bf482009-09-11 16:11:19 -04006791 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04006792
Josef Bacikba1bf482009-09-11 16:11:19 -04006793 /* odd, couldn't find the block group, leave it alone */
6794 if (!block_group)
6795 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006796
liubocdcb7252011-08-03 10:15:25 +00006797 min_free = btrfs_block_group_used(&block_group->item);
6798
Josef Bacikba1bf482009-09-11 16:11:19 -04006799 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00006800 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04006801 goto out;
Chris Mason323da792008-05-09 11:46:48 -04006802
Josef Bacikba1bf482009-09-11 16:11:19 -04006803 space_info = block_group->space_info;
6804 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04006805
Josef Bacikba1bf482009-09-11 16:11:19 -04006806 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04006807
Josef Bacikba1bf482009-09-11 16:11:19 -04006808 /*
6809 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04006810 * relocate it unless we're able to allocate a new chunk below.
6811 *
6812 * Otherwise, we need to make sure we have room in the space to handle
6813 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04006814 */
Chris Mason7ce618d2009-09-22 14:48:44 -04006815 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00006816 (space_info->bytes_used + space_info->bytes_reserved +
6817 space_info->bytes_pinned + space_info->bytes_readonly +
6818 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04006819 spin_unlock(&space_info->lock);
6820 goto out;
6821 }
6822 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006823
Josef Bacikba1bf482009-09-11 16:11:19 -04006824 /*
6825 * ok we don't have enough space, but maybe we have free space on our
6826 * devices to allocate new chunks for relocation, so loop through our
6827 * alloc devices and guess if we have enough space. However, if we
6828 * were marked as full, then we know there aren't enough chunks, and we
6829 * can just return.
6830 */
6831 ret = -1;
6832 if (full)
6833 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05006834
liubocdcb7252011-08-03 10:15:25 +00006835 /*
6836 * index:
6837 * 0: raid10
6838 * 1: raid1
6839 * 2: dup
6840 * 3: raid0
6841 * 4: single
6842 */
6843 index = get_block_group_index(block_group);
6844 if (index == 0) {
6845 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04006846 /* Divide by 2 */
6847 min_free >>= 1;
liubocdcb7252011-08-03 10:15:25 +00006848 } else if (index == 1) {
6849 dev_min = 2;
6850 } else if (index == 2) {
Josef Bacik6719db62011-08-20 08:29:51 -04006851 /* Multiply by 2 */
6852 min_free <<= 1;
liubocdcb7252011-08-03 10:15:25 +00006853 } else if (index == 3) {
6854 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04006855 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00006856 }
6857
Josef Bacikba1bf482009-09-11 16:11:19 -04006858 mutex_lock(&root->fs_info->chunk_mutex);
6859 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00006860 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04006861
Josef Bacikba1bf482009-09-11 16:11:19 -04006862 /*
6863 * check to make sure we can actually find a chunk with enough
6864 * space to fit our block group in.
6865 */
6866 if (device->total_bytes > device->bytes_used + min_free) {
6867 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00006868 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04006869 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00006870 dev_nr++;
6871
6872 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05006873 break;
liubocdcb7252011-08-03 10:15:25 +00006874
Josef Bacikba1bf482009-09-11 16:11:19 -04006875 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05006876 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006877 }
Josef Bacikba1bf482009-09-11 16:11:19 -04006878 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05006879out:
Josef Bacikba1bf482009-09-11 16:11:19 -04006880 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006881 return ret;
6882}
6883
Christoph Hellwigb2950862008-12-02 09:54:17 -05006884static int find_first_block_group(struct btrfs_root *root,
6885 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04006886{
Chris Mason925baed2008-06-25 16:01:30 -04006887 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006888 struct btrfs_key found_key;
6889 struct extent_buffer *leaf;
6890 int slot;
6891
6892 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6893 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006894 goto out;
6895
Chris Masond3977122009-01-05 21:25:51 -05006896 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006897 slot = path->slots[0];
6898 leaf = path->nodes[0];
6899 if (slot >= btrfs_header_nritems(leaf)) {
6900 ret = btrfs_next_leaf(root, path);
6901 if (ret == 0)
6902 continue;
6903 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006904 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04006905 break;
6906 }
6907 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6908
6909 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04006910 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6911 ret = 0;
6912 goto out;
6913 }
Chris Mason0b86a832008-03-24 15:01:56 -04006914 path->slots[0]++;
6915 }
Chris Mason925baed2008-06-25 16:01:30 -04006916out:
Chris Mason0b86a832008-03-24 15:01:56 -04006917 return ret;
6918}
6919
Josef Bacik0af3d002010-06-21 14:48:16 -04006920void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
6921{
6922 struct btrfs_block_group_cache *block_group;
6923 u64 last = 0;
6924
6925 while (1) {
6926 struct inode *inode;
6927
6928 block_group = btrfs_lookup_first_block_group(info, last);
6929 while (block_group) {
6930 spin_lock(&block_group->lock);
6931 if (block_group->iref)
6932 break;
6933 spin_unlock(&block_group->lock);
6934 block_group = next_block_group(info->tree_root,
6935 block_group);
6936 }
6937 if (!block_group) {
6938 if (last == 0)
6939 break;
6940 last = 0;
6941 continue;
6942 }
6943
6944 inode = block_group->inode;
6945 block_group->iref = 0;
6946 block_group->inode = NULL;
6947 spin_unlock(&block_group->lock);
6948 iput(inode);
6949 last = block_group->key.objectid + block_group->key.offset;
6950 btrfs_put_block_group(block_group);
6951 }
6952}
6953
Zheng Yan1a40e232008-09-26 10:09:34 -04006954int btrfs_free_block_groups(struct btrfs_fs_info *info)
6955{
6956 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04006957 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04006958 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04006959 struct rb_node *n;
6960
Yan Zheng11833d62009-09-11 16:11:19 -04006961 down_write(&info->extent_commit_sem);
6962 while (!list_empty(&info->caching_block_groups)) {
6963 caching_ctl = list_entry(info->caching_block_groups.next,
6964 struct btrfs_caching_control, list);
6965 list_del(&caching_ctl->list);
6966 put_caching_control(caching_ctl);
6967 }
6968 up_write(&info->extent_commit_sem);
6969
Zheng Yan1a40e232008-09-26 10:09:34 -04006970 spin_lock(&info->block_group_cache_lock);
6971 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6972 block_group = rb_entry(n, struct btrfs_block_group_cache,
6973 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04006974 rb_erase(&block_group->cache_node,
6975 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04006976 spin_unlock(&info->block_group_cache_lock);
6977
Josef Bacik80eb2342008-10-29 14:49:05 -04006978 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006979 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006980 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006981
Josef Bacik817d52f2009-07-13 21:29:25 -04006982 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04006983 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04006984
Josef Bacik3c148742011-02-02 15:53:47 +00006985 /*
6986 * We haven't cached this block group, which means we could
6987 * possibly have excluded extents on this block group.
6988 */
6989 if (block_group->cached == BTRFS_CACHE_NO)
6990 free_excluded_extents(info->extent_root, block_group);
6991
Josef Bacik817d52f2009-07-13 21:29:25 -04006992 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00006993 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04006994
6995 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006996 }
6997 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04006998
6999 /* now that all the block groups are freed, go through and
7000 * free all the space_info structs. This is only called during
7001 * the final stages of unmount, and so we know nobody is
7002 * using them. We call synchronize_rcu() once before we start,
7003 * just to be on the safe side.
7004 */
7005 synchronize_rcu();
7006
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007007 release_global_block_rsv(info);
7008
Chris Mason4184ea72009-03-10 12:39:20 -04007009 while(!list_empty(&info->space_info)) {
7010 space_info = list_entry(info->space_info.next,
7011 struct btrfs_space_info,
7012 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007013 if (space_info->bytes_pinned > 0 ||
Josef Bacikfb25e912011-07-26 17:00:46 -04007014 space_info->bytes_reserved > 0 ||
7015 space_info->bytes_may_use > 0) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007016 WARN_ON(1);
7017 dump_space_info(space_info, 0, 0);
7018 }
Chris Mason4184ea72009-03-10 12:39:20 -04007019 list_del(&space_info->list);
7020 kfree(space_info);
7021 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007022 return 0;
7023}
7024
Yan, Zhengb742bb82010-05-16 10:46:24 -04007025static void __link_block_group(struct btrfs_space_info *space_info,
7026 struct btrfs_block_group_cache *cache)
7027{
7028 int index = get_block_group_index(cache);
7029
7030 down_write(&space_info->groups_sem);
7031 list_add_tail(&cache->list, &space_info->block_groups[index]);
7032 up_write(&space_info->groups_sem);
7033}
7034
Chris Mason9078a3e2007-04-26 16:46:15 -04007035int btrfs_read_block_groups(struct btrfs_root *root)
7036{
7037 struct btrfs_path *path;
7038 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007039 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007040 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007041 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007042 struct btrfs_key key;
7043 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007044 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04007045 int need_clear = 0;
7046 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04007047
Chris Masonbe744172007-05-06 10:15:01 -04007048 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007049 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007050 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007051 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007052 path = btrfs_alloc_path();
7053 if (!path)
7054 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04007055 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04007056
Josef Bacik0af3d002010-06-21 14:48:16 -04007057 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
7058 if (cache_gen != 0 &&
7059 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
7060 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04007061 if (btrfs_test_opt(root, CLEAR_CACHE))
7062 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04007063 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
7064 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04007065
Chris Masond3977122009-01-05 21:25:51 -05007066 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007067 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007068 if (ret > 0)
7069 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007070 if (ret != 0)
7071 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04007072 leaf = path->nodes[0];
7073 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007074 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007075 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007076 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007077 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007078 }
Li Zefan34d52cb2011-03-29 13:46:06 +08007079 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7080 GFP_NOFS);
7081 if (!cache->free_space_ctl) {
7082 kfree(cache);
7083 ret = -ENOMEM;
7084 goto error;
7085 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007086
Yan Zhengd2fb3432008-12-11 16:30:39 -05007087 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007088 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007089 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007090 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007091 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007092
Josef Bacik0af3d002010-06-21 14:48:16 -04007093 if (need_clear)
7094 cache->disk_cache_state = BTRFS_DC_CLEAR;
7095
Chris Mason5f39d392007-10-15 16:14:19 -04007096 read_extent_buffer(leaf, &cache->item,
7097 btrfs_item_ptr_offset(leaf, path->slots[0]),
7098 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007099 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007100
Chris Mason9078a3e2007-04-26 16:46:15 -04007101 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02007102 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007103 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007104 cache->sectorsize = root->sectorsize;
7105
Li Zefan34d52cb2011-03-29 13:46:06 +08007106 btrfs_init_free_space_ctl(cache);
7107
Josef Bacik817d52f2009-07-13 21:29:25 -04007108 /*
Josef Bacik3c148742011-02-02 15:53:47 +00007109 * We need to exclude the super stripes now so that the space
7110 * info has super bytes accounted for, otherwise we'll think
7111 * we have more space than we actually do.
7112 */
7113 exclude_super_stripes(root, cache);
7114
7115 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04007116 * check for two cases, either we are full, and therefore
7117 * don't need to bother with the caching work since we won't
7118 * find any space, or we are empty, and we can just add all
7119 * the space in and be done with it. This saves us _alot_ of
7120 * time, particularly in the full case.
7121 */
7122 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007123 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007124 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007125 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007126 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007127 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007128 cache->cached = BTRFS_CACHE_FINISHED;
7129 add_new_free_space(cache, root->fs_info,
7130 found_key.objectid,
7131 found_key.objectid +
7132 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007133 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007134 }
Chris Mason96b51792007-10-15 16:15:19 -04007135
Chris Mason6324fbf2008-03-24 15:01:59 -04007136 ret = update_space_info(info, cache->flags, found_key.offset,
7137 btrfs_block_group_used(&cache->item),
7138 &space_info);
7139 BUG_ON(ret);
7140 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007141 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007142 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007143 spin_unlock(&cache->space_info->lock);
7144
Yan, Zhengb742bb82010-05-16 10:46:24 -04007145 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007146
Josef Bacik0f9dd462008-09-23 13:14:11 -04007147 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7148 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007149
7150 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007151 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00007152 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04007153 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007154
7155 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7156 if (!(get_alloc_profile(root, space_info->flags) &
7157 (BTRFS_BLOCK_GROUP_RAID10 |
7158 BTRFS_BLOCK_GROUP_RAID1 |
7159 BTRFS_BLOCK_GROUP_DUP)))
7160 continue;
7161 /*
7162 * avoid allocating from un-mirrored block group if there are
7163 * mirrored block groups.
7164 */
7165 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007166 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007167 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007168 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007169 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007170
7171 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007172 ret = 0;
7173error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007174 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007175 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007176}
Chris Mason6324fbf2008-03-24 15:01:59 -04007177
7178int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7179 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007180 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007181 u64 size)
7182{
7183 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007184 struct btrfs_root *extent_root;
7185 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007186
7187 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007188
Chris Mason12fcfd22009-03-24 10:24:20 -04007189 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007190
Chris Mason8f18cf12008-04-25 16:53:30 -04007191 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007192 if (!cache)
7193 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08007194 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7195 GFP_NOFS);
7196 if (!cache->free_space_ctl) {
7197 kfree(cache);
7198 return -ENOMEM;
7199 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007200
Chris Masone17cade2008-04-15 15:41:47 -04007201 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007202 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007203 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007204 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007205 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04007206
Yan Zhengd2fb3432008-12-11 16:30:39 -05007207 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007208 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007209 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007210 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007211
Li Zefan34d52cb2011-03-29 13:46:06 +08007212 btrfs_init_free_space_ctl(cache);
7213
Chris Mason6324fbf2008-03-24 15:01:59 -04007214 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007215 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7216 cache->flags = type;
7217 btrfs_set_block_group_flags(&cache->item, type);
7218
Yan Zheng11833d62009-09-11 16:11:19 -04007219 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007220 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007221 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007222
Josef Bacik817d52f2009-07-13 21:29:25 -04007223 add_new_free_space(cache, root->fs_info, chunk_offset,
7224 chunk_offset + size);
7225
Yan Zheng11833d62009-09-11 16:11:19 -04007226 free_excluded_extents(root, cache);
7227
Chris Mason6324fbf2008-03-24 15:01:59 -04007228 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7229 &cache->space_info);
7230 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007231
7232 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007233 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007234 spin_unlock(&cache->space_info->lock);
7235
Yan, Zhengb742bb82010-05-16 10:46:24 -04007236 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007237
Josef Bacik0f9dd462008-09-23 13:14:11 -04007238 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7239 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007240
Chris Mason6324fbf2008-03-24 15:01:59 -04007241 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7242 sizeof(cache->item));
7243 BUG_ON(ret);
7244
Chris Masond18a2c42008-04-04 15:40:00 -04007245 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007246
Chris Mason6324fbf2008-03-24 15:01:59 -04007247 return 0;
7248}
Zheng Yan1a40e232008-09-26 10:09:34 -04007249
7250int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7251 struct btrfs_root *root, u64 group_start)
7252{
7253 struct btrfs_path *path;
7254 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007255 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04007256 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04007257 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04007258 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04007259 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04007260 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04007261
Zheng Yan1a40e232008-09-26 10:09:34 -04007262 root = root->fs_info->extent_root;
7263
7264 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7265 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007266 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007267
liubo9f7c43c2011-03-07 02:13:33 +00007268 /*
7269 * Free the reserved super bytes from this block group before
7270 * remove it.
7271 */
7272 free_excluded_extents(root, block_group);
7273
Zheng Yan1a40e232008-09-26 10:09:34 -04007274 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04007275 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7276 BTRFS_BLOCK_GROUP_RAID1 |
7277 BTRFS_BLOCK_GROUP_RAID10))
7278 factor = 2;
7279 else
7280 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007281
Chris Mason44fb5512009-06-04 15:34:51 -04007282 /* make sure this block group isn't part of an allocation cluster */
7283 cluster = &root->fs_info->data_alloc_cluster;
7284 spin_lock(&cluster->refill_lock);
7285 btrfs_return_cluster_to_free_space(block_group, cluster);
7286 spin_unlock(&cluster->refill_lock);
7287
7288 /*
7289 * make sure this block group isn't part of a metadata
7290 * allocation cluster
7291 */
7292 cluster = &root->fs_info->meta_alloc_cluster;
7293 spin_lock(&cluster->refill_lock);
7294 btrfs_return_cluster_to_free_space(block_group, cluster);
7295 spin_unlock(&cluster->refill_lock);
7296
Zheng Yan1a40e232008-09-26 10:09:34 -04007297 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007298 if (!path) {
7299 ret = -ENOMEM;
7300 goto out;
7301 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007302
Josef Bacik0af3d002010-06-21 14:48:16 -04007303 inode = lookup_free_space_inode(root, block_group, path);
7304 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00007305 ret = btrfs_orphan_add(trans, inode);
7306 BUG_ON(ret);
Josef Bacik0af3d002010-06-21 14:48:16 -04007307 clear_nlink(inode);
7308 /* One for the block groups ref */
7309 spin_lock(&block_group->lock);
7310 if (block_group->iref) {
7311 block_group->iref = 0;
7312 block_group->inode = NULL;
7313 spin_unlock(&block_group->lock);
7314 iput(inode);
7315 } else {
7316 spin_unlock(&block_group->lock);
7317 }
7318 /* One for our lookup ref */
7319 iput(inode);
7320 }
7321
7322 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
7323 key.offset = block_group->key.objectid;
7324 key.type = 0;
7325
7326 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
7327 if (ret < 0)
7328 goto out;
7329 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02007330 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007331 if (ret == 0) {
7332 ret = btrfs_del_item(trans, tree_root, path);
7333 if (ret)
7334 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02007335 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007336 }
7337
Yan Zheng3dfdb932009-01-21 10:49:16 -05007338 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007339 rb_erase(&block_group->cache_node,
7340 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007341 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007342
Josef Bacik80eb2342008-10-29 14:49:05 -04007343 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007344 /*
7345 * we must use list_del_init so people can check to see if they
7346 * are still on the list after taking the semaphore
7347 */
7348 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007349 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007350
Josef Bacik817d52f2009-07-13 21:29:25 -04007351 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007352 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007353
7354 btrfs_remove_free_space_cache(block_group);
7355
Yan Zhengc146afa2008-11-12 14:34:12 -05007356 spin_lock(&block_group->space_info->lock);
7357 block_group->space_info->total_bytes -= block_group->key.offset;
7358 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04007359 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05007360 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007361
Josef Bacik0af3d002010-06-21 14:48:16 -04007362 memcpy(&key, &block_group->key, sizeof(key));
7363
Chris Mason283bb192009-07-24 16:30:55 -04007364 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007365
Chris Masonfa9c0d72009-04-03 09:47:43 -04007366 btrfs_put_block_group(block_group);
7367 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007368
7369 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7370 if (ret > 0)
7371 ret = -EIO;
7372 if (ret < 0)
7373 goto out;
7374
7375 ret = btrfs_del_item(trans, root, path);
7376out:
7377 btrfs_free_path(path);
7378 return ret;
7379}
liuboacce9522011-01-06 19:30:25 +08007380
liuboc59021f2011-03-07 02:13:14 +00007381int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
7382{
7383 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00007384 struct btrfs_super_block *disk_super;
7385 u64 features;
7386 u64 flags;
7387 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00007388 int ret;
7389
liubo1aba86d2011-04-08 08:44:37 +00007390 disk_super = &fs_info->super_copy;
7391 if (!btrfs_super_root(disk_super))
7392 return 1;
liuboc59021f2011-03-07 02:13:14 +00007393
liubo1aba86d2011-04-08 08:44:37 +00007394 features = btrfs_super_incompat_flags(disk_super);
7395 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
7396 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00007397
liubo1aba86d2011-04-08 08:44:37 +00007398 flags = BTRFS_BLOCK_GROUP_SYSTEM;
7399 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00007400 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00007401 goto out;
liuboc59021f2011-03-07 02:13:14 +00007402
liubo1aba86d2011-04-08 08:44:37 +00007403 if (mixed) {
7404 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
7405 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7406 } else {
7407 flags = BTRFS_BLOCK_GROUP_METADATA;
7408 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7409 if (ret)
7410 goto out;
7411
7412 flags = BTRFS_BLOCK_GROUP_DATA;
7413 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7414 }
7415out:
liuboc59021f2011-03-07 02:13:14 +00007416 return ret;
7417}
7418
liuboacce9522011-01-06 19:30:25 +08007419int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
7420{
7421 return unpin_extent_range(root, start, end);
7422}
7423
7424int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00007425 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08007426{
Li Dongyang5378e602011-03-24 10:24:27 +00007427 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08007428}
Li Dongyangf7039b12011-03-24 10:24:28 +00007429
7430int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
7431{
7432 struct btrfs_fs_info *fs_info = root->fs_info;
7433 struct btrfs_block_group_cache *cache = NULL;
7434 u64 group_trimmed;
7435 u64 start;
7436 u64 end;
7437 u64 trimmed = 0;
7438 int ret = 0;
7439
7440 cache = btrfs_lookup_block_group(fs_info, range->start);
7441
7442 while (cache) {
7443 if (cache->key.objectid >= (range->start + range->len)) {
7444 btrfs_put_block_group(cache);
7445 break;
7446 }
7447
7448 start = max(range->start, cache->key.objectid);
7449 end = min(range->start + range->len,
7450 cache->key.objectid + cache->key.offset);
7451
7452 if (end - start >= range->minlen) {
7453 if (!block_group_cache_done(cache)) {
7454 ret = cache_block_group(cache, NULL, root, 0);
7455 if (!ret)
7456 wait_block_group_cache_done(cache);
7457 }
7458 ret = btrfs_trim_block_group(cache,
7459 &group_trimmed,
7460 start,
7461 end,
7462 range->minlen);
7463
7464 trimmed += group_trimmed;
7465 if (ret) {
7466 btrfs_put_block_group(cache);
7467 break;
7468 }
7469 }
7470
7471 cache = next_block_group(fs_info->tree_root, cache);
7472 }
7473
7474 range->len = trimmed;
7475 return ret;
7476}