blob: 6cfcc9060c837a3f718cdc371b89ea547d2d531b [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 Bacikfb25e912011-07-26 17:00:46 -040055/*
56 * Control how reservations are dealt with.
57 *
58 * RESERVE_FREE - freeing a reservation.
59 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
60 * ENOSPC accounting
61 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
62 * bytes_may_use as the ENOSPC accounting is done elsewhere
63 */
64enum {
65 RESERVE_FREE = 0,
66 RESERVE_ALLOC = 1,
67 RESERVE_ALLOC_NO_ACCOUNT = 2,
68};
69
Josef Bacikf3465ca2008-11-12 14:19:50 -050070static int update_block_group(struct btrfs_trans_handle *trans,
71 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040072 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040073static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
74 struct btrfs_root *root,
75 u64 bytenr, u64 num_bytes, u64 parent,
76 u64 root_objectid, u64 owner_objectid,
77 u64 owner_offset, int refs_to_drop,
78 struct btrfs_delayed_extent_op *extra_op);
79static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
80 struct extent_buffer *leaf,
81 struct btrfs_extent_item *ei);
82static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
83 struct btrfs_root *root,
84 u64 parent, u64 root_objectid,
85 u64 flags, u64 owner, u64 offset,
86 struct btrfs_key *ins, int ref_mod);
87static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
88 struct btrfs_root *root,
89 u64 parent, u64 root_objectid,
90 u64 flags, struct btrfs_disk_key *key,
91 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050092static int do_chunk_alloc(struct btrfs_trans_handle *trans,
93 struct btrfs_root *extent_root, u64 alloc_bytes,
94 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040095static int find_next_key(struct btrfs_path *path, int level,
96 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040097static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
98 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -040099static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
100 u64 num_bytes, int reserve);
Josef Bacik6a632092009-02-20 11:00:09 -0500101
Josef Bacik817d52f2009-07-13 21:29:25 -0400102static noinline int
103block_group_cache_done(struct btrfs_block_group_cache *cache)
104{
105 smp_mb();
106 return cache->cached == BTRFS_CACHE_FINISHED;
107}
108
Josef Bacik0f9dd462008-09-23 13:14:11 -0400109static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
110{
111 return (cache->flags & bits) == bits;
112}
113
David Sterba62a45b62011-04-20 15:52:26 +0200114static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000115{
116 atomic_inc(&cache->count);
117}
118
119void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
120{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400121 if (atomic_dec_and_test(&cache->count)) {
122 WARN_ON(cache->pinned > 0);
123 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800124 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000125 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400126 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000127}
128
Josef Bacik0f9dd462008-09-23 13:14:11 -0400129/*
130 * this adds the block group to the fs_info rb tree for the block group
131 * cache
132 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500133static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400134 struct btrfs_block_group_cache *block_group)
135{
136 struct rb_node **p;
137 struct rb_node *parent = NULL;
138 struct btrfs_block_group_cache *cache;
139
140 spin_lock(&info->block_group_cache_lock);
141 p = &info->block_group_cache_tree.rb_node;
142
143 while (*p) {
144 parent = *p;
145 cache = rb_entry(parent, struct btrfs_block_group_cache,
146 cache_node);
147 if (block_group->key.objectid < cache->key.objectid) {
148 p = &(*p)->rb_left;
149 } else if (block_group->key.objectid > cache->key.objectid) {
150 p = &(*p)->rb_right;
151 } else {
152 spin_unlock(&info->block_group_cache_lock);
153 return -EEXIST;
154 }
155 }
156
157 rb_link_node(&block_group->cache_node, parent, p);
158 rb_insert_color(&block_group->cache_node,
159 &info->block_group_cache_tree);
160 spin_unlock(&info->block_group_cache_lock);
161
162 return 0;
163}
164
165/*
166 * This will return the block group at or after bytenr if contains is 0, else
167 * it will return the block group that contains the bytenr
168 */
169static struct btrfs_block_group_cache *
170block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
171 int contains)
172{
173 struct btrfs_block_group_cache *cache, *ret = NULL;
174 struct rb_node *n;
175 u64 end, start;
176
177 spin_lock(&info->block_group_cache_lock);
178 n = info->block_group_cache_tree.rb_node;
179
180 while (n) {
181 cache = rb_entry(n, struct btrfs_block_group_cache,
182 cache_node);
183 end = cache->key.objectid + cache->key.offset - 1;
184 start = cache->key.objectid;
185
186 if (bytenr < start) {
187 if (!contains && (!ret || start < ret->key.objectid))
188 ret = cache;
189 n = n->rb_left;
190 } else if (bytenr > start) {
191 if (contains && bytenr <= end) {
192 ret = cache;
193 break;
194 }
195 n = n->rb_right;
196 } else {
197 ret = cache;
198 break;
199 }
200 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500201 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000202 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400203 spin_unlock(&info->block_group_cache_lock);
204
205 return ret;
206}
207
Yan Zheng11833d62009-09-11 16:11:19 -0400208static int add_excluded_extent(struct btrfs_root *root,
209 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400210{
Yan Zheng11833d62009-09-11 16:11:19 -0400211 u64 end = start + num_bytes - 1;
212 set_extent_bits(&root->fs_info->freed_extents[0],
213 start, end, EXTENT_UPTODATE, GFP_NOFS);
214 set_extent_bits(&root->fs_info->freed_extents[1],
215 start, end, EXTENT_UPTODATE, GFP_NOFS);
216 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400217}
218
Yan Zheng11833d62009-09-11 16:11:19 -0400219static void free_excluded_extents(struct btrfs_root *root,
220 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400221{
Yan Zheng11833d62009-09-11 16:11:19 -0400222 u64 start, end;
223
224 start = cache->key.objectid;
225 end = start + cache->key.offset - 1;
226
227 clear_extent_bits(&root->fs_info->freed_extents[0],
228 start, end, EXTENT_UPTODATE, GFP_NOFS);
229 clear_extent_bits(&root->fs_info->freed_extents[1],
230 start, end, EXTENT_UPTODATE, GFP_NOFS);
231}
232
233static int exclude_super_stripes(struct btrfs_root *root,
234 struct btrfs_block_group_cache *cache)
235{
Josef Bacik817d52f2009-07-13 21:29:25 -0400236 u64 bytenr;
237 u64 *logical;
238 int stripe_len;
239 int i, nr, ret;
240
Yan, Zheng06b23312009-11-26 09:31:11 +0000241 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
242 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
243 cache->bytes_super += stripe_len;
244 ret = add_excluded_extent(root, cache->key.objectid,
245 stripe_len);
246 BUG_ON(ret);
247 }
248
Josef Bacik817d52f2009-07-13 21:29:25 -0400249 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
250 bytenr = btrfs_sb_offset(i);
251 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
252 cache->key.objectid, bytenr,
253 0, &logical, &nr, &stripe_len);
254 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400255
Josef Bacik817d52f2009-07-13 21:29:25 -0400256 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400257 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400258 ret = add_excluded_extent(root, logical[nr],
259 stripe_len);
260 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400261 }
Yan Zheng11833d62009-09-11 16:11:19 -0400262
Josef Bacik817d52f2009-07-13 21:29:25 -0400263 kfree(logical);
264 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400265 return 0;
266}
267
Yan Zheng11833d62009-09-11 16:11:19 -0400268static struct btrfs_caching_control *
269get_caching_control(struct btrfs_block_group_cache *cache)
270{
271 struct btrfs_caching_control *ctl;
272
273 spin_lock(&cache->lock);
274 if (cache->cached != BTRFS_CACHE_STARTED) {
275 spin_unlock(&cache->lock);
276 return NULL;
277 }
278
Josef Bacikdde5abe2010-09-16 16:17:03 -0400279 /* We're loading it the fast way, so we don't have a caching_ctl. */
280 if (!cache->caching_ctl) {
281 spin_unlock(&cache->lock);
282 return NULL;
283 }
284
Yan Zheng11833d62009-09-11 16:11:19 -0400285 ctl = cache->caching_ctl;
286 atomic_inc(&ctl->count);
287 spin_unlock(&cache->lock);
288 return ctl;
289}
290
291static void put_caching_control(struct btrfs_caching_control *ctl)
292{
293 if (atomic_dec_and_test(&ctl->count))
294 kfree(ctl);
295}
296
Josef Bacik0f9dd462008-09-23 13:14:11 -0400297/*
298 * this is only called by cache_block_group, since we could have freed extents
299 * we need to check the pinned_extents for any extents that can't be used yet
300 * since their free space will be released as soon as the transaction commits.
301 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400302static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400303 struct btrfs_fs_info *info, u64 start, u64 end)
304{
Josef Bacik817d52f2009-07-13 21:29:25 -0400305 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400306 int ret;
307
308 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400309 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400310 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400311 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400312 if (ret)
313 break;
314
Yan, Zheng06b23312009-11-26 09:31:11 +0000315 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316 start = extent_end + 1;
317 } else if (extent_start > start && extent_start < end) {
318 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400319 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500320 ret = btrfs_add_free_space(block_group, start,
321 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400322 BUG_ON(ret);
323 start = extent_end + 1;
324 } else {
325 break;
326 }
327 }
328
329 if (start < end) {
330 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500332 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400333 BUG_ON(ret);
334 }
335
Josef Bacik817d52f2009-07-13 21:29:25 -0400336 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400337}
338
Josef Bacikbab39bf2011-06-30 14:42:28 -0400339static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400340{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400341 struct btrfs_block_group_cache *block_group;
342 struct btrfs_fs_info *fs_info;
343 struct btrfs_caching_control *caching_ctl;
344 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400345 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400346 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400347 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400348 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400349 u64 last = 0;
350 u32 nritems;
351 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400352
Josef Bacikbab39bf2011-06-30 14:42:28 -0400353 caching_ctl = container_of(work, struct btrfs_caching_control, work);
354 block_group = caching_ctl->block_group;
355 fs_info = block_group->fs_info;
356 extent_root = fs_info->extent_root;
357
Chris Masone37c9e62007-05-09 20:13:14 -0400358 path = btrfs_alloc_path();
359 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400360 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400361
Josef Bacik817d52f2009-07-13 21:29:25 -0400362 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400363
Chris Mason5cd57b22008-06-25 16:01:30 -0400364 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400365 * We don't want to deadlock with somebody trying to allocate a new
366 * extent for the extent root while also trying to search the extent
367 * root to add free space. So we skip locking and search the commit
368 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400369 */
370 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400371 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400372 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400373
Yan Zhenge4404d62008-12-12 10:03:26 -0500374 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400375 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400376 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400377again:
Yan Zheng11833d62009-09-11 16:11:19 -0400378 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400379 /* need to make sure the commit_root doesn't disappear */
380 down_read(&fs_info->extent_commit_sem);
381
Yan Zheng11833d62009-09-11 16:11:19 -0400382 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400383 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400384 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500385
Yan Zheng11833d62009-09-11 16:11:19 -0400386 leaf = path->nodes[0];
387 nritems = btrfs_header_nritems(leaf);
388
Chris Masond3977122009-01-05 21:25:51 -0500389 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200390 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400391 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400392 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400393 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400394
Yan Zheng11833d62009-09-11 16:11:19 -0400395 if (path->slots[0] < nritems) {
396 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
397 } else {
398 ret = find_next_key(path, 0, &key);
399 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400400 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400401
Josef Bacik589d8ad2011-05-11 17:30:53 -0400402 if (need_resched() ||
403 btrfs_next_leaf(extent_root, path)) {
404 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400405 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400406 up_read(&fs_info->extent_commit_sem);
407 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400408 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400409 goto again;
410 }
411 leaf = path->nodes[0];
412 nritems = btrfs_header_nritems(leaf);
413 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400414 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400415
Yan Zheng11833d62009-09-11 16:11:19 -0400416 if (key.objectid < block_group->key.objectid) {
417 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400418 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400419 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400420
Chris Masone37c9e62007-05-09 20:13:14 -0400421 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400422 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400423 break;
Yan7d7d6062007-09-14 16:15:28 -0400424
Yan Zheng11833d62009-09-11 16:11:19 -0400425 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400426 total_found += add_new_free_space(block_group,
427 fs_info, last,
428 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400429 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400430
Yan Zheng11833d62009-09-11 16:11:19 -0400431 if (total_found > (1024 * 1024 * 2)) {
432 total_found = 0;
433 wake_up(&caching_ctl->wait);
434 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400435 }
Chris Masone37c9e62007-05-09 20:13:14 -0400436 path->slots[0]++;
437 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400438 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400439
440 total_found += add_new_free_space(block_group, fs_info, last,
441 block_group->key.objectid +
442 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400443 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400444
445 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400446 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400447 block_group->cached = BTRFS_CACHE_FINISHED;
448 spin_unlock(&block_group->lock);
449
Chris Mason54aa1f42007-06-22 14:16:25 -0400450err:
Chris Masone37c9e62007-05-09 20:13:14 -0400451 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400452 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400453
Yan Zheng11833d62009-09-11 16:11:19 -0400454 free_excluded_extents(extent_root, block_group);
455
456 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400457out:
Yan Zheng11833d62009-09-11 16:11:19 -0400458 wake_up(&caching_ctl->wait);
459
460 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000461 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400462}
463
Josef Bacik9d66e232010-08-25 16:54:15 -0400464static int cache_block_group(struct btrfs_block_group_cache *cache,
465 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500466 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400467 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400468{
Yan Zheng11833d62009-09-11 16:11:19 -0400469 struct btrfs_fs_info *fs_info = cache->fs_info;
470 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400471 int ret = 0;
472
Yan Zheng11833d62009-09-11 16:11:19 -0400473 smp_mb();
474 if (cache->cached != BTRFS_CACHE_NO)
475 return 0;
476
Josef Bacik9d66e232010-08-25 16:54:15 -0400477 /*
478 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500479 * to have the normal tree locking. Also if we are currently trying to
480 * allocate blocks for the tree root we can't do the fast caching since
481 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400482 */
Li Dongyangf7039b12011-03-24 10:24:28 +0000483 if (trans && (!trans->transaction->in_commit) &&
Josef Bacik73bc1872011-10-03 14:07:49 -0400484 (root && root != root->fs_info->tree_root) &&
485 btrfs_test_opt(root, SPACE_CACHE)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400486 spin_lock(&cache->lock);
487 if (cache->cached != BTRFS_CACHE_NO) {
488 spin_unlock(&cache->lock);
489 return 0;
490 }
491 cache->cached = BTRFS_CACHE_STARTED;
492 spin_unlock(&cache->lock);
493
494 ret = load_free_space_cache(fs_info, cache);
495
496 spin_lock(&cache->lock);
497 if (ret == 1) {
498 cache->cached = BTRFS_CACHE_FINISHED;
499 cache->last_byte_to_unpin = (u64)-1;
500 } else {
501 cache->cached = BTRFS_CACHE_NO;
502 }
503 spin_unlock(&cache->lock);
Josef Bacik3c148742011-02-02 15:53:47 +0000504 if (ret == 1) {
505 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400506 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000507 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400508 }
509
510 if (load_cache_only)
511 return 0;
512
Miao Xiefc0e4a32011-03-24 11:41:21 +0000513 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -0400514 BUG_ON(!caching_ctl);
515
516 INIT_LIST_HEAD(&caching_ctl->list);
517 mutex_init(&caching_ctl->mutex);
518 init_waitqueue_head(&caching_ctl->wait);
519 caching_ctl->block_group = cache;
520 caching_ctl->progress = cache->key.objectid;
521 /* one for caching kthread, one for caching block group list */
522 atomic_set(&caching_ctl->count, 2);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400523 caching_ctl->work.func = caching_thread;
Yan Zheng11833d62009-09-11 16:11:19 -0400524
Josef Bacik817d52f2009-07-13 21:29:25 -0400525 spin_lock(&cache->lock);
526 if (cache->cached != BTRFS_CACHE_NO) {
527 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400528 kfree(caching_ctl);
529 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400530 }
Yan Zheng11833d62009-09-11 16:11:19 -0400531 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400532 cache->cached = BTRFS_CACHE_STARTED;
533 spin_unlock(&cache->lock);
534
Yan Zheng11833d62009-09-11 16:11:19 -0400535 down_write(&fs_info->extent_commit_sem);
536 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
537 up_write(&fs_info->extent_commit_sem);
538
Josef Bacik11dfe352009-11-13 20:12:59 +0000539 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400540
Josef Bacikbab39bf2011-06-30 14:42:28 -0400541 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400542
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400543 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400544}
545
Josef Bacik0f9dd462008-09-23 13:14:11 -0400546/*
547 * return the block group that starts at or after bytenr
548 */
Chris Masond3977122009-01-05 21:25:51 -0500549static struct btrfs_block_group_cache *
550btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400551{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400552 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400553
Josef Bacik0f9dd462008-09-23 13:14:11 -0400554 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400555
Josef Bacik0f9dd462008-09-23 13:14:11 -0400556 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400557}
558
Josef Bacik0f9dd462008-09-23 13:14:11 -0400559/*
Sankar P9f556842009-05-14 13:52:22 -0400560 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400561 */
Chris Masond3977122009-01-05 21:25:51 -0500562struct btrfs_block_group_cache *btrfs_lookup_block_group(
563 struct btrfs_fs_info *info,
564 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400565{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400566 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400567
Josef Bacik0f9dd462008-09-23 13:14:11 -0400568 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400569
Josef Bacik0f9dd462008-09-23 13:14:11 -0400570 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400571}
Chris Mason0b86a832008-03-24 15:01:56 -0400572
Josef Bacik0f9dd462008-09-23 13:14:11 -0400573static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
574 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400575{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400576 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400577 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400578
Yan, Zhengb742bb82010-05-16 10:46:24 -0400579 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
580 BTRFS_BLOCK_GROUP_METADATA;
581
Chris Mason4184ea72009-03-10 12:39:20 -0400582 rcu_read_lock();
583 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400584 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400585 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400586 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400587 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400588 }
Chris Mason4184ea72009-03-10 12:39:20 -0400589 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400590 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400591}
592
Chris Mason4184ea72009-03-10 12:39:20 -0400593/*
594 * after adding space to the filesystem, we need to clear the full flags
595 * on all the space infos.
596 */
597void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
598{
599 struct list_head *head = &info->space_info;
600 struct btrfs_space_info *found;
601
602 rcu_read_lock();
603 list_for_each_entry_rcu(found, head, list)
604 found->full = 0;
605 rcu_read_unlock();
606}
607
Josef Bacik80eb2342008-10-29 14:49:05 -0400608static u64 div_factor(u64 num, int factor)
609{
610 if (factor == 10)
611 return num;
612 num *= factor;
613 do_div(num, 10);
614 return num;
615}
616
Chris Masone5bc2452010-10-26 13:37:56 -0400617static u64 div_factor_fine(u64 num, int factor)
618{
619 if (factor == 100)
620 return num;
621 num *= factor;
622 do_div(num, 100);
623 return num;
624}
625
Yan Zhengd2fb3432008-12-11 16:30:39 -0500626u64 btrfs_find_block_group(struct btrfs_root *root,
627 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400628{
Chris Mason96b51792007-10-15 16:15:19 -0400629 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400630 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500631 u64 last = max(search_hint, search_start);
632 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400633 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500634 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400635 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400636again:
Zheng Yane8569812008-09-26 10:05:48 -0400637 while (1) {
638 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400639 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400640 break;
Chris Mason96b51792007-10-15 16:15:19 -0400641
Chris Masonc286ac42008-07-22 23:06:41 -0400642 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400643 last = cache->key.objectid + cache->key.offset;
644 used = btrfs_block_group_used(&cache->item);
645
Yan Zhengd2fb3432008-12-11 16:30:39 -0500646 if ((full_search || !cache->ro) &&
647 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400648 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500649 div_factor(cache->key.offset, factor)) {
650 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400651 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400652 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400653 goto found;
654 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400655 }
Chris Masonc286ac42008-07-22 23:06:41 -0400656 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400657 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400658 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400659 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400660 if (!wrapped) {
661 last = search_start;
662 wrapped = 1;
663 goto again;
664 }
665 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400666 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400667 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400668 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400669 goto again;
670 }
Chris Masonbe744172007-05-06 10:15:01 -0400671found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500672 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400673}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400674
Chris Masone02119d2008-09-05 16:13:11 -0400675/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400676int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400677{
678 int ret;
679 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400680 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400681
Zheng Yan31840ae2008-09-23 13:14:14 -0400682 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700683 if (!path)
684 return -ENOMEM;
685
Chris Masone02119d2008-09-05 16:13:11 -0400686 key.objectid = start;
687 key.offset = len;
688 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
689 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
690 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400691 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500692 return ret;
693}
694
Chris Masond8d5f3e2007-12-11 12:42:00 -0500695/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400696 * helper function to lookup reference count and flags of extent.
697 *
698 * the head node for delayed ref is used to store the sum of all the
699 * reference count modifications queued up in the rbtree. the head
700 * node may also store the extent flags to set. This way you can check
701 * to see what the reference count and extent flags would be if all of
702 * the delayed refs are not processed.
703 */
704int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
705 struct btrfs_root *root, u64 bytenr,
706 u64 num_bytes, u64 *refs, u64 *flags)
707{
708 struct btrfs_delayed_ref_head *head;
709 struct btrfs_delayed_ref_root *delayed_refs;
710 struct btrfs_path *path;
711 struct btrfs_extent_item *ei;
712 struct extent_buffer *leaf;
713 struct btrfs_key key;
714 u32 item_size;
715 u64 num_refs;
716 u64 extent_flags;
717 int ret;
718
719 path = btrfs_alloc_path();
720 if (!path)
721 return -ENOMEM;
722
723 key.objectid = bytenr;
724 key.type = BTRFS_EXTENT_ITEM_KEY;
725 key.offset = num_bytes;
726 if (!trans) {
727 path->skip_locking = 1;
728 path->search_commit_root = 1;
729 }
730again:
731 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
732 &key, path, 0, 0);
733 if (ret < 0)
734 goto out_free;
735
736 if (ret == 0) {
737 leaf = path->nodes[0];
738 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
739 if (item_size >= sizeof(*ei)) {
740 ei = btrfs_item_ptr(leaf, path->slots[0],
741 struct btrfs_extent_item);
742 num_refs = btrfs_extent_refs(leaf, ei);
743 extent_flags = btrfs_extent_flags(leaf, ei);
744 } else {
745#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
746 struct btrfs_extent_item_v0 *ei0;
747 BUG_ON(item_size != sizeof(*ei0));
748 ei0 = btrfs_item_ptr(leaf, path->slots[0],
749 struct btrfs_extent_item_v0);
750 num_refs = btrfs_extent_refs_v0(leaf, ei0);
751 /* FIXME: this isn't correct for data */
752 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
753#else
754 BUG();
755#endif
756 }
757 BUG_ON(num_refs == 0);
758 } else {
759 num_refs = 0;
760 extent_flags = 0;
761 ret = 0;
762 }
763
764 if (!trans)
765 goto out;
766
767 delayed_refs = &trans->transaction->delayed_refs;
768 spin_lock(&delayed_refs->lock);
769 head = btrfs_find_delayed_ref_head(trans, bytenr);
770 if (head) {
771 if (!mutex_trylock(&head->mutex)) {
772 atomic_inc(&head->node.refs);
773 spin_unlock(&delayed_refs->lock);
774
David Sterbab3b4aa72011-04-21 01:20:15 +0200775 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400776
David Sterba8cc33e52011-05-02 15:29:25 +0200777 /*
778 * Mutex was contended, block until it's released and try
779 * again
780 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400781 mutex_lock(&head->mutex);
782 mutex_unlock(&head->mutex);
783 btrfs_put_delayed_ref(&head->node);
784 goto again;
785 }
786 if (head->extent_op && head->extent_op->update_flags)
787 extent_flags |= head->extent_op->flags_to_set;
788 else
789 BUG_ON(num_refs == 0);
790
791 num_refs += head->node.ref_mod;
792 mutex_unlock(&head->mutex);
793 }
794 spin_unlock(&delayed_refs->lock);
795out:
796 WARN_ON(num_refs == 0);
797 if (refs)
798 *refs = num_refs;
799 if (flags)
800 *flags = extent_flags;
801out_free:
802 btrfs_free_path(path);
803 return ret;
804}
805
806/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500807 * Back reference rules. Back refs have three main goals:
808 *
809 * 1) differentiate between all holders of references to an extent so that
810 * when a reference is dropped we can make sure it was a valid reference
811 * before freeing the extent.
812 *
813 * 2) Provide enough information to quickly find the holders of an extent
814 * if we notice a given block is corrupted or bad.
815 *
816 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
817 * maintenance. This is actually the same as #2, but with a slightly
818 * different use case.
819 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400820 * There are two kinds of back refs. The implicit back refs is optimized
821 * for pointers in non-shared tree blocks. For a given pointer in a block,
822 * back refs of this kind provide information about the block's owner tree
823 * and the pointer's key. These information allow us to find the block by
824 * b-tree searching. The full back refs is for pointers in tree blocks not
825 * referenced by their owner trees. The location of tree block is recorded
826 * in the back refs. Actually the full back refs is generic, and can be
827 * used in all cases the implicit back refs is used. The major shortcoming
828 * of the full back refs is its overhead. Every time a tree block gets
829 * COWed, we have to update back refs entry for all pointers in it.
830 *
831 * For a newly allocated tree block, we use implicit back refs for
832 * pointers in it. This means most tree related operations only involve
833 * implicit back refs. For a tree block created in old transaction, the
834 * only way to drop a reference to it is COW it. So we can detect the
835 * event that tree block loses its owner tree's reference and do the
836 * back refs conversion.
837 *
838 * When a tree block is COW'd through a tree, there are four cases:
839 *
840 * The reference count of the block is one and the tree is the block's
841 * owner tree. Nothing to do in this case.
842 *
843 * The reference count of the block is one and the tree is not the
844 * block's owner tree. In this case, full back refs is used for pointers
845 * in the block. Remove these full back refs, add implicit back refs for
846 * every pointers in the new block.
847 *
848 * The reference count of the block is greater than one and the tree is
849 * the block's owner tree. In this case, implicit back refs is used for
850 * pointers in the block. Add full back refs for every pointers in the
851 * block, increase lower level extents' reference counts. The original
852 * implicit back refs are entailed to the new block.
853 *
854 * The reference count of the block is greater than one and the tree is
855 * not the block's owner tree. Add implicit back refs for every pointer in
856 * the new block, increase lower level extents' reference count.
857 *
858 * Back Reference Key composing:
859 *
860 * The key objectid corresponds to the first byte in the extent,
861 * The key type is used to differentiate between types of back refs.
862 * There are different meanings of the key offset for different types
863 * of back refs.
864 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500865 * File extents can be referenced by:
866 *
867 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400868 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500869 * - different offsets inside a file (bookend extents in file.c)
870 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500872 *
873 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500874 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400875 * - original offset in the file
876 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400877 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400878 * The key offset for the implicit back refs is hash of the first
879 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500880 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500882 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400883 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500884 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400885 * The key offset for the implicit back refs is the first byte of
886 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500887 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400888 * When a file extent is allocated, The implicit back refs is used.
889 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500890 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400891 * (root_key.objectid, inode objectid, offset in file, 1)
892 *
893 * When a file extent is removed file truncation, we find the
894 * corresponding implicit back refs and check the following fields:
895 *
896 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500897 *
898 * Btree extents can be referenced by:
899 *
900 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500901 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902 * Both the implicit back refs and the full back refs for tree blocks
903 * only consist of key. The key offset for the implicit back refs is
904 * objectid of block's owner tree. The key offset for the full back refs
905 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500906 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400907 * When implicit back refs is used, information about the lowest key and
908 * level of the tree block are required. These information are stored in
909 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500910 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400911
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400912#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
913static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
914 struct btrfs_root *root,
915 struct btrfs_path *path,
916 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500917{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 struct btrfs_extent_item *item;
919 struct btrfs_extent_item_v0 *ei0;
920 struct btrfs_extent_ref_v0 *ref0;
921 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400922 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400923 struct btrfs_key key;
924 struct btrfs_key found_key;
925 u32 new_size = sizeof(*item);
926 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500927 int ret;
928
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400929 leaf = path->nodes[0];
930 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500931
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400932 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
933 ei0 = btrfs_item_ptr(leaf, path->slots[0],
934 struct btrfs_extent_item_v0);
935 refs = btrfs_extent_refs_v0(leaf, ei0);
936
937 if (owner == (u64)-1) {
938 while (1) {
939 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
940 ret = btrfs_next_leaf(root, path);
941 if (ret < 0)
942 return ret;
943 BUG_ON(ret > 0);
944 leaf = path->nodes[0];
945 }
946 btrfs_item_key_to_cpu(leaf, &found_key,
947 path->slots[0]);
948 BUG_ON(key.objectid != found_key.objectid);
949 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
950 path->slots[0]++;
951 continue;
952 }
953 ref0 = btrfs_item_ptr(leaf, path->slots[0],
954 struct btrfs_extent_ref_v0);
955 owner = btrfs_ref_objectid_v0(leaf, ref0);
956 break;
957 }
958 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200959 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400960
961 if (owner < BTRFS_FIRST_FREE_OBJECTID)
962 new_size += sizeof(*bi);
963
964 new_size -= sizeof(*ei0);
965 ret = btrfs_search_slot(trans, root, &key, path,
966 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400967 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 return ret;
969 BUG_ON(ret);
970
971 ret = btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400972
973 leaf = path->nodes[0];
974 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
975 btrfs_set_extent_refs(leaf, item, refs);
976 /* FIXME: get real generation */
977 btrfs_set_extent_generation(leaf, item, 0);
978 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
979 btrfs_set_extent_flags(leaf, item,
980 BTRFS_EXTENT_FLAG_TREE_BLOCK |
981 BTRFS_BLOCK_FLAG_FULL_BACKREF);
982 bi = (struct btrfs_tree_block_info *)(item + 1);
983 /* FIXME: get first key of the block */
984 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
985 btrfs_set_tree_block_level(leaf, bi, (int)owner);
986 } else {
987 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
988 }
989 btrfs_mark_buffer_dirty(leaf);
990 return 0;
991}
992#endif
993
994static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
995{
996 u32 high_crc = ~(u32)0;
997 u32 low_crc = ~(u32)0;
998 __le64 lenum;
999
1000 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001001 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001003 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001004 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001005 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001006
1007 return ((u64)high_crc << 31) ^ (u64)low_crc;
1008}
1009
1010static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1011 struct btrfs_extent_data_ref *ref)
1012{
1013 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1014 btrfs_extent_data_ref_objectid(leaf, ref),
1015 btrfs_extent_data_ref_offset(leaf, ref));
1016}
1017
1018static int match_extent_data_ref(struct extent_buffer *leaf,
1019 struct btrfs_extent_data_ref *ref,
1020 u64 root_objectid, u64 owner, u64 offset)
1021{
1022 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1023 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1024 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1025 return 0;
1026 return 1;
1027}
1028
1029static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1030 struct btrfs_root *root,
1031 struct btrfs_path *path,
1032 u64 bytenr, u64 parent,
1033 u64 root_objectid,
1034 u64 owner, u64 offset)
1035{
1036 struct btrfs_key key;
1037 struct btrfs_extent_data_ref *ref;
1038 struct extent_buffer *leaf;
1039 u32 nritems;
1040 int ret;
1041 int recow;
1042 int err = -ENOENT;
1043
1044 key.objectid = bytenr;
1045 if (parent) {
1046 key.type = BTRFS_SHARED_DATA_REF_KEY;
1047 key.offset = parent;
1048 } else {
1049 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1050 key.offset = hash_extent_data_ref(root_objectid,
1051 owner, offset);
1052 }
1053again:
1054 recow = 0;
1055 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1056 if (ret < 0) {
1057 err = ret;
1058 goto fail;
1059 }
1060
1061 if (parent) {
1062 if (!ret)
1063 return 0;
1064#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1065 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001066 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001067 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1068 if (ret < 0) {
1069 err = ret;
1070 goto fail;
1071 }
1072 if (!ret)
1073 return 0;
1074#endif
1075 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001076 }
1077
1078 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001079 nritems = btrfs_header_nritems(leaf);
1080 while (1) {
1081 if (path->slots[0] >= nritems) {
1082 ret = btrfs_next_leaf(root, path);
1083 if (ret < 0)
1084 err = ret;
1085 if (ret)
1086 goto fail;
1087
1088 leaf = path->nodes[0];
1089 nritems = btrfs_header_nritems(leaf);
1090 recow = 1;
1091 }
1092
1093 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1094 if (key.objectid != bytenr ||
1095 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1096 goto fail;
1097
1098 ref = btrfs_item_ptr(leaf, path->slots[0],
1099 struct btrfs_extent_data_ref);
1100
1101 if (match_extent_data_ref(leaf, ref, root_objectid,
1102 owner, offset)) {
1103 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001104 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001105 goto again;
1106 }
1107 err = 0;
1108 break;
1109 }
1110 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001111 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001112fail:
1113 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001114}
1115
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001116static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1117 struct btrfs_root *root,
1118 struct btrfs_path *path,
1119 u64 bytenr, u64 parent,
1120 u64 root_objectid, u64 owner,
1121 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001122{
1123 struct btrfs_key key;
1124 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001125 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001126 u32 num_refs;
1127 int ret;
1128
1129 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001130 if (parent) {
1131 key.type = BTRFS_SHARED_DATA_REF_KEY;
1132 key.offset = parent;
1133 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001134 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001135 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1136 key.offset = hash_extent_data_ref(root_objectid,
1137 owner, offset);
1138 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001139 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001140
1141 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1142 if (ret && ret != -EEXIST)
1143 goto fail;
1144
1145 leaf = path->nodes[0];
1146 if (parent) {
1147 struct btrfs_shared_data_ref *ref;
1148 ref = btrfs_item_ptr(leaf, path->slots[0],
1149 struct btrfs_shared_data_ref);
1150 if (ret == 0) {
1151 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1152 } else {
1153 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1154 num_refs += refs_to_add;
1155 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1156 }
1157 } else {
1158 struct btrfs_extent_data_ref *ref;
1159 while (ret == -EEXIST) {
1160 ref = btrfs_item_ptr(leaf, path->slots[0],
1161 struct btrfs_extent_data_ref);
1162 if (match_extent_data_ref(leaf, ref, root_objectid,
1163 owner, offset))
1164 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001165 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001166 key.offset++;
1167 ret = btrfs_insert_empty_item(trans, root, path, &key,
1168 size);
1169 if (ret && ret != -EEXIST)
1170 goto fail;
1171
1172 leaf = path->nodes[0];
1173 }
1174 ref = btrfs_item_ptr(leaf, path->slots[0],
1175 struct btrfs_extent_data_ref);
1176 if (ret == 0) {
1177 btrfs_set_extent_data_ref_root(leaf, ref,
1178 root_objectid);
1179 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1180 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1181 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1182 } else {
1183 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1184 num_refs += refs_to_add;
1185 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1186 }
1187 }
1188 btrfs_mark_buffer_dirty(leaf);
1189 ret = 0;
1190fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001191 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001192 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001193}
1194
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001195static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1196 struct btrfs_root *root,
1197 struct btrfs_path *path,
1198 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001199{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001200 struct btrfs_key key;
1201 struct btrfs_extent_data_ref *ref1 = NULL;
1202 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001203 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001204 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001205 int ret = 0;
1206
1207 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001208 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1209
1210 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1211 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1212 struct btrfs_extent_data_ref);
1213 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1214 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1215 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1216 struct btrfs_shared_data_ref);
1217 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1218#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1219 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1220 struct btrfs_extent_ref_v0 *ref0;
1221 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1222 struct btrfs_extent_ref_v0);
1223 num_refs = btrfs_ref_count_v0(leaf, ref0);
1224#endif
1225 } else {
1226 BUG();
1227 }
1228
Chris Mason56bec292009-03-13 10:10:06 -04001229 BUG_ON(num_refs < refs_to_drop);
1230 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001231
Zheng Yan31840ae2008-09-23 13:14:14 -04001232 if (num_refs == 0) {
1233 ret = btrfs_del_item(trans, root, path);
1234 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001235 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1236 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1237 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1238 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1239#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1240 else {
1241 struct btrfs_extent_ref_v0 *ref0;
1242 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1243 struct btrfs_extent_ref_v0);
1244 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1245 }
1246#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001247 btrfs_mark_buffer_dirty(leaf);
1248 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001249 return ret;
1250}
1251
1252static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1253 struct btrfs_path *path,
1254 struct btrfs_extent_inline_ref *iref)
1255{
1256 struct btrfs_key key;
1257 struct extent_buffer *leaf;
1258 struct btrfs_extent_data_ref *ref1;
1259 struct btrfs_shared_data_ref *ref2;
1260 u32 num_refs = 0;
1261
1262 leaf = path->nodes[0];
1263 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1264 if (iref) {
1265 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1266 BTRFS_EXTENT_DATA_REF_KEY) {
1267 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1268 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1269 } else {
1270 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1271 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1272 }
1273 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1274 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1275 struct btrfs_extent_data_ref);
1276 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1277 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1278 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1279 struct btrfs_shared_data_ref);
1280 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1281#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1282 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1283 struct btrfs_extent_ref_v0 *ref0;
1284 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1285 struct btrfs_extent_ref_v0);
1286 num_refs = btrfs_ref_count_v0(leaf, ref0);
1287#endif
1288 } else {
1289 WARN_ON(1);
1290 }
1291 return num_refs;
1292}
1293
1294static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1295 struct btrfs_root *root,
1296 struct btrfs_path *path,
1297 u64 bytenr, u64 parent,
1298 u64 root_objectid)
1299{
1300 struct btrfs_key key;
1301 int ret;
1302
1303 key.objectid = bytenr;
1304 if (parent) {
1305 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1306 key.offset = parent;
1307 } else {
1308 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1309 key.offset = root_objectid;
1310 }
1311
1312 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1313 if (ret > 0)
1314 ret = -ENOENT;
1315#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1316 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001317 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001318 key.type = BTRFS_EXTENT_REF_V0_KEY;
1319 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1320 if (ret > 0)
1321 ret = -ENOENT;
1322 }
1323#endif
1324 return ret;
1325}
1326
1327static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1328 struct btrfs_root *root,
1329 struct btrfs_path *path,
1330 u64 bytenr, u64 parent,
1331 u64 root_objectid)
1332{
1333 struct btrfs_key key;
1334 int ret;
1335
1336 key.objectid = bytenr;
1337 if (parent) {
1338 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1339 key.offset = parent;
1340 } else {
1341 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1342 key.offset = root_objectid;
1343 }
1344
1345 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001346 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001347 return ret;
1348}
1349
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001350static inline int extent_ref_type(u64 parent, u64 owner)
1351{
1352 int type;
1353 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1354 if (parent > 0)
1355 type = BTRFS_SHARED_BLOCK_REF_KEY;
1356 else
1357 type = BTRFS_TREE_BLOCK_REF_KEY;
1358 } else {
1359 if (parent > 0)
1360 type = BTRFS_SHARED_DATA_REF_KEY;
1361 else
1362 type = BTRFS_EXTENT_DATA_REF_KEY;
1363 }
1364 return type;
1365}
1366
Yan Zheng2c47e6052009-06-27 21:07:35 -04001367static int find_next_key(struct btrfs_path *path, int level,
1368 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001369
1370{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001371 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001372 if (!path->nodes[level])
1373 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001374 if (path->slots[level] + 1 >=
1375 btrfs_header_nritems(path->nodes[level]))
1376 continue;
1377 if (level == 0)
1378 btrfs_item_key_to_cpu(path->nodes[level], key,
1379 path->slots[level] + 1);
1380 else
1381 btrfs_node_key_to_cpu(path->nodes[level], key,
1382 path->slots[level] + 1);
1383 return 0;
1384 }
1385 return 1;
1386}
1387
1388/*
1389 * look for inline back ref. if back ref is found, *ref_ret is set
1390 * to the address of inline back ref, and 0 is returned.
1391 *
1392 * if back ref isn't found, *ref_ret is set to the address where it
1393 * should be inserted, and -ENOENT is returned.
1394 *
1395 * if insert is true and there are too many inline back refs, the path
1396 * points to the extent item, and -EAGAIN is returned.
1397 *
1398 * NOTE: inline back refs are ordered in the same way that back ref
1399 * items in the tree are ordered.
1400 */
1401static noinline_for_stack
1402int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1403 struct btrfs_root *root,
1404 struct btrfs_path *path,
1405 struct btrfs_extent_inline_ref **ref_ret,
1406 u64 bytenr, u64 num_bytes,
1407 u64 parent, u64 root_objectid,
1408 u64 owner, u64 offset, int insert)
1409{
1410 struct btrfs_key key;
1411 struct extent_buffer *leaf;
1412 struct btrfs_extent_item *ei;
1413 struct btrfs_extent_inline_ref *iref;
1414 u64 flags;
1415 u64 item_size;
1416 unsigned long ptr;
1417 unsigned long end;
1418 int extra_size;
1419 int type;
1420 int want;
1421 int ret;
1422 int err = 0;
1423
1424 key.objectid = bytenr;
1425 key.type = BTRFS_EXTENT_ITEM_KEY;
1426 key.offset = num_bytes;
1427
1428 want = extent_ref_type(parent, owner);
1429 if (insert) {
1430 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001431 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001432 } else
1433 extra_size = -1;
1434 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1435 if (ret < 0) {
1436 err = ret;
1437 goto out;
1438 }
1439 BUG_ON(ret);
1440
1441 leaf = path->nodes[0];
1442 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1443#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1444 if (item_size < sizeof(*ei)) {
1445 if (!insert) {
1446 err = -ENOENT;
1447 goto out;
1448 }
1449 ret = convert_extent_item_v0(trans, root, path, owner,
1450 extra_size);
1451 if (ret < 0) {
1452 err = ret;
1453 goto out;
1454 }
1455 leaf = path->nodes[0];
1456 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1457 }
1458#endif
1459 BUG_ON(item_size < sizeof(*ei));
1460
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001461 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1462 flags = btrfs_extent_flags(leaf, ei);
1463
1464 ptr = (unsigned long)(ei + 1);
1465 end = (unsigned long)ei + item_size;
1466
1467 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1468 ptr += sizeof(struct btrfs_tree_block_info);
1469 BUG_ON(ptr > end);
1470 } else {
1471 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1472 }
1473
1474 err = -ENOENT;
1475 while (1) {
1476 if (ptr >= end) {
1477 WARN_ON(ptr > end);
1478 break;
1479 }
1480 iref = (struct btrfs_extent_inline_ref *)ptr;
1481 type = btrfs_extent_inline_ref_type(leaf, iref);
1482 if (want < type)
1483 break;
1484 if (want > type) {
1485 ptr += btrfs_extent_inline_ref_size(type);
1486 continue;
1487 }
1488
1489 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1490 struct btrfs_extent_data_ref *dref;
1491 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1492 if (match_extent_data_ref(leaf, dref, root_objectid,
1493 owner, offset)) {
1494 err = 0;
1495 break;
1496 }
1497 if (hash_extent_data_ref_item(leaf, dref) <
1498 hash_extent_data_ref(root_objectid, owner, offset))
1499 break;
1500 } else {
1501 u64 ref_offset;
1502 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1503 if (parent > 0) {
1504 if (parent == ref_offset) {
1505 err = 0;
1506 break;
1507 }
1508 if (ref_offset < parent)
1509 break;
1510 } else {
1511 if (root_objectid == ref_offset) {
1512 err = 0;
1513 break;
1514 }
1515 if (ref_offset < root_objectid)
1516 break;
1517 }
1518 }
1519 ptr += btrfs_extent_inline_ref_size(type);
1520 }
1521 if (err == -ENOENT && insert) {
1522 if (item_size + extra_size >=
1523 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1524 err = -EAGAIN;
1525 goto out;
1526 }
1527 /*
1528 * To add new inline back ref, we have to make sure
1529 * there is no corresponding back ref item.
1530 * For simplicity, we just do not add new inline back
1531 * ref if there is any kind of item for this block
1532 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001533 if (find_next_key(path, 0, &key) == 0 &&
1534 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001535 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001536 err = -EAGAIN;
1537 goto out;
1538 }
1539 }
1540 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1541out:
Yan Zheng85d41982009-06-11 08:51:10 -04001542 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001543 path->keep_locks = 0;
1544 btrfs_unlock_up_safe(path, 1);
1545 }
1546 return err;
1547}
1548
1549/*
1550 * helper to add new inline back ref
1551 */
1552static noinline_for_stack
1553int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1554 struct btrfs_root *root,
1555 struct btrfs_path *path,
1556 struct btrfs_extent_inline_ref *iref,
1557 u64 parent, u64 root_objectid,
1558 u64 owner, u64 offset, int refs_to_add,
1559 struct btrfs_delayed_extent_op *extent_op)
1560{
1561 struct extent_buffer *leaf;
1562 struct btrfs_extent_item *ei;
1563 unsigned long ptr;
1564 unsigned long end;
1565 unsigned long item_offset;
1566 u64 refs;
1567 int size;
1568 int type;
1569 int ret;
1570
1571 leaf = path->nodes[0];
1572 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1573 item_offset = (unsigned long)iref - (unsigned long)ei;
1574
1575 type = extent_ref_type(parent, owner);
1576 size = btrfs_extent_inline_ref_size(type);
1577
1578 ret = btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001579
1580 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1581 refs = btrfs_extent_refs(leaf, ei);
1582 refs += refs_to_add;
1583 btrfs_set_extent_refs(leaf, ei, refs);
1584 if (extent_op)
1585 __run_delayed_extent_op(extent_op, leaf, ei);
1586
1587 ptr = (unsigned long)ei + item_offset;
1588 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1589 if (ptr < end - size)
1590 memmove_extent_buffer(leaf, ptr + size, ptr,
1591 end - size - ptr);
1592
1593 iref = (struct btrfs_extent_inline_ref *)ptr;
1594 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1595 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1596 struct btrfs_extent_data_ref *dref;
1597 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1598 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1599 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1600 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1601 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1602 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1603 struct btrfs_shared_data_ref *sref;
1604 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1605 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1606 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1607 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1608 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1609 } else {
1610 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1611 }
1612 btrfs_mark_buffer_dirty(leaf);
1613 return 0;
1614}
1615
1616static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1617 struct btrfs_root *root,
1618 struct btrfs_path *path,
1619 struct btrfs_extent_inline_ref **ref_ret,
1620 u64 bytenr, u64 num_bytes, u64 parent,
1621 u64 root_objectid, u64 owner, u64 offset)
1622{
1623 int ret;
1624
1625 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1626 bytenr, num_bytes, parent,
1627 root_objectid, owner, offset, 0);
1628 if (ret != -ENOENT)
1629 return ret;
1630
David Sterbab3b4aa72011-04-21 01:20:15 +02001631 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001632 *ref_ret = NULL;
1633
1634 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1635 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1636 root_objectid);
1637 } else {
1638 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1639 root_objectid, owner, offset);
1640 }
1641 return ret;
1642}
1643
1644/*
1645 * helper to update/remove inline back ref
1646 */
1647static noinline_for_stack
1648int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1649 struct btrfs_root *root,
1650 struct btrfs_path *path,
1651 struct btrfs_extent_inline_ref *iref,
1652 int refs_to_mod,
1653 struct btrfs_delayed_extent_op *extent_op)
1654{
1655 struct extent_buffer *leaf;
1656 struct btrfs_extent_item *ei;
1657 struct btrfs_extent_data_ref *dref = NULL;
1658 struct btrfs_shared_data_ref *sref = NULL;
1659 unsigned long ptr;
1660 unsigned long end;
1661 u32 item_size;
1662 int size;
1663 int type;
1664 int ret;
1665 u64 refs;
1666
1667 leaf = path->nodes[0];
1668 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1669 refs = btrfs_extent_refs(leaf, ei);
1670 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1671 refs += refs_to_mod;
1672 btrfs_set_extent_refs(leaf, ei, refs);
1673 if (extent_op)
1674 __run_delayed_extent_op(extent_op, leaf, ei);
1675
1676 type = btrfs_extent_inline_ref_type(leaf, iref);
1677
1678 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1679 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1680 refs = btrfs_extent_data_ref_count(leaf, dref);
1681 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1682 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1683 refs = btrfs_shared_data_ref_count(leaf, sref);
1684 } else {
1685 refs = 1;
1686 BUG_ON(refs_to_mod != -1);
1687 }
1688
1689 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1690 refs += refs_to_mod;
1691
1692 if (refs > 0) {
1693 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1694 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1695 else
1696 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1697 } else {
1698 size = btrfs_extent_inline_ref_size(type);
1699 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1700 ptr = (unsigned long)iref;
1701 end = (unsigned long)ei + item_size;
1702 if (ptr + size < end)
1703 memmove_extent_buffer(leaf, ptr, ptr + size,
1704 end - ptr - size);
1705 item_size -= size;
1706 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001707 }
1708 btrfs_mark_buffer_dirty(leaf);
1709 return 0;
1710}
1711
1712static noinline_for_stack
1713int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1714 struct btrfs_root *root,
1715 struct btrfs_path *path,
1716 u64 bytenr, u64 num_bytes, u64 parent,
1717 u64 root_objectid, u64 owner,
1718 u64 offset, int refs_to_add,
1719 struct btrfs_delayed_extent_op *extent_op)
1720{
1721 struct btrfs_extent_inline_ref *iref;
1722 int ret;
1723
1724 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1725 bytenr, num_bytes, parent,
1726 root_objectid, owner, offset, 1);
1727 if (ret == 0) {
1728 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1729 ret = update_inline_extent_backref(trans, root, path, iref,
1730 refs_to_add, extent_op);
1731 } else if (ret == -ENOENT) {
1732 ret = setup_inline_extent_backref(trans, root, path, iref,
1733 parent, root_objectid,
1734 owner, offset, refs_to_add,
1735 extent_op);
1736 }
1737 return ret;
1738}
1739
1740static int insert_extent_backref(struct btrfs_trans_handle *trans,
1741 struct btrfs_root *root,
1742 struct btrfs_path *path,
1743 u64 bytenr, u64 parent, u64 root_objectid,
1744 u64 owner, u64 offset, int refs_to_add)
1745{
1746 int ret;
1747 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1748 BUG_ON(refs_to_add != 1);
1749 ret = insert_tree_block_ref(trans, root, path, bytenr,
1750 parent, root_objectid);
1751 } else {
1752 ret = insert_extent_data_ref(trans, root, path, bytenr,
1753 parent, root_objectid,
1754 owner, offset, refs_to_add);
1755 }
1756 return ret;
1757}
1758
1759static int remove_extent_backref(struct btrfs_trans_handle *trans,
1760 struct btrfs_root *root,
1761 struct btrfs_path *path,
1762 struct btrfs_extent_inline_ref *iref,
1763 int refs_to_drop, int is_data)
1764{
1765 int ret;
1766
1767 BUG_ON(!is_data && refs_to_drop != 1);
1768 if (iref) {
1769 ret = update_inline_extent_backref(trans, root, path, iref,
1770 -refs_to_drop, NULL);
1771 } else if (is_data) {
1772 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1773 } else {
1774 ret = btrfs_del_item(trans, root, path);
1775 }
1776 return ret;
1777}
1778
Li Dongyang5378e602011-03-24 10:24:27 +00001779static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001780 u64 start, u64 len)
1781{
Li Dongyang5378e602011-03-24 10:24:27 +00001782 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001783}
Chris Mason15916de2008-11-19 21:17:22 -05001784
Liu Hui1f3c79a2009-01-05 15:57:51 -05001785static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001786 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001787{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001788 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001789 u64 discarded_bytes = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001790 struct btrfs_multi_bio *multi = NULL;
1791
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001792
Liu Hui1f3c79a2009-01-05 15:57:51 -05001793 /* Tell the block device(s) that the sectors can be discarded */
Li Dongyang5378e602011-03-24 10:24:27 +00001794 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1795 bytenr, &num_bytes, &multi, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001796 if (!ret) {
1797 struct btrfs_bio_stripe *stripe = multi->stripes;
1798 int i;
1799
Liu Hui1f3c79a2009-01-05 15:57:51 -05001800
1801 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001802 if (!stripe->dev->can_discard)
1803 continue;
1804
Li Dongyang5378e602011-03-24 10:24:27 +00001805 ret = btrfs_issue_discard(stripe->dev->bdev,
1806 stripe->physical,
1807 stripe->length);
1808 if (!ret)
1809 discarded_bytes += stripe->length;
1810 else if (ret != -EOPNOTSUPP)
1811 break;
Josef Bacikd5e20032011-08-04 14:52:27 +00001812
1813 /*
1814 * Just in case we get back EOPNOTSUPP for some reason,
1815 * just ignore the return value so we don't screw up
1816 * people calling discard_extent.
1817 */
1818 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001819 }
1820 kfree(multi);
1821 }
Li Dongyang5378e602011-03-24 10:24:27 +00001822
1823 if (actual_bytes)
1824 *actual_bytes = discarded_bytes;
1825
Liu Hui1f3c79a2009-01-05 15:57:51 -05001826
1827 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001828}
1829
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001830int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1831 struct btrfs_root *root,
1832 u64 bytenr, u64 num_bytes, u64 parent,
1833 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001834{
1835 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001836 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1837 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001838
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001839 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1840 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1841 parent, root_objectid, (int)owner,
1842 BTRFS_ADD_DELAYED_REF, NULL);
1843 } else {
1844 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1845 parent, root_objectid, owner, offset,
1846 BTRFS_ADD_DELAYED_REF, NULL);
1847 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001848 return ret;
1849}
1850
Chris Mason925baed2008-06-25 16:01:30 -04001851static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001852 struct btrfs_root *root,
1853 u64 bytenr, u64 num_bytes,
1854 u64 parent, u64 root_objectid,
1855 u64 owner, u64 offset, int refs_to_add,
1856 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001857{
Chris Mason5caf2a02007-04-02 11:20:42 -04001858 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001859 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001860 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001861 u64 refs;
1862 int ret;
1863 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001864
Chris Mason5caf2a02007-04-02 11:20:42 -04001865 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001866 if (!path)
1867 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001868
Chris Mason3c12ac72008-04-21 12:01:38 -04001869 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001870 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001871 /* this will setup the path even if it fails to insert the back ref */
1872 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1873 path, bytenr, num_bytes, parent,
1874 root_objectid, owner, offset,
1875 refs_to_add, extent_op);
1876 if (ret == 0)
1877 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001878
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001879 if (ret != -EAGAIN) {
1880 err = ret;
1881 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001882 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001883
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001884 leaf = path->nodes[0];
1885 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1886 refs = btrfs_extent_refs(leaf, item);
1887 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1888 if (extent_op)
1889 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001890
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001891 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001892 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001893
Chris Mason3c12ac72008-04-21 12:01:38 -04001894 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001895 path->leave_spinning = 1;
1896
Chris Mason56bec292009-03-13 10:10:06 -04001897 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001898 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001899 path, bytenr, parent, root_objectid,
1900 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001901 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001902out:
Chris Mason74493f72007-12-11 09:25:06 -05001903 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001904 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001905}
1906
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001907static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1908 struct btrfs_root *root,
1909 struct btrfs_delayed_ref_node *node,
1910 struct btrfs_delayed_extent_op *extent_op,
1911 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001912{
Chris Mason56bec292009-03-13 10:10:06 -04001913 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001914 struct btrfs_delayed_data_ref *ref;
1915 struct btrfs_key ins;
1916 u64 parent = 0;
1917 u64 ref_root = 0;
1918 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001919
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001920 ins.objectid = node->bytenr;
1921 ins.offset = node->num_bytes;
1922 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001923
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001924 ref = btrfs_delayed_node_to_data_ref(node);
1925 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1926 parent = ref->parent;
1927 else
1928 ref_root = ref->root;
1929
1930 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1931 if (extent_op) {
1932 BUG_ON(extent_op->update_key);
1933 flags |= extent_op->flags_to_set;
1934 }
1935 ret = alloc_reserved_file_extent(trans, root,
1936 parent, ref_root, flags,
1937 ref->objectid, ref->offset,
1938 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001939 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1940 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1941 node->num_bytes, parent,
1942 ref_root, ref->objectid,
1943 ref->offset, node->ref_mod,
1944 extent_op);
1945 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1946 ret = __btrfs_free_extent(trans, root, node->bytenr,
1947 node->num_bytes, parent,
1948 ref_root, ref->objectid,
1949 ref->offset, node->ref_mod,
1950 extent_op);
1951 } else {
1952 BUG();
1953 }
Chris Mason56bec292009-03-13 10:10:06 -04001954 return ret;
1955}
1956
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001957static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1958 struct extent_buffer *leaf,
1959 struct btrfs_extent_item *ei)
1960{
1961 u64 flags = btrfs_extent_flags(leaf, ei);
1962 if (extent_op->update_flags) {
1963 flags |= extent_op->flags_to_set;
1964 btrfs_set_extent_flags(leaf, ei, flags);
1965 }
1966
1967 if (extent_op->update_key) {
1968 struct btrfs_tree_block_info *bi;
1969 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1970 bi = (struct btrfs_tree_block_info *)(ei + 1);
1971 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1972 }
1973}
1974
1975static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1976 struct btrfs_root *root,
1977 struct btrfs_delayed_ref_node *node,
1978 struct btrfs_delayed_extent_op *extent_op)
1979{
1980 struct btrfs_key key;
1981 struct btrfs_path *path;
1982 struct btrfs_extent_item *ei;
1983 struct extent_buffer *leaf;
1984 u32 item_size;
1985 int ret;
1986 int err = 0;
1987
1988 path = btrfs_alloc_path();
1989 if (!path)
1990 return -ENOMEM;
1991
1992 key.objectid = node->bytenr;
1993 key.type = BTRFS_EXTENT_ITEM_KEY;
1994 key.offset = node->num_bytes;
1995
1996 path->reada = 1;
1997 path->leave_spinning = 1;
1998 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1999 path, 0, 1);
2000 if (ret < 0) {
2001 err = ret;
2002 goto out;
2003 }
2004 if (ret > 0) {
2005 err = -EIO;
2006 goto out;
2007 }
2008
2009 leaf = path->nodes[0];
2010 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2011#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2012 if (item_size < sizeof(*ei)) {
2013 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2014 path, (u64)-1, 0);
2015 if (ret < 0) {
2016 err = ret;
2017 goto out;
2018 }
2019 leaf = path->nodes[0];
2020 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2021 }
2022#endif
2023 BUG_ON(item_size < sizeof(*ei));
2024 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2025 __run_delayed_extent_op(extent_op, leaf, ei);
2026
2027 btrfs_mark_buffer_dirty(leaf);
2028out:
2029 btrfs_free_path(path);
2030 return err;
2031}
2032
2033static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2034 struct btrfs_root *root,
2035 struct btrfs_delayed_ref_node *node,
2036 struct btrfs_delayed_extent_op *extent_op,
2037 int insert_reserved)
2038{
2039 int ret = 0;
2040 struct btrfs_delayed_tree_ref *ref;
2041 struct btrfs_key ins;
2042 u64 parent = 0;
2043 u64 ref_root = 0;
2044
2045 ins.objectid = node->bytenr;
2046 ins.offset = node->num_bytes;
2047 ins.type = BTRFS_EXTENT_ITEM_KEY;
2048
2049 ref = btrfs_delayed_node_to_tree_ref(node);
2050 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2051 parent = ref->parent;
2052 else
2053 ref_root = ref->root;
2054
2055 BUG_ON(node->ref_mod != 1);
2056 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2057 BUG_ON(!extent_op || !extent_op->update_flags ||
2058 !extent_op->update_key);
2059 ret = alloc_reserved_tree_block(trans, root,
2060 parent, ref_root,
2061 extent_op->flags_to_set,
2062 &extent_op->key,
2063 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2065 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2066 node->num_bytes, parent, ref_root,
2067 ref->level, 0, 1, extent_op);
2068 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2069 ret = __btrfs_free_extent(trans, root, node->bytenr,
2070 node->num_bytes, parent, ref_root,
2071 ref->level, 0, 1, extent_op);
2072 } else {
2073 BUG();
2074 }
2075 return ret;
2076}
2077
Chris Mason56bec292009-03-13 10:10:06 -04002078/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002079static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2080 struct btrfs_root *root,
2081 struct btrfs_delayed_ref_node *node,
2082 struct btrfs_delayed_extent_op *extent_op,
2083 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002084{
Josef Bacikeb099672009-02-12 09:27:38 -05002085 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002087 struct btrfs_delayed_ref_head *head;
2088 /*
2089 * we've hit the end of the chain and we were supposed
2090 * to insert this extent into the tree. But, it got
2091 * deleted before we ever needed to insert it, so all
2092 * we have to do is clean up the accounting
2093 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002094 BUG_ON(extent_op);
2095 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002096 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002097 btrfs_pin_extent(root, node->bytenr,
2098 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002099 if (head->is_data) {
2100 ret = btrfs_del_csums(trans, root,
2101 node->bytenr,
2102 node->num_bytes);
2103 BUG_ON(ret);
2104 }
Chris Mason56bec292009-03-13 10:10:06 -04002105 }
Chris Mason56bec292009-03-13 10:10:06 -04002106 mutex_unlock(&head->mutex);
2107 return 0;
2108 }
Josef Bacikeb099672009-02-12 09:27:38 -05002109
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002110 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2111 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2112 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2113 insert_reserved);
2114 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2115 node->type == BTRFS_SHARED_DATA_REF_KEY)
2116 ret = run_delayed_data_ref(trans, root, node, extent_op,
2117 insert_reserved);
2118 else
2119 BUG();
2120 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002121}
2122
Chris Mason56bec292009-03-13 10:10:06 -04002123static noinline struct btrfs_delayed_ref_node *
2124select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002125{
Chris Mason56bec292009-03-13 10:10:06 -04002126 struct rb_node *node;
2127 struct btrfs_delayed_ref_node *ref;
2128 int action = BTRFS_ADD_DELAYED_REF;
2129again:
2130 /*
2131 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2132 * this prevents ref count from going down to zero when
2133 * there still are pending delayed ref.
2134 */
2135 node = rb_prev(&head->node.rb_node);
2136 while (1) {
2137 if (!node)
2138 break;
2139 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2140 rb_node);
2141 if (ref->bytenr != head->node.bytenr)
2142 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002143 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002144 return ref;
2145 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002146 }
Chris Mason56bec292009-03-13 10:10:06 -04002147 if (action == BTRFS_ADD_DELAYED_REF) {
2148 action = BTRFS_DROP_DELAYED_REF;
2149 goto again;
2150 }
2151 return NULL;
2152}
2153
Chris Masonc3e69d52009-03-13 10:17:05 -04002154static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2155 struct btrfs_root *root,
2156 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002157{
Chris Mason56bec292009-03-13 10:10:06 -04002158 struct btrfs_delayed_ref_root *delayed_refs;
2159 struct btrfs_delayed_ref_node *ref;
2160 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002161 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002162 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002163 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002164 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002165
2166 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002167 while (1) {
2168 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002169 /* pick a new head ref from the cluster list */
2170 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002171 break;
Chris Mason56bec292009-03-13 10:10:06 -04002172
Chris Masonc3e69d52009-03-13 10:17:05 -04002173 locked_ref = list_entry(cluster->next,
2174 struct btrfs_delayed_ref_head, cluster);
2175
2176 /* grab the lock that says we are going to process
2177 * all the refs for this head */
2178 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2179
2180 /*
2181 * we may have dropped the spin lock to get the head
2182 * mutex lock, and that might have given someone else
2183 * time to free the head. If that's true, it has been
2184 * removed from our list and we can move on.
2185 */
2186 if (ret == -EAGAIN) {
2187 locked_ref = NULL;
2188 count++;
2189 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002190 }
2191 }
2192
2193 /*
2194 * record the must insert reserved flag before we
2195 * drop the spin lock.
2196 */
2197 must_insert_reserved = locked_ref->must_insert_reserved;
2198 locked_ref->must_insert_reserved = 0;
2199
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002200 extent_op = locked_ref->extent_op;
2201 locked_ref->extent_op = NULL;
2202
Chris Mason56bec292009-03-13 10:10:06 -04002203 /*
2204 * locked_ref is the head node, so we have to go one
2205 * node back for any delayed ref updates
2206 */
Chris Mason56bec292009-03-13 10:10:06 -04002207 ref = select_delayed_ref(locked_ref);
2208 if (!ref) {
2209 /* All delayed refs have been processed, Go ahead
2210 * and send the head node to run_one_delayed_ref,
2211 * so that any accounting fixes can happen
2212 */
2213 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002214
2215 if (extent_op && must_insert_reserved) {
2216 kfree(extent_op);
2217 extent_op = NULL;
2218 }
2219
2220 if (extent_op) {
2221 spin_unlock(&delayed_refs->lock);
2222
2223 ret = run_delayed_extent_op(trans, root,
2224 ref, extent_op);
2225 BUG_ON(ret);
2226 kfree(extent_op);
2227
2228 cond_resched();
2229 spin_lock(&delayed_refs->lock);
2230 continue;
2231 }
2232
Chris Masonc3e69d52009-03-13 10:17:05 -04002233 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002234 locked_ref = NULL;
2235 }
2236
2237 ref->in_tree = 0;
2238 rb_erase(&ref->rb_node, &delayed_refs->root);
2239 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002240
Chris Mason56bec292009-03-13 10:10:06 -04002241 spin_unlock(&delayed_refs->lock);
2242
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002243 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002244 must_insert_reserved);
2245 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002246
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002247 btrfs_put_delayed_ref(ref);
2248 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002249 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002250
Chris Mason1887be62009-03-13 10:11:24 -04002251 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002252 spin_lock(&delayed_refs->lock);
2253 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002254 return count;
2255}
2256
2257/*
2258 * this starts processing the delayed reference count updates and
2259 * extent insertions we have queued up so far. count can be
2260 * 0, which means to process everything in the tree at the start
2261 * of the run (but not newly added entries), or it can be some target
2262 * number you'd like to process.
2263 */
2264int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2265 struct btrfs_root *root, unsigned long count)
2266{
2267 struct rb_node *node;
2268 struct btrfs_delayed_ref_root *delayed_refs;
2269 struct btrfs_delayed_ref_node *ref;
2270 struct list_head cluster;
2271 int ret;
2272 int run_all = count == (unsigned long)-1;
2273 int run_most = 0;
2274
2275 if (root == root->fs_info->extent_root)
2276 root = root->fs_info->tree_root;
2277
2278 delayed_refs = &trans->transaction->delayed_refs;
2279 INIT_LIST_HEAD(&cluster);
2280again:
2281 spin_lock(&delayed_refs->lock);
2282 if (count == 0) {
2283 count = delayed_refs->num_entries * 2;
2284 run_most = 1;
2285 }
2286 while (1) {
2287 if (!(run_all || run_most) &&
2288 delayed_refs->num_heads_ready < 64)
2289 break;
2290
2291 /*
2292 * go find something we can process in the rbtree. We start at
2293 * the beginning of the tree, and then build a cluster
2294 * of refs to process starting at the first one we are able to
2295 * lock
2296 */
2297 ret = btrfs_find_ref_cluster(trans, &cluster,
2298 delayed_refs->run_delayed_start);
2299 if (ret)
2300 break;
2301
2302 ret = run_clustered_refs(trans, root, &cluster);
2303 BUG_ON(ret < 0);
2304
2305 count -= min_t(unsigned long, ret, count);
2306
2307 if (count == 0)
2308 break;
2309 }
2310
Chris Mason56bec292009-03-13 10:10:06 -04002311 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002312 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002313 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002314 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002315 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002316
2317 while (node) {
2318 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2319 rb_node);
2320 if (btrfs_delayed_ref_is_head(ref)) {
2321 struct btrfs_delayed_ref_head *head;
2322
2323 head = btrfs_delayed_node_to_head(ref);
2324 atomic_inc(&ref->refs);
2325
2326 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002327 /*
2328 * Mutex was contended, block until it's
2329 * released and try again
2330 */
Chris Mason56bec292009-03-13 10:10:06 -04002331 mutex_lock(&head->mutex);
2332 mutex_unlock(&head->mutex);
2333
2334 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002335 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002336 goto again;
2337 }
2338 node = rb_next(node);
2339 }
2340 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002341 schedule_timeout(1);
2342 goto again;
2343 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002344out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002345 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002346 return 0;
2347}
2348
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002349int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2350 struct btrfs_root *root,
2351 u64 bytenr, u64 num_bytes, u64 flags,
2352 int is_data)
2353{
2354 struct btrfs_delayed_extent_op *extent_op;
2355 int ret;
2356
2357 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2358 if (!extent_op)
2359 return -ENOMEM;
2360
2361 extent_op->flags_to_set = flags;
2362 extent_op->update_flags = 1;
2363 extent_op->update_key = 0;
2364 extent_op->is_data = is_data ? 1 : 0;
2365
2366 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2367 if (ret)
2368 kfree(extent_op);
2369 return ret;
2370}
2371
2372static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2373 struct btrfs_root *root,
2374 struct btrfs_path *path,
2375 u64 objectid, u64 offset, u64 bytenr)
2376{
2377 struct btrfs_delayed_ref_head *head;
2378 struct btrfs_delayed_ref_node *ref;
2379 struct btrfs_delayed_data_ref *data_ref;
2380 struct btrfs_delayed_ref_root *delayed_refs;
2381 struct rb_node *node;
2382 int ret = 0;
2383
2384 ret = -ENOENT;
2385 delayed_refs = &trans->transaction->delayed_refs;
2386 spin_lock(&delayed_refs->lock);
2387 head = btrfs_find_delayed_ref_head(trans, bytenr);
2388 if (!head)
2389 goto out;
2390
2391 if (!mutex_trylock(&head->mutex)) {
2392 atomic_inc(&head->node.refs);
2393 spin_unlock(&delayed_refs->lock);
2394
David Sterbab3b4aa72011-04-21 01:20:15 +02002395 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002396
David Sterba8cc33e52011-05-02 15:29:25 +02002397 /*
2398 * Mutex was contended, block until it's released and let
2399 * caller try again
2400 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002401 mutex_lock(&head->mutex);
2402 mutex_unlock(&head->mutex);
2403 btrfs_put_delayed_ref(&head->node);
2404 return -EAGAIN;
2405 }
2406
2407 node = rb_prev(&head->node.rb_node);
2408 if (!node)
2409 goto out_unlock;
2410
2411 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2412
2413 if (ref->bytenr != bytenr)
2414 goto out_unlock;
2415
2416 ret = 1;
2417 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2418 goto out_unlock;
2419
2420 data_ref = btrfs_delayed_node_to_data_ref(ref);
2421
2422 node = rb_prev(node);
2423 if (node) {
2424 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2425 if (ref->bytenr == bytenr)
2426 goto out_unlock;
2427 }
2428
2429 if (data_ref->root != root->root_key.objectid ||
2430 data_ref->objectid != objectid || data_ref->offset != offset)
2431 goto out_unlock;
2432
2433 ret = 0;
2434out_unlock:
2435 mutex_unlock(&head->mutex);
2436out:
2437 spin_unlock(&delayed_refs->lock);
2438 return ret;
2439}
2440
2441static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2442 struct btrfs_root *root,
2443 struct btrfs_path *path,
2444 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002445{
2446 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002447 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002448 struct btrfs_extent_data_ref *ref;
2449 struct btrfs_extent_inline_ref *iref;
2450 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002451 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002452 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002453 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002454
Chris Masonbe20aa92007-12-17 20:14:01 -05002455 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002456 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002457 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002458
Chris Masonbe20aa92007-12-17 20:14:01 -05002459 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2460 if (ret < 0)
2461 goto out;
2462 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002463
2464 ret = -ENOENT;
2465 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002466 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002467
Zheng Yan31840ae2008-09-23 13:14:14 -04002468 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002469 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002470 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002471
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002472 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002473 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002474
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002475 ret = 1;
2476 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2477#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2478 if (item_size < sizeof(*ei)) {
2479 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2480 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002481 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002482#endif
2483 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2484
2485 if (item_size != sizeof(*ei) +
2486 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2487 goto out;
2488
2489 if (btrfs_extent_generation(leaf, ei) <=
2490 btrfs_root_last_snapshot(&root->root_item))
2491 goto out;
2492
2493 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2494 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2495 BTRFS_EXTENT_DATA_REF_KEY)
2496 goto out;
2497
2498 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2499 if (btrfs_extent_refs(leaf, ei) !=
2500 btrfs_extent_data_ref_count(leaf, ref) ||
2501 btrfs_extent_data_ref_root(leaf, ref) !=
2502 root->root_key.objectid ||
2503 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2504 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2505 goto out;
2506
Yan Zhengf321e492008-07-30 09:26:11 -04002507 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002508out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002509 return ret;
2510}
2511
2512int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2513 struct btrfs_root *root,
2514 u64 objectid, u64 offset, u64 bytenr)
2515{
2516 struct btrfs_path *path;
2517 int ret;
2518 int ret2;
2519
2520 path = btrfs_alloc_path();
2521 if (!path)
2522 return -ENOENT;
2523
2524 do {
2525 ret = check_committed_ref(trans, root, path, objectid,
2526 offset, bytenr);
2527 if (ret && ret != -ENOENT)
2528 goto out;
2529
2530 ret2 = check_delayed_ref(trans, root, path, objectid,
2531 offset, bytenr);
2532 } while (ret2 == -EAGAIN);
2533
2534 if (ret2 && ret2 != -ENOENT) {
2535 ret = ret2;
2536 goto out;
2537 }
2538
2539 if (ret != -ENOENT || ret2 != -ENOENT)
2540 ret = 0;
2541out:
Yan Zhengf321e492008-07-30 09:26:11 -04002542 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002543 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2544 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002545 return ret;
2546}
2547
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002548static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002549 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002550 struct extent_buffer *buf,
2551 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002552{
2553 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002554 u64 num_bytes;
2555 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002556 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002557 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002558 struct btrfs_key key;
2559 struct btrfs_file_extent_item *fi;
2560 int i;
2561 int level;
2562 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002563 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002564 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002565
2566 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002567 nritems = btrfs_header_nritems(buf);
2568 level = btrfs_header_level(buf);
2569
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002570 if (!root->ref_cows && level == 0)
2571 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002572
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002573 if (inc)
2574 process_func = btrfs_inc_extent_ref;
2575 else
2576 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002577
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002578 if (full_backref)
2579 parent = buf->start;
2580 else
2581 parent = 0;
2582
Zheng Yan31840ae2008-09-23 13:14:14 -04002583 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002584 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002585 btrfs_item_key_to_cpu(buf, &key, i);
2586 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002587 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002588 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002589 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002590 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002591 BTRFS_FILE_EXTENT_INLINE)
2592 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002593 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2594 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002595 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002596
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002597 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2598 key.offset -= btrfs_file_extent_offset(buf, fi);
2599 ret = process_func(trans, root, bytenr, num_bytes,
2600 parent, ref_root, key.objectid,
2601 key.offset);
2602 if (ret)
2603 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002604 } else {
2605 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002606 num_bytes = btrfs_level_size(root, level - 1);
2607 ret = process_func(trans, root, bytenr, num_bytes,
2608 parent, ref_root, level - 1, 0);
2609 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002610 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002611 }
2612 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002613 return 0;
2614fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002615 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002616 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002617}
2618
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002619int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2620 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002621{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002622 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2623}
Zheng Yan31840ae2008-09-23 13:14:14 -04002624
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002625int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2626 struct extent_buffer *buf, int full_backref)
2627{
2628 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002629}
2630
Chris Mason9078a3e2007-04-26 16:46:15 -04002631static int write_one_cache_group(struct btrfs_trans_handle *trans,
2632 struct btrfs_root *root,
2633 struct btrfs_path *path,
2634 struct btrfs_block_group_cache *cache)
2635{
2636 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002637 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002638 unsigned long bi;
2639 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002640
Chris Mason9078a3e2007-04-26 16:46:15 -04002641 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002642 if (ret < 0)
2643 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002644 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002645
2646 leaf = path->nodes[0];
2647 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2648 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2649 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002650 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002651fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002652 if (ret)
2653 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002654 return 0;
2655
2656}
2657
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002658static struct btrfs_block_group_cache *
2659next_block_group(struct btrfs_root *root,
2660 struct btrfs_block_group_cache *cache)
2661{
2662 struct rb_node *node;
2663 spin_lock(&root->fs_info->block_group_cache_lock);
2664 node = rb_next(&cache->cache_node);
2665 btrfs_put_block_group(cache);
2666 if (node) {
2667 cache = rb_entry(node, struct btrfs_block_group_cache,
2668 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002669 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002670 } else
2671 cache = NULL;
2672 spin_unlock(&root->fs_info->block_group_cache_lock);
2673 return cache;
2674}
2675
Josef Bacik0af3d002010-06-21 14:48:16 -04002676static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2677 struct btrfs_trans_handle *trans,
2678 struct btrfs_path *path)
2679{
2680 struct btrfs_root *root = block_group->fs_info->tree_root;
2681 struct inode *inode = NULL;
2682 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002683 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002684 int num_pages = 0;
2685 int retries = 0;
2686 int ret = 0;
2687
2688 /*
2689 * If this block group is smaller than 100 megs don't bother caching the
2690 * block group.
2691 */
2692 if (block_group->key.offset < (100 * 1024 * 1024)) {
2693 spin_lock(&block_group->lock);
2694 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2695 spin_unlock(&block_group->lock);
2696 return 0;
2697 }
2698
2699again:
2700 inode = lookup_free_space_inode(root, block_group, path);
2701 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2702 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002703 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002704 goto out;
2705 }
2706
2707 if (IS_ERR(inode)) {
2708 BUG_ON(retries);
2709 retries++;
2710
2711 if (block_group->ro)
2712 goto out_free;
2713
2714 ret = create_free_space_inode(root, trans, block_group, path);
2715 if (ret)
2716 goto out_free;
2717 goto again;
2718 }
2719
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002720 /* We've already setup this transaction, go ahead and exit */
2721 if (block_group->cache_generation == trans->transid &&
2722 i_size_read(inode)) {
2723 dcs = BTRFS_DC_SETUP;
2724 goto out_put;
2725 }
2726
Josef Bacik0af3d002010-06-21 14:48:16 -04002727 /*
2728 * We want to set the generation to 0, that way if anything goes wrong
2729 * from here on out we know not to trust this cache when we load up next
2730 * time.
2731 */
2732 BTRFS_I(inode)->generation = 0;
2733 ret = btrfs_update_inode(trans, root, inode);
2734 WARN_ON(ret);
2735
2736 if (i_size_read(inode) > 0) {
2737 ret = btrfs_truncate_free_space_cache(root, trans, path,
2738 inode);
2739 if (ret)
2740 goto out_put;
2741 }
2742
2743 spin_lock(&block_group->lock);
2744 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002745 /* We're not cached, don't bother trying to write stuff out */
2746 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002747 spin_unlock(&block_group->lock);
2748 goto out_put;
2749 }
2750 spin_unlock(&block_group->lock);
2751
2752 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2753 if (!num_pages)
2754 num_pages = 1;
2755
2756 /*
2757 * Just to make absolutely sure we have enough space, we're going to
2758 * preallocate 12 pages worth of space for each block group. In
2759 * practice we ought to use at most 8, but we need extra space so we can
2760 * add our header and have a terminator between the extents and the
2761 * bitmaps.
2762 */
2763 num_pages *= 16;
2764 num_pages *= PAGE_CACHE_SIZE;
2765
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002766 ret = btrfs_check_data_free_space(inode, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04002767 if (ret)
2768 goto out_put;
2769
2770 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2771 num_pages, num_pages,
2772 &alloc_hint);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002773 if (!ret)
Josef Bacik2b209822010-12-03 13:17:53 -05002774 dcs = BTRFS_DC_SETUP;
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002775 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04002776
Josef Bacik0af3d002010-06-21 14:48:16 -04002777out_put:
2778 iput(inode);
2779out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02002780 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002781out:
2782 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002783 if (!ret)
2784 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05002785 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002786 spin_unlock(&block_group->lock);
2787
2788 return ret;
2789}
2790
Chris Mason96b51792007-10-15 16:15:19 -04002791int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2792 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002793{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002794 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002795 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002796 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002797 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002798
2799 path = btrfs_alloc_path();
2800 if (!path)
2801 return -ENOMEM;
2802
Josef Bacik0af3d002010-06-21 14:48:16 -04002803again:
2804 while (1) {
2805 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2806 while (cache) {
2807 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2808 break;
2809 cache = next_block_group(root, cache);
2810 }
2811 if (!cache) {
2812 if (last == 0)
2813 break;
2814 last = 0;
2815 continue;
2816 }
2817 err = cache_save_setup(cache, trans, path);
2818 last = cache->key.objectid + cache->key.offset;
2819 btrfs_put_block_group(cache);
2820 }
2821
Chris Masond3977122009-01-05 21:25:51 -05002822 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002823 if (last == 0) {
2824 err = btrfs_run_delayed_refs(trans, root,
2825 (unsigned long)-1);
2826 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002827 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002828
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002829 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2830 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002831 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2832 btrfs_put_block_group(cache);
2833 goto again;
2834 }
2835
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002836 if (cache->dirty)
2837 break;
2838 cache = next_block_group(root, cache);
2839 }
2840 if (!cache) {
2841 if (last == 0)
2842 break;
2843 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002844 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002845 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002846
Josef Bacik0cb59c92010-07-02 12:14:14 -04002847 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2848 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002849 cache->dirty = 0;
2850 last = cache->key.objectid + cache->key.offset;
2851
2852 err = write_one_cache_group(trans, root, path, cache);
2853 BUG_ON(err);
2854 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002855 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002856
Josef Bacik0cb59c92010-07-02 12:14:14 -04002857 while (1) {
2858 /*
2859 * I don't think this is needed since we're just marking our
2860 * preallocated extent as written, but just in case it can't
2861 * hurt.
2862 */
2863 if (last == 0) {
2864 err = btrfs_run_delayed_refs(trans, root,
2865 (unsigned long)-1);
2866 BUG_ON(err);
2867 }
2868
2869 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2870 while (cache) {
2871 /*
2872 * Really this shouldn't happen, but it could if we
2873 * couldn't write the entire preallocated extent and
2874 * splitting the extent resulted in a new block.
2875 */
2876 if (cache->dirty) {
2877 btrfs_put_block_group(cache);
2878 goto again;
2879 }
2880 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2881 break;
2882 cache = next_block_group(root, cache);
2883 }
2884 if (!cache) {
2885 if (last == 0)
2886 break;
2887 last = 0;
2888 continue;
2889 }
2890
2891 btrfs_write_out_cache(root, trans, cache, path);
2892
2893 /*
2894 * If we didn't have an error then the cache state is still
2895 * NEED_WRITE, so we can set it to WRITTEN.
2896 */
2897 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2898 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2899 last = cache->key.objectid + cache->key.offset;
2900 btrfs_put_block_group(cache);
2901 }
2902
Chris Mason9078a3e2007-04-26 16:46:15 -04002903 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002904 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002905}
2906
Yan Zhengd2fb3432008-12-11 16:30:39 -05002907int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2908{
2909 struct btrfs_block_group_cache *block_group;
2910 int readonly = 0;
2911
2912 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2913 if (!block_group || block_group->ro)
2914 readonly = 1;
2915 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002916 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002917 return readonly;
2918}
2919
Chris Mason593060d2008-03-25 16:50:33 -04002920static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2921 u64 total_bytes, u64 bytes_used,
2922 struct btrfs_space_info **space_info)
2923{
2924 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002925 int i;
2926 int factor;
2927
2928 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2929 BTRFS_BLOCK_GROUP_RAID10))
2930 factor = 2;
2931 else
2932 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002933
2934 found = __find_space_info(info, flags);
2935 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002936 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002937 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002938 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002939 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002940 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002941 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002942 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002943 *space_info = found;
2944 return 0;
2945 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002946 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002947 if (!found)
2948 return -ENOMEM;
2949
Yan, Zhengb742bb82010-05-16 10:46:24 -04002950 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2951 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04002952 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002953 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04002954 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2955 BTRFS_BLOCK_GROUP_SYSTEM |
2956 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04002957 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002958 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002959 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002960 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002961 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002962 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002963 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002964 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002965 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04002966 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04002967 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04002968 found->flush = 0;
2969 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04002970 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002971 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04002972 return 0;
2973}
2974
Chris Mason8790d502008-04-03 16:29:03 -04002975static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2976{
2977 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002978 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002979 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002980 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002981 if (extra_flags) {
2982 if (flags & BTRFS_BLOCK_GROUP_DATA)
2983 fs_info->avail_data_alloc_bits |= extra_flags;
2984 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2985 fs_info->avail_metadata_alloc_bits |= extra_flags;
2986 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2987 fs_info->avail_system_alloc_bits |= extra_flags;
2988 }
2989}
Chris Mason593060d2008-03-25 16:50:33 -04002990
Yan Zheng2b820322008-11-17 21:11:30 -05002991u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002992{
Chris Masoncd02dca2010-12-13 14:56:23 -05002993 /*
2994 * we add in the count of missing devices because we want
2995 * to make sure that any RAID levels on a degraded FS
2996 * continue to be honored.
2997 */
2998 u64 num_devices = root->fs_info->fs_devices->rw_devices +
2999 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04003000
3001 if (num_devices == 1)
3002 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3003 if (num_devices < 4)
3004 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3005
Chris Masonec44a352008-04-28 15:29:52 -04003006 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3007 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003008 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003009 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003010 }
Chris Masonec44a352008-04-28 15:29:52 -04003011
3012 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003013 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003014 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003015 }
Chris Masonec44a352008-04-28 15:29:52 -04003016
3017 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3018 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3019 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3020 (flags & BTRFS_BLOCK_GROUP_DUP)))
3021 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3022 return flags;
3023}
3024
Yan, Zhengb742bb82010-05-16 10:46:24 -04003025static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003026{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003027 if (flags & BTRFS_BLOCK_GROUP_DATA)
3028 flags |= root->fs_info->avail_data_alloc_bits &
3029 root->fs_info->data_alloc_profile;
3030 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3031 flags |= root->fs_info->avail_system_alloc_bits &
3032 root->fs_info->system_alloc_profile;
3033 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3034 flags |= root->fs_info->avail_metadata_alloc_bits &
3035 root->fs_info->metadata_alloc_profile;
3036 return btrfs_reduce_alloc_profile(root, flags);
3037}
Josef Bacik6a632092009-02-20 11:00:09 -05003038
Miao Xie6d07bce2011-01-05 10:07:31 +00003039u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003040{
3041 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003042
Yan, Zhengb742bb82010-05-16 10:46:24 -04003043 if (data)
3044 flags = BTRFS_BLOCK_GROUP_DATA;
3045 else if (root == root->fs_info->chunk_root)
3046 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3047 else
3048 flags = BTRFS_BLOCK_GROUP_METADATA;
3049
3050 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003051}
3052
3053void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3054{
Josef Bacik6a632092009-02-20 11:00:09 -05003055 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003056 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003057}
3058
3059/*
3060 * This will check the space that the inode allocates from to make sure we have
3061 * enough space for bytes.
3062 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003063int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003064{
3065 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003066 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003067 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003068 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003069
3070 /* make sure bytes are sectorsize aligned */
3071 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3072
Li Zefan82d59022011-04-20 10:33:24 +08003073 if (root == root->fs_info->tree_root ||
3074 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003075 alloc_chunk = 0;
3076 committed = 1;
3077 }
3078
Josef Bacik6a632092009-02-20 11:00:09 -05003079 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003080 if (!data_sinfo)
3081 goto alloc;
3082
Josef Bacik6a632092009-02-20 11:00:09 -05003083again:
3084 /* make sure we have enough space to handle the data first */
3085 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003086 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3087 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3088 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003089
3090 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003091 struct btrfs_trans_handle *trans;
3092
Josef Bacik6a632092009-02-20 11:00:09 -05003093 /*
3094 * if we don't have enough free bytes in this space then we need
3095 * to alloc a new chunk.
3096 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003097 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003098 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003099
Chris Mason0e4f8f82011-04-15 16:05:44 -04003100 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003101 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003102alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003103 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003104 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003105 if (IS_ERR(trans))
3106 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003107
3108 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3109 bytes + 2 * 1024 * 1024,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003110 alloc_target,
3111 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003112 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003113 if (ret < 0) {
3114 if (ret != -ENOSPC)
3115 return ret;
3116 else
3117 goto commit_trans;
3118 }
Chris Mason33b4d472009-09-22 14:45:50 -04003119
3120 if (!data_sinfo) {
3121 btrfs_set_inode_space_info(root, inode);
3122 data_sinfo = BTRFS_I(inode)->space_info;
3123 }
Josef Bacik6a632092009-02-20 11:00:09 -05003124 goto again;
3125 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003126
3127 /*
3128 * If we have less pinned bytes than we want to allocate then
3129 * don't bother committing the transaction, it won't help us.
3130 */
3131 if (data_sinfo->bytes_pinned < bytes)
3132 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003133 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003134
3135 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003136commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003137 if (!committed &&
3138 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003139 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003140 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003141 if (IS_ERR(trans))
3142 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003143 ret = btrfs_commit_transaction(trans, root);
3144 if (ret)
3145 return ret;
3146 goto again;
3147 }
3148
Josef Bacik6a632092009-02-20 11:00:09 -05003149 return -ENOSPC;
3150 }
3151 data_sinfo->bytes_may_use += bytes;
Josef Bacik6a632092009-02-20 11:00:09 -05003152 spin_unlock(&data_sinfo->lock);
3153
Josef Bacik9ed74f22009-09-11 16:12:44 -04003154 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003155}
3156
3157/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003158 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003159 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003160void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003161{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003162 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003163 struct btrfs_space_info *data_sinfo;
3164
3165 /* make sure bytes are sectorsize aligned */
3166 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3167
3168 data_sinfo = BTRFS_I(inode)->space_info;
3169 spin_lock(&data_sinfo->lock);
3170 data_sinfo->bytes_may_use -= bytes;
Josef Bacik6a632092009-02-20 11:00:09 -05003171 spin_unlock(&data_sinfo->lock);
3172}
3173
Josef Bacik97e728d2009-04-21 17:40:57 -04003174static void force_metadata_allocation(struct btrfs_fs_info *info)
3175{
3176 struct list_head *head = &info->space_info;
3177 struct btrfs_space_info *found;
3178
3179 rcu_read_lock();
3180 list_for_each_entry_rcu(found, head, list) {
3181 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003182 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003183 }
3184 rcu_read_unlock();
3185}
3186
Chris Masone5bc2452010-10-26 13:37:56 -04003187static int should_alloc_chunk(struct btrfs_root *root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003188 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3189 int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003190{
Josef Bacikfb25e912011-07-26 17:00:46 -04003191 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003192 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003193 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003194 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003195
Chris Mason0e4f8f82011-04-15 16:05:44 -04003196 if (force == CHUNK_ALLOC_FORCE)
3197 return 1;
3198
3199 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003200 * We need to take into account the global rsv because for all intents
3201 * and purposes it's used space. Don't worry about locking the
3202 * global_rsv, it doesn't change except when the transaction commits.
3203 */
3204 num_allocated += global_rsv->size;
3205
3206 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003207 * in limited mode, we want to have some free space up to
3208 * about 1% of the FS size.
3209 */
3210 if (force == CHUNK_ALLOC_LIMITED) {
3211 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3212 thresh = max_t(u64, 64 * 1024 * 1024,
3213 div_factor_fine(thresh, 1));
3214
3215 if (num_bytes - num_allocated < thresh)
3216 return 1;
3217 }
3218
3219 /*
3220 * we have two similar checks here, one based on percentage
3221 * and once based on a hard number of 256MB. The idea
3222 * is that if we have a good amount of free
3223 * room, don't allocate a chunk. A good mount is
3224 * less than 80% utilized of the chunks we have allocated,
3225 * or more than 256MB free
3226 */
3227 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003228 return 0;
3229
Chris Mason0e4f8f82011-04-15 16:05:44 -04003230 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
Yan, Zheng424499d2010-05-16 10:46:25 -04003231 return 0;
3232
Chris Masone5bc2452010-10-26 13:37:56 -04003233 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003234
3235 /* 256MB or 5% of the FS */
Chris Masone5bc2452010-10-26 13:37:56 -04003236 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3237
3238 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003239 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003240 return 1;
3241}
3242
Chris Mason6324fbf2008-03-24 15:01:59 -04003243static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3244 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003245 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003246{
3247 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003248 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003249 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003250 int ret = 0;
3251
Yan Zheng2b820322008-11-17 21:11:30 -05003252 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003253
Chris Mason6324fbf2008-03-24 15:01:59 -04003254 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003255 if (!space_info) {
3256 ret = update_space_info(extent_root->fs_info, flags,
3257 0, 0, &space_info);
3258 BUG_ON(ret);
3259 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003260 BUG_ON(!space_info);
3261
Josef Bacik6d741192011-04-11 20:20:11 -04003262again:
Josef Bacik25179202008-10-29 14:49:05 -04003263 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003264 if (space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003265 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003266 if (space_info->full) {
3267 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003268 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003269 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003270
Chris Mason0e4f8f82011-04-15 16:05:44 -04003271 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003272 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003273 return 0;
3274 } else if (space_info->chunk_alloc) {
3275 wait_for_alloc = 1;
3276 } else {
3277 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003278 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003279
Josef Bacik25179202008-10-29 14:49:05 -04003280 spin_unlock(&space_info->lock);
3281
Josef Bacik6d741192011-04-11 20:20:11 -04003282 mutex_lock(&fs_info->chunk_mutex);
3283
3284 /*
3285 * The chunk_mutex is held throughout the entirety of a chunk
3286 * allocation, so once we've acquired the chunk_mutex we know that the
3287 * other guy is done and we need to recheck and see if we should
3288 * allocate.
3289 */
3290 if (wait_for_alloc) {
3291 mutex_unlock(&fs_info->chunk_mutex);
3292 wait_for_alloc = 0;
3293 goto again;
3294 }
3295
Josef Bacik97e728d2009-04-21 17:40:57 -04003296 /*
Josef Bacik67377732010-09-16 16:19:09 -04003297 * If we have mixed data/metadata chunks we want to make sure we keep
3298 * allocating mixed chunks instead of individual chunks.
3299 */
3300 if (btrfs_mixed_space_info(space_info))
3301 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3302
3303 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003304 * if we're doing a data chunk, go ahead and make sure that
3305 * we keep a reasonable number of metadata chunks allocated in the
3306 * FS as well.
3307 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003308 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003309 fs_info->data_chunk_allocations++;
3310 if (!(fs_info->data_chunk_allocations %
3311 fs_info->metadata_ratio))
3312 force_metadata_allocation(fs_info);
3313 }
3314
Yan Zheng2b820322008-11-17 21:11:30 -05003315 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003316 if (ret < 0 && ret != -ENOSPC)
3317 goto out;
3318
Josef Bacik9ed74f22009-09-11 16:12:44 -04003319 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003320 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003321 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003322 else
3323 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003324
Chris Mason0e4f8f82011-04-15 16:05:44 -04003325 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003326 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003327 spin_unlock(&space_info->lock);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003328out:
Yan Zhengc146afa2008-11-12 14:34:12 -05003329 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003330 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003331}
3332
Yan, Zheng5da9d012010-05-16 10:46:25 -04003333/*
3334 * shrink metadata reservation for delalloc
3335 */
3336static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacikf104d042011-10-14 13:56:58 -04003337 struct btrfs_root *root, u64 to_reclaim,
3338 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003339{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003340 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003341 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003342 u64 reserved;
3343 u64 max_reclaim;
3344 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003345 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003346 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003347 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003348 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003349
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003350 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003351 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003352
3353 smp_mb();
Josef Bacikfb25e912011-07-26 17:00:46 -04003354 reserved = space_info->bytes_may_use;
Chris Mason36e39c42011-03-12 07:08:42 -05003355 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003356
3357 if (reserved == 0)
3358 return 0;
3359
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003360 smp_mb();
3361 if (root->fs_info->delalloc_bytes == 0) {
3362 if (trans)
3363 return 0;
3364 btrfs_wait_ordered_extents(root, 0, 0);
3365 return 0;
3366 }
3367
Yan, Zheng5da9d012010-05-16 10:46:25 -04003368 max_reclaim = min(reserved, to_reclaim);
Josef Bacik877da172011-10-14 14:02:10 -04003369 nr_pages = max_t(unsigned long, nr_pages,
3370 max_reclaim >> PAGE_CACHE_SHIFT);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003371 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003372 /* have the flusher threads jump in and do some IO */
3373 smp_mb();
3374 nr_pages = min_t(unsigned long, nr_pages,
3375 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3376 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003377
Josef Bacik0019f102010-10-15 15:18:40 -04003378 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04003379 if (reserved > space_info->bytes_may_use)
3380 reclaimed += reserved - space_info->bytes_may_use;
3381 reserved = space_info->bytes_may_use;
Josef Bacik0019f102010-10-15 15:18:40 -04003382 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003383
Chris Mason36e39c42011-03-12 07:08:42 -05003384 loops++;
3385
Yan, Zheng5da9d012010-05-16 10:46:25 -04003386 if (reserved == 0 || reclaimed >= max_reclaim)
3387 break;
3388
3389 if (trans && trans->transaction->blocked)
3390 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003391
Josef Bacikf104d042011-10-14 13:56:58 -04003392 if (wait_ordered && !trans) {
3393 btrfs_wait_ordered_extents(root, 0, 0);
3394 } else {
3395 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003396
Josef Bacikf104d042011-10-14 13:56:58 -04003397 /* We were interrupted, exit */
3398 if (time_left)
3399 break;
3400 }
Josef Bacikb1953bc2011-01-21 21:10:01 +00003401
Chris Mason36e39c42011-03-12 07:08:42 -05003402 /* we've kicked the IO a few times, if anything has been freed,
3403 * exit. There is no sense in looping here for a long time
3404 * when we really need to commit the transaction, or there are
3405 * just too many writers without enough free space
3406 */
3407
3408 if (loops > 3) {
3409 smp_mb();
3410 if (progress != space_info->reservation_progress)
3411 break;
3412 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003413
Yan, Zheng5da9d012010-05-16 10:46:25 -04003414 }
Josef Bacikf104d042011-10-14 13:56:58 -04003415
Yan, Zheng5da9d012010-05-16 10:46:25 -04003416 return reclaimed >= to_reclaim;
3417}
3418
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003419/**
3420 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
3421 * @root - the root we're allocating for
3422 * @block_rsv - the block_rsv we're allocating for
3423 * @orig_bytes - the number of bytes we want
3424 * @flush - wether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003425 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003426 * This will reserve orgi_bytes number of bytes from the space info associated
3427 * with the block_rsv. If there is not enough space it will make an attempt to
3428 * flush out space to make room. It will do this by flushing delalloc if
3429 * possible or committing the transaction. If flush is 0 then no attempts to
3430 * regain reservations will be made and this will fail if there is not enough
3431 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003432 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003433static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003434 struct btrfs_block_rsv *block_rsv,
Josef Bacik36ba0222011-10-18 12:15:48 -04003435 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003436{
3437 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003438 struct btrfs_trans_handle *trans;
Josef Bacik2bf64752011-09-26 17:12:22 -04003439 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003440 u64 num_bytes = orig_bytes;
3441 int retries = 0;
3442 int ret = 0;
Josef Bacik38227932010-10-26 12:52:53 -04003443 bool committed = false;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003444 bool flushing = false;
Josef Bacikf104d042011-10-14 13:56:58 -04003445 bool wait_ordered = false;
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003446
3447 trans = (struct btrfs_trans_handle *)current->journal_info;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003448again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003449 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003450 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003451 /*
3452 * We only want to wait if somebody other than us is flushing and we are
3453 * actually alloed to flush.
3454 */
3455 while (flush && !flushing && space_info->flush) {
3456 spin_unlock(&space_info->lock);
3457 /*
3458 * If we have a trans handle we can't wait because the flusher
3459 * may have to commit the transaction, which would mean we would
3460 * deadlock since we are waiting for the flusher to finish, but
3461 * hold the current transaction open.
3462 */
3463 if (trans)
3464 return -EAGAIN;
3465 ret = wait_event_interruptible(space_info->wait,
3466 !space_info->flush);
3467 /* Must have been interrupted, return */
3468 if (ret)
3469 return -EINTR;
3470
3471 spin_lock(&space_info->lock);
3472 }
3473
3474 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04003475 used = space_info->bytes_used + space_info->bytes_reserved +
3476 space_info->bytes_pinned + space_info->bytes_readonly +
3477 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003478
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003479 /*
3480 * The idea here is that we've not already over-reserved the block group
3481 * then we can go ahead and save our reservation first and then start
3482 * flushing if we need to. Otherwise if we've already overcommitted
3483 * lets start flushing stuff first and then come back and try to make
3484 * our reservation.
3485 */
Josef Bacik2bf64752011-09-26 17:12:22 -04003486 if (used <= space_info->total_bytes) {
3487 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04003488 space_info->bytes_may_use += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003489 ret = 0;
3490 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003491 /*
3492 * Ok set num_bytes to orig_bytes since we aren't
3493 * overocmmitted, this way we only try and reclaim what
3494 * we need.
3495 */
3496 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003497 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003498 } else {
3499 /*
3500 * Ok we're over committed, set num_bytes to the overcommitted
3501 * amount plus the amount of bytes that we need for this
3502 * reservation.
3503 */
Josef Bacikf104d042011-10-14 13:56:58 -04003504 wait_ordered = true;
Josef Bacik2bf64752011-09-26 17:12:22 -04003505 num_bytes = used - space_info->total_bytes +
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003506 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003507 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003508
Josef Bacik36ba0222011-10-18 12:15:48 -04003509 if (ret) {
Josef Bacik2bf64752011-09-26 17:12:22 -04003510 u64 profile = btrfs_get_alloc_profile(root, 0);
3511 u64 avail;
3512
Josef Bacik7e355b82011-10-18 13:07:31 -04003513 /*
3514 * If we have a lot of space that's pinned, don't bother doing
3515 * the overcommit dance yet and just commit the transaction.
3516 */
3517 avail = (space_info->total_bytes - space_info->bytes_used) * 8;
3518 do_div(avail, 10);
3519 if (space_info->bytes_pinned >= avail && flush && !trans &&
3520 !committed) {
3521 space_info->flush = 1;
3522 flushing = true;
3523 spin_unlock(&space_info->lock);
3524 goto commit;
3525 }
3526
Josef Bacik2bf64752011-09-26 17:12:22 -04003527 spin_lock(&root->fs_info->free_chunk_lock);
3528 avail = root->fs_info->free_chunk_space;
3529
3530 /*
3531 * If we have dup, raid1 or raid10 then only half of the free
3532 * space is actually useable.
3533 */
3534 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3535 BTRFS_BLOCK_GROUP_RAID1 |
3536 BTRFS_BLOCK_GROUP_RAID10))
3537 avail >>= 1;
3538
3539 /*
3540 * If we aren't flushing don't let us overcommit too much, say
3541 * 1/8th of the space. If we can flush, let it overcommit up to
3542 * 1/2 of the space.
3543 */
3544 if (flush)
3545 avail >>= 3;
3546 else
3547 avail >>= 1;
3548 spin_unlock(&root->fs_info->free_chunk_lock);
3549
Josef Bacik9a82ca62011-10-05 16:35:28 -04003550 if (used + num_bytes < space_info->total_bytes + avail) {
Josef Bacik2bf64752011-09-26 17:12:22 -04003551 space_info->bytes_may_use += orig_bytes;
3552 ret = 0;
Josef Bacikf104d042011-10-14 13:56:58 -04003553 } else {
3554 wait_ordered = true;
Josef Bacik2bf64752011-09-26 17:12:22 -04003555 }
3556 }
3557
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003558 /*
3559 * Couldn't make our reservation, save our place so while we're trying
3560 * to reclaim space we can actually use it instead of somebody else
3561 * stealing it from us.
3562 */
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003563 if (ret && flush) {
3564 flushing = true;
3565 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003566 }
3567
Yan, Zhengf0486c62010-05-16 10:46:25 -04003568 spin_unlock(&space_info->lock);
3569
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003570 if (!ret || !flush)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003571 goto out;
3572
3573 /*
3574 * We do synchronous shrinking since we don't actually unreserve
3575 * metadata until after the IO is completed.
3576 */
Josef Bacikf104d042011-10-14 13:56:58 -04003577 ret = shrink_delalloc(trans, root, num_bytes, wait_ordered);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003578 if (ret < 0)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003579 goto out;
3580
Chris Mason75c195a2011-07-27 15:57:44 -04003581 ret = 0;
3582
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003583 /*
3584 * So if we were overcommitted it's possible that somebody else flushed
3585 * out enough space and we simply didn't have enough space to reclaim,
3586 * so go back around and try again.
3587 */
3588 if (retries < 2) {
Josef Bacikf104d042011-10-14 13:56:58 -04003589 wait_ordered = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003590 retries++;
3591 goto again;
3592 }
3593
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003594 ret = -EAGAIN;
Chris Mason75c195a2011-07-27 15:57:44 -04003595 if (trans)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003596 goto out;
3597
Josef Bacik7e355b82011-10-18 13:07:31 -04003598commit:
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003599 ret = -ENOSPC;
Chris Mason75c195a2011-07-27 15:57:44 -04003600 if (committed)
3601 goto out;
3602
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003603 trans = btrfs_join_transaction(root);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003604 if (IS_ERR(trans))
3605 goto out;
3606 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003607 if (!ret) {
3608 trans = NULL;
3609 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003610 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003611 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003612
3613out:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003614 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003615 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003616 space_info->flush = 0;
3617 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003618 spin_unlock(&space_info->lock);
3619 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04003620 return ret;
3621}
3622
3623static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3624 struct btrfs_root *root)
3625{
Josef Bacik4c13d752011-08-30 11:31:29 -04003626 struct btrfs_block_rsv *block_rsv = NULL;
3627
3628 if (root->ref_cows || root == root->fs_info->csum_root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003629 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04003630
3631 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003632 block_rsv = root->block_rsv;
3633
3634 if (!block_rsv)
3635 block_rsv = &root->fs_info->empty_block_rsv;
3636
3637 return block_rsv;
3638}
3639
3640static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3641 u64 num_bytes)
3642{
3643 int ret = -ENOSPC;
3644 spin_lock(&block_rsv->lock);
3645 if (block_rsv->reserved >= num_bytes) {
3646 block_rsv->reserved -= num_bytes;
3647 if (block_rsv->reserved < block_rsv->size)
3648 block_rsv->full = 0;
3649 ret = 0;
3650 }
3651 spin_unlock(&block_rsv->lock);
3652 return ret;
3653}
3654
3655static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3656 u64 num_bytes, int update_size)
3657{
3658 spin_lock(&block_rsv->lock);
3659 block_rsv->reserved += num_bytes;
3660 if (update_size)
3661 block_rsv->size += num_bytes;
3662 else if (block_rsv->reserved >= block_rsv->size)
3663 block_rsv->full = 1;
3664 spin_unlock(&block_rsv->lock);
3665}
3666
David Sterba62a45b62011-04-20 15:52:26 +02003667static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3668 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003669{
3670 struct btrfs_space_info *space_info = block_rsv->space_info;
3671
3672 spin_lock(&block_rsv->lock);
3673 if (num_bytes == (u64)-1)
3674 num_bytes = block_rsv->size;
3675 block_rsv->size -= num_bytes;
3676 if (block_rsv->reserved >= block_rsv->size) {
3677 num_bytes = block_rsv->reserved - block_rsv->size;
3678 block_rsv->reserved = block_rsv->size;
3679 block_rsv->full = 1;
3680 } else {
3681 num_bytes = 0;
3682 }
3683 spin_unlock(&block_rsv->lock);
3684
3685 if (num_bytes > 0) {
3686 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003687 spin_lock(&dest->lock);
3688 if (!dest->full) {
3689 u64 bytes_to_add;
3690
3691 bytes_to_add = dest->size - dest->reserved;
3692 bytes_to_add = min(num_bytes, bytes_to_add);
3693 dest->reserved += bytes_to_add;
3694 if (dest->reserved >= dest->size)
3695 dest->full = 1;
3696 num_bytes -= bytes_to_add;
3697 }
3698 spin_unlock(&dest->lock);
3699 }
3700 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003701 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04003702 space_info->bytes_may_use -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003703 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003704 spin_unlock(&space_info->lock);
3705 }
3706 }
3707}
3708
3709static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3710 struct btrfs_block_rsv *dst, u64 num_bytes)
3711{
3712 int ret;
3713
3714 ret = block_rsv_use_bytes(src, num_bytes);
3715 if (ret)
3716 return ret;
3717
3718 block_rsv_add_bytes(dst, num_bytes, 1);
3719 return 0;
3720}
3721
3722void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3723{
3724 memset(rsv, 0, sizeof(*rsv));
3725 spin_lock_init(&rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003726}
3727
3728struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3729{
3730 struct btrfs_block_rsv *block_rsv;
3731 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003732
3733 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3734 if (!block_rsv)
3735 return NULL;
3736
3737 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003738 block_rsv->space_info = __find_space_info(fs_info,
3739 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003740 return block_rsv;
3741}
3742
3743void btrfs_free_block_rsv(struct btrfs_root *root,
3744 struct btrfs_block_rsv *rsv)
3745{
Josef Bacikdabdb642011-08-08 12:50:18 -04003746 btrfs_block_rsv_release(root, rsv, (u64)-1);
3747 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003748}
3749
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003750int btrfs_block_rsv_add(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003751 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003752 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003753{
3754 int ret;
3755
3756 if (num_bytes == 0)
3757 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003758
Josef Bacik36ba0222011-10-18 12:15:48 -04003759 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003760 if (!ret) {
3761 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3762 return 0;
3763 }
3764
Yan, Zhengf0486c62010-05-16 10:46:25 -04003765 return ret;
3766}
3767
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003768int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04003769 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003770{
3771 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003772 int ret = -ENOSPC;
3773
3774 if (!block_rsv)
3775 return 0;
3776
3777 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04003778 num_bytes = div_factor(block_rsv->size, min_factor);
3779 if (block_rsv->reserved >= num_bytes)
3780 ret = 0;
3781 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003782
Josef Bacik36ba0222011-10-18 12:15:48 -04003783 return ret;
3784}
3785
3786int btrfs_block_rsv_refill(struct btrfs_root *root,
3787 struct btrfs_block_rsv *block_rsv,
3788 u64 min_reserved)
3789{
3790 u64 num_bytes = 0;
3791 int ret = -ENOSPC;
3792
3793 if (!block_rsv)
3794 return 0;
3795
3796 spin_lock(&block_rsv->lock);
3797 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04003798 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003799 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04003800 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04003801 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003802 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04003803
Yan, Zhengf0486c62010-05-16 10:46:25 -04003804 if (!ret)
3805 return 0;
3806
Josef Bacik36ba0222011-10-18 12:15:48 -04003807 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, 1);
Josef Bacikdabdb642011-08-08 12:50:18 -04003808 if (!ret) {
3809 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3810 return 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003811 }
3812
Josef Bacik13553e52011-08-08 13:33:21 -04003813 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003814}
3815
3816int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3817 struct btrfs_block_rsv *dst_rsv,
3818 u64 num_bytes)
3819{
3820 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3821}
3822
3823void btrfs_block_rsv_release(struct btrfs_root *root,
3824 struct btrfs_block_rsv *block_rsv,
3825 u64 num_bytes)
3826{
3827 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3828 if (global_rsv->full || global_rsv == block_rsv ||
3829 block_rsv->space_info != global_rsv->space_info)
3830 global_rsv = NULL;
3831 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3832}
3833
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003834/*
3835 * helper to calculate size of global block reservation.
3836 * the desired value is sum of space used by extent tree,
3837 * checksum tree and root tree
3838 */
3839static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3840{
3841 struct btrfs_space_info *sinfo;
3842 u64 num_bytes;
3843 u64 meta_used;
3844 u64 data_used;
3845 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003846
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003847 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3848 spin_lock(&sinfo->lock);
3849 data_used = sinfo->bytes_used;
3850 spin_unlock(&sinfo->lock);
3851
3852 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3853 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003854 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3855 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003856 meta_used = sinfo->bytes_used;
3857 spin_unlock(&sinfo->lock);
3858
3859 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3860 csum_size * 2;
3861 num_bytes += div64_u64(data_used + meta_used, 50);
3862
3863 if (num_bytes * 3 > meta_used)
3864 num_bytes = div64_u64(meta_used, 3);
3865
3866 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3867}
3868
3869static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3870{
3871 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3872 struct btrfs_space_info *sinfo = block_rsv->space_info;
3873 u64 num_bytes;
3874
3875 num_bytes = calc_global_metadata_size(fs_info);
3876
3877 spin_lock(&block_rsv->lock);
3878 spin_lock(&sinfo->lock);
3879
3880 block_rsv->size = num_bytes;
3881
3882 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003883 sinfo->bytes_reserved + sinfo->bytes_readonly +
3884 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003885
3886 if (sinfo->total_bytes > num_bytes) {
3887 num_bytes = sinfo->total_bytes - num_bytes;
3888 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04003889 sinfo->bytes_may_use += num_bytes;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003890 }
3891
3892 if (block_rsv->reserved >= block_rsv->size) {
3893 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003894 sinfo->bytes_may_use -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003895 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003896 block_rsv->reserved = block_rsv->size;
3897 block_rsv->full = 1;
3898 }
David Sterba182608c2011-05-05 13:13:16 +02003899
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003900 spin_unlock(&sinfo->lock);
3901 spin_unlock(&block_rsv->lock);
3902}
3903
Yan, Zhengf0486c62010-05-16 10:46:25 -04003904static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3905{
3906 struct btrfs_space_info *space_info;
3907
3908 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3909 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003910
3911 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003912 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003913 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003914 fs_info->trans_block_rsv.space_info = space_info;
3915 fs_info->empty_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003916
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003917 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3918 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3919 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3920 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003921 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003922
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003923 update_global_block_rsv(fs_info);
3924}
3925
3926static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3927{
3928 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3929 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3930 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3931 WARN_ON(fs_info->trans_block_rsv.size > 0);
3932 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3933 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3934 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003935}
3936
Yan, Zhenga22285a2010-05-16 10:48:46 -04003937void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3938 struct btrfs_root *root)
3939{
3940 if (!trans->bytes_reserved)
3941 return;
3942
Josef Bacikb24e03d2011-10-14 14:40:17 -04003943 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003944 trans->bytes_reserved = 0;
3945}
3946
Yan, Zhengd68fc572010-05-16 10:49:58 -04003947int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3948 struct inode *inode)
3949{
3950 struct btrfs_root *root = BTRFS_I(inode)->root;
3951 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3952 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3953
3954 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04003955 * We need to hold space in order to delete our orphan item once we've
3956 * added it, so this takes the reservation so we can release it later
3957 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003958 */
Chris Masonff5714c2011-05-28 07:00:39 -04003959 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003960 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3961}
3962
3963void btrfs_orphan_release_metadata(struct inode *inode)
3964{
3965 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04003966 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003967 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3968}
3969
Yan, Zhenga22285a2010-05-16 10:48:46 -04003970int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3971 struct btrfs_pending_snapshot *pending)
3972{
3973 struct btrfs_root *root = pending->root;
3974 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3975 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3976 /*
3977 * two for root back/forward refs, two for directory entries
3978 * and one for root of the snapshot.
3979 */
Miao Xie16cdcec2011-04-22 18:12:22 +08003980 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003981 dst_rsv->space_info = src_rsv->space_info;
3982 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3983}
3984
Josef Bacik7709cde2011-08-04 10:25:02 -04003985/**
3986 * drop_outstanding_extent - drop an outstanding extent
3987 * @inode: the inode we're dropping the extent for
3988 *
3989 * This is called when we are freeing up an outstanding extent, either called
3990 * after an error or after an extent is written. This will return the number of
3991 * reserved extents that need to be freed. This must be called with
3992 * BTRFS_I(inode)->lock held.
3993 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00003994static unsigned drop_outstanding_extent(struct inode *inode)
3995{
3996 unsigned dropped_extents = 0;
3997
Josef Bacik9e0baf62011-07-15 15:16:44 +00003998 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
3999 BTRFS_I(inode)->outstanding_extents--;
4000
4001 /*
4002 * If we have more or the same amount of outsanding extents than we have
4003 * reserved then we need to leave the reserved extents count alone.
4004 */
4005 if (BTRFS_I(inode)->outstanding_extents >=
4006 BTRFS_I(inode)->reserved_extents)
Josef Bacik7709cde2011-08-04 10:25:02 -04004007 return 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004008
4009 dropped_extents = BTRFS_I(inode)->reserved_extents -
4010 BTRFS_I(inode)->outstanding_extents;
4011 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004012 return dropped_extents;
4013}
4014
Josef Bacik7709cde2011-08-04 10:25:02 -04004015/**
4016 * calc_csum_metadata_size - return the amount of metada space that must be
4017 * reserved/free'd for the given bytes.
4018 * @inode: the inode we're manipulating
4019 * @num_bytes: the number of bytes in question
4020 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4021 *
4022 * This adjusts the number of csum_bytes in the inode and then returns the
4023 * correct amount of metadata that must either be reserved or freed. We
4024 * calculate how many checksums we can fit into one leaf and then divide the
4025 * number of bytes that will need to be checksumed by this value to figure out
4026 * how many checksums will be required. If we are adding bytes then the number
4027 * may go up and we will return the number of additional bytes that must be
4028 * reserved. If it is going down we will return the number of bytes that must
4029 * be freed.
4030 *
4031 * This must be called with BTRFS_I(inode)->lock held.
4032 */
4033static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4034 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004035{
Josef Bacik7709cde2011-08-04 10:25:02 -04004036 struct btrfs_root *root = BTRFS_I(inode)->root;
4037 u64 csum_size;
4038 int num_csums_per_leaf;
4039 int num_csums;
4040 int old_csums;
4041
4042 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4043 BTRFS_I(inode)->csum_bytes == 0)
4044 return 0;
4045
4046 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4047 if (reserve)
4048 BTRFS_I(inode)->csum_bytes += num_bytes;
4049 else
4050 BTRFS_I(inode)->csum_bytes -= num_bytes;
4051 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4052 num_csums_per_leaf = (int)div64_u64(csum_size,
4053 sizeof(struct btrfs_csum_item) +
4054 sizeof(struct btrfs_disk_key));
4055 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4056 num_csums = num_csums + num_csums_per_leaf - 1;
4057 num_csums = num_csums / num_csums_per_leaf;
4058
4059 old_csums = old_csums + num_csums_per_leaf - 1;
4060 old_csums = old_csums / num_csums_per_leaf;
4061
4062 /* No change, no need to reserve more */
4063 if (old_csums == num_csums)
4064 return 0;
4065
4066 if (reserve)
4067 return btrfs_calc_trans_metadata_size(root,
4068 num_csums - old_csums);
4069
4070 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004071}
4072
4073int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4074{
4075 struct btrfs_root *root = BTRFS_I(inode)->root;
4076 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004077 u64 to_reserve = 0;
4078 unsigned nr_extents = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -04004079 int flush = 1;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004080 int ret;
4081
Josef Bacikc09544e2011-08-30 10:19:10 -04004082 if (btrfs_is_free_space_inode(root, inode))
4083 flush = 0;
4084
4085 if (flush && btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004086 schedule_timeout(1);
4087
4088 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004089
Josef Bacik9e0baf62011-07-15 15:16:44 +00004090 spin_lock(&BTRFS_I(inode)->lock);
4091 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004092
Josef Bacik9e0baf62011-07-15 15:16:44 +00004093 if (BTRFS_I(inode)->outstanding_extents >
4094 BTRFS_I(inode)->reserved_extents) {
4095 nr_extents = BTRFS_I(inode)->outstanding_extents -
4096 BTRFS_I(inode)->reserved_extents;
4097 BTRFS_I(inode)->reserved_extents += nr_extents;
4098
Miao Xie16cdcec2011-04-22 18:12:22 +08004099 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004100 }
Josef Bacik7709cde2011-08-04 10:25:02 -04004101 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004102 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004103
Josef Bacik36ba0222011-10-18 12:15:48 -04004104 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004105 if (ret) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004106 u64 to_free = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004107 unsigned dropped;
Josef Bacik7ed49f12011-08-19 15:45:52 -04004108
Josef Bacik7709cde2011-08-04 10:25:02 -04004109 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004110 dropped = drop_outstanding_extent(inode);
Josef Bacik7ed49f12011-08-19 15:45:52 -04004111 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04004112 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik7ed49f12011-08-19 15:45:52 -04004113 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4114
4115 /*
4116 * Somebody could have come in and twiddled with the
4117 * reservation, so if we have to free more than we would have
4118 * reserved from this reservation go ahead and release those
4119 * bytes.
4120 */
4121 to_free -= to_reserve;
4122 if (to_free)
4123 btrfs_block_rsv_release(root, block_rsv, to_free);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004124 return ret;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004125 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004126
4127 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4128
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004129 return 0;
4130}
4131
Josef Bacik7709cde2011-08-04 10:25:02 -04004132/**
4133 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4134 * @inode: the inode to release the reservation for
4135 * @num_bytes: the number of bytes we're releasing
4136 *
4137 * This will release the metadata reservation for an inode. This can be called
4138 * once we complete IO for a given set of bytes to release their metadata
4139 * reservations.
4140 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004141void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4142{
4143 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004144 u64 to_free = 0;
4145 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004146
4147 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004148 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004149 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004150
Josef Bacik7709cde2011-08-04 10:25:02 -04004151 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4152 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004153 if (dropped > 0)
4154 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004155
4156 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4157 to_free);
4158}
4159
Josef Bacik7709cde2011-08-04 10:25:02 -04004160/**
4161 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4162 * @inode: inode we're writing to
4163 * @num_bytes: the number of bytes we want to allocate
4164 *
4165 * This will do the following things
4166 *
4167 * o reserve space in the data space info for num_bytes
4168 * o reserve space in the metadata space info based on number of outstanding
4169 * extents and how much csums will be needed
4170 * o add to the inodes ->delalloc_bytes
4171 * o add it to the fs_info's delalloc inodes list.
4172 *
4173 * This will return 0 for success and -ENOSPC if there is no space left.
4174 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004175int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4176{
4177 int ret;
4178
4179 ret = btrfs_check_data_free_space(inode, num_bytes);
4180 if (ret)
4181 return ret;
4182
4183 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4184 if (ret) {
4185 btrfs_free_reserved_data_space(inode, num_bytes);
4186 return ret;
4187 }
4188
4189 return 0;
4190}
4191
Josef Bacik7709cde2011-08-04 10:25:02 -04004192/**
4193 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4194 * @inode: inode we're releasing space for
4195 * @num_bytes: the number of bytes we want to free up
4196 *
4197 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4198 * called in the case that we don't need the metadata AND data reservations
4199 * anymore. So if there is an error or we insert an inline extent.
4200 *
4201 * This function will release the metadata space that was not used and will
4202 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4203 * list if there are no delalloc bytes left.
4204 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004205void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4206{
4207 btrfs_delalloc_release_metadata(inode, num_bytes);
4208 btrfs_free_reserved_data_space(inode, num_bytes);
4209}
4210
Chris Mason9078a3e2007-04-26 16:46:15 -04004211static int update_block_group(struct btrfs_trans_handle *trans,
4212 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004213 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004214{
Josef Bacik0af3d002010-06-21 14:48:16 -04004215 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004216 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004217 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004218 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004219 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004220 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004221
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004222 /* block accounting for super block */
4223 spin_lock(&info->delalloc_lock);
4224 old_val = btrfs_super_bytes_used(&info->super_copy);
4225 if (alloc)
4226 old_val += num_bytes;
4227 else
4228 old_val -= num_bytes;
4229 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004230 spin_unlock(&info->delalloc_lock);
4231
Chris Masond3977122009-01-05 21:25:51 -05004232 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004233 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004234 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004235 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004236 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4237 BTRFS_BLOCK_GROUP_RAID1 |
4238 BTRFS_BLOCK_GROUP_RAID10))
4239 factor = 2;
4240 else
4241 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004242 /*
4243 * If this block group has free space cache written out, we
4244 * need to make sure to load it if we are removing space. This
4245 * is because we need the unpinning stage to actually add the
4246 * space back to the block group, otherwise we will leak space.
4247 */
4248 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004249 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004250
Chris Masondb945352007-10-15 16:15:53 -04004251 byte_in_group = bytenr - cache->key.objectid;
4252 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004253
Josef Bacik25179202008-10-29 14:49:05 -04004254 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004255 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004256
Josef Bacik73bc1872011-10-03 14:07:49 -04004257 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04004258 cache->disk_cache_state < BTRFS_DC_CLEAR)
4259 cache->disk_cache_state = BTRFS_DC_CLEAR;
4260
Josef Bacik0f9dd462008-09-23 13:14:11 -04004261 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004262 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004263 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004264 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004265 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004266 btrfs_set_block_group_used(&cache->item, old_val);
4267 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004268 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004269 cache->space_info->bytes_used += num_bytes;
4270 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004271 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004272 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004273 } else {
Chris Masondb945352007-10-15 16:15:53 -04004274 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004275 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004276 cache->pinned += num_bytes;
4277 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004278 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004279 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004280 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004281 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004282
Yan, Zhengf0486c62010-05-16 10:46:25 -04004283 set_extent_dirty(info->pinned_extents,
4284 bytenr, bytenr + num_bytes - 1,
4285 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004286 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004287 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004288 total -= num_bytes;
4289 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004290 }
4291 return 0;
4292}
Chris Mason6324fbf2008-03-24 15:01:59 -04004293
Chris Masona061fc82008-05-07 11:43:44 -04004294static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4295{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004296 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004297 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004298
4299 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4300 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004301 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004302
Yan Zhengd2fb3432008-12-11 16:30:39 -05004303 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004304 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004305
4306 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004307}
4308
Yan, Zhengf0486c62010-05-16 10:46:25 -04004309static int pin_down_extent(struct btrfs_root *root,
4310 struct btrfs_block_group_cache *cache,
4311 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004312{
Yan Zheng11833d62009-09-11 16:11:19 -04004313 spin_lock(&cache->space_info->lock);
4314 spin_lock(&cache->lock);
4315 cache->pinned += num_bytes;
4316 cache->space_info->bytes_pinned += num_bytes;
4317 if (reserved) {
4318 cache->reserved -= num_bytes;
4319 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004320 }
Yan Zheng11833d62009-09-11 16:11:19 -04004321 spin_unlock(&cache->lock);
4322 spin_unlock(&cache->space_info->lock);
4323
Yan, Zhengf0486c62010-05-16 10:46:25 -04004324 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4325 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004326 return 0;
4327}
Chris Mason9078a3e2007-04-26 16:46:15 -04004328
Yan, Zhengf0486c62010-05-16 10:46:25 -04004329/*
4330 * this function must be called within transaction
4331 */
4332int btrfs_pin_extent(struct btrfs_root *root,
4333 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004334{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004335 struct btrfs_block_group_cache *cache;
4336
4337 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4338 BUG_ON(!cache);
4339
4340 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4341
4342 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004343 return 0;
4344}
Zheng Yane8569812008-09-26 10:05:48 -04004345
Josef Bacikfb25e912011-07-26 17:00:46 -04004346/**
4347 * btrfs_update_reserved_bytes - update the block_group and space info counters
4348 * @cache: The cache we are manipulating
4349 * @num_bytes: The number of bytes in question
4350 * @reserve: One of the reservation enums
4351 *
4352 * This is called by the allocator when it reserves space, or by somebody who is
4353 * freeing space that was never actually used on disk. For example if you
4354 * reserve some space for a new leaf in transaction A and before transaction A
4355 * commits you free that leaf, you call this with reserve set to 0 in order to
4356 * clear the reservation.
4357 *
4358 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4359 * ENOSPC accounting. For data we handle the reservation through clearing the
4360 * delalloc bits in the io_tree. We have to do this since we could end up
4361 * allocating less disk space for the amount of data we have reserved in the
4362 * case of compression.
4363 *
4364 * If this is a reservation and the block group has become read only we cannot
4365 * make the reservation and return -EAGAIN, otherwise this function always
4366 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04004367 */
Josef Bacikfb25e912011-07-26 17:00:46 -04004368static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4369 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004370{
Josef Bacikfb25e912011-07-26 17:00:46 -04004371 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004372 int ret = 0;
Josef Bacikfb25e912011-07-26 17:00:46 -04004373 spin_lock(&space_info->lock);
4374 spin_lock(&cache->lock);
4375 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004376 if (cache->ro) {
4377 ret = -EAGAIN;
4378 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04004379 cache->reserved += num_bytes;
4380 space_info->bytes_reserved += num_bytes;
4381 if (reserve == RESERVE_ALLOC) {
4382 BUG_ON(space_info->bytes_may_use < num_bytes);
4383 space_info->bytes_may_use -= num_bytes;
4384 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004385 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004386 } else {
4387 if (cache->ro)
4388 space_info->bytes_readonly += num_bytes;
4389 cache->reserved -= num_bytes;
4390 space_info->bytes_reserved -= num_bytes;
4391 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004392 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004393 spin_unlock(&cache->lock);
4394 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004395 return ret;
4396}
4397
Yan Zheng11833d62009-09-11 16:11:19 -04004398int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4399 struct btrfs_root *root)
4400{
4401 struct btrfs_fs_info *fs_info = root->fs_info;
4402 struct btrfs_caching_control *next;
4403 struct btrfs_caching_control *caching_ctl;
4404 struct btrfs_block_group_cache *cache;
4405
4406 down_write(&fs_info->extent_commit_sem);
4407
4408 list_for_each_entry_safe(caching_ctl, next,
4409 &fs_info->caching_block_groups, list) {
4410 cache = caching_ctl->block_group;
4411 if (block_group_cache_done(cache)) {
4412 cache->last_byte_to_unpin = (u64)-1;
4413 list_del_init(&caching_ctl->list);
4414 put_caching_control(caching_ctl);
4415 } else {
4416 cache->last_byte_to_unpin = caching_ctl->progress;
4417 }
4418 }
4419
4420 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4421 fs_info->pinned_extents = &fs_info->freed_extents[1];
4422 else
4423 fs_info->pinned_extents = &fs_info->freed_extents[0];
4424
4425 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004426
4427 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004428 return 0;
4429}
4430
4431static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4432{
4433 struct btrfs_fs_info *fs_info = root->fs_info;
4434 struct btrfs_block_group_cache *cache = NULL;
4435 u64 len;
4436
4437 while (start <= end) {
4438 if (!cache ||
4439 start >= cache->key.objectid + cache->key.offset) {
4440 if (cache)
4441 btrfs_put_block_group(cache);
4442 cache = btrfs_lookup_block_group(fs_info, start);
4443 BUG_ON(!cache);
4444 }
4445
4446 len = cache->key.objectid + cache->key.offset - start;
4447 len = min(len, end + 1 - start);
4448
4449 if (start < cache->last_byte_to_unpin) {
4450 len = min(len, cache->last_byte_to_unpin - start);
4451 btrfs_add_free_space(cache, start, len);
4452 }
Josef Bacik25179202008-10-29 14:49:05 -04004453
Yan, Zhengf0486c62010-05-16 10:46:25 -04004454 start += len;
4455
Josef Bacik25179202008-10-29 14:49:05 -04004456 spin_lock(&cache->space_info->lock);
4457 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004458 cache->pinned -= len;
4459 cache->space_info->bytes_pinned -= len;
Josef Bacik37be25b2011-08-05 10:25:38 -04004460 if (cache->ro)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004461 cache->space_info->bytes_readonly += len;
Josef Bacik25179202008-10-29 14:49:05 -04004462 spin_unlock(&cache->lock);
4463 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004464 }
4465
4466 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004467 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004468 return 0;
4469}
4470
4471int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004472 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004473{
Yan Zheng11833d62009-09-11 16:11:19 -04004474 struct btrfs_fs_info *fs_info = root->fs_info;
4475 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04004476 u64 start;
4477 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05004478 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004479
Yan Zheng11833d62009-09-11 16:11:19 -04004480 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4481 unpin = &fs_info->freed_extents[1];
4482 else
4483 unpin = &fs_info->freed_extents[0];
4484
Chris Masond3977122009-01-05 21:25:51 -05004485 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004486 ret = find_first_extent_bit(unpin, 0, &start, &end,
4487 EXTENT_DIRTY);
4488 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004489 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004490
Li Dongyang5378e602011-03-24 10:24:27 +00004491 if (btrfs_test_opt(root, DISCARD))
4492 ret = btrfs_discard_extent(root, start,
4493 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004494
Chris Mason1a5bc162007-10-15 16:15:26 -04004495 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004496 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004497 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004498 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004499
Chris Masone20d96d2007-03-22 12:13:20 -04004500 return 0;
4501}
4502
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004503static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4504 struct btrfs_root *root,
4505 u64 bytenr, u64 num_bytes, u64 parent,
4506 u64 root_objectid, u64 owner_objectid,
4507 u64 owner_offset, int refs_to_drop,
4508 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004509{
Chris Masone2fa7222007-03-12 16:22:34 -04004510 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004511 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004512 struct btrfs_fs_info *info = root->fs_info;
4513 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004514 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004515 struct btrfs_extent_item *ei;
4516 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004517 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004518 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004519 int extent_slot = 0;
4520 int found_extent = 0;
4521 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004522 u32 item_size;
4523 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004524
Chris Mason5caf2a02007-04-02 11:20:42 -04004525 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004526 if (!path)
4527 return -ENOMEM;
4528
Chris Mason3c12ac72008-04-21 12:01:38 -04004529 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004530 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004531
4532 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4533 BUG_ON(!is_data && refs_to_drop != 1);
4534
4535 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4536 bytenr, num_bytes, parent,
4537 root_objectid, owner_objectid,
4538 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004539 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004540 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004541 while (extent_slot >= 0) {
4542 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004543 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004544 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004545 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004546 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4547 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004548 found_extent = 1;
4549 break;
4550 }
4551 if (path->slots[0] - extent_slot > 5)
4552 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004553 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004554 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004555#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4556 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4557 if (found_extent && item_size < sizeof(*ei))
4558 found_extent = 0;
4559#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004560 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004561 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004562 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004563 NULL, refs_to_drop,
4564 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004565 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004566 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04004567 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004568
4569 key.objectid = bytenr;
4570 key.type = BTRFS_EXTENT_ITEM_KEY;
4571 key.offset = num_bytes;
4572
Zheng Yan31840ae2008-09-23 13:14:14 -04004573 ret = btrfs_search_slot(trans, extent_root,
4574 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004575 if (ret) {
4576 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004577 ", was looking for %llu\n", ret,
4578 (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00004579 if (ret > 0)
4580 btrfs_print_leaf(extent_root,
4581 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004582 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004583 BUG_ON(ret);
4584 extent_slot = path->slots[0];
4585 }
Chris Mason7bb86312007-12-11 09:25:06 -05004586 } else {
4587 btrfs_print_leaf(extent_root, path->nodes[0]);
4588 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004589 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004590 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004591 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004592 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004593 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004594 (unsigned long long)owner_objectid,
4595 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004596 }
Chris Mason5f39d392007-10-15 16:14:19 -04004597
4598 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004599 item_size = btrfs_item_size_nr(leaf, extent_slot);
4600#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4601 if (item_size < sizeof(*ei)) {
4602 BUG_ON(found_extent || extent_slot != path->slots[0]);
4603 ret = convert_extent_item_v0(trans, extent_root, path,
4604 owner_objectid, 0);
4605 BUG_ON(ret < 0);
4606
David Sterbab3b4aa72011-04-21 01:20:15 +02004607 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004608 path->leave_spinning = 1;
4609
4610 key.objectid = bytenr;
4611 key.type = BTRFS_EXTENT_ITEM_KEY;
4612 key.offset = num_bytes;
4613
4614 ret = btrfs_search_slot(trans, extent_root, &key, path,
4615 -1, 1);
4616 if (ret) {
4617 printk(KERN_ERR "umm, got %d back from search"
4618 ", was looking for %llu\n", ret,
4619 (unsigned long long)bytenr);
4620 btrfs_print_leaf(extent_root, path->nodes[0]);
4621 }
4622 BUG_ON(ret);
4623 extent_slot = path->slots[0];
4624 leaf = path->nodes[0];
4625 item_size = btrfs_item_size_nr(leaf, extent_slot);
4626 }
4627#endif
4628 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004629 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004630 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004631 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4632 struct btrfs_tree_block_info *bi;
4633 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4634 bi = (struct btrfs_tree_block_info *)(ei + 1);
4635 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004636 }
4637
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004638 refs = btrfs_extent_refs(leaf, ei);
4639 BUG_ON(refs < refs_to_drop);
4640 refs -= refs_to_drop;
4641
4642 if (refs > 0) {
4643 if (extent_op)
4644 __run_delayed_extent_op(extent_op, leaf, ei);
4645 /*
4646 * In the case of inline back ref, reference count will
4647 * be updated by remove_extent_backref
4648 */
4649 if (iref) {
4650 BUG_ON(!found_extent);
4651 } else {
4652 btrfs_set_extent_refs(leaf, ei, refs);
4653 btrfs_mark_buffer_dirty(leaf);
4654 }
4655 if (found_extent) {
4656 ret = remove_extent_backref(trans, extent_root, path,
4657 iref, refs_to_drop,
4658 is_data);
4659 BUG_ON(ret);
4660 }
4661 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004662 if (found_extent) {
4663 BUG_ON(is_data && refs_to_drop !=
4664 extent_data_ref_count(root, path, iref));
4665 if (iref) {
4666 BUG_ON(path->slots[0] != extent_slot);
4667 } else {
4668 BUG_ON(path->slots[0] != extent_slot + 1);
4669 path->slots[0] = extent_slot;
4670 num_to_del = 2;
4671 }
Chris Mason78fae272007-03-25 11:35:08 -04004672 }
Chris Masonb9473432009-03-13 11:00:37 -04004673
Chris Mason952fcca2008-02-18 16:33:44 -05004674 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4675 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004676 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004677 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01004678
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004679 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004680 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4681 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004682 } else {
4683 invalidate_mapping_pages(info->btree_inode->i_mapping,
4684 bytenr >> PAGE_CACHE_SHIFT,
4685 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004686 }
4687
Yan, Zhengf0486c62010-05-16 10:46:25 -04004688 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004689 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004690 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004691 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004692 return ret;
4693}
4694
4695/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004696 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004697 * delayed ref for that extent as well. This searches the delayed ref tree for
4698 * a given extent, and if there are no other delayed refs to be processed, it
4699 * removes it from the tree.
4700 */
4701static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4702 struct btrfs_root *root, u64 bytenr)
4703{
4704 struct btrfs_delayed_ref_head *head;
4705 struct btrfs_delayed_ref_root *delayed_refs;
4706 struct btrfs_delayed_ref_node *ref;
4707 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004708 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004709
4710 delayed_refs = &trans->transaction->delayed_refs;
4711 spin_lock(&delayed_refs->lock);
4712 head = btrfs_find_delayed_ref_head(trans, bytenr);
4713 if (!head)
4714 goto out;
4715
4716 node = rb_prev(&head->node.rb_node);
4717 if (!node)
4718 goto out;
4719
4720 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4721
4722 /* there are still entries for this ref, we can't drop it */
4723 if (ref->bytenr == bytenr)
4724 goto out;
4725
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004726 if (head->extent_op) {
4727 if (!head->must_insert_reserved)
4728 goto out;
4729 kfree(head->extent_op);
4730 head->extent_op = NULL;
4731 }
4732
Chris Mason1887be62009-03-13 10:11:24 -04004733 /*
4734 * waiting for the lock here would deadlock. If someone else has it
4735 * locked they are already in the process of dropping it anyway
4736 */
4737 if (!mutex_trylock(&head->mutex))
4738 goto out;
4739
4740 /*
4741 * at this point we have a head with no other entries. Go
4742 * ahead and process it.
4743 */
4744 head->node.in_tree = 0;
4745 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004746
Chris Mason1887be62009-03-13 10:11:24 -04004747 delayed_refs->num_entries--;
4748
4749 /*
4750 * we don't take a ref on the node because we're removing it from the
4751 * tree, so we just steal the ref the tree was holding.
4752 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004753 delayed_refs->num_heads--;
4754 if (list_empty(&head->cluster))
4755 delayed_refs->num_heads_ready--;
4756
4757 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004758 spin_unlock(&delayed_refs->lock);
4759
Yan, Zhengf0486c62010-05-16 10:46:25 -04004760 BUG_ON(head->extent_op);
4761 if (head->must_insert_reserved)
4762 ret = 1;
4763
4764 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004765 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004766 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004767out:
4768 spin_unlock(&delayed_refs->lock);
4769 return 0;
4770}
4771
Yan, Zhengf0486c62010-05-16 10:46:25 -04004772void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4773 struct btrfs_root *root,
4774 struct extent_buffer *buf,
4775 u64 parent, int last_ref)
4776{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004777 struct btrfs_block_group_cache *cache = NULL;
4778 int ret;
4779
4780 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4781 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4782 parent, root->root_key.objectid,
4783 btrfs_header_level(buf),
4784 BTRFS_DROP_DELAYED_REF, NULL);
4785 BUG_ON(ret);
4786 }
4787
4788 if (!last_ref)
4789 return;
4790
Yan, Zhengf0486c62010-05-16 10:46:25 -04004791 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004792
4793 if (btrfs_header_generation(buf) == trans->transid) {
4794 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4795 ret = check_ref_cleanup(trans, root, buf->start);
4796 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04004797 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004798 }
4799
4800 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4801 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04004802 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004803 }
4804
4805 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4806
4807 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04004808 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004809 }
4810out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004811 /*
4812 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4813 * anymore.
4814 */
4815 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004816 btrfs_put_block_group(cache);
4817}
4818
Chris Mason925baed2008-06-25 16:01:30 -04004819int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004820 struct btrfs_root *root,
4821 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004822 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004823{
4824 int ret;
4825
Chris Mason56bec292009-03-13 10:10:06 -04004826 /*
4827 * tree log blocks never actually go into the extent allocation
4828 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004829 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004830 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4831 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004832 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004833 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004834 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004835 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4836 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4837 parent, root_objectid, (int)owner,
4838 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004839 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004840 } else {
4841 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4842 parent, root_objectid, owner,
4843 offset, BTRFS_DROP_DELAYED_REF, NULL);
4844 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004845 }
Chris Mason925baed2008-06-25 16:01:30 -04004846 return ret;
4847}
4848
Chris Mason87ee04e2007-11-30 11:30:34 -05004849static u64 stripe_align(struct btrfs_root *root, u64 val)
4850{
4851 u64 mask = ((u64)root->stripesize - 1);
4852 u64 ret = (val + mask) & ~mask;
4853 return ret;
4854}
4855
Chris Masonfec577f2007-02-26 10:40:21 -05004856/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004857 * when we wait for progress in the block group caching, its because
4858 * our allocation attempt failed at least once. So, we must sleep
4859 * and let some progress happen before we try again.
4860 *
4861 * This function will sleep at least once waiting for new free space to
4862 * show up, and then it will check the block group free space numbers
4863 * for our min num_bytes. Another option is to have it go ahead
4864 * and look in the rbtree for a free extent of a given size, but this
4865 * is a good start.
4866 */
4867static noinline int
4868wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4869 u64 num_bytes)
4870{
Yan Zheng11833d62009-09-11 16:11:19 -04004871 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004872 DEFINE_WAIT(wait);
4873
Yan Zheng11833d62009-09-11 16:11:19 -04004874 caching_ctl = get_caching_control(cache);
4875 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004876 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004877
Yan Zheng11833d62009-09-11 16:11:19 -04004878 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08004879 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004880
4881 put_caching_control(caching_ctl);
4882 return 0;
4883}
4884
4885static noinline int
4886wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4887{
4888 struct btrfs_caching_control *caching_ctl;
4889 DEFINE_WAIT(wait);
4890
4891 caching_ctl = get_caching_control(cache);
4892 if (!caching_ctl)
4893 return 0;
4894
4895 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4896
4897 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004898 return 0;
4899}
4900
Yan, Zhengb742bb82010-05-16 10:46:24 -04004901static int get_block_group_index(struct btrfs_block_group_cache *cache)
4902{
4903 int index;
4904 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4905 index = 0;
4906 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4907 index = 1;
4908 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4909 index = 2;
4910 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4911 index = 3;
4912 else
4913 index = 4;
4914 return index;
4915}
4916
Josef Bacik817d52f2009-07-13 21:29:25 -04004917enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004918 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004919 LOOP_CACHING_NOWAIT = 1,
4920 LOOP_CACHING_WAIT = 2,
4921 LOOP_ALLOC_CHUNK = 3,
4922 LOOP_NO_EMPTY_SIZE = 4,
4923};
4924
4925/*
Chris Masonfec577f2007-02-26 10:40:21 -05004926 * walks the btree of allocated extents and find a hole of a given size.
4927 * The key ins is changed to record the hole:
4928 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004929 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004930 * ins->offset == number of blocks
4931 * Any available blocks before search_start are skipped.
4932 */
Chris Masond3977122009-01-05 21:25:51 -05004933static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004934 struct btrfs_root *orig_root,
4935 u64 num_bytes, u64 empty_size,
4936 u64 search_start, u64 search_end,
4937 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00004938 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05004939{
Josef Bacik80eb2342008-10-29 14:49:05 -04004940 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004941 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004942 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004943 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004944 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004945 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004946 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004947 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004948 int last_ptr_loop = 0;
4949 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004950 int index = 0;
Josef Bacikfb25e912011-07-26 17:00:46 -04004951 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
4952 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04004953 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004954 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004955 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004956 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004957 u64 ideal_cache_percent = 0;
4958 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004959
Chris Masondb945352007-10-15 16:15:53 -04004960 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004961 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004962 ins->objectid = 0;
4963 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004964
Josef Bacik2552d172009-04-03 10:14:19 -04004965 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004966 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00004967 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004968 return -ENOSPC;
4969 }
Josef Bacik2552d172009-04-03 10:14:19 -04004970
Josef Bacik67377732010-09-16 16:19:09 -04004971 /*
4972 * If the space info is for both data and metadata it means we have a
4973 * small filesystem and we can't use the clustering stuff.
4974 */
4975 if (btrfs_mixed_space_info(space_info))
4976 use_cluster = false;
4977
Chris Mason0ef3e662008-05-24 14:04:53 -04004978 if (orig_root->ref_cows || empty_size)
4979 allowed_chunk_alloc = 1;
4980
Josef Bacik67377732010-09-16 16:19:09 -04004981 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004982 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004983 if (!btrfs_test_opt(root, SSD))
4984 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004985 }
4986
Josef Bacik67377732010-09-16 16:19:09 -04004987 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4988 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004989 last_ptr = &root->fs_info->data_alloc_cluster;
4990 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004991
Chris Mason239b14b2008-03-24 15:02:07 -04004992 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004993 spin_lock(&last_ptr->lock);
4994 if (last_ptr->block_group)
4995 hint_byte = last_ptr->window_start;
4996 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004997 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004998
Chris Masona061fc82008-05-07 11:43:44 -04004999 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005000 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005001
Josef Bacik817d52f2009-07-13 21:29:25 -04005002 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005003 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005004
Josef Bacik2552d172009-04-03 10:14:19 -04005005 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005006ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04005007 block_group = btrfs_lookup_block_group(root->fs_info,
5008 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04005009 /*
5010 * we don't want to use the block group if it doesn't match our
5011 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005012 *
5013 * However if we are re-searching with an ideal block group
5014 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005015 */
5016 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05005017 (block_group->cached != BTRFS_CACHE_NO ||
5018 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04005019 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005020 if (list_empty(&block_group->list) ||
5021 block_group->ro) {
5022 /*
5023 * someone is removing this block group,
5024 * we can't jump into the have_block_group
5025 * target because our list pointers are not
5026 * valid
5027 */
5028 btrfs_put_block_group(block_group);
5029 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005030 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005031 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005032 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005033 }
Josef Bacik2552d172009-04-03 10:14:19 -04005034 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005035 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005036 }
Chris Mason42e70e72008-11-07 18:17:11 -05005037 }
Josef Bacik2552d172009-04-03 10:14:19 -04005038search:
Josef Bacik80eb2342008-10-29 14:49:05 -04005039 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005040 list_for_each_entry(block_group, &space_info->block_groups[index],
5041 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005042 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005043 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005044
Josef Bacik11dfe352009-11-13 20:12:59 +00005045 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005046 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005047
Chris Mason83a50de2010-12-13 15:06:46 -05005048 /*
5049 * this can happen if we end up cycling through all the
5050 * raid types, but we want to make sure we only allocate
5051 * for the proper type.
5052 */
5053 if (!block_group_bits(block_group, data)) {
5054 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5055 BTRFS_BLOCK_GROUP_RAID1 |
5056 BTRFS_BLOCK_GROUP_RAID10;
5057
5058 /*
5059 * if they asked for extra copies and this block group
5060 * doesn't provide them, bail. This does allow us to
5061 * fill raid0 from raid1.
5062 */
5063 if ((data & extra) && !(block_group->flags & extra))
5064 goto loop;
5065 }
5066
Josef Bacik2552d172009-04-03 10:14:19 -04005067have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04005068 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05005069 u64 free_percent;
5070
Josef Bacikb8399de2010-12-08 09:15:11 -05005071 ret = cache_block_group(block_group, trans,
5072 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04005073 if (block_group->cached == BTRFS_CACHE_FINISHED)
5074 goto have_block_group;
5075
Josef Bacikccf0e722009-11-10 21:23:48 -05005076 free_percent = btrfs_block_group_used(&block_group->item);
5077 free_percent *= 100;
5078 free_percent = div64_u64(free_percent,
5079 block_group->key.offset);
5080 free_percent = 100 - free_percent;
5081 if (free_percent > ideal_cache_percent &&
5082 likely(!block_group->ro)) {
5083 ideal_cache_offset = block_group->key.objectid;
5084 ideal_cache_percent = free_percent;
5085 }
5086
Josef Bacik817d52f2009-07-13 21:29:25 -04005087 /*
Josef Bacikbab39bf2011-06-30 14:42:28 -04005088 * The caching workers are limited to 2 threads, so we
5089 * can queue as much work as we care to.
Josef Bacik817d52f2009-07-13 21:29:25 -04005090 */
Josef Bacikbab39bf2011-06-30 14:42:28 -04005091 if (loop > LOOP_FIND_IDEAL) {
Josef Bacikb8399de2010-12-08 09:15:11 -05005092 ret = cache_block_group(block_group, trans,
5093 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04005094 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04005095 }
Josef Bacikccf0e722009-11-10 21:23:48 -05005096 found_uncached_bg = true;
5097
5098 /*
5099 * If loop is set for cached only, try the next block
5100 * group.
5101 */
5102 if (loop == LOOP_FIND_IDEAL)
5103 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05005104 }
5105
Josef Bacik817d52f2009-07-13 21:29:25 -04005106 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05005107 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04005108 found_uncached_bg = true;
5109
Josef Bacikea6a4782008-11-20 12:16:16 -05005110 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005111 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005112
Chris Masonff5714c2011-05-28 07:00:39 -04005113 spin_lock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005114 if (cached &&
Chris Masonff5714c2011-05-28 07:00:39 -04005115 block_group->free_space_ctl->free_space <
5116 num_bytes + empty_size) {
5117 spin_unlock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005118 goto loop;
5119 }
Chris Masonff5714c2011-05-28 07:00:39 -04005120 spin_unlock(&block_group->free_space_ctl->tree_lock);
Josef Bacikcca1c812011-05-13 11:07:12 -04005121
Josef Bacik0a243252009-09-11 16:11:20 -04005122 /*
5123 * Ok we want to try and use the cluster allocator, so lets look
5124 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5125 * have tried the cluster allocator plenty of times at this
5126 * point and not have found anything, so we are likely way too
5127 * fragmented for the clustering stuff to find anything, so lets
5128 * just skip it and let the allocator find whatever block it can
5129 * find
5130 */
5131 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005132 /*
5133 * the refill lock keeps out other
5134 * people trying to start a new cluster
5135 */
5136 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005137 if (last_ptr->block_group &&
5138 (last_ptr->block_group->ro ||
5139 !block_group_bits(last_ptr->block_group, data))) {
5140 offset = 0;
5141 goto refill_cluster;
5142 }
5143
Chris Masonfa9c0d792009-04-03 09:47:43 -04005144 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5145 num_bytes, search_start);
5146 if (offset) {
5147 /* we have a block, we're done */
5148 spin_unlock(&last_ptr->refill_lock);
5149 goto checks;
5150 }
5151
5152 spin_lock(&last_ptr->lock);
5153 /*
5154 * whoops, this cluster doesn't actually point to
5155 * this block group. Get a ref on the block
5156 * group is does point to and try again
5157 */
5158 if (!last_ptr_loop && last_ptr->block_group &&
liuboff1f2b42011-07-27 09:49:18 +00005159 last_ptr->block_group != block_group &&
5160 index <=
5161 get_block_group_index(last_ptr->block_group)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005162
5163 btrfs_put_block_group(block_group);
5164 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005165 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005166 spin_unlock(&last_ptr->lock);
5167 spin_unlock(&last_ptr->refill_lock);
5168
5169 last_ptr_loop = 1;
5170 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005171 /*
5172 * we know this block group is properly
5173 * in the list because
5174 * btrfs_remove_block_group, drops the
5175 * cluster before it removes the block
5176 * group from the list
5177 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04005178 goto have_block_group;
5179 }
5180 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005181refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04005182 /*
5183 * this cluster didn't work out, free it and
5184 * start over
5185 */
5186 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5187
5188 last_ptr_loop = 0;
5189
5190 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005191 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005192 block_group, last_ptr,
5193 offset, num_bytes,
5194 empty_cluster + empty_size);
5195 if (ret == 0) {
5196 /*
5197 * now pull our allocation out of this
5198 * cluster
5199 */
5200 offset = btrfs_alloc_from_cluster(block_group,
5201 last_ptr, num_bytes,
5202 search_start);
5203 if (offset) {
5204 /* we found one, proceed */
5205 spin_unlock(&last_ptr->refill_lock);
5206 goto checks;
5207 }
Josef Bacik0a243252009-09-11 16:11:20 -04005208 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5209 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005210 spin_unlock(&last_ptr->refill_lock);
5211
Josef Bacik0a243252009-09-11 16:11:20 -04005212 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005213 wait_block_group_cache_progress(block_group,
5214 num_bytes + empty_cluster + empty_size);
5215 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005216 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005217
Chris Masonfa9c0d792009-04-03 09:47:43 -04005218 /*
5219 * at this point we either didn't find a cluster
5220 * or we weren't able to allocate a block from our
5221 * cluster. Free the cluster we've been trying
5222 * to use, and go to the next block group
5223 */
Josef Bacik0a243252009-09-11 16:11:20 -04005224 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005225 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005226 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005227 }
5228
Josef Bacik6226cb02009-04-03 10:14:18 -04005229 offset = btrfs_find_space_for_alloc(block_group, search_start,
5230 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005231 /*
5232 * If we didn't find a chunk, and we haven't failed on this
5233 * block group before, and this block group is in the middle of
5234 * caching and we are ok with waiting, then go ahead and wait
5235 * for progress to be made, and set failed_alloc to true.
5236 *
5237 * If failed_alloc is true then we've already waited on this
5238 * block group once and should move on to the next block group.
5239 */
5240 if (!offset && !failed_alloc && !cached &&
5241 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005242 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005243 num_bytes + empty_size);
5244 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005245 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005246 } else if (!offset) {
5247 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005248 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005249checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005250 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005251 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005252 if (search_start + num_bytes >= search_end) {
5253 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005254 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005255 }
Chris Masone37c9e62007-05-09 20:13:14 -04005256
Josef Bacik2552d172009-04-03 10:14:19 -04005257 /* move on to the next group */
5258 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005259 block_group->key.objectid + block_group->key.offset) {
5260 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005261 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005262 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005263
Josef Bacik2552d172009-04-03 10:14:19 -04005264 ins->objectid = search_start;
5265 ins->offset = num_bytes;
5266
Josef Bacik6226cb02009-04-03 10:14:18 -04005267 if (offset < search_start)
5268 btrfs_add_free_space(block_group, offset,
5269 search_start - offset);
5270 BUG_ON(offset > search_start);
5271
Josef Bacikfb25e912011-07-26 17:00:46 -04005272 ret = btrfs_update_reserved_bytes(block_group, num_bytes,
5273 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005274 if (ret == -EAGAIN) {
5275 btrfs_add_free_space(block_group, offset, num_bytes);
5276 goto loop;
5277 }
Yan Zheng11833d62009-09-11 16:11:19 -04005278
Josef Bacik2552d172009-04-03 10:14:19 -04005279 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005280 ins->objectid = search_start;
5281 ins->offset = num_bytes;
5282
5283 if (offset < search_start)
5284 btrfs_add_free_space(block_group, offset,
5285 search_start - offset);
5286 BUG_ON(offset > search_start);
Josef Bacikd82a6f12011-05-11 15:26:06 -04005287 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005288 break;
5289loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005290 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005291 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005292 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d792009-04-03 09:47:43 -04005293 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005294 }
5295 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005296
Yan, Zhengb742bb82010-05-16 10:46:24 -04005297 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5298 goto search;
5299
Josef Bacikccf0e722009-11-10 21:23:48 -05005300 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5301 * for them to make caching progress. Also
5302 * determine the best possible bg to cache
5303 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5304 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005305 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5306 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5307 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5308 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04005309 */
Josef Bacik723bda22011-05-27 16:11:38 -04005310 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005311 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005312 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005313 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005314 loop++;
Josef Bacikbab39bf2011-06-30 14:42:28 -04005315 if (!ideal_cache_percent)
Josef Bacik817d52f2009-07-13 21:29:25 -04005316 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005317
5318 /*
5319 * 1 of the following 2 things have happened so far
5320 *
5321 * 1) We found an ideal block group for caching that
5322 * is mostly full and will cache quickly, so we might
5323 * as well wait for it.
5324 *
5325 * 2) We searched for cached only and we didn't find
5326 * anything, and we didn't start any caching kthreads
5327 * either, so chances are we will loop through and
5328 * start a couple caching kthreads, and then come back
5329 * around and just wait for them. This will be slower
5330 * because we will have 2 caching kthreads reading at
5331 * the same time when we could have just started one
5332 * and waited for it to get far enough to give us an
5333 * allocation, so go ahead and go to the wait caching
5334 * loop.
5335 */
5336 loop = LOOP_CACHING_WAIT;
5337 search_start = ideal_cache_offset;
5338 ideal_cache_percent = 0;
5339 goto ideal_cache;
5340 } else if (loop == LOOP_FIND_IDEAL) {
5341 /*
5342 * Didn't find a uncached bg, wait on anything we find
5343 * next.
5344 */
5345 loop = LOOP_CACHING_WAIT;
5346 goto search;
5347 }
5348
Josef Bacik723bda22011-05-27 16:11:38 -04005349 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04005350
5351 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik723bda22011-05-27 16:11:38 -04005352 if (allowed_chunk_alloc) {
5353 ret = do_chunk_alloc(trans, root, num_bytes +
5354 2 * 1024 * 1024, data,
5355 CHUNK_ALLOC_LIMITED);
5356 allowed_chunk_alloc = 0;
5357 if (ret == 1)
5358 done_chunk_alloc = 1;
5359 } else if (!done_chunk_alloc &&
5360 space_info->force_alloc ==
5361 CHUNK_ALLOC_NO_FORCE) {
5362 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
5363 }
5364
5365 /*
5366 * We didn't allocate a chunk, go ahead and drop the
5367 * empty size and loop again.
5368 */
5369 if (!done_chunk_alloc)
5370 loop = LOOP_NO_EMPTY_SIZE;
5371 }
5372
5373 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005374 empty_size = 0;
5375 empty_cluster = 0;
5376 }
Chris Mason42e70e72008-11-07 18:17:11 -05005377
Josef Bacik723bda22011-05-27 16:11:38 -04005378 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04005379 } else if (!ins->objectid) {
5380 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04005381 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005382 ret = 0;
5383 }
Chris Mason0b86a832008-03-24 15:01:56 -04005384
Chris Mason0f70abe2007-02-28 16:46:22 -05005385 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005386}
Chris Masonec44a352008-04-28 15:29:52 -04005387
Josef Bacik9ed74f22009-09-11 16:12:44 -04005388static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5389 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005390{
5391 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005392 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005393
Josef Bacik9ed74f22009-09-11 16:12:44 -04005394 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04005395 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
5396 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05005397 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005398 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005399 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005400 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005401 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5402 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005403 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005404 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005405 (unsigned long long)info->bytes_pinned,
5406 (unsigned long long)info->bytes_reserved,
5407 (unsigned long long)info->bytes_may_use,
5408 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005409 spin_unlock(&info->lock);
5410
5411 if (!dump_block_groups)
5412 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005413
Josef Bacik80eb2342008-10-29 14:49:05 -04005414 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005415again:
5416 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005417 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005418 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5419 "%llu pinned %llu reserved\n",
5420 (unsigned long long)cache->key.objectid,
5421 (unsigned long long)cache->key.offset,
5422 (unsigned long long)btrfs_block_group_used(&cache->item),
5423 (unsigned long long)cache->pinned,
5424 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005425 btrfs_dump_free_space(cache, bytes);
5426 spin_unlock(&cache->lock);
5427 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005428 if (++index < BTRFS_NR_RAID_TYPES)
5429 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005430 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005431}
Zheng Yane8569812008-09-26 10:05:48 -04005432
Yan Zheng11833d62009-09-11 16:11:19 -04005433int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5434 struct btrfs_root *root,
5435 u64 num_bytes, u64 min_alloc_size,
5436 u64 empty_size, u64 hint_byte,
5437 u64 search_end, struct btrfs_key *ins,
5438 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005439{
5440 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005441 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005442
Josef Bacik6a632092009-02-20 11:00:09 -05005443 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005444again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005445 /*
5446 * the only place that sets empty_size is btrfs_realloc_node, which
5447 * is not called recursively on allocations
5448 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005449 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005450 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005451 num_bytes + 2 * 1024 * 1024, data,
5452 CHUNK_ALLOC_NO_FORCE);
Chris Mason0b86a832008-03-24 15:01:56 -04005453
Chris Masondb945352007-10-15 16:15:53 -04005454 WARN_ON(num_bytes < root->sectorsize);
5455 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005456 search_start, search_end, hint_byte,
5457 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005458
Chris Mason98d20f62008-04-14 09:46:10 -04005459 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5460 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005461 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005462 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005463 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005464 num_bytes, data, CHUNK_ALLOC_FORCE);
Chris Mason98d20f62008-04-14 09:46:10 -04005465 goto again;
5466 }
Chris Mason91435652011-02-16 13:10:41 -05005467 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005468 struct btrfs_space_info *sinfo;
5469
5470 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005471 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5472 "wanted %llu\n", (unsigned long long)data,
5473 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005474 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005475 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005476
liubo1abe9b82011-03-24 11:18:59 +00005477 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5478
Josef Bacik0f9dd462008-09-23 13:14:11 -04005479 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005480}
5481
Chris Mason65b51a02008-08-01 15:11:20 -04005482int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5483{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005484 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005485 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005486
Josef Bacik0f9dd462008-09-23 13:14:11 -04005487 cache = btrfs_lookup_block_group(root->fs_info, start);
5488 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005489 printk(KERN_ERR "Unable to find block group for %llu\n",
5490 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005491 return -ENOSPC;
5492 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005493
Li Dongyang5378e602011-03-24 10:24:27 +00005494 if (btrfs_test_opt(root, DISCARD))
5495 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005496
Josef Bacik0f9dd462008-09-23 13:14:11 -04005497 btrfs_add_free_space(cache, start, len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005498 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005499 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005500
liubo1abe9b82011-03-24 11:18:59 +00005501 trace_btrfs_reserved_extent_free(root, start, len);
5502
Chris Masone6dcd2d2008-07-17 12:53:50 -04005503 return ret;
5504}
5505
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005506static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5507 struct btrfs_root *root,
5508 u64 parent, u64 root_objectid,
5509 u64 flags, u64 owner, u64 offset,
5510 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005511{
5512 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005513 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005514 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005515 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005516 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005517 struct extent_buffer *leaf;
5518 int type;
5519 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005520
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005521 if (parent > 0)
5522 type = BTRFS_SHARED_DATA_REF_KEY;
5523 else
5524 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005525
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005526 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005527
5528 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005529 if (!path)
5530 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005531
Chris Masonb9473432009-03-13 11:00:37 -04005532 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005533 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5534 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005535 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005536
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005537 leaf = path->nodes[0];
5538 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005539 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005540 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5541 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5542 btrfs_set_extent_flags(leaf, extent_item,
5543 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005544
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005545 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5546 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5547 if (parent > 0) {
5548 struct btrfs_shared_data_ref *ref;
5549 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5550 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5551 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5552 } else {
5553 struct btrfs_extent_data_ref *ref;
5554 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5555 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5556 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5557 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5558 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5559 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005560
5561 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005562 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005563
Yan, Zhengf0486c62010-05-16 10:46:25 -04005564 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005565 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005566 printk(KERN_ERR "btrfs update block group failed for %llu "
5567 "%llu\n", (unsigned long long)ins->objectid,
5568 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005569 BUG();
5570 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005571 return ret;
5572}
5573
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005574static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5575 struct btrfs_root *root,
5576 u64 parent, u64 root_objectid,
5577 u64 flags, struct btrfs_disk_key *key,
5578 int level, struct btrfs_key *ins)
5579{
5580 int ret;
5581 struct btrfs_fs_info *fs_info = root->fs_info;
5582 struct btrfs_extent_item *extent_item;
5583 struct btrfs_tree_block_info *block_info;
5584 struct btrfs_extent_inline_ref *iref;
5585 struct btrfs_path *path;
5586 struct extent_buffer *leaf;
5587 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5588
5589 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005590 if (!path)
5591 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005592
5593 path->leave_spinning = 1;
5594 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5595 ins, size);
5596 BUG_ON(ret);
5597
5598 leaf = path->nodes[0];
5599 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5600 struct btrfs_extent_item);
5601 btrfs_set_extent_refs(leaf, extent_item, 1);
5602 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5603 btrfs_set_extent_flags(leaf, extent_item,
5604 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5605 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5606
5607 btrfs_set_tree_block_key(leaf, block_info, key);
5608 btrfs_set_tree_block_level(leaf, block_info, level);
5609
5610 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5611 if (parent > 0) {
5612 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5613 btrfs_set_extent_inline_ref_type(leaf, iref,
5614 BTRFS_SHARED_BLOCK_REF_KEY);
5615 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5616 } else {
5617 btrfs_set_extent_inline_ref_type(leaf, iref,
5618 BTRFS_TREE_BLOCK_REF_KEY);
5619 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5620 }
5621
5622 btrfs_mark_buffer_dirty(leaf);
5623 btrfs_free_path(path);
5624
Yan, Zhengf0486c62010-05-16 10:46:25 -04005625 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005626 if (ret) {
5627 printk(KERN_ERR "btrfs update block group failed for %llu "
5628 "%llu\n", (unsigned long long)ins->objectid,
5629 (unsigned long long)ins->offset);
5630 BUG();
5631 }
5632 return ret;
5633}
5634
5635int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5636 struct btrfs_root *root,
5637 u64 root_objectid, u64 owner,
5638 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005639{
5640 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04005641
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005642 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005643
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005644 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5645 0, root_objectid, owner, offset,
5646 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005647 return ret;
5648}
Chris Masone02119d2008-09-05 16:13:11 -04005649
5650/*
5651 * this is used by the tree logging recovery code. It records that
5652 * an extent has been allocated and makes sure to clear the free
5653 * space cache bits as well
5654 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005655int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5656 struct btrfs_root *root,
5657 u64 root_objectid, u64 owner, u64 offset,
5658 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005659{
5660 int ret;
5661 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005662 struct btrfs_caching_control *caching_ctl;
5663 u64 start = ins->objectid;
5664 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005665
Chris Masone02119d2008-09-05 16:13:11 -04005666 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005667 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005668 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005669
Yan Zheng11833d62009-09-11 16:11:19 -04005670 if (!caching_ctl) {
5671 BUG_ON(!block_group_cache_done(block_group));
5672 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5673 BUG_ON(ret);
5674 } else {
5675 mutex_lock(&caching_ctl->mutex);
5676
5677 if (start >= caching_ctl->progress) {
5678 ret = add_excluded_extent(root, start, num_bytes);
5679 BUG_ON(ret);
5680 } else if (start + num_bytes <= caching_ctl->progress) {
5681 ret = btrfs_remove_free_space(block_group,
5682 start, num_bytes);
5683 BUG_ON(ret);
5684 } else {
5685 num_bytes = caching_ctl->progress - start;
5686 ret = btrfs_remove_free_space(block_group,
5687 start, num_bytes);
5688 BUG_ON(ret);
5689
5690 start = caching_ctl->progress;
5691 num_bytes = ins->objectid + ins->offset -
5692 caching_ctl->progress;
5693 ret = add_excluded_extent(root, start, num_bytes);
5694 BUG_ON(ret);
5695 }
5696
5697 mutex_unlock(&caching_ctl->mutex);
5698 put_caching_control(caching_ctl);
5699 }
5700
Josef Bacikfb25e912011-07-26 17:00:46 -04005701 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
5702 RESERVE_ALLOC_NO_ACCOUNT);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005703 BUG_ON(ret);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005704 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005705 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5706 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005707 return ret;
5708}
5709
Chris Mason65b51a02008-08-01 15:11:20 -04005710struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5711 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005712 u64 bytenr, u32 blocksize,
5713 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005714{
5715 struct extent_buffer *buf;
5716
5717 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5718 if (!buf)
5719 return ERR_PTR(-ENOMEM);
5720 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04005721 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005722 btrfs_tree_lock(buf);
5723 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005724
5725 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005726 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005727
Chris Masond0c803c2008-09-11 16:17:57 -04005728 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005729 /*
5730 * we allow two log transactions at a time, use different
5731 * EXENT bit to differentiate dirty pages.
5732 */
5733 if (root->log_transid % 2 == 0)
5734 set_extent_dirty(&root->dirty_log_pages, buf->start,
5735 buf->start + buf->len - 1, GFP_NOFS);
5736 else
5737 set_extent_new(&root->dirty_log_pages, buf->start,
5738 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005739 } else {
5740 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5741 buf->start + buf->len - 1, GFP_NOFS);
5742 }
Chris Mason65b51a02008-08-01 15:11:20 -04005743 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005744 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005745 return buf;
5746}
5747
Yan, Zhengf0486c62010-05-16 10:46:25 -04005748static struct btrfs_block_rsv *
5749use_block_rsv(struct btrfs_trans_handle *trans,
5750 struct btrfs_root *root, u32 blocksize)
5751{
5752 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005753 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005754 int ret;
5755
5756 block_rsv = get_block_rsv(trans, root);
5757
5758 if (block_rsv->size == 0) {
Josef Bacik36ba0222011-10-18 12:15:48 -04005759 ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005760 /*
5761 * If we couldn't reserve metadata bytes try and use some from
5762 * the global reserve.
5763 */
5764 if (ret && block_rsv != global_rsv) {
5765 ret = block_rsv_use_bytes(global_rsv, blocksize);
5766 if (!ret)
5767 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005768 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005769 } else if (ret) {
5770 return ERR_PTR(ret);
5771 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005772 return block_rsv;
5773 }
5774
5775 ret = block_rsv_use_bytes(block_rsv, blocksize);
5776 if (!ret)
5777 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005778 if (ret) {
5779 WARN_ON(1);
Josef Bacik36ba0222011-10-18 12:15:48 -04005780 ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005781 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00005782 return block_rsv;
5783 } else if (ret && block_rsv != global_rsv) {
5784 ret = block_rsv_use_bytes(global_rsv, blocksize);
5785 if (!ret)
5786 return global_rsv;
5787 }
5788 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005789
Yan, Zhengf0486c62010-05-16 10:46:25 -04005790 return ERR_PTR(-ENOSPC);
5791}
5792
5793static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5794{
5795 block_rsv_add_bytes(block_rsv, blocksize, 0);
5796 block_rsv_release_bytes(block_rsv, NULL, 0);
5797}
5798
Chris Masonfec577f2007-02-26 10:40:21 -05005799/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005800 * finds a free extent and does all the dirty work required for allocation
5801 * returns the key for the extent through ins, and a tree buffer for
5802 * the first block of the extent through buf.
5803 *
Chris Masonfec577f2007-02-26 10:40:21 -05005804 * returns the tree buffer or NULL.
5805 */
Chris Mason5f39d392007-10-15 16:14:19 -04005806struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005807 struct btrfs_root *root, u32 blocksize,
5808 u64 parent, u64 root_objectid,
5809 struct btrfs_disk_key *key, int level,
5810 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005811{
Chris Masone2fa7222007-03-12 16:22:34 -04005812 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005813 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005814 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005815 u64 flags = 0;
5816 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005817
Yan, Zhengf0486c62010-05-16 10:46:25 -04005818
5819 block_rsv = use_block_rsv(trans, root, blocksize);
5820 if (IS_ERR(block_rsv))
5821 return ERR_CAST(block_rsv);
5822
5823 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5824 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005825 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005826 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005827 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005828 }
Chris Mason55c69072008-01-09 15:55:33 -05005829
Chris Mason4008c042009-02-12 14:09:45 -05005830 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5831 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005832 BUG_ON(IS_ERR(buf));
5833
5834 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5835 if (parent == 0)
5836 parent = ins.objectid;
5837 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5838 } else
5839 BUG_ON(parent > 0);
5840
5841 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5842 struct btrfs_delayed_extent_op *extent_op;
5843 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5844 BUG_ON(!extent_op);
5845 if (key)
5846 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5847 else
5848 memset(&extent_op->key, 0, sizeof(extent_op->key));
5849 extent_op->flags_to_set = flags;
5850 extent_op->update_key = 1;
5851 extent_op->update_flags = 1;
5852 extent_op->is_data = 0;
5853
5854 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5855 ins.offset, parent, root_objectid,
5856 level, BTRFS_ADD_DELAYED_EXTENT,
5857 extent_op);
5858 BUG_ON(ret);
5859 }
Chris Masonfec577f2007-02-26 10:40:21 -05005860 return buf;
5861}
Chris Masona28ec192007-03-06 20:08:01 -05005862
Yan Zheng2c47e6052009-06-27 21:07:35 -04005863struct walk_control {
5864 u64 refs[BTRFS_MAX_LEVEL];
5865 u64 flags[BTRFS_MAX_LEVEL];
5866 struct btrfs_key update_progress;
5867 int stage;
5868 int level;
5869 int shared_level;
5870 int update_ref;
5871 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005872 int reada_slot;
5873 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005874};
5875
5876#define DROP_REFERENCE 1
5877#define UPDATE_BACKREF 2
5878
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005879static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5880 struct btrfs_root *root,
5881 struct walk_control *wc,
5882 struct btrfs_path *path)
5883{
5884 u64 bytenr;
5885 u64 generation;
5886 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005887 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005888 u32 nritems;
5889 u32 blocksize;
5890 struct btrfs_key key;
5891 struct extent_buffer *eb;
5892 int ret;
5893 int slot;
5894 int nread = 0;
5895
5896 if (path->slots[wc->level] < wc->reada_slot) {
5897 wc->reada_count = wc->reada_count * 2 / 3;
5898 wc->reada_count = max(wc->reada_count, 2);
5899 } else {
5900 wc->reada_count = wc->reada_count * 3 / 2;
5901 wc->reada_count = min_t(int, wc->reada_count,
5902 BTRFS_NODEPTRS_PER_BLOCK(root));
5903 }
5904
5905 eb = path->nodes[wc->level];
5906 nritems = btrfs_header_nritems(eb);
5907 blocksize = btrfs_level_size(root, wc->level - 1);
5908
5909 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5910 if (nread >= wc->reada_count)
5911 break;
5912
5913 cond_resched();
5914 bytenr = btrfs_node_blockptr(eb, slot);
5915 generation = btrfs_node_ptr_generation(eb, slot);
5916
5917 if (slot == path->slots[wc->level])
5918 goto reada;
5919
5920 if (wc->stage == UPDATE_BACKREF &&
5921 generation <= root->root_key.offset)
5922 continue;
5923
Yan, Zheng94fcca92009-10-09 09:25:16 -04005924 /* We don't lock the tree block, it's OK to be racy here */
5925 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5926 &refs, &flags);
5927 BUG_ON(ret);
5928 BUG_ON(refs == 0);
5929
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005930 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005931 if (refs == 1)
5932 goto reada;
5933
Yan, Zheng94fcca92009-10-09 09:25:16 -04005934 if (wc->level == 1 &&
5935 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5936 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005937 if (!wc->update_ref ||
5938 generation <= root->root_key.offset)
5939 continue;
5940 btrfs_node_key_to_cpu(eb, &key, slot);
5941 ret = btrfs_comp_cpu_keys(&key,
5942 &wc->update_progress);
5943 if (ret < 0)
5944 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005945 } else {
5946 if (wc->level == 1 &&
5947 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5948 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005949 }
5950reada:
5951 ret = readahead_tree_block(root, bytenr, blocksize,
5952 generation);
5953 if (ret)
5954 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005955 nread++;
5956 }
5957 wc->reada_slot = slot;
5958}
5959
Chris Mason9aca1d52007-03-13 11:09:37 -04005960/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005961 * hepler to process tree block while walking down the tree.
5962 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005963 * when wc->stage == UPDATE_BACKREF, this function updates
5964 * back refs for pointers in the block.
5965 *
5966 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005967 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005968static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5969 struct btrfs_root *root,
5970 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005971 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005972{
5973 int level = wc->level;
5974 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005975 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5976 int ret;
5977
5978 if (wc->stage == UPDATE_BACKREF &&
5979 btrfs_header_owner(eb) != root->root_key.objectid)
5980 return 1;
5981
5982 /*
5983 * when reference count of tree block is 1, it won't increase
5984 * again. once full backref flag is set, we never clear it.
5985 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005986 if (lookup_info &&
5987 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5988 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005989 BUG_ON(!path->locks[level]);
5990 ret = btrfs_lookup_extent_info(trans, root,
5991 eb->start, eb->len,
5992 &wc->refs[level],
5993 &wc->flags[level]);
5994 BUG_ON(ret);
5995 BUG_ON(wc->refs[level] == 0);
5996 }
5997
Yan Zheng2c47e6052009-06-27 21:07:35 -04005998 if (wc->stage == DROP_REFERENCE) {
5999 if (wc->refs[level] > 1)
6000 return 1;
6001
6002 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006003 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006004 path->locks[level] = 0;
6005 }
6006 return 0;
6007 }
6008
6009 /* wc->stage == UPDATE_BACKREF */
6010 if (!(wc->flags[level] & flag)) {
6011 BUG_ON(!path->locks[level]);
6012 ret = btrfs_inc_ref(trans, root, eb, 1);
6013 BUG_ON(ret);
6014 ret = btrfs_dec_ref(trans, root, eb, 0);
6015 BUG_ON(ret);
6016 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6017 eb->len, flag, 0);
6018 BUG_ON(ret);
6019 wc->flags[level] |= flag;
6020 }
6021
6022 /*
6023 * the block is shared by multiple trees, so it's not good to
6024 * keep the tree lock
6025 */
6026 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006027 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006028 path->locks[level] = 0;
6029 }
6030 return 0;
6031}
6032
6033/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006034 * hepler to process tree block pointer.
6035 *
6036 * when wc->stage == DROP_REFERENCE, this function checks
6037 * reference count of the block pointed to. if the block
6038 * is shared and we need update back refs for the subtree
6039 * rooted at the block, this function changes wc->stage to
6040 * UPDATE_BACKREF. if the block is shared and there is no
6041 * need to update back, this function drops the reference
6042 * to the block.
6043 *
6044 * NOTE: return value 1 means we should stop walking down.
6045 */
6046static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6047 struct btrfs_root *root,
6048 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006049 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006050{
6051 u64 bytenr;
6052 u64 generation;
6053 u64 parent;
6054 u32 blocksize;
6055 struct btrfs_key key;
6056 struct extent_buffer *next;
6057 int level = wc->level;
6058 int reada = 0;
6059 int ret = 0;
6060
6061 generation = btrfs_node_ptr_generation(path->nodes[level],
6062 path->slots[level]);
6063 /*
6064 * if the lower level block was created before the snapshot
6065 * was created, we know there is no need to update back refs
6066 * for the subtree
6067 */
6068 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006069 generation <= root->root_key.offset) {
6070 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006071 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006072 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006073
6074 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6075 blocksize = btrfs_level_size(root, level - 1);
6076
6077 next = btrfs_find_tree_block(root, bytenr, blocksize);
6078 if (!next) {
6079 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00006080 if (!next)
6081 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006082 reada = 1;
6083 }
6084 btrfs_tree_lock(next);
6085 btrfs_set_lock_blocking(next);
6086
Yan, Zheng94fcca92009-10-09 09:25:16 -04006087 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6088 &wc->refs[level - 1],
6089 &wc->flags[level - 1]);
6090 BUG_ON(ret);
6091 BUG_ON(wc->refs[level - 1] == 0);
6092 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006093
Yan, Zheng94fcca92009-10-09 09:25:16 -04006094 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006095 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006096 if (level == 1 &&
6097 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6098 goto skip;
6099
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006100 if (!wc->update_ref ||
6101 generation <= root->root_key.offset)
6102 goto skip;
6103
6104 btrfs_node_key_to_cpu(path->nodes[level], &key,
6105 path->slots[level]);
6106 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6107 if (ret < 0)
6108 goto skip;
6109
6110 wc->stage = UPDATE_BACKREF;
6111 wc->shared_level = level - 1;
6112 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006113 } else {
6114 if (level == 1 &&
6115 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6116 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006117 }
6118
6119 if (!btrfs_buffer_uptodate(next, generation)) {
6120 btrfs_tree_unlock(next);
6121 free_extent_buffer(next);
6122 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006123 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006124 }
6125
6126 if (!next) {
6127 if (reada && level == 1)
6128 reada_walk_down(trans, root, wc, path);
6129 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006130 if (!next)
6131 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006132 btrfs_tree_lock(next);
6133 btrfs_set_lock_blocking(next);
6134 }
6135
6136 level--;
6137 BUG_ON(level != btrfs_header_level(next));
6138 path->nodes[level] = next;
6139 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006140 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006141 wc->level = level;
6142 if (wc->level == 1)
6143 wc->reada_slot = 0;
6144 return 0;
6145skip:
6146 wc->refs[level - 1] = 0;
6147 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006148 if (wc->stage == DROP_REFERENCE) {
6149 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6150 parent = path->nodes[level]->start;
6151 } else {
6152 BUG_ON(root->root_key.objectid !=
6153 btrfs_header_owner(path->nodes[level]));
6154 parent = 0;
6155 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006156
Yan, Zheng94fcca92009-10-09 09:25:16 -04006157 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6158 root->root_key.objectid, level - 1, 0);
6159 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006160 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006161 btrfs_tree_unlock(next);
6162 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006163 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006164 return 1;
6165}
6166
6167/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006168 * hepler to process tree block while walking up the tree.
6169 *
6170 * when wc->stage == DROP_REFERENCE, this function drops
6171 * reference count on the block.
6172 *
6173 * when wc->stage == UPDATE_BACKREF, this function changes
6174 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6175 * to UPDATE_BACKREF previously while processing the block.
6176 *
6177 * NOTE: return value 1 means we should stop walking up.
6178 */
6179static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6180 struct btrfs_root *root,
6181 struct btrfs_path *path,
6182 struct walk_control *wc)
6183{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006184 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006185 int level = wc->level;
6186 struct extent_buffer *eb = path->nodes[level];
6187 u64 parent = 0;
6188
6189 if (wc->stage == UPDATE_BACKREF) {
6190 BUG_ON(wc->shared_level < level);
6191 if (level < wc->shared_level)
6192 goto out;
6193
Yan Zheng2c47e6052009-06-27 21:07:35 -04006194 ret = find_next_key(path, level + 1, &wc->update_progress);
6195 if (ret > 0)
6196 wc->update_ref = 0;
6197
6198 wc->stage = DROP_REFERENCE;
6199 wc->shared_level = -1;
6200 path->slots[level] = 0;
6201
6202 /*
6203 * check reference count again if the block isn't locked.
6204 * we should start walking down the tree again if reference
6205 * count is one.
6206 */
6207 if (!path->locks[level]) {
6208 BUG_ON(level == 0);
6209 btrfs_tree_lock(eb);
6210 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006211 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006212
6213 ret = btrfs_lookup_extent_info(trans, root,
6214 eb->start, eb->len,
6215 &wc->refs[level],
6216 &wc->flags[level]);
6217 BUG_ON(ret);
6218 BUG_ON(wc->refs[level] == 0);
6219 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04006220 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006221 return 1;
6222 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006223 }
6224 }
6225
6226 /* wc->stage == DROP_REFERENCE */
6227 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6228
6229 if (wc->refs[level] == 1) {
6230 if (level == 0) {
6231 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6232 ret = btrfs_dec_ref(trans, root, eb, 1);
6233 else
6234 ret = btrfs_dec_ref(trans, root, eb, 0);
6235 BUG_ON(ret);
6236 }
6237 /* make block locked assertion in clean_tree_block happy */
6238 if (!path->locks[level] &&
6239 btrfs_header_generation(eb) == trans->transid) {
6240 btrfs_tree_lock(eb);
6241 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006242 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006243 }
6244 clean_tree_block(trans, root, eb);
6245 }
6246
6247 if (eb == root->node) {
6248 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6249 parent = eb->start;
6250 else
6251 BUG_ON(root->root_key.objectid !=
6252 btrfs_header_owner(eb));
6253 } else {
6254 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6255 parent = path->nodes[level + 1]->start;
6256 else
6257 BUG_ON(root->root_key.objectid !=
6258 btrfs_header_owner(path->nodes[level + 1]));
6259 }
6260
Yan, Zhengf0486c62010-05-16 10:46:25 -04006261 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006262out:
6263 wc->refs[level] = 0;
6264 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006265 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006266}
6267
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006268static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6269 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006270 struct btrfs_path *path,
6271 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006272{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006273 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006274 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006275 int ret;
6276
Yan Zheng2c47e6052009-06-27 21:07:35 -04006277 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006278 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006279 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006280 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006281
Yan Zheng2c47e6052009-06-27 21:07:35 -04006282 if (level == 0)
6283 break;
6284
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006285 if (path->slots[level] >=
6286 btrfs_header_nritems(path->nodes[level]))
6287 break;
6288
Yan, Zheng94fcca92009-10-09 09:25:16 -04006289 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006290 if (ret > 0) {
6291 path->slots[level]++;
6292 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006293 } else if (ret < 0)
6294 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006295 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006296 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006297 return 0;
6298}
6299
Chris Masond3977122009-01-05 21:25:51 -05006300static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006301 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006302 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006303 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006304{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006305 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006306 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006307
Yan Zheng2c47e6052009-06-27 21:07:35 -04006308 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6309 while (level < max_level && path->nodes[level]) {
6310 wc->level = level;
6311 if (path->slots[level] + 1 <
6312 btrfs_header_nritems(path->nodes[level])) {
6313 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006314 return 0;
6315 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006316 ret = walk_up_proc(trans, root, path, wc);
6317 if (ret > 0)
6318 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006319
Yan Zheng2c47e6052009-06-27 21:07:35 -04006320 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04006321 btrfs_tree_unlock_rw(path->nodes[level],
6322 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006323 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006324 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006325 free_extent_buffer(path->nodes[level]);
6326 path->nodes[level] = NULL;
6327 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006328 }
6329 }
6330 return 1;
6331}
6332
Chris Mason9aca1d52007-03-13 11:09:37 -04006333/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006334 * drop a subvolume tree.
6335 *
6336 * this function traverses the tree freeing any blocks that only
6337 * referenced by the tree.
6338 *
6339 * when a shared tree block is found. this function decreases its
6340 * reference count by one. if update_ref is true, this function
6341 * also make sure backrefs for the shared block and all lower level
6342 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006343 */
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006344void btrfs_drop_snapshot(struct btrfs_root *root,
6345 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006346{
Chris Mason5caf2a02007-04-02 11:20:42 -04006347 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006348 struct btrfs_trans_handle *trans;
6349 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006350 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006351 struct walk_control *wc;
6352 struct btrfs_key key;
6353 int err = 0;
6354 int ret;
6355 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006356
Chris Mason5caf2a02007-04-02 11:20:42 -04006357 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006358 if (!path) {
6359 err = -ENOMEM;
6360 goto out;
6361 }
Chris Mason20524f02007-03-10 06:35:47 -05006362
Yan Zheng2c47e6052009-06-27 21:07:35 -04006363 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07006364 if (!wc) {
6365 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006366 err = -ENOMEM;
6367 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07006368 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006369
Yan, Zhenga22285a2010-05-16 10:48:46 -04006370 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006371 BUG_ON(IS_ERR(trans));
6372
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006373 if (block_rsv)
6374 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006375
Chris Mason9f3a7422007-08-07 15:52:19 -04006376 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006377 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006378 path->nodes[level] = btrfs_lock_root_node(root);
6379 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006380 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006381 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006382 memset(&wc->update_progress, 0,
6383 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006384 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006385 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006386 memcpy(&wc->update_progress, &key,
6387 sizeof(wc->update_progress));
6388
Chris Mason6702ed42007-08-07 16:15:09 -04006389 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006390 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006391 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006392 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6393 path->lowest_level = 0;
6394 if (ret < 0) {
6395 err = ret;
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006396 goto out_free;
Chris Mason9f3a7422007-08-07 15:52:19 -04006397 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006398 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006399
Chris Mason7d9eb122008-07-08 14:19:17 -04006400 /*
6401 * unlock our path, this is safe because only this
6402 * function is allowed to delete this snapshot
6403 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006404 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006405
Yan Zheng2c47e6052009-06-27 21:07:35 -04006406 level = btrfs_header_level(root->node);
6407 while (1) {
6408 btrfs_tree_lock(path->nodes[level]);
6409 btrfs_set_lock_blocking(path->nodes[level]);
6410
6411 ret = btrfs_lookup_extent_info(trans, root,
6412 path->nodes[level]->start,
6413 path->nodes[level]->len,
6414 &wc->refs[level],
6415 &wc->flags[level]);
6416 BUG_ON(ret);
6417 BUG_ON(wc->refs[level] == 0);
6418
6419 if (level == root_item->drop_level)
6420 break;
6421
6422 btrfs_tree_unlock(path->nodes[level]);
6423 WARN_ON(wc->refs[level] != 1);
6424 level--;
6425 }
6426 }
6427
6428 wc->level = level;
6429 wc->shared_level = -1;
6430 wc->stage = DROP_REFERENCE;
6431 wc->update_ref = update_ref;
6432 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006433 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006434
6435 while (1) {
6436 ret = walk_down_tree(trans, root, path, wc);
6437 if (ret < 0) {
6438 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006439 break;
6440 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006441
6442 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6443 if (ret < 0) {
6444 err = ret;
6445 break;
6446 }
6447
6448 if (ret > 0) {
6449 BUG_ON(wc->stage != DROP_REFERENCE);
6450 break;
6451 }
6452
6453 if (wc->stage == DROP_REFERENCE) {
6454 level = wc->level;
6455 btrfs_node_key(path->nodes[level],
6456 &root_item->drop_progress,
6457 path->slots[level]);
6458 root_item->drop_level = level;
6459 }
6460
6461 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006462 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006463 ret = btrfs_update_root(trans, tree_root,
6464 &root->root_key,
6465 root_item);
6466 BUG_ON(ret);
6467
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006468 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006469 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006470 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006471 if (block_rsv)
6472 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006473 }
Chris Mason20524f02007-03-10 06:35:47 -05006474 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006475 btrfs_release_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006476 BUG_ON(err);
6477
6478 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6479 BUG_ON(ret);
6480
Yan, Zheng76dda932009-09-21 16:00:26 -04006481 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6482 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6483 NULL, NULL);
6484 BUG_ON(ret < 0);
6485 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006486 /* if we fail to delete the orphan item this time
6487 * around, it'll get picked up the next time.
6488 *
6489 * The most common failure here is just -ENOENT.
6490 */
6491 btrfs_del_orphan_item(trans, tree_root,
6492 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006493 }
6494 }
6495
6496 if (root->in_radix) {
6497 btrfs_free_fs_root(tree_root->fs_info, root);
6498 } else {
6499 free_extent_buffer(root->node);
6500 free_extent_buffer(root->commit_root);
6501 kfree(root);
6502 }
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006503out_free:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006504 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006505 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006506 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006507out:
6508 if (err)
6509 btrfs_std_error(root->fs_info, err);
6510 return;
Chris Mason20524f02007-03-10 06:35:47 -05006511}
Chris Mason9078a3e2007-04-26 16:46:15 -04006512
Yan Zheng2c47e6052009-06-27 21:07:35 -04006513/*
6514 * drop subtree rooted at tree block 'node'.
6515 *
6516 * NOTE: this function will unlock and release tree block 'node'
6517 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006518int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6519 struct btrfs_root *root,
6520 struct extent_buffer *node,
6521 struct extent_buffer *parent)
6522{
6523 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006524 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006525 int level;
6526 int parent_level;
6527 int ret = 0;
6528 int wret;
6529
Yan Zheng2c47e6052009-06-27 21:07:35 -04006530 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6531
Yan Zhengf82d02d2008-10-29 14:49:05 -04006532 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006533 if (!path)
6534 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006535
Yan Zheng2c47e6052009-06-27 21:07:35 -04006536 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006537 if (!wc) {
6538 btrfs_free_path(path);
6539 return -ENOMEM;
6540 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006541
Chris Masonb9447ef2009-03-09 11:45:38 -04006542 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006543 parent_level = btrfs_header_level(parent);
6544 extent_buffer_get(parent);
6545 path->nodes[parent_level] = parent;
6546 path->slots[parent_level] = btrfs_header_nritems(parent);
6547
Chris Masonb9447ef2009-03-09 11:45:38 -04006548 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006549 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006550 path->nodes[level] = node;
6551 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006552 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006553
6554 wc->refs[parent_level] = 1;
6555 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6556 wc->level = level;
6557 wc->shared_level = -1;
6558 wc->stage = DROP_REFERENCE;
6559 wc->update_ref = 0;
6560 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006561 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006562
6563 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006564 wret = walk_down_tree(trans, root, path, wc);
6565 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006566 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006567 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006568 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006569
Yan Zheng2c47e6052009-06-27 21:07:35 -04006570 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006571 if (wret < 0)
6572 ret = wret;
6573 if (wret != 0)
6574 break;
6575 }
6576
Yan Zheng2c47e6052009-06-27 21:07:35 -04006577 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006578 btrfs_free_path(path);
6579 return ret;
6580}
6581
Chris Masonec44a352008-04-28 15:29:52 -04006582static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6583{
6584 u64 num_devices;
6585 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6586 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6587
Chris Masoncd02dca2010-12-13 14:56:23 -05006588 /*
6589 * we add in the count of missing devices because we want
6590 * to make sure that any RAID levels on a degraded FS
6591 * continue to be honored.
6592 */
6593 num_devices = root->fs_info->fs_devices->rw_devices +
6594 root->fs_info->fs_devices->missing_devices;
6595
Chris Masonec44a352008-04-28 15:29:52 -04006596 if (num_devices == 1) {
6597 stripped |= BTRFS_BLOCK_GROUP_DUP;
6598 stripped = flags & ~stripped;
6599
6600 /* turn raid0 into single device chunks */
6601 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6602 return stripped;
6603
6604 /* turn mirroring into duplication */
6605 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6606 BTRFS_BLOCK_GROUP_RAID10))
6607 return stripped | BTRFS_BLOCK_GROUP_DUP;
6608 return flags;
6609 } else {
6610 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006611 if (flags & stripped)
6612 return flags;
6613
6614 stripped |= BTRFS_BLOCK_GROUP_DUP;
6615 stripped = flags & ~stripped;
6616
6617 /* switch duplicated blocks with raid1 */
6618 if (flags & BTRFS_BLOCK_GROUP_DUP)
6619 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6620
6621 /* turn single device chunks into raid0 */
6622 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6623 }
6624 return flags;
6625}
6626
Miao Xie199c36e2011-07-15 10:34:36 +00006627static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04006628{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006629 struct btrfs_space_info *sinfo = cache->space_info;
6630 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00006631 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006632 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04006633
Chris Masonc286ac42008-07-22 23:06:41 -04006634
Miao Xie199c36e2011-07-15 10:34:36 +00006635 /*
6636 * We need some metadata space and system metadata space for
6637 * allocating chunks in some corner cases until we force to set
6638 * it to be readonly.
6639 */
6640 if ((sinfo->flags &
6641 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
6642 !force)
6643 min_allocable_bytes = 1 * 1024 * 1024;
6644 else
6645 min_allocable_bytes = 0;
6646
Yan, Zhengf0486c62010-05-16 10:46:25 -04006647 spin_lock(&sinfo->lock);
6648 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00006649
6650 if (cache->ro) {
6651 ret = 0;
6652 goto out;
6653 }
6654
Yan, Zhengf0486c62010-05-16 10:46:25 -04006655 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6656 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04006657
Yan, Zhengf0486c62010-05-16 10:46:25 -04006658 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04006659 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
6660 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04006661 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006662 cache->ro = 1;
6663 ret = 0;
6664 }
WuBo61cfea92011-07-26 03:30:11 +00006665out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04006666 spin_unlock(&cache->lock);
6667 spin_unlock(&sinfo->lock);
6668 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04006669}
6670
Yan, Zhengf0486c62010-05-16 10:46:25 -04006671int btrfs_set_block_group_ro(struct btrfs_root *root,
6672 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006673
6674{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006675 struct btrfs_trans_handle *trans;
6676 u64 alloc_flags;
6677 int ret;
6678
6679 BUG_ON(cache->ro);
6680
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006681 trans = btrfs_join_transaction(root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006682 BUG_ON(IS_ERR(trans));
6683
6684 alloc_flags = update_block_group_flags(root, cache->flags);
6685 if (alloc_flags != cache->flags)
Chris Mason0e4f8f82011-04-15 16:05:44 -04006686 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6687 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006688
Miao Xie199c36e2011-07-15 10:34:36 +00006689 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006690 if (!ret)
6691 goto out;
6692 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006693 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6694 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006695 if (ret < 0)
6696 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00006697 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006698out:
6699 btrfs_end_transaction(trans, root);
6700 return ret;
6701}
6702
Chris Masonc87f08c2011-02-16 13:57:04 -05006703int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
6704 struct btrfs_root *root, u64 type)
6705{
6706 u64 alloc_flags = get_alloc_profile(root, type);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006707 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6708 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05006709}
6710
Miao Xie6d07bce2011-01-05 10:07:31 +00006711/*
6712 * helper to account the unused space of all the readonly block group in the
6713 * list. takes mirrors into account.
6714 */
6715static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
6716{
6717 struct btrfs_block_group_cache *block_group;
6718 u64 free_bytes = 0;
6719 int factor;
6720
6721 list_for_each_entry(block_group, groups_list, list) {
6722 spin_lock(&block_group->lock);
6723
6724 if (!block_group->ro) {
6725 spin_unlock(&block_group->lock);
6726 continue;
6727 }
6728
6729 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
6730 BTRFS_BLOCK_GROUP_RAID10 |
6731 BTRFS_BLOCK_GROUP_DUP))
6732 factor = 2;
6733 else
6734 factor = 1;
6735
6736 free_bytes += (block_group->key.offset -
6737 btrfs_block_group_used(&block_group->item)) *
6738 factor;
6739
6740 spin_unlock(&block_group->lock);
6741 }
6742
6743 return free_bytes;
6744}
6745
6746/*
6747 * helper to account the unused space of all the readonly block group in the
6748 * space_info. takes mirrors into account.
6749 */
6750u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
6751{
6752 int i;
6753 u64 free_bytes = 0;
6754
6755 spin_lock(&sinfo->lock);
6756
6757 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
6758 if (!list_empty(&sinfo->block_groups[i]))
6759 free_bytes += __btrfs_get_ro_block_group_free_space(
6760 &sinfo->block_groups[i]);
6761
6762 spin_unlock(&sinfo->lock);
6763
6764 return free_bytes;
6765}
6766
Yan, Zhengf0486c62010-05-16 10:46:25 -04006767int btrfs_set_block_group_rw(struct btrfs_root *root,
6768 struct btrfs_block_group_cache *cache)
6769{
6770 struct btrfs_space_info *sinfo = cache->space_info;
6771 u64 num_bytes;
6772
6773 BUG_ON(!cache->ro);
6774
6775 spin_lock(&sinfo->lock);
6776 spin_lock(&cache->lock);
6777 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6778 cache->bytes_super - btrfs_block_group_used(&cache->item);
6779 sinfo->bytes_readonly -= num_bytes;
6780 cache->ro = 0;
6781 spin_unlock(&cache->lock);
6782 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006783 return 0;
6784}
6785
Josef Bacikba1bf482009-09-11 16:11:19 -04006786/*
6787 * checks to see if its even possible to relocate this block group.
6788 *
6789 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6790 * ok to go ahead and try.
6791 */
6792int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04006793{
Zheng Yan1a40e232008-09-26 10:09:34 -04006794 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04006795 struct btrfs_space_info *space_info;
6796 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6797 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00006798 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04006799 u64 dev_min = 1;
6800 u64 dev_nr = 0;
liubocdcb7252011-08-03 10:15:25 +00006801 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04006802 int full = 0;
6803 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006804
Josef Bacikba1bf482009-09-11 16:11:19 -04006805 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04006806
Josef Bacikba1bf482009-09-11 16:11:19 -04006807 /* odd, couldn't find the block group, leave it alone */
6808 if (!block_group)
6809 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006810
liubocdcb7252011-08-03 10:15:25 +00006811 min_free = btrfs_block_group_used(&block_group->item);
6812
Josef Bacikba1bf482009-09-11 16:11:19 -04006813 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00006814 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04006815 goto out;
Chris Mason323da792008-05-09 11:46:48 -04006816
Josef Bacikba1bf482009-09-11 16:11:19 -04006817 space_info = block_group->space_info;
6818 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04006819
Josef Bacikba1bf482009-09-11 16:11:19 -04006820 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04006821
Josef Bacikba1bf482009-09-11 16:11:19 -04006822 /*
6823 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04006824 * relocate it unless we're able to allocate a new chunk below.
6825 *
6826 * Otherwise, we need to make sure we have room in the space to handle
6827 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04006828 */
Chris Mason7ce618d2009-09-22 14:48:44 -04006829 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00006830 (space_info->bytes_used + space_info->bytes_reserved +
6831 space_info->bytes_pinned + space_info->bytes_readonly +
6832 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04006833 spin_unlock(&space_info->lock);
6834 goto out;
6835 }
6836 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006837
Josef Bacikba1bf482009-09-11 16:11:19 -04006838 /*
6839 * ok we don't have enough space, but maybe we have free space on our
6840 * devices to allocate new chunks for relocation, so loop through our
6841 * alloc devices and guess if we have enough space. However, if we
6842 * were marked as full, then we know there aren't enough chunks, and we
6843 * can just return.
6844 */
6845 ret = -1;
6846 if (full)
6847 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05006848
liubocdcb7252011-08-03 10:15:25 +00006849 /*
6850 * index:
6851 * 0: raid10
6852 * 1: raid1
6853 * 2: dup
6854 * 3: raid0
6855 * 4: single
6856 */
6857 index = get_block_group_index(block_group);
6858 if (index == 0) {
6859 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04006860 /* Divide by 2 */
6861 min_free >>= 1;
liubocdcb7252011-08-03 10:15:25 +00006862 } else if (index == 1) {
6863 dev_min = 2;
6864 } else if (index == 2) {
Josef Bacik6719db62011-08-20 08:29:51 -04006865 /* Multiply by 2 */
6866 min_free <<= 1;
liubocdcb7252011-08-03 10:15:25 +00006867 } else if (index == 3) {
6868 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04006869 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00006870 }
6871
Josef Bacikba1bf482009-09-11 16:11:19 -04006872 mutex_lock(&root->fs_info->chunk_mutex);
6873 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00006874 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04006875
Josef Bacikba1bf482009-09-11 16:11:19 -04006876 /*
6877 * check to make sure we can actually find a chunk with enough
6878 * space to fit our block group in.
6879 */
6880 if (device->total_bytes > device->bytes_used + min_free) {
6881 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00006882 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04006883 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00006884 dev_nr++;
6885
6886 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05006887 break;
liubocdcb7252011-08-03 10:15:25 +00006888
Josef Bacikba1bf482009-09-11 16:11:19 -04006889 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05006890 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006891 }
Josef Bacikba1bf482009-09-11 16:11:19 -04006892 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05006893out:
Josef Bacikba1bf482009-09-11 16:11:19 -04006894 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006895 return ret;
6896}
6897
Christoph Hellwigb2950862008-12-02 09:54:17 -05006898static int find_first_block_group(struct btrfs_root *root,
6899 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04006900{
Chris Mason925baed2008-06-25 16:01:30 -04006901 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006902 struct btrfs_key found_key;
6903 struct extent_buffer *leaf;
6904 int slot;
6905
6906 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6907 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006908 goto out;
6909
Chris Masond3977122009-01-05 21:25:51 -05006910 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006911 slot = path->slots[0];
6912 leaf = path->nodes[0];
6913 if (slot >= btrfs_header_nritems(leaf)) {
6914 ret = btrfs_next_leaf(root, path);
6915 if (ret == 0)
6916 continue;
6917 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006918 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04006919 break;
6920 }
6921 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6922
6923 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04006924 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6925 ret = 0;
6926 goto out;
6927 }
Chris Mason0b86a832008-03-24 15:01:56 -04006928 path->slots[0]++;
6929 }
Chris Mason925baed2008-06-25 16:01:30 -04006930out:
Chris Mason0b86a832008-03-24 15:01:56 -04006931 return ret;
6932}
6933
Josef Bacik0af3d002010-06-21 14:48:16 -04006934void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
6935{
6936 struct btrfs_block_group_cache *block_group;
6937 u64 last = 0;
6938
6939 while (1) {
6940 struct inode *inode;
6941
6942 block_group = btrfs_lookup_first_block_group(info, last);
6943 while (block_group) {
6944 spin_lock(&block_group->lock);
6945 if (block_group->iref)
6946 break;
6947 spin_unlock(&block_group->lock);
6948 block_group = next_block_group(info->tree_root,
6949 block_group);
6950 }
6951 if (!block_group) {
6952 if (last == 0)
6953 break;
6954 last = 0;
6955 continue;
6956 }
6957
6958 inode = block_group->inode;
6959 block_group->iref = 0;
6960 block_group->inode = NULL;
6961 spin_unlock(&block_group->lock);
6962 iput(inode);
6963 last = block_group->key.objectid + block_group->key.offset;
6964 btrfs_put_block_group(block_group);
6965 }
6966}
6967
Zheng Yan1a40e232008-09-26 10:09:34 -04006968int btrfs_free_block_groups(struct btrfs_fs_info *info)
6969{
6970 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04006971 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04006972 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04006973 struct rb_node *n;
6974
Yan Zheng11833d62009-09-11 16:11:19 -04006975 down_write(&info->extent_commit_sem);
6976 while (!list_empty(&info->caching_block_groups)) {
6977 caching_ctl = list_entry(info->caching_block_groups.next,
6978 struct btrfs_caching_control, list);
6979 list_del(&caching_ctl->list);
6980 put_caching_control(caching_ctl);
6981 }
6982 up_write(&info->extent_commit_sem);
6983
Zheng Yan1a40e232008-09-26 10:09:34 -04006984 spin_lock(&info->block_group_cache_lock);
6985 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6986 block_group = rb_entry(n, struct btrfs_block_group_cache,
6987 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04006988 rb_erase(&block_group->cache_node,
6989 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04006990 spin_unlock(&info->block_group_cache_lock);
6991
Josef Bacik80eb2342008-10-29 14:49:05 -04006992 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006993 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006994 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006995
Josef Bacik817d52f2009-07-13 21:29:25 -04006996 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04006997 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04006998
Josef Bacik3c148742011-02-02 15:53:47 +00006999 /*
7000 * We haven't cached this block group, which means we could
7001 * possibly have excluded extents on this block group.
7002 */
7003 if (block_group->cached == BTRFS_CACHE_NO)
7004 free_excluded_extents(info->extent_root, block_group);
7005
Josef Bacik817d52f2009-07-13 21:29:25 -04007006 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007007 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007008
7009 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007010 }
7011 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007012
7013 /* now that all the block groups are freed, go through and
7014 * free all the space_info structs. This is only called during
7015 * the final stages of unmount, and so we know nobody is
7016 * using them. We call synchronize_rcu() once before we start,
7017 * just to be on the safe side.
7018 */
7019 synchronize_rcu();
7020
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007021 release_global_block_rsv(info);
7022
Chris Mason4184ea72009-03-10 12:39:20 -04007023 while(!list_empty(&info->space_info)) {
7024 space_info = list_entry(info->space_info.next,
7025 struct btrfs_space_info,
7026 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007027 if (space_info->bytes_pinned > 0 ||
Josef Bacikfb25e912011-07-26 17:00:46 -04007028 space_info->bytes_reserved > 0 ||
7029 space_info->bytes_may_use > 0) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007030 WARN_ON(1);
7031 dump_space_info(space_info, 0, 0);
7032 }
Chris Mason4184ea72009-03-10 12:39:20 -04007033 list_del(&space_info->list);
7034 kfree(space_info);
7035 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007036 return 0;
7037}
7038
Yan, Zhengb742bb82010-05-16 10:46:24 -04007039static void __link_block_group(struct btrfs_space_info *space_info,
7040 struct btrfs_block_group_cache *cache)
7041{
7042 int index = get_block_group_index(cache);
7043
7044 down_write(&space_info->groups_sem);
7045 list_add_tail(&cache->list, &space_info->block_groups[index]);
7046 up_write(&space_info->groups_sem);
7047}
7048
Chris Mason9078a3e2007-04-26 16:46:15 -04007049int btrfs_read_block_groups(struct btrfs_root *root)
7050{
7051 struct btrfs_path *path;
7052 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007053 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007054 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007055 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007056 struct btrfs_key key;
7057 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007058 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04007059 int need_clear = 0;
7060 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04007061
Chris Masonbe744172007-05-06 10:15:01 -04007062 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007063 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007064 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007065 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007066 path = btrfs_alloc_path();
7067 if (!path)
7068 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04007069 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04007070
Josef Bacik0af3d002010-06-21 14:48:16 -04007071 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04007072 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04007073 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
7074 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04007075 if (btrfs_test_opt(root, CLEAR_CACHE))
7076 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04007077
Chris Masond3977122009-01-05 21:25:51 -05007078 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007079 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007080 if (ret > 0)
7081 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007082 if (ret != 0)
7083 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04007084 leaf = path->nodes[0];
7085 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007086 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007087 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007088 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007089 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007090 }
Li Zefan34d52cb2011-03-29 13:46:06 +08007091 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7092 GFP_NOFS);
7093 if (!cache->free_space_ctl) {
7094 kfree(cache);
7095 ret = -ENOMEM;
7096 goto error;
7097 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007098
Yan Zhengd2fb3432008-12-11 16:30:39 -05007099 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007100 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007101 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007102 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007103 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007104
Josef Bacik0af3d002010-06-21 14:48:16 -04007105 if (need_clear)
7106 cache->disk_cache_state = BTRFS_DC_CLEAR;
7107
Chris Mason5f39d392007-10-15 16:14:19 -04007108 read_extent_buffer(leaf, &cache->item,
7109 btrfs_item_ptr_offset(leaf, path->slots[0]),
7110 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007111 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007112
Chris Mason9078a3e2007-04-26 16:46:15 -04007113 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02007114 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007115 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007116 cache->sectorsize = root->sectorsize;
7117
Li Zefan34d52cb2011-03-29 13:46:06 +08007118 btrfs_init_free_space_ctl(cache);
7119
Josef Bacik817d52f2009-07-13 21:29:25 -04007120 /*
Josef Bacik3c148742011-02-02 15:53:47 +00007121 * We need to exclude the super stripes now so that the space
7122 * info has super bytes accounted for, otherwise we'll think
7123 * we have more space than we actually do.
7124 */
7125 exclude_super_stripes(root, cache);
7126
7127 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04007128 * check for two cases, either we are full, and therefore
7129 * don't need to bother with the caching work since we won't
7130 * find any space, or we are empty, and we can just add all
7131 * the space in and be done with it. This saves us _alot_ of
7132 * time, particularly in the full case.
7133 */
7134 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007135 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007136 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007137 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007138 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007139 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007140 cache->cached = BTRFS_CACHE_FINISHED;
7141 add_new_free_space(cache, root->fs_info,
7142 found_key.objectid,
7143 found_key.objectid +
7144 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007145 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007146 }
Chris Mason96b51792007-10-15 16:15:19 -04007147
Chris Mason6324fbf2008-03-24 15:01:59 -04007148 ret = update_space_info(info, cache->flags, found_key.offset,
7149 btrfs_block_group_used(&cache->item),
7150 &space_info);
7151 BUG_ON(ret);
7152 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007153 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007154 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007155 spin_unlock(&cache->space_info->lock);
7156
Yan, Zhengb742bb82010-05-16 10:46:24 -04007157 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007158
Josef Bacik0f9dd462008-09-23 13:14:11 -04007159 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7160 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04007161
7162 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007163 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00007164 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04007165 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007166
7167 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7168 if (!(get_alloc_profile(root, space_info->flags) &
7169 (BTRFS_BLOCK_GROUP_RAID10 |
7170 BTRFS_BLOCK_GROUP_RAID1 |
7171 BTRFS_BLOCK_GROUP_DUP)))
7172 continue;
7173 /*
7174 * avoid allocating from un-mirrored block group if there are
7175 * mirrored block groups.
7176 */
7177 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007178 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007179 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007180 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007181 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007182
7183 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007184 ret = 0;
7185error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007186 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007187 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007188}
Chris Mason6324fbf2008-03-24 15:01:59 -04007189
7190int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7191 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007192 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007193 u64 size)
7194{
7195 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007196 struct btrfs_root *extent_root;
7197 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007198
7199 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007200
Chris Mason12fcfd22009-03-24 10:24:20 -04007201 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007202
Chris Mason8f18cf12008-04-25 16:53:30 -04007203 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007204 if (!cache)
7205 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08007206 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7207 GFP_NOFS);
7208 if (!cache->free_space_ctl) {
7209 kfree(cache);
7210 return -ENOMEM;
7211 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007212
Chris Masone17cade2008-04-15 15:41:47 -04007213 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007214 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007215 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007216 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007217 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04007218
Yan Zhengd2fb3432008-12-11 16:30:39 -05007219 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007220 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007221 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007222 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007223
Li Zefan34d52cb2011-03-29 13:46:06 +08007224 btrfs_init_free_space_ctl(cache);
7225
Chris Mason6324fbf2008-03-24 15:01:59 -04007226 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007227 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7228 cache->flags = type;
7229 btrfs_set_block_group_flags(&cache->item, type);
7230
Yan Zheng11833d62009-09-11 16:11:19 -04007231 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007232 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007233 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007234
Josef Bacik817d52f2009-07-13 21:29:25 -04007235 add_new_free_space(cache, root->fs_info, chunk_offset,
7236 chunk_offset + size);
7237
Yan Zheng11833d62009-09-11 16:11:19 -04007238 free_excluded_extents(root, cache);
7239
Chris Mason6324fbf2008-03-24 15:01:59 -04007240 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7241 &cache->space_info);
7242 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007243
7244 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007245 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007246 spin_unlock(&cache->space_info->lock);
7247
Yan, Zhengb742bb82010-05-16 10:46:24 -04007248 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007249
Josef Bacik0f9dd462008-09-23 13:14:11 -04007250 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7251 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007252
Chris Mason6324fbf2008-03-24 15:01:59 -04007253 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7254 sizeof(cache->item));
7255 BUG_ON(ret);
7256
Chris Masond18a2c42008-04-04 15:40:00 -04007257 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007258
Chris Mason6324fbf2008-03-24 15:01:59 -04007259 return 0;
7260}
Zheng Yan1a40e232008-09-26 10:09:34 -04007261
7262int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7263 struct btrfs_root *root, u64 group_start)
7264{
7265 struct btrfs_path *path;
7266 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007267 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04007268 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04007269 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04007270 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04007271 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04007272 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04007273
Zheng Yan1a40e232008-09-26 10:09:34 -04007274 root = root->fs_info->extent_root;
7275
7276 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7277 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007278 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007279
liubo9f7c43c2011-03-07 02:13:33 +00007280 /*
7281 * Free the reserved super bytes from this block group before
7282 * remove it.
7283 */
7284 free_excluded_extents(root, block_group);
7285
Zheng Yan1a40e232008-09-26 10:09:34 -04007286 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04007287 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7288 BTRFS_BLOCK_GROUP_RAID1 |
7289 BTRFS_BLOCK_GROUP_RAID10))
7290 factor = 2;
7291 else
7292 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007293
Chris Mason44fb5512009-06-04 15:34:51 -04007294 /* make sure this block group isn't part of an allocation cluster */
7295 cluster = &root->fs_info->data_alloc_cluster;
7296 spin_lock(&cluster->refill_lock);
7297 btrfs_return_cluster_to_free_space(block_group, cluster);
7298 spin_unlock(&cluster->refill_lock);
7299
7300 /*
7301 * make sure this block group isn't part of a metadata
7302 * allocation cluster
7303 */
7304 cluster = &root->fs_info->meta_alloc_cluster;
7305 spin_lock(&cluster->refill_lock);
7306 btrfs_return_cluster_to_free_space(block_group, cluster);
7307 spin_unlock(&cluster->refill_lock);
7308
Zheng Yan1a40e232008-09-26 10:09:34 -04007309 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007310 if (!path) {
7311 ret = -ENOMEM;
7312 goto out;
7313 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007314
Ilya Dryomov10b2f342011-10-02 13:56:53 +03007315 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007316 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00007317 ret = btrfs_orphan_add(trans, inode);
7318 BUG_ON(ret);
Josef Bacik0af3d002010-06-21 14:48:16 -04007319 clear_nlink(inode);
7320 /* One for the block groups ref */
7321 spin_lock(&block_group->lock);
7322 if (block_group->iref) {
7323 block_group->iref = 0;
7324 block_group->inode = NULL;
7325 spin_unlock(&block_group->lock);
7326 iput(inode);
7327 } else {
7328 spin_unlock(&block_group->lock);
7329 }
7330 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04007331 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04007332 }
7333
7334 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
7335 key.offset = block_group->key.objectid;
7336 key.type = 0;
7337
7338 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
7339 if (ret < 0)
7340 goto out;
7341 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02007342 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007343 if (ret == 0) {
7344 ret = btrfs_del_item(trans, tree_root, path);
7345 if (ret)
7346 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02007347 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007348 }
7349
Yan Zheng3dfdb932009-01-21 10:49:16 -05007350 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007351 rb_erase(&block_group->cache_node,
7352 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007353 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007354
Josef Bacik80eb2342008-10-29 14:49:05 -04007355 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007356 /*
7357 * we must use list_del_init so people can check to see if they
7358 * are still on the list after taking the semaphore
7359 */
7360 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007361 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007362
Josef Bacik817d52f2009-07-13 21:29:25 -04007363 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007364 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007365
7366 btrfs_remove_free_space_cache(block_group);
7367
Yan Zhengc146afa2008-11-12 14:34:12 -05007368 spin_lock(&block_group->space_info->lock);
7369 block_group->space_info->total_bytes -= block_group->key.offset;
7370 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04007371 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05007372 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007373
Josef Bacik0af3d002010-06-21 14:48:16 -04007374 memcpy(&key, &block_group->key, sizeof(key));
7375
Chris Mason283bb192009-07-24 16:30:55 -04007376 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007377
Chris Masonfa9c0d792009-04-03 09:47:43 -04007378 btrfs_put_block_group(block_group);
7379 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007380
7381 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7382 if (ret > 0)
7383 ret = -EIO;
7384 if (ret < 0)
7385 goto out;
7386
7387 ret = btrfs_del_item(trans, root, path);
7388out:
7389 btrfs_free_path(path);
7390 return ret;
7391}
liuboacce9522011-01-06 19:30:25 +08007392
liuboc59021f2011-03-07 02:13:14 +00007393int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
7394{
7395 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00007396 struct btrfs_super_block *disk_super;
7397 u64 features;
7398 u64 flags;
7399 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00007400 int ret;
7401
liubo1aba86d2011-04-08 08:44:37 +00007402 disk_super = &fs_info->super_copy;
7403 if (!btrfs_super_root(disk_super))
7404 return 1;
liuboc59021f2011-03-07 02:13:14 +00007405
liubo1aba86d2011-04-08 08:44:37 +00007406 features = btrfs_super_incompat_flags(disk_super);
7407 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
7408 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00007409
liubo1aba86d2011-04-08 08:44:37 +00007410 flags = BTRFS_BLOCK_GROUP_SYSTEM;
7411 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00007412 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00007413 goto out;
liuboc59021f2011-03-07 02:13:14 +00007414
liubo1aba86d2011-04-08 08:44:37 +00007415 if (mixed) {
7416 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
7417 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7418 } else {
7419 flags = BTRFS_BLOCK_GROUP_METADATA;
7420 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7421 if (ret)
7422 goto out;
7423
7424 flags = BTRFS_BLOCK_GROUP_DATA;
7425 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7426 }
7427out:
liuboc59021f2011-03-07 02:13:14 +00007428 return ret;
7429}
7430
liuboacce9522011-01-06 19:30:25 +08007431int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
7432{
7433 return unpin_extent_range(root, start, end);
7434}
7435
7436int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00007437 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08007438{
Li Dongyang5378e602011-03-24 10:24:27 +00007439 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08007440}
Li Dongyangf7039b12011-03-24 10:24:28 +00007441
7442int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
7443{
7444 struct btrfs_fs_info *fs_info = root->fs_info;
7445 struct btrfs_block_group_cache *cache = NULL;
7446 u64 group_trimmed;
7447 u64 start;
7448 u64 end;
7449 u64 trimmed = 0;
7450 int ret = 0;
7451
7452 cache = btrfs_lookup_block_group(fs_info, range->start);
7453
7454 while (cache) {
7455 if (cache->key.objectid >= (range->start + range->len)) {
7456 btrfs_put_block_group(cache);
7457 break;
7458 }
7459
7460 start = max(range->start, cache->key.objectid);
7461 end = min(range->start + range->len,
7462 cache->key.objectid + cache->key.offset);
7463
7464 if (end - start >= range->minlen) {
7465 if (!block_group_cache_done(cache)) {
7466 ret = cache_block_group(cache, NULL, root, 0);
7467 if (!ret)
7468 wait_block_group_cache_done(cache);
7469 }
7470 ret = btrfs_trim_block_group(cache,
7471 &group_trimmed,
7472 start,
7473 end,
7474 range->minlen);
7475
7476 trimmed += group_trimmed;
7477 if (ret) {
7478 btrfs_put_block_group(cache);
7479 break;
7480 }
7481 }
7482
7483 cache = next_block_group(fs_info->tree_root, cache);
7484 }
7485
7486 range->len = trimmed;
7487 return ret;
7488}