blob: b457f195636eb478408a8db8abca5123896df3b9 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050027#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040031#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040033#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040034#include "free-space-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Chris Mason0e4f8f82011-04-15 16:05:44 -040036/* control flags for do_chunk_alloc's force field
37 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
38 * if we really need one.
39 *
40 * CHUNK_ALLOC_FORCE means it must try to allocate one
41 *
42 * CHUNK_ALLOC_LIMITED means to only try and allocate one
43 * if we have very few chunks already allocated. This is
44 * used as part of the clustering code to help make sure
45 * we have a good pool of storage to cluster in, without
46 * filling the FS with empty chunks
47 *
48 */
49enum {
50 CHUNK_ALLOC_NO_FORCE = 0,
51 CHUNK_ALLOC_FORCE = 1,
52 CHUNK_ALLOC_LIMITED = 2,
53};
54
Josef Bacikf3465ca2008-11-12 14:19:50 -050055static int update_block_group(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040057 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040058static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
59 struct btrfs_root *root,
60 u64 bytenr, u64 num_bytes, u64 parent,
61 u64 root_objectid, u64 owner_objectid,
62 u64 owner_offset, int refs_to_drop,
63 struct btrfs_delayed_extent_op *extra_op);
64static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
65 struct extent_buffer *leaf,
66 struct btrfs_extent_item *ei);
67static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
68 struct btrfs_root *root,
69 u64 parent, u64 root_objectid,
70 u64 flags, u64 owner, u64 offset,
71 struct btrfs_key *ins, int ref_mod);
72static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
73 struct btrfs_root *root,
74 u64 parent, u64 root_objectid,
75 u64 flags, struct btrfs_disk_key *key,
76 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050077static int do_chunk_alloc(struct btrfs_trans_handle *trans,
78 struct btrfs_root *extent_root, u64 alloc_bytes,
79 u64 flags, int force);
Yan Zheng11833d62009-09-11 16:11:19 -040080static int find_next_key(struct btrfs_path *path, int level,
81 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -040082static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
83 int dump_block_groups);
Josef Bacik6a632092009-02-20 11:00:09 -050084
Josef Bacik817d52f2009-07-13 21:29:25 -040085static noinline int
86block_group_cache_done(struct btrfs_block_group_cache *cache)
87{
88 smp_mb();
89 return cache->cached == BTRFS_CACHE_FINISHED;
90}
91
Josef Bacik0f9dd462008-09-23 13:14:11 -040092static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
93{
94 return (cache->flags & bits) == bits;
95}
96
David Sterba62a45b62011-04-20 15:52:26 +020097static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +000098{
99 atomic_inc(&cache->count);
100}
101
102void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
103{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400104 if (atomic_dec_and_test(&cache->count)) {
105 WARN_ON(cache->pinned > 0);
106 WARN_ON(cache->reserved > 0);
107 WARN_ON(cache->reserved_pinned > 0);
Josef Bacik11dfe352009-11-13 20:12:59 +0000108 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400109 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000110}
111
Josef Bacik0f9dd462008-09-23 13:14:11 -0400112/*
113 * this adds the block group to the fs_info rb tree for the block group
114 * cache
115 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500116static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400117 struct btrfs_block_group_cache *block_group)
118{
119 struct rb_node **p;
120 struct rb_node *parent = NULL;
121 struct btrfs_block_group_cache *cache;
122
123 spin_lock(&info->block_group_cache_lock);
124 p = &info->block_group_cache_tree.rb_node;
125
126 while (*p) {
127 parent = *p;
128 cache = rb_entry(parent, struct btrfs_block_group_cache,
129 cache_node);
130 if (block_group->key.objectid < cache->key.objectid) {
131 p = &(*p)->rb_left;
132 } else if (block_group->key.objectid > cache->key.objectid) {
133 p = &(*p)->rb_right;
134 } else {
135 spin_unlock(&info->block_group_cache_lock);
136 return -EEXIST;
137 }
138 }
139
140 rb_link_node(&block_group->cache_node, parent, p);
141 rb_insert_color(&block_group->cache_node,
142 &info->block_group_cache_tree);
143 spin_unlock(&info->block_group_cache_lock);
144
145 return 0;
146}
147
148/*
149 * This will return the block group at or after bytenr if contains is 0, else
150 * it will return the block group that contains the bytenr
151 */
152static struct btrfs_block_group_cache *
153block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
154 int contains)
155{
156 struct btrfs_block_group_cache *cache, *ret = NULL;
157 struct rb_node *n;
158 u64 end, start;
159
160 spin_lock(&info->block_group_cache_lock);
161 n = info->block_group_cache_tree.rb_node;
162
163 while (n) {
164 cache = rb_entry(n, struct btrfs_block_group_cache,
165 cache_node);
166 end = cache->key.objectid + cache->key.offset - 1;
167 start = cache->key.objectid;
168
169 if (bytenr < start) {
170 if (!contains && (!ret || start < ret->key.objectid))
171 ret = cache;
172 n = n->rb_left;
173 } else if (bytenr > start) {
174 if (contains && bytenr <= end) {
175 ret = cache;
176 break;
177 }
178 n = n->rb_right;
179 } else {
180 ret = cache;
181 break;
182 }
183 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500184 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000185 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400186 spin_unlock(&info->block_group_cache_lock);
187
188 return ret;
189}
190
Yan Zheng11833d62009-09-11 16:11:19 -0400191static int add_excluded_extent(struct btrfs_root *root,
192 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400193{
Yan Zheng11833d62009-09-11 16:11:19 -0400194 u64 end = start + num_bytes - 1;
195 set_extent_bits(&root->fs_info->freed_extents[0],
196 start, end, EXTENT_UPTODATE, GFP_NOFS);
197 set_extent_bits(&root->fs_info->freed_extents[1],
198 start, end, EXTENT_UPTODATE, GFP_NOFS);
199 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400200}
201
Yan Zheng11833d62009-09-11 16:11:19 -0400202static void free_excluded_extents(struct btrfs_root *root,
203 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400204{
Yan Zheng11833d62009-09-11 16:11:19 -0400205 u64 start, end;
206
207 start = cache->key.objectid;
208 end = start + cache->key.offset - 1;
209
210 clear_extent_bits(&root->fs_info->freed_extents[0],
211 start, end, EXTENT_UPTODATE, GFP_NOFS);
212 clear_extent_bits(&root->fs_info->freed_extents[1],
213 start, end, EXTENT_UPTODATE, GFP_NOFS);
214}
215
216static int exclude_super_stripes(struct btrfs_root *root,
217 struct btrfs_block_group_cache *cache)
218{
Josef Bacik817d52f2009-07-13 21:29:25 -0400219 u64 bytenr;
220 u64 *logical;
221 int stripe_len;
222 int i, nr, ret;
223
Yan, Zheng06b23312009-11-26 09:31:11 +0000224 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
225 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
226 cache->bytes_super += stripe_len;
227 ret = add_excluded_extent(root, cache->key.objectid,
228 stripe_len);
229 BUG_ON(ret);
230 }
231
Josef Bacik817d52f2009-07-13 21:29:25 -0400232 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
233 bytenr = btrfs_sb_offset(i);
234 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
235 cache->key.objectid, bytenr,
236 0, &logical, &nr, &stripe_len);
237 BUG_ON(ret);
Yan Zheng11833d62009-09-11 16:11:19 -0400238
Josef Bacik817d52f2009-07-13 21:29:25 -0400239 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400240 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400241 ret = add_excluded_extent(root, logical[nr],
242 stripe_len);
243 BUG_ON(ret);
Josef Bacik817d52f2009-07-13 21:29:25 -0400244 }
Yan Zheng11833d62009-09-11 16:11:19 -0400245
Josef Bacik817d52f2009-07-13 21:29:25 -0400246 kfree(logical);
247 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400248 return 0;
249}
250
Yan Zheng11833d62009-09-11 16:11:19 -0400251static struct btrfs_caching_control *
252get_caching_control(struct btrfs_block_group_cache *cache)
253{
254 struct btrfs_caching_control *ctl;
255
256 spin_lock(&cache->lock);
257 if (cache->cached != BTRFS_CACHE_STARTED) {
258 spin_unlock(&cache->lock);
259 return NULL;
260 }
261
Josef Bacikdde5abe2010-09-16 16:17:03 -0400262 /* We're loading it the fast way, so we don't have a caching_ctl. */
263 if (!cache->caching_ctl) {
264 spin_unlock(&cache->lock);
265 return NULL;
266 }
267
Yan Zheng11833d62009-09-11 16:11:19 -0400268 ctl = cache->caching_ctl;
269 atomic_inc(&ctl->count);
270 spin_unlock(&cache->lock);
271 return ctl;
272}
273
274static void put_caching_control(struct btrfs_caching_control *ctl)
275{
276 if (atomic_dec_and_test(&ctl->count))
277 kfree(ctl);
278}
279
Josef Bacik0f9dd462008-09-23 13:14:11 -0400280/*
281 * this is only called by cache_block_group, since we could have freed extents
282 * we need to check the pinned_extents for any extents that can't be used yet
283 * since their free space will be released as soon as the transaction commits.
284 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400285static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400286 struct btrfs_fs_info *info, u64 start, u64 end)
287{
Josef Bacik817d52f2009-07-13 21:29:25 -0400288 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400289 int ret;
290
291 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400292 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400293 &extent_start, &extent_end,
Yan Zheng11833d62009-09-11 16:11:19 -0400294 EXTENT_DIRTY | EXTENT_UPTODATE);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400295 if (ret)
296 break;
297
Yan, Zheng06b23312009-11-26 09:31:11 +0000298 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400299 start = extent_end + 1;
300 } else if (extent_start > start && extent_start < end) {
301 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400302 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500303 ret = btrfs_add_free_space(block_group, start,
304 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400305 BUG_ON(ret);
306 start = extent_end + 1;
307 } else {
308 break;
309 }
310 }
311
312 if (start < end) {
313 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400314 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500315 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316 BUG_ON(ret);
317 }
318
Josef Bacik817d52f2009-07-13 21:29:25 -0400319 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400320}
321
Josef Bacik817d52f2009-07-13 21:29:25 -0400322static int caching_kthread(void *data)
Chris Masone37c9e62007-05-09 20:13:14 -0400323{
Josef Bacik817d52f2009-07-13 21:29:25 -0400324 struct btrfs_block_group_cache *block_group = data;
325 struct btrfs_fs_info *fs_info = block_group->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400326 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
327 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400328 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400329 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400330 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400332 u64 last = 0;
333 u32 nritems;
334 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400335
Chris Masone37c9e62007-05-09 20:13:14 -0400336 path = btrfs_alloc_path();
337 if (!path)
338 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400339
Josef Bacik817d52f2009-07-13 21:29:25 -0400340 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400341
Chris Mason5cd57b22008-06-25 16:01:30 -0400342 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400343 * We don't want to deadlock with somebody trying to allocate a new
344 * extent for the extent root while also trying to search the extent
345 * root to add free space. So we skip locking and search the commit
346 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400347 */
348 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400349 path->search_commit_root = 1;
350 path->reada = 2;
351
Yan Zhenge4404d62008-12-12 10:03:26 -0500352 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400353 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400354 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400355again:
Yan Zheng11833d62009-09-11 16:11:19 -0400356 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400357 /* need to make sure the commit_root doesn't disappear */
358 down_read(&fs_info->extent_commit_sem);
359
Yan Zheng11833d62009-09-11 16:11:19 -0400360 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400361 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400362 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500363
Yan Zheng11833d62009-09-11 16:11:19 -0400364 leaf = path->nodes[0];
365 nritems = btrfs_header_nritems(leaf);
366
Chris Masond3977122009-01-05 21:25:51 -0500367 while (1) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400368 smp_mb();
Yan Zheng11833d62009-09-11 16:11:19 -0400369 if (fs_info->closing > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400370 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400371 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400372 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400373
Yan Zheng11833d62009-09-11 16:11:19 -0400374 if (path->slots[0] < nritems) {
375 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
376 } else {
377 ret = find_next_key(path, 0, &key);
378 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400379 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400380
Yan Zheng11833d62009-09-11 16:11:19 -0400381 caching_ctl->progress = last;
David Sterbab3b4aa72011-04-21 01:20:15 +0200382 btrfs_release_path(path);
Yan Zheng11833d62009-09-11 16:11:19 -0400383 up_read(&fs_info->extent_commit_sem);
384 mutex_unlock(&caching_ctl->mutex);
385 if (btrfs_transaction_in_commit(fs_info))
Chris Masonf36f3042009-07-30 10:04:48 -0400386 schedule_timeout(1);
Yan Zheng11833d62009-09-11 16:11:19 -0400387 else
388 cond_resched();
389 goto again;
390 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400391
Yan Zheng11833d62009-09-11 16:11:19 -0400392 if (key.objectid < block_group->key.objectid) {
393 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400394 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400395 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400396
Chris Masone37c9e62007-05-09 20:13:14 -0400397 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400398 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400399 break;
Yan7d7d6062007-09-14 16:15:28 -0400400
Yan Zheng11833d62009-09-11 16:11:19 -0400401 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400402 total_found += add_new_free_space(block_group,
403 fs_info, last,
404 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400405 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400406
Yan Zheng11833d62009-09-11 16:11:19 -0400407 if (total_found > (1024 * 1024 * 2)) {
408 total_found = 0;
409 wake_up(&caching_ctl->wait);
410 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400411 }
Chris Masone37c9e62007-05-09 20:13:14 -0400412 path->slots[0]++;
413 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400414 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400415
416 total_found += add_new_free_space(block_group, fs_info, last,
417 block_group->key.objectid +
418 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400419 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400420
421 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400422 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400423 block_group->cached = BTRFS_CACHE_FINISHED;
424 spin_unlock(&block_group->lock);
425
Chris Mason54aa1f42007-06-22 14:16:25 -0400426err:
Chris Masone37c9e62007-05-09 20:13:14 -0400427 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400428 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400429
Yan Zheng11833d62009-09-11 16:11:19 -0400430 free_excluded_extents(extent_root, block_group);
431
432 mutex_unlock(&caching_ctl->mutex);
433 wake_up(&caching_ctl->wait);
434
435 put_caching_control(caching_ctl);
436 atomic_dec(&block_group->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000437 btrfs_put_block_group(block_group);
438
Josef Bacik817d52f2009-07-13 21:29:25 -0400439 return 0;
440}
441
Josef Bacik9d66e232010-08-25 16:54:15 -0400442static int cache_block_group(struct btrfs_block_group_cache *cache,
443 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500444 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400445 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400446{
Yan Zheng11833d62009-09-11 16:11:19 -0400447 struct btrfs_fs_info *fs_info = cache->fs_info;
448 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400449 struct task_struct *tsk;
450 int ret = 0;
451
Yan Zheng11833d62009-09-11 16:11:19 -0400452 smp_mb();
453 if (cache->cached != BTRFS_CACHE_NO)
454 return 0;
455
Josef Bacik9d66e232010-08-25 16:54:15 -0400456 /*
457 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500458 * to have the normal tree locking. Also if we are currently trying to
459 * allocate blocks for the tree root we can't do the fast caching since
460 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400461 */
Li Dongyangf7039b12011-03-24 10:24:28 +0000462 if (trans && (!trans->transaction->in_commit) &&
Josef Bacikb8399de2010-12-08 09:15:11 -0500463 (root && root != root->fs_info->tree_root)) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400464 spin_lock(&cache->lock);
465 if (cache->cached != BTRFS_CACHE_NO) {
466 spin_unlock(&cache->lock);
467 return 0;
468 }
469 cache->cached = BTRFS_CACHE_STARTED;
470 spin_unlock(&cache->lock);
471
472 ret = load_free_space_cache(fs_info, cache);
473
474 spin_lock(&cache->lock);
475 if (ret == 1) {
476 cache->cached = BTRFS_CACHE_FINISHED;
477 cache->last_byte_to_unpin = (u64)-1;
478 } else {
479 cache->cached = BTRFS_CACHE_NO;
480 }
481 spin_unlock(&cache->lock);
Josef Bacik3c148742011-02-02 15:53:47 +0000482 if (ret == 1) {
483 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400484 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000485 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400486 }
487
488 if (load_cache_only)
489 return 0;
490
Miao Xiefc0e4a32011-03-24 11:41:21 +0000491 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -0400492 BUG_ON(!caching_ctl);
493
494 INIT_LIST_HEAD(&caching_ctl->list);
495 mutex_init(&caching_ctl->mutex);
496 init_waitqueue_head(&caching_ctl->wait);
497 caching_ctl->block_group = cache;
498 caching_ctl->progress = cache->key.objectid;
499 /* one for caching kthread, one for caching block group list */
500 atomic_set(&caching_ctl->count, 2);
501
Josef Bacik817d52f2009-07-13 21:29:25 -0400502 spin_lock(&cache->lock);
503 if (cache->cached != BTRFS_CACHE_NO) {
504 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400505 kfree(caching_ctl);
506 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400507 }
Yan Zheng11833d62009-09-11 16:11:19 -0400508 cache->caching_ctl = caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400509 cache->cached = BTRFS_CACHE_STARTED;
510 spin_unlock(&cache->lock);
511
Yan Zheng11833d62009-09-11 16:11:19 -0400512 down_write(&fs_info->extent_commit_sem);
513 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
514 up_write(&fs_info->extent_commit_sem);
515
516 atomic_inc(&cache->space_info->caching_threads);
Josef Bacik11dfe352009-11-13 20:12:59 +0000517 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400518
Josef Bacik817d52f2009-07-13 21:29:25 -0400519 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
520 cache->key.objectid);
521 if (IS_ERR(tsk)) {
522 ret = PTR_ERR(tsk);
523 printk(KERN_ERR "error running thread %d\n", ret);
524 BUG();
525 }
526
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400527 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400528}
529
Josef Bacik0f9dd462008-09-23 13:14:11 -0400530/*
531 * return the block group that starts at or after bytenr
532 */
Chris Masond3977122009-01-05 21:25:51 -0500533static struct btrfs_block_group_cache *
534btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400535{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400536 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400537
Josef Bacik0f9dd462008-09-23 13:14:11 -0400538 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400539
Josef Bacik0f9dd462008-09-23 13:14:11 -0400540 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400541}
542
Josef Bacik0f9dd462008-09-23 13:14:11 -0400543/*
Sankar P9f556842009-05-14 13:52:22 -0400544 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400545 */
Chris Masond3977122009-01-05 21:25:51 -0500546struct btrfs_block_group_cache *btrfs_lookup_block_group(
547 struct btrfs_fs_info *info,
548 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400549{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400550 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400551
Josef Bacik0f9dd462008-09-23 13:14:11 -0400552 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400553
Josef Bacik0f9dd462008-09-23 13:14:11 -0400554 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400555}
Chris Mason0b86a832008-03-24 15:01:56 -0400556
Josef Bacik0f9dd462008-09-23 13:14:11 -0400557static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
558 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400559{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400560 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400561 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400562
Yan, Zhengb742bb82010-05-16 10:46:24 -0400563 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
564 BTRFS_BLOCK_GROUP_METADATA;
565
Chris Mason4184ea72009-03-10 12:39:20 -0400566 rcu_read_lock();
567 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400568 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400569 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400570 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400571 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400572 }
Chris Mason4184ea72009-03-10 12:39:20 -0400573 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400574 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400575}
576
Chris Mason4184ea72009-03-10 12:39:20 -0400577/*
578 * after adding space to the filesystem, we need to clear the full flags
579 * on all the space infos.
580 */
581void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
582{
583 struct list_head *head = &info->space_info;
584 struct btrfs_space_info *found;
585
586 rcu_read_lock();
587 list_for_each_entry_rcu(found, head, list)
588 found->full = 0;
589 rcu_read_unlock();
590}
591
Josef Bacik80eb2342008-10-29 14:49:05 -0400592static u64 div_factor(u64 num, int factor)
593{
594 if (factor == 10)
595 return num;
596 num *= factor;
597 do_div(num, 10);
598 return num;
599}
600
Chris Masone5bc2452010-10-26 13:37:56 -0400601static u64 div_factor_fine(u64 num, int factor)
602{
603 if (factor == 100)
604 return num;
605 num *= factor;
606 do_div(num, 100);
607 return num;
608}
609
Yan Zhengd2fb3432008-12-11 16:30:39 -0500610u64 btrfs_find_block_group(struct btrfs_root *root,
611 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400612{
Chris Mason96b51792007-10-15 16:15:19 -0400613 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400614 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500615 u64 last = max(search_hint, search_start);
616 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400617 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500618 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400619 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400620again:
Zheng Yane8569812008-09-26 10:05:48 -0400621 while (1) {
622 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400623 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400624 break;
Chris Mason96b51792007-10-15 16:15:19 -0400625
Chris Masonc286ac42008-07-22 23:06:41 -0400626 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400627 last = cache->key.objectid + cache->key.offset;
628 used = btrfs_block_group_used(&cache->item);
629
Yan Zhengd2fb3432008-12-11 16:30:39 -0500630 if ((full_search || !cache->ro) &&
631 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400632 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500633 div_factor(cache->key.offset, factor)) {
634 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400635 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400636 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400637 goto found;
638 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400639 }
Chris Masonc286ac42008-07-22 23:06:41 -0400640 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400641 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400642 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400643 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400644 if (!wrapped) {
645 last = search_start;
646 wrapped = 1;
647 goto again;
648 }
649 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400650 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400651 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400652 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400653 goto again;
654 }
Chris Masonbe744172007-05-06 10:15:01 -0400655found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500656 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400657}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400658
Chris Masone02119d2008-09-05 16:13:11 -0400659/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400660int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400661{
662 int ret;
663 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400664 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400665
Zheng Yan31840ae2008-09-23 13:14:14 -0400666 path = btrfs_alloc_path();
667 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400668 key.objectid = start;
669 key.offset = len;
670 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
671 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
672 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400673 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500674 return ret;
675}
676
Chris Masond8d5f3e2007-12-11 12:42:00 -0500677/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400678 * helper function to lookup reference count and flags of extent.
679 *
680 * the head node for delayed ref is used to store the sum of all the
681 * reference count modifications queued up in the rbtree. the head
682 * node may also store the extent flags to set. This way you can check
683 * to see what the reference count and extent flags would be if all of
684 * the delayed refs are not processed.
685 */
686int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
687 struct btrfs_root *root, u64 bytenr,
688 u64 num_bytes, u64 *refs, u64 *flags)
689{
690 struct btrfs_delayed_ref_head *head;
691 struct btrfs_delayed_ref_root *delayed_refs;
692 struct btrfs_path *path;
693 struct btrfs_extent_item *ei;
694 struct extent_buffer *leaf;
695 struct btrfs_key key;
696 u32 item_size;
697 u64 num_refs;
698 u64 extent_flags;
699 int ret;
700
701 path = btrfs_alloc_path();
702 if (!path)
703 return -ENOMEM;
704
705 key.objectid = bytenr;
706 key.type = BTRFS_EXTENT_ITEM_KEY;
707 key.offset = num_bytes;
708 if (!trans) {
709 path->skip_locking = 1;
710 path->search_commit_root = 1;
711 }
712again:
713 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
714 &key, path, 0, 0);
715 if (ret < 0)
716 goto out_free;
717
718 if (ret == 0) {
719 leaf = path->nodes[0];
720 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
721 if (item_size >= sizeof(*ei)) {
722 ei = btrfs_item_ptr(leaf, path->slots[0],
723 struct btrfs_extent_item);
724 num_refs = btrfs_extent_refs(leaf, ei);
725 extent_flags = btrfs_extent_flags(leaf, ei);
726 } else {
727#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
728 struct btrfs_extent_item_v0 *ei0;
729 BUG_ON(item_size != sizeof(*ei0));
730 ei0 = btrfs_item_ptr(leaf, path->slots[0],
731 struct btrfs_extent_item_v0);
732 num_refs = btrfs_extent_refs_v0(leaf, ei0);
733 /* FIXME: this isn't correct for data */
734 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
735#else
736 BUG();
737#endif
738 }
739 BUG_ON(num_refs == 0);
740 } else {
741 num_refs = 0;
742 extent_flags = 0;
743 ret = 0;
744 }
745
746 if (!trans)
747 goto out;
748
749 delayed_refs = &trans->transaction->delayed_refs;
750 spin_lock(&delayed_refs->lock);
751 head = btrfs_find_delayed_ref_head(trans, bytenr);
752 if (head) {
753 if (!mutex_trylock(&head->mutex)) {
754 atomic_inc(&head->node.refs);
755 spin_unlock(&delayed_refs->lock);
756
David Sterbab3b4aa72011-04-21 01:20:15 +0200757 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400758
David Sterba8cc33e52011-05-02 15:29:25 +0200759 /*
760 * Mutex was contended, block until it's released and try
761 * again
762 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400763 mutex_lock(&head->mutex);
764 mutex_unlock(&head->mutex);
765 btrfs_put_delayed_ref(&head->node);
766 goto again;
767 }
768 if (head->extent_op && head->extent_op->update_flags)
769 extent_flags |= head->extent_op->flags_to_set;
770 else
771 BUG_ON(num_refs == 0);
772
773 num_refs += head->node.ref_mod;
774 mutex_unlock(&head->mutex);
775 }
776 spin_unlock(&delayed_refs->lock);
777out:
778 WARN_ON(num_refs == 0);
779 if (refs)
780 *refs = num_refs;
781 if (flags)
782 *flags = extent_flags;
783out_free:
784 btrfs_free_path(path);
785 return ret;
786}
787
788/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500789 * Back reference rules. Back refs have three main goals:
790 *
791 * 1) differentiate between all holders of references to an extent so that
792 * when a reference is dropped we can make sure it was a valid reference
793 * before freeing the extent.
794 *
795 * 2) Provide enough information to quickly find the holders of an extent
796 * if we notice a given block is corrupted or bad.
797 *
798 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
799 * maintenance. This is actually the same as #2, but with a slightly
800 * different use case.
801 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400802 * There are two kinds of back refs. The implicit back refs is optimized
803 * for pointers in non-shared tree blocks. For a given pointer in a block,
804 * back refs of this kind provide information about the block's owner tree
805 * and the pointer's key. These information allow us to find the block by
806 * b-tree searching. The full back refs is for pointers in tree blocks not
807 * referenced by their owner trees. The location of tree block is recorded
808 * in the back refs. Actually the full back refs is generic, and can be
809 * used in all cases the implicit back refs is used. The major shortcoming
810 * of the full back refs is its overhead. Every time a tree block gets
811 * COWed, we have to update back refs entry for all pointers in it.
812 *
813 * For a newly allocated tree block, we use implicit back refs for
814 * pointers in it. This means most tree related operations only involve
815 * implicit back refs. For a tree block created in old transaction, the
816 * only way to drop a reference to it is COW it. So we can detect the
817 * event that tree block loses its owner tree's reference and do the
818 * back refs conversion.
819 *
820 * When a tree block is COW'd through a tree, there are four cases:
821 *
822 * The reference count of the block is one and the tree is the block's
823 * owner tree. Nothing to do in this case.
824 *
825 * The reference count of the block is one and the tree is not the
826 * block's owner tree. In this case, full back refs is used for pointers
827 * in the block. Remove these full back refs, add implicit back refs for
828 * every pointers in the new block.
829 *
830 * The reference count of the block is greater than one and the tree is
831 * the block's owner tree. In this case, implicit back refs is used for
832 * pointers in the block. Add full back refs for every pointers in the
833 * block, increase lower level extents' reference counts. The original
834 * implicit back refs are entailed to the new block.
835 *
836 * The reference count of the block is greater than one and the tree is
837 * not the block's owner tree. Add implicit back refs for every pointer in
838 * the new block, increase lower level extents' reference count.
839 *
840 * Back Reference Key composing:
841 *
842 * The key objectid corresponds to the first byte in the extent,
843 * The key type is used to differentiate between types of back refs.
844 * There are different meanings of the key offset for different types
845 * of back refs.
846 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500847 * File extents can be referenced by:
848 *
849 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400850 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500851 * - different offsets inside a file (bookend extents in file.c)
852 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400853 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500854 *
855 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500856 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857 * - original offset in the file
858 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400859 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400860 * The key offset for the implicit back refs is hash of the first
861 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500862 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400863 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500864 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400865 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500866 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400867 * The key offset for the implicit back refs is the first byte of
868 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500869 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400870 * When a file extent is allocated, The implicit back refs is used.
871 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500872 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400873 * (root_key.objectid, inode objectid, offset in file, 1)
874 *
875 * When a file extent is removed file truncation, we find the
876 * corresponding implicit back refs and check the following fields:
877 *
878 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500879 *
880 * Btree extents can be referenced by:
881 *
882 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500883 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400884 * Both the implicit back refs and the full back refs for tree blocks
885 * only consist of key. The key offset for the implicit back refs is
886 * objectid of block's owner tree. The key offset for the full back refs
887 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500888 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400889 * When implicit back refs is used, information about the lowest key and
890 * level of the tree block are required. These information are stored in
891 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500892 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400893
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400894#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
895static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
896 struct btrfs_root *root,
897 struct btrfs_path *path,
898 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500899{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400900 struct btrfs_extent_item *item;
901 struct btrfs_extent_item_v0 *ei0;
902 struct btrfs_extent_ref_v0 *ref0;
903 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400904 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400905 struct btrfs_key key;
906 struct btrfs_key found_key;
907 u32 new_size = sizeof(*item);
908 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500909 int ret;
910
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911 leaf = path->nodes[0];
912 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500913
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400914 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
915 ei0 = btrfs_item_ptr(leaf, path->slots[0],
916 struct btrfs_extent_item_v0);
917 refs = btrfs_extent_refs_v0(leaf, ei0);
918
919 if (owner == (u64)-1) {
920 while (1) {
921 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
922 ret = btrfs_next_leaf(root, path);
923 if (ret < 0)
924 return ret;
925 BUG_ON(ret > 0);
926 leaf = path->nodes[0];
927 }
928 btrfs_item_key_to_cpu(leaf, &found_key,
929 path->slots[0]);
930 BUG_ON(key.objectid != found_key.objectid);
931 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
932 path->slots[0]++;
933 continue;
934 }
935 ref0 = btrfs_item_ptr(leaf, path->slots[0],
936 struct btrfs_extent_ref_v0);
937 owner = btrfs_ref_objectid_v0(leaf, ref0);
938 break;
939 }
940 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200941 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400942
943 if (owner < BTRFS_FIRST_FREE_OBJECTID)
944 new_size += sizeof(*bi);
945
946 new_size -= sizeof(*ei0);
947 ret = btrfs_search_slot(trans, root, &key, path,
948 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400949 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 return ret;
951 BUG_ON(ret);
952
953 ret = btrfs_extend_item(trans, root, path, new_size);
954 BUG_ON(ret);
955
956 leaf = path->nodes[0];
957 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
958 btrfs_set_extent_refs(leaf, item, refs);
959 /* FIXME: get real generation */
960 btrfs_set_extent_generation(leaf, item, 0);
961 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
962 btrfs_set_extent_flags(leaf, item,
963 BTRFS_EXTENT_FLAG_TREE_BLOCK |
964 BTRFS_BLOCK_FLAG_FULL_BACKREF);
965 bi = (struct btrfs_tree_block_info *)(item + 1);
966 /* FIXME: get first key of the block */
967 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
968 btrfs_set_tree_block_level(leaf, bi, (int)owner);
969 } else {
970 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
971 }
972 btrfs_mark_buffer_dirty(leaf);
973 return 0;
974}
975#endif
976
977static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
978{
979 u32 high_crc = ~(u32)0;
980 u32 low_crc = ~(u32)0;
981 __le64 lenum;
982
983 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +0100984 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +0100986 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400987 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +0100988 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400989
990 return ((u64)high_crc << 31) ^ (u64)low_crc;
991}
992
993static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
994 struct btrfs_extent_data_ref *ref)
995{
996 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
997 btrfs_extent_data_ref_objectid(leaf, ref),
998 btrfs_extent_data_ref_offset(leaf, ref));
999}
1000
1001static int match_extent_data_ref(struct extent_buffer *leaf,
1002 struct btrfs_extent_data_ref *ref,
1003 u64 root_objectid, u64 owner, u64 offset)
1004{
1005 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1006 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1007 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1008 return 0;
1009 return 1;
1010}
1011
1012static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1013 struct btrfs_root *root,
1014 struct btrfs_path *path,
1015 u64 bytenr, u64 parent,
1016 u64 root_objectid,
1017 u64 owner, u64 offset)
1018{
1019 struct btrfs_key key;
1020 struct btrfs_extent_data_ref *ref;
1021 struct extent_buffer *leaf;
1022 u32 nritems;
1023 int ret;
1024 int recow;
1025 int err = -ENOENT;
1026
1027 key.objectid = bytenr;
1028 if (parent) {
1029 key.type = BTRFS_SHARED_DATA_REF_KEY;
1030 key.offset = parent;
1031 } else {
1032 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1033 key.offset = hash_extent_data_ref(root_objectid,
1034 owner, offset);
1035 }
1036again:
1037 recow = 0;
1038 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1039 if (ret < 0) {
1040 err = ret;
1041 goto fail;
1042 }
1043
1044 if (parent) {
1045 if (!ret)
1046 return 0;
1047#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1048 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001049 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001050 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1051 if (ret < 0) {
1052 err = ret;
1053 goto fail;
1054 }
1055 if (!ret)
1056 return 0;
1057#endif
1058 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001059 }
1060
1061 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001062 nritems = btrfs_header_nritems(leaf);
1063 while (1) {
1064 if (path->slots[0] >= nritems) {
1065 ret = btrfs_next_leaf(root, path);
1066 if (ret < 0)
1067 err = ret;
1068 if (ret)
1069 goto fail;
1070
1071 leaf = path->nodes[0];
1072 nritems = btrfs_header_nritems(leaf);
1073 recow = 1;
1074 }
1075
1076 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1077 if (key.objectid != bytenr ||
1078 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1079 goto fail;
1080
1081 ref = btrfs_item_ptr(leaf, path->slots[0],
1082 struct btrfs_extent_data_ref);
1083
1084 if (match_extent_data_ref(leaf, ref, root_objectid,
1085 owner, offset)) {
1086 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001087 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001088 goto again;
1089 }
1090 err = 0;
1091 break;
1092 }
1093 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001094 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095fail:
1096 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001097}
1098
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001099static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1100 struct btrfs_root *root,
1101 struct btrfs_path *path,
1102 u64 bytenr, u64 parent,
1103 u64 root_objectid, u64 owner,
1104 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001105{
1106 struct btrfs_key key;
1107 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001108 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001109 u32 num_refs;
1110 int ret;
1111
1112 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001113 if (parent) {
1114 key.type = BTRFS_SHARED_DATA_REF_KEY;
1115 key.offset = parent;
1116 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001117 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001118 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1119 key.offset = hash_extent_data_ref(root_objectid,
1120 owner, offset);
1121 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001122 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001123
1124 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1125 if (ret && ret != -EEXIST)
1126 goto fail;
1127
1128 leaf = path->nodes[0];
1129 if (parent) {
1130 struct btrfs_shared_data_ref *ref;
1131 ref = btrfs_item_ptr(leaf, path->slots[0],
1132 struct btrfs_shared_data_ref);
1133 if (ret == 0) {
1134 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1135 } else {
1136 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1137 num_refs += refs_to_add;
1138 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1139 }
1140 } else {
1141 struct btrfs_extent_data_ref *ref;
1142 while (ret == -EEXIST) {
1143 ref = btrfs_item_ptr(leaf, path->slots[0],
1144 struct btrfs_extent_data_ref);
1145 if (match_extent_data_ref(leaf, ref, root_objectid,
1146 owner, offset))
1147 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001148 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001149 key.offset++;
1150 ret = btrfs_insert_empty_item(trans, root, path, &key,
1151 size);
1152 if (ret && ret != -EEXIST)
1153 goto fail;
1154
1155 leaf = path->nodes[0];
1156 }
1157 ref = btrfs_item_ptr(leaf, path->slots[0],
1158 struct btrfs_extent_data_ref);
1159 if (ret == 0) {
1160 btrfs_set_extent_data_ref_root(leaf, ref,
1161 root_objectid);
1162 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1163 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1164 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1165 } else {
1166 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1167 num_refs += refs_to_add;
1168 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1169 }
1170 }
1171 btrfs_mark_buffer_dirty(leaf);
1172 ret = 0;
1173fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001174 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001175 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001176}
1177
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001178static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1179 struct btrfs_root *root,
1180 struct btrfs_path *path,
1181 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001182{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183 struct btrfs_key key;
1184 struct btrfs_extent_data_ref *ref1 = NULL;
1185 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001186 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001187 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001188 int ret = 0;
1189
1190 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001191 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1192
1193 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1194 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1195 struct btrfs_extent_data_ref);
1196 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1197 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1198 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1199 struct btrfs_shared_data_ref);
1200 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1201#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1202 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1203 struct btrfs_extent_ref_v0 *ref0;
1204 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1205 struct btrfs_extent_ref_v0);
1206 num_refs = btrfs_ref_count_v0(leaf, ref0);
1207#endif
1208 } else {
1209 BUG();
1210 }
1211
Chris Mason56bec292009-03-13 10:10:06 -04001212 BUG_ON(num_refs < refs_to_drop);
1213 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001214
Zheng Yan31840ae2008-09-23 13:14:14 -04001215 if (num_refs == 0) {
1216 ret = btrfs_del_item(trans, root, path);
1217 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001218 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1219 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1220 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1221 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1222#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1223 else {
1224 struct btrfs_extent_ref_v0 *ref0;
1225 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1226 struct btrfs_extent_ref_v0);
1227 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1228 }
1229#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001230 btrfs_mark_buffer_dirty(leaf);
1231 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001232 return ret;
1233}
1234
1235static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1236 struct btrfs_path *path,
1237 struct btrfs_extent_inline_ref *iref)
1238{
1239 struct btrfs_key key;
1240 struct extent_buffer *leaf;
1241 struct btrfs_extent_data_ref *ref1;
1242 struct btrfs_shared_data_ref *ref2;
1243 u32 num_refs = 0;
1244
1245 leaf = path->nodes[0];
1246 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1247 if (iref) {
1248 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1249 BTRFS_EXTENT_DATA_REF_KEY) {
1250 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1251 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1252 } else {
1253 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1254 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1255 }
1256 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1257 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1258 struct btrfs_extent_data_ref);
1259 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1260 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1261 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1262 struct btrfs_shared_data_ref);
1263 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1264#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1265 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1266 struct btrfs_extent_ref_v0 *ref0;
1267 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1268 struct btrfs_extent_ref_v0);
1269 num_refs = btrfs_ref_count_v0(leaf, ref0);
1270#endif
1271 } else {
1272 WARN_ON(1);
1273 }
1274 return num_refs;
1275}
1276
1277static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1278 struct btrfs_root *root,
1279 struct btrfs_path *path,
1280 u64 bytenr, u64 parent,
1281 u64 root_objectid)
1282{
1283 struct btrfs_key key;
1284 int ret;
1285
1286 key.objectid = bytenr;
1287 if (parent) {
1288 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1289 key.offset = parent;
1290 } else {
1291 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1292 key.offset = root_objectid;
1293 }
1294
1295 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1296 if (ret > 0)
1297 ret = -ENOENT;
1298#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1299 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001300 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001301 key.type = BTRFS_EXTENT_REF_V0_KEY;
1302 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1303 if (ret > 0)
1304 ret = -ENOENT;
1305 }
1306#endif
1307 return ret;
1308}
1309
1310static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1311 struct btrfs_root *root,
1312 struct btrfs_path *path,
1313 u64 bytenr, u64 parent,
1314 u64 root_objectid)
1315{
1316 struct btrfs_key key;
1317 int ret;
1318
1319 key.objectid = bytenr;
1320 if (parent) {
1321 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1322 key.offset = parent;
1323 } else {
1324 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1325 key.offset = root_objectid;
1326 }
1327
1328 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001329 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001330 return ret;
1331}
1332
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001333static inline int extent_ref_type(u64 parent, u64 owner)
1334{
1335 int type;
1336 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1337 if (parent > 0)
1338 type = BTRFS_SHARED_BLOCK_REF_KEY;
1339 else
1340 type = BTRFS_TREE_BLOCK_REF_KEY;
1341 } else {
1342 if (parent > 0)
1343 type = BTRFS_SHARED_DATA_REF_KEY;
1344 else
1345 type = BTRFS_EXTENT_DATA_REF_KEY;
1346 }
1347 return type;
1348}
1349
Yan Zheng2c47e6052009-06-27 21:07:35 -04001350static int find_next_key(struct btrfs_path *path, int level,
1351 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001352
1353{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001354 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001355 if (!path->nodes[level])
1356 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001357 if (path->slots[level] + 1 >=
1358 btrfs_header_nritems(path->nodes[level]))
1359 continue;
1360 if (level == 0)
1361 btrfs_item_key_to_cpu(path->nodes[level], key,
1362 path->slots[level] + 1);
1363 else
1364 btrfs_node_key_to_cpu(path->nodes[level], key,
1365 path->slots[level] + 1);
1366 return 0;
1367 }
1368 return 1;
1369}
1370
1371/*
1372 * look for inline back ref. if back ref is found, *ref_ret is set
1373 * to the address of inline back ref, and 0 is returned.
1374 *
1375 * if back ref isn't found, *ref_ret is set to the address where it
1376 * should be inserted, and -ENOENT is returned.
1377 *
1378 * if insert is true and there are too many inline back refs, the path
1379 * points to the extent item, and -EAGAIN is returned.
1380 *
1381 * NOTE: inline back refs are ordered in the same way that back ref
1382 * items in the tree are ordered.
1383 */
1384static noinline_for_stack
1385int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1386 struct btrfs_root *root,
1387 struct btrfs_path *path,
1388 struct btrfs_extent_inline_ref **ref_ret,
1389 u64 bytenr, u64 num_bytes,
1390 u64 parent, u64 root_objectid,
1391 u64 owner, u64 offset, int insert)
1392{
1393 struct btrfs_key key;
1394 struct extent_buffer *leaf;
1395 struct btrfs_extent_item *ei;
1396 struct btrfs_extent_inline_ref *iref;
1397 u64 flags;
1398 u64 item_size;
1399 unsigned long ptr;
1400 unsigned long end;
1401 int extra_size;
1402 int type;
1403 int want;
1404 int ret;
1405 int err = 0;
1406
1407 key.objectid = bytenr;
1408 key.type = BTRFS_EXTENT_ITEM_KEY;
1409 key.offset = num_bytes;
1410
1411 want = extent_ref_type(parent, owner);
1412 if (insert) {
1413 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001414 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001415 } else
1416 extra_size = -1;
1417 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1418 if (ret < 0) {
1419 err = ret;
1420 goto out;
1421 }
1422 BUG_ON(ret);
1423
1424 leaf = path->nodes[0];
1425 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1426#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1427 if (item_size < sizeof(*ei)) {
1428 if (!insert) {
1429 err = -ENOENT;
1430 goto out;
1431 }
1432 ret = convert_extent_item_v0(trans, root, path, owner,
1433 extra_size);
1434 if (ret < 0) {
1435 err = ret;
1436 goto out;
1437 }
1438 leaf = path->nodes[0];
1439 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1440 }
1441#endif
1442 BUG_ON(item_size < sizeof(*ei));
1443
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001444 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1445 flags = btrfs_extent_flags(leaf, ei);
1446
1447 ptr = (unsigned long)(ei + 1);
1448 end = (unsigned long)ei + item_size;
1449
1450 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1451 ptr += sizeof(struct btrfs_tree_block_info);
1452 BUG_ON(ptr > end);
1453 } else {
1454 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1455 }
1456
1457 err = -ENOENT;
1458 while (1) {
1459 if (ptr >= end) {
1460 WARN_ON(ptr > end);
1461 break;
1462 }
1463 iref = (struct btrfs_extent_inline_ref *)ptr;
1464 type = btrfs_extent_inline_ref_type(leaf, iref);
1465 if (want < type)
1466 break;
1467 if (want > type) {
1468 ptr += btrfs_extent_inline_ref_size(type);
1469 continue;
1470 }
1471
1472 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1473 struct btrfs_extent_data_ref *dref;
1474 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1475 if (match_extent_data_ref(leaf, dref, root_objectid,
1476 owner, offset)) {
1477 err = 0;
1478 break;
1479 }
1480 if (hash_extent_data_ref_item(leaf, dref) <
1481 hash_extent_data_ref(root_objectid, owner, offset))
1482 break;
1483 } else {
1484 u64 ref_offset;
1485 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1486 if (parent > 0) {
1487 if (parent == ref_offset) {
1488 err = 0;
1489 break;
1490 }
1491 if (ref_offset < parent)
1492 break;
1493 } else {
1494 if (root_objectid == ref_offset) {
1495 err = 0;
1496 break;
1497 }
1498 if (ref_offset < root_objectid)
1499 break;
1500 }
1501 }
1502 ptr += btrfs_extent_inline_ref_size(type);
1503 }
1504 if (err == -ENOENT && insert) {
1505 if (item_size + extra_size >=
1506 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1507 err = -EAGAIN;
1508 goto out;
1509 }
1510 /*
1511 * To add new inline back ref, we have to make sure
1512 * there is no corresponding back ref item.
1513 * For simplicity, we just do not add new inline back
1514 * ref if there is any kind of item for this block
1515 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001516 if (find_next_key(path, 0, &key) == 0 &&
1517 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001518 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001519 err = -EAGAIN;
1520 goto out;
1521 }
1522 }
1523 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1524out:
Yan Zheng85d41982009-06-11 08:51:10 -04001525 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001526 path->keep_locks = 0;
1527 btrfs_unlock_up_safe(path, 1);
1528 }
1529 return err;
1530}
1531
1532/*
1533 * helper to add new inline back ref
1534 */
1535static noinline_for_stack
1536int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1537 struct btrfs_root *root,
1538 struct btrfs_path *path,
1539 struct btrfs_extent_inline_ref *iref,
1540 u64 parent, u64 root_objectid,
1541 u64 owner, u64 offset, int refs_to_add,
1542 struct btrfs_delayed_extent_op *extent_op)
1543{
1544 struct extent_buffer *leaf;
1545 struct btrfs_extent_item *ei;
1546 unsigned long ptr;
1547 unsigned long end;
1548 unsigned long item_offset;
1549 u64 refs;
1550 int size;
1551 int type;
1552 int ret;
1553
1554 leaf = path->nodes[0];
1555 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1556 item_offset = (unsigned long)iref - (unsigned long)ei;
1557
1558 type = extent_ref_type(parent, owner);
1559 size = btrfs_extent_inline_ref_size(type);
1560
1561 ret = btrfs_extend_item(trans, root, path, size);
1562 BUG_ON(ret);
1563
1564 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1565 refs = btrfs_extent_refs(leaf, ei);
1566 refs += refs_to_add;
1567 btrfs_set_extent_refs(leaf, ei, refs);
1568 if (extent_op)
1569 __run_delayed_extent_op(extent_op, leaf, ei);
1570
1571 ptr = (unsigned long)ei + item_offset;
1572 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1573 if (ptr < end - size)
1574 memmove_extent_buffer(leaf, ptr + size, ptr,
1575 end - size - ptr);
1576
1577 iref = (struct btrfs_extent_inline_ref *)ptr;
1578 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1579 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1580 struct btrfs_extent_data_ref *dref;
1581 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1582 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1583 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1584 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1585 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1586 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1587 struct btrfs_shared_data_ref *sref;
1588 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1589 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1590 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1591 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1592 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1593 } else {
1594 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1595 }
1596 btrfs_mark_buffer_dirty(leaf);
1597 return 0;
1598}
1599
1600static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1601 struct btrfs_root *root,
1602 struct btrfs_path *path,
1603 struct btrfs_extent_inline_ref **ref_ret,
1604 u64 bytenr, u64 num_bytes, u64 parent,
1605 u64 root_objectid, u64 owner, u64 offset)
1606{
1607 int ret;
1608
1609 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1610 bytenr, num_bytes, parent,
1611 root_objectid, owner, offset, 0);
1612 if (ret != -ENOENT)
1613 return ret;
1614
David Sterbab3b4aa72011-04-21 01:20:15 +02001615 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001616 *ref_ret = NULL;
1617
1618 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1619 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1620 root_objectid);
1621 } else {
1622 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1623 root_objectid, owner, offset);
1624 }
1625 return ret;
1626}
1627
1628/*
1629 * helper to update/remove inline back ref
1630 */
1631static noinline_for_stack
1632int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1633 struct btrfs_root *root,
1634 struct btrfs_path *path,
1635 struct btrfs_extent_inline_ref *iref,
1636 int refs_to_mod,
1637 struct btrfs_delayed_extent_op *extent_op)
1638{
1639 struct extent_buffer *leaf;
1640 struct btrfs_extent_item *ei;
1641 struct btrfs_extent_data_ref *dref = NULL;
1642 struct btrfs_shared_data_ref *sref = NULL;
1643 unsigned long ptr;
1644 unsigned long end;
1645 u32 item_size;
1646 int size;
1647 int type;
1648 int ret;
1649 u64 refs;
1650
1651 leaf = path->nodes[0];
1652 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1653 refs = btrfs_extent_refs(leaf, ei);
1654 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1655 refs += refs_to_mod;
1656 btrfs_set_extent_refs(leaf, ei, refs);
1657 if (extent_op)
1658 __run_delayed_extent_op(extent_op, leaf, ei);
1659
1660 type = btrfs_extent_inline_ref_type(leaf, iref);
1661
1662 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1663 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1664 refs = btrfs_extent_data_ref_count(leaf, dref);
1665 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1666 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1667 refs = btrfs_shared_data_ref_count(leaf, sref);
1668 } else {
1669 refs = 1;
1670 BUG_ON(refs_to_mod != -1);
1671 }
1672
1673 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1674 refs += refs_to_mod;
1675
1676 if (refs > 0) {
1677 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1678 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1679 else
1680 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1681 } else {
1682 size = btrfs_extent_inline_ref_size(type);
1683 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1684 ptr = (unsigned long)iref;
1685 end = (unsigned long)ei + item_size;
1686 if (ptr + size < end)
1687 memmove_extent_buffer(leaf, ptr, ptr + size,
1688 end - ptr - size);
1689 item_size -= size;
1690 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1691 BUG_ON(ret);
1692 }
1693 btrfs_mark_buffer_dirty(leaf);
1694 return 0;
1695}
1696
1697static noinline_for_stack
1698int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1699 struct btrfs_root *root,
1700 struct btrfs_path *path,
1701 u64 bytenr, u64 num_bytes, u64 parent,
1702 u64 root_objectid, u64 owner,
1703 u64 offset, int refs_to_add,
1704 struct btrfs_delayed_extent_op *extent_op)
1705{
1706 struct btrfs_extent_inline_ref *iref;
1707 int ret;
1708
1709 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1710 bytenr, num_bytes, parent,
1711 root_objectid, owner, offset, 1);
1712 if (ret == 0) {
1713 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1714 ret = update_inline_extent_backref(trans, root, path, iref,
1715 refs_to_add, extent_op);
1716 } else if (ret == -ENOENT) {
1717 ret = setup_inline_extent_backref(trans, root, path, iref,
1718 parent, root_objectid,
1719 owner, offset, refs_to_add,
1720 extent_op);
1721 }
1722 return ret;
1723}
1724
1725static int insert_extent_backref(struct btrfs_trans_handle *trans,
1726 struct btrfs_root *root,
1727 struct btrfs_path *path,
1728 u64 bytenr, u64 parent, u64 root_objectid,
1729 u64 owner, u64 offset, int refs_to_add)
1730{
1731 int ret;
1732 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1733 BUG_ON(refs_to_add != 1);
1734 ret = insert_tree_block_ref(trans, root, path, bytenr,
1735 parent, root_objectid);
1736 } else {
1737 ret = insert_extent_data_ref(trans, root, path, bytenr,
1738 parent, root_objectid,
1739 owner, offset, refs_to_add);
1740 }
1741 return ret;
1742}
1743
1744static int remove_extent_backref(struct btrfs_trans_handle *trans,
1745 struct btrfs_root *root,
1746 struct btrfs_path *path,
1747 struct btrfs_extent_inline_ref *iref,
1748 int refs_to_drop, int is_data)
1749{
1750 int ret;
1751
1752 BUG_ON(!is_data && refs_to_drop != 1);
1753 if (iref) {
1754 ret = update_inline_extent_backref(trans, root, path, iref,
1755 -refs_to_drop, NULL);
1756 } else if (is_data) {
1757 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1758 } else {
1759 ret = btrfs_del_item(trans, root, path);
1760 }
1761 return ret;
1762}
1763
Li Dongyang5378e602011-03-24 10:24:27 +00001764static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001765 u64 start, u64 len)
1766{
Li Dongyang5378e602011-03-24 10:24:27 +00001767 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001768}
Chris Mason15916de2008-11-19 21:17:22 -05001769
Liu Hui1f3c79a2009-01-05 15:57:51 -05001770static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001771 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001772{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001773 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001774 u64 discarded_bytes = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001775 struct btrfs_multi_bio *multi = NULL;
1776
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001777
Liu Hui1f3c79a2009-01-05 15:57:51 -05001778 /* Tell the block device(s) that the sectors can be discarded */
Li Dongyang5378e602011-03-24 10:24:27 +00001779 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1780 bytenr, &num_bytes, &multi, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001781 if (!ret) {
1782 struct btrfs_bio_stripe *stripe = multi->stripes;
1783 int i;
1784
Liu Hui1f3c79a2009-01-05 15:57:51 -05001785
1786 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Li Dongyang5378e602011-03-24 10:24:27 +00001787 ret = btrfs_issue_discard(stripe->dev->bdev,
1788 stripe->physical,
1789 stripe->length);
1790 if (!ret)
1791 discarded_bytes += stripe->length;
1792 else if (ret != -EOPNOTSUPP)
1793 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001794 }
1795 kfree(multi);
1796 }
Li Dongyang5378e602011-03-24 10:24:27 +00001797 if (discarded_bytes && ret == -EOPNOTSUPP)
1798 ret = 0;
1799
1800 if (actual_bytes)
1801 *actual_bytes = discarded_bytes;
1802
Liu Hui1f3c79a2009-01-05 15:57:51 -05001803
1804 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001805}
1806
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001807int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1808 struct btrfs_root *root,
1809 u64 bytenr, u64 num_bytes, u64 parent,
1810 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04001811{
1812 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001813 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1814 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001815
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001816 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1817 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1818 parent, root_objectid, (int)owner,
1819 BTRFS_ADD_DELAYED_REF, NULL);
1820 } else {
1821 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1822 parent, root_objectid, owner, offset,
1823 BTRFS_ADD_DELAYED_REF, NULL);
1824 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001825 return ret;
1826}
1827
Chris Mason925baed2008-06-25 16:01:30 -04001828static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001829 struct btrfs_root *root,
1830 u64 bytenr, u64 num_bytes,
1831 u64 parent, u64 root_objectid,
1832 u64 owner, u64 offset, int refs_to_add,
1833 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001834{
Chris Mason5caf2a02007-04-02 11:20:42 -04001835 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001836 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001837 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001838 u64 refs;
1839 int ret;
1840 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001841
Chris Mason5caf2a02007-04-02 11:20:42 -04001842 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001843 if (!path)
1844 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001845
Chris Mason3c12ac72008-04-21 12:01:38 -04001846 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001847 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001848 /* this will setup the path even if it fails to insert the back ref */
1849 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1850 path, bytenr, num_bytes, parent,
1851 root_objectid, owner, offset,
1852 refs_to_add, extent_op);
1853 if (ret == 0)
1854 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001855
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001856 if (ret != -EAGAIN) {
1857 err = ret;
1858 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001859 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001860
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001861 leaf = path->nodes[0];
1862 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1863 refs = btrfs_extent_refs(leaf, item);
1864 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1865 if (extent_op)
1866 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001868 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001869 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001870
Chris Mason3c12ac72008-04-21 12:01:38 -04001871 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001872 path->leave_spinning = 1;
1873
Chris Mason56bec292009-03-13 10:10:06 -04001874 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001875 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876 path, bytenr, parent, root_objectid,
1877 owner, offset, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -05001878 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001879out:
Chris Mason74493f72007-12-11 09:25:06 -05001880 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001881 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001882}
1883
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001884static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1885 struct btrfs_root *root,
1886 struct btrfs_delayed_ref_node *node,
1887 struct btrfs_delayed_extent_op *extent_op,
1888 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001889{
Chris Mason56bec292009-03-13 10:10:06 -04001890 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001891 struct btrfs_delayed_data_ref *ref;
1892 struct btrfs_key ins;
1893 u64 parent = 0;
1894 u64 ref_root = 0;
1895 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001896
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001897 ins.objectid = node->bytenr;
1898 ins.offset = node->num_bytes;
1899 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001900
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001901 ref = btrfs_delayed_node_to_data_ref(node);
1902 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1903 parent = ref->parent;
1904 else
1905 ref_root = ref->root;
1906
1907 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1908 if (extent_op) {
1909 BUG_ON(extent_op->update_key);
1910 flags |= extent_op->flags_to_set;
1911 }
1912 ret = alloc_reserved_file_extent(trans, root,
1913 parent, ref_root, flags,
1914 ref->objectid, ref->offset,
1915 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001916 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1917 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1918 node->num_bytes, parent,
1919 ref_root, ref->objectid,
1920 ref->offset, node->ref_mod,
1921 extent_op);
1922 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1923 ret = __btrfs_free_extent(trans, root, node->bytenr,
1924 node->num_bytes, parent,
1925 ref_root, ref->objectid,
1926 ref->offset, node->ref_mod,
1927 extent_op);
1928 } else {
1929 BUG();
1930 }
Chris Mason56bec292009-03-13 10:10:06 -04001931 return ret;
1932}
1933
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001934static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1935 struct extent_buffer *leaf,
1936 struct btrfs_extent_item *ei)
1937{
1938 u64 flags = btrfs_extent_flags(leaf, ei);
1939 if (extent_op->update_flags) {
1940 flags |= extent_op->flags_to_set;
1941 btrfs_set_extent_flags(leaf, ei, flags);
1942 }
1943
1944 if (extent_op->update_key) {
1945 struct btrfs_tree_block_info *bi;
1946 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1947 bi = (struct btrfs_tree_block_info *)(ei + 1);
1948 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1949 }
1950}
1951
1952static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1953 struct btrfs_root *root,
1954 struct btrfs_delayed_ref_node *node,
1955 struct btrfs_delayed_extent_op *extent_op)
1956{
1957 struct btrfs_key key;
1958 struct btrfs_path *path;
1959 struct btrfs_extent_item *ei;
1960 struct extent_buffer *leaf;
1961 u32 item_size;
1962 int ret;
1963 int err = 0;
1964
1965 path = btrfs_alloc_path();
1966 if (!path)
1967 return -ENOMEM;
1968
1969 key.objectid = node->bytenr;
1970 key.type = BTRFS_EXTENT_ITEM_KEY;
1971 key.offset = node->num_bytes;
1972
1973 path->reada = 1;
1974 path->leave_spinning = 1;
1975 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1976 path, 0, 1);
1977 if (ret < 0) {
1978 err = ret;
1979 goto out;
1980 }
1981 if (ret > 0) {
1982 err = -EIO;
1983 goto out;
1984 }
1985
1986 leaf = path->nodes[0];
1987 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1988#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1989 if (item_size < sizeof(*ei)) {
1990 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1991 path, (u64)-1, 0);
1992 if (ret < 0) {
1993 err = ret;
1994 goto out;
1995 }
1996 leaf = path->nodes[0];
1997 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1998 }
1999#endif
2000 BUG_ON(item_size < sizeof(*ei));
2001 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2002 __run_delayed_extent_op(extent_op, leaf, ei);
2003
2004 btrfs_mark_buffer_dirty(leaf);
2005out:
2006 btrfs_free_path(path);
2007 return err;
2008}
2009
2010static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2011 struct btrfs_root *root,
2012 struct btrfs_delayed_ref_node *node,
2013 struct btrfs_delayed_extent_op *extent_op,
2014 int insert_reserved)
2015{
2016 int ret = 0;
2017 struct btrfs_delayed_tree_ref *ref;
2018 struct btrfs_key ins;
2019 u64 parent = 0;
2020 u64 ref_root = 0;
2021
2022 ins.objectid = node->bytenr;
2023 ins.offset = node->num_bytes;
2024 ins.type = BTRFS_EXTENT_ITEM_KEY;
2025
2026 ref = btrfs_delayed_node_to_tree_ref(node);
2027 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2028 parent = ref->parent;
2029 else
2030 ref_root = ref->root;
2031
2032 BUG_ON(node->ref_mod != 1);
2033 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2034 BUG_ON(!extent_op || !extent_op->update_flags ||
2035 !extent_op->update_key);
2036 ret = alloc_reserved_tree_block(trans, root,
2037 parent, ref_root,
2038 extent_op->flags_to_set,
2039 &extent_op->key,
2040 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002041 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2042 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2043 node->num_bytes, parent, ref_root,
2044 ref->level, 0, 1, extent_op);
2045 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2046 ret = __btrfs_free_extent(trans, root, node->bytenr,
2047 node->num_bytes, parent, ref_root,
2048 ref->level, 0, 1, extent_op);
2049 } else {
2050 BUG();
2051 }
2052 return ret;
2053}
2054
Chris Mason56bec292009-03-13 10:10:06 -04002055/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002056static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2057 struct btrfs_root *root,
2058 struct btrfs_delayed_ref_node *node,
2059 struct btrfs_delayed_extent_op *extent_op,
2060 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002061{
Josef Bacikeb099672009-02-12 09:27:38 -05002062 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002063 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002064 struct btrfs_delayed_ref_head *head;
2065 /*
2066 * we've hit the end of the chain and we were supposed
2067 * to insert this extent into the tree. But, it got
2068 * deleted before we ever needed to insert it, so all
2069 * we have to do is clean up the accounting
2070 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002071 BUG_ON(extent_op);
2072 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002073 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002074 btrfs_pin_extent(root, node->bytenr,
2075 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002076 if (head->is_data) {
2077 ret = btrfs_del_csums(trans, root,
2078 node->bytenr,
2079 node->num_bytes);
2080 BUG_ON(ret);
2081 }
Chris Mason56bec292009-03-13 10:10:06 -04002082 }
Chris Mason56bec292009-03-13 10:10:06 -04002083 mutex_unlock(&head->mutex);
2084 return 0;
2085 }
Josef Bacikeb099672009-02-12 09:27:38 -05002086
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002087 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2088 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2089 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2090 insert_reserved);
2091 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2092 node->type == BTRFS_SHARED_DATA_REF_KEY)
2093 ret = run_delayed_data_ref(trans, root, node, extent_op,
2094 insert_reserved);
2095 else
2096 BUG();
2097 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002098}
2099
Chris Mason56bec292009-03-13 10:10:06 -04002100static noinline struct btrfs_delayed_ref_node *
2101select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002102{
Chris Mason56bec292009-03-13 10:10:06 -04002103 struct rb_node *node;
2104 struct btrfs_delayed_ref_node *ref;
2105 int action = BTRFS_ADD_DELAYED_REF;
2106again:
2107 /*
2108 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2109 * this prevents ref count from going down to zero when
2110 * there still are pending delayed ref.
2111 */
2112 node = rb_prev(&head->node.rb_node);
2113 while (1) {
2114 if (!node)
2115 break;
2116 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2117 rb_node);
2118 if (ref->bytenr != head->node.bytenr)
2119 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002120 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002121 return ref;
2122 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002123 }
Chris Mason56bec292009-03-13 10:10:06 -04002124 if (action == BTRFS_ADD_DELAYED_REF) {
2125 action = BTRFS_DROP_DELAYED_REF;
2126 goto again;
2127 }
2128 return NULL;
2129}
2130
Chris Masonc3e69d52009-03-13 10:17:05 -04002131static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2132 struct btrfs_root *root,
2133 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002134{
Chris Mason56bec292009-03-13 10:10:06 -04002135 struct btrfs_delayed_ref_root *delayed_refs;
2136 struct btrfs_delayed_ref_node *ref;
2137 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002138 struct btrfs_delayed_extent_op *extent_op;
Chris Mason56bec292009-03-13 10:10:06 -04002139 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002140 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002141 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002142
2143 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002144 while (1) {
2145 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002146 /* pick a new head ref from the cluster list */
2147 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002148 break;
Chris Mason56bec292009-03-13 10:10:06 -04002149
Chris Masonc3e69d52009-03-13 10:17:05 -04002150 locked_ref = list_entry(cluster->next,
2151 struct btrfs_delayed_ref_head, cluster);
2152
2153 /* grab the lock that says we are going to process
2154 * all the refs for this head */
2155 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2156
2157 /*
2158 * we may have dropped the spin lock to get the head
2159 * mutex lock, and that might have given someone else
2160 * time to free the head. If that's true, it has been
2161 * removed from our list and we can move on.
2162 */
2163 if (ret == -EAGAIN) {
2164 locked_ref = NULL;
2165 count++;
2166 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002167 }
2168 }
2169
2170 /*
2171 * record the must insert reserved flag before we
2172 * drop the spin lock.
2173 */
2174 must_insert_reserved = locked_ref->must_insert_reserved;
2175 locked_ref->must_insert_reserved = 0;
2176
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002177 extent_op = locked_ref->extent_op;
2178 locked_ref->extent_op = NULL;
2179
Chris Mason56bec292009-03-13 10:10:06 -04002180 /*
2181 * locked_ref is the head node, so we have to go one
2182 * node back for any delayed ref updates
2183 */
Chris Mason56bec292009-03-13 10:10:06 -04002184 ref = select_delayed_ref(locked_ref);
2185 if (!ref) {
2186 /* All delayed refs have been processed, Go ahead
2187 * and send the head node to run_one_delayed_ref,
2188 * so that any accounting fixes can happen
2189 */
2190 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002191
2192 if (extent_op && must_insert_reserved) {
2193 kfree(extent_op);
2194 extent_op = NULL;
2195 }
2196
2197 if (extent_op) {
2198 spin_unlock(&delayed_refs->lock);
2199
2200 ret = run_delayed_extent_op(trans, root,
2201 ref, extent_op);
2202 BUG_ON(ret);
2203 kfree(extent_op);
2204
2205 cond_resched();
2206 spin_lock(&delayed_refs->lock);
2207 continue;
2208 }
2209
Chris Masonc3e69d52009-03-13 10:17:05 -04002210 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002211 locked_ref = NULL;
2212 }
2213
2214 ref->in_tree = 0;
2215 rb_erase(&ref->rb_node, &delayed_refs->root);
2216 delayed_refs->num_entries--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002217
Chris Mason56bec292009-03-13 10:10:06 -04002218 spin_unlock(&delayed_refs->lock);
2219
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002220 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002221 must_insert_reserved);
2222 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002223
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002224 btrfs_put_delayed_ref(ref);
2225 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002226 count++;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002227
Chris Mason1887be62009-03-13 10:11:24 -04002228 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002229 spin_lock(&delayed_refs->lock);
2230 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002231 return count;
2232}
2233
2234/*
2235 * this starts processing the delayed reference count updates and
2236 * extent insertions we have queued up so far. count can be
2237 * 0, which means to process everything in the tree at the start
2238 * of the run (but not newly added entries), or it can be some target
2239 * number you'd like to process.
2240 */
2241int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2242 struct btrfs_root *root, unsigned long count)
2243{
2244 struct rb_node *node;
2245 struct btrfs_delayed_ref_root *delayed_refs;
2246 struct btrfs_delayed_ref_node *ref;
2247 struct list_head cluster;
2248 int ret;
2249 int run_all = count == (unsigned long)-1;
2250 int run_most = 0;
2251
2252 if (root == root->fs_info->extent_root)
2253 root = root->fs_info->tree_root;
2254
2255 delayed_refs = &trans->transaction->delayed_refs;
2256 INIT_LIST_HEAD(&cluster);
2257again:
2258 spin_lock(&delayed_refs->lock);
2259 if (count == 0) {
2260 count = delayed_refs->num_entries * 2;
2261 run_most = 1;
2262 }
2263 while (1) {
2264 if (!(run_all || run_most) &&
2265 delayed_refs->num_heads_ready < 64)
2266 break;
2267
2268 /*
2269 * go find something we can process in the rbtree. We start at
2270 * the beginning of the tree, and then build a cluster
2271 * of refs to process starting at the first one we are able to
2272 * lock
2273 */
2274 ret = btrfs_find_ref_cluster(trans, &cluster,
2275 delayed_refs->run_delayed_start);
2276 if (ret)
2277 break;
2278
2279 ret = run_clustered_refs(trans, root, &cluster);
2280 BUG_ON(ret < 0);
2281
2282 count -= min_t(unsigned long, ret, count);
2283
2284 if (count == 0)
2285 break;
2286 }
2287
Chris Mason56bec292009-03-13 10:10:06 -04002288 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04002289 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002290 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002291 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002292 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002293
2294 while (node) {
2295 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2296 rb_node);
2297 if (btrfs_delayed_ref_is_head(ref)) {
2298 struct btrfs_delayed_ref_head *head;
2299
2300 head = btrfs_delayed_node_to_head(ref);
2301 atomic_inc(&ref->refs);
2302
2303 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002304 /*
2305 * Mutex was contended, block until it's
2306 * released and try again
2307 */
Chris Mason56bec292009-03-13 10:10:06 -04002308 mutex_lock(&head->mutex);
2309 mutex_unlock(&head->mutex);
2310
2311 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002312 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002313 goto again;
2314 }
2315 node = rb_next(node);
2316 }
2317 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002318 schedule_timeout(1);
2319 goto again;
2320 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002321out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002322 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05002323 return 0;
2324}
2325
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002326int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2327 struct btrfs_root *root,
2328 u64 bytenr, u64 num_bytes, u64 flags,
2329 int is_data)
2330{
2331 struct btrfs_delayed_extent_op *extent_op;
2332 int ret;
2333
2334 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2335 if (!extent_op)
2336 return -ENOMEM;
2337
2338 extent_op->flags_to_set = flags;
2339 extent_op->update_flags = 1;
2340 extent_op->update_key = 0;
2341 extent_op->is_data = is_data ? 1 : 0;
2342
2343 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2344 if (ret)
2345 kfree(extent_op);
2346 return ret;
2347}
2348
2349static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2350 struct btrfs_root *root,
2351 struct btrfs_path *path,
2352 u64 objectid, u64 offset, u64 bytenr)
2353{
2354 struct btrfs_delayed_ref_head *head;
2355 struct btrfs_delayed_ref_node *ref;
2356 struct btrfs_delayed_data_ref *data_ref;
2357 struct btrfs_delayed_ref_root *delayed_refs;
2358 struct rb_node *node;
2359 int ret = 0;
2360
2361 ret = -ENOENT;
2362 delayed_refs = &trans->transaction->delayed_refs;
2363 spin_lock(&delayed_refs->lock);
2364 head = btrfs_find_delayed_ref_head(trans, bytenr);
2365 if (!head)
2366 goto out;
2367
2368 if (!mutex_trylock(&head->mutex)) {
2369 atomic_inc(&head->node.refs);
2370 spin_unlock(&delayed_refs->lock);
2371
David Sterbab3b4aa72011-04-21 01:20:15 +02002372 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002373
David Sterba8cc33e52011-05-02 15:29:25 +02002374 /*
2375 * Mutex was contended, block until it's released and let
2376 * caller try again
2377 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002378 mutex_lock(&head->mutex);
2379 mutex_unlock(&head->mutex);
2380 btrfs_put_delayed_ref(&head->node);
2381 return -EAGAIN;
2382 }
2383
2384 node = rb_prev(&head->node.rb_node);
2385 if (!node)
2386 goto out_unlock;
2387
2388 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2389
2390 if (ref->bytenr != bytenr)
2391 goto out_unlock;
2392
2393 ret = 1;
2394 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2395 goto out_unlock;
2396
2397 data_ref = btrfs_delayed_node_to_data_ref(ref);
2398
2399 node = rb_prev(node);
2400 if (node) {
2401 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2402 if (ref->bytenr == bytenr)
2403 goto out_unlock;
2404 }
2405
2406 if (data_ref->root != root->root_key.objectid ||
2407 data_ref->objectid != objectid || data_ref->offset != offset)
2408 goto out_unlock;
2409
2410 ret = 0;
2411out_unlock:
2412 mutex_unlock(&head->mutex);
2413out:
2414 spin_unlock(&delayed_refs->lock);
2415 return ret;
2416}
2417
2418static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2419 struct btrfs_root *root,
2420 struct btrfs_path *path,
2421 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002422{
2423 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002424 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002425 struct btrfs_extent_data_ref *ref;
2426 struct btrfs_extent_inline_ref *iref;
2427 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002428 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002429 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002430 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002431
Chris Masonbe20aa92007-12-17 20:14:01 -05002432 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002433 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002434 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002435
Chris Masonbe20aa92007-12-17 20:14:01 -05002436 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2437 if (ret < 0)
2438 goto out;
2439 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04002440
2441 ret = -ENOENT;
2442 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002443 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002444
Zheng Yan31840ae2008-09-23 13:14:14 -04002445 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002446 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002447 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002448
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002449 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002450 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002451
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002452 ret = 1;
2453 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2454#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2455 if (item_size < sizeof(*ei)) {
2456 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2457 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002458 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002459#endif
2460 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2461
2462 if (item_size != sizeof(*ei) +
2463 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2464 goto out;
2465
2466 if (btrfs_extent_generation(leaf, ei) <=
2467 btrfs_root_last_snapshot(&root->root_item))
2468 goto out;
2469
2470 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2471 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2472 BTRFS_EXTENT_DATA_REF_KEY)
2473 goto out;
2474
2475 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2476 if (btrfs_extent_refs(leaf, ei) !=
2477 btrfs_extent_data_ref_count(leaf, ref) ||
2478 btrfs_extent_data_ref_root(leaf, ref) !=
2479 root->root_key.objectid ||
2480 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2481 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2482 goto out;
2483
Yan Zhengf321e492008-07-30 09:26:11 -04002484 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002485out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002486 return ret;
2487}
2488
2489int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2490 struct btrfs_root *root,
2491 u64 objectid, u64 offset, u64 bytenr)
2492{
2493 struct btrfs_path *path;
2494 int ret;
2495 int ret2;
2496
2497 path = btrfs_alloc_path();
2498 if (!path)
2499 return -ENOENT;
2500
2501 do {
2502 ret = check_committed_ref(trans, root, path, objectid,
2503 offset, bytenr);
2504 if (ret && ret != -ENOENT)
2505 goto out;
2506
2507 ret2 = check_delayed_ref(trans, root, path, objectid,
2508 offset, bytenr);
2509 } while (ret2 == -EAGAIN);
2510
2511 if (ret2 && ret2 != -ENOENT) {
2512 ret = ret2;
2513 goto out;
2514 }
2515
2516 if (ret != -ENOENT || ret2 != -ENOENT)
2517 ret = 0;
2518out:
Yan Zhengf321e492008-07-30 09:26:11 -04002519 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002520 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2521 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002522 return ret;
2523}
2524
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002525static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002526 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002527 struct extent_buffer *buf,
2528 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002529{
2530 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002531 u64 num_bytes;
2532 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002533 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002534 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002535 struct btrfs_key key;
2536 struct btrfs_file_extent_item *fi;
2537 int i;
2538 int level;
2539 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002540 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002541 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04002542
2543 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002544 nritems = btrfs_header_nritems(buf);
2545 level = btrfs_header_level(buf);
2546
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002547 if (!root->ref_cows && level == 0)
2548 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002549
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002550 if (inc)
2551 process_func = btrfs_inc_extent_ref;
2552 else
2553 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002554
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002555 if (full_backref)
2556 parent = buf->start;
2557 else
2558 parent = 0;
2559
Zheng Yan31840ae2008-09-23 13:14:14 -04002560 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002561 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002562 btrfs_item_key_to_cpu(buf, &key, i);
2563 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002564 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002565 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002566 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002567 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002568 BTRFS_FILE_EXTENT_INLINE)
2569 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002570 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2571 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002572 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002573
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002574 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2575 key.offset -= btrfs_file_extent_offset(buf, fi);
2576 ret = process_func(trans, root, bytenr, num_bytes,
2577 parent, ref_root, key.objectid,
2578 key.offset);
2579 if (ret)
2580 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002581 } else {
2582 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002583 num_bytes = btrfs_level_size(root, level - 1);
2584 ret = process_func(trans, root, bytenr, num_bytes,
2585 parent, ref_root, level - 1, 0);
2586 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002587 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002588 }
2589 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002590 return 0;
2591fail:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002592 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002593 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002594}
2595
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002596int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2597 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002598{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002599 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2600}
Zheng Yan31840ae2008-09-23 13:14:14 -04002601
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002602int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2603 struct extent_buffer *buf, int full_backref)
2604{
2605 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002606}
2607
Chris Mason9078a3e2007-04-26 16:46:15 -04002608static int write_one_cache_group(struct btrfs_trans_handle *trans,
2609 struct btrfs_root *root,
2610 struct btrfs_path *path,
2611 struct btrfs_block_group_cache *cache)
2612{
2613 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002614 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002615 unsigned long bi;
2616 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002617
Chris Mason9078a3e2007-04-26 16:46:15 -04002618 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002619 if (ret < 0)
2620 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04002621 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002622
2623 leaf = path->nodes[0];
2624 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2625 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2626 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002627 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002628fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04002629 if (ret)
2630 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002631 return 0;
2632
2633}
2634
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002635static struct btrfs_block_group_cache *
2636next_block_group(struct btrfs_root *root,
2637 struct btrfs_block_group_cache *cache)
2638{
2639 struct rb_node *node;
2640 spin_lock(&root->fs_info->block_group_cache_lock);
2641 node = rb_next(&cache->cache_node);
2642 btrfs_put_block_group(cache);
2643 if (node) {
2644 cache = rb_entry(node, struct btrfs_block_group_cache,
2645 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002646 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002647 } else
2648 cache = NULL;
2649 spin_unlock(&root->fs_info->block_group_cache_lock);
2650 return cache;
2651}
2652
Josef Bacik0af3d002010-06-21 14:48:16 -04002653static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2654 struct btrfs_trans_handle *trans,
2655 struct btrfs_path *path)
2656{
2657 struct btrfs_root *root = block_group->fs_info->tree_root;
2658 struct inode *inode = NULL;
2659 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002660 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002661 int num_pages = 0;
2662 int retries = 0;
2663 int ret = 0;
2664
2665 /*
2666 * If this block group is smaller than 100 megs don't bother caching the
2667 * block group.
2668 */
2669 if (block_group->key.offset < (100 * 1024 * 1024)) {
2670 spin_lock(&block_group->lock);
2671 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2672 spin_unlock(&block_group->lock);
2673 return 0;
2674 }
2675
2676again:
2677 inode = lookup_free_space_inode(root, block_group, path);
2678 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2679 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002680 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002681 goto out;
2682 }
2683
2684 if (IS_ERR(inode)) {
2685 BUG_ON(retries);
2686 retries++;
2687
2688 if (block_group->ro)
2689 goto out_free;
2690
2691 ret = create_free_space_inode(root, trans, block_group, path);
2692 if (ret)
2693 goto out_free;
2694 goto again;
2695 }
2696
2697 /*
2698 * We want to set the generation to 0, that way if anything goes wrong
2699 * from here on out we know not to trust this cache when we load up next
2700 * time.
2701 */
2702 BTRFS_I(inode)->generation = 0;
2703 ret = btrfs_update_inode(trans, root, inode);
2704 WARN_ON(ret);
2705
2706 if (i_size_read(inode) > 0) {
2707 ret = btrfs_truncate_free_space_cache(root, trans, path,
2708 inode);
2709 if (ret)
2710 goto out_put;
2711 }
2712
2713 spin_lock(&block_group->lock);
2714 if (block_group->cached != BTRFS_CACHE_FINISHED) {
Josef Bacik2b209822010-12-03 13:17:53 -05002715 /* We're not cached, don't bother trying to write stuff out */
2716 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002717 spin_unlock(&block_group->lock);
2718 goto out_put;
2719 }
2720 spin_unlock(&block_group->lock);
2721
2722 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2723 if (!num_pages)
2724 num_pages = 1;
2725
2726 /*
2727 * Just to make absolutely sure we have enough space, we're going to
2728 * preallocate 12 pages worth of space for each block group. In
2729 * practice we ought to use at most 8, but we need extra space so we can
2730 * add our header and have a terminator between the extents and the
2731 * bitmaps.
2732 */
2733 num_pages *= 16;
2734 num_pages *= PAGE_CACHE_SIZE;
2735
2736 ret = btrfs_check_data_free_space(inode, num_pages);
2737 if (ret)
2738 goto out_put;
2739
2740 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2741 num_pages, num_pages,
2742 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05002743 if (!ret)
2744 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04002745 btrfs_free_reserved_data_space(inode, num_pages);
2746out_put:
2747 iput(inode);
2748out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02002749 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002750out:
2751 spin_lock(&block_group->lock);
Josef Bacik2b209822010-12-03 13:17:53 -05002752 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04002753 spin_unlock(&block_group->lock);
2754
2755 return ret;
2756}
2757
Chris Mason96b51792007-10-15 16:15:19 -04002758int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2759 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04002760{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002761 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002762 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002763 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04002764 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002765
2766 path = btrfs_alloc_path();
2767 if (!path)
2768 return -ENOMEM;
2769
Josef Bacik0af3d002010-06-21 14:48:16 -04002770again:
2771 while (1) {
2772 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2773 while (cache) {
2774 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2775 break;
2776 cache = next_block_group(root, cache);
2777 }
2778 if (!cache) {
2779 if (last == 0)
2780 break;
2781 last = 0;
2782 continue;
2783 }
2784 err = cache_save_setup(cache, trans, path);
2785 last = cache->key.objectid + cache->key.offset;
2786 btrfs_put_block_group(cache);
2787 }
2788
Chris Masond3977122009-01-05 21:25:51 -05002789 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002790 if (last == 0) {
2791 err = btrfs_run_delayed_refs(trans, root,
2792 (unsigned long)-1);
2793 BUG_ON(err);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002794 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002795
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002796 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2797 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04002798 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2799 btrfs_put_block_group(cache);
2800 goto again;
2801 }
2802
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002803 if (cache->dirty)
2804 break;
2805 cache = next_block_group(root, cache);
2806 }
2807 if (!cache) {
2808 if (last == 0)
2809 break;
2810 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04002811 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04002812 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002813
Josef Bacik0cb59c92010-07-02 12:14:14 -04002814 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2815 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002816 cache->dirty = 0;
2817 last = cache->key.objectid + cache->key.offset;
2818
2819 err = write_one_cache_group(trans, root, path, cache);
2820 BUG_ON(err);
2821 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04002822 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002823
Josef Bacik0cb59c92010-07-02 12:14:14 -04002824 while (1) {
2825 /*
2826 * I don't think this is needed since we're just marking our
2827 * preallocated extent as written, but just in case it can't
2828 * hurt.
2829 */
2830 if (last == 0) {
2831 err = btrfs_run_delayed_refs(trans, root,
2832 (unsigned long)-1);
2833 BUG_ON(err);
2834 }
2835
2836 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2837 while (cache) {
2838 /*
2839 * Really this shouldn't happen, but it could if we
2840 * couldn't write the entire preallocated extent and
2841 * splitting the extent resulted in a new block.
2842 */
2843 if (cache->dirty) {
2844 btrfs_put_block_group(cache);
2845 goto again;
2846 }
2847 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2848 break;
2849 cache = next_block_group(root, cache);
2850 }
2851 if (!cache) {
2852 if (last == 0)
2853 break;
2854 last = 0;
2855 continue;
2856 }
2857
2858 btrfs_write_out_cache(root, trans, cache, path);
2859
2860 /*
2861 * If we didn't have an error then the cache state is still
2862 * NEED_WRITE, so we can set it to WRITTEN.
2863 */
2864 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2865 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2866 last = cache->key.objectid + cache->key.offset;
2867 btrfs_put_block_group(cache);
2868 }
2869
Chris Mason9078a3e2007-04-26 16:46:15 -04002870 btrfs_free_path(path);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002871 return 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002872}
2873
Yan Zhengd2fb3432008-12-11 16:30:39 -05002874int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2875{
2876 struct btrfs_block_group_cache *block_group;
2877 int readonly = 0;
2878
2879 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2880 if (!block_group || block_group->ro)
2881 readonly = 1;
2882 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002883 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002884 return readonly;
2885}
2886
Chris Mason593060d2008-03-25 16:50:33 -04002887static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2888 u64 total_bytes, u64 bytes_used,
2889 struct btrfs_space_info **space_info)
2890{
2891 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002892 int i;
2893 int factor;
2894
2895 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2896 BTRFS_BLOCK_GROUP_RAID10))
2897 factor = 2;
2898 else
2899 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002900
2901 found = __find_space_info(info, flags);
2902 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04002903 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002904 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002905 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002906 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002907 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04002908 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002909 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04002910 *space_info = found;
2911 return 0;
2912 }
Yan Zhengc146afa2008-11-12 14:34:12 -05002913 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04002914 if (!found)
2915 return -ENOMEM;
2916
Yan, Zhengb742bb82010-05-16 10:46:24 -04002917 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2918 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04002919 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002920 spin_lock_init(&found->lock);
Yan, Zhengb742bb82010-05-16 10:46:24 -04002921 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2922 BTRFS_BLOCK_GROUP_SYSTEM |
2923 BTRFS_BLOCK_GROUP_METADATA);
Chris Mason593060d2008-03-25 16:50:33 -04002924 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04002925 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002926 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04002927 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04002928 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04002929 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05002930 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002931 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002932 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04002933 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04002934 found->chunk_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002935 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04002936 list_add_rcu(&found->list, &info->space_info);
Josef Bacik817d52f2009-07-13 21:29:25 -04002937 atomic_set(&found->caching_threads, 0);
Chris Mason593060d2008-03-25 16:50:33 -04002938 return 0;
2939}
2940
Chris Mason8790d502008-04-03 16:29:03 -04002941static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2942{
2943 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04002944 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002945 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04002946 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04002947 if (extra_flags) {
2948 if (flags & BTRFS_BLOCK_GROUP_DATA)
2949 fs_info->avail_data_alloc_bits |= extra_flags;
2950 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2951 fs_info->avail_metadata_alloc_bits |= extra_flags;
2952 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2953 fs_info->avail_system_alloc_bits |= extra_flags;
2954 }
2955}
Chris Mason593060d2008-03-25 16:50:33 -04002956
Yan Zheng2b820322008-11-17 21:11:30 -05002957u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04002958{
Chris Masoncd02dca2010-12-13 14:56:23 -05002959 /*
2960 * we add in the count of missing devices because we want
2961 * to make sure that any RAID levels on a degraded FS
2962 * continue to be honored.
2963 */
2964 u64 num_devices = root->fs_info->fs_devices->rw_devices +
2965 root->fs_info->fs_devices->missing_devices;
Chris Masona061fc82008-05-07 11:43:44 -04002966
2967 if (num_devices == 1)
2968 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2969 if (num_devices < 4)
2970 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2971
Chris Masonec44a352008-04-28 15:29:52 -04002972 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2973 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04002974 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04002975 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04002976 }
Chris Masonec44a352008-04-28 15:29:52 -04002977
2978 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04002979 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04002980 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04002981 }
Chris Masonec44a352008-04-28 15:29:52 -04002982
2983 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2984 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2985 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2986 (flags & BTRFS_BLOCK_GROUP_DUP)))
2987 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2988 return flags;
2989}
2990
Yan, Zhengb742bb82010-05-16 10:46:24 -04002991static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05002992{
Yan, Zhengb742bb82010-05-16 10:46:24 -04002993 if (flags & BTRFS_BLOCK_GROUP_DATA)
2994 flags |= root->fs_info->avail_data_alloc_bits &
2995 root->fs_info->data_alloc_profile;
2996 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2997 flags |= root->fs_info->avail_system_alloc_bits &
2998 root->fs_info->system_alloc_profile;
2999 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3000 flags |= root->fs_info->avail_metadata_alloc_bits &
3001 root->fs_info->metadata_alloc_profile;
3002 return btrfs_reduce_alloc_profile(root, flags);
3003}
Josef Bacik6a632092009-02-20 11:00:09 -05003004
Miao Xie6d07bce2011-01-05 10:07:31 +00003005u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003006{
3007 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003008
Yan, Zhengb742bb82010-05-16 10:46:24 -04003009 if (data)
3010 flags = BTRFS_BLOCK_GROUP_DATA;
3011 else if (root == root->fs_info->chunk_root)
3012 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3013 else
3014 flags = BTRFS_BLOCK_GROUP_METADATA;
3015
3016 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003017}
3018
3019void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3020{
Josef Bacik6a632092009-02-20 11:00:09 -05003021 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04003022 BTRFS_BLOCK_GROUP_DATA);
Josef Bacik6a632092009-02-20 11:00:09 -05003023}
3024
3025/*
3026 * This will check the space that the inode allocates from to make sure we have
3027 * enough space for bytes.
3028 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003029int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003030{
3031 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003032 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikab6e24102010-03-19 14:38:13 +00003033 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003034 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003035
3036 /* make sure bytes are sectorsize aligned */
3037 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3038
Josef Bacik0af3d002010-06-21 14:48:16 -04003039 if (root == root->fs_info->tree_root) {
3040 alloc_chunk = 0;
3041 committed = 1;
3042 }
3043
Josef Bacik6a632092009-02-20 11:00:09 -05003044 data_sinfo = BTRFS_I(inode)->space_info;
Chris Mason33b4d472009-09-22 14:45:50 -04003045 if (!data_sinfo)
3046 goto alloc;
3047
Josef Bacik6a632092009-02-20 11:00:09 -05003048again:
3049 /* make sure we have enough space to handle the data first */
3050 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003051 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3052 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3053 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003054
3055 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003056 struct btrfs_trans_handle *trans;
3057
Josef Bacik6a632092009-02-20 11:00:09 -05003058 /*
3059 * if we don't have enough free bytes in this space then we need
3060 * to alloc a new chunk.
3061 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003062 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003063 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003064
Chris Mason0e4f8f82011-04-15 16:05:44 -04003065 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003066 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003067alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003068 alloc_target = btrfs_get_alloc_profile(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003069 trans = btrfs_join_transaction(root, 1);
3070 if (IS_ERR(trans))
3071 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003072
3073 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3074 bytes + 2 * 1024 * 1024,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003075 alloc_target,
3076 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003077 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003078 if (ret < 0) {
3079 if (ret != -ENOSPC)
3080 return ret;
3081 else
3082 goto commit_trans;
3083 }
Chris Mason33b4d472009-09-22 14:45:50 -04003084
3085 if (!data_sinfo) {
3086 btrfs_set_inode_space_info(root, inode);
3087 data_sinfo = BTRFS_I(inode)->space_info;
3088 }
Josef Bacik6a632092009-02-20 11:00:09 -05003089 goto again;
3090 }
3091 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003092
3093 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003094commit_trans:
Sage Weildd7e0b72009-09-29 18:38:44 -04003095 if (!committed && !root->fs_info->open_ioctl_trans) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003096 committed = 1;
3097 trans = btrfs_join_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003098 if (IS_ERR(trans))
3099 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003100 ret = btrfs_commit_transaction(trans, root);
3101 if (ret)
3102 return ret;
3103 goto again;
3104 }
3105
Josef Bacik6a632092009-02-20 11:00:09 -05003106 return -ENOSPC;
3107 }
3108 data_sinfo->bytes_may_use += bytes;
3109 BTRFS_I(inode)->reserved_bytes += bytes;
3110 spin_unlock(&data_sinfo->lock);
3111
Josef Bacik9ed74f22009-09-11 16:12:44 -04003112 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003113}
3114
3115/*
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003116 * called when we are clearing an delalloc extent from the
3117 * inode's io_tree or there was an error for whatever reason
3118 * after calling btrfs_check_data_free_space
Josef Bacik6a632092009-02-20 11:00:09 -05003119 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003120void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003121{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003122 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003123 struct btrfs_space_info *data_sinfo;
3124
3125 /* make sure bytes are sectorsize aligned */
3126 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3127
3128 data_sinfo = BTRFS_I(inode)->space_info;
3129 spin_lock(&data_sinfo->lock);
3130 data_sinfo->bytes_may_use -= bytes;
3131 BTRFS_I(inode)->reserved_bytes -= bytes;
3132 spin_unlock(&data_sinfo->lock);
3133}
3134
Josef Bacik97e728d2009-04-21 17:40:57 -04003135static void force_metadata_allocation(struct btrfs_fs_info *info)
3136{
3137 struct list_head *head = &info->space_info;
3138 struct btrfs_space_info *found;
3139
3140 rcu_read_lock();
3141 list_for_each_entry_rcu(found, head, list) {
3142 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003143 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003144 }
3145 rcu_read_unlock();
3146}
3147
Chris Masone5bc2452010-10-26 13:37:56 -04003148static int should_alloc_chunk(struct btrfs_root *root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003149 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3150 int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003151{
3152 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003153 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003154 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003155
Chris Mason0e4f8f82011-04-15 16:05:44 -04003156 if (force == CHUNK_ALLOC_FORCE)
3157 return 1;
3158
3159 /*
3160 * in limited mode, we want to have some free space up to
3161 * about 1% of the FS size.
3162 */
3163 if (force == CHUNK_ALLOC_LIMITED) {
3164 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3165 thresh = max_t(u64, 64 * 1024 * 1024,
3166 div_factor_fine(thresh, 1));
3167
3168 if (num_bytes - num_allocated < thresh)
3169 return 1;
3170 }
3171
3172 /*
3173 * we have two similar checks here, one based on percentage
3174 * and once based on a hard number of 256MB. The idea
3175 * is that if we have a good amount of free
3176 * room, don't allocate a chunk. A good mount is
3177 * less than 80% utilized of the chunks we have allocated,
3178 * or more than 256MB free
3179 */
3180 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
Yan, Zheng424499d2010-05-16 10:46:25 -04003181 return 0;
3182
Chris Mason0e4f8f82011-04-15 16:05:44 -04003183 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
Yan, Zheng424499d2010-05-16 10:46:25 -04003184 return 0;
3185
Chris Masone5bc2452010-10-26 13:37:56 -04003186 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003187
3188 /* 256MB or 5% of the FS */
Chris Masone5bc2452010-10-26 13:37:56 -04003189 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3190
3191 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003192 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003193 return 1;
3194}
3195
Chris Mason6324fbf2008-03-24 15:01:59 -04003196static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3197 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04003198 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003199{
3200 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003201 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003202 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003203 int ret = 0;
3204
Yan Zheng2b820322008-11-17 21:11:30 -05003205 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04003206
Chris Mason6324fbf2008-03-24 15:01:59 -04003207 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003208 if (!space_info) {
3209 ret = update_space_info(extent_root->fs_info, flags,
3210 0, 0, &space_info);
3211 BUG_ON(ret);
3212 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003213 BUG_ON(!space_info);
3214
Josef Bacik6d741192011-04-11 20:20:11 -04003215again:
Josef Bacik25179202008-10-29 14:49:05 -04003216 spin_lock(&space_info->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003217 if (space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003218 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003219 if (space_info->full) {
3220 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003221 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003222 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003223
Chris Mason0e4f8f82011-04-15 16:05:44 -04003224 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003225 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003226 return 0;
3227 } else if (space_info->chunk_alloc) {
3228 wait_for_alloc = 1;
3229 } else {
3230 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003231 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003232
Josef Bacik25179202008-10-29 14:49:05 -04003233 spin_unlock(&space_info->lock);
3234
Josef Bacik6d741192011-04-11 20:20:11 -04003235 mutex_lock(&fs_info->chunk_mutex);
3236
3237 /*
3238 * The chunk_mutex is held throughout the entirety of a chunk
3239 * allocation, so once we've acquired the chunk_mutex we know that the
3240 * other guy is done and we need to recheck and see if we should
3241 * allocate.
3242 */
3243 if (wait_for_alloc) {
3244 mutex_unlock(&fs_info->chunk_mutex);
3245 wait_for_alloc = 0;
3246 goto again;
3247 }
3248
Josef Bacik97e728d2009-04-21 17:40:57 -04003249 /*
Josef Bacik67377732010-09-16 16:19:09 -04003250 * If we have mixed data/metadata chunks we want to make sure we keep
3251 * allocating mixed chunks instead of individual chunks.
3252 */
3253 if (btrfs_mixed_space_info(space_info))
3254 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3255
3256 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003257 * if we're doing a data chunk, go ahead and make sure that
3258 * we keep a reasonable number of metadata chunks allocated in the
3259 * FS as well.
3260 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003261 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003262 fs_info->data_chunk_allocations++;
3263 if (!(fs_info->data_chunk_allocations %
3264 fs_info->metadata_ratio))
3265 force_metadata_allocation(fs_info);
3266 }
3267
Yan Zheng2b820322008-11-17 21:11:30 -05003268 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003269 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003270 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003271 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003272 else
3273 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003274
Chris Mason0e4f8f82011-04-15 16:05:44 -04003275 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003276 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003277 spin_unlock(&space_info->lock);
Yan Zhengc146afa2008-11-12 14:34:12 -05003278 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003279 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003280}
3281
Yan, Zheng5da9d012010-05-16 10:46:25 -04003282/*
3283 * shrink metadata reservation for delalloc
3284 */
3285static int shrink_delalloc(struct btrfs_trans_handle *trans,
Josef Bacik0019f102010-10-15 15:18:40 -04003286 struct btrfs_root *root, u64 to_reclaim, int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003287{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003288 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003289 struct btrfs_space_info *space_info;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003290 u64 reserved;
3291 u64 max_reclaim;
3292 u64 reclaimed = 0;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003293 long time_left;
Chris Masonbf9022e2010-10-26 13:40:45 -04003294 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003295 int loops = 0;
Chris Mason36e39c42011-03-12 07:08:42 -05003296 unsigned long progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003297
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003298 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003299 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003300
3301 smp_mb();
Josef Bacik0019f102010-10-15 15:18:40 -04003302 reserved = space_info->bytes_reserved;
Chris Mason36e39c42011-03-12 07:08:42 -05003303 progress = space_info->reservation_progress;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003304
3305 if (reserved == 0)
3306 return 0;
3307
3308 max_reclaim = min(reserved, to_reclaim);
3309
Josef Bacikb1953bc2011-01-21 21:10:01 +00003310 while (loops < 1024) {
Chris Masonbf9022e2010-10-26 13:40:45 -04003311 /* have the flusher threads jump in and do some IO */
3312 smp_mb();
3313 nr_pages = min_t(unsigned long, nr_pages,
3314 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3315 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003316
Josef Bacik0019f102010-10-15 15:18:40 -04003317 spin_lock(&space_info->lock);
Chris Mason36e39c42011-03-12 07:08:42 -05003318 if (reserved > space_info->bytes_reserved)
Josef Bacik0019f102010-10-15 15:18:40 -04003319 reclaimed += reserved - space_info->bytes_reserved;
3320 reserved = space_info->bytes_reserved;
3321 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003322
Chris Mason36e39c42011-03-12 07:08:42 -05003323 loops++;
3324
Yan, Zheng5da9d012010-05-16 10:46:25 -04003325 if (reserved == 0 || reclaimed >= max_reclaim)
3326 break;
3327
3328 if (trans && trans->transaction->blocked)
3329 return -EAGAIN;
Chris Masonbf9022e2010-10-26 13:40:45 -04003330
Chris Mason36e39c42011-03-12 07:08:42 -05003331 time_left = schedule_timeout_interruptible(1);
Josef Bacikb1953bc2011-01-21 21:10:01 +00003332
3333 /* We were interrupted, exit */
3334 if (time_left)
3335 break;
3336
Chris Mason36e39c42011-03-12 07:08:42 -05003337 /* we've kicked the IO a few times, if anything has been freed,
3338 * exit. There is no sense in looping here for a long time
3339 * when we really need to commit the transaction, or there are
3340 * just too many writers without enough free space
3341 */
3342
3343 if (loops > 3) {
3344 smp_mb();
3345 if (progress != space_info->reservation_progress)
3346 break;
3347 }
Chris Masonbf9022e2010-10-26 13:40:45 -04003348
Yan, Zheng5da9d012010-05-16 10:46:25 -04003349 }
3350 return reclaimed >= to_reclaim;
3351}
3352
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003353/*
3354 * Retries tells us how many times we've called reserve_metadata_bytes. The
3355 * idea is if this is the first call (retries == 0) then we will add to our
3356 * reserved count if we can't make the allocation in order to hold our place
3357 * while we go and try and free up space. That way for retries > 1 we don't try
3358 * and add space, we just check to see if the amount of unused space is >= the
3359 * total space, meaning that our reservation is valid.
3360 *
3361 * However if we don't intend to retry this reservation, pass -1 as retries so
3362 * that it short circuits this logic.
3363 */
3364static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3365 struct btrfs_root *root,
3366 struct btrfs_block_rsv *block_rsv,
3367 u64 orig_bytes, int flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003368{
3369 struct btrfs_space_info *space_info = block_rsv->space_info;
3370 u64 unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003371 u64 num_bytes = orig_bytes;
3372 int retries = 0;
3373 int ret = 0;
3374 bool reserved = false;
Josef Bacik38227932010-10-26 12:52:53 -04003375 bool committed = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003376
3377again:
3378 ret = -ENOSPC;
3379 if (reserved)
3380 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003381
3382 spin_lock(&space_info->lock);
3383 unused = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik6d487552010-10-15 15:13:32 -04003384 space_info->bytes_pinned + space_info->bytes_readonly +
3385 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003386
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003387 /*
3388 * The idea here is that we've not already over-reserved the block group
3389 * then we can go ahead and save our reservation first and then start
3390 * flushing if we need to. Otherwise if we've already overcommitted
3391 * lets start flushing stuff first and then come back and try to make
3392 * our reservation.
3393 */
3394 if (unused <= space_info->total_bytes) {
Arne Jansen6f334342010-11-12 23:17:56 +00003395 unused = space_info->total_bytes - unused;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003396 if (unused >= num_bytes) {
3397 if (!reserved)
3398 space_info->bytes_reserved += orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003399 ret = 0;
3400 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003401 /*
3402 * Ok set num_bytes to orig_bytes since we aren't
3403 * overocmmitted, this way we only try and reclaim what
3404 * we need.
3405 */
3406 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003407 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003408 } else {
3409 /*
3410 * Ok we're over committed, set num_bytes to the overcommitted
3411 * amount plus the amount of bytes that we need for this
3412 * reservation.
3413 */
3414 num_bytes = unused - space_info->total_bytes +
3415 (orig_bytes * (retries + 1));
Yan, Zhengf0486c62010-05-16 10:46:25 -04003416 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003417
3418 /*
3419 * Couldn't make our reservation, save our place so while we're trying
3420 * to reclaim space we can actually use it instead of somebody else
3421 * stealing it from us.
3422 */
3423 if (ret && !reserved) {
3424 space_info->bytes_reserved += orig_bytes;
3425 reserved = true;
3426 }
3427
Yan, Zhengf0486c62010-05-16 10:46:25 -04003428 spin_unlock(&space_info->lock);
3429
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003430 if (!ret)
3431 return 0;
3432
3433 if (!flush)
3434 goto out;
3435
3436 /*
3437 * We do synchronous shrinking since we don't actually unreserve
3438 * metadata until after the IO is completed.
3439 */
3440 ret = shrink_delalloc(trans, root, num_bytes, 1);
3441 if (ret > 0)
3442 return 0;
3443 else if (ret < 0)
3444 goto out;
3445
3446 /*
3447 * So if we were overcommitted it's possible that somebody else flushed
3448 * out enough space and we simply didn't have enough space to reclaim,
3449 * so go back around and try again.
3450 */
3451 if (retries < 2) {
3452 retries++;
3453 goto again;
3454 }
3455
3456 spin_lock(&space_info->lock);
3457 /*
3458 * Not enough space to be reclaimed, don't bother committing the
3459 * transaction.
3460 */
3461 if (space_info->bytes_pinned < orig_bytes)
3462 ret = -ENOSPC;
3463 spin_unlock(&space_info->lock);
3464 if (ret)
3465 goto out;
3466
3467 ret = -EAGAIN;
Josef Bacik38227932010-10-26 12:52:53 -04003468 if (trans || committed)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003469 goto out;
3470
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003471 ret = -ENOSPC;
3472 trans = btrfs_join_transaction(root, 1);
3473 if (IS_ERR(trans))
3474 goto out;
3475 ret = btrfs_commit_transaction(trans, root);
Josef Bacik38227932010-10-26 12:52:53 -04003476 if (!ret) {
3477 trans = NULL;
3478 committed = true;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003479 goto again;
Josef Bacik38227932010-10-26 12:52:53 -04003480 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003481
3482out:
3483 if (reserved) {
3484 spin_lock(&space_info->lock);
3485 space_info->bytes_reserved -= orig_bytes;
3486 spin_unlock(&space_info->lock);
3487 }
3488
Yan, Zhengf0486c62010-05-16 10:46:25 -04003489 return ret;
3490}
3491
3492static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3493 struct btrfs_root *root)
3494{
3495 struct btrfs_block_rsv *block_rsv;
3496 if (root->ref_cows)
3497 block_rsv = trans->block_rsv;
3498 else
3499 block_rsv = root->block_rsv;
3500
3501 if (!block_rsv)
3502 block_rsv = &root->fs_info->empty_block_rsv;
3503
3504 return block_rsv;
3505}
3506
3507static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3508 u64 num_bytes)
3509{
3510 int ret = -ENOSPC;
3511 spin_lock(&block_rsv->lock);
3512 if (block_rsv->reserved >= num_bytes) {
3513 block_rsv->reserved -= num_bytes;
3514 if (block_rsv->reserved < block_rsv->size)
3515 block_rsv->full = 0;
3516 ret = 0;
3517 }
3518 spin_unlock(&block_rsv->lock);
3519 return ret;
3520}
3521
3522static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3523 u64 num_bytes, int update_size)
3524{
3525 spin_lock(&block_rsv->lock);
3526 block_rsv->reserved += num_bytes;
3527 if (update_size)
3528 block_rsv->size += num_bytes;
3529 else if (block_rsv->reserved >= block_rsv->size)
3530 block_rsv->full = 1;
3531 spin_unlock(&block_rsv->lock);
3532}
3533
David Sterba62a45b62011-04-20 15:52:26 +02003534static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3535 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003536{
3537 struct btrfs_space_info *space_info = block_rsv->space_info;
3538
3539 spin_lock(&block_rsv->lock);
3540 if (num_bytes == (u64)-1)
3541 num_bytes = block_rsv->size;
3542 block_rsv->size -= num_bytes;
3543 if (block_rsv->reserved >= block_rsv->size) {
3544 num_bytes = block_rsv->reserved - block_rsv->size;
3545 block_rsv->reserved = block_rsv->size;
3546 block_rsv->full = 1;
3547 } else {
3548 num_bytes = 0;
3549 }
3550 spin_unlock(&block_rsv->lock);
3551
3552 if (num_bytes > 0) {
3553 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00003554 spin_lock(&dest->lock);
3555 if (!dest->full) {
3556 u64 bytes_to_add;
3557
3558 bytes_to_add = dest->size - dest->reserved;
3559 bytes_to_add = min(num_bytes, bytes_to_add);
3560 dest->reserved += bytes_to_add;
3561 if (dest->reserved >= dest->size)
3562 dest->full = 1;
3563 num_bytes -= bytes_to_add;
3564 }
3565 spin_unlock(&dest->lock);
3566 }
3567 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003568 spin_lock(&space_info->lock);
3569 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003570 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003571 spin_unlock(&space_info->lock);
3572 }
3573 }
3574}
3575
3576static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3577 struct btrfs_block_rsv *dst, u64 num_bytes)
3578{
3579 int ret;
3580
3581 ret = block_rsv_use_bytes(src, num_bytes);
3582 if (ret)
3583 return ret;
3584
3585 block_rsv_add_bytes(dst, num_bytes, 1);
3586 return 0;
3587}
3588
3589void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3590{
3591 memset(rsv, 0, sizeof(*rsv));
3592 spin_lock_init(&rsv->lock);
3593 atomic_set(&rsv->usage, 1);
3594 rsv->priority = 6;
3595 INIT_LIST_HEAD(&rsv->list);
3596}
3597
3598struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3599{
3600 struct btrfs_block_rsv *block_rsv;
3601 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003602
3603 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3604 if (!block_rsv)
3605 return NULL;
3606
3607 btrfs_init_block_rsv(block_rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003608 block_rsv->space_info = __find_space_info(fs_info,
3609 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003610 return block_rsv;
3611}
3612
3613void btrfs_free_block_rsv(struct btrfs_root *root,
3614 struct btrfs_block_rsv *rsv)
3615{
3616 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3617 btrfs_block_rsv_release(root, rsv, (u64)-1);
3618 if (!rsv->durable)
3619 kfree(rsv);
3620 }
3621}
3622
3623/*
3624 * make the block_rsv struct be able to capture freed space.
3625 * the captured space will re-add to the the block_rsv struct
3626 * after transaction commit
3627 */
3628void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3629 struct btrfs_block_rsv *block_rsv)
3630{
3631 block_rsv->durable = 1;
3632 mutex_lock(&fs_info->durable_block_rsv_mutex);
3633 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3634 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3635}
3636
3637int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3638 struct btrfs_root *root,
3639 struct btrfs_block_rsv *block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003640 u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003641{
3642 int ret;
3643
3644 if (num_bytes == 0)
3645 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003646
3647 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003648 if (!ret) {
3649 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3650 return 0;
3651 }
3652
Yan, Zhengf0486c62010-05-16 10:46:25 -04003653 return ret;
3654}
3655
3656int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3657 struct btrfs_root *root,
3658 struct btrfs_block_rsv *block_rsv,
3659 u64 min_reserved, int min_factor)
3660{
3661 u64 num_bytes = 0;
3662 int commit_trans = 0;
3663 int ret = -ENOSPC;
3664
3665 if (!block_rsv)
3666 return 0;
3667
3668 spin_lock(&block_rsv->lock);
3669 if (min_factor > 0)
3670 num_bytes = div_factor(block_rsv->size, min_factor);
3671 if (min_reserved > num_bytes)
3672 num_bytes = min_reserved;
3673
3674 if (block_rsv->reserved >= num_bytes) {
3675 ret = 0;
3676 } else {
3677 num_bytes -= block_rsv->reserved;
3678 if (block_rsv->durable &&
3679 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3680 commit_trans = 1;
3681 }
3682 spin_unlock(&block_rsv->lock);
3683 if (!ret)
3684 return 0;
3685
3686 if (block_rsv->refill_used) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003687 ret = reserve_metadata_bytes(trans, root, block_rsv,
3688 num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003689 if (!ret) {
3690 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3691 return 0;
3692 }
3693 }
3694
3695 if (commit_trans) {
3696 if (trans)
3697 return -EAGAIN;
3698
3699 trans = btrfs_join_transaction(root, 1);
3700 BUG_ON(IS_ERR(trans));
3701 ret = btrfs_commit_transaction(trans, root);
3702 return 0;
3703 }
3704
Yan, Zhengf0486c62010-05-16 10:46:25 -04003705 return -ENOSPC;
3706}
3707
3708int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3709 struct btrfs_block_rsv *dst_rsv,
3710 u64 num_bytes)
3711{
3712 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3713}
3714
3715void btrfs_block_rsv_release(struct btrfs_root *root,
3716 struct btrfs_block_rsv *block_rsv,
3717 u64 num_bytes)
3718{
3719 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3720 if (global_rsv->full || global_rsv == block_rsv ||
3721 block_rsv->space_info != global_rsv->space_info)
3722 global_rsv = NULL;
3723 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3724}
3725
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003726/*
3727 * helper to calculate size of global block reservation.
3728 * the desired value is sum of space used by extent tree,
3729 * checksum tree and root tree
3730 */
3731static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3732{
3733 struct btrfs_space_info *sinfo;
3734 u64 num_bytes;
3735 u64 meta_used;
3736 u64 data_used;
3737 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003738
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003739 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3740 spin_lock(&sinfo->lock);
3741 data_used = sinfo->bytes_used;
3742 spin_unlock(&sinfo->lock);
3743
3744 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3745 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04003746 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3747 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003748 meta_used = sinfo->bytes_used;
3749 spin_unlock(&sinfo->lock);
3750
3751 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3752 csum_size * 2;
3753 num_bytes += div64_u64(data_used + meta_used, 50);
3754
3755 if (num_bytes * 3 > meta_used)
3756 num_bytes = div64_u64(meta_used, 3);
3757
3758 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3759}
3760
3761static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3762{
3763 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3764 struct btrfs_space_info *sinfo = block_rsv->space_info;
3765 u64 num_bytes;
3766
3767 num_bytes = calc_global_metadata_size(fs_info);
3768
3769 spin_lock(&block_rsv->lock);
3770 spin_lock(&sinfo->lock);
3771
3772 block_rsv->size = num_bytes;
3773
3774 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04003775 sinfo->bytes_reserved + sinfo->bytes_readonly +
3776 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003777
3778 if (sinfo->total_bytes > num_bytes) {
3779 num_bytes = sinfo->total_bytes - num_bytes;
3780 block_rsv->reserved += num_bytes;
3781 sinfo->bytes_reserved += num_bytes;
3782 }
3783
3784 if (block_rsv->reserved >= block_rsv->size) {
3785 num_bytes = block_rsv->reserved - block_rsv->size;
3786 sinfo->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05003787 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003788 block_rsv->reserved = block_rsv->size;
3789 block_rsv->full = 1;
3790 }
David Sterba182608c2011-05-05 13:13:16 +02003791
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003792 spin_unlock(&sinfo->lock);
3793 spin_unlock(&block_rsv->lock);
3794}
3795
Yan, Zhengf0486c62010-05-16 10:46:25 -04003796static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3797{
3798 struct btrfs_space_info *space_info;
3799
3800 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3801 fs_info->chunk_block_rsv.space_info = space_info;
3802 fs_info->chunk_block_rsv.priority = 10;
3803
3804 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003805 fs_info->global_block_rsv.space_info = space_info;
3806 fs_info->global_block_rsv.priority = 10;
3807 fs_info->global_block_rsv.refill_used = 1;
3808 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003809 fs_info->trans_block_rsv.space_info = space_info;
3810 fs_info->empty_block_rsv.space_info = space_info;
3811 fs_info->empty_block_rsv.priority = 10;
3812
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003813 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3814 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3815 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3816 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003817 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003818
3819 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3820
3821 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3822
3823 update_global_block_rsv(fs_info);
3824}
3825
3826static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3827{
3828 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3829 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3830 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3831 WARN_ON(fs_info->trans_block_rsv.size > 0);
3832 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3833 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3834 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003835}
3836
Yan, Zhenga22285a2010-05-16 10:48:46 -04003837static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3838{
3839 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3840 3 * num_items;
3841}
3842
3843int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3844 struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003845 int num_items)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003846{
3847 u64 num_bytes;
3848 int ret;
3849
3850 if (num_items == 0 || root->fs_info->chunk_root == root)
3851 return 0;
3852
3853 num_bytes = calc_trans_metadata_size(root, num_items);
3854 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003855 num_bytes);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003856 if (!ret) {
3857 trans->bytes_reserved += num_bytes;
3858 trans->block_rsv = &root->fs_info->trans_block_rsv;
3859 }
3860 return ret;
3861}
3862
3863void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3864 struct btrfs_root *root)
3865{
3866 if (!trans->bytes_reserved)
3867 return;
3868
3869 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3870 btrfs_block_rsv_release(root, trans->block_rsv,
3871 trans->bytes_reserved);
3872 trans->bytes_reserved = 0;
3873}
3874
Yan, Zhengd68fc572010-05-16 10:49:58 -04003875int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3876 struct inode *inode)
3877{
3878 struct btrfs_root *root = BTRFS_I(inode)->root;
3879 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3880 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3881
3882 /*
3883 * one for deleting orphan item, one for updating inode and
3884 * two for calling btrfs_truncate_inode_items.
3885 *
3886 * btrfs_truncate_inode_items is a delete operation, it frees
3887 * more space than it uses in most cases. So two units of
3888 * metadata space should be enough for calling it many times.
3889 * If all of the metadata space is used, we can commit
3890 * transaction and use space it freed.
3891 */
3892 u64 num_bytes = calc_trans_metadata_size(root, 4);
3893 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3894}
3895
3896void btrfs_orphan_release_metadata(struct inode *inode)
3897{
3898 struct btrfs_root *root = BTRFS_I(inode)->root;
3899 u64 num_bytes = calc_trans_metadata_size(root, 4);
3900 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3901}
3902
Yan, Zhenga22285a2010-05-16 10:48:46 -04003903int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3904 struct btrfs_pending_snapshot *pending)
3905{
3906 struct btrfs_root *root = pending->root;
3907 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3908 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3909 /*
3910 * two for root back/forward refs, two for directory entries
3911 * and one for root of the snapshot.
3912 */
3913 u64 num_bytes = calc_trans_metadata_size(root, 5);
3914 dst_rsv->space_info = src_rsv->space_info;
3915 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3916}
3917
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003918static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3919{
3920 return num_bytes >>= 3;
3921}
3922
3923int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3924{
3925 struct btrfs_root *root = BTRFS_I(inode)->root;
3926 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3927 u64 to_reserve;
3928 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05003929 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003930 int ret;
3931
3932 if (btrfs_transaction_in_commit(root->fs_info))
3933 schedule_timeout(1);
3934
3935 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003936
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003937 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
Josef Bacik57a45ced2011-01-25 16:30:38 -05003938 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
3939
3940 if (nr_extents > reserved_extents) {
3941 nr_extents -= reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003942 to_reserve = calc_trans_metadata_size(root, nr_extents);
3943 } else {
3944 nr_extents = 0;
3945 to_reserve = 0;
3946 }
Josef Bacik57a45ced2011-01-25 16:30:38 -05003947
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003948 to_reserve += calc_csum_metadata_size(inode, num_bytes);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003949 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
3950 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003951 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003952
Josef Bacik57a45ced2011-01-25 16:30:38 -05003953 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003954 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003955
3956 block_rsv_add_bytes(block_rsv, to_reserve, 1);
3957
3958 if (block_rsv->size > 512 * 1024 * 1024)
Josef Bacik0019f102010-10-15 15:18:40 -04003959 shrink_delalloc(NULL, root, to_reserve, 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003960
3961 return 0;
3962}
3963
3964void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
3965{
3966 struct btrfs_root *root = BTRFS_I(inode)->root;
3967 u64 to_free;
3968 int nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05003969 int reserved_extents;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003970
3971 num_bytes = ALIGN(num_bytes, root->sectorsize);
3972 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik3c148742011-02-02 15:53:47 +00003973 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003974
Josef Bacik57a45ced2011-01-25 16:30:38 -05003975 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
3976 do {
3977 int old, new;
3978
3979 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
3980 if (nr_extents >= reserved_extents) {
3981 nr_extents = 0;
3982 break;
3983 }
3984 old = reserved_extents;
3985 nr_extents = reserved_extents - nr_extents;
3986 new = reserved_extents - nr_extents;
3987 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
3988 reserved_extents, new);
3989 if (likely(old == reserved_extents))
3990 break;
3991 reserved_extents = old;
3992 } while (1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003993
3994 to_free = calc_csum_metadata_size(inode, num_bytes);
3995 if (nr_extents > 0)
3996 to_free += calc_trans_metadata_size(root, nr_extents);
3997
3998 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
3999 to_free);
4000}
4001
4002int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4003{
4004 int ret;
4005
4006 ret = btrfs_check_data_free_space(inode, num_bytes);
4007 if (ret)
4008 return ret;
4009
4010 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4011 if (ret) {
4012 btrfs_free_reserved_data_space(inode, num_bytes);
4013 return ret;
4014 }
4015
4016 return 0;
4017}
4018
4019void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4020{
4021 btrfs_delalloc_release_metadata(inode, num_bytes);
4022 btrfs_free_reserved_data_space(inode, num_bytes);
4023}
4024
Chris Mason9078a3e2007-04-26 16:46:15 -04004025static int update_block_group(struct btrfs_trans_handle *trans,
4026 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004027 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004028{
Josef Bacik0af3d002010-06-21 14:48:16 -04004029 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004030 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004031 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004032 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004033 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004034 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004035
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004036 /* block accounting for super block */
4037 spin_lock(&info->delalloc_lock);
4038 old_val = btrfs_super_bytes_used(&info->super_copy);
4039 if (alloc)
4040 old_val += num_bytes;
4041 else
4042 old_val -= num_bytes;
4043 btrfs_set_super_bytes_used(&info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004044 spin_unlock(&info->delalloc_lock);
4045
Chris Masond3977122009-01-05 21:25:51 -05004046 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004047 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004048 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04004049 return -1;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004050 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4051 BTRFS_BLOCK_GROUP_RAID1 |
4052 BTRFS_BLOCK_GROUP_RAID10))
4053 factor = 2;
4054 else
4055 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004056 /*
4057 * If this block group has free space cache written out, we
4058 * need to make sure to load it if we are removing space. This
4059 * is because we need the unpinning stage to actually add the
4060 * space back to the block group, otherwise we will leak space.
4061 */
4062 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004063 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004064
Chris Masondb945352007-10-15 16:15:53 -04004065 byte_in_group = bytenr - cache->key.objectid;
4066 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004067
Josef Bacik25179202008-10-29 14:49:05 -04004068 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004069 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004070
4071 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4072 cache->disk_cache_state < BTRFS_DC_CLEAR)
4073 cache->disk_cache_state = BTRFS_DC_CLEAR;
4074
Josef Bacik0f9dd462008-09-23 13:14:11 -04004075 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004076 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004077 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004078 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004079 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004080 btrfs_set_block_group_used(&cache->item, old_val);
4081 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004082 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004083 cache->space_info->reservation_progress++;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004084 cache->space_info->bytes_used += num_bytes;
4085 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004086 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004087 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004088 } else {
Chris Masondb945352007-10-15 16:15:53 -04004089 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004090 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004091 cache->pinned += num_bytes;
4092 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004093 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004094 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004095 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004096 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004097
Yan, Zhengf0486c62010-05-16 10:46:25 -04004098 set_extent_dirty(info->pinned_extents,
4099 bytenr, bytenr + num_bytes - 1,
4100 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004101 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004102 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004103 total -= num_bytes;
4104 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004105 }
4106 return 0;
4107}
Chris Mason6324fbf2008-03-24 15:01:59 -04004108
Chris Masona061fc82008-05-07 11:43:44 -04004109static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4110{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004111 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004112 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004113
4114 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4115 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004116 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004117
Yan Zhengd2fb3432008-12-11 16:30:39 -05004118 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004119 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004120
4121 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004122}
4123
Yan, Zhengf0486c62010-05-16 10:46:25 -04004124static int pin_down_extent(struct btrfs_root *root,
4125 struct btrfs_block_group_cache *cache,
4126 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004127{
Yan Zheng11833d62009-09-11 16:11:19 -04004128 spin_lock(&cache->space_info->lock);
4129 spin_lock(&cache->lock);
4130 cache->pinned += num_bytes;
4131 cache->space_info->bytes_pinned += num_bytes;
4132 if (reserved) {
4133 cache->reserved -= num_bytes;
4134 cache->space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004135 cache->space_info->reservation_progress++;
Yan324ae4d2007-11-16 14:57:08 -05004136 }
Yan Zheng11833d62009-09-11 16:11:19 -04004137 spin_unlock(&cache->lock);
4138 spin_unlock(&cache->space_info->lock);
4139
Yan, Zhengf0486c62010-05-16 10:46:25 -04004140 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4141 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004142 return 0;
4143}
Chris Mason9078a3e2007-04-26 16:46:15 -04004144
Yan, Zhengf0486c62010-05-16 10:46:25 -04004145/*
4146 * this function must be called within transaction
4147 */
4148int btrfs_pin_extent(struct btrfs_root *root,
4149 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004150{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004151 struct btrfs_block_group_cache *cache;
4152
4153 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4154 BUG_ON(!cache);
4155
4156 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4157
4158 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004159 return 0;
4160}
Zheng Yane8569812008-09-26 10:05:48 -04004161
Yan, Zhengf0486c62010-05-16 10:46:25 -04004162/*
4163 * update size of reserved extents. this function may return -EAGAIN
4164 * if 'reserve' is true or 'sinfo' is false.
4165 */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004166int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4167 u64 num_bytes, int reserve, int sinfo)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004168{
4169 int ret = 0;
4170 if (sinfo) {
4171 struct btrfs_space_info *space_info = cache->space_info;
4172 spin_lock(&space_info->lock);
4173 spin_lock(&cache->lock);
4174 if (reserve) {
4175 if (cache->ro) {
4176 ret = -EAGAIN;
4177 } else {
4178 cache->reserved += num_bytes;
4179 space_info->bytes_reserved += num_bytes;
4180 }
4181 } else {
4182 if (cache->ro)
4183 space_info->bytes_readonly += num_bytes;
4184 cache->reserved -= num_bytes;
4185 space_info->bytes_reserved -= num_bytes;
Chris Mason36e39c42011-03-12 07:08:42 -05004186 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004187 }
4188 spin_unlock(&cache->lock);
4189 spin_unlock(&space_info->lock);
4190 } else {
4191 spin_lock(&cache->lock);
4192 if (cache->ro) {
4193 ret = -EAGAIN;
4194 } else {
4195 if (reserve)
4196 cache->reserved += num_bytes;
4197 else
4198 cache->reserved -= num_bytes;
4199 }
4200 spin_unlock(&cache->lock);
4201 }
4202 return ret;
4203}
4204
Yan Zheng11833d62009-09-11 16:11:19 -04004205int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4206 struct btrfs_root *root)
4207{
4208 struct btrfs_fs_info *fs_info = root->fs_info;
4209 struct btrfs_caching_control *next;
4210 struct btrfs_caching_control *caching_ctl;
4211 struct btrfs_block_group_cache *cache;
4212
4213 down_write(&fs_info->extent_commit_sem);
4214
4215 list_for_each_entry_safe(caching_ctl, next,
4216 &fs_info->caching_block_groups, list) {
4217 cache = caching_ctl->block_group;
4218 if (block_group_cache_done(cache)) {
4219 cache->last_byte_to_unpin = (u64)-1;
4220 list_del_init(&caching_ctl->list);
4221 put_caching_control(caching_ctl);
4222 } else {
4223 cache->last_byte_to_unpin = caching_ctl->progress;
4224 }
4225 }
4226
4227 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4228 fs_info->pinned_extents = &fs_info->freed_extents[1];
4229 else
4230 fs_info->pinned_extents = &fs_info->freed_extents[0];
4231
4232 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004233
4234 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004235 return 0;
4236}
4237
4238static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4239{
4240 struct btrfs_fs_info *fs_info = root->fs_info;
4241 struct btrfs_block_group_cache *cache = NULL;
4242 u64 len;
4243
4244 while (start <= end) {
4245 if (!cache ||
4246 start >= cache->key.objectid + cache->key.offset) {
4247 if (cache)
4248 btrfs_put_block_group(cache);
4249 cache = btrfs_lookup_block_group(fs_info, start);
4250 BUG_ON(!cache);
4251 }
4252
4253 len = cache->key.objectid + cache->key.offset - start;
4254 len = min(len, end + 1 - start);
4255
4256 if (start < cache->last_byte_to_unpin) {
4257 len = min(len, cache->last_byte_to_unpin - start);
4258 btrfs_add_free_space(cache, start, len);
4259 }
Josef Bacik25179202008-10-29 14:49:05 -04004260
Yan, Zhengf0486c62010-05-16 10:46:25 -04004261 start += len;
4262
Josef Bacik25179202008-10-29 14:49:05 -04004263 spin_lock(&cache->space_info->lock);
4264 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004265 cache->pinned -= len;
4266 cache->space_info->bytes_pinned -= len;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004267 if (cache->ro) {
4268 cache->space_info->bytes_readonly += len;
4269 } else if (cache->reserved_pinned > 0) {
4270 len = min(len, cache->reserved_pinned);
4271 cache->reserved_pinned -= len;
4272 cache->space_info->bytes_reserved += len;
4273 }
Josef Bacik25179202008-10-29 14:49:05 -04004274 spin_unlock(&cache->lock);
4275 spin_unlock(&cache->space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004276 }
4277
4278 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004279 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004280 return 0;
4281}
4282
4283int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004284 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05004285{
Yan Zheng11833d62009-09-11 16:11:19 -04004286 struct btrfs_fs_info *fs_info = root->fs_info;
4287 struct extent_io_tree *unpin;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004288 struct btrfs_block_rsv *block_rsv;
4289 struct btrfs_block_rsv *next_rsv;
Chris Mason1a5bc162007-10-15 16:15:26 -04004290 u64 start;
4291 u64 end;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004292 int idx;
Chris Masona28ec192007-03-06 20:08:01 -05004293 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004294
Yan Zheng11833d62009-09-11 16:11:19 -04004295 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4296 unpin = &fs_info->freed_extents[1];
4297 else
4298 unpin = &fs_info->freed_extents[0];
4299
Chris Masond3977122009-01-05 21:25:51 -05004300 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04004301 ret = find_first_extent_bit(unpin, 0, &start, &end,
4302 EXTENT_DIRTY);
4303 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05004304 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004305
Li Dongyang5378e602011-03-24 10:24:27 +00004306 if (btrfs_test_opt(root, DISCARD))
4307 ret = btrfs_discard_extent(root, start,
4308 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004309
Chris Mason1a5bc162007-10-15 16:15:26 -04004310 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04004311 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04004312 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004313 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004314
Yan, Zhengf0486c62010-05-16 10:46:25 -04004315 mutex_lock(&fs_info->durable_block_rsv_mutex);
4316 list_for_each_entry_safe(block_rsv, next_rsv,
4317 &fs_info->durable_block_rsv_list, list) {
Chris Masona28ec192007-03-06 20:08:01 -05004318
Yan, Zhengf0486c62010-05-16 10:46:25 -04004319 idx = trans->transid & 0x1;
4320 if (block_rsv->freed[idx] > 0) {
4321 block_rsv_add_bytes(block_rsv,
4322 block_rsv->freed[idx], 0);
4323 block_rsv->freed[idx] = 0;
Chris Mason8ef97622007-03-26 10:15:30 -04004324 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004325 if (atomic_read(&block_rsv->usage) == 0) {
4326 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
Zheng Yan31840ae2008-09-23 13:14:14 -04004327
Yan, Zhengf0486c62010-05-16 10:46:25 -04004328 if (block_rsv->freed[0] == 0 &&
4329 block_rsv->freed[1] == 0) {
4330 list_del_init(&block_rsv->list);
4331 kfree(block_rsv);
4332 }
4333 } else {
4334 btrfs_block_rsv_release(root, block_rsv, 0);
4335 }
4336 }
4337 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4338
Chris Masone20d96d2007-03-22 12:13:20 -04004339 return 0;
4340}
4341
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004342static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4343 struct btrfs_root *root,
4344 u64 bytenr, u64 num_bytes, u64 parent,
4345 u64 root_objectid, u64 owner_objectid,
4346 u64 owner_offset, int refs_to_drop,
4347 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05004348{
Chris Masone2fa7222007-03-12 16:22:34 -04004349 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004350 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004351 struct btrfs_fs_info *info = root->fs_info;
4352 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004353 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004354 struct btrfs_extent_item *ei;
4355 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004356 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004357 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004358 int extent_slot = 0;
4359 int found_extent = 0;
4360 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004361 u32 item_size;
4362 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05004363
Chris Mason5caf2a02007-04-02 11:20:42 -04004364 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004365 if (!path)
4366 return -ENOMEM;
4367
Chris Mason3c12ac72008-04-21 12:01:38 -04004368 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04004369 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004370
4371 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4372 BUG_ON(!is_data && refs_to_drop != 1);
4373
4374 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4375 bytenr, num_bytes, parent,
4376 root_objectid, owner_objectid,
4377 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004378 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004379 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004380 while (extent_slot >= 0) {
4381 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004382 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004383 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004384 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004385 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4386 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004387 found_extent = 1;
4388 break;
4389 }
4390 if (path->slots[0] - extent_slot > 5)
4391 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004392 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004393 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004394#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4395 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4396 if (found_extent && item_size < sizeof(*ei))
4397 found_extent = 0;
4398#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04004399 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004400 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04004401 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004402 NULL, refs_to_drop,
4403 is_data);
Zheng Yan31840ae2008-09-23 13:14:14 -04004404 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004405 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04004406 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004407
4408 key.objectid = bytenr;
4409 key.type = BTRFS_EXTENT_ITEM_KEY;
4410 key.offset = num_bytes;
4411
Zheng Yan31840ae2008-09-23 13:14:14 -04004412 ret = btrfs_search_slot(trans, extent_root,
4413 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004414 if (ret) {
4415 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05004416 ", was looking for %llu\n", ret,
4417 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004418 btrfs_print_leaf(extent_root, path->nodes[0]);
4419 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004420 BUG_ON(ret);
4421 extent_slot = path->slots[0];
4422 }
Chris Mason7bb86312007-12-11 09:25:06 -05004423 } else {
4424 btrfs_print_leaf(extent_root, path->nodes[0]);
4425 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05004426 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004427 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004428 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04004429 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05004430 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004431 (unsigned long long)owner_objectid,
4432 (unsigned long long)owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004433 }
Chris Mason5f39d392007-10-15 16:14:19 -04004434
4435 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004436 item_size = btrfs_item_size_nr(leaf, extent_slot);
4437#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4438 if (item_size < sizeof(*ei)) {
4439 BUG_ON(found_extent || extent_slot != path->slots[0]);
4440 ret = convert_extent_item_v0(trans, extent_root, path,
4441 owner_objectid, 0);
4442 BUG_ON(ret < 0);
4443
David Sterbab3b4aa72011-04-21 01:20:15 +02004444 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004445 path->leave_spinning = 1;
4446
4447 key.objectid = bytenr;
4448 key.type = BTRFS_EXTENT_ITEM_KEY;
4449 key.offset = num_bytes;
4450
4451 ret = btrfs_search_slot(trans, extent_root, &key, path,
4452 -1, 1);
4453 if (ret) {
4454 printk(KERN_ERR "umm, got %d back from search"
4455 ", was looking for %llu\n", ret,
4456 (unsigned long long)bytenr);
4457 btrfs_print_leaf(extent_root, path->nodes[0]);
4458 }
4459 BUG_ON(ret);
4460 extent_slot = path->slots[0];
4461 leaf = path->nodes[0];
4462 item_size = btrfs_item_size_nr(leaf, extent_slot);
4463 }
4464#endif
4465 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05004466 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004467 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004468 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4469 struct btrfs_tree_block_info *bi;
4470 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4471 bi = (struct btrfs_tree_block_info *)(ei + 1);
4472 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05004473 }
4474
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004475 refs = btrfs_extent_refs(leaf, ei);
4476 BUG_ON(refs < refs_to_drop);
4477 refs -= refs_to_drop;
4478
4479 if (refs > 0) {
4480 if (extent_op)
4481 __run_delayed_extent_op(extent_op, leaf, ei);
4482 /*
4483 * In the case of inline back ref, reference count will
4484 * be updated by remove_extent_backref
4485 */
4486 if (iref) {
4487 BUG_ON(!found_extent);
4488 } else {
4489 btrfs_set_extent_refs(leaf, ei, refs);
4490 btrfs_mark_buffer_dirty(leaf);
4491 }
4492 if (found_extent) {
4493 ret = remove_extent_backref(trans, extent_root, path,
4494 iref, refs_to_drop,
4495 is_data);
4496 BUG_ON(ret);
4497 }
4498 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004499 if (found_extent) {
4500 BUG_ON(is_data && refs_to_drop !=
4501 extent_data_ref_count(root, path, iref));
4502 if (iref) {
4503 BUG_ON(path->slots[0] != extent_slot);
4504 } else {
4505 BUG_ON(path->slots[0] != extent_slot + 1);
4506 path->slots[0] = extent_slot;
4507 num_to_del = 2;
4508 }
Chris Mason78fae272007-03-25 11:35:08 -04004509 }
Chris Masonb9473432009-03-13 11:00:37 -04004510
Chris Mason952fcca2008-02-18 16:33:44 -05004511 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4512 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04004513 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02004514 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01004515
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004516 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05004517 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4518 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04004519 } else {
4520 invalidate_mapping_pages(info->btree_inode->i_mapping,
4521 bytenr >> PAGE_CACHE_SHIFT,
4522 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05004523 }
4524
Yan, Zhengf0486c62010-05-16 10:46:25 -04004525 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
Chris Masondcbdd4d2008-12-16 13:51:01 -05004526 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05004527 }
Chris Mason5caf2a02007-04-02 11:20:42 -04004528 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004529 return ret;
4530}
4531
4532/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004533 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004534 * delayed ref for that extent as well. This searches the delayed ref tree for
4535 * a given extent, and if there are no other delayed refs to be processed, it
4536 * removes it from the tree.
4537 */
4538static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4539 struct btrfs_root *root, u64 bytenr)
4540{
4541 struct btrfs_delayed_ref_head *head;
4542 struct btrfs_delayed_ref_root *delayed_refs;
4543 struct btrfs_delayed_ref_node *ref;
4544 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004545 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004546
4547 delayed_refs = &trans->transaction->delayed_refs;
4548 spin_lock(&delayed_refs->lock);
4549 head = btrfs_find_delayed_ref_head(trans, bytenr);
4550 if (!head)
4551 goto out;
4552
4553 node = rb_prev(&head->node.rb_node);
4554 if (!node)
4555 goto out;
4556
4557 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4558
4559 /* there are still entries for this ref, we can't drop it */
4560 if (ref->bytenr == bytenr)
4561 goto out;
4562
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004563 if (head->extent_op) {
4564 if (!head->must_insert_reserved)
4565 goto out;
4566 kfree(head->extent_op);
4567 head->extent_op = NULL;
4568 }
4569
Chris Mason1887be62009-03-13 10:11:24 -04004570 /*
4571 * waiting for the lock here would deadlock. If someone else has it
4572 * locked they are already in the process of dropping it anyway
4573 */
4574 if (!mutex_trylock(&head->mutex))
4575 goto out;
4576
4577 /*
4578 * at this point we have a head with no other entries. Go
4579 * ahead and process it.
4580 */
4581 head->node.in_tree = 0;
4582 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04004583
Chris Mason1887be62009-03-13 10:11:24 -04004584 delayed_refs->num_entries--;
4585
4586 /*
4587 * we don't take a ref on the node because we're removing it from the
4588 * tree, so we just steal the ref the tree was holding.
4589 */
Chris Masonc3e69d52009-03-13 10:17:05 -04004590 delayed_refs->num_heads--;
4591 if (list_empty(&head->cluster))
4592 delayed_refs->num_heads_ready--;
4593
4594 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04004595 spin_unlock(&delayed_refs->lock);
4596
Yan, Zhengf0486c62010-05-16 10:46:25 -04004597 BUG_ON(head->extent_op);
4598 if (head->must_insert_reserved)
4599 ret = 1;
4600
4601 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04004602 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004603 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004604out:
4605 spin_unlock(&delayed_refs->lock);
4606 return 0;
4607}
4608
Yan, Zhengf0486c62010-05-16 10:46:25 -04004609void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4610 struct btrfs_root *root,
4611 struct extent_buffer *buf,
4612 u64 parent, int last_ref)
4613{
4614 struct btrfs_block_rsv *block_rsv;
4615 struct btrfs_block_group_cache *cache = NULL;
4616 int ret;
4617
4618 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4619 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4620 parent, root->root_key.objectid,
4621 btrfs_header_level(buf),
4622 BTRFS_DROP_DELAYED_REF, NULL);
4623 BUG_ON(ret);
4624 }
4625
4626 if (!last_ref)
4627 return;
4628
4629 block_rsv = get_block_rsv(trans, root);
4630 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zheng3bf84a52010-05-31 09:04:46 +00004631 if (block_rsv->space_info != cache->space_info)
4632 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004633
4634 if (btrfs_header_generation(buf) == trans->transid) {
4635 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4636 ret = check_ref_cleanup(trans, root, buf->start);
4637 if (!ret)
4638 goto pin;
4639 }
4640
4641 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4642 pin_down_extent(root, cache, buf->start, buf->len, 1);
4643 goto pin;
4644 }
4645
4646 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4647
4648 btrfs_add_free_space(cache, buf->start, buf->len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00004649 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004650 if (ret == -EAGAIN) {
4651 /* block group became read-only */
Li Dongyangb4d00d52011-03-24 10:24:25 +00004652 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004653 goto out;
4654 }
4655
4656 ret = 1;
4657 spin_lock(&block_rsv->lock);
4658 if (block_rsv->reserved < block_rsv->size) {
4659 block_rsv->reserved += buf->len;
4660 ret = 0;
4661 }
4662 spin_unlock(&block_rsv->lock);
4663
4664 if (ret) {
4665 spin_lock(&cache->space_info->lock);
4666 cache->space_info->bytes_reserved -= buf->len;
Chris Mason36e39c42011-03-12 07:08:42 -05004667 cache->space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004668 spin_unlock(&cache->space_info->lock);
4669 }
4670 goto out;
4671 }
4672pin:
4673 if (block_rsv->durable && !cache->ro) {
4674 ret = 0;
4675 spin_lock(&cache->lock);
4676 if (!cache->ro) {
4677 cache->reserved_pinned += buf->len;
4678 ret = 1;
4679 }
4680 spin_unlock(&cache->lock);
4681
4682 if (ret) {
4683 spin_lock(&block_rsv->lock);
4684 block_rsv->freed[trans->transid & 0x1] += buf->len;
4685 spin_unlock(&block_rsv->lock);
4686 }
4687 }
4688out:
Josef Bacika826d6d2011-03-16 13:42:43 -04004689 /*
4690 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4691 * anymore.
4692 */
4693 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004694 btrfs_put_block_group(cache);
4695}
4696
Chris Mason925baed2008-06-25 16:01:30 -04004697int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04004698 struct btrfs_root *root,
4699 u64 bytenr, u64 num_bytes, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004700 u64 root_objectid, u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04004701{
4702 int ret;
4703
Chris Mason56bec292009-03-13 10:10:06 -04004704 /*
4705 * tree log blocks never actually go into the extent allocation
4706 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004707 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004708 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4709 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04004710 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04004711 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04004712 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004713 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4714 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4715 parent, root_objectid, (int)owner,
4716 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason1887be62009-03-13 10:11:24 -04004717 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004718 } else {
4719 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4720 parent, root_objectid, owner,
4721 offset, BTRFS_DROP_DELAYED_REF, NULL);
4722 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004723 }
Chris Mason925baed2008-06-25 16:01:30 -04004724 return ret;
4725}
4726
Chris Mason87ee04e2007-11-30 11:30:34 -05004727static u64 stripe_align(struct btrfs_root *root, u64 val)
4728{
4729 u64 mask = ((u64)root->stripesize - 1);
4730 u64 ret = (val + mask) & ~mask;
4731 return ret;
4732}
4733
Chris Masonfec577f2007-02-26 10:40:21 -05004734/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004735 * when we wait for progress in the block group caching, its because
4736 * our allocation attempt failed at least once. So, we must sleep
4737 * and let some progress happen before we try again.
4738 *
4739 * This function will sleep at least once waiting for new free space to
4740 * show up, and then it will check the block group free space numbers
4741 * for our min num_bytes. Another option is to have it go ahead
4742 * and look in the rbtree for a free extent of a given size, but this
4743 * is a good start.
4744 */
4745static noinline int
4746wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4747 u64 num_bytes)
4748{
Yan Zheng11833d62009-09-11 16:11:19 -04004749 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004750 DEFINE_WAIT(wait);
4751
Yan Zheng11833d62009-09-11 16:11:19 -04004752 caching_ctl = get_caching_control(cache);
4753 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04004754 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004755
Yan Zheng11833d62009-09-11 16:11:19 -04004756 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Josef Bacik817d52f2009-07-13 21:29:25 -04004757 (cache->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004758
4759 put_caching_control(caching_ctl);
4760 return 0;
4761}
4762
4763static noinline int
4764wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4765{
4766 struct btrfs_caching_control *caching_ctl;
4767 DEFINE_WAIT(wait);
4768
4769 caching_ctl = get_caching_control(cache);
4770 if (!caching_ctl)
4771 return 0;
4772
4773 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4774
4775 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04004776 return 0;
4777}
4778
Yan, Zhengb742bb82010-05-16 10:46:24 -04004779static int get_block_group_index(struct btrfs_block_group_cache *cache)
4780{
4781 int index;
4782 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4783 index = 0;
4784 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4785 index = 1;
4786 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4787 index = 2;
4788 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4789 index = 3;
4790 else
4791 index = 4;
4792 return index;
4793}
4794
Josef Bacik817d52f2009-07-13 21:29:25 -04004795enum btrfs_loop_type {
Josef Bacikccf0e722009-11-10 21:23:48 -05004796 LOOP_FIND_IDEAL = 0,
Josef Bacik817d52f2009-07-13 21:29:25 -04004797 LOOP_CACHING_NOWAIT = 1,
4798 LOOP_CACHING_WAIT = 2,
4799 LOOP_ALLOC_CHUNK = 3,
4800 LOOP_NO_EMPTY_SIZE = 4,
4801};
4802
4803/*
Chris Masonfec577f2007-02-26 10:40:21 -05004804 * walks the btree of allocated extents and find a hole of a given size.
4805 * The key ins is changed to record the hole:
4806 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04004807 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05004808 * ins->offset == number of blocks
4809 * Any available blocks before search_start are skipped.
4810 */
Chris Masond3977122009-01-05 21:25:51 -05004811static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004812 struct btrfs_root *orig_root,
4813 u64 num_bytes, u64 empty_size,
4814 u64 search_start, u64 search_end,
4815 u64 hint_byte, struct btrfs_key *ins,
Chris Mason98ed5172008-01-03 10:01:48 -05004816 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05004817{
Josef Bacik80eb2342008-10-29 14:49:05 -04004818 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05004819 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004820 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04004821 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04004822 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04004823 int allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05004824 int done_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04004825 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004826 int last_ptr_loop = 0;
4827 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004828 int index = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04004829 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04004830 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004831 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04004832 bool use_cluster = true;
Josef Bacikccf0e722009-11-10 21:23:48 -05004833 u64 ideal_cache_percent = 0;
4834 u64 ideal_cache_offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05004835
Chris Masondb945352007-10-15 16:15:53 -04004836 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04004837 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04004838 ins->objectid = 0;
4839 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04004840
Josef Bacik2552d172009-04-03 10:14:19 -04004841 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00004842 if (!space_info) {
4843 printk(KERN_ERR "No space info for %d\n", data);
4844 return -ENOSPC;
4845 }
Josef Bacik2552d172009-04-03 10:14:19 -04004846
Josef Bacik67377732010-09-16 16:19:09 -04004847 /*
4848 * If the space info is for both data and metadata it means we have a
4849 * small filesystem and we can't use the clustering stuff.
4850 */
4851 if (btrfs_mixed_space_info(space_info))
4852 use_cluster = false;
4853
Chris Mason0ef3e662008-05-24 14:04:53 -04004854 if (orig_root->ref_cows || empty_size)
4855 allowed_chunk_alloc = 1;
4856
Josef Bacik67377732010-09-16 16:19:09 -04004857 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004858 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05004859 if (!btrfs_test_opt(root, SSD))
4860 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04004861 }
4862
Josef Bacik67377732010-09-16 16:19:09 -04004863 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4864 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004865 last_ptr = &root->fs_info->data_alloc_cluster;
4866 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004867
Chris Mason239b14b2008-03-24 15:02:07 -04004868 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004869 spin_lock(&last_ptr->lock);
4870 if (last_ptr->block_group)
4871 hint_byte = last_ptr->window_start;
4872 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04004873 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004874
Chris Masona061fc82008-05-07 11:43:44 -04004875 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04004876 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04004877
Josef Bacik817d52f2009-07-13 21:29:25 -04004878 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004879 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004880
Josef Bacik2552d172009-04-03 10:14:19 -04004881 if (search_start == hint_byte) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004882ideal_cache:
Josef Bacik2552d172009-04-03 10:14:19 -04004883 block_group = btrfs_lookup_block_group(root->fs_info,
4884 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04004885 /*
4886 * we don't want to use the block group if it doesn't match our
4887 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05004888 *
4889 * However if we are re-searching with an ideal block group
4890 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04004891 */
4892 if (block_group && block_group_bits(block_group, data) &&
Josef Bacikccf0e722009-11-10 21:23:48 -05004893 (block_group->cached != BTRFS_CACHE_NO ||
4894 search_start == ideal_cache_offset)) {
Josef Bacik2552d172009-04-03 10:14:19 -04004895 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04004896 if (list_empty(&block_group->list) ||
4897 block_group->ro) {
4898 /*
4899 * someone is removing this block group,
4900 * we can't jump into the have_block_group
4901 * target because our list pointers are not
4902 * valid
4903 */
4904 btrfs_put_block_group(block_group);
4905 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05004906 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04004907 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04004908 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05004909 }
Josef Bacik2552d172009-04-03 10:14:19 -04004910 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04004911 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004912 }
Chris Mason42e70e72008-11-07 18:17:11 -05004913 }
Josef Bacik2552d172009-04-03 10:14:19 -04004914search:
Josef Bacik80eb2342008-10-29 14:49:05 -04004915 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004916 list_for_each_entry(block_group, &space_info->block_groups[index],
4917 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04004918 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04004919 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05004920
Josef Bacik11dfe352009-11-13 20:12:59 +00004921 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04004922 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05004923
Chris Mason83a50de2010-12-13 15:06:46 -05004924 /*
4925 * this can happen if we end up cycling through all the
4926 * raid types, but we want to make sure we only allocate
4927 * for the proper type.
4928 */
4929 if (!block_group_bits(block_group, data)) {
4930 u64 extra = BTRFS_BLOCK_GROUP_DUP |
4931 BTRFS_BLOCK_GROUP_RAID1 |
4932 BTRFS_BLOCK_GROUP_RAID10;
4933
4934 /*
4935 * if they asked for extra copies and this block group
4936 * doesn't provide them, bail. This does allow us to
4937 * fill raid0 from raid1.
4938 */
4939 if ((data & extra) && !(block_group->flags & extra))
4940 goto loop;
4941 }
4942
Josef Bacik2552d172009-04-03 10:14:19 -04004943have_block_group:
Josef Bacik817d52f2009-07-13 21:29:25 -04004944 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
Josef Bacikccf0e722009-11-10 21:23:48 -05004945 u64 free_percent;
4946
Josef Bacikb8399de2010-12-08 09:15:11 -05004947 ret = cache_block_group(block_group, trans,
4948 orig_root, 1);
Josef Bacik9d66e232010-08-25 16:54:15 -04004949 if (block_group->cached == BTRFS_CACHE_FINISHED)
4950 goto have_block_group;
4951
Josef Bacikccf0e722009-11-10 21:23:48 -05004952 free_percent = btrfs_block_group_used(&block_group->item);
4953 free_percent *= 100;
4954 free_percent = div64_u64(free_percent,
4955 block_group->key.offset);
4956 free_percent = 100 - free_percent;
4957 if (free_percent > ideal_cache_percent &&
4958 likely(!block_group->ro)) {
4959 ideal_cache_offset = block_group->key.objectid;
4960 ideal_cache_percent = free_percent;
4961 }
4962
Josef Bacik817d52f2009-07-13 21:29:25 -04004963 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05004964 * We only want to start kthread caching if we are at
4965 * the point where we will wait for caching to make
4966 * progress, or if our ideal search is over and we've
4967 * found somebody to start caching.
Josef Bacik817d52f2009-07-13 21:29:25 -04004968 */
4969 if (loop > LOOP_CACHING_NOWAIT ||
Josef Bacikccf0e722009-11-10 21:23:48 -05004970 (loop > LOOP_FIND_IDEAL &&
4971 atomic_read(&space_info->caching_threads) < 2)) {
Josef Bacikb8399de2010-12-08 09:15:11 -05004972 ret = cache_block_group(block_group, trans,
4973 orig_root, 0);
Josef Bacik817d52f2009-07-13 21:29:25 -04004974 BUG_ON(ret);
Josef Bacik2552d172009-04-03 10:14:19 -04004975 }
Josef Bacikccf0e722009-11-10 21:23:48 -05004976 found_uncached_bg = true;
4977
4978 /*
4979 * If loop is set for cached only, try the next block
4980 * group.
4981 */
4982 if (loop == LOOP_FIND_IDEAL)
4983 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05004984 }
4985
Josef Bacik817d52f2009-07-13 21:29:25 -04004986 cached = block_group_cache_done(block_group);
Josef Bacikccf0e722009-11-10 21:23:48 -05004987 if (unlikely(!cached))
Josef Bacik817d52f2009-07-13 21:29:25 -04004988 found_uncached_bg = true;
4989
Josef Bacikea6a4782008-11-20 12:16:16 -05004990 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04004991 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004992
Josef Bacik0a243252009-09-11 16:11:20 -04004993 /*
4994 * Ok we want to try and use the cluster allocator, so lets look
4995 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4996 * have tried the cluster allocator plenty of times at this
4997 * point and not have found anything, so we are likely way too
4998 * fragmented for the clustering stuff to find anything, so lets
4999 * just skip it and let the allocator find whatever block it can
5000 * find
5001 */
5002 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005003 /*
5004 * the refill lock keeps out other
5005 * people trying to start a new cluster
5006 */
5007 spin_lock(&last_ptr->refill_lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005008 if (last_ptr->block_group &&
5009 (last_ptr->block_group->ro ||
5010 !block_group_bits(last_ptr->block_group, data))) {
5011 offset = 0;
5012 goto refill_cluster;
5013 }
5014
Chris Masonfa9c0d792009-04-03 09:47:43 -04005015 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5016 num_bytes, search_start);
5017 if (offset) {
5018 /* we have a block, we're done */
5019 spin_unlock(&last_ptr->refill_lock);
5020 goto checks;
5021 }
5022
5023 spin_lock(&last_ptr->lock);
5024 /*
5025 * whoops, this cluster doesn't actually point to
5026 * this block group. Get a ref on the block
5027 * group is does point to and try again
5028 */
5029 if (!last_ptr_loop && last_ptr->block_group &&
5030 last_ptr->block_group != block_group) {
5031
5032 btrfs_put_block_group(block_group);
5033 block_group = last_ptr->block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005034 btrfs_get_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005035 spin_unlock(&last_ptr->lock);
5036 spin_unlock(&last_ptr->refill_lock);
5037
5038 last_ptr_loop = 1;
5039 search_start = block_group->key.objectid;
Chris Mason44fb5512009-06-04 15:34:51 -04005040 /*
5041 * we know this block group is properly
5042 * in the list because
5043 * btrfs_remove_block_group, drops the
5044 * cluster before it removes the block
5045 * group from the list
5046 */
Chris Masonfa9c0d792009-04-03 09:47:43 -04005047 goto have_block_group;
5048 }
5049 spin_unlock(&last_ptr->lock);
Chris Mason44fb5512009-06-04 15:34:51 -04005050refill_cluster:
Chris Masonfa9c0d792009-04-03 09:47:43 -04005051 /*
5052 * this cluster didn't work out, free it and
5053 * start over
5054 */
5055 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5056
5057 last_ptr_loop = 0;
5058
5059 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005060 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005061 block_group, last_ptr,
5062 offset, num_bytes,
5063 empty_cluster + empty_size);
5064 if (ret == 0) {
5065 /*
5066 * now pull our allocation out of this
5067 * cluster
5068 */
5069 offset = btrfs_alloc_from_cluster(block_group,
5070 last_ptr, num_bytes,
5071 search_start);
5072 if (offset) {
5073 /* we found one, proceed */
5074 spin_unlock(&last_ptr->refill_lock);
5075 goto checks;
5076 }
Josef Bacik0a243252009-09-11 16:11:20 -04005077 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5078 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005079 spin_unlock(&last_ptr->refill_lock);
5080
Josef Bacik0a243252009-09-11 16:11:20 -04005081 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005082 wait_block_group_cache_progress(block_group,
5083 num_bytes + empty_cluster + empty_size);
5084 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005085 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005086
Chris Masonfa9c0d792009-04-03 09:47:43 -04005087 /*
5088 * at this point we either didn't find a cluster
5089 * or we weren't able to allocate a block from our
5090 * cluster. Free the cluster we've been trying
5091 * to use, and go to the next block group
5092 */
Josef Bacik0a243252009-09-11 16:11:20 -04005093 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005094 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005095 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005096 }
5097
Josef Bacik6226cb02009-04-03 10:14:18 -04005098 offset = btrfs_find_space_for_alloc(block_group, search_start,
5099 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005100 /*
5101 * If we didn't find a chunk, and we haven't failed on this
5102 * block group before, and this block group is in the middle of
5103 * caching and we are ok with waiting, then go ahead and wait
5104 * for progress to be made, and set failed_alloc to true.
5105 *
5106 * If failed_alloc is true then we've already waited on this
5107 * block group once and should move on to the next block group.
5108 */
5109 if (!offset && !failed_alloc && !cached &&
5110 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005111 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005112 num_bytes + empty_size);
5113 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005114 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005115 } else if (!offset) {
5116 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005117 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005118checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005119 search_start = stripe_align(root, offset);
Josef Bacik2552d172009-04-03 10:14:19 -04005120 /* move on to the next group */
Josef Bacik6226cb02009-04-03 10:14:18 -04005121 if (search_start + num_bytes >= search_end) {
5122 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005123 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005124 }
Chris Masone37c9e62007-05-09 20:13:14 -04005125
Josef Bacik2552d172009-04-03 10:14:19 -04005126 /* move on to the next group */
5127 if (search_start + num_bytes >
Josef Bacik6226cb02009-04-03 10:14:18 -04005128 block_group->key.objectid + block_group->key.offset) {
5129 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005130 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005131 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005132
Josef Bacik2552d172009-04-03 10:14:19 -04005133 ins->objectid = search_start;
5134 ins->offset = num_bytes;
5135
Josef Bacik6226cb02009-04-03 10:14:18 -04005136 if (offset < search_start)
5137 btrfs_add_free_space(block_group, offset,
5138 search_start - offset);
5139 BUG_ON(offset > search_start);
5140
Li Dongyangb4d00d52011-03-24 10:24:25 +00005141 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005142 (data & BTRFS_BLOCK_GROUP_DATA));
5143 if (ret == -EAGAIN) {
5144 btrfs_add_free_space(block_group, offset, num_bytes);
5145 goto loop;
5146 }
Yan Zheng11833d62009-09-11 16:11:19 -04005147
Josef Bacik2552d172009-04-03 10:14:19 -04005148 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005149 ins->objectid = search_start;
5150 ins->offset = num_bytes;
5151
5152 if (offset < search_start)
5153 btrfs_add_free_space(block_group, offset,
5154 search_start - offset);
5155 BUG_ON(offset > search_start);
Josef Bacik2552d172009-04-03 10:14:19 -04005156 break;
5157loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005158 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005159 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005160 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d792009-04-03 09:47:43 -04005161 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005162 }
5163 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005164
Yan, Zhengb742bb82010-05-16 10:46:24 -04005165 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5166 goto search;
5167
Josef Bacikccf0e722009-11-10 21:23:48 -05005168 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5169 * for them to make caching progress. Also
5170 * determine the best possible bg to cache
5171 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5172 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005173 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5174 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5175 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5176 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04005177 */
Josef Bacik817d52f2009-07-13 21:29:25 -04005178 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5179 (found_uncached_bg || empty_size || empty_cluster ||
5180 allowed_chunk_alloc)) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005181 index = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005182 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005183 found_uncached_bg = false;
Josef Bacikccf0e722009-11-10 21:23:48 -05005184 loop++;
5185 if (!ideal_cache_percent &&
5186 atomic_read(&space_info->caching_threads))
Josef Bacik817d52f2009-07-13 21:29:25 -04005187 goto search;
Josef Bacikccf0e722009-11-10 21:23:48 -05005188
5189 /*
5190 * 1 of the following 2 things have happened so far
5191 *
5192 * 1) We found an ideal block group for caching that
5193 * is mostly full and will cache quickly, so we might
5194 * as well wait for it.
5195 *
5196 * 2) We searched for cached only and we didn't find
5197 * anything, and we didn't start any caching kthreads
5198 * either, so chances are we will loop through and
5199 * start a couple caching kthreads, and then come back
5200 * around and just wait for them. This will be slower
5201 * because we will have 2 caching kthreads reading at
5202 * the same time when we could have just started one
5203 * and waited for it to get far enough to give us an
5204 * allocation, so go ahead and go to the wait caching
5205 * loop.
5206 */
5207 loop = LOOP_CACHING_WAIT;
5208 search_start = ideal_cache_offset;
5209 ideal_cache_percent = 0;
5210 goto ideal_cache;
5211 } else if (loop == LOOP_FIND_IDEAL) {
5212 /*
5213 * Didn't find a uncached bg, wait on anything we find
5214 * next.
5215 */
5216 loop = LOOP_CACHING_WAIT;
5217 goto search;
5218 }
5219
5220 if (loop < LOOP_CACHING_WAIT) {
5221 loop++;
5222 goto search;
Josef Bacik817d52f2009-07-13 21:29:25 -04005223 }
5224
5225 if (loop == LOOP_ALLOC_CHUNK) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005226 empty_size = 0;
5227 empty_cluster = 0;
5228 }
Chris Mason42e70e72008-11-07 18:17:11 -05005229
Josef Bacik2552d172009-04-03 10:14:19 -04005230 if (allowed_chunk_alloc) {
5231 ret = do_chunk_alloc(trans, root, num_bytes +
Chris Mason0e4f8f82011-04-15 16:05:44 -04005232 2 * 1024 * 1024, data,
5233 CHUNK_ALLOC_LIMITED);
Josef Bacik2552d172009-04-03 10:14:19 -04005234 allowed_chunk_alloc = 0;
Josef Bacikccf0e722009-11-10 21:23:48 -05005235 done_chunk_alloc = 1;
Chris Mason0e4f8f82011-04-15 16:05:44 -04005236 } else if (!done_chunk_alloc &&
5237 space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) {
5238 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005239 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005240
Josef Bacik817d52f2009-07-13 21:29:25 -04005241 if (loop < LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005242 loop++;
Josef Bacik2552d172009-04-03 10:14:19 -04005243 goto search;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005244 }
Josef Bacik2552d172009-04-03 10:14:19 -04005245 ret = -ENOSPC;
5246 } else if (!ins->objectid) {
5247 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04005248 }
Chris Mason0b86a832008-03-24 15:01:56 -04005249
Josef Bacik80eb2342008-10-29 14:49:05 -04005250 /* we found what we needed */
5251 if (ins->objectid) {
5252 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05005253 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04005254
Chris Masonfa9c0d792009-04-03 09:47:43 -04005255 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005256 ret = 0;
5257 }
Chris Mason0b86a832008-03-24 15:01:56 -04005258
Chris Mason0f70abe2007-02-28 16:46:22 -05005259 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005260}
Chris Masonec44a352008-04-28 15:29:52 -04005261
Josef Bacik9ed74f22009-09-11 16:12:44 -04005262static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5263 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005264{
5265 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005266 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005267
Josef Bacik9ed74f22009-09-11 16:12:44 -04005268 spin_lock(&info->lock);
Chris Masond3977122009-01-05 21:25:51 -05005269 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5270 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005271 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005272 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005273 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005274 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5275 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005276 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005277 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005278 (unsigned long long)info->bytes_pinned,
5279 (unsigned long long)info->bytes_reserved,
5280 (unsigned long long)info->bytes_may_use,
5281 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005282 spin_unlock(&info->lock);
5283
5284 if (!dump_block_groups)
5285 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005286
Josef Bacik80eb2342008-10-29 14:49:05 -04005287 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005288again:
5289 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005290 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05005291 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5292 "%llu pinned %llu reserved\n",
5293 (unsigned long long)cache->key.objectid,
5294 (unsigned long long)cache->key.offset,
5295 (unsigned long long)btrfs_block_group_used(&cache->item),
5296 (unsigned long long)cache->pinned,
5297 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005298 btrfs_dump_free_space(cache, bytes);
5299 spin_unlock(&cache->lock);
5300 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005301 if (++index < BTRFS_NR_RAID_TYPES)
5302 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005303 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005304}
Zheng Yane8569812008-09-26 10:05:48 -04005305
Yan Zheng11833d62009-09-11 16:11:19 -04005306int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5307 struct btrfs_root *root,
5308 u64 num_bytes, u64 min_alloc_size,
5309 u64 empty_size, u64 hint_byte,
5310 u64 search_end, struct btrfs_key *ins,
5311 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005312{
5313 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04005314 u64 search_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005315
Josef Bacik6a632092009-02-20 11:00:09 -05005316 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005317again:
Chris Mason0ef3e662008-05-24 14:04:53 -04005318 /*
5319 * the only place that sets empty_size is btrfs_realloc_node, which
5320 * is not called recursively on allocations
5321 */
Josef Bacik83d3c962009-12-07 21:45:59 +00005322 if (empty_size || root->ref_cows)
Chris Mason6324fbf2008-03-24 15:01:59 -04005323 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005324 num_bytes + 2 * 1024 * 1024, data,
5325 CHUNK_ALLOC_NO_FORCE);
Chris Mason0b86a832008-03-24 15:01:56 -04005326
Chris Masondb945352007-10-15 16:15:53 -04005327 WARN_ON(num_bytes < root->sectorsize);
5328 ret = find_free_extent(trans, root, num_bytes, empty_size,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005329 search_start, search_end, hint_byte,
5330 ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005331
Chris Mason98d20f62008-04-14 09:46:10 -04005332 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5333 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005334 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04005335 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04005336 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04005337 num_bytes, data, CHUNK_ALLOC_FORCE);
Chris Mason98d20f62008-04-14 09:46:10 -04005338 goto again;
5339 }
Chris Mason91435652011-02-16 13:10:41 -05005340 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005341 struct btrfs_space_info *sinfo;
5342
5343 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05005344 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5345 "wanted %llu\n", (unsigned long long)data,
5346 (unsigned long long)num_bytes);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005347 dump_space_info(sinfo, num_bytes, 1);
Chris Mason925baed2008-06-25 16:01:30 -04005348 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005349
liubo1abe9b82011-03-24 11:18:59 +00005350 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5351
Josef Bacik0f9dd462008-09-23 13:14:11 -04005352 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005353}
5354
Chris Mason65b51a02008-08-01 15:11:20 -04005355int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5356{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005357 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005358 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005359
Josef Bacik0f9dd462008-09-23 13:14:11 -04005360 cache = btrfs_lookup_block_group(root->fs_info, start);
5361 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005362 printk(KERN_ERR "Unable to find block group for %llu\n",
5363 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005364 return -ENOSPC;
5365 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005366
Li Dongyang5378e602011-03-24 10:24:27 +00005367 if (btrfs_test_opt(root, DISCARD))
5368 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005369
Josef Bacik0f9dd462008-09-23 13:14:11 -04005370 btrfs_add_free_space(cache, start, len);
Li Dongyangb4d00d52011-03-24 10:24:25 +00005371 btrfs_update_reserved_bytes(cache, len, 0, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005372 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005373
liubo1abe9b82011-03-24 11:18:59 +00005374 trace_btrfs_reserved_extent_free(root, start, len);
5375
Chris Masone6dcd2d2008-07-17 12:53:50 -04005376 return ret;
5377}
5378
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005379static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5380 struct btrfs_root *root,
5381 u64 parent, u64 root_objectid,
5382 u64 flags, u64 owner, u64 offset,
5383 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005384{
5385 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005386 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005387 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005388 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005389 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005390 struct extent_buffer *leaf;
5391 int type;
5392 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005393
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005394 if (parent > 0)
5395 type = BTRFS_SHARED_DATA_REF_KEY;
5396 else
5397 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005398
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005399 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005400
5401 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005402 if (!path)
5403 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005404
Chris Masonb9473432009-03-13 11:00:37 -04005405 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005406 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5407 ins, size);
Chris Masonccd467d2007-06-28 15:57:36 -04005408 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005409
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005410 leaf = path->nodes[0];
5411 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005412 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005413 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5414 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5415 btrfs_set_extent_flags(leaf, extent_item,
5416 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05005417
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005418 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5419 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5420 if (parent > 0) {
5421 struct btrfs_shared_data_ref *ref;
5422 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5423 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5424 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5425 } else {
5426 struct btrfs_extent_data_ref *ref;
5427 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5428 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5429 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5430 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5431 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5432 }
Chris Mason47e4bb92008-02-01 14:51:59 -05005433
5434 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005435 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005436
Yan, Zhengf0486c62010-05-16 10:46:25 -04005437 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Chris Masonf5947062008-02-04 10:10:13 -05005438 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05005439 printk(KERN_ERR "btrfs update block group failed for %llu "
5440 "%llu\n", (unsigned long long)ins->objectid,
5441 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005442 BUG();
5443 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005444 return ret;
5445}
5446
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005447static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5448 struct btrfs_root *root,
5449 u64 parent, u64 root_objectid,
5450 u64 flags, struct btrfs_disk_key *key,
5451 int level, struct btrfs_key *ins)
5452{
5453 int ret;
5454 struct btrfs_fs_info *fs_info = root->fs_info;
5455 struct btrfs_extent_item *extent_item;
5456 struct btrfs_tree_block_info *block_info;
5457 struct btrfs_extent_inline_ref *iref;
5458 struct btrfs_path *path;
5459 struct extent_buffer *leaf;
5460 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5461
5462 path = btrfs_alloc_path();
5463 BUG_ON(!path);
5464
5465 path->leave_spinning = 1;
5466 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5467 ins, size);
5468 BUG_ON(ret);
5469
5470 leaf = path->nodes[0];
5471 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5472 struct btrfs_extent_item);
5473 btrfs_set_extent_refs(leaf, extent_item, 1);
5474 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5475 btrfs_set_extent_flags(leaf, extent_item,
5476 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5477 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5478
5479 btrfs_set_tree_block_key(leaf, block_info, key);
5480 btrfs_set_tree_block_level(leaf, block_info, level);
5481
5482 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5483 if (parent > 0) {
5484 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5485 btrfs_set_extent_inline_ref_type(leaf, iref,
5486 BTRFS_SHARED_BLOCK_REF_KEY);
5487 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5488 } else {
5489 btrfs_set_extent_inline_ref_type(leaf, iref,
5490 BTRFS_TREE_BLOCK_REF_KEY);
5491 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5492 }
5493
5494 btrfs_mark_buffer_dirty(leaf);
5495 btrfs_free_path(path);
5496
Yan, Zhengf0486c62010-05-16 10:46:25 -04005497 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005498 if (ret) {
5499 printk(KERN_ERR "btrfs update block group failed for %llu "
5500 "%llu\n", (unsigned long long)ins->objectid,
5501 (unsigned long long)ins->offset);
5502 BUG();
5503 }
5504 return ret;
5505}
5506
5507int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5508 struct btrfs_root *root,
5509 u64 root_objectid, u64 owner,
5510 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005511{
5512 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04005513
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005514 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005515
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005516 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5517 0, root_objectid, owner, offset,
5518 BTRFS_ADD_DELAYED_EXTENT, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005519 return ret;
5520}
Chris Masone02119d2008-09-05 16:13:11 -04005521
5522/*
5523 * this is used by the tree logging recovery code. It records that
5524 * an extent has been allocated and makes sure to clear the free
5525 * space cache bits as well
5526 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005527int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5528 struct btrfs_root *root,
5529 u64 root_objectid, u64 owner, u64 offset,
5530 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005531{
5532 int ret;
5533 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04005534 struct btrfs_caching_control *caching_ctl;
5535 u64 start = ins->objectid;
5536 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04005537
Chris Masone02119d2008-09-05 16:13:11 -04005538 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05005539 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04005540 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04005541
Yan Zheng11833d62009-09-11 16:11:19 -04005542 if (!caching_ctl) {
5543 BUG_ON(!block_group_cache_done(block_group));
5544 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5545 BUG_ON(ret);
5546 } else {
5547 mutex_lock(&caching_ctl->mutex);
5548
5549 if (start >= caching_ctl->progress) {
5550 ret = add_excluded_extent(root, start, num_bytes);
5551 BUG_ON(ret);
5552 } else if (start + num_bytes <= caching_ctl->progress) {
5553 ret = btrfs_remove_free_space(block_group,
5554 start, num_bytes);
5555 BUG_ON(ret);
5556 } else {
5557 num_bytes = caching_ctl->progress - start;
5558 ret = btrfs_remove_free_space(block_group,
5559 start, num_bytes);
5560 BUG_ON(ret);
5561
5562 start = caching_ctl->progress;
5563 num_bytes = ins->objectid + ins->offset -
5564 caching_ctl->progress;
5565 ret = add_excluded_extent(root, start, num_bytes);
5566 BUG_ON(ret);
5567 }
5568
5569 mutex_unlock(&caching_ctl->mutex);
5570 put_caching_control(caching_ctl);
5571 }
5572
Li Dongyangb4d00d52011-03-24 10:24:25 +00005573 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005574 BUG_ON(ret);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005575 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005576 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5577 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04005578 return ret;
5579}
5580
Chris Mason65b51a02008-08-01 15:11:20 -04005581struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5582 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05005583 u64 bytenr, u32 blocksize,
5584 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04005585{
5586 struct extent_buffer *buf;
5587
5588 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5589 if (!buf)
5590 return ERR_PTR(-ENOMEM);
5591 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05005592 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04005593 btrfs_tree_lock(buf);
5594 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005595
5596 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04005597 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005598
Chris Masond0c803c2008-09-11 16:17:57 -04005599 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00005600 /*
5601 * we allow two log transactions at a time, use different
5602 * EXENT bit to differentiate dirty pages.
5603 */
5604 if (root->log_transid % 2 == 0)
5605 set_extent_dirty(&root->dirty_log_pages, buf->start,
5606 buf->start + buf->len - 1, GFP_NOFS);
5607 else
5608 set_extent_new(&root->dirty_log_pages, buf->start,
5609 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04005610 } else {
5611 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5612 buf->start + buf->len - 1, GFP_NOFS);
5613 }
Chris Mason65b51a02008-08-01 15:11:20 -04005614 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005615 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04005616 return buf;
5617}
5618
Yan, Zhengf0486c62010-05-16 10:46:25 -04005619static struct btrfs_block_rsv *
5620use_block_rsv(struct btrfs_trans_handle *trans,
5621 struct btrfs_root *root, u32 blocksize)
5622{
5623 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005624 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005625 int ret;
5626
5627 block_rsv = get_block_rsv(trans, root);
5628
5629 if (block_rsv->size == 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005630 ret = reserve_metadata_bytes(trans, root, block_rsv,
5631 blocksize, 0);
Josef Bacik68a82272011-01-24 21:43:20 +00005632 /*
5633 * If we couldn't reserve metadata bytes try and use some from
5634 * the global reserve.
5635 */
5636 if (ret && block_rsv != global_rsv) {
5637 ret = block_rsv_use_bytes(global_rsv, blocksize);
5638 if (!ret)
5639 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005640 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00005641 } else if (ret) {
5642 return ERR_PTR(ret);
5643 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005644 return block_rsv;
5645 }
5646
5647 ret = block_rsv_use_bytes(block_rsv, blocksize);
5648 if (!ret)
5649 return block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00005650 if (ret) {
5651 WARN_ON(1);
5652 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5653 0);
5654 if (!ret) {
5655 spin_lock(&block_rsv->lock);
5656 block_rsv->size += blocksize;
5657 spin_unlock(&block_rsv->lock);
5658 return block_rsv;
5659 } else if (ret && block_rsv != global_rsv) {
5660 ret = block_rsv_use_bytes(global_rsv, blocksize);
5661 if (!ret)
5662 return global_rsv;
5663 }
5664 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005665
Yan, Zhengf0486c62010-05-16 10:46:25 -04005666 return ERR_PTR(-ENOSPC);
5667}
5668
5669static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5670{
5671 block_rsv_add_bytes(block_rsv, blocksize, 0);
5672 block_rsv_release_bytes(block_rsv, NULL, 0);
5673}
5674
Chris Masonfec577f2007-02-26 10:40:21 -05005675/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005676 * finds a free extent and does all the dirty work required for allocation
5677 * returns the key for the extent through ins, and a tree buffer for
5678 * the first block of the extent through buf.
5679 *
Chris Masonfec577f2007-02-26 10:40:21 -05005680 * returns the tree buffer or NULL.
5681 */
Chris Mason5f39d392007-10-15 16:14:19 -04005682struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005683 struct btrfs_root *root, u32 blocksize,
5684 u64 parent, u64 root_objectid,
5685 struct btrfs_disk_key *key, int level,
5686 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05005687{
Chris Masone2fa7222007-03-12 16:22:34 -04005688 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005689 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04005690 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005691 u64 flags = 0;
5692 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005693
Yan, Zhengf0486c62010-05-16 10:46:25 -04005694
5695 block_rsv = use_block_rsv(trans, root, blocksize);
5696 if (IS_ERR(block_rsv))
5697 return ERR_CAST(block_rsv);
5698
5699 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5700 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05005701 if (ret) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005702 unuse_block_rsv(block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005703 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05005704 }
Chris Mason55c69072008-01-09 15:55:33 -05005705
Chris Mason4008c042009-02-12 14:09:45 -05005706 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5707 blocksize, level);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005708 BUG_ON(IS_ERR(buf));
5709
5710 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5711 if (parent == 0)
5712 parent = ins.objectid;
5713 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5714 } else
5715 BUG_ON(parent > 0);
5716
5717 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5718 struct btrfs_delayed_extent_op *extent_op;
5719 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5720 BUG_ON(!extent_op);
5721 if (key)
5722 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5723 else
5724 memset(&extent_op->key, 0, sizeof(extent_op->key));
5725 extent_op->flags_to_set = flags;
5726 extent_op->update_key = 1;
5727 extent_op->update_flags = 1;
5728 extent_op->is_data = 0;
5729
5730 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5731 ins.offset, parent, root_objectid,
5732 level, BTRFS_ADD_DELAYED_EXTENT,
5733 extent_op);
5734 BUG_ON(ret);
5735 }
Chris Masonfec577f2007-02-26 10:40:21 -05005736 return buf;
5737}
Chris Masona28ec192007-03-06 20:08:01 -05005738
Yan Zheng2c47e6052009-06-27 21:07:35 -04005739struct walk_control {
5740 u64 refs[BTRFS_MAX_LEVEL];
5741 u64 flags[BTRFS_MAX_LEVEL];
5742 struct btrfs_key update_progress;
5743 int stage;
5744 int level;
5745 int shared_level;
5746 int update_ref;
5747 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005748 int reada_slot;
5749 int reada_count;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005750};
5751
5752#define DROP_REFERENCE 1
5753#define UPDATE_BACKREF 2
5754
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005755static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5756 struct btrfs_root *root,
5757 struct walk_control *wc,
5758 struct btrfs_path *path)
5759{
5760 u64 bytenr;
5761 u64 generation;
5762 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005763 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005764 u32 nritems;
5765 u32 blocksize;
5766 struct btrfs_key key;
5767 struct extent_buffer *eb;
5768 int ret;
5769 int slot;
5770 int nread = 0;
5771
5772 if (path->slots[wc->level] < wc->reada_slot) {
5773 wc->reada_count = wc->reada_count * 2 / 3;
5774 wc->reada_count = max(wc->reada_count, 2);
5775 } else {
5776 wc->reada_count = wc->reada_count * 3 / 2;
5777 wc->reada_count = min_t(int, wc->reada_count,
5778 BTRFS_NODEPTRS_PER_BLOCK(root));
5779 }
5780
5781 eb = path->nodes[wc->level];
5782 nritems = btrfs_header_nritems(eb);
5783 blocksize = btrfs_level_size(root, wc->level - 1);
5784
5785 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5786 if (nread >= wc->reada_count)
5787 break;
5788
5789 cond_resched();
5790 bytenr = btrfs_node_blockptr(eb, slot);
5791 generation = btrfs_node_ptr_generation(eb, slot);
5792
5793 if (slot == path->slots[wc->level])
5794 goto reada;
5795
5796 if (wc->stage == UPDATE_BACKREF &&
5797 generation <= root->root_key.offset)
5798 continue;
5799
Yan, Zheng94fcca92009-10-09 09:25:16 -04005800 /* We don't lock the tree block, it's OK to be racy here */
5801 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5802 &refs, &flags);
5803 BUG_ON(ret);
5804 BUG_ON(refs == 0);
5805
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005806 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005807 if (refs == 1)
5808 goto reada;
5809
Yan, Zheng94fcca92009-10-09 09:25:16 -04005810 if (wc->level == 1 &&
5811 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5812 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005813 if (!wc->update_ref ||
5814 generation <= root->root_key.offset)
5815 continue;
5816 btrfs_node_key_to_cpu(eb, &key, slot);
5817 ret = btrfs_comp_cpu_keys(&key,
5818 &wc->update_progress);
5819 if (ret < 0)
5820 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005821 } else {
5822 if (wc->level == 1 &&
5823 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5824 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005825 }
5826reada:
5827 ret = readahead_tree_block(root, bytenr, blocksize,
5828 generation);
5829 if (ret)
5830 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005831 nread++;
5832 }
5833 wc->reada_slot = slot;
5834}
5835
Chris Mason9aca1d52007-03-13 11:09:37 -04005836/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005837 * hepler to process tree block while walking down the tree.
5838 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04005839 * when wc->stage == UPDATE_BACKREF, this function updates
5840 * back refs for pointers in the block.
5841 *
5842 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04005843 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005844static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5845 struct btrfs_root *root,
5846 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005847 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04005848{
5849 int level = wc->level;
5850 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04005851 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5852 int ret;
5853
5854 if (wc->stage == UPDATE_BACKREF &&
5855 btrfs_header_owner(eb) != root->root_key.objectid)
5856 return 1;
5857
5858 /*
5859 * when reference count of tree block is 1, it won't increase
5860 * again. once full backref flag is set, we never clear it.
5861 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04005862 if (lookup_info &&
5863 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5864 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005865 BUG_ON(!path->locks[level]);
5866 ret = btrfs_lookup_extent_info(trans, root,
5867 eb->start, eb->len,
5868 &wc->refs[level],
5869 &wc->flags[level]);
5870 BUG_ON(ret);
5871 BUG_ON(wc->refs[level] == 0);
5872 }
5873
Yan Zheng2c47e6052009-06-27 21:07:35 -04005874 if (wc->stage == DROP_REFERENCE) {
5875 if (wc->refs[level] > 1)
5876 return 1;
5877
5878 if (path->locks[level] && !wc->keep_locks) {
5879 btrfs_tree_unlock(eb);
5880 path->locks[level] = 0;
5881 }
5882 return 0;
5883 }
5884
5885 /* wc->stage == UPDATE_BACKREF */
5886 if (!(wc->flags[level] & flag)) {
5887 BUG_ON(!path->locks[level]);
5888 ret = btrfs_inc_ref(trans, root, eb, 1);
5889 BUG_ON(ret);
5890 ret = btrfs_dec_ref(trans, root, eb, 0);
5891 BUG_ON(ret);
5892 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5893 eb->len, flag, 0);
5894 BUG_ON(ret);
5895 wc->flags[level] |= flag;
5896 }
5897
5898 /*
5899 * the block is shared by multiple trees, so it's not good to
5900 * keep the tree lock
5901 */
5902 if (path->locks[level] && level > 0) {
5903 btrfs_tree_unlock(eb);
5904 path->locks[level] = 0;
5905 }
5906 return 0;
5907}
5908
5909/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005910 * hepler to process tree block pointer.
5911 *
5912 * when wc->stage == DROP_REFERENCE, this function checks
5913 * reference count of the block pointed to. if the block
5914 * is shared and we need update back refs for the subtree
5915 * rooted at the block, this function changes wc->stage to
5916 * UPDATE_BACKREF. if the block is shared and there is no
5917 * need to update back, this function drops the reference
5918 * to the block.
5919 *
5920 * NOTE: return value 1 means we should stop walking down.
5921 */
5922static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5923 struct btrfs_root *root,
5924 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04005925 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005926{
5927 u64 bytenr;
5928 u64 generation;
5929 u64 parent;
5930 u32 blocksize;
5931 struct btrfs_key key;
5932 struct extent_buffer *next;
5933 int level = wc->level;
5934 int reada = 0;
5935 int ret = 0;
5936
5937 generation = btrfs_node_ptr_generation(path->nodes[level],
5938 path->slots[level]);
5939 /*
5940 * if the lower level block was created before the snapshot
5941 * was created, we know there is no need to update back refs
5942 * for the subtree
5943 */
5944 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04005945 generation <= root->root_key.offset) {
5946 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005947 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005948 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005949
5950 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5951 blocksize = btrfs_level_size(root, level - 1);
5952
5953 next = btrfs_find_tree_block(root, bytenr, blocksize);
5954 if (!next) {
5955 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00005956 if (!next)
5957 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005958 reada = 1;
5959 }
5960 btrfs_tree_lock(next);
5961 btrfs_set_lock_blocking(next);
5962
Yan, Zheng94fcca92009-10-09 09:25:16 -04005963 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5964 &wc->refs[level - 1],
5965 &wc->flags[level - 1]);
5966 BUG_ON(ret);
5967 BUG_ON(wc->refs[level - 1] == 0);
5968 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005969
Yan, Zheng94fcca92009-10-09 09:25:16 -04005970 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005971 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005972 if (level == 1 &&
5973 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5974 goto skip;
5975
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005976 if (!wc->update_ref ||
5977 generation <= root->root_key.offset)
5978 goto skip;
5979
5980 btrfs_node_key_to_cpu(path->nodes[level], &key,
5981 path->slots[level]);
5982 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5983 if (ret < 0)
5984 goto skip;
5985
5986 wc->stage = UPDATE_BACKREF;
5987 wc->shared_level = level - 1;
5988 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04005989 } else {
5990 if (level == 1 &&
5991 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5992 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005993 }
5994
5995 if (!btrfs_buffer_uptodate(next, generation)) {
5996 btrfs_tree_unlock(next);
5997 free_extent_buffer(next);
5998 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005999 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006000 }
6001
6002 if (!next) {
6003 if (reada && level == 1)
6004 reada_walk_down(trans, root, wc, path);
6005 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006006 if (!next)
6007 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006008 btrfs_tree_lock(next);
6009 btrfs_set_lock_blocking(next);
6010 }
6011
6012 level--;
6013 BUG_ON(level != btrfs_header_level(next));
6014 path->nodes[level] = next;
6015 path->slots[level] = 0;
6016 path->locks[level] = 1;
6017 wc->level = level;
6018 if (wc->level == 1)
6019 wc->reada_slot = 0;
6020 return 0;
6021skip:
6022 wc->refs[level - 1] = 0;
6023 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006024 if (wc->stage == DROP_REFERENCE) {
6025 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6026 parent = path->nodes[level]->start;
6027 } else {
6028 BUG_ON(root->root_key.objectid !=
6029 btrfs_header_owner(path->nodes[level]));
6030 parent = 0;
6031 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006032
Yan, Zheng94fcca92009-10-09 09:25:16 -04006033 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6034 root->root_key.objectid, level - 1, 0);
6035 BUG_ON(ret);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006036 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006037 btrfs_tree_unlock(next);
6038 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006039 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006040 return 1;
6041}
6042
6043/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006044 * hepler to process tree block while walking up the tree.
6045 *
6046 * when wc->stage == DROP_REFERENCE, this function drops
6047 * reference count on the block.
6048 *
6049 * when wc->stage == UPDATE_BACKREF, this function changes
6050 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6051 * to UPDATE_BACKREF previously while processing the block.
6052 *
6053 * NOTE: return value 1 means we should stop walking up.
6054 */
6055static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6056 struct btrfs_root *root,
6057 struct btrfs_path *path,
6058 struct walk_control *wc)
6059{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006060 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006061 int level = wc->level;
6062 struct extent_buffer *eb = path->nodes[level];
6063 u64 parent = 0;
6064
6065 if (wc->stage == UPDATE_BACKREF) {
6066 BUG_ON(wc->shared_level < level);
6067 if (level < wc->shared_level)
6068 goto out;
6069
Yan Zheng2c47e6052009-06-27 21:07:35 -04006070 ret = find_next_key(path, level + 1, &wc->update_progress);
6071 if (ret > 0)
6072 wc->update_ref = 0;
6073
6074 wc->stage = DROP_REFERENCE;
6075 wc->shared_level = -1;
6076 path->slots[level] = 0;
6077
6078 /*
6079 * check reference count again if the block isn't locked.
6080 * we should start walking down the tree again if reference
6081 * count is one.
6082 */
6083 if (!path->locks[level]) {
6084 BUG_ON(level == 0);
6085 btrfs_tree_lock(eb);
6086 btrfs_set_lock_blocking(eb);
6087 path->locks[level] = 1;
6088
6089 ret = btrfs_lookup_extent_info(trans, root,
6090 eb->start, eb->len,
6091 &wc->refs[level],
6092 &wc->flags[level]);
6093 BUG_ON(ret);
6094 BUG_ON(wc->refs[level] == 0);
6095 if (wc->refs[level] == 1) {
6096 btrfs_tree_unlock(eb);
6097 path->locks[level] = 0;
6098 return 1;
6099 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006100 }
6101 }
6102
6103 /* wc->stage == DROP_REFERENCE */
6104 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6105
6106 if (wc->refs[level] == 1) {
6107 if (level == 0) {
6108 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6109 ret = btrfs_dec_ref(trans, root, eb, 1);
6110 else
6111 ret = btrfs_dec_ref(trans, root, eb, 0);
6112 BUG_ON(ret);
6113 }
6114 /* make block locked assertion in clean_tree_block happy */
6115 if (!path->locks[level] &&
6116 btrfs_header_generation(eb) == trans->transid) {
6117 btrfs_tree_lock(eb);
6118 btrfs_set_lock_blocking(eb);
6119 path->locks[level] = 1;
6120 }
6121 clean_tree_block(trans, root, eb);
6122 }
6123
6124 if (eb == root->node) {
6125 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6126 parent = eb->start;
6127 else
6128 BUG_ON(root->root_key.objectid !=
6129 btrfs_header_owner(eb));
6130 } else {
6131 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6132 parent = path->nodes[level + 1]->start;
6133 else
6134 BUG_ON(root->root_key.objectid !=
6135 btrfs_header_owner(path->nodes[level + 1]));
6136 }
6137
Yan, Zhengf0486c62010-05-16 10:46:25 -04006138 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006139out:
6140 wc->refs[level] = 0;
6141 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006142 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006143}
6144
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006145static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6146 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006147 struct btrfs_path *path,
6148 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006149{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006150 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006151 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006152 int ret;
6153
Yan Zheng2c47e6052009-06-27 21:07:35 -04006154 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006155 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006156 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006157 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006158
Yan Zheng2c47e6052009-06-27 21:07:35 -04006159 if (level == 0)
6160 break;
6161
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006162 if (path->slots[level] >=
6163 btrfs_header_nritems(path->nodes[level]))
6164 break;
6165
Yan, Zheng94fcca92009-10-09 09:25:16 -04006166 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006167 if (ret > 0) {
6168 path->slots[level]++;
6169 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006170 } else if (ret < 0)
6171 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006172 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006173 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006174 return 0;
6175}
6176
Chris Masond3977122009-01-05 21:25:51 -05006177static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006178 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006179 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006180 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006181{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006182 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006183 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006184
Yan Zheng2c47e6052009-06-27 21:07:35 -04006185 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6186 while (level < max_level && path->nodes[level]) {
6187 wc->level = level;
6188 if (path->slots[level] + 1 <
6189 btrfs_header_nritems(path->nodes[level])) {
6190 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006191 return 0;
6192 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006193 ret = walk_up_proc(trans, root, path, wc);
6194 if (ret > 0)
6195 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006196
Yan Zheng2c47e6052009-06-27 21:07:35 -04006197 if (path->locks[level]) {
6198 btrfs_tree_unlock(path->nodes[level]);
6199 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006200 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006201 free_extent_buffer(path->nodes[level]);
6202 path->nodes[level] = NULL;
6203 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006204 }
6205 }
6206 return 1;
6207}
6208
Chris Mason9aca1d52007-03-13 11:09:37 -04006209/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006210 * drop a subvolume tree.
6211 *
6212 * this function traverses the tree freeing any blocks that only
6213 * referenced by the tree.
6214 *
6215 * when a shared tree block is found. this function decreases its
6216 * reference count by one. if update_ref is true, this function
6217 * also make sure backrefs for the shared block and all lower level
6218 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006219 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006220int btrfs_drop_snapshot(struct btrfs_root *root,
6221 struct btrfs_block_rsv *block_rsv, int update_ref)
Chris Mason20524f02007-03-10 06:35:47 -05006222{
Chris Mason5caf2a02007-04-02 11:20:42 -04006223 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006224 struct btrfs_trans_handle *trans;
6225 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006226 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006227 struct walk_control *wc;
6228 struct btrfs_key key;
6229 int err = 0;
6230 int ret;
6231 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006232
Chris Mason5caf2a02007-04-02 11:20:42 -04006233 path = btrfs_alloc_path();
6234 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05006235
Yan Zheng2c47e6052009-06-27 21:07:35 -04006236 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6237 BUG_ON(!wc);
6238
Yan, Zhenga22285a2010-05-16 10:48:46 -04006239 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006240 BUG_ON(IS_ERR(trans));
6241
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006242 if (block_rsv)
6243 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006244
Chris Mason9f3a7422007-08-07 15:52:19 -04006245 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006246 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006247 path->nodes[level] = btrfs_lock_root_node(root);
6248 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006249 path->slots[level] = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006250 path->locks[level] = 1;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006251 memset(&wc->update_progress, 0,
6252 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006253 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006254 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006255 memcpy(&wc->update_progress, &key,
6256 sizeof(wc->update_progress));
6257
Chris Mason6702ed42007-08-07 16:15:09 -04006258 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006259 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006260 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006261 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6262 path->lowest_level = 0;
6263 if (ret < 0) {
6264 err = ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006265 goto out;
6266 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006267 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006268
Chris Mason7d9eb122008-07-08 14:19:17 -04006269 /*
6270 * unlock our path, this is safe because only this
6271 * function is allowed to delete this snapshot
6272 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006273 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006274
Yan Zheng2c47e6052009-06-27 21:07:35 -04006275 level = btrfs_header_level(root->node);
6276 while (1) {
6277 btrfs_tree_lock(path->nodes[level]);
6278 btrfs_set_lock_blocking(path->nodes[level]);
6279
6280 ret = btrfs_lookup_extent_info(trans, root,
6281 path->nodes[level]->start,
6282 path->nodes[level]->len,
6283 &wc->refs[level],
6284 &wc->flags[level]);
6285 BUG_ON(ret);
6286 BUG_ON(wc->refs[level] == 0);
6287
6288 if (level == root_item->drop_level)
6289 break;
6290
6291 btrfs_tree_unlock(path->nodes[level]);
6292 WARN_ON(wc->refs[level] != 1);
6293 level--;
6294 }
6295 }
6296
6297 wc->level = level;
6298 wc->shared_level = -1;
6299 wc->stage = DROP_REFERENCE;
6300 wc->update_ref = update_ref;
6301 wc->keep_locks = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006302 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006303
6304 while (1) {
6305 ret = walk_down_tree(trans, root, path, wc);
6306 if (ret < 0) {
6307 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006308 break;
6309 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006310
6311 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6312 if (ret < 0) {
6313 err = ret;
6314 break;
6315 }
6316
6317 if (ret > 0) {
6318 BUG_ON(wc->stage != DROP_REFERENCE);
6319 break;
6320 }
6321
6322 if (wc->stage == DROP_REFERENCE) {
6323 level = wc->level;
6324 btrfs_node_key(path->nodes[level],
6325 &root_item->drop_progress,
6326 path->slots[level]);
6327 root_item->drop_level = level;
6328 }
6329
6330 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006331 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006332 ret = btrfs_update_root(trans, tree_root,
6333 &root->root_key,
6334 root_item);
6335 BUG_ON(ret);
6336
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006337 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006338 trans = btrfs_start_transaction(tree_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006339 BUG_ON(IS_ERR(trans));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006340 if (block_rsv)
6341 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006342 }
Chris Mason20524f02007-03-10 06:35:47 -05006343 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006344 btrfs_release_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006345 BUG_ON(err);
6346
6347 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6348 BUG_ON(ret);
6349
Yan, Zheng76dda932009-09-21 16:00:26 -04006350 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6351 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6352 NULL, NULL);
6353 BUG_ON(ret < 0);
6354 if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006355 /* if we fail to delete the orphan item this time
6356 * around, it'll get picked up the next time.
6357 *
6358 * The most common failure here is just -ENOENT.
6359 */
6360 btrfs_del_orphan_item(trans, tree_root,
6361 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04006362 }
6363 }
6364
6365 if (root->in_radix) {
6366 btrfs_free_fs_root(tree_root->fs_info, root);
6367 } else {
6368 free_extent_buffer(root->node);
6369 free_extent_buffer(root->commit_root);
6370 kfree(root);
6371 }
Chris Mason9f3a7422007-08-07 15:52:19 -04006372out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006373 btrfs_end_transaction_throttle(trans, tree_root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006374 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04006375 btrfs_free_path(path);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006376 return err;
Chris Mason20524f02007-03-10 06:35:47 -05006377}
Chris Mason9078a3e2007-04-26 16:46:15 -04006378
Yan Zheng2c47e6052009-06-27 21:07:35 -04006379/*
6380 * drop subtree rooted at tree block 'node'.
6381 *
6382 * NOTE: this function will unlock and release tree block 'node'
6383 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04006384int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6385 struct btrfs_root *root,
6386 struct extent_buffer *node,
6387 struct extent_buffer *parent)
6388{
6389 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006390 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006391 int level;
6392 int parent_level;
6393 int ret = 0;
6394 int wret;
6395
Yan Zheng2c47e6052009-06-27 21:07:35 -04006396 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6397
Yan Zhengf82d02d2008-10-29 14:49:05 -04006398 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006399 if (!path)
6400 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006401
Yan Zheng2c47e6052009-06-27 21:07:35 -04006402 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006403 if (!wc) {
6404 btrfs_free_path(path);
6405 return -ENOMEM;
6406 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006407
Chris Masonb9447ef2009-03-09 11:45:38 -04006408 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006409 parent_level = btrfs_header_level(parent);
6410 extent_buffer_get(parent);
6411 path->nodes[parent_level] = parent;
6412 path->slots[parent_level] = btrfs_header_nritems(parent);
6413
Chris Masonb9447ef2009-03-09 11:45:38 -04006414 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006415 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006416 path->nodes[level] = node;
6417 path->slots[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006418 path->locks[level] = 1;
6419
6420 wc->refs[parent_level] = 1;
6421 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6422 wc->level = level;
6423 wc->shared_level = -1;
6424 wc->stage = DROP_REFERENCE;
6425 wc->update_ref = 0;
6426 wc->keep_locks = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006427 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006428
6429 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006430 wret = walk_down_tree(trans, root, path, wc);
6431 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04006432 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006433 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006434 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006435
Yan Zheng2c47e6052009-06-27 21:07:35 -04006436 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006437 if (wret < 0)
6438 ret = wret;
6439 if (wret != 0)
6440 break;
6441 }
6442
Yan Zheng2c47e6052009-06-27 21:07:35 -04006443 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04006444 btrfs_free_path(path);
6445 return ret;
6446}
6447
Chris Masonec44a352008-04-28 15:29:52 -04006448static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6449{
6450 u64 num_devices;
6451 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6452 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6453
Chris Masoncd02dca2010-12-13 14:56:23 -05006454 /*
6455 * we add in the count of missing devices because we want
6456 * to make sure that any RAID levels on a degraded FS
6457 * continue to be honored.
6458 */
6459 num_devices = root->fs_info->fs_devices->rw_devices +
6460 root->fs_info->fs_devices->missing_devices;
6461
Chris Masonec44a352008-04-28 15:29:52 -04006462 if (num_devices == 1) {
6463 stripped |= BTRFS_BLOCK_GROUP_DUP;
6464 stripped = flags & ~stripped;
6465
6466 /* turn raid0 into single device chunks */
6467 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6468 return stripped;
6469
6470 /* turn mirroring into duplication */
6471 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6472 BTRFS_BLOCK_GROUP_RAID10))
6473 return stripped | BTRFS_BLOCK_GROUP_DUP;
6474 return flags;
6475 } else {
6476 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04006477 if (flags & stripped)
6478 return flags;
6479
6480 stripped |= BTRFS_BLOCK_GROUP_DUP;
6481 stripped = flags & ~stripped;
6482
6483 /* switch duplicated blocks with raid1 */
6484 if (flags & BTRFS_BLOCK_GROUP_DUP)
6485 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6486
6487 /* turn single device chunks into raid0 */
6488 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6489 }
6490 return flags;
6491}
6492
Yan, Zhengf0486c62010-05-16 10:46:25 -04006493static int set_block_group_ro(struct btrfs_block_group_cache *cache)
Chris Mason0ef3e662008-05-24 14:04:53 -04006494{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006495 struct btrfs_space_info *sinfo = cache->space_info;
6496 u64 num_bytes;
6497 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04006498
Yan, Zhengf0486c62010-05-16 10:46:25 -04006499 if (cache->ro)
6500 return 0;
Chris Masonc286ac42008-07-22 23:06:41 -04006501
Yan, Zhengf0486c62010-05-16 10:46:25 -04006502 spin_lock(&sinfo->lock);
6503 spin_lock(&cache->lock);
6504 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6505 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04006506
Yan, Zhengf0486c62010-05-16 10:46:25 -04006507 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
6508 sinfo->bytes_may_use + sinfo->bytes_readonly +
Chris Mason65e53412010-12-24 06:41:52 -05006509 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04006510 sinfo->bytes_readonly += num_bytes;
6511 sinfo->bytes_reserved += cache->reserved_pinned;
6512 cache->reserved_pinned = 0;
6513 cache->ro = 1;
6514 ret = 0;
6515 }
Chris Mason65e53412010-12-24 06:41:52 -05006516
Yan, Zhengf0486c62010-05-16 10:46:25 -04006517 spin_unlock(&cache->lock);
6518 spin_unlock(&sinfo->lock);
6519 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04006520}
6521
Yan, Zhengf0486c62010-05-16 10:46:25 -04006522int btrfs_set_block_group_ro(struct btrfs_root *root,
6523 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006524
6525{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006526 struct btrfs_trans_handle *trans;
6527 u64 alloc_flags;
6528 int ret;
6529
6530 BUG_ON(cache->ro);
6531
6532 trans = btrfs_join_transaction(root, 1);
6533 BUG_ON(IS_ERR(trans));
6534
6535 alloc_flags = update_block_group_flags(root, cache->flags);
6536 if (alloc_flags != cache->flags)
Chris Mason0e4f8f82011-04-15 16:05:44 -04006537 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6538 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006539
6540 ret = set_block_group_ro(cache);
6541 if (!ret)
6542 goto out;
6543 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006544 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6545 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006546 if (ret < 0)
6547 goto out;
6548 ret = set_block_group_ro(cache);
6549out:
6550 btrfs_end_transaction(trans, root);
6551 return ret;
6552}
6553
Chris Masonc87f08c2011-02-16 13:57:04 -05006554int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
6555 struct btrfs_root *root, u64 type)
6556{
6557 u64 alloc_flags = get_alloc_profile(root, type);
Chris Mason0e4f8f82011-04-15 16:05:44 -04006558 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6559 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05006560}
6561
Miao Xie6d07bce2011-01-05 10:07:31 +00006562/*
6563 * helper to account the unused space of all the readonly block group in the
6564 * list. takes mirrors into account.
6565 */
6566static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
6567{
6568 struct btrfs_block_group_cache *block_group;
6569 u64 free_bytes = 0;
6570 int factor;
6571
6572 list_for_each_entry(block_group, groups_list, list) {
6573 spin_lock(&block_group->lock);
6574
6575 if (!block_group->ro) {
6576 spin_unlock(&block_group->lock);
6577 continue;
6578 }
6579
6580 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
6581 BTRFS_BLOCK_GROUP_RAID10 |
6582 BTRFS_BLOCK_GROUP_DUP))
6583 factor = 2;
6584 else
6585 factor = 1;
6586
6587 free_bytes += (block_group->key.offset -
6588 btrfs_block_group_used(&block_group->item)) *
6589 factor;
6590
6591 spin_unlock(&block_group->lock);
6592 }
6593
6594 return free_bytes;
6595}
6596
6597/*
6598 * helper to account the unused space of all the readonly block group in the
6599 * space_info. takes mirrors into account.
6600 */
6601u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
6602{
6603 int i;
6604 u64 free_bytes = 0;
6605
6606 spin_lock(&sinfo->lock);
6607
6608 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
6609 if (!list_empty(&sinfo->block_groups[i]))
6610 free_bytes += __btrfs_get_ro_block_group_free_space(
6611 &sinfo->block_groups[i]);
6612
6613 spin_unlock(&sinfo->lock);
6614
6615 return free_bytes;
6616}
6617
Yan, Zhengf0486c62010-05-16 10:46:25 -04006618int btrfs_set_block_group_rw(struct btrfs_root *root,
6619 struct btrfs_block_group_cache *cache)
6620{
6621 struct btrfs_space_info *sinfo = cache->space_info;
6622 u64 num_bytes;
6623
6624 BUG_ON(!cache->ro);
6625
6626 spin_lock(&sinfo->lock);
6627 spin_lock(&cache->lock);
6628 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6629 cache->bytes_super - btrfs_block_group_used(&cache->item);
6630 sinfo->bytes_readonly -= num_bytes;
6631 cache->ro = 0;
6632 spin_unlock(&cache->lock);
6633 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006634 return 0;
6635}
6636
Josef Bacikba1bf482009-09-11 16:11:19 -04006637/*
6638 * checks to see if its even possible to relocate this block group.
6639 *
6640 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6641 * ok to go ahead and try.
6642 */
6643int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04006644{
Zheng Yan1a40e232008-09-26 10:09:34 -04006645 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04006646 struct btrfs_space_info *space_info;
6647 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6648 struct btrfs_device *device;
6649 int full = 0;
6650 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006651
Josef Bacikba1bf482009-09-11 16:11:19 -04006652 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04006653
Josef Bacikba1bf482009-09-11 16:11:19 -04006654 /* odd, couldn't find the block group, leave it alone */
6655 if (!block_group)
6656 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05006657
Josef Bacikba1bf482009-09-11 16:11:19 -04006658 /* no bytes used, we're good */
6659 if (!btrfs_block_group_used(&block_group->item))
6660 goto out;
Chris Mason323da792008-05-09 11:46:48 -04006661
Josef Bacikba1bf482009-09-11 16:11:19 -04006662 space_info = block_group->space_info;
6663 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04006664
Josef Bacikba1bf482009-09-11 16:11:19 -04006665 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04006666
Josef Bacikba1bf482009-09-11 16:11:19 -04006667 /*
6668 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04006669 * relocate it unless we're able to allocate a new chunk below.
6670 *
6671 * Otherwise, we need to make sure we have room in the space to handle
6672 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04006673 */
Chris Mason7ce618d2009-09-22 14:48:44 -04006674 if ((space_info->total_bytes != block_group->key.offset) &&
6675 (space_info->bytes_used + space_info->bytes_reserved +
Josef Bacikba1bf482009-09-11 16:11:19 -04006676 space_info->bytes_pinned + space_info->bytes_readonly +
6677 btrfs_block_group_used(&block_group->item) <
Chris Mason7ce618d2009-09-22 14:48:44 -04006678 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04006679 spin_unlock(&space_info->lock);
6680 goto out;
6681 }
6682 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006683
Josef Bacikba1bf482009-09-11 16:11:19 -04006684 /*
6685 * ok we don't have enough space, but maybe we have free space on our
6686 * devices to allocate new chunks for relocation, so loop through our
6687 * alloc devices and guess if we have enough space. However, if we
6688 * were marked as full, then we know there aren't enough chunks, and we
6689 * can just return.
6690 */
6691 ret = -1;
6692 if (full)
6693 goto out;
Chris Mason4313b392008-01-03 09:08:48 -05006694
Josef Bacikba1bf482009-09-11 16:11:19 -04006695 mutex_lock(&root->fs_info->chunk_mutex);
6696 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
6697 u64 min_free = btrfs_block_group_used(&block_group->item);
Miao Xie7bfc8372011-01-05 10:07:26 +00006698 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04006699
Josef Bacikba1bf482009-09-11 16:11:19 -04006700 /*
6701 * check to make sure we can actually find a chunk with enough
6702 * space to fit our block group in.
6703 */
6704 if (device->total_bytes > device->bytes_used + min_free) {
6705 ret = find_free_dev_extent(NULL, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00006706 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04006707 if (!ret)
Yan73e48b22008-01-03 14:14:39 -05006708 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04006709 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05006710 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006711 }
Josef Bacikba1bf482009-09-11 16:11:19 -04006712 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05006713out:
Josef Bacikba1bf482009-09-11 16:11:19 -04006714 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006715 return ret;
6716}
6717
Christoph Hellwigb2950862008-12-02 09:54:17 -05006718static int find_first_block_group(struct btrfs_root *root,
6719 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04006720{
Chris Mason925baed2008-06-25 16:01:30 -04006721 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006722 struct btrfs_key found_key;
6723 struct extent_buffer *leaf;
6724 int slot;
6725
6726 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6727 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006728 goto out;
6729
Chris Masond3977122009-01-05 21:25:51 -05006730 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006731 slot = path->slots[0];
6732 leaf = path->nodes[0];
6733 if (slot >= btrfs_header_nritems(leaf)) {
6734 ret = btrfs_next_leaf(root, path);
6735 if (ret == 0)
6736 continue;
6737 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006738 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04006739 break;
6740 }
6741 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6742
6743 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04006744 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6745 ret = 0;
6746 goto out;
6747 }
Chris Mason0b86a832008-03-24 15:01:56 -04006748 path->slots[0]++;
6749 }
Chris Mason925baed2008-06-25 16:01:30 -04006750out:
Chris Mason0b86a832008-03-24 15:01:56 -04006751 return ret;
6752}
6753
Josef Bacik0af3d002010-06-21 14:48:16 -04006754void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
6755{
6756 struct btrfs_block_group_cache *block_group;
6757 u64 last = 0;
6758
6759 while (1) {
6760 struct inode *inode;
6761
6762 block_group = btrfs_lookup_first_block_group(info, last);
6763 while (block_group) {
6764 spin_lock(&block_group->lock);
6765 if (block_group->iref)
6766 break;
6767 spin_unlock(&block_group->lock);
6768 block_group = next_block_group(info->tree_root,
6769 block_group);
6770 }
6771 if (!block_group) {
6772 if (last == 0)
6773 break;
6774 last = 0;
6775 continue;
6776 }
6777
6778 inode = block_group->inode;
6779 block_group->iref = 0;
6780 block_group->inode = NULL;
6781 spin_unlock(&block_group->lock);
6782 iput(inode);
6783 last = block_group->key.objectid + block_group->key.offset;
6784 btrfs_put_block_group(block_group);
6785 }
6786}
6787
Zheng Yan1a40e232008-09-26 10:09:34 -04006788int btrfs_free_block_groups(struct btrfs_fs_info *info)
6789{
6790 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04006791 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04006792 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04006793 struct rb_node *n;
6794
Yan Zheng11833d62009-09-11 16:11:19 -04006795 down_write(&info->extent_commit_sem);
6796 while (!list_empty(&info->caching_block_groups)) {
6797 caching_ctl = list_entry(info->caching_block_groups.next,
6798 struct btrfs_caching_control, list);
6799 list_del(&caching_ctl->list);
6800 put_caching_control(caching_ctl);
6801 }
6802 up_write(&info->extent_commit_sem);
6803
Zheng Yan1a40e232008-09-26 10:09:34 -04006804 spin_lock(&info->block_group_cache_lock);
6805 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6806 block_group = rb_entry(n, struct btrfs_block_group_cache,
6807 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04006808 rb_erase(&block_group->cache_node,
6809 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04006810 spin_unlock(&info->block_group_cache_lock);
6811
Josef Bacik80eb2342008-10-29 14:49:05 -04006812 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006813 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006814 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006815
Josef Bacik817d52f2009-07-13 21:29:25 -04006816 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04006817 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04006818
Josef Bacik3c148742011-02-02 15:53:47 +00006819 /*
6820 * We haven't cached this block group, which means we could
6821 * possibly have excluded extents on this block group.
6822 */
6823 if (block_group->cached == BTRFS_CACHE_NO)
6824 free_excluded_extents(info->extent_root, block_group);
6825
Josef Bacik817d52f2009-07-13 21:29:25 -04006826 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00006827 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04006828
6829 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006830 }
6831 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04006832
6833 /* now that all the block groups are freed, go through and
6834 * free all the space_info structs. This is only called during
6835 * the final stages of unmount, and so we know nobody is
6836 * using them. We call synchronize_rcu() once before we start,
6837 * just to be on the safe side.
6838 */
6839 synchronize_rcu();
6840
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006841 release_global_block_rsv(info);
6842
Chris Mason4184ea72009-03-10 12:39:20 -04006843 while(!list_empty(&info->space_info)) {
6844 space_info = list_entry(info->space_info.next,
6845 struct btrfs_space_info,
6846 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006847 if (space_info->bytes_pinned > 0 ||
6848 space_info->bytes_reserved > 0) {
6849 WARN_ON(1);
6850 dump_space_info(space_info, 0, 0);
6851 }
Chris Mason4184ea72009-03-10 12:39:20 -04006852 list_del(&space_info->list);
6853 kfree(space_info);
6854 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006855 return 0;
6856}
6857
Yan, Zhengb742bb82010-05-16 10:46:24 -04006858static void __link_block_group(struct btrfs_space_info *space_info,
6859 struct btrfs_block_group_cache *cache)
6860{
6861 int index = get_block_group_index(cache);
6862
6863 down_write(&space_info->groups_sem);
6864 list_add_tail(&cache->list, &space_info->block_groups[index]);
6865 up_write(&space_info->groups_sem);
6866}
6867
Chris Mason9078a3e2007-04-26 16:46:15 -04006868int btrfs_read_block_groups(struct btrfs_root *root)
6869{
6870 struct btrfs_path *path;
6871 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04006872 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04006873 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04006874 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04006875 struct btrfs_key key;
6876 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04006877 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04006878 int need_clear = 0;
6879 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04006880
Chris Masonbe744172007-05-06 10:15:01 -04006881 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04006882 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006883 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04006884 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04006885 path = btrfs_alloc_path();
6886 if (!path)
6887 return -ENOMEM;
6888
Josef Bacik0af3d002010-06-21 14:48:16 -04006889 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
6890 if (cache_gen != 0 &&
6891 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
6892 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04006893 if (btrfs_test_opt(root, CLEAR_CACHE))
6894 need_clear = 1;
Josef Bacik8216ef82010-10-28 16:55:47 -04006895 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
6896 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
Josef Bacik0af3d002010-06-21 14:48:16 -04006897
Chris Masond3977122009-01-05 21:25:51 -05006898 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006899 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006900 if (ret > 0)
6901 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006902 if (ret != 0)
6903 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04006904 leaf = path->nodes[0];
6905 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04006906 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04006907 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04006908 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006909 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04006910 }
Chris Mason3e1ad542007-05-07 20:03:49 -04006911
Yan Zhengd2fb3432008-12-11 16:30:39 -05006912 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006913 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04006914 spin_lock_init(&cache->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04006915 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006916 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006917 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04006918
Josef Bacik0af3d002010-06-21 14:48:16 -04006919 if (need_clear)
6920 cache->disk_cache_state = BTRFS_DC_CLEAR;
6921
Josef Bacik96303082009-07-13 21:29:25 -04006922 /*
6923 * we only want to have 32k of ram per block group for keeping
6924 * track of free space, and if we pass 1/2 of that we want to
6925 * start converting things over to using bitmaps
6926 */
6927 cache->extents_thresh = ((1024 * 32) / 2) /
6928 sizeof(struct btrfs_free_space);
6929
Chris Mason5f39d392007-10-15 16:14:19 -04006930 read_extent_buffer(leaf, &cache->item,
6931 btrfs_item_ptr_offset(leaf, path->slots[0]),
6932 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04006933 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04006934
Chris Mason9078a3e2007-04-26 16:46:15 -04006935 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02006936 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006937 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04006938 cache->sectorsize = root->sectorsize;
6939
Josef Bacik817d52f2009-07-13 21:29:25 -04006940 /*
Josef Bacik3c148742011-02-02 15:53:47 +00006941 * We need to exclude the super stripes now so that the space
6942 * info has super bytes accounted for, otherwise we'll think
6943 * we have more space than we actually do.
6944 */
6945 exclude_super_stripes(root, cache);
6946
6947 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04006948 * check for two cases, either we are full, and therefore
6949 * don't need to bother with the caching work since we won't
6950 * find any space, or we are empty, and we can just add all
6951 * the space in and be done with it. This saves us _alot_ of
6952 * time, particularly in the full case.
6953 */
6954 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04006955 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04006956 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04006957 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006958 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04006959 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04006960 cache->cached = BTRFS_CACHE_FINISHED;
6961 add_new_free_space(cache, root->fs_info,
6962 found_key.objectid,
6963 found_key.objectid +
6964 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04006965 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006966 }
Chris Mason96b51792007-10-15 16:15:19 -04006967
Chris Mason6324fbf2008-03-24 15:01:59 -04006968 ret = update_space_info(info, cache->flags, found_key.offset,
6969 btrfs_block_group_used(&cache->item),
6970 &space_info);
6971 BUG_ON(ret);
6972 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04006973 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006974 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04006975 spin_unlock(&cache->space_info->lock);
6976
Yan, Zhengb742bb82010-05-16 10:46:24 -04006977 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04006978
Josef Bacik0f9dd462008-09-23 13:14:11 -04006979 ret = btrfs_add_block_group_cache(root->fs_info, cache);
6980 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04006981
6982 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05006983 if (btrfs_chunk_readonly(root, cache->key.objectid))
Yan, Zhengf0486c62010-05-16 10:46:25 -04006984 set_block_group_ro(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04006985 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006986
6987 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
6988 if (!(get_alloc_profile(root, space_info->flags) &
6989 (BTRFS_BLOCK_GROUP_RAID10 |
6990 BTRFS_BLOCK_GROUP_RAID1 |
6991 BTRFS_BLOCK_GROUP_DUP)))
6992 continue;
6993 /*
6994 * avoid allocating from un-mirrored block group if there are
6995 * mirrored block groups.
6996 */
6997 list_for_each_entry(cache, &space_info->block_groups[3], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006998 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006999 list_for_each_entry(cache, &space_info->block_groups[4], list)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007000 set_block_group_ro(cache);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007001 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007002
7003 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007004 ret = 0;
7005error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007006 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007007 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007008}
Chris Mason6324fbf2008-03-24 15:01:59 -04007009
7010int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7011 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007012 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007013 u64 size)
7014{
7015 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007016 struct btrfs_root *extent_root;
7017 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007018
7019 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007020
Chris Mason12fcfd22009-03-24 10:24:20 -04007021 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007022
Chris Mason8f18cf12008-04-25 16:53:30 -04007023 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007024 if (!cache)
7025 return -ENOMEM;
7026
Chris Masone17cade2008-04-15 15:41:47 -04007027 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007028 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007029 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007030 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007031 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04007032
7033 /*
7034 * we only want to have 32k of ram per block group for keeping track
7035 * of free space, and if we pass 1/2 of that we want to start
7036 * converting things over to using bitmaps
7037 */
7038 cache->extents_thresh = ((1024 * 32) / 2) /
7039 sizeof(struct btrfs_free_space);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007040 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007041 spin_lock_init(&cache->lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04007042 spin_lock_init(&cache->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007043 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007044 INIT_LIST_HEAD(&cache->cluster_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007045
Chris Mason6324fbf2008-03-24 15:01:59 -04007046 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007047 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7048 cache->flags = type;
7049 btrfs_set_block_group_flags(&cache->item, type);
7050
Yan Zheng11833d62009-09-11 16:11:19 -04007051 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007052 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007053 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007054
Josef Bacik817d52f2009-07-13 21:29:25 -04007055 add_new_free_space(cache, root->fs_info, chunk_offset,
7056 chunk_offset + size);
7057
Yan Zheng11833d62009-09-11 16:11:19 -04007058 free_excluded_extents(root, cache);
7059
Chris Mason6324fbf2008-03-24 15:01:59 -04007060 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7061 &cache->space_info);
7062 BUG_ON(ret);
Josef Bacik1b2da372009-09-11 16:11:20 -04007063
7064 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007065 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007066 spin_unlock(&cache->space_info->lock);
7067
Yan, Zhengb742bb82010-05-16 10:46:24 -04007068 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007069
Josef Bacik0f9dd462008-09-23 13:14:11 -04007070 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7071 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04007072
Chris Mason6324fbf2008-03-24 15:01:59 -04007073 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7074 sizeof(cache->item));
7075 BUG_ON(ret);
7076
Chris Masond18a2c42008-04-04 15:40:00 -04007077 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007078
Chris Mason6324fbf2008-03-24 15:01:59 -04007079 return 0;
7080}
Zheng Yan1a40e232008-09-26 10:09:34 -04007081
7082int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7083 struct btrfs_root *root, u64 group_start)
7084{
7085 struct btrfs_path *path;
7086 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007087 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04007088 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04007089 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04007090 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04007091 int ret;
Josef Bacik89a55892010-10-14 14:52:27 -04007092 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04007093
Zheng Yan1a40e232008-09-26 10:09:34 -04007094 root = root->fs_info->extent_root;
7095
7096 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7097 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007098 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007099
liubo9f7c43c2011-03-07 02:13:33 +00007100 /*
7101 * Free the reserved super bytes from this block group before
7102 * remove it.
7103 */
7104 free_excluded_extents(root, block_group);
7105
Zheng Yan1a40e232008-09-26 10:09:34 -04007106 memcpy(&key, &block_group->key, sizeof(key));
Josef Bacik89a55892010-10-14 14:52:27 -04007107 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7108 BTRFS_BLOCK_GROUP_RAID1 |
7109 BTRFS_BLOCK_GROUP_RAID10))
7110 factor = 2;
7111 else
7112 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007113
Chris Mason44fb5512009-06-04 15:34:51 -04007114 /* make sure this block group isn't part of an allocation cluster */
7115 cluster = &root->fs_info->data_alloc_cluster;
7116 spin_lock(&cluster->refill_lock);
7117 btrfs_return_cluster_to_free_space(block_group, cluster);
7118 spin_unlock(&cluster->refill_lock);
7119
7120 /*
7121 * make sure this block group isn't part of a metadata
7122 * allocation cluster
7123 */
7124 cluster = &root->fs_info->meta_alloc_cluster;
7125 spin_lock(&cluster->refill_lock);
7126 btrfs_return_cluster_to_free_space(block_group, cluster);
7127 spin_unlock(&cluster->refill_lock);
7128
Zheng Yan1a40e232008-09-26 10:09:34 -04007129 path = btrfs_alloc_path();
7130 BUG_ON(!path);
7131
Josef Bacik0af3d002010-06-21 14:48:16 -04007132 inode = lookup_free_space_inode(root, block_group, path);
7133 if (!IS_ERR(inode)) {
7134 btrfs_orphan_add(trans, inode);
7135 clear_nlink(inode);
7136 /* One for the block groups ref */
7137 spin_lock(&block_group->lock);
7138 if (block_group->iref) {
7139 block_group->iref = 0;
7140 block_group->inode = NULL;
7141 spin_unlock(&block_group->lock);
7142 iput(inode);
7143 } else {
7144 spin_unlock(&block_group->lock);
7145 }
7146 /* One for our lookup ref */
7147 iput(inode);
7148 }
7149
7150 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
7151 key.offset = block_group->key.objectid;
7152 key.type = 0;
7153
7154 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
7155 if (ret < 0)
7156 goto out;
7157 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02007158 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007159 if (ret == 0) {
7160 ret = btrfs_del_item(trans, tree_root, path);
7161 if (ret)
7162 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02007163 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007164 }
7165
Yan Zheng3dfdb932009-01-21 10:49:16 -05007166 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007167 rb_erase(&block_group->cache_node,
7168 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05007169 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007170
Josef Bacik80eb2342008-10-29 14:49:05 -04007171 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007172 /*
7173 * we must use list_del_init so people can check to see if they
7174 * are still on the list after taking the semaphore
7175 */
7176 list_del_init(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007177 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007178
Josef Bacik817d52f2009-07-13 21:29:25 -04007179 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007180 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007181
7182 btrfs_remove_free_space_cache(block_group);
7183
Yan Zhengc146afa2008-11-12 14:34:12 -05007184 spin_lock(&block_group->space_info->lock);
7185 block_group->space_info->total_bytes -= block_group->key.offset;
7186 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04007187 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05007188 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04007189
Josef Bacik0af3d002010-06-21 14:48:16 -04007190 memcpy(&key, &block_group->key, sizeof(key));
7191
Chris Mason283bb192009-07-24 16:30:55 -04007192 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05007193
Chris Masonfa9c0d792009-04-03 09:47:43 -04007194 btrfs_put_block_group(block_group);
7195 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04007196
7197 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7198 if (ret > 0)
7199 ret = -EIO;
7200 if (ret < 0)
7201 goto out;
7202
7203 ret = btrfs_del_item(trans, root, path);
7204out:
7205 btrfs_free_path(path);
7206 return ret;
7207}
liuboacce9522011-01-06 19:30:25 +08007208
liuboc59021f2011-03-07 02:13:14 +00007209int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
7210{
7211 struct btrfs_space_info *space_info;
7212 int ret;
7213
7214 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0,
7215 &space_info);
7216 if (ret)
7217 return ret;
7218
7219 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0,
7220 &space_info);
7221 if (ret)
7222 return ret;
7223
7224 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0,
7225 &space_info);
7226 if (ret)
7227 return ret;
7228
7229 return ret;
7230}
7231
liuboacce9522011-01-06 19:30:25 +08007232int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
7233{
7234 return unpin_extent_range(root, start, end);
7235}
7236
7237int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00007238 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08007239{
Li Dongyang5378e602011-03-24 10:24:27 +00007240 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08007241}
Li Dongyangf7039b12011-03-24 10:24:28 +00007242
7243int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
7244{
7245 struct btrfs_fs_info *fs_info = root->fs_info;
7246 struct btrfs_block_group_cache *cache = NULL;
7247 u64 group_trimmed;
7248 u64 start;
7249 u64 end;
7250 u64 trimmed = 0;
7251 int ret = 0;
7252
7253 cache = btrfs_lookup_block_group(fs_info, range->start);
7254
7255 while (cache) {
7256 if (cache->key.objectid >= (range->start + range->len)) {
7257 btrfs_put_block_group(cache);
7258 break;
7259 }
7260
7261 start = max(range->start, cache->key.objectid);
7262 end = min(range->start + range->len,
7263 cache->key.objectid + cache->key.offset);
7264
7265 if (end - start >= range->minlen) {
7266 if (!block_group_cache_done(cache)) {
7267 ret = cache_block_group(cache, NULL, root, 0);
7268 if (!ret)
7269 wait_block_group_cache_done(cache);
7270 }
7271 ret = btrfs_trim_block_group(cache,
7272 &group_trimmed,
7273 start,
7274 end,
7275 range->minlen);
7276
7277 trimmed += group_trimmed;
7278 if (ret) {
7279 btrfs_put_block_group(cache);
7280 break;
7281 }
7282 }
7283
7284 cache = next_block_group(fs_info->tree_root, cache);
7285 }
7286
7287 range->len = trimmed;
7288 return ret;
7289}