blob: 7021dde74d81890c8a717260c1a9a1ca1b92f9c5 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050027#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040031#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040033#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040034#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Chris Mason0e4f8f82011-04-15 16:05:44 -040036/* control flags for do_chunk_alloc's force field
37 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
38 * if we really need one.
39 *
40 * CHUNK_ALLOC_FORCE means it must try to allocate one
41 *
42 * CHUNK_ALLOC_LIMITED means to only try and allocate one
43 * if we have very few chunks already allocated. This is
44 * used as part of the clustering code to help make sure
45 * we have a good pool of storage to cluster in, without
46 * filling the FS with empty chunks
47 *
48 */
49enum {
50 CHUNK_ALLOC_NO_FORCE = 0,
51 CHUNK_ALLOC_FORCE = 1,
52 CHUNK_ALLOC_LIMITED = 2,
53};
54
Josef Bacikf3465ca2008-11-12 14:19:50 -050055static int update_block_group(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040057 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040058static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
59 struct btrfs_root *root,
60 u64 bytenr, u64 num_bytes, u64 parent,
61 u64 root_objectid, u64 owner_objectid,
62 u64 owner_offset, int refs_to_drop,
63 struct btrfs_delayed_extent_op *extra_op);
64static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
65 struct extent_buffer *leaf,
66 struct btrfs_extent_item *ei);
67static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
68 struct btrfs_root *root,
69 u64 parent, u64 root_objectid,
70 u64 flags, u64 owner, u64 offset,
71 struct btrfs_key *ins, int ref_mod);
72static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
73 struct btrfs_root *root,
74 u64 parent, u64 root_objectid,
75 u64 flags, struct btrfs_disk_key *key,
76 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050077static int do_chunk_alloc(struct btrfs_trans_handle *trans,
78 struct btrfs_root *extent_root, u64 alloc_bytes,
79 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040080static int find_next_key(struct btrfs_path *path, int level,
81 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040082static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
83 int dump_block_groups);
Josef Bacik6a632092009-02-20 11:00:09 -050084
Josef Bacik817d52f2009-07-13 21:29:25 -040085static noinline int
86block_group_cache_done(struct btrfs_block_group_cache *cache)
87{
88 smp_mb();
89 return cache->cached == BTRFS_CACHE_FINISHED;
90}
91
Josef Bacik0f9dd462008-09-23 13:14:11 -040092static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
93{
94 return (cache->flags & bits) == bits;
95}
96
David Sterba62a45b62011-04-20 15:52:26 +020097static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +000098{
99 atomic_inc(&cache->count);
100}
101
102void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
103{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400104 if (atomic_dec_and_test(&cache->count)) {
105 WARN_ON(cache->pinned > 0);
106 WARN_ON(cache->reserved > 0);
107 WARN_ON(cache->reserved_pinned > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800108 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000109 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400110 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000111}
112
Josef Bacik0f9dd462008-09-23 13:14:11 -0400113/*
114 * this adds the block group to the fs_info rb tree for the block group
115 * cache
116 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500117static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400118 struct btrfs_block_group_cache *block_group)
119{
120 struct rb_node **p;
121 struct rb_node *parent = NULL;
122 struct btrfs_block_group_cache *cache;
123
124 spin_lock(&info->block_group_cache_lock);
125 p = &info->block_group_cache_tree.rb_node;
126
127 while (*p) {
128 parent = *p;
129 cache = rb_entry(parent, struct btrfs_block_group_cache,
130 cache_node);
131 if (block_group->key.objectid < cache->key.objectid) {
132 p = &(*p)->rb_left;
133 } else if (block_group->key.objectid > cache->key.objectid) {
134 p = &(*p)->rb_right;
135 } else {
136 spin_unlock(&info->block_group_cache_lock);
137 return -EEXIST;
138 }
139 }
140
141 rb_link_node(&block_group->cache_node, parent, p);
142 rb_insert_color(&block_group->cache_node,
143 &info->block_group_cache_tree);
144 spin_unlock(&info->block_group_cache_lock);
145
146 return 0;
147}
148
149/*
150 * This will return the block group at or after bytenr if contains is 0, else
151 * it will return the block group that contains the bytenr
152 */
153static struct btrfs_block_group_cache *
154block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
155 int contains)
156{
157 struct btrfs_block_group_cache *cache, *ret = NULL;
158 struct rb_node *n;
159 u64 end, start;
160
161 spin_lock(&info->block_group_cache_lock);
162 n = info->block_group_cache_tree.rb_node;
163
164 while (n) {
165 cache = rb_entry(n, struct btrfs_block_group_cache,
166 cache_node);
167 end = cache->key.objectid + cache->key.offset - 1;
168 start = cache->key.objectid;
169
170 if (bytenr < start) {
171 if (!contains && (!ret || start < ret->key.objectid))
172 ret = cache;
173 n = n->rb_left;
174 } else if (bytenr > start) {
175 if (contains && bytenr <= end) {
176 ret = cache;
177 break;
178 }
179 n = n->rb_right;
180 } else {
181 ret = cache;
182 break;
183 }
184 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500185 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000186 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400187 spin_unlock(&info->block_group_cache_lock);
188
189 return ret;
190}
191
Yan Zheng11833d62009-09-11 16:11:19 -0400192static int add_excluded_extent(struct btrfs_root *root,
193 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400194{
Yan Zheng11833d62009-09-11 16:11:19 -0400195 u64 end = start + num_bytes - 1;
196 set_extent_bits(&root->fs_info->freed_extents[0],
197 start, end, EXTENT_UPTODATE, GFP_NOFS);
198 set_extent_bits(&root->fs_info->freed_extents[1],
199 start, end, EXTENT_UPTODATE, GFP_NOFS);
200 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400201}
202
Yan Zheng11833d62009-09-11 16:11:19 -0400203static void free_excluded_extents(struct btrfs_root *root,
204 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400205{
Yan Zheng11833d62009-09-11 16:11:19 -0400206 u64 start, end;
207
208 start = cache->key.objectid;
209 end = start + cache->key.offset - 1;
210
211 clear_extent_bits(&root->fs_info->freed_extents[0],
212 start, end, EXTENT_UPTODATE, GFP_NOFS);
213 clear_extent_bits(&root->fs_info->freed_extents[1],
214 start, end, EXTENT_UPTODATE, GFP_NOFS);
215}
216
217static int exclude_super_stripes(struct btrfs_root *root,
218 struct btrfs_block_group_cache *cache)
219{
Josef Bacik817d52f2009-07-13 21:29:25 -0400220 u64 bytenr;
221 u64 *logical;
222 int stripe_len;
223 int i, nr, ret;
224
Yan, Zheng06b23312009-11-26 09:31:11 +0000225 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
226 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
227 cache->bytes_super += stripe_len;
228 ret = add_excluded_extent(root, cache->key.objectid,
229 stripe_len);
230 BUG_ON(ret);
231 }
232
Josef Bacik817d52f2009-07-13 21:29:25 -0400233 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
234 bytenr = btrfs_sb_offset(i);
235 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
236 cache->key.objectid, bytenr,
237 0, &logical, &nr, &stripe_len);
238 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400239
Josef Bacik817d52f2009-07-13 21:29:25 -0400240 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400241 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400242 ret = add_excluded_extent(root, logical[nr],
243 stripe_len);
244 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400245 }
Yan Zheng11833d62009-09-11 16:11:19 -0400246
Josef Bacik817d52f2009-07-13 21:29:25 -0400247 kfree(logical);
248 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400249 return 0;
250}
251
Yan Zheng11833d62009-09-11 16:11:19 -0400252static struct btrfs_caching_control *
253get_caching_control(struct btrfs_block_group_cache *cache)
254{
255 struct btrfs_caching_control *ctl;
256
257 spin_lock(&cache->lock);
258 if (cache->cached != BTRFS_CACHE_STARTED) {
259 spin_unlock(&cache->lock);
260 return NULL;
261 }
262
Josef Bacikdde5abe2010-09-16 16:17:03 -0400263 /* We're loading it the fast way, so we don't have a caching_ctl. */
264 if (!cache->caching_ctl) {
265 spin_unlock(&cache->lock);
266 return NULL;
267 }
268
Yan Zheng11833d62009-09-11 16:11:19 -0400269 ctl = cache->caching_ctl;
270 atomic_inc(&ctl->count);
271 spin_unlock(&cache->lock);
272 return ctl;
273}
274
275static void put_caching_control(struct btrfs_caching_control *ctl)
276{
277 if (atomic_dec_and_test(&ctl->count))
278 kfree(ctl);
279}
280
Josef Bacik0f9dd462008-09-23 13:14:11 -0400281/*
282 * this is only called by cache_block_group, since we could have freed extents
283 * we need to check the pinned_extents for any extents that can't be used yet
284 * since their free space will be released as soon as the transaction commits.
285 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400286static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400287 struct btrfs_fs_info *info, u64 start, u64 end)
288{
Josef Bacik817d52f2009-07-13 21:29:25 -0400289 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400290 int ret;
291
292 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400293 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400294 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400295 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400296 if (ret)
297 break;
298
Yan, Zheng06b23312009-11-26 09:31:11 +0000299 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300 start = extent_end + 1;
301 } else if (extent_start > start && extent_start < end) {
302 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400303 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500304 ret = btrfs_add_free_space(block_group, start,
305 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400306 BUG_ON(ret);
307 start = extent_end + 1;
308 } else {
309 break;
310 }
311 }
312
313 if (start < end) {
314 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400315 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500316 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400317 BUG_ON(ret);
318 }
319
Josef Bacik817d52f2009-07-13 21:29:25 -0400320 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400321}
322
Josef Bacikbab39bf2011-06-30 14:42:28 -0400323static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400324{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400325 struct btrfs_block_group_cache *block_group;
326 struct btrfs_fs_info *fs_info;
327 struct btrfs_caching_control *caching_ctl;
328 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400329 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400330 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400331 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400332 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400333 u64 last = 0;
334 u32 nritems;
335 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400336
Josef Bacikbab39bf2011-06-30 14:42:28 -0400337 caching_ctl = container_of(work, struct btrfs_caching_control, work);
338 block_group = caching_ctl->block_group;
339 fs_info = block_group->fs_info;
340 extent_root = fs_info->extent_root;
341
Chris Masone37c9e62007-05-09 20:13:14 -0400342 path = btrfs_alloc_path();
343 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400344 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400345
Josef Bacik817d52f2009-07-13 21:29:25 -0400346 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400347
Chris Mason5cd57b22008-06-25 16:01:30 -0400348 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400349 * We don't want to deadlock with somebody trying to allocate a new
350 * extent for the extent root while also trying to search the extent
351 * root to add free space. So we skip locking and search the commit
352 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400353 */
354 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400355 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400356 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400357
Yan Zhenge4404d62008-12-12 10:03:26 -0500358 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400359 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400360 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400361again:
Yan Zheng11833d62009-09-11 16:11:19 -0400362 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400363 /* need to make sure the commit_root doesn't disappear */
364 down_read(&fs_info->extent_commit_sem);
365
Yan Zheng11833d62009-09-11 16:11:19 -0400366 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400367 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400368 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500369
Yan Zheng11833d62009-09-11 16:11:19 -0400370 leaf = path->nodes[0];
371 nritems = btrfs_header_nritems(leaf);
372
Chris Masond3977122009-01-05 21:25:51 -0500373 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200374 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400375 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400376 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400377 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400378
Yan Zheng11833d62009-09-11 16:11:19 -0400379 if (path->slots[0] < nritems) {
380 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
381 } else {
382 ret = find_next_key(path, 0, &key);
383 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400384 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400385
Josef Bacik589d8ad2011-05-11 17:30:53 -0400386 if (need_resched() ||
387 btrfs_next_leaf(extent_root, path)) {
388 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400389 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400390 up_read(&fs_info->extent_commit_sem);
391 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400392 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400393 goto again;
394 }
395 leaf = path->nodes[0];
396 nritems = btrfs_header_nritems(leaf);
397 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400398 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400399
Yan Zheng11833d62009-09-11 16:11:19 -0400400 if (key.objectid < block_group->key.objectid) {
401 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400402 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400403 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400404
Chris Masone37c9e62007-05-09 20:13:14 -0400405 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400406 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400407 break;
Yan7d7d6062007-09-14 16:15:28 -0400408
Yan Zheng11833d62009-09-11 16:11:19 -0400409 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400410 total_found += add_new_free_space(block_group,
411 fs_info, last,
412 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400413 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400414
Yan Zheng11833d62009-09-11 16:11:19 -0400415 if (total_found > (1024 * 1024 * 2)) {
416 total_found = 0;
417 wake_up(&caching_ctl->wait);
418 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400419 }
Chris Masone37c9e62007-05-09 20:13:14 -0400420 path->slots[0]++;
421 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400422 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400423
424 total_found += add_new_free_space(block_group, fs_info, last,
425 block_group->key.objectid +
426 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400427 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400428
429 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400430 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400431 block_group->cached = BTRFS_CACHE_FINISHED;
432 spin_unlock(&block_group->lock);
433
Chris Mason54aa1f42007-06-22 14:16:25 -0400434err:
Chris Masone37c9e62007-05-09 20:13:14 -0400435 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400436 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400437
Yan Zheng11833d62009-09-11 16:11:19 -0400438 free_excluded_extents(extent_root, block_group);
439
440 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400441out:
Yan Zheng11833d62009-09-11 16:11:19 -0400442 wake_up(&caching_ctl->wait);
443
444 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000445 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400446}
447
Josef Bacik9d66e232010-08-25 16:54:15 -0400448static int cache_block_group(struct btrfs_block_group_cache *cache,
449 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500450 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400451 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400452{
Yan Zheng11833d62009-09-11 16:11:19 -0400453 struct btrfs_fs_info *fs_info = cache->fs_info;
454 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400455 int ret = 0;
456
Yan Zheng11833d62009-09-11 16:11:19 -0400457 smp_mb();
458 if (cache->cached != BTRFS_CACHE_NO)
459 return 0;
460
Josef Bacik9d66e232010-08-25 16:54:15 -0400461 /*
462 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500463 * to have the normal tree locking. Also if we are currently trying to
464 * allocate blocks for the tree root we can't do the fast caching since
465 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400466 */
Li Dongyangf7039b12011-03-24 10:24:28 +0000467 if (trans && (!trans->transaction->in_commit) &&
Josef Bacikb8399de2010-12-08 09:15:11 -0500468 (root && root != root->fs_info->tree_root)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400469 spin_lock(&cache->lock);
470 if (cache->cached != BTRFS_CACHE_NO) {
471 spin_unlock(&cache->lock);
472 return 0;
473 }
474 cache->cached = BTRFS_CACHE_STARTED;
475 spin_unlock(&cache->lock);
476
477 ret = load_free_space_cache(fs_info, cache);
478
479 spin_lock(&cache->lock);
480 if (ret == 1) {
481 cache->cached = BTRFS_CACHE_FINISHED;
482 cache->last_byte_to_unpin = (u64)-1;
483 } else {
484 cache->cached = BTRFS_CACHE_NO;
485 }
486 spin_unlock(&cache->lock);
Josef Bacik3c148742011-02-02 15:53:47 +0000487 if (ret == 1) {
488 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400489 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000490 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400491 }
492
493 if (load_cache_only)
494 return 0;
495
Miao Xiefc0e4a32011-03-24 11:41:21 +0000496 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -0400497 BUG_ON(!caching_ctl);
498
499 INIT_LIST_HEAD(&caching_ctl->list);
500 mutex_init(&caching_ctl->mutex);
501 init_waitqueue_head(&caching_ctl->wait);
502 caching_ctl->block_group = cache;
503 caching_ctl->progress = cache->key.objectid;
504 /* one for caching kthread, one for caching block group list */
505 atomic_set(&caching_ctl->count, 2);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400506 caching_ctl->work.func = caching_thread;
Yan Zheng11833d62009-09-11 16:11:19 -0400507
Josef Bacik817d52f2009-07-13 21:29:25 -0400508 spin_lock(&cache->lock);
509 if (cache->cached != BTRFS_CACHE_NO) {
510 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400511 kfree(caching_ctl);
512 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400513 }
Yan Zheng11833d62009-09-11 16:11:19 -0400514 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400515 cache->cached = BTRFS_CACHE_STARTED;
516 spin_unlock(&cache->lock);
517
Yan Zheng11833d62009-09-11 16:11:19 -0400518 down_write(&fs_info->extent_commit_sem);
519 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
520 up_write(&fs_info->extent_commit_sem);
521
Josef Bacik11dfe352009-11-13 20:12:59 +0000522 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400523
Josef Bacikbab39bf2011-06-30 14:42:28 -0400524 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400525
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400526 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400527}
528
Josef Bacik0f9dd462008-09-23 13:14:11 -0400529/*
530 * return the block group that starts at or after bytenr
531 */
Chris Masond3977122009-01-05 21:25:51 -0500532static struct btrfs_block_group_cache *
533btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400534{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400535 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400536
Josef Bacik0f9dd462008-09-23 13:14:11 -0400537 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400538
Josef Bacik0f9dd462008-09-23 13:14:11 -0400539 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400540}
541
Josef Bacik0f9dd462008-09-23 13:14:11 -0400542/*
Sankar P9f556842009-05-14 13:52:22 -0400543 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400544 */
Chris Masond3977122009-01-05 21:25:51 -0500545struct btrfs_block_group_cache *btrfs_lookup_block_group(
546 struct btrfs_fs_info *info,
547 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400548{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400549 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400550
Josef Bacik0f9dd462008-09-23 13:14:11 -0400551 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400552
Josef Bacik0f9dd462008-09-23 13:14:11 -0400553 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400554}
Chris Mason0b86a832008-03-24 15:01:56 -0400555
Josef Bacik0f9dd462008-09-23 13:14:11 -0400556static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
557 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400558{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400559 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400560 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400561
Yan, Zhengb742bb82010-05-16 10:46:24 -0400562 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
563 BTRFS_BLOCK_GROUP_METADATA;
564
Chris Mason4184ea72009-03-10 12:39:20 -0400565 rcu_read_lock();
566 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400567 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400568 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400569 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400570 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400571 }
Chris Mason4184ea72009-03-10 12:39:20 -0400572 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400573 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400574}
575
Chris Mason4184ea72009-03-10 12:39:20 -0400576/*
577 * after adding space to the filesystem, we need to clear the full flags
578 * on all the space infos.
579 */
580void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
581{
582 struct list_head *head = &info->space_info;
583 struct btrfs_space_info *found;
584
585 rcu_read_lock();
586 list_for_each_entry_rcu(found, head, list)
587 found->full = 0;
588 rcu_read_unlock();
589}
590
Josef Bacik80eb2342008-10-29 14:49:05 -0400591static u64 div_factor(u64 num, int factor)
592{
593 if (factor == 10)
594 return num;
595 num *= factor;
596 do_div(num, 10);
597 return num;
598}
599
Chris Masone5bc2452010-10-26 13:37:56 -0400600static u64 div_factor_fine(u64 num, int factor)
601{
602 if (factor == 100)
603 return num;
604 num *= factor;
605 do_div(num, 100);
606 return num;
607}
608
Yan Zhengd2fb3432008-12-11 16:30:39 -0500609u64 btrfs_find_block_group(struct btrfs_root *root,
610 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400611{
Chris Mason96b51792007-10-15 16:15:19 -0400612 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400613 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500614 u64 last = max(search_hint, search_start);
615 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400616 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500617 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400618 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400619again:
Zheng Yane8569812008-09-26 10:05:48 -0400620 while (1) {
621 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400622 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400623 break;
Chris Mason96b51792007-10-15 16:15:19 -0400624
Chris Masonc286ac42008-07-22 23:06:41 -0400625 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400626 last = cache->key.objectid + cache->key.offset;
627 used = btrfs_block_group_used(&cache->item);
628
Yan Zhengd2fb3432008-12-11 16:30:39 -0500629 if ((full_search || !cache->ro) &&
630 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400631 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500632 div_factor(cache->key.offset, factor)) {
633 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400634 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400635 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400636 goto found;
637 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400638 }
Chris Masonc286ac42008-07-22 23:06:41 -0400639 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400640 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400641 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400642 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400643 if (!wrapped) {
644 last = search_start;
645 wrapped = 1;
646 goto again;
647 }
648 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400649 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400650 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400651 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400652 goto again;
653 }
Chris Masonbe744172007-05-06 10:15:01 -0400654found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500655 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400656}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400657
Chris Masone02119d2008-09-05 16:13:11 -0400658/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400659int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400660{
661 int ret;
662 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400663 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400664
Zheng Yan31840ae2008-09-23 13:14:14 -0400665 path = btrfs_alloc_path();
666 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400667 key.objectid = start;
668 key.offset = len;
669 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
670 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
671 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400672 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500673 return ret;
674}
675
Chris Masond8d5f3e2007-12-11 12:42:00 -0500676/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400677 * helper function to lookup reference count and flags of extent.
678 *
679 * the head node for delayed ref is used to store the sum of all the
680 * reference count modifications queued up in the rbtree. the head
681 * node may also store the extent flags to set. This way you can check
682 * to see what the reference count and extent flags would be if all of
683 * the delayed refs are not processed.
684 */
685int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
686 struct btrfs_root *root, u64 bytenr,
687 u64 num_bytes, u64 *refs, u64 *flags)
688{
689 struct btrfs_delayed_ref_head *head;
690 struct btrfs_delayed_ref_root *delayed_refs;
691 struct btrfs_path *path;
692 struct btrfs_extent_item *ei;
693 struct extent_buffer *leaf;
694 struct btrfs_key key;
695 u32 item_size;
696 u64 num_refs;
697 u64 extent_flags;
698 int ret;
699
700 path = btrfs_alloc_path();
701 if (!path)
702 return -ENOMEM;
703
704 key.objectid = bytenr;
705 key.type = BTRFS_EXTENT_ITEM_KEY;
706 key.offset = num_bytes;
707 if (!trans) {
708 path->skip_locking = 1;
709 path->search_commit_root = 1;
710 }
711again:
712 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
713 &key, path, 0, 0);
714 if (ret < 0)
715 goto out_free;
716
717 if (ret == 0) {
718 leaf = path->nodes[0];
719 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
720 if (item_size >= sizeof(*ei)) {
721 ei = btrfs_item_ptr(leaf, path->slots[0],
722 struct btrfs_extent_item);
723 num_refs = btrfs_extent_refs(leaf, ei);
724 extent_flags = btrfs_extent_flags(leaf, ei);
725 } else {
726#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
727 struct btrfs_extent_item_v0 *ei0;
728 BUG_ON(item_size != sizeof(*ei0));
729 ei0 = btrfs_item_ptr(leaf, path->slots[0],
730 struct btrfs_extent_item_v0);
731 num_refs = btrfs_extent_refs_v0(leaf, ei0);
732 /* FIXME: this isn't correct for data */
733 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
734#else
735 BUG();
736#endif
737 }
738 BUG_ON(num_refs == 0);
739 } else {
740 num_refs = 0;
741 extent_flags = 0;
742 ret = 0;
743 }
744
745 if (!trans)
746 goto out;
747
748 delayed_refs = &trans->transaction->delayed_refs;
749 spin_lock(&delayed_refs->lock);
750 head = btrfs_find_delayed_ref_head(trans, bytenr);
751 if (head) {
752 if (!mutex_trylock(&head->mutex)) {
753 atomic_inc(&head->node.refs);
754 spin_unlock(&delayed_refs->lock);
755
David Sterbab3b4aa72011-04-21 01:20:15 +0200756 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400757
David Sterba8cc33e52011-05-02 15:29:25 +0200758 /*
759 * Mutex was contended, block until it's released and try
760 * again
761 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400762 mutex_lock(&head->mutex);
763 mutex_unlock(&head->mutex);
764 btrfs_put_delayed_ref(&head->node);
765 goto again;
766 }
767 if (head->extent_op && head->extent_op->update_flags)
768 extent_flags |= head->extent_op->flags_to_set;
769 else
770 BUG_ON(num_refs == 0);
771
772 num_refs += head->node.ref_mod;
773 mutex_unlock(&head->mutex);
774 }
775 spin_unlock(&delayed_refs->lock);
776out:
777 WARN_ON(num_refs == 0);
778 if (refs)
779 *refs = num_refs;
780 if (flags)
781 *flags = extent_flags;
782out_free:
783 btrfs_free_path(path);
784 return ret;
785}
786
787/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500788 * Back reference rules. Back refs have three main goals:
789 *
790 * 1) differentiate between all holders of references to an extent so that
791 * when a reference is dropped we can make sure it was a valid reference
792 * before freeing the extent.
793 *
794 * 2) Provide enough information to quickly find the holders of an extent
795 * if we notice a given block is corrupted or bad.
796 *
797 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
798 * maintenance. This is actually the same as #2, but with a slightly
799 * different use case.
800 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400801 * There are two kinds of back refs. The implicit back refs is optimized
802 * for pointers in non-shared tree blocks. For a given pointer in a block,
803 * back refs of this kind provide information about the block's owner tree
804 * and the pointer's key. These information allow us to find the block by
805 * b-tree searching. The full back refs is for pointers in tree blocks not
806 * referenced by their owner trees. The location of tree block is recorded
807 * in the back refs. Actually the full back refs is generic, and can be
808 * used in all cases the implicit back refs is used. The major shortcoming
809 * of the full back refs is its overhead. Every time a tree block gets
810 * COWed, we have to update back refs entry for all pointers in it.
811 *
812 * For a newly allocated tree block, we use implicit back refs for
813 * pointers in it. This means most tree related operations only involve
814 * implicit back refs. For a tree block created in old transaction, the
815 * only way to drop a reference to it is COW it. So we can detect the
816 * event that tree block loses its owner tree's reference and do the
817 * back refs conversion.
818 *
819 * When a tree block is COW'd through a tree, there are four cases:
820 *
821 * The reference count of the block is one and the tree is the block's
822 * owner tree. Nothing to do in this case.
823 *
824 * The reference count of the block is one and the tree is not the
825 * block's owner tree. In this case, full back refs is used for pointers
826 * in the block. Remove these full back refs, add implicit back refs for
827 * every pointers in the new block.
828 *
829 * The reference count of the block is greater than one and the tree is
830 * the block's owner tree. In this case, implicit back refs is used for
831 * pointers in the block. Add full back refs for every pointers in the
832 * block, increase lower level extents' reference counts. The original
833 * implicit back refs are entailed to the new block.
834 *
835 * The reference count of the block is greater than one and the tree is
836 * not the block's owner tree. Add implicit back refs for every pointer in
837 * the new block, increase lower level extents' reference count.
838 *
839 * Back Reference Key composing:
840 *
841 * The key objectid corresponds to the first byte in the extent,
842 * The key type is used to differentiate between types of back refs.
843 * There are different meanings of the key offset for different types
844 * of back refs.
845 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500846 * File extents can be referenced by:
847 *
848 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400849 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500850 * - different offsets inside a file (bookend extents in file.c)
851 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400852 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500853 *
854 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500855 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400856 * - original offset in the file
857 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400858 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400859 * The key offset for the implicit back refs is hash of the first
860 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500861 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400862 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500863 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400864 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500865 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 * The key offset for the implicit back refs is the first byte of
867 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500868 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400869 * When a file extent is allocated, The implicit back refs is used.
870 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500871 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400872 * (root_key.objectid, inode objectid, offset in file, 1)
873 *
874 * When a file extent is removed file truncation, we find the
875 * corresponding implicit back refs and check the following fields:
876 *
877 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500878 *
879 * Btree extents can be referenced by:
880 *
881 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500882 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400883 * Both the implicit back refs and the full back refs for tree blocks
884 * only consist of key. The key offset for the implicit back refs is
885 * objectid of block's owner tree. The key offset for the full back refs
886 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500887 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400888 * When implicit back refs is used, information about the lowest key and
889 * level of the tree block are required. These information are stored in
890 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500891 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400892
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400893#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
894static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
895 struct btrfs_root *root,
896 struct btrfs_path *path,
897 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500898{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 struct btrfs_extent_item *item;
900 struct btrfs_extent_item_v0 *ei0;
901 struct btrfs_extent_ref_v0 *ref0;
902 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400903 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400904 struct btrfs_key key;
905 struct btrfs_key found_key;
906 u32 new_size = sizeof(*item);
907 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500908 int ret;
909
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400910 leaf = path->nodes[0];
911 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500912
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400913 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
914 ei0 = btrfs_item_ptr(leaf, path->slots[0],
915 struct btrfs_extent_item_v0);
916 refs = btrfs_extent_refs_v0(leaf, ei0);
917
918 if (owner == (u64)-1) {
919 while (1) {
920 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
921 ret = btrfs_next_leaf(root, path);
922 if (ret < 0)
923 return ret;
924 BUG_ON(ret > 0);
925 leaf = path->nodes[0];
926 }
927 btrfs_item_key_to_cpu(leaf, &found_key,
928 path->slots[0]);
929 BUG_ON(key.objectid != found_key.objectid);
930 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
931 path->slots[0]++;
932 continue;
933 }
934 ref0 = btrfs_item_ptr(leaf, path->slots[0],
935 struct btrfs_extent_ref_v0);
936 owner = btrfs_ref_objectid_v0(leaf, ref0);
937 break;
938 }
939 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200940 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400941
942 if (owner < BTRFS_FIRST_FREE_OBJECTID)
943 new_size += sizeof(*bi);
944
945 new_size -= sizeof(*ei0);
946 ret = btrfs_search_slot(trans, root, &key, path,
947 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400948 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 return ret;
950 BUG_ON(ret);
951
952 ret = btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400953
954 leaf = path->nodes[0];
955 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
956 btrfs_set_extent_refs(leaf, item, refs);
957 /* FIXME: get real generation */
958 btrfs_set_extent_generation(leaf, item, 0);
959 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
960 btrfs_set_extent_flags(leaf, item,
961 BTRFS_EXTENT_FLAG_TREE_BLOCK |
962 BTRFS_BLOCK_FLAG_FULL_BACKREF);
963 bi = (struct btrfs_tree_block_info *)(item + 1);
964 /* FIXME: get first key of the block */
965 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
966 btrfs_set_tree_block_level(leaf, bi, (int)owner);
967 } else {
968 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
969 }
970 btrfs_mark_buffer_dirty(leaf);
971 return 0;
972}
973#endif
974
975static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
976{
977 u32 high_crc = ~(u32)0;
978 u32 low_crc = ~(u32)0;
979 __le64 lenum;
980
981 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100982 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400983 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100984 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100986 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400987
988 return ((u64)high_crc << 31) ^ (u64)low_crc;
989}
990
991static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
992 struct btrfs_extent_data_ref *ref)
993{
994 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
995 btrfs_extent_data_ref_objectid(leaf, ref),
996 btrfs_extent_data_ref_offset(leaf, ref));
997}
998
999static int match_extent_data_ref(struct extent_buffer *leaf,
1000 struct btrfs_extent_data_ref *ref,
1001 u64 root_objectid, u64 owner, u64 offset)
1002{
1003 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1004 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1005 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1006 return 0;
1007 return 1;
1008}
1009
1010static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1011 struct btrfs_root *root,
1012 struct btrfs_path *path,
1013 u64 bytenr, u64 parent,
1014 u64 root_objectid,
1015 u64 owner, u64 offset)
1016{
1017 struct btrfs_key key;
1018 struct btrfs_extent_data_ref *ref;
1019 struct extent_buffer *leaf;
1020 u32 nritems;
1021 int ret;
1022 int recow;
1023 int err = -ENOENT;
1024
1025 key.objectid = bytenr;
1026 if (parent) {
1027 key.type = BTRFS_SHARED_DATA_REF_KEY;
1028 key.offset = parent;
1029 } else {
1030 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1031 key.offset = hash_extent_data_ref(root_objectid,
1032 owner, offset);
1033 }
1034again:
1035 recow = 0;
1036 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1037 if (ret < 0) {
1038 err = ret;
1039 goto fail;
1040 }
1041
1042 if (parent) {
1043 if (!ret)
1044 return 0;
1045#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1046 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001047 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001048 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1049 if (ret < 0) {
1050 err = ret;
1051 goto fail;
1052 }
1053 if (!ret)
1054 return 0;
1055#endif
1056 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001057 }
1058
1059 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001060 nritems = btrfs_header_nritems(leaf);
1061 while (1) {
1062 if (path->slots[0] >= nritems) {
1063 ret = btrfs_next_leaf(root, path);
1064 if (ret < 0)
1065 err = ret;
1066 if (ret)
1067 goto fail;
1068
1069 leaf = path->nodes[0];
1070 nritems = btrfs_header_nritems(leaf);
1071 recow = 1;
1072 }
1073
1074 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1075 if (key.objectid != bytenr ||
1076 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1077 goto fail;
1078
1079 ref = btrfs_item_ptr(leaf, path->slots[0],
1080 struct btrfs_extent_data_ref);
1081
1082 if (match_extent_data_ref(leaf, ref, root_objectid,
1083 owner, offset)) {
1084 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001085 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001086 goto again;
1087 }
1088 err = 0;
1089 break;
1090 }
1091 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001092 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001093fail:
1094 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001095}
1096
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001097static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1098 struct btrfs_root *root,
1099 struct btrfs_path *path,
1100 u64 bytenr, u64 parent,
1101 u64 root_objectid, u64 owner,
1102 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001103{
1104 struct btrfs_key key;
1105 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001106 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001107 u32 num_refs;
1108 int ret;
1109
1110 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001111 if (parent) {
1112 key.type = BTRFS_SHARED_DATA_REF_KEY;
1113 key.offset = parent;
1114 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001115 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001116 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1117 key.offset = hash_extent_data_ref(root_objectid,
1118 owner, offset);
1119 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001120 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001121
1122 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1123 if (ret && ret != -EEXIST)
1124 goto fail;
1125
1126 leaf = path->nodes[0];
1127 if (parent) {
1128 struct btrfs_shared_data_ref *ref;
1129 ref = btrfs_item_ptr(leaf, path->slots[0],
1130 struct btrfs_shared_data_ref);
1131 if (ret == 0) {
1132 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1133 } else {
1134 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1135 num_refs += refs_to_add;
1136 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1137 }
1138 } else {
1139 struct btrfs_extent_data_ref *ref;
1140 while (ret == -EEXIST) {
1141 ref = btrfs_item_ptr(leaf, path->slots[0],
1142 struct btrfs_extent_data_ref);
1143 if (match_extent_data_ref(leaf, ref, root_objectid,
1144 owner, offset))
1145 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001146 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001147 key.offset++;
1148 ret = btrfs_insert_empty_item(trans, root, path, &key,
1149 size);
1150 if (ret && ret != -EEXIST)
1151 goto fail;
1152
1153 leaf = path->nodes[0];
1154 }
1155 ref = btrfs_item_ptr(leaf, path->slots[0],
1156 struct btrfs_extent_data_ref);
1157 if (ret == 0) {
1158 btrfs_set_extent_data_ref_root(leaf, ref,
1159 root_objectid);
1160 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1161 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1162 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1163 } else {
1164 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1165 num_refs += refs_to_add;
1166 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1167 }
1168 }
1169 btrfs_mark_buffer_dirty(leaf);
1170 ret = 0;
1171fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001172 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001173 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001174}
1175
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001176static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1177 struct btrfs_root *root,
1178 struct btrfs_path *path,
1179 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001180{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001181 struct btrfs_key key;
1182 struct btrfs_extent_data_ref *ref1 = NULL;
1183 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001184 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001185 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001186 int ret = 0;
1187
1188 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001189 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1190
1191 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1192 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1193 struct btrfs_extent_data_ref);
1194 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1195 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1196 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1197 struct btrfs_shared_data_ref);
1198 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1199#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1200 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1201 struct btrfs_extent_ref_v0 *ref0;
1202 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1203 struct btrfs_extent_ref_v0);
1204 num_refs = btrfs_ref_count_v0(leaf, ref0);
1205#endif
1206 } else {
1207 BUG();
1208 }
1209
Chris Mason56bec292009-03-13 10:10:06 -04001210 BUG_ON(num_refs < refs_to_drop);
1211 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001212
Zheng Yan31840ae2008-09-23 13:14:14 -04001213 if (num_refs == 0) {
1214 ret = btrfs_del_item(trans, root, path);
1215 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001216 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1217 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1218 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1219 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1220#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1221 else {
1222 struct btrfs_extent_ref_v0 *ref0;
1223 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1224 struct btrfs_extent_ref_v0);
1225 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1226 }
1227#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001228 btrfs_mark_buffer_dirty(leaf);
1229 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001230 return ret;
1231}
1232
1233static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1234 struct btrfs_path *path,
1235 struct btrfs_extent_inline_ref *iref)
1236{
1237 struct btrfs_key key;
1238 struct extent_buffer *leaf;
1239 struct btrfs_extent_data_ref *ref1;
1240 struct btrfs_shared_data_ref *ref2;
1241 u32 num_refs = 0;
1242
1243 leaf = path->nodes[0];
1244 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1245 if (iref) {
1246 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1247 BTRFS_EXTENT_DATA_REF_KEY) {
1248 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1249 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1250 } else {
1251 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1252 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1253 }
1254 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1255 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1256 struct btrfs_extent_data_ref);
1257 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1258 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1259 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1260 struct btrfs_shared_data_ref);
1261 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1262#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1263 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1264 struct btrfs_extent_ref_v0 *ref0;
1265 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1266 struct btrfs_extent_ref_v0);
1267 num_refs = btrfs_ref_count_v0(leaf, ref0);
1268#endif
1269 } else {
1270 WARN_ON(1);
1271 }
1272 return num_refs;
1273}
1274
1275static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1276 struct btrfs_root *root,
1277 struct btrfs_path *path,
1278 u64 bytenr, u64 parent,
1279 u64 root_objectid)
1280{
1281 struct btrfs_key key;
1282 int ret;
1283
1284 key.objectid = bytenr;
1285 if (parent) {
1286 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1287 key.offset = parent;
1288 } else {
1289 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1290 key.offset = root_objectid;
1291 }
1292
1293 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1294 if (ret > 0)
1295 ret = -ENOENT;
1296#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1297 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001298 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001299 key.type = BTRFS_EXTENT_REF_V0_KEY;
1300 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1301 if (ret > 0)
1302 ret = -ENOENT;
1303 }
1304#endif
1305 return ret;
1306}
1307
1308static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1309 struct btrfs_root *root,
1310 struct btrfs_path *path,
1311 u64 bytenr, u64 parent,
1312 u64 root_objectid)
1313{
1314 struct btrfs_key key;
1315 int ret;
1316
1317 key.objectid = bytenr;
1318 if (parent) {
1319 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1320 key.offset = parent;
1321 } else {
1322 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1323 key.offset = root_objectid;
1324 }
1325
1326 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001327 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001328 return ret;
1329}
1330
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331static inline int extent_ref_type(u64 parent, u64 owner)
1332{
1333 int type;
1334 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1335 if (parent > 0)
1336 type = BTRFS_SHARED_BLOCK_REF_KEY;
1337 else
1338 type = BTRFS_TREE_BLOCK_REF_KEY;
1339 } else {
1340 if (parent > 0)
1341 type = BTRFS_SHARED_DATA_REF_KEY;
1342 else
1343 type = BTRFS_EXTENT_DATA_REF_KEY;
1344 }
1345 return type;
1346}
1347
Yan Zheng2c47e6052009-06-27 21:07:35 -04001348static int find_next_key(struct btrfs_path *path, int level,
1349 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001350
1351{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001352 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001353 if (!path->nodes[level])
1354 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001355 if (path->slots[level] + 1 >=
1356 btrfs_header_nritems(path->nodes[level]))
1357 continue;
1358 if (level == 0)
1359 btrfs_item_key_to_cpu(path->nodes[level], key,
1360 path->slots[level] + 1);
1361 else
1362 btrfs_node_key_to_cpu(path->nodes[level], key,
1363 path->slots[level] + 1);
1364 return 0;
1365 }
1366 return 1;
1367}
1368
1369/*
1370 * look for inline back ref. if back ref is found, *ref_ret is set
1371 * to the address of inline back ref, and 0 is returned.
1372 *
1373 * if back ref isn't found, *ref_ret is set to the address where it
1374 * should be inserted, and -ENOENT is returned.
1375 *
1376 * if insert is true and there are too many inline back refs, the path
1377 * points to the extent item, and -EAGAIN is returned.
1378 *
1379 * NOTE: inline back refs are ordered in the same way that back ref
1380 * items in the tree are ordered.
1381 */
1382static noinline_for_stack
1383int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1384 struct btrfs_root *root,
1385 struct btrfs_path *path,
1386 struct btrfs_extent_inline_ref **ref_ret,
1387 u64 bytenr, u64 num_bytes,
1388 u64 parent, u64 root_objectid,
1389 u64 owner, u64 offset, int insert)
1390{
1391 struct btrfs_key key;
1392 struct extent_buffer *leaf;
1393 struct btrfs_extent_item *ei;
1394 struct btrfs_extent_inline_ref *iref;
1395 u64 flags;
1396 u64 item_size;
1397 unsigned long ptr;
1398 unsigned long end;
1399 int extra_size;
1400 int type;
1401 int want;
1402 int ret;
1403 int err = 0;
1404
1405 key.objectid = bytenr;
1406 key.type = BTRFS_EXTENT_ITEM_KEY;
1407 key.offset = num_bytes;
1408
1409 want = extent_ref_type(parent, owner);
1410 if (insert) {
1411 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001412 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001413 } else
1414 extra_size = -1;
1415 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1416 if (ret < 0) {
1417 err = ret;
1418 goto out;
1419 }
1420 BUG_ON(ret);
1421
1422 leaf = path->nodes[0];
1423 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1424#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1425 if (item_size < sizeof(*ei)) {
1426 if (!insert) {
1427 err = -ENOENT;
1428 goto out;
1429 }
1430 ret = convert_extent_item_v0(trans, root, path, owner,
1431 extra_size);
1432 if (ret < 0) {
1433 err = ret;
1434 goto out;
1435 }
1436 leaf = path->nodes[0];
1437 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1438 }
1439#endif
1440 BUG_ON(item_size < sizeof(*ei));
1441
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001442 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1443 flags = btrfs_extent_flags(leaf, ei);
1444
1445 ptr = (unsigned long)(ei + 1);
1446 end = (unsigned long)ei + item_size;
1447
1448 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1449 ptr += sizeof(struct btrfs_tree_block_info);
1450 BUG_ON(ptr > end);
1451 } else {
1452 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1453 }
1454
1455 err = -ENOENT;
1456 while (1) {
1457 if (ptr >= end) {
1458 WARN_ON(ptr > end);
1459 break;
1460 }
1461 iref = (struct btrfs_extent_inline_ref *)ptr;
1462 type = btrfs_extent_inline_ref_type(leaf, iref);
1463 if (want < type)
1464 break;
1465 if (want > type) {
1466 ptr += btrfs_extent_inline_ref_size(type);
1467 continue;
1468 }
1469
1470 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1471 struct btrfs_extent_data_ref *dref;
1472 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1473 if (match_extent_data_ref(leaf, dref, root_objectid,
1474 owner, offset)) {
1475 err = 0;
1476 break;
1477 }
1478 if (hash_extent_data_ref_item(leaf, dref) <
1479 hash_extent_data_ref(root_objectid, owner, offset))
1480 break;
1481 } else {
1482 u64 ref_offset;
1483 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1484 if (parent > 0) {
1485 if (parent == ref_offset) {
1486 err = 0;
1487 break;
1488 }
1489 if (ref_offset < parent)
1490 break;
1491 } else {
1492 if (root_objectid == ref_offset) {
1493 err = 0;
1494 break;
1495 }
1496 if (ref_offset < root_objectid)
1497 break;
1498 }
1499 }
1500 ptr += btrfs_extent_inline_ref_size(type);
1501 }
1502 if (err == -ENOENT && insert) {
1503 if (item_size + extra_size >=
1504 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1505 err = -EAGAIN;
1506 goto out;
1507 }
1508 /*
1509 * To add new inline back ref, we have to make sure
1510 * there is no corresponding back ref item.
1511 * For simplicity, we just do not add new inline back
1512 * ref if there is any kind of item for this block
1513 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001514 if (find_next_key(path, 0, &key) == 0 &&
1515 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001516 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001517 err = -EAGAIN;
1518 goto out;
1519 }
1520 }
1521 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1522out:
Yan Zheng85d41982009-06-11 08:51:10 -04001523 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001524 path->keep_locks = 0;
1525 btrfs_unlock_up_safe(path, 1);
1526 }
1527 return err;
1528}
1529
1530/*
1531 * helper to add new inline back ref
1532 */
1533static noinline_for_stack
1534int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1535 struct btrfs_root *root,
1536 struct btrfs_path *path,
1537 struct btrfs_extent_inline_ref *iref,
1538 u64 parent, u64 root_objectid,
1539 u64 owner, u64 offset, int refs_to_add,
1540 struct btrfs_delayed_extent_op *extent_op)
1541{
1542 struct extent_buffer *leaf;
1543 struct btrfs_extent_item *ei;
1544 unsigned long ptr;
1545 unsigned long end;
1546 unsigned long item_offset;
1547 u64 refs;
1548 int size;
1549 int type;
1550 int ret;
1551
1552 leaf = path->nodes[0];
1553 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1554 item_offset = (unsigned long)iref - (unsigned long)ei;
1555
1556 type = extent_ref_type(parent, owner);
1557 size = btrfs_extent_inline_ref_size(type);
1558
1559 ret = btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001560
1561 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1562 refs = btrfs_extent_refs(leaf, ei);
1563 refs += refs_to_add;
1564 btrfs_set_extent_refs(leaf, ei, refs);
1565 if (extent_op)
1566 __run_delayed_extent_op(extent_op, leaf, ei);
1567
1568 ptr = (unsigned long)ei + item_offset;
1569 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1570 if (ptr < end - size)
1571 memmove_extent_buffer(leaf, ptr + size, ptr,
1572 end - size - ptr);
1573
1574 iref = (struct btrfs_extent_inline_ref *)ptr;
1575 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1576 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1577 struct btrfs_extent_data_ref *dref;
1578 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1579 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1580 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1581 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1582 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1583 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1584 struct btrfs_shared_data_ref *sref;
1585 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1586 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1587 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1588 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1589 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1590 } else {
1591 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1592 }
1593 btrfs_mark_buffer_dirty(leaf);
1594 return 0;
1595}
1596
1597static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1598 struct btrfs_root *root,
1599 struct btrfs_path *path,
1600 struct btrfs_extent_inline_ref **ref_ret,
1601 u64 bytenr, u64 num_bytes, u64 parent,
1602 u64 root_objectid, u64 owner, u64 offset)
1603{
1604 int ret;
1605
1606 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1607 bytenr, num_bytes, parent,
1608 root_objectid, owner, offset, 0);
1609 if (ret != -ENOENT)
1610 return ret;
1611
David Sterbab3b4aa72011-04-21 01:20:15 +02001612 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001613 *ref_ret = NULL;
1614
1615 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1616 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1617 root_objectid);
1618 } else {
1619 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1620 root_objectid, owner, offset);
1621 }
1622 return ret;
1623}
1624
1625/*
1626 * helper to update/remove inline back ref
1627 */
1628static noinline_for_stack
1629int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1630 struct btrfs_root *root,
1631 struct btrfs_path *path,
1632 struct btrfs_extent_inline_ref *iref,
1633 int refs_to_mod,
1634 struct btrfs_delayed_extent_op *extent_op)
1635{
1636 struct extent_buffer *leaf;
1637 struct btrfs_extent_item *ei;
1638 struct btrfs_extent_data_ref *dref = NULL;
1639 struct btrfs_shared_data_ref *sref = NULL;
1640 unsigned long ptr;
1641 unsigned long end;
1642 u32 item_size;
1643 int size;
1644 int type;
1645 int ret;
1646 u64 refs;
1647
1648 leaf = path->nodes[0];
1649 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1650 refs = btrfs_extent_refs(leaf, ei);
1651 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1652 refs += refs_to_mod;
1653 btrfs_set_extent_refs(leaf, ei, refs);
1654 if (extent_op)
1655 __run_delayed_extent_op(extent_op, leaf, ei);
1656
1657 type = btrfs_extent_inline_ref_type(leaf, iref);
1658
1659 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1660 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1661 refs = btrfs_extent_data_ref_count(leaf, dref);
1662 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1663 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1664 refs = btrfs_shared_data_ref_count(leaf, sref);
1665 } else {
1666 refs = 1;
1667 BUG_ON(refs_to_mod != -1);
1668 }
1669
1670 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1671 refs += refs_to_mod;
1672
1673 if (refs > 0) {
1674 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1675 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1676 else
1677 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1678 } else {
1679 size = btrfs_extent_inline_ref_size(type);
1680 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1681 ptr = (unsigned long)iref;
1682 end = (unsigned long)ei + item_size;
1683 if (ptr + size < end)
1684 memmove_extent_buffer(leaf, ptr, ptr + size,
1685 end - ptr - size);
1686 item_size -= size;
1687 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001688 }
1689 btrfs_mark_buffer_dirty(leaf);
1690 return 0;
1691}
1692
1693static noinline_for_stack
1694int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1695 struct btrfs_root *root,
1696 struct btrfs_path *path,
1697 u64 bytenr, u64 num_bytes, u64 parent,
1698 u64 root_objectid, u64 owner,
1699 u64 offset, int refs_to_add,
1700 struct btrfs_delayed_extent_op *extent_op)
1701{
1702 struct btrfs_extent_inline_ref *iref;
1703 int ret;
1704
1705 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1706 bytenr, num_bytes, parent,
1707 root_objectid, owner, offset, 1);
1708 if (ret == 0) {
1709 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1710 ret = update_inline_extent_backref(trans, root, path, iref,
1711 refs_to_add, extent_op);
1712 } else if (ret == -ENOENT) {
1713 ret = setup_inline_extent_backref(trans, root, path, iref,
1714 parent, root_objectid,
1715 owner, offset, refs_to_add,
1716 extent_op);
1717 }
1718 return ret;
1719}
1720
1721static int insert_extent_backref(struct btrfs_trans_handle *trans,
1722 struct btrfs_root *root,
1723 struct btrfs_path *path,
1724 u64 bytenr, u64 parent, u64 root_objectid,
1725 u64 owner, u64 offset, int refs_to_add)
1726{
1727 int ret;
1728 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1729 BUG_ON(refs_to_add != 1);
1730 ret = insert_tree_block_ref(trans, root, path, bytenr,
1731 parent, root_objectid);
1732 } else {
1733 ret = insert_extent_data_ref(trans, root, path, bytenr,
1734 parent, root_objectid,
1735 owner, offset, refs_to_add);
1736 }
1737 return ret;
1738}
1739
1740static int remove_extent_backref(struct btrfs_trans_handle *trans,
1741 struct btrfs_root *root,
1742 struct btrfs_path *path,
1743 struct btrfs_extent_inline_ref *iref,
1744 int refs_to_drop, int is_data)
1745{
1746 int ret;
1747
1748 BUG_ON(!is_data && refs_to_drop != 1);
1749 if (iref) {
1750 ret = update_inline_extent_backref(trans, root, path, iref,
1751 -refs_to_drop, NULL);
1752 } else if (is_data) {
1753 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1754 } else {
1755 ret = btrfs_del_item(trans, root, path);
1756 }
1757 return ret;
1758}
1759
Li Dongyang5378e602011-03-24 10:24:27 +00001760static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001761 u64 start, u64 len)
1762{
Li Dongyang5378e602011-03-24 10:24:27 +00001763 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001764}
Chris Mason15916de2008-11-19 21:17:22 -05001765
Liu Hui1f3c79a2009-01-05 15:57:51 -05001766static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001767 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001768{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001769 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001770 u64 discarded_bytes = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001771 struct btrfs_multi_bio *multi = NULL;
1772
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001773
Liu Hui1f3c79a2009-01-05 15:57:51 -05001774 /* Tell the block device(s) that the sectors can be discarded */
Li Dongyang5378e602011-03-24 10:24:27 +00001775 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1776 bytenr, &num_bytes, &multi, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001777 if (!ret) {
1778 struct btrfs_bio_stripe *stripe = multi->stripes;
1779 int i;
1780
Liu Hui1f3c79a2009-01-05 15:57:51 -05001781
1782 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Li Dongyang5378e602011-03-24 10:24:27 +00001783 ret = btrfs_issue_discard(stripe->dev->bdev,
1784 stripe->physical,
1785 stripe->length);
1786 if (!ret)
1787 discarded_bytes += stripe->length;
1788 else if (ret != -EOPNOTSUPP)
1789 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001790 }
1791 kfree(multi);
1792 }
Li Dongyang5378e602011-03-24 10:24:27 +00001793 if (discarded_bytes && ret == -EOPNOTSUPP)
1794 ret = 0;
1795
1796 if (actual_bytes)
1797 *actual_bytes = discarded_bytes;
1798
Liu Hui1f3c79a2009-01-05 15:57:51 -05001799
1800 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001801}
1802
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001803int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1804 struct btrfs_root *root,
1805 u64 bytenr, u64 num_bytes, u64 parent,
1806 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001807{
1808 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001809 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1810 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001811
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001812 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1813 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1814 parent, root_objectid, (int)owner,
1815 BTRFS_ADD_DELAYED_REF, NULL);
1816 } else {
1817 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1818 parent, root_objectid, owner, offset,
1819 BTRFS_ADD_DELAYED_REF, NULL);
1820 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001821 return ret;
1822}
1823
Chris Mason925baed2008-06-25 16:01:30 -04001824static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001825 struct btrfs_root *root,
1826 u64 bytenr, u64 num_bytes,
1827 u64 parent, u64 root_objectid,
1828 u64 owner, u64 offset, int refs_to_add,
1829 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001830{
Chris Mason5caf2a02007-04-02 11:20:42 -04001831 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001832 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001833 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001834 u64 refs;
1835 int ret;
1836 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001837
Chris Mason5caf2a02007-04-02 11:20:42 -04001838 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001839 if (!path)
1840 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001841
Chris Mason3c12ac72008-04-21 12:01:38 -04001842 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001843 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001844 /* this will setup the path even if it fails to insert the back ref */
1845 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1846 path, bytenr, num_bytes, parent,
1847 root_objectid, owner, offset,
1848 refs_to_add, extent_op);
1849 if (ret == 0)
1850 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001851
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001852 if (ret != -EAGAIN) {
1853 err = ret;
1854 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001855 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001856
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001857 leaf = path->nodes[0];
1858 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1859 refs = btrfs_extent_refs(leaf, item);
1860 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1861 if (extent_op)
1862 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001863
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001864 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001865 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001866
Chris Mason3c12ac72008-04-21 12:01:38 -04001867 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001868 path->leave_spinning = 1;
1869
Chris Mason56bec292009-03-13 10:10:06 -04001870 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001871 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001872 path, bytenr, parent, root_objectid,
1873 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001874 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001875out:
Chris Mason74493f72007-12-11 09:25:06 -05001876 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001877 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001878}
1879
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001880static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1881 struct btrfs_root *root,
1882 struct btrfs_delayed_ref_node *node,
1883 struct btrfs_delayed_extent_op *extent_op,
1884 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001885{
Chris Mason56bec292009-03-13 10:10:06 -04001886 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001887 struct btrfs_delayed_data_ref *ref;
1888 struct btrfs_key ins;
1889 u64 parent = 0;
1890 u64 ref_root = 0;
1891 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001892
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001893 ins.objectid = node->bytenr;
1894 ins.offset = node->num_bytes;
1895 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001896
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001897 ref = btrfs_delayed_node_to_data_ref(node);
1898 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1899 parent = ref->parent;
1900 else
1901 ref_root = ref->root;
1902
1903 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1904 if (extent_op) {
1905 BUG_ON(extent_op->update_key);
1906 flags |= extent_op->flags_to_set;
1907 }
1908 ret = alloc_reserved_file_extent(trans, root,
1909 parent, ref_root, flags,
1910 ref->objectid, ref->offset,
1911 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001912 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1913 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1914 node->num_bytes, parent,
1915 ref_root, ref->objectid,
1916 ref->offset, node->ref_mod,
1917 extent_op);
1918 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1919 ret = __btrfs_free_extent(trans, root, node->bytenr,
1920 node->num_bytes, parent,
1921 ref_root, ref->objectid,
1922 ref->offset, node->ref_mod,
1923 extent_op);
1924 } else {
1925 BUG();
1926 }
Chris Mason56bec292009-03-13 10:10:06 -04001927 return ret;
1928}
1929
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001930static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1931 struct extent_buffer *leaf,
1932 struct btrfs_extent_item *ei)
1933{
1934 u64 flags = btrfs_extent_flags(leaf, ei);
1935 if (extent_op->update_flags) {
1936 flags |= extent_op->flags_to_set;
1937 btrfs_set_extent_flags(leaf, ei, flags);
1938 }
1939
1940 if (extent_op->update_key) {
1941 struct btrfs_tree_block_info *bi;
1942 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1943 bi = (struct btrfs_tree_block_info *)(ei + 1);
1944 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1945 }
1946}
1947
1948static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1949 struct btrfs_root *root,
1950 struct btrfs_delayed_ref_node *node,
1951 struct btrfs_delayed_extent_op *extent_op)
1952{
1953 struct btrfs_key key;
1954 struct btrfs_path *path;
1955 struct btrfs_extent_item *ei;
1956 struct extent_buffer *leaf;
1957 u32 item_size;
1958 int ret;
1959 int err = 0;
1960
1961 path = btrfs_alloc_path();
1962 if (!path)
1963 return -ENOMEM;
1964
1965 key.objectid = node->bytenr;
1966 key.type = BTRFS_EXTENT_ITEM_KEY;
1967 key.offset = node->num_bytes;
1968
1969 path->reada = 1;
1970 path->leave_spinning = 1;
1971 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1972 path, 0, 1);
1973 if (ret < 0) {
1974 err = ret;
1975 goto out;
1976 }
1977 if (ret > 0) {
1978 err = -EIO;
1979 goto out;
1980 }
1981
1982 leaf = path->nodes[0];
1983 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1984#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1985 if (item_size < sizeof(*ei)) {
1986 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1987 path, (u64)-1, 0);
1988 if (ret < 0) {
1989 err = ret;
1990 goto out;
1991 }
1992 leaf = path->nodes[0];
1993 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1994 }
1995#endif
1996 BUG_ON(item_size < sizeof(*ei));
1997 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1998 __run_delayed_extent_op(extent_op, leaf, ei);
1999
2000 btrfs_mark_buffer_dirty(leaf);
2001out:
2002 btrfs_free_path(path);
2003 return err;
2004}
2005
2006static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2007 struct btrfs_root *root,
2008 struct btrfs_delayed_ref_node *node,
2009 struct btrfs_delayed_extent_op *extent_op,
2010 int insert_reserved)
2011{
2012 int ret = 0;
2013 struct btrfs_delayed_tree_ref *ref;
2014 struct btrfs_key ins;
2015 u64 parent = 0;
2016 u64 ref_root = 0;
2017
2018 ins.objectid = node->bytenr;
2019 ins.offset = node->num_bytes;
2020 ins.type = BTRFS_EXTENT_ITEM_KEY;
2021
2022 ref = btrfs_delayed_node_to_tree_ref(node);
2023 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2024 parent = ref->parent;
2025 else
2026 ref_root = ref->root;
2027
2028 BUG_ON(node->ref_mod != 1);
2029 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2030 BUG_ON(!extent_op || !extent_op->update_flags ||
2031 !extent_op->update_key);
2032 ret = alloc_reserved_tree_block(trans, root,
2033 parent, ref_root,
2034 extent_op->flags_to_set,
2035 &extent_op->key,
2036 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002037 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2038 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2039 node->num_bytes, parent, ref_root,
2040 ref->level, 0, 1, extent_op);
2041 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2042 ret = __btrfs_free_extent(trans, root, node->bytenr,
2043 node->num_bytes, parent, ref_root,
2044 ref->level, 0, 1, extent_op);
2045 } else {
2046 BUG();
2047 }
2048 return ret;
2049}
2050
Chris Mason56bec292009-03-13 10:10:06 -04002051/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002052static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2053 struct btrfs_root *root,
2054 struct btrfs_delayed_ref_node *node,
2055 struct btrfs_delayed_extent_op *extent_op,
2056 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002057{
Josef Bacikeb099672009-02-12 09:27:38 -05002058 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002059 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002060 struct btrfs_delayed_ref_head *head;
2061 /*
2062 * we've hit the end of the chain and we were supposed
2063 * to insert this extent into the tree. But, it got
2064 * deleted before we ever needed to insert it, so all
2065 * we have to do is clean up the accounting
2066 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002067 BUG_ON(extent_op);
2068 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002069 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002070 btrfs_pin_extent(root, node->bytenr,
2071 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002072 if (head->is_data) {
2073 ret = btrfs_del_csums(trans, root,
2074 node->bytenr,
2075 node->num_bytes);
2076 BUG_ON(ret);
2077 }
Chris Mason56bec292009-03-13 10:10:06 -04002078 }
Chris Mason56bec292009-03-13 10:10:06 -04002079 mutex_unlock(&head->mutex);
2080 return 0;
2081 }
Josef Bacikeb099672009-02-12 09:27:38 -05002082
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002083 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2084 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2085 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2086 insert_reserved);
2087 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2088 node->type == BTRFS_SHARED_DATA_REF_KEY)
2089 ret = run_delayed_data_ref(trans, root, node, extent_op,
2090 insert_reserved);
2091 else
2092 BUG();
2093 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002094}
2095
Chris Mason56bec292009-03-13 10:10:06 -04002096static noinline struct btrfs_delayed_ref_node *
2097select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002098{
Chris Mason56bec292009-03-13 10:10:06 -04002099 struct rb_node *node;
2100 struct btrfs_delayed_ref_node *ref;
2101 int action = BTRFS_ADD_DELAYED_REF;
2102again:
2103 /*
2104 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2105 * this prevents ref count from going down to zero when
2106 * there still are pending delayed ref.
2107 */
2108 node = rb_prev(&head->node.rb_node);
2109 while (1) {
2110 if (!node)
2111 break;
2112 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2113 rb_node);
2114 if (ref->bytenr != head->node.bytenr)
2115 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002116 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002117 return ref;
2118 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002119 }
Chris Mason56bec292009-03-13 10:10:06 -04002120 if (action == BTRFS_ADD_DELAYED_REF) {
2121 action = BTRFS_DROP_DELAYED_REF;
2122 goto again;
2123 }
2124 return NULL;
2125}
2126
Chris Masonc3e69d52009-03-13 10:17:05 -04002127static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2128 struct btrfs_root *root,
2129 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002130{
Chris Mason56bec292009-03-13 10:10:06 -04002131 struct btrfs_delayed_ref_root *delayed_refs;
2132 struct btrfs_delayed_ref_node *ref;
2133 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002134 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002135 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002136 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002137 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002138
2139 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002140 while (1) {
2141 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002142 /* pick a new head ref from the cluster list */
2143 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002144 break;
Chris Mason56bec292009-03-13 10:10:06 -04002145
Chris Masonc3e69d52009-03-13 10:17:05 -04002146 locked_ref = list_entry(cluster->next,
2147 struct btrfs_delayed_ref_head, cluster);
2148
2149 /* grab the lock that says we are going to process
2150 * all the refs for this head */
2151 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2152
2153 /*
2154 * we may have dropped the spin lock to get the head
2155 * mutex lock, and that might have given someone else
2156 * time to free the head. If that's true, it has been
2157 * removed from our list and we can move on.
2158 */
2159 if (ret == -EAGAIN) {
2160 locked_ref = NULL;
2161 count++;
2162 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002163 }
2164 }
2165
2166 /*
2167 * record the must insert reserved flag before we
2168 * drop the spin lock.
2169 */
2170 must_insert_reserved = locked_ref->must_insert_reserved;
2171 locked_ref->must_insert_reserved = 0;
2172
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002173 extent_op = locked_ref->extent_op;
2174 locked_ref->extent_op = NULL;
2175
Chris Mason56bec292009-03-13 10:10:06 -04002176 /*
2177 * locked_ref is the head node, so we have to go one
2178 * node back for any delayed ref updates
2179 */
Chris Mason56bec292009-03-13 10:10:06 -04002180 ref = select_delayed_ref(locked_ref);
2181 if (!ref) {
2182 /* All delayed refs have been processed, Go ahead
2183 * and send the head node to run_one_delayed_ref,
2184 * so that any accounting fixes can happen
2185 */
2186 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002187
2188 if (extent_op && must_insert_reserved) {
2189 kfree(extent_op);
2190 extent_op = NULL;
2191 }
2192
2193 if (extent_op) {
2194 spin_unlock(&delayed_refs->lock);
2195
2196 ret = run_delayed_extent_op(trans, root,
2197 ref, extent_op);
2198 BUG_ON(ret);
2199 kfree(extent_op);
2200
2201 cond_resched();
2202 spin_lock(&delayed_refs->lock);
2203 continue;
2204 }
2205
Chris Masonc3e69d52009-03-13 10:17:05 -04002206 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002207 locked_ref = NULL;
2208 }
2209
2210 ref->in_tree = 0;
2211 rb_erase(&ref->rb_node, &delayed_refs->root);
2212 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002213
Chris Mason56bec292009-03-13 10:10:06 -04002214 spin_unlock(&delayed_refs->lock);
2215
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002216 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002217 must_insert_reserved);
2218 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002219
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002220 btrfs_put_delayed_ref(ref);
2221 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002222 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002223
Chris Mason1887be62009-03-13 10:11:24 -04002224 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002225 spin_lock(&delayed_refs->lock);
2226 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002227 return count;
2228}
2229
2230/*
2231 * this starts processing the delayed reference count updates and
2232 * extent insertions we have queued up so far. count can be
2233 * 0, which means to process everything in the tree at the start
2234 * of the run (but not newly added entries), or it can be some target
2235 * number you'd like to process.
2236 */
2237int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2238 struct btrfs_root *root, unsigned long count)
2239{
2240 struct rb_node *node;
2241 struct btrfs_delayed_ref_root *delayed_refs;
2242 struct btrfs_delayed_ref_node *ref;
2243 struct list_head cluster;
2244 int ret;
2245 int run_all = count == (unsigned long)-1;
2246 int run_most = 0;
2247
2248 if (root == root->fs_info->extent_root)
2249 root = root->fs_info->tree_root;
2250
2251 delayed_refs = &trans->transaction->delayed_refs;
2252 INIT_LIST_HEAD(&cluster);
2253again:
2254 spin_lock(&delayed_refs->lock);
2255 if (count == 0) {
2256 count = delayed_refs->num_entries * 2;
2257 run_most = 1;
2258 }
2259 while (1) {
2260 if (!(run_all || run_most) &&
2261 delayed_refs->num_heads_ready < 64)
2262 break;
2263
2264 /*
2265 * go find something we can process in the rbtree. We start at
2266 * the beginning of the tree, and then build a cluster
2267 * of refs to process starting at the first one we are able to
2268 * lock
2269 */
2270 ret = btrfs_find_ref_cluster(trans, &cluster,
2271 delayed_refs->run_delayed_start);
2272 if (ret)
2273 break;
2274
2275 ret = run_clustered_refs(trans, root, &cluster);
2276 BUG_ON(ret < 0);
2277
2278 count -= min_t(unsigned long, ret, count);
2279
2280 if (count == 0)
2281 break;
2282 }
2283
Chris Mason56bec292009-03-13 10:10:06 -04002284 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002285 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002286 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002287 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002288 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002289
2290 while (node) {
2291 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2292 rb_node);
2293 if (btrfs_delayed_ref_is_head(ref)) {
2294 struct btrfs_delayed_ref_head *head;
2295
2296 head = btrfs_delayed_node_to_head(ref);
2297 atomic_inc(&ref->refs);
2298
2299 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002300 /*
2301 * Mutex was contended, block until it's
2302 * released and try again
2303 */
Chris Mason56bec292009-03-13 10:10:06 -04002304 mutex_lock(&head->mutex);
2305 mutex_unlock(&head->mutex);
2306
2307 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002308 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002309 goto again;
2310 }
2311 node = rb_next(node);
2312 }
2313 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002314 schedule_timeout(1);
2315 goto again;
2316 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002317out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002318 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002319 return 0;
2320}
2321
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002322int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2323 struct btrfs_root *root,
2324 u64 bytenr, u64 num_bytes, u64 flags,
2325 int is_data)
2326{
2327 struct btrfs_delayed_extent_op *extent_op;
2328 int ret;
2329
2330 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2331 if (!extent_op)
2332 return -ENOMEM;
2333
2334 extent_op->flags_to_set = flags;
2335 extent_op->update_flags = 1;
2336 extent_op->update_key = 0;
2337 extent_op->is_data = is_data ? 1 : 0;
2338
2339 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2340 if (ret)
2341 kfree(extent_op);
2342 return ret;
2343}
2344
2345static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2346 struct btrfs_root *root,
2347 struct btrfs_path *path,
2348 u64 objectid, u64 offset, u64 bytenr)
2349{
2350 struct btrfs_delayed_ref_head *head;
2351 struct btrfs_delayed_ref_node *ref;
2352 struct btrfs_delayed_data_ref *data_ref;
2353 struct btrfs_delayed_ref_root *delayed_refs;
2354 struct rb_node *node;
2355 int ret = 0;
2356
2357 ret = -ENOENT;
2358 delayed_refs = &trans->transaction->delayed_refs;
2359 spin_lock(&delayed_refs->lock);
2360 head = btrfs_find_delayed_ref_head(trans, bytenr);
2361 if (!head)
2362 goto out;
2363
2364 if (!mutex_trylock(&head->mutex)) {
2365 atomic_inc(&head->node.refs);
2366 spin_unlock(&delayed_refs->lock);
2367
David Sterbab3b4aa72011-04-21 01:20:15 +02002368 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002369
David Sterba8cc33e52011-05-02 15:29:25 +02002370 /*
2371 * Mutex was contended, block until it's released and let
2372 * caller try again
2373 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002374 mutex_lock(&head->mutex);
2375 mutex_unlock(&head->mutex);
2376 btrfs_put_delayed_ref(&head->node);
2377 return -EAGAIN;
2378 }
2379
2380 node = rb_prev(&head->node.rb_node);
2381 if (!node)
2382 goto out_unlock;
2383
2384 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2385
2386 if (ref->bytenr != bytenr)
2387 goto out_unlock;
2388
2389 ret = 1;
2390 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2391 goto out_unlock;
2392
2393 data_ref = btrfs_delayed_node_to_data_ref(ref);
2394
2395 node = rb_prev(node);
2396 if (node) {
2397 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2398 if (ref->bytenr == bytenr)
2399 goto out_unlock;
2400 }
2401
2402 if (data_ref->root != root->root_key.objectid ||
2403 data_ref->objectid != objectid || data_ref->offset != offset)
2404 goto out_unlock;
2405
2406 ret = 0;
2407out_unlock:
2408 mutex_unlock(&head->mutex);
2409out:
2410 spin_unlock(&delayed_refs->lock);
2411 return ret;
2412}
2413
2414static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2415 struct btrfs_root *root,
2416 struct btrfs_path *path,
2417 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002418{
2419 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002420 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002421 struct btrfs_extent_data_ref *ref;
2422 struct btrfs_extent_inline_ref *iref;
2423 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002424 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002425 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002426 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002427
Chris Masonbe20aa92007-12-17 20:14:01 -05002428 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002429 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002430 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002431
Chris Masonbe20aa92007-12-17 20:14:01 -05002432 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2433 if (ret < 0)
2434 goto out;
2435 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002436
2437 ret = -ENOENT;
2438 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002439 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002440
Zheng Yan31840ae2008-09-23 13:14:14 -04002441 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002442 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002443 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002444
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002445 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002446 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002447
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002448 ret = 1;
2449 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2450#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2451 if (item_size < sizeof(*ei)) {
2452 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2453 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002454 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002455#endif
2456 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2457
2458 if (item_size != sizeof(*ei) +
2459 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2460 goto out;
2461
2462 if (btrfs_extent_generation(leaf, ei) <=
2463 btrfs_root_last_snapshot(&root->root_item))
2464 goto out;
2465
2466 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2467 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2468 BTRFS_EXTENT_DATA_REF_KEY)
2469 goto out;
2470
2471 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2472 if (btrfs_extent_refs(leaf, ei) !=
2473 btrfs_extent_data_ref_count(leaf, ref) ||
2474 btrfs_extent_data_ref_root(leaf, ref) !=
2475 root->root_key.objectid ||
2476 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2477 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2478 goto out;
2479
Yan Zhengf321e492008-07-30 09:26:11 -04002480 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002481out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002482 return ret;
2483}
2484
2485int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2486 struct btrfs_root *root,
2487 u64 objectid, u64 offset, u64 bytenr)
2488{
2489 struct btrfs_path *path;
2490 int ret;
2491 int ret2;
2492
2493 path = btrfs_alloc_path();
2494 if (!path)
2495 return -ENOENT;
2496
2497 do {
2498 ret = check_committed_ref(trans, root, path, objectid,
2499 offset, bytenr);
2500 if (ret && ret != -ENOENT)
2501 goto out;
2502
2503 ret2 = check_delayed_ref(trans, root, path, objectid,
2504 offset, bytenr);
2505 } while (ret2 == -EAGAIN);
2506
2507 if (ret2 && ret2 != -ENOENT) {
2508 ret = ret2;
2509 goto out;
2510 }
2511
2512 if (ret != -ENOENT || ret2 != -ENOENT)
2513 ret = 0;
2514out:
Yan Zhengf321e492008-07-30 09:26:11 -04002515 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002516 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2517 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002518 return ret;
2519}
2520
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002521static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002522 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002523 struct extent_buffer *buf,
2524 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002525{
2526 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002527 u64 num_bytes;
2528 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002529 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002530 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002531 struct btrfs_key key;
2532 struct btrfs_file_extent_item *fi;
2533 int i;
2534 int level;
2535 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002536 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002537 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002538
2539 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002540 nritems = btrfs_header_nritems(buf);
2541 level = btrfs_header_level(buf);
2542
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002543 if (!root->ref_cows && level == 0)
2544 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002545
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002546 if (inc)
2547 process_func = btrfs_inc_extent_ref;
2548 else
2549 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002550
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002551 if (full_backref)
2552 parent = buf->start;
2553 else
2554 parent = 0;
2555
Zheng Yan31840ae2008-09-23 13:14:14 -04002556 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002557 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002558 btrfs_item_key_to_cpu(buf, &key, i);
2559 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002560 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002561 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002562 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002563 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002564 BTRFS_FILE_EXTENT_INLINE)
2565 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002566 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2567 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002568 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002569
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002570 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2571 key.offset -= btrfs_file_extent_offset(buf, fi);
2572 ret = process_func(trans, root, bytenr, num_bytes,
2573 parent, ref_root, key.objectid,
2574 key.offset);
2575 if (ret)
2576 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002577 } else {
2578 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002579 num_bytes = btrfs_level_size(root, level - 1);
2580 ret = process_func(trans, root, bytenr, num_bytes,
2581 parent, ref_root, level - 1, 0);
2582 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002583 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002584 }
2585 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002586 return 0;
2587fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002588 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002589 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002590}
2591
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002592int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2593 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002594{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002595 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2596}
Zheng Yan31840ae2008-09-23 13:14:14 -04002597
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002598int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2599 struct extent_buffer *buf, int full_backref)
2600{
2601 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002602}
2603
Chris Mason9078a3e2007-04-26 16:46:15 -04002604static int write_one_cache_group(struct btrfs_trans_handle *trans,
2605 struct btrfs_root *root,
2606 struct btrfs_path *path,
2607 struct btrfs_block_group_cache *cache)
2608{
2609 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002610 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002611 unsigned long bi;
2612 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002613
Chris Mason9078a3e2007-04-26 16:46:15 -04002614 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002615 if (ret < 0)
2616 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002617 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002618
2619 leaf = path->nodes[0];
2620 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2621 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2622 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002623 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002624fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002625 if (ret)
2626 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002627 return 0;
2628
2629}
2630
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002631static struct btrfs_block_group_cache *
2632next_block_group(struct btrfs_root *root,
2633 struct btrfs_block_group_cache *cache)
2634{
2635 struct rb_node *node;
2636 spin_lock(&root->fs_info->block_group_cache_lock);
2637 node = rb_next(&cache->cache_node);
2638 btrfs_put_block_group(cache);
2639 if (node) {
2640 cache = rb_entry(node, struct btrfs_block_group_cache,
2641 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002642 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002643 } else
2644 cache = NULL;
2645 spin_unlock(&root->fs_info->block_group_cache_lock);
2646 return cache;
2647}
2648
Josef Bacik0af3d002010-06-21 14:48:16 -04002649static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2650 struct btrfs_trans_handle *trans,
2651 struct btrfs_path *path)
2652{
2653 struct btrfs_root *root = block_group->fs_info->tree_root;
2654 struct inode *inode = NULL;
2655 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002656 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002657 int num_pages = 0;
2658 int retries = 0;
2659 int ret = 0;
2660
2661 /*
2662 * If this block group is smaller than 100 megs don't bother caching the
2663 * block group.
2664 */
2665 if (block_group->key.offset < (100 * 1024 * 1024)) {
2666 spin_lock(&block_group->lock);
2667 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2668 spin_unlock(&block_group->lock);
2669 return 0;
2670 }
2671
2672again:
2673 inode = lookup_free_space_inode(root, block_group, path);
2674 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2675 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002676 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002677 goto out;
2678 }
2679
2680 if (IS_ERR(inode)) {
2681 BUG_ON(retries);
2682 retries++;
2683
2684 if (block_group->ro)
2685 goto out_free;
2686
2687 ret = create_free_space_inode(root, trans, block_group, path);
2688 if (ret)
2689 goto out_free;
2690 goto again;
2691 }
2692
2693 /*
2694 * We want to set the generation to 0, that way if anything goes wrong
2695 * from here on out we know not to trust this cache when we load up next
2696 * time.
2697 */
2698 BTRFS_I(inode)->generation = 0;
2699 ret = btrfs_update_inode(trans, root, inode);
2700 WARN_ON(ret);
2701
2702 if (i_size_read(inode) > 0) {
2703 ret = btrfs_truncate_free_space_cache(root, trans, path,
2704 inode);
2705 if (ret)
2706 goto out_put;
2707 }
2708
2709 spin_lock(&block_group->lock);
2710 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002711 /* We're not cached, don't bother trying to write stuff out */
2712 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002713 spin_unlock(&block_group->lock);
2714 goto out_put;
2715 }
2716 spin_unlock(&block_group->lock);
2717
2718 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2719 if (!num_pages)
2720 num_pages = 1;
2721
2722 /*
2723 * Just to make absolutely sure we have enough space, we're going to
2724 * preallocate 12 pages worth of space for each block group. In
2725 * practice we ought to use at most 8, but we need extra space so we can
2726 * add our header and have a terminator between the extents and the
2727 * bitmaps.
2728 */
2729 num_pages *= 16;
2730 num_pages *= PAGE_CACHE_SIZE;
2731
2732 ret = btrfs_check_data_free_space(inode, num_pages);
2733 if (ret)
2734 goto out_put;
2735
2736 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2737 num_pages, num_pages,
2738 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002739 if (!ret)
2740 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002741 btrfs_free_reserved_data_space(inode, num_pages);
2742out_put:
2743 iput(inode);
2744out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02002745 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002746out:
2747 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002748 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002749 spin_unlock(&block_group->lock);
2750
2751 return ret;
2752}
2753
Chris Mason96b51792007-10-15 16:15:19 -04002754int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2755 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002756{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002757 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002758 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002759 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002760 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002761
2762 path = btrfs_alloc_path();
2763 if (!path)
2764 return -ENOMEM;
2765
Josef Bacik0af3d002010-06-21 14:48:16 -04002766again:
2767 while (1) {
2768 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2769 while (cache) {
2770 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2771 break;
2772 cache = next_block_group(root, cache);
2773 }
2774 if (!cache) {
2775 if (last == 0)
2776 break;
2777 last = 0;
2778 continue;
2779 }
2780 err = cache_save_setup(cache, trans, path);
2781 last = cache->key.objectid + cache->key.offset;
2782 btrfs_put_block_group(cache);
2783 }
2784
Chris Masond3977122009-01-05 21:25:51 -05002785 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002786 if (last == 0) {
2787 err = btrfs_run_delayed_refs(trans, root,
2788 (unsigned long)-1);
2789 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002790 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002791
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002792 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2793 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002794 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2795 btrfs_put_block_group(cache);
2796 goto again;
2797 }
2798
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002799 if (cache->dirty)
2800 break;
2801 cache = next_block_group(root, cache);
2802 }
2803 if (!cache) {
2804 if (last == 0)
2805 break;
2806 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002807 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002808 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002809
Josef Bacik0cb59c92010-07-02 12:14:14 -04002810 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2811 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002812 cache->dirty = 0;
2813 last = cache->key.objectid + cache->key.offset;
2814
2815 err = write_one_cache_group(trans, root, path, cache);
2816 BUG_ON(err);
2817 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002818 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002819
Josef Bacik0cb59c92010-07-02 12:14:14 -04002820 while (1) {
2821 /*
2822 * I don't think this is needed since we're just marking our
2823 * preallocated extent as written, but just in case it can't
2824 * hurt.
2825 */
2826 if (last == 0) {
2827 err = btrfs_run_delayed_refs(trans, root,
2828 (unsigned long)-1);
2829 BUG_ON(err);
2830 }
2831
2832 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2833 while (cache) {
2834 /*
2835 * Really this shouldn't happen, but it could if we
2836 * couldn't write the entire preallocated extent and
2837 * splitting the extent resulted in a new block.
2838 */
2839 if (cache->dirty) {
2840 btrfs_put_block_group(cache);
2841 goto again;
2842 }
2843 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2844 break;
2845 cache = next_block_group(root, cache);
2846 }
2847 if (!cache) {
2848 if (last == 0)
2849 break;
2850 last = 0;
2851 continue;
2852 }
2853
2854 btrfs_write_out_cache(root, trans, cache, path);
2855
2856 /*
2857 * If we didn't have an error then the cache state is still
2858 * NEED_WRITE, so we can set it to WRITTEN.
2859 */
2860 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2861 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2862 last = cache->key.objectid + cache->key.offset;
2863 btrfs_put_block_group(cache);
2864 }
2865
Chris Mason9078a3e2007-04-26 16:46:15 -04002866 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002867 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002868}
2869
Yan Zhengd2fb3432008-12-11 16:30:39 -05002870int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2871{
2872 struct btrfs_block_group_cache *block_group;
2873 int readonly = 0;
2874
2875 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2876 if (!block_group || block_group->ro)
2877 readonly = 1;
2878 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002879 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002880 return readonly;
2881}
2882
Chris Mason593060d2008-03-25 16:50:33 -04002883static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2884 u64 total_bytes, u64 bytes_used,
2885 struct btrfs_space_info **space_info)
2886{
2887 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002888 int i;
2889 int factor;
2890
2891 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2892 BTRFS_BLOCK_GROUP_RAID10))
2893 factor = 2;
2894 else
2895 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002896
2897 found = __find_space_info(info, flags);
2898 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002899 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002900 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002901 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002902 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002903 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002904 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002905 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002906 *space_info = found;
2907 return 0;
2908 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002909 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002910 if (!found)
2911 return -ENOMEM;
2912
Yan, Zhengb742bb82010-05-16 10:46:24 -04002913 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2914 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04002915 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002916 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04002917 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2918 BTRFS_BLOCK_GROUP_SYSTEM |
2919 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04002920 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002921 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002922 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002923 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002924 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002925 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002926 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002927 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002928 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04002929 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04002930 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04002931 found->flush = 0;
2932 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04002933 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002934 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04002935 return 0;
2936}
2937
Chris Mason8790d502008-04-03 16:29:03 -04002938static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2939{
2940 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002941 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002942 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002943 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002944 if (extra_flags) {
2945 if (flags & BTRFS_BLOCK_GROUP_DATA)
2946 fs_info->avail_data_alloc_bits |= extra_flags;
2947 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2948 fs_info->avail_metadata_alloc_bits |= extra_flags;
2949 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2950 fs_info->avail_system_alloc_bits |= extra_flags;
2951 }
2952}
Chris Mason593060d2008-03-25 16:50:33 -04002953
Yan Zheng2b820322008-11-17 21:11:30 -05002954u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002955{
Chris Masoncd02dca2010-12-13 14:56:23 -05002956 /*
2957 * we add in the count of missing devices because we want
2958 * to make sure that any RAID levels on a degraded FS
2959 * continue to be honored.
2960 */
2961 u64 num_devices = root->fs_info->fs_devices->rw_devices +
2962 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002963
2964 if (num_devices == 1)
2965 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2966 if (num_devices < 4)
2967 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2968
Chris Masonec44a352008-04-28 15:29:52 -04002969 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2970 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002971 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002972 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002973 }
Chris Masonec44a352008-04-28 15:29:52 -04002974
2975 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002976 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002977 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002978 }
Chris Masonec44a352008-04-28 15:29:52 -04002979
2980 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2981 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2982 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2983 (flags & BTRFS_BLOCK_GROUP_DUP)))
2984 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2985 return flags;
2986}
2987
Yan, Zhengb742bb82010-05-16 10:46:24 -04002988static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05002989{
Yan, Zhengb742bb82010-05-16 10:46:24 -04002990 if (flags & BTRFS_BLOCK_GROUP_DATA)
2991 flags |= root->fs_info->avail_data_alloc_bits &
2992 root->fs_info->data_alloc_profile;
2993 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2994 flags |= root->fs_info->avail_system_alloc_bits &
2995 root->fs_info->system_alloc_profile;
2996 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
2997 flags |= root->fs_info->avail_metadata_alloc_bits &
2998 root->fs_info->metadata_alloc_profile;
2999 return btrfs_reduce_alloc_profile(root, flags);
3000}
Josef Bacik6a632092009-02-20 11:00:09 -05003001
Miao Xie6d07bce2011-01-05 10:07:31 +00003002u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003003{
3004 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003005
Yan, Zhengb742bb82010-05-16 10:46:24 -04003006 if (data)
3007 flags = BTRFS_BLOCK_GROUP_DATA;
3008 else if (root == root->fs_info->chunk_root)
3009 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3010 else
3011 flags = BTRFS_BLOCK_GROUP_METADATA;
3012
3013 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003014}
3015
3016void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3017{
Josef Bacik6a632092009-02-20 11:00:09 -05003018 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003019 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003020}
3021
3022/*
3023 * This will check the space that the inode allocates from to make sure we have
3024 * enough space for bytes.
3025 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003026int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003027{
3028 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003029 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003030 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003031 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003032
3033 /* make sure bytes are sectorsize aligned */
3034 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3035
Li Zefan82d59022011-04-20 10:33:24 +08003036 if (root == root->fs_info->tree_root ||
3037 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003038 alloc_chunk = 0;
3039 committed = 1;
3040 }
3041
Josef Bacik6a632092009-02-20 11:00:09 -05003042 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003043 if (!data_sinfo)
3044 goto alloc;
3045
Josef Bacik6a632092009-02-20 11:00:09 -05003046again:
3047 /* make sure we have enough space to handle the data first */
3048 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003049 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3050 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3051 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003052
3053 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003054 struct btrfs_trans_handle *trans;
3055
Josef Bacik6a632092009-02-20 11:00:09 -05003056 /*
3057 * if we don't have enough free bytes in this space then we need
3058 * to alloc a new chunk.
3059 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003060 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003061 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003062
Chris Mason0e4f8f82011-04-15 16:05:44 -04003063 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003064 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003065alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003066 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003067 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003068 if (IS_ERR(trans))
3069 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003070
3071 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3072 bytes + 2 * 1024 * 1024,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003073 alloc_target,
3074 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003075 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003076 if (ret < 0) {
3077 if (ret != -ENOSPC)
3078 return ret;
3079 else
3080 goto commit_trans;
3081 }
Chris Mason33b4d472009-09-22 14:45:50 -04003082
3083 if (!data_sinfo) {
3084 btrfs_set_inode_space_info(root, inode);
3085 data_sinfo = BTRFS_I(inode)->space_info;
3086 }
Josef Bacik6a632092009-02-20 11:00:09 -05003087 goto again;
3088 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003089
3090 /*
3091 * If we have less pinned bytes than we want to allocate then
3092 * don't bother committing the transaction, it won't help us.
3093 */
3094 if (data_sinfo->bytes_pinned < bytes)
3095 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003096 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003097
3098 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003099commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003100 if (!committed &&
3101 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003102 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003103 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003104 if (IS_ERR(trans))
3105 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003106 ret = btrfs_commit_transaction(trans, root);
3107 if (ret)
3108 return ret;
3109 goto again;
3110 }
3111
Josef Bacik6a632092009-02-20 11:00:09 -05003112 return -ENOSPC;
3113 }
3114 data_sinfo->bytes_may_use += bytes;
3115 BTRFS_I(inode)->reserved_bytes += bytes;
3116 spin_unlock(&data_sinfo->lock);
3117
Josef Bacik9ed74f22009-09-11 16:12:44 -04003118 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003119}
3120
3121/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003122 * called when we are clearing an delalloc extent from the
3123 * inode's io_tree or there was an error for whatever reason
3124 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003125 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003126void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003127{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003128 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003129 struct btrfs_space_info *data_sinfo;
3130
3131 /* make sure bytes are sectorsize aligned */
3132 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3133
3134 data_sinfo = BTRFS_I(inode)->space_info;
3135 spin_lock(&data_sinfo->lock);
3136 data_sinfo->bytes_may_use -= bytes;
3137 BTRFS_I(inode)->reserved_bytes -= bytes;
3138 spin_unlock(&data_sinfo->lock);
3139}
3140
Josef Bacik97e728d2009-04-21 17:40:57 -04003141static void force_metadata_allocation(struct btrfs_fs_info *info)
3142{
3143 struct list_head *head = &info->space_info;
3144 struct btrfs_space_info *found;
3145
3146 rcu_read_lock();
3147 list_for_each_entry_rcu(found, head, list) {
3148 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003149 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003150 }
3151 rcu_read_unlock();
3152}
3153
Chris Masone5bc2452010-10-26 13:37:56 -04003154static int should_alloc_chunk(struct btrfs_root *root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003155 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3156 int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003157{
3158 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003159 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003160 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003161
Chris Mason0e4f8f82011-04-15 16:05:44 -04003162 if (force == CHUNK_ALLOC_FORCE)
3163 return 1;
3164
3165 /*
3166 * in limited mode, we want to have some free space up to
3167 * about 1% of the FS size.
3168 */
3169 if (force == CHUNK_ALLOC_LIMITED) {
3170 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3171 thresh = max_t(u64, 64 * 1024 * 1024,
3172 div_factor_fine(thresh, 1));
3173
3174 if (num_bytes - num_allocated < thresh)
3175 return 1;
3176 }
3177
3178 /*
3179 * we have two similar checks here, one based on percentage
3180 * and once based on a hard number of 256MB. The idea
3181 * is that if we have a good amount of free
3182 * room, don't allocate a chunk. A good mount is
3183 * less than 80% utilized of the chunks we have allocated,
3184 * or more than 256MB free
3185 */
3186 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003187 return 0;
3188
Chris Mason0e4f8f82011-04-15 16:05:44 -04003189 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
Yan, Zheng424499d2010-05-16 10:46:25 -04003190 return 0;
3191
Chris Masone5bc2452010-10-26 13:37:56 -04003192 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003193
3194 /* 256MB or 5% of the FS */
Chris Masone5bc2452010-10-26 13:37:56 -04003195 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3196
3197 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003198 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003199 return 1;
3200}
3201
Chris Mason6324fbf2008-03-24 15:01:59 -04003202static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3203 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003204 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003205{
3206 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003207 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003208 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003209 int ret = 0;
3210
Yan Zheng2b820322008-11-17 21:11:30 -05003211 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003212
Chris Mason6324fbf2008-03-24 15:01:59 -04003213 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003214 if (!space_info) {
3215 ret = update_space_info(extent_root->fs_info, flags,
3216 0, 0, &space_info);
3217 BUG_ON(ret);
3218 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003219 BUG_ON(!space_info);
3220
Josef Bacik6d741192011-04-11 20:20:11 -04003221again:
Josef Bacik25179202008-10-29 14:49:05 -04003222 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003223 if (space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003224 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003225 if (space_info->full) {
3226 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003227 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003228 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003229
Chris Mason0e4f8f82011-04-15 16:05:44 -04003230 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003231 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003232 return 0;
3233 } else if (space_info->chunk_alloc) {
3234 wait_for_alloc = 1;
3235 } else {
3236 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003237 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003238
Josef Bacik25179202008-10-29 14:49:05 -04003239 spin_unlock(&space_info->lock);
3240
Josef Bacik6d741192011-04-11 20:20:11 -04003241 mutex_lock(&fs_info->chunk_mutex);
3242
3243 /*
3244 * The chunk_mutex is held throughout the entirety of a chunk
3245 * allocation, so once we've acquired the chunk_mutex we know that the
3246 * other guy is done and we need to recheck and see if we should
3247 * allocate.
3248 */
3249 if (wait_for_alloc) {
3250 mutex_unlock(&fs_info->chunk_mutex);
3251 wait_for_alloc = 0;
3252 goto again;
3253 }
3254
Josef Bacik97e728d2009-04-21 17:40:57 -04003255 /*
Josef Bacik67377732010-09-16 16:19:09 -04003256 * If we have mixed data/metadata chunks we want to make sure we keep
3257 * allocating mixed chunks instead of individual chunks.
3258 */
3259 if (btrfs_mixed_space_info(space_info))
3260 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3261
3262 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003263 * if we're doing a data chunk, go ahead and make sure that
3264 * we keep a reasonable number of metadata chunks allocated in the
3265 * FS as well.
3266 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003267 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003268 fs_info->data_chunk_allocations++;
3269 if (!(fs_info->data_chunk_allocations %
3270 fs_info->metadata_ratio))
3271 force_metadata_allocation(fs_info);
3272 }
3273
Yan Zheng2b820322008-11-17 21:11:30 -05003274 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003275 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003276 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003277 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003278 else
3279 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003280
Chris Mason0e4f8f82011-04-15 16:05:44 -04003281 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003282 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003283 spin_unlock(&space_info->lock);
Yan Zhengc146afa2008-11-12 14:34:12 -05003284 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003285 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003286}
3287
Yan, Zheng5da9d012010-05-16 10:46:25 -04003288/*
3289 * shrink metadata reservation for delalloc
3290 */
3291static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003292 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003293{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003294 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003295 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003296 u64 reserved;
3297 u64 max_reclaim;
3298 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003299 long time_left;
Chris Masonbf9022e2010-10-26 13:40:45 -04003300 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003301 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003302 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003303
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003304 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003305 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003306
3307 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003308 reserved = space_info->bytes_reserved;
Chris Mason36e39c42011-03-12 07:08:42 -05003309 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003310
3311 if (reserved == 0)
3312 return 0;
3313
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003314 smp_mb();
3315 if (root->fs_info->delalloc_bytes == 0) {
3316 if (trans)
3317 return 0;
3318 btrfs_wait_ordered_extents(root, 0, 0);
3319 return 0;
3320 }
3321
Yan, Zheng5da9d012010-05-16 10:46:25 -04003322 max_reclaim = min(reserved, to_reclaim);
3323
Josef Bacikb1953bc2011-01-21 21:10:01 +00003324 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003325 /* have the flusher threads jump in and do some IO */
3326 smp_mb();
3327 nr_pages = min_t(unsigned long, nr_pages,
3328 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3329 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003330
Josef Bacik0019f102010-10-15 15:18:40 -04003331 spin_lock(&space_info->lock);
Chris Mason36e39c42011-03-12 07:08:42 -05003332 if (reserved > space_info->bytes_reserved)
Josef Bacik0019f102010-10-15 15:18:40 -04003333 reclaimed += reserved - space_info->bytes_reserved;
3334 reserved = space_info->bytes_reserved;
3335 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003336
Chris Mason36e39c42011-03-12 07:08:42 -05003337 loops++;
3338
Yan, Zheng5da9d012010-05-16 10:46:25 -04003339 if (reserved == 0 || reclaimed >= max_reclaim)
3340 break;
3341
3342 if (trans && trans->transaction->blocked)
3343 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003344
Chris Mason36e39c42011-03-12 07:08:42 -05003345 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003346
3347 /* We were interrupted, exit */
3348 if (time_left)
3349 break;
3350
Chris Mason36e39c42011-03-12 07:08:42 -05003351 /* we've kicked the IO a few times, if anything has been freed,
3352 * exit. There is no sense in looping here for a long time
3353 * when we really need to commit the transaction, or there are
3354 * just too many writers without enough free space
3355 */
3356
3357 if (loops > 3) {
3358 smp_mb();
3359 if (progress != space_info->reservation_progress)
3360 break;
3361 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003362
Yan, Zheng5da9d012010-05-16 10:46:25 -04003363 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003364 if (reclaimed >= to_reclaim && !trans)
3365 btrfs_wait_ordered_extents(root, 0, 0);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003366 return reclaimed >= to_reclaim;
3367}
3368
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003369/*
3370 * Retries tells us how many times we've called reserve_metadata_bytes. The
3371 * idea is if this is the first call (retries == 0) then we will add to our
3372 * reserved count if we can't make the allocation in order to hold our place
3373 * while we go and try and free up space. That way for retries > 1 we don't try
3374 * and add space, we just check to see if the amount of unused space is >= the
3375 * total space, meaning that our reservation is valid.
3376 *
3377 * However if we don't intend to retry this reservation, pass -1 as retries so
3378 * that it short circuits this logic.
3379 */
3380static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3381 struct btrfs_root *root,
3382 struct btrfs_block_rsv *block_rsv,
3383 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003384{
3385 struct btrfs_space_info *space_info = block_rsv->space_info;
3386 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003387 u64 num_bytes = orig_bytes;
3388 int retries = 0;
3389 int ret = 0;
Josef Bacik38227932010-10-26 12:52:53 -04003390 bool committed = false;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003391 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003392
3393again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003394 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003395 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003396 /*
3397 * We only want to wait if somebody other than us is flushing and we are
3398 * actually alloed to flush.
3399 */
3400 while (flush && !flushing && space_info->flush) {
3401 spin_unlock(&space_info->lock);
3402 /*
3403 * If we have a trans handle we can't wait because the flusher
3404 * may have to commit the transaction, which would mean we would
3405 * deadlock since we are waiting for the flusher to finish, but
3406 * hold the current transaction open.
3407 */
3408 if (trans)
3409 return -EAGAIN;
3410 ret = wait_event_interruptible(space_info->wait,
3411 !space_info->flush);
3412 /* Must have been interrupted, return */
3413 if (ret)
3414 return -EINTR;
3415
3416 spin_lock(&space_info->lock);
3417 }
3418
3419 ret = -ENOSPC;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003420 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003421 space_info->bytes_pinned + space_info->bytes_readonly +
3422 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003423
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003424 /*
3425 * The idea here is that we've not already over-reserved the block group
3426 * then we can go ahead and save our reservation first and then start
3427 * flushing if we need to. Otherwise if we've already overcommitted
3428 * lets start flushing stuff first and then come back and try to make
3429 * our reservation.
3430 */
3431 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003432 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003433 if (unused >= num_bytes) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003434 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003435 ret = 0;
3436 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003437 /*
3438 * Ok set num_bytes to orig_bytes since we aren't
3439 * overocmmitted, this way we only try and reclaim what
3440 * we need.
3441 */
3442 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003443 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003444 } else {
3445 /*
3446 * Ok we're over committed, set num_bytes to the overcommitted
3447 * amount plus the amount of bytes that we need for this
3448 * reservation.
3449 */
3450 num_bytes = unused - space_info->total_bytes +
3451 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003452 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003453
3454 /*
3455 * Couldn't make our reservation, save our place so while we're trying
3456 * to reclaim space we can actually use it instead of somebody else
3457 * stealing it from us.
3458 */
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003459 if (ret && flush) {
3460 flushing = true;
3461 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003462 }
3463
Yan, Zhengf0486c62010-05-16 10:46:25 -04003464 spin_unlock(&space_info->lock);
3465
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003466 if (!ret || !flush)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003467 goto out;
3468
3469 /*
3470 * We do synchronous shrinking since we don't actually unreserve
3471 * metadata until after the IO is completed.
3472 */
3473 ret = shrink_delalloc(trans, root, num_bytes, 1);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003474 if (ret < 0)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003475 goto out;
3476
3477 /*
3478 * So if we were overcommitted it's possible that somebody else flushed
3479 * out enough space and we simply didn't have enough space to reclaim,
3480 * so go back around and try again.
3481 */
3482 if (retries < 2) {
3483 retries++;
3484 goto again;
3485 }
3486
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003487 /*
3488 * Not enough space to be reclaimed, don't bother committing the
3489 * transaction.
3490 */
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003491 spin_lock(&space_info->lock);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003492 if (space_info->bytes_pinned < orig_bytes)
3493 ret = -ENOSPC;
3494 spin_unlock(&space_info->lock);
3495 if (ret)
3496 goto out;
3497
3498 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003499 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003500 goto out;
3501
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003502 ret = -ENOSPC;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003503 trans = btrfs_join_transaction(root);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003504 if (IS_ERR(trans))
3505 goto out;
3506 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003507 if (!ret) {
3508 trans = NULL;
3509 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003510 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003511 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003512
3513out:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003514 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003515 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003516 space_info->flush = 0;
3517 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003518 spin_unlock(&space_info->lock);
3519 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04003520 return ret;
3521}
3522
3523static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3524 struct btrfs_root *root)
3525{
3526 struct btrfs_block_rsv *block_rsv;
3527 if (root->ref_cows)
3528 block_rsv = trans->block_rsv;
3529 else
3530 block_rsv = root->block_rsv;
3531
3532 if (!block_rsv)
3533 block_rsv = &root->fs_info->empty_block_rsv;
3534
3535 return block_rsv;
3536}
3537
3538static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3539 u64 num_bytes)
3540{
3541 int ret = -ENOSPC;
3542 spin_lock(&block_rsv->lock);
3543 if (block_rsv->reserved >= num_bytes) {
3544 block_rsv->reserved -= num_bytes;
3545 if (block_rsv->reserved < block_rsv->size)
3546 block_rsv->full = 0;
3547 ret = 0;
3548 }
3549 spin_unlock(&block_rsv->lock);
3550 return ret;
3551}
3552
3553static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3554 u64 num_bytes, int update_size)
3555{
3556 spin_lock(&block_rsv->lock);
3557 block_rsv->reserved += num_bytes;
3558 if (update_size)
3559 block_rsv->size += num_bytes;
3560 else if (block_rsv->reserved >= block_rsv->size)
3561 block_rsv->full = 1;
3562 spin_unlock(&block_rsv->lock);
3563}
3564
David Sterba62a45b62011-04-20 15:52:26 +02003565static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3566 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003567{
3568 struct btrfs_space_info *space_info = block_rsv->space_info;
3569
3570 spin_lock(&block_rsv->lock);
3571 if (num_bytes == (u64)-1)
3572 num_bytes = block_rsv->size;
3573 block_rsv->size -= num_bytes;
3574 if (block_rsv->reserved >= block_rsv->size) {
3575 num_bytes = block_rsv->reserved - block_rsv->size;
3576 block_rsv->reserved = block_rsv->size;
3577 block_rsv->full = 1;
3578 } else {
3579 num_bytes = 0;
3580 }
3581 spin_unlock(&block_rsv->lock);
3582
3583 if (num_bytes > 0) {
3584 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003585 spin_lock(&dest->lock);
3586 if (!dest->full) {
3587 u64 bytes_to_add;
3588
3589 bytes_to_add = dest->size - dest->reserved;
3590 bytes_to_add = min(num_bytes, bytes_to_add);
3591 dest->reserved += bytes_to_add;
3592 if (dest->reserved >= dest->size)
3593 dest->full = 1;
3594 num_bytes -= bytes_to_add;
3595 }
3596 spin_unlock(&dest->lock);
3597 }
3598 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003599 spin_lock(&space_info->lock);
3600 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003601 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003602 spin_unlock(&space_info->lock);
3603 }
3604 }
3605}
3606
3607static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3608 struct btrfs_block_rsv *dst, u64 num_bytes)
3609{
3610 int ret;
3611
3612 ret = block_rsv_use_bytes(src, num_bytes);
3613 if (ret)
3614 return ret;
3615
3616 block_rsv_add_bytes(dst, num_bytes, 1);
3617 return 0;
3618}
3619
3620void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3621{
3622 memset(rsv, 0, sizeof(*rsv));
3623 spin_lock_init(&rsv->lock);
3624 atomic_set(&rsv->usage, 1);
3625 rsv->priority = 6;
3626 INIT_LIST_HEAD(&rsv->list);
3627}
3628
3629struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3630{
3631 struct btrfs_block_rsv *block_rsv;
3632 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003633
3634 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3635 if (!block_rsv)
3636 return NULL;
3637
3638 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003639 block_rsv->space_info = __find_space_info(fs_info,
3640 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003641 return block_rsv;
3642}
3643
3644void btrfs_free_block_rsv(struct btrfs_root *root,
3645 struct btrfs_block_rsv *rsv)
3646{
3647 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3648 btrfs_block_rsv_release(root, rsv, (u64)-1);
3649 if (!rsv->durable)
3650 kfree(rsv);
3651 }
3652}
3653
3654/*
3655 * make the block_rsv struct be able to capture freed space.
3656 * the captured space will re-add to the the block_rsv struct
3657 * after transaction commit
3658 */
3659void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3660 struct btrfs_block_rsv *block_rsv)
3661{
3662 block_rsv->durable = 1;
3663 mutex_lock(&fs_info->durable_block_rsv_mutex);
3664 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3665 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3666}
3667
3668int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3669 struct btrfs_root *root,
3670 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003671 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003672{
3673 int ret;
3674
3675 if (num_bytes == 0)
3676 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003677
3678 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003679 if (!ret) {
3680 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3681 return 0;
3682 }
3683
Yan, Zhengf0486c62010-05-16 10:46:25 -04003684 return ret;
3685}
3686
3687int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3688 struct btrfs_root *root,
3689 struct btrfs_block_rsv *block_rsv,
3690 u64 min_reserved, int min_factor)
3691{
3692 u64 num_bytes = 0;
3693 int commit_trans = 0;
3694 int ret = -ENOSPC;
3695
3696 if (!block_rsv)
3697 return 0;
3698
3699 spin_lock(&block_rsv->lock);
3700 if (min_factor > 0)
3701 num_bytes = div_factor(block_rsv->size, min_factor);
3702 if (min_reserved > num_bytes)
3703 num_bytes = min_reserved;
3704
3705 if (block_rsv->reserved >= num_bytes) {
3706 ret = 0;
3707 } else {
3708 num_bytes -= block_rsv->reserved;
3709 if (block_rsv->durable &&
3710 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3711 commit_trans = 1;
3712 }
3713 spin_unlock(&block_rsv->lock);
3714 if (!ret)
3715 return 0;
3716
3717 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003718 ret = reserve_metadata_bytes(trans, root, block_rsv,
3719 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003720 if (!ret) {
3721 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3722 return 0;
3723 }
3724 }
3725
3726 if (commit_trans) {
3727 if (trans)
3728 return -EAGAIN;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003729 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003730 BUG_ON(IS_ERR(trans));
3731 ret = btrfs_commit_transaction(trans, root);
3732 return 0;
3733 }
3734
Yan, Zhengf0486c62010-05-16 10:46:25 -04003735 return -ENOSPC;
3736}
3737
3738int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3739 struct btrfs_block_rsv *dst_rsv,
3740 u64 num_bytes)
3741{
3742 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3743}
3744
3745void btrfs_block_rsv_release(struct btrfs_root *root,
3746 struct btrfs_block_rsv *block_rsv,
3747 u64 num_bytes)
3748{
3749 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3750 if (global_rsv->full || global_rsv == block_rsv ||
3751 block_rsv->space_info != global_rsv->space_info)
3752 global_rsv = NULL;
3753 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3754}
3755
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003756/*
3757 * helper to calculate size of global block reservation.
3758 * the desired value is sum of space used by extent tree,
3759 * checksum tree and root tree
3760 */
3761static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3762{
3763 struct btrfs_space_info *sinfo;
3764 u64 num_bytes;
3765 u64 meta_used;
3766 u64 data_used;
3767 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003768
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003769 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3770 spin_lock(&sinfo->lock);
3771 data_used = sinfo->bytes_used;
3772 spin_unlock(&sinfo->lock);
3773
3774 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3775 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003776 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3777 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003778 meta_used = sinfo->bytes_used;
3779 spin_unlock(&sinfo->lock);
3780
3781 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3782 csum_size * 2;
3783 num_bytes += div64_u64(data_used + meta_used, 50);
3784
3785 if (num_bytes * 3 > meta_used)
3786 num_bytes = div64_u64(meta_used, 3);
3787
3788 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3789}
3790
3791static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3792{
3793 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3794 struct btrfs_space_info *sinfo = block_rsv->space_info;
3795 u64 num_bytes;
3796
3797 num_bytes = calc_global_metadata_size(fs_info);
3798
3799 spin_lock(&block_rsv->lock);
3800 spin_lock(&sinfo->lock);
3801
3802 block_rsv->size = num_bytes;
3803
3804 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003805 sinfo->bytes_reserved + sinfo->bytes_readonly +
3806 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003807
3808 if (sinfo->total_bytes > num_bytes) {
3809 num_bytes = sinfo->total_bytes - num_bytes;
3810 block_rsv->reserved += num_bytes;
3811 sinfo->bytes_reserved += num_bytes;
3812 }
3813
3814 if (block_rsv->reserved >= block_rsv->size) {
3815 num_bytes = block_rsv->reserved - block_rsv->size;
3816 sinfo->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003817 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003818 block_rsv->reserved = block_rsv->size;
3819 block_rsv->full = 1;
3820 }
David Sterba182608c2011-05-05 13:13:16 +02003821
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003822 spin_unlock(&sinfo->lock);
3823 spin_unlock(&block_rsv->lock);
3824}
3825
Yan, Zhengf0486c62010-05-16 10:46:25 -04003826static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3827{
3828 struct btrfs_space_info *space_info;
3829
3830 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3831 fs_info->chunk_block_rsv.space_info = space_info;
3832 fs_info->chunk_block_rsv.priority = 10;
3833
3834 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003835 fs_info->global_block_rsv.space_info = space_info;
3836 fs_info->global_block_rsv.priority = 10;
3837 fs_info->global_block_rsv.refill_used = 1;
3838 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003839 fs_info->trans_block_rsv.space_info = space_info;
3840 fs_info->empty_block_rsv.space_info = space_info;
3841 fs_info->empty_block_rsv.priority = 10;
3842
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003843 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3844 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3845 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3846 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003847 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003848
3849 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3850
3851 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3852
3853 update_global_block_rsv(fs_info);
3854}
3855
3856static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3857{
3858 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3859 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3860 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3861 WARN_ON(fs_info->trans_block_rsv.size > 0);
3862 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3863 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3864 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003865}
3866
Josef Bacikfcb80c22011-05-03 10:40:22 -04003867int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans,
3868 struct btrfs_root *root,
3869 struct btrfs_block_rsv *rsv)
3870{
3871 struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv;
3872 u64 num_bytes;
3873 int ret;
3874
3875 /*
3876 * Truncate should be freeing data, but give us 2 items just in case it
3877 * needs to use some space. We may want to be smarter about this in the
3878 * future.
3879 */
Chris Masonff5714c2011-05-28 07:00:39 -04003880 num_bytes = btrfs_calc_trans_metadata_size(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04003881
3882 /* We already have enough bytes, just return */
3883 if (rsv->reserved >= num_bytes)
3884 return 0;
3885
3886 num_bytes -= rsv->reserved;
3887
3888 /*
3889 * You should have reserved enough space before hand to do this, so this
3890 * should not fail.
3891 */
3892 ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes);
3893 BUG_ON(ret);
3894
3895 return 0;
3896}
3897
Yan, Zhenga22285a2010-05-16 10:48:46 -04003898void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3899 struct btrfs_root *root)
3900{
3901 if (!trans->bytes_reserved)
3902 return;
3903
3904 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3905 btrfs_block_rsv_release(root, trans->block_rsv,
3906 trans->bytes_reserved);
3907 trans->bytes_reserved = 0;
3908}
3909
Yan, Zhengd68fc572010-05-16 10:49:58 -04003910int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3911 struct inode *inode)
3912{
3913 struct btrfs_root *root = BTRFS_I(inode)->root;
3914 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3915 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3916
3917 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04003918 * We need to hold space in order to delete our orphan item once we've
3919 * added it, so this takes the reservation so we can release it later
3920 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003921 */
Chris Masonff5714c2011-05-28 07:00:39 -04003922 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003923 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3924}
3925
3926void btrfs_orphan_release_metadata(struct inode *inode)
3927{
3928 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04003929 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003930 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3931}
3932
Yan, Zhenga22285a2010-05-16 10:48:46 -04003933int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3934 struct btrfs_pending_snapshot *pending)
3935{
3936 struct btrfs_root *root = pending->root;
3937 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3938 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3939 /*
3940 * two for root back/forward refs, two for directory entries
3941 * and one for root of the snapshot.
3942 */
Miao Xie16cdcec2011-04-22 18:12:22 +08003943 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003944 dst_rsv->space_info = src_rsv->space_info;
3945 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3946}
3947
Josef Bacik9e0baf62011-07-15 15:16:44 +00003948static unsigned drop_outstanding_extent(struct inode *inode)
3949{
3950 unsigned dropped_extents = 0;
3951
3952 spin_lock(&BTRFS_I(inode)->lock);
3953 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
3954 BTRFS_I(inode)->outstanding_extents--;
3955
3956 /*
3957 * If we have more or the same amount of outsanding extents than we have
3958 * reserved then we need to leave the reserved extents count alone.
3959 */
3960 if (BTRFS_I(inode)->outstanding_extents >=
3961 BTRFS_I(inode)->reserved_extents)
3962 goto out;
3963
3964 dropped_extents = BTRFS_I(inode)->reserved_extents -
3965 BTRFS_I(inode)->outstanding_extents;
3966 BTRFS_I(inode)->reserved_extents -= dropped_extents;
3967out:
3968 spin_unlock(&BTRFS_I(inode)->lock);
3969 return dropped_extents;
3970}
3971
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003972static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3973{
3974 return num_bytes >>= 3;
3975}
3976
3977int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3978{
3979 struct btrfs_root *root = BTRFS_I(inode)->root;
3980 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00003981 u64 to_reserve = 0;
3982 unsigned nr_extents = 0;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003983 int ret;
3984
3985 if (btrfs_transaction_in_commit(root->fs_info))
3986 schedule_timeout(1);
3987
3988 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003989
Josef Bacik9e0baf62011-07-15 15:16:44 +00003990 spin_lock(&BTRFS_I(inode)->lock);
3991 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05003992
Josef Bacik9e0baf62011-07-15 15:16:44 +00003993 if (BTRFS_I(inode)->outstanding_extents >
3994 BTRFS_I(inode)->reserved_extents) {
3995 nr_extents = BTRFS_I(inode)->outstanding_extents -
3996 BTRFS_I(inode)->reserved_extents;
3997 BTRFS_I(inode)->reserved_extents += nr_extents;
3998
Miao Xie16cdcec2011-04-22 18:12:22 +08003999 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004000 }
Josef Bacik9e0baf62011-07-15 15:16:44 +00004001 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004002
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004003 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004004 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004005 if (ret) {
4006 unsigned dropped;
4007 /*
4008 * We don't need the return value since our reservation failed,
4009 * we just need to clean up our counter.
4010 */
4011 dropped = drop_outstanding_extent(inode);
4012 WARN_ON(dropped > 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004013 return ret;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004014 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004015
4016 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4017
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004018 return 0;
4019}
4020
4021void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4022{
4023 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004024 u64 to_free = 0;
4025 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004026
4027 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004028 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004029
4030 to_free = calc_csum_metadata_size(inode, num_bytes);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004031 if (dropped > 0)
4032 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004033
4034 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4035 to_free);
4036}
4037
4038int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4039{
4040 int ret;
4041
4042 ret = btrfs_check_data_free_space(inode, num_bytes);
4043 if (ret)
4044 return ret;
4045
4046 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4047 if (ret) {
4048 btrfs_free_reserved_data_space(inode, num_bytes);
4049 return ret;
4050 }
4051
4052 return 0;
4053}
4054
4055void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4056{
4057 btrfs_delalloc_release_metadata(inode, num_bytes);
4058 btrfs_free_reserved_data_space(inode, num_bytes);
4059}
4060
Chris Mason9078a3e2007-04-26 16:46:15 -04004061static int update_block_group(struct btrfs_trans_handle *trans,
4062 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004063 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004064{
Josef Bacik0af3d002010-06-21 14:48:16 -04004065 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004066 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004067 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004068 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004069 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004070 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004071
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004072 /* block accounting for super block */
4073 spin_lock(&info->delalloc_lock);
4074 old_val = btrfs_super_bytes_used(&info->super_copy);
4075 if (alloc)
4076 old_val += num_bytes;
4077 else
4078 old_val -= num_bytes;
4079 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004080 spin_unlock(&info->delalloc_lock);
4081
Chris Masond3977122009-01-05 21:25:51 -05004082 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004083 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004084 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004085 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004086 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4087 BTRFS_BLOCK_GROUP_RAID1 |
4088 BTRFS_BLOCK_GROUP_RAID10))
4089 factor = 2;
4090 else
4091 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004092 /*
4093 * If this block group has free space cache written out, we
4094 * need to make sure to load it if we are removing space. This
4095 * is because we need the unpinning stage to actually add the
4096 * space back to the block group, otherwise we will leak space.
4097 */
4098 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004099 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004100
Chris Masondb945352007-10-15 16:15:53 -04004101 byte_in_group = bytenr - cache->key.objectid;
4102 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004103
Josef Bacik25179202008-10-29 14:49:05 -04004104 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004105 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004106
4107 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4108 cache->disk_cache_state < BTRFS_DC_CLEAR)
4109 cache->disk_cache_state = BTRFS_DC_CLEAR;
4110
Josef Bacik0f9dd462008-09-23 13:14:11 -04004111 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004112 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004113 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004114 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004115 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004116 btrfs_set_block_group_used(&cache->item, old_val);
4117 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004118 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004119 cache->space_info->reservation_progress++;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004120 cache->space_info->bytes_used += num_bytes;
4121 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004122 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004123 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004124 } else {
Chris Masondb945352007-10-15 16:15:53 -04004125 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004126 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004127 cache->pinned += num_bytes;
4128 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004129 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004130 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004131 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004132 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004133
Yan, Zhengf0486c62010-05-16 10:46:25 -04004134 set_extent_dirty(info->pinned_extents,
4135 bytenr, bytenr + num_bytes - 1,
4136 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004137 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004138 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004139 total -= num_bytes;
4140 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004141 }
4142 return 0;
4143}
Chris Mason6324fbf2008-03-24 15:01:59 -04004144
Chris Masona061fc82008-05-07 11:43:44 -04004145static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4146{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004147 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004148 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004149
4150 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4151 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004152 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004153
Yan Zhengd2fb3432008-12-11 16:30:39 -05004154 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004155 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004156
4157 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004158}
4159
Yan, Zhengf0486c62010-05-16 10:46:25 -04004160static int pin_down_extent(struct btrfs_root *root,
4161 struct btrfs_block_group_cache *cache,
4162 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004163{
Yan Zheng11833d62009-09-11 16:11:19 -04004164 spin_lock(&cache->space_info->lock);
4165 spin_lock(&cache->lock);
4166 cache->pinned += num_bytes;
4167 cache->space_info->bytes_pinned += num_bytes;
4168 if (reserved) {
4169 cache->reserved -= num_bytes;
4170 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004171 cache->space_info->reservation_progress++;
Yan324ae4d2007-11-16 14:57:08 -05004172 }
Yan Zheng11833d62009-09-11 16:11:19 -04004173 spin_unlock(&cache->lock);
4174 spin_unlock(&cache->space_info->lock);
4175
Yan, Zhengf0486c62010-05-16 10:46:25 -04004176 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4177 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004178 return 0;
4179}
Chris Mason9078a3e2007-04-26 16:46:15 -04004180
Yan, Zhengf0486c62010-05-16 10:46:25 -04004181/*
4182 * this function must be called within transaction
4183 */
4184int btrfs_pin_extent(struct btrfs_root *root,
4185 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004186{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004187 struct btrfs_block_group_cache *cache;
4188
4189 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4190 BUG_ON(!cache);
4191
4192 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4193
4194 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004195 return 0;
4196}
Zheng Yane8569812008-09-26 10:05:48 -04004197
Yan, Zhengf0486c62010-05-16 10:46:25 -04004198/*
4199 * update size of reserved extents. this function may return -EAGAIN
4200 * if 'reserve' is true or 'sinfo' is false.
4201 */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004202int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4203 u64 num_bytes, int reserve, int sinfo)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004204{
4205 int ret = 0;
4206 if (sinfo) {
4207 struct btrfs_space_info *space_info = cache->space_info;
4208 spin_lock(&space_info->lock);
4209 spin_lock(&cache->lock);
4210 if (reserve) {
4211 if (cache->ro) {
4212 ret = -EAGAIN;
4213 } else {
4214 cache->reserved += num_bytes;
4215 space_info->bytes_reserved += num_bytes;
4216 }
4217 } else {
4218 if (cache->ro)
4219 space_info->bytes_readonly += num_bytes;
4220 cache->reserved -= num_bytes;
4221 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004222 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004223 }
4224 spin_unlock(&cache->lock);
4225 spin_unlock(&space_info->lock);
4226 } else {
4227 spin_lock(&cache->lock);
4228 if (cache->ro) {
4229 ret = -EAGAIN;
4230 } else {
4231 if (reserve)
4232 cache->reserved += num_bytes;
4233 else
4234 cache->reserved -= num_bytes;
4235 }
4236 spin_unlock(&cache->lock);
4237 }
4238 return ret;
4239}
4240
Yan Zheng11833d62009-09-11 16:11:19 -04004241int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4242 struct btrfs_root *root)
4243{
4244 struct btrfs_fs_info *fs_info = root->fs_info;
4245 struct btrfs_caching_control *next;
4246 struct btrfs_caching_control *caching_ctl;
4247 struct btrfs_block_group_cache *cache;
4248
4249 down_write(&fs_info->extent_commit_sem);
4250
4251 list_for_each_entry_safe(caching_ctl, next,
4252 &fs_info->caching_block_groups, list) {
4253 cache = caching_ctl->block_group;
4254 if (block_group_cache_done(cache)) {
4255 cache->last_byte_to_unpin = (u64)-1;
4256 list_del_init(&caching_ctl->list);
4257 put_caching_control(caching_ctl);
4258 } else {
4259 cache->last_byte_to_unpin = caching_ctl->progress;
4260 }
4261 }
4262
4263 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4264 fs_info->pinned_extents = &fs_info->freed_extents[1];
4265 else
4266 fs_info->pinned_extents = &fs_info->freed_extents[0];
4267
4268 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004269
4270 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004271 return 0;
4272}
4273
4274static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4275{
4276 struct btrfs_fs_info *fs_info = root->fs_info;
4277 struct btrfs_block_group_cache *cache = NULL;
4278 u64 len;
4279
4280 while (start <= end) {
4281 if (!cache ||
4282 start >= cache->key.objectid + cache->key.offset) {
4283 if (cache)
4284 btrfs_put_block_group(cache);
4285 cache = btrfs_lookup_block_group(fs_info, start);
4286 BUG_ON(!cache);
4287 }
4288
4289 len = cache->key.objectid + cache->key.offset - start;
4290 len = min(len, end + 1 - start);
4291
4292 if (start < cache->last_byte_to_unpin) {
4293 len = min(len, cache->last_byte_to_unpin - start);
4294 btrfs_add_free_space(cache, start, len);
4295 }
Josef Bacik25179202008-10-29 14:49:05 -04004296
Yan, Zhengf0486c62010-05-16 10:46:25 -04004297 start += len;
4298
Josef Bacik25179202008-10-29 14:49:05 -04004299 spin_lock(&cache->space_info->lock);
4300 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004301 cache->pinned -= len;
4302 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004303 if (cache->ro) {
4304 cache->space_info->bytes_readonly += len;
4305 } else if (cache->reserved_pinned > 0) {
4306 len = min(len, cache->reserved_pinned);
4307 cache->reserved_pinned -= len;
4308 cache->space_info->bytes_reserved += len;
4309 }
Josef Bacik25179202008-10-29 14:49:05 -04004310 spin_unlock(&cache->lock);
4311 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004312 }
4313
4314 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004315 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004316 return 0;
4317}
4318
4319int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004320 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004321{
Yan Zheng11833d62009-09-11 16:11:19 -04004322 struct btrfs_fs_info *fs_info = root->fs_info;
4323 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004324 struct btrfs_block_rsv *block_rsv;
4325 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004326 u64 start;
4327 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004328 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004329 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004330
Yan Zheng11833d62009-09-11 16:11:19 -04004331 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4332 unpin = &fs_info->freed_extents[1];
4333 else
4334 unpin = &fs_info->freed_extents[0];
4335
Chris Masond3977122009-01-05 21:25:51 -05004336 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004337 ret = find_first_extent_bit(unpin, 0, &start, &end,
4338 EXTENT_DIRTY);
4339 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004340 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004341
Li Dongyang5378e602011-03-24 10:24:27 +00004342 if (btrfs_test_opt(root, DISCARD))
4343 ret = btrfs_discard_extent(root, start,
4344 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004345
Chris Mason1a5bc162007-10-15 16:15:26 -04004346 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004347 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004348 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004349 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004350
Yan, Zhengf0486c62010-05-16 10:46:25 -04004351 mutex_lock(&fs_info->durable_block_rsv_mutex);
4352 list_for_each_entry_safe(block_rsv, next_rsv,
4353 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004354
Yan, Zhengf0486c62010-05-16 10:46:25 -04004355 idx = trans->transid & 0x1;
4356 if (block_rsv->freed[idx] > 0) {
4357 block_rsv_add_bytes(block_rsv,
4358 block_rsv->freed[idx], 0);
4359 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004360 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004361 if (atomic_read(&block_rsv->usage) == 0) {
4362 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004363
Yan, Zhengf0486c62010-05-16 10:46:25 -04004364 if (block_rsv->freed[0] == 0 &&
4365 block_rsv->freed[1] == 0) {
4366 list_del_init(&block_rsv->list);
4367 kfree(block_rsv);
4368 }
4369 } else {
4370 btrfs_block_rsv_release(root, block_rsv, 0);
4371 }
4372 }
4373 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4374
Chris Masone20d96d2007-03-22 12:13:20 -04004375 return 0;
4376}
4377
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004378static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4379 struct btrfs_root *root,
4380 u64 bytenr, u64 num_bytes, u64 parent,
4381 u64 root_objectid, u64 owner_objectid,
4382 u64 owner_offset, int refs_to_drop,
4383 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004384{
Chris Masone2fa7222007-03-12 16:22:34 -04004385 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004386 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004387 struct btrfs_fs_info *info = root->fs_info;
4388 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004389 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004390 struct btrfs_extent_item *ei;
4391 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004392 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004393 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004394 int extent_slot = 0;
4395 int found_extent = 0;
4396 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004397 u32 item_size;
4398 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004399
Chris Mason5caf2a02007-04-02 11:20:42 -04004400 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004401 if (!path)
4402 return -ENOMEM;
4403
Chris Mason3c12ac72008-04-21 12:01:38 -04004404 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004405 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004406
4407 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4408 BUG_ON(!is_data && refs_to_drop != 1);
4409
4410 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4411 bytenr, num_bytes, parent,
4412 root_objectid, owner_objectid,
4413 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004414 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004415 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004416 while (extent_slot >= 0) {
4417 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004418 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004419 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004420 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004421 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4422 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004423 found_extent = 1;
4424 break;
4425 }
4426 if (path->slots[0] - extent_slot > 5)
4427 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004428 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004429 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004430#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4431 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4432 if (found_extent && item_size < sizeof(*ei))
4433 found_extent = 0;
4434#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004435 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004436 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004437 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004438 NULL, refs_to_drop,
4439 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004440 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004441 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04004442 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004443
4444 key.objectid = bytenr;
4445 key.type = BTRFS_EXTENT_ITEM_KEY;
4446 key.offset = num_bytes;
4447
Zheng Yan31840ae2008-09-23 13:14:14 -04004448 ret = btrfs_search_slot(trans, extent_root,
4449 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004450 if (ret) {
4451 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004452 ", was looking for %llu\n", ret,
4453 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004454 btrfs_print_leaf(extent_root, path->nodes[0]);
4455 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004456 BUG_ON(ret);
4457 extent_slot = path->slots[0];
4458 }
Chris Mason7bb86312007-12-11 09:25:06 -05004459 } else {
4460 btrfs_print_leaf(extent_root, path->nodes[0]);
4461 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004462 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004463 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004464 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004465 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004466 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004467 (unsigned long long)owner_objectid,
4468 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004469 }
Chris Mason5f39d392007-10-15 16:14:19 -04004470
4471 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004472 item_size = btrfs_item_size_nr(leaf, extent_slot);
4473#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4474 if (item_size < sizeof(*ei)) {
4475 BUG_ON(found_extent || extent_slot != path->slots[0]);
4476 ret = convert_extent_item_v0(trans, extent_root, path,
4477 owner_objectid, 0);
4478 BUG_ON(ret < 0);
4479
David Sterbab3b4aa72011-04-21 01:20:15 +02004480 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004481 path->leave_spinning = 1;
4482
4483 key.objectid = bytenr;
4484 key.type = BTRFS_EXTENT_ITEM_KEY;
4485 key.offset = num_bytes;
4486
4487 ret = btrfs_search_slot(trans, extent_root, &key, path,
4488 -1, 1);
4489 if (ret) {
4490 printk(KERN_ERR "umm, got %d back from search"
4491 ", was looking for %llu\n", ret,
4492 (unsigned long long)bytenr);
4493 btrfs_print_leaf(extent_root, path->nodes[0]);
4494 }
4495 BUG_ON(ret);
4496 extent_slot = path->slots[0];
4497 leaf = path->nodes[0];
4498 item_size = btrfs_item_size_nr(leaf, extent_slot);
4499 }
4500#endif
4501 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004502 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004503 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004504 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4505 struct btrfs_tree_block_info *bi;
4506 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4507 bi = (struct btrfs_tree_block_info *)(ei + 1);
4508 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004509 }
4510
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004511 refs = btrfs_extent_refs(leaf, ei);
4512 BUG_ON(refs < refs_to_drop);
4513 refs -= refs_to_drop;
4514
4515 if (refs > 0) {
4516 if (extent_op)
4517 __run_delayed_extent_op(extent_op, leaf, ei);
4518 /*
4519 * In the case of inline back ref, reference count will
4520 * be updated by remove_extent_backref
4521 */
4522 if (iref) {
4523 BUG_ON(!found_extent);
4524 } else {
4525 btrfs_set_extent_refs(leaf, ei, refs);
4526 btrfs_mark_buffer_dirty(leaf);
4527 }
4528 if (found_extent) {
4529 ret = remove_extent_backref(trans, extent_root, path,
4530 iref, refs_to_drop,
4531 is_data);
4532 BUG_ON(ret);
4533 }
4534 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004535 if (found_extent) {
4536 BUG_ON(is_data && refs_to_drop !=
4537 extent_data_ref_count(root, path, iref));
4538 if (iref) {
4539 BUG_ON(path->slots[0] != extent_slot);
4540 } else {
4541 BUG_ON(path->slots[0] != extent_slot + 1);
4542 path->slots[0] = extent_slot;
4543 num_to_del = 2;
4544 }
Chris Mason78fae272007-03-25 11:35:08 -04004545 }
Chris Masonb9473432009-03-13 11:00:37 -04004546
Chris Mason952fcca2008-02-18 16:33:44 -05004547 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4548 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004549 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004550 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01004551
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004552 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004553 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4554 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004555 } else {
4556 invalidate_mapping_pages(info->btree_inode->i_mapping,
4557 bytenr >> PAGE_CACHE_SHIFT,
4558 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004559 }
4560
Yan, Zhengf0486c62010-05-16 10:46:25 -04004561 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004562 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004563 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004564 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004565 return ret;
4566}
4567
4568/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004569 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004570 * delayed ref for that extent as well. This searches the delayed ref tree for
4571 * a given extent, and if there are no other delayed refs to be processed, it
4572 * removes it from the tree.
4573 */
4574static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4575 struct btrfs_root *root, u64 bytenr)
4576{
4577 struct btrfs_delayed_ref_head *head;
4578 struct btrfs_delayed_ref_root *delayed_refs;
4579 struct btrfs_delayed_ref_node *ref;
4580 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004581 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004582
4583 delayed_refs = &trans->transaction->delayed_refs;
4584 spin_lock(&delayed_refs->lock);
4585 head = btrfs_find_delayed_ref_head(trans, bytenr);
4586 if (!head)
4587 goto out;
4588
4589 node = rb_prev(&head->node.rb_node);
4590 if (!node)
4591 goto out;
4592
4593 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4594
4595 /* there are still entries for this ref, we can't drop it */
4596 if (ref->bytenr == bytenr)
4597 goto out;
4598
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004599 if (head->extent_op) {
4600 if (!head->must_insert_reserved)
4601 goto out;
4602 kfree(head->extent_op);
4603 head->extent_op = NULL;
4604 }
4605
Chris Mason1887be62009-03-13 10:11:24 -04004606 /*
4607 * waiting for the lock here would deadlock. If someone else has it
4608 * locked they are already in the process of dropping it anyway
4609 */
4610 if (!mutex_trylock(&head->mutex))
4611 goto out;
4612
4613 /*
4614 * at this point we have a head with no other entries. Go
4615 * ahead and process it.
4616 */
4617 head->node.in_tree = 0;
4618 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004619
Chris Mason1887be62009-03-13 10:11:24 -04004620 delayed_refs->num_entries--;
4621
4622 /*
4623 * we don't take a ref on the node because we're removing it from the
4624 * tree, so we just steal the ref the tree was holding.
4625 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004626 delayed_refs->num_heads--;
4627 if (list_empty(&head->cluster))
4628 delayed_refs->num_heads_ready--;
4629
4630 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004631 spin_unlock(&delayed_refs->lock);
4632
Yan, Zhengf0486c62010-05-16 10:46:25 -04004633 BUG_ON(head->extent_op);
4634 if (head->must_insert_reserved)
4635 ret = 1;
4636
4637 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004638 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004639 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004640out:
4641 spin_unlock(&delayed_refs->lock);
4642 return 0;
4643}
4644
Yan, Zhengf0486c62010-05-16 10:46:25 -04004645void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4646 struct btrfs_root *root,
4647 struct extent_buffer *buf,
4648 u64 parent, int last_ref)
4649{
4650 struct btrfs_block_rsv *block_rsv;
4651 struct btrfs_block_group_cache *cache = NULL;
4652 int ret;
4653
4654 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4655 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4656 parent, root->root_key.objectid,
4657 btrfs_header_level(buf),
4658 BTRFS_DROP_DELAYED_REF, NULL);
4659 BUG_ON(ret);
4660 }
4661
4662 if (!last_ref)
4663 return;
4664
4665 block_rsv = get_block_rsv(trans, root);
4666 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004667 if (block_rsv->space_info != cache->space_info)
4668 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004669
4670 if (btrfs_header_generation(buf) == trans->transid) {
4671 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4672 ret = check_ref_cleanup(trans, root, buf->start);
4673 if (!ret)
4674 goto pin;
4675 }
4676
4677 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4678 pin_down_extent(root, cache, buf->start, buf->len, 1);
4679 goto pin;
4680 }
4681
4682 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4683
4684 btrfs_add_free_space(cache, buf->start, buf->len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00004685 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004686 if (ret == -EAGAIN) {
4687 /* block group became read-only */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004688 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004689 goto out;
4690 }
4691
4692 ret = 1;
4693 spin_lock(&block_rsv->lock);
4694 if (block_rsv->reserved < block_rsv->size) {
4695 block_rsv->reserved += buf->len;
4696 ret = 0;
4697 }
4698 spin_unlock(&block_rsv->lock);
4699
4700 if (ret) {
4701 spin_lock(&cache->space_info->lock);
4702 cache->space_info->bytes_reserved -= buf->len;
Chris Mason36e39c42011-03-12 07:08:42 -05004703 cache->space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004704 spin_unlock(&cache->space_info->lock);
4705 }
4706 goto out;
4707 }
4708pin:
4709 if (block_rsv->durable && !cache->ro) {
4710 ret = 0;
4711 spin_lock(&cache->lock);
4712 if (!cache->ro) {
4713 cache->reserved_pinned += buf->len;
4714 ret = 1;
4715 }
4716 spin_unlock(&cache->lock);
4717
4718 if (ret) {
4719 spin_lock(&block_rsv->lock);
4720 block_rsv->freed[trans->transid & 0x1] += buf->len;
4721 spin_unlock(&block_rsv->lock);
4722 }
4723 }
4724out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004725 /*
4726 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4727 * anymore.
4728 */
4729 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004730 btrfs_put_block_group(cache);
4731}
4732
Chris Mason925baed2008-06-25 16:01:30 -04004733int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004734 struct btrfs_root *root,
4735 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004736 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004737{
4738 int ret;
4739
Chris Mason56bec292009-03-13 10:10:06 -04004740 /*
4741 * tree log blocks never actually go into the extent allocation
4742 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004743 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004744 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4745 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004746 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004747 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004748 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004749 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4750 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4751 parent, root_objectid, (int)owner,
4752 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004753 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004754 } else {
4755 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4756 parent, root_objectid, owner,
4757 offset, BTRFS_DROP_DELAYED_REF, NULL);
4758 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004759 }
Chris Mason925baed2008-06-25 16:01:30 -04004760 return ret;
4761}
4762
Chris Mason87ee04e2007-11-30 11:30:34 -05004763static u64 stripe_align(struct btrfs_root *root, u64 val)
4764{
4765 u64 mask = ((u64)root->stripesize - 1);
4766 u64 ret = (val + mask) & ~mask;
4767 return ret;
4768}
4769
Chris Masonfec577f2007-02-26 10:40:21 -05004770/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004771 * when we wait for progress in the block group caching, its because
4772 * our allocation attempt failed at least once. So, we must sleep
4773 * and let some progress happen before we try again.
4774 *
4775 * This function will sleep at least once waiting for new free space to
4776 * show up, and then it will check the block group free space numbers
4777 * for our min num_bytes. Another option is to have it go ahead
4778 * and look in the rbtree for a free extent of a given size, but this
4779 * is a good start.
4780 */
4781static noinline int
4782wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4783 u64 num_bytes)
4784{
Yan Zheng11833d62009-09-11 16:11:19 -04004785 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004786 DEFINE_WAIT(wait);
4787
Yan Zheng11833d62009-09-11 16:11:19 -04004788 caching_ctl = get_caching_control(cache);
4789 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004790 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004791
Yan Zheng11833d62009-09-11 16:11:19 -04004792 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08004793 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004794
4795 put_caching_control(caching_ctl);
4796 return 0;
4797}
4798
4799static noinline int
4800wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4801{
4802 struct btrfs_caching_control *caching_ctl;
4803 DEFINE_WAIT(wait);
4804
4805 caching_ctl = get_caching_control(cache);
4806 if (!caching_ctl)
4807 return 0;
4808
4809 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4810
4811 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004812 return 0;
4813}
4814
Yan, Zhengb742bb82010-05-16 10:46:24 -04004815static int get_block_group_index(struct btrfs_block_group_cache *cache)
4816{
4817 int index;
4818 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4819 index = 0;
4820 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4821 index = 1;
4822 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4823 index = 2;
4824 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4825 index = 3;
4826 else
4827 index = 4;
4828 return index;
4829}
4830
Josef Bacik817d52f2009-07-13 21:29:25 -04004831enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004832 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004833 LOOP_CACHING_NOWAIT = 1,
4834 LOOP_CACHING_WAIT = 2,
4835 LOOP_ALLOC_CHUNK = 3,
4836 LOOP_NO_EMPTY_SIZE = 4,
4837};
4838
4839/*
Chris Masonfec577f2007-02-26 10:40:21 -05004840 * walks the btree of allocated extents and find a hole of a given size.
4841 * The key ins is changed to record the hole:
4842 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004843 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004844 * ins->offset == number of blocks
4845 * Any available blocks before search_start are skipped.
4846 */
Chris Masond3977122009-01-05 21:25:51 -05004847static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004848 struct btrfs_root *orig_root,
4849 u64 num_bytes, u64 empty_size,
4850 u64 search_start, u64 search_end,
4851 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00004852 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004853{
Josef Bacik80eb2342008-10-29 14:49:05 -04004854 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004855 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004856 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004857 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004858 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004859 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004860 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004861 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004862 int last_ptr_loop = 0;
4863 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004864 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004865 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004866 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004867 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004868 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004869 u64 ideal_cache_percent = 0;
4870 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004871
Chris Masondb945352007-10-15 16:15:53 -04004872 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004873 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004874 ins->objectid = 0;
4875 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004876
Josef Bacik2552d172009-04-03 10:14:19 -04004877 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004878 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00004879 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004880 return -ENOSPC;
4881 }
Josef Bacik2552d172009-04-03 10:14:19 -04004882
Josef Bacik67377732010-09-16 16:19:09 -04004883 /*
4884 * If the space info is for both data and metadata it means we have a
4885 * small filesystem and we can't use the clustering stuff.
4886 */
4887 if (btrfs_mixed_space_info(space_info))
4888 use_cluster = false;
4889
Chris Mason0ef3e662008-05-24 14:04:53 -04004890 if (orig_root->ref_cows || empty_size)
4891 allowed_chunk_alloc = 1;
4892
Josef Bacik67377732010-09-16 16:19:09 -04004893 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004894 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004895 if (!btrfs_test_opt(root, SSD))
4896 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004897 }
4898
Josef Bacik67377732010-09-16 16:19:09 -04004899 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4900 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004901 last_ptr = &root->fs_info->data_alloc_cluster;
4902 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004903
Chris Mason239b14b2008-03-24 15:02:07 -04004904 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004905 spin_lock(&last_ptr->lock);
4906 if (last_ptr->block_group)
4907 hint_byte = last_ptr->window_start;
4908 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004909 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004910
Chris Masona061fc82008-05-07 11:43:44 -04004911 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004912 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004913
Josef Bacik817d52f2009-07-13 21:29:25 -04004914 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004915 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004916
Josef Bacik2552d172009-04-03 10:14:19 -04004917 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004918ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004919 block_group = btrfs_lookup_block_group(root->fs_info,
4920 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004921 /*
4922 * we don't want to use the block group if it doesn't match our
4923 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004924 *
4925 * However if we are re-searching with an ideal block group
4926 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004927 */
4928 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004929 (block_group->cached != BTRFS_CACHE_NO ||
4930 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004931 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004932 if (list_empty(&block_group->list) ||
4933 block_group->ro) {
4934 /*
4935 * someone is removing this block group,
4936 * we can't jump into the have_block_group
4937 * target because our list pointers are not
4938 * valid
4939 */
4940 btrfs_put_block_group(block_group);
4941 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004942 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004943 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004944 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004945 }
Josef Bacik2552d172009-04-03 10:14:19 -04004946 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004947 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004948 }
Chris Mason42e70e72008-11-07 18:17:11 -05004949 }
Josef Bacik2552d172009-04-03 10:14:19 -04004950search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004951 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004952 list_for_each_entry(block_group, &space_info->block_groups[index],
4953 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004954 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004955 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004956
Josef Bacik11dfe352009-11-13 20:12:59 +00004957 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004958 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004959
Chris Mason83a50de2010-12-13 15:06:46 -05004960 /*
4961 * this can happen if we end up cycling through all the
4962 * raid types, but we want to make sure we only allocate
4963 * for the proper type.
4964 */
4965 if (!block_group_bits(block_group, data)) {
4966 u64 extra = BTRFS_BLOCK_GROUP_DUP |
4967 BTRFS_BLOCK_GROUP_RAID1 |
4968 BTRFS_BLOCK_GROUP_RAID10;
4969
4970 /*
4971 * if they asked for extra copies and this block group
4972 * doesn't provide them, bail. This does allow us to
4973 * fill raid0 from raid1.
4974 */
4975 if ((data & extra) && !(block_group->flags & extra))
4976 goto loop;
4977 }
4978
Josef Bacik2552d172009-04-03 10:14:19 -04004979have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004980 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004981 u64 free_percent;
4982
Josef Bacikb8399de2010-12-08 09:15:11 -05004983 ret = cache_block_group(block_group, trans,
4984 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04004985 if (block_group->cached == BTRFS_CACHE_FINISHED)
4986 goto have_block_group;
4987
Josef Bacikccf0e722009-11-10 21:23:48 -05004988 free_percent = btrfs_block_group_used(&block_group->item);
4989 free_percent *= 100;
4990 free_percent = div64_u64(free_percent,
4991 block_group->key.offset);
4992 free_percent = 100 - free_percent;
4993 if (free_percent > ideal_cache_percent &&
4994 likely(!block_group->ro)) {
4995 ideal_cache_offset = block_group->key.objectid;
4996 ideal_cache_percent = free_percent;
4997 }
4998
Josef Bacik817d52f2009-07-13 21:29:25 -04004999 /*
Josef Bacikbab39bf2011-06-30 14:42:28 -04005000 * The caching workers are limited to 2 threads, so we
5001 * can queue as much work as we care to.
Josef Bacik817d52f2009-07-13 21:29:25 -04005002 */
Josef Bacikbab39bf2011-06-30 14:42:28 -04005003 if (loop > LOOP_FIND_IDEAL) {
Josef Bacikb8399de2010-12-08 09:15:11 -05005004 ret = cache_block_group(block_group, trans,
5005 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005006 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005007 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005008 found_uncached_bg = true;
5009
5010 /*
5011 * If loop is set for cached only, try the next block
5012 * group.
5013 */
5014 if (loop == LOOP_FIND_IDEAL)
5015 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005016 }
5017
Josef Bacik817d52f2009-07-13 21:29:25 -04005018 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005019 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005020 found_uncached_bg = true;
5021
Josef Bacikea6a4782008-11-20 12:16:16 -05005022 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005023 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005024
Chris Masonff5714c2011-05-28 07:00:39 -04005025 spin_lock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005026 if (cached &&
Chris Masonff5714c2011-05-28 07:00:39 -04005027 block_group->free_space_ctl->free_space <
5028 num_bytes + empty_size) {
5029 spin_unlock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005030 goto loop;
5031 }
Chris Masonff5714c2011-05-28 07:00:39 -04005032 spin_unlock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005033
Josef Bacik0a243252009-09-11 16:11:20 -04005034 /*
5035 * Ok we want to try and use the cluster allocator, so lets look
5036 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5037 * have tried the cluster allocator plenty of times at this
5038 * point and not have found anything, so we are likely way too
5039 * fragmented for the clustering stuff to find anything, so lets
5040 * just skip it and let the allocator find whatever block it can
5041 * find
5042 */
5043 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005044 /*
5045 * the refill lock keeps out other
5046 * people trying to start a new cluster
5047 */
5048 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005049 if (last_ptr->block_group &&
5050 (last_ptr->block_group->ro ||
5051 !block_group_bits(last_ptr->block_group, data))) {
5052 offset = 0;
5053 goto refill_cluster;
5054 }
5055
Chris Masonfa9c0d792009-04-03 09:47:43 -04005056 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5057 num_bytes, search_start);
5058 if (offset) {
5059 /* we have a block, we're done */
5060 spin_unlock(&last_ptr->refill_lock);
5061 goto checks;
5062 }
5063
5064 spin_lock(&last_ptr->lock);
5065 /*
5066 * whoops, this cluster doesn't actually point to
5067 * this block group. Get a ref on the block
5068 * group is does point to and try again
5069 */
5070 if (!last_ptr_loop && last_ptr->block_group &&
5071 last_ptr->block_group != block_group) {
5072
5073 btrfs_put_block_group(block_group);
5074 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005075 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005076 spin_unlock(&last_ptr->lock);
5077 spin_unlock(&last_ptr->refill_lock);
5078
5079 last_ptr_loop = 1;
5080 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005081 /*
5082 * we know this block group is properly
5083 * in the list because
5084 * btrfs_remove_block_group, drops the
5085 * cluster before it removes the block
5086 * group from the list
5087 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04005088 goto have_block_group;
5089 }
5090 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005091refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04005092 /*
5093 * this cluster didn't work out, free it and
5094 * start over
5095 */
5096 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5097
5098 last_ptr_loop = 0;
5099
5100 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005101 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005102 block_group, last_ptr,
5103 offset, num_bytes,
5104 empty_cluster + empty_size);
5105 if (ret == 0) {
5106 /*
5107 * now pull our allocation out of this
5108 * cluster
5109 */
5110 offset = btrfs_alloc_from_cluster(block_group,
5111 last_ptr, num_bytes,
5112 search_start);
5113 if (offset) {
5114 /* we found one, proceed */
5115 spin_unlock(&last_ptr->refill_lock);
5116 goto checks;
5117 }
Josef Bacik0a243252009-09-11 16:11:20 -04005118 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5119 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005120 spin_unlock(&last_ptr->refill_lock);
5121
Josef Bacik0a243252009-09-11 16:11:20 -04005122 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005123 wait_block_group_cache_progress(block_group,
5124 num_bytes + empty_cluster + empty_size);
5125 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005126 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005127
Chris Masonfa9c0d792009-04-03 09:47:43 -04005128 /*
5129 * at this point we either didn't find a cluster
5130 * or we weren't able to allocate a block from our
5131 * cluster. Free the cluster we've been trying
5132 * to use, and go to the next block group
5133 */
Josef Bacik0a243252009-09-11 16:11:20 -04005134 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005135 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005136 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005137 }
5138
Josef Bacik6226cb02009-04-03 10:14:18 -04005139 offset = btrfs_find_space_for_alloc(block_group, search_start,
5140 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005141 /*
5142 * If we didn't find a chunk, and we haven't failed on this
5143 * block group before, and this block group is in the middle of
5144 * caching and we are ok with waiting, then go ahead and wait
5145 * for progress to be made, and set failed_alloc to true.
5146 *
5147 * If failed_alloc is true then we've already waited on this
5148 * block group once and should move on to the next block group.
5149 */
5150 if (!offset && !failed_alloc && !cached &&
5151 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005152 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005153 num_bytes + empty_size);
5154 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005155 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005156 } else if (!offset) {
5157 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005158 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005159checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005160 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005161 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005162 if (search_start + num_bytes >= search_end) {
5163 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005164 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005165 }
Chris Masone37c9e62007-05-09 20:13:14 -04005166
Josef Bacik2552d172009-04-03 10:14:19 -04005167 /* move on to the next group */
5168 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005169 block_group->key.objectid + block_group->key.offset) {
5170 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005171 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005172 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005173
Josef Bacik2552d172009-04-03 10:14:19 -04005174 ins->objectid = search_start;
5175 ins->offset = num_bytes;
5176
Josef Bacik6226cb02009-04-03 10:14:18 -04005177 if (offset < search_start)
5178 btrfs_add_free_space(block_group, offset,
5179 search_start - offset);
5180 BUG_ON(offset > search_start);
5181
Li Dongyangb4d00d52011-03-24 10:24:25 +00005182 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005183 (data & BTRFS_BLOCK_GROUP_DATA));
5184 if (ret == -EAGAIN) {
5185 btrfs_add_free_space(block_group, offset, num_bytes);
5186 goto loop;
5187 }
Yan Zheng11833d62009-09-11 16:11:19 -04005188
Josef Bacik2552d172009-04-03 10:14:19 -04005189 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005190 ins->objectid = search_start;
5191 ins->offset = num_bytes;
5192
5193 if (offset < search_start)
5194 btrfs_add_free_space(block_group, offset,
5195 search_start - offset);
5196 BUG_ON(offset > search_start);
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005197 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005198 break;
5199loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005200 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005201 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005202 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d792009-04-03 09:47:43 -04005203 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005204 }
5205 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005206
Yan, Zhengb742bb82010-05-16 10:46:24 -04005207 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5208 goto search;
5209
Josef Bacikccf0e722009-11-10 21:23:48 -05005210 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5211 * for them to make caching progress. Also
5212 * determine the best possible bg to cache
5213 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5214 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005215 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5216 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5217 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5218 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04005219 */
Josef Bacik723bda22011-05-27 16:11:38 -04005220 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005221 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005222 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005223 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005224 loop++;
Josef Bacikbab39bf2011-06-30 14:42:28 -04005225 if (!ideal_cache_percent)
Josef Bacik817d52f2009-07-13 21:29:25 -04005226 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005227
5228 /*
5229 * 1 of the following 2 things have happened so far
5230 *
5231 * 1) We found an ideal block group for caching that
5232 * is mostly full and will cache quickly, so we might
5233 * as well wait for it.
5234 *
5235 * 2) We searched for cached only and we didn't find
5236 * anything, and we didn't start any caching kthreads
5237 * either, so chances are we will loop through and
5238 * start a couple caching kthreads, and then come back
5239 * around and just wait for them. This will be slower
5240 * because we will have 2 caching kthreads reading at
5241 * the same time when we could have just started one
5242 * and waited for it to get far enough to give us an
5243 * allocation, so go ahead and go to the wait caching
5244 * loop.
5245 */
5246 loop = LOOP_CACHING_WAIT;
5247 search_start = ideal_cache_offset;
5248 ideal_cache_percent = 0;
5249 goto ideal_cache;
5250 } else if (loop == LOOP_FIND_IDEAL) {
5251 /*
5252 * Didn't find a uncached bg, wait on anything we find
5253 * next.
5254 */
5255 loop = LOOP_CACHING_WAIT;
5256 goto search;
5257 }
5258
Josef Bacik723bda22011-05-27 16:11:38 -04005259 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04005260
5261 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik723bda22011-05-27 16:11:38 -04005262 if (allowed_chunk_alloc) {
5263 ret = do_chunk_alloc(trans, root, num_bytes +
5264 2 * 1024 * 1024, data,
5265 CHUNK_ALLOC_LIMITED);
5266 allowed_chunk_alloc = 0;
5267 if (ret == 1)
5268 done_chunk_alloc = 1;
5269 } else if (!done_chunk_alloc &&
5270 space_info->force_alloc ==
5271 CHUNK_ALLOC_NO_FORCE) {
5272 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
5273 }
5274
5275 /*
5276 * We didn't allocate a chunk, go ahead and drop the
5277 * empty size and loop again.
5278 */
5279 if (!done_chunk_alloc)
5280 loop = LOOP_NO_EMPTY_SIZE;
5281 }
5282
5283 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005284 empty_size = 0;
5285 empty_cluster = 0;
5286 }
Chris Mason42e70e72008-11-07 18:17:11 -05005287
Josef Bacik723bda22011-05-27 16:11:38 -04005288 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04005289 } else if (!ins->objectid) {
5290 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005291 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005292 ret = 0;
5293 }
Chris Mason0b86a832008-03-24 15:01:56 -04005294
Chris Mason0f70abe2007-02-28 16:46:22 -05005295 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005296}
Chris Masonec44a352008-04-28 15:29:52 -04005297
Josef Bacik9ed74f22009-09-11 16:12:44 -04005298static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5299 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005300{
5301 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005302 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005303
Josef Bacik9ed74f22009-09-11 16:12:44 -04005304 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005305 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5306 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005307 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005308 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005309 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005310 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5311 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005312 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005313 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005314 (unsigned long long)info->bytes_pinned,
5315 (unsigned long long)info->bytes_reserved,
5316 (unsigned long long)info->bytes_may_use,
5317 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005318 spin_unlock(&info->lock);
5319
5320 if (!dump_block_groups)
5321 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005322
Josef Bacik80eb2342008-10-29 14:49:05 -04005323 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005324again:
5325 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005326 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005327 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5328 "%llu pinned %llu reserved\n",
5329 (unsigned long long)cache->key.objectid,
5330 (unsigned long long)cache->key.offset,
5331 (unsigned long long)btrfs_block_group_used(&cache->item),
5332 (unsigned long long)cache->pinned,
5333 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005334 btrfs_dump_free_space(cache, bytes);
5335 spin_unlock(&cache->lock);
5336 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005337 if (++index < BTRFS_NR_RAID_TYPES)
5338 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005339 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005340}
Zheng Yane8569812008-09-26 10:05:48 -04005341
Yan Zheng11833d62009-09-11 16:11:19 -04005342int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5343 struct btrfs_root *root,
5344 u64 num_bytes, u64 min_alloc_size,
5345 u64 empty_size, u64 hint_byte,
5346 u64 search_end, struct btrfs_key *ins,
5347 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005348{
5349 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005350 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005351
Josef Bacik6a632092009-02-20 11:00:09 -05005352 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005353again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005354 /*
5355 * the only place that sets empty_size is btrfs_realloc_node, which
5356 * is not called recursively on allocations
5357 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005358 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005359 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005360 num_bytes + 2 * 1024 * 1024, data,
5361 CHUNK_ALLOC_NO_FORCE);
Chris Mason0b86a832008-03-24 15:01:56 -04005362
Chris Masondb945352007-10-15 16:15:53 -04005363 WARN_ON(num_bytes < root->sectorsize);
5364 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005365 search_start, search_end, hint_byte,
5366 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005367
Chris Mason98d20f62008-04-14 09:46:10 -04005368 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5369 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005370 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005371 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005372 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005373 num_bytes, data, CHUNK_ALLOC_FORCE);
Chris Mason98d20f62008-04-14 09:46:10 -04005374 goto again;
5375 }
Chris Mason91435652011-02-16 13:10:41 -05005376 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005377 struct btrfs_space_info *sinfo;
5378
5379 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005380 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5381 "wanted %llu\n", (unsigned long long)data,
5382 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005383 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005384 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005385
liubo1abe9b82011-03-24 11:18:59 +00005386 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5387
Josef Bacik0f9dd462008-09-23 13:14:11 -04005388 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005389}
5390
Chris Mason65b51a02008-08-01 15:11:20 -04005391int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5392{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005393 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005394 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005395
Josef Bacik0f9dd462008-09-23 13:14:11 -04005396 cache = btrfs_lookup_block_group(root->fs_info, start);
5397 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005398 printk(KERN_ERR "Unable to find block group for %llu\n",
5399 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005400 return -ENOSPC;
5401 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005402
Li Dongyang5378e602011-03-24 10:24:27 +00005403 if (btrfs_test_opt(root, DISCARD))
5404 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005405
Josef Bacik0f9dd462008-09-23 13:14:11 -04005406 btrfs_add_free_space(cache, start, len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00005407 btrfs_update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005408 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005409
liubo1abe9b82011-03-24 11:18:59 +00005410 trace_btrfs_reserved_extent_free(root, start, len);
5411
Chris Masone6dcd2d2008-07-17 12:53:50 -04005412 return ret;
5413}
5414
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005415static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5416 struct btrfs_root *root,
5417 u64 parent, u64 root_objectid,
5418 u64 flags, u64 owner, u64 offset,
5419 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005420{
5421 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005422 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005423 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005424 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005425 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005426 struct extent_buffer *leaf;
5427 int type;
5428 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005429
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005430 if (parent > 0)
5431 type = BTRFS_SHARED_DATA_REF_KEY;
5432 else
5433 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005434
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005435 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005436
5437 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005438 if (!path)
5439 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005440
Chris Masonb9473432009-03-13 11:00:37 -04005441 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005442 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5443 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005444 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005445
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005446 leaf = path->nodes[0];
5447 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005448 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005449 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5450 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5451 btrfs_set_extent_flags(leaf, extent_item,
5452 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005453
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005454 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5455 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5456 if (parent > 0) {
5457 struct btrfs_shared_data_ref *ref;
5458 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5459 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5460 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5461 } else {
5462 struct btrfs_extent_data_ref *ref;
5463 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5464 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5465 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5466 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5467 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5468 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005469
5470 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005471 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005472
Yan, Zhengf0486c62010-05-16 10:46:25 -04005473 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005474 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005475 printk(KERN_ERR "btrfs update block group failed for %llu "
5476 "%llu\n", (unsigned long long)ins->objectid,
5477 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005478 BUG();
5479 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005480 return ret;
5481}
5482
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005483static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5484 struct btrfs_root *root,
5485 u64 parent, u64 root_objectid,
5486 u64 flags, struct btrfs_disk_key *key,
5487 int level, struct btrfs_key *ins)
5488{
5489 int ret;
5490 struct btrfs_fs_info *fs_info = root->fs_info;
5491 struct btrfs_extent_item *extent_item;
5492 struct btrfs_tree_block_info *block_info;
5493 struct btrfs_extent_inline_ref *iref;
5494 struct btrfs_path *path;
5495 struct extent_buffer *leaf;
5496 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5497
5498 path = btrfs_alloc_path();
5499 BUG_ON(!path);
5500
5501 path->leave_spinning = 1;
5502 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5503 ins, size);
5504 BUG_ON(ret);
5505
5506 leaf = path->nodes[0];
5507 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5508 struct btrfs_extent_item);
5509 btrfs_set_extent_refs(leaf, extent_item, 1);
5510 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5511 btrfs_set_extent_flags(leaf, extent_item,
5512 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5513 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5514
5515 btrfs_set_tree_block_key(leaf, block_info, key);
5516 btrfs_set_tree_block_level(leaf, block_info, level);
5517
5518 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5519 if (parent > 0) {
5520 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5521 btrfs_set_extent_inline_ref_type(leaf, iref,
5522 BTRFS_SHARED_BLOCK_REF_KEY);
5523 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5524 } else {
5525 btrfs_set_extent_inline_ref_type(leaf, iref,
5526 BTRFS_TREE_BLOCK_REF_KEY);
5527 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5528 }
5529
5530 btrfs_mark_buffer_dirty(leaf);
5531 btrfs_free_path(path);
5532
Yan, Zhengf0486c62010-05-16 10:46:25 -04005533 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005534 if (ret) {
5535 printk(KERN_ERR "btrfs update block group failed for %llu "
5536 "%llu\n", (unsigned long long)ins->objectid,
5537 (unsigned long long)ins->offset);
5538 BUG();
5539 }
5540 return ret;
5541}
5542
5543int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5544 struct btrfs_root *root,
5545 u64 root_objectid, u64 owner,
5546 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005547{
5548 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04005549
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005550 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005551
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005552 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5553 0, root_objectid, owner, offset,
5554 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005555 return ret;
5556}
Chris Masone02119d2008-09-05 16:13:11 -04005557
5558/*
5559 * this is used by the tree logging recovery code. It records that
5560 * an extent has been allocated and makes sure to clear the free
5561 * space cache bits as well
5562 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005563int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5564 struct btrfs_root *root,
5565 u64 root_objectid, u64 owner, u64 offset,
5566 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005567{
5568 int ret;
5569 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005570 struct btrfs_caching_control *caching_ctl;
5571 u64 start = ins->objectid;
5572 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005573
Chris Masone02119d2008-09-05 16:13:11 -04005574 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005575 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005576 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005577
Yan Zheng11833d62009-09-11 16:11:19 -04005578 if (!caching_ctl) {
5579 BUG_ON(!block_group_cache_done(block_group));
5580 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5581 BUG_ON(ret);
5582 } else {
5583 mutex_lock(&caching_ctl->mutex);
5584
5585 if (start >= caching_ctl->progress) {
5586 ret = add_excluded_extent(root, start, num_bytes);
5587 BUG_ON(ret);
5588 } else if (start + num_bytes <= caching_ctl->progress) {
5589 ret = btrfs_remove_free_space(block_group,
5590 start, num_bytes);
5591 BUG_ON(ret);
5592 } else {
5593 num_bytes = caching_ctl->progress - start;
5594 ret = btrfs_remove_free_space(block_group,
5595 start, num_bytes);
5596 BUG_ON(ret);
5597
5598 start = caching_ctl->progress;
5599 num_bytes = ins->objectid + ins->offset -
5600 caching_ctl->progress;
5601 ret = add_excluded_extent(root, start, num_bytes);
5602 BUG_ON(ret);
5603 }
5604
5605 mutex_unlock(&caching_ctl->mutex);
5606 put_caching_control(caching_ctl);
5607 }
5608
Li Dongyangb4d00d52011-03-24 10:24:25 +00005609 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005610 BUG_ON(ret);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005611 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005612 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5613 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005614 return ret;
5615}
5616
Chris Mason65b51a02008-08-01 15:11:20 -04005617struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5618 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005619 u64 bytenr, u32 blocksize,
5620 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005621{
5622 struct extent_buffer *buf;
5623
5624 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5625 if (!buf)
5626 return ERR_PTR(-ENOMEM);
5627 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005628 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005629 btrfs_tree_lock(buf);
5630 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005631
5632 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005633 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005634
Chris Masond0c803c2008-09-11 16:17:57 -04005635 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005636 /*
5637 * we allow two log transactions at a time, use different
5638 * EXENT bit to differentiate dirty pages.
5639 */
5640 if (root->log_transid % 2 == 0)
5641 set_extent_dirty(&root->dirty_log_pages, buf->start,
5642 buf->start + buf->len - 1, GFP_NOFS);
5643 else
5644 set_extent_new(&root->dirty_log_pages, buf->start,
5645 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005646 } else {
5647 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5648 buf->start + buf->len - 1, GFP_NOFS);
5649 }
Chris Mason65b51a02008-08-01 15:11:20 -04005650 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005651 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005652 return buf;
5653}
5654
Yan, Zhengf0486c62010-05-16 10:46:25 -04005655static struct btrfs_block_rsv *
5656use_block_rsv(struct btrfs_trans_handle *trans,
5657 struct btrfs_root *root, u32 blocksize)
5658{
5659 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005660 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005661 int ret;
5662
5663 block_rsv = get_block_rsv(trans, root);
5664
5665 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005666 ret = reserve_metadata_bytes(trans, root, block_rsv,
5667 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005668 /*
5669 * If we couldn't reserve metadata bytes try and use some from
5670 * the global reserve.
5671 */
5672 if (ret && block_rsv != global_rsv) {
5673 ret = block_rsv_use_bytes(global_rsv, blocksize);
5674 if (!ret)
5675 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005676 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005677 } else if (ret) {
5678 return ERR_PTR(ret);
5679 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005680 return block_rsv;
5681 }
5682
5683 ret = block_rsv_use_bytes(block_rsv, blocksize);
5684 if (!ret)
5685 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005686 if (ret) {
5687 WARN_ON(1);
5688 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5689 0);
5690 if (!ret) {
5691 spin_lock(&block_rsv->lock);
5692 block_rsv->size += blocksize;
5693 spin_unlock(&block_rsv->lock);
5694 return block_rsv;
5695 } else if (ret && block_rsv != global_rsv) {
5696 ret = block_rsv_use_bytes(global_rsv, blocksize);
5697 if (!ret)
5698 return global_rsv;
5699 }
5700 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005701
Yan, Zhengf0486c62010-05-16 10:46:25 -04005702 return ERR_PTR(-ENOSPC);
5703}
5704
5705static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5706{
5707 block_rsv_add_bytes(block_rsv, blocksize, 0);
5708 block_rsv_release_bytes(block_rsv, NULL, 0);
5709}
5710
Chris Masonfec577f2007-02-26 10:40:21 -05005711/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005712 * finds a free extent and does all the dirty work required for allocation
5713 * returns the key for the extent through ins, and a tree buffer for
5714 * the first block of the extent through buf.
5715 *
Chris Masonfec577f2007-02-26 10:40:21 -05005716 * returns the tree buffer or NULL.
5717 */
Chris Mason5f39d392007-10-15 16:14:19 -04005718struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005719 struct btrfs_root *root, u32 blocksize,
5720 u64 parent, u64 root_objectid,
5721 struct btrfs_disk_key *key, int level,
5722 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005723{
Chris Masone2fa7222007-03-12 16:22:34 -04005724 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005725 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005726 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005727 u64 flags = 0;
5728 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005729
Yan, Zhengf0486c62010-05-16 10:46:25 -04005730
5731 block_rsv = use_block_rsv(trans, root, blocksize);
5732 if (IS_ERR(block_rsv))
5733 return ERR_CAST(block_rsv);
5734
5735 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5736 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005737 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005738 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005739 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005740 }
Chris Mason55c69072008-01-09 15:55:33 -05005741
Chris Mason4008c042009-02-12 14:09:45 -05005742 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5743 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005744 BUG_ON(IS_ERR(buf));
5745
5746 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5747 if (parent == 0)
5748 parent = ins.objectid;
5749 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5750 } else
5751 BUG_ON(parent > 0);
5752
5753 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5754 struct btrfs_delayed_extent_op *extent_op;
5755 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5756 BUG_ON(!extent_op);
5757 if (key)
5758 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5759 else
5760 memset(&extent_op->key, 0, sizeof(extent_op->key));
5761 extent_op->flags_to_set = flags;
5762 extent_op->update_key = 1;
5763 extent_op->update_flags = 1;
5764 extent_op->is_data = 0;
5765
5766 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5767 ins.offset, parent, root_objectid,
5768 level, BTRFS_ADD_DELAYED_EXTENT,
5769 extent_op);
5770 BUG_ON(ret);
5771 }
Chris Masonfec577f2007-02-26 10:40:21 -05005772 return buf;
5773}
Chris Masona28ec192007-03-06 20:08:01 -05005774
Yan Zheng2c47e6052009-06-27 21:07:35 -04005775struct walk_control {
5776 u64 refs[BTRFS_MAX_LEVEL];
5777 u64 flags[BTRFS_MAX_LEVEL];
5778 struct btrfs_key update_progress;
5779 int stage;
5780 int level;
5781 int shared_level;
5782 int update_ref;
5783 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005784 int reada_slot;
5785 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005786};
5787
5788#define DROP_REFERENCE 1
5789#define UPDATE_BACKREF 2
5790
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005791static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5792 struct btrfs_root *root,
5793 struct walk_control *wc,
5794 struct btrfs_path *path)
5795{
5796 u64 bytenr;
5797 u64 generation;
5798 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005799 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005800 u32 nritems;
5801 u32 blocksize;
5802 struct btrfs_key key;
5803 struct extent_buffer *eb;
5804 int ret;
5805 int slot;
5806 int nread = 0;
5807
5808 if (path->slots[wc->level] < wc->reada_slot) {
5809 wc->reada_count = wc->reada_count * 2 / 3;
5810 wc->reada_count = max(wc->reada_count, 2);
5811 } else {
5812 wc->reada_count = wc->reada_count * 3 / 2;
5813 wc->reada_count = min_t(int, wc->reada_count,
5814 BTRFS_NODEPTRS_PER_BLOCK(root));
5815 }
5816
5817 eb = path->nodes[wc->level];
5818 nritems = btrfs_header_nritems(eb);
5819 blocksize = btrfs_level_size(root, wc->level - 1);
5820
5821 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5822 if (nread >= wc->reada_count)
5823 break;
5824
5825 cond_resched();
5826 bytenr = btrfs_node_blockptr(eb, slot);
5827 generation = btrfs_node_ptr_generation(eb, slot);
5828
5829 if (slot == path->slots[wc->level])
5830 goto reada;
5831
5832 if (wc->stage == UPDATE_BACKREF &&
5833 generation <= root->root_key.offset)
5834 continue;
5835
Yan, Zheng94fcca92009-10-09 09:25:16 -04005836 /* We don't lock the tree block, it's OK to be racy here */
5837 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5838 &refs, &flags);
5839 BUG_ON(ret);
5840 BUG_ON(refs == 0);
5841
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005842 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005843 if (refs == 1)
5844 goto reada;
5845
Yan, Zheng94fcca92009-10-09 09:25:16 -04005846 if (wc->level == 1 &&
5847 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5848 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005849 if (!wc->update_ref ||
5850 generation <= root->root_key.offset)
5851 continue;
5852 btrfs_node_key_to_cpu(eb, &key, slot);
5853 ret = btrfs_comp_cpu_keys(&key,
5854 &wc->update_progress);
5855 if (ret < 0)
5856 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005857 } else {
5858 if (wc->level == 1 &&
5859 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5860 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005861 }
5862reada:
5863 ret = readahead_tree_block(root, bytenr, blocksize,
5864 generation);
5865 if (ret)
5866 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005867 nread++;
5868 }
5869 wc->reada_slot = slot;
5870}
5871
Chris Mason9aca1d52007-03-13 11:09:37 -04005872/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005873 * hepler to process tree block while walking down the tree.
5874 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005875 * when wc->stage == UPDATE_BACKREF, this function updates
5876 * back refs for pointers in the block.
5877 *
5878 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005879 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005880static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5881 struct btrfs_root *root,
5882 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005883 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005884{
5885 int level = wc->level;
5886 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005887 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5888 int ret;
5889
5890 if (wc->stage == UPDATE_BACKREF &&
5891 btrfs_header_owner(eb) != root->root_key.objectid)
5892 return 1;
5893
5894 /*
5895 * when reference count of tree block is 1, it won't increase
5896 * again. once full backref flag is set, we never clear it.
5897 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005898 if (lookup_info &&
5899 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5900 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005901 BUG_ON(!path->locks[level]);
5902 ret = btrfs_lookup_extent_info(trans, root,
5903 eb->start, eb->len,
5904 &wc->refs[level],
5905 &wc->flags[level]);
5906 BUG_ON(ret);
5907 BUG_ON(wc->refs[level] == 0);
5908 }
5909
Yan Zheng2c47e6052009-06-27 21:07:35 -04005910 if (wc->stage == DROP_REFERENCE) {
5911 if (wc->refs[level] > 1)
5912 return 1;
5913
5914 if (path->locks[level] && !wc->keep_locks) {
5915 btrfs_tree_unlock(eb);
5916 path->locks[level] = 0;
5917 }
5918 return 0;
5919 }
5920
5921 /* wc->stage == UPDATE_BACKREF */
5922 if (!(wc->flags[level] & flag)) {
5923 BUG_ON(!path->locks[level]);
5924 ret = btrfs_inc_ref(trans, root, eb, 1);
5925 BUG_ON(ret);
5926 ret = btrfs_dec_ref(trans, root, eb, 0);
5927 BUG_ON(ret);
5928 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5929 eb->len, flag, 0);
5930 BUG_ON(ret);
5931 wc->flags[level] |= flag;
5932 }
5933
5934 /*
5935 * the block is shared by multiple trees, so it's not good to
5936 * keep the tree lock
5937 */
5938 if (path->locks[level] && level > 0) {
5939 btrfs_tree_unlock(eb);
5940 path->locks[level] = 0;
5941 }
5942 return 0;
5943}
5944
5945/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005946 * hepler to process tree block pointer.
5947 *
5948 * when wc->stage == DROP_REFERENCE, this function checks
5949 * reference count of the block pointed to. if the block
5950 * is shared and we need update back refs for the subtree
5951 * rooted at the block, this function changes wc->stage to
5952 * UPDATE_BACKREF. if the block is shared and there is no
5953 * need to update back, this function drops the reference
5954 * to the block.
5955 *
5956 * NOTE: return value 1 means we should stop walking down.
5957 */
5958static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5959 struct btrfs_root *root,
5960 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005961 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005962{
5963 u64 bytenr;
5964 u64 generation;
5965 u64 parent;
5966 u32 blocksize;
5967 struct btrfs_key key;
5968 struct extent_buffer *next;
5969 int level = wc->level;
5970 int reada = 0;
5971 int ret = 0;
5972
5973 generation = btrfs_node_ptr_generation(path->nodes[level],
5974 path->slots[level]);
5975 /*
5976 * if the lower level block was created before the snapshot
5977 * was created, we know there is no need to update back refs
5978 * for the subtree
5979 */
5980 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005981 generation <= root->root_key.offset) {
5982 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005983 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005984 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005985
5986 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5987 blocksize = btrfs_level_size(root, level - 1);
5988
5989 next = btrfs_find_tree_block(root, bytenr, blocksize);
5990 if (!next) {
5991 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00005992 if (!next)
5993 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005994 reada = 1;
5995 }
5996 btrfs_tree_lock(next);
5997 btrfs_set_lock_blocking(next);
5998
Yan, Zheng94fcca92009-10-09 09:25:16 -04005999 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6000 &wc->refs[level - 1],
6001 &wc->flags[level - 1]);
6002 BUG_ON(ret);
6003 BUG_ON(wc->refs[level - 1] == 0);
6004 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006005
Yan, Zheng94fcca92009-10-09 09:25:16 -04006006 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006007 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006008 if (level == 1 &&
6009 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6010 goto skip;
6011
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006012 if (!wc->update_ref ||
6013 generation <= root->root_key.offset)
6014 goto skip;
6015
6016 btrfs_node_key_to_cpu(path->nodes[level], &key,
6017 path->slots[level]);
6018 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6019 if (ret < 0)
6020 goto skip;
6021
6022 wc->stage = UPDATE_BACKREF;
6023 wc->shared_level = level - 1;
6024 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006025 } else {
6026 if (level == 1 &&
6027 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6028 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006029 }
6030
6031 if (!btrfs_buffer_uptodate(next, generation)) {
6032 btrfs_tree_unlock(next);
6033 free_extent_buffer(next);
6034 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006035 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006036 }
6037
6038 if (!next) {
6039 if (reada && level == 1)
6040 reada_walk_down(trans, root, wc, path);
6041 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006042 if (!next)
6043 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006044 btrfs_tree_lock(next);
6045 btrfs_set_lock_blocking(next);
6046 }
6047
6048 level--;
6049 BUG_ON(level != btrfs_header_level(next));
6050 path->nodes[level] = next;
6051 path->slots[level] = 0;
6052 path->locks[level] = 1;
6053 wc->level = level;
6054 if (wc->level == 1)
6055 wc->reada_slot = 0;
6056 return 0;
6057skip:
6058 wc->refs[level - 1] = 0;
6059 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006060 if (wc->stage == DROP_REFERENCE) {
6061 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6062 parent = path->nodes[level]->start;
6063 } else {
6064 BUG_ON(root->root_key.objectid !=
6065 btrfs_header_owner(path->nodes[level]));
6066 parent = 0;
6067 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006068
Yan, Zheng94fcca92009-10-09 09:25:16 -04006069 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6070 root->root_key.objectid, level - 1, 0);
6071 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006072 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006073 btrfs_tree_unlock(next);
6074 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006075 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006076 return 1;
6077}
6078
6079/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006080 * hepler to process tree block while walking up the tree.
6081 *
6082 * when wc->stage == DROP_REFERENCE, this function drops
6083 * reference count on the block.
6084 *
6085 * when wc->stage == UPDATE_BACKREF, this function changes
6086 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6087 * to UPDATE_BACKREF previously while processing the block.
6088 *
6089 * NOTE: return value 1 means we should stop walking up.
6090 */
6091static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6092 struct btrfs_root *root,
6093 struct btrfs_path *path,
6094 struct walk_control *wc)
6095{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006096 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006097 int level = wc->level;
6098 struct extent_buffer *eb = path->nodes[level];
6099 u64 parent = 0;
6100
6101 if (wc->stage == UPDATE_BACKREF) {
6102 BUG_ON(wc->shared_level < level);
6103 if (level < wc->shared_level)
6104 goto out;
6105
Yan Zheng2c47e6052009-06-27 21:07:35 -04006106 ret = find_next_key(path, level + 1, &wc->update_progress);
6107 if (ret > 0)
6108 wc->update_ref = 0;
6109
6110 wc->stage = DROP_REFERENCE;
6111 wc->shared_level = -1;
6112 path->slots[level] = 0;
6113
6114 /*
6115 * check reference count again if the block isn't locked.
6116 * we should start walking down the tree again if reference
6117 * count is one.
6118 */
6119 if (!path->locks[level]) {
6120 BUG_ON(level == 0);
6121 btrfs_tree_lock(eb);
6122 btrfs_set_lock_blocking(eb);
6123 path->locks[level] = 1;
6124
6125 ret = btrfs_lookup_extent_info(trans, root,
6126 eb->start, eb->len,
6127 &wc->refs[level],
6128 &wc->flags[level]);
6129 BUG_ON(ret);
6130 BUG_ON(wc->refs[level] == 0);
6131 if (wc->refs[level] == 1) {
6132 btrfs_tree_unlock(eb);
6133 path->locks[level] = 0;
6134 return 1;
6135 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006136 }
6137 }
6138
6139 /* wc->stage == DROP_REFERENCE */
6140 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6141
6142 if (wc->refs[level] == 1) {
6143 if (level == 0) {
6144 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6145 ret = btrfs_dec_ref(trans, root, eb, 1);
6146 else
6147 ret = btrfs_dec_ref(trans, root, eb, 0);
6148 BUG_ON(ret);
6149 }
6150 /* make block locked assertion in clean_tree_block happy */
6151 if (!path->locks[level] &&
6152 btrfs_header_generation(eb) == trans->transid) {
6153 btrfs_tree_lock(eb);
6154 btrfs_set_lock_blocking(eb);
6155 path->locks[level] = 1;
6156 }
6157 clean_tree_block(trans, root, eb);
6158 }
6159
6160 if (eb == root->node) {
6161 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6162 parent = eb->start;
6163 else
6164 BUG_ON(root->root_key.objectid !=
6165 btrfs_header_owner(eb));
6166 } else {
6167 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6168 parent = path->nodes[level + 1]->start;
6169 else
6170 BUG_ON(root->root_key.objectid !=
6171 btrfs_header_owner(path->nodes[level + 1]));
6172 }
6173
Yan, Zhengf0486c62010-05-16 10:46:25 -04006174 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006175out:
6176 wc->refs[level] = 0;
6177 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006178 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006179}
6180
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006181static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6182 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006183 struct btrfs_path *path,
6184 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006185{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006186 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006187 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006188 int ret;
6189
Yan Zheng2c47e6052009-06-27 21:07:35 -04006190 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006191 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006192 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006193 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006194
Yan Zheng2c47e6052009-06-27 21:07:35 -04006195 if (level == 0)
6196 break;
6197
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006198 if (path->slots[level] >=
6199 btrfs_header_nritems(path->nodes[level]))
6200 break;
6201
Yan, Zheng94fcca92009-10-09 09:25:16 -04006202 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006203 if (ret > 0) {
6204 path->slots[level]++;
6205 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006206 } else if (ret < 0)
6207 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006208 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006209 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006210 return 0;
6211}
6212
Chris Masond3977122009-01-05 21:25:51 -05006213static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006214 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006215 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006216 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006217{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006218 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006219 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006220
Yan Zheng2c47e6052009-06-27 21:07:35 -04006221 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6222 while (level < max_level && path->nodes[level]) {
6223 wc->level = level;
6224 if (path->slots[level] + 1 <
6225 btrfs_header_nritems(path->nodes[level])) {
6226 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006227 return 0;
6228 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006229 ret = walk_up_proc(trans, root, path, wc);
6230 if (ret > 0)
6231 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006232
Yan Zheng2c47e6052009-06-27 21:07:35 -04006233 if (path->locks[level]) {
6234 btrfs_tree_unlock(path->nodes[level]);
6235 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006236 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006237 free_extent_buffer(path->nodes[level]);
6238 path->nodes[level] = NULL;
6239 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006240 }
6241 }
6242 return 1;
6243}
6244
Chris Mason9aca1d52007-03-13 11:09:37 -04006245/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006246 * drop a subvolume tree.
6247 *
6248 * this function traverses the tree freeing any blocks that only
6249 * referenced by the tree.
6250 *
6251 * when a shared tree block is found. this function decreases its
6252 * reference count by one. if update_ref is true, this function
6253 * also make sure backrefs for the shared block and all lower level
6254 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006255 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006256int btrfs_drop_snapshot(struct btrfs_root *root,
6257 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006258{
Chris Mason5caf2a02007-04-02 11:20:42 -04006259 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006260 struct btrfs_trans_handle *trans;
6261 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006262 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006263 struct walk_control *wc;
6264 struct btrfs_key key;
6265 int err = 0;
6266 int ret;
6267 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006268
Chris Mason5caf2a02007-04-02 11:20:42 -04006269 path = btrfs_alloc_path();
6270 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006271
Yan Zheng2c47e6052009-06-27 21:07:35 -04006272 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6273 BUG_ON(!wc);
6274
Yan, Zhenga22285a2010-05-16 10:48:46 -04006275 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006276 BUG_ON(IS_ERR(trans));
6277
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006278 if (block_rsv)
6279 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006280
Chris Mason9f3a7422007-08-07 15:52:19 -04006281 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006282 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006283 path->nodes[level] = btrfs_lock_root_node(root);
6284 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006285 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006286 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006287 memset(&wc->update_progress, 0,
6288 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006289 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006290 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006291 memcpy(&wc->update_progress, &key,
6292 sizeof(wc->update_progress));
6293
Chris Mason6702ed42007-08-07 16:15:09 -04006294 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006295 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006296 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006297 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6298 path->lowest_level = 0;
6299 if (ret < 0) {
6300 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006301 goto out;
6302 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006303 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006304
Chris Mason7d9eb122008-07-08 14:19:17 -04006305 /*
6306 * unlock our path, this is safe because only this
6307 * function is allowed to delete this snapshot
6308 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006309 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006310
Yan Zheng2c47e6052009-06-27 21:07:35 -04006311 level = btrfs_header_level(root->node);
6312 while (1) {
6313 btrfs_tree_lock(path->nodes[level]);
6314 btrfs_set_lock_blocking(path->nodes[level]);
6315
6316 ret = btrfs_lookup_extent_info(trans, root,
6317 path->nodes[level]->start,
6318 path->nodes[level]->len,
6319 &wc->refs[level],
6320 &wc->flags[level]);
6321 BUG_ON(ret);
6322 BUG_ON(wc->refs[level] == 0);
6323
6324 if (level == root_item->drop_level)
6325 break;
6326
6327 btrfs_tree_unlock(path->nodes[level]);
6328 WARN_ON(wc->refs[level] != 1);
6329 level--;
6330 }
6331 }
6332
6333 wc->level = level;
6334 wc->shared_level = -1;
6335 wc->stage = DROP_REFERENCE;
6336 wc->update_ref = update_ref;
6337 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006338 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006339
6340 while (1) {
6341 ret = walk_down_tree(trans, root, path, wc);
6342 if (ret < 0) {
6343 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006344 break;
6345 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006346
6347 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6348 if (ret < 0) {
6349 err = ret;
6350 break;
6351 }
6352
6353 if (ret > 0) {
6354 BUG_ON(wc->stage != DROP_REFERENCE);
6355 break;
6356 }
6357
6358 if (wc->stage == DROP_REFERENCE) {
6359 level = wc->level;
6360 btrfs_node_key(path->nodes[level],
6361 &root_item->drop_progress,
6362 path->slots[level]);
6363 root_item->drop_level = level;
6364 }
6365
6366 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006367 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006368 ret = btrfs_update_root(trans, tree_root,
6369 &root->root_key,
6370 root_item);
6371 BUG_ON(ret);
6372
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006373 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006374 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006375 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006376 if (block_rsv)
6377 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006378 }
Chris Mason20524f02007-03-10 06:35:47 -05006379 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006380 btrfs_release_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006381 BUG_ON(err);
6382
6383 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6384 BUG_ON(ret);
6385
Yan, Zheng76dda932009-09-21 16:00:26 -04006386 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6387 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6388 NULL, NULL);
6389 BUG_ON(ret < 0);
6390 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006391 /* if we fail to delete the orphan item this time
6392 * around, it'll get picked up the next time.
6393 *
6394 * The most common failure here is just -ENOENT.
6395 */
6396 btrfs_del_orphan_item(trans, tree_root,
6397 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006398 }
6399 }
6400
6401 if (root->in_radix) {
6402 btrfs_free_fs_root(tree_root->fs_info, root);
6403 } else {
6404 free_extent_buffer(root->node);
6405 free_extent_buffer(root->commit_root);
6406 kfree(root);
6407 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006408out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006409 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006410 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006411 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006412 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006413}
Chris Mason9078a3e2007-04-26 16:46:15 -04006414
Yan Zheng2c47e6052009-06-27 21:07:35 -04006415/*
6416 * drop subtree rooted at tree block 'node'.
6417 *
6418 * NOTE: this function will unlock and release tree block 'node'
6419 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006420int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6421 struct btrfs_root *root,
6422 struct extent_buffer *node,
6423 struct extent_buffer *parent)
6424{
6425 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006426 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006427 int level;
6428 int parent_level;
6429 int ret = 0;
6430 int wret;
6431
Yan Zheng2c47e6052009-06-27 21:07:35 -04006432 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6433
Yan Zhengf82d02d2008-10-29 14:49:05 -04006434 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006435 if (!path)
6436 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006437
Yan Zheng2c47e6052009-06-27 21:07:35 -04006438 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006439 if (!wc) {
6440 btrfs_free_path(path);
6441 return -ENOMEM;
6442 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006443
Chris Masonb9447ef2009-03-09 11:45:38 -04006444 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006445 parent_level = btrfs_header_level(parent);
6446 extent_buffer_get(parent);
6447 path->nodes[parent_level] = parent;
6448 path->slots[parent_level] = btrfs_header_nritems(parent);
6449
Chris Masonb9447ef2009-03-09 11:45:38 -04006450 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006451 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006452 path->nodes[level] = node;
6453 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006454 path->locks[level] = 1;
6455
6456 wc->refs[parent_level] = 1;
6457 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6458 wc->level = level;
6459 wc->shared_level = -1;
6460 wc->stage = DROP_REFERENCE;
6461 wc->update_ref = 0;
6462 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006463 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006464
6465 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006466 wret = walk_down_tree(trans, root, path, wc);
6467 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006468 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006469 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006470 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006471
Yan Zheng2c47e6052009-06-27 21:07:35 -04006472 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006473 if (wret < 0)
6474 ret = wret;
6475 if (wret != 0)
6476 break;
6477 }
6478
Yan Zheng2c47e6052009-06-27 21:07:35 -04006479 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006480 btrfs_free_path(path);
6481 return ret;
6482}
6483
Chris Masonec44a352008-04-28 15:29:52 -04006484static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6485{
6486 u64 num_devices;
6487 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6488 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6489
Chris Masoncd02dca2010-12-13 14:56:23 -05006490 /*
6491 * we add in the count of missing devices because we want
6492 * to make sure that any RAID levels on a degraded FS
6493 * continue to be honored.
6494 */
6495 num_devices = root->fs_info->fs_devices->rw_devices +
6496 root->fs_info->fs_devices->missing_devices;
6497
Chris Masonec44a352008-04-28 15:29:52 -04006498 if (num_devices == 1) {
6499 stripped |= BTRFS_BLOCK_GROUP_DUP;
6500 stripped = flags & ~stripped;
6501
6502 /* turn raid0 into single device chunks */
6503 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6504 return stripped;
6505
6506 /* turn mirroring into duplication */
6507 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6508 BTRFS_BLOCK_GROUP_RAID10))
6509 return stripped | BTRFS_BLOCK_GROUP_DUP;
6510 return flags;
6511 } else {
6512 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006513 if (flags & stripped)
6514 return flags;
6515
6516 stripped |= BTRFS_BLOCK_GROUP_DUP;
6517 stripped = flags & ~stripped;
6518
6519 /* switch duplicated blocks with raid1 */
6520 if (flags & BTRFS_BLOCK_GROUP_DUP)
6521 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6522
6523 /* turn single device chunks into raid0 */
6524 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6525 }
6526 return flags;
6527}
6528
Miao Xie199c36e2011-07-15 10:34:36 +00006529static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04006530{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006531 struct btrfs_space_info *sinfo = cache->space_info;
6532 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00006533 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006534 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04006535
Yan, Zhengf0486c62010-05-16 10:46:25 -04006536 if (cache->ro)
6537 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04006538
Miao Xie199c36e2011-07-15 10:34:36 +00006539 /*
6540 * We need some metadata space and system metadata space for
6541 * allocating chunks in some corner cases until we force to set
6542 * it to be readonly.
6543 */
6544 if ((sinfo->flags &
6545 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
6546 !force)
6547 min_allocable_bytes = 1 * 1024 * 1024;
6548 else
6549 min_allocable_bytes = 0;
6550
Yan, Zhengf0486c62010-05-16 10:46:25 -04006551 spin_lock(&sinfo->lock);
6552 spin_lock(&cache->lock);
6553 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6554 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04006555
Yan, Zhengf0486c62010-05-16 10:46:25 -04006556 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
6557 sinfo->bytes_may_use + sinfo->bytes_readonly +
Miao Xie199c36e2011-07-15 10:34:36 +00006558 cache->reserved_pinned + num_bytes + min_allocable_bytes <=
6559 sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04006560 sinfo->bytes_readonly += num_bytes;
6561 sinfo->bytes_reserved += cache->reserved_pinned;
6562 cache->reserved_pinned = 0;
6563 cache->ro = 1;
6564 ret = 0;
6565 }
Chris Mason65e53412010-12-24 06:41:52 -05006566
Yan, Zhengf0486c62010-05-16 10:46:25 -04006567 spin_unlock(&cache->lock);
6568 spin_unlock(&sinfo->lock);
6569 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04006570}
6571
Yan, Zhengf0486c62010-05-16 10:46:25 -04006572int btrfs_set_block_group_ro(struct btrfs_root *root,
6573 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006574
6575{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006576 struct btrfs_trans_handle *trans;
6577 u64 alloc_flags;
6578 int ret;
6579
6580 BUG_ON(cache->ro);
6581
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006582 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006583 BUG_ON(IS_ERR(trans));
6584
6585 alloc_flags = update_block_group_flags(root, cache->flags);
6586 if (alloc_flags != cache->flags)
Chris Mason0e4f8f82011-04-15 16:05:44 -04006587 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6588 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006589
Miao Xie199c36e2011-07-15 10:34:36 +00006590 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006591 if (!ret)
6592 goto out;
6593 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006594 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6595 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006596 if (ret < 0)
6597 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00006598 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006599out:
6600 btrfs_end_transaction(trans, root);
6601 return ret;
6602}
6603
Chris Masonc87f08c2011-02-16 13:57:04 -05006604int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
6605 struct btrfs_root *root, u64 type)
6606{
6607 u64 alloc_flags = get_alloc_profile(root, type);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006608 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6609 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05006610}
6611
Miao Xie6d07bce2011-01-05 10:07:31 +00006612/*
6613 * helper to account the unused space of all the readonly block group in the
6614 * list. takes mirrors into account.
6615 */
6616static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
6617{
6618 struct btrfs_block_group_cache *block_group;
6619 u64 free_bytes = 0;
6620 int factor;
6621
6622 list_for_each_entry(block_group, groups_list, list) {
6623 spin_lock(&block_group->lock);
6624
6625 if (!block_group->ro) {
6626 spin_unlock(&block_group->lock);
6627 continue;
6628 }
6629
6630 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
6631 BTRFS_BLOCK_GROUP_RAID10 |
6632 BTRFS_BLOCK_GROUP_DUP))
6633 factor = 2;
6634 else
6635 factor = 1;
6636
6637 free_bytes += (block_group->key.offset -
6638 btrfs_block_group_used(&block_group->item)) *
6639 factor;
6640
6641 spin_unlock(&block_group->lock);
6642 }
6643
6644 return free_bytes;
6645}
6646
6647/*
6648 * helper to account the unused space of all the readonly block group in the
6649 * space_info. takes mirrors into account.
6650 */
6651u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
6652{
6653 int i;
6654 u64 free_bytes = 0;
6655
6656 spin_lock(&sinfo->lock);
6657
6658 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
6659 if (!list_empty(&sinfo->block_groups[i]))
6660 free_bytes += __btrfs_get_ro_block_group_free_space(
6661 &sinfo->block_groups[i]);
6662
6663 spin_unlock(&sinfo->lock);
6664
6665 return free_bytes;
6666}
6667
Yan, Zhengf0486c62010-05-16 10:46:25 -04006668int btrfs_set_block_group_rw(struct btrfs_root *root,
6669 struct btrfs_block_group_cache *cache)
6670{
6671 struct btrfs_space_info *sinfo = cache->space_info;
6672 u64 num_bytes;
6673
6674 BUG_ON(!cache->ro);
6675
6676 spin_lock(&sinfo->lock);
6677 spin_lock(&cache->lock);
6678 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6679 cache->bytes_super - btrfs_block_group_used(&cache->item);
6680 sinfo->bytes_readonly -= num_bytes;
6681 cache->ro = 0;
6682 spin_unlock(&cache->lock);
6683 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006684 return 0;
6685}
6686
Josef Bacikba1bf482009-09-11 16:11:19 -04006687/*
6688 * checks to see if its even possible to relocate this block group.
6689 *
6690 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6691 * ok to go ahead and try.
6692 */
6693int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04006694{
Zheng Yan1a40e232008-09-26 10:09:34 -04006695 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04006696 struct btrfs_space_info *space_info;
6697 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6698 struct btrfs_device *device;
6699 int full = 0;
6700 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006701
Josef Bacikba1bf482009-09-11 16:11:19 -04006702 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04006703
Josef Bacikba1bf482009-09-11 16:11:19 -04006704 /* odd, couldn't find the block group, leave it alone */
6705 if (!block_group)
6706 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006707
Josef Bacikba1bf482009-09-11 16:11:19 -04006708 /* no bytes used, we're good */
6709 if (!btrfs_block_group_used(&block_group->item))
6710 goto out;
Chris Mason323da792008-05-09 11:46:48 -04006711
Josef Bacikba1bf482009-09-11 16:11:19 -04006712 space_info = block_group->space_info;
6713 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04006714
Josef Bacikba1bf482009-09-11 16:11:19 -04006715 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04006716
Josef Bacikba1bf482009-09-11 16:11:19 -04006717 /*
6718 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04006719 * relocate it unless we're able to allocate a new chunk below.
6720 *
6721 * Otherwise, we need to make sure we have room in the space to handle
6722 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04006723 */
Chris Mason7ce618d2009-09-22 14:48:44 -04006724 if ((space_info->total_bytes != block_group->key.offset) &&
6725 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04006726 space_info->bytes_pinned + space_info->bytes_readonly +
6727 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04006728 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04006729 spin_unlock(&space_info->lock);
6730 goto out;
6731 }
6732 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006733
Josef Bacikba1bf482009-09-11 16:11:19 -04006734 /*
6735 * ok we don't have enough space, but maybe we have free space on our
6736 * devices to allocate new chunks for relocation, so loop through our
6737 * alloc devices and guess if we have enough space. However, if we
6738 * were marked as full, then we know there aren't enough chunks, and we
6739 * can just return.
6740 */
6741 ret = -1;
6742 if (full)
6743 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05006744
Josef Bacikba1bf482009-09-11 16:11:19 -04006745 mutex_lock(&root->fs_info->chunk_mutex);
6746 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
6747 u64 min_free = btrfs_block_group_used(&block_group->item);
Miao Xie7bfc8372011-01-05 10:07:26 +00006748 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04006749
Josef Bacikba1bf482009-09-11 16:11:19 -04006750 /*
6751 * check to make sure we can actually find a chunk with enough
6752 * space to fit our block group in.
6753 */
6754 if (device->total_bytes > device->bytes_used + min_free) {
6755 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00006756 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04006757 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05006758 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04006759 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05006760 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006761 }
Josef Bacikba1bf482009-09-11 16:11:19 -04006762 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05006763out:
Josef Bacikba1bf482009-09-11 16:11:19 -04006764 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006765 return ret;
6766}
6767
Christoph Hellwigb2950862008-12-02 09:54:17 -05006768static int find_first_block_group(struct btrfs_root *root,
6769 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04006770{
Chris Mason925baed2008-06-25 16:01:30 -04006771 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006772 struct btrfs_key found_key;
6773 struct extent_buffer *leaf;
6774 int slot;
6775
6776 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6777 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006778 goto out;
6779
Chris Masond3977122009-01-05 21:25:51 -05006780 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006781 slot = path->slots[0];
6782 leaf = path->nodes[0];
6783 if (slot >= btrfs_header_nritems(leaf)) {
6784 ret = btrfs_next_leaf(root, path);
6785 if (ret == 0)
6786 continue;
6787 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006788 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04006789 break;
6790 }
6791 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6792
6793 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04006794 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6795 ret = 0;
6796 goto out;
6797 }
Chris Mason0b86a832008-03-24 15:01:56 -04006798 path->slots[0]++;
6799 }
Chris Mason925baed2008-06-25 16:01:30 -04006800out:
Chris Mason0b86a832008-03-24 15:01:56 -04006801 return ret;
6802}
6803
Josef Bacik0af3d002010-06-21 14:48:16 -04006804void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
6805{
6806 struct btrfs_block_group_cache *block_group;
6807 u64 last = 0;
6808
6809 while (1) {
6810 struct inode *inode;
6811
6812 block_group = btrfs_lookup_first_block_group(info, last);
6813 while (block_group) {
6814 spin_lock(&block_group->lock);
6815 if (block_group->iref)
6816 break;
6817 spin_unlock(&block_group->lock);
6818 block_group = next_block_group(info->tree_root,
6819 block_group);
6820 }
6821 if (!block_group) {
6822 if (last == 0)
6823 break;
6824 last = 0;
6825 continue;
6826 }
6827
6828 inode = block_group->inode;
6829 block_group->iref = 0;
6830 block_group->inode = NULL;
6831 spin_unlock(&block_group->lock);
6832 iput(inode);
6833 last = block_group->key.objectid + block_group->key.offset;
6834 btrfs_put_block_group(block_group);
6835 }
6836}
6837
Zheng Yan1a40e232008-09-26 10:09:34 -04006838int btrfs_free_block_groups(struct btrfs_fs_info *info)
6839{
6840 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04006841 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04006842 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04006843 struct rb_node *n;
6844
Yan Zheng11833d62009-09-11 16:11:19 -04006845 down_write(&info->extent_commit_sem);
6846 while (!list_empty(&info->caching_block_groups)) {
6847 caching_ctl = list_entry(info->caching_block_groups.next,
6848 struct btrfs_caching_control, list);
6849 list_del(&caching_ctl->list);
6850 put_caching_control(caching_ctl);
6851 }
6852 up_write(&info->extent_commit_sem);
6853
Zheng Yan1a40e232008-09-26 10:09:34 -04006854 spin_lock(&info->block_group_cache_lock);
6855 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6856 block_group = rb_entry(n, struct btrfs_block_group_cache,
6857 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04006858 rb_erase(&block_group->cache_node,
6859 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04006860 spin_unlock(&info->block_group_cache_lock);
6861
Josef Bacik80eb2342008-10-29 14:49:05 -04006862 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006863 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006864 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006865
Josef Bacik817d52f2009-07-13 21:29:25 -04006866 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04006867 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04006868
Josef Bacik3c148742011-02-02 15:53:47 +00006869 /*
6870 * We haven't cached this block group, which means we could
6871 * possibly have excluded extents on this block group.
6872 */
6873 if (block_group->cached == BTRFS_CACHE_NO)
6874 free_excluded_extents(info->extent_root, block_group);
6875
Josef Bacik817d52f2009-07-13 21:29:25 -04006876 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00006877 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04006878
6879 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006880 }
6881 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04006882
6883 /* now that all the block groups are freed, go through and
6884 * free all the space_info structs. This is only called during
6885 * the final stages of unmount, and so we know nobody is
6886 * using them. We call synchronize_rcu() once before we start,
6887 * just to be on the safe side.
6888 */
6889 synchronize_rcu();
6890
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006891 release_global_block_rsv(info);
6892
Chris Mason4184ea72009-03-10 12:39:20 -04006893 while(!list_empty(&info->space_info)) {
6894 space_info = list_entry(info->space_info.next,
6895 struct btrfs_space_info,
6896 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006897 if (space_info->bytes_pinned > 0 ||
6898 space_info->bytes_reserved > 0) {
6899 WARN_ON(1);
6900 dump_space_info(space_info, 0, 0);
6901 }
Chris Mason4184ea72009-03-10 12:39:20 -04006902 list_del(&space_info->list);
6903 kfree(space_info);
6904 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006905 return 0;
6906}
6907
Yan, Zhengb742bb82010-05-16 10:46:24 -04006908static void __link_block_group(struct btrfs_space_info *space_info,
6909 struct btrfs_block_group_cache *cache)
6910{
6911 int index = get_block_group_index(cache);
6912
6913 down_write(&space_info->groups_sem);
6914 list_add_tail(&cache->list, &space_info->block_groups[index]);
6915 up_write(&space_info->groups_sem);
6916}
6917
Chris Mason9078a3e2007-04-26 16:46:15 -04006918int btrfs_read_block_groups(struct btrfs_root *root)
6919{
6920 struct btrfs_path *path;
6921 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04006922 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04006923 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04006924 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04006925 struct btrfs_key key;
6926 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04006927 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04006928 int need_clear = 0;
6929 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04006930
Chris Masonbe744172007-05-06 10:15:01 -04006931 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04006932 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006933 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04006934 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04006935 path = btrfs_alloc_path();
6936 if (!path)
6937 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04006938 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04006939
Josef Bacik0af3d002010-06-21 14:48:16 -04006940 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
6941 if (cache_gen != 0 &&
6942 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
6943 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04006944 if (btrfs_test_opt(root, CLEAR_CACHE))
6945 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04006946 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
6947 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04006948
Chris Masond3977122009-01-05 21:25:51 -05006949 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006950 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006951 if (ret > 0)
6952 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006953 if (ret != 0)
6954 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04006955 leaf = path->nodes[0];
6956 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04006957 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04006958 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04006959 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006960 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04006961 }
Li Zefan34d52cb2011-03-29 13:46:06 +08006962 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
6963 GFP_NOFS);
6964 if (!cache->free_space_ctl) {
6965 kfree(cache);
6966 ret = -ENOMEM;
6967 goto error;
6968 }
Chris Mason3e1ad542007-05-07 20:03:49 -04006969
Yan Zhengd2fb3432008-12-11 16:30:39 -05006970 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006971 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04006972 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006973 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006974 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04006975
Josef Bacik0af3d002010-06-21 14:48:16 -04006976 if (need_clear)
6977 cache->disk_cache_state = BTRFS_DC_CLEAR;
6978
Chris Mason5f39d392007-10-15 16:14:19 -04006979 read_extent_buffer(leaf, &cache->item,
6980 btrfs_item_ptr_offset(leaf, path->slots[0]),
6981 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04006982 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04006983
Chris Mason9078a3e2007-04-26 16:46:15 -04006984 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02006985 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006986 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04006987 cache->sectorsize = root->sectorsize;
6988
Li Zefan34d52cb2011-03-29 13:46:06 +08006989 btrfs_init_free_space_ctl(cache);
6990
Josef Bacik817d52f2009-07-13 21:29:25 -04006991 /*
Josef Bacik3c148742011-02-02 15:53:47 +00006992 * We need to exclude the super stripes now so that the space
6993 * info has super bytes accounted for, otherwise we'll think
6994 * we have more space than we actually do.
6995 */
6996 exclude_super_stripes(root, cache);
6997
6998 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04006999 * check for two cases, either we are full, and therefore
7000 * don't need to bother with the caching work since we won't
7001 * find any space, or we are empty, and we can just add all
7002 * the space in and be done with it. This saves us _alot_ of
7003 * time, particularly in the full case.
7004 */
7005 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007006 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007007 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007008 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007009 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007010 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007011 cache->cached = BTRFS_CACHE_FINISHED;
7012 add_new_free_space(cache, root->fs_info,
7013 found_key.objectid,
7014 found_key.objectid +
7015 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007016 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007017 }
Chris Mason96b51792007-10-15 16:15:19 -04007018
Chris Mason6324fbf2008-03-24 15:01:59 -04007019 ret = update_space_info(info, cache->flags, found_key.offset,
7020 btrfs_block_group_used(&cache->item),
7021 &space_info);
7022 BUG_ON(ret);
7023 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007024 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007025 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007026 spin_unlock(&cache->space_info->lock);
7027
Yan, Zhengb742bb82010-05-16 10:46:24 -04007028 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007029
Josef Bacik0f9dd462008-09-23 13:14:11 -04007030 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7031 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007032
7033 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007034 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00007035 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04007036 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007037
7038 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7039 if (!(get_alloc_profile(root, space_info->flags) &
7040 (BTRFS_BLOCK_GROUP_RAID10 |
7041 BTRFS_BLOCK_GROUP_RAID1 |
7042 BTRFS_BLOCK_GROUP_DUP)))
7043 continue;
7044 /*
7045 * avoid allocating from un-mirrored block group if there are
7046 * mirrored block groups.
7047 */
7048 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007049 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007050 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007051 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007052 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007053
7054 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007055 ret = 0;
7056error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007057 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007058 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007059}
Chris Mason6324fbf2008-03-24 15:01:59 -04007060
7061int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7062 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007063 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007064 u64 size)
7065{
7066 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007067 struct btrfs_root *extent_root;
7068 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007069
7070 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007071
Chris Mason12fcfd22009-03-24 10:24:20 -04007072 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007073
Chris Mason8f18cf12008-04-25 16:53:30 -04007074 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007075 if (!cache)
7076 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08007077 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7078 GFP_NOFS);
7079 if (!cache->free_space_ctl) {
7080 kfree(cache);
7081 return -ENOMEM;
7082 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007083
Chris Masone17cade2008-04-15 15:41:47 -04007084 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007085 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007086 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007087 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007088 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04007089
Yan Zhengd2fb3432008-12-11 16:30:39 -05007090 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007091 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007092 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007093 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007094
Li Zefan34d52cb2011-03-29 13:46:06 +08007095 btrfs_init_free_space_ctl(cache);
7096
Chris Mason6324fbf2008-03-24 15:01:59 -04007097 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007098 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7099 cache->flags = type;
7100 btrfs_set_block_group_flags(&cache->item, type);
7101
Yan Zheng11833d62009-09-11 16:11:19 -04007102 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007103 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007104 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007105
Josef Bacik817d52f2009-07-13 21:29:25 -04007106 add_new_free_space(cache, root->fs_info, chunk_offset,
7107 chunk_offset + size);
7108
Yan Zheng11833d62009-09-11 16:11:19 -04007109 free_excluded_extents(root, cache);
7110
Chris Mason6324fbf2008-03-24 15:01:59 -04007111 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7112 &cache->space_info);
7113 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007114
7115 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007116 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007117 spin_unlock(&cache->space_info->lock);
7118
Yan, Zhengb742bb82010-05-16 10:46:24 -04007119 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007120
Josef Bacik0f9dd462008-09-23 13:14:11 -04007121 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7122 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007123
Chris Mason6324fbf2008-03-24 15:01:59 -04007124 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7125 sizeof(cache->item));
7126 BUG_ON(ret);
7127
Chris Masond18a2c42008-04-04 15:40:00 -04007128 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007129
Chris Mason6324fbf2008-03-24 15:01:59 -04007130 return 0;
7131}
Zheng Yan1a40e232008-09-26 10:09:34 -04007132
7133int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7134 struct btrfs_root *root, u64 group_start)
7135{
7136 struct btrfs_path *path;
7137 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007138 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04007139 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04007140 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04007141 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04007142 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04007143 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04007144
Zheng Yan1a40e232008-09-26 10:09:34 -04007145 root = root->fs_info->extent_root;
7146
7147 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7148 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007149 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007150
liubo9f7c43c2011-03-07 02:13:33 +00007151 /*
7152 * Free the reserved super bytes from this block group before
7153 * remove it.
7154 */
7155 free_excluded_extents(root, block_group);
7156
Zheng Yan1a40e232008-09-26 10:09:34 -04007157 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04007158 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7159 BTRFS_BLOCK_GROUP_RAID1 |
7160 BTRFS_BLOCK_GROUP_RAID10))
7161 factor = 2;
7162 else
7163 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007164
Chris Mason44fb5512009-06-04 15:34:51 -04007165 /* make sure this block group isn't part of an allocation cluster */
7166 cluster = &root->fs_info->data_alloc_cluster;
7167 spin_lock(&cluster->refill_lock);
7168 btrfs_return_cluster_to_free_space(block_group, cluster);
7169 spin_unlock(&cluster->refill_lock);
7170
7171 /*
7172 * make sure this block group isn't part of a metadata
7173 * allocation cluster
7174 */
7175 cluster = &root->fs_info->meta_alloc_cluster;
7176 spin_lock(&cluster->refill_lock);
7177 btrfs_return_cluster_to_free_space(block_group, cluster);
7178 spin_unlock(&cluster->refill_lock);
7179
Zheng Yan1a40e232008-09-26 10:09:34 -04007180 path = btrfs_alloc_path();
7181 BUG_ON(!path);
7182
Josef Bacik0af3d002010-06-21 14:48:16 -04007183 inode = lookup_free_space_inode(root, block_group, path);
7184 if (!IS_ERR(inode)) {
7185 btrfs_orphan_add(trans, inode);
7186 clear_nlink(inode);
7187 /* One for the block groups ref */
7188 spin_lock(&block_group->lock);
7189 if (block_group->iref) {
7190 block_group->iref = 0;
7191 block_group->inode = NULL;
7192 spin_unlock(&block_group->lock);
7193 iput(inode);
7194 } else {
7195 spin_unlock(&block_group->lock);
7196 }
7197 /* One for our lookup ref */
7198 iput(inode);
7199 }
7200
7201 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
7202 key.offset = block_group->key.objectid;
7203 key.type = 0;
7204
7205 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
7206 if (ret < 0)
7207 goto out;
7208 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02007209 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007210 if (ret == 0) {
7211 ret = btrfs_del_item(trans, tree_root, path);
7212 if (ret)
7213 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02007214 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007215 }
7216
Yan Zheng3dfdb932009-01-21 10:49:16 -05007217 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007218 rb_erase(&block_group->cache_node,
7219 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007220 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007221
Josef Bacik80eb2342008-10-29 14:49:05 -04007222 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007223 /*
7224 * we must use list_del_init so people can check to see if they
7225 * are still on the list after taking the semaphore
7226 */
7227 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007228 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007229
Josef Bacik817d52f2009-07-13 21:29:25 -04007230 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007231 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007232
7233 btrfs_remove_free_space_cache(block_group);
7234
Yan Zhengc146afa2008-11-12 14:34:12 -05007235 spin_lock(&block_group->space_info->lock);
7236 block_group->space_info->total_bytes -= block_group->key.offset;
7237 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04007238 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05007239 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007240
Josef Bacik0af3d002010-06-21 14:48:16 -04007241 memcpy(&key, &block_group->key, sizeof(key));
7242
Chris Mason283bb192009-07-24 16:30:55 -04007243 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007244
Chris Masonfa9c0d792009-04-03 09:47:43 -04007245 btrfs_put_block_group(block_group);
7246 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007247
7248 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7249 if (ret > 0)
7250 ret = -EIO;
7251 if (ret < 0)
7252 goto out;
7253
7254 ret = btrfs_del_item(trans, root, path);
7255out:
7256 btrfs_free_path(path);
7257 return ret;
7258}
liuboacce9522011-01-06 19:30:25 +08007259
liuboc59021f2011-03-07 02:13:14 +00007260int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
7261{
7262 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00007263 struct btrfs_super_block *disk_super;
7264 u64 features;
7265 u64 flags;
7266 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00007267 int ret;
7268
liubo1aba86d2011-04-08 08:44:37 +00007269 disk_super = &fs_info->super_copy;
7270 if (!btrfs_super_root(disk_super))
7271 return 1;
liuboc59021f2011-03-07 02:13:14 +00007272
liubo1aba86d2011-04-08 08:44:37 +00007273 features = btrfs_super_incompat_flags(disk_super);
7274 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
7275 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00007276
liubo1aba86d2011-04-08 08:44:37 +00007277 flags = BTRFS_BLOCK_GROUP_SYSTEM;
7278 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00007279 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00007280 goto out;
liuboc59021f2011-03-07 02:13:14 +00007281
liubo1aba86d2011-04-08 08:44:37 +00007282 if (mixed) {
7283 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
7284 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7285 } else {
7286 flags = BTRFS_BLOCK_GROUP_METADATA;
7287 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7288 if (ret)
7289 goto out;
7290
7291 flags = BTRFS_BLOCK_GROUP_DATA;
7292 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7293 }
7294out:
liuboc59021f2011-03-07 02:13:14 +00007295 return ret;
7296}
7297
liuboacce9522011-01-06 19:30:25 +08007298int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
7299{
7300 return unpin_extent_range(root, start, end);
7301}
7302
7303int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00007304 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08007305{
Li Dongyang5378e602011-03-24 10:24:27 +00007306 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08007307}
Li Dongyangf7039b12011-03-24 10:24:28 +00007308
7309int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
7310{
7311 struct btrfs_fs_info *fs_info = root->fs_info;
7312 struct btrfs_block_group_cache *cache = NULL;
7313 u64 group_trimmed;
7314 u64 start;
7315 u64 end;
7316 u64 trimmed = 0;
7317 int ret = 0;
7318
7319 cache = btrfs_lookup_block_group(fs_info, range->start);
7320
7321 while (cache) {
7322 if (cache->key.objectid >= (range->start + range->len)) {
7323 btrfs_put_block_group(cache);
7324 break;
7325 }
7326
7327 start = max(range->start, cache->key.objectid);
7328 end = min(range->start + range->len,
7329 cache->key.objectid + cache->key.offset);
7330
7331 if (end - start >= range->minlen) {
7332 if (!block_group_cache_done(cache)) {
7333 ret = cache_block_group(cache, NULL, root, 0);
7334 if (!ret)
7335 wait_block_group_cache_done(cache);
7336 }
7337 ret = btrfs_trim_block_group(cache,
7338 &group_trimmed,
7339 start,
7340 end,
7341 range->minlen);
7342
7343 trimmed += group_trimmed;
7344 if (ret) {
7345 btrfs_put_block_group(cache);
7346 break;
7347 }
7348 }
7349
7350 cache = next_block_group(fs_info->tree_root, cache);
7351 }
7352
7353 range->len = trimmed;
7354 return ret;
7355}